2 * Copyright 1995-2016 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
);
37 static int x509_certify(X509_STORE
*ctx
, const char *CAfile
, const EVP_MD
*digest
,
38 X509
*x
, X509
*xca
, EVP_PKEY
*pkey
,
39 STACK_OF(OPENSSL_STRING
) *sigopts
, const char *serialfile
,
40 int create
, int days
, int clrext
, CONF
*conf
,
41 const char *section
, ASN1_INTEGER
*sno
, int reqfile
);
42 static int purpose_print(BIO
*bio
, X509
*cert
, X509_PURPOSE
*pt
);
44 typedef enum OPTION_choice
{
45 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
46 OPT_INFORM
, OPT_OUTFORM
, OPT_KEYFORM
, OPT_REQ
, OPT_CAFORM
,
47 OPT_CAKEYFORM
, OPT_SIGOPT
, OPT_DAYS
, OPT_PASSIN
, OPT_EXTFILE
,
48 OPT_EXTENSIONS
, OPT_IN
, OPT_OUT
, OPT_SIGNKEY
, OPT_CA
,
49 OPT_CAKEY
, OPT_CASERIAL
, OPT_SET_SERIAL
, OPT_FORCE_PUBKEY
,
50 OPT_ADDTRUST
, OPT_ADDREJECT
, OPT_SETALIAS
, OPT_CERTOPT
, OPT_NAMEOPT
,
51 OPT_C
, OPT_EMAIL
, OPT_OCSP_URI
, OPT_SERIAL
, OPT_NEXT_SERIAL
,
52 OPT_MODULUS
, OPT_PUBKEY
, OPT_X509TOREQ
, OPT_TEXT
, OPT_HASH
,
53 OPT_ISSUER_HASH
, OPT_SUBJECT
, OPT_ISSUER
, OPT_FINGERPRINT
, OPT_DATES
,
54 OPT_PURPOSE
, OPT_STARTDATE
, OPT_ENDDATE
, OPT_CHECKEND
, OPT_CHECKHOST
,
55 OPT_CHECKEMAIL
, OPT_CHECKIP
, OPT_NOOUT
, OPT_TRUSTOUT
, OPT_CLRTRUST
,
56 OPT_CLRREJECT
, OPT_ALIAS
, OPT_CACREATESERIAL
, OPT_CLREXT
, OPT_OCSPID
,
59 OPT_BADSIG
, OPT_MD
, OPT_ENGINE
, OPT_NOCERT
62 const OPTIONS x509_options
[] = {
63 {"help", OPT_HELP
, '-', "Display this summary"},
64 {"inform", OPT_INFORM
, 'f',
65 "Input format - default PEM (one of DER, NET or PEM)"},
66 {"in", OPT_IN
, '<', "Input file - default stdin"},
67 {"outform", OPT_OUTFORM
, 'f',
68 "Output format - default PEM (one of DER, NET or PEM)"},
69 {"out", OPT_OUT
, '>', "Output file - default stdout"},
70 {"keyform", OPT_KEYFORM
, 'F', "Private key format - default PEM"},
71 {"passin", OPT_PASSIN
, 's', "Private key password/pass-phrase source"},
72 {"serial", OPT_SERIAL
, '-', "Print serial number value"},
73 {"subject_hash", OPT_HASH
, '-', "Print subject hash value"},
74 {"issuer_hash", OPT_ISSUER_HASH
, '-', "Print issuer hash value"},
75 {"hash", OPT_HASH
, '-', "Synonym for -subject_hash"},
76 {"subject", OPT_SUBJECT
, '-', "Print subject DN"},
77 {"issuer", OPT_ISSUER
, '-', "Print issuer DN"},
78 {"email", OPT_EMAIL
, '-', "Print email address(es)"},
79 {"startdate", OPT_STARTDATE
, '-', "Set notBefore field"},
80 {"enddate", OPT_ENDDATE
, '-', "Set notAfter field"},
81 {"purpose", OPT_PURPOSE
, '-', "Print out certificate purposes"},
82 {"dates", OPT_DATES
, '-', "Both Before and After dates"},
83 {"modulus", OPT_MODULUS
, '-', "Print the RSA key modulus"},
84 {"pubkey", OPT_PUBKEY
, '-', "Output the public key"},
85 {"fingerprint", OPT_FINGERPRINT
, '-',
86 "Print the certificate fingerprint"},
87 {"alias", OPT_ALIAS
, '-', "Output certificate alias"},
88 {"noout", OPT_NOOUT
, '-', "No output, just status"},
89 {"nocert", OPT_NOCERT
, '-', "No certificate output"},
90 {"ocspid", OPT_OCSPID
, '-',
91 "Print OCSP hash values for the subject name and public key"},
92 {"ocsp_uri", OPT_OCSP_URI
, '-', "Print OCSP Responder URL(s)"},
93 {"trustout", OPT_TRUSTOUT
, '-', "Output a trusted certificate"},
94 {"clrtrust", OPT_CLRTRUST
, '-', "Clear all trusted purposes"},
95 {"clrext", OPT_CLREXT
, '-', "Clear all certificate extensions"},
96 {"addtrust", OPT_ADDTRUST
, 's', "Trust certificate for a given purpose"},
97 {"addreject", OPT_ADDREJECT
, 's',
98 "Reject certificate for a given purpose"},
99 {"setalias", OPT_SETALIAS
, 's', "Set certificate alias"},
100 {"days", OPT_DAYS
, 'n',
101 "How long till expiry of a signed certificate - def 30 days"},
102 {"checkend", OPT_CHECKEND
, 'M',
103 "Check whether the cert expires in the next arg seconds"},
104 {OPT_MORE_STR
, 1, 1, "Exit 1 if so, 0 if not"},
105 {"signkey", OPT_SIGNKEY
, '<', "Self sign cert with arg"},
106 {"x509toreq", OPT_X509TOREQ
, '-',
107 "Output a certification request object"},
108 {"req", OPT_REQ
, '-', "Input is a certificate request, sign and output"},
109 {"CA", OPT_CA
, '<', "Set the CA certificate, must be PEM format"},
110 {"CAkey", OPT_CAKEY
, 's',
111 "The CA key, must be PEM format; if not in CAfile"},
112 {"CAcreateserial", OPT_CACREATESERIAL
, '-',
113 "Create serial number file if it does not exist"},
114 {"CAserial", OPT_CASERIAL
, 's', "Serial file"},
115 {"set_serial", OPT_SET_SERIAL
, 's', "Serial number to use"},
116 {"text", OPT_TEXT
, '-', "Print the certificate in text form"},
117 {"C", OPT_C
, '-', "Print out C code forms"},
118 {"extfile", OPT_EXTFILE
, '<', "File with X509V3 extensions to add"},
119 {"extensions", OPT_EXTENSIONS
, 's', "Section from config file to use"},
120 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
121 {"certopt", OPT_CERTOPT
, 's', "Various certificate text options"},
122 {"checkhost", OPT_CHECKHOST
, 's', "Check certificate matches host"},
123 {"checkemail", OPT_CHECKEMAIL
, 's', "Check certificate matches email"},
124 {"checkip", OPT_CHECKIP
, 's', "Check certificate matches ipaddr"},
125 {"CAform", OPT_CAFORM
, 'F', "CA format - default PEM"},
126 {"CAkeyform", OPT_CAKEYFORM
, 'F', "CA key format - default PEM"},
127 {"sigopt", OPT_SIGOPT
, 's', "Signature parameter in n:v form"},
128 {"force_pubkey", OPT_FORCE_PUBKEY
, '<', "Force the Key to put inside certificate"},
129 {"next_serial", OPT_NEXT_SERIAL
, '-', "Increment current certificate serial number"},
130 {"clrreject", OPT_CLRREJECT
, '-',
131 "Clears all the prohibited or rejected uses of the certificate"},
132 {"badsig", OPT_BADSIG
, '-', "Corrupt last byte of certificate signature (for test)"},
133 {"", OPT_MD
, '-', "Any supported digest"},
134 #ifndef OPENSSL_NO_MD5
135 {"subject_hash_old", OPT_SUBJECT_HASH_OLD
, '-',
136 "Print old-style (MD5) issuer hash value"},
137 {"issuer_hash_old", OPT_ISSUER_HASH_OLD
, '-',
138 "Print old-style (MD5) subject hash value"},
140 #ifndef OPENSSL_NO_ENGINE
141 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
146 int x509_main(int argc
, char **argv
)
148 ASN1_INTEGER
*sno
= NULL
;
149 ASN1_OBJECT
*objtmp
= NULL
;
151 CONF
*extconf
= NULL
;
152 EVP_PKEY
*Upkey
= NULL
, *CApkey
= NULL
, *fkey
= NULL
;
153 STACK_OF(ASN1_OBJECT
) *trust
= NULL
, *reject
= NULL
;
154 STACK_OF(OPENSSL_STRING
) *sigopts
= NULL
;
155 X509
*x
= NULL
, *xca
= NULL
;
156 X509_REQ
*req
= NULL
, *rq
= NULL
;
157 X509_STORE
*ctx
= NULL
;
158 const EVP_MD
*digest
= NULL
;
159 char *CAkeyfile
= NULL
, *CAserial
= NULL
, *fkeyfile
= NULL
, *alias
= NULL
;
160 char *checkhost
= NULL
, *checkemail
= NULL
, *checkip
= NULL
;
161 char *extsect
= NULL
, *extfile
= NULL
, *passin
= NULL
, *passinarg
= NULL
;
162 char *infile
= NULL
, *outfile
= NULL
, *keyfile
= NULL
, *CAfile
= NULL
;
163 char buf
[256], *prog
;
164 int x509req
= 0, days
= DEF_DAYS
, modulus
= 0, pubkey
= 0, pprint
= 0;
165 int C
= 0, CAformat
= FORMAT_PEM
, CAkeyformat
= FORMAT_PEM
;
166 int fingerprint
= 0, reqfile
= 0, need_rand
= 0, checkend
= 0;
167 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, keyformat
= FORMAT_PEM
;
168 int next_serial
= 0, subject_hash
= 0, issuer_hash
= 0, ocspid
= 0;
169 int noout
= 0, sign_flag
= 0, CA_flag
= 0, CA_createserial
= 0, email
= 0;
170 int ocsp_uri
= 0, trustout
= 0, clrtrust
= 0, clrreject
= 0, aliasout
= 0;
171 int ret
= 1, i
, num
= 0, badsig
= 0, clrext
= 0, nocert
= 0;
172 int text
= 0, serial
= 0, subject
= 0, issuer
= 0, startdate
= 0;
174 time_t checkoffset
= 0;
175 unsigned long nmflag
= 0, certflag
= 0;
179 #ifndef OPENSSL_NO_MD5
180 int subject_hash_old
= 0, issuer_hash_old
= 0;
183 ctx
= X509_STORE_new();
186 X509_STORE_set_verify_cb(ctx
, callb
);
188 prog
= opt_init(argc
, argv
, x509_options
);
189 while ((o
= opt_next()) != OPT_EOF
) {
194 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
197 opt_help(x509_options
);
201 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &informat
))
208 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &outformat
))
212 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &keyformat
))
216 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &CAformat
))
220 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &CAkeyformat
))
227 reqfile
= need_rand
= 1;
232 sigopts
= sk_OPENSSL_STRING_new_null();
233 if (!sigopts
|| !sk_OPENSSL_STRING_push(sigopts
, opt_arg()))
237 days
= atoi(opt_arg());
240 passinarg
= opt_arg();
259 CAkeyfile
= opt_arg();
262 CAserial
= opt_arg();
266 BIO_printf(bio_err
, "Serial number supplied twice\n");
269 if ((sno
= s2i_ASN1_INTEGER(NULL
, opt_arg())) == NULL
)
272 case OPT_FORCE_PUBKEY
:
273 fkeyfile
= opt_arg();
276 if ((objtmp
= OBJ_txt2obj(opt_arg(), 0)) == NULL
) {
278 "%s: Invalid trust object value %s\n",
282 if (trust
== NULL
&& (trust
= sk_ASN1_OBJECT_new_null()) == NULL
)
284 sk_ASN1_OBJECT_push(trust
, objtmp
);
289 if ((objtmp
= OBJ_txt2obj(opt_arg(), 0)) == NULL
) {
291 "%s: Invalid reject object value %s\n",
296 && (reject
= sk_ASN1_OBJECT_new_null()) == NULL
)
298 sk_ASN1_OBJECT_push(reject
, objtmp
);
307 if (!set_cert_ex(&certflag
, opt_arg()))
312 if (!set_name_ex(&nmflag
, opt_arg()))
316 e
= setup_engine(opt_arg(), 0);
330 case OPT_NEXT_SERIAL
:
351 case OPT_FINGERPRINT
:
355 subject_hash
= ++num
;
357 case OPT_ISSUER_HASH
:
387 case OPT_CACREATESERIAL
:
388 CA_createserial
= ++num
;
399 #ifndef OPENSSL_NO_MD5
400 case OPT_SUBJECT_HASH_OLD
:
401 subject_hash_old
= ++num
;
403 case OPT_ISSUER_HASH_OLD
:
404 issuer_hash_old
= ++num
;
407 case OPT_SUBJECT_HASH_OLD
:
408 case OPT_ISSUER_HASH_OLD
:
419 if (!opt_imax(opt_arg(), &temp
))
421 checkoffset
= (time_t)temp
;
422 if ((intmax_t)checkoffset
!= temp
) {
423 BIO_printf(bio_err
, "%s: checkend time out of range %s\n",
430 checkhost
= opt_arg();
433 checkemail
= opt_arg();
439 if (!opt_md(opt_unknown(), &digest
))
443 argc
= opt_num_rest();
446 BIO_printf(bio_err
, "%s: Unknown parameter %s\n", prog
, argv
[0]);
451 nmflag
= XN_FLAG_ONELINE
;
453 out
= bio_open_default(outfile
, 'w', outformat
);
458 app_RAND_load_file(NULL
, 0);
460 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
461 BIO_printf(bio_err
, "Error getting password\n");
465 if (!X509_STORE_set_default_paths(ctx
)) {
466 ERR_print_errors(bio_err
);
471 fkey
= load_pubkey(fkeyfile
, keyformat
, 0, NULL
, e
, "Forced key");
476 if ((CAkeyfile
== NULL
) && (CA_flag
) && (CAformat
== FORMAT_PEM
)) {
478 } else if ((CA_flag
) && (CAkeyfile
== NULL
)) {
480 "need to specify a CAkey if using the CA command\n");
486 if ((extconf
= app_load_config(extfile
)) == NULL
)
489 extsect
= NCONF_get_string(extconf
, "default", "extensions");
495 X509V3_set_ctx_test(&ctx2
);
496 X509V3_set_nconf(&ctx2
, extconf
);
497 if (!X509V3_EXT_add_nconf(extconf
, &ctx2
, extsect
, NULL
)) {
499 "Error Loading extension section %s\n", extsect
);
500 ERR_print_errors(bio_err
);
509 if (!sign_flag
&& !CA_flag
) {
510 BIO_printf(bio_err
, "We need a private key to sign with\n");
513 in
= bio_open_default(infile
, 'r', informat
);
516 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
520 ERR_print_errors(bio_err
);
524 if ((pkey
= X509_REQ_get0_pubkey(req
)) == NULL
) {
525 BIO_printf(bio_err
, "error unpacking public key\n");
528 i
= X509_REQ_verify(req
, pkey
);
530 BIO_printf(bio_err
, "Signature verification error\n");
531 ERR_print_errors(bio_err
);
536 "Signature did not match the certificate request\n");
539 BIO_printf(bio_err
, "Signature ok\n");
541 print_name(bio_err
, "subject=", X509_REQ_get_subject_name(req
),
544 if ((x
= X509_new()) == NULL
)
548 sno
= ASN1_INTEGER_new();
549 if (sno
== NULL
|| !rand_serial(NULL
, sno
))
551 if (!X509_set_serialNumber(x
, sno
))
553 ASN1_INTEGER_free(sno
);
555 } else if (!X509_set_serialNumber(x
, sno
))
558 if (!X509_set_issuer_name(x
, X509_REQ_get_subject_name(req
)))
560 if (!X509_set_subject_name(x
, X509_REQ_get_subject_name(req
)))
562 if (!set_cert_times(x
, NULL
, NULL
, days
))
566 X509_set_pubkey(x
, fkey
);
568 pkey
= X509_REQ_get0_pubkey(req
);
569 X509_set_pubkey(x
, pkey
);
572 x
= load_cert(infile
, informat
, "Certificate");
577 xca
= load_cert(CAfile
, CAformat
, "CA Certificate");
582 if (!noout
|| text
|| next_serial
) {
583 OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
588 X509_alias_set1(x
, (unsigned char *)alias
, -1);
593 X509_reject_clear(x
);
596 for (i
= 0; i
< sk_ASN1_OBJECT_num(trust
); i
++) {
597 objtmp
= sk_ASN1_OBJECT_value(trust
, i
);
598 X509_add1_trust_object(x
, objtmp
);
604 for (i
= 0; i
< sk_ASN1_OBJECT_num(reject
); i
++) {
605 objtmp
= sk_ASN1_OBJECT_value(reject
, i
);
606 X509_add1_reject_object(x
, objtmp
);
612 const ASN1_BIT_STRING
*signature
;
614 X509_get0_signature(&signature
, NULL
, x
);
615 corrupt_signature(signature
);
619 for (i
= 1; i
<= num
; i
++) {
621 print_name(out
, "issuer=", X509_get_issuer_name(x
), nmflag
);
622 } else if (subject
== i
) {
623 print_name(out
, "subject=",
624 X509_get_subject_name(x
), nmflag
);
625 } else if (serial
== i
) {
626 BIO_printf(out
, "serial=");
627 i2a_ASN1_INTEGER(out
, X509_get_serialNumber(x
));
628 BIO_printf(out
, "\n");
629 } else if (next_serial
== i
) {
630 ASN1_INTEGER
*ser
= X509_get_serialNumber(x
);
631 BIGNUM
*bnser
= ASN1_INTEGER_to_BN(ser
, NULL
);
635 if (!BN_add_word(bnser
, 1))
637 ser
= BN_to_ASN1_INTEGER(bnser
, NULL
);
641 i2a_ASN1_INTEGER(out
, ser
);
642 ASN1_INTEGER_free(ser
);
644 } else if ((email
== i
) || (ocsp_uri
== i
)) {
646 STACK_OF(OPENSSL_STRING
) *emlst
;
648 emlst
= X509_get1_email(x
);
650 emlst
= X509_get1_ocsp(x
);
651 for (j
= 0; j
< sk_OPENSSL_STRING_num(emlst
); j
++)
652 BIO_printf(out
, "%s\n",
653 sk_OPENSSL_STRING_value(emlst
, j
));
654 X509_email_free(emlst
);
655 } else if (aliasout
== i
) {
656 unsigned char *alstr
;
657 alstr
= X509_alias_get0(x
, NULL
);
659 BIO_printf(out
, "%s\n", alstr
);
661 BIO_puts(out
, "<No Alias>\n");
662 } else if (subject_hash
== i
) {
663 BIO_printf(out
, "%08lx\n", X509_subject_name_hash(x
));
665 #ifndef OPENSSL_NO_MD5
666 else if (subject_hash_old
== i
) {
667 BIO_printf(out
, "%08lx\n", X509_subject_name_hash_old(x
));
670 else if (issuer_hash
== i
) {
671 BIO_printf(out
, "%08lx\n", X509_issuer_name_hash(x
));
673 #ifndef OPENSSL_NO_MD5
674 else if (issuer_hash_old
== i
) {
675 BIO_printf(out
, "%08lx\n", X509_issuer_name_hash_old(x
));
678 else if (pprint
== i
) {
681 BIO_printf(out
, "Certificate purposes:\n");
682 for (j
= 0; j
< X509_PURPOSE_get_count(); j
++) {
683 ptmp
= X509_PURPOSE_get0(j
);
684 purpose_print(out
, x
, ptmp
);
686 } else if (modulus
== i
) {
689 pkey
= X509_get0_pubkey(x
);
691 BIO_printf(bio_err
, "Modulus=unavailable\n");
692 ERR_print_errors(bio_err
);
695 BIO_printf(out
, "Modulus=");
696 #ifndef OPENSSL_NO_RSA
697 if (EVP_PKEY_id(pkey
) == EVP_PKEY_RSA
) {
699 RSA_get0_key(EVP_PKEY_get0_RSA(pkey
), &n
, NULL
, NULL
);
703 #ifndef OPENSSL_NO_DSA
704 if (EVP_PKEY_id(pkey
) == EVP_PKEY_DSA
) {
705 const BIGNUM
*dsapub
= NULL
;
706 DSA_get0_key(EVP_PKEY_get0_DSA(pkey
), &dsapub
, NULL
);
707 BN_print(out
, dsapub
);
711 BIO_printf(out
, "Wrong Algorithm type");
713 BIO_printf(out
, "\n");
714 } else if (pubkey
== i
) {
717 pkey
= X509_get0_pubkey(x
);
719 BIO_printf(bio_err
, "Error getting public key\n");
720 ERR_print_errors(bio_err
);
723 PEM_write_bio_PUBKEY(out
, pkey
);
729 X509_NAME_oneline(X509_get_subject_name(x
), buf
, sizeof buf
);
730 BIO_printf(out
, "/*\n"
731 " * Subject: %s\n", buf
);
733 X509_NAME_oneline(X509_get_issuer_name(x
), buf
, sizeof buf
);
734 BIO_printf(out
, " * Issuer: %s\n"
737 len
= i2d_X509(x
, NULL
);
738 m
= app_malloc(len
, "x509 name buffer");
739 d
= (unsigned char *)m
;
740 len
= i2d_X509_NAME(X509_get_subject_name(x
), &d
);
741 print_array(out
, "the_subject_name", len
, (unsigned char *)m
);
742 d
= (unsigned char *)m
;
743 len
= i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x
), &d
);
744 print_array(out
, "the_public_key", len
, (unsigned char *)m
);
745 d
= (unsigned char *)m
;
746 len
= i2d_X509(x
, &d
);
747 print_array(out
, "the_certificate", len
, (unsigned char *)m
);
749 } else if (text
== i
) {
750 X509_print_ex(out
, x
, nmflag
, certflag
);
751 } else if (startdate
== i
) {
752 BIO_puts(out
, "notBefore=");
753 ASN1_TIME_print(out
, X509_get0_notBefore(x
));
755 } else if (enddate
== i
) {
756 BIO_puts(out
, "notAfter=");
757 ASN1_TIME_print(out
, X509_get0_notAfter(x
));
759 } else if (fingerprint
== i
) {
762 unsigned char md
[EVP_MAX_MD_SIZE
];
763 const EVP_MD
*fdig
= digest
;
768 if (!X509_digest(x
, fdig
, md
, &n
)) {
769 BIO_printf(bio_err
, "out of memory\n");
772 BIO_printf(out
, "%s Fingerprint=",
773 OBJ_nid2sn(EVP_MD_type(fdig
)));
774 for (j
= 0; j
< (int)n
; j
++) {
775 BIO_printf(out
, "%02X%c", md
[j
], (j
+ 1 == (int)n
)
780 /* should be in the library */
781 else if ((sign_flag
== i
) && (x509req
== 0)) {
782 BIO_printf(bio_err
, "Getting Private key\n");
784 Upkey
= load_key(keyfile
, keyformat
, 0,
785 passin
, e
, "Private key");
791 if (!sign(x
, Upkey
, days
, clrext
, digest
, extconf
, extsect
))
793 } else if (CA_flag
== i
) {
794 BIO_printf(bio_err
, "Getting CA Private Key\n");
795 if (CAkeyfile
!= NULL
) {
796 CApkey
= load_key(CAkeyfile
, CAkeyformat
,
797 0, passin
, e
, "CA Private Key");
803 if (!x509_certify(ctx
, CAfile
, digest
, x
, xca
,
805 CAserial
, CA_createserial
, days
, clrext
,
806 extconf
, extsect
, sno
, reqfile
))
808 } else if (x509req
== i
) {
811 BIO_printf(bio_err
, "Getting request Private Key\n");
812 if (keyfile
== NULL
) {
813 BIO_printf(bio_err
, "no request key file specified\n");
816 pk
= load_key(keyfile
, keyformat
, 0,
817 passin
, e
, "request key");
822 BIO_printf(bio_err
, "Generating certificate request\n");
824 rq
= X509_to_X509_REQ(x
, pk
, digest
);
827 ERR_print_errors(bio_err
);
831 X509_REQ_print(out
, rq
);
832 PEM_write_bio_X509_REQ(out
, rq
);
835 } else if (ocspid
== i
) {
836 X509_ocspid_print(out
, x
);
842 time_t tcheck
= time(NULL
) + checkoffset
;
844 if (X509_cmp_time(X509_get0_notAfter(x
), &tcheck
) < 0) {
845 BIO_printf(out
, "Certificate will expire\n");
848 BIO_printf(out
, "Certificate will not expire\n");
854 print_cert_checks(out
, x
, checkhost
, checkemail
, checkip
);
856 if (noout
|| nocert
) {
861 if (outformat
== FORMAT_ASN1
)
862 i
= i2d_X509_bio(out
, x
);
863 else if (outformat
== FORMAT_PEM
) {
865 i
= PEM_write_bio_X509_AUX(out
, x
);
867 i
= PEM_write_bio_X509(out
, x
);
869 BIO_printf(bio_err
, "bad output format specified for outfile\n");
873 BIO_printf(bio_err
, "unable to write certificate\n");
874 ERR_print_errors(bio_err
);
880 app_RAND_write_file(NULL
);
883 X509_STORE_free(ctx
);
887 EVP_PKEY_free(Upkey
);
888 EVP_PKEY_free(CApkey
);
890 sk_OPENSSL_STRING_free(sigopts
);
892 ASN1_INTEGER_free(sno
);
893 sk_ASN1_OBJECT_pop_free(trust
, ASN1_OBJECT_free
);
894 sk_ASN1_OBJECT_pop_free(reject
, ASN1_OBJECT_free
);
895 ASN1_OBJECT_free(objtmp
);
897 OPENSSL_free(passin
);
901 static ASN1_INTEGER
*x509_load_serial(const char *CAfile
, const char *serialfile
,
904 char *buf
= NULL
, *p
;
905 ASN1_INTEGER
*bs
= NULL
;
906 BIGNUM
*serial
= NULL
;
909 len
= ((serialfile
== NULL
)
910 ? (strlen(CAfile
) + strlen(POSTFIX
) + 1)
911 : (strlen(serialfile
))) + 1;
912 buf
= app_malloc(len
, "serial# buffer");
913 if (serialfile
== NULL
) {
914 OPENSSL_strlcpy(buf
, CAfile
, len
);
915 for (p
= buf
; *p
; p
++)
920 OPENSSL_strlcat(buf
, POSTFIX
, len
);
922 OPENSSL_strlcpy(buf
, serialfile
, len
);
924 serial
= load_serial(buf
, create
, NULL
);
928 if (!BN_add_word(serial
, 1)) {
929 BIO_printf(bio_err
, "add_word failure\n");
933 if (!save_serial(buf
, NULL
, serial
, &bs
))
942 static int x509_certify(X509_STORE
*ctx
, const char *CAfile
, const EVP_MD
*digest
,
943 X509
*x
, X509
*xca
, EVP_PKEY
*pkey
,
944 STACK_OF(OPENSSL_STRING
) *sigopts
,
945 const char *serialfile
, int create
,
946 int days
, int clrext
, CONF
*conf
, const char *section
,
947 ASN1_INTEGER
*sno
, int reqfile
)
950 ASN1_INTEGER
*bs
= NULL
;
951 X509_STORE_CTX
*xsc
= NULL
;
954 upkey
= X509_get0_pubkey(xca
);
956 BIO_printf(bio_err
, "Error obtaining CA X509 public key\n");
959 EVP_PKEY_copy_parameters(upkey
, pkey
);
961 xsc
= X509_STORE_CTX_new();
962 if (xsc
== NULL
|| !X509_STORE_CTX_init(xsc
, ctx
, x
, NULL
)) {
963 BIO_printf(bio_err
, "Error initialising X509 store\n");
968 else if ((bs
= x509_load_serial(CAfile
, serialfile
, create
)) == NULL
)
972 * NOTE: this certificate can/should be self signed, unless it was a
973 * certificate request in which case it is not.
975 X509_STORE_CTX_set_cert(xsc
, x
);
976 X509_STORE_CTX_set_flags(xsc
, X509_V_FLAG_CHECK_SS_SIGNATURE
);
977 if (!reqfile
&& X509_verify_cert(xsc
) <= 0)
980 if (!X509_check_private_key(xca
, pkey
)) {
982 "CA certificate and CA private key do not match\n");
986 if (!X509_set_issuer_name(x
, X509_get_subject_name(xca
)))
988 if (!X509_set_serialNumber(x
, bs
))
991 if (!set_cert_times(x
, NULL
, NULL
, days
))
995 while (X509_get_ext_count(x
) > 0)
996 X509_delete_ext(x
, 0);
1001 X509_set_version(x
, 2); /* version 3 certificate */
1002 X509V3_set_ctx(&ctx2
, xca
, x
, NULL
, NULL
, 0);
1003 X509V3_set_nconf(&ctx2
, conf
);
1004 if (!X509V3_EXT_add_nconf(conf
, &ctx2
, section
, x
))
1008 if (!do_X509_sign(x
, pkey
, digest
, sigopts
))
1012 X509_STORE_CTX_free(xsc
);
1014 ERR_print_errors(bio_err
);
1016 ASN1_INTEGER_free(bs
);
1020 static int callb(int ok
, X509_STORE_CTX
*ctx
)
1026 * it is ok to use a self signed certificate This case will catch both
1027 * the initial ok == 0 and the final ok == 1 calls to this function
1029 err
= X509_STORE_CTX_get_error(ctx
);
1030 if (err
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
)
1034 * BAD we should have gotten an error. Normally if everything worked
1035 * X509_STORE_CTX_get_error(ctx) will still be set to
1036 * DEPTH_ZERO_SELF_....
1040 "error with certificate to be certified - should be self signed\n");
1043 err_cert
= X509_STORE_CTX_get_current_cert(ctx
);
1044 print_name(bio_err
, NULL
, X509_get_subject_name(err_cert
), 0);
1046 "error with certificate - error %d at depth %d\n%s\n", err
,
1047 X509_STORE_CTX_get_error_depth(ctx
),
1048 X509_verify_cert_error_string(err
));
1054 static int sign(X509
*x
, EVP_PKEY
*pkey
, int days
, int clrext
,
1055 const EVP_MD
*digest
, CONF
*conf
, const char *section
)
1058 if (!X509_set_issuer_name(x
, X509_get_subject_name(x
)))
1060 if (!set_cert_times(x
, NULL
, NULL
, days
))
1062 if (!X509_set_pubkey(x
, pkey
))
1065 while (X509_get_ext_count(x
) > 0)
1066 X509_delete_ext(x
, 0);
1070 X509_set_version(x
, 2); /* version 3 certificate */
1071 X509V3_set_ctx(&ctx
, x
, x
, NULL
, NULL
, 0);
1072 X509V3_set_nconf(&ctx
, conf
);
1073 if (!X509V3_EXT_add_nconf(conf
, &ctx
, section
, x
))
1076 if (!X509_sign(x
, pkey
, digest
))
1080 ERR_print_errors(bio_err
);
1084 static int purpose_print(BIO
*bio
, X509
*cert
, X509_PURPOSE
*pt
)
1088 id
= X509_PURPOSE_get_id(pt
);
1089 pname
= X509_PURPOSE_get0_name(pt
);
1090 for (i
= 0; i
< 2; i
++) {
1091 idret
= X509_check_purpose(cert
, id
, i
);
1092 BIO_printf(bio
, "%s%s : ", pname
, i
? " CA" : "");
1094 BIO_printf(bio
, "Yes\n");
1095 else if (idret
== 0)
1096 BIO_printf(bio
, "No\n");
1098 BIO_printf(bio
, "Yes (WARNING code=%d)\n", idret
);