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.]
63 #include <openssl/bio.h>
64 #include <openssl/asn1.h>
65 #include <openssl/err.h>
66 #include <openssl/bn.h>
67 #include <openssl/evp.h>
68 #include <openssl/x509.h>
69 #include <openssl/x509v3.h>
70 #include <openssl/objects.h>
71 #include <openssl/pem.h>
72 #ifndef OPENSSL_NO_RSA
73 # include <openssl/rsa.h>
75 #ifndef OPENSSL_NO_DSA
76 # include <openssl/dsa.h>
80 #define POSTFIX ".srl"
83 static int callb(int ok, X509_STORE_CTX *ctx);
84 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
85 const EVP_MD *digest, CONF *conf, char *section);
86 static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
87 X509 *x, X509 *xca, EVP_PKEY *pkey,
88 STACK_OF(OPENSSL_STRING) *sigopts, char *serial,
89 int create, int days, int clrext, CONF *conf,
90 char *section, ASN1_INTEGER *sno, int reqfile);
91 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
93 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
94 static int force_version = 2;
97 typedef enum OPTION_choice {
98 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
99 OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM,
100 OPT_CAKEYFORM, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE,
101 OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_SIGNKEY, OPT_CA,
102 OPT_CAKEY, OPT_CASERIAL, OPT_SET_SERIAL, OPT_FORCE_PUBKEY,
103 OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_NAMEOPT,
104 OPT_C, OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL,
105 OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH,
106 OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES,
107 OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST,
108 OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST,
109 OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID,
110 #ifndef OPENSSL_NO_MD5
111 OPT_SUBJECT_HASH_OLD,
114 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
117 OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT
120 OPTIONS x509_options[] = {
121 {"help", OPT_HELP, '-', "Display this summary"},
122 {"inform", OPT_INFORM, 'f',
123 "Input format - default PEM (one of DER, NET or PEM)"},
124 {"in", OPT_IN, '<', "Input file - default stdin"},
125 {"outform", OPT_OUTFORM, 'f',
126 "Output format - default PEM (one of DER, NET or PEM)"},
127 {"out", OPT_OUT, '>', "Output file - default stdout"},
128 {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"},
129 {"passin", OPT_PASSIN, 's', "Private key password source"},
130 {"serial", OPT_SERIAL, '-', "Print serial number value"},
131 {"subject_hash", OPT_HASH, '-', "Print subject hash value"},
132 {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"},
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"},
139 {"hash", OPT_HASH, '-', "Synonym for -subject_hash"},
140 {"subject", OPT_SUBJECT, '-', "Print subject DN"},
141 {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
142 {"email", OPT_EMAIL, '-', "Print email address(es)"},
143 {"startdate", OPT_STARTDATE, '-', "Set notBefore field"},
144 {"enddate", OPT_ENDDATE, '-', "Set notAfter field"},
145 {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"},
146 {"dates", OPT_DATES, '-', "Both Before and After dates"},
147 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
148 {"pubkey", OPT_PUBKEY, '-', "Output the public key"},
149 {"fingerprint", OPT_FINGERPRINT, '-',
150 "Print the certificate fingerprint"},
151 {"alias", OPT_ALIAS, '-', "Output certificate alias"},
152 {"noout", OPT_NOOUT, '-', "No output, just status"},
153 {"nocert", OPT_NOCERT, '-', "No certificate output"},
154 {"ocspid", OPT_OCSPID, '-',
155 "Print OCSP hash values for the subject name and public key"},
156 {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"},
157 {"trustout", OPT_TRUSTOUT, '-', "Output a trusted certificate"},
158 {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"},
159 {"clrext", OPT_CLREXT, '-', "Clear all rejected purposes"},
160 {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"},
161 {"addreject", OPT_ADDREJECT, 's',
162 "Reject certificate for a given purpose"},
163 {"setalias", OPT_SETALIAS, 's', "Set certificate alias"},
164 {"days", OPT_DAYS, 'n',
165 "How long till expiry of a signed certificate - def 30 days"},
166 {"checkend", OPT_CHECKEND, 'p',
167 "Check whether the cert expires in the next arg seconds"},
168 {OPT_MORE_STR, 1, 1, "Exit 1 if so, 0 if not"},
169 {"signkey", OPT_SIGNKEY, '<', "Self sign cert with arg"},
170 {"x509toreq", OPT_X509TOREQ, '-',
171 "Output a certification request object"},
172 {"req", OPT_REQ, '-', "Input is a certificate request, sign and output"},
173 {"CA", OPT_CA, '<', "Set the CA certificate, must be PEM format"},
174 {"CAkey", OPT_CAKEY, '<',
175 "The CA key, must be PEM format; if not in CAfile"},
176 {"CAcreateserial", OPT_CACREATESERIAL, '-',
177 "Create serial number file if it does not exist"},
178 {"CAserial", OPT_CASERIAL, 's', "Serial file"},
179 {"set_serial", OPT_SET_SERIAL, 's', "Serial number to use"},
180 {"text", OPT_TEXT, '-', "Print the certificate in text form"},
181 {"C", OPT_C, '-', "Print out C code forms"},
182 {"extfile", OPT_EXTFILE, '<', "File with X509V3 extensions to add"},
183 {"extensions", OPT_EXTENSIONS, 's', "Section from config file to use"},
184 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
185 {"certopt", OPT_CERTOPT, 's', "Various certificate text options"},
186 {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"},
187 {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"},
188 {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"},
189 {"CAform", OPT_CAFORM, 'F', "CA format - default PEM"},
190 {"CAkeyform", OPT_CAKEYFORM, 'F', "CA key format - default PEM"},
191 {"sigopt", OPT_SIGOPT, 's'},
192 {"force_pubkey", OPT_FORCE_PUBKEY, '<'},
193 {"next_serial", OPT_NEXT_SERIAL, '-'},
194 {"clrreject", OPT_CLRREJECT, '-'},
195 {"badsig", OPT_BADSIG, '-'},
196 {"", OPT_MD, '-', "Any supported digest"},
197 #ifndef OPENSSL_NO_ENGINE
198 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
200 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
201 {"force_version", OPT_FORCE_VERSION, 'p'},
206 int x509_main(int argc, char **argv)
208 ASN1_INTEGER *sno = NULL;
211 CONF *extconf = NULL;
212 EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL;
213 STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
214 STACK_OF(OPENSSL_STRING) *sigopts = NULL;
215 X509 *x = NULL, *xca = NULL;
216 X509_REQ *req = NULL, *rq = NULL;
217 X509_STORE *ctx = NULL;
218 const EVP_MD *digest = NULL;
219 char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL;
220 char *checkhost = NULL, *checkemail = NULL, *checkip = NULL;
221 char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL;
222 char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL;
223 char buf[256], *prog;
224 int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0;
225 int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM;
226 int fingerprint = 0, reqfile = 0, need_rand = 0, checkend = 0;
227 int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
228 int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0;
229 int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0;
230 int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0;
231 int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0;
232 int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0;
233 int checkoffset = 0, enddate = 0;
234 unsigned long nmflag = 0, certflag = 0;
237 #ifndef OPENSSL_NO_MD5
238 int subject_hash_old = 0, issuer_hash_old = 0;
241 ctx = X509_STORE_new();
244 X509_STORE_set_verify_cb(ctx, callb);
246 prog = opt_init(argc, argv, x509_options);
247 while ((o = opt_next()) != OPT_EOF) {
252 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
255 opt_help(x509_options);
259 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
266 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
270 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
274 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat))
278 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAkeyformat))
285 reqfile = need_rand = 1;
290 sigopts = sk_OPENSSL_STRING_new_null();
291 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
294 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
295 case OPT_FORCE_VERSION:
296 force_version = atoi(opt_arg()) - 1;
300 days = atoi(opt_arg());
303 passinarg = opt_arg();
322 CAkeyfile = opt_arg();
325 CAserial = opt_arg();
328 if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL)
331 case OPT_FORCE_PUBKEY:
332 fkeyfile = opt_arg();
335 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
337 "%s: Invalid trust object value %s\n",
341 if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL)
343 sk_ASN1_OBJECT_push(trust, objtmp);
347 if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) {
349 "%s: Invalid reject object value %s\n",
354 && (reject = sk_ASN1_OBJECT_new_null()) == NULL)
356 sk_ASN1_OBJECT_push(reject, objtmp);
364 if (!set_cert_ex(&certflag, opt_arg()))
368 if (!set_name_ex(&nmflag, opt_arg()))
372 e = setup_engine(opt_arg(), 0);
386 case OPT_NEXT_SERIAL:
407 case OPT_FINGERPRINT:
411 subject_hash = ++num;
413 case OPT_ISSUER_HASH:
443 case OPT_CACREATESERIAL:
444 CA_createserial = ++num;
455 #ifndef OPENSSL_NO_MD5
456 case OPT_SUBJECT_HASH_OLD:
457 subject_hash_old = ++num;
459 case OPT_ISSUER_HASH_OLD:
460 issuer_hash_old = ++num;
468 checkoffset = atoi(opt_arg());
472 checkhost = opt_arg();
475 checkemail = opt_arg();
481 if (!opt_md(opt_unknown(), &digest))
485 argc = opt_num_rest();
488 BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]);
492 out = bio_open_default(outfile, "w");
497 app_RAND_load_file(NULL, 0);
499 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
500 BIO_printf(bio_err, "Error getting password\n");
504 if (!X509_STORE_set_default_paths(ctx)) {
505 ERR_print_errors(bio_err);
510 fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key");
515 if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) {
517 } else if ((CA_flag) && (CAkeyfile == NULL)) {
519 "need to specify a CAkey if using the CA command\n");
525 if ((extconf = app_load_config(extfile)) == NULL)
528 extsect = NCONF_get_string(extconf, "default", "extensions");
534 X509V3_set_ctx_test(&ctx2);
535 X509V3_set_nconf(&ctx2, extconf);
536 if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) {
538 "Error Loading extension section %s\n", extsect);
539 ERR_print_errors(bio_err);
548 if (!sign_flag && !CA_flag) {
549 BIO_printf(bio_err, "We need a private key to sign with\n");
552 in = bio_open_default(infile, "r");
555 req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
559 ERR_print_errors(bio_err);
563 if ((req->req_info == NULL) ||
564 (req->req_info->pubkey == NULL) ||
565 (req->req_info->pubkey->public_key == NULL) ||
566 (req->req_info->pubkey->public_key->data == NULL)) {
568 "The certificate request appears to corrupted\n");
569 BIO_printf(bio_err, "It does not contain a public key\n");
572 if ((pkey = X509_REQ_get_pubkey(req)) == NULL) {
573 BIO_printf(bio_err, "error unpacking public key\n");
576 i = X509_REQ_verify(req, pkey);
579 BIO_printf(bio_err, "Signature verification error\n");
580 ERR_print_errors(bio_err);
585 "Signature did not match the certificate request\n");
588 BIO_printf(bio_err, "Signature ok\n");
590 print_name(bio_err, "subject=", X509_REQ_get_subject_name(req),
593 if ((x = X509_new()) == NULL)
597 sno = ASN1_INTEGER_new();
598 if (!sno || !rand_serial(NULL, sno))
600 if (!X509_set_serialNumber(x, sno))
602 ASN1_INTEGER_free(sno);
604 } else if (!X509_set_serialNumber(x, sno))
607 if (!X509_set_issuer_name(x, req->req_info->subject))
609 if (!X509_set_subject_name(x, req->req_info->subject))
612 X509_gmtime_adj(X509_get_notBefore(x), 0);
613 X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL);
615 X509_set_pubkey(x, fkey);
617 pkey = X509_REQ_get_pubkey(req);
618 X509_set_pubkey(x, pkey);
622 x = load_cert(infile, informat, NULL, e, "Certificate");
627 xca = load_cert(CAfile, CAformat, NULL, e, "CA Certificate");
632 if (!noout || text || next_serial) {
633 OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
638 X509_alias_set1(x, (unsigned char *)alias, -1);
643 X509_reject_clear(x);
646 for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) {
647 objtmp = sk_ASN1_OBJECT_value(trust, i);
648 X509_add1_trust_object(x, objtmp);
653 for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) {
654 objtmp = sk_ASN1_OBJECT_value(reject, i);
655 X509_add1_reject_object(x, objtmp);
660 for (i = 1; i <= num; i++) {
662 print_name(out, "issuer= ", X509_get_issuer_name(x), nmflag);
663 } else if (subject == i) {
664 print_name(out, "subject= ",
665 X509_get_subject_name(x), nmflag);
666 } else if (serial == i) {
667 BIO_printf(out, "serial=");
668 i2a_ASN1_INTEGER(out, X509_get_serialNumber(x));
669 BIO_printf(out, "\n");
670 } else if (next_serial == i) {
673 ser = X509_get_serialNumber(x);
674 bnser = ASN1_INTEGER_to_BN(ser, NULL);
677 if (!BN_add_word(bnser, 1))
679 ser = BN_to_ASN1_INTEGER(bnser, NULL);
683 i2a_ASN1_INTEGER(out, ser);
684 ASN1_INTEGER_free(ser);
686 } else if ((email == i) || (ocsp_uri == i)) {
688 STACK_OF(OPENSSL_STRING) *emlst;
690 emlst = X509_get1_email(x);
692 emlst = X509_get1_ocsp(x);
693 for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++)
694 BIO_printf(out, "%s\n",
695 sk_OPENSSL_STRING_value(emlst, j));
696 X509_email_free(emlst);
697 } else if (aliasout == i) {
698 unsigned char *alstr;
699 alstr = X509_alias_get0(x, NULL);
701 BIO_printf(out, "%s\n", alstr);
703 BIO_puts(out, "<No Alias>\n");
704 } else if (subject_hash == i) {
705 BIO_printf(out, "%08lx\n", X509_subject_name_hash(x));
707 #ifndef OPENSSL_NO_MD5
708 else if (subject_hash_old == i) {
709 BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x));
712 else if (issuer_hash == i) {
713 BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x));
715 #ifndef OPENSSL_NO_MD5
716 else if (issuer_hash_old == i) {
717 BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x));
720 else if (pprint == i) {
723 BIO_printf(out, "Certificate purposes:\n");
724 for (j = 0; j < X509_PURPOSE_get_count(); j++) {
725 ptmp = X509_PURPOSE_get0(j);
726 purpose_print(out, x, ptmp);
728 } else if (modulus == i) {
731 pkey = X509_get_pubkey(x);
733 BIO_printf(bio_err, "Modulus=unavailable\n");
734 ERR_print_errors(bio_err);
737 BIO_printf(out, "Modulus=");
738 #ifndef OPENSSL_NO_RSA
739 if (pkey->type == EVP_PKEY_RSA)
740 BN_print(out, pkey->pkey.rsa->n);
743 #ifndef OPENSSL_NO_DSA
744 if (pkey->type == EVP_PKEY_DSA)
745 BN_print(out, pkey->pkey.dsa->pub_key);
748 BIO_printf(out, "Wrong Algorithm type");
749 BIO_printf(out, "\n");
751 } else if (pubkey == i) {
754 pkey = X509_get_pubkey(x);
756 BIO_printf(bio_err, "Error getting public key\n");
757 ERR_print_errors(bio_err);
760 PEM_write_bio_PUBKEY(out, pkey);
767 X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof buf);
768 BIO_printf(out, "/*\n"
769 " * Subject: %s\n", buf);
771 m = X509_NAME_oneline(X509_get_issuer_name(x), buf, sizeof buf);
772 BIO_printf(out, " * Issuer: %s\n"
775 len = i2d_X509(x, NULL);
776 m = app_malloc(len, "x509 name buffer");
777 d = (unsigned char *)m;
778 len = i2d_X509_NAME(X509_get_subject_name(x), &d);
779 print_array(out, "the_subject_name", len, (unsigned char *)m);
780 d = (unsigned char *)m;
781 len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d);
782 print_array(out, "the_public_key", len, (unsigned char *)m);
783 d = (unsigned char *)m;
784 len = i2d_X509(x, &d);
785 print_array(out, "the_certificate", len, (unsigned char *)m);
787 } else if (text == i) {
788 X509_print_ex(out, x, nmflag, certflag);
789 } else if (startdate == i) {
790 BIO_puts(out, "notBefore=");
791 ASN1_TIME_print(out, X509_get_notBefore(x));
793 } else if (enddate == i) {
794 BIO_puts(out, "notAfter=");
795 ASN1_TIME_print(out, X509_get_notAfter(x));
797 } else if (fingerprint == i) {
800 unsigned char md[EVP_MAX_MD_SIZE];
801 const EVP_MD *fdig = digest;
806 if (!X509_digest(x, fdig, md, &n)) {
807 BIO_printf(bio_err, "out of memory\n");
810 BIO_printf(out, "%s Fingerprint=",
811 OBJ_nid2sn(EVP_MD_type(fdig)));
812 for (j = 0; j < (int)n; j++) {
813 BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n)
818 /* should be in the library */
819 else if ((sign_flag == i) && (x509req == 0)) {
820 BIO_printf(bio_err, "Getting Private key\n");
822 Upkey = load_key(keyfile, keyformat, 0,
823 passin, e, "Private key");
829 if (!sign(x, Upkey, days, clrext, digest, extconf, extsect))
831 } else if (CA_flag == i) {
832 BIO_printf(bio_err, "Getting CA Private Key\n");
833 if (CAkeyfile != NULL) {
834 CApkey = load_key(CAkeyfile, CAkeyformat,
835 0, passin, e, "CA Private Key");
841 if (!x509_certify(ctx, CAfile, digest, x, xca,
843 CAserial, CA_createserial, days, clrext,
844 extconf, extsect, sno, reqfile))
846 } else if (x509req == i) {
849 BIO_printf(bio_err, "Getting request Private Key\n");
850 if (keyfile == NULL) {
851 BIO_printf(bio_err, "no request key file specified\n");
854 pk = load_key(keyfile, keyformat, 0,
855 passin, e, "request key");
860 BIO_printf(bio_err, "Generating certificate request\n");
862 rq = X509_to_X509_REQ(x, pk, digest);
865 ERR_print_errors(bio_err);
869 X509_REQ_print(out, rq);
870 PEM_write_bio_X509_REQ(out, rq);
873 } else if (ocspid == i) {
874 X509_ocspid_print(out, x);
880 time_t tcheck = time(NULL) + checkoffset;
882 if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) {
883 BIO_printf(out, "Certificate will expire\n");
886 BIO_printf(out, "Certificate will not expire\n");
892 print_cert_checks(out, x, checkhost, checkemail, checkip);
894 if (noout || nocert) {
900 x->signature->data[x->signature->length - 1] ^= 0x1;
902 if (outformat == FORMAT_ASN1)
903 i = i2d_X509_bio(out, x);
904 else if (outformat == FORMAT_PEM) {
906 i = PEM_write_bio_X509_AUX(out, x);
908 i = PEM_write_bio_X509(out, x);
909 } else if (outformat == FORMAT_NETSCAPE) {
911 ASN1_OCTET_STRING hdr;
913 hdr.data = (unsigned char *)NETSCAPE_CERT_HDR;
914 hdr.length = strlen(NETSCAPE_CERT_HDR);
918 i = ASN1_item_i2d_bio(ASN1_ITEM_rptr(NETSCAPE_X509), out, &nx);
920 BIO_printf(bio_err, "bad output format specified for outfile\n");
924 BIO_printf(bio_err, "unable to write certificate\n");
925 ERR_print_errors(bio_err);
931 app_RAND_write_file(NULL);
935 X509_STORE_free(ctx);
939 EVP_PKEY_free(Upkey);
940 EVP_PKEY_free(CApkey);
942 sk_OPENSSL_STRING_free(sigopts);
944 ASN1_INTEGER_free(sno);
945 sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
946 sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
947 OPENSSL_free(passin);
951 static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile,
954 char *buf = NULL, *p;
955 ASN1_INTEGER *bs = NULL;
956 BIGNUM *serial = NULL;
959 len = ((serialfile == NULL)
960 ? (strlen(CAfile) + strlen(POSTFIX) + 1)
961 : (strlen(serialfile))) + 1;
962 buf = app_malloc(len, "serial# buffer");
963 if (serialfile == NULL) {
964 BUF_strlcpy(buf, CAfile, len);
965 for (p = buf; *p; p++)
970 BUF_strlcat(buf, POSTFIX, len);
972 BUF_strlcpy(buf, serialfile, len);
974 serial = load_serial(buf, create, NULL);
978 if (!BN_add_word(serial, 1)) {
979 BIO_printf(bio_err, "add_word failure\n");
983 if (!save_serial(buf, NULL, serial, &bs))
992 static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
993 X509 *x, X509 *xca, EVP_PKEY *pkey,
994 STACK_OF(OPENSSL_STRING) *sigopts,
995 char *serialfile, int create,
996 int days, int clrext, CONF *conf, char *section,
997 ASN1_INTEGER *sno, int reqfile)
1000 ASN1_INTEGER *bs = NULL;
1004 upkey = X509_get_pubkey(xca);
1005 EVP_PKEY_copy_parameters(upkey, pkey);
1006 EVP_PKEY_free(upkey);
1008 if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) {
1009 BIO_printf(bio_err, "Error initialising X509 store\n");
1014 else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)
1018 * NOTE: this certificate can/should be self signed, unless it was a
1019 * certificate request in which case it is not.
1021 X509_STORE_CTX_set_cert(&xsc, x);
1022 X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
1023 if (!reqfile && X509_verify_cert(&xsc) <= 0)
1026 if (!X509_check_private_key(xca, pkey)) {
1028 "CA certificate and CA private key do not match\n");
1032 if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
1034 if (!X509_set_serialNumber(x, bs))
1037 if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL)
1040 /* hardwired expired */
1041 if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL)
1045 while (X509_get_ext_count(x) > 0)
1046 X509_delete_ext(x, 0);
1051 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1052 X509_set_version(x, force_version);
1054 X509_set_version(x, 2); /* version 3 certificate */
1056 X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
1057 X509V3_set_nconf(&ctx2, conf);
1058 if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
1062 if (!do_X509_sign(x, pkey, digest, sigopts))
1066 X509_STORE_CTX_cleanup(&xsc);
1068 ERR_print_errors(bio_err);
1070 ASN1_INTEGER_free(bs);
1074 static int callb(int ok, X509_STORE_CTX *ctx)
1080 * it is ok to use a self signed certificate This case will catch both
1081 * the initial ok == 0 and the final ok == 1 calls to this function
1083 err = X509_STORE_CTX_get_error(ctx);
1084 if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1088 * BAD we should have gotten an error. Normally if everything worked
1089 * X509_STORE_CTX_get_error(ctx) will still be set to
1090 * DEPTH_ZERO_SELF_....
1094 "error with certificate to be certified - should be self signed\n");
1097 err_cert = X509_STORE_CTX_get_current_cert(ctx);
1098 print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
1100 "error with certificate - error %d at depth %d\n%s\n", err,
1101 X509_STORE_CTX_get_error_depth(ctx),
1102 X509_verify_cert_error_string(err));
1108 static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext,
1109 const EVP_MD *digest, CONF *conf, char *section)
1112 if (!X509_set_issuer_name(x, X509_get_subject_name(x)))
1114 if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL)
1117 if (X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days) ==
1121 if (!X509_set_pubkey(x, pkey))
1124 while (X509_get_ext_count(x) > 0)
1125 X509_delete_ext(x, 0);
1129 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1130 X509_set_version(x, force_version);
1132 X509_set_version(x, 2); /* version 3 certificate */
1134 X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
1135 X509V3_set_nconf(&ctx, conf);
1136 if (!X509V3_EXT_add_nconf(conf, &ctx, section, x))
1139 if (!X509_sign(x, pkey, digest))
1143 ERR_print_errors(bio_err);
1147 static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
1151 id = X509_PURPOSE_get_id(pt);
1152 pname = X509_PURPOSE_get0_name(pt);
1153 for (i = 0; i < 2; i++) {
1154 idret = X509_check_purpose(cert, id, i);
1155 BIO_printf(bio, "%s%s : ", pname, i ? " CA" : "");
1157 BIO_printf(bio, "Yes\n");
1158 else if (idret == 0)
1159 BIO_printf(bio, "No\n");
1161 BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);