2 * Copyright 1995-2019 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
17 #include <openssl/bio.h>
18 #include <openssl/evp.h>
19 #include <openssl/conf.h>
20 #include <openssl/err.h>
21 #include <openssl/asn1.h>
22 #include <openssl/x509.h>
23 #include <openssl/x509v3.h>
24 #include <openssl/objects.h>
25 #include <openssl/pem.h>
26 #include <openssl/bn.h>
27 #include <openssl/lhash.h>
28 #ifndef OPENSSL_NO_RSA
29 # include <openssl/rsa.h>
31 #ifndef OPENSSL_NO_DSA
32 # include <openssl/dsa.h>
37 #define BITS "default_bits"
38 #define KEYFILE "default_keyfile"
39 #define PROMPT "prompt"
40 #define DISTINGUISHED_NAME "distinguished_name"
41 #define ATTRIBUTES "attributes"
42 #define V3_EXTENSIONS "x509_extensions"
43 #define REQ_EXTENSIONS "req_extensions"
44 #define STRING_MASK "string_mask"
45 #define UTF8_IN "utf8"
47 #define DEFAULT_KEY_LENGTH 2048
48 #define MIN_KEY_LENGTH 512
50 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *dn
, int mutlirdn
,
51 int attribs
, unsigned long chtype
);
52 static int build_subject(X509_REQ
*req
, const char *subj
, unsigned long chtype
,
54 static int prompt_info(X509_REQ
*req
,
55 STACK_OF(CONF_VALUE
) *dn_sk
, const char *dn_sect
,
56 STACK_OF(CONF_VALUE
) *attr_sk
, const char *attr_sect
,
57 int attribs
, unsigned long chtype
);
58 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *sk
,
59 STACK_OF(CONF_VALUE
) *attr
, int attribs
,
60 unsigned long chtype
);
61 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
62 char *value
, int nid
, int n_min
, int n_max
,
63 unsigned long chtype
);
64 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
65 char *value
, int nid
, int n_min
, int n_max
,
66 unsigned long chtype
, int mval
);
67 static int genpkey_cb(EVP_PKEY_CTX
*ctx
);
68 static int build_data(char *text
, const char *def
,
69 char *value
, int n_min
, int n_max
,
70 char *buf
, const int buf_size
,
71 const char *desc1
, const char *desc2
73 static int req_check_len(int len
, int n_min
, int n_max
);
74 static int check_end(const char *str
, const char *end
);
75 static int join(char buf
[], size_t buf_size
, const char *name
,
76 const char *tail
, const char *desc
);
77 static EVP_PKEY_CTX
*set_keygen_ctx(const char *gstr
,
78 int *pkey_type
, long *pkeylen
,
79 char **palgnam
, ENGINE
*keygen_engine
);
80 static CONF
*req_conf
= NULL
;
81 static CONF
*addext_conf
= NULL
;
84 typedef enum OPTION_choice
{
85 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
86 OPT_INFORM
, OPT_OUTFORM
, OPT_ENGINE
, OPT_KEYGEN_ENGINE
, OPT_KEY
,
87 OPT_PUBKEY
, OPT_NEW
, OPT_CONFIG
, OPT_KEYFORM
, OPT_IN
, OPT_OUT
,
88 OPT_KEYOUT
, OPT_PASSIN
, OPT_PASSOUT
, OPT_NEWKEY
,
89 OPT_PKEYOPT
, OPT_SIGOPT
, OPT_BATCH
, OPT_NEWHDR
, OPT_MODULUS
,
90 OPT_VERIFY
, OPT_NODES
, OPT_NOOUT
, OPT_VERBOSE
, OPT_UTF8
,
91 OPT_NAMEOPT
, OPT_REQOPT
, OPT_SUBJ
, OPT_SUBJECT
, OPT_TEXT
, OPT_X509
,
92 OPT_MULTIVALUE_RDN
, OPT_DAYS
, OPT_SET_SERIAL
, OPT_ADDEXT
, OPT_EXTENSIONS
,
93 OPT_REQEXTS
, OPT_PRECERT
, OPT_MD
,
97 const OPTIONS req_options
[] = {
98 {"help", OPT_HELP
, '-', "Display this summary"},
99 {"inform", OPT_INFORM
, 'F', "Input format - DER or PEM"},
100 {"outform", OPT_OUTFORM
, 'F', "Output format - DER or PEM"},
101 {"in", OPT_IN
, '<', "Input file"},
102 {"out", OPT_OUT
, '>', "Output file"},
103 {"key", OPT_KEY
, 's', "Private key to use"},
104 {"keyform", OPT_KEYFORM
, 'f', "Key file format"},
105 {"pubkey", OPT_PUBKEY
, '-', "Output public key"},
106 {"new", OPT_NEW
, '-', "New request"},
107 {"config", OPT_CONFIG
, '<', "Request template file"},
108 {"keyout", OPT_KEYOUT
, '>', "File to send the key to"},
109 {"passin", OPT_PASSIN
, 's', "Private key password source"},
110 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
112 {"newkey", OPT_NEWKEY
, 's', "Specify as type:bits"},
113 {"pkeyopt", OPT_PKEYOPT
, 's', "Public key options as opt:value"},
114 {"sigopt", OPT_SIGOPT
, 's', "Signature parameter in n:v form"},
115 {"batch", OPT_BATCH
, '-',
116 "Do not ask anything during request generation"},
117 {"newhdr", OPT_NEWHDR
, '-', "Output \"NEW\" in the header lines"},
118 {"modulus", OPT_MODULUS
, '-', "RSA modulus"},
119 {"verify", OPT_VERIFY
, '-', "Verify signature on REQ"},
120 {"nodes", OPT_NODES
, '-', "Don't encrypt the output key"},
121 {"noout", OPT_NOOUT
, '-', "Do not output REQ"},
122 {"verbose", OPT_VERBOSE
, '-', "Verbose output"},
123 {"utf8", OPT_UTF8
, '-', "Input characters are UTF8 (default ASCII)"},
124 {"nameopt", OPT_NAMEOPT
, 's', "Various certificate name options"},
125 {"reqopt", OPT_REQOPT
, 's', "Various request text options"},
126 {"text", OPT_TEXT
, '-', "Text form of request"},
127 {"x509", OPT_X509
, '-',
128 "Output a x509 structure instead of a cert request"},
129 {OPT_MORE_STR
, 1, 1, "(Required by some CA's)"},
130 {"subj", OPT_SUBJ
, 's', "Set or modify request subject"},
131 {"subject", OPT_SUBJECT
, '-', "Output the request's subject"},
132 {"multivalue-rdn", OPT_MULTIVALUE_RDN
, '-',
133 "Enable support for multivalued RDNs"},
134 {"days", OPT_DAYS
, 'p', "Number of days cert is valid for"},
135 {"set_serial", OPT_SET_SERIAL
, 's', "Serial number to use"},
136 {"addext", OPT_ADDEXT
, 's',
137 "Additional cert extension key=value pair (may be given more than once)"},
138 {"extensions", OPT_EXTENSIONS
, 's',
139 "Cert extension section (override value in config file)"},
140 {"reqexts", OPT_REQEXTS
, 's',
141 "Request extension section (override value in config file)"},
142 {"precert", OPT_PRECERT
, '-', "Add a poison extension (implies -new)"},
143 {"", OPT_MD
, '-', "Any supported digest"},
144 #ifndef OPENSSL_NO_ENGINE
145 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
146 {"keygen_engine", OPT_KEYGEN_ENGINE
, 's',
147 "Specify engine to be used for key generation operations"},
154 * An LHASH of strings, where each string is an extension name.
156 static unsigned long ext_name_hash(const OPENSSL_STRING
*a
)
158 return OPENSSL_LH_strhash((const char *)a
);
161 static int ext_name_cmp(const OPENSSL_STRING
*a
, const OPENSSL_STRING
*b
)
163 return strcmp((const char *)a
, (const char *)b
);
166 static void exts_cleanup(OPENSSL_STRING
*x
)
168 OPENSSL_free((char *)x
);
172 * Is the |kv| key already duplicated? This is remarkably tricky to get
173 * right. Return 0 if unique, -1 on runtime error; 1 if found or a syntax
176 static int duplicated(LHASH_OF(OPENSSL_STRING
) *addexts
, char *kv
)
182 /* Skip leading whitespace, make a copy. */
183 while (*kv
&& isspace(*kv
))
186 if ((p
= strchr(kv
, '=')) == NULL
)
189 if ((kv
= OPENSSL_strdup(kv
)) == NULL
)
192 /* Skip trailing space before the equal sign. */
193 for (p
= kv
+ off
; p
> kv
; --p
)
202 /* Finally have a clean "key"; see if it's there [by attempt to add it]. */
203 if ((p
= (char *)lh_OPENSSL_STRING_insert(addexts
, (OPENSSL_STRING
*)kv
))
204 != NULL
|| lh_OPENSSL_STRING_error(addexts
)) {
205 OPENSSL_free(p
!= NULL
? p
: kv
);
212 int req_main(int argc
, char **argv
)
214 ASN1_INTEGER
*serial
= NULL
;
215 BIO
*in
= NULL
, *out
= NULL
;
216 ENGINE
*e
= NULL
, *gen_eng
= NULL
;
217 EVP_PKEY
*pkey
= NULL
;
218 EVP_PKEY_CTX
*genctx
= NULL
;
219 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
, *sigopts
= NULL
;
220 LHASH_OF(OPENSSL_STRING
) *addexts
= NULL
;
222 X509_REQ
*req
= NULL
;
223 const EVP_CIPHER
*cipher
= NULL
;
224 const EVP_MD
*md_alg
= NULL
, *digest
= NULL
;
225 BIO
*addext_bio
= NULL
;
226 char *extensions
= NULL
, *infile
= NULL
;
227 char *outfile
= NULL
, *keyfile
= NULL
;
228 char *keyalgstr
= NULL
, *p
, *prog
, *passargin
= NULL
, *passargout
= NULL
;
229 char *passin
= NULL
, *passout
= NULL
;
230 char *nofree_passin
= NULL
, *nofree_passout
= NULL
;
231 char *req_exts
= NULL
, *subj
= NULL
;
232 char *template = default_config_file
, *keyout
= NULL
;
233 const char *keyalg
= NULL
;
235 int ret
= 1, x509
= 0, days
= 0, i
= 0, newreq
= 0, verbose
= 0;
236 int pkey_type
= -1, private = 0;
237 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, keyform
= FORMAT_PEM
;
238 int modulus
= 0, multirdn
= 0, verify
= 0, noout
= 0, text
= 0;
239 int nodes
= 0, newhdr
= 0, subject
= 0, pubkey
= 0, precert
= 0;
241 unsigned long chtype
= MBSTRING_ASC
, reqflag
= 0;
243 #ifndef OPENSSL_NO_DES
244 cipher
= EVP_des_ede3_cbc();
247 prog
= opt_init(argc
, argv
, req_options
);
248 while ((o
= opt_next()) != OPT_EOF
) {
253 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
256 opt_help(req_options
);
260 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
264 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
268 e
= setup_engine(opt_arg(), 0);
270 case OPT_KEYGEN_ENGINE
:
271 #ifndef OPENSSL_NO_ENGINE
272 gen_eng
= ENGINE_by_id(opt_arg());
273 if (gen_eng
== NULL
) {
274 BIO_printf(bio_err
, "Can't find keygen engine %s\n", *argv
);
289 template = opt_arg();
292 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
305 passargin
= opt_arg();
308 passargout
= opt_arg();
320 pkeyopts
= sk_OPENSSL_STRING_new_null();
321 if (!pkeyopts
|| !sk_OPENSSL_STRING_push(pkeyopts
, opt_arg()))
326 sigopts
= sk_OPENSSL_STRING_new_null();
327 if (!sigopts
|| !sk_OPENSSL_STRING_push(sigopts
, opt_arg()))
352 chtype
= MBSTRING_UTF8
;
355 if (!set_nameopt(opt_arg()))
359 if (!set_cert_ex(&reqflag
, opt_arg()))
369 days
= atoi(opt_arg());
372 if (serial
!= NULL
) {
373 BIO_printf(bio_err
, "Serial number supplied twice\n");
376 serial
= s2i_ASN1_INTEGER(NULL
, opt_arg());
386 case OPT_MULTIVALUE_RDN
:
391 if (addexts
== NULL
) {
392 addexts
= lh_OPENSSL_STRING_new(ext_name_hash
, ext_name_cmp
);
393 addext_bio
= BIO_new(BIO_s_mem());
394 if (addexts
== NULL
|| addext_bio
== NULL
)
397 i
= duplicated(addexts
, p
);
400 if (i
< 0 || BIO_printf(addext_bio
, "%s\n", opt_arg()) < 0)
404 extensions
= opt_arg();
407 req_exts
= opt_arg();
410 newreq
= precert
= 1;
413 if (!opt_md(opt_unknown(), &md_alg
))
419 argc
= opt_num_rest();
424 BIO_printf(bio_err
, "Ignoring -days; not generating a certificate\n");
425 if (x509
&& infile
== NULL
)
428 /* TODO: simplify this as pkey is still always NULL here */
429 private = newreq
&& (pkey
== NULL
) ? 1 : 0;
431 if (!app_passwd(passargin
, passargout
, &passin
, &passout
)) {
432 BIO_printf(bio_err
, "Error getting passwords\n");
437 BIO_printf(bio_err
, "Using configuration from %s\n", template);
438 if ((req_conf
= app_load_config(template)) == NULL
)
443 "Using additional configuration from command line\n");
444 if ((addext_conf
= app_load_config_bio(addext_bio
, NULL
)) == NULL
)
447 if (template != default_config_file
&& !app_load_modules(req_conf
))
450 if (req_conf
!= NULL
) {
451 p
= NCONF_get_string(req_conf
, NULL
, "oid_file");
457 oid_bio
= BIO_new_file(p
, "r");
458 if (oid_bio
== NULL
) {
460 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
461 ERR_print_errors(bio_err);
464 OBJ_create_objects(oid_bio
);
469 if (!add_oid_section(req_conf
))
472 if (md_alg
== NULL
) {
473 p
= NCONF_get_string(req_conf
, SECTION
, "default_md");
477 if (!opt_md(p
, &md_alg
))
483 if (extensions
== NULL
) {
484 extensions
= NCONF_get_string(req_conf
, SECTION
, V3_EXTENSIONS
);
485 if (extensions
== NULL
)
488 if (extensions
!= NULL
) {
489 /* Check syntax of file */
491 X509V3_set_ctx_test(&ctx
);
492 X509V3_set_nconf(&ctx
, req_conf
);
493 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, extensions
, NULL
)) {
495 "Error Loading extension section %s\n", extensions
);
499 if (addext_conf
!= NULL
) {
500 /* Check syntax of command line extensions */
502 X509V3_set_ctx_test(&ctx
);
503 X509V3_set_nconf(&ctx
, addext_conf
);
504 if (!X509V3_EXT_add_nconf(addext_conf
, &ctx
, "default", NULL
)) {
505 BIO_printf(bio_err
, "Error Loading command line extensions\n");
510 if (passin
== NULL
) {
511 passin
= nofree_passin
=
512 NCONF_get_string(req_conf
, SECTION
, "input_password");
517 if (passout
== NULL
) {
518 passout
= nofree_passout
=
519 NCONF_get_string(req_conf
, SECTION
, "output_password");
524 p
= NCONF_get_string(req_conf
, SECTION
, STRING_MASK
);
528 if (p
!= NULL
&& !ASN1_STRING_set_default_mask_asc(p
)) {
529 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", p
);
533 if (chtype
!= MBSTRING_UTF8
) {
534 p
= NCONF_get_string(req_conf
, SECTION
, UTF8_IN
);
537 else if (strcmp(p
, "yes") == 0)
538 chtype
= MBSTRING_UTF8
;
541 if (req_exts
== NULL
) {
542 req_exts
= NCONF_get_string(req_conf
, SECTION
, REQ_EXTENSIONS
);
543 if (req_exts
== NULL
)
546 if (req_exts
!= NULL
) {
547 /* Check syntax of file */
549 X509V3_set_ctx_test(&ctx
);
550 X509V3_set_nconf(&ctx
, req_conf
);
551 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, req_exts
, NULL
)) {
553 "Error Loading request extension section %s\n",
559 if (keyfile
!= NULL
) {
560 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "Private Key");
562 /* load_key() has already printed an appropriate message */
565 app_RAND_load_conf(req_conf
, SECTION
);
569 if (newreq
&& (pkey
== NULL
)) {
570 app_RAND_load_conf(req_conf
, SECTION
);
572 if (!NCONF_get_number(req_conf
, SECTION
, BITS
, &newkey
)) {
573 newkey
= DEFAULT_KEY_LENGTH
;
576 if (keyalg
!= NULL
) {
577 genctx
= set_keygen_ctx(keyalg
, &pkey_type
, &newkey
,
578 &keyalgstr
, gen_eng
);
583 if (newkey
< MIN_KEY_LENGTH
584 && (pkey_type
== EVP_PKEY_RSA
|| pkey_type
== EVP_PKEY_DSA
)) {
585 BIO_printf(bio_err
, "private key length is too short,\n");
586 BIO_printf(bio_err
, "it needs to be at least %d bits, not %ld\n",
587 MIN_KEY_LENGTH
, newkey
);
591 if (pkey_type
== EVP_PKEY_RSA
&& newkey
> OPENSSL_RSA_MAX_MODULUS_BITS
)
593 "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
594 " Your key size is %ld! Larger key size may behave not as expected.\n",
595 OPENSSL_RSA_MAX_MODULUS_BITS
, newkey
);
597 #ifndef OPENSSL_NO_DSA
598 if (pkey_type
== EVP_PKEY_DSA
&& newkey
> OPENSSL_DSA_MAX_MODULUS_BITS
)
600 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
601 " Your key size is %ld! Larger key size may behave not as expected.\n",
602 OPENSSL_DSA_MAX_MODULUS_BITS
, newkey
);
605 if (genctx
== NULL
) {
606 genctx
= set_keygen_ctx(NULL
, &pkey_type
, &newkey
,
607 &keyalgstr
, gen_eng
);
612 if (pkeyopts
!= NULL
) {
614 for (i
= 0; i
< sk_OPENSSL_STRING_num(pkeyopts
); i
++) {
615 genopt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
616 if (pkey_ctrl_string(genctx
, genopt
) <= 0) {
617 BIO_printf(bio_err
, "parameter error \"%s\"\n", genopt
);
618 ERR_print_errors(bio_err
);
624 if (pkey_type
== EVP_PKEY_EC
) {
625 BIO_printf(bio_err
, "Generating an EC private key\n");
627 BIO_printf(bio_err
, "Generating a %s private key\n", keyalgstr
);
630 EVP_PKEY_CTX_set_cb(genctx
, genpkey_cb
);
631 EVP_PKEY_CTX_set_app_data(genctx
, bio_err
);
633 if (EVP_PKEY_keygen(genctx
, &pkey
) <= 0) {
634 BIO_puts(bio_err
, "Error Generating Key\n");
638 EVP_PKEY_CTX_free(genctx
);
641 if (keyout
== NULL
) {
642 keyout
= NCONF_get_string(req_conf
, SECTION
, KEYFILE
);
648 BIO_printf(bio_err
, "writing new private key to stdout\n");
650 BIO_printf(bio_err
, "writing new private key to '%s'\n", keyout
);
651 out
= bio_open_owner(keyout
, outformat
, private);
655 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_rsa_key");
658 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_key");
662 if ((p
!= NULL
) && (strcmp(p
, "no") == 0))
670 if (!PEM_write_bio_PrivateKey(out
, pkey
, cipher
,
671 NULL
, 0, NULL
, passout
)) {
672 if ((ERR_GET_REASON(ERR_peek_error()) ==
673 PEM_R_PROBLEMS_GETTING_PASSWORD
) && (i
< 3)) {
682 BIO_printf(bio_err
, "-----\n");
686 in
= bio_open_default(infile
, 'r', informat
);
690 if (informat
== FORMAT_ASN1
)
691 req
= d2i_X509_REQ_bio(in
, NULL
);
693 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
695 BIO_printf(bio_err
, "unable to load X509 request\n");
700 if (newreq
|| x509
) {
702 BIO_printf(bio_err
, "you need to specify a private key\n");
707 req
= X509_REQ_new();
712 i
= make_REQ(req
, pkey
, subj
, multirdn
, !x509
, chtype
);
713 subj
= NULL
; /* done processing '-subj' option */
715 BIO_printf(bio_err
, "problems making Certificate Request\n");
722 if ((x509ss
= X509_new()) == NULL
)
725 /* Set version to V3 */
726 if ((extensions
!= NULL
|| addext_conf
!= NULL
)
727 && !X509_set_version(x509ss
, 2))
729 if (serial
!= NULL
) {
730 if (!X509_set_serialNumber(x509ss
, serial
))
733 if (!rand_serial(NULL
, X509_get_serialNumber(x509ss
)))
737 if (!X509_set_issuer_name(x509ss
, X509_REQ_get_subject_name(req
)))
740 /* set default days if it's not specified */
743 if (!set_cert_times(x509ss
, NULL
, NULL
, days
))
745 if (!X509_set_subject_name
746 (x509ss
, X509_REQ_get_subject_name(req
)))
748 tmppkey
= X509_REQ_get0_pubkey(req
);
749 if (!tmppkey
|| !X509_set_pubkey(x509ss
, tmppkey
))
752 /* Set up V3 context struct */
754 X509V3_set_ctx(&ext_ctx
, x509ss
, x509ss
, NULL
, NULL
, 0);
755 X509V3_set_nconf(&ext_ctx
, req_conf
);
758 if (extensions
!= NULL
&& !X509V3_EXT_add_nconf(req_conf
,
759 &ext_ctx
, extensions
,
761 BIO_printf(bio_err
, "Error Loading extension section %s\n",
765 if (addext_conf
!= NULL
766 && !X509V3_EXT_add_nconf(addext_conf
, &ext_ctx
, "default",
768 BIO_printf(bio_err
, "Error Loading command line extensions\n");
772 /* If a pre-cert was requested, we need to add a poison extension */
774 if (X509_add1_ext_i2d(x509ss
, NID_ct_precert_poison
, NULL
, 1, 0)
776 BIO_printf(bio_err
, "Error adding poison extension\n");
781 i
= do_X509_sign(x509ss
, pkey
, digest
, sigopts
);
783 ERR_print_errors(bio_err
);
789 /* Set up V3 context struct */
791 X509V3_set_ctx(&ext_ctx
, NULL
, NULL
, req
, NULL
, 0);
792 X509V3_set_nconf(&ext_ctx
, req_conf
);
796 && !X509V3_EXT_REQ_add_nconf(req_conf
, &ext_ctx
,
798 BIO_printf(bio_err
, "Error Loading extension section %s\n",
802 if (addext_conf
!= NULL
803 && !X509V3_EXT_REQ_add_nconf(addext_conf
, &ext_ctx
, "default",
805 BIO_printf(bio_err
, "Error Loading command line extensions\n");
808 i
= do_X509_REQ_sign(req
, pkey
, digest
, sigopts
);
810 ERR_print_errors(bio_err
);
817 BIO_printf(bio_err
, "Cannot modify certificate subject\n");
823 BIO_printf(bio_err
, "Modifying Request's Subject\n");
824 print_name(bio_err
, "old subject=",
825 X509_REQ_get_subject_name(req
), get_nameopt());
828 if (build_subject(req
, subj
, chtype
, multirdn
) == 0) {
829 BIO_printf(bio_err
, "ERROR: cannot modify subject\n");
835 print_name(bio_err
, "new subject=",
836 X509_REQ_get_subject_name(req
), get_nameopt());
840 if (verify
&& !x509
) {
841 EVP_PKEY
*tpubkey
= pkey
;
843 if (tpubkey
== NULL
) {
844 tpubkey
= X509_REQ_get0_pubkey(req
);
849 i
= X509_REQ_verify(req
, tpubkey
);
854 BIO_printf(bio_err
, "verify failure\n");
855 ERR_print_errors(bio_err
);
856 } else { /* if (i > 0) */
857 BIO_printf(bio_err
, "verify OK\n");
861 if (noout
&& !text
&& !modulus
&& !subject
&& !pubkey
) {
866 out
= bio_open_default(outfile
,
867 keyout
!= NULL
&& outfile
!= NULL
&&
868 strcmp(keyout
, outfile
) == 0 ? 'a' : 'w',
874 EVP_PKEY
*tpubkey
= X509_REQ_get0_pubkey(req
);
876 if (tpubkey
== NULL
) {
877 BIO_printf(bio_err
, "Error getting public key\n");
878 ERR_print_errors(bio_err
);
881 PEM_write_bio_PUBKEY(out
, tpubkey
);
886 ret
= X509_print_ex(out
, x509ss
, get_nameopt(), reqflag
);
888 ret
= X509_REQ_print_ex(out
, req
, get_nameopt(), reqflag
);
892 BIO_printf(bio_err
, "Error printing certificate\n");
894 BIO_printf(bio_err
, "Error printing certificate request\n");
896 ERR_print_errors(bio_err
);
903 print_name(out
, "subject=", X509_get_subject_name(x509ss
),
906 print_name(out
, "subject=", X509_REQ_get_subject_name(req
),
914 tpubkey
= X509_get0_pubkey(x509ss
);
916 tpubkey
= X509_REQ_get0_pubkey(req
);
917 if (tpubkey
== NULL
) {
918 fprintf(stdout
, "Modulus=unavailable\n");
921 fprintf(stdout
, "Modulus=");
922 #ifndef OPENSSL_NO_RSA
923 if (EVP_PKEY_base_id(tpubkey
) == EVP_PKEY_RSA
) {
925 RSA_get0_key(EVP_PKEY_get0_RSA(tpubkey
), &n
, NULL
, NULL
);
929 fprintf(stdout
, "Wrong Algorithm type");
930 fprintf(stdout
, "\n");
933 if (!noout
&& !x509
) {
934 if (outformat
== FORMAT_ASN1
)
935 i
= i2d_X509_REQ_bio(out
, req
);
937 i
= PEM_write_bio_X509_REQ_NEW(out
, req
);
939 i
= PEM_write_bio_X509_REQ(out
, req
);
941 BIO_printf(bio_err
, "unable to write X509 request\n");
945 if (!noout
&& x509
&& (x509ss
!= NULL
)) {
946 if (outformat
== FORMAT_ASN1
)
947 i
= i2d_X509_bio(out
, x509ss
);
949 i
= PEM_write_bio_X509(out
, x509ss
);
951 BIO_printf(bio_err
, "unable to write X509 certificate\n");
958 ERR_print_errors(bio_err
);
960 NCONF_free(req_conf
);
961 NCONF_free(addext_conf
);
962 BIO_free(addext_bio
);
966 EVP_PKEY_CTX_free(genctx
);
967 sk_OPENSSL_STRING_free(pkeyopts
);
968 sk_OPENSSL_STRING_free(sigopts
);
969 lh_OPENSSL_STRING_doall(addexts
, exts_cleanup
);
970 lh_OPENSSL_STRING_free(addexts
);
971 #ifndef OPENSSL_NO_ENGINE
972 ENGINE_free(gen_eng
);
974 OPENSSL_free(keyalgstr
);
977 ASN1_INTEGER_free(serial
);
979 if (passin
!= nofree_passin
)
980 OPENSSL_free(passin
);
981 if (passout
!= nofree_passout
)
982 OPENSSL_free(passout
);
986 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *subj
, int multirdn
,
987 int attribs
, unsigned long chtype
)
991 STACK_OF(CONF_VALUE
) *dn_sk
, *attr_sk
= NULL
;
992 char *tmp
, *dn_sect
, *attr_sect
;
994 tmp
= NCONF_get_string(req_conf
, SECTION
, PROMPT
);
997 if ((tmp
!= NULL
) && strcmp(tmp
, "no") == 0)
1000 dn_sect
= NCONF_get_string(req_conf
, SECTION
, DISTINGUISHED_NAME
);
1001 if (dn_sect
== NULL
) {
1002 BIO_printf(bio_err
, "unable to find '%s' in config\n",
1003 DISTINGUISHED_NAME
);
1006 dn_sk
= NCONF_get_section(req_conf
, dn_sect
);
1007 if (dn_sk
== NULL
) {
1008 BIO_printf(bio_err
, "unable to get '%s' section\n", dn_sect
);
1012 attr_sect
= NCONF_get_string(req_conf
, SECTION
, ATTRIBUTES
);
1013 if (attr_sect
== NULL
) {
1017 attr_sk
= NCONF_get_section(req_conf
, attr_sect
);
1018 if (attr_sk
== NULL
) {
1019 BIO_printf(bio_err
, "unable to get '%s' section\n", attr_sect
);
1024 /* setup version number */
1025 if (!X509_REQ_set_version(req
, 0L))
1026 goto err
; /* version 1 */
1029 i
= build_subject(req
, subj
, chtype
, multirdn
);
1031 i
= auto_info(req
, dn_sk
, attr_sk
, attribs
, chtype
);
1033 i
= prompt_info(req
, dn_sk
, dn_sect
, attr_sk
, attr_sect
, attribs
,
1038 if (!X509_REQ_set_pubkey(req
, pkey
))
1047 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
1048 * where characters may be escaped by \
1050 static int build_subject(X509_REQ
*req
, const char *subject
, unsigned long chtype
,
1055 if ((n
= parse_name(subject
, chtype
, multirdn
)) == NULL
)
1058 if (!X509_REQ_set_subject_name(req
, n
)) {
1066 static int prompt_info(X509_REQ
*req
,
1067 STACK_OF(CONF_VALUE
) *dn_sk
, const char *dn_sect
,
1068 STACK_OF(CONF_VALUE
) *attr_sk
, const char *attr_sect
,
1069 int attribs
, unsigned long chtype
)
1080 subj
= X509_REQ_get_subject_name(req
);
1084 "You are about to be asked to enter information that will be incorporated\n");
1085 BIO_printf(bio_err
, "into your certificate request.\n");
1087 "What you are about to enter is what is called a Distinguished Name or a DN.\n");
1089 "There are quite a few fields but you can leave some blank\n");
1091 "For some fields there will be a default value,\n");
1093 "If you enter '.', the field will be left blank.\n");
1094 BIO_printf(bio_err
, "-----\n");
1097 if (sk_CONF_VALUE_num(dn_sk
)) {
1102 if (sk_CONF_VALUE_num(dn_sk
) <= i
)
1105 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1108 if (!check_end(type
, "_min") || !check_end(type
, "_max") ||
1109 !check_end(type
, "_default") || !check_end(type
, "_value"))
1112 * Skip past any leading X. X: X, etc to allow for multiple
1115 for (p
= v
->name
; *p
; p
++)
1116 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1128 /* If OBJ not recognised ignore it */
1129 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1131 if (!join(buf
, sizeof(buf
), v
->name
, "_default", "Name"))
1133 if ((def
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1138 if (!join(buf
, sizeof(buf
), v
->name
, "_value", "Name"))
1140 if ((value
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1145 if (!join(buf
, sizeof(buf
), v
->name
, "_min", "Name"))
1147 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_min
)) {
1153 if (!join(buf
, sizeof(buf
), v
->name
, "_max", "Name"))
1155 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_max
)) {
1160 if (!add_DN_object(subj
, v
->value
, def
, value
, nid
,
1161 n_min
, n_max
, chtype
, mval
))
1164 if (X509_NAME_entry_count(subj
) == 0) {
1166 "error, no objects specified in config file\n");
1171 if ((attr_sk
!= NULL
) && (sk_CONF_VALUE_num(attr_sk
) > 0)
1174 "\nPlease enter the following 'extra' attributes\n");
1176 "to be sent with your certificate request\n");
1183 if ((attr_sk
== NULL
) || (sk_CONF_VALUE_num(attr_sk
) <= i
))
1186 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1188 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1191 if (!join(buf
, sizeof(buf
), type
, "_default", "Name"))
1193 if ((def
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1199 if (!join(buf
, sizeof(buf
), type
, "_value", "Name"))
1201 if ((value
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1207 if (!join(buf
, sizeof(buf
), type
,"_min", "Name"))
1209 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_min
)) {
1214 if (!join(buf
, sizeof(buf
), type
, "_max", "Name"))
1216 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_max
)) {
1221 if (!add_attribute_object(req
,
1222 v
->value
, def
, value
, nid
, n_min
,
1228 BIO_printf(bio_err
, "No template, please set one up.\n");
1236 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *dn_sk
,
1237 STACK_OF(CONF_VALUE
) *attr_sk
, int attribs
,
1238 unsigned long chtype
)
1240 int i
, spec_char
, plus_char
;
1246 subj
= X509_REQ_get_subject_name(req
);
1248 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1250 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1254 * Skip past any leading X. X: X, etc to allow for multiple instances
1256 for (p
= v
->name
; *p
; p
++) {
1257 #ifndef CHARSET_EBCDIC
1258 spec_char
= ((*p
== ':') || (*p
== ',') || (*p
== '.'));
1260 spec_char
= ((*p
== os_toascii
[':']) || (*p
== os_toascii
[','])
1261 || (*p
== os_toascii
['.']));
1270 #ifndef CHARSET_EBCDIC
1271 plus_char
= (*type
== '+');
1273 plus_char
= (*type
== os_toascii
['+']);
1281 if (!X509_NAME_add_entry_by_txt(subj
, type
, chtype
,
1282 (unsigned char *)v
->value
, -1, -1,
1288 if (!X509_NAME_entry_count(subj
)) {
1289 BIO_printf(bio_err
, "error, no objects specified in config file\n");
1293 for (i
= 0; i
< sk_CONF_VALUE_num(attr_sk
); i
++) {
1294 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1295 if (!X509_REQ_add1_attr_by_txt(req
, v
->name
, chtype
,
1296 (unsigned char *)v
->value
, -1))
1303 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
1304 char *value
, int nid
, int n_min
, int n_max
,
1305 unsigned long chtype
, int mval
)
1310 ret
= build_data(text
, def
, value
, n_min
, n_max
, buf
, sizeof(buf
),
1311 "DN value", "DN default");
1312 if ((ret
== 0) || (ret
== 1))
1316 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
1317 (unsigned char *)buf
, -1, -1, mval
))
1323 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
1324 char *value
, int nid
, int n_min
,
1325 int n_max
, unsigned long chtype
)
1330 ret
= build_data(text
, def
, value
, n_min
, n_max
, buf
, sizeof(buf
),
1331 "Attribute value", "Attribute default");
1332 if ((ret
== 0) || (ret
== 1))
1336 if (!X509_REQ_add1_attr_by_NID(req
, nid
, chtype
,
1337 (unsigned char *)buf
, -1)) {
1338 BIO_printf(bio_err
, "Error adding attribute\n");
1339 ERR_print_errors(bio_err
);
1347 static int build_data(char *text
, const char *def
,
1348 char *value
, int n_min
, int n_max
,
1349 char *buf
, const int buf_size
,
1350 const char *desc1
, const char *desc2
1356 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1357 (void)BIO_flush(bio_err
);
1358 if (value
!= NULL
) {
1359 if (!join(buf
, buf_size
, value
, "\n", desc1
))
1361 BIO_printf(bio_err
, "%s\n", value
);
1365 if (!fgets(buf
, buf_size
, stdin
))
1375 if (buf
[0] == '\n') {
1376 if ((def
== NULL
) || (def
[0] == '\0'))
1378 if (!join(buf
, buf_size
, def
, "\n", desc2
))
1380 } else if ((buf
[0] == '.') && (buf
[1] == '\n')) {
1385 if (buf
[i
- 1] != '\n') {
1386 BIO_printf(bio_err
, "weird input :-(\n");
1390 #ifdef CHARSET_EBCDIC
1391 ebcdic2ascii(buf
, buf
, i
);
1393 if (!req_check_len(i
, n_min
, n_max
)) {
1401 static int req_check_len(int len
, int n_min
, int n_max
)
1403 if ((n_min
> 0) && (len
< n_min
)) {
1405 "string is too short, it needs to be at least %d bytes long\n",
1409 if ((n_max
>= 0) && (len
> n_max
)) {
1411 "string is too long, it needs to be no more than %d bytes long\n",
1418 /* Check if the end of a string matches 'end' */
1419 static int check_end(const char *str
, const char *end
)
1428 tmp
= str
+ slen
- elen
;
1429 return strcmp(tmp
, end
);
1433 * Merge the two strings together into the result buffer checking for
1434 * overflow and producing an error message if there is.
1436 static int join(char buf
[], size_t buf_size
, const char *name
,
1437 const char *tail
, const char *desc
)
1439 const size_t name_len
= strlen(name
), tail_len
= strlen(tail
);
1441 if (name_len
+ tail_len
+ 1 > buf_size
) {
1442 BIO_printf(bio_err
, "%s '%s' too long\n", desc
, name
);
1445 memcpy(buf
, name
, name_len
);
1446 memcpy(buf
+ name_len
, tail
, tail_len
+ 1);
1450 static EVP_PKEY_CTX
*set_keygen_ctx(const char *gstr
,
1451 int *pkey_type
, long *pkeylen
,
1452 char **palgnam
, ENGINE
*keygen_engine
)
1454 EVP_PKEY_CTX
*gctx
= NULL
;
1455 EVP_PKEY
*param
= NULL
;
1458 const char *paramfile
= NULL
;
1461 *pkey_type
= EVP_PKEY_RSA
;
1463 } else if (gstr
[0] >= '0' && gstr
[0] <= '9') {
1464 *pkey_type
= EVP_PKEY_RSA
;
1465 keylen
= atol(gstr
);
1467 } else if (strncmp(gstr
, "param:", 6) == 0) {
1468 paramfile
= gstr
+ 6;
1470 const char *p
= strchr(gstr
, ':');
1473 const EVP_PKEY_ASN1_METHOD
*ameth
;
1480 * The lookup of a the string will cover all engines so keep a note
1481 * of the implementation.
1484 ameth
= EVP_PKEY_asn1_find_str(&tmpeng
, gstr
, len
);
1486 if (ameth
== NULL
) {
1487 BIO_printf(bio_err
, "Unknown algorithm %.*s\n", len
, gstr
);
1491 EVP_PKEY_asn1_get0_info(NULL
, pkey_type
, NULL
, NULL
, NULL
, ameth
);
1492 #ifndef OPENSSL_NO_ENGINE
1493 ENGINE_finish(tmpeng
);
1495 if (*pkey_type
== EVP_PKEY_RSA
) {
1497 keylen
= atol(p
+ 1);
1502 } else if (p
!= NULL
) {
1507 if (paramfile
!= NULL
) {
1508 pbio
= BIO_new_file(paramfile
, "r");
1510 BIO_printf(bio_err
, "Can't open parameter file %s\n", paramfile
);
1513 param
= PEM_read_bio_Parameters(pbio
, NULL
);
1515 if (param
== NULL
) {
1518 (void)BIO_reset(pbio
);
1519 x
= PEM_read_bio_X509(pbio
, NULL
, NULL
, NULL
);
1521 param
= X509_get_pubkey(x
);
1528 if (param
== NULL
) {
1529 BIO_printf(bio_err
, "Error reading parameter file %s\n", paramfile
);
1532 if (*pkey_type
== -1) {
1533 *pkey_type
= EVP_PKEY_id(param
);
1534 } else if (*pkey_type
!= EVP_PKEY_base_id(param
)) {
1535 BIO_printf(bio_err
, "Key Type does not match parameters\n");
1536 EVP_PKEY_free(param
);
1541 if (palgnam
!= NULL
) {
1542 const EVP_PKEY_ASN1_METHOD
*ameth
;
1546 ameth
= EVP_PKEY_asn1_find(&tmpeng
, *pkey_type
);
1547 if (ameth
== NULL
) {
1548 BIO_puts(bio_err
, "Internal error: can't find key algorithm\n");
1551 EVP_PKEY_asn1_get0_info(NULL
, NULL
, NULL
, NULL
, &anam
, ameth
);
1552 *palgnam
= OPENSSL_strdup(anam
);
1553 #ifndef OPENSSL_NO_ENGINE
1554 ENGINE_finish(tmpeng
);
1558 if (param
!= NULL
) {
1559 gctx
= EVP_PKEY_CTX_new(param
, keygen_engine
);
1560 *pkeylen
= EVP_PKEY_bits(param
);
1561 EVP_PKEY_free(param
);
1563 gctx
= EVP_PKEY_CTX_new_id(*pkey_type
, keygen_engine
);
1567 BIO_puts(bio_err
, "Error allocating keygen context\n");
1568 ERR_print_errors(bio_err
);
1572 if (EVP_PKEY_keygen_init(gctx
) <= 0) {
1573 BIO_puts(bio_err
, "Error initializing keygen context\n");
1574 ERR_print_errors(bio_err
);
1575 EVP_PKEY_CTX_free(gctx
);
1578 #ifndef OPENSSL_NO_RSA
1579 if ((*pkey_type
== EVP_PKEY_RSA
) && (keylen
!= -1)) {
1580 if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx
, keylen
) <= 0) {
1581 BIO_puts(bio_err
, "Error setting RSA keysize\n");
1582 ERR_print_errors(bio_err
);
1583 EVP_PKEY_CTX_free(gctx
);
1592 static int genpkey_cb(EVP_PKEY_CTX
*ctx
)
1595 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
1597 p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);
1606 BIO_write(b
, &c
, 1);
1611 static int do_sign_init(EVP_MD_CTX
*ctx
, EVP_PKEY
*pkey
,
1612 const EVP_MD
*md
, STACK_OF(OPENSSL_STRING
) *sigopts
)
1614 EVP_PKEY_CTX
*pkctx
= NULL
;
1620 * EVP_PKEY_get_default_digest_nid() returns 2 if the digest is mandatory
1621 * for this algorithm.
1623 if (EVP_PKEY_get_default_digest_nid(pkey
, &def_nid
) == 2
1624 && def_nid
== NID_undef
) {
1625 /* The signing algorithm requires there to be no digest */
1628 if (!EVP_DigestSignInit(ctx
, &pkctx
, md
, NULL
, pkey
))
1630 for (i
= 0; i
< sk_OPENSSL_STRING_num(sigopts
); i
++) {
1631 char *sigopt
= sk_OPENSSL_STRING_value(sigopts
, i
);
1632 if (pkey_ctrl_string(pkctx
, sigopt
) <= 0) {
1633 BIO_printf(bio_err
, "parameter error \"%s\"\n", sigopt
);
1634 ERR_print_errors(bio_err
);
1641 int do_X509_sign(X509
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1642 STACK_OF(OPENSSL_STRING
) *sigopts
)
1645 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
1647 rv
= do_sign_init(mctx
, pkey
, md
, sigopts
);
1649 rv
= X509_sign_ctx(x
, mctx
);
1650 EVP_MD_CTX_free(mctx
);
1651 return rv
> 0 ? 1 : 0;
1654 int do_X509_REQ_sign(X509_REQ
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1655 STACK_OF(OPENSSL_STRING
) *sigopts
)
1658 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
1659 rv
= do_sign_init(mctx
, pkey
, md
, sigopts
);
1661 rv
= X509_REQ_sign_ctx(x
, mctx
);
1662 EVP_MD_CTX_free(mctx
);
1663 return rv
> 0 ? 1 : 0;
1666 int do_X509_CRL_sign(X509_CRL
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1667 STACK_OF(OPENSSL_STRING
) *sigopts
)
1670 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
1671 rv
= do_sign_init(mctx
, pkey
, md
, sigopts
);
1673 rv
= X509_CRL_sign_ctx(x
, mctx
);
1674 EVP_MD_CTX_free(mctx
);
1675 return rv
> 0 ? 1 : 0;