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