Skip to content

Commit 26be494

Browse files
committed
pkcs11: Add missing support for pkcs11 to SortDecryptionKeys
Add the missing support for pkcs11 to SortDecryptionKeys. Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
1 parent b76ab4a commit 26be494

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

config/pkcs11config/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ func getDefaultCryptoConfigOpts() (*OcicryptConfig, error) {
111111
// GetUserPkcs11Config gets the user's Pkcs11Conig either from a configuration file or if none is
112112
// found the default ones are returned
113113
func GetUserPkcs11Config() (*pkcs11.Pkcs11Config, error) {
114-
fmt.Print("Note: pkcs11 support is currently experimental\n")
115114
ic, err := getConfiguration()
116115
if err != nil {
117116
return &pkcs11.Pkcs11Config{}, err

utils/utils.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import (
2525
"fmt"
2626
"strings"
2727

28+
"github.com/containers/ocicrypt/config/pkcs11config"
2829
"github.com/containers/ocicrypt/crypto/pkcs11"
2930
"github.com/go-jose/go-jose/v4"
31+
"go.yaml.in/yaml/v3"
3032
"golang.org/x/crypto/openpgp"
3133
)
3234

@@ -216,22 +218,37 @@ func SortDecryptionKeys(b64ItemList string) (map[string][][]byte, error) {
216218
return nil, errors.New("Could not base64 decode a passed decryption key password")
217219
}
218220
}
221+
219222
var key string
220-
isPrivKey, err := IsPrivateKey(keyData, password)
221-
if IsPasswordError(err) {
222-
return nil, err
223-
}
224-
if isPrivKey {
225-
key = "privkeys"
226-
if _, ok := dcparameters["privkeys-passwords"]; !ok {
227-
dcparameters["privkeys-passwords"] = [][]byte{password}
228-
} else {
229-
dcparameters["privkeys-passwords"] = append(dcparameters["privkeys-passwords"], password)
223+
if IsPkcs11PrivateKey(keyData) {
224+
p11conf, err := pkcs11config.GetUserPkcs11Config()
225+
if err != nil {
226+
return nil, err
227+
}
228+
p11ConfYaml, err := yaml.Marshal(p11conf)
229+
if err != nil {
230+
return nil, fmt.Errorf("Could not marshal Pkcs11Config to Yaml: %w", err)
231+
}
232+
dcparameters["pkcs11-config"] = [][]byte{p11ConfYaml}
233+
234+
key = "pkcs11-yamls"
235+
} else {
236+
isPrivKey, err := IsPrivateKey(keyData, password)
237+
if IsPasswordError(err) {
238+
return nil, err
239+
}
240+
if isPrivKey {
241+
key = "privkeys"
242+
if _, ok := dcparameters["privkeys-passwords"]; !ok {
243+
dcparameters["privkeys-passwords"] = [][]byte{password}
244+
} else {
245+
dcparameters["privkeys-passwords"] = append(dcparameters["privkeys-passwords"], password)
246+
}
247+
} else if IsCertificate(keyData) {
248+
key = "x509s"
249+
} else if IsGPGPrivateKeyRing(keyData) {
250+
key = "gpg-privatekeys"
230251
}
231-
} else if IsCertificate(keyData) {
232-
key = "x509s"
233-
} else if IsGPGPrivateKeyRing(keyData) {
234-
key = "gpg-privatekeys"
235252
}
236253
if key != "" {
237254
values := dcparameters[key]

0 commit comments

Comments
 (0)