/
pkcs10.go
285 lines (242 loc) · 6.87 KB
/
pkcs10.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
package pkcs10
import (
"crypto"
"crypto/dsa"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"errors"
"io"
"math/big"
)
var (
oidAttrUnstructuredName = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 2}
oidAttrChallengePassword = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 7}
)
type CsrAttribute int
const (
CsrAttrUnknown CsrAttribute = iota
CsrAttrUnstructuredName
CsrAttrChallengePassword
)
type pkcs10 struct {
Info pkcs10Info
Algo pkix.AlgorithmIdentifier
Sig asn1.BitString
}
type pkcs10Info struct {
Version int
Subject pkix.RDNSequence
SubjectInfo publicKeyInfo
Attributes asn1.RawValue
}
type publicKeyInfo struct {
Algorithm pkix.AlgorithmIdentifier
PublicKey asn1.BitString
}
type attribute struct {
Name asn1.ObjectIdentifier
Value asn1.RawValue
}
type Attribute struct {
Name CsrAttribute
Value interface{}
}
type CertificationRequest struct {
Version int
Subject pkix.Name
SubjectKeyAlgorithm x509.PublicKeyAlgorithm
SubjectKey interface{}
SignatureAlgorithm x509.SignatureAlgorithm
Signature []byte
Attributes []*Attribute
}
func CreateCertificateRequest(rand io.Reader, csr *CertificationRequest, pub interface{}, priv interface{}) ([]byte, error) {
rsaPub, ok := pub.(*rsa.PublicKey)
if !ok {
return nil, errors.New("pkcs10: non-RSA public keys not supported")
}
rsaPriv, ok := priv.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("x509: non-RSA private keys not supported")
}
if rsaPriv != nil {
}
asn1PublicKey, err := asn1.Marshal(rsaPublicKey{
N: rsaPub.N,
E: rsaPub.E,
})
if err != nil {
return nil, err
}
encodedPublicKey := asn1.BitString{BitLength: len(asn1PublicKey) * 8, Bytes: asn1PublicKey}
var p pkcs10
p.Info.Subject = csr.Subject.ToRDNSequence()
p.Info.Version = csr.Version
p.Info.SubjectInfo.Algorithm = pkix.AlgorithmIdentifier{Algorithm: oidPublicKeyRsa}
p.Info.SubjectInfo.PublicKey = encodedPublicKey
p.Info.Attributes = asn1.RawValue{Class: 2, Tag: 0, IsCompound: true, FullBytes: []byte{160, 0}}
pkcsInfoContents, err := asn1.Marshal(p.Info)
if err != nil {
return nil, err
}
h := sha1.New()
h.Write(pkcsInfoContents)
digest := h.Sum(nil)
signature, err := rsa.SignPKCS1v15(rand, rsaPriv, crypto.SHA1, digest)
if err != nil {
return nil, err
}
p.Algo = pkix.AlgorithmIdentifier{Algorithm: oidSignatureSHA1WithRSA}
p.Sig = asn1.BitString{Bytes: signature, BitLength: len(signature) * 8}
return asn1.Marshal(p)
}
func ParsePKCS10CSR(der []byte) (csr *CertificationRequest, err error) {
var p pkcs10
if _, err := asn1.Unmarshal(der, &p); err != nil {
return nil, err
}
certReq := &CertificationRequest{
Version: p.Info.Version,
Signature: p.Sig.Bytes,
SignatureAlgorithm: getSignatureAlgorithmFromOID(p.Algo.Algorithm),
}
certReq.SubjectKeyAlgorithm = getPublicKeyAlgorithmFromOID(p.Info.SubjectInfo.Algorithm.Algorithm)
pubKey, err := parsePublicKey(certReq.SubjectKeyAlgorithm, &p.Info.SubjectInfo)
if err != nil {
return nil, err
}
certReq.SubjectKey = pubKey
certReq.Subject.FillFromRDNSequence(&p.Info.Subject)
rest := p.Info.Attributes.Bytes
for {
if len(rest) == 0 {
break
}
var a attribute
rest, err = asn1.Unmarshal(rest, &a)
if err != nil {
return nil, err
}
var i interface{}
_, err := asn1.Unmarshal(a.Value.Bytes, &i)
if err != nil {
return nil, err
}
attr := &Attribute{
Name: getAttributeNameFromOID(a.Name),
Value: i,
}
certReq.Attributes = append(certReq.Attributes, attr)
}
return certReq, nil
}
func getAttributeNameFromOID(oid asn1.ObjectIdentifier) CsrAttribute {
switch {
case oid.Equal(oidAttrUnstructuredName):
return CsrAttrUnstructuredName
case oid.Equal(oidAttrChallengePassword):
return CsrAttrChallengePassword
}
return CsrAttrUnknown
}
// The following code is lifted straight from the source of the x509.go file since
// there is no point implementing my own solution and there isn't any way to access
// it since its not exported. If at some point in time this is included in the stdlib
// then it can be removed or some other solution can be produced.
var (
oidSignatureMD2WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 2}
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, 4, 3, 2}
)
func getSignatureAlgorithmFromOID(oid asn1.ObjectIdentifier) x509.SignatureAlgorithm {
switch {
case oid.Equal(oidSignatureMD2WithRSA):
return x509.MD2WithRSA
case oid.Equal(oidSignatureMD5WithRSA):
return x509.MD5WithRSA
case oid.Equal(oidSignatureSHA1WithRSA):
return x509.SHA1WithRSA
case oid.Equal(oidSignatureSHA256WithRSA):
return x509.SHA256WithRSA
case oid.Equal(oidSignatureSHA384WithRSA):
return x509.SHA384WithRSA
case oid.Equal(oidSignatureSHA512WithRSA):
return x509.SHA512WithRSA
case oid.Equal(oidSignatureDSAWithSHA1):
return x509.DSAWithSHA1
case oid.Equal(oidSignatureDSAWithSHA256):
return x509.DSAWithSHA256
}
return x509.UnknownSignatureAlgorithm
}
var (
oidPublicKeyRsa = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
oidPublicKeyDsa = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1}
)
func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) x509.PublicKeyAlgorithm {
switch {
case oid.Equal(oidPublicKeyRsa):
return x509.RSA
case oid.Equal(oidPublicKeyDsa):
return x509.DSA
}
return x509.UnknownPublicKeyAlgorithm
}
type dsaAlgorithmParameters struct {
P, Q, G *big.Int
}
type rsaPublicKey struct {
N *big.Int
E int
}
func parsePublicKey(algo x509.PublicKeyAlgorithm, keyData *publicKeyInfo) (interface{}, error) {
asn1Data := keyData.PublicKey.RightAlign()
switch algo {
case x509.RSA:
p := new(rsaPublicKey)
_, err := asn1.Unmarshal(asn1Data, p)
if err != nil {
return nil, err
}
pub := &rsa.PublicKey{
E: p.E,
N: p.N,
}
return pub, nil
case x509.DSA:
var p *big.Int
_, err := asn1.Unmarshal(asn1Data, &p)
if err != nil {
return nil, err
}
paramsData := keyData.Algorithm.Parameters.FullBytes
params := new(dsaAlgorithmParameters)
_, err = asn1.Unmarshal(paramsData, params)
if err != nil {
return nil, err
}
if p.Sign() <= 0 || params.P.Sign() <= 0 || params.Q.Sign() <= 0 || params.G.Sign() <= 0 {
return nil, errors.New("zero or negative DSA parameter")
}
pub := &dsa.PublicKey{
Parameters: dsa.Parameters{
P: params.P,
Q: params.Q,
G: params.G,
},
Y: p,
}
return pub, nil
default:
return nil, nil
}
panic("unreachable")
}