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