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