@@ -66,8 +66,11 @@ type StandardECCrypto struct {
6666type keylist map [string ]any
6767
6868type StandardCrypto struct {
69- // Lists of keys first sorted by algorithm
70- keys map [string ]keylist
69+ // Lists of keysByAlg first sorted by algorithm
70+ keysByAlg map [string ]keylist
71+
72+ // Lists all keys by identifier.
73+ keysByID keylist
7174}
7275
7376// NewStandardCrypto Create a new instance of standard crypto
@@ -83,20 +86,26 @@ func NewStandardCrypto(cfg StandardConfig) (*StandardCrypto, error) {
8386}
8487
8588func loadKeys (ks []KeyPairInfo ) (* StandardCrypto , error ) {
86- keys := make (map [string ]keylist )
89+ keysByAlg := make (map [string ]keylist )
90+ keysByID := make (keylist )
8791 for _ , k := range ks {
8892 slog .Info ("crypto cfg loading" , "id" , k .KID , "alg" , k .Algorithm )
89- if _ , ok := keys [k .Algorithm ]; ! ok {
90- keys [k .Algorithm ] = make (map [string ]any )
93+ if _ , ok := keysByID [k .KID ]; ok {
94+ return nil , fmt .Errorf ("duplicate key identifier [%s]" , k .KID )
95+ }
96+ if _ , ok := keysByAlg [k .Algorithm ]; ! ok {
97+ keysByAlg [k .Algorithm ] = make (map [string ]any )
9198 }
9299 loadedKey , err := loadKey (k )
93100 if err != nil {
94101 return nil , err
95102 }
96- keys [k.Algorithm ][k.KID ] = loadedKey
103+ keysByAlg [k.Algorithm ][k.KID ] = loadedKey
104+ keysByID [k .KID ] = loadedKey
97105 }
98106 return & StandardCrypto {
99- keys : keys ,
107+ keysByAlg : keysByAlg ,
108+ keysByID : keysByID ,
100109 }, nil
101110}
102111
@@ -139,13 +148,14 @@ func loadKey(k KeyPairInfo) (any, error) {
139148}
140149
141150func loadDeprecatedKeys (rsaKeys map [string ]StandardKeyInfo , ecKeys map [string ]StandardKeyInfo ) (* StandardCrypto , error ) {
142- keys := make (map [string ]keylist )
151+ keysByAlg := make (map [string ]keylist )
152+ keysByID := make (keylist )
143153
144154 if len (ecKeys ) > 0 {
145- keys [AlgorithmECP256R1 ] = make (map [string ]any )
155+ keysByAlg [AlgorithmECP256R1 ] = make (map [string ]any )
146156 }
147157 if len (rsaKeys ) > 0 {
148- keys [AlgorithmRSA2048 ] = make (map [string ]any )
158+ keysByAlg [AlgorithmRSA2048 ] = make (map [string ]any )
149159 }
150160
151161 for id , kasInfo := range rsaKeys {
@@ -169,7 +179,7 @@ func loadDeprecatedKeys(rsaKeys map[string]StandardKeyInfo, ecKeys map[string]St
169179 return nil , fmt .Errorf ("ocrypto.NewAsymEncryption failed: %w" , err )
170180 }
171181
172- keys [ AlgorithmRSA2048 ][ id ] = StandardRSACrypto {
182+ k : = StandardRSACrypto {
173183 KeyPairInfo : KeyPairInfo {
174184 Algorithm : AlgorithmRSA2048 ,
175185 KID : id ,
@@ -179,6 +189,8 @@ func loadDeprecatedKeys(rsaKeys map[string]StandardKeyInfo, ecKeys map[string]St
179189 asymDecryption : asymDecryption ,
180190 asymEncryption : asymEncryption ,
181191 }
192+ keysByAlg [AlgorithmRSA2048 ][id ] = k
193+ keysByID [id ] = k
182194 }
183195 for id , kasInfo := range ecKeys {
184196 slog .Info ("cfg.ECKeys" , "id" , id , "kasInfo" , kasInfo )
@@ -192,7 +204,7 @@ func loadDeprecatedKeys(rsaKeys map[string]StandardKeyInfo, ecKeys map[string]St
192204 if err != nil {
193205 return nil , fmt .Errorf ("failed to EC certificate file: %w" , err )
194206 }
195- keys [ AlgorithmECP256R1 ][ id ] = StandardECCrypto {
207+ k : = StandardECCrypto {
196208 KeyPairInfo : KeyPairInfo {
197209 Algorithm : AlgorithmRSA2048 ,
198210 KID : id ,
@@ -202,15 +214,18 @@ func loadDeprecatedKeys(rsaKeys map[string]StandardKeyInfo, ecKeys map[string]St
202214 ecPrivateKeyPem : string (privatePemData ),
203215 ecCertificatePEM : string (ecCertificatePEM ),
204216 }
217+ keysByAlg [AlgorithmECP256R1 ][id ] = k
218+ keysByID [id ] = k
205219 }
206220
207221 return & StandardCrypto {
208- keys : keys ,
222+ keysByAlg : keysByAlg ,
223+ keysByID : keysByID ,
209224 }, nil
210225}
211226
212227func (s StandardCrypto ) FindKID (alg string ) string {
213- if ks , ok := s .keys [alg ]; ok && len (ks ) > 0 {
228+ if ks , ok := s .keysByAlg [alg ]; ok && len (ks ) > 0 {
214229 for kid := range ks {
215230 return kid
216231 }
@@ -219,17 +234,13 @@ func (s StandardCrypto) FindKID(alg string) string {
219234}
220235
221236func (s StandardCrypto ) RSAPublicKey (kid string ) (string , error ) {
222- rsaKeys , ok := s .keys [AlgorithmRSA2048 ]
223- if ! ok || len (rsaKeys ) == 0 {
224- return "" , ErrCertNotFound
225- }
226- k , ok := rsaKeys [kid ]
237+ k , ok := s .keysByID [kid ]
227238 if ! ok {
228- return "" , ErrCertNotFound
239+ return "" , fmt . Errorf ( "no rsa key with id [%s]: %w" , kid , ErrCertNotFound )
229240 }
230241 rsa , ok := k .(StandardRSACrypto )
231242 if ! ok {
232- return "" , ErrCertNotFound
243+ return "" , fmt . Errorf ( "key with id [%s] is not an RSA key: %w" , kid , ErrCertNotFound )
233244 }
234245
235246 pem , err := rsa .asymEncryption .PublicKeyInPemFormat ()
@@ -241,27 +252,19 @@ func (s StandardCrypto) RSAPublicKey(kid string) (string, error) {
241252}
242253
243254func (s StandardCrypto ) ECCertificate (kid string ) (string , error ) {
244- ecKeys , ok := s .keys [AlgorithmECP256R1 ]
245- if ! ok || len (ecKeys ) == 0 {
246- return "" , ErrCertNotFound
247- }
248- k , ok := ecKeys [kid ]
255+ k , ok := s .keysByID [kid ]
249256 if ! ok {
250- return "" , ErrCertNotFound
257+ return "" , fmt . Errorf ( "no ec key with id [%s]: %w" , kid , ErrCertNotFound )
251258 }
252259 ec , ok := k .(StandardECCrypto )
253260 if ! ok {
254- return "" , ErrCertNotFound
261+ return "" , fmt . Errorf ( "key with id [%s] is not an EC key: %w" , kid , ErrCertNotFound )
255262 }
256263 return ec .ecCertificatePEM , nil
257264}
258265
259266func (s StandardCrypto ) ECPublicKey (kid string ) (string , error ) {
260- ecKeys , ok := s .keys [AlgorithmECP256R1 ]
261- if ! ok || len (ecKeys ) == 0 {
262- return "" , ErrCertNotFound
263- }
264- k , ok := ecKeys [kid ]
267+ k , ok := s .keysByID [kid ]
265268 if ! ok {
266269 return "" , ErrCertNotFound
267270 }
@@ -293,11 +296,7 @@ func (s StandardCrypto) ECPublicKey(kid string) (string, error) {
293296}
294297
295298func (s StandardCrypto ) RSADecrypt (_ crypto.Hash , kid string , _ string , ciphertext []byte ) ([]byte , error ) {
296- rsaKeys , ok := s .keys [AlgorithmRSA2048 ]
297- if ! ok || len (rsaKeys ) == 0 {
298- return nil , ErrCertNotFound
299- }
300- k , ok := rsaKeys [kid ]
299+ k , ok := s .keysByID [kid ]
301300 if ! ok {
302301 return nil , ErrCertNotFound
303302 }
@@ -315,11 +314,10 @@ func (s StandardCrypto) RSADecrypt(_ crypto.Hash, kid string, _ string, cipherte
315314}
316315
317316func (s StandardCrypto ) RSAPublicKeyAsJSON (kid string ) (string , error ) {
318- rsaKeys , ok := s .keys [ AlgorithmRSA2048 ]
319- if ! ok || len ( rsaKeys ) == 0 {
317+ k , ok := s .keysByID [ kid ]
318+ if ! ok {
320319 return "" , ErrCertNotFound
321320 }
322- k , ok := rsaKeys [kid ]
323321 if ! ok {
324322 return "" , ErrCertNotFound
325323 }
@@ -357,11 +355,7 @@ func (s StandardCrypto) GenerateNanoTDFSymmetricKey(kasKID string, ephemeralPubl
357355 }
358356 ephemeralECDSAPublicKeyPEM := pem .EncodeToMemory (pemBlock )
359357
360- ecKeys , ok := s .keys [AlgorithmECP256R1 ]
361- if ! ok || len (ecKeys ) == 0 {
362- return nil , ErrNoKeys
363- }
364- k , ok := ecKeys [kasKID ]
358+ k , ok := s .keysByID [kasKID ]
365359 if ! ok {
366360 return nil , ErrKeyPairInfoNotFound
367361 }
0 commit comments