forked from ThalesGroup/gose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdsa_signer.go
172 lines (146 loc) · 4.95 KB
/
ecdsa_signer.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
// 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 gose
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"github.com/ThalesGroup/gose/jose"
"math/big"
)
//ECDSAOptions Implements crypto.SignerOpts
type ECDSAOptions struct {
Hash crypto.Hash
keySizeBytes int
curveBits int
curve elliptic.Curve
}
//HashFunc returns the crypto.Hash
func (opts *ECDSAOptions) HashFunc() crypto.Hash {
return opts.Hash
}
//-------------------
//ECDSASigningKey implements ECDSA crypto.SigningKey
type ECDSASigningKey struct {
jwk jose.Jwk
key crypto.Signer
certs []*x509.Certificate
}
//Algorithm returns the jose.Alg for this key
func (signer ECDSASigningKey) Algorithm() jose.Alg {
return signer.jwk.Alg()
}
// Key returns the underlying key used to sign
func (signer *ECDSASigningKey) Key() crypto.Signer {
return signer.key
}
// Sign digest and sign the given data.
// The output signature is encoded as r || s which is different to the standard go crypto interface specification.
// The serialization format is chosen instead to match that defined in the JSON Web Signature spec
// https://tools.ietf.org/html/rfc7515#appendix-A.3.1.
func (signer *ECDSASigningKey) Sign(requested jose.KeyOps, data []byte) (signature []byte, err error) {
ops := intersection(validSignerOps, signer.jwk.Ops())
if !isSubset(ops, []jose.KeyOps{requested}) {
err = ErrInvalidOperations
return
}
opts := algToOptsMap[signer.jwk.Alg()]
if !opts.HashFunc().Available() {
err = ErrHashUnavailable
return
}
hasher := opts.HashFunc().New()
if _, err := hasher.Write([]byte(data)); err != nil {
panic(err)
}
// Sign the string and return r, s
key := signer.key.(*ecdsa.PrivateKey)
var r, s *big.Int
if r, s, err = ecdsa.Sign(rand.Reader, key, hasher.Sum(nil)); err == nil {
curveBits := key.Curve.Params().BitSize
options := opts.(*ECDSAOptions)
if options.curveBits != curveBits {
err = ErrInvalidKey
return
}
keyBytes := (curveBits + 7) / 8
// We serialize the outpus (r and s) into big-endian byte arrays and pad
// them with zeros on the left to make sure the sizes work out. Both arrays
// must be keyBytes long, and the output must be 2*keyBytes long.
rBytes := r.Bytes()
rBytesPadded := make([]byte, keyBytes)
copy(rBytesPadded[keyBytes-len(rBytes):], rBytes)
sBytes := s.Bytes()
sBytesPadded := make([]byte, keyBytes)
copy(sBytesPadded[keyBytes-len(sBytes):], sBytes)
signature = append(rBytesPadded, sBytesPadded...)
return
}
return
}
// Verifier get the matching verification key.
func (signer *ECDSASigningKey) Verifier() (VerificationKey, error) {
publicJwk, err := PublicFromPrivate(signer.jwk)
if err != nil {
return nil, err
}
return NewVerificationKey(publicJwk)
}
//Kid returns the kid string value
func (signer *ECDSASigningKey) Kid() string {
/* JIT jwk load. */
return signer.jwk.Kid()
}
//Marshal marshals the key into a compact JWK representation or error
func (signer *ECDSASigningKey) Marshal() (string, error) {
return JwkToString(signer.jwk)
}
const ecdsaPrivateKeyPerType = "ECDSA PRIVATE KEY"
//MarshalPem marshals the key into a PEM string or error
func (signer *ECDSASigningKey) MarshalPem() (p string, err error) {
pemType := ecdsaPrivateKeyPerType
var derEncoded []byte
if derEncoded, err = x509.MarshalECPrivateKey(signer.key.(*ecdsa.PrivateKey)); err != nil {
return
}
block := pem.Block{
Type: pemType,
Bytes: derEncoded,
}
output := bytes.Buffer{}
if err = pem.Encode(&output, &block); err != nil {
return
}
return string(output.Bytes()), nil
}
//Certificates returns certificate chain of this key
func (signer *ECDSASigningKey) Certificates() []*x509.Certificate {
return signer.certs
}
//Jwk returns key as a jose.JWK type, or errors
func (signer *ECDSASigningKey) Jwk() (jose.Jwk, error) {
/* Return a copy of our JWK. */
return JwkFromPrivateKey(signer.key, signer.jwk.Ops(), signer.certs)
}