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