xref: /freebsd/crypto/openssl/apps/x509.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
234252e89SJung-uk Kim  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
874664626SKris Kennaway  */
974664626SKris Kennaway 
1074664626SKris Kennaway #include <stdio.h>
1174664626SKris Kennaway #include <stdlib.h>
1274664626SKris Kennaway #include <string.h>
1374664626SKris Kennaway #include "apps.h"
14e71b7053SJung-uk Kim #include "progs.h"
1574664626SKris Kennaway #include <openssl/bio.h>
1674664626SKris Kennaway #include <openssl/asn1.h>
1774664626SKris Kennaway #include <openssl/err.h>
1874664626SKris Kennaway #include <openssl/bn.h>
1974664626SKris Kennaway #include <openssl/evp.h>
2074664626SKris Kennaway #include <openssl/x509.h>
2174664626SKris Kennaway #include <openssl/x509v3.h>
2274664626SKris Kennaway #include <openssl/objects.h>
2374664626SKris Kennaway #include <openssl/pem.h>
243b4e3dcbSSimon L. B. Nielsen #include <openssl/rsa.h>
253b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DSA
263b4e3dcbSSimon L. B. Nielsen # include <openssl/dsa.h>
273b4e3dcbSSimon L. B. Nielsen #endif
2874664626SKris Kennaway 
2974664626SKris Kennaway #undef POSTFIX
3074664626SKris Kennaway #define POSTFIX ".srl"
31*b077aed3SPierre Pronchery #define DEFAULT_DAYS    30 /* default cert validity period in days */
32*b077aed3SPierre Pronchery #define UNSET_DAYS      -2 /* -1 is used for testing expiration checks */
33*b077aed3SPierre Pronchery #define EXT_COPY_UNSET     -1
3474664626SKris Kennaway 
35e71b7053SJung-uk Kim static int callb(int ok, X509_STORE_CTX *ctx);
36*b077aed3SPierre Pronchery static ASN1_INTEGER *x509_load_serial(const char *CAfile,
37*b077aed3SPierre Pronchery                                       const char *serialfile, int create);
38e71b7053SJung-uk Kim static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
39*b077aed3SPierre Pronchery static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names);
40e71b7053SJung-uk Kim 
41e71b7053SJung-uk Kim typedef enum OPTION_choice {
42*b077aed3SPierre Pronchery     OPT_COMMON,
43e71b7053SJung-uk Kim     OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM,
44*b077aed3SPierre Pronchery     OPT_CAKEYFORM, OPT_VFYOPT, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE,
45*b077aed3SPierre Pronchery     OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_KEY, OPT_SIGNKEY, OPT_CA, OPT_CAKEY,
46*b077aed3SPierre Pronchery     OPT_CASERIAL, OPT_SET_SERIAL, OPT_NEW, OPT_FORCE_PUBKEY, OPT_SUBJ,
47*b077aed3SPierre Pronchery     OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_DATEOPT, OPT_NAMEOPT,
48*b077aed3SPierre Pronchery     OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL,
49e71b7053SJung-uk Kim     OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH,
50e71b7053SJung-uk Kim     OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES,
51e71b7053SJung-uk Kim     OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST,
52e71b7053SJung-uk Kim     OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST,
53e71b7053SJung-uk Kim     OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID,
54*b077aed3SPierre Pronchery     OPT_SUBJECT_HASH_OLD, OPT_ISSUER_HASH_OLD, OPT_COPY_EXTENSIONS,
55e71b7053SJung-uk Kim     OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT, OPT_PRESERVE_DATES,
56*b077aed3SPierre Pronchery     OPT_R_ENUM, OPT_PROV_ENUM, OPT_EXT
57e71b7053SJung-uk Kim } OPTION_CHOICE;
58e71b7053SJung-uk Kim 
59e71b7053SJung-uk Kim const OPTIONS x509_options[] = {
60*b077aed3SPierre Pronchery     OPT_SECTION("General"),
61e71b7053SJung-uk Kim     {"help", OPT_HELP, '-', "Display this summary"},
62*b077aed3SPierre Pronchery 
63*b077aed3SPierre Pronchery     {"in", OPT_IN, '<',
64*b077aed3SPierre Pronchery      "Certificate input, or CSR input file with -req (default stdin)"},
65*b077aed3SPierre Pronchery     {"passin", OPT_PASSIN, 's', "Private key and cert file pass-phrase source"},
66*b077aed3SPierre Pronchery     {"new", OPT_NEW, '-', "Generate a certificate from scratch"},
67*b077aed3SPierre Pronchery     {"x509toreq", OPT_X509TOREQ, '-',
68*b077aed3SPierre Pronchery      "Output a certification request (rather than a certificate)"},
69*b077aed3SPierre Pronchery     {"req", OPT_REQ, '-', "Input is a CSR file (rather than a certificate)"},
70*b077aed3SPierre Pronchery     {"copy_extensions", OPT_COPY_EXTENSIONS, 's',
71*b077aed3SPierre Pronchery      "copy extensions when converting from CSR to x509 or vice versa"},
72e71b7053SJung-uk Kim     {"inform", OPT_INFORM, 'f',
73*b077aed3SPierre Pronchery      "CSR input file format (DER or PEM) - default PEM"},
74*b077aed3SPierre Pronchery     {"vfyopt", OPT_VFYOPT, 's', "CSR verification parameter in n:v form"},
75*b077aed3SPierre Pronchery     {"key", OPT_KEY, 's',
76*b077aed3SPierre Pronchery      "Key for signing, and to include unless using -force_pubkey"},
77*b077aed3SPierre Pronchery     {"signkey", OPT_SIGNKEY, 's',
78*b077aed3SPierre Pronchery      "Same as -key"},
79*b077aed3SPierre Pronchery     {"keyform", OPT_KEYFORM, 'E',
80*b077aed3SPierre Pronchery      "Key input format (ENGINE, other values ignored)"},
81e71b7053SJung-uk Kim     {"out", OPT_OUT, '>', "Output file - default stdout"},
82*b077aed3SPierre Pronchery     {"outform", OPT_OUTFORM, 'f',
83*b077aed3SPierre Pronchery      "Output format (DER or PEM) - default PEM"},
84*b077aed3SPierre Pronchery     {"nocert", OPT_NOCERT, '-',
85*b077aed3SPierre Pronchery      "No cert output (except for requested printing)"},
86*b077aed3SPierre Pronchery     {"noout", OPT_NOOUT, '-', "No output (except for requested printing)"},
87*b077aed3SPierre Pronchery 
88*b077aed3SPierre Pronchery     OPT_SECTION("Certificate printing"),
89*b077aed3SPierre Pronchery     {"text", OPT_TEXT, '-', "Print the certificate in text form"},
90*b077aed3SPierre Pronchery     {"dateopt", OPT_DATEOPT, 's', "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."},
91*b077aed3SPierre Pronchery     {"certopt", OPT_CERTOPT, 's', "Various certificate text printing options"},
92*b077aed3SPierre Pronchery     {"fingerprint", OPT_FINGERPRINT, '-', "Print the certificate fingerprint"},
93*b077aed3SPierre Pronchery     {"alias", OPT_ALIAS, '-', "Print certificate alias"},
94e71b7053SJung-uk Kim     {"serial", OPT_SERIAL, '-', "Print serial number value"},
95*b077aed3SPierre Pronchery     {"startdate", OPT_STARTDATE, '-', "Print the notBefore field"},
96*b077aed3SPierre Pronchery     {"enddate", OPT_ENDDATE, '-', "Print the notAfter field"},
97*b077aed3SPierre Pronchery     {"dates", OPT_DATES, '-', "Print both notBefore and notAfter fields"},
98e71b7053SJung-uk Kim     {"subject", OPT_SUBJECT, '-', "Print subject DN"},
99e71b7053SJung-uk Kim     {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
100*b077aed3SPierre Pronchery     {"nameopt", OPT_NAMEOPT, 's',
101*b077aed3SPierre Pronchery      "Certificate subject/issuer name printing options"},
102e71b7053SJung-uk Kim     {"email", OPT_EMAIL, '-', "Print email address(es)"},
103*b077aed3SPierre Pronchery     {"hash", OPT_HASH, '-', "Synonym for -subject_hash (for backward compat)"},
104*b077aed3SPierre Pronchery     {"subject_hash", OPT_HASH, '-', "Print subject hash value"},
1051f13597dSJung-uk Kim #ifndef OPENSSL_NO_MD5
106e71b7053SJung-uk Kim     {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-',
107e71b7053SJung-uk Kim      "Print old-style (MD5) subject hash value"},
108*b077aed3SPierre Pronchery #endif
109*b077aed3SPierre Pronchery     {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"},
110*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_MD5
11158f35182SJung-uk Kim     {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-',
11258f35182SJung-uk Kim      "Print old-style (MD5) issuer hash value"},
1131f13597dSJung-uk Kim #endif
114*b077aed3SPierre Pronchery     {"ext", OPT_EXT, 's',
115*b077aed3SPierre Pronchery      "Restrict which X.509 extensions to print and/or copy"},
116*b077aed3SPierre Pronchery     {"ocspid", OPT_OCSPID, '-',
117*b077aed3SPierre Pronchery      "Print OCSP hash values for the subject name and public key"},
118*b077aed3SPierre Pronchery     {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"},
119*b077aed3SPierre Pronchery     {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"},
120*b077aed3SPierre Pronchery     {"pubkey", OPT_PUBKEY, '-', "Print the public key in PEM format"},
121*b077aed3SPierre Pronchery     {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
122*b077aed3SPierre Pronchery 
123*b077aed3SPierre Pronchery     OPT_SECTION("Certificate checking"),
124*b077aed3SPierre Pronchery     {"checkend", OPT_CHECKEND, 'M',
125*b077aed3SPierre Pronchery      "Check whether cert expires in the next arg seconds"},
126*b077aed3SPierre Pronchery     {OPT_MORE_STR, 1, 1, "Exit 1 (failure) if so, 0 if not"},
127*b077aed3SPierre Pronchery     {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"},
128*b077aed3SPierre Pronchery     {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"},
129*b077aed3SPierre Pronchery     {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"},
130*b077aed3SPierre Pronchery 
131*b077aed3SPierre Pronchery     OPT_SECTION("Certificate output"),
132*b077aed3SPierre Pronchery     {"set_serial", OPT_SET_SERIAL, 's',
133*b077aed3SPierre Pronchery      "Serial number to use, overrides -CAserial"},
134*b077aed3SPierre Pronchery     {"next_serial", OPT_NEXT_SERIAL, '-',
135*b077aed3SPierre Pronchery      "Increment current certificate serial number"},
136*b077aed3SPierre Pronchery     {"days", OPT_DAYS, 'n',
137*b077aed3SPierre Pronchery      "Number of days until newly generated certificate expires - default 30"},
138*b077aed3SPierre Pronchery     {"preserve_dates", OPT_PRESERVE_DATES, '-',
139*b077aed3SPierre Pronchery      "Preserve existing validity dates"},
140*b077aed3SPierre Pronchery     {"subj", OPT_SUBJ, 's', "Set or override certificate subject (and issuer)"},
141*b077aed3SPierre Pronchery     {"force_pubkey", OPT_FORCE_PUBKEY, '<',
142*b077aed3SPierre Pronchery      "Place the given key in new certificate"},
143*b077aed3SPierre Pronchery     {"clrext", OPT_CLREXT, '-',
144*b077aed3SPierre Pronchery      "Do not take over any extensions from the source certificate or request"},
145*b077aed3SPierre Pronchery     {"extfile", OPT_EXTFILE, '<', "Config file with X509V3 extensions to add"},
146*b077aed3SPierre Pronchery     {"extensions", OPT_EXTENSIONS, 's',
147*b077aed3SPierre Pronchery      "Section of extfile to use - default: unnamed section"},
148*b077aed3SPierre Pronchery     {"sigopt", OPT_SIGOPT, 's', "Signature parameter, in n:v form"},
149*b077aed3SPierre Pronchery     {"badsig", OPT_BADSIG, '-',
150*b077aed3SPierre Pronchery      "Corrupt last byte of certificate signature (for test)"},
151*b077aed3SPierre Pronchery     {"", OPT_MD, '-', "Any supported digest, used for signing and printing"},
152*b077aed3SPierre Pronchery 
153*b077aed3SPierre Pronchery     OPT_SECTION("Micro-CA"),
154*b077aed3SPierre Pronchery     {"CA", OPT_CA, '<',
155*b077aed3SPierre Pronchery      "Use the given CA certificate, conflicts with -key"},
156*b077aed3SPierre Pronchery     {"CAform", OPT_CAFORM, 'F', "CA cert format (PEM/DER/P12); has no effect"},
157*b077aed3SPierre Pronchery     {"CAkey", OPT_CAKEY, 's', "The corresponding CA key; default is -CA arg"},
158*b077aed3SPierre Pronchery     {"CAkeyform", OPT_CAKEYFORM, 'E',
159*b077aed3SPierre Pronchery      "CA key format (ENGINE, other values ignored)"},
160*b077aed3SPierre Pronchery     {"CAserial", OPT_CASERIAL, 's',
161*b077aed3SPierre Pronchery      "File that keeps track of CA-generated serial number"},
162*b077aed3SPierre Pronchery     {"CAcreateserial", OPT_CACREATESERIAL, '-',
163*b077aed3SPierre Pronchery      "Create CA serial number file if it does not exist"},
164*b077aed3SPierre Pronchery 
165*b077aed3SPierre Pronchery     OPT_SECTION("Certificate trust output"),
166*b077aed3SPierre Pronchery     {"trustout", OPT_TRUSTOUT, '-', "Mark certificate PEM output as trusted"},
167*b077aed3SPierre Pronchery     {"setalias", OPT_SETALIAS, 's', "Set certificate alias (nickname)"},
168*b077aed3SPierre Pronchery     {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"},
169*b077aed3SPierre Pronchery     {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"},
170*b077aed3SPierre Pronchery     {"clrreject", OPT_CLRREJECT, '-',
171*b077aed3SPierre Pronchery      "Clears all the prohibited or rejected uses of the certificate"},
172*b077aed3SPierre Pronchery     {"addreject", OPT_ADDREJECT, 's',
173*b077aed3SPierre Pronchery      "Reject certificate for a given purpose"},
174*b077aed3SPierre Pronchery 
175*b077aed3SPierre Pronchery     OPT_R_OPTIONS,
176fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
177e71b7053SJung-uk Kim     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
178fceca8a3SJacques Vidrine #endif
179*b077aed3SPierre Pronchery     OPT_PROV_OPTIONS,
180e71b7053SJung-uk Kim     {NULL}
18174664626SKris Kennaway };
18274664626SKris Kennaway 
warn_copying(ASN1_OBJECT * excluded,const char * names)183*b077aed3SPierre Pronchery static void warn_copying(ASN1_OBJECT *excluded, const char *names)
184*b077aed3SPierre Pronchery {
185*b077aed3SPierre Pronchery     const char *sn = OBJ_nid2sn(OBJ_obj2nid(excluded));
186*b077aed3SPierre Pronchery 
187*b077aed3SPierre Pronchery     if (names != NULL && strstr(names, sn) != NULL)
188*b077aed3SPierre Pronchery         BIO_printf(bio_err,
189*b077aed3SPierre Pronchery                    "Warning: -ext should not specify copying %s extension to CSR; ignoring this\n",
190*b077aed3SPierre Pronchery                    sn);
191*b077aed3SPierre Pronchery }
192*b077aed3SPierre Pronchery 
x509_to_req(X509 * cert,int ext_copy,const char * names)193*b077aed3SPierre Pronchery static X509_REQ *x509_to_req(X509 *cert, int ext_copy, const char *names)
194*b077aed3SPierre Pronchery {
195*b077aed3SPierre Pronchery     const STACK_OF(X509_EXTENSION) *cert_exts = X509_get0_extensions(cert);
196*b077aed3SPierre Pronchery     int i, n = sk_X509_EXTENSION_num(cert_exts /* may be NULL */);
197*b077aed3SPierre Pronchery     ASN1_OBJECT *skid = OBJ_nid2obj(NID_subject_key_identifier);
198*b077aed3SPierre Pronchery     ASN1_OBJECT *akid = OBJ_nid2obj(NID_authority_key_identifier);
199*b077aed3SPierre Pronchery     STACK_OF(X509_EXTENSION) *exts;
200*b077aed3SPierre Pronchery     X509_REQ *req = X509_to_X509_REQ(cert, NULL, NULL);
201*b077aed3SPierre Pronchery 
202*b077aed3SPierre Pronchery     if (req == NULL)
203*b077aed3SPierre Pronchery         return NULL;
204*b077aed3SPierre Pronchery 
205*b077aed3SPierre Pronchery     /*
206*b077aed3SPierre Pronchery      * Filter out SKID and AKID extensions, which make no sense in a CSR.
207*b077aed3SPierre Pronchery      * If names is not NULL, copy only those extensions listed there.
208*b077aed3SPierre Pronchery      */
209*b077aed3SPierre Pronchery     warn_copying(skid, names);
210*b077aed3SPierre Pronchery     warn_copying(akid, names);
211*b077aed3SPierre Pronchery     if ((exts = sk_X509_EXTENSION_new_reserve(NULL, n)) == NULL)
212*b077aed3SPierre Pronchery         goto err;
213*b077aed3SPierre Pronchery     for (i = 0; i < n; i++) {
214*b077aed3SPierre Pronchery         X509_EXTENSION *ex = sk_X509_EXTENSION_value(cert_exts, i);
215*b077aed3SPierre Pronchery         ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
216*b077aed3SPierre Pronchery 
217*b077aed3SPierre Pronchery         if (OBJ_cmp(obj, skid) != 0 && OBJ_cmp(obj, akid) != 0
218*b077aed3SPierre Pronchery                 && !sk_X509_EXTENSION_push(exts, ex))
219*b077aed3SPierre Pronchery             goto err;
220*b077aed3SPierre Pronchery     }
221*b077aed3SPierre Pronchery 
222*b077aed3SPierre Pronchery     if (sk_X509_EXTENSION_num(exts) > 0) {
223*b077aed3SPierre Pronchery         if (ext_copy != EXT_COPY_UNSET && ext_copy != EXT_COPY_NONE
224*b077aed3SPierre Pronchery                 && !X509_REQ_add_extensions(req, exts)) {
225*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Error copying extensions from certificate\n");
226*b077aed3SPierre Pronchery             goto err;
227*b077aed3SPierre Pronchery         }
228*b077aed3SPierre Pronchery     }
229*b077aed3SPierre Pronchery     sk_X509_EXTENSION_free(exts);
230*b077aed3SPierre Pronchery     return req;
231*b077aed3SPierre Pronchery 
232*b077aed3SPierre Pronchery  err:
233*b077aed3SPierre Pronchery     sk_X509_EXTENSION_free(exts);
234*b077aed3SPierre Pronchery     X509_REQ_free(req);
235*b077aed3SPierre Pronchery     return NULL;
236*b077aed3SPierre Pronchery }
237*b077aed3SPierre Pronchery 
self_signed(X509_STORE * ctx,X509 * cert)238*b077aed3SPierre Pronchery static int self_signed(X509_STORE *ctx, X509 *cert)
239*b077aed3SPierre Pronchery {
240*b077aed3SPierre Pronchery     X509_STORE_CTX *xsc = X509_STORE_CTX_new();
241*b077aed3SPierre Pronchery     int ret = 0;
242*b077aed3SPierre Pronchery 
243*b077aed3SPierre Pronchery     if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, cert, NULL)) {
244*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Error initialising X509 store\n");
245*b077aed3SPierre Pronchery     } else {
246*b077aed3SPierre Pronchery         X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
247*b077aed3SPierre Pronchery         ret = X509_verify_cert(xsc) > 0;
248*b077aed3SPierre Pronchery     }
249*b077aed3SPierre Pronchery     X509_STORE_CTX_free(xsc);
250*b077aed3SPierre Pronchery     return ret;
251*b077aed3SPierre Pronchery }
252*b077aed3SPierre Pronchery 
x509_main(int argc,char ** argv)253e71b7053SJung-uk Kim int x509_main(int argc, char **argv)
25474664626SKris Kennaway {
2555c87c606SMark Murray     ASN1_INTEGER *sno = NULL;
256e71b7053SJung-uk Kim     ASN1_OBJECT *objtmp = NULL;
25774664626SKris Kennaway     BIO *out = NULL;
258e71b7053SJung-uk Kim     CONF *extconf = NULL;
259*b077aed3SPierre Pronchery     int ext_copy = EXT_COPY_UNSET;
260*b077aed3SPierre Pronchery     X509V3_CTX ext_ctx;
261*b077aed3SPierre Pronchery     EVP_PKEY *privkey = NULL, *CAkey = NULL, *pubkey = NULL;
262*b077aed3SPierre Pronchery     EVP_PKEY *pkey;
263*b077aed3SPierre Pronchery     int newcert = 0;
264*b077aed3SPierre Pronchery     char *subj = NULL, *digest = NULL;
265*b077aed3SPierre Pronchery     X509_NAME *fsubj = NULL;
266*b077aed3SPierre Pronchery     const unsigned long chtype = MBSTRING_ASC;
267*b077aed3SPierre Pronchery     const int multirdn = 1;
268f579bf8eSKris Kennaway     STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
269*b077aed3SPierre Pronchery     STACK_OF(OPENSSL_STRING) *sigopts = NULL, *vfyopts = NULL;
270*b077aed3SPierre Pronchery     X509 *x = NULL, *xca = NULL, *issuer_cert;
271e71b7053SJung-uk Kim     X509_REQ *req = NULL, *rq = NULL;
272e71b7053SJung-uk Kim     X509_STORE *ctx = NULL;
273*b077aed3SPierre Pronchery     char *CAkeyfile = NULL, *CAserial = NULL, *pubkeyfile = NULL, *alias = NULL;
274*b077aed3SPierre Pronchery     char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;
275*b077aed3SPierre Pronchery     char *ext_names = NULL;
276e71b7053SJung-uk Kim     char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;
277*b077aed3SPierre Pronchery     char *infile = NULL, *outfile = NULL, *privkeyfile = NULL, *CAfile = NULL;
278e71b7053SJung-uk Kim     char *prog;
279*b077aed3SPierre Pronchery     int days = UNSET_DAYS; /* not explicitly set */
280*b077aed3SPierre Pronchery     int x509toreq = 0, modulus = 0, print_pubkey = 0, pprint = 0;
281*b077aed3SPierre Pronchery     int CAformat = FORMAT_UNDEF, CAkeyformat = FORMAT_UNDEF;
282*b077aed3SPierre Pronchery     unsigned long dateopt = ASN1_DTFLGS_RFC822;
283e71b7053SJung-uk Kim     int fingerprint = 0, reqfile = 0, checkend = 0;
284*b077aed3SPierre Pronchery     int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, keyformat = FORMAT_UNDEF;
285e71b7053SJung-uk Kim     int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;
286*b077aed3SPierre Pronchery     int noout = 0, CA_createserial = 0, email = 0;
287e71b7053SJung-uk Kim     int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;
288*b077aed3SPierre Pronchery     int ret = 1, i, j, num = 0, badsig = 0, clrext = 0, nocert = 0;
289e71b7053SJung-uk Kim     int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0, ext = 0;
290e71b7053SJung-uk Kim     int enddate = 0;
291e71b7053SJung-uk Kim     time_t checkoffset = 0;
292e71b7053SJung-uk Kim     unsigned long certflag = 0;
293e71b7053SJung-uk Kim     int preserve_dates = 0;
294e71b7053SJung-uk Kim     OPTION_CHOICE o;
295e71b7053SJung-uk Kim     ENGINE *e = NULL;
2961f13597dSJung-uk Kim #ifndef OPENSSL_NO_MD5
2971f13597dSJung-uk Kim     int subject_hash_old = 0, issuer_hash_old = 0;
2981f13597dSJung-uk Kim #endif
29974664626SKris Kennaway 
30074664626SKris Kennaway     ctx = X509_STORE_new();
3016f9291ceSJung-uk Kim     if (ctx == NULL)
3026f9291ceSJung-uk Kim         goto end;
3031f13597dSJung-uk Kim     X509_STORE_set_verify_cb(ctx, callb);
30474664626SKris Kennaway 
305e71b7053SJung-uk Kim     prog = opt_init(argc, argv, x509_options);
306e71b7053SJung-uk Kim     while ((o = opt_next()) != OPT_EOF) {
307e71b7053SJung-uk Kim         switch (o) {
308e71b7053SJung-uk Kim         case OPT_EOF:
309e71b7053SJung-uk Kim         case OPT_ERR:
310e71b7053SJung-uk Kim  opthelp:
311e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
312e71b7053SJung-uk Kim             goto end;
313e71b7053SJung-uk Kim         case OPT_HELP:
314e71b7053SJung-uk Kim             opt_help(x509_options);
315e71b7053SJung-uk Kim             ret = 0;
316e71b7053SJung-uk Kim             goto end;
317e71b7053SJung-uk Kim         case OPT_INFORM:
318*b077aed3SPierre Pronchery             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
319e71b7053SJung-uk Kim                 goto opthelp;
320e71b7053SJung-uk Kim             break;
321e71b7053SJung-uk Kim         case OPT_IN:
322e71b7053SJung-uk Kim             infile = opt_arg();
323e71b7053SJung-uk Kim             break;
324e71b7053SJung-uk Kim         case OPT_OUTFORM:
325e71b7053SJung-uk Kim             if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
326e71b7053SJung-uk Kim                 goto opthelp;
327e71b7053SJung-uk Kim             break;
328e71b7053SJung-uk Kim         case OPT_KEYFORM:
329*b077aed3SPierre Pronchery             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
330e71b7053SJung-uk Kim                 goto opthelp;
331e71b7053SJung-uk Kim             break;
332e71b7053SJung-uk Kim         case OPT_CAFORM:
333*b077aed3SPierre Pronchery             if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAformat))
334e71b7053SJung-uk Kim                 goto opthelp;
335e71b7053SJung-uk Kim             break;
336e71b7053SJung-uk Kim         case OPT_CAKEYFORM:
337*b077aed3SPierre Pronchery             if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat))
338e71b7053SJung-uk Kim                 goto opthelp;
339e71b7053SJung-uk Kim             break;
340e71b7053SJung-uk Kim         case OPT_OUT:
341e71b7053SJung-uk Kim             outfile = opt_arg();
342e71b7053SJung-uk Kim             break;
343e71b7053SJung-uk Kim         case OPT_REQ:
34474664626SKris Kennaway             reqfile = 1;
345e71b7053SJung-uk Kim             break;
346e71b7053SJung-uk Kim 
347*b077aed3SPierre Pronchery         case OPT_DATEOPT:
348*b077aed3SPierre Pronchery             if (!set_dateopt(&dateopt, opt_arg())) {
349*b077aed3SPierre Pronchery                 BIO_printf(bio_err,
350*b077aed3SPierre Pronchery                            "Invalid date format: %s\n", opt_arg());
351*b077aed3SPierre Pronchery                 goto end;
352*b077aed3SPierre Pronchery             }
353*b077aed3SPierre Pronchery             break;
354*b077aed3SPierre Pronchery         case OPT_COPY_EXTENSIONS:
355*b077aed3SPierre Pronchery             if (!set_ext_copy(&ext_copy, opt_arg())) {
356*b077aed3SPierre Pronchery                 BIO_printf(bio_err,
357*b077aed3SPierre Pronchery                            "Invalid extension copy option: %s\n", opt_arg());
358*b077aed3SPierre Pronchery                 goto end;
359*b077aed3SPierre Pronchery             }
360*b077aed3SPierre Pronchery             break;
361*b077aed3SPierre Pronchery 
362e71b7053SJung-uk Kim         case OPT_SIGOPT:
3631f13597dSJung-uk Kim             if (!sigopts)
3641f13597dSJung-uk Kim                 sigopts = sk_OPENSSL_STRING_new_null();
365e71b7053SJung-uk Kim             if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
366e71b7053SJung-uk Kim                 goto opthelp;
36774664626SKris Kennaway             break;
368*b077aed3SPierre Pronchery         case OPT_VFYOPT:
369*b077aed3SPierre Pronchery             if (!vfyopts)
370*b077aed3SPierre Pronchery                 vfyopts = sk_OPENSSL_STRING_new_null();
371*b077aed3SPierre Pronchery             if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
372e71b7053SJung-uk Kim                 goto opthelp;
373*b077aed3SPierre Pronchery             break;
374*b077aed3SPierre Pronchery         case OPT_DAYS:
375e71b7053SJung-uk Kim             days = atoi(opt_arg());
376*b077aed3SPierre Pronchery             if (days < -1) {
377*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "%s: -days parameter arg must be >= -1\n",
378*b077aed3SPierre Pronchery                            prog);
379*b077aed3SPierre Pronchery                 goto end;
380*b077aed3SPierre Pronchery             }
381e71b7053SJung-uk Kim             break;
382e71b7053SJung-uk Kim         case OPT_PASSIN:
383e71b7053SJung-uk Kim             passinarg = opt_arg();
384e71b7053SJung-uk Kim             break;
385e71b7053SJung-uk Kim         case OPT_EXTFILE:
386e71b7053SJung-uk Kim             extfile = opt_arg();
387e71b7053SJung-uk Kim             break;
388e71b7053SJung-uk Kim         case OPT_R_CASES:
389e71b7053SJung-uk Kim             if (!opt_rand(o))
39074664626SKris Kennaway                 goto end;
391e71b7053SJung-uk Kim             break;
392*b077aed3SPierre Pronchery         case OPT_PROV_CASES:
393*b077aed3SPierre Pronchery             if (!opt_provider(o))
394*b077aed3SPierre Pronchery                 goto end;
395*b077aed3SPierre Pronchery             break;
396e71b7053SJung-uk Kim         case OPT_EXTENSIONS:
397e71b7053SJung-uk Kim             extsect = opt_arg();
398e71b7053SJung-uk Kim             break;
399*b077aed3SPierre Pronchery         case OPT_KEY:
400e71b7053SJung-uk Kim         case OPT_SIGNKEY:
401*b077aed3SPierre Pronchery             privkeyfile = opt_arg();
402e71b7053SJung-uk Kim             break;
403e71b7053SJung-uk Kim         case OPT_CA:
404e71b7053SJung-uk Kim             CAfile = opt_arg();
405e71b7053SJung-uk Kim             break;
406e71b7053SJung-uk Kim         case OPT_CAKEY:
407e71b7053SJung-uk Kim             CAkeyfile = opt_arg();
408e71b7053SJung-uk Kim             break;
409e71b7053SJung-uk Kim         case OPT_CASERIAL:
410e71b7053SJung-uk Kim             CAserial = opt_arg();
411e71b7053SJung-uk Kim             break;
412e71b7053SJung-uk Kim         case OPT_SET_SERIAL:
413e71b7053SJung-uk Kim             if (sno != NULL) {
414e71b7053SJung-uk Kim                 BIO_printf(bio_err, "Serial number supplied twice\n");
415e71b7053SJung-uk Kim                 goto opthelp;
41674664626SKris Kennaway             }
417e71b7053SJung-uk Kim             if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
418e71b7053SJung-uk Kim                 goto opthelp;
419e71b7053SJung-uk Kim             break;
420*b077aed3SPierre Pronchery         case OPT_NEW:
421*b077aed3SPierre Pronchery             newcert = 1;
422*b077aed3SPierre Pronchery             break;
423e71b7053SJung-uk Kim         case OPT_FORCE_PUBKEY:
424*b077aed3SPierre Pronchery             pubkeyfile = opt_arg();
425*b077aed3SPierre Pronchery             break;
426*b077aed3SPierre Pronchery         case OPT_SUBJ:
427*b077aed3SPierre Pronchery             subj = opt_arg();
428e71b7053SJung-uk Kim             break;
429e71b7053SJung-uk Kim         case OPT_ADDTRUST:
430*b077aed3SPierre Pronchery             if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
431*b077aed3SPierre Pronchery                 goto end;
432e71b7053SJung-uk Kim             if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
433*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "%s: Invalid trust object value %s\n",
434e71b7053SJung-uk Kim                            prog, opt_arg());
435e71b7053SJung-uk Kim                 goto opthelp;
436e71b7053SJung-uk Kim             }
437e71b7053SJung-uk Kim             sk_ASN1_OBJECT_push(trust, objtmp);
438e71b7053SJung-uk Kim             trustout = 1;
439e71b7053SJung-uk Kim             break;
440e71b7053SJung-uk Kim         case OPT_ADDREJECT:
441*b077aed3SPierre Pronchery             if (reject == NULL && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
442*b077aed3SPierre Pronchery                 goto end;
443e71b7053SJung-uk Kim             if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
444*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "%s: Invalid reject object value %s\n",
445e71b7053SJung-uk Kim                            prog, opt_arg());
446e71b7053SJung-uk Kim                 goto opthelp;
447e71b7053SJung-uk Kim             }
448e71b7053SJung-uk Kim             sk_ASN1_OBJECT_push(reject, objtmp);
449e71b7053SJung-uk Kim             trustout = 1;
450e71b7053SJung-uk Kim             break;
451e71b7053SJung-uk Kim         case OPT_SETALIAS:
452e71b7053SJung-uk Kim             alias = opt_arg();
453e71b7053SJung-uk Kim             trustout = 1;
454e71b7053SJung-uk Kim             break;
455e71b7053SJung-uk Kim         case OPT_CERTOPT:
456e71b7053SJung-uk Kim             if (!set_cert_ex(&certflag, opt_arg()))
457e71b7053SJung-uk Kim                 goto opthelp;
458e71b7053SJung-uk Kim             break;
459e71b7053SJung-uk Kim         case OPT_NAMEOPT:
460e71b7053SJung-uk Kim             if (!set_nameopt(opt_arg()))
461e71b7053SJung-uk Kim                 goto opthelp;
462e71b7053SJung-uk Kim             break;
463e71b7053SJung-uk Kim         case OPT_ENGINE:
464e71b7053SJung-uk Kim             e = setup_engine(opt_arg(), 0);
465e71b7053SJung-uk Kim             break;
466e71b7053SJung-uk Kim         case OPT_EMAIL:
467e71b7053SJung-uk Kim             email = ++num;
468e71b7053SJung-uk Kim             break;
469e71b7053SJung-uk Kim         case OPT_OCSP_URI:
470e71b7053SJung-uk Kim             ocsp_uri = ++num;
471e71b7053SJung-uk Kim             break;
472e71b7053SJung-uk Kim         case OPT_SERIAL:
473e71b7053SJung-uk Kim             serial = ++num;
474e71b7053SJung-uk Kim             break;
475e71b7053SJung-uk Kim         case OPT_NEXT_SERIAL:
476e71b7053SJung-uk Kim             next_serial = ++num;
477e71b7053SJung-uk Kim             break;
478e71b7053SJung-uk Kim         case OPT_MODULUS:
479e71b7053SJung-uk Kim             modulus = ++num;
480e71b7053SJung-uk Kim             break;
481e71b7053SJung-uk Kim         case OPT_PUBKEY:
482*b077aed3SPierre Pronchery             print_pubkey = ++num;
483e71b7053SJung-uk Kim             break;
484e71b7053SJung-uk Kim         case OPT_X509TOREQ:
485*b077aed3SPierre Pronchery             x509toreq = 1;
486e71b7053SJung-uk Kim             break;
487e71b7053SJung-uk Kim         case OPT_TEXT:
488e71b7053SJung-uk Kim             text = ++num;
489e71b7053SJung-uk Kim             break;
490e71b7053SJung-uk Kim         case OPT_SUBJECT:
491e71b7053SJung-uk Kim             subject = ++num;
492e71b7053SJung-uk Kim             break;
493e71b7053SJung-uk Kim         case OPT_ISSUER:
494e71b7053SJung-uk Kim             issuer = ++num;
495e71b7053SJung-uk Kim             break;
496e71b7053SJung-uk Kim         case OPT_FINGERPRINT:
497e71b7053SJung-uk Kim             fingerprint = ++num;
498e71b7053SJung-uk Kim             break;
499e71b7053SJung-uk Kim         case OPT_HASH:
500e71b7053SJung-uk Kim             subject_hash = ++num;
501e71b7053SJung-uk Kim             break;
502e71b7053SJung-uk Kim         case OPT_ISSUER_HASH:
503e71b7053SJung-uk Kim             issuer_hash = ++num;
504e71b7053SJung-uk Kim             break;
505e71b7053SJung-uk Kim         case OPT_PURPOSE:
506e71b7053SJung-uk Kim             pprint = ++num;
507e71b7053SJung-uk Kim             break;
508e71b7053SJung-uk Kim         case OPT_STARTDATE:
509e71b7053SJung-uk Kim             startdate = ++num;
510e71b7053SJung-uk Kim             break;
511e71b7053SJung-uk Kim         case OPT_ENDDATE:
512e71b7053SJung-uk Kim             enddate = ++num;
513e71b7053SJung-uk Kim             break;
514e71b7053SJung-uk Kim         case OPT_NOOUT:
515e71b7053SJung-uk Kim             noout = ++num;
516e71b7053SJung-uk Kim             break;
517e71b7053SJung-uk Kim         case OPT_EXT:
518e71b7053SJung-uk Kim             ext = ++num;
519*b077aed3SPierre Pronchery             ext_names = opt_arg();
520e71b7053SJung-uk Kim             break;
521e71b7053SJung-uk Kim         case OPT_NOCERT:
522e71b7053SJung-uk Kim             nocert = 1;
523e71b7053SJung-uk Kim             break;
524e71b7053SJung-uk Kim         case OPT_TRUSTOUT:
525e71b7053SJung-uk Kim             trustout = 1;
526e71b7053SJung-uk Kim             break;
527e71b7053SJung-uk Kim         case OPT_CLRTRUST:
528e71b7053SJung-uk Kim             clrtrust = ++num;
529e71b7053SJung-uk Kim             break;
530e71b7053SJung-uk Kim         case OPT_CLRREJECT:
531e71b7053SJung-uk Kim             clrreject = ++num;
532e71b7053SJung-uk Kim             break;
533e71b7053SJung-uk Kim         case OPT_ALIAS:
534e71b7053SJung-uk Kim             aliasout = ++num;
535e71b7053SJung-uk Kim             break;
536e71b7053SJung-uk Kim         case OPT_CACREATESERIAL:
537cfc39718SJung-uk Kim             CA_createserial = 1;
538e71b7053SJung-uk Kim             break;
539e71b7053SJung-uk Kim         case OPT_CLREXT:
540e71b7053SJung-uk Kim             clrext = 1;
541e71b7053SJung-uk Kim             break;
542e71b7053SJung-uk Kim         case OPT_OCSPID:
543e71b7053SJung-uk Kim             ocspid = ++num;
544e71b7053SJung-uk Kim             break;
545e71b7053SJung-uk Kim         case OPT_BADSIG:
546e71b7053SJung-uk Kim             badsig = 1;
547e71b7053SJung-uk Kim             break;
548e71b7053SJung-uk Kim #ifndef OPENSSL_NO_MD5
549e71b7053SJung-uk Kim         case OPT_SUBJECT_HASH_OLD:
550e71b7053SJung-uk Kim             subject_hash_old = ++num;
551e71b7053SJung-uk Kim             break;
552e71b7053SJung-uk Kim         case OPT_ISSUER_HASH_OLD:
553e71b7053SJung-uk Kim             issuer_hash_old = ++num;
554e71b7053SJung-uk Kim             break;
555e71b7053SJung-uk Kim #else
556e71b7053SJung-uk Kim         case OPT_SUBJECT_HASH_OLD:
557e71b7053SJung-uk Kim         case OPT_ISSUER_HASH_OLD:
558e71b7053SJung-uk Kim             break;
559e71b7053SJung-uk Kim #endif
560e71b7053SJung-uk Kim         case OPT_DATES:
561e71b7053SJung-uk Kim             startdate = ++num;
562e71b7053SJung-uk Kim             enddate = ++num;
563e71b7053SJung-uk Kim             break;
564e71b7053SJung-uk Kim         case OPT_CHECKEND:
565e71b7053SJung-uk Kim             checkend = 1;
566e71b7053SJung-uk Kim             {
567*b077aed3SPierre Pronchery                 ossl_intmax_t temp = 0;
568*b077aed3SPierre Pronchery                 if (!opt_intmax(opt_arg(), &temp))
569e71b7053SJung-uk Kim                     goto opthelp;
570e71b7053SJung-uk Kim                 checkoffset = (time_t)temp;
571*b077aed3SPierre Pronchery                 if ((ossl_intmax_t)checkoffset != temp) {
572*b077aed3SPierre Pronchery                     BIO_printf(bio_err, "%s: Checkend time out of range %s\n",
573e71b7053SJung-uk Kim                                prog, opt_arg());
574e71b7053SJung-uk Kim                     goto opthelp;
575e71b7053SJung-uk Kim                 }
576e71b7053SJung-uk Kim             }
577e71b7053SJung-uk Kim             break;
578e71b7053SJung-uk Kim         case OPT_CHECKHOST:
579e71b7053SJung-uk Kim             checkhost = opt_arg();
580e71b7053SJung-uk Kim             break;
581e71b7053SJung-uk Kim         case OPT_CHECKEMAIL:
582e71b7053SJung-uk Kim             checkemail = opt_arg();
583e71b7053SJung-uk Kim             break;
584e71b7053SJung-uk Kim         case OPT_CHECKIP:
585e71b7053SJung-uk Kim             checkip = opt_arg();
586e71b7053SJung-uk Kim             break;
587e71b7053SJung-uk Kim         case OPT_PRESERVE_DATES:
588e71b7053SJung-uk Kim             preserve_dates = 1;
589e71b7053SJung-uk Kim             break;
590e71b7053SJung-uk Kim         case OPT_MD:
591*b077aed3SPierre Pronchery             digest = opt_unknown();
592*b077aed3SPierre Pronchery             break;
593e71b7053SJung-uk Kim         }
594e71b7053SJung-uk Kim     }
595*b077aed3SPierre Pronchery 
596*b077aed3SPierre Pronchery     /* No extra arguments. */
597e71b7053SJung-uk Kim     argc = opt_num_rest();
598*b077aed3SPierre Pronchery     if (argc != 0)
599e71b7053SJung-uk Kim         goto opthelp;
600*b077aed3SPierre Pronchery 
601*b077aed3SPierre Pronchery     if (!app_RAND_load())
602*b077aed3SPierre Pronchery         goto end;
603*b077aed3SPierre Pronchery 
604*b077aed3SPierre Pronchery     if (preserve_dates && days != UNSET_DAYS) {
605*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Cannot use -preserve_dates with -days option\n");
606*b077aed3SPierre Pronchery         goto end;
607e71b7053SJung-uk Kim     }
608*b077aed3SPierre Pronchery     if (days == UNSET_DAYS)
609*b077aed3SPierre Pronchery         days = DEFAULT_DAYS;
6105c87c606SMark Murray 
611e71b7053SJung-uk Kim     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
612f579bf8eSKris Kennaway         BIO_printf(bio_err, "Error getting password\n");
613f579bf8eSKris Kennaway         goto end;
614f579bf8eSKris Kennaway     }
61574664626SKris Kennaway 
616*b077aed3SPierre Pronchery     if (!X509_STORE_set_default_paths_ex(ctx, app_get0_libctx(),
617*b077aed3SPierre Pronchery                                          app_get0_propq()))
618*b077aed3SPierre Pronchery         goto end;
619*b077aed3SPierre Pronchery 
620*b077aed3SPierre Pronchery     if (newcert && infile != NULL) {
621*b077aed3SPierre Pronchery         BIO_printf(bio_err, "The -in option cannot be used with -new\n");
62274664626SKris Kennaway         goto end;
62374664626SKris Kennaway     }
624*b077aed3SPierre Pronchery     if (newcert && reqfile) {
6256f9291ceSJung-uk Kim         BIO_printf(bio_err,
626*b077aed3SPierre Pronchery                    "The -req option cannot be used with -new\n");
627*b077aed3SPierre Pronchery         goto end;
628*b077aed3SPierre Pronchery     }
629*b077aed3SPierre Pronchery     if (privkeyfile != NULL) {
630*b077aed3SPierre Pronchery         privkey = load_key(privkeyfile, keyformat, 0, passin, e, "private key");
631*b077aed3SPierre Pronchery         if (privkey == NULL)
632*b077aed3SPierre Pronchery             goto end;
633*b077aed3SPierre Pronchery     }
634*b077aed3SPierre Pronchery     if (pubkeyfile != NULL) {
635*b077aed3SPierre Pronchery         if ((pubkey = load_pubkey(pubkeyfile, keyformat, 0, NULL, e,
636*b077aed3SPierre Pronchery                                   "explicitly set public key")) == NULL)
63774664626SKris Kennaway             goto end;
63874664626SKris Kennaway     }
63974664626SKris Kennaway 
640*b077aed3SPierre Pronchery     if (newcert) {
641*b077aed3SPierre Pronchery         if (subj == NULL) {
642*b077aed3SPierre Pronchery             BIO_printf(bio_err,
643*b077aed3SPierre Pronchery                        "The -new option requires a subject to be set using -subj\n");
644*b077aed3SPierre Pronchery             goto end;
645*b077aed3SPierre Pronchery         }
646*b077aed3SPierre Pronchery         if (privkeyfile == NULL && pubkeyfile == NULL) {
647*b077aed3SPierre Pronchery             BIO_printf(bio_err,
648*b077aed3SPierre Pronchery                        "The -new option requires using the -key or -force_pubkey option\n");
649*b077aed3SPierre Pronchery             goto end;
650*b077aed3SPierre Pronchery         }
651*b077aed3SPierre Pronchery     }
652*b077aed3SPierre Pronchery     if (subj != NULL
653*b077aed3SPierre Pronchery             && (fsubj = parse_name(subj, chtype, multirdn, "subject")) == NULL)
654*b077aed3SPierre Pronchery         goto end;
655*b077aed3SPierre Pronchery 
656*b077aed3SPierre Pronchery     if (CAkeyfile == NULL)
657*b077aed3SPierre Pronchery         CAkeyfile = CAfile;
658*b077aed3SPierre Pronchery     if (CAfile != NULL) {
659*b077aed3SPierre Pronchery         if (privkeyfile != NULL) {
660*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Cannot use both -key/-signkey and -CA option\n");
661*b077aed3SPierre Pronchery             goto end;
662*b077aed3SPierre Pronchery         }
663*b077aed3SPierre Pronchery     } else {
664*b077aed3SPierre Pronchery #define WARN_NO_CA(opt) BIO_printf(bio_err, \
665*b077aed3SPierre Pronchery         "Warning: ignoring " opt " option since -CA option is not given\n");
666*b077aed3SPierre Pronchery         if (CAkeyfile != NULL)
667*b077aed3SPierre Pronchery             WARN_NO_CA("-CAkey");
668*b077aed3SPierre Pronchery         if (CAkeyformat != FORMAT_UNDEF)
669*b077aed3SPierre Pronchery             WARN_NO_CA("-CAkeyform");
670*b077aed3SPierre Pronchery         if (CAformat != FORMAT_UNDEF)
671*b077aed3SPierre Pronchery             WARN_NO_CA("-CAform");
672*b077aed3SPierre Pronchery         if (CAserial != NULL)
673*b077aed3SPierre Pronchery             WARN_NO_CA("-CAserial");
674*b077aed3SPierre Pronchery         if (CA_createserial)
675*b077aed3SPierre Pronchery             WARN_NO_CA("-CAcreateserial");
676*b077aed3SPierre Pronchery     }
677*b077aed3SPierre Pronchery 
678*b077aed3SPierre Pronchery     if (extfile == NULL) {
679*b077aed3SPierre Pronchery         if (extsect != NULL)
680*b077aed3SPierre Pronchery             BIO_printf(bio_err,
681*b077aed3SPierre Pronchery                        "Warning: ignoring -extensions option without -extfile\n");
682*b077aed3SPierre Pronchery     } else {
68374664626SKris Kennaway         X509V3_CTX ctx2;
684*b077aed3SPierre Pronchery 
685e71b7053SJung-uk Kim         if ((extconf = app_load_config(extfile)) == NULL)
68674664626SKris Kennaway             goto end;
687e71b7053SJung-uk Kim         if (extsect == NULL) {
6885c87c606SMark Murray             extsect = NCONF_get_string(extconf, "default", "extensions");
689e71b7053SJung-uk Kim             if (extsect == NULL) {
6905c87c606SMark Murray                 ERR_clear_error();
6915c87c606SMark Murray                 extsect = "default";
6925c87c606SMark Murray             }
6935c87c606SMark Murray         }
69474664626SKris Kennaway         X509V3_set_ctx_test(&ctx2);
6955c87c606SMark Murray         X509V3_set_nconf(&ctx2, extconf);
6966f9291ceSJung-uk Kim         if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
69774664626SKris Kennaway             BIO_printf(bio_err,
698*b077aed3SPierre Pronchery                        "Error checking extension section %s\n", extsect);
69974664626SKris Kennaway             goto end;
70074664626SKris Kennaway         }
70174664626SKris Kennaway     }
70274664626SKris Kennaway 
7036f9291ceSJung-uk Kim     if (reqfile) {
704*b077aed3SPierre Pronchery         req = load_csr(infile, informat, "certificate request input");
705*b077aed3SPierre Pronchery         if (req == NULL)
70674664626SKris Kennaway             goto end;
70774664626SKris Kennaway 
708e71b7053SJung-uk Kim         if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) {
709*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Error unpacking public key from CSR\n");
71074664626SKris Kennaway             goto end;
71174664626SKris Kennaway         }
712*b077aed3SPierre Pronchery         i = do_X509_REQ_verify(req, pkey, vfyopts);
713*b077aed3SPierre Pronchery         if (i <= 0) {
714*b077aed3SPierre Pronchery             BIO_printf(bio_err, i < 0
715*b077aed3SPierre Pronchery                        ? "Error while verifying certificate request self-signature\n"
716*b077aed3SPierre Pronchery                        : "Certificate request self-signature did not match the contents\n");
71774664626SKris Kennaway             goto end;
71874664626SKris Kennaway         }
719*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Certificate request self-signature ok\n");
720*b077aed3SPierre Pronchery 
721*b077aed3SPierre Pronchery         print_name(bio_err, "subject=", X509_REQ_get_subject_name(req));
722*b077aed3SPierre Pronchery     } else if (!x509toreq && ext_copy != EXT_COPY_UNSET) {
723*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Warning: ignoring -copy_extensions since neither -x509toreq nor -req is given\n");
724*b077aed3SPierre Pronchery     }
725*b077aed3SPierre Pronchery 
726*b077aed3SPierre Pronchery     if (reqfile || newcert) {
727*b077aed3SPierre Pronchery         if (preserve_dates)
7286f9291ceSJung-uk Kim             BIO_printf(bio_err,
729*b077aed3SPierre Pronchery                        "Warning: ignoring -preserve_dates option with -req or -new\n");
730*b077aed3SPierre Pronchery         preserve_dates = 0;
731*b077aed3SPierre Pronchery         if (privkeyfile == NULL && CAkeyfile == NULL) {
732*b077aed3SPierre Pronchery             BIO_printf(bio_err,
733*b077aed3SPierre Pronchery                        "We need a private key to sign with, use -key or -CAkey or -CA with private key\n");
73474664626SKris Kennaway             goto end;
735e71b7053SJung-uk Kim         }
736*b077aed3SPierre Pronchery         if ((x = X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL)
7376f9291ceSJung-uk Kim             goto end;
738*b077aed3SPierre Pronchery         if (CAfile == NULL && sno == NULL) {
7396be8ae07SJacques Vidrine             sno = ASN1_INTEGER_new();
740e71b7053SJung-uk Kim             if (sno == NULL || !rand_serial(NULL, sno))
7415c87c606SMark Murray                 goto end;
742*b077aed3SPierre Pronchery         }
743*b077aed3SPierre Pronchery         if (req != NULL && ext_copy != EXT_COPY_UNSET) {
744*b077aed3SPierre Pronchery             if (clrext && ext_copy != EXT_COPY_NONE) {
745*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Must not use -clrext together with -copy_extensions\n");
7466be8ae07SJacques Vidrine                 goto end;
747*b077aed3SPierre Pronchery             } else if (!copy_extensions(x, req, ext_copy)) {
748*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Error copying extensions from request\n");
7493b4e3dcbSSimon L. B. Nielsen                 goto end;
7507bded2dbSJung-uk Kim             }
751e71b7053SJung-uk Kim         }
752e71b7053SJung-uk Kim     } else {
753*b077aed3SPierre Pronchery         x = load_cert_pass(infile, informat, 1, passin, "certificate");
7546f9291ceSJung-uk Kim         if (x == NULL)
7556f9291ceSJung-uk Kim             goto end;
756*b077aed3SPierre Pronchery     }
757*b077aed3SPierre Pronchery     if ((fsubj != NULL || req != NULL)
758*b077aed3SPierre Pronchery         && !X509_set_subject_name(x, fsubj != NULL ? fsubj :
759*b077aed3SPierre Pronchery                                   X509_REQ_get_subject_name(req)))
7606f9291ceSJung-uk Kim         goto end;
761*b077aed3SPierre Pronchery     if ((pubkey != NULL || privkey != NULL || req != NULL)
762*b077aed3SPierre Pronchery         && !X509_set_pubkey(x, pubkey != NULL ? pubkey :
763*b077aed3SPierre Pronchery                             privkey != NULL ? privkey :
764*b077aed3SPierre Pronchery                             X509_REQ_get0_pubkey(req)))
765*b077aed3SPierre Pronchery         goto end;
766*b077aed3SPierre Pronchery 
767*b077aed3SPierre Pronchery     if (CAfile != NULL) {
768*b077aed3SPierre Pronchery         xca = load_cert_pass(CAfile, CAformat, 1, passin, "CA certificate");
769*b077aed3SPierre Pronchery         if (xca == NULL)
77034252e89SJung-uk Kim             goto end;
77174664626SKris Kennaway     }
77274664626SKris Kennaway 
773e71b7053SJung-uk Kim     out = bio_open_default(outfile, 'w', outformat);
774e71b7053SJung-uk Kim     if (out == NULL)
775e71b7053SJung-uk Kim         goto end;
77674664626SKris Kennaway 
777e71b7053SJung-uk Kim     if (!noout || text || next_serial)
778e71b7053SJung-uk Kim         OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
77974664626SKris Kennaway 
7806f9291ceSJung-uk Kim     if (alias)
7816f9291ceSJung-uk Kim         X509_alias_set1(x, (unsigned char *)alias, -1);
782f579bf8eSKris Kennaway 
7836f9291ceSJung-uk Kim     if (clrtrust)
7846f9291ceSJung-uk Kim         X509_trust_clear(x);
7856f9291ceSJung-uk Kim     if (clrreject)
7866f9291ceSJung-uk Kim         X509_reject_clear(x);
787f579bf8eSKris Kennaway 
788e71b7053SJung-uk Kim     if (trust != NULL) {
789*b077aed3SPierre Pronchery         for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++)
790*b077aed3SPierre Pronchery             X509_add1_trust_object(x, sk_ASN1_OBJECT_value(trust, i));
791f579bf8eSKris Kennaway     }
792f579bf8eSKris Kennaway 
793e71b7053SJung-uk Kim     if (reject != NULL) {
794*b077aed3SPierre Pronchery         for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++)
795*b077aed3SPierre Pronchery             X509_add1_reject_object(x, sk_ASN1_OBJECT_value(reject, i));
796e71b7053SJung-uk Kim     }
797e71b7053SJung-uk Kim 
798*b077aed3SPierre Pronchery     if (clrext && ext_names != NULL)
799*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Warning: Ignoring -ext since -clrext is given\n");
800*b077aed3SPierre Pronchery     for (i = X509_get_ext_count(x) - 1; i >= 0; i--) {
801*b077aed3SPierre Pronchery         X509_EXTENSION *ex = X509_get_ext(x, i);
802*b077aed3SPierre Pronchery         const char *sn = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ex)));
803*b077aed3SPierre Pronchery 
804*b077aed3SPierre Pronchery         if (clrext || (ext_names != NULL && strstr(ext_names, sn) == NULL))
805*b077aed3SPierre Pronchery             X509_EXTENSION_free(X509_delete_ext(x, i));
806*b077aed3SPierre Pronchery     }
807*b077aed3SPierre Pronchery 
808*b077aed3SPierre Pronchery     issuer_cert = x;
809*b077aed3SPierre Pronchery     if (CAfile != NULL) {
810*b077aed3SPierre Pronchery         issuer_cert = xca;
811*b077aed3SPierre Pronchery         if (sno == NULL)
812*b077aed3SPierre Pronchery             sno = x509_load_serial(CAfile, CAserial, CA_createserial);
813*b077aed3SPierre Pronchery         if (sno == NULL)
814*b077aed3SPierre Pronchery             goto end;
815*b077aed3SPierre Pronchery         if (!x509toreq && !reqfile && !newcert && !self_signed(ctx, x))
816*b077aed3SPierre Pronchery             goto end;
817*b077aed3SPierre Pronchery     }
818*b077aed3SPierre Pronchery 
819*b077aed3SPierre Pronchery     if (sno != NULL && !X509_set_serialNumber(x, sno))
820*b077aed3SPierre Pronchery         goto end;
821*b077aed3SPierre Pronchery 
822*b077aed3SPierre Pronchery     if (reqfile || newcert || privkey != NULL || CAfile != NULL) {
823*b077aed3SPierre Pronchery         if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
824*b077aed3SPierre Pronchery             goto end;
825*b077aed3SPierre Pronchery         if (!X509_set_issuer_name(x, X509_get_subject_name(issuer_cert)))
826*b077aed3SPierre Pronchery             goto end;
827*b077aed3SPierre Pronchery     }
828*b077aed3SPierre Pronchery 
829*b077aed3SPierre Pronchery     X509V3_set_ctx(&ext_ctx, issuer_cert, x, NULL, NULL, X509V3_CTX_REPLACE);
830*b077aed3SPierre Pronchery     /* prepare fallback for AKID, but only if issuer cert equals subject cert */
831*b077aed3SPierre Pronchery     if (CAfile == NULL) {
832*b077aed3SPierre Pronchery         if (!X509V3_set_issuer_pkey(&ext_ctx, privkey))
833*b077aed3SPierre Pronchery             goto end;
834*b077aed3SPierre Pronchery     }
835*b077aed3SPierre Pronchery     if (extconf != NULL && !x509toreq) {
836*b077aed3SPierre Pronchery         X509V3_set_nconf(&ext_ctx, extconf);
837*b077aed3SPierre Pronchery         if (!X509V3_EXT_add_nconf(extconf, &ext_ctx, extsect, x)) {
838*b077aed3SPierre Pronchery             BIO_printf(bio_err,
839*b077aed3SPierre Pronchery                        "Error adding extensions from section %s\n", extsect);
840*b077aed3SPierre Pronchery             goto end;
841*b077aed3SPierre Pronchery         }
842*b077aed3SPierre Pronchery     }
843*b077aed3SPierre Pronchery 
844*b077aed3SPierre Pronchery     /* At this point the contents of the certificate x have been finished. */
845*b077aed3SPierre Pronchery 
846*b077aed3SPierre Pronchery     pkey = X509_get0_pubkey(x);
847*b077aed3SPierre Pronchery     if ((print_pubkey != 0 || modulus != 0) && pkey == NULL) {
848*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Error getting public key\n");
849*b077aed3SPierre Pronchery         goto end;
850*b077aed3SPierre Pronchery     }
851*b077aed3SPierre Pronchery 
852*b077aed3SPierre Pronchery     if (x509toreq) { /* also works in conjunction with -req */
853*b077aed3SPierre Pronchery         if (privkey == NULL) {
854*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Must specify request signing key using -key\n");
855*b077aed3SPierre Pronchery             goto end;
856*b077aed3SPierre Pronchery         }
857*b077aed3SPierre Pronchery         if (clrext && ext_copy != EXT_COPY_NONE) {
858*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Must not use -clrext together with -copy_extensions\n");
859*b077aed3SPierre Pronchery             goto end;
860*b077aed3SPierre Pronchery         }
861*b077aed3SPierre Pronchery         if ((rq = x509_to_req(x, ext_copy, ext_names)) == NULL)
862*b077aed3SPierre Pronchery             goto end;
863*b077aed3SPierre Pronchery         if (extconf != NULL) {
864*b077aed3SPierre Pronchery             X509V3_set_nconf(&ext_ctx, extconf);
865*b077aed3SPierre Pronchery             if (!X509V3_EXT_REQ_add_nconf(extconf, &ext_ctx, extsect, rq)) {
866*b077aed3SPierre Pronchery                 BIO_printf(bio_err,
867*b077aed3SPierre Pronchery                            "Error adding request extensions from section %s\n", extsect);
868*b077aed3SPierre Pronchery                 goto end;
869*b077aed3SPierre Pronchery             }
870*b077aed3SPierre Pronchery         }
871*b077aed3SPierre Pronchery         if (!do_X509_REQ_sign(rq, privkey, digest, sigopts))
872*b077aed3SPierre Pronchery             goto end;
873*b077aed3SPierre Pronchery         if (!noout) {
874*b077aed3SPierre Pronchery             if (outformat == FORMAT_ASN1) {
875*b077aed3SPierre Pronchery                 X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT);
876*b077aed3SPierre Pronchery                 i = i2d_X509_bio(out, x);
877*b077aed3SPierre Pronchery             } else {
878*b077aed3SPierre Pronchery                 i = PEM_write_bio_X509_REQ(out, rq);
879*b077aed3SPierre Pronchery             }
880*b077aed3SPierre Pronchery             if (!i) {
881*b077aed3SPierre Pronchery                 BIO_printf(bio_err,
882*b077aed3SPierre Pronchery                            "Unable to write certificate request\n");
883*b077aed3SPierre Pronchery                 goto end;
884*b077aed3SPierre Pronchery             }
885*b077aed3SPierre Pronchery         }
886*b077aed3SPierre Pronchery         noout = 1;
887*b077aed3SPierre Pronchery     } else if (privkey != NULL) {
888*b077aed3SPierre Pronchery         if (!do_X509_sign(x, privkey, digest, sigopts, &ext_ctx))
889*b077aed3SPierre Pronchery             goto end;
890*b077aed3SPierre Pronchery     } else if (CAfile != NULL) {
891*b077aed3SPierre Pronchery         if ((CAkey = load_key(CAkeyfile, CAkeyformat,
892*b077aed3SPierre Pronchery                               0, passin, e, "CA private key")) == NULL)
893*b077aed3SPierre Pronchery             goto end;
894*b077aed3SPierre Pronchery         if (!X509_check_private_key(xca, CAkey)) {
895*b077aed3SPierre Pronchery             BIO_printf(bio_err,
896*b077aed3SPierre Pronchery                        "CA certificate and CA private key do not match\n");
897*b077aed3SPierre Pronchery             goto end;
898*b077aed3SPierre Pronchery         }
899*b077aed3SPierre Pronchery 
900*b077aed3SPierre Pronchery         if (!do_X509_sign(x, CAkey, digest, sigopts, &ext_ctx))
901*b077aed3SPierre Pronchery             goto end;
902*b077aed3SPierre Pronchery     }
903e71b7053SJung-uk Kim     if (badsig) {
904e71b7053SJung-uk Kim         const ASN1_BIT_STRING *signature;
905e71b7053SJung-uk Kim 
906e71b7053SJung-uk Kim         X509_get0_signature(&signature, NULL, x);
907e71b7053SJung-uk Kim         corrupt_signature(signature);
908f579bf8eSKris Kennaway     }
909f579bf8eSKris Kennaway 
910*b077aed3SPierre Pronchery     /* Process print options in the given order, as indicated by index i */
9116f9291ceSJung-uk Kim     for (i = 1; i <= num; i++) {
912*b077aed3SPierre Pronchery         if (i == issuer) {
913*b077aed3SPierre Pronchery             print_name(out, "issuer=", X509_get_issuer_name(x));
914*b077aed3SPierre Pronchery         } else if (i == subject) {
915*b077aed3SPierre Pronchery             print_name(out, "subject=", X509_get_subject_name(x));
916*b077aed3SPierre Pronchery         } else if (i == serial) {
917e71b7053SJung-uk Kim             BIO_printf(out, "serial=");
918*b077aed3SPierre Pronchery             i2a_ASN1_INTEGER(out, X509_get0_serialNumber(x));
919e71b7053SJung-uk Kim             BIO_printf(out, "\n");
920*b077aed3SPierre Pronchery         } else if (i == next_serial) {
921*b077aed3SPierre Pronchery             ASN1_INTEGER *ser;
922*b077aed3SPierre Pronchery             BIGNUM *bnser = ASN1_INTEGER_to_BN(X509_get0_serialNumber(x), NULL);
923e71b7053SJung-uk Kim 
924*b077aed3SPierre Pronchery             if (bnser == NULL)
9256be8ae07SJacques Vidrine                 goto end;
926*b077aed3SPierre Pronchery             if (!BN_add_word(bnser, 1)
927*b077aed3SPierre Pronchery                     || (ser = BN_to_ASN1_INTEGER(bnser, NULL)) == NULL) {
928*b077aed3SPierre Pronchery                 BN_free(bnser);
9296be8ae07SJacques Vidrine                 goto end;
930*b077aed3SPierre Pronchery             }
9316be8ae07SJacques Vidrine             BN_free(bnser);
9326be8ae07SJacques Vidrine             i2a_ASN1_INTEGER(out, ser);
9336be8ae07SJacques Vidrine             ASN1_INTEGER_free(ser);
9346be8ae07SJacques Vidrine             BIO_puts(out, "\n");
935*b077aed3SPierre Pronchery         } else if (i == email || i == ocsp_uri) {
936*b077aed3SPierre Pronchery             STACK_OF(OPENSSL_STRING) *emlst =
937*b077aed3SPierre Pronchery                 i == email ? X509_get1_email(x) : X509_get1_ocsp(x);
938*b077aed3SPierre Pronchery 
9391f13597dSJung-uk Kim             for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
940*b077aed3SPierre Pronchery                 BIO_printf(out, "%s\n", sk_OPENSSL_STRING_value(emlst, j));
941ddd58736SKris Kennaway             X509_email_free(emlst);
942*b077aed3SPierre Pronchery         } else if (i == aliasout) {
943*b077aed3SPierre Pronchery             unsigned char *alstr = X509_alias_get0(x, NULL);
944*b077aed3SPierre Pronchery 
9456f9291ceSJung-uk Kim             if (alstr)
946e71b7053SJung-uk Kim                 BIO_printf(out, "%s\n", alstr);
9476f9291ceSJung-uk Kim             else
948e71b7053SJung-uk Kim                 BIO_puts(out, "<No Alias>\n");
949*b077aed3SPierre Pronchery         } else if (i == subject_hash) {
950e71b7053SJung-uk Kim             BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
9511f13597dSJung-uk Kim #ifndef OPENSSL_NO_MD5
952*b077aed3SPierre Pronchery         } else if (i == subject_hash_old) {
953e71b7053SJung-uk Kim             BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
9541f13597dSJung-uk Kim #endif
955*b077aed3SPierre Pronchery         } else if (i == issuer_hash) {
956e71b7053SJung-uk Kim             BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
9571f13597dSJung-uk Kim #ifndef OPENSSL_NO_MD5
958*b077aed3SPierre Pronchery         } else if (i == issuer_hash_old) {
959e71b7053SJung-uk Kim             BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
9601f13597dSJung-uk Kim #endif
961*b077aed3SPierre Pronchery         } else if (i == pprint) {
962e71b7053SJung-uk Kim             BIO_printf(out, "Certificate purposes:\n");
963*b077aed3SPierre Pronchery             for (j = 0; j < X509_PURPOSE_get_count(); j++)
964*b077aed3SPierre Pronchery                 purpose_print(out, x, X509_PURPOSE_get0(j));
965*b077aed3SPierre Pronchery         } else if (i == modulus) {
966e71b7053SJung-uk Kim             BIO_printf(out, "Modulus=");
967*b077aed3SPierre Pronchery             if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS")) {
968*b077aed3SPierre Pronchery                 BIGNUM *n = NULL;
969*b077aed3SPierre Pronchery 
970*b077aed3SPierre Pronchery                 /* Every RSA key has an 'n' */
971*b077aed3SPierre Pronchery                 EVP_PKEY_get_bn_param(pkey, "n", &n);
972e71b7053SJung-uk Kim                 BN_print(out, n);
973*b077aed3SPierre Pronchery                 BN_free(n);
974*b077aed3SPierre Pronchery             } else if (EVP_PKEY_is_a(pkey, "DSA")) {
975*b077aed3SPierre Pronchery                 BIGNUM *dsapub = NULL;
976*b077aed3SPierre Pronchery 
977*b077aed3SPierre Pronchery                 /* Every DSA key has a 'pub' */
978*b077aed3SPierre Pronchery                 EVP_PKEY_get_bn_param(pkey, "pub", &dsapub);
979e71b7053SJung-uk Kim                 BN_print(out, dsapub);
980*b077aed3SPierre Pronchery                 BN_free(dsapub);
981*b077aed3SPierre Pronchery             } else {
982*b077aed3SPierre Pronchery                 BIO_printf(out, "No modulus for this public key type");
983e71b7053SJung-uk Kim             }
984e71b7053SJung-uk Kim             BIO_printf(out, "\n");
985*b077aed3SPierre Pronchery         } else if (i == print_pubkey) {
986e71b7053SJung-uk Kim             PEM_write_bio_PUBKEY(out, pkey);
987*b077aed3SPierre Pronchery         } else if (i == text) {
988e71b7053SJung-uk Kim             X509_print_ex(out, x, get_nameopt(), certflag);
989*b077aed3SPierre Pronchery         } else if (i == startdate) {
990e71b7053SJung-uk Kim             BIO_puts(out, "notBefore=");
991*b077aed3SPierre Pronchery             ASN1_TIME_print_ex(out, X509_get0_notBefore(x), dateopt);
992e71b7053SJung-uk Kim             BIO_puts(out, "\n");
993*b077aed3SPierre Pronchery         } else if (i == enddate) {
994e71b7053SJung-uk Kim             BIO_puts(out, "notAfter=");
995*b077aed3SPierre Pronchery             ASN1_TIME_print_ex(out, X509_get0_notAfter(x), dateopt);
996e71b7053SJung-uk Kim             BIO_puts(out, "\n");
997*b077aed3SPierre Pronchery         } else if (i == fingerprint) {
99874664626SKris Kennaway             unsigned int n;
99974664626SKris Kennaway             unsigned char md[EVP_MAX_MD_SIZE];
1000*b077aed3SPierre Pronchery             const char *fdigname = digest;
1001*b077aed3SPierre Pronchery             EVP_MD *fdig;
1002*b077aed3SPierre Pronchery             int digres;
100374664626SKris Kennaway 
1004*b077aed3SPierre Pronchery             if (fdigname == NULL)
1005*b077aed3SPierre Pronchery                 fdigname = "SHA1";
10061f13597dSJung-uk Kim 
1007*b077aed3SPierre Pronchery             if ((fdig = EVP_MD_fetch(app_get0_libctx(), fdigname,
1008*b077aed3SPierre Pronchery                                      app_get0_propq())) == NULL) {
1009*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Unknown digest\n");
101074664626SKris Kennaway                 goto end;
101174664626SKris Kennaway             }
1012*b077aed3SPierre Pronchery             digres = X509_digest(x, fdig, md, &n);
1013*b077aed3SPierre Pronchery             EVP_MD_free(fdig);
1014*b077aed3SPierre Pronchery             if (!digres) {
1015*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "Out of memory\n");
10166f9291ceSJung-uk Kim                 goto end;
101774664626SKris Kennaway             }
101874664626SKris Kennaway 
1019*b077aed3SPierre Pronchery             BIO_printf(out, "%s Fingerprint=", fdigname);
1020*b077aed3SPierre Pronchery             for (j = 0; j < (int)n; j++)
1021*b077aed3SPierre Pronchery                 BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n) ? '\n' : ':');
1022*b077aed3SPierre Pronchery         } else if (i == ocspid) {
10235c87c606SMark Murray             X509_ocspid_print(out, x);
1024*b077aed3SPierre Pronchery         } else if (i == ext) {
1025*b077aed3SPierre Pronchery             print_x509v3_exts(out, x, ext_names);
102674664626SKris Kennaway         }
102774664626SKris Kennaway     }
102874664626SKris Kennaway 
10296f9291ceSJung-uk Kim     if (checkend) {
10303b4e3dcbSSimon L. B. Nielsen         time_t tcheck = time(NULL) + checkoffset;
1031ddd58736SKris Kennaway 
1032*b077aed3SPierre Pronchery         ret = X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0;
1033*b077aed3SPierre Pronchery         if (ret)
1034ddd58736SKris Kennaway             BIO_printf(out, "Certificate will expire\n");
1035*b077aed3SPierre Pronchery         else
1036ddd58736SKris Kennaway             BIO_printf(out, "Certificate will not expire\n");
1037ddd58736SKris Kennaway         goto end;
1038ddd58736SKris Kennaway     }
1039ddd58736SKris Kennaway 
1040e71b7053SJung-uk Kim     print_cert_checks(out, x, checkhost, checkemail, checkip);
10417bded2dbSJung-uk Kim 
1042e71b7053SJung-uk Kim     if (noout || nocert) {
104374664626SKris Kennaway         ret = 0;
104474664626SKris Kennaway         goto end;
104574664626SKris Kennaway     }
104674664626SKris Kennaway 
1047e71b7053SJung-uk Kim     if (outformat == FORMAT_ASN1) {
104874664626SKris Kennaway         i = i2d_X509_bio(out, x);
1049e71b7053SJung-uk Kim     } else if (outformat == FORMAT_PEM) {
10506f9291ceSJung-uk Kim         if (trustout)
10516f9291ceSJung-uk Kim             i = PEM_write_bio_X509_AUX(out, x);
10526f9291ceSJung-uk Kim         else
10536f9291ceSJung-uk Kim             i = PEM_write_bio_X509(out, x);
10546f9291ceSJung-uk Kim     } else {
1055*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Bad output format specified for outfile\n");
105674664626SKris Kennaway         goto end;
105774664626SKris Kennaway     }
10586f9291ceSJung-uk Kim     if (!i) {
1059*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Unable to write certificate\n");
106074664626SKris Kennaway         goto end;
106174664626SKris Kennaway     }
106274664626SKris Kennaway     ret = 0;
1063*b077aed3SPierre Pronchery 
106474664626SKris Kennaway  end:
1065*b077aed3SPierre Pronchery     if (ret != 0)
1066*b077aed3SPierre Pronchery         ERR_print_errors(bio_err);
10675c87c606SMark Murray     NCONF_free(extconf);
1068ddd58736SKris Kennaway     BIO_free_all(out);
106974664626SKris Kennaway     X509_STORE_free(ctx);
1070*b077aed3SPierre Pronchery     X509_NAME_free(fsubj);
107174664626SKris Kennaway     X509_REQ_free(req);
107274664626SKris Kennaway     X509_free(x);
107374664626SKris Kennaway     X509_free(xca);
1074*b077aed3SPierre Pronchery     EVP_PKEY_free(privkey);
1075*b077aed3SPierre Pronchery     EVP_PKEY_free(CAkey);
1076*b077aed3SPierre Pronchery     EVP_PKEY_free(pubkey);
10771f13597dSJung-uk Kim     sk_OPENSSL_STRING_free(sigopts);
1078*b077aed3SPierre Pronchery     sk_OPENSSL_STRING_free(vfyopts);
107974664626SKris Kennaway     X509_REQ_free(rq);
10805c87c606SMark Murray     ASN1_INTEGER_free(sno);
1081f579bf8eSKris Kennaway     sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
1082f579bf8eSKris Kennaway     sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
10836cf8931aSJung-uk Kim     release_engine(e);
1084*b077aed3SPierre Pronchery     clear_free(passin);
1085e71b7053SJung-uk Kim     return ret;
108674664626SKris Kennaway }
108774664626SKris Kennaway 
x509_load_serial(const char * CAfile,const char * serialfile,int create)1088e71b7053SJung-uk Kim static ASN1_INTEGER *x509_load_serial(const char *CAfile,
1089e71b7053SJung-uk Kim                                       const char *serialfile, int create)
109074664626SKris Kennaway {
1091e71b7053SJung-uk Kim     char *buf = NULL;
1092ced566fdSJacques Vidrine     ASN1_INTEGER *bs = NULL;
109374664626SKris Kennaway     BIGNUM *serial = NULL;
1094cfc39718SJung-uk Kim     int defaultfile = 0, file_exists;
109574664626SKris Kennaway 
10966f9291ceSJung-uk Kim     if (serialfile == NULL) {
1097e71b7053SJung-uk Kim         const char *p = strrchr(CAfile, '.');
1098e71b7053SJung-uk Kim         size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
109974664626SKris Kennaway 
1100e71b7053SJung-uk Kim         buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
1101e71b7053SJung-uk Kim         memcpy(buf, CAfile, len);
1102e71b7053SJung-uk Kim         memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
1103e71b7053SJung-uk Kim         serialfile = buf;
1104cfc39718SJung-uk Kim         defaultfile = 1;
1105e71b7053SJung-uk Kim     }
1106e71b7053SJung-uk Kim 
1107cfc39718SJung-uk Kim     serial = load_serial(serialfile, &file_exists, create || defaultfile, NULL);
11086f9291ceSJung-uk Kim     if (serial == NULL)
11096f9291ceSJung-uk Kim         goto end;
111074664626SKris Kennaway 
11116f9291ceSJung-uk Kim     if (!BN_add_word(serial, 1)) {
1112*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Serial number increment failure\n");
11136f9291ceSJung-uk Kim         goto end;
11146f9291ceSJung-uk Kim     }
11155c87c606SMark Murray 
1116cfc39718SJung-uk Kim     if (file_exists || create)
1117cfc39718SJung-uk Kim         save_serial(serialfile, NULL, serial, &bs);
1118cfc39718SJung-uk Kim     else
1119cfc39718SJung-uk Kim         bs = BN_to_ASN1_INTEGER(serial, NULL);
11205c87c606SMark Murray 
11215c87c606SMark Murray  end:
11226f9291ceSJung-uk Kim     OPENSSL_free(buf);
11235c87c606SMark Murray     BN_free(serial);
1124ced566fdSJacques Vidrine     return bs;
11255c87c606SMark Murray }
11265c87c606SMark Murray 
callb(int ok,X509_STORE_CTX * ctx)1127e71b7053SJung-uk Kim static int callb(int ok, X509_STORE_CTX *ctx)
112874664626SKris Kennaway {
112974664626SKris Kennaway     int err;
113074664626SKris Kennaway     X509 *err_cert;
113174664626SKris Kennaway 
11326f9291ceSJung-uk Kim     /*
1133*b077aed3SPierre Pronchery      * It is ok to use a self-signed certificate. This case will catch both
1134*b077aed3SPierre Pronchery      * the initial ok == 0 and the final ok == 1 calls to this function.
11356f9291ceSJung-uk Kim      */
113674664626SKris Kennaway     err = X509_STORE_CTX_get_error(ctx);
113774664626SKris Kennaway     if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1138ddd58736SKris Kennaway         return 1;
113974664626SKris Kennaway 
11406f9291ceSJung-uk Kim     /*
11416f9291ceSJung-uk Kim      * BAD we should have gotten an error.  Normally if everything worked
11426f9291ceSJung-uk Kim      * X509_STORE_CTX_get_error(ctx) will still be set to
11436f9291ceSJung-uk Kim      * DEPTH_ZERO_SELF_....
11446f9291ceSJung-uk Kim      */
11456f9291ceSJung-uk Kim     if (ok) {
11466f9291ceSJung-uk Kim         BIO_printf(bio_err,
1147*b077aed3SPierre Pronchery                    "Error with certificate to be certified - should be self-signed\n");
1148ddd58736SKris Kennaway         return 0;
11496f9291ceSJung-uk Kim     } else {
115074664626SKris Kennaway         err_cert = X509_STORE_CTX_get_current_cert(ctx);
1151*b077aed3SPierre Pronchery         print_name(bio_err, "subject=", X509_get_subject_name(err_cert));
11526f9291ceSJung-uk Kim         BIO_printf(bio_err,
1153*b077aed3SPierre Pronchery                    "Error with certificate - error %d at depth %d\n%s\n", err,
11546f9291ceSJung-uk Kim                    X509_STORE_CTX_get_error_depth(ctx),
115574664626SKris Kennaway                    X509_verify_cert_error_string(err));
1156ddd58736SKris Kennaway         return 1;
115774664626SKris Kennaway     }
115874664626SKris Kennaway }
115974664626SKris Kennaway 
purpose_print(BIO * bio,X509 * cert,X509_PURPOSE * pt)1160f579bf8eSKris Kennaway static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
1161f579bf8eSKris Kennaway {
1162f579bf8eSKris Kennaway     int id, i, idret;
1163e71b7053SJung-uk Kim     const char *pname;
1164f579bf8eSKris Kennaway     id = X509_PURPOSE_get_id(pt);
1165f579bf8eSKris Kennaway     pname = X509_PURPOSE_get0_name(pt);
11666f9291ceSJung-uk Kim     for (i = 0; i < 2; i++) {
1167f579bf8eSKris Kennaway         idret = X509_check_purpose(cert, id, i);
1168f579bf8eSKris Kennaway         BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
11696f9291ceSJung-uk Kim         if (idret == 1)
11706f9291ceSJung-uk Kim             BIO_printf(bio, "Yes\n");
11716f9291ceSJung-uk Kim         else if (idret == 0)
11726f9291ceSJung-uk Kim             BIO_printf(bio, "No\n");
11736f9291ceSJung-uk Kim         else
11746f9291ceSJung-uk Kim             BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
1175f579bf8eSKris Kennaway     }
1176f579bf8eSKris Kennaway     return 1;
1177f579bf8eSKris Kennaway }
1178e71b7053SJung-uk Kim 
parse_ext_names(char * names,const char ** result)1179e71b7053SJung-uk Kim static int parse_ext_names(char *names, const char **result)
1180e71b7053SJung-uk Kim {
1181e71b7053SJung-uk Kim     char *p, *q;
1182e71b7053SJung-uk Kim     int cnt = 0, len = 0;
1183e71b7053SJung-uk Kim 
1184e71b7053SJung-uk Kim     p = q = names;
1185e71b7053SJung-uk Kim     len = strlen(names);
1186e71b7053SJung-uk Kim 
1187e71b7053SJung-uk Kim     while (q - names <= len) {
1188e71b7053SJung-uk Kim         if (*q != ',' && *q != '\0') {
1189e71b7053SJung-uk Kim             q++;
1190e71b7053SJung-uk Kim             continue;
1191e71b7053SJung-uk Kim         }
1192e71b7053SJung-uk Kim         if (p != q) {
1193e71b7053SJung-uk Kim             /* found */
1194e71b7053SJung-uk Kim             if (result != NULL) {
1195e71b7053SJung-uk Kim                 result[cnt] = p;
1196e71b7053SJung-uk Kim                 *q = '\0';
1197e71b7053SJung-uk Kim             }
1198e71b7053SJung-uk Kim             cnt++;
1199e71b7053SJung-uk Kim         }
1200e71b7053SJung-uk Kim         p = ++q;
1201e71b7053SJung-uk Kim     }
1202e71b7053SJung-uk Kim 
1203e71b7053SJung-uk Kim     return cnt;
1204e71b7053SJung-uk Kim }
1205e71b7053SJung-uk Kim 
print_x509v3_exts(BIO * bio,X509 * x,const char * ext_names)1206e71b7053SJung-uk Kim static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names)
1207e71b7053SJung-uk Kim {
1208e71b7053SJung-uk Kim     const STACK_OF(X509_EXTENSION) *exts = NULL;
1209e71b7053SJung-uk Kim     STACK_OF(X509_EXTENSION) *exts2 = NULL;
1210e71b7053SJung-uk Kim     X509_EXTENSION *ext = NULL;
1211e71b7053SJung-uk Kim     ASN1_OBJECT *obj;
1212e71b7053SJung-uk Kim     int i, j, ret = 0, num, nn = 0;
1213e71b7053SJung-uk Kim     const char *sn, **names = NULL;
1214e71b7053SJung-uk Kim     char *tmp_ext_names = NULL;
1215e71b7053SJung-uk Kim 
1216e71b7053SJung-uk Kim     exts = X509_get0_extensions(x);
1217e71b7053SJung-uk Kim     if ((num = sk_X509_EXTENSION_num(exts)) <= 0) {
1218*b077aed3SPierre Pronchery         BIO_printf(bio_err, "No extensions in certificate\n");
1219e71b7053SJung-uk Kim         ret = 1;
1220e71b7053SJung-uk Kim         goto end;
1221e71b7053SJung-uk Kim     }
1222e71b7053SJung-uk Kim 
1223e71b7053SJung-uk Kim     /* parse comma separated ext name string */
1224e71b7053SJung-uk Kim     if ((tmp_ext_names = OPENSSL_strdup(ext_names)) == NULL)
1225e71b7053SJung-uk Kim         goto end;
1226e71b7053SJung-uk Kim     if ((nn = parse_ext_names(tmp_ext_names, NULL)) == 0) {
1227e71b7053SJung-uk Kim         BIO_printf(bio, "Invalid extension names: %s\n", ext_names);
1228e71b7053SJung-uk Kim         goto end;
1229e71b7053SJung-uk Kim     }
1230e71b7053SJung-uk Kim     if ((names = OPENSSL_malloc(sizeof(char *) * nn)) == NULL)
1231e71b7053SJung-uk Kim         goto end;
1232e71b7053SJung-uk Kim     parse_ext_names(tmp_ext_names, names);
1233e71b7053SJung-uk Kim 
1234e71b7053SJung-uk Kim     for (i = 0; i < num; i++) {
1235e71b7053SJung-uk Kim         ext = sk_X509_EXTENSION_value(exts, i);
1236e71b7053SJung-uk Kim 
1237e71b7053SJung-uk Kim         /* check if this ext is what we want */
1238e71b7053SJung-uk Kim         obj = X509_EXTENSION_get_object(ext);
1239e71b7053SJung-uk Kim         sn = OBJ_nid2sn(OBJ_obj2nid(obj));
1240e71b7053SJung-uk Kim         if (sn == NULL || strcmp(sn, "UNDEF") == 0)
1241e71b7053SJung-uk Kim             continue;
1242e71b7053SJung-uk Kim 
1243e71b7053SJung-uk Kim         for (j = 0; j < nn; j++) {
1244e71b7053SJung-uk Kim             if (strcmp(sn, names[j]) == 0) {
1245e71b7053SJung-uk Kim                 /* push the extension into a new stack */
1246e71b7053SJung-uk Kim                 if (exts2 == NULL
1247e71b7053SJung-uk Kim                     && (exts2 = sk_X509_EXTENSION_new_null()) == NULL)
1248e71b7053SJung-uk Kim                     goto end;
1249e71b7053SJung-uk Kim                 if (!sk_X509_EXTENSION_push(exts2, ext))
1250e71b7053SJung-uk Kim                     goto end;
1251e71b7053SJung-uk Kim             }
1252e71b7053SJung-uk Kim         }
1253e71b7053SJung-uk Kim     }
1254e71b7053SJung-uk Kim 
1255e71b7053SJung-uk Kim     if (!sk_X509_EXTENSION_num(exts2)) {
1256e71b7053SJung-uk Kim         BIO_printf(bio, "No extensions matched with %s\n", ext_names);
1257e71b7053SJung-uk Kim         ret = 1;
1258e71b7053SJung-uk Kim         goto end;
1259e71b7053SJung-uk Kim     }
1260e71b7053SJung-uk Kim 
1261e71b7053SJung-uk Kim     ret = X509V3_extensions_print(bio, NULL, exts2, 0, 0);
1262e71b7053SJung-uk Kim  end:
1263e71b7053SJung-uk Kim     sk_X509_EXTENSION_free(exts2);
1264e71b7053SJung-uk Kim     OPENSSL_free(names);
1265e71b7053SJung-uk Kim     OPENSSL_free(tmp_ext_names);
1266e71b7053SJung-uk Kim     return ret;
1267e71b7053SJung-uk Kim }
1268