forked from certnanny/sscep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileutils.c
405 lines (361 loc) · 10.3 KB
/
fileutils.c
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* sscep -- Simple SCEP client implementation
* Copyright (c) Jarkko Turkulainen 2003. All rights reserved.
* See the file COPYRIGHT for licensing information.
*/
/* Misc. cert/crl manipulation routines */
#include "sscep.h"
/* Open the inner, decrypted PKCS7 and try to write CRL. */
void
write_crl(struct scep *s) {
PKCS7 *p7;
STACK_OF(X509_CRL) *crls;
X509_CRL *crl;
FILE *fp;
/* Get CRL */
p7 = s->reply_p7;
crls = p7->d.sign->crl;
/* We expect only one CRL: */
crl = sk_X509_CRL_value(crls, 0);
if (crl == NULL) {
fprintf(stderr, "%s: cannot find CRL in reply\n", pname);
exit (SCEP_PKISTATUS_FILE);
}
/* Write PEM-formatted file: */
if (!(fp = fopen(w_char, "w"))) {
fprintf(stderr, "%s: cannot open CRL file for writing\n",
pname);
exit (SCEP_PKISTATUS_FILE);
}
if (v_flag)
printf("%s: writing CRL\n", pname);
if (d_flag)
PEM_write_X509_CRL(stdout, crl);
if (PEM_write_X509_CRL(fp, crl) != 1) {
fprintf(stderr, "%s: error while writing CRL "
"file\n", pname);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
printf("%s: CRL written as %s\n", pname, w_char);
(void)fclose(fp);
}
static int
compare_subject(X509 * cert)
{
int rc = X509_NAME_cmp(X509_get_subject_name(cert), X509_REQ_get_subject_name(request));
if (rc)
{
/* X509_NAME_cmp should return 0 when X509_get_subject_name()
* and X509_REQ_get_subject_name() match. There is a bug
* report on that issue (1422).
*
* Assume we cannot trust X509_NAME_cmp() and perform a strcmp()
* when X509_NAME_cmp returns true (which is in fact false ;-))
*/
char cert_buf[1024];
char req_buf[1024];
X509_NAME_oneline(X509_get_subject_name(cert), cert_buf, sizeof(cert_buf));
X509_NAME_oneline(X509_REQ_get_subject_name(request), req_buf, sizeof(req_buf));
if (v_flag)
printf (" X509_NAME_cmp() workaround: strcmp request subject (%s) to cert subject (%s)\n", req_buf, cert_buf);
rc = strcmp (cert_buf, req_buf);
}
return rc;
} /* is_same_cn */
/* Open the inner, decrypted PKCS7 and try to write cert. */
void
write_local_cert(struct scep *s) {
PKCS7 *p7;
STACK_OF(X509) *certs;
X509 *cert = NULL;
FILE *fp;
int i;
localcert = NULL;
/* Get certs */
p7 = s->reply_p7;
certs = p7->d.sign->cert;
if (v_flag) {
printf ("write_local_cert(): found %d cert(s)\n", sk_X509_num(certs));
}
/* Find cert */
for (i = 0; i < sk_X509_num(certs); i++) {
char buffer[1024];
cert = sk_X509_value(certs, i);
if (v_flag) {
printf("%s: found certificate with\n"
" subject: '%s'\n", pname,
X509_NAME_oneline(X509_get_subject_name(cert),
buffer, sizeof(buffer)));
printf(" issuer: %s\n",
X509_NAME_oneline(X509_get_issuer_name(cert),
buffer, sizeof(buffer)));
printf(" request_subject: '%s'\n",
X509_NAME_oneline(X509_REQ_get_subject_name(request),
buffer, sizeof(buffer)));
}
/* The subject has to match that of our request */
if (!compare_subject(cert)) {
if (v_flag)
printf ("CN's of request and certificate matched!\n");
/* The subject cannot be the issuer (selfsigned) */
if (X509_NAME_cmp(X509_get_subject_name(cert),
X509_get_issuer_name(cert))) {
localcert = cert;
break;
}
}
}
if (localcert == NULL) {
fprintf(stderr, "%s: cannot find requested certificate\n",
pname);
exit (SCEP_PKISTATUS_FILE);
}
/* Write PEM-formatted file: */
if (!(fp = fopen(l_char, "w"))) {
fprintf(stderr, "%s: cannot open cert file for writing\n",
pname);
exit (SCEP_PKISTATUS_FILE);
}
if (v_flag)
printf("%s: writing cert\n", pname);
if (d_flag)
PEM_write_X509(stdout, localcert);
if (PEM_write_X509(fp, localcert) != 1) {
fprintf(stderr, "%s: error while writing certificate "
"file\n", pname);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
printf("%s: certificate written as %s\n", pname, l_char);
(void)fclose(fp);
}
/* Open the inner, decrypted PKCS7 and try to write cert. */
void
write_other_cert(struct scep *s) {
PKCS7 *p7;
STACK_OF(X509) *certs;
X509 *cert = NULL;
FILE *fp;
int i;
othercert = NULL;
/* Get certs */
p7 = s->reply_p7;
certs = p7->d.sign->cert;
/* Find cert */
for (i = 0; i < sk_X509_num(certs); i++) {
char buffer[1024];
cert = sk_X509_value(certs, i);
if (v_flag) {
printf("%s: found certificate with\n"
" subject: %s\n", pname,
X509_NAME_oneline(X509_get_subject_name(cert),
buffer, sizeof(buffer)));
printf(" issuer: %s\n",
X509_NAME_oneline(X509_get_issuer_name(cert),
buffer, sizeof(buffer)));
}
/* The serial has to match to requested one */
if (!ASN1_INTEGER_cmp(X509_get_serialNumber(cert),
s->ias_getcert->serial)) {
othercert = cert;
break;
}
}
if (othercert == NULL) {
fprintf(stderr, "%s: cannot find certificate\n", pname);
exit (SCEP_PKISTATUS_FILE);
}
/* Write PEM-formatted file: */
if (!(fp = fopen(w_char, "w"))) {
fprintf(stderr, "%s: cannot open cert file for writing\n",
pname);
exit (SCEP_PKISTATUS_FILE);
}
if (v_flag)
printf("%s: writing cert\n", pname);
if (d_flag)
PEM_write_X509(stdout, othercert);
if (PEM_write_X509(fp, othercert) != 1) {
fprintf(stderr, "%s: error while writing certificate "
"file\n", pname);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
printf("%s: certificate written as %s\n", pname, w_char);
(void)fclose(fp);
}
/*
* Open the inner, decrypted PKCS7 and try to write CA/RA certificates
*/
int
write_ca_ra(struct http_reply *s) {
BIO *bio;
PKCS7 *p7;
STACK_OF(X509) *certs = NULL;
X509 *cert = NULL;
FILE *fp = NULL;
int c, i, index;
unsigned int n;
unsigned char md[EVP_MAX_MD_SIZE];
X509_EXTENSION *ext;
/* Create read-only memory bio */
bio = BIO_new_mem_buf(s->payload, s->bytes);
p7 = d2i_PKCS7_bio(bio, NULL);
if (p7 == NULL) {
fprintf(stderr, "%s: error reading PKCS#7 data\n", pname);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
/* Get certs */
i = OBJ_obj2nid(p7->type);
switch (i) {
case NID_pkcs7_signed:
certs = p7->d.sign->cert;
break;
default:
printf("%s: wrong PKCS#7 type\n", pname);
exit (SCEP_PKISTATUS_FILE);
}
/* Check */
if (certs == NULL) {
fprintf(stderr, "%s: cannot find certificates\n", pname);
exit (SCEP_PKISTATUS_FILE);
}
/* Verify the chain
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
/* Find cert */
for (i = 0; i < sk_X509_num(certs); i++) {
char buffer[1024];
char name[1024];
memset(buffer, 0, 1024);
memset(name, 0, 1024);
cert = sk_X509_value(certs, i);
/* Create name */
snprintf(name, 1024, "%s-%d", c_char, i);
/* Read and print certificate information */
printf("\n%s: found certificate with\n subject: %s\n", pname,
X509_NAME_oneline(X509_get_subject_name(cert),
buffer, sizeof(buffer)));
printf(" issuer: %s\n",
X509_NAME_oneline(X509_get_issuer_name(cert),
buffer, sizeof(buffer)));
if (!X509_digest(cert, fp_alg, md, &n)) {
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
/* Print key usage: */
index = X509_get_ext_by_NID(cert, NID_key_usage, -1);
if (index < 0) {
if (v_flag)
fprintf(stderr, "%s: cannot find key usage\n",
pname);
/* exit (SCEP_PKISTATUS_FILE); */
} else {
ext = X509_get_ext(cert, index);
printf(" usage: ");
X509V3_EXT_print_fp(stdout, ext, 0, 0);
printf("\n");
}
printf(" %s fingerprint: ", OBJ_nid2sn(EVP_MD_type(fp_alg)));
for (c = 0; c < (int)n; c++) {
printf("%02X%c",md[c], (c + 1 == (int)n) ?'\n':':');
}
/* Write PEM-formatted file: */
if (!(fp = fopen(name, "w"))) {
fprintf(stderr, "%s: cannot open cert file for "
"writing\n", pname);
exit (SCEP_PKISTATUS_FILE);
}
if (v_flag)
printf("%s: writing cert\n", pname);
if (d_flag)
PEM_write_X509(stdout, cert);
if (PEM_write_X509(fp, cert) != 1) {
fprintf(stderr, "%s: error while writing certificate "
"file\n", pname);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
printf("%s: certificate written as %s\n", pname, name);
}
(void)fclose(fp);
exit (SCEP_PKISTATUS_SUCCESS);
}
/* Read CA cert and optionally, encyption CA cert */
void
read_ca_cert(void) {
/* Read CA cert file */
if (!c_flag || !(cafile = fopen(c_char, "r"))) {
fprintf(stderr, "%s: cannot open CA cert file\n", pname);
exit (SCEP_PKISTATUS_FILE);
}
if (!PEM_read_X509(cafile, &cacert, NULL, NULL)) {
fprintf(stderr, "%s: error while reading CA cert\n", pname);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
fclose(cafile);
/* Read enc CA cert */
if (e_flag) {
if (!(cafile = fopen(e_char, "r"))) {
fprintf(stderr, "%s: cannot open enc CA cert file\n",
pname);
exit (SCEP_PKISTATUS_FILE);
}
if (!PEM_read_X509(cafile, &encert, NULL, NULL)) {
fprintf(stderr,"%s: error while reading enc CA cert\n",
pname);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
fclose(cafile);
}
}
/* Read local certificate (GetCert and GetCrl) */
void
read_cert(X509** cert, char* filename) {
FILE *file;
if (!(file = fopen(filename, "r"))) {
fprintf(stderr, "%s: cannot open cert file %s\n", pname, filename);
exit (SCEP_PKISTATUS_FILE);
}
if (!PEM_read_X509(file, cert, NULL, NULL)) {
fprintf(stderr, "%s: error while reading cert %s\n", pname, filename);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
fclose(file);
}
/* Read private key */
void
read_key(EVP_PKEY** key, char* filename) {
FILE *file;
/* Read private key file */
if (!(file = fopen(filename, "r"))) {
fprintf(stderr, "%s: cannot open private key file %s\n", pname, filename);
exit (SCEP_PKISTATUS_FILE);
}
if (!PEM_read_PrivateKey(file, key, NULL, NULL)) {
fprintf(stderr, "%s: error while reading private key %s\n", pname, filename);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
fclose(file);
}
/* Read PKCS#10 request */
void
read_request(void) {
/* Read certificate request file */
if (!r_flag || !(reqfile = fopen(r_char, "r"))) {
fprintf(stderr, "%s: cannot open certificate request\n", pname);
exit (SCEP_PKISTATUS_FILE);
}
if (!PEM_read_X509_REQ(reqfile, &request, NULL, NULL)) {
fprintf(stderr, "%s: error while reading request file\n", pname);
ERR_print_errors_fp(stderr);
exit (SCEP_PKISTATUS_FILE);
}
fclose(reqfile);
}