-
Notifications
You must be signed in to change notification settings - Fork 39
/
pkix.go
213 lines (197 loc) · 6.85 KB
/
pkix.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
//
// Copyright (c) SAS Institute Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package x509tools
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"errors"
"fmt"
)
var (
// RFC 3279
OidPublicKeyRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
OidPublicKeyDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1}
OidPublicKeyECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1}
oidSignatureMD5WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 4}
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}
oidSignatureDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3}
oidSignatureDSAWithSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 3, 2}
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}
oidISOSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 29}
// RFC 4055
OidMGF1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 8}
OidSignatureRSAPSS = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 10}
oidExtensionSubjectKeyId = asn1.ObjectIdentifier{2, 5, 29, 14}
oidExtensionKeyUsage = asn1.ObjectIdentifier{2, 5, 29, 15}
oidExtensionSubjectAltName = asn1.ObjectIdentifier{2, 5, 29, 17}
oidExtensionBasicConstraints = asn1.ObjectIdentifier{2, 5, 29, 19}
oidExtensionAuthorityKeyId = asn1.ObjectIdentifier{2, 5, 29, 35}
oidExtensionExtendedKeyUsage = asn1.ObjectIdentifier{2, 5, 29, 37}
)
type sigAlgInfo struct {
oid asn1.ObjectIdentifier
pubKeyAlgo x509.PublicKeyAlgorithm
hash crypto.Hash
}
var sigAlgInfos = []sigAlgInfo{
// without a digest
{OidPublicKeyRSA, x509.RSA, 0},
{OidSignatureRSAPSS, x509.RSA, 0},
{OidPublicKeyDSA, x509.DSA, 0},
{OidPublicKeyECDSA, x509.ECDSA, 0},
// with a digest
{oidSignatureMD5WithRSA, x509.RSA, crypto.MD5},
{oidSignatureSHA1WithRSA, x509.RSA, crypto.SHA1},
{oidISOSignatureSHA1WithRSA, x509.RSA, crypto.SHA1},
{oidSignatureSHA256WithRSA, x509.RSA, crypto.SHA256},
{oidSignatureSHA384WithRSA, x509.RSA, crypto.SHA384},
{oidSignatureSHA512WithRSA, x509.RSA, crypto.SHA512},
{oidSignatureDSAWithSHA1, x509.DSA, crypto.SHA1},
{oidSignatureDSAWithSHA256, x509.DSA, crypto.SHA256},
{oidSignatureECDSAWithSHA1, x509.ECDSA, crypto.SHA1},
{oidSignatureECDSAWithSHA256, x509.ECDSA, crypto.SHA256},
{oidSignatureECDSAWithSHA384, x509.ECDSA, crypto.SHA384},
{oidSignatureECDSAWithSHA512, x509.ECDSA, crypto.SHA512},
}
// Given a public key and signer options, return the appropriate X.509 digest and signature algorithms
func PkixAlgorithms(pub crypto.PublicKey, opts crypto.SignerOpts) (digestAlg, sigAlg pkix.AlgorithmIdentifier, err error) {
digestAlg, ok := PkixDigestAlgorithm(opts.HashFunc())
if !ok {
err = errors.New("unsupported digest algorithm")
return
}
if pss, ok := opts.(*rsa.PSSOptions); ok {
rsapub, ok := pub.(*rsa.PublicKey)
if !ok {
err = errors.New("RSA-PSS is only valid for RSA keys")
return
}
var params asn1.RawValue
params, err = MarshalRSAPSSParameters(rsapub, pss)
if err != nil {
return
}
sigAlg = pkix.AlgorithmIdentifier{
Algorithm: OidSignatureRSAPSS,
Parameters: params,
}
return
}
switch pub.(type) {
case *rsa.PublicKey:
sigAlg.Algorithm = OidPublicKeyRSA
case *ecdsa.PublicKey:
sigAlg.Algorithm = OidPublicKeyECDSA
default:
err = errors.New("unsupported public key algorithm")
return
}
sigAlg.Parameters = asn1.NullRawValue
return
}
// Convert a crypto.Hash to a X.509 AlgorithmIdentifier
func PkixDigestAlgorithm(hash crypto.Hash) (alg pkix.AlgorithmIdentifier, ok bool) {
if oid, ok2 := HashOids[hash]; ok2 {
alg.Algorithm = oid
alg.Parameters = asn1.NullRawValue
ok = true
}
return
}
// Convert a X.509 AlgorithmIdentifier to a crypto.Hash
func PkixDigestToHash(alg pkix.AlgorithmIdentifier) (hash crypto.Hash, ok bool) {
for hash, oid := range HashOids {
if alg.Algorithm.Equal(oid) {
return hash, true
}
}
return 0, false
}
// Convert a X.509 AlgorithmIdentifier to a crypto.Hash
func PkixDigestToHashE(alg pkix.AlgorithmIdentifier) (hash crypto.Hash, err error) {
hash, ok := PkixDigestToHash(alg)
if ok && hash.Available() {
return hash, nil
}
return 0, UnknownDigestError{Algorithm: alg.Algorithm}
}
// Convert a crypto.PublicKey to a X.509 AlgorithmIdentifier
func PkixPublicKeyAlgorithm(pub crypto.PublicKey) (alg pkix.AlgorithmIdentifier, ok bool) {
_, alg, err := PkixAlgorithms(pub, nil)
return alg, err == nil
}
// Verify a signature using the algorithm specified by the given X.509 AlgorithmIdentifier
func PkixVerify(pub crypto.PublicKey, digestAlg, sigAlg pkix.AlgorithmIdentifier, digest, sig []byte) error {
hash, err := PkixDigestToHashE(digestAlg)
if err != nil {
return err
}
var info sigAlgInfo
for _, a := range sigAlgInfos {
if sigAlg.Algorithm.Equal(a.oid) {
info = a
}
}
if info.hash != 0 && info.hash != hash {
return errors.New("signature type does not match digest type")
}
switch info.pubKeyAlgo {
case x509.RSA:
key, ok := pub.(*rsa.PublicKey)
if !ok {
return errors.New("incorrect key type for signature")
}
if sigAlg.Algorithm.Equal(OidSignatureRSAPSS) {
opts, err := UnmarshalRSAPSSParameters(hash, sigAlg.Parameters)
if err != nil {
return err
}
return rsa.VerifyPSS(key, hash, digest, sig, opts)
}
return rsa.VerifyPKCS1v15(key, hash, digest, sig)
case x509.ECDSA:
key, ok := pub.(*ecdsa.PublicKey)
if !ok {
return errors.New("incorrect key type for signature")
}
esig, err := UnmarshalEcdsaSignature(sig)
if err != nil {
return err
}
if !ecdsa.Verify(key, digest, esig.R, esig.S) {
return errors.New("ECDSA verification failed")
}
return nil
default:
return errors.New("unsupported public key algorithm")
}
}
type UnknownDigestError struct {
Algorithm asn1.ObjectIdentifier
}
func (e UnknownDigestError) Error() string {
return fmt.Sprintf("unsupported hash algorithm %s", e.Algorithm)
}