2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/bio.h>
15 #include <openssl/asn1.h>
16 #include <openssl/err.h>
17 #include <openssl/bn.h>
18 #include <openssl/evp.h>
19 #include <openssl/x509.h>
20 #include <openssl/x509v3.h>
21 #include <openssl/objects.h>
22 #include <openssl/pem.h>
23 #ifndef OPENSSL_NO_RSA
24 # include <openssl/rsa.h>
26 #ifndef OPENSSL_NO_DSA
27 # include <openssl/dsa.h>
31 #define POSTFIX ".srl"
34 static int callb(int ok
, X509_STORE_CTX
*ctx
);
35 static int sign(X509
*x
, EVP_PKEY
*pkey
, int days
, int clrext
,
36 const EVP_MD
*digest
, CONF
*conf
, const char *section
,
38 static int x509_certify(X509_STORE
*ctx
, const char *CAfile
, const EVP_MD
*digest
,
39 X509
*x
, X509
*xca
, EVP_PKEY
*pkey
,
40 STACK_OF(OPENSSL_STRING
) *sigopts
, const char *serialfile
,
41 int create
, int days
, int clrext
, CONF
*conf
,
42 const char *section
, ASN1_INTEGER
*sno
, int reqfile
,
44 static int purpose_print(BIO
*bio
, X509
*cert
, X509_PURPOSE
*pt
);
46 typedef enum OPTION_choice
{
47 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
48 OPT_INFORM
, OPT_OUTFORM
, OPT_KEYFORM
, OPT_REQ
, OPT_CAFORM
,
49 OPT_CAKEYFORM
, OPT_SIGOPT
, OPT_DAYS
, OPT_PASSIN
, OPT_EXTFILE
,
50 OPT_EXTENSIONS
, OPT_IN
, OPT_OUT
, OPT_SIGNKEY
, OPT_CA
,
51 OPT_CAKEY
, OPT_CASERIAL
, OPT_SET_SERIAL
, OPT_FORCE_PUBKEY
,
52 OPT_ADDTRUST
, OPT_ADDREJECT
, OPT_SETALIAS
, OPT_CERTOPT
, OPT_NAMEOPT
,
53 OPT_C
, OPT_EMAIL
, OPT_OCSP_URI
, OPT_SERIAL
, OPT_NEXT_SERIAL
,
54 OPT_MODULUS
, OPT_PUBKEY
, OPT_X509TOREQ
, OPT_TEXT
, OPT_HASH
,
55 OPT_ISSUER_HASH
, OPT_SUBJECT
, OPT_ISSUER
, OPT_FINGERPRINT
, OPT_DATES
,
56 OPT_PURPOSE
, OPT_STARTDATE
, OPT_ENDDATE
, OPT_CHECKEND
, OPT_CHECKHOST
,
57 OPT_CHECKEMAIL
, OPT_CHECKIP
, OPT_NOOUT
, OPT_TRUSTOUT
, OPT_CLRTRUST
,
58 OPT_CLRREJECT
, OPT_ALIAS
, OPT_CACREATESERIAL
, OPT_CLREXT
, OPT_OCSPID
,
61 OPT_BADSIG
, OPT_MD
, OPT_ENGINE
, OPT_NOCERT
, OPT_PRESERVE_DATES
,
65 const OPTIONS x509_options
[] = {
66 {"help", OPT_HELP
, '-', "Display this summary"},
67 {"inform", OPT_INFORM
, 'f',
68 "Input format - default PEM (one of DER, NET or PEM)"},
69 {"in", OPT_IN
, '<', "Input file - default stdin"},
70 {"outform", OPT_OUTFORM
, 'f',
71 "Output format - default PEM (one of DER, NET or PEM)"},
72 {"out", OPT_OUT
, '>', "Output file - default stdout"},
73 {"keyform", OPT_KEYFORM
, 'F', "Private key format - default PEM"},
74 {"passin", OPT_PASSIN
, 's', "Private key password/pass-phrase source"},
75 {"serial", OPT_SERIAL
, '-', "Print serial number value"},
76 {"subject_hash", OPT_HASH
, '-', "Print subject hash value"},
77 {"issuer_hash", OPT_ISSUER_HASH
, '-', "Print issuer hash value"},
78 {"hash", OPT_HASH
, '-', "Synonym for -subject_hash"},
79 {"subject", OPT_SUBJECT
, '-', "Print subject DN"},
80 {"issuer", OPT_ISSUER
, '-', "Print issuer DN"},
81 {"email", OPT_EMAIL
, '-', "Print email address(es)"},
82 {"startdate", OPT_STARTDATE
, '-', "Set notBefore field"},
83 {"enddate", OPT_ENDDATE
, '-', "Set notAfter field"},
84 {"purpose", OPT_PURPOSE
, '-', "Print out certificate purposes"},
85 {"dates", OPT_DATES
, '-', "Both Before and After dates"},
86 {"modulus", OPT_MODULUS
, '-', "Print the RSA key modulus"},
87 {"pubkey", OPT_PUBKEY
, '-', "Output the public key"},
88 {"fingerprint", OPT_FINGERPRINT
, '-',
89 "Print the certificate fingerprint"},
90 {"alias", OPT_ALIAS
, '-', "Output certificate alias"},
91 {"noout", OPT_NOOUT
, '-', "No output, just status"},
92 {"nocert", OPT_NOCERT
, '-', "No certificate output"},
93 {"ocspid", OPT_OCSPID
, '-',
94 "Print OCSP hash values for the subject name and public key"},
95 {"ocsp_uri", OPT_OCSP_URI
, '-', "Print OCSP Responder URL(s)"},
96 {"trustout", OPT_TRUSTOUT
, '-', "Output a trusted certificate"},
97 {"clrtrust", OPT_CLRTRUST
, '-', "Clear all trusted purposes"},
98 {"clrext", OPT_CLREXT
, '-', "Clear all certificate extensions"},
99 {"addtrust", OPT_ADDTRUST
, 's', "Trust certificate for a given purpose"},
100 {"addreject", OPT_ADDREJECT
, 's',
101 "Reject certificate for a given purpose"},
102 {"setalias", OPT_SETALIAS
, 's', "Set certificate alias"},
103 {"days", OPT_DAYS
, 'n',
104 "How long till expiry of a signed certificate - def 30 days"},
105 {"checkend", OPT_CHECKEND
, 'M',
106 "Check whether the cert expires in the next arg seconds"},
107 {OPT_MORE_STR
, 1, 1, "Exit 1 if so, 0 if not"},
108 {"signkey", OPT_SIGNKEY
, '<', "Self sign cert with arg"},
109 {"x509toreq", OPT_X509TOREQ
, '-',
110 "Output a certification request object"},
111 {"req", OPT_REQ
, '-', "Input is a certificate request, sign and output"},
112 {"CA", OPT_CA
, '<', "Set the CA certificate, must be PEM format"},
113 {"CAkey", OPT_CAKEY
, 's',
114 "The CA key, must be PEM format; if not in CAfile"},
115 {"CAcreateserial", OPT_CACREATESERIAL
, '-',
116 "Create serial number file if it does not exist"},
117 {"CAserial", OPT_CASERIAL
, 's', "Serial file"},
118 {"set_serial", OPT_SET_SERIAL
, 's', "Serial number to use"},
119 {"text", OPT_TEXT
, '-', "Print the certificate in text form"},
120 {"C", OPT_C
, '-', "Print out C code forms"},
121 {"extfile", OPT_EXTFILE
, '<', "File with X509V3 extensions to add"},
123 {"extensions", OPT_EXTENSIONS
, 's', "Section from config file to use"},
124 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
125 {"certopt", OPT_CERTOPT
, 's', "Various certificate text options"},
126 {"checkhost", OPT_CHECKHOST
, 's', "Check certificate matches host"},
127 {"checkemail", OPT_CHECKEMAIL
, 's', "Check certificate matches email"},
128 {"checkip", OPT_CHECKIP
, 's', "Check certificate matches ipaddr"},
129 {"CAform", OPT_CAFORM
, 'F', "CA format - default PEM"},
130 {"CAkeyform", OPT_CAKEYFORM
, 'F', "CA key format - default PEM"},
131 {"sigopt", OPT_SIGOPT
, 's', "Signature parameter in n:v form"},
132 {"force_pubkey", OPT_FORCE_PUBKEY
, '<', "Force the Key to put inside certificate"},
133 {"next_serial", OPT_NEXT_SERIAL
, '-', "Increment current certificate serial number"},
134 {"clrreject", OPT_CLRREJECT
, '-',
135 "Clears all the prohibited or rejected uses of the certificate"},
136 {"badsig", OPT_BADSIG
, '-', "Corrupt last byte of certificate signature (for test)"},
137 {"", OPT_MD
, '-', "Any supported digest"},
138 #ifndef OPENSSL_NO_MD5
139 {"subject_hash_old", OPT_SUBJECT_HASH_OLD
, '-',
140 "Print old-style (MD5) issuer hash value"},
141 {"issuer_hash_old", OPT_ISSUER_HASH_OLD
, '-',
142 "Print old-style (MD5) subject hash value"},
144 #ifndef OPENSSL_NO_ENGINE
145 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
147 {"preserve_dates", OPT_PRESERVE_DATES
, '-', "preserve existing dates when signing"},
151 int x509_main(int argc
, char **argv
)
153 ASN1_INTEGER
*sno
= NULL
;
154 ASN1_OBJECT
*objtmp
= NULL
;
156 CONF
*extconf
= NULL
;
157 EVP_PKEY
*Upkey
= NULL
, *CApkey
= NULL
, *fkey
= NULL
;
158 STACK_OF(ASN1_OBJECT
) *trust
= NULL
, *reject
= NULL
;
159 STACK_OF(OPENSSL_STRING
) *sigopts
= NULL
;
160 X509
*x
= NULL
, *xca
= NULL
;
161 X509_REQ
*req
= NULL
, *rq
= NULL
;
162 X509_STORE
*ctx
= NULL
;
163 const EVP_MD
*digest
= NULL
;
164 char *CAkeyfile
= NULL
, *CAserial
= NULL
, *fkeyfile
= NULL
, *alias
= NULL
;
165 char *checkhost
= NULL
, *checkemail
= NULL
, *checkip
= NULL
;
166 char *extsect
= NULL
, *extfile
= NULL
, *passin
= NULL
, *passinarg
= NULL
;
167 char *infile
= NULL
, *outfile
= NULL
, *keyfile
= NULL
, *CAfile
= NULL
;
169 int x509req
= 0, days
= DEF_DAYS
, modulus
= 0, pubkey
= 0, pprint
= 0;
170 int C
= 0, CAformat
= FORMAT_PEM
, CAkeyformat
= FORMAT_PEM
;
171 int fingerprint
= 0, reqfile
= 0, checkend
= 0;
172 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, keyformat
= FORMAT_PEM
;
173 int next_serial
= 0, subject_hash
= 0, issuer_hash
= 0, ocspid
= 0;
174 int noout
= 0, sign_flag
= 0, CA_flag
= 0, CA_createserial
= 0, email
= 0;
175 int ocsp_uri
= 0, trustout
= 0, clrtrust
= 0, clrreject
= 0, aliasout
= 0;
176 int ret
= 1, i
, num
= 0, badsig
= 0, clrext
= 0, nocert
= 0;
177 int text
= 0, serial
= 0, subject
= 0, issuer
= 0, startdate
= 0;
179 time_t checkoffset
= 0;
180 unsigned long certflag
= 0;
181 int preserve_dates
= 0;
184 #ifndef OPENSSL_NO_MD5
185 int subject_hash_old
= 0, issuer_hash_old
= 0;
188 ctx
= X509_STORE_new();
191 X509_STORE_set_verify_cb(ctx
, callb
);
193 prog
= opt_init(argc
, argv
, x509_options
);
194 while ((o
= opt_next()) != OPT_EOF
) {
199 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
202 opt_help(x509_options
);
206 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &informat
))
213 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &outformat
))
217 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &keyformat
))
221 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &CAformat
))
225 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &CAkeyformat
))
237 sigopts
= sk_OPENSSL_STRING_new_null();
238 if (!sigopts
|| !sk_OPENSSL_STRING_push(sigopts
, opt_arg()))
244 days
= atoi(opt_arg());
247 passinarg
= opt_arg();
268 CAkeyfile
= opt_arg();
271 CAserial
= opt_arg();
275 BIO_printf(bio_err
, "Serial number supplied twice\n");
278 if ((sno
= s2i_ASN1_INTEGER(NULL
, opt_arg())) == NULL
)
281 case OPT_FORCE_PUBKEY
:
282 fkeyfile
= opt_arg();
285 if ((objtmp
= OBJ_txt2obj(opt_arg(), 0)) == NULL
) {
287 "%s: Invalid trust object value %s\n",
291 if (trust
== NULL
&& (trust
= sk_ASN1_OBJECT_new_null()) == NULL
)
293 sk_ASN1_OBJECT_push(trust
, objtmp
);
298 if ((objtmp
= OBJ_txt2obj(opt_arg(), 0)) == NULL
) {
300 "%s: Invalid reject object value %s\n",
305 && (reject
= sk_ASN1_OBJECT_new_null()) == NULL
)
307 sk_ASN1_OBJECT_push(reject
, objtmp
);
316 if (!set_cert_ex(&certflag
, opt_arg()))
320 if (!set_nameopt(opt_arg()))
324 e
= setup_engine(opt_arg(), 0);
338 case OPT_NEXT_SERIAL
:
359 case OPT_FINGERPRINT
:
363 subject_hash
= ++num
;
365 case OPT_ISSUER_HASH
:
395 case OPT_CACREATESERIAL
:
396 CA_createserial
= ++num
;
407 #ifndef OPENSSL_NO_MD5
408 case OPT_SUBJECT_HASH_OLD
:
409 subject_hash_old
= ++num
;
411 case OPT_ISSUER_HASH_OLD
:
412 issuer_hash_old
= ++num
;
415 case OPT_SUBJECT_HASH_OLD
:
416 case OPT_ISSUER_HASH_OLD
:
427 if (!opt_imax(opt_arg(), &temp
))
429 checkoffset
= (time_t)temp
;
430 if ((intmax_t)checkoffset
!= temp
) {
431 BIO_printf(bio_err
, "%s: checkend time out of range %s\n",
438 checkhost
= opt_arg();
441 checkemail
= opt_arg();
446 case OPT_PRESERVE_DATES
:
447 if (days
!= DEF_DAYS
)
452 if (!opt_md(opt_unknown(), &digest
))
456 argc
= opt_num_rest();
459 BIO_printf(bio_err
, "%s: Unknown parameter %s\n", prog
, argv
[0]);
463 out
= bio_open_default(outfile
, 'w', outformat
);
467 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
468 BIO_printf(bio_err
, "Error getting password\n");
472 if (!X509_STORE_set_default_paths(ctx
)) {
473 ERR_print_errors(bio_err
);
477 if (fkeyfile
!= NULL
) {
478 fkey
= load_pubkey(fkeyfile
, keyformat
, 0, NULL
, e
, "Forced key");
483 if ((CAkeyfile
== NULL
) && (CA_flag
) && (CAformat
== FORMAT_PEM
)) {
485 } else if ((CA_flag
) && (CAkeyfile
== NULL
)) {
487 "need to specify a CAkey if using the CA command\n");
491 if (extfile
!= NULL
) {
493 if ((extconf
= app_load_config(extfile
)) == NULL
)
495 if (extsect
== NULL
) {
496 extsect
= NCONF_get_string(extconf
, "default", "extensions");
497 if (extsect
== NULL
) {
502 X509V3_set_ctx_test(&ctx2
);
503 X509V3_set_nconf(&ctx2
, extconf
);
504 if (!X509V3_EXT_add_nconf(extconf
, &ctx2
, extsect
, NULL
)) {
506 "Error Loading extension section %s\n", extsect
);
507 ERR_print_errors(bio_err
);
516 if (!sign_flag
&& !CA_flag
) {
517 BIO_printf(bio_err
, "We need a private key to sign with\n");
520 in
= bio_open_default(infile
, 'r', informat
);
523 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
527 ERR_print_errors(bio_err
);
531 if ((pkey
= X509_REQ_get0_pubkey(req
)) == NULL
) {
532 BIO_printf(bio_err
, "error unpacking public key\n");
535 i
= X509_REQ_verify(req
, pkey
);
537 BIO_printf(bio_err
, "Signature verification error\n");
538 ERR_print_errors(bio_err
);
543 "Signature did not match the certificate request\n");
546 BIO_printf(bio_err
, "Signature ok\n");
549 print_name(bio_err
, "subject=", X509_REQ_get_subject_name(req
),
552 if ((x
= X509_new()) == NULL
)
556 sno
= ASN1_INTEGER_new();
557 if (sno
== NULL
|| !rand_serial(NULL
, sno
))
559 if (!X509_set_serialNumber(x
, sno
))
561 ASN1_INTEGER_free(sno
);
563 } else if (!X509_set_serialNumber(x
, sno
)) {
567 if (!X509_set_issuer_name(x
, X509_REQ_get_subject_name(req
)))
569 if (!X509_set_subject_name(x
, X509_REQ_get_subject_name(req
)))
571 if (!set_cert_times(x
, NULL
, NULL
, days
))
575 X509_set_pubkey(x
, fkey
);
577 pkey
= X509_REQ_get0_pubkey(req
);
578 X509_set_pubkey(x
, pkey
);
581 x
= load_cert(infile
, informat
, "Certificate");
587 xca
= load_cert(CAfile
, CAformat
, "CA Certificate");
592 if (!noout
|| text
|| next_serial
) {
593 OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
598 X509_alias_set1(x
, (unsigned char *)alias
, -1);
603 X509_reject_clear(x
);
606 for (i
= 0; i
< sk_ASN1_OBJECT_num(trust
); i
++) {
607 objtmp
= sk_ASN1_OBJECT_value(trust
, i
);
608 X509_add1_trust_object(x
, objtmp
);
613 if (reject
!= NULL
) {
614 for (i
= 0; i
< sk_ASN1_OBJECT_num(reject
); i
++) {
615 objtmp
= sk_ASN1_OBJECT_value(reject
, i
);
616 X509_add1_reject_object(x
, objtmp
);
622 const ASN1_BIT_STRING
*signature
;
624 X509_get0_signature(&signature
, NULL
, x
);
625 corrupt_signature(signature
);
629 for (i
= 1; i
<= num
; i
++) {
631 print_name(out
, "issuer=", X509_get_issuer_name(x
), get_nameopt());
632 } else if (subject
== i
) {
633 print_name(out
, "subject=",
634 X509_get_subject_name(x
), get_nameopt());
635 } else if (serial
== i
) {
636 BIO_printf(out
, "serial=");
637 i2a_ASN1_INTEGER(out
, X509_get_serialNumber(x
));
638 BIO_printf(out
, "\n");
639 } else if (next_serial
== i
) {
640 ASN1_INTEGER
*ser
= X509_get_serialNumber(x
);
641 BIGNUM
*bnser
= ASN1_INTEGER_to_BN(ser
, NULL
);
645 if (!BN_add_word(bnser
, 1))
647 ser
= BN_to_ASN1_INTEGER(bnser
, NULL
);
651 i2a_ASN1_INTEGER(out
, ser
);
652 ASN1_INTEGER_free(ser
);
654 } else if ((email
== i
) || (ocsp_uri
== i
)) {
656 STACK_OF(OPENSSL_STRING
) *emlst
;
658 emlst
= X509_get1_email(x
);
660 emlst
= X509_get1_ocsp(x
);
661 for (j
= 0; j
< sk_OPENSSL_STRING_num(emlst
); j
++)
662 BIO_printf(out
, "%s\n",
663 sk_OPENSSL_STRING_value(emlst
, j
));
664 X509_email_free(emlst
);
665 } else if (aliasout
== i
) {
666 unsigned char *alstr
;
667 alstr
= X509_alias_get0(x
, NULL
);
669 BIO_printf(out
, "%s\n", alstr
);
671 BIO_puts(out
, "<No Alias>\n");
672 } else if (subject_hash
== i
) {
673 BIO_printf(out
, "%08lx\n", X509_subject_name_hash(x
));
675 #ifndef OPENSSL_NO_MD5
676 else if (subject_hash_old
== i
) {
677 BIO_printf(out
, "%08lx\n", X509_subject_name_hash_old(x
));
680 else if (issuer_hash
== i
) {
681 BIO_printf(out
, "%08lx\n", X509_issuer_name_hash(x
));
683 #ifndef OPENSSL_NO_MD5
684 else if (issuer_hash_old
== i
) {
685 BIO_printf(out
, "%08lx\n", X509_issuer_name_hash_old(x
));
688 else if (pprint
== i
) {
691 BIO_printf(out
, "Certificate purposes:\n");
692 for (j
= 0; j
< X509_PURPOSE_get_count(); j
++) {
693 ptmp
= X509_PURPOSE_get0(j
);
694 purpose_print(out
, x
, ptmp
);
696 } else if (modulus
== i
) {
699 pkey
= X509_get0_pubkey(x
);
701 BIO_printf(bio_err
, "Modulus=unavailable\n");
702 ERR_print_errors(bio_err
);
705 BIO_printf(out
, "Modulus=");
706 #ifndef OPENSSL_NO_RSA
707 if (EVP_PKEY_id(pkey
) == EVP_PKEY_RSA
) {
709 RSA_get0_key(EVP_PKEY_get0_RSA(pkey
), &n
, NULL
, NULL
);
713 #ifndef OPENSSL_NO_DSA
714 if (EVP_PKEY_id(pkey
) == EVP_PKEY_DSA
) {
715 const BIGNUM
*dsapub
= NULL
;
716 DSA_get0_key(EVP_PKEY_get0_DSA(pkey
), &dsapub
, NULL
);
717 BN_print(out
, dsapub
);
721 BIO_printf(out
, "Wrong Algorithm type");
723 BIO_printf(out
, "\n");
724 } else if (pubkey
== i
) {
727 pkey
= X509_get0_pubkey(x
);
729 BIO_printf(bio_err
, "Error getting public key\n");
730 ERR_print_errors(bio_err
);
733 PEM_write_bio_PUBKEY(out
, pkey
);
739 print_name(out
, "/*\n"
740 " * Subject: ", X509_get_subject_name(x
), get_nameopt());
741 print_name(out
, " * Issuer: ", X509_get_issuer_name(x
), get_nameopt());
742 BIO_puts(out
, " */\n");
744 len
= i2d_X509(x
, NULL
);
745 m
= app_malloc(len
, "x509 name buffer");
746 d
= (unsigned char *)m
;
747 len
= i2d_X509_NAME(X509_get_subject_name(x
), &d
);
748 print_array(out
, "the_subject_name", len
, (unsigned char *)m
);
749 d
= (unsigned char *)m
;
750 len
= i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x
), &d
);
751 print_array(out
, "the_public_key", len
, (unsigned char *)m
);
752 d
= (unsigned char *)m
;
753 len
= i2d_X509(x
, &d
);
754 print_array(out
, "the_certificate", len
, (unsigned char *)m
);
756 } else if (text
== i
) {
757 X509_print_ex(out
, x
, get_nameopt(), certflag
);
758 } else if (startdate
== i
) {
759 BIO_puts(out
, "notBefore=");
760 ASN1_TIME_print(out
, X509_get0_notBefore(x
));
762 } else if (enddate
== i
) {
763 BIO_puts(out
, "notAfter=");
764 ASN1_TIME_print(out
, X509_get0_notAfter(x
));
766 } else if (fingerprint
== i
) {
769 unsigned char md
[EVP_MAX_MD_SIZE
];
770 const EVP_MD
*fdig
= digest
;
775 if (!X509_digest(x
, fdig
, md
, &n
)) {
776 BIO_printf(bio_err
, "out of memory\n");
779 BIO_printf(out
, "%s Fingerprint=",
780 OBJ_nid2sn(EVP_MD_type(fdig
)));
781 for (j
= 0; j
< (int)n
; j
++) {
782 BIO_printf(out
, "%02X%c", md
[j
], (j
+ 1 == (int)n
)
787 /* should be in the library */
788 else if ((sign_flag
== i
) && (x509req
== 0)) {
789 BIO_printf(bio_err
, "Getting Private key\n");
791 Upkey
= load_key(keyfile
, keyformat
, 0,
792 passin
, e
, "Private key");
797 if (!sign(x
, Upkey
, days
, clrext
, digest
, extconf
, extsect
, preserve_dates
))
799 } else if (CA_flag
== i
) {
800 BIO_printf(bio_err
, "Getting CA Private Key\n");
801 if (CAkeyfile
!= NULL
) {
802 CApkey
= load_key(CAkeyfile
, CAkeyformat
,
803 0, passin
, e
, "CA Private Key");
808 if (!x509_certify(ctx
, CAfile
, digest
, x
, xca
,
810 CAserial
, CA_createserial
, days
, clrext
,
811 extconf
, extsect
, sno
, reqfile
, preserve_dates
))
813 } else if (x509req
== i
) {
816 BIO_printf(bio_err
, "Getting request Private Key\n");
817 if (keyfile
== NULL
) {
818 BIO_printf(bio_err
, "no request key file specified\n");
821 pk
= load_key(keyfile
, keyformat
, 0,
822 passin
, e
, "request key");
827 BIO_printf(bio_err
, "Generating certificate request\n");
829 rq
= X509_to_X509_REQ(x
, pk
, digest
);
832 ERR_print_errors(bio_err
);
836 X509_REQ_print_ex(out
, rq
, get_nameopt(), X509_FLAG_COMPAT
);
837 PEM_write_bio_X509_REQ(out
, rq
);
840 } else if (ocspid
== i
) {
841 X509_ocspid_print(out
, x
);
847 time_t tcheck
= time(NULL
) + checkoffset
;
849 if (X509_cmp_time(X509_get0_notAfter(x
), &tcheck
) < 0) {
850 BIO_printf(out
, "Certificate will expire\n");
853 BIO_printf(out
, "Certificate will not expire\n");
859 print_cert_checks(out
, x
, checkhost
, checkemail
, checkip
);
861 if (noout
|| nocert
) {
866 if (outformat
== FORMAT_ASN1
) {
867 i
= i2d_X509_bio(out
, x
);
868 } else if (outformat
== FORMAT_PEM
) {
870 i
= PEM_write_bio_X509_AUX(out
, x
);
872 i
= PEM_write_bio_X509(out
, x
);
874 BIO_printf(bio_err
, "bad output format specified for outfile\n");
878 BIO_printf(bio_err
, "unable to write certificate\n");
879 ERR_print_errors(bio_err
);
886 X509_STORE_free(ctx
);
890 EVP_PKEY_free(Upkey
);
891 EVP_PKEY_free(CApkey
);
893 sk_OPENSSL_STRING_free(sigopts
);
895 ASN1_INTEGER_free(sno
);
896 sk_ASN1_OBJECT_pop_free(trust
, ASN1_OBJECT_free
);
897 sk_ASN1_OBJECT_pop_free(reject
, ASN1_OBJECT_free
);
898 ASN1_OBJECT_free(objtmp
);
900 OPENSSL_free(passin
);
904 static ASN1_INTEGER
*x509_load_serial(const char *CAfile
,
905 const char *serialfile
, int create
)
908 ASN1_INTEGER
*bs
= NULL
;
909 BIGNUM
*serial
= NULL
;
911 if (serialfile
== NULL
) {
912 const char *p
= strchr(CAfile
, '.');
913 size_t len
= p
!= NULL
? (size_t)(p
- CAfile
) : strlen(CAfile
);
915 buf
= app_malloc(len
+ sizeof(POSTFIX
), "serial# buffer");
916 memcpy(buf
, CAfile
, len
);
917 memcpy(buf
+ len
, POSTFIX
, sizeof(POSTFIX
));
921 serial
= load_serial(serialfile
, create
, NULL
);
925 if (!BN_add_word(serial
, 1)) {
926 BIO_printf(bio_err
, "add_word failure\n");
930 if (!save_serial(serialfile
, NULL
, serial
, &bs
))
939 static int x509_certify(X509_STORE
*ctx
, const char *CAfile
, const EVP_MD
*digest
,
940 X509
*x
, X509
*xca
, EVP_PKEY
*pkey
,
941 STACK_OF(OPENSSL_STRING
) *sigopts
,
942 const char *serialfile
, int create
,
943 int days
, int clrext
, CONF
*conf
, const char *section
,
944 ASN1_INTEGER
*sno
, int reqfile
, int preserve_dates
)
947 ASN1_INTEGER
*bs
= NULL
;
948 X509_STORE_CTX
*xsc
= NULL
;
951 upkey
= X509_get0_pubkey(xca
);
953 BIO_printf(bio_err
, "Error obtaining CA X509 public key\n");
956 EVP_PKEY_copy_parameters(upkey
, pkey
);
958 xsc
= X509_STORE_CTX_new();
959 if (xsc
== NULL
|| !X509_STORE_CTX_init(xsc
, ctx
, x
, NULL
)) {
960 BIO_printf(bio_err
, "Error initialising X509 store\n");
965 else if ((bs
= x509_load_serial(CAfile
, serialfile
, create
)) == NULL
)
969 * NOTE: this certificate can/should be self signed, unless it was a
970 * certificate request in which case it is not.
972 X509_STORE_CTX_set_cert(xsc
, x
);
973 X509_STORE_CTX_set_flags(xsc
, X509_V_FLAG_CHECK_SS_SIGNATURE
);
974 if (!reqfile
&& X509_verify_cert(xsc
) <= 0)
977 if (!X509_check_private_key(xca
, pkey
)) {
979 "CA certificate and CA private key do not match\n");
983 if (!X509_set_issuer_name(x
, X509_get_subject_name(xca
)))
985 if (!X509_set_serialNumber(x
, bs
))
988 if (!preserve_dates
&& !set_cert_times(x
, NULL
, NULL
, days
))
992 while (X509_get_ext_count(x
) > 0)
993 X509_delete_ext(x
, 0);
998 X509_set_version(x
, 2); /* version 3 certificate */
999 X509V3_set_ctx(&ctx2
, xca
, x
, NULL
, NULL
, 0);
1000 X509V3_set_nconf(&ctx2
, conf
);
1001 if (!X509V3_EXT_add_nconf(conf
, &ctx2
, section
, x
))
1005 if (!do_X509_sign(x
, pkey
, digest
, sigopts
))
1009 X509_STORE_CTX_free(xsc
);
1011 ERR_print_errors(bio_err
);
1013 ASN1_INTEGER_free(bs
);
1017 static int callb(int ok
, X509_STORE_CTX
*ctx
)
1023 * it is ok to use a self signed certificate This case will catch both
1024 * the initial ok == 0 and the final ok == 1 calls to this function
1026 err
= X509_STORE_CTX_get_error(ctx
);
1027 if (err
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
)
1031 * BAD we should have gotten an error. Normally if everything worked
1032 * X509_STORE_CTX_get_error(ctx) will still be set to
1033 * DEPTH_ZERO_SELF_....
1037 "error with certificate to be certified - should be self signed\n");
1040 err_cert
= X509_STORE_CTX_get_current_cert(ctx
);
1041 print_name(bio_err
, NULL
, X509_get_subject_name(err_cert
), 0);
1043 "error with certificate - error %d at depth %d\n%s\n", err
,
1044 X509_STORE_CTX_get_error_depth(ctx
),
1045 X509_verify_cert_error_string(err
));
1051 static int sign(X509
*x
, EVP_PKEY
*pkey
, int days
, int clrext
,
1052 const EVP_MD
*digest
, CONF
*conf
, const char *section
,
1056 if (!X509_set_issuer_name(x
, X509_get_subject_name(x
)))
1058 if (!preserve_dates
&& !set_cert_times(x
, NULL
, NULL
, days
))
1060 if (!X509_set_pubkey(x
, pkey
))
1063 while (X509_get_ext_count(x
) > 0)
1064 X509_delete_ext(x
, 0);
1068 X509_set_version(x
, 2); /* version 3 certificate */
1069 X509V3_set_ctx(&ctx
, x
, x
, NULL
, NULL
, 0);
1070 X509V3_set_nconf(&ctx
, conf
);
1071 if (!X509V3_EXT_add_nconf(conf
, &ctx
, section
, x
))
1074 if (!X509_sign(x
, pkey
, digest
))
1078 ERR_print_errors(bio_err
);
1082 static int purpose_print(BIO
*bio
, X509
*cert
, X509_PURPOSE
*pt
)
1086 id
= X509_PURPOSE_get_id(pt
);
1087 pname
= X509_PURPOSE_get0_name(pt
);
1088 for (i
= 0; i
< 2; i
++) {
1089 idret
= X509_check_purpose(cert
, id
, i
);
1090 BIO_printf(bio
, "%s%s : ", pname
, i
? " CA" : "");
1092 BIO_printf(bio
, "Yes\n");
1093 else if (idret
== 0)
1094 BIO_printf(bio
, "No\n");
1096 BIO_printf(bio
, "Yes (WARNING code=%d)\n", idret
);