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