Skip to content

Commit 05a2b63

Browse files
committed
Create CryptoConfig constructors in place of dcparameters
Signed-off-by: Brandon Lum <[email protected]>
1 parent fdab4f4 commit 05a2b63

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

pkg/encryption/config/config.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,45 @@ func InitEncryption(parameters, dcparameters map[string][][]byte) *CryptoConfig
6161
},
6262
}
6363
}
64+
65+
// CombineCryptoConfigs takes a CryptoConfig list and creates a single CryptoConfig
66+
// containing the crypto configuration of all the key bundles
67+
func CombineCryptoConfigs(ccs []CryptoConfig) CryptoConfig {
68+
ecparam := map[string][][]byte{}
69+
ecdcparam := map[string][][]byte{}
70+
dcparam := map[string][][]byte{}
71+
72+
for _, cc := range ccs {
73+
if ec := cc.EncryptConfig; ec != nil {
74+
addToMap(ecparam, ec.Parameters)
75+
addToMap(ecdcparam, ec.DecryptConfig.Parameters)
76+
}
77+
78+
if dc := cc.DecryptConfig; dc != nil {
79+
addToMap(dcparam, dc.Parameters)
80+
}
81+
}
82+
83+
return CryptoConfig{
84+
EncryptConfig: &EncryptConfig{
85+
Parameters: ecparam,
86+
DecryptConfig: DecryptConfig{
87+
Parameters: ecdcparam,
88+
},
89+
},
90+
DecryptConfig: &DecryptConfig{
91+
Parameters: dcparam,
92+
},
93+
}
94+
95+
}
96+
97+
func addToMap(orig map[string][][]byte, add map[string][][]byte) {
98+
for k, v := range add {
99+
if ov, ok := orig[k]; ok {
100+
orig[k] = append(ov, v...)
101+
} else {
102+
orig[k] = v
103+
}
104+
}
105+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
// NewJweCryptoConfig returns a CryptoConfig that contains the required configuration for using
20+
// the jwe keyunwrap interface
21+
func NewJweCryptoConfig(pubKey *[]byte, privKey *[]byte, privKeyPassword *string) CryptoConfig {
22+
pubKeys := [][]byte{}
23+
privKeys := [][]byte{}
24+
privKeysPasswords := [][]byte{}
25+
26+
if pubKey != nil {
27+
pubKeys = append(pubKeys, *pubKey)
28+
}
29+
if privKey != nil {
30+
privKeys = append(privKeys, *privKey)
31+
}
32+
if privKeyPassword != nil {
33+
privKeysPasswords = append(privKeysPasswords, []byte(*privKeyPassword))
34+
}
35+
36+
dc := DecryptConfig{
37+
Parameters: map[string][][]byte{
38+
"privkeys": privKeys,
39+
"privkeys-passwords": privKeysPasswords,
40+
},
41+
}
42+
43+
ep := map[string][][]byte{
44+
"pubkeys": pubKeys,
45+
}
46+
47+
return CryptoConfig{
48+
EncryptConfig: &EncryptConfig{
49+
Parameters: ep,
50+
DecryptConfig: dc,
51+
},
52+
DecryptConfig: &dc,
53+
}
54+
}
55+
56+
// NewPkcs7CryptoConfig returns a CryptoConfig that contains the required configuration for using
57+
// the pkcs7 keyunwrap interface
58+
func NewPkcs7CryptoConfig(x509 *[]byte, privKey *[]byte, privKeyPassword *string) CryptoConfig {
59+
x509s := [][]byte{}
60+
privKeys := [][]byte{}
61+
privKeysPasswords := [][]byte{}
62+
63+
if x509 != nil {
64+
x509s = append(x509s, *x509)
65+
}
66+
if privKey != nil {
67+
privKeys = append(privKeys, *privKey)
68+
}
69+
if privKeyPassword != nil {
70+
privKeysPasswords = append(privKeysPasswords, []byte(*privKeyPassword))
71+
}
72+
73+
dc := DecryptConfig{
74+
Parameters: map[string][][]byte{
75+
"privkeys": privKeys,
76+
"privkeys-passwords": privKeysPasswords,
77+
},
78+
}
79+
80+
ep := map[string][][]byte{
81+
"x509s": x509s,
82+
}
83+
84+
return CryptoConfig{
85+
EncryptConfig: &EncryptConfig{
86+
Parameters: ep,
87+
DecryptConfig: dc,
88+
},
89+
DecryptConfig: &dc,
90+
}
91+
}

0 commit comments

Comments
 (0)