2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #include <openssl/bio.h>
16 #include <openssl/asn1.h>
17 #include <openssl/err.h>
18 #include <openssl/bn.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
21 #include <openssl/x509v3.h>
22 #include <openssl/objects.h>
23 #include <openssl/pem.h>
24 #ifndef OPENSSL_NO_RSA
25 # include <openssl/rsa.h>
27 #ifndef OPENSSL_NO_DSA
28 # include <openssl/dsa.h>
32 #define POSTFIX ".srl"
35 static int callb(int ok
, X509_STORE_CTX
*ctx
);
36 static int sign(X509
*x
, EVP_PKEY
*pkey
, EVP_PKEY
*fkey
,
37 STACK_OF(OPENSSL_STRING
) *sigopts
,
39 const EVP_MD
*digest
, CONF
*conf
, const char *section
,
41 static int x509_certify(X509_STORE
*ctx
, const char *CAfile
, const EVP_MD
*digest
,
42 X509
*x
, X509
*xca
, EVP_PKEY
*pkey
,
43 STACK_OF(OPENSSL_STRING
) *sigopts
, const char *serialfile
,
44 int create
, int days
, int clrext
, CONF
*conf
,
45 const char *section
, ASN1_INTEGER
*sno
, int reqfile
,
47 static int purpose_print(BIO
*bio
, X509
*cert
, X509_PURPOSE
*pt
);
48 static int print_x509v3_exts(BIO
*bio
, X509
*x
, const char *exts
);
50 typedef enum OPTION_choice
{
51 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
52 OPT_INFORM
, OPT_OUTFORM
, OPT_KEYFORM
, OPT_REQ
, OPT_CAFORM
,
53 OPT_CAKEYFORM
, OPT_VFYOPT
, OPT_SIGOPT
, OPT_DAYS
, OPT_PASSIN
, OPT_EXTFILE
,
54 OPT_EXTENSIONS
, OPT_IN
, OPT_OUT
, OPT_SIGNKEY
, OPT_CA
, OPT_CAKEY
,
55 OPT_CASERIAL
, OPT_SET_SERIAL
, OPT_NEW
, OPT_FORCE_PUBKEY
, OPT_SUBJ
,
56 OPT_ADDTRUST
, OPT_ADDREJECT
, OPT_SETALIAS
, OPT_CERTOPT
, OPT_NAMEOPT
,
57 OPT_EMAIL
, OPT_OCSP_URI
, OPT_SERIAL
, OPT_NEXT_SERIAL
,
58 OPT_MODULUS
, OPT_PUBKEY
, OPT_X509TOREQ
, OPT_TEXT
, OPT_HASH
,
59 OPT_ISSUER_HASH
, OPT_SUBJECT
, OPT_ISSUER
, OPT_FINGERPRINT
, OPT_DATES
,
60 OPT_PURPOSE
, OPT_STARTDATE
, OPT_ENDDATE
, OPT_CHECKEND
, OPT_CHECKHOST
,
61 OPT_CHECKEMAIL
, OPT_CHECKIP
, OPT_NOOUT
, OPT_TRUSTOUT
, OPT_CLRTRUST
,
62 OPT_CLRREJECT
, OPT_ALIAS
, OPT_CACREATESERIAL
, OPT_CLREXT
, OPT_OCSPID
,
65 OPT_BADSIG
, OPT_MD
, OPT_ENGINE
, OPT_NOCERT
, OPT_PRESERVE_DATES
,
66 OPT_R_ENUM
, OPT_PROV_ENUM
, OPT_EXT
69 const OPTIONS x509_options
[] = {
70 OPT_SECTION("General"),
71 {"help", OPT_HELP
, '-', "Display this summary"},
72 #ifndef OPENSSL_NO_ENGINE
73 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
76 {"inform", OPT_INFORM
, 'f',
77 "CSR input format (DER or PEM) - default PEM"},
78 {"in", OPT_IN
, '<', "Input file - default stdin"},
79 {"passin", OPT_PASSIN
, 's', "Private key and cert file pass-phrase source"},
80 {"outform", OPT_OUTFORM
, 'f',
81 "Output format (DER or PEM) - default PEM"},
82 {"out", OPT_OUT
, '>', "Output file - default stdout"},
83 {"keyform", OPT_KEYFORM
, 'E', "Private key format (ENGINE, other values ignored)"},
84 {"req", OPT_REQ
, '-', "Input is a certificate request, sign and output"},
85 {"vfyopt", OPT_VFYOPT
, 's', "Verification parameter in n:v form"},
87 OPT_SECTION("Output"),
88 {"serial", OPT_SERIAL
, '-', "Print serial number value"},
89 {"subject_hash", OPT_HASH
, '-', "Print subject hash value"},
90 {"issuer_hash", OPT_ISSUER_HASH
, '-', "Print issuer hash value"},
91 {"hash", OPT_HASH
, '-', "Synonym for -subject_hash"},
92 {"subject", OPT_SUBJECT
, '-', "Print subject DN"},
93 {"issuer", OPT_ISSUER
, '-', "Print issuer DN"},
94 {"email", OPT_EMAIL
, '-', "Print email address(es)"},
95 {"purpose", OPT_PURPOSE
, '-', "Print out certificate purposes"},
96 {"modulus", OPT_MODULUS
, '-', "Print the RSA key modulus"},
97 {"pubkey", OPT_PUBKEY
, '-', "Output the public key"},
98 {"fingerprint", OPT_FINGERPRINT
, '-', "Print the certificate fingerprint"},
99 {"alias", OPT_ALIAS
, '-', "Output certificate alias"},
100 {"noout", OPT_NOOUT
, '-', "No output, just status"},
101 {"ocspid", OPT_OCSPID
, '-',
102 "Print OCSP hash values for the subject name and public key"},
103 {"ocsp_uri", OPT_OCSP_URI
, '-', "Print OCSP Responder URL(s)"},
104 {"nocert", OPT_NOCERT
, '-', "No certificate output"},
105 {"trustout", OPT_TRUSTOUT
, '-', "Output a trusted certificate"},
106 {"x509toreq", OPT_X509TOREQ
, '-',
107 "Output a certification request object"},
108 {"checkend", OPT_CHECKEND
, 'M',
109 "Check whether the cert expires in the next arg seconds"},
110 {OPT_MORE_STR
, 1, 1, "Exit 1 if so, 0 if not"},
111 {"text", OPT_TEXT
, '-', "Print the certificate in text form"},
112 {"ext", OPT_EXT
, 's', "Print various X509V3 extensions"},
113 #ifndef OPENSSL_NO_MD5
114 {"subject_hash_old", OPT_SUBJECT_HASH_OLD
, '-',
115 "Print old-style (MD5) subject hash value"},
116 {"issuer_hash_old", OPT_ISSUER_HASH_OLD
, '-',
117 "Print old-style (MD5) issuer hash value"},
119 {"nameopt", OPT_NAMEOPT
, 's', "Certificate subject/issuer name printing options"},
121 OPT_SECTION("Certificate"),
122 {"startdate", OPT_STARTDATE
, '-', "Set notBefore field"},
123 {"enddate", OPT_ENDDATE
, '-', "Set notAfter field"},
124 {"dates", OPT_DATES
, '-', "Both Before and After dates"},
125 {"clrtrust", OPT_CLRTRUST
, '-', "Clear all trusted purposes"},
126 {"clrext", OPT_CLREXT
, '-', "Clear all certificate extensions"},
127 {"addtrust", OPT_ADDTRUST
, 's', "Trust certificate for a given purpose"},
128 {"addreject", OPT_ADDREJECT
, 's',
129 "Reject certificate for a given purpose"},
130 {"setalias", OPT_SETALIAS
, 's', "Set certificate alias"},
131 {"days", OPT_DAYS
, 'n',
132 "How long till expiry of a signed certificate - def 30 days"},
133 {"signkey", OPT_SIGNKEY
, 's', "Self-sign cert with arg"},
134 {"set_serial", OPT_SET_SERIAL
, 's', "Serial number to use"},
135 {"extensions", OPT_EXTENSIONS
, 's', "Section from config file to use"},
136 {"certopt", OPT_CERTOPT
, 's', "Various certificate text options"},
137 {"checkhost", OPT_CHECKHOST
, 's', "Check certificate matches host"},
138 {"checkemail", OPT_CHECKEMAIL
, 's', "Check certificate matches email"},
139 {"checkip", OPT_CHECKIP
, 's', "Check certificate matches ipaddr"},
140 {"force_pubkey", OPT_FORCE_PUBKEY
, '<', "Force the key to put inside certificate"},
141 {"subj", OPT_SUBJ
, 's', "Set or override certificate subject (and issuer)"},
144 {"CA", OPT_CA
, '<', "Set the CA certificate, must be PEM format"},
145 {"CAkey", OPT_CAKEY
, 's',
146 "The CA key, must be PEM format; if not in CAfile"},
147 {"extfile", OPT_EXTFILE
, '<', "File with X509V3 extensions to add"},
150 {"CAform", OPT_CAFORM
, 'F', "CA cert format (PEM/DER/P12); has no effect"},
151 {"CAkeyform", OPT_CAKEYFORM
, 'E', "CA key format (ENGINE, other values ignored)"},
152 {"sigopt", OPT_SIGOPT
, 's', "Signature parameter in n:v form"},
153 {"CAcreateserial", OPT_CACREATESERIAL
, '-',
154 "Create serial number file if it does not exist"},
155 {"CAserial", OPT_CASERIAL
, 's', "Serial file"},
156 {"new", OPT_NEW
, '-', "Generate a certificate from scratch"},
157 {"next_serial", OPT_NEXT_SERIAL
, '-', "Increment current certificate serial number"},
158 {"clrreject", OPT_CLRREJECT
, '-',
159 "Clears all the prohibited or rejected uses of the certificate"},
160 {"badsig", OPT_BADSIG
, '-', "Corrupt last byte of certificate signature (for test)"},
161 {"", OPT_MD
, '-', "Any supported digest"},
162 {"preserve_dates", OPT_PRESERVE_DATES
, '-', "preserve existing dates when signing"},
166 int x509_main(int argc
, char **argv
)
168 ASN1_INTEGER
*sno
= NULL
;
169 ASN1_OBJECT
*objtmp
= NULL
;
171 CONF
*extconf
= NULL
;
172 EVP_PKEY
*Upkey
= NULL
, *CApkey
= NULL
, *fkey
= NULL
;
175 X509_NAME
*fsubj
= NULL
;
176 const unsigned long chtype
= MBSTRING_ASC
;
177 const int multirdn
= 1;
178 STACK_OF(ASN1_OBJECT
) *trust
= NULL
, *reject
= NULL
;
179 STACK_OF(OPENSSL_STRING
) *sigopts
= NULL
, *vfyopts
= NULL
;
180 X509
*x
= NULL
, *xca
= NULL
;
181 X509_REQ
*req
= NULL
, *rq
= NULL
;
182 X509_STORE
*ctx
= NULL
;
183 const EVP_MD
*digest
= NULL
;
184 char *CAkeyfile
= NULL
, *CAserial
= NULL
, *fkeyfile
= NULL
, *alias
= NULL
;
185 char *checkhost
= NULL
, *checkemail
= NULL
, *checkip
= NULL
, *exts
= NULL
;
186 char *extsect
= NULL
, *extfile
= NULL
, *passin
= NULL
, *passinarg
= NULL
;
187 char *infile
= NULL
, *outfile
= NULL
, *keyfile
= NULL
, *CAfile
= NULL
;
189 int x509req
= 0, days
= DEF_DAYS
, modulus
= 0, pubkey
= 0, pprint
= 0;
190 int CAformat
= FORMAT_PEM
, CAkeyformat
= FORMAT_PEM
;
191 int fingerprint
= 0, reqfile
= 0, checkend
= 0;
192 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, keyformat
= FORMAT_PEM
;
193 int next_serial
= 0, subject_hash
= 0, issuer_hash
= 0, ocspid
= 0;
194 int noout
= 0, sign_flag
= 0, CA_flag
= 0, CA_createserial
= 0, email
= 0;
195 int ocsp_uri
= 0, trustout
= 0, clrtrust
= 0, clrreject
= 0, aliasout
= 0;
196 int ret
= 1, i
, num
= 0, badsig
= 0, clrext
= 0, nocert
= 0;
197 int text
= 0, serial
= 0, subject
= 0, issuer
= 0, startdate
= 0, ext
= 0;
199 time_t checkoffset
= 0;
200 unsigned long certflag
= 0;
201 int preserve_dates
= 0;
204 #ifndef OPENSSL_NO_MD5
205 int subject_hash_old
= 0, issuer_hash_old
= 0;
208 ctx
= X509_STORE_new();
211 X509_STORE_set_verify_cb(ctx
, callb
);
213 prog
= opt_init(argc
, argv
, x509_options
);
214 while ((o
= opt_next()) != OPT_EOF
) {
219 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
222 opt_help(x509_options
);
226 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
233 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &outformat
))
237 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyformat
))
241 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &CAformat
))
245 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &CAkeyformat
))
257 sigopts
= sk_OPENSSL_STRING_new_null();
258 if (!sigopts
|| !sk_OPENSSL_STRING_push(sigopts
, opt_arg()))
263 vfyopts
= sk_OPENSSL_STRING_new_null();
264 if (!vfyopts
|| !sk_OPENSSL_STRING_push(vfyopts
, opt_arg()))
270 days
= atoi(opt_arg());
273 passinarg
= opt_arg();
283 if (!opt_provider(o
))
298 CAkeyfile
= opt_arg();
301 CAserial
= opt_arg();
305 BIO_printf(bio_err
, "Serial number supplied twice\n");
308 if ((sno
= s2i_ASN1_INTEGER(NULL
, opt_arg())) == NULL
)
314 case OPT_FORCE_PUBKEY
:
315 fkeyfile
= opt_arg();
321 if ((objtmp
= OBJ_txt2obj(opt_arg(), 0)) == NULL
) {
323 "%s: Invalid trust object value %s\n",
327 if (trust
== NULL
&& (trust
= sk_ASN1_OBJECT_new_null()) == NULL
)
329 sk_ASN1_OBJECT_push(trust
, objtmp
);
334 if ((objtmp
= OBJ_txt2obj(opt_arg(), 0)) == NULL
) {
336 "%s: Invalid reject object value %s\n",
341 && (reject
= sk_ASN1_OBJECT_new_null()) == NULL
)
343 sk_ASN1_OBJECT_push(reject
, objtmp
);
352 if (!set_cert_ex(&certflag
, opt_arg()))
356 if (!set_nameopt(opt_arg()))
360 e
= setup_engine(opt_arg(), 0);
371 case OPT_NEXT_SERIAL
:
392 case OPT_FINGERPRINT
:
396 subject_hash
= ++num
;
398 case OPT_ISSUER_HASH
:
432 case OPT_CACREATESERIAL
:
433 CA_createserial
= ++num
;
444 #ifndef OPENSSL_NO_MD5
445 case OPT_SUBJECT_HASH_OLD
:
446 subject_hash_old
= ++num
;
448 case OPT_ISSUER_HASH_OLD
:
449 issuer_hash_old
= ++num
;
452 case OPT_SUBJECT_HASH_OLD
:
453 case OPT_ISSUER_HASH_OLD
:
464 if (!opt_imax(opt_arg(), &temp
))
466 checkoffset
= (time_t)temp
;
467 if ((intmax_t)checkoffset
!= temp
) {
468 BIO_printf(bio_err
, "%s: checkend time out of range %s\n",
475 checkhost
= opt_arg();
478 checkemail
= opt_arg();
483 case OPT_PRESERVE_DATES
:
484 if (days
!= DEF_DAYS
)
489 if (!opt_md(opt_unknown(), &digest
))
493 argc
= opt_num_rest();
496 BIO_printf(bio_err
, "%s: Unknown parameter %s\n", prog
, argv
[0]);
500 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
501 BIO_printf(bio_err
, "Error getting password\n");
505 if (!X509_STORE_set_default_paths_ex(ctx
, app_get0_libctx(),
507 ERR_print_errors(bio_err
);
511 if (newcert
&& infile
!= NULL
) {
512 BIO_printf(bio_err
, "The -in option must not be used since -new is set\n");
515 if (newcert
&& fkeyfile
== NULL
) {
517 "The -new option requires a public key to be set using -force_pubkey\n");
520 if (fkeyfile
!= NULL
) {
521 fkey
= load_pubkey(fkeyfile
, keyformat
, 0, NULL
, e
, "forced key");
526 if (newcert
&& subj
== NULL
) {
528 "The -new option requires a subject to be set using -subj\n");
532 && (fsubj
= parse_name(subj
, chtype
, multirdn
, "subject")) == NULL
)
535 if (CAkeyfile
== NULL
&& CA_flag
&& CAformat
== FORMAT_PEM
) {
537 } else if (CA_flag
&& CAkeyfile
== NULL
) {
539 "need to specify a CAkey if using the CA command\n");
541 } else if (!CA_flag
&& CAkeyfile
!= NULL
) {
543 "ignoring -CAkey option since no -CA option is given\n");
546 if (extfile
!= NULL
) {
548 if ((extconf
= app_load_config(extfile
)) == NULL
)
550 if (extsect
== NULL
) {
551 extsect
= NCONF_get_string(extconf
, "default", "extensions");
552 if (extsect
== NULL
) {
557 X509V3_set_ctx_test(&ctx2
);
558 X509V3_set_nconf(&ctx2
, extconf
);
559 if (!X509V3_EXT_add_nconf(extconf
, &ctx2
, extsect
, NULL
)) {
561 "Error Loading extension section %s\n", extsect
);
562 ERR_print_errors(bio_err
);
570 req
= load_csr(infile
, informat
, "certificate request input");
574 if ((pkey
= X509_REQ_get0_pubkey(req
)) == NULL
) {
575 BIO_printf(bio_err
, "error unpacking public key\n");
578 i
= do_X509_REQ_verify(req
, pkey
, vfyopts
);
580 BIO_printf(bio_err
, "Request self-signature verification error\n");
581 ERR_print_errors(bio_err
);
586 "Request self-signature did not match the certificate request\n");
589 BIO_printf(bio_err
, "Request self-signature ok\n");
592 print_name(bio_err
, "subject=", X509_REQ_get_subject_name(req
),
596 if (reqfile
|| newcert
) {
599 if (!sign_flag
&& CAkeyfile
== NULL
) {
601 "We need a private key to sign with, use -signkey or -CAkey or -CA <file> with private key\n");
604 if ((x
= X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL
)
608 sno
= ASN1_INTEGER_new();
609 if (sno
== NULL
|| !rand_serial(NULL
, sno
))
611 if (!X509_set_serialNumber(x
, sno
))
613 ASN1_INTEGER_free(sno
);
615 } else if (!X509_set_serialNumber(x
, sno
)) {
619 n
= req
== NULL
? fsubj
: X509_REQ_get_subject_name(req
);
620 if (!X509_set_issuer_name(x
, n
) || !X509_set_subject_name(x
, n
))
622 if (!set_cert_times(x
, NULL
, NULL
, days
))
625 if (!X509_set_pubkey(x
, fkey
!= NULL
? fkey
: X509_REQ_get0_pubkey(req
)))
628 x
= load_cert_pass(infile
, 1, passin
, "certificate");
631 if (fkey
!= NULL
&& !X509_set_pubkey(x
, fkey
))
633 if (fsubj
!= NULL
&& !X509_set_subject_name(x
, fsubj
))
638 xca
= load_cert_pass(CAfile
, 1, passin
, "CA certificate");
643 out
= bio_open_default(outfile
, 'w', outformat
);
647 if (!noout
|| text
|| next_serial
)
648 OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3");
651 X509_alias_set1(x
, (unsigned char *)alias
, -1);
656 X509_reject_clear(x
);
659 for (i
= 0; i
< sk_ASN1_OBJECT_num(trust
); i
++) {
660 objtmp
= sk_ASN1_OBJECT_value(trust
, i
);
661 X509_add1_trust_object(x
, objtmp
);
666 if (reject
!= NULL
) {
667 for (i
= 0; i
< sk_ASN1_OBJECT_num(reject
); i
++) {
668 objtmp
= sk_ASN1_OBJECT_value(reject
, i
);
669 X509_add1_reject_object(x
, objtmp
);
675 const ASN1_BIT_STRING
*signature
;
677 X509_get0_signature(&signature
, NULL
, x
);
678 corrupt_signature(signature
);
682 for (i
= 1; i
<= num
; i
++) {
684 print_name(out
, "issuer=", X509_get_issuer_name(x
), get_nameopt());
685 } else if (subject
== i
) {
686 print_name(out
, "subject=",
687 X509_get_subject_name(x
), get_nameopt());
688 } else if (serial
== i
) {
689 BIO_printf(out
, "serial=");
690 i2a_ASN1_INTEGER(out
, X509_get0_serialNumber(x
));
691 BIO_printf(out
, "\n");
692 } else if (next_serial
== i
) {
693 ASN1_INTEGER
*ser
= X509_get_serialNumber(x
);
694 BIGNUM
*bnser
= ASN1_INTEGER_to_BN(ser
, NULL
);
698 if (!BN_add_word(bnser
, 1))
700 ser
= BN_to_ASN1_INTEGER(bnser
, NULL
);
704 i2a_ASN1_INTEGER(out
, ser
);
705 ASN1_INTEGER_free(ser
);
707 } else if (email
== i
|| ocsp_uri
== i
) {
709 STACK_OF(OPENSSL_STRING
) *emlst
;
711 emlst
= X509_get1_email(x
);
713 emlst
= X509_get1_ocsp(x
);
714 for (j
= 0; j
< sk_OPENSSL_STRING_num(emlst
); j
++)
715 BIO_printf(out
, "%s\n",
716 sk_OPENSSL_STRING_value(emlst
, j
));
717 X509_email_free(emlst
);
718 } else if (aliasout
== i
) {
719 unsigned char *alstr
;
720 alstr
= X509_alias_get0(x
, NULL
);
722 BIO_printf(out
, "%s\n", alstr
);
724 BIO_puts(out
, "<No Alias>\n");
725 } else if (subject_hash
== i
) {
726 BIO_printf(out
, "%08lx\n", X509_subject_name_hash(x
));
728 #ifndef OPENSSL_NO_MD5
729 else if (subject_hash_old
== i
) {
730 BIO_printf(out
, "%08lx\n", X509_subject_name_hash_old(x
));
733 else if (issuer_hash
== i
) {
734 BIO_printf(out
, "%08lx\n", X509_issuer_name_hash(x
));
736 #ifndef OPENSSL_NO_MD5
737 else if (issuer_hash_old
== i
) {
738 BIO_printf(out
, "%08lx\n", X509_issuer_name_hash_old(x
));
741 else if (pprint
== i
) {
744 BIO_printf(out
, "Certificate purposes:\n");
745 for (j
= 0; j
< X509_PURPOSE_get_count(); j
++) {
746 ptmp
= X509_PURPOSE_get0(j
);
747 purpose_print(out
, x
, ptmp
);
749 } else if (modulus
== i
) {
752 pkey
= X509_get0_pubkey(x
);
754 BIO_printf(bio_err
, "Modulus=unavailable\n");
755 ERR_print_errors(bio_err
);
758 BIO_printf(out
, "Modulus=");
759 #ifndef OPENSSL_NO_RSA
760 if (EVP_PKEY_id(pkey
) == EVP_PKEY_RSA
) {
762 RSA_get0_key(EVP_PKEY_get0_RSA(pkey
), &n
, NULL
, NULL
);
766 #ifndef OPENSSL_NO_DSA
767 if (EVP_PKEY_id(pkey
) == EVP_PKEY_DSA
) {
768 const BIGNUM
*dsapub
= NULL
;
769 DSA_get0_key(EVP_PKEY_get0_DSA(pkey
), &dsapub
, NULL
);
770 BN_print(out
, dsapub
);
774 BIO_printf(out
, "Wrong Algorithm type");
776 BIO_printf(out
, "\n");
777 } else if (pubkey
== i
) {
780 pkey
= X509_get0_pubkey(x
);
782 BIO_printf(bio_err
, "Error getting public key\n");
783 ERR_print_errors(bio_err
);
786 PEM_write_bio_PUBKEY(out
, pkey
);
787 } else if (text
== i
) {
788 X509_print_ex(out
, x
, get_nameopt(), certflag
);
789 } else if (startdate
== i
) {
790 BIO_puts(out
, "notBefore=");
791 ASN1_TIME_print(out
, X509_get0_notBefore(x
));
793 } else if (enddate
== i
) {
794 BIO_puts(out
, "notAfter=");
795 ASN1_TIME_print(out
, X509_get0_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");
828 if (!sign(x
, Upkey
, fkey
, sigopts
, days
, clrext
, digest
,
829 extconf
, extsect
, preserve_dates
))
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");
840 if (!x509_certify(ctx
, CAfile
, digest
, x
, xca
,
842 CAserial
, CA_createserial
, days
, clrext
,
843 extconf
, extsect
, sno
, reqfile
, preserve_dates
))
845 } else if (x509req
== i
) {
848 BIO_printf(bio_err
, "Getting request Private Key\n");
849 if (keyfile
== NULL
) {
850 BIO_printf(bio_err
, "no request key file specified\n");
853 pk
= load_key(keyfile
, keyformat
, 0,
854 passin
, e
, "request key");
859 BIO_printf(bio_err
, "Generating certificate request\n");
861 rq
= X509_to_X509_REQ(x
, pk
, digest
);
864 ERR_print_errors(bio_err
);
868 X509_REQ_print_ex(out
, rq
, get_nameopt(), X509_FLAG_COMPAT
);
869 PEM_write_bio_X509_REQ(out
, rq
);
872 } else if (ocspid
== i
) {
873 X509_ocspid_print(out
, x
);
874 } else if (ext
== i
) {
875 print_x509v3_exts(out
, x
, exts
);
881 time_t tcheck
= time(NULL
) + checkoffset
;
883 if (X509_cmp_time(X509_get0_notAfter(x
), &tcheck
) < 0) {
884 BIO_printf(out
, "Certificate will expire\n");
887 BIO_printf(out
, "Certificate will not expire\n");
893 print_cert_checks(out
, x
, checkhost
, checkemail
, checkip
);
895 if (noout
|| nocert
) {
900 if (outformat
== FORMAT_ASN1
) {
901 i
= i2d_X509_bio(out
, x
);
902 } else if (outformat
== FORMAT_PEM
) {
904 i
= PEM_write_bio_X509_AUX(out
, x
);
906 i
= PEM_write_bio_X509(out
, x
);
908 BIO_printf(bio_err
, "bad output format specified for outfile\n");
912 BIO_printf(bio_err
, "unable to write certificate\n");
913 ERR_print_errors(bio_err
);
920 X509_STORE_free(ctx
);
921 X509_NAME_free(fsubj
);
925 EVP_PKEY_free(Upkey
);
926 EVP_PKEY_free(CApkey
);
928 sk_OPENSSL_STRING_free(sigopts
);
929 sk_OPENSSL_STRING_free(vfyopts
);
931 ASN1_INTEGER_free(sno
);
932 sk_ASN1_OBJECT_pop_free(trust
, ASN1_OBJECT_free
);
933 sk_ASN1_OBJECT_pop_free(reject
, ASN1_OBJECT_free
);
934 ASN1_OBJECT_free(objtmp
);
940 static ASN1_INTEGER
*x509_load_serial(const char *CAfile
,
941 const char *serialfile
, int create
)
944 ASN1_INTEGER
*bs
= NULL
;
945 BIGNUM
*serial
= NULL
;
947 if (serialfile
== NULL
) {
948 const char *p
= strrchr(CAfile
, '.');
949 size_t len
= p
!= NULL
? (size_t)(p
- CAfile
) : strlen(CAfile
);
951 buf
= app_malloc(len
+ sizeof(POSTFIX
), "serial# buffer");
952 memcpy(buf
, CAfile
, len
);
953 memcpy(buf
+ len
, POSTFIX
, sizeof(POSTFIX
));
957 serial
= load_serial(serialfile
, create
, NULL
);
961 if (!BN_add_word(serial
, 1)) {
962 BIO_printf(bio_err
, "add_word failure\n");
966 if (!save_serial(serialfile
, NULL
, serial
, &bs
))
975 static int x509_certify(X509_STORE
*ctx
, const char *CAfile
, const EVP_MD
*digest
,
976 X509
*x
, X509
*xca
, EVP_PKEY
*pkey
,
977 STACK_OF(OPENSSL_STRING
) *sigopts
,
978 const char *serialfile
, int create
,
979 int days
, int clrext
, CONF
*conf
, const char *section
,
980 ASN1_INTEGER
*sno
, int reqfile
, int preserve_dates
)
983 ASN1_INTEGER
*bs
= NULL
;
984 X509_STORE_CTX
*xsc
= NULL
;
987 upkey
= X509_get0_pubkey(xca
);
989 BIO_printf(bio_err
, "Error obtaining CA X509 public key\n");
992 EVP_PKEY_copy_parameters(upkey
, pkey
);
994 xsc
= X509_STORE_CTX_new();
995 if (xsc
== NULL
|| !X509_STORE_CTX_init(xsc
, ctx
, x
, NULL
)) {
996 BIO_printf(bio_err
, "Error initialising X509 store\n");
1001 else if ((bs
= x509_load_serial(CAfile
, serialfile
, create
)) == NULL
)
1005 * NOTE: this certificate can/should be self-signed, unless it was a
1006 * certificate request in which case it is not.
1008 X509_STORE_CTX_set_cert(xsc
, x
);
1009 X509_STORE_CTX_set_flags(xsc
, X509_V_FLAG_CHECK_SS_SIGNATURE
);
1010 if (!reqfile
&& X509_verify_cert(xsc
) <= 0)
1013 if (!X509_check_private_key(xca
, pkey
)) {
1015 "CA certificate and CA private key do not match\n");
1019 if (!X509_set_issuer_name(x
, X509_get_subject_name(xca
)))
1021 if (!X509_set_serialNumber(x
, bs
))
1024 if (!preserve_dates
&& !set_cert_times(x
, NULL
, NULL
, days
))
1028 while (X509_get_ext_count(x
) > 0)
1029 X509_delete_ext(x
, 0);
1034 X509_set_version(x
, 2); /* version 3 certificate */
1035 X509V3_set_ctx(&ctx2
, xca
, x
, NULL
, NULL
, 0);
1036 X509V3_set_nconf(&ctx2
, conf
);
1037 if (!X509V3_EXT_add_nconf(conf
, &ctx2
, section
, x
))
1041 if (!do_X509_sign(x
, pkey
, digest
, sigopts
))
1045 X509_STORE_CTX_free(xsc
);
1047 ERR_print_errors(bio_err
);
1049 ASN1_INTEGER_free(bs
);
1053 static int callb(int ok
, X509_STORE_CTX
*ctx
)
1059 * It is ok to use a self-signed certificate. This case will catch both
1060 * the initial ok == 0 and the final ok == 1 calls to this function.
1062 err
= X509_STORE_CTX_get_error(ctx
);
1063 if (err
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
)
1067 * BAD we should have gotten an error. Normally if everything worked
1068 * X509_STORE_CTX_get_error(ctx) will still be set to
1069 * DEPTH_ZERO_SELF_....
1073 "error with certificate to be certified - should be self-signed\n");
1076 err_cert
= X509_STORE_CTX_get_current_cert(ctx
);
1077 print_name(bio_err
, NULL
, X509_get_subject_name(err_cert
), 0);
1079 "error with certificate - error %d at depth %d\n%s\n", err
,
1080 X509_STORE_CTX_get_error_depth(ctx
),
1081 X509_verify_cert_error_string(err
));
1086 /* self-issue; self-sign unless a forced public key (fkey) is given */
1087 static int sign(X509
*x
, EVP_PKEY
*pkey
, EVP_PKEY
*fkey
,
1088 STACK_OF(OPENSSL_STRING
) *sigopts
,
1089 int days
, int clrext
,
1090 const EVP_MD
*digest
, CONF
*conf
, const char *section
,
1093 if (!X509_set_issuer_name(x
, X509_get_subject_name(x
)))
1095 if (!preserve_dates
&& !set_cert_times(x
, NULL
, NULL
, days
))
1097 if (fkey
== NULL
&& !X509_set_pubkey(x
, pkey
))
1100 while (X509_get_ext_count(x
) > 0)
1101 X509_delete_ext(x
, 0);
1105 X509_set_version(x
, 2); /* version 3 certificate */
1106 X509V3_set_ctx(&ctx
, x
, x
, NULL
, NULL
, 0);
1107 X509V3_set_nconf(&ctx
, conf
);
1108 if (!X509V3_EXT_add_nconf(conf
, &ctx
, section
, x
))
1111 if (!do_X509_sign(x
, pkey
, digest
, sigopts
))
1115 ERR_print_errors(bio_err
);
1119 static int purpose_print(BIO
*bio
, X509
*cert
, X509_PURPOSE
*pt
)
1123 id
= X509_PURPOSE_get_id(pt
);
1124 pname
= X509_PURPOSE_get0_name(pt
);
1125 for (i
= 0; i
< 2; i
++) {
1126 idret
= X509_check_purpose(cert
, id
, i
);
1127 BIO_printf(bio
, "%s%s : ", pname
, i
? " CA" : "");
1129 BIO_printf(bio
, "Yes\n");
1130 else if (idret
== 0)
1131 BIO_printf(bio
, "No\n");
1133 BIO_printf(bio
, "Yes (WARNING code=%d)\n", idret
);
1138 static int parse_ext_names(char *names
, const char **result
)
1141 int cnt
= 0, len
= 0;
1144 len
= strlen(names
);
1146 while (q
- names
<= len
) {
1147 if (*q
!= ',' && *q
!= '\0') {
1153 if (result
!= NULL
) {
1165 static int print_x509v3_exts(BIO
*bio
, X509
*x
, const char *ext_names
)
1167 const STACK_OF(X509_EXTENSION
) *exts
= NULL
;
1168 STACK_OF(X509_EXTENSION
) *exts2
= NULL
;
1169 X509_EXTENSION
*ext
= NULL
;
1171 int i
, j
, ret
= 0, num
, nn
= 0;
1172 const char *sn
, **names
= NULL
;
1173 char *tmp_ext_names
= NULL
;
1175 exts
= X509_get0_extensions(x
);
1176 if ((num
= sk_X509_EXTENSION_num(exts
)) <= 0) {
1177 BIO_printf(bio
, "No extensions in certificate\n");
1182 /* parse comma separated ext name string */
1183 if ((tmp_ext_names
= OPENSSL_strdup(ext_names
)) == NULL
)
1185 if ((nn
= parse_ext_names(tmp_ext_names
, NULL
)) == 0) {
1186 BIO_printf(bio
, "Invalid extension names: %s\n", ext_names
);
1189 if ((names
= OPENSSL_malloc(sizeof(char *) * nn
)) == NULL
)
1191 parse_ext_names(tmp_ext_names
, names
);
1193 for (i
= 0; i
< num
; i
++) {
1194 ext
= sk_X509_EXTENSION_value(exts
, i
);
1196 /* check if this ext is what we want */
1197 obj
= X509_EXTENSION_get_object(ext
);
1198 sn
= OBJ_nid2sn(OBJ_obj2nid(obj
));
1199 if (sn
== NULL
|| strcmp(sn
, "UNDEF") == 0)
1202 for (j
= 0; j
< nn
; j
++) {
1203 if (strcmp(sn
, names
[j
]) == 0) {
1204 /* push the extension into a new stack */
1206 && (exts2
= sk_X509_EXTENSION_new_null()) == NULL
)
1208 if (!sk_X509_EXTENSION_push(exts2
, ext
))
1214 if (!sk_X509_EXTENSION_num(exts2
)) {
1215 BIO_printf(bio
, "No extensions matched with %s\n", ext_names
);
1220 ret
= X509V3_extensions_print(bio
, NULL
, exts2
, 0, 0);
1222 sk_X509_EXTENSION_free(exts2
);
1223 OPENSSL_free(names
);
1224 OPENSSL_free(tmp_ext_names
);