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