-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate.go
355 lines (289 loc) · 8.89 KB
/
certificate.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package ejbca
import (
"crypto/x509"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"strconv"
)
func (c *RESTClient) FinalizeCertificateEnrollment(requestId int, password string) (*CertificateData, error) {
log.Printf("[INFO] Finalizing certificate enrollment for request ID %d", requestId)
endpoint := fmt.Sprintf("v1/certificate/%d/finalize", requestId)
payload := &finalizeCertificateEnrollment{
ResponseFormat: "DER",
Password: password,
}
requestConfig := &request{
Method: "POST",
Endpoint: endpoint,
Payload: payload,
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, err
}
jsonResp := &certificateDataResponse{}
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, err
}
data, err := serializeCertificateResponse(jsonResp)
if err != nil {
return nil, err
}
return data, nil
}
func (c *RESTClient) RevokeCertificate(rca *RevokeCertificate) (*RevokeCertificateResponse, error) {
log.Printf("[INFO] Revoking certificate with serial number %s", rca.CertificateSerialNumber)
endpoint := fmt.Sprintf("v1/certificate/%s/%s/revoke", rca.IssuerDn, rca.CertificateSerialNumber)
// Construct query if reason and/or date were specified
query := make(map[string]string)
if rca.Reason != "" {
query["reason"] = rca.Reason
}
if rca.Date != "" {
query["date"] = rca.Date
}
requestConfig := &request{
Method: "PUT",
Endpoint: endpoint,
Query: query,
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, err
}
jsonResp := &RevokeCertificateResponse{}
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, err
}
return jsonResp, nil
}
func (c *RESTClient) EnrollPKCS10(enrollment *PKCS10CSREnrollment) (*CertificateData, error) {
log.Println("[INFO] Enrolling PKCS10 certificate")
if enrollment.CertificateProfileName == "" {
if c.defaultCertificateProfileName == "" {
return nil, fmt.Errorf("certificate profile is required")
} else {
enrollment.CertificateProfileName = c.defaultCertificateProfileName
}
}
if enrollment.EndEntityProfileName == "" {
if c.defaultEndEntityProfileName == "" {
return nil, fmt.Errorf("end entity profile is required")
} else {
enrollment.EndEntityProfileName = c.defaultEndEntityProfileName
}
}
if enrollment.CertificateAuthorityName == "" {
if c.defaultCertificateAuthorityName == "" {
return nil, fmt.Errorf("certificate authority name is required")
} else {
enrollment.CertificateAuthorityName = c.defaultCertificateAuthorityName
}
}
requestConfig := &request{
Method: "POST",
Endpoint: "v1/certificate/pkcs10enroll",
Payload: enrollment,
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, err
}
jsonResp := &certificateDataResponse{}
err = json.NewDecoder(resp.Body).Decode(jsonResp)
if err != nil {
return nil, err
}
return serializeCertificateResponse(jsonResp)
}
// CheckRevocationStatus checks if the certificate issued by issuerDn with serial number certificateSerialNumber is revoked.
// Give certificateSerialNumber a hex encoded serial number. IE hex representation of a really large unsigned integer.
func (c *RESTClient) CheckRevocationStatus(issuerDn string, certificateSerialNumber string) (*GetRevocationStatusResponse, error) {
log.Printf("[INFO] Checking revocation status of certificate wtih serial number %s issued by CA with DN %s", certificateSerialNumber, issuerDn)
endpoint := fmt.Sprintf("v1/certificate/%s/%s/revocationstatus", issuerDn, certificateSerialNumber)
requestConfig := &request{
Method: "GET",
Endpoint: endpoint,
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, err
}
jsonResp := &GetRevocationStatusResponse{}
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, err
}
return jsonResp, nil
}
func (c *RESTClient) EnrollKeystore(keystore *EnrollKeystore) (*CertificateData, error) {
log.Printf("[INFO] Enrolling keystore with algorithm %s", keystore.KeyAlg)
requestConfig := &request{
Method: "POST",
Endpoint: "v1/certificate/enrollkeystore",
Payload: keystore,
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, err
}
jsonResp := &certificateDataResponse{}
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, err
}
return serializeCertificateResponse(jsonResp)
}
func (c *RESTClient) GetExpiringCertificates(days int, offset int, maxNumberOfResults int) (*ExpiringCertificates, error) {
log.Println("[INFO] Getting list of expiring certificates")
// Construct query if reason and/or date were specified
query := make(map[string]string)
query["days"] = strconv.Itoa(days)
query["offset"] = strconv.Itoa(offset)
if maxNumberOfResults > 0 {
query["maxNumberOfResults"] = strconv.Itoa(maxNumberOfResults)
}
requestConfig := &request{
Method: "GET",
Endpoint: "v1/certificate/expire",
Query: query,
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, err
}
jsonResp := &expiringCertificatesResp{}
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, err
}
var data []*CertificateData
for _, cert := range jsonResp.CertificatesRestResponse.Certificates {
var leaf *CertificateData
leaf, err = serializeCertificateResponse(&cert)
if err != nil {
return nil, err
}
data = append(data, leaf)
}
expiringCerts := new(ExpiringCertificates)
expiringCerts.PaginationRestResponseComponent = jsonResp.PaginationRestResponseComponent
expiringCerts.CertificatesRestResponse = data
return expiringCerts, nil
}
func (c *RESTClient) EnrollCertificateRequest(certificateRequest *EnrollCertificateRequest) (*CertificateData, error) {
log.Println("[INFO] Enrolling certificate request")
requestConfig := &request{
Method: "POST",
Endpoint: "v1/certificate/certificaterequest",
Payload: certificateRequest,
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, err
}
jsonResp := &certificateDataResponse{}
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, err
}
return serializeCertificateResponse(jsonResp)
}
func (c *RESTClient) SearchCertificates(criteria *SearchCertificate) ([]*CertificateData, bool, error) {
log.Printf("[INFO] Searching EJBCA for certificates with criteria %x", criteria)
requestConfig := &request{
Method: "POST",
Endpoint: "v1/certificate/search",
Payload: criteria,
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, false, err
}
jsonResp := &searchCertificateCriteriaResponse{}
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, false, err
}
var data []*CertificateData
for _, cert := range jsonResp.Certificates {
var temp *CertificateData
temp, err = serializeCertificateResponse(&cert)
if err != nil {
return nil, false, err
}
data = append(data, temp)
}
return data, jsonResp.MoreResults, nil
}
func (c *RESTClient) GetV1CertificateStatus() (*V1CertificateEndpointStatus, error) {
log.Println("[INFO] Getting EJBCA V1 Certificate Endpoint Status")
requestConfig := &request{
Method: "GET",
Endpoint: "v1/certificate/status",
}
resp, err := c.sendRESTRequest(requestConfig)
if err != nil {
return nil, err
}
jsonResp := &V1CertificateEndpointStatus{}
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, err
}
return jsonResp, nil
}
func serializeCertificateResponse(resp *certificateDataResponse) (*CertificateData, error) {
data := new(CertificateData)
var err error
var certificates []*x509.Certificate
if resp.ResponseFormat == "DER" {
// First, extract leaf
var decoded []byte
if resp.Certificate != "" {
// Certificate is usually base64 encoded PEM
decoded, err = base64.StdEncoding.DecodeString(resp.Certificate)
if err != nil {
return nil, err
}
certificates, err = x509.ParseCertificates(decoded)
if err != nil {
// Sometimes, certificate response is base64 encoded twice. Before returning error, try decoding again
decoded, err = base64.StdEncoding.DecodeString(string(decoded))
if err != nil {
return nil, err
}
certificates, err = x509.ParseCertificates(decoded)
if err != nil {
return nil, err
}
}
if len(certificates) > 0 {
data.Certificate = certificates[0]
}
}
if len(resp.CertificateChain) > 0 {
// Then, extract chain
for _, cert := range resp.CertificateChain {
decoded, err = base64.StdEncoding.DecodeString(cert)
if err != nil {
return nil, err
}
var i *x509.Certificate
i, err = x509.ParseCertificate(decoded)
if err != nil {
return nil, err
}
data.CertificateChain = append(data.CertificateChain, i)
}
}
}
data.SerialNumber = resp.SerialNumber
data.CertificateProfile = resp.CertificateProfile
data.EndEntityProfile = resp.EndEntityProfile
return data, nil
}