2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
60 * Until the key-gen callbacks are modified to use newer prototypes, we allow
61 * deprecated functions for openssl-internal code
63 #ifdef OPENSSL_NO_DEPRECATED
64 # undef OPENSSL_NO_DEPRECATED
71 #ifdef OPENSSL_NO_STDIO
75 #include <openssl/bio.h>
76 #include <openssl/evp.h>
77 #include <openssl/conf.h>
78 #include <openssl/err.h>
79 #include <openssl/asn1.h>
80 #include <openssl/x509.h>
81 #include <openssl/x509v3.h>
82 #include <openssl/objects.h>
83 #include <openssl/pem.h>
84 #include <openssl/bn.h>
85 #ifndef OPENSSL_NO_RSA
86 # include <openssl/rsa.h>
88 #ifndef OPENSSL_NO_DSA
89 # include <openssl/dsa.h>
94 #define BITS "default_bits"
95 #define KEYFILE "default_keyfile"
96 #define PROMPT "prompt"
97 #define DISTINGUISHED_NAME "distinguished_name"
98 #define ATTRIBUTES "attributes"
99 #define V3_EXTENSIONS "x509_extensions"
100 #define REQ_EXTENSIONS "req_extensions"
101 #define STRING_MASK "string_mask"
102 #define UTF8_IN "utf8"
104 #define DEFAULT_KEY_LENGTH 512
105 #define MIN_KEY_LENGTH 384
108 #define PROG req_main
111 * -inform arg - input format - default PEM (DER or PEM)
112 * -outform arg - output format - default PEM
113 * -in arg - input file - default stdin
114 * -out arg - output file - default stdout
115 * -verify - check request signature
116 * -noout - don't print stuff out.
117 * -text - print out human readable text.
118 * -nodes - no des encryption
119 * -config file - Load configuration file.
120 * -key file - make a request using key in file (or use it for verification).
121 * -keyform arg - key file format.
122 * -rand file(s) - load the file(s) into the PRNG.
123 * -newkey - make a key and a request.
124 * -modulus - print RSA modulus.
125 * -pubkey - output Public Key.
126 * -x509 - output a self signed X509 structure instead.
127 * -asn1-kludge - output new certificate request in a format that some CA's
128 * require. This format is wrong
131 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *dn
, int mutlirdn
,
132 int attribs
, unsigned long chtype
);
133 static int build_subject(X509_REQ
*req
, char *subj
, unsigned long chtype
,
135 static int prompt_info(X509_REQ
*req
,
136 STACK_OF(CONF_VALUE
) *dn_sk
, char *dn_sect
,
137 STACK_OF(CONF_VALUE
) *attr_sk
, char *attr_sect
,
138 int attribs
, unsigned long chtype
);
139 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *sk
,
140 STACK_OF(CONF_VALUE
) *attr
, int attribs
,
141 unsigned long chtype
);
142 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
143 char *value
, int nid
, int n_min
, int n_max
,
144 unsigned long chtype
);
145 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
146 char *value
, int nid
, int n_min
, int n_max
,
147 unsigned long chtype
, int mval
);
148 #ifndef OPENSSL_NO_RSA
149 static int MS_CALLBACK
req_cb(int p
, int n
, BN_GENCB
*cb
);
151 static int req_check_len(int len
, int n_min
, int n_max
);
152 static int check_end(const char *str
, const char *end
);
154 static char *default_config_file
= NULL
;
156 static CONF
*req_conf
= NULL
;
157 static int batch
= 0;
164 int MAIN(int, char **);
166 int MAIN(int argc
, char **argv
)
169 #ifndef OPENSSL_NO_DSA
170 DSA
*dsa_params
= NULL
;
172 #ifndef OPENSSL_NO_ECDSA
173 EC_KEY
*ec_params
= NULL
;
175 unsigned long nmflag
= 0, reqflag
= 0;
176 int ex
= 1, x509
= 0, days
= 30;
178 X509_REQ
*req
= NULL
;
179 EVP_PKEY
*pkey
= NULL
;
180 int i
= 0, badops
= 0, newreq
= 0, verbose
= 0, pkey_type
= TYPE_RSA
;
182 BIO
*in
= NULL
, *out
= NULL
;
183 int informat
, outformat
, verify
= 0, noout
= 0, text
= 0, keyform
=
185 int nodes
= 0, kludge
= 0, newhdr
= 0, subject
= 0, pubkey
= 0;
186 char *infile
, *outfile
, *prog
, *keyfile
= NULL
, *template =
187 NULL
, *keyout
= NULL
;
188 #ifndef OPENSSL_NO_ENGINE
191 char *extensions
= NULL
;
192 char *req_exts
= NULL
;
193 const EVP_CIPHER
*cipher
= NULL
;
194 ASN1_INTEGER
*serial
= NULL
;
197 char *passargin
= NULL
, *passargout
= NULL
;
198 char *passin
= NULL
, *passout
= NULL
;
202 const EVP_MD
*md_alg
= NULL
, *digest
= EVP_sha1();
203 unsigned long chtype
= MBSTRING_ASC
;
210 #ifndef OPENSSL_NO_DES
211 cipher
= EVP_des_ede3_cbc();
216 if ((bio_err
= BIO_new(BIO_s_file())) != NULL
)
217 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
221 informat
= FORMAT_PEM
;
222 outformat
= FORMAT_PEM
;
228 if (strcmp(*argv
, "-inform") == 0) {
231 informat
= str2fmt(*(++argv
));
232 } else if (strcmp(*argv
, "-outform") == 0) {
235 outformat
= str2fmt(*(++argv
));
237 #ifndef OPENSSL_NO_ENGINE
238 else if (strcmp(*argv
, "-engine") == 0) {
244 else if (strcmp(*argv
, "-key") == 0) {
248 } else if (strcmp(*argv
, "-pubkey") == 0) {
250 } else if (strcmp(*argv
, "-new") == 0) {
252 } else if (strcmp(*argv
, "-config") == 0) {
255 template = *(++argv
);
256 } else if (strcmp(*argv
, "-keyform") == 0) {
259 keyform
= str2fmt(*(++argv
));
260 } else if (strcmp(*argv
, "-in") == 0) {
264 } else if (strcmp(*argv
, "-out") == 0) {
268 } else if (strcmp(*argv
, "-keyout") == 0) {
272 } else if (strcmp(*argv
, "-passin") == 0) {
275 passargin
= *(++argv
);
276 } else if (strcmp(*argv
, "-passout") == 0) {
279 passargout
= *(++argv
);
280 } else if (strcmp(*argv
, "-rand") == 0) {
284 } else if (strcmp(*argv
, "-newkey") == 0) {
290 is_numeric
= p
[0] >= '0' && p
[0] <= '9';
291 if (strncmp("rsa:", p
, 4) == 0 || is_numeric
) {
292 pkey_type
= TYPE_RSA
;
297 #ifndef OPENSSL_NO_DSA
298 if (strncmp("dsa:", p
, 4) == 0) {
302 pkey_type
= TYPE_DSA
;
304 if ((in
= BIO_new_file(p
, "r")) == NULL
) {
309 PEM_read_bio_DSAparams(in
, NULL
, NULL
, NULL
)) == NULL
) {
313 PEM_read_bio_X509(in
, NULL
, NULL
, NULL
)) == NULL
) {
315 "unable to load DSA parameters from file\n");
319 if ((dtmp
= X509_get_pubkey(xtmp
)) == NULL
)
321 if (dtmp
->type
== EVP_PKEY_DSA
)
322 dsa_params
= DSAparams_dup(dtmp
->pkey
.dsa
);
325 if (dsa_params
== NULL
) {
327 "Certificate does not contain DSA parameters\n");
333 newkey
= BN_num_bits(dsa_params
->p
);
336 #ifndef OPENSSL_NO_ECDSA
337 if (strncmp("ec:", p
, 3) == 0) {
344 if ((in
= BIO_new_file(p
, "r")) == NULL
) {
348 if ((ec_params
= EC_KEY_new()) == NULL
)
350 group
= PEM_read_bio_ECPKParameters(in
, NULL
, NULL
, NULL
);
352 EC_KEY_free(ec_params
);
356 PEM_read_bio_X509(in
, NULL
, NULL
, NULL
)) == NULL
) {
358 "unable to load EC parameters from file\n");
362 if ((dtmp
= X509_get_pubkey(xtmp
)) == NULL
)
364 if (dtmp
->type
== EVP_PKEY_EC
)
365 ec_params
= EC_KEY_dup(dtmp
->pkey
.ec
);
368 if (ec_params
== NULL
) {
370 "Certificate does not contain EC parameters\n");
374 if (EC_KEY_set_group(ec_params
, group
) == 0)
376 EC_GROUP_free(group
);
381 newkey
= EC_GROUP_get_degree(EC_KEY_get0_group(ec_params
));
384 #ifndef OPENSSL_NO_DH
385 if (strncmp("dh:", p
, 4) == 0) {
395 } else if (strcmp(*argv
, "-batch") == 0)
397 else if (strcmp(*argv
, "-newhdr") == 0)
399 else if (strcmp(*argv
, "-modulus") == 0)
401 else if (strcmp(*argv
, "-verify") == 0)
403 else if (strcmp(*argv
, "-nodes") == 0)
405 else if (strcmp(*argv
, "-noout") == 0)
407 else if (strcmp(*argv
, "-verbose") == 0)
409 else if (strcmp(*argv
, "-utf8") == 0)
410 chtype
= MBSTRING_UTF8
;
411 else if (strcmp(*argv
, "-nameopt") == 0) {
414 if (!set_name_ex(&nmflag
, *(++argv
)))
416 } else if (strcmp(*argv
, "-reqopt") == 0) {
419 if (!set_cert_ex(&reqflag
, *(++argv
)))
421 } else if (strcmp(*argv
, "-subject") == 0)
423 else if (strcmp(*argv
, "-text") == 0)
425 else if (strcmp(*argv
, "-x509") == 0)
427 else if (strcmp(*argv
, "-asn1-kludge") == 0)
429 else if (strcmp(*argv
, "-no-asn1-kludge") == 0)
431 else if (strcmp(*argv
, "-subj") == 0) {
435 } else if (strcmp(*argv
, "-multivalue-rdn") == 0)
437 else if (strcmp(*argv
, "-days") == 0) {
440 days
= atoi(*(++argv
));
443 } else if (strcmp(*argv
, "-set_serial") == 0) {
446 serial
= s2i_ASN1_INTEGER(NULL
, *(++argv
));
449 } else if ((md_alg
= EVP_get_digestbyname(&((*argv
)[1]))) != NULL
) {
452 } else if (strcmp(*argv
, "-extensions") == 0) {
455 extensions
= *(++argv
);
456 } else if (strcmp(*argv
, "-reqexts") == 0) {
459 req_exts
= *(++argv
);
461 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
471 BIO_printf(bio_err
, "%s [options] <infile >outfile\n", prog
);
472 BIO_printf(bio_err
, "where options are\n");
473 BIO_printf(bio_err
, " -inform arg input format - DER or PEM\n");
474 BIO_printf(bio_err
, " -outform arg output format - DER or PEM\n");
475 BIO_printf(bio_err
, " -in arg input file\n");
476 BIO_printf(bio_err
, " -out arg output file\n");
477 BIO_printf(bio_err
, " -text text form of request\n");
478 BIO_printf(bio_err
, " -pubkey output public key\n");
479 BIO_printf(bio_err
, " -noout do not output REQ\n");
480 BIO_printf(bio_err
, " -verify verify signature on REQ\n");
481 BIO_printf(bio_err
, " -modulus RSA modulus\n");
482 BIO_printf(bio_err
, " -nodes don't encrypt the output key\n");
483 #ifndef OPENSSL_NO_ENGINE
485 " -engine e use engine e, possibly a hardware device\n");
487 BIO_printf(bio_err
, " -subject output the request's subject\n");
488 BIO_printf(bio_err
, " -passin private key password source\n");
490 " -key file use the private key contained in file\n");
491 BIO_printf(bio_err
, " -keyform arg key file format\n");
492 BIO_printf(bio_err
, " -keyout arg file to send the key to\n");
493 BIO_printf(bio_err
, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR
,
494 LIST_SEPARATOR_CHAR
);
496 " load the file (or the files in the directory) into\n");
497 BIO_printf(bio_err
, " the random number generator\n");
499 " -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
501 " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
502 #ifndef OPENSSL_NO_ECDSA
504 " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
507 " -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
508 BIO_printf(bio_err
, " -config file request template file.\n");
510 " -subj arg set or modify request subject\n");
512 " -multivalue-rdn enable support for multivalued RDNs\n");
513 BIO_printf(bio_err
, " -new new request.\n");
515 " -batch do not ask anything during request generation\n");
517 " -x509 output a x509 structure instead of a cert. req.\n");
519 " -days number of days a certificate generated by -x509 is valid for.\n");
521 " -set_serial serial number to use for a certificate generated by -x509.\n");
523 " -newhdr output \"NEW\" in the header lines\n");
525 " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
527 " have been reported as requiring\n");
529 " -extensions .. specify certificate extension section (override value in config file)\n");
531 " -reqexts .. specify request extension section (override value in config file)\n");
533 " -utf8 input characters are UTF8 (default ASCII)\n");
535 " -nameopt arg - various certificate name options\n");
537 " -reqopt arg - various request text options\n\n");
541 ERR_load_crypto_strings();
542 if (!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
)) {
543 BIO_printf(bio_err
, "Error getting passwords\n");
546 #ifndef MONOLITH /* else this has happened in openssl.c
547 * (global `config') */
548 /* Lets load up our environment a little */
549 p
= getenv("OPENSSL_CONF");
551 p
= getenv("SSLEAY_CONF");
553 p
= to_free
= make_config_name();
554 default_config_file
= p
;
555 config
= NCONF_new(NULL
);
556 i
= NCONF_load(config
, p
, &errline
);
559 if (template != NULL
) {
563 BIO_printf(bio_err
, "Using configuration from %s\n", template);
564 req_conf
= NCONF_new(NULL
);
565 i
= NCONF_load(req_conf
, template, &errline
);
567 BIO_printf(bio_err
, "error on line %ld of %s\n", errline
,
574 if (req_conf
== NULL
) {
575 BIO_printf(bio_err
, "Unable to load config info from %s\n",
576 default_config_file
);
580 BIO_printf(bio_err
, "Using configuration from %s\n",
581 default_config_file
);
584 if (req_conf
!= NULL
) {
585 if (!load_config(bio_err
, req_conf
))
587 p
= NCONF_get_string(req_conf
, NULL
, "oid_file");
593 oid_bio
= BIO_new_file(p
, "r");
594 if (oid_bio
== NULL
) {
596 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
597 ERR_print_errors(bio_err);
600 OBJ_create_objects(oid_bio
);
605 if (!add_oid_section(bio_err
, req_conf
))
608 if (md_alg
== NULL
) {
609 p
= NCONF_get_string(req_conf
, SECTION
, "default_md");
613 if ((md_alg
= EVP_get_digestbyname(p
)) != NULL
)
619 extensions
= NCONF_get_string(req_conf
, SECTION
, V3_EXTENSIONS
);
624 /* Check syntax of file */
626 X509V3_set_ctx_test(&ctx
);
627 X509V3_set_nconf(&ctx
, req_conf
);
628 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, extensions
, NULL
)) {
630 "Error Loading extension section %s\n", extensions
);
636 passin
= NCONF_get_string(req_conf
, SECTION
, "input_password");
642 passout
= NCONF_get_string(req_conf
, SECTION
, "output_password");
647 p
= NCONF_get_string(req_conf
, SECTION
, STRING_MASK
);
651 if (p
&& !ASN1_STRING_set_default_mask_asc(p
)) {
652 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", p
);
656 if (chtype
!= MBSTRING_UTF8
) {
657 p
= NCONF_get_string(req_conf
, SECTION
, UTF8_IN
);
660 else if (!strcmp(p
, "yes"))
661 chtype
= MBSTRING_UTF8
;
665 req_exts
= NCONF_get_string(req_conf
, SECTION
, REQ_EXTENSIONS
);
670 /* Check syntax of file */
672 X509V3_set_ctx_test(&ctx
);
673 X509V3_set_nconf(&ctx
, req_conf
);
674 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, req_exts
, NULL
)) {
676 "Error Loading request extension section %s\n",
682 in
= BIO_new(BIO_s_file());
683 out
= BIO_new(BIO_s_file());
684 if ((in
== NULL
) || (out
== NULL
))
687 #ifndef OPENSSL_NO_ENGINE
688 e
= setup_engine(bio_err
, engine
, 0);
691 if (keyfile
!= NULL
) {
692 pkey
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
696 * load_key() has already printed an appropriate message
700 char *randfile
= NCONF_get_string(req_conf
, SECTION
, "RANDFILE");
701 if (randfile
== NULL
)
703 app_RAND_load_file(randfile
, bio_err
, 0);
707 if (newreq
&& (pkey
== NULL
)) {
708 #ifndef OPENSSL_NO_RSA
711 char *randfile
= NCONF_get_string(req_conf
, SECTION
, "RANDFILE");
712 if (randfile
== NULL
)
714 app_RAND_load_file(randfile
, bio_err
, 0);
716 app_RAND_load_files(inrand
);
719 if (!NCONF_get_number(req_conf
, SECTION
, BITS
, &newkey
))
720 newkey
= DEFAULT_KEY_LENGTH
;
723 if (newkey
< MIN_KEY_LENGTH
724 && (pkey_type
== TYPE_RSA
|| pkey_type
== TYPE_DSA
)) {
725 BIO_printf(bio_err
, "private key length is too short,\n");
726 BIO_printf(bio_err
, "it needs to be at least %d bits, not %ld\n",
727 MIN_KEY_LENGTH
, newkey
);
730 BIO_printf(bio_err
, "Generating a %ld bit %s private key\n",
731 newkey
, (pkey_type
== TYPE_RSA
) ? "RSA" :
732 (pkey_type
== TYPE_DSA
) ? "DSA" : "EC");
734 if ((pkey
= EVP_PKEY_new()) == NULL
)
737 #ifndef OPENSSL_NO_RSA
738 BN_GENCB_set(&cb
, req_cb
, bio_err
);
739 if (pkey_type
== TYPE_RSA
) {
740 RSA
*rsa
= RSA_new();
741 BIGNUM
*bn
= BN_new();
742 if (!bn
|| !rsa
|| !BN_set_word(bn
, 0x10001) ||
743 !RSA_generate_key_ex(rsa
, newkey
, bn
, &cb
) ||
744 !EVP_PKEY_assign_RSA(pkey
, rsa
)) {
754 #ifndef OPENSSL_NO_DSA
755 if (pkey_type
== TYPE_DSA
) {
756 if (!DSA_generate_key(dsa_params
))
758 if (!EVP_PKEY_assign_DSA(pkey
, dsa_params
))
763 #ifndef OPENSSL_NO_ECDSA
764 if (pkey_type
== TYPE_EC
) {
765 if (!EC_KEY_generate_key(ec_params
))
767 if (!EVP_PKEY_assign_EC_KEY(pkey
, ec_params
))
773 app_RAND_write_file(randfile
, bio_err
);
778 if (keyout
== NULL
) {
779 keyout
= NCONF_get_string(req_conf
, SECTION
, KEYFILE
);
784 if (keyout
== NULL
) {
785 BIO_printf(bio_err
, "writing new private key to stdout\n");
786 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
787 #ifdef OPENSSL_SYS_VMS
789 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
790 out
= BIO_push(tmpbio
, out
);
794 BIO_printf(bio_err
, "writing new private key to '%s'\n", keyout
);
795 if (BIO_write_filename(out
, keyout
) <= 0) {
801 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_rsa_key");
804 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_key");
808 if ((p
!= NULL
) && (strcmp(p
, "no") == 0))
815 if (!PEM_write_bio_PrivateKey(out
, pkey
, cipher
,
816 NULL
, 0, NULL
, passout
)) {
817 if ((ERR_GET_REASON(ERR_peek_error()) ==
818 PEM_R_PROBLEMS_GETTING_PASSWORD
) && (i
< 3)) {
825 BIO_printf(bio_err
, "-----\n");
830 * Since we are using a pre-existing certificate request, the kludge
831 * 'format' info should not be changed.
835 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
837 if (BIO_read_filename(in
, infile
) <= 0) {
843 if (informat
== FORMAT_ASN1
)
844 req
= d2i_X509_REQ_bio(in
, NULL
);
845 else if (informat
== FORMAT_PEM
)
846 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
849 "bad input format specified for X509 request\n");
853 BIO_printf(bio_err
, "unable to load X509 request\n");
858 if (newreq
|| x509
) {
860 BIO_printf(bio_err
, "you need to specify a private key\n");
863 #ifndef OPENSSL_NO_DSA
864 if (pkey
->type
== EVP_PKEY_DSA
)
867 #ifndef OPENSSL_NO_ECDSA
868 if (pkey
->type
== EVP_PKEY_EC
)
869 digest
= EVP_ecdsa();
872 req
= X509_REQ_new();
877 i
= make_REQ(req
, pkey
, subj
, multirdn
, !x509
, chtype
);
878 subj
= NULL
; /* done processing '-subj' option */
880 && !sk_X509_ATTRIBUTE_num(req
->req_info
->attributes
)) {
881 sk_X509_ATTRIBUTE_free(req
->req_info
->attributes
);
882 req
->req_info
->attributes
= NULL
;
885 BIO_printf(bio_err
, "problems making Certificate Request\n");
892 if ((x509ss
= X509_new()) == NULL
)
895 /* Set version to V3 */
896 if (extensions
&& !X509_set_version(x509ss
, 2))
899 if (!X509_set_serialNumber(x509ss
, serial
))
902 if (!rand_serial(NULL
, X509_get_serialNumber(x509ss
)))
906 if (!X509_set_issuer_name(x509ss
, X509_REQ_get_subject_name(req
)))
908 if (!X509_gmtime_adj(X509_get_notBefore(x509ss
), 0))
911 (X509_get_notAfter(x509ss
), (long)60 * 60 * 24 * days
))
913 if (!X509_set_subject_name
914 (x509ss
, X509_REQ_get_subject_name(req
)))
916 tmppkey
= X509_REQ_get_pubkey(req
);
917 if (!tmppkey
|| !X509_set_pubkey(x509ss
, tmppkey
))
919 EVP_PKEY_free(tmppkey
);
921 /* Set up V3 context struct */
923 X509V3_set_ctx(&ext_ctx
, x509ss
, x509ss
, NULL
, NULL
, 0);
924 X509V3_set_nconf(&ext_ctx
, req_conf
);
927 if (extensions
&& !X509V3_EXT_add_nconf(req_conf
,
928 &ext_ctx
, extensions
,
930 BIO_printf(bio_err
, "Error Loading extension section %s\n",
935 if (!(i
= X509_sign(x509ss
, pkey
, digest
)))
940 /* Set up V3 context struct */
942 X509V3_set_ctx(&ext_ctx
, NULL
, NULL
, req
, NULL
, 0);
943 X509V3_set_nconf(&ext_ctx
, req_conf
);
946 if (req_exts
&& !X509V3_EXT_REQ_add_nconf(req_conf
,
949 BIO_printf(bio_err
, "Error Loading extension section %s\n",
953 if (!(i
= X509_REQ_sign(req
, pkey
, digest
)))
959 BIO_printf(bio_err
, "Cannot modifiy certificate subject\n");
965 BIO_printf(bio_err
, "Modifying Request's Subject\n");
966 print_name(bio_err
, "old subject=",
967 X509_REQ_get_subject_name(req
), nmflag
);
970 if (build_subject(req
, subj
, chtype
, multirdn
) == 0) {
971 BIO_printf(bio_err
, "ERROR: cannot modify subject\n");
976 req
->req_info
->enc
.modified
= 1;
979 print_name(bio_err
, "new subject=",
980 X509_REQ_get_subject_name(req
), nmflag
);
984 if (verify
&& !x509
) {
988 pkey
= X509_REQ_get_pubkey(req
);
994 i
= X509_REQ_verify(req
, pkey
);
1002 } else if (i
== 0) {
1003 BIO_printf(bio_err
, "verify failure\n");
1004 ERR_print_errors(bio_err
);
1005 } else /* if (i > 0) */
1006 BIO_printf(bio_err
, "verify OK\n");
1009 if (noout
&& !text
&& !modulus
&& !subject
&& !pubkey
) {
1014 if (outfile
== NULL
) {
1015 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
1016 #ifdef OPENSSL_SYS_VMS
1018 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
1019 out
= BIO_push(tmpbio
, out
);
1023 if ((keyout
!= NULL
) && (strcmp(outfile
, keyout
) == 0))
1024 i
= (int)BIO_append_filename(out
, outfile
);
1026 i
= (int)BIO_write_filename(out
, outfile
);
1035 tpubkey
= X509_REQ_get_pubkey(req
);
1036 if (tpubkey
== NULL
) {
1037 BIO_printf(bio_err
, "Error getting public key\n");
1038 ERR_print_errors(bio_err
);
1041 PEM_write_bio_PUBKEY(out
, tpubkey
);
1042 EVP_PKEY_free(tpubkey
);
1047 X509_print_ex(out
, x509ss
, nmflag
, reqflag
);
1049 X509_REQ_print_ex(out
, req
, nmflag
, reqflag
);
1054 print_name(out
, "subject=", X509_get_subject_name(x509ss
),
1057 print_name(out
, "subject=", X509_REQ_get_subject_name(req
),
1065 tpubkey
= X509_get_pubkey(x509ss
);
1067 tpubkey
= X509_REQ_get_pubkey(req
);
1068 if (tpubkey
== NULL
) {
1069 fprintf(stdout
, "Modulus=unavailable\n");
1072 fprintf(stdout
, "Modulus=");
1073 #ifndef OPENSSL_NO_RSA
1074 if (tpubkey
->type
== EVP_PKEY_RSA
)
1075 BN_print(out
, tpubkey
->pkey
.rsa
->n
);
1078 fprintf(stdout
, "Wrong Algorithm type");
1079 EVP_PKEY_free(tpubkey
);
1080 fprintf(stdout
, "\n");
1083 if (!noout
&& !x509
) {
1084 if (outformat
== FORMAT_ASN1
)
1085 i
= i2d_X509_REQ_bio(out
, req
);
1086 else if (outformat
== FORMAT_PEM
) {
1088 i
= PEM_write_bio_X509_REQ_NEW(out
, req
);
1090 i
= PEM_write_bio_X509_REQ(out
, req
);
1092 BIO_printf(bio_err
, "bad output format specified for outfile\n");
1096 BIO_printf(bio_err
, "unable to write X509 request\n");
1100 if (!noout
&& x509
&& (x509ss
!= NULL
)) {
1101 if (outformat
== FORMAT_ASN1
)
1102 i
= i2d_X509_bio(out
, x509ss
);
1103 else if (outformat
== FORMAT_PEM
)
1104 i
= PEM_write_bio_X509(out
, x509ss
);
1106 BIO_printf(bio_err
, "bad output format specified for outfile\n");
1110 BIO_printf(bio_err
, "unable to write X509 certificate\n");
1118 OPENSSL_free(to_free
);
1121 ERR_print_errors(bio_err
);
1123 if ((req_conf
!= NULL
) && (req_conf
!= config
))
1124 NCONF_free(req_conf
);
1127 EVP_PKEY_free(pkey
);
1130 ASN1_INTEGER_free(serial
);
1131 if (passargin
&& passin
)
1132 OPENSSL_free(passin
);
1133 if (passargout
&& passout
)
1134 OPENSSL_free(passout
);
1136 #ifndef OPENSSL_NO_DSA
1137 if (dsa_params
!= NULL
)
1138 DSA_free(dsa_params
);
1140 #ifndef OPENSSL_NO_ECDSA
1141 if (ec_params
!= NULL
)
1142 EC_KEY_free(ec_params
);
1148 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *subj
, int multirdn
,
1149 int attribs
, unsigned long chtype
)
1153 STACK_OF(CONF_VALUE
) *dn_sk
, *attr_sk
= NULL
;
1154 char *tmp
, *dn_sect
, *attr_sect
;
1156 tmp
= NCONF_get_string(req_conf
, SECTION
, PROMPT
);
1159 if ((tmp
!= NULL
) && !strcmp(tmp
, "no"))
1162 dn_sect
= NCONF_get_string(req_conf
, SECTION
, DISTINGUISHED_NAME
);
1163 if (dn_sect
== NULL
) {
1164 BIO_printf(bio_err
, "unable to find '%s' in config\n",
1165 DISTINGUISHED_NAME
);
1168 dn_sk
= NCONF_get_section(req_conf
, dn_sect
);
1169 if (dn_sk
== NULL
) {
1170 BIO_printf(bio_err
, "unable to get '%s' section\n", dn_sect
);
1174 attr_sect
= NCONF_get_string(req_conf
, SECTION
, ATTRIBUTES
);
1175 if (attr_sect
== NULL
) {
1179 attr_sk
= NCONF_get_section(req_conf
, attr_sect
);
1180 if (attr_sk
== NULL
) {
1181 BIO_printf(bio_err
, "unable to get '%s' section\n", attr_sect
);
1186 /* setup version number */
1187 if (!X509_REQ_set_version(req
, 0L))
1188 goto err
; /* version 1 */
1191 i
= auto_info(req
, dn_sk
, attr_sk
, attribs
, chtype
);
1194 i
= build_subject(req
, subj
, chtype
, multirdn
);
1196 i
= prompt_info(req
, dn_sk
, dn_sect
, attr_sk
, attr_sect
, attribs
,
1202 if (!X509_REQ_set_pubkey(req
, pkey
))
1211 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
1212 * where characters may be escaped by \
1214 static int build_subject(X509_REQ
*req
, char *subject
, unsigned long chtype
,
1219 if (!(n
= parse_name(subject
, chtype
, multirdn
)))
1222 if (!X509_REQ_set_subject_name(req
, n
)) {
1230 static int prompt_info(X509_REQ
*req
,
1231 STACK_OF(CONF_VALUE
) *dn_sk
, char *dn_sect
,
1232 STACK_OF(CONF_VALUE
) *attr_sk
, char *attr_sect
,
1233 int attribs
, unsigned long chtype
)
1244 subj
= X509_REQ_get_subject_name(req
);
1248 "You are about to be asked to enter information that will be incorporated\n");
1249 BIO_printf(bio_err
, "into your certificate request.\n");
1251 "What you are about to enter is what is called a Distinguished Name or a DN.\n");
1253 "There are quite a few fields but you can leave some blank\n");
1255 "For some fields there will be a default value,\n");
1257 "If you enter '.', the field will be left blank.\n");
1258 BIO_printf(bio_err
, "-----\n");
1261 if (sk_CONF_VALUE_num(dn_sk
)) {
1265 if (sk_CONF_VALUE_num(dn_sk
) <= i
)
1268 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1271 if (!check_end(type
, "_min") || !check_end(type
, "_max") ||
1272 !check_end(type
, "_default") || !check_end(type
, "_value"))
1275 * Skip past any leading X. X: X, etc to allow for multiple
1278 for (p
= v
->name
; *p
; p
++)
1279 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1290 /* If OBJ not recognised ignore it */
1291 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1293 if (BIO_snprintf(buf
, sizeof buf
, "%s_default", v
->name
)
1294 >= (int)sizeof(buf
)) {
1295 BIO_printf(bio_err
, "Name '%s' too long\n", v
->name
);
1299 if ((def
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1304 BIO_snprintf(buf
, sizeof buf
, "%s_value", v
->name
);
1305 if ((value
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1310 BIO_snprintf(buf
, sizeof buf
, "%s_min", v
->name
);
1311 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_min
)) {
1316 BIO_snprintf(buf
, sizeof buf
, "%s_max", v
->name
);
1317 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_max
)) {
1322 if (!add_DN_object(subj
, v
->value
, def
, value
, nid
,
1323 n_min
, n_max
, chtype
, mval
))
1326 if (X509_NAME_entry_count(subj
) == 0) {
1328 "error, no objects specified in config file\n");
1333 if ((attr_sk
!= NULL
) && (sk_CONF_VALUE_num(attr_sk
) > 0)
1336 "\nPlease enter the following 'extra' attributes\n");
1338 "to be sent with your certificate request\n");
1344 if ((attr_sk
== NULL
) || (sk_CONF_VALUE_num(attr_sk
) <= i
))
1347 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1349 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1352 if (BIO_snprintf(buf
, sizeof buf
, "%s_default", type
)
1353 >= (int)sizeof(buf
)) {
1354 BIO_printf(bio_err
, "Name '%s' too long\n", v
->name
);
1358 if ((def
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1364 BIO_snprintf(buf
, sizeof buf
, "%s_value", type
);
1365 if ((value
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1371 BIO_snprintf(buf
, sizeof buf
, "%s_min", type
);
1372 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_min
)) {
1377 BIO_snprintf(buf
, sizeof buf
, "%s_max", type
);
1378 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_max
)) {
1383 if (!add_attribute_object(req
,
1384 v
->value
, def
, value
, nid
, n_min
,
1390 BIO_printf(bio_err
, "No template, please set one up.\n");
1398 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *dn_sk
,
1399 STACK_OF(CONF_VALUE
) *attr_sk
, int attribs
,
1400 unsigned long chtype
)
1408 subj
= X509_REQ_get_subject_name(req
);
1410 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1412 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1416 * Skip past any leading X. X: X, etc to allow for multiple instances
1418 for (p
= v
->name
; *p
; p
++)
1419 #ifndef CHARSET_EBCDIC
1420 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1422 if ((*p
== os_toascii
[':']) || (*p
== os_toascii
[','])
1423 || (*p
== os_toascii
['.'])) {
1430 #ifndef CHARSET_EBCDIC
1433 if (*p
== os_toascii
['+'])
1440 if (!X509_NAME_add_entry_by_txt(subj
, type
, chtype
,
1441 (unsigned char *)v
->value
, -1, -1,
1447 if (!X509_NAME_entry_count(subj
)) {
1448 BIO_printf(bio_err
, "error, no objects specified in config file\n");
1452 for (i
= 0; i
< sk_CONF_VALUE_num(attr_sk
); i
++) {
1453 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1454 if (!X509_REQ_add1_attr_by_txt(req
, v
->name
, chtype
,
1455 (unsigned char *)v
->value
, -1))
1462 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
1463 char *value
, int nid
, int n_min
, int n_max
,
1464 unsigned long chtype
, int mval
)
1467 MS_STATIC
char buf
[1024];
1470 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1471 (void)BIO_flush(bio_err
);
1472 if (value
!= NULL
) {
1473 BUF_strlcpy(buf
, value
, sizeof buf
);
1474 BUF_strlcat(buf
, "\n", sizeof buf
);
1475 BIO_printf(bio_err
, "%s\n", value
);
1479 if (!fgets(buf
, sizeof buf
, stdin
))
1489 else if (buf
[0] == '\n') {
1490 if ((def
== NULL
) || (def
[0] == '\0'))
1492 BUF_strlcpy(buf
, def
, sizeof buf
);
1493 BUF_strlcat(buf
, "\n", sizeof buf
);
1494 } else if ((buf
[0] == '.') && (buf
[1] == '\n'))
1498 if (buf
[i
- 1] != '\n') {
1499 BIO_printf(bio_err
, "weird input :-(\n");
1503 #ifdef CHARSET_EBCDIC
1504 ebcdic2ascii(buf
, buf
, i
);
1506 if (!req_check_len(i
, n_min
, n_max
)) {
1512 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
1513 (unsigned char *)buf
, -1, -1, mval
))
1520 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
1521 char *value
, int nid
, int n_min
,
1522 int n_max
, unsigned long chtype
)
1525 static char buf
[1024];
1529 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1530 (void)BIO_flush(bio_err
);
1531 if (value
!= NULL
) {
1532 BUF_strlcpy(buf
, value
, sizeof buf
);
1533 BUF_strlcat(buf
, "\n", sizeof buf
);
1534 BIO_printf(bio_err
, "%s\n", value
);
1538 if (!fgets(buf
, sizeof buf
, stdin
))
1548 else if (buf
[0] == '\n') {
1549 if ((def
== NULL
) || (def
[0] == '\0'))
1551 BUF_strlcpy(buf
, def
, sizeof buf
);
1552 BUF_strlcat(buf
, "\n", sizeof buf
);
1553 } else if ((buf
[0] == '.') && (buf
[1] == '\n'))
1557 if (buf
[i
- 1] != '\n') {
1558 BIO_printf(bio_err
, "weird input :-(\n");
1562 #ifdef CHARSET_EBCDIC
1563 ebcdic2ascii(buf
, buf
, i
);
1565 if (!req_check_len(i
, n_min
, n_max
)) {
1571 if (!X509_REQ_add1_attr_by_NID(req
, nid
, chtype
,
1572 (unsigned char *)buf
, -1)) {
1573 BIO_printf(bio_err
, "Error adding attribute\n");
1574 ERR_print_errors(bio_err
);
1583 #ifndef OPENSSL_NO_RSA
1584 static int MS_CALLBACK
req_cb(int p
, int n
, BN_GENCB
*cb
)
1596 BIO_write(cb
->arg
, &c
, 1);
1597 (void)BIO_flush(cb
->arg
);
1605 static int req_check_len(int len
, int n_min
, int n_max
)
1607 if ((n_min
> 0) && (len
< n_min
)) {
1609 "string is too short, it needs to be at least %d bytes long\n",
1613 if ((n_max
>= 0) && (len
> n_max
)) {
1615 "string is too long, it needs to be less than %d bytes long\n",
1622 /* Check if the end of a string matches 'end' */
1623 static int check_end(const char *str
, const char *end
)
1631 tmp
= str
+ slen
- elen
;
1632 return strcmp(tmp
, end
);