-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
x509.go
275 lines (245 loc) · 7.17 KB
/
x509.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2019 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package x509
import (
"context"
"crypto/dsa"
"crypto/ecdsa"
"crypto/rsa"
"crypto/sha1" // #nosec G505
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/loadimpact/k6/js/common"
)
// X509 certificate functionality
type X509 struct{}
// Certificate is an X.509 certificate
type Certificate struct {
Subject Subject
Issuer Issuer
NotBefore string `js:"notBefore"`
NotAfter string `js:"notAfter"`
AltNames []string `js:"altNames"`
SignatureAlgorithm string `js:"signatureAlgorithm"`
FingerPrint []byte `js:"fingerPrint"`
PublicKey PublicKey `js:"publicKey"`
}
// RDN is a component of an X.509 distinguished name
type RDN struct {
Type string
Value string
}
// Subject is a certificate subject
type Subject struct {
CommonName string `js:"commonName"`
Country string
PostalCode string `js:"postalCode"`
StateOrProvinceName string `js:"stateOrProvinceName"`
LocalityName string `js:"localityName"`
StreetAddress string `js:"streetAddress"`
OrganizationName string `js:"organizationName"`
OrganizationalUnitName []string `js:"organizationalUnitName"`
Names []RDN
}
// Issuer is a certificate issuer
type Issuer struct {
CommonName string `js:"commonName"`
Country string
StateOrProvinceName string `js:"stateOrProvinceName"`
LocalityName string `js:"localityName"`
OrganizationName string `js:"organizationName"`
Names []RDN
}
// PublicKey is used for decryption and signature verification
type PublicKey struct {
Algorithm string
Key interface{}
}
// New constructs the X509 interface
func New() *X509 {
return &X509{}
}
// Parse produces an entire X.509 certificate
func (X509) Parse(ctx context.Context, encoded []byte) Certificate {
parsed, err := parseCertificate(encoded)
if err != nil {
throw(ctx, err)
}
certificate, err := makeCertificate(parsed)
if err != nil {
throw(ctx, err)
}
return certificate
}
// GetAltNames extracts alt names
func (X509) GetAltNames(ctx context.Context, encoded []byte) []string {
parsed, err := parseCertificate(encoded)
if err != nil {
throw(ctx, err)
}
return altNames(parsed)
}
// GetIssuer extracts certificate issuer
func (X509) GetIssuer(ctx context.Context, encoded []byte) Issuer {
parsed, err := parseCertificate(encoded)
if err != nil {
throw(ctx, err)
}
return makeIssuer(parsed.Issuer)
}
// GetSubject extracts certificate subject
func (X509) GetSubject(ctx context.Context, encoded []byte) Subject {
parsed, err := parseCertificate(encoded)
if err != nil {
throw(ctx, err)
}
return makeSubject(parsed.Subject)
}
func parseCertificate(encoded []byte) (*x509.Certificate, error) {
decoded, _ := pem.Decode(encoded)
if decoded == nil {
err := errors.New("failed to decode certificate PEM file")
return nil, err
}
parsed, err := x509.ParseCertificate(decoded.Bytes)
if err != nil {
err = errors.Wrap(err, "failed to parse certificate")
return nil, err
}
return parsed, nil
}
func makeCertificate(parsed *x509.Certificate) (Certificate, error) {
publicKey, err := makePublicKey(parsed.PublicKey)
if err != nil {
return Certificate{}, err
}
return Certificate{
Subject: makeSubject(parsed.Subject),
Issuer: makeIssuer(parsed.Issuer),
NotBefore: iso8601(parsed.NotBefore),
NotAfter: iso8601(parsed.NotAfter),
AltNames: altNames(parsed),
SignatureAlgorithm: signatureAlgorithm(parsed.SignatureAlgorithm),
FingerPrint: fingerPrint(parsed),
PublicKey: publicKey,
}, nil
}
func makeSubject(subject pkix.Name) Subject {
return Subject{
CommonName: subject.CommonName,
Country: first(subject.Country),
PostalCode: first(subject.PostalCode),
StateOrProvinceName: first(subject.Province),
LocalityName: first(subject.Locality),
StreetAddress: first(subject.StreetAddress),
OrganizationName: first(subject.Organization),
OrganizationalUnitName: subject.OrganizationalUnit,
Names: makeRdns(subject.Names),
}
}
func makeIssuer(issuer pkix.Name) Issuer {
return Issuer{
CommonName: issuer.CommonName,
Country: first(issuer.Country),
StateOrProvinceName: first(issuer.Province),
LocalityName: first(issuer.Locality),
OrganizationName: first(issuer.Organization),
Names: makeRdns(issuer.Names),
}
}
func makePublicKey(parsed interface{}) (PublicKey, error) {
var algorithm string
switch parsed.(type) {
case *dsa.PublicKey:
algorithm = "DSA"
case *ecdsa.PublicKey:
algorithm = "ECDSA"
case *rsa.PublicKey:
algorithm = "RSA"
default:
err := errors.New("unsupported public key algorithm")
return PublicKey{}, err
}
return PublicKey{
Algorithm: algorithm,
Key: parsed,
}, nil
}
func first(values []string) string {
if len(values) > 0 {
return values[0]
}
return ""
}
func iso8601(value time.Time) string {
return value.Format(time.RFC3339)
}
func makeRdns(names []pkix.AttributeTypeAndValue) []RDN {
var result = make([]RDN, len(names))
for i, name := range names {
result[i] = makeRdn(name)
}
return result
}
func makeRdn(name pkix.AttributeTypeAndValue) RDN {
return RDN{
Type: name.Type.String(),
Value: fmt.Sprintf("%v", name.Value),
}
}
func altNames(parsed *x509.Certificate) []string {
var names []string
names = append(names, parsed.DNSNames...)
names = append(names, parsed.EmailAddresses...)
names = append(names, ipAddresses(parsed)...)
names = append(names, uris(parsed)...)
return names
}
func ipAddresses(parsed *x509.Certificate) []string {
strings := make([]string, len(parsed.IPAddresses))
for i, item := range parsed.IPAddresses {
strings[i] = item.String()
}
return strings
}
func uris(parsed *x509.Certificate) []string {
strings := make([]string, len(parsed.URIs))
for i, item := range parsed.URIs {
strings[i] = item.String()
}
return strings
}
func signatureAlgorithm(value x509.SignatureAlgorithm) string {
if value == x509.UnknownSignatureAlgorithm {
return "UnknownSignatureAlgorithm"
}
return value.String()
}
func fingerPrint(parsed *x509.Certificate) []byte {
bytes := sha1.Sum(parsed.Raw) // #nosec G401
return bytes[:]
}
func throw(ctx context.Context, err error) {
common.Throw(common.GetRuntime(ctx), err)
}