2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
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>
26 #ifndef OPENSSL_NO_DSA
27 # include <openssl/dsa.h>
31 #define POSTFIX ".srl"
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,
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,
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);
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,
62 OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT, OPT_PRESERVE_DATES,
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"},
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"},
146 #ifndef OPENSSL_NO_ENGINE
147 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
149 {"preserve_dates", OPT_PRESERVE_DATES, '-', "preserve existing dates when signing"},
153 int x509_main(int argc, char **argv)
155 ASN1_INTEGER *sno = NULL;
156 ASN1_OBJECT *objtmp = 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;
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;
181 time_t checkoffset = 0;
182 unsigned long certflag = 0;
183 int preserve_dates = 0;
186 #ifndef OPENSSL_NO_MD5
187 int subject_hash_old = 0, issuer_hash_old = 0;
190 ctx = X509_STORE_new();
193 X509_STORE_set_verify_cb(ctx, callb);
195 prog = opt_init(argc, argv, x509_options);
196 while ((o = opt_next()) != OPT_EOF) {
201 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
204 opt_help(x509_options);
208 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
215 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
219 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
223 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat))
227 if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat))
239 sigopts = sk_OPENSSL_STRING_new_null();
240 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
246 days = atoi(opt_arg());
249 passinarg = opt_arg();
270 CAkeyfile = opt_arg();
273 CAserial = opt_arg();
277 BIO_printf(bio_err, "Serial number supplied twice\n");
280 if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
283 case OPT_FORCE_PUBKEY:
284 fkeyfile = opt_arg();
287 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
289 "%s: Invalid trust object value %s\n",
293 if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
295 sk_ASN1_OBJECT_push(trust, objtmp);
300 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
302 "%s: Invalid reject object value %s\n",
307 && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
309 sk_ASN1_OBJECT_push(reject, objtmp);
318 if (!set_cert_ex(&certflag, opt_arg()))
322 if (!set_nameopt(opt_arg()))
326 e = setup_engine(opt_arg(), 0);
340 case OPT_NEXT_SERIAL:
361 case OPT_FINGERPRINT:
365 subject_hash = ++num;
367 case OPT_ISSUER_HASH:
401 case OPT_CACREATESERIAL:
402 CA_createserial = ++num;
413 #ifndef OPENSSL_NO_MD5
414 case OPT_SUBJECT_HASH_OLD:
415 subject_hash_old = ++num;
417 case OPT_ISSUER_HASH_OLD:
418 issuer_hash_old = ++num;
421 case OPT_SUBJECT_HASH_OLD:
422 case OPT_ISSUER_HASH_OLD:
433 if (!opt_imax(opt_arg(), &temp))
435 checkoffset = (time_t)temp;
436 if ((intmax_t)checkoffset != temp) {
437 BIO_printf(bio_err, "%s: checkend time out of range %s\n",
444 checkhost = opt_arg();
447 checkemail = opt_arg();
452 case OPT_PRESERVE_DATES:
453 if (days != DEF_DAYS)
458 if (!opt_md(opt_unknown(), &digest))
462 argc = opt_num_rest();
465 BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]);
469 out = bio_open_default(outfile, 'w', outformat);
473 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
474 BIO_printf(bio_err, "Error getting password\n");
478 if (!X509_STORE_set_default_paths(ctx)) {
479 ERR_print_errors(bio_err);
483 if (fkeyfile != NULL) {
484 fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key");
489 if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
491 } else if ((CA_flag) && (CAkeyfile == NULL)) {
493 "need to specify a CAkey if using the CA command\n");
497 if (extfile != NULL) {
499 if ((extconf = app_load_config(extfile)) == NULL)
501 if (extsect == NULL) {
502 extsect = NCONF_get_string(extconf, "default", "extensions");
503 if (extsect == NULL) {
508 X509V3_set_ctx_test(&ctx2);
509 X509V3_set_nconf(&ctx2, extconf);
510 if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
512 "Error Loading extension section %s\n", extsect);
513 ERR_print_errors(bio_err);
522 if (!sign_flag && !CA_flag) {
523 BIO_printf(bio_err, "We need a private key to sign with\n");
526 in = bio_open_default(infile, 'r', informat);
529 req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
533 ERR_print_errors(bio_err);
537 if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) {
538 BIO_printf(bio_err, "error unpacking public key\n");
541 i = X509_REQ_verify(req, pkey);
543 BIO_printf(bio_err, "Signature verification error\n");
544 ERR_print_errors(bio_err);
549 "Signature did not match the certificate request\n");
552 BIO_printf(bio_err, "Signature ok\n");
555 print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),
558 if ((x = X509_new()) == NULL)
562 sno = ASN1_INTEGER_new();
563 if (sno == NULL || !rand_serial(NULL, sno))
565 if (!X509_set_serialNumber(x, sno))
567 ASN1_INTEGER_free(sno);
569 } else if (!X509_set_serialNumber(x, sno)) {
573 if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req)))
575 if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req)))
577 if (!set_cert_times(x, NULL, NULL, days))
581 X509_set_pubkey(x, fkey);
583 pkey = X509_REQ_get0_pubkey(req);
584 X509_set_pubkey(x, pkey);
587 x = load_cert(infile, informat, "Certificate");
593 xca = load_cert(CAfile, CAformat, "CA Certificate");
598 if (!noout || text || next_serial) {
599 OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
604 X509_alias_set1(x, (unsigned char *)alias, -1);
609 X509_reject_clear(x);
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);
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);
628 const ASN1_BIT_STRING *signature;
630 X509_get0_signature(&signature, NULL, x);
631 corrupt_signature(signature);
635 for (i = 1; i <= num; 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);
651 if (!BN_add_word(bnser, 1))
653 ser = BN_to_ASN1_INTEGER(bnser, NULL);
657 i2a_ASN1_INTEGER(out, ser);
658 ASN1_INTEGER_free(ser);
660 } else if ((email == i) || (ocsp_uri == i)) {
662 STACK_OF(OPENSSL_STRING) *emlst;
664 emlst = X509_get1_email(x);
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);
675 BIO_printf(out, "%s\n", alstr);
677 BIO_puts(out, "<No Alias>\n");
678 } else if (subject_hash == i) {
679 BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
681 #ifndef OPENSSL_NO_MD5
682 else if (subject_hash_old == i) {
683 BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
686 else if (issuer_hash == i) {
687 BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
689 #ifndef OPENSSL_NO_MD5
690 else if (issuer_hash_old == i) {
691 BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
694 else if (pprint == i) {
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);
702 } else if (modulus == i) {
705 pkey = X509_get0_pubkey(x);
707 BIO_printf(bio_err, "Modulus=unavailable\n");
708 ERR_print_errors(bio_err);
711 BIO_printf(out, "Modulus=");
712 #ifndef OPENSSL_NO_RSA
713 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
715 RSA_get0_key(EVP_PKEY_get0_RSA(pkey), &n, NULL, NULL);
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);
727 BIO_printf(out, "Wrong Algorithm type");
729 BIO_printf(out, "\n");
730 } else if (pubkey == i) {
733 pkey = X509_get0_pubkey(x);
735 BIO_printf(bio_err, "Error getting public key\n");
736 ERR_print_errors(bio_err);
739 PEM_write_bio_PUBKEY(out, pkey);
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");
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);
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));
768 } else if (enddate == i) {
769 BIO_puts(out, "notAfter=");
770 ASN1_TIME_print(out, X509_get0_notAfter(x));
772 } else if (fingerprint == i) {
775 unsigned char md[EVP_MAX_MD_SIZE];
776 const EVP_MD *fdig = digest;
781 if (!X509_digest(x, fdig, md, &n)) {
782 BIO_printf(bio_err, "out of memory\n");
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)
793 /* should be in the library */
794 else if ((sign_flag == i) && (x509req == 0)) {
795 BIO_printf(bio_err, "Getting Private key\n");
797 Upkey = load_key(keyfile, keyformat, 0,
798 passin, e, "Private key");
803 if (!sign(x, Upkey, days, clrext, digest, extconf, extsect, preserve_dates))
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");
814 if (!x509_certify(ctx, CAfile, digest, x, xca,
816 CAserial, CA_createserial, days, clrext,
817 extconf, extsect, sno, reqfile, preserve_dates))
819 } else if (x509req == i) {
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");
827 pk = load_key(keyfile, keyformat, 0,
828 passin, e, "request key");
833 BIO_printf(bio_err, "Generating certificate request\n");
835 rq = X509_to_X509_REQ(x, pk, digest);
838 ERR_print_errors(bio_err);
842 X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT);
843 PEM_write_bio_X509_REQ(out, rq);
846 } else if (ocspid == i) {
847 X509_ocspid_print(out, x);
848 } else if (ext == i) {
849 print_x509v3_exts(out, x, exts);
855 time_t tcheck = time(NULL) + checkoffset;
857 if (X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0) {
858 BIO_printf(out, "Certificate will expire\n");
861 BIO_printf(out, "Certificate will not expire\n");
867 print_cert_checks(out, x, checkhost, checkemail, checkip);
869 if (noout || nocert) {
874 if (outformat == FORMAT_ASN1) {
875 i = i2d_X509_bio(out, x);
876 } else if (outformat == FORMAT_PEM) {
878 i = PEM_write_bio_X509_AUX(out, x);
880 i = PEM_write_bio_X509(out, x);
882 BIO_printf(bio_err, "bad output format specified for outfile\n");
886 BIO_printf(bio_err, "unable to write certificate\n");
887 ERR_print_errors(bio_err);
894 X509_STORE_free(ctx);
898 EVP_PKEY_free(Upkey);
899 EVP_PKEY_free(CApkey);
901 sk_OPENSSL_STRING_free(sigopts);
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);
908 OPENSSL_free(passin);
912 static ASN1_INTEGER *x509_load_serial(const char *CAfile,
913 const char *serialfile, int create)
916 ASN1_INTEGER *bs = NULL;
917 BIGNUM *serial = NULL;
919 if (serialfile == NULL) {
920 const char *p = strchr(CAfile, '.');
921 size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
923 buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
924 memcpy(buf, CAfile, len);
925 memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
929 serial = load_serial(serialfile, create, NULL);
933 if (!BN_add_word(serial, 1)) {
934 BIO_printf(bio_err, "add_word failure\n");
938 if (!save_serial(serialfile, NULL, serial, &bs))
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)
955 ASN1_INTEGER *bs = NULL;
956 X509_STORE_CTX *xsc = NULL;
959 upkey = X509_get0_pubkey(xca);
961 BIO_printf(bio_err, "Error obtaining CA X509 public key\n");
964 EVP_PKEY_copy_parameters(upkey, pkey);
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");
973 else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)
977 * NOTE: this certificate can/should be self signed, unless it was a
978 * certificate request in which case it is not.
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)
985 if (!X509_check_private_key(xca, pkey)) {
987 "CA certificate and CA private key do not match\n");
991 if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
993 if (!X509_set_serialNumber(x, bs))
996 if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
1000 while (X509_get_ext_count(x) > 0)
1001 X509_delete_ext(x, 0);
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))
1013 if (!do_X509_sign(x, pkey, digest, sigopts))
1017 X509_STORE_CTX_free(xsc);
1019 ERR_print_errors(bio_err);
1021 ASN1_INTEGER_free(bs);
1025 static int callb(int ok, X509_STORE_CTX *ctx)
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
1034 err = X509_STORE_CTX_get_error(ctx);
1035 if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
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_....
1045 "error with certificate to be certified - should be self signed\n");
1048 err_cert = X509_STORE_CTX_get_current_cert(ctx);
1049 print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
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));
1059 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
1060 const EVP_MD *digest, CONF *conf, const char *section,
1064 if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
1066 if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
1068 if (!X509_set_pubkey(x, pkey))
1071 while (X509_get_ext_count(x) > 0)
1072 X509_delete_ext(x, 0);
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))
1082 if (!X509_sign(x, pkey, digest))
1086 ERR_print_errors(bio_err);
1090 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
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" : "");
1100 BIO_printf(bio, "Yes\n");
1101 else if (idret == 0)
1102 BIO_printf(bio, "No\n");
1104 BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
1109 static int parse_ext_names(char *names, const char **result)
1112 int cnt = 0, len = 0;
1115 len = strlen(names);
1117 while (q - names <= len) {
1118 if (*q != ',' && *q != '\0') {
1124 if (result != NULL) {
1136 static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names)
1138 const STACK_OF(X509_EXTENSION) *exts = NULL;
1139 STACK_OF(X509_EXTENSION) *exts2 = NULL;
1140 X509_EXTENSION *ext = NULL;
1142 int i, j, ret = 0, num, nn = 0;
1143 const char *sn, **names = NULL;
1144 char *tmp_ext_names = NULL;
1146 exts = X509_get0_extensions(x);
1147 if ((num = sk_X509_EXTENSION_num(exts)) <= 0) {
1148 BIO_printf(bio, "No extensions in certificate\n");
1153 /* parse comma separated ext name string */
1154 if ((tmp_ext_names = OPENSSL_strdup(ext_names)) == NULL)
1156 if ((nn = parse_ext_names(tmp_ext_names, NULL)) == 0) {
1157 BIO_printf(bio, "Invalid extension names: %s\n", ext_names);
1160 if ((names = OPENSSL_malloc(sizeof(char *) * nn)) == NULL)
1162 parse_ext_names(tmp_ext_names, names);
1164 for (i = 0; i < num; i++) {
1165 ext = sk_X509_EXTENSION_value(exts, i);
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)
1173 for (j = 0; j < nn; j++) {
1174 if (strcmp(sn, names[j]) == 0) {
1175 /* push the extension into a new stack */
1177 && (exts2 = sk_X509_EXTENSION_new_null()) == NULL)
1179 if (!sk_X509_EXTENSION_push(exts2, ext))
1185 if (!sk_X509_EXTENSION_num(exts2)) {
1186 BIO_printf(bio, "No extensions matched with %s\n", ext_names);
1191 ret = X509V3_extensions_print(bio, NULL, exts2, 0, 0);
1193 sk_X509_EXTENSION_free(exts2);
1194 OPENSSL_free(names);
1195 OPENSSL_free(tmp_ext_names);