Debian Codegraph @

core/registry/registry.go

raw symbols SCIP
1 // Copyright 2019 Google LLC
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 registry provides a container that for each supported key type holds
16 // a corresponding KeyManager object, which can generate new keys or
17 // instantiate the primitive corresponding to given key.
18 //
19 // Registry is initialized at startup, and is later used to instantiate
20 // primitives for given keys or keysets. Keeping KeyManagers for all primitives
21 // in a single Registry (rather than having a separate KeyManager per
22 // primitive) enables modular construction of compound primitives from "simple"
23 // ones, e.g., AES-CTR-HMAC AEAD encryption uses IND-CPA encryption and a MAC.
24 //
25 // Note that regular users will usually not work directly with Registry, but
26 // rather via primitive factories, which in the background query the Registry
27 // for specific KeyManagers. Registry is public though, to enable
28 // configurations with custom primitives and KeyManagers.
29 package registryscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/
30
31 import (
32 "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/"
33 "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/"
34
35 "google.golang.org/protobuf/protoscip-go gomod google.golang.org/protobuf v1.36.8 `google.golang.org/protobuf/proto`/"
36 "github.com/tink-crypto/tink-go/v2/internal/internalapiscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/internalapi`/"
37 "github.com/tink-crypto/tink-go/v2/internal/syncmapscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/syncmap`/"
38 tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/ "github.com/tink-crypto/tink-go/v2/proto/tink_go_protoscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/"
39 )
40
41 var (
42 keyManagersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/keyManagers. = syncmapscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/syncmap`/.Newscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/syncmap`/New().[string, KeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#]() // typeURL -> KeyManager
43 kmsClientsMuscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClientsMu. syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#
44 kmsClientsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClients. = []KMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#{}
45 )
46
47 // RegisterKeyManager registers the given key manager.
48 // Does not allow to overwrite existing key managers.
49 func RegisterKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/RegisterKeyManager().(keyManager KeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#) error {
50 typeURL := keyManager.TypeURLscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#TypeURL.()
51 if _, existed := keyManagersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/keyManagers..LoadOrStorescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/syncmap`/Map#LoadOrStore().(typeURL, keyManager); existed {
52 return 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().("registry.RegisterKeyManager: type %s already registered", typeURL)
53 }
54 return nil
55 }
56
57 // GetKeyManager returns the key manager for the given typeURL if existed.
58 func GetKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKeyManager().(typeURL string) (KeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#, error) {
59 keyManager, existed := keyManagersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/keyManagers..Loadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/syncmap`/Map#Load().(typeURL)
60 if !existed {
61 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().("registry.GetKeyManager: unsupported key type: %s", typeURL)
62 }
63 return keyManager, nil
64 }
65
66 // NewKeyData generates a new KeyData for the given key template.
67 func NewKeyDatascip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/NewKeyData().(template *tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#) (*tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyDatascip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyData#, error) {
68 if template == nil {
69 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().("registry.NewKeyData: invalid key template")
70 }
71 keyManager, err := GetKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKeyManager().(template.TypeUrlscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#TypeUrl.)
72 if err != nil {
73 return nil, err
74 }
75 return keyManager.NewKeyDatascip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#NewKeyData.(template.Valuescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#Value.)
76 }
77
78 // NewKey generates a new key for the given key template.
79 //
80 // Deprecated: use [NewKeyData] instead.
81 func NewKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/NewKey().(template *tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#) (protoscip-go gomod google.golang.org/protobuf v1.36.8 `google.golang.org/protobuf/proto`/.Messagescip-go gomod google.golang.org/protobuf v1.36.8 `google.golang.org/protobuf/proto`/Message#, error) {
82 if template == nil {
83 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().("registry.NewKey: invalid key template")
84 }
85 keyManager, err := GetKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKeyManager().(template.TypeUrlscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#TypeUrl.)
86 if err != nil {
87 return nil, err
88 }
89 return keyManager.NewKeyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#NewKey.(template.Valuescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#Value.)
90 }
91
92 // PrimitiveFromKeyData creates a new primitive for the key given in the given KeyData.
93 // Note that the returned primitive does not add/remove the output prefix.
94 // It is the caller's responsibility to handle this correctly, based on the key's output_prefix_type.
95 func PrimitiveFromKeyDatascip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/PrimitiveFromKeyData().(keyData *tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyDatascip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyData#) (any, error) {
96 if keyData == nil {
97 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().("registry.PrimitiveFromKeyData: invalid key data")
98 }
99 return Primitivescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/Primitive().(keyData.TypeUrlscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyData#TypeUrl., keyData.Valuescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyData#Value.)
100 }
101
102 // Primitive creates a new primitive for the given serialized key using the KeyManager
103 // identified by the given typeURL.
104 // Note that the returned primitive does not add/remove the output prefix.
105 // It is the caller's responsibility to handle this correctly, based on the key's output_prefix_type.
106 func Primitivescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/Primitive().(typeURL string, serializedKey []byte) (any, error) {
107 if len(serializedKey) == 0 {
108 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().("registry.Primitive: invalid serialized key")
109 }
110 keyManager, err := GetKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKeyManager().(typeURL)
111 if err != nil {
112 return nil, err
113 }
114 return keyManager.Primitivescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KeyManager#Primitive.(serializedKey)
115 }
116
117 // RegisterKMSClient is used to register a new KMS client.
118 //
119 // This function adds an object to a global list. It should only be called on
120 // startup.
121 //
122 // In many cases, registering a KMS client is not needed. Instead, call
123 // kmsClient.GetAEAD to get a remote AEAD, and then use it to encrypt
124 // a keyset with keyset.Write, or to create an envelope AEAD using
125 // aead.NewKMSEnvelopeAEAD2.
126 func RegisterKMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/RegisterKMSClient().(kmsClient KMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#) {
127 kmsClientsMuscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClientsMu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Lock().()
128 defer kmsClientsMuscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClientsMu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Unlock().()
129 kmsClientsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClients. = append(kmsClientsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClients., kmsClient)
130 }
131
132 // GetKMSClient fetches a KMSClient by a given URI.
133 func GetKMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/GetKMSClient().(keyURI string) (KMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#, error) {
134 kmsClientsMuscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClientsMu..RLockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#RLock().()
135 defer kmsClientsMuscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClientsMu..RUnlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#RUnlock().()
136 for _, kmsClient := range kmsClientsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClients. {
137 if kmsClient.Supportedscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#Supported.(keyURI) {
138 return kmsClient, nil
139 }
140 }
141 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().("KMS client supporting %s not found", keyURI)
142 }
143
144 // ClearKMSClients removes all registered KMS clients.
145 //
146 // Should only be used in tests.
147 func ClearKMSClientsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/ClearKMSClients().() {
148 kmsClientsMuscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClientsMu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Lock().()
149 defer kmsClientsMuscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClientsMu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Unlock().()
150 kmsClientsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/kmsClients. = []KMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#{}
151 }
152
153 // UnregisterKeyManager unregisters the key manager for the given typeURL.
154 // Does nothing if the key manager is not registered.
155 //
156 // This function is intended to be used in tests only and is an internal API.
157 func UnregisterKeyManagerscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/UnregisterKeyManager().(typeURL string, _ internalapiscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/internalapi`/.Tokenscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/internalapi`/Token#) { keyManagersscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/keyManagers..Deletescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/syncmap`/Map#Delete().(typeURL) }