-
Notifications
You must be signed in to change notification settings - Fork 499
/
keys.go
224 lines (198 loc) · 5.78 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// Copyright 2021 The Sigstore 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 cosign
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
_ "crypto/sha256" // for `crypto.SHA256`
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/theupdateframework/go-tuf/encrypted"
"github.com/sigstore/cosign/pkg/oci/static"
"github.com/sigstore/sigstore/pkg/cryptoutils"
"github.com/sigstore/sigstore/pkg/signature"
)
const (
CosignPrivateKeyPemType = "ENCRYPTED COSIGN PRIVATE KEY"
// PEM-encoded PKCS #1 RSA private key
RSAPrivateKeyPemType = "RSA PRIVATE KEY"
// PEM-encoded ECDSA private key
ECPrivateKeyPemType = "EC PRIVATE KEY"
// PEM-encoded PKCS #8 RSA, ECDSA or ED25519 private key
PrivateKeyPemType = "PRIVATE KEY"
BundleKey = static.BundleAnnotationKey
)
// PassFunc is the function to be called to retrieve the signer password. If
// nil, then it assumes that no password is provided.
type PassFunc func(bool) ([]byte, error)
type Keys struct {
private crypto.PrivateKey
public crypto.PublicKey
}
type KeysBytes struct {
PrivateBytes []byte
PublicBytes []byte
password []byte
}
func GeneratePrivateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
}
func ImportKeyPair(keyPath string, pf PassFunc) (*KeysBytes, error) {
kb, err := os.ReadFile(filepath.Clean(keyPath))
if err != nil {
return nil, err
}
p, _ := pem.Decode(kb)
if p == nil {
return nil, fmt.Errorf("invalid pem block")
}
var pk crypto.Signer
switch p.Type {
case RSAPrivateKeyPemType:
rsaPk, err := x509.ParsePKCS1PrivateKey(p.Bytes)
if err != nil {
return nil, fmt.Errorf("error parsing rsa private key: %w", err)
}
if err = cryptoutils.ValidatePubKey(rsaPk.Public()); err != nil {
return nil, fmt.Errorf("error validating rsa key: %w", err)
}
pk = rsaPk
case ECPrivateKeyPemType:
ecdsaPk, err := x509.ParseECPrivateKey(p.Bytes)
if err != nil {
return nil, fmt.Errorf("error parsing ecdsa private key")
}
if err = cryptoutils.ValidatePubKey(ecdsaPk.Public()); err != nil {
return nil, fmt.Errorf("error validating ecdsa key: %w", err)
}
pk = ecdsaPk
case PrivateKeyPemType:
pkcs8Pk, err := x509.ParsePKCS8PrivateKey(p.Bytes)
if err != nil {
return nil, fmt.Errorf("error parsing pkcs #8 private key")
}
switch k := pkcs8Pk.(type) {
case *rsa.PrivateKey:
if err = cryptoutils.ValidatePubKey(k.Public()); err != nil {
return nil, fmt.Errorf("error validating rsa key: %w", err)
}
pk = k
case *ecdsa.PrivateKey:
if err = cryptoutils.ValidatePubKey(k.Public()); err != nil {
return nil, fmt.Errorf("error validating ecdsa key: %w", err)
}
pk = k
case ed25519.PrivateKey:
if err = cryptoutils.ValidatePubKey(k.Public()); err != nil {
return nil, fmt.Errorf("error validating ed25519 key: %w", err)
}
pk = k
default:
return nil, fmt.Errorf("unexpected private key")
}
default:
return nil, fmt.Errorf("unsupported private key")
}
return marshalKeyPair(Keys{pk, pk.Public()}, pf)
}
func marshalKeyPair(keypair Keys, pf PassFunc) (key *KeysBytes, err error) {
x509Encoded, err := x509.MarshalPKCS8PrivateKey(keypair.private)
if err != nil {
return nil, fmt.Errorf("x509 encoding private key: %w", err)
}
password := []byte{}
if pf != nil {
password, err = pf(true)
if err != nil {
return nil, err
}
}
encBytes, err := encrypted.Encrypt(x509Encoded, password)
if err != nil {
return nil, err
}
// store in PEM format
privBytes := pem.EncodeToMemory(&pem.Block{
Bytes: encBytes,
Type: CosignPrivateKeyPemType,
})
// Now do the public key
pubBytes, err := cryptoutils.MarshalPublicKeyToPEM(keypair.public)
if err != nil {
return nil, err
}
return &KeysBytes{
PrivateBytes: privBytes,
PublicBytes: pubBytes,
password: password,
}, nil
}
func GenerateKeyPair(pf PassFunc) (*KeysBytes, error) {
priv, err := GeneratePrivateKey()
if err != nil {
return nil, err
}
return marshalKeyPair(Keys{priv, priv.Public()}, pf)
}
func (k *KeysBytes) Password() []byte {
return k.password
}
func PemToECDSAKey(pemBytes []byte) (*ecdsa.PublicKey, error) {
pub, err := cryptoutils.UnmarshalPEMToPublicKey(pemBytes)
if err != nil {
return nil, err
}
ecdsaPub, ok := pub.(*ecdsa.PublicKey)
if !ok {
return nil, fmt.Errorf("invalid public key: was %T, require *ecdsa.PublicKey", pub)
}
return ecdsaPub, nil
}
func LoadPrivateKey(key []byte, pass []byte) (signature.SignerVerifier, error) {
// Decrypt first
p, _ := pem.Decode(key)
if p == nil {
return nil, errors.New("invalid pem block")
}
if p.Type != CosignPrivateKeyPemType {
return nil, fmt.Errorf("unsupported pem type: %s", p.Type)
}
x509Encoded, err := encrypted.Decrypt(p.Bytes, pass)
if err != nil {
return nil, fmt.Errorf("decrypt: %w", err)
}
pk, err := x509.ParsePKCS8PrivateKey(x509Encoded)
if err != nil {
return nil, fmt.Errorf("parsing private key: %w", err)
}
switch pk := pk.(type) {
case *rsa.PrivateKey:
return signature.LoadRSAPKCS1v15SignerVerifier(pk, crypto.SHA256)
case *ecdsa.PrivateKey:
return signature.LoadECDSASignerVerifier(pk, crypto.SHA256)
case ed25519.PrivateKey:
return signature.LoadED25519SignerVerifier(pk)
default:
return nil, errors.New("unsupported key type")
}
}