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