| 1 |
package sigstore |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/rsa" |
| 8 |
"crypto/x509" |
| 9 |
"encoding/pem" |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
|
| 13 |
"github.com/secure-systems-lab/go-securesystemslib/encrypted" |
| 14 |
"github.com/sigstore/sigstore/pkg/cryptoutils" |
| 15 |
"github.com/sigstore/sigstore/pkg/signature" |
| 16 |
) |
| 17 |
|
| 18 |
// The following code was copied from github.com/sigstore. |
| 19 |
// FIXME: Eliminate that duplication. |
| 20 |
|
| 21 |
// Copyright 2021 The Sigstore Authors. |
| 22 |
|
| 23 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 24 |
// you may not use this file except in compliance with the License. |
| 25 |
// You may obtain a copy of the License at |
| 26 |
|
| 27 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 28 |
|
| 29 |
// Unless required by applicable law or agreed to in writing, software |
| 30 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 31 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 32 |
// See the License for the specific language governing permissions and |
| 33 |
// limitations under the License. |
| 34 |
|
| 35 |
const ( |
| 36 |
// from sigstore/cosign/pkg/cosign.CosignPrivateKeyPemType. |
| 37 |
cosignPrivateKeyPemType = "ENCRYPTED COSIGN PRIVATE KEY" |
| 38 |
// from sigstore/cosign/pkg/cosign.SigstorePrivateKeyPemType. |
| 39 |
sigstorePrivateKeyPemType = "ENCRYPTED SIGSTORE PRIVATE KEY" |
| 40 |
) |
| 41 |
|
| 42 |
// from sigstore/cosign/pkg/cosign.loadPrivateKey |
| 43 |
// FIXME: Do we need all of these key formats? |
| 44 |
func loadPrivateKey(key []byte, pass []byte) (signature.SignerVerifier, error) { |
| 45 |
// Decrypt first |
| 46 |
p, _ := pem.Decode(key) |
| 47 |
if p == nil { |
| 48 |
return nil, errors.New("invalid pem block") |
| 49 |
} |
| 50 |
if p.Type != sigstorePrivateKeyPemType && p.Type != cosignPrivateKeyPemType { |
| 51 |
return nil, fmt.Errorf("unsupported pem type: %s", p.Type) |
| 52 |
} |
| 53 |
|
| 54 |
x509Encoded, err := encrypted.Decrypt(p.Bytes, pass) |
| 55 |
if err != nil { |
| 56 |
return nil, fmt.Errorf("decrypt: %w", err) |
| 57 |
} |
| 58 |
|
| 59 |
pk, err := x509.ParsePKCS8PrivateKey(x509Encoded) |
| 60 |
if err != nil { |
| 61 |
return nil, fmt.Errorf("parsing private key: %w", err) |
| 62 |
} |
| 63 |
switch pk := pk.(type) { |
| 64 |
case *rsa.PrivateKey: |
| 65 |
return signature.LoadRSAPKCS1v15SignerVerifier(pk, crypto.SHA256) |
| 66 |
case *ecdsa.PrivateKey: |
| 67 |
return signature.LoadECDSASignerVerifier(pk, crypto.SHA256) |
| 68 |
case ed25519.PrivateKey: |
| 69 |
return signature.LoadED25519SignerVerifier(pk) |
| 70 |
default: |
| 71 |
return nil, errors.New("unsupported key type") |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// simplified from sigstore/cosign/pkg/cosign.marshalKeyPair |
| 76 |
// loadPrivateKey always requires a encryption, so this always requires a passphrase. |
| 77 |
func marshalKeyPair(privateKey crypto.PrivateKey, publicKey crypto.PublicKey, password []byte) (_privateKey []byte, _publicKey []byte, err error) { |
| 78 |
x509Encoded, err := x509.MarshalPKCS8PrivateKey(privateKey) |
| 79 |
if err != nil { |
| 80 |
return nil, nil, fmt.Errorf("x509 encoding private key: %w", err) |
| 81 |
} |
| 82 |
|
| 83 |
encBytes, err := encrypted.Encrypt(x509Encoded, password) |
| 84 |
if err != nil { |
| 85 |
return nil, nil, err |
| 86 |
} |
| 87 |
|
| 88 |
// store in PEM format |
| 89 |
privBytes := pem.EncodeToMemory(&pem.Block{ |
| 90 |
Bytes: encBytes, |
| 91 |
// Use the older “COSIGN” type name; as of 2023-03-30 cosign’s main branch generates “SIGSTORE” types, |
| 92 |
// but a version of cosign that can accept them has not yet been released. |
| 93 |
Type: cosignPrivateKeyPemType, |
| 94 |
}) |
| 95 |
|
| 96 |
// Now do the public key |
| 97 |
pubBytes, err := cryptoutils.MarshalPublicKeyToPEM(publicKey) |
| 98 |
if err != nil { |
| 99 |
return nil, nil, err |
| 100 |
} |
| 101 |
|
| 102 |
return privBytes, pubBytes, nil |
| 103 |
} |