This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
forked from smallstep/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
205 lines (194 loc) · 8.87 KB
/
convert.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
package x509util
import (
"crypto/x509"
"github.com/pkg/errors"
stepx509 "github.com/smallstep/cli/pkg/x509"
)
// ParseCertificate parses a single certificate from the given ASN.1 DER data.
func ParseCertificate(asn1Data []byte) (*x509.Certificate, error) {
cert, err := stepx509.ParseCertificate(asn1Data)
if err != nil {
return nil, errors.Wrap(err, "error parsing certificate")
}
return ToX509Certificate(cert), nil
}
// ParseCertificateRequest parses a single certificate request from the given
// ASN.1 DER data.
func ParseCertificateRequest(asn1Data []byte) (*x509.CertificateRequest, error) {
csr, err := stepx509.ParseCertificateRequest(asn1Data)
if err != nil {
return nil, errors.Wrap(err, "error parsing certificate request")
}
return ToX509CertificateRequest(csr), nil
}
// CheckCertificateRequestSignature verifies that signature is a valid signature
// over signed from csr's public key.
//
// CheckCertificateRequestSignature reports whether the signature on csr is
// valid.
func CheckCertificateRequestSignature(csr *x509.CertificateRequest) error {
if stepx509.SignatureAlgorithm(csr.SignatureAlgorithm) == stepx509.ED25519SIG {
return ToStepX509CertificateRequest(csr).CheckSignature()
}
return csr.CheckSignature()
}
// ToStepX509Certificate converts a x509.Certificate from the standard library
// to the step version of the x509.Certificate.
func ToStepX509Certificate(cert *x509.Certificate) *stepx509.Certificate {
return &stepx509.Certificate{
Raw: cert.Raw,
RawTBSCertificate: cert.RawTBSCertificate,
RawSubjectPublicKeyInfo: cert.RawSubjectPublicKeyInfo,
RawSubject: cert.RawSubject,
RawIssuer: cert.RawIssuer,
Signature: cert.Signature,
SignatureAlgorithm: stepx509.SignatureAlgorithm(cert.SignatureAlgorithm),
PublicKeyAlgorithm: stepx509.PublicKeyAlgorithm(cert.PublicKeyAlgorithm),
PublicKey: cert.PublicKey,
Version: cert.Version,
SerialNumber: cert.SerialNumber,
Issuer: cert.Issuer,
Subject: cert.Subject,
NotBefore: cert.NotBefore,
NotAfter: cert.NotAfter,
KeyUsage: stepx509.KeyUsage(cert.KeyUsage),
Extensions: cert.Extensions,
ExtraExtensions: cert.ExtraExtensions,
UnhandledCriticalExtensions: cert.UnhandledCriticalExtensions,
ExtKeyUsage: toStepExtKeyUsage(cert.ExtKeyUsage),
UnknownExtKeyUsage: cert.UnknownExtKeyUsage,
BasicConstraintsValid: cert.BasicConstraintsValid,
IsCA: cert.IsCA,
MaxPathLen: cert.MaxPathLen,
MaxPathLenZero: cert.MaxPathLenZero,
SubjectKeyId: cert.SubjectKeyId,
AuthorityKeyId: cert.AuthorityKeyId,
OCSPServer: cert.OCSPServer,
IssuingCertificateURL: cert.IssuingCertificateURL,
DNSNames: cert.DNSNames,
EmailAddresses: cert.EmailAddresses,
IPAddresses: cert.IPAddresses,
URIs: cert.URIs,
PermittedDNSDomainsCritical: cert.PermittedDNSDomainsCritical,
PermittedDNSDomains: cert.PermittedDNSDomains,
ExcludedDNSDomains: cert.ExcludedDNSDomains,
PermittedIPRanges: cert.PermittedIPRanges,
ExcludedIPRanges: cert.ExcludedIPRanges,
PermittedEmailAddresses: cert.PermittedEmailAddresses,
ExcludedEmailAddresses: cert.ExcludedEmailAddresses,
PermittedURIDomains: cert.PermittedURIDomains,
ExcludedURIDomains: cert.ExcludedURIDomains,
CRLDistributionPoints: cert.CRLDistributionPoints,
PolicyIdentifiers: cert.PolicyIdentifiers,
}
}
func toStepExtKeyUsage(eku []x509.ExtKeyUsage) []stepx509.ExtKeyUsage {
var ret []stepx509.ExtKeyUsage
for _, u := range eku {
ret = append(ret, stepx509.ExtKeyUsage(u))
}
return ret
}
// ToX509Certificate converts a x509.Certificate from the internal package to
// the standard version of the x509.Certificate.
func ToX509Certificate(cert *stepx509.Certificate) *x509.Certificate {
return &x509.Certificate{
Raw: cert.Raw,
RawTBSCertificate: cert.RawTBSCertificate,
RawSubjectPublicKeyInfo: cert.RawSubjectPublicKeyInfo,
RawSubject: cert.RawSubject,
RawIssuer: cert.RawIssuer,
Signature: cert.Signature,
SignatureAlgorithm: x509.SignatureAlgorithm(cert.SignatureAlgorithm),
PublicKeyAlgorithm: x509.PublicKeyAlgorithm(cert.PublicKeyAlgorithm),
PublicKey: cert.PublicKey,
Version: cert.Version,
SerialNumber: cert.SerialNumber,
Issuer: cert.Issuer,
Subject: cert.Subject,
NotBefore: cert.NotBefore,
NotAfter: cert.NotAfter,
KeyUsage: x509.KeyUsage(cert.KeyUsage),
Extensions: cert.Extensions,
ExtraExtensions: cert.ExtraExtensions,
UnhandledCriticalExtensions: cert.UnhandledCriticalExtensions,
ExtKeyUsage: toExtKeyUsage(cert.ExtKeyUsage),
UnknownExtKeyUsage: cert.UnknownExtKeyUsage,
BasicConstraintsValid: cert.BasicConstraintsValid,
IsCA: cert.IsCA,
MaxPathLen: cert.MaxPathLen,
MaxPathLenZero: cert.MaxPathLenZero,
SubjectKeyId: cert.SubjectKeyId,
AuthorityKeyId: cert.AuthorityKeyId,
OCSPServer: cert.OCSPServer,
IssuingCertificateURL: cert.IssuingCertificateURL,
DNSNames: cert.DNSNames,
EmailAddresses: cert.EmailAddresses,
IPAddresses: cert.IPAddresses,
URIs: cert.URIs,
PermittedDNSDomainsCritical: cert.PermittedDNSDomainsCritical,
PermittedDNSDomains: cert.PermittedDNSDomains,
ExcludedDNSDomains: cert.ExcludedDNSDomains,
PermittedIPRanges: cert.PermittedIPRanges,
ExcludedIPRanges: cert.ExcludedIPRanges,
PermittedEmailAddresses: cert.PermittedEmailAddresses,
ExcludedEmailAddresses: cert.ExcludedEmailAddresses,
PermittedURIDomains: cert.PermittedURIDomains,
ExcludedURIDomains: cert.ExcludedURIDomains,
CRLDistributionPoints: cert.CRLDistributionPoints,
PolicyIdentifiers: cert.PolicyIdentifiers,
}
}
func toExtKeyUsage(eku []stepx509.ExtKeyUsage) []x509.ExtKeyUsage {
var ret []x509.ExtKeyUsage
for _, u := range eku {
ret = append(ret, x509.ExtKeyUsage(u))
}
return ret
}
// ToStepX509CertificateRequest converts a x509.CertificateRequest from the standard library
// to the step version of the x509.CertificateRequest.
func ToStepX509CertificateRequest(csr *x509.CertificateRequest) *stepx509.CertificateRequest {
return &stepx509.CertificateRequest{
Raw: csr.Raw,
RawTBSCertificateRequest: csr.RawTBSCertificateRequest,
RawSubjectPublicKeyInfo: csr.RawSubjectPublicKeyInfo,
RawSubject: csr.RawSubject,
Version: csr.Version,
Signature: csr.Signature,
SignatureAlgorithm: stepx509.SignatureAlgorithm(csr.SignatureAlgorithm),
PublicKeyAlgorithm: stepx509.PublicKeyAlgorithm(csr.PublicKeyAlgorithm),
PublicKey: csr.PublicKey,
Subject: csr.Subject,
Attributes: csr.Attributes,
Extensions: csr.Extensions,
ExtraExtensions: csr.ExtraExtensions,
DNSNames: csr.DNSNames,
EmailAddresses: csr.EmailAddresses,
IPAddresses: csr.IPAddresses,
URIs: csr.URIs,
}
}
// ToX509CertificateRequest converts a x509.CertificateRequest from the internal package to
// the standard version of the x509.CertificateRequest.
func ToX509CertificateRequest(csr *stepx509.CertificateRequest) *x509.CertificateRequest {
return &x509.CertificateRequest{
Raw: csr.Raw,
RawTBSCertificateRequest: csr.RawTBSCertificateRequest,
RawSubjectPublicKeyInfo: csr.RawSubjectPublicKeyInfo,
RawSubject: csr.RawSubject,
Version: csr.Version,
Signature: csr.Signature,
SignatureAlgorithm: x509.SignatureAlgorithm(csr.SignatureAlgorithm),
PublicKeyAlgorithm: x509.PublicKeyAlgorithm(csr.PublicKeyAlgorithm),
PublicKey: csr.PublicKey,
Subject: csr.Subject,
Attributes: csr.Attributes,
Extensions: csr.Extensions,
ExtraExtensions: csr.ExtraExtensions,
DNSNames: csr.DNSNames,
EmailAddresses: csr.EmailAddresses,
IPAddresses: csr.IPAddresses,
URIs: csr.URIs,
}
}