2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
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>
24 #ifndef OPENSSL_NO_RSA
25 # include <openssl/rsa.h>
27 #ifndef OPENSSL_NO_DSA
28 # include <openssl/dsa.h>
32 #define POSTFIX ".srl"
35 static int callb(int ok, X509_STORE_CTX *ctx);
36 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
37 const EVP_MD *digest, CONF *conf, const char *section,
39 static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest,
40 X509 *x, X509 *xca, EVP_PKEY *pkey,
41 STACK_OF(OPENSSL_STRING) *sigopts, const char *serialfile,
42 int create, int days, int clrext, CONF *conf,
43 const char *section, ASN1_INTEGER *sno, int reqfile,
45 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
46 static int print_x509v3_exts(BIO *bio, X509 *x, const char *exts);
48 typedef enum OPTION_choice {
49 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
50 OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM,
51 OPT_CAKEYFORM, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE,
52 OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_SIGNKEY, OPT_CA,
53 OPT_CAKEY, OPT_CASERIAL, OPT_SET_SERIAL, OPT_FORCE_PUBKEY,
54 OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_NAMEOPT,
55 OPT_C, OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL,
56 OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH,
57 OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES,
58 OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST,
59 OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST,
60 OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID,
63 OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT, OPT_PRESERVE_DATES,
67 const OPTIONS x509_options[] = {
68 {"help", OPT_HELP, '-', "Display this summary"},
69 {"inform", OPT_INFORM, 'f',
70 "Input format - default PEM (one of DER or PEM)"},
71 {"in", OPT_IN, '<', "Input file - default stdin"},
72 {"outform", OPT_OUTFORM, 'f',
73 "Output format - default PEM (one of DER or PEM)"},
74 {"out", OPT_OUT, '>', "Output file - default stdout"},
75 {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"},
76 {"passin", OPT_PASSIN, 's', "Private key password/pass-phrase source"},
77 {"serial", OPT_SERIAL, '-', "Print serial number value"},
78 {"subject_hash", OPT_HASH, '-', "Print subject hash value"},
79 {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"},
80 {"hash", OPT_HASH, '-', "Synonym for -subject_hash"},
81 {"subject", OPT_SUBJECT, '-', "Print subject DN"},
82 {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
83 {"email", OPT_EMAIL, '-', "Print email address(es)"},
84 {"startdate", OPT_STARTDATE, '-', "Set notBefore field"},
85 {"enddate", OPT_ENDDATE, '-', "Set notAfter field"},
86 {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"},
87 {"dates", OPT_DATES, '-', "Both Before and After dates"},
88 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
89 {"pubkey", OPT_PUBKEY, '-', "Output the public key"},
90 {"fingerprint", OPT_FINGERPRINT, '-',
91 "Print the certificate fingerprint"},
92 {"alias", OPT_ALIAS, '-', "Output certificate alias"},
93 {"noout", OPT_NOOUT, '-', "No output, just status"},
94 {"nocert", OPT_NOCERT, '-', "No certificate output"},
95 {"ocspid", OPT_OCSPID, '-',
96 "Print OCSP hash values for the subject name and public key"},
97 {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"},
98 {"trustout", OPT_TRUSTOUT, '-', "Output a trusted certificate"},
99 {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"},
100 {"clrext", OPT_CLREXT, '-', "Clear all certificate extensions"},
101 {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"},
102 {"addreject", OPT_ADDREJECT, 's',
103 "Reject certificate for a given purpose"},
104 {"setalias", OPT_SETALIAS, 's', "Set certificate alias"},
105 {"days", OPT_DAYS, 'n',
106 "How long till expiry of a signed certificate - def 30 days"},
107 {"checkend", OPT_CHECKEND, 'M',
108 "Check whether the cert expires in the next arg seconds"},
109 {OPT_MORE_STR, 1, 1, "Exit 1 if so, 0 if not"},
110 {"signkey", OPT_SIGNKEY, '<', "Self sign cert with arg"},
111 {"x509toreq", OPT_X509TOREQ, '-',
112 "Output a certification request object"},
113 {"req", OPT_REQ, '-', "Input is a certificate request, sign and output"},
114 {"CA", OPT_CA, '<', "Set the CA certificate, must be PEM format"},
115 {"CAkey", OPT_CAKEY, 's',
116 "The CA key, must be PEM format; if not in CAfile"},
117 {"CAcreateserial", OPT_CACREATESERIAL, '-',
118 "Create serial number file if it does not exist"},
119 {"CAserial", OPT_CASERIAL, 's', "Serial file"},
120 {"set_serial", OPT_SET_SERIAL, 's', "Serial number to use"},
121 {"text", OPT_TEXT, '-', "Print the certificate in text form"},
122 {"ext", OPT_EXT, 's', "Print various X509V3 extensions"},
123 {"C", OPT_C, '-', "Print out C code forms"},
124 {"extfile", OPT_EXTFILE, '<', "File with X509V3 extensions to add"},
126 {"extensions", OPT_EXTENSIONS, 's', "Section from config file to use"},
127 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
128 {"certopt", OPT_CERTOPT, 's', "Various certificate text options"},
129 {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"},
130 {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"},
131 {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"},
132 {"CAform", OPT_CAFORM, 'F', "CA format - default PEM"},
133 {"CAkeyform", OPT_CAKEYFORM, 'f', "CA key format - default PEM"},
134 {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"},
135 {"force_pubkey", OPT_FORCE_PUBKEY, '<', "Force the Key to put inside certificate"},
136 {"next_serial", OPT_NEXT_SERIAL, '-', "Increment current certificate serial number"},
137 {"clrreject", OPT_CLRREJECT, '-',
138 "Clears all the prohibited or rejected uses of the certificate"},
139 {"badsig", OPT_BADSIG, '-', "Corrupt last byte of certificate signature (for test)"},
140 {"", OPT_MD, '-', "Any supported digest"},
141 #ifndef OPENSSL_NO_MD5
142 {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-',
143 "Print old-style (MD5) issuer hash value"},
144 {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-',
145 "Print old-style (MD5) subject hash value"},
147 #ifndef OPENSSL_NO_ENGINE
148 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
150 {"preserve_dates", OPT_PRESERVE_DATES, '-', "preserve existing dates when signing"},
154 int x509_main(int argc, char **argv)
156 ASN1_INTEGER *sno = NULL;
157 ASN1_OBJECT *objtmp = NULL;
159 CONF *extconf = NULL;
160 EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL;
161 STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
162 STACK_OF(OPENSSL_STRING) *sigopts = NULL;
163 X509 *x = NULL, *xca = NULL;
164 X509_REQ *req = NULL, *rq = NULL;
165 X509_STORE *ctx = NULL;
166 const EVP_MD *digest = NULL;
167 char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL;
168 char *checkhost = NULL, *checkemail = NULL, *checkip = NULL, *exts = NULL;
169 char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;
170 char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;
172 int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0;
173 int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM;
174 int fingerprint = 0, reqfile = 0, checkend = 0;
175 int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
176 int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;
177 int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0;
178 int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;
179 int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0;
180 int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0, ext = 0;
182 time_t checkoffset = 0;
183 unsigned long certflag = 0;
184 int preserve_dates = 0;
187 #ifndef OPENSSL_NO_MD5
188 int subject_hash_old = 0, issuer_hash_old = 0;
191 ctx = X509_STORE_new();
194 X509_STORE_set_verify_cb(ctx, callb);
196 prog = opt_init(argc, argv, x509_options);
197 while ((o = opt_next()) != OPT_EOF) {
202 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
205 opt_help(x509_options);
209 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
216 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
220 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
224 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat))
228 if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat))
240 sigopts = sk_OPENSSL_STRING_new_null();
241 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
247 days = atoi(opt_arg());
250 passinarg = opt_arg();
271 CAkeyfile = opt_arg();
274 CAserial = opt_arg();
278 BIO_printf(bio_err, "Serial number supplied twice\n");
281 if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
284 case OPT_FORCE_PUBKEY:
285 fkeyfile = opt_arg();
288 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
290 "%s: Invalid trust object value %s\n",
294 if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
296 sk_ASN1_OBJECT_push(trust, objtmp);
301 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
303 "%s: Invalid reject object value %s\n",
308 && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
310 sk_ASN1_OBJECT_push(reject, objtmp);
319 if (!set_cert_ex(&certflag, opt_arg()))
323 if (!set_nameopt(opt_arg()))
327 e = setup_engine(opt_arg(), 0);
341 case OPT_NEXT_SERIAL:
362 case OPT_FINGERPRINT:
366 subject_hash = ++num;
368 case OPT_ISSUER_HASH:
402 case OPT_CACREATESERIAL:
403 CA_createserial = ++num;
414 #ifndef OPENSSL_NO_MD5
415 case OPT_SUBJECT_HASH_OLD:
416 subject_hash_old = ++num;
418 case OPT_ISSUER_HASH_OLD:
419 issuer_hash_old = ++num;
422 case OPT_SUBJECT_HASH_OLD:
423 case OPT_ISSUER_HASH_OLD:
434 if (!opt_imax(opt_arg(), &temp))
436 checkoffset = (time_t)temp;
437 if ((intmax_t)checkoffset != temp) {
438 BIO_printf(bio_err, "%s: checkend time out of range %s\n",
445 checkhost = opt_arg();
448 checkemail = opt_arg();
453 case OPT_PRESERVE_DATES:
454 if (days != DEF_DAYS)
459 if (!opt_md(opt_unknown(), &digest))
463 argc = opt_num_rest();
466 BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]);
470 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
471 BIO_printf(bio_err, "Error getting password\n");
475 if (!X509_STORE_set_default_paths(ctx)) {
476 ERR_print_errors(bio_err);
480 if (fkeyfile != NULL) {
481 fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key");
486 if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
488 } else if ((CA_flag) && (CAkeyfile == NULL)) {
490 "need to specify a CAkey if using the CA command\n");
494 if (extfile != NULL) {
496 if ((extconf = app_load_config(extfile)) == NULL)
498 if (extsect == NULL) {
499 extsect = NCONF_get_string(extconf, "default", "extensions");
500 if (extsect == NULL) {
505 X509V3_set_ctx_test(&ctx2);
506 X509V3_set_nconf(&ctx2, extconf);
507 if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
509 "Error Loading extension section %s\n", extsect);
510 ERR_print_errors(bio_err);
519 if (!sign_flag && !CA_flag) {
520 BIO_printf(bio_err, "We need a private key to sign with\n");
523 in = bio_open_default(infile, 'r', informat);
526 req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
530 ERR_print_errors(bio_err);
534 if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) {
535 BIO_printf(bio_err, "error unpacking public key\n");
538 i = X509_REQ_verify(req, pkey);
540 BIO_printf(bio_err, "Signature verification error\n");
541 ERR_print_errors(bio_err);
546 "Signature did not match the certificate request\n");
549 BIO_printf(bio_err, "Signature ok\n");
552 print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),
555 if ((x = X509_new()) == NULL)
559 sno = ASN1_INTEGER_new();
560 if (sno == NULL || !rand_serial(NULL, sno))
562 if (!X509_set_serialNumber(x, sno))
564 ASN1_INTEGER_free(sno);
566 } else if (!X509_set_serialNumber(x, sno)) {
570 if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req)))
572 if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req)))
574 if (!set_cert_times(x, NULL, NULL, days))
578 X509_set_pubkey(x, fkey);
580 pkey = X509_REQ_get0_pubkey(req);
581 X509_set_pubkey(x, pkey);
584 x = load_cert(infile, informat, "Certificate");
590 xca = load_cert(CAfile, CAformat, "CA Certificate");
595 out = bio_open_default(outfile, 'w', outformat);
599 if (!noout || text || next_serial)
600 OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
603 X509_alias_set1(x, (unsigned char *)alias, -1);
608 X509_reject_clear(x);
611 for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
612 objtmp = sk_ASN1_OBJECT_value(trust, i);
613 X509_add1_trust_object(x, objtmp);
618 if (reject != NULL) {
619 for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
620 objtmp = sk_ASN1_OBJECT_value(reject, i);
621 X509_add1_reject_object(x, objtmp);
627 const ASN1_BIT_STRING *signature;
629 X509_get0_signature(&signature, NULL, x);
630 corrupt_signature(signature);
634 for (i = 1; i <= num; i++) {
636 print_name(out, "issuer=", X509_get_issuer_name(x), get_nameopt());
637 } else if (subject == i) {
638 print_name(out, "subject=",
639 X509_get_subject_name(x), get_nameopt());
640 } else if (serial == i) {
641 BIO_printf(out, "serial=");
642 i2a_ASN1_INTEGER(out, X509_get_serialNumber(x));
643 BIO_printf(out, "\n");
644 } else if (next_serial == i) {
645 ASN1_INTEGER *ser = X509_get_serialNumber(x);
646 BIGNUM *bnser = ASN1_INTEGER_to_BN(ser, NULL);
650 if (!BN_add_word(bnser, 1))
652 ser = BN_to_ASN1_INTEGER(bnser, NULL);
656 i2a_ASN1_INTEGER(out, ser);
657 ASN1_INTEGER_free(ser);
659 } else if ((email == i) || (ocsp_uri == i)) {
661 STACK_OF(OPENSSL_STRING) *emlst;
663 emlst = X509_get1_email(x);
665 emlst = X509_get1_ocsp(x);
666 for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
667 BIO_printf(out, "%s\n",
668 sk_OPENSSL_STRING_value(emlst, j));
669 X509_email_free(emlst);
670 } else if (aliasout == i) {
671 unsigned char *alstr;
672 alstr = X509_alias_get0(x, NULL);
674 BIO_printf(out, "%s\n", alstr);
676 BIO_puts(out, "<No Alias>\n");
677 } else if (subject_hash == i) {
678 BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
680 #ifndef OPENSSL_NO_MD5
681 else if (subject_hash_old == i) {
682 BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
685 else if (issuer_hash == i) {
686 BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
688 #ifndef OPENSSL_NO_MD5
689 else if (issuer_hash_old == i) {
690 BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
693 else if (pprint == i) {
696 BIO_printf(out, "Certificate purposes:\n");
697 for (j = 0; j < X509_PURPOSE_get_count(); j++) {
698 ptmp = X509_PURPOSE_get0(j);
699 purpose_print(out, x, ptmp);
701 } else if (modulus == i) {
704 pkey = X509_get0_pubkey(x);
706 BIO_printf(bio_err, "Modulus=unavailable\n");
707 ERR_print_errors(bio_err);
710 BIO_printf(out, "Modulus=");
711 #ifndef OPENSSL_NO_RSA
712 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
714 RSA_get0_key(EVP_PKEY_get0_RSA(pkey), &n, NULL, NULL);
718 #ifndef OPENSSL_NO_DSA
719 if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA) {
720 const BIGNUM *dsapub = NULL;
721 DSA_get0_key(EVP_PKEY_get0_DSA(pkey), &dsapub, NULL);
722 BN_print(out, dsapub);
726 BIO_printf(out, "Wrong Algorithm type");
728 BIO_printf(out, "\n");
729 } else if (pubkey == i) {
732 pkey = X509_get0_pubkey(x);
734 BIO_printf(bio_err, "Error getting public key\n");
735 ERR_print_errors(bio_err);
738 PEM_write_bio_PUBKEY(out, pkey);
744 print_name(out, "/*\n"
745 " * Subject: ", X509_get_subject_name(x), get_nameopt());
746 print_name(out, " * Issuer: ", X509_get_issuer_name(x), get_nameopt());
747 BIO_puts(out, " */\n");
749 len = i2d_X509(x, NULL);
750 m = app_malloc(len, "x509 name buffer");
751 d = (unsigned char *)m;
752 len = i2d_X509_NAME(X509_get_subject_name(x), &d);
753 print_array(out, "the_subject_name", len, (unsigned char *)m);
754 d = (unsigned char *)m;
755 len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
756 print_array(out, "the_public_key", len, (unsigned char *)m);
757 d = (unsigned char *)m;
758 len = i2d_X509(x, &d);
759 print_array(out, "the_certificate", len, (unsigned char *)m);
761 } else if (text == i) {
762 X509_print_ex(out, x, get_nameopt(), certflag);
763 } else if (startdate == i) {
764 BIO_puts(out, "notBefore=");
765 ASN1_TIME_print(out, X509_get0_notBefore(x));
767 } else if (enddate == i) {
768 BIO_puts(out, "notAfter=");
769 ASN1_TIME_print(out, X509_get0_notAfter(x));
771 } else if (fingerprint == i) {
774 unsigned char md[EVP_MAX_MD_SIZE];
775 const EVP_MD *fdig = digest;
780 if (!X509_digest(x, fdig, md, &n)) {
781 BIO_printf(bio_err, "out of memory\n");
784 BIO_printf(out, "%s Fingerprint=",
785 OBJ_nid2sn(EVP_MD_type(fdig)));
786 for (j = 0; j < (int)n; j++) {
787 BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n)
792 /* should be in the library */
793 else if ((sign_flag == i) && (x509req == 0)) {
794 BIO_printf(bio_err, "Getting Private key\n");
796 Upkey = load_key(keyfile, keyformat, 0,
797 passin, e, "Private key");
802 if (!sign(x, Upkey, days, clrext, digest, extconf, extsect, preserve_dates))
804 } else if (CA_flag == i) {
805 BIO_printf(bio_err, "Getting CA Private Key\n");
806 if (CAkeyfile != NULL) {
807 CApkey = load_key(CAkeyfile, CAkeyformat,
808 0, passin, e, "CA Private Key");
813 if (!x509_certify(ctx, CAfile, digest, x, xca,
815 CAserial, CA_createserial, days, clrext,
816 extconf, extsect, sno, reqfile, preserve_dates))
818 } else if (x509req == i) {
821 BIO_printf(bio_err, "Getting request Private Key\n");
822 if (keyfile == NULL) {
823 BIO_printf(bio_err, "no request key file specified\n");
826 pk = load_key(keyfile, keyformat, 0,
827 passin, e, "request key");
832 BIO_printf(bio_err, "Generating certificate request\n");
834 rq = X509_to_X509_REQ(x, pk, digest);
837 ERR_print_errors(bio_err);
841 X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT);
842 PEM_write_bio_X509_REQ(out, rq);
845 } else if (ocspid == i) {
846 X509_ocspid_print(out, x);
847 } else if (ext == i) {
848 print_x509v3_exts(out, x, exts);
854 time_t tcheck = time(NULL) + checkoffset;
856 if (X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0) {
857 BIO_printf(out, "Certificate will expire\n");
860 BIO_printf(out, "Certificate will not expire\n");
866 print_cert_checks(out, x, checkhost, checkemail, checkip);
868 if (noout || nocert) {
873 if (outformat == FORMAT_ASN1) {
874 i = i2d_X509_bio(out, x);
875 } else if (outformat == FORMAT_PEM) {
877 i = PEM_write_bio_X509_AUX(out, x);
879 i = PEM_write_bio_X509(out, x);
881 BIO_printf(bio_err, "bad output format specified for outfile\n");
885 BIO_printf(bio_err, "unable to write certificate\n");
886 ERR_print_errors(bio_err);
893 X509_STORE_free(ctx);
897 EVP_PKEY_free(Upkey);
898 EVP_PKEY_free(CApkey);
900 sk_OPENSSL_STRING_free(sigopts);
902 ASN1_INTEGER_free(sno);
903 sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
904 sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
905 ASN1_OBJECT_free(objtmp);
907 OPENSSL_free(passin);
911 static ASN1_INTEGER *x509_load_serial(const char *CAfile,
912 const char *serialfile, int create)
915 ASN1_INTEGER *bs = NULL;
916 BIGNUM *serial = NULL;
918 if (serialfile == NULL) {
919 const char *p = strrchr(CAfile, '.');
920 size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
922 buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
923 memcpy(buf, CAfile, len);
924 memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
928 serial = load_serial(serialfile, create, NULL);
932 if (!BN_add_word(serial, 1)) {
933 BIO_printf(bio_err, "add_word failure\n");
937 if (!save_serial(serialfile, NULL, serial, &bs))
946 static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest,
947 X509 *x, X509 *xca, EVP_PKEY *pkey,
948 STACK_OF(OPENSSL_STRING) *sigopts,
949 const char *serialfile, int create,
950 int days, int clrext, CONF *conf, const char *section,
951 ASN1_INTEGER *sno, int reqfile, int preserve_dates)
954 ASN1_INTEGER *bs = NULL;
955 X509_STORE_CTX *xsc = NULL;
958 upkey = X509_get0_pubkey(xca);
960 BIO_printf(bio_err, "Error obtaining CA X509 public key\n");
963 EVP_PKEY_copy_parameters(upkey, pkey);
965 xsc = X509_STORE_CTX_new();
966 if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, x, NULL)) {
967 BIO_printf(bio_err, "Error initialising X509 store\n");
972 else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)
976 * NOTE: this certificate can/should be self signed, unless it was a
977 * certificate request in which case it is not.
979 X509_STORE_CTX_set_cert(xsc, x);
980 X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
981 if (!reqfile && X509_verify_cert(xsc) <= 0)
984 if (!X509_check_private_key(xca, pkey)) {
986 "CA certificate and CA private key do not match\n");
990 if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
992 if (!X509_set_serialNumber(x, bs))
995 if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
999 while (X509_get_ext_count(x) > 0)
1000 X509_delete_ext(x, 0);
1005 X509_set_version(x, 2); /* version 3 certificate */
1006 X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
1007 X509V3_set_nconf(&ctx2, conf);
1008 if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
1012 if (!do_X509_sign(x, pkey, digest, sigopts))
1016 X509_STORE_CTX_free(xsc);
1018 ERR_print_errors(bio_err);
1020 ASN1_INTEGER_free(bs);
1024 static int callb(int ok, X509_STORE_CTX *ctx)
1030 * it is ok to use a self signed certificate This case will catch both
1031 * the initial ok == 0 and the final ok == 1 calls to this function
1033 err = X509_STORE_CTX_get_error(ctx);
1034 if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1038 * BAD we should have gotten an error. Normally if everything worked
1039 * X509_STORE_CTX_get_error(ctx) will still be set to
1040 * DEPTH_ZERO_SELF_....
1044 "error with certificate to be certified - should be self signed\n");
1047 err_cert = X509_STORE_CTX_get_current_cert(ctx);
1048 print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
1050 "error with certificate - error %d at depth %d\n%s\n", err,
1051 X509_STORE_CTX_get_error_depth(ctx),
1052 X509_verify_cert_error_string(err));
1058 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
1059 const EVP_MD *digest, CONF *conf, const char *section,
1063 if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
1065 if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
1067 if (!X509_set_pubkey(x, pkey))
1070 while (X509_get_ext_count(x) > 0)
1071 X509_delete_ext(x, 0);
1075 X509_set_version(x, 2); /* version 3 certificate */
1076 X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
1077 X509V3_set_nconf(&ctx, conf);
1078 if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
1081 if (!X509_sign(x, pkey, digest))
1085 ERR_print_errors(bio_err);
1089 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
1093 id = X509_PURPOSE_get_id(pt);
1094 pname = X509_PURPOSE_get0_name(pt);
1095 for (i = 0; i < 2; i++) {
1096 idret = X509_check_purpose(cert, id, i);
1097 BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
1099 BIO_printf(bio, "Yes\n");
1100 else if (idret == 0)
1101 BIO_printf(bio, "No\n");
1103 BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
1108 static int parse_ext_names(char *names, const char **result)
1111 int cnt = 0, len = 0;
1114 len = strlen(names);
1116 while (q - names <= len) {
1117 if (*q != ',' && *q != '\0') {
1123 if (result != NULL) {
1135 static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names)
1137 const STACK_OF(X509_EXTENSION) *exts = NULL;
1138 STACK_OF(X509_EXTENSION) *exts2 = NULL;
1139 X509_EXTENSION *ext = NULL;
1141 int i, j, ret = 0, num, nn = 0;
1142 const char *sn, **names = NULL;
1143 char *tmp_ext_names = NULL;
1145 exts = X509_get0_extensions(x);
1146 if ((num = sk_X509_EXTENSION_num(exts)) <= 0) {
1147 BIO_printf(bio, "No extensions in certificate\n");
1152 /* parse comma separated ext name string */
1153 if ((tmp_ext_names = OPENSSL_strdup(ext_names)) == NULL)
1155 if ((nn = parse_ext_names(tmp_ext_names, NULL)) == 0) {
1156 BIO_printf(bio, "Invalid extension names: %s\n", ext_names);
1159 if ((names = OPENSSL_malloc(sizeof(char *) * nn)) == NULL)
1161 parse_ext_names(tmp_ext_names, names);
1163 for (i = 0; i < num; i++) {
1164 ext = sk_X509_EXTENSION_value(exts, i);
1166 /* check if this ext is what we want */
1167 obj = X509_EXTENSION_get_object(ext);
1168 sn = OBJ_nid2sn(OBJ_obj2nid(obj));
1169 if (sn == NULL || strcmp(sn, "UNDEF") == 0)
1172 for (j = 0; j < nn; j++) {
1173 if (strcmp(sn, names[j]) == 0) {
1174 /* push the extension into a new stack */
1176 && (exts2 = sk_X509_EXTENSION_new_null()) == NULL)
1178 if (!sk_X509_EXTENSION_push(exts2, ext))
1184 if (!sk_X509_EXTENSION_num(exts2)) {
1185 BIO_printf(bio, "No extensions matched with %s\n", ext_names);
1190 ret = X509V3_extensions_print(bio, NULL, exts2, 0, 0);
1192 sk_X509_EXTENSION_free(exts2);
1193 OPENSSL_free(names);
1194 OPENSSL_free(tmp_ext_names);