Skip to content

Commit

Permalink
Create CryptoConfig constructors in place of dcparameters
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Lum <lumjjb@gmail.com>
  • Loading branch information
lumjjb committed Jul 25, 2019
1 parent fdab4f4 commit 05a2b63
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/encryption/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,45 @@ func InitEncryption(parameters, dcparameters map[string][][]byte) *CryptoConfig
},
}
}

// CombineCryptoConfigs takes a CryptoConfig list and creates a single CryptoConfig
// containing the crypto configuration of all the key bundles
func CombineCryptoConfigs(ccs []CryptoConfig) CryptoConfig {
ecparam := map[string][][]byte{}
ecdcparam := map[string][][]byte{}
dcparam := map[string][][]byte{}

for _, cc := range ccs {
if ec := cc.EncryptConfig; ec != nil {
addToMap(ecparam, ec.Parameters)
addToMap(ecdcparam, ec.DecryptConfig.Parameters)
}

if dc := cc.DecryptConfig; dc != nil {
addToMap(dcparam, dc.Parameters)
}
}

return CryptoConfig{
EncryptConfig: &EncryptConfig{
Parameters: ecparam,
DecryptConfig: DecryptConfig{
Parameters: ecdcparam,
},
},
DecryptConfig: &DecryptConfig{
Parameters: dcparam,
},
}

}

func addToMap(orig map[string][][]byte, add map[string][][]byte) {
for k, v := range add {
if ov, ok := orig[k]; ok {
orig[k] = append(ov, v...)
} else {
orig[k] = v
}
}
}
91 changes: 91 additions & 0 deletions pkg/encryption/config/constructors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

// NewJweCryptoConfig returns a CryptoConfig that contains the required configuration for using
// the jwe keyunwrap interface
func NewJweCryptoConfig(pubKey *[]byte, privKey *[]byte, privKeyPassword *string) CryptoConfig {
pubKeys := [][]byte{}
privKeys := [][]byte{}
privKeysPasswords := [][]byte{}

if pubKey != nil {
pubKeys = append(pubKeys, *pubKey)
}
if privKey != nil {
privKeys = append(privKeys, *privKey)
}
if privKeyPassword != nil {
privKeysPasswords = append(privKeysPasswords, []byte(*privKeyPassword))
}

dc := DecryptConfig{
Parameters: map[string][][]byte{
"privkeys": privKeys,
"privkeys-passwords": privKeysPasswords,
},
}

ep := map[string][][]byte{
"pubkeys": pubKeys,
}

return CryptoConfig{
EncryptConfig: &EncryptConfig{
Parameters: ep,
DecryptConfig: dc,
},
DecryptConfig: &dc,
}
}

// NewPkcs7CryptoConfig returns a CryptoConfig that contains the required configuration for using
// the pkcs7 keyunwrap interface
func NewPkcs7CryptoConfig(x509 *[]byte, privKey *[]byte, privKeyPassword *string) CryptoConfig {
x509s := [][]byte{}
privKeys := [][]byte{}
privKeysPasswords := [][]byte{}

if x509 != nil {
x509s = append(x509s, *x509)
}
if privKey != nil {
privKeys = append(privKeys, *privKey)
}
if privKeyPassword != nil {
privKeysPasswords = append(privKeysPasswords, []byte(*privKeyPassword))
}

dc := DecryptConfig{
Parameters: map[string][][]byte{
"privkeys": privKeys,
"privkeys-passwords": privKeysPasswords,
},
}

ep := map[string][][]byte{
"x509s": x509s,
}

return CryptoConfig{
EncryptConfig: &EncryptConfig{
Parameters: ep,
DecryptConfig: dc,
},
DecryptConfig: &dc,
}
}

0 comments on commit 05a2b63

Please sign in to comment.