|
| 1 | +package crypto |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto" |
| 5 | + "crypto/ecdsa" |
| 6 | + "crypto/rand" |
| 7 | + "encoding/asn1" |
| 8 | + "encoding/json" |
| 9 | + "errors" |
| 10 | + "math/big" |
| 11 | +) |
| 12 | + |
| 13 | +// ErrECDSAVerification is missing from crypto/ecdsa compared to crypto/rsa |
| 14 | +var ErrECDSAVerification = errors.New("crypto/ecdsa: verification error") |
| 15 | + |
| 16 | +// SigningMethodECDSA implements the ECDSA family of signing methods signing |
| 17 | +// methods |
| 18 | +type SigningMethodECDSA struct { |
| 19 | + Name string |
| 20 | + Hash crypto.Hash |
| 21 | + _ struct{} |
| 22 | +} |
| 23 | + |
| 24 | +// ECPoint is a marshalling structure for the EC points R and S. |
| 25 | +type ECPoint struct { |
| 26 | + R *big.Int |
| 27 | + S *big.Int |
| 28 | +} |
| 29 | + |
| 30 | +// Specific instances of EC SigningMethods. |
| 31 | +var ( |
| 32 | + // SigningMethodES256 implements ES256. |
| 33 | + SigningMethodES256 = &SigningMethodECDSA{ |
| 34 | + Name: "ES256", |
| 35 | + Hash: crypto.SHA256, |
| 36 | + } |
| 37 | + |
| 38 | + // SigningMethodES384 implements ES384. |
| 39 | + SigningMethodES384 = &SigningMethodECDSA{ |
| 40 | + Name: "ES384", |
| 41 | + Hash: crypto.SHA384, |
| 42 | + } |
| 43 | + |
| 44 | + // SigningMethodES512 implements ES512. |
| 45 | + SigningMethodES512 = &SigningMethodECDSA{ |
| 46 | + Name: "ES512", |
| 47 | + Hash: crypto.SHA512, |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +// Alg returns the name of the SigningMethodECDSA instance. |
| 52 | +func (m *SigningMethodECDSA) Alg() string { return m.Name } |
| 53 | + |
| 54 | +// Verify implements the Verify method from SigningMethod. |
| 55 | +// For this verify method, key must be an *ecdsa.PublicKey. |
| 56 | +func (m *SigningMethodECDSA) Verify(raw []byte, signature Signature, key interface{}) error { |
| 57 | + |
| 58 | + ecdsaKey, ok := key.(*ecdsa.PublicKey) |
| 59 | + if !ok { |
| 60 | + return ErrInvalidKey |
| 61 | + } |
| 62 | + |
| 63 | + // Unmarshal asn1 ECPoint |
| 64 | + var ecpoint ECPoint |
| 65 | + if _, err := asn1.Unmarshal(signature, &ecpoint); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + // Verify the signature |
| 70 | + if !ecdsa.Verify(ecdsaKey, m.sum(raw), ecpoint.R, ecpoint.S) { |
| 71 | + return ErrECDSAVerification |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// Sign implements the Sign method from SigningMethod. |
| 77 | +// For this signing method, key must be an *ecdsa.PrivateKey. |
| 78 | +func (m *SigningMethodECDSA) Sign(data []byte, key interface{}) (Signature, error) { |
| 79 | + |
| 80 | + ecdsaKey, ok := key.(*ecdsa.PrivateKey) |
| 81 | + if !ok { |
| 82 | + return nil, ErrInvalidKey |
| 83 | + } |
| 84 | + |
| 85 | + r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, m.sum(data)) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + signature, err := asn1.Marshal(ECPoint{R: r, S: s}) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + return Signature(signature), nil |
| 95 | +} |
| 96 | + |
| 97 | +func (m *SigningMethodECDSA) sum(b []byte) []byte { |
| 98 | + h := m.Hash.New() |
| 99 | + h.Write(b) |
| 100 | + return h.Sum(nil) |
| 101 | +} |
| 102 | + |
| 103 | +// Hasher implements the Hasher method from SigningMethod. |
| 104 | +func (m *SigningMethodECDSA) Hasher() crypto.Hash { |
| 105 | + return m.Hash |
| 106 | +} |
| 107 | + |
| 108 | +// MarshalJSON is in case somebody decides to place SigningMethodECDSA |
| 109 | +// inside the Header, presumably because they (wrongly) decided it was a good |
| 110 | +// idea to use the SigningMethod itself instead of the SigningMethod's Alg |
| 111 | +// method. In order to keep things sane, marshalling this will simply |
| 112 | +// return the JSON-compatible representation of m.Alg(). |
| 113 | +func (m *SigningMethodECDSA) MarshalJSON() ([]byte, error) { |
| 114 | + return []byte(`"` + m.Alg() + `"`), nil |
| 115 | +} |
| 116 | + |
| 117 | +var _ json.Marshaler = (*SigningMethodECDSA)(nil) |
0 commit comments