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