| 1 | // Copyright 2017 Google Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package fakekms provides a fake implementation of registry.KMSClient. |
| 16 | // |
| 17 | // Normally, a 'keyURI' identifies a key that is stored remotely by the KMS, |
| 18 | // and every operation is executed remotely using a RPC call to the KMS, since |
| 19 | // the key should not be sent to the client. |
| 20 | // In this fake implementation we want to avoid these RPC calls. We achieve this |
| 21 | // by encoding the key in the 'keyURI'. So the client simply needs to decode |
| 22 | // the key and generate an AEAD out of it. This is of course insecure and should |
| 23 | // only be used in testing. |
| 24 | package fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/ |
| 25 | |
| 26 | import ( |
| 27 | "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/" |
| 28 | "contextscip-go gomod github.com/golang/go/src go1.24.0 context/" |
| 29 | "encoding/base64scip-go gomod github.com/golang/go/src go1.24.0 `encoding/base64`/" |
| 30 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 31 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 32 | |
| 33 | "github.com/tink-crypto/tink-go/v2/aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/" |
| 34 | "github.com/tink-crypto/tink-go/v2/core/registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/" |
| 35 | "github.com/tink-crypto/tink-go/v2/keysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/" |
| 36 | "github.com/tink-crypto/tink-go/v2/testkeysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testkeyset`/" |
| 37 | "github.com/tink-crypto/tink-go/v2/tinkscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/" |
| 38 | ) |
| 39 | |
| 40 | const fakePrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakePrefix. = "fake-kms://" |
| 41 | |
| 42 | var _ registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/.KMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient# = (*fakeClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#)(nil) |
| 43 | |
| 44 | type fakeClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient# struct { |
| 45 | uriPrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#uriPrefix. string |
| 46 | } |
| 47 | |
| 48 | type fakeAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext# struct { |
| 49 | aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#aead. tinkscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/.AEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD# |
| 50 | } |
| 51 | |
| 52 | // EncryptWithContext implements the [tink.AEADWithContext] interface for encryption. |
| 53 | // The call fails if the context is canceled. |
| 54 | func (a *fakeAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#) EncryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#EncryptWithContext().(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, plaintext, associatedData []byte) ([]byte, error) { |
| 55 | select { |
| 56 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.24.0 context/Context#Done().(): |
| 57 | return nil, ctx.Errscip-go gomod github.com/golang/go/src go1.24.0 context/Context#Err().() |
| 58 | default: |
| 59 | return a.aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#aead..Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Encrypt.(plaintext, associatedData) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // DecryptWithContext implements the [tink.AEADWithContext] interface for decryption. |
| 64 | // The call fails if the context is canceled. |
| 65 | func (a *fakeAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#) DecryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#DecryptWithContext().(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, ciphertext, associatedData []byte) ([]byte, error) { |
| 66 | select { |
| 67 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.24.0 context/Context#Done().(): |
| 68 | return nil, ctx.Errscip-go gomod github.com/golang/go/src go1.24.0 context/Context#Err().() |
| 69 | default: |
| 70 | return a.aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#aead..Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Decrypt.(ciphertext, associatedData) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // NewClient returns a fake KMS client which will handle keys with uriPrefix prefix. |
| 75 | // keyURI must have the following format: 'fake-kms://<base64 encoded aead keyset>'. |
| 76 | func NewClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewClient().(uriPrefix string) (registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/.KMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#, error) { |
| 77 | if !stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasPrefix().(stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.24.0 strings/ToLower().(uriPrefix), fakePrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakePrefix.) { |
| 78 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("uriPrefix must start with %s, but got %s", fakePrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakePrefix., uriPrefix) |
| 79 | } |
| 80 | return &fakeClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#{ |
| 81 | uriPrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#uriPrefix.: uriPrefix, |
| 82 | }, nil |
| 83 | } |
| 84 | |
| 85 | // Supported returns true if this client does support keyURI. |
| 86 | func (c *fakeClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#) Supportedscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#Supported().(keyURI string) bool { |
| 87 | return stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasPrefix().(keyURI, c.uriPrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#uriPrefix.) |
| 88 | } |
| 89 | |
| 90 | // GetAEAD returns an AEAD by keyURI. |
| 91 | func (c *fakeClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#) GetAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#GetAEAD().(keyURI string) (tinkscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/.AEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#, error) { |
| 92 | if !c.Supportedscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#Supported().(keyURI) { |
| 93 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("keyURI must start with prefix %s, but got %s", c.uriPrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeClient#uriPrefix., keyURI) |
| 94 | } |
| 95 | return NewAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEAD().(keyURI) |
| 96 | } |
| 97 | |
| 98 | // NewAEAD returns a new [tink.AEAD] for the given keyURI. |
| 99 | func NewAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEAD().(keyURI string) (tinkscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/.AEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#, error) { |
| 100 | encodeKeyset := stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/TrimPrefix().(keyURI, fakePrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakePrefix.) |
| 101 | keysetData, err := base64scip-go gomod github.com/golang/go/src go1.24.0 `encoding/base64`/.RawURLEncodingscip-go gomod github.com/golang/go/src go1.24.0 `encoding/base64`/RawURLEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/base64`/Encoding#DecodeString().(encodeKeyset) |
| 102 | if err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | reader := keysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/.NewBinaryReaderscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/NewBinaryReader().(bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.24.0 bytes/NewReader().(keysetData)) |
| 106 | handle, err := testkeysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testkeyset`/.Readscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testkeyset`/Read().(reader) |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | return aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.Newscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/New().(handle) |
| 111 | } |
| 112 | |
| 113 | // NewAEADWithContext returns a new [tink.AeadWithContext] for the given keyURI. |
| 114 | // |
| 115 | // The returned AEADWithContext will fail if the context is canceled. |
| 116 | func NewAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEADWithContext().(keyURI string) (tinkscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/.AEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEADWithContext#, error) { |
| 117 | aead, err := NewAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEAD().(keyURI) |
| 118 | if err != nil { |
| 119 | return nil, err |
| 120 | } |
| 121 | return &fakeAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#{aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakeAEADWithContext#aead.: aead}, nil |
| 122 | } |
| 123 | |
| 124 | // NewKeyURI returns a new, random fake KMS key URI. |
| 125 | func NewKeyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewKeyURI().() (string, error) { |
| 126 | handle, err := keysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/.NewHandlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/NewHandle().(aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128GCMKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128GCMKeyTemplate().()) |
| 127 | if err != nil { |
| 128 | return "", err |
| 129 | } |
| 130 | buf := new(bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#) |
| 131 | writer := keysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/.NewBinaryWriterscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/NewBinaryWriter().(buf) |
| 132 | err = testkeysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testkeyset`/.Writescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testkeyset`/Write().(handle, writer) |
| 133 | if err != nil { |
| 134 | return "", err |
| 135 | } |
| 136 | return fakePrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/fakePrefix. + base64scip-go gomod github.com/golang/go/src go1.24.0 `encoding/base64`/.RawURLEncodingscip-go gomod github.com/golang/go/src go1.24.0 `encoding/base64`/RawURLEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/base64`/Encoding#EncodeToString().(buf.Bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#Bytes().()), nil |
| 137 | } |