Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/apps/x509.c

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