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