1 /*
2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "apps.h"
14 #include "progs.h"
15 #include <openssl/bio.h>
16 #include <openssl/asn1.h>
17 #include <openssl/err.h>
18 #include <openssl/bn.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
21 #include <openssl/x509v3.h>
22 #include <openssl/objects.h>
23 #include <openssl/pem.h>
24 #include <openssl/rsa.h>
25 #ifndef OPENSSL_NO_DSA
26 # include <openssl/dsa.h>
27 #endif
28 #include "internal/e_os.h" /* For isatty() */
29
30 #undef POSTFIX
31 #define POSTFIX ".srl"
32 #define DEFAULT_DAYS 30 /* default certificate validity period in days */
33 #define UNSET_DAYS -2 /* -1 may be used for testing expiration checks */
34 #define EXT_COPY_UNSET -1
35
36 static int callb(int ok, X509_STORE_CTX *ctx);
37 static ASN1_INTEGER *x509_load_serial(const char *CAfile,
38 const char *serialfile, int create);
39 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
40 static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names);
41
42 typedef enum OPTION_choice {
43 OPT_COMMON,
44 OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM,
45 OPT_CAKEYFORM, OPT_VFYOPT, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE,
46 OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_KEY, OPT_SIGNKEY, OPT_CA, OPT_CAKEY,
47 OPT_CASERIAL, OPT_SET_SERIAL, OPT_NEW, OPT_FORCE_PUBKEY, OPT_ISSU, OPT_SUBJ,
48 OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_DATEOPT, OPT_NAMEOPT,
49 OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL,
50 OPT_MODULUS, OPT_MULTI, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH,
51 OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES,
52 OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST,
53 OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST,
54 OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID,
55 OPT_SUBJECT_HASH_OLD, OPT_ISSUER_HASH_OLD, OPT_COPY_EXTENSIONS,
56 OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT, OPT_PRESERVE_DATES,
57 OPT_NOT_BEFORE, OPT_NOT_AFTER,
58 OPT_R_ENUM, OPT_PROV_ENUM, OPT_EXT
59 } OPTION_CHOICE;
60
61 const OPTIONS x509_options[] = {
62 OPT_SECTION("General"),
63 {"help", OPT_HELP, '-', "Display this summary"},
64
65 {"in", OPT_IN, '<',
66 "Certificate input, or CSR input file with -req (default stdin)"},
67 {"passin", OPT_PASSIN, 's', "Private key and cert file pass-phrase source"},
68 {"new", OPT_NEW, '-', "Generate a certificate from scratch"},
69 {"x509toreq", OPT_X509TOREQ, '-',
70 "Output a certification request (rather than a certificate)"},
71 {"req", OPT_REQ, '-', "Input is a CSR file (rather than a certificate)"},
72 {"copy_extensions", OPT_COPY_EXTENSIONS, 's',
73 "copy extensions when converting from CSR to x509 or vice versa"},
74 {"inform", OPT_INFORM, 'f',
75 "CSR input format to use (PEM or DER; by default try PEM first)"},
76 {"vfyopt", OPT_VFYOPT, 's', "CSR verification parameter in n:v form"},
77 {"key", OPT_KEY, 's',
78 "Key for signing, and to include unless using -force_pubkey"},
79 {"signkey", OPT_SIGNKEY, 's',
80 "Same as -key"},
81 {"keyform", OPT_KEYFORM, 'E',
82 "Key input format (ENGINE, other values ignored)"},
83 {"out", OPT_OUT, '>', "Output file - default stdout"},
84 {"outform", OPT_OUTFORM, 'f',
85 "Output format (DER or PEM) - default PEM"},
86 {"nocert", OPT_NOCERT, '-',
87 "No cert output (except for requested printing)"},
88 {"noout", OPT_NOOUT, '-', "No output (except for requested printing)"},
89
90 OPT_SECTION("Certificate printing"),
91 {"text", OPT_TEXT, '-', "Print the certificate in text form"},
92 {"dateopt", OPT_DATEOPT, 's',
93 "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."},
94 {"certopt", OPT_CERTOPT, 's', "Various certificate text printing options"},
95 {"fingerprint", OPT_FINGERPRINT, '-', "Print the certificate fingerprint"},
96 {"alias", OPT_ALIAS, '-', "Print certificate alias"},
97 {"serial", OPT_SERIAL, '-', "Print serial number value"},
98 {"startdate", OPT_STARTDATE, '-', "Print the notBefore field"},
99 {"enddate", OPT_ENDDATE, '-', "Print the notAfter field"},
100 {"dates", OPT_DATES, '-', "Print both notBefore and notAfter fields"},
101 {"subject", OPT_SUBJECT, '-', "Print subject DN"},
102 {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
103 {"nameopt", OPT_NAMEOPT, 's',
104 "Certificate subject/issuer name printing options"},
105 {"email", OPT_EMAIL, '-', "Print email address(es)"},
106 {"hash", OPT_HASH, '-', "Synonym for -subject_hash (for backward compat)"},
107 {"subject_hash", OPT_HASH, '-', "Print subject hash value"},
108 #ifndef OPENSSL_NO_MD5
109 {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-',
110 "Print old-style (MD5) subject hash value"},
111 #endif
112 {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"},
113 #ifndef OPENSSL_NO_MD5
114 {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-',
115 "Print old-style (MD5) issuer hash value"},
116 #endif
117 {"ext", OPT_EXT, 's',
118 "Restrict which X.509 extensions to print and/or copy"},
119 {"ocspid", OPT_OCSPID, '-',
120 "Print OCSP hash values for the subject name and public key"},
121 {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"},
122 {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"},
123 {"pubkey", OPT_PUBKEY, '-', "Print the public key in PEM format"},
124 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
125 {"multi", OPT_MULTI, '-', "Process multiple certificates"},
126
127 OPT_SECTION("Certificate checking"),
128 {"checkend", OPT_CHECKEND, 'M',
129 "Check whether cert expires in the next arg seconds"},
130 {OPT_MORE_STR, 1, 1, "Exit 1 (failure) if so, 0 if not"},
131 {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"},
132 {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"},
133 {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"},
134
135 OPT_SECTION("Certificate output"),
136 {"set_serial", OPT_SET_SERIAL, 's',
137 "Serial number to use, overrides -CAserial"},
138 {"next_serial", OPT_NEXT_SERIAL, '-',
139 "Increment current certificate serial number"},
140 {"not_before", OPT_NOT_BEFORE, 's',
141 "[CC]YYMMDDHHMMSSZ value for notBefore certificate field"},
142 {"not_after", OPT_NOT_AFTER, 's',
143 "[CC]YYMMDDHHMMSSZ value for notAfter certificate field, overrides -days"},
144 {"days", OPT_DAYS, 'n',
145 "Number of days until newly generated certificate expires - default 30"},
146 {"preserve_dates", OPT_PRESERVE_DATES, '-',
147 "Preserve existing validity dates"},
148 {"set_issuer", OPT_ISSU, 's', "Set or override certificate issuer"},
149 {"set_subject", OPT_SUBJ, 's', "Set or override certificate subject (and issuer)"},
150 {"subj", OPT_SUBJ, 's', "Alias for -set_subject"},
151 {"force_pubkey", OPT_FORCE_PUBKEY, '<',
152 "Key to be placed in new certificate or certificate request"},
153 {"clrext", OPT_CLREXT, '-',
154 "Do not take over any extensions from the source certificate or request"},
155 {"extfile", OPT_EXTFILE, '<', "Config file with X509V3 extensions to add"},
156 {"extensions", OPT_EXTENSIONS, 's',
157 "Section of extfile to use - default: unnamed section"},
158 {"sigopt", OPT_SIGOPT, 's', "Signature parameter, in n:v form"},
159 {"badsig", OPT_BADSIG, '-',
160 "Corrupt last byte of certificate signature (for test)"},
161 {"", OPT_MD, '-', "Any supported digest, used for signing and printing"},
162
163 OPT_SECTION("Micro-CA"),
164 {"CA", OPT_CA, '<',
165 "Use the given CA certificate, conflicts with -key"},
166 {"CAform", OPT_CAFORM, 'F', "CA cert format (PEM/DER/P12); has no effect"},
167 {"CAkey", OPT_CAKEY, 's', "The corresponding CA key; default is -CA arg"},
168 {"CAkeyform", OPT_CAKEYFORM, 'E',
169 "CA key format (ENGINE, other values ignored)"},
170 {"CAserial", OPT_CASERIAL, 's',
171 "File that keeps track of CA-generated serial number"},
172 {"CAcreateserial", OPT_CACREATESERIAL, '-',
173 "Create CA serial number file if it does not exist"},
174
175 OPT_SECTION("Certificate trust output"),
176 {"trustout", OPT_TRUSTOUT, '-', "Mark certificate PEM output as trusted"},
177 {"setalias", OPT_SETALIAS, 's', "Set certificate alias (nickname)"},
178 {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"},
179 {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"},
180 {"clrreject", OPT_CLRREJECT, '-',
181 "Clears all the prohibited or rejected uses of the certificate"},
182 {"addreject", OPT_ADDREJECT, 's',
183 "Reject certificate for a given purpose"},
184
185 OPT_R_OPTIONS,
186 #ifndef OPENSSL_NO_ENGINE
187 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
188 #endif
189 OPT_PROV_OPTIONS,
190 {NULL}
191 };
192
warn_copying(ASN1_OBJECT * excluded,const char * names)193 static void warn_copying(ASN1_OBJECT *excluded, const char *names)
194 {
195 const char *sn = OBJ_nid2sn(OBJ_obj2nid(excluded));
196
197 if (names != NULL && strstr(names, sn) != NULL)
198 BIO_printf(bio_err,
199 "Warning: -ext should not specify copying %s extension to CSR; ignoring this\n",
200 sn);
201 }
202
x509_to_req(X509 * cert,int ext_copy,const char * names)203 static X509_REQ *x509_to_req(X509 *cert, int ext_copy, const char *names)
204 {
205 const STACK_OF(X509_EXTENSION) *cert_exts = X509_get0_extensions(cert);
206 int i, n = sk_X509_EXTENSION_num(cert_exts /* may be NULL */);
207 ASN1_OBJECT *skid = OBJ_nid2obj(NID_subject_key_identifier);
208 ASN1_OBJECT *akid = OBJ_nid2obj(NID_authority_key_identifier);
209 STACK_OF(X509_EXTENSION) *exts;
210 X509_REQ *req = X509_to_X509_REQ(cert, NULL, NULL);
211
212 if (req == NULL)
213 return NULL;
214
215 /*
216 * Filter out SKID and AKID extensions, which make no sense in a CSR.
217 * If names is not NULL, copy only those extensions listed there.
218 */
219 warn_copying(skid, names);
220 warn_copying(akid, names);
221 if ((exts = sk_X509_EXTENSION_new_reserve(NULL, n)) == NULL)
222 goto err;
223 for (i = 0; i < n; i++) {
224 X509_EXTENSION *ex = sk_X509_EXTENSION_value(cert_exts, i);
225 ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
226
227 if (OBJ_cmp(obj, skid) != 0 && OBJ_cmp(obj, akid) != 0
228 && !sk_X509_EXTENSION_push(exts, ex))
229 goto err;
230 }
231
232 if (sk_X509_EXTENSION_num(exts) > 0) {
233 if (ext_copy != EXT_COPY_UNSET && ext_copy != EXT_COPY_NONE
234 && !X509_REQ_add_extensions(req, exts)) {
235 BIO_printf(bio_err, "Error copying extensions from certificate\n");
236 goto err;
237 }
238 }
239 sk_X509_EXTENSION_free(exts);
240 return req;
241
242 err:
243 sk_X509_EXTENSION_free(exts);
244 X509_REQ_free(req);
245 return NULL;
246 }
247
self_signed(X509_STORE * ctx,X509 * cert)248 static int self_signed(X509_STORE *ctx, X509 *cert)
249 {
250 X509_STORE_CTX *xsc = X509_STORE_CTX_new();
251 int ret = 0;
252
253 if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, cert, NULL)) {
254 BIO_printf(bio_err, "Error initialising X509 store\n");
255 } else {
256 X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
257 ret = X509_verify_cert(xsc) > 0;
258 }
259 X509_STORE_CTX_free(xsc);
260 return ret;
261 }
262
x509_main(int argc,char ** argv)263 int x509_main(int argc, char **argv)
264 {
265 ASN1_INTEGER *sno = NULL;
266 ASN1_OBJECT *objtmp = NULL;
267 BIO *out = NULL;
268 CONF *extconf = NULL;
269 int ext_copy = EXT_COPY_UNSET;
270 X509V3_CTX ext_ctx;
271 EVP_PKEY *privkey = NULL, *CAkey = NULL, *pubkey = NULL;
272 EVP_PKEY *pkey;
273 int newcert = 0;
274 char *issu = NULL, *subj = NULL, *digest = NULL;
275 X509_NAME *fissu = NULL, *fsubj = NULL;
276 const unsigned long chtype = MBSTRING_ASC;
277 const int multirdn = 1;
278 STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
279 STACK_OF(OPENSSL_STRING) *sigopts = NULL, *vfyopts = NULL;
280 X509 *x = NULL, *xca = NULL, *issuer_cert;
281 X509_REQ *req = NULL, *rq = NULL;
282 X509_STORE *ctx = NULL;
283 char *CAkeyfile = NULL, *CAserial = NULL, *pubkeyfile = NULL, *alias = NULL;
284 char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;
285 STACK_OF(X509) *certs = NULL;
286 char *ext_names = NULL;
287 char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;
288 char *infile = NULL, *outfile = NULL, *privkeyfile = NULL, *CAfile = NULL;
289 char *prog, *not_before = NULL, *not_after = NULL;
290 int days = UNSET_DAYS; /* not explicitly set */
291 int x509toreq = 0, modulus = 0, multi = 0, print_pubkey = 0, pprint = 0;
292 int CAformat = FORMAT_UNDEF, CAkeyformat = FORMAT_UNDEF;
293 unsigned long dateopt = ASN1_DTFLGS_RFC822;
294 int fingerprint = 0, reqfile = 0, checkend = 0;
295 int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, keyformat = FORMAT_UNDEF;
296 int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;
297 int noout = 0, CA_createserial = 0, email = 0;
298 int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;
299 int ret = 1, i, j, k = 0, num = 0, badsig = 0, clrext = 0, nocert = 0;
300 int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0, ext = 0;
301 int enddate = 0;
302 time_t checkoffset = 0;
303 unsigned long certflag = 0;
304 int preserve_dates = 0;
305 OPTION_CHOICE o;
306 ENGINE *e = NULL;
307 #ifndef OPENSSL_NO_MD5
308 int subject_hash_old = 0, issuer_hash_old = 0;
309 #endif
310
311 ctx = X509_STORE_new();
312 if (ctx == NULL)
313 goto err;
314 X509_STORE_set_verify_cb(ctx, callb);
315
316 opt_set_unknown_name("digest");
317 prog = opt_init(argc, argv, x509_options);
318 while ((o = opt_next()) != OPT_EOF) {
319 switch (o) {
320 case OPT_EOF:
321 case OPT_ERR:
322 opthelp:
323 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
324 goto err;
325 case OPT_HELP:
326 opt_help(x509_options);
327 ret = 0;
328 goto end;
329 case OPT_INFORM:
330 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
331 goto opthelp;
332 break;
333 case OPT_IN:
334 infile = opt_arg();
335 break;
336 case OPT_OUTFORM:
337 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
338 goto opthelp;
339 break;
340 case OPT_KEYFORM:
341 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
342 goto opthelp;
343 break;
344 case OPT_CAFORM:
345 if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAformat))
346 goto opthelp;
347 break;
348 case OPT_CAKEYFORM:
349 if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat))
350 goto opthelp;
351 break;
352 case OPT_OUT:
353 outfile = opt_arg();
354 break;
355 case OPT_REQ:
356 reqfile = 1;
357 break;
358
359 case OPT_DATEOPT:
360 if (!set_dateopt(&dateopt, opt_arg())) {
361 BIO_printf(bio_err,
362 "Invalid date format: %s\n", opt_arg());
363 goto err;
364 }
365 break;
366 case OPT_COPY_EXTENSIONS:
367 if (!set_ext_copy(&ext_copy, opt_arg())) {
368 BIO_printf(bio_err,
369 "Invalid extension copy option: %s\n", opt_arg());
370 goto err;
371 }
372 break;
373
374 case OPT_SIGOPT:
375 if (!sigopts)
376 sigopts = sk_OPENSSL_STRING_new_null();
377 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
378 goto opthelp;
379 break;
380 case OPT_VFYOPT:
381 if (!vfyopts)
382 vfyopts = sk_OPENSSL_STRING_new_null();
383 if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
384 goto opthelp;
385 break;
386 case OPT_NOT_BEFORE:
387 not_before = opt_arg();
388 break;
389 case OPT_NOT_AFTER:
390 not_after = opt_arg();
391 break;
392 case OPT_DAYS:
393 days = atoi(opt_arg());
394 if (days <= UNSET_DAYS) {
395 BIO_printf(bio_err, "%s: -days parameter arg must be >= -1\n",
396 prog);
397 goto err;
398 }
399 break;
400 case OPT_PASSIN:
401 passinarg = opt_arg();
402 break;
403 case OPT_EXTFILE:
404 extfile = opt_arg();
405 break;
406 case OPT_R_CASES:
407 if (!opt_rand(o))
408 goto end;
409 break;
410 case OPT_PROV_CASES:
411 if (!opt_provider(o))
412 goto end;
413 break;
414 case OPT_EXTENSIONS:
415 extsect = opt_arg();
416 break;
417 case OPT_KEY:
418 case OPT_SIGNKEY:
419 privkeyfile = opt_arg();
420 break;
421 case OPT_CA:
422 CAfile = opt_arg();
423 break;
424 case OPT_CAKEY:
425 CAkeyfile = opt_arg();
426 break;
427 case OPT_CASERIAL:
428 CAserial = opt_arg();
429 break;
430 case OPT_SET_SERIAL:
431 if (sno != NULL) {
432 BIO_printf(bio_err, "Serial number supplied twice\n");
433 goto opthelp;
434 }
435 if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
436 goto opthelp;
437 break;
438 case OPT_NEW:
439 newcert = 1;
440 break;
441 case OPT_FORCE_PUBKEY:
442 pubkeyfile = opt_arg();
443 break;
444 case OPT_ISSU:
445 issu = opt_arg();
446 break;
447 case OPT_SUBJ:
448 subj = opt_arg();
449 break;
450 case OPT_ADDTRUST:
451 if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
452 goto err;
453 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
454 BIO_printf(bio_err, "%s: Invalid trust object value %s\n",
455 prog, opt_arg());
456 goto opthelp;
457 }
458 if (!sk_ASN1_OBJECT_push(trust, objtmp))
459 goto err;
460 trustout = 1;
461 break;
462 case OPT_ADDREJECT:
463 if (reject == NULL && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
464 goto err;
465 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
466 BIO_printf(bio_err, "%s: Invalid reject object value %s\n",
467 prog, opt_arg());
468 goto opthelp;
469 }
470 if (!sk_ASN1_OBJECT_push(reject, objtmp))
471 goto err;
472 trustout = 1;
473 break;
474 case OPT_SETALIAS:
475 alias = opt_arg();
476 trustout = 1;
477 break;
478 case OPT_CERTOPT:
479 if (!set_cert_ex(&certflag, opt_arg()))
480 goto opthelp;
481 break;
482 case OPT_NAMEOPT:
483 if (!set_nameopt(opt_arg()))
484 goto opthelp;
485 break;
486 case OPT_ENGINE:
487 e = setup_engine(opt_arg(), 0);
488 break;
489 case OPT_EMAIL:
490 email = ++num;
491 break;
492 case OPT_OCSP_URI:
493 ocsp_uri = ++num;
494 break;
495 case OPT_SERIAL:
496 serial = ++num;
497 break;
498 case OPT_NEXT_SERIAL:
499 next_serial = ++num;
500 break;
501 case OPT_MODULUS:
502 modulus = ++num;
503 break;
504 case OPT_MULTI:
505 multi = 1;
506 break;
507 case OPT_PUBKEY:
508 print_pubkey = ++num;
509 break;
510 case OPT_X509TOREQ:
511 x509toreq = 1;
512 break;
513 case OPT_TEXT:
514 text = ++num;
515 break;
516 case OPT_SUBJECT:
517 subject = ++num;
518 break;
519 case OPT_ISSUER:
520 issuer = ++num;
521 break;
522 case OPT_FINGERPRINT:
523 fingerprint = ++num;
524 break;
525 case OPT_HASH:
526 subject_hash = ++num;
527 break;
528 case OPT_ISSUER_HASH:
529 issuer_hash = ++num;
530 break;
531 case OPT_PURPOSE:
532 pprint = ++num;
533 break;
534 case OPT_STARTDATE:
535 startdate = ++num;
536 break;
537 case OPT_ENDDATE:
538 enddate = ++num;
539 break;
540 case OPT_NOOUT:
541 noout = ++num;
542 break;
543 case OPT_EXT:
544 ext = ++num;
545 ext_names = opt_arg();
546 break;
547 case OPT_NOCERT:
548 nocert = 1;
549 break;
550 case OPT_TRUSTOUT:
551 trustout = 1;
552 break;
553 case OPT_CLRTRUST:
554 clrtrust = ++num;
555 break;
556 case OPT_CLRREJECT:
557 clrreject = ++num;
558 break;
559 case OPT_ALIAS:
560 aliasout = ++num;
561 break;
562 case OPT_CACREATESERIAL:
563 CA_createserial = 1;
564 break;
565 case OPT_CLREXT:
566 clrext = 1;
567 break;
568 case OPT_OCSPID:
569 ocspid = ++num;
570 break;
571 case OPT_BADSIG:
572 badsig = 1;
573 break;
574 #ifndef OPENSSL_NO_MD5
575 case OPT_SUBJECT_HASH_OLD:
576 subject_hash_old = ++num;
577 break;
578 case OPT_ISSUER_HASH_OLD:
579 issuer_hash_old = ++num;
580 break;
581 #else
582 case OPT_SUBJECT_HASH_OLD:
583 case OPT_ISSUER_HASH_OLD:
584 break;
585 #endif
586 case OPT_DATES:
587 startdate = ++num;
588 enddate = ++num;
589 break;
590 case OPT_CHECKEND:
591 checkend = 1;
592 {
593 ossl_intmax_t temp = 0;
594 if (!opt_intmax(opt_arg(), &temp))
595 goto opthelp;
596 checkoffset = (time_t)temp;
597 if ((ossl_intmax_t)checkoffset != temp) {
598 BIO_printf(bio_err, "%s: Checkend time out of range %s\n",
599 prog, opt_arg());
600 goto opthelp;
601 }
602 }
603 break;
604 case OPT_CHECKHOST:
605 checkhost = opt_arg();
606 break;
607 case OPT_CHECKEMAIL:
608 checkemail = opt_arg();
609 break;
610 case OPT_CHECKIP:
611 checkip = opt_arg();
612 break;
613 case OPT_PRESERVE_DATES:
614 preserve_dates = 1;
615 break;
616 case OPT_MD:
617 digest = opt_unknown();
618 break;
619 }
620 }
621 /* No extra arguments. */
622 if (!opt_check_rest_arg(NULL))
623 goto opthelp;
624
625 if (!app_RAND_load())
626 goto err;
627
628 if (!opt_check_md(digest))
629 goto opthelp;
630
631 if (preserve_dates && not_before != NULL) {
632 BIO_printf(bio_err, "Cannot use -preserve_dates with -not_before option\n");
633 goto err;
634 }
635 if (preserve_dates && not_after != NULL) {
636 BIO_printf(bio_err, "Cannot use -preserve_dates with -not_after option\n");
637 goto err;
638 }
639 if (preserve_dates && days != UNSET_DAYS) {
640 BIO_printf(bio_err, "Cannot use -preserve_dates with -days option\n");
641 goto err;
642 }
643 if (days == UNSET_DAYS)
644 days = DEFAULT_DAYS;
645 else if (not_after != NULL)
646 BIO_printf(bio_err, "Warning: -not_after option overriding -days option\n");
647
648 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
649 BIO_printf(bio_err, "Error getting password\n");
650 goto err;
651 }
652
653 if (!X509_STORE_set_default_paths_ex(ctx, app_get0_libctx(),
654 app_get0_propq()))
655 goto err;
656
657 if (newcert && infile != NULL) {
658 BIO_printf(bio_err, "The -in option cannot be used with -new\n");
659 goto err;
660 }
661 if (newcert && reqfile) {
662 BIO_printf(bio_err, "The -req option cannot be used with -new\n");
663 goto err;
664 }
665 if (privkeyfile != NULL) {
666 privkey = load_key(privkeyfile, keyformat, 0, passin, e, "private key");
667 if (privkey == NULL)
668 goto err;
669 }
670 if (pubkeyfile != NULL) {
671 if ((pubkey = load_pubkey(pubkeyfile, keyformat, 0, NULL, e,
672 "explicitly set public key")) == NULL)
673 goto err;
674 }
675
676 if (newcert) {
677 if (subj == NULL) {
678 BIO_printf(bio_err,
679 "The -new option requires a subject to be set using -subj\n");
680 goto err;
681 }
682 if (privkeyfile == NULL && pubkeyfile == NULL) {
683 BIO_printf(bio_err,
684 "The -new option requires using the -key or -force_pubkey option\n");
685 goto err;
686 }
687 }
688 if (issu != NULL
689 && (fissu = parse_name(issu, chtype, multirdn, "issuer")) == NULL)
690 goto err;
691 if (subj != NULL
692 && (fsubj = parse_name(subj, chtype, multirdn, "subject")) == NULL)
693 goto err;
694
695 if (CAkeyfile == NULL)
696 CAkeyfile = CAfile;
697 if (CAfile != NULL) {
698 if (privkeyfile != NULL) {
699 BIO_printf(bio_err, "Cannot use both -key/-signkey and -CA option\n");
700 goto err;
701 }
702 } else {
703 #define WARN_NO_CA(opt) BIO_printf(bio_err, \
704 "Warning: ignoring " opt " option since -CA option is not given\n");
705 if (CAkeyfile != NULL)
706 WARN_NO_CA("-CAkey");
707 if (CAkeyformat != FORMAT_UNDEF)
708 WARN_NO_CA("-CAkeyform");
709 if (CAformat != FORMAT_UNDEF)
710 WARN_NO_CA("-CAform");
711 if (CAserial != NULL)
712 WARN_NO_CA("-CAserial");
713 if (CA_createserial)
714 WARN_NO_CA("-CAcreateserial");
715 }
716
717 if (extfile == NULL) {
718 if (extsect != NULL)
719 BIO_printf(bio_err,
720 "Warning: ignoring -extensions option without -extfile\n");
721 } else {
722 X509V3_CTX ctx2;
723
724 if ((extconf = app_load_config(extfile)) == NULL)
725 goto err;
726 if (extsect == NULL) {
727 extsect = app_conf_try_string(extconf, "default", "extensions");
728 if (extsect == NULL)
729 extsect = "default";
730 }
731 X509V3_set_ctx_test(&ctx2);
732 X509V3_set_nconf(&ctx2, extconf);
733 if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
734 BIO_printf(bio_err,
735 "Error checking extension section %s\n", extsect);
736 goto err;
737 }
738 }
739
740 if (multi && (reqfile || newcert)) {
741 BIO_printf(bio_err, "Error: -multi cannot be used with -req or -new\n");
742 goto err;
743 }
744
745 if (reqfile) {
746 if (infile == NULL && isatty(fileno_stdin()))
747 BIO_printf(bio_err,
748 "Warning: Reading cert request from stdin since no -in option is given\n");
749 req = load_csr_autofmt(infile, informat, vfyopts,
750 "certificate request input");
751 if (req == NULL)
752 goto err;
753
754 if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) {
755 BIO_printf(bio_err, "Error unpacking public key from CSR\n");
756 goto err;
757 }
758 i = do_X509_REQ_verify(req, pkey, vfyopts);
759 if (i <= 0) {
760 BIO_printf(bio_err, i < 0
761 ? "Error while verifying certificate request self-signature\n"
762 : "Certificate request self-signature did not match the contents\n");
763 goto err;
764 }
765 BIO_printf(bio_err, "Certificate request self-signature ok\n");
766
767 print_name(bio_err, "subject=", X509_REQ_get_subject_name(req));
768 } else if (!x509toreq && ext_copy != EXT_COPY_UNSET) {
769 BIO_printf(bio_err, "Warning: ignoring -copy_extensions since neither -x509toreq nor -req is given\n");
770 }
771
772 if (reqfile || newcert) {
773 if (preserve_dates)
774 BIO_printf(bio_err,
775 "Warning: ignoring -preserve_dates option with -req or -new\n");
776 preserve_dates = 0;
777 if (privkeyfile == NULL && CAkeyfile == NULL) {
778 BIO_printf(bio_err,
779 "We need a private key to sign with, use -key or -CAkey or -CA with private key\n");
780 goto err;
781 }
782 if ((x = X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL)
783 goto err;
784 if (CAfile == NULL && sno == NULL) {
785 sno = ASN1_INTEGER_new();
786 if (sno == NULL || !rand_serial(NULL, sno))
787 goto err;
788 }
789 if (req != NULL && ext_copy != EXT_COPY_UNSET) {
790 if (clrext && ext_copy != EXT_COPY_NONE) {
791 BIO_printf(bio_err, "Must not use -clrext together with -copy_extensions\n");
792 goto err;
793 } else if (!copy_extensions(x, req, ext_copy)) {
794 BIO_printf(bio_err, "Error copying extensions from request\n");
795 goto err;
796 }
797 }
798 } else {
799 if (infile == NULL && isatty(fileno_stdin()))
800 BIO_printf(bio_err,
801 "Warning: Reading certificate(s) from stdin since no -in or -new option is given\n");
802 if (multi) {
803 certs = sk_X509_new_null();
804 if (certs == NULL)
805 goto err;
806 if (!load_certs(infile, 1, &certs, passin, NULL))
807 goto err;
808 if (sk_X509_num(certs) <= 0)
809 goto err;
810 } else {
811 x = load_cert_pass(infile, informat, 1, passin, "certificate");
812 if (x == NULL)
813 goto err;
814 }
815 }
816
817 out = bio_open_default(outfile, 'w', outformat);
818 if (out == NULL)
819 goto err;
820
821 cert_loop:
822 if (multi)
823 x = sk_X509_value(certs, k);
824
825 if ((fsubj != NULL || req != NULL)
826 && !X509_set_subject_name(x, fsubj != NULL ? fsubj :
827 X509_REQ_get_subject_name(req)))
828 goto err;
829 if ((pubkey != NULL || privkey != NULL || req != NULL)
830 && !X509_set_pubkey(x, pubkey != NULL ? pubkey :
831 privkey != NULL ? privkey :
832 X509_REQ_get0_pubkey(req)))
833 goto err;
834
835 if (CAfile != NULL) {
836 xca = load_cert_pass(CAfile, CAformat, 1, passin, "CA certificate");
837 if (xca == NULL)
838 goto err;
839 }
840
841 if (alias)
842 X509_alias_set1(x, (unsigned char *)alias, -1);
843
844 if (clrtrust)
845 X509_trust_clear(x);
846 if (clrreject)
847 X509_reject_clear(x);
848
849 if (trust != NULL) {
850 for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++)
851 X509_add1_trust_object(x, sk_ASN1_OBJECT_value(trust, i));
852 }
853
854 if (reject != NULL) {
855 for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++)
856 X509_add1_reject_object(x, sk_ASN1_OBJECT_value(reject, i));
857 }
858
859 if (clrext && ext_names != NULL)
860 BIO_printf(bio_err, "Warning: Ignoring -ext since -clrext is given\n");
861 for (i = X509_get_ext_count(x) - 1; i >= 0; i--) {
862 X509_EXTENSION *ex = X509_get_ext(x, i);
863 const char *sn = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ex)));
864
865 if (clrext || (ext_names != NULL && strstr(ext_names, sn) == NULL))
866 X509_EXTENSION_free(X509_delete_ext(x, i));
867 }
868
869 issuer_cert = x;
870 if (CAfile != NULL) {
871 issuer_cert = xca;
872 if (sno == NULL)
873 sno = x509_load_serial(CAfile, CAserial, CA_createserial);
874 if (sno == NULL)
875 goto err;
876 if (!x509toreq && !reqfile && !newcert && !self_signed(ctx, x))
877 goto err;
878 } else {
879 if (privkey != NULL && !cert_matches_key(x, privkey))
880 BIO_printf(bio_err,
881 "Warning: Signature key and public key of cert do not match\n");
882 }
883
884 if (sno != NULL && !X509_set_serialNumber(x, sno))
885 goto err;
886
887 if (reqfile || newcert || privkey != NULL || CAfile != NULL) {
888 if (!preserve_dates && !set_cert_times(x, not_before, not_after, days, 1))
889 goto err;
890 if (fissu != NULL) {
891 if (!X509_set_issuer_name(x, fissu))
892 goto err;
893 } else {
894 if (!X509_set_issuer_name(x, X509_get_subject_name(issuer_cert)))
895 goto err;
896 }
897 }
898
899 X509V3_set_ctx(&ext_ctx, issuer_cert, x, NULL, NULL, X509V3_CTX_REPLACE);
900 /* prepare fallback for AKID, but only if issuer cert equals subject cert */
901 if (CAfile == NULL) {
902 if (!X509V3_set_issuer_pkey(&ext_ctx, privkey))
903 goto err;
904 }
905 if (extconf != NULL && !x509toreq) {
906 X509V3_set_nconf(&ext_ctx, extconf);
907 if (!X509V3_EXT_add_nconf(extconf, &ext_ctx, extsect, x)) {
908 BIO_printf(bio_err,
909 "Error adding extensions from section %s\n", extsect);
910 goto err;
911 }
912 }
913
914 /* At this point the contents of the certificate x have been finished. */
915
916 pkey = X509_get0_pubkey(x);
917 if ((print_pubkey != 0 || modulus != 0) && pkey == NULL) {
918 BIO_printf(bio_err, "Error getting public key\n");
919 goto err;
920 }
921
922 if (x509toreq) { /* also works in conjunction with -req */
923 if (privkey == NULL) {
924 BIO_printf(bio_err, "Must specify request signing key using -key\n");
925 goto err;
926 }
927 if (clrext && ext_copy != EXT_COPY_NONE) {
928 BIO_printf(bio_err, "Must not use -clrext together with -copy_extensions\n");
929 goto err;
930 }
931 if ((rq = x509_to_req(x, ext_copy, ext_names)) == NULL)
932 goto err;
933 if (extconf != NULL) {
934 X509V3_set_nconf(&ext_ctx, extconf);
935 if (!X509V3_EXT_REQ_add_nconf(extconf, &ext_ctx, extsect, rq)) {
936 BIO_printf(bio_err,
937 "Error adding request extensions from section %s\n", extsect);
938 goto err;
939 }
940 }
941 if (!do_X509_REQ_sign(rq, privkey, digest, sigopts))
942 goto err;
943 if (!noout) {
944 if (outformat == FORMAT_ASN1) {
945 X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT);
946 i = i2d_X509_bio(out, x);
947 } else {
948 i = PEM_write_bio_X509_REQ(out, rq);
949 }
950 if (!i) {
951 BIO_printf(bio_err,
952 "Unable to write certificate request\n");
953 goto err;
954 }
955 }
956 noout = 1;
957 } else if (CAfile != NULL) {
958 if ((CAkey = load_key(CAkeyfile, CAkeyformat,
959 0, passin, e, "CA private key")) == NULL)
960 goto err;
961 if (!X509_check_private_key(xca, CAkey)) {
962 BIO_printf(bio_err,
963 "CA certificate and CA private key do not match\n");
964 goto err;
965 }
966
967 if (!do_X509_sign(x, 0, CAkey, digest, sigopts, &ext_ctx))
968 goto err;
969 } else if (privkey != NULL) {
970 if (!do_X509_sign(x, 0, privkey, digest, sigopts, &ext_ctx))
971 goto err;
972 }
973 if (badsig) {
974 const ASN1_BIT_STRING *signature;
975
976 X509_get0_signature(&signature, NULL, x);
977 corrupt_signature(signature);
978 }
979
980 /* Process print options in the given order, as indicated by index i */
981 for (i = 1; i <= num; i++) {
982 if (i == issuer) {
983 print_name(out, "issuer=", X509_get_issuer_name(x));
984 } else if (i == subject) {
985 print_name(out, "subject=", X509_get_subject_name(x));
986 } else if (i == serial) {
987 BIO_printf(out, "serial=");
988 i2a_ASN1_INTEGER(out, X509_get0_serialNumber(x));
989 BIO_printf(out, "\n");
990 } else if (i == next_serial) {
991 ASN1_INTEGER *ser;
992 BIGNUM *bnser = ASN1_INTEGER_to_BN(X509_get0_serialNumber(x), NULL);
993
994 if (bnser == NULL)
995 goto err;
996 if (!BN_add_word(bnser, 1)
997 || (ser = BN_to_ASN1_INTEGER(bnser, NULL)) == NULL) {
998 BN_free(bnser);
999 goto err;
1000 }
1001 BN_free(bnser);
1002 i2a_ASN1_INTEGER(out, ser);
1003 ASN1_INTEGER_free(ser);
1004 BIO_puts(out, "\n");
1005 } else if (i == email || i == ocsp_uri) {
1006 STACK_OF(OPENSSL_STRING) *emlst =
1007 i == email ? X509_get1_email(x) : X509_get1_ocsp(x);
1008
1009 for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
1010 BIO_printf(out, "%s\n", sk_OPENSSL_STRING_value(emlst, j));
1011 X509_email_free(emlst);
1012 } else if (i == aliasout) {
1013 unsigned char *alstr = X509_alias_get0(x, NULL);
1014
1015 if (alstr)
1016 BIO_printf(out, "%s\n", alstr);
1017 else
1018 BIO_puts(out, "<No Alias>\n");
1019 } else if (i == subject_hash) {
1020 BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
1021 #ifndef OPENSSL_NO_MD5
1022 } else if (i == subject_hash_old) {
1023 BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
1024 #endif
1025 } else if (i == issuer_hash) {
1026 BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
1027 #ifndef OPENSSL_NO_MD5
1028 } else if (i == issuer_hash_old) {
1029 BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
1030 #endif
1031 } else if (i == pprint) {
1032 BIO_printf(out, "Certificate purposes:\n");
1033 for (j = 0; j < X509_PURPOSE_get_count(); j++)
1034 purpose_print(out, x, X509_PURPOSE_get0(j));
1035 } else if (i == modulus) {
1036 BIO_printf(out, "Modulus=");
1037 if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS")) {
1038 BIGNUM *n = NULL;
1039
1040 /* Every RSA key has an 'n' */
1041 EVP_PKEY_get_bn_param(pkey, "n", &n);
1042 BN_print(out, n);
1043 BN_free(n);
1044 } else if (EVP_PKEY_is_a(pkey, "DSA")) {
1045 BIGNUM *dsapub = NULL;
1046
1047 /* Every DSA key has a 'pub' */
1048 EVP_PKEY_get_bn_param(pkey, "pub", &dsapub);
1049 BN_print(out, dsapub);
1050 BN_free(dsapub);
1051 } else {
1052 BIO_printf(out, "No modulus for this public key type");
1053 }
1054 BIO_printf(out, "\n");
1055 } else if (i == print_pubkey) {
1056 PEM_write_bio_PUBKEY(out, pkey);
1057 } else if (i == text) {
1058 X509_print_ex(out, x, get_nameopt(), certflag);
1059 } else if (i == startdate) {
1060 BIO_puts(out, "notBefore=");
1061 ASN1_TIME_print_ex(out, X509_get0_notBefore(x), dateopt);
1062 BIO_puts(out, "\n");
1063 } else if (i == enddate) {
1064 BIO_puts(out, "notAfter=");
1065 ASN1_TIME_print_ex(out, X509_get0_notAfter(x), dateopt);
1066 BIO_puts(out, "\n");
1067 } else if (i == fingerprint) {
1068 unsigned int n;
1069 unsigned char md[EVP_MAX_MD_SIZE];
1070 const char *fdigname = digest;
1071 EVP_MD *fdig;
1072 int digres;
1073
1074 if (fdigname == NULL)
1075 fdigname = "SHA1";
1076
1077 if ((fdig = EVP_MD_fetch(app_get0_libctx(), fdigname,
1078 app_get0_propq())) == NULL) {
1079 BIO_printf(bio_err, "Unknown digest\n");
1080 goto err;
1081 }
1082 digres = X509_digest(x, fdig, md, &n);
1083 EVP_MD_free(fdig);
1084 if (!digres) {
1085 BIO_printf(bio_err, "Out of memory\n");
1086 goto err;
1087 }
1088
1089 BIO_printf(out, "%s Fingerprint=", fdigname);
1090 for (j = 0; j < (int)n; j++)
1091 BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n) ? '\n' : ':');
1092 } else if (i == ocspid) {
1093 X509_ocspid_print(out, x);
1094 } else if (i == ext) {
1095 print_x509v3_exts(out, x, ext_names);
1096 }
1097 }
1098
1099 if (checkend) {
1100 time_t tcheck = time(NULL) + checkoffset;
1101
1102 ret = X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0;
1103 if (ret)
1104 BIO_printf(out, "Certificate will expire\n");
1105 else
1106 BIO_printf(out, "Certificate will not expire\n");
1107 goto end_cert_loop;
1108 }
1109
1110 if (!check_cert_attributes(out, x, checkhost, checkemail, checkip, 1))
1111 goto err;
1112
1113 if (noout || nocert) {
1114 ret = 0;
1115 goto end_cert_loop;
1116 }
1117
1118 if (outformat == FORMAT_ASN1) {
1119 i = i2d_X509_bio(out, x);
1120 } else if (outformat == FORMAT_PEM) {
1121 if (trustout)
1122 i = PEM_write_bio_X509_AUX(out, x);
1123 else
1124 i = PEM_write_bio_X509(out, x);
1125 } else {
1126 BIO_printf(bio_err, "Bad output format specified for outfile\n");
1127 goto err;
1128 }
1129 if (!i) {
1130 BIO_printf(bio_err, "Unable to write certificate\n");
1131 goto err;
1132 }
1133
1134 end_cert_loop:
1135 if (multi && ++k < sk_X509_num(certs))
1136 goto cert_loop;
1137
1138 ret = 0;
1139 goto end;
1140
1141 err:
1142 ERR_print_errors(bio_err);
1143
1144 end:
1145 if (multi) {
1146 sk_X509_pop_free(certs, X509_free);
1147 x = NULL;
1148 }
1149 NCONF_free(extconf);
1150 BIO_free_all(out);
1151 X509_STORE_free(ctx);
1152 X509_NAME_free(fissu);
1153 X509_NAME_free(fsubj);
1154 X509_REQ_free(req);
1155 X509_free(x);
1156 X509_free(xca);
1157 EVP_PKEY_free(privkey);
1158 EVP_PKEY_free(CAkey);
1159 EVP_PKEY_free(pubkey);
1160 sk_OPENSSL_STRING_free(sigopts);
1161 sk_OPENSSL_STRING_free(vfyopts);
1162 X509_REQ_free(rq);
1163 ASN1_INTEGER_free(sno);
1164 sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
1165 sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
1166 release_engine(e);
1167 clear_free(passin);
1168 return ret;
1169 }
1170
x509_load_serial(const char * CAfile,const char * serialfile,int create)1171 static ASN1_INTEGER *x509_load_serial(const char *CAfile,
1172 const char *serialfile, int create)
1173 {
1174 char *buf = NULL;
1175 ASN1_INTEGER *bs = NULL;
1176 BIGNUM *serial = NULL;
1177 int defaultfile = 0, file_exists;
1178
1179 if (serialfile == NULL) {
1180 const char *p = strrchr(CAfile, '.');
1181 size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
1182
1183 buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
1184 memcpy(buf, CAfile, len);
1185 memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
1186 serialfile = buf;
1187 defaultfile = 1;
1188 }
1189
1190 serial = load_serial(serialfile, &file_exists, create || defaultfile, NULL);
1191 if (serial == NULL)
1192 goto end;
1193
1194 if (!BN_add_word(serial, 1)) {
1195 BIO_printf(bio_err, "Serial number increment failure\n");
1196 goto end;
1197 }
1198
1199 if (file_exists || create)
1200 save_serial(serialfile, NULL, serial, &bs);
1201 else
1202 bs = BN_to_ASN1_INTEGER(serial, NULL);
1203
1204 end:
1205 OPENSSL_free(buf);
1206 BN_free(serial);
1207 return bs;
1208 }
1209
callb(int ok,X509_STORE_CTX * ctx)1210 static int callb(int ok, X509_STORE_CTX *ctx)
1211 {
1212 int err;
1213 X509 *err_cert;
1214
1215 /*
1216 * It is ok to use a self-signed certificate. This case will catch both
1217 * the initial ok == 0 and the final ok == 1 calls to this function.
1218 */
1219 err = X509_STORE_CTX_get_error(ctx);
1220 if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1221 return 1;
1222
1223 if (!ok) {
1224 err_cert = X509_STORE_CTX_get_current_cert(ctx);
1225 print_name(bio_err, "subject=", X509_get_subject_name(err_cert));
1226 BIO_printf(bio_err,
1227 "Error with certificate - error %d at depth %d\n%s\n", err,
1228 X509_STORE_CTX_get_error_depth(ctx),
1229 X509_verify_cert_error_string(err));
1230 return 1;
1231 }
1232
1233 return 1;
1234 }
1235
purpose_print(BIO * bio,X509 * cert,X509_PURPOSE * pt)1236 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
1237 {
1238 int id, i, idret;
1239 const char *pname;
1240 id = X509_PURPOSE_get_id(pt);
1241 pname = X509_PURPOSE_get0_name(pt);
1242 for (i = 0; i < 2; i++) {
1243 idret = X509_check_purpose(cert, id, i);
1244 BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
1245 if (idret == 1)
1246 BIO_printf(bio, "Yes\n");
1247 else if (idret == 0)
1248 BIO_printf(bio, "No\n");
1249 else
1250 BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
1251 }
1252 return 1;
1253 }
1254
parse_ext_names(char * names,const char ** result)1255 static int parse_ext_names(char *names, const char **result)
1256 {
1257 char *p, *q;
1258 int cnt = 0, len = 0;
1259
1260 p = q = names;
1261 len = (int)strlen(names);
1262
1263 while (q - names <= len) {
1264 if (*q != ',' && *q != '\0') {
1265 q++;
1266 continue;
1267 }
1268 if (p != q) {
1269 /* found */
1270 if (result != NULL) {
1271 result[cnt] = p;
1272 *q = '\0';
1273 }
1274 cnt++;
1275 }
1276 p = ++q;
1277 }
1278
1279 return cnt;
1280 }
1281
print_x509v3_exts(BIO * bio,X509 * x,const char * ext_names)1282 static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names)
1283 {
1284 const STACK_OF(X509_EXTENSION) *exts = NULL;
1285 STACK_OF(X509_EXTENSION) *exts2 = NULL;
1286 X509_EXTENSION *ext = NULL;
1287 ASN1_OBJECT *obj;
1288 int i, j, ret = 0, num, nn = 0;
1289 const char *sn, **names = NULL;
1290 char *tmp_ext_names = NULL;
1291
1292 exts = X509_get0_extensions(x);
1293 if ((num = sk_X509_EXTENSION_num(exts)) <= 0) {
1294 BIO_printf(bio_err, "No extensions in certificate\n");
1295 ret = 1;
1296 goto end;
1297 }
1298
1299 /* parse comma separated ext name string */
1300 if ((tmp_ext_names = OPENSSL_strdup(ext_names)) == NULL)
1301 goto end;
1302 if ((nn = parse_ext_names(tmp_ext_names, NULL)) == 0) {
1303 BIO_printf(bio, "Invalid extension names: %s\n", ext_names);
1304 goto end;
1305 }
1306 if ((names = OPENSSL_malloc_array(nn, sizeof(char *))) == NULL)
1307 goto end;
1308 parse_ext_names(tmp_ext_names, names);
1309
1310 for (i = 0; i < num; i++) {
1311 ext = sk_X509_EXTENSION_value(exts, i);
1312
1313 /* check if this ext is what we want */
1314 obj = X509_EXTENSION_get_object(ext);
1315 sn = OBJ_nid2sn(OBJ_obj2nid(obj));
1316 if (sn == NULL || strcmp(sn, "UNDEF") == 0)
1317 continue;
1318
1319 for (j = 0; j < nn; j++) {
1320 if (strcmp(sn, names[j]) == 0) {
1321 /* push the extension into a new stack */
1322 if (exts2 == NULL
1323 && (exts2 = sk_X509_EXTENSION_new_null()) == NULL)
1324 goto end;
1325 if (!sk_X509_EXTENSION_push(exts2, ext))
1326 goto end;
1327 }
1328 }
1329 }
1330
1331 if (!sk_X509_EXTENSION_num(exts2)) {
1332 BIO_printf(bio, "No extensions matched with %s\n", ext_names);
1333 ret = 1;
1334 goto end;
1335 }
1336
1337 ret = X509V3_extensions_print(bio, NULL, exts2, 0, 0);
1338 end:
1339 sk_X509_EXTENSION_free(exts2);
1340 OPENSSL_free(names);
1341 OPENSSL_free(tmp_ext_names);
1342 return ret;
1343 }
1344