-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.go
183 lines (151 loc) · 5.25 KB
/
crypto.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
package license
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/asn1"
"errors"
"math/big"
)
var (
oidSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}
oidSignatureSHA256WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
oidSignatureSHA384WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12}
oidSignatureSHA512WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13}
oidSignatureECDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1}
oidSignatureECDSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
oidSignatureECDSAWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
oidSignatureECDSAWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}
)
var signatureAlgorithmDetails = []struct {
algo x509.SignatureAlgorithm
name string
oid asn1.ObjectIdentifier
pubKeyAlgo x509.PublicKeyAlgorithm
hash crypto.Hash
}{
{x509.SHA1WithRSA, "SHA1-RSA", oidSignatureSHA1WithRSA, x509.RSA, crypto.SHA1},
{x509.SHA256WithRSA, "SHA256-RSA", oidSignatureSHA256WithRSA, x509.RSA, crypto.SHA256},
{x509.SHA384WithRSA, "SHA384-RSA", oidSignatureSHA384WithRSA, x509.RSA, crypto.SHA384},
{x509.SHA512WithRSA, "SHA512-RSA", oidSignatureSHA512WithRSA, x509.RSA, crypto.SHA512},
{x509.ECDSAWithSHA1, "ECDSA-SHA1", oidSignatureECDSAWithSHA1, x509.ECDSA, crypto.SHA1},
{x509.ECDSAWithSHA256, "ECDSA-SHA256", oidSignatureECDSAWithSHA256, x509.ECDSA, crypto.SHA256},
{x509.ECDSAWithSHA384, "ECDSA-SHA384", oidSignatureECDSAWithSHA384, x509.ECDSA, crypto.SHA384},
{x509.ECDSAWithSHA512, "ECDSA-SHA512", oidSignatureECDSAWithSHA512, x509.ECDSA, crypto.SHA512},
}
// Identify Signature Algorithm by oid.
func authorityHashFromAlgorithm(key interface{}, license asnSignedLicense) ([]byte, crypto.Hash, error) {
var hashFunc crypto.Hash
digest, err := authorityHashFromKey(key)
if err != nil {
return nil, hashFunc, err
}
if !bytes.Equal(digest, license.AuthorityKeyID) {
return nil, hashFunc, errors.New("invalid Authority Id")
}
var pubKeyAlgo x509.PublicKeyAlgorithm
switch key.(type) {
case *rsa.PublicKey:
pubKeyAlgo = x509.RSA
case *ecdsa.PublicKey:
pubKeyAlgo = x509.ECDSA
}
for _, match := range signatureAlgorithmDetails {
if license.SignatureAlgorithm.Equal(match.oid) && match.pubKeyAlgo == pubKeyAlgo {
return asnLicenseHash(license, match.hash)
}
}
return nil, hashFunc, errors.New("algorithm unimplemented")
}
func authorityHashFromPublicKey(key interface{}) ([]byte, crypto.Hash, asn1.ObjectIdentifier, error) {
var (
signatureAlgorithm asn1.ObjectIdentifier
hashFunc crypto.Hash
)
digest, err := authorityHashFromKey(key)
if err != nil {
return nil, hashFunc, signatureAlgorithm, err
}
switch pub := key.(type) {
case *rsa.PublicKey:
hashFunc = crypto.SHA256
signatureAlgorithm = oidSignatureSHA256WithRSA
case *ecdsa.PublicKey:
switch pub.Curve {
case elliptic.P224(), elliptic.P256():
hashFunc = crypto.SHA256
signatureAlgorithm = oidSignatureECDSAWithSHA256
case elliptic.P384():
hashFunc = crypto.SHA384
signatureAlgorithm = oidSignatureECDSAWithSHA384
case elliptic.P521():
hashFunc = crypto.SHA512
signatureAlgorithm = oidSignatureECDSAWithSHA512
}
default:
return nil, hashFunc, signatureAlgorithm, errors.New("only RSA and ECDSA keys supported")
}
return digest, hashFunc, signatureAlgorithm, nil
}
func checkSignature(digest, signature []byte, hashType crypto.Hash, publicKey crypto.PublicKey) (err error) {
if !hashType.Available() {
return errors.New("cannot verify signature: algorithm unimplemented")
}
switch pub := publicKey.(type) {
case *rsa.PublicKey:
return rsa.VerifyPKCS1v15(pub, hashType, digest, signature)
case *ecdsa.PublicKey:
return ecdsaVerifyPCKS(pub, digest, signature)
}
return errors.New("cannot verify signature: only RSA and ECDSA keys supported")
}
func ecdsaVerifyPCKS(pub *ecdsa.PublicKey, digest, signature []byte) error {
type ecdsaSignature struct {
R, S *big.Int
}
ecdsaSig := new(ecdsaSignature)
rest, err := asn1.Unmarshal(signature, ecdsaSig)
if err != nil || len(rest) != 0 {
return errors.New("license: malformed data")
}
if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 || !ecdsa.Verify(pub, digest, ecdsaSig.R, ecdsaSig.S) {
return errors.New("license: verification failure")
}
return nil
}
func authorityHashFromKey(key interface{}) ([]byte, error) {
sigBytes, err := x509.MarshalPKIXPublicKey(key) // *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey
if err != nil {
return nil, err
}
digest := sha1.New()
_, err = digest.Write(sigBytes)
if err != nil {
return nil, err
}
return digest.Sum(nil), nil
}
func signAsnObject(license asnSignedLicense, key crypto.Signer, hash crypto.Hash) ([]byte, error) {
digest, _, err := asnLicenseHash(license, hash)
if err != nil {
return nil, err
}
return key.Sign(rand.Reader, digest, hash)
}
func asnLicenseHash(license asnSignedLicense, h crypto.Hash) (hash []byte, hashFunc crypto.Hash, err error) {
asnData, err := asn1.Marshal(license)
if err != nil {
return nil, h, err
}
digest := h.New()
_, err = digest.Write(asnData)
if err != nil {
return nil, h, err
}
return digest.Sum(nil), h, nil
}