-
Notifications
You must be signed in to change notification settings - Fork 86
/
ecdsa.go
302 lines (273 loc) · 9.11 KB
/
ecdsa.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2024 Thales Group
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package crypto11
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/asn1"
"io"
"math/big"
"github.com/miekg/pkcs11"
"github.com/pkg/errors"
)
// errUnsupportedEllipticCurve is returned when an elliptic curve
// unsupported by crypto11 is specified. Note that the error behavior
// for an elliptic curve unsupported by the underlying PKCS#11
// implementation will be different.
var errUnsupportedEllipticCurve = errors.New("unsupported elliptic curve")
// pkcs11PrivateKeyECDSA contains a reference to a loaded PKCS#11 ECDSA private key object.
type pkcs11PrivateKeyECDSA struct {
pkcs11PrivateKey
}
// Information about an Elliptic Curve
type curveInfo struct {
// ASN.1 marshaled OID
oid []byte
// Curve definition in Go form
curve elliptic.Curve
}
// ASN.1 marshal some value and panic on error
func mustMarshal(val interface{}) []byte {
if b, err := asn1.Marshal(val); err != nil {
panic(err)
} else {
return b
}
}
// Note: some of these are outside what crypto/elliptic currently
// knows about. So I'm making a (reasonable) assumption about what
// they will be called if they are either added or if someone
// specifies them explicitly.
//
// For public key export, the curve has to be a known one, otherwise
// you're stuffed. This is probably better fixed by adding well-known
// curves to crypto/elliptic rather than having a private copy here.
var wellKnownCurves = map[string]curveInfo{
"P-192": {
mustMarshal(asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 1}),
nil,
},
"P-224": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 33}),
elliptic.P224(),
},
"P-256": {
mustMarshal(asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7}),
elliptic.P256(),
},
"P-384": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 34}),
elliptic.P384(),
},
"P-521": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 35}),
elliptic.P521(),
},
"K-163": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 1}),
nil,
},
"K-233": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 26}),
nil,
},
"K-283": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 16}),
nil,
},
"K-409": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 36}),
nil,
},
"K-571": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 38}),
nil,
},
"B-163": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 15}),
nil,
},
"B-233": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 27}),
nil,
},
"B-283": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 17}),
nil,
},
"B-409": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 37}),
nil,
},
"B-571": {
mustMarshal(asn1.ObjectIdentifier{1, 3, 132, 0, 39}),
nil,
},
}
func marshalEcParams(c elliptic.Curve) ([]byte, error) {
if ci, ok := wellKnownCurves[c.Params().Name]; ok {
return ci.oid, nil
}
// TODO use ANSI X9.62 ECParameters representation instead
return nil, errUnsupportedEllipticCurve
}
func unmarshalEcParams(b []byte) (elliptic.Curve, error) {
// See if it's a well-known curve
for _, ci := range wellKnownCurves {
if bytes.Equal(b, ci.oid) {
if ci.curve != nil {
return ci.curve, nil
}
return nil, errUnsupportedEllipticCurve
}
}
// TODO try ANSI X9.62 ECParameters representation
return nil, errUnsupportedEllipticCurve
}
func unmarshalEcPoint(b []byte, c elliptic.Curve) (*big.Int, *big.Int, error) {
var pointBytes []byte
extra, err := asn1.Unmarshal(b, &pointBytes)
if err != nil {
return nil, nil, errors.WithMessage(err, "elliptic curve point is invalid ASN.1")
}
if len(extra) > 0 {
// We weren't expecting extra data
return nil, nil, errors.New("unexpected data found when parsing elliptic curve point")
}
x, y := elliptic.Unmarshal(c, pointBytes)
if x == nil || y == nil {
return nil, nil, errors.New("failed to parse elliptic curve point")
}
return x, y, nil
}
// Export the public key corresponding to a private ECDSA key.
func exportECDSAPublicKey(session *pkcs11Session, pubHandle pkcs11.ObjectHandle) (crypto.PublicKey, error) {
var err error
var attributes []*pkcs11.Attribute
var pub ecdsa.PublicKey
template := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_ECDSA_PARAMS, nil),
pkcs11.NewAttribute(pkcs11.CKA_EC_POINT, nil),
}
if attributes, err = session.ctx.GetAttributeValue(session.handle, pubHandle, template); err != nil {
return nil, err
}
if pub.Curve, err = unmarshalEcParams(attributes[0].Value); err != nil {
return nil, err
}
if pub.X, pub.Y, err = unmarshalEcPoint(attributes[1].Value, pub.Curve); err != nil {
return nil, err
}
return &pub, nil
}
// GenerateECDSAKeyPair creates a ECDSA key pair on the token using curve c. The id parameter is used to
// set CKA_ID and must be non-nil. Only a limited set of named elliptic curves are supported. The
// underlying PKCS#11 implementation may impose further restrictions.
func (c *Context) GenerateECDSAKeyPair(id []byte, curve elliptic.Curve) (Signer, error) {
if c.closed.Get() {
return nil, errClosed
}
public, err := NewAttributeSetWithID(id)
if err != nil {
return nil, err
}
// Copy the AttributeSet to allow modifications.
private := public.Copy()
return c.GenerateECDSAKeyPairWithAttributes(public, private, curve)
}
// GenerateECDSAKeyPairWithLabel creates a ECDSA key pair on the token using curve c. The id and label parameters are used to
// set CKA_ID and CKA_LABEL respectively and must be non-nil. Only a limited set of named elliptic curves are supported. The
// underlying PKCS#11 implementation may impose further restrictions.
func (c *Context) GenerateECDSAKeyPairWithLabel(id, label []byte, curve elliptic.Curve) (Signer, error) {
if c.closed.Get() {
return nil, errClosed
}
public, err := NewAttributeSetWithIDAndLabel(id, label)
if err != nil {
return nil, err
}
// Copy the AttributeSet to allow modifications.
private := public.Copy()
return c.GenerateECDSAKeyPairWithAttributes(public, private, curve)
}
// GenerateECDSAKeyPairWithAttributes generates an ECDSA key pair on the token. After this function returns, public and
// private will contain the attributes applied to the key pair. If required attributes are missing, they will be set to
// a default value.
func (c *Context) GenerateECDSAKeyPairWithAttributes(public, private AttributeSet, curve elliptic.Curve) (Signer, error) {
if c.closed.Get() {
return nil, errClosed
}
var k Signer
err := c.withSession(func(session *pkcs11Session) error {
parameters, err := marshalEcParams(curve)
if err != nil {
return err
}
public.AddIfNotPresent([]*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_PUBLIC_KEY),
pkcs11.NewAttribute(pkcs11.CKA_KEY_TYPE, pkcs11.CKK_ECDSA),
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
pkcs11.NewAttribute(pkcs11.CKA_VERIFY, true),
pkcs11.NewAttribute(pkcs11.CKA_ECDSA_PARAMS, parameters),
})
private.AddIfNotPresent([]*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
pkcs11.NewAttribute(pkcs11.CKA_SIGN, true),
pkcs11.NewAttribute(pkcs11.CKA_SENSITIVE, true),
pkcs11.NewAttribute(pkcs11.CKA_EXTRACTABLE, false),
})
mech := []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_ECDSA_KEY_PAIR_GEN, nil)}
pubHandle, privHandle, err := session.ctx.GenerateKeyPair(session.handle,
mech,
public.ToSlice(),
private.ToSlice())
if err != nil {
return err
}
pub, err := exportECDSAPublicKey(session, pubHandle)
if err != nil {
return err
}
k = &pkcs11PrivateKeyECDSA{
pkcs11PrivateKey: pkcs11PrivateKey{
pkcs11Object: pkcs11Object{
handle: privHandle,
context: c,
},
pubKeyHandle: pubHandle,
pubKey: pub,
}}
return nil
})
return k, err
}
// Sign signs a message using an ECDSA key.
//
// This completes the implemention of crypto.Signer for pkcs11PrivateKeyECDSA.
//
// PKCS#11 expects to pick its own random data where necessary for signatures, so the rand argument is ignored.
//
// The return value is a DER-encoded byteblock.
func (signer *pkcs11PrivateKeyECDSA) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
return signer.context.dsaGeneric(signer.handle, pkcs11.CKM_ECDSA, digest)
}