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 2048
105 #define MIN_KEY_LENGTH 512
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 static int genpkey_cb(EVP_PKEY_CTX
*ctx
);
149 static int req_check_len(int len
, int n_min
, int n_max
);
150 static int check_end(const char *str
, const char *end
);
151 static EVP_PKEY_CTX
*set_keygen_ctx(BIO
*err
, const char *gstr
,
152 int *pkey_type
, long *pkeylen
,
153 char **palgnam
, ENGINE
*keygen_engine
);
155 static char *default_config_file
= NULL
;
157 static CONF
*req_conf
= NULL
;
158 static int batch
= 0;
160 int MAIN(int, char **);
162 int MAIN(int argc
, char **argv
)
164 ENGINE
*e
= NULL
, *gen_eng
= NULL
;
165 unsigned long nmflag
= 0, reqflag
= 0;
166 int ex
= 1, x509
= 0, days
= 30;
168 X509_REQ
*req
= NULL
;
169 EVP_PKEY_CTX
*genctx
= NULL
;
170 const char *keyalg
= NULL
;
171 char *keyalgstr
= NULL
;
172 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
, *sigopts
= NULL
;
173 EVP_PKEY
*pkey
= NULL
;
174 int i
= 0, badops
= 0, newreq
= 0, verbose
= 0, pkey_type
= -1;
176 BIO
*in
= NULL
, *out
= NULL
;
177 int informat
, outformat
, verify
= 0, noout
= 0, text
= 0, keyform
=
179 int nodes
= 0, kludge
= 0, newhdr
= 0, subject
= 0, pubkey
= 0;
180 char *infile
, *outfile
, *prog
, *keyfile
= NULL
, *template =
181 NULL
, *keyout
= NULL
;
183 char *extensions
= NULL
;
184 char *req_exts
= NULL
;
185 const EVP_CIPHER
*cipher
= NULL
;
186 ASN1_INTEGER
*serial
= NULL
;
189 char *passargin
= NULL
, *passargout
= NULL
;
190 char *passin
= NULL
, *passout
= NULL
;
194 const EVP_MD
*md_alg
= NULL
, *digest
= NULL
;
195 unsigned long chtype
= MBSTRING_ASC
;
202 #ifndef OPENSSL_NO_DES
203 cipher
= EVP_des_ede3_cbc();
208 if ((bio_err
= BIO_new(BIO_s_file())) != NULL
)
209 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
213 informat
= FORMAT_PEM
;
214 outformat
= FORMAT_PEM
;
220 if (strcmp(*argv
, "-inform") == 0) {
223 informat
= str2fmt(*(++argv
));
224 } else if (strcmp(*argv
, "-outform") == 0) {
227 outformat
= str2fmt(*(++argv
));
229 #ifndef OPENSSL_NO_ENGINE
230 else if (strcmp(*argv
, "-engine") == 0) {
234 } else if (strcmp(*argv
, "-keygen_engine") == 0) {
237 gen_eng
= ENGINE_by_id(*(++argv
));
238 if (gen_eng
== NULL
) {
239 BIO_printf(bio_err
, "Can't find keygen engine %s\n", *argv
);
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) {
289 } else if (strcmp(*argv
, "-pkeyopt") == 0) {
293 pkeyopts
= sk_OPENSSL_STRING_new_null();
294 if (!pkeyopts
|| !sk_OPENSSL_STRING_push(pkeyopts
, *(++argv
)))
296 } else if (strcmp(*argv
, "-sigopt") == 0) {
300 sigopts
= sk_OPENSSL_STRING_new_null();
301 if (!sigopts
|| !sk_OPENSSL_STRING_push(sigopts
, *(++argv
)))
303 } else if (strcmp(*argv
, "-batch") == 0)
305 else if (strcmp(*argv
, "-newhdr") == 0)
307 else if (strcmp(*argv
, "-modulus") == 0)
309 else if (strcmp(*argv
, "-verify") == 0)
311 else if (strcmp(*argv
, "-nodes") == 0)
313 else if (strcmp(*argv
, "-noout") == 0)
315 else if (strcmp(*argv
, "-verbose") == 0)
317 else if (strcmp(*argv
, "-utf8") == 0)
318 chtype
= MBSTRING_UTF8
;
319 else if (strcmp(*argv
, "-nameopt") == 0) {
322 if (!set_name_ex(&nmflag
, *(++argv
)))
324 } else if (strcmp(*argv
, "-reqopt") == 0) {
327 if (!set_cert_ex(&reqflag
, *(++argv
)))
329 } else if (strcmp(*argv
, "-subject") == 0)
331 else if (strcmp(*argv
, "-text") == 0)
333 else if (strcmp(*argv
, "-x509") == 0) {
335 } else if (strcmp(*argv
, "-asn1-kludge") == 0)
337 else if (strcmp(*argv
, "-no-asn1-kludge") == 0)
339 else if (strcmp(*argv
, "-subj") == 0) {
343 } else if (strcmp(*argv
, "-multivalue-rdn") == 0)
345 else if (strcmp(*argv
, "-days") == 0) {
348 days
= atoi(*(++argv
));
351 } else if (strcmp(*argv
, "-set_serial") == 0) {
354 serial
= s2i_ASN1_INTEGER(NULL
, *(++argv
));
357 } else if (strcmp(*argv
, "-extensions") == 0) {
360 extensions
= *(++argv
);
361 } else if (strcmp(*argv
, "-reqexts") == 0) {
364 req_exts
= *(++argv
);
365 } else if ((md_alg
= EVP_get_digestbyname(&((*argv
)[1]))) != NULL
) {
369 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
379 BIO_printf(bio_err
, "%s [options] <infile >outfile\n", prog
);
380 BIO_printf(bio_err
, "where options are\n");
381 BIO_printf(bio_err
, " -inform arg input format - DER or PEM\n");
382 BIO_printf(bio_err
, " -outform arg output format - DER or PEM\n");
383 BIO_printf(bio_err
, " -in arg input file\n");
384 BIO_printf(bio_err
, " -out arg output file\n");
385 BIO_printf(bio_err
, " -text text form of request\n");
386 BIO_printf(bio_err
, " -pubkey output public key\n");
387 BIO_printf(bio_err
, " -noout do not output REQ\n");
388 BIO_printf(bio_err
, " -verify verify signature on REQ\n");
389 BIO_printf(bio_err
, " -modulus RSA modulus\n");
390 BIO_printf(bio_err
, " -nodes don't encrypt the output key\n");
391 #ifndef OPENSSL_NO_ENGINE
393 " -engine e use engine e, possibly a hardware device\n");
395 BIO_printf(bio_err
, " -subject output the request's subject\n");
396 BIO_printf(bio_err
, " -passin private key password source\n");
398 " -key file use the private key contained in file\n");
399 BIO_printf(bio_err
, " -keyform arg key file format\n");
400 BIO_printf(bio_err
, " -keyout arg file to send the key to\n");
401 BIO_printf(bio_err
, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR
,
402 LIST_SEPARATOR_CHAR
);
404 " load the file (or the files in the directory) into\n");
405 BIO_printf(bio_err
, " the random number generator\n");
407 " -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
409 " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
410 #ifndef OPENSSL_NO_ECDSA
412 " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
415 " -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
416 BIO_printf(bio_err
, " -config file request template file.\n");
418 " -subj arg set or modify request subject\n");
420 " -multivalue-rdn enable support for multivalued RDNs\n");
421 BIO_printf(bio_err
, " -new new request.\n");
423 " -batch do not ask anything during request generation\n");
425 " -x509 output a x509 structure instead of a cert. req.\n");
427 " -days number of days a certificate generated by -x509 is valid for.\n");
429 " -set_serial serial number to use for a certificate generated by -x509.\n");
431 " -newhdr output \"NEW\" in the header lines\n");
433 " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
435 " have been reported as requiring\n");
437 " -extensions .. specify certificate extension section (override value in config file)\n");
439 " -reqexts .. specify request extension section (override value in config file)\n");
441 " -utf8 input characters are UTF8 (default ASCII)\n");
443 " -nameopt arg - various certificate name options\n");
445 " -reqopt arg - various request text options\n\n");
449 if (x509
&& infile
== NULL
)
452 ERR_load_crypto_strings();
453 if (!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
)) {
454 BIO_printf(bio_err
, "Error getting passwords\n");
457 #ifndef MONOLITH /* else this has happened in openssl.c
458 * (global `config') */
459 /* Lets load up our environment a little */
460 p
= getenv("OPENSSL_CONF");
462 p
= getenv("SSLEAY_CONF");
464 p
= to_free
= make_config_name();
465 default_config_file
= p
;
466 config
= NCONF_new(NULL
);
467 i
= NCONF_load(config
, p
, &errline
);
470 if (template != NULL
) {
474 BIO_printf(bio_err
, "Using configuration from %s\n", template);
475 req_conf
= NCONF_new(NULL
);
476 i
= NCONF_load(req_conf
, template, &errline
);
478 BIO_printf(bio_err
, "error on line %ld of %s\n", errline
,
485 if (req_conf
== NULL
) {
486 BIO_printf(bio_err
, "Unable to load config info from %s\n",
487 default_config_file
);
491 BIO_printf(bio_err
, "Using configuration from %s\n",
492 default_config_file
);
495 if (req_conf
!= NULL
) {
496 if (!load_config(bio_err
, req_conf
))
498 p
= NCONF_get_string(req_conf
, NULL
, "oid_file");
504 oid_bio
= BIO_new_file(p
, "r");
505 if (oid_bio
== NULL
) {
507 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
508 ERR_print_errors(bio_err);
511 OBJ_create_objects(oid_bio
);
516 if (!add_oid_section(bio_err
, req_conf
))
519 if (md_alg
== NULL
) {
520 p
= NCONF_get_string(req_conf
, SECTION
, "default_md");
524 if ((md_alg
= EVP_get_digestbyname(p
)) != NULL
)
530 extensions
= NCONF_get_string(req_conf
, SECTION
, V3_EXTENSIONS
);
535 /* Check syntax of file */
537 X509V3_set_ctx_test(&ctx
);
538 X509V3_set_nconf(&ctx
, req_conf
);
539 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, extensions
, NULL
)) {
541 "Error Loading extension section %s\n", extensions
);
547 passin
= NCONF_get_string(req_conf
, SECTION
, "input_password");
553 passout
= NCONF_get_string(req_conf
, SECTION
, "output_password");
558 p
= NCONF_get_string(req_conf
, SECTION
, STRING_MASK
);
562 if (p
&& !ASN1_STRING_set_default_mask_asc(p
)) {
563 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", p
);
567 if (chtype
!= MBSTRING_UTF8
) {
568 p
= NCONF_get_string(req_conf
, SECTION
, UTF8_IN
);
571 else if (!strcmp(p
, "yes"))
572 chtype
= MBSTRING_UTF8
;
576 req_exts
= NCONF_get_string(req_conf
, SECTION
, REQ_EXTENSIONS
);
581 /* Check syntax of file */
583 X509V3_set_ctx_test(&ctx
);
584 X509V3_set_nconf(&ctx
, req_conf
);
585 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, req_exts
, NULL
)) {
587 "Error Loading request extension section %s\n",
593 in
= BIO_new(BIO_s_file());
594 out
= BIO_new(BIO_s_file());
595 if ((in
== NULL
) || (out
== NULL
))
598 e
= setup_engine(bio_err
, engine
, 0);
600 if (keyfile
!= NULL
) {
601 pkey
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
605 * load_key() has already printed an appropriate message
609 char *randfile
= NCONF_get_string(req_conf
, SECTION
, "RANDFILE");
610 if (randfile
== NULL
)
612 app_RAND_load_file(randfile
, bio_err
, 0);
616 if (newreq
&& (pkey
== NULL
)) {
617 char *randfile
= NCONF_get_string(req_conf
, SECTION
, "RANDFILE");
618 if (randfile
== NULL
)
620 app_RAND_load_file(randfile
, bio_err
, 0);
622 app_RAND_load_files(inrand
);
624 if (!NCONF_get_number(req_conf
, SECTION
, BITS
, &newkey
)) {
625 newkey
= DEFAULT_KEY_LENGTH
;
629 genctx
= set_keygen_ctx(bio_err
, keyalg
, &pkey_type
, &newkey
,
630 &keyalgstr
, gen_eng
);
635 if (newkey
< MIN_KEY_LENGTH
636 && (pkey_type
== EVP_PKEY_RSA
|| pkey_type
== EVP_PKEY_DSA
)) {
637 BIO_printf(bio_err
, "private key length is too short,\n");
638 BIO_printf(bio_err
, "it needs to be at least %d bits, not %ld\n",
639 MIN_KEY_LENGTH
, newkey
);
644 genctx
= set_keygen_ctx(bio_err
, NULL
, &pkey_type
, &newkey
,
645 &keyalgstr
, gen_eng
);
652 for (i
= 0; i
< sk_OPENSSL_STRING_num(pkeyopts
); i
++) {
653 genopt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
654 if (pkey_ctrl_string(genctx
, genopt
) <= 0) {
655 BIO_printf(bio_err
, "parameter error \"%s\"\n", genopt
);
656 ERR_print_errors(bio_err
);
662 BIO_printf(bio_err
, "Generating a %s private key\n", keyalgstr
);
664 EVP_PKEY_CTX_set_cb(genctx
, genpkey_cb
);
665 EVP_PKEY_CTX_set_app_data(genctx
, bio_err
);
667 if (EVP_PKEY_keygen(genctx
, &pkey
) <= 0) {
668 BIO_puts(bio_err
, "Error Generating Key\n");
672 EVP_PKEY_CTX_free(genctx
);
675 app_RAND_write_file(randfile
, bio_err
);
677 if (keyout
== NULL
) {
678 keyout
= NCONF_get_string(req_conf
, SECTION
, KEYFILE
);
683 if (keyout
== NULL
) {
684 BIO_printf(bio_err
, "writing new private key to stdout\n");
685 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
686 #ifdef OPENSSL_SYS_VMS
688 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
689 out
= BIO_push(tmpbio
, out
);
693 BIO_printf(bio_err
, "writing new private key to '%s'\n", keyout
);
694 if (BIO_write_filename(out
, keyout
) <= 0) {
700 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_rsa_key");
703 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_key");
707 if ((p
!= NULL
) && (strcmp(p
, "no") == 0))
714 if (!PEM_write_bio_PrivateKey(out
, pkey
, cipher
,
715 NULL
, 0, NULL
, passout
)) {
716 if ((ERR_GET_REASON(ERR_peek_error()) ==
717 PEM_R_PROBLEMS_GETTING_PASSWORD
) && (i
< 3)) {
724 BIO_printf(bio_err
, "-----\n");
729 * Since we are using a pre-existing certificate request, the kludge
730 * 'format' info should not be changed.
734 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
736 if (BIO_read_filename(in
, infile
) <= 0) {
742 if (informat
== FORMAT_ASN1
)
743 req
= d2i_X509_REQ_bio(in
, NULL
);
744 else if (informat
== FORMAT_PEM
)
745 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
748 "bad input format specified for X509 request\n");
752 BIO_printf(bio_err
, "unable to load X509 request\n");
757 if (newreq
|| x509
) {
759 BIO_printf(bio_err
, "you need to specify a private key\n");
764 req
= X509_REQ_new();
769 i
= make_REQ(req
, pkey
, subj
, multirdn
, !x509
, chtype
);
770 subj
= NULL
; /* done processing '-subj' option */
772 && !sk_X509_ATTRIBUTE_num(req
->req_info
->attributes
)) {
773 sk_X509_ATTRIBUTE_free(req
->req_info
->attributes
);
774 req
->req_info
->attributes
= NULL
;
777 BIO_printf(bio_err
, "problems making Certificate Request\n");
784 if ((x509ss
= X509_new()) == NULL
)
787 /* Set version to V3 */
788 if (extensions
&& !X509_set_version(x509ss
, 2))
791 if (!X509_set_serialNumber(x509ss
, serial
))
794 if (!rand_serial(NULL
, X509_get_serialNumber(x509ss
)))
798 if (!X509_set_issuer_name(x509ss
, X509_REQ_get_subject_name(req
)))
800 if (!X509_gmtime_adj(X509_get_notBefore(x509ss
), 0))
802 if (!X509_time_adj_ex(X509_get_notAfter(x509ss
), days
, 0, NULL
))
804 if (!X509_set_subject_name
805 (x509ss
, X509_REQ_get_subject_name(req
)))
807 tmppkey
= X509_REQ_get_pubkey(req
);
808 if (!tmppkey
|| !X509_set_pubkey(x509ss
, tmppkey
))
810 EVP_PKEY_free(tmppkey
);
812 /* Set up V3 context struct */
814 X509V3_set_ctx(&ext_ctx
, x509ss
, x509ss
, NULL
, NULL
, 0);
815 X509V3_set_nconf(&ext_ctx
, req_conf
);
818 if (extensions
&& !X509V3_EXT_add_nconf(req_conf
,
819 &ext_ctx
, extensions
,
821 BIO_printf(bio_err
, "Error Loading extension section %s\n",
826 i
= do_X509_sign(bio_err
, x509ss
, pkey
, digest
, sigopts
);
828 ERR_print_errors(bio_err
);
834 /* Set up V3 context struct */
836 X509V3_set_ctx(&ext_ctx
, NULL
, NULL
, req
, NULL
, 0);
837 X509V3_set_nconf(&ext_ctx
, req_conf
);
840 if (req_exts
&& !X509V3_EXT_REQ_add_nconf(req_conf
,
843 BIO_printf(bio_err
, "Error Loading extension section %s\n",
847 i
= do_X509_REQ_sign(bio_err
, req
, pkey
, digest
, sigopts
);
849 ERR_print_errors(bio_err
);
856 BIO_printf(bio_err
, "Cannot modifiy certificate subject\n");
862 BIO_printf(bio_err
, "Modifying Request's Subject\n");
863 print_name(bio_err
, "old subject=",
864 X509_REQ_get_subject_name(req
), nmflag
);
867 if (build_subject(req
, subj
, chtype
, multirdn
) == 0) {
868 BIO_printf(bio_err
, "ERROR: cannot modify subject\n");
873 req
->req_info
->enc
.modified
= 1;
876 print_name(bio_err
, "new subject=",
877 X509_REQ_get_subject_name(req
), nmflag
);
881 if (verify
&& !x509
) {
885 pkey
= X509_REQ_get_pubkey(req
);
891 i
= X509_REQ_verify(req
, pkey
);
900 BIO_printf(bio_err
, "verify failure\n");
901 ERR_print_errors(bio_err
);
902 } else /* if (i > 0) */
903 BIO_printf(bio_err
, "verify OK\n");
906 if (noout
&& !text
&& !modulus
&& !subject
&& !pubkey
) {
911 if (outfile
== NULL
) {
912 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
913 #ifdef OPENSSL_SYS_VMS
915 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
916 out
= BIO_push(tmpbio
, out
);
920 if ((keyout
!= NULL
) && (strcmp(outfile
, keyout
) == 0))
921 i
= (int)BIO_append_filename(out
, outfile
);
923 i
= (int)BIO_write_filename(out
, outfile
);
932 tpubkey
= X509_REQ_get_pubkey(req
);
933 if (tpubkey
== NULL
) {
934 BIO_printf(bio_err
, "Error getting public key\n");
935 ERR_print_errors(bio_err
);
938 PEM_write_bio_PUBKEY(out
, tpubkey
);
939 EVP_PKEY_free(tpubkey
);
944 X509_print_ex(out
, x509ss
, nmflag
, reqflag
);
946 X509_REQ_print_ex(out
, req
, nmflag
, reqflag
);
951 print_name(out
, "subject=", X509_get_subject_name(x509ss
),
954 print_name(out
, "subject=", X509_REQ_get_subject_name(req
),
962 tpubkey
= X509_get_pubkey(x509ss
);
964 tpubkey
= X509_REQ_get_pubkey(req
);
965 if (tpubkey
== NULL
) {
966 fprintf(stdout
, "Modulus=unavailable\n");
969 fprintf(stdout
, "Modulus=");
970 #ifndef OPENSSL_NO_RSA
971 if (EVP_PKEY_base_id(tpubkey
) == EVP_PKEY_RSA
)
972 BN_print(out
, tpubkey
->pkey
.rsa
->n
);
975 fprintf(stdout
, "Wrong Algorithm type");
976 EVP_PKEY_free(tpubkey
);
977 fprintf(stdout
, "\n");
980 if (!noout
&& !x509
) {
981 if (outformat
== FORMAT_ASN1
)
982 i
= i2d_X509_REQ_bio(out
, req
);
983 else if (outformat
== FORMAT_PEM
) {
985 i
= PEM_write_bio_X509_REQ_NEW(out
, req
);
987 i
= PEM_write_bio_X509_REQ(out
, req
);
989 BIO_printf(bio_err
, "bad output format specified for outfile\n");
993 BIO_printf(bio_err
, "unable to write X509 request\n");
997 if (!noout
&& x509
&& (x509ss
!= NULL
)) {
998 if (outformat
== FORMAT_ASN1
)
999 i
= i2d_X509_bio(out
, x509ss
);
1000 else if (outformat
== FORMAT_PEM
)
1001 i
= PEM_write_bio_X509(out
, x509ss
);
1003 BIO_printf(bio_err
, "bad output format specified for outfile\n");
1007 BIO_printf(bio_err
, "unable to write X509 certificate\n");
1015 OPENSSL_free(to_free
);
1018 ERR_print_errors(bio_err
);
1020 if ((req_conf
!= NULL
) && (req_conf
!= config
))
1021 NCONF_free(req_conf
);
1024 EVP_PKEY_free(pkey
);
1026 EVP_PKEY_CTX_free(genctx
);
1028 sk_OPENSSL_STRING_free(pkeyopts
);
1030 sk_OPENSSL_STRING_free(sigopts
);
1031 #ifndef OPENSSL_NO_ENGINE
1033 ENGINE_free(gen_eng
);
1036 OPENSSL_free(keyalgstr
);
1039 ASN1_INTEGER_free(serial
);
1041 if (passargin
&& passin
)
1042 OPENSSL_free(passin
);
1043 if (passargout
&& passout
)
1044 OPENSSL_free(passout
);
1050 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *subj
, int multirdn
,
1051 int attribs
, unsigned long chtype
)
1055 STACK_OF(CONF_VALUE
) *dn_sk
, *attr_sk
= NULL
;
1056 char *tmp
, *dn_sect
, *attr_sect
;
1058 tmp
= NCONF_get_string(req_conf
, SECTION
, PROMPT
);
1061 if ((tmp
!= NULL
) && !strcmp(tmp
, "no"))
1064 dn_sect
= NCONF_get_string(req_conf
, SECTION
, DISTINGUISHED_NAME
);
1065 if (dn_sect
== NULL
) {
1066 BIO_printf(bio_err
, "unable to find '%s' in config\n",
1067 DISTINGUISHED_NAME
);
1070 dn_sk
= NCONF_get_section(req_conf
, dn_sect
);
1071 if (dn_sk
== NULL
) {
1072 BIO_printf(bio_err
, "unable to get '%s' section\n", dn_sect
);
1076 attr_sect
= NCONF_get_string(req_conf
, SECTION
, ATTRIBUTES
);
1077 if (attr_sect
== NULL
) {
1081 attr_sk
= NCONF_get_section(req_conf
, attr_sect
);
1082 if (attr_sk
== NULL
) {
1083 BIO_printf(bio_err
, "unable to get '%s' section\n", attr_sect
);
1088 /* setup version number */
1089 if (!X509_REQ_set_version(req
, 0L))
1090 goto err
; /* version 1 */
1093 i
= auto_info(req
, dn_sk
, attr_sk
, attribs
, chtype
);
1096 i
= build_subject(req
, subj
, chtype
, multirdn
);
1098 i
= prompt_info(req
, dn_sk
, dn_sect
, attr_sk
, attr_sect
, attribs
,
1104 if (!X509_REQ_set_pubkey(req
, pkey
))
1113 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
1114 * where characters may be escaped by \
1116 static int build_subject(X509_REQ
*req
, char *subject
, unsigned long chtype
,
1121 if (!(n
= parse_name(subject
, chtype
, multirdn
)))
1124 if (!X509_REQ_set_subject_name(req
, n
)) {
1132 static int prompt_info(X509_REQ
*req
,
1133 STACK_OF(CONF_VALUE
) *dn_sk
, char *dn_sect
,
1134 STACK_OF(CONF_VALUE
) *attr_sk
, char *attr_sect
,
1135 int attribs
, unsigned long chtype
)
1146 subj
= X509_REQ_get_subject_name(req
);
1150 "You are about to be asked to enter information that will be incorporated\n");
1151 BIO_printf(bio_err
, "into your certificate request.\n");
1153 "What you are about to enter is what is called a Distinguished Name or a DN.\n");
1155 "There are quite a few fields but you can leave some blank\n");
1157 "For some fields there will be a default value,\n");
1159 "If you enter '.', the field will be left blank.\n");
1160 BIO_printf(bio_err
, "-----\n");
1163 if (sk_CONF_VALUE_num(dn_sk
)) {
1167 if (sk_CONF_VALUE_num(dn_sk
) <= i
)
1170 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1173 if (!check_end(type
, "_min") || !check_end(type
, "_max") ||
1174 !check_end(type
, "_default") || !check_end(type
, "_value"))
1177 * Skip past any leading X. X: X, etc to allow for multiple
1180 for (p
= v
->name
; *p
; p
++)
1181 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1192 /* If OBJ not recognised ignore it */
1193 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1195 if (BIO_snprintf(buf
, sizeof(buf
), "%s_default", v
->name
)
1196 >= (int)sizeof(buf
)) {
1197 BIO_printf(bio_err
, "Name '%s' too long\n", v
->name
);
1201 if ((def
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1206 BIO_snprintf(buf
, sizeof(buf
), "%s_value", v
->name
);
1207 if ((value
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1212 BIO_snprintf(buf
, sizeof(buf
), "%s_min", v
->name
);
1213 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_min
)) {
1218 BIO_snprintf(buf
, sizeof(buf
), "%s_max", v
->name
);
1219 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_max
)) {
1224 if (!add_DN_object(subj
, v
->value
, def
, value
, nid
,
1225 n_min
, n_max
, chtype
, mval
))
1228 if (X509_NAME_entry_count(subj
) == 0) {
1230 "error, no objects specified in config file\n");
1235 if ((attr_sk
!= NULL
) && (sk_CONF_VALUE_num(attr_sk
) > 0)
1238 "\nPlease enter the following 'extra' attributes\n");
1240 "to be sent with your certificate request\n");
1246 if ((attr_sk
== NULL
) || (sk_CONF_VALUE_num(attr_sk
) <= i
))
1249 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1251 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1254 if (BIO_snprintf(buf
, sizeof(buf
), "%s_default", type
)
1255 >= (int)sizeof(buf
)) {
1256 BIO_printf(bio_err
, "Name '%s' too long\n", v
->name
);
1260 if ((def
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1266 BIO_snprintf(buf
, sizeof(buf
), "%s_value", type
);
1267 if ((value
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1273 BIO_snprintf(buf
, sizeof(buf
), "%s_min", type
);
1274 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_min
)) {
1279 BIO_snprintf(buf
, sizeof(buf
), "%s_max", type
);
1280 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_max
)) {
1285 if (!add_attribute_object(req
,
1286 v
->value
, def
, value
, nid
, n_min
,
1292 BIO_printf(bio_err
, "No template, please set one up.\n");
1300 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *dn_sk
,
1301 STACK_OF(CONF_VALUE
) *attr_sk
, int attribs
,
1302 unsigned long chtype
)
1310 subj
= X509_REQ_get_subject_name(req
);
1312 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1314 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1318 * Skip past any leading X. X: X, etc to allow for multiple instances
1320 for (p
= v
->name
; *p
; p
++)
1321 #ifndef CHARSET_EBCDIC
1322 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1324 if ((*p
== os_toascii
[':']) || (*p
== os_toascii
[','])
1325 || (*p
== os_toascii
['.'])) {
1332 #ifndef CHARSET_EBCDIC
1335 if (*type
== os_toascii
['+']) {
1341 if (!X509_NAME_add_entry_by_txt(subj
, type
, chtype
,
1342 (unsigned char *)v
->value
, -1, -1,
1348 if (!X509_NAME_entry_count(subj
)) {
1349 BIO_printf(bio_err
, "error, no objects specified in config file\n");
1353 for (i
= 0; i
< sk_CONF_VALUE_num(attr_sk
); i
++) {
1354 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1355 if (!X509_REQ_add1_attr_by_txt(req
, v
->name
, chtype
,
1356 (unsigned char *)v
->value
, -1))
1363 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
1364 char *value
, int nid
, int n_min
, int n_max
,
1365 unsigned long chtype
, int mval
)
1368 MS_STATIC
char buf
[1024];
1371 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1372 (void)BIO_flush(bio_err
);
1373 if (value
!= NULL
) {
1374 BUF_strlcpy(buf
, value
, sizeof(buf
));
1375 BUF_strlcat(buf
, "\n", sizeof(buf
));
1376 BIO_printf(bio_err
, "%s\n", value
);
1380 if (!fgets(buf
, sizeof(buf
), stdin
))
1390 else if (buf
[0] == '\n') {
1391 if ((def
== NULL
) || (def
[0] == '\0'))
1393 BUF_strlcpy(buf
, def
, sizeof(buf
));
1394 BUF_strlcat(buf
, "\n", sizeof(buf
));
1395 } else if ((buf
[0] == '.') && (buf
[1] == '\n'))
1399 if (buf
[i
- 1] != '\n') {
1400 BIO_printf(bio_err
, "weird input :-(\n");
1404 #ifdef CHARSET_EBCDIC
1405 ebcdic2ascii(buf
, buf
, i
);
1407 if (!req_check_len(i
, n_min
, n_max
)) {
1413 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
1414 (unsigned char *)buf
, -1, -1, mval
))
1421 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
1422 char *value
, int nid
, int n_min
,
1423 int n_max
, unsigned long chtype
)
1426 static char buf
[1024];
1430 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1431 (void)BIO_flush(bio_err
);
1432 if (value
!= NULL
) {
1433 BUF_strlcpy(buf
, value
, sizeof(buf
));
1434 BUF_strlcat(buf
, "\n", sizeof(buf
));
1435 BIO_printf(bio_err
, "%s\n", value
);
1439 if (!fgets(buf
, sizeof(buf
), stdin
))
1449 else if (buf
[0] == '\n') {
1450 if ((def
== NULL
) || (def
[0] == '\0'))
1452 BUF_strlcpy(buf
, def
, sizeof(buf
));
1453 BUF_strlcat(buf
, "\n", sizeof(buf
));
1454 } else if ((buf
[0] == '.') && (buf
[1] == '\n'))
1458 if (buf
[i
- 1] != '\n') {
1459 BIO_printf(bio_err
, "weird input :-(\n");
1463 #ifdef CHARSET_EBCDIC
1464 ebcdic2ascii(buf
, buf
, i
);
1466 if (!req_check_len(i
, n_min
, n_max
)) {
1472 if (!X509_REQ_add1_attr_by_NID(req
, nid
, chtype
,
1473 (unsigned char *)buf
, -1)) {
1474 BIO_printf(bio_err
, "Error adding attribute\n");
1475 ERR_print_errors(bio_err
);
1484 static int req_check_len(int len
, int n_min
, int n_max
)
1486 if ((n_min
> 0) && (len
< n_min
)) {
1488 "string is too short, it needs to be at least %d bytes long\n",
1492 if ((n_max
>= 0) && (len
> n_max
)) {
1494 "string is too long, it needs to be less than %d bytes long\n",
1501 /* Check if the end of a string matches 'end' */
1502 static int check_end(const char *str
, const char *end
)
1510 tmp
= str
+ slen
- elen
;
1511 return strcmp(tmp
, end
);
1514 static EVP_PKEY_CTX
*set_keygen_ctx(BIO
*err
, const char *gstr
,
1515 int *pkey_type
, long *pkeylen
,
1516 char **palgnam
, ENGINE
*keygen_engine
)
1518 EVP_PKEY_CTX
*gctx
= NULL
;
1519 EVP_PKEY
*param
= NULL
;
1522 const char *paramfile
= NULL
;
1525 *pkey_type
= EVP_PKEY_RSA
;
1527 } else if (gstr
[0] >= '0' && gstr
[0] <= '9') {
1528 *pkey_type
= EVP_PKEY_RSA
;
1529 keylen
= atol(gstr
);
1531 } else if (!strncmp(gstr
, "param:", 6))
1532 paramfile
= gstr
+ 6;
1534 const char *p
= strchr(gstr
, ':');
1537 const EVP_PKEY_ASN1_METHOD
*ameth
;
1544 * The lookup of a the string will cover all engines so keep a note
1545 * of the implementation.
1548 ameth
= EVP_PKEY_asn1_find_str(&tmpeng
, gstr
, len
);
1551 BIO_printf(err
, "Unknown algorithm %.*s\n", len
, gstr
);
1555 EVP_PKEY_asn1_get0_info(NULL
, pkey_type
, NULL
, NULL
, NULL
, ameth
);
1556 #ifndef OPENSSL_NO_ENGINE
1558 ENGINE_finish(tmpeng
);
1560 if (*pkey_type
== EVP_PKEY_RSA
) {
1562 keylen
= atol(p
+ 1);
1571 pbio
= BIO_new_file(paramfile
, "r");
1573 BIO_printf(err
, "Can't open parameter file %s\n", paramfile
);
1576 param
= PEM_read_bio_Parameters(pbio
, NULL
);
1580 (void)BIO_reset(pbio
);
1581 x
= PEM_read_bio_X509(pbio
, NULL
, NULL
, NULL
);
1583 param
= X509_get_pubkey(x
);
1591 BIO_printf(err
, "Error reading parameter file %s\n", paramfile
);
1594 if (*pkey_type
== -1)
1595 *pkey_type
= EVP_PKEY_id(param
);
1596 else if (*pkey_type
!= EVP_PKEY_base_id(param
)) {
1597 BIO_printf(err
, "Key Type does not match parameters\n");
1598 EVP_PKEY_free(param
);
1604 const EVP_PKEY_ASN1_METHOD
*ameth
;
1607 ameth
= EVP_PKEY_asn1_find(&tmpeng
, *pkey_type
);
1609 BIO_puts(err
, "Internal error: can't find key algorithm\n");
1612 EVP_PKEY_asn1_get0_info(NULL
, NULL
, NULL
, NULL
, &anam
, ameth
);
1613 *palgnam
= BUF_strdup(anam
);
1614 #ifndef OPENSSL_NO_ENGINE
1616 ENGINE_finish(tmpeng
);
1621 gctx
= EVP_PKEY_CTX_new(param
, keygen_engine
);
1622 *pkeylen
= EVP_PKEY_bits(param
);
1623 EVP_PKEY_free(param
);
1625 gctx
= EVP_PKEY_CTX_new_id(*pkey_type
, keygen_engine
);
1628 BIO_puts(err
, "Error allocating keygen context\n");
1629 ERR_print_errors(err
);
1633 if (EVP_PKEY_keygen_init(gctx
) <= 0) {
1634 BIO_puts(err
, "Error initializing keygen context\n");
1635 ERR_print_errors(err
);
1638 #ifndef OPENSSL_NO_RSA
1639 if ((*pkey_type
== EVP_PKEY_RSA
) && (keylen
!= -1)) {
1640 if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx
, keylen
) <= 0) {
1641 BIO_puts(err
, "Error setting RSA keysize\n");
1642 ERR_print_errors(err
);
1643 EVP_PKEY_CTX_free(gctx
);
1652 static int genpkey_cb(EVP_PKEY_CTX
*ctx
)
1655 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
1657 p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);
1666 BIO_write(b
, &c
, 1);
1674 static int do_sign_init(BIO
*err
, EVP_MD_CTX
*ctx
, EVP_PKEY
*pkey
,
1675 const EVP_MD
*md
, STACK_OF(OPENSSL_STRING
) *sigopts
)
1677 EVP_PKEY_CTX
*pkctx
= NULL
;
1679 EVP_MD_CTX_init(ctx
);
1680 if (!EVP_DigestSignInit(ctx
, &pkctx
, md
, NULL
, pkey
))
1682 for (i
= 0; i
< sk_OPENSSL_STRING_num(sigopts
); i
++) {
1683 char *sigopt
= sk_OPENSSL_STRING_value(sigopts
, i
);
1684 if (pkey_ctrl_string(pkctx
, sigopt
) <= 0) {
1685 BIO_printf(err
, "parameter error \"%s\"\n", sigopt
);
1686 ERR_print_errors(bio_err
);
1693 int do_X509_sign(BIO
*err
, X509
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1694 STACK_OF(OPENSSL_STRING
) *sigopts
)
1698 EVP_MD_CTX_init(&mctx
);
1699 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1701 rv
= X509_sign_ctx(x
, &mctx
);
1702 EVP_MD_CTX_cleanup(&mctx
);
1703 return rv
> 0 ? 1 : 0;
1706 int do_X509_REQ_sign(BIO
*err
, X509_REQ
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1707 STACK_OF(OPENSSL_STRING
) *sigopts
)
1711 EVP_MD_CTX_init(&mctx
);
1712 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1714 rv
= X509_REQ_sign_ctx(x
, &mctx
);
1715 EVP_MD_CTX_cleanup(&mctx
);
1716 return rv
> 0 ? 1 : 0;
1719 int do_X509_CRL_sign(BIO
*err
, X509_CRL
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1720 STACK_OF(OPENSSL_STRING
) *sigopts
)
1724 EVP_MD_CTX_init(&mctx
);
1725 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1727 rv
= X509_CRL_sign_ctx(x
, &mctx
);
1728 EVP_MD_CTX_cleanup(&mctx
);
1729 return rv
> 0 ? 1 : 0;