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