Debian Codegraph @

crypto/keys/key.go

raw symbols SCIP
1 package keys
2
3 import (
4 "bytes"
5 "crypto/ecdsa"
6 "crypto/ed25519"
7 "crypto/elliptic"
8 "crypto/rand"
9 "crypto/rsa"
10 "crypto/x509"
11 "math/big"
12
13 "github.com/pkg/errors"
14 "golang.org/x/crypto/ssh"
15 )
16
17 var (
18 // DefaultKeyType is the default type of a private key.
19 DefaultKeyType = "EC"
20 // DefaultKeySize is the default size (in # of bits) of a private key.
21 DefaultKeySize = 2048
22 // DefaultKeyCurve is the default curve of a private key.
23 DefaultKeyCurve = "P-256"
24 // DefaultSignatureAlgorithm is the default signature algorithm used on a
25 // certificate with the default key type.
26 DefaultSignatureAlgorithm = x509.ECDSAWithSHA256
27 // MinRSAKeyBytes is the minimum acceptable size (in bytes) for RSA keys
28 // signed by the authority.
29 MinRSAKeyBytes = 256
30 )
31
32 // PublicKey extracts a public key from a private key.
33 func PublicKey(priv interface{}) (interface{}, error) {
34 switch k := priv.(type) {
35 case *rsa.PrivateKey:
36 return &k.PublicKey, nil
37 case *ecdsa.PrivateKey:
38 return &k.PublicKey, nil
39 case ed25519.PrivateKey:
40 return k.Public(), nil
41 case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey:
42 return k, nil
43 default:
44 return nil, errors.Errorf("unrecognized key type: %T", priv)
45 }
46 }
47
48 // GenerateDefaultKey generates a public/private key pair using sane defaults
49 // for key type, curve, and size.
50 func GenerateDefaultKey() (interface{}, error) {
51 return GenerateKey(DefaultKeyType, DefaultKeyCurve, DefaultKeySize)
52 }
53
54 // GenerateKey generates a key of the given type (kty).
55 func GenerateKey(kty, crv string, size int) (interface{}, error) {
56 switch kty {
57 case "EC":
58 return generateECKey(crv)
59 case "RSA":
60 return generateRSAKey(size)
61 case "OKP":
62 return generateOKPKey(crv)
63 case "oct":
64 return generateOctKey(size)
65 default:
66 return nil, errors.Errorf("unrecognized key type: %s", kty)
67 }
68 }
69
70 // ExtractKey returns the given public or private key or extracts the public key
71 // if a x509.Certificate or x509.CertificateRequest is given.
72 func ExtractKey(in interface{}) (interface{}, error) {
73 switch k := in.(type) {
74 case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey, *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey:
75 return in, nil
76 case []byte:
77 return in, nil
78 case *x509.Certificate:
79 return k.PublicKey, nil
80 case *x509.CertificateRequest:
81 return k.PublicKey, nil
82 case *ssh.Certificate:
83 sshCryptoPubKey, ok := k.Key.(ssh.CryptoPublicKey)
84 if !ok {
85 return nil, errors.New("ssh public key could not be cast to ssh CryptoPublicKey")
86 }
87 return sshCryptoPubKey.CryptoPublicKey(), nil
88 case ssh.PublicKey:
89 sshCryptoPubKey, ok := k.(ssh.CryptoPublicKey)
90 if !ok {
91 return nil, errors.New("ssh public key could not be cast to ssh CryptoPublicKey")
92 }
93 return sshCryptoPubKey.CryptoPublicKey(), nil
94 default:
95 return nil, errors.Errorf("cannot extract the key from type '%T'", k)
96 }
97 }
98
99 // VerifyPair that the public key matches the given private key.
100 func VerifyPair(pubkey interface{}, key interface{}) error {
101 switch pub := pubkey.(type) {
102 case *rsa.PublicKey:
103 priv, ok := key.(*rsa.PrivateKey)
104 if !ok {
105 return errors.New("private key type does not match public key type")
106 }
107 if pub.N.Cmp(priv.N) != 0 {
108 return errors.New("private key does not match public key")
109 }
110 case *ecdsa.PublicKey:
111 priv, ok := key.(*ecdsa.PrivateKey)
112 if !ok {
113 return errors.New("private key type does not match public key type")
114 }
115 if pub.X.Cmp(priv.X) != 0 || pub.Y.Cmp(priv.Y) != 0 {
116 return errors.New("private key does not match public key")
117 }
118 case ed25519.PublicKey:
119 priv, ok := key.(ed25519.PrivateKey)
120 if !ok {
121 return errors.New("private key type does not match public key type")
122 }
123 if !bytes.Equal(priv.Public().(ed25519.PublicKey), pub) {
124 return errors.New("private key does not match public key")
125 }
126 default:
127 return errors.Errorf("unsupported public key type %T", pub)
128 }
129 return nil
130 }
131
132 func generateECKey(crv string) (interface{}, error) {
133 var c elliptic.Curve
134 switch crv {
135 case "P-256":
136 c = elliptic.P256()
137 case "P-384":
138 c = elliptic.P384()
139 case "P-521":
140 c = elliptic.P521()
141 default:
142 return nil, errors.Errorf("invalid value for argument crv (crv: '%s')", crv)
143 }
144
145 key, err := ecdsa.GenerateKey(c, rand.Reader)
146 if err != nil {
147 return nil, errors.Wrap(err, "error generating EC key")
148 }
149
150 return key, nil
151 }
152
153 func generateRSAKey(bits int) (interface{}, error) {
154 key, err := rsa.GenerateKey(rand.Reader, bits)
155 if err != nil {
156 return nil, errors.Wrap(err, "error generating RSA key")
157 }
158
159 return key, nil
160 }
161
162 func generateOKPKey(crv string) (interface{}, error) {
163 switch crv {
164 case "Ed25519":
165 _, key, err := ed25519.GenerateKey(rand.Reader)
166 if err != nil {
167 return nil, errors.Wrap(err, "error generating Ed25519 key")
168 }
169
170 return key, nil
171 default:
172 return nil, errors.Errorf("missing or invalid value for argument 'crv'. "+
173 "expected 'Ed25519', but got '%s'", crv)
174 }
175 }
176
177 func generateOctKey(size int) (interface{}, error) {
178 const chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
179 result := make([]byte, size)
180 for i := range result {
181 num, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
182 if err != nil {
183 return nil, err
184 }
185 result[i] = chars[num.Int64()]
186 }
187 return result, nil
188 }