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