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