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