1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
62 #include <openssl/bio.h>
63 #include <openssl/asn1.h>
64 #include <openssl/err.h>
65 #include <openssl/bn.h>
66 #include <openssl/evp.h>
67 #include <openssl/x509.h>
68 #include <openssl/x509v3.h>
69 #include <openssl/objects.h>
70 #include <openssl/pem.h>
71 #ifndef OPENSSL_NO_RSA
72 # include <openssl/rsa.h>
74 #ifndef OPENSSL_NO_DSA
75 # include <openssl/dsa.h>
79 #define POSTFIX ".srl"
82 static int callb(int ok, X509_STORE_CTX *ctx);
83 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
84 const EVP_MD *digest, CONF *conf, char *section);
85 static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
86 X509 *x, X509 *xca, EVP_PKEY *pkey,
87 STACK_OF(OPENSSL_STRING) *sigopts, char *serial,
88 int create, int days, int clrext, CONF *conf,
89 char *section, ASN1_INTEGER *sno, int reqfile);
90 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
92 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
93 static int force_version = 2;
96 typedef enum OPTION_choice {
97 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
98 OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM,
99 OPT_CAKEYFORM, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE,
100 OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_SIGNKEY, OPT_CA,
101 OPT_CAKEY, OPT_CASERIAL, OPT_SET_SERIAL, OPT_FORCE_PUBKEY,
102 OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_NAMEOPT,
103 OPT_C, OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL,
104 OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH,
105 OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES,
106 OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST,
107 OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST,
108 OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID,
109 OPT_SUBJECT_HASH_OLD,
112 OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT
115 OPTIONS x509_options[] = {
116 {"help", OPT_HELP, '-', "Display this summary"},
117 {"inform", OPT_INFORM, 'f',
118 "Input format - default PEM (one of DER, NET or PEM)"},
119 {"in", OPT_IN, '<', "Input file - default stdin"},
120 {"outform", OPT_OUTFORM, 'f',
121 "Output format - default PEM (one of DER, NET or PEM)"},
122 {"out", OPT_OUT, '>', "Output file - default stdout"},
123 {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"},
124 {"passin", OPT_PASSIN, 's', "Private key password source"},
125 {"serial", OPT_SERIAL, '-', "Print serial number value"},
126 {"subject_hash", OPT_HASH, '-', "Print subject hash value"},
127 {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"},
128 {"hash", OPT_HASH, '-', "Synonym for -subject_hash"},
129 {"subject", OPT_SUBJECT, '-', "Print subject DN"},
130 {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
131 {"email", OPT_EMAIL, '-', "Print email address(es)"},
132 {"startdate", OPT_STARTDATE, '-', "Set notBefore field"},
133 {"enddate", OPT_ENDDATE, '-', "Set notAfter field"},
134 {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"},
135 {"dates", OPT_DATES, '-', "Both Before and After dates"},
136 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
137 {"pubkey", OPT_PUBKEY, '-', "Output the public key"},
138 {"fingerprint", OPT_FINGERPRINT, '-',
139 "Print the certificate fingerprint"},
140 {"alias", OPT_ALIAS, '-', "Output certificate alias"},
141 {"noout", OPT_NOOUT, '-', "No output, just status"},
142 {"nocert", OPT_NOCERT, '-', "No certificate output"},
143 {"ocspid", OPT_OCSPID, '-',
144 "Print OCSP hash values for the subject name and public key"},
145 {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"},
146 {"trustout", OPT_TRUSTOUT, '-', "Output a trusted certificate"},
147 {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"},
148 {"clrext", OPT_CLREXT, '-', "Clear all rejected purposes"},
149 {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"},
150 {"addreject", OPT_ADDREJECT, 's',
151 "Reject certificate for a given purpose"},
152 {"setalias", OPT_SETALIAS, 's', "Set certificate alias"},
153 {"days", OPT_DAYS, 'n',
154 "How long till expiry of a signed certificate - def 30 days"},
155 {"checkend", OPT_CHECKEND, 'p',
156 "Check whether the cert expires in the next arg seconds"},
157 {OPT_MORE_STR, 1, 1, "Exit 1 if so, 0 if not"},
158 {"signkey", OPT_SIGNKEY, '<', "Self sign cert with arg"},
159 {"x509toreq", OPT_X509TOREQ, '-',
160 "Output a certification request object"},
161 {"req", OPT_REQ, '-', "Input is a certificate request, sign and output"},
162 {"CA", OPT_CA, '<', "Set the CA certificate, must be PEM format"},
163 {"CAkey", OPT_CAKEY, '<',
164 "The CA key, must be PEM format; if not in CAfile"},
165 {"CAcreateserial", OPT_CACREATESERIAL, '-',
166 "Create serial number file if it does not exist"},
167 {"CAserial", OPT_CASERIAL, 's', "Serial file"},
168 {"set_serial", OPT_SET_SERIAL, 's', "Serial number to use"},
169 {"text", OPT_TEXT, '-', "Print the certificate in text form"},
170 {"C", OPT_C, '-', "Print out C code forms"},
171 {"extfile", OPT_EXTFILE, '<', "File with X509V3 extensions to add"},
172 {"extensions", OPT_EXTENSIONS, 's', "Section from config file to use"},
173 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
174 {"certopt", OPT_CERTOPT, 's', "Various certificate text options"},
175 {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"},
176 {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"},
177 {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"},
178 {"CAform", OPT_CAFORM, 'F', "CA format - default PEM"},
179 {"CAkeyform", OPT_CAKEYFORM, 'F', "CA key format - default PEM"},
180 {"sigopt", OPT_SIGOPT, 's'},
181 {"force_pubkey", OPT_FORCE_PUBKEY, '<'},
182 {"next_serial", OPT_NEXT_SERIAL, '-'},
183 {"clrreject", OPT_CLRREJECT, '-'},
184 {"badsig", OPT_BADSIG, '-'},
185 {"", OPT_MD, '-', "Any supported digest"},
186 #ifndef OPENSSL_NO_MD5
187 {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-',
188 "Print old-style (MD5) issuer hash value"},
189 {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-',
190 "Print old-style (MD5) subject hash value"},
192 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
193 {"force_version", OPT_FORCE_VERSION, 'p'},
195 #ifndef OPENSSL_NO_ENGINE
196 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
201 int x509_main(int argc, char **argv)
203 ASN1_INTEGER *sno = NULL;
206 CONF *extconf = NULL;
207 EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL;
208 STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
209 STACK_OF(OPENSSL_STRING) *sigopts = NULL;
210 X509 *x = NULL, *xca = NULL;
211 X509_REQ *req = NULL, *rq = NULL;
212 X509_STORE *ctx = NULL;
213 const EVP_MD *digest = NULL;
214 char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL;
215 char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;
216 char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;
217 char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;
218 char buf[256], *prog;
219 int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0;
220 int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM;
221 int fingerprint = 0, reqfile = 0, need_rand = 0, checkend = 0;
222 int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
223 int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;
224 int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0;
225 int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;
226 int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0;
227 int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0;
228 int checkoffset = 0, enddate = 0;
229 unsigned long nmflag = 0, certflag = 0;
233 #ifndef OPENSSL_NO_MD5
234 int subject_hash_old = 0, issuer_hash_old = 0;
237 ctx = X509_STORE_new();
240 X509_STORE_set_verify_cb(ctx, callb);
242 prog = opt_init(argc, argv, x509_options);
243 while ((o = opt_next()) != OPT_EOF) {
248 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
251 opt_help(x509_options);
255 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
262 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
266 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
270 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat))
274 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAkeyformat))
281 reqfile = need_rand = 1;
286 sigopts = sk_OPENSSL_STRING_new_null();
287 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
290 case OPT_FORCE_VERSION:
291 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
292 force_version = atoi(opt_arg()) - 1;
296 days = atoi(opt_arg());
299 passinarg = opt_arg();
318 CAkeyfile = opt_arg();
321 CAserial = opt_arg();
324 if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
327 case OPT_FORCE_PUBKEY:
328 fkeyfile = opt_arg();
331 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
333 "%s: Invalid trust object value %s\n",
337 if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
339 sk_ASN1_OBJECT_push(trust, objtmp);
343 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
345 "%s: Invalid reject object value %s\n",
350 && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
352 sk_ASN1_OBJECT_push(reject, objtmp);
360 if (!set_cert_ex(&certflag, opt_arg()))
365 if (!set_name_ex(&nmflag, opt_arg()))
369 e = setup_engine(opt_arg(), 0);
383 case OPT_NEXT_SERIAL:
404 case OPT_FINGERPRINT:
408 subject_hash = ++num;
410 case OPT_ISSUER_HASH:
440 case OPT_CACREATESERIAL:
441 CA_createserial = ++num;
452 #ifndef OPENSSL_NO_MD5
453 case OPT_SUBJECT_HASH_OLD:
454 subject_hash_old = ++num;
456 case OPT_ISSUER_HASH_OLD:
457 issuer_hash_old = ++num;
460 case OPT_SUBJECT_HASH_OLD:
461 case OPT_ISSUER_HASH_OLD:
469 checkoffset = atoi(opt_arg());
473 checkhost = opt_arg();
476 checkemail = opt_arg();
482 if (!opt_md(opt_unknown(), &digest))
486 argc = opt_num_rest();
489 BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]);
494 nmflag = XN_FLAG_ONELINE;
496 if (!app_load_modules(NULL))
499 out = bio_open_default(outfile, 'w', outformat);
504 app_RAND_load_file(NULL, 0);
506 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
507 BIO_printf(bio_err, "Error getting password\n");
511 if (!X509_STORE_set_default_paths(ctx)) {
512 ERR_print_errors(bio_err);
517 fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key");
522 if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
524 } else if ((CA_flag) && (CAkeyfile == NULL)) {
526 "need to specify a CAkey if using the CA command\n");
532 if ((extconf = app_load_config(extfile)) == NULL)
535 extsect = NCONF_get_string(extconf, "default", "extensions");
541 X509V3_set_ctx_test(&ctx2);
542 X509V3_set_nconf(&ctx2, extconf);
543 if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
545 "Error Loading extension section %s\n", extsect);
546 ERR_print_errors(bio_err);
555 if (!sign_flag && !CA_flag) {
556 BIO_printf(bio_err, "We need a private key to sign with\n");
559 in = bio_open_default(infile, 'r', informat);
562 req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
566 ERR_print_errors(bio_err);
570 if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {
571 BIO_printf(bio_err, "error unpacking public key\n");
574 i = X509_REQ_verify(req, pkey);
577 BIO_printf(bio_err, "Signature verification error\n");
578 ERR_print_errors(bio_err);
583 "Signature did not match the certificate request\n");
586 BIO_printf(bio_err, "Signature ok\n");
588 print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),
591 if ((x = X509_new()) == NULL)
595 sno = ASN1_INTEGER_new();
596 if (!sno || !rand_serial(NULL, sno))
598 if (!X509_set_serialNumber(x, sno))
600 ASN1_INTEGER_free(sno);
602 } else if (!X509_set_serialNumber(x, sno))
605 if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req)))
607 if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req)))
610 X509_gmtime_adj(X509_get_notBefore(x), 0);
611 X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);
613 X509_set_pubkey(x, fkey);
615 pkey = X509_REQ_get_pubkey(req);
616 X509_set_pubkey(x, pkey);
620 x = load_cert(infile, informat, NULL, e, "Certificate");
625 xca = load_cert(CAfile, CAformat, NULL, e, "CA Certificate");
630 if (!noout || text || next_serial) {
631 OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
636 X509_alias_set1(x, (unsigned char *)alias, -1);
641 X509_reject_clear(x);
644 for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
645 objtmp = sk_ASN1_OBJECT_value(trust, i);
646 X509_add1_trust_object(x, objtmp);
651 for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
652 objtmp = sk_ASN1_OBJECT_value(reject, i);
653 X509_add1_reject_object(x, objtmp);
658 for (i = 1; i <= num; i++) {
660 print_name(out, "issuer= ", X509_get_issuer_name(x), nmflag);
661 } else if (subject == i) {
662 print_name(out, "subject= ",
663 X509_get_subject_name(x), nmflag);
664 } else if (serial == i) {
665 BIO_printf(out, "serial=");
666 i2a_ASN1_INTEGER(out, X509_get_serialNumber(x));
667 BIO_printf(out, "\n");
668 } else if (next_serial == i) {
671 ser = X509_get_serialNumber(x);
672 bnser = ASN1_INTEGER_to_BN(ser, NULL);
675 if (!BN_add_word(bnser, 1))
677 ser = BN_to_ASN1_INTEGER(bnser, NULL);
681 i2a_ASN1_INTEGER(out, ser);
682 ASN1_INTEGER_free(ser);
684 } else if ((email == i) || (ocsp_uri == i)) {
686 STACK_OF(OPENSSL_STRING) *emlst;
688 emlst = X509_get1_email(x);
690 emlst = X509_get1_ocsp(x);
691 for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
692 BIO_printf(out, "%s\n",
693 sk_OPENSSL_STRING_value(emlst, j));
694 X509_email_free(emlst);
695 } else if (aliasout == i) {
696 unsigned char *alstr;
697 alstr = X509_alias_get0(x, NULL);
699 BIO_printf(out, "%s\n", alstr);
701 BIO_puts(out, "<No Alias>\n");
702 } else if (subject_hash == i) {
703 BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
705 #ifndef OPENSSL_NO_MD5
706 else if (subject_hash_old == i) {
707 BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
710 else if (issuer_hash == i) {
711 BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
713 #ifndef OPENSSL_NO_MD5
714 else if (issuer_hash_old == i) {
715 BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
718 else if (pprint == i) {
721 BIO_printf(out, "Certificate purposes:\n");
722 for (j = 0; j < X509_PURPOSE_get_count(); j++) {
723 ptmp = X509_PURPOSE_get0(j);
724 purpose_print(out, x, ptmp);
726 } else if (modulus == i) {
729 pkey = X509_get_pubkey(x);
731 BIO_printf(bio_err, "Modulus=unavailable\n");
732 ERR_print_errors(bio_err);
735 BIO_printf(out, "Modulus=");
736 #ifndef OPENSSL_NO_RSA
737 if (pkey->type == EVP_PKEY_RSA)
738 BN_print(out, pkey->pkey.rsa->n);
741 #ifndef OPENSSL_NO_DSA
742 if (pkey->type == EVP_PKEY_DSA)
743 BN_print(out, pkey->pkey.dsa->pub_key);
746 BIO_printf(out, "Wrong Algorithm type");
747 BIO_printf(out, "\n");
749 } else if (pubkey == i) {
752 pkey = X509_get_pubkey(x);
754 BIO_printf(bio_err, "Error getting public key\n");
755 ERR_print_errors(bio_err);
758 PEM_write_bio_PUBKEY(out, pkey);
765 X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof buf);
766 BIO_printf(out, "/*\n"
767 " * Subject: %s\n", buf);
769 m = X509_NAME_oneline(X509_get_issuer_name(x), buf, sizeof buf);
770 BIO_printf(out, " * Issuer: %s\n"
773 len = i2d_X509(x, NULL);
774 m = app_malloc(len, "x509 name buffer");
775 d = (unsigned char *)m;
776 len = i2d_X509_NAME(X509_get_subject_name(x), &d);
777 print_array(out, "the_subject_name", len, (unsigned char *)m);
778 d = (unsigned char *)m;
779 len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
780 print_array(out, "the_public_key", len, (unsigned char *)m);
781 d = (unsigned char *)m;
782 len = i2d_X509(x, &d);
783 print_array(out, "the_certificate", len, (unsigned char *)m);
785 } else if (text == i) {
786 X509_print_ex(out, x, nmflag, certflag);
787 } else if (startdate == i) {
788 BIO_puts(out, "notBefore=");
789 ASN1_TIME_print(out, X509_get_notBefore(x));
791 } else if (enddate == i) {
792 BIO_puts(out, "notAfter=");
793 ASN1_TIME_print(out, X509_get_notAfter(x));
795 } else if (fingerprint == i) {
798 unsigned char md[EVP_MAX_MD_SIZE];
799 const EVP_MD *fdig = digest;
804 if (!X509_digest(x, fdig, md, &n)) {
805 BIO_printf(bio_err, "out of memory\n");
808 BIO_printf(out, "%s Fingerprint=",
809 OBJ_nid2sn(EVP_MD_type(fdig)));
810 for (j = 0; j < (int)n; j++) {
811 BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n)
816 /* should be in the library */
817 else if ((sign_flag == i) && (x509req == 0)) {
818 BIO_printf(bio_err, "Getting Private key\n");
820 Upkey = load_key(keyfile, keyformat, 0,
821 passin, e, "Private key");
827 if (!sign(x, Upkey, days, clrext, digest, extconf, extsect))
829 } else if (CA_flag == i) {
830 BIO_printf(bio_err, "Getting CA Private Key\n");
831 if (CAkeyfile != NULL) {
832 CApkey = load_key(CAkeyfile, CAkeyformat,
833 0, passin, e, "CA Private Key");
839 if (!x509_certify(ctx, CAfile, digest, x, xca,
841 CAserial, CA_createserial, days, clrext,
842 extconf, extsect, sno, reqfile))
844 } else if (x509req == i) {
847 BIO_printf(bio_err, "Getting request Private Key\n");
848 if (keyfile == NULL) {
849 BIO_printf(bio_err, "no request key file specified\n");
852 pk = load_key(keyfile, keyformat, 0,
853 passin, e, "request key");
858 BIO_printf(bio_err, "Generating certificate request\n");
860 rq = X509_to_X509_REQ(x, pk, digest);
863 ERR_print_errors(bio_err);
867 X509_REQ_print(out, rq);
868 PEM_write_bio_X509_REQ(out, rq);
871 } else if (ocspid == i) {
872 X509_ocspid_print(out, x);
878 time_t tcheck = time(NULL) + checkoffset;
880 if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {
881 BIO_printf(out, "Certificate will expire\n");
884 BIO_printf(out, "Certificate will not expire\n");
890 print_cert_checks(out, x, checkhost, checkemail, checkip);
892 if (noout || nocert) {
898 ASN1_BIT_STRING *signature;
900 X509_get0_signature(&signature, NULL, x);
901 s = ASN1_STRING_data(signature);
902 s[ASN1_STRING_length(signature) - 1] ^= 0x1;
905 if (outformat == FORMAT_ASN1)
906 i = i2d_X509_bio(out, x);
907 else if (outformat == FORMAT_PEM) {
909 i = PEM_write_bio_X509_AUX(out, x);
911 i = PEM_write_bio_X509(out, x);
913 BIO_printf(bio_err, "bad output format specified for outfile\n");
917 BIO_printf(bio_err, "unable to write certificate\n");
918 ERR_print_errors(bio_err);
924 app_RAND_write_file(NULL);
928 X509_STORE_free(ctx);
932 EVP_PKEY_free(Upkey);
933 EVP_PKEY_free(CApkey);
935 sk_OPENSSL_STRING_free(sigopts);
937 ASN1_INTEGER_free(sno);
938 sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
939 sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
940 OPENSSL_free(passin);
944 static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile,
947 char *buf = NULL, *p;
948 ASN1_INTEGER *bs = NULL;
949 BIGNUM *serial = NULL;
952 len = ((serialfile == NULL)
953 ? (strlen(CAfile) + strlen(POSTFIX) + 1)
954 : (strlen(serialfile))) + 1;
955 buf = app_malloc(len, "serial# buffer");
956 if (serialfile == NULL) {
957 BUF_strlcpy(buf, CAfile, len);
958 for (p = buf; *p; p++)
963 BUF_strlcat(buf, POSTFIX, len);
965 BUF_strlcpy(buf, serialfile, len);
967 serial = load_serial(buf, create, NULL);
971 if (!BN_add_word(serial, 1)) {
972 BIO_printf(bio_err, "add_word failure\n");
976 if (!save_serial(buf, NULL, serial, &bs))
985 static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
986 X509 *x, X509 *xca, EVP_PKEY *pkey,
987 STACK_OF(OPENSSL_STRING) *sigopts,
988 char *serialfile, int create,
989 int days, int clrext, CONF *conf, char *section,
990 ASN1_INTEGER *sno, int reqfile)
993 ASN1_INTEGER *bs = NULL;
997 upkey = X509_get_pubkey(xca);
998 EVP_PKEY_copy_parameters(upkey, pkey);
999 EVP_PKEY_free(upkey);
1001 if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) {
1002 BIO_printf(bio_err, "Error initialising X509 store\n");
1007 else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)
1011 * NOTE: this certificate can/should be self signed, unless it was a
1012 * certificate request in which case it is not.
1014 X509_STORE_CTX_set_cert(&xsc, x);
1015 X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
1016 if (!reqfile && X509_verify_cert(&xsc) <= 0)
1019 if (!X509_check_private_key(xca, pkey)) {
1021 "CA certificate and CA private key do not match\n");
1025 if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
1027 if (!X509_set_serialNumber(x, bs))
1030 if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL)
1033 /* hardwired expired */
1034 if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
1038 while (X509_get_ext_count(x) > 0)
1039 X509_delete_ext(x, 0);
1044 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1045 X509_set_version(x, force_version);
1047 X509_set_version(x, 2); /* version 3 certificate */
1049 X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
1050 X509V3_set_nconf(&ctx2, conf);
1051 if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
1055 if (!do_X509_sign(x, pkey, digest, sigopts))
1059 X509_STORE_CTX_cleanup(&xsc);
1061 ERR_print_errors(bio_err);
1063 ASN1_INTEGER_free(bs);
1067 static int callb(int ok, X509_STORE_CTX *ctx)
1073 * it is ok to use a self signed certificate This case will catch both
1074 * the initial ok == 0 and the final ok == 1 calls to this function
1076 err = X509_STORE_CTX_get_error(ctx);
1077 if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1081 * BAD we should have gotten an error. Normally if everything worked
1082 * X509_STORE_CTX_get_error(ctx) will still be set to
1083 * DEPTH_ZERO_SELF_....
1087 "error with certificate to be certified - should be self signed\n");
1090 err_cert = X509_STORE_CTX_get_current_cert(ctx);
1091 print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
1093 "error with certificate - error %d at depth %d\n%s\n", err,
1094 X509_STORE_CTX_get_error_depth(ctx),
1095 X509_verify_cert_error_string(err));
1101 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
1102 const EVP_MD *digest, CONF *conf, char *section)
1105 if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
1107 if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL)
1110 if (X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days) ==
1114 if (!X509_set_pubkey(x, pkey))
1117 while (X509_get_ext_count(x) > 0)
1118 X509_delete_ext(x, 0);
1122 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1123 X509_set_version(x, force_version);
1125 X509_set_version(x, 2); /* version 3 certificate */
1127 X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
1128 X509V3_set_nconf(&ctx, conf);
1129 if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
1132 if (!X509_sign(x, pkey, digest))
1136 ERR_print_errors(bio_err);
1140 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
1144 id = X509_PURPOSE_get_id(pt);
1145 pname = X509_PURPOSE_get0_name(pt);
1146 for (i = 0; i < 2; i++) {
1147 idret = X509_check_purpose(cert, id, i);
1148 BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
1150 BIO_printf(bio, "Yes\n");
1151 else if (idret == 0)
1152 BIO_printf(bio, "No\n");
1154 BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);