-
Notifications
You must be signed in to change notification settings - Fork 35
/
spkac.go
203 lines (176 loc) · 4.75 KB
/
spkac.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
package gold
import (
"bytes"
// "crypto/ecdsa"
// "crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"errors"
"math/big"
)
type pkacInfo struct {
Raw asn1.RawContent
PublicKey publicKeyInfo
Challenge string
}
type spkacInfo struct {
Raw asn1.RawContent
Pkac pkacInfo
Algorithm pkix.AlgorithmIdentifier
Signature asn1.BitString
}
type rsaPublicKey struct {
N *big.Int
E int
}
type publicKeyInfo struct {
Raw asn1.RawContent
Algorithm pkix.AlgorithmIdentifier
PublicKey asn1.BitString
}
var (
oidPublicKeyRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
)
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
}
if p.N.Sign() <= 0 {
return nil, errors.New("x509: RSA modulus is not a positive number")
}
if p.E <= 0 {
return nil, errors.New("x509: RSA public exponent is not a positive number")
}
pub := &rsa.PublicKey{
E: p.E,
N: p.N,
}
return pub, nil
default:
// DSA and EC not supported everywhere
return nil, nil
}
}
func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) x509.PublicKeyAlgorithm {
if oid.Equal(oidPublicKeyRSA) {
return x509.RSA
}
return x509.UnknownPublicKeyAlgorithm
}
// ParseSPKAC returns the public key from a KEYGEN's SPKAC request
func ParseSPKAC(spkacBase64 string) (pub interface{}, err error) {
var info spkacInfo
derBytes, err := base64.StdEncoding.DecodeString(spkacBase64)
if err != nil {
return nil, err
}
if _, err = asn1.Unmarshal(derBytes, &info); err != nil {
return
}
algo := getPublicKeyAlgorithmFromOID(info.Pkac.PublicKey.Algorithm.Algorithm)
if algo == x509.UnknownPublicKeyAlgorithm {
return nil, errors.New("x509: unknown public key algorithm")
}
pub, err = parsePublicKey(algo, &info.Pkac.PublicKey)
if err != nil {
return
}
return
}
// NewSPKACx509 creates a new x509 self-signed cert based on the SPKAC value
func NewSPKACx509(uri string, name string, spkacBase64 string) ([]byte, error) {
public, err := ParseSPKAC(spkacBase64)
if err != nil {
return nil, err
}
pubKey := public.(*rsa.PublicKey)
rsaPub, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return nil, err
}
h := sha1.New()
pubSha1 := h.Sum(rsaPub)[:20]
priv, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return nil, err
}
template := x509.Certificate{
SerialNumber: new(big.Int).SetInt64(42),
Subject: pkix.Name{
CommonName: name,
Organization: []string{"WebID"},
// Country: []string{"US"},
},
NotBefore: notBefore,
NotAfter: notAfter,
SubjectKeyId: pubSha1,
BasicConstraintsValid: true,
}
// add WebID in the subjectAltName field
var rawValues []asn1.RawValue
rawValues = append(rawValues, asn1.RawValue{Class: 2, Tag: 6, Bytes: []byte(uri)})
values, err := asn1.Marshal(rawValues)
if err != nil {
return nil, err
}
template.ExtraExtensions = []pkix.Extension{{Id: subjectAltName, Value: values}}
template.Extensions = template.ExtraExtensions
certDerBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, public, priv)
return certDerBytes, nil
}
// NewRSAcert creates a new RSA x509 self-signed certificate
func NewRSAcert(uri string, name string, priv *rsa.PrivateKey) (*tls.Certificate, error) {
uri = "URI: " + uri
template := x509.Certificate{
SerialNumber: new(big.Int).SetInt64(42),
Subject: pkix.Name{
CommonName: name,
Organization: []string{"WebID"},
// Country: []string{"US"},
},
NotBefore: notBefore,
NotAfter: notAfter,
BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
rawValues := []asn1.RawValue{
{Class: 2, Tag: 6, Bytes: []byte(uri)},
}
values, err := asn1.Marshal(rawValues)
if err != nil {
return nil, err
}
template.ExtraExtensions = []pkix.Extension{{Id: subjectAltName, Value: values}}
keyPEM := bytes.NewBuffer(nil)
err = pem.Encode(keyPEM, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
if err != nil {
return nil, err
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
return nil, err
}
certPEM := bytes.NewBuffer(nil)
err = pem.Encode(certPEM, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair(certPEM.Bytes(), keyPEM.Bytes())
if err != nil {
return nil, err
}
return &cert, nil
}