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.]
64 #include <openssl/bio.h>
65 #include <openssl/evp.h>
66 #include <openssl/conf.h>
67 #include <openssl/err.h>
68 #include <openssl/asn1.h>
69 #include <openssl/x509.h>
70 #include <openssl/x509v3.h>
71 #include <openssl/objects.h>
72 #include <openssl/pem.h>
73 #include <openssl/bn.h>
74 #ifndef OPENSSL_NO_RSA
75 # include <openssl/rsa.h>
77 #ifndef OPENSSL_NO_DSA
78 # include <openssl/dsa.h>
83 #define BITS "default_bits"
84 #define KEYFILE "default_keyfile"
85 #define PROMPT "prompt"
86 #define DISTINGUISHED_NAME "distinguished_name"
87 #define ATTRIBUTES "attributes"
88 #define V3_EXTENSIONS "x509_extensions"
89 #define REQ_EXTENSIONS "req_extensions"
90 #define STRING_MASK "string_mask"
91 #define UTF8_IN "utf8"
93 #define DEFAULT_KEY_LENGTH 512
94 #define MIN_KEY_LENGTH 384
100 * -inform arg - input format - default PEM (DER or PEM)
101 * -outform arg - output format - default PEM
102 * -in arg - input file - default stdin
103 * -out arg - output file - default stdout
104 * -verify - check request signature
105 * -noout - don't print stuff out.
106 * -text - print out human readable text.
107 * -nodes - no des encryption
108 * -config file - Load configuration file.
109 * -key file - make a request using key in file (or use it for verification).
110 * -keyform arg - key file format.
111 * -rand file(s) - load the file(s) into the PRNG.
112 * -newkey - make a key and a request.
113 * -modulus - print RSA modulus.
114 * -pubkey - output Public Key.
115 * -x509 - output a self signed X509 structure instead.
116 * -asn1-kludge - output new certificate request in a format that some CA's
117 * require. This format is wrong
120 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *dn
, int mutlirdn
,
121 int attribs
, unsigned long chtype
);
122 static int build_subject(X509_REQ
*req
, char *subj
, unsigned long chtype
,
124 static int prompt_info(X509_REQ
*req
,
125 STACK_OF(CONF_VALUE
) *dn_sk
, char *dn_sect
,
126 STACK_OF(CONF_VALUE
) *attr_sk
, char *attr_sect
,
127 int attribs
, unsigned long chtype
);
128 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *sk
,
129 STACK_OF(CONF_VALUE
) *attr
, int attribs
,
130 unsigned long chtype
);
131 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
132 char *value
, int nid
, int n_min
, int n_max
,
133 unsigned long chtype
);
134 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
135 char *value
, int nid
, int n_min
, int n_max
,
136 unsigned long chtype
, int mval
);
137 static int genpkey_cb(EVP_PKEY_CTX
*ctx
);
138 static int req_check_len(int len
, int n_min
, int n_max
);
139 static int check_end(const char *str
, const char *end
);
140 static EVP_PKEY_CTX
*set_keygen_ctx(BIO
*err
, const char *gstr
,
141 int *pkey_type
, long *pkeylen
,
142 char **palgnam
, ENGINE
*keygen_engine
);
144 static char *default_config_file
= NULL
;
146 static CONF
*req_conf
= NULL
;
147 static int batch
= 0;
149 int MAIN(int, char **);
151 int MAIN(int argc
, char **argv
)
153 ENGINE
*e
= NULL
, *gen_eng
= NULL
;
154 unsigned long nmflag
= 0, reqflag
= 0;
155 int ex
= 1, x509
= 0, days
= 30;
157 X509_REQ
*req
= NULL
;
158 EVP_PKEY_CTX
*genctx
= NULL
;
159 const char *keyalg
= NULL
;
160 char *keyalgstr
= NULL
;
161 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
, *sigopts
= NULL
;
162 EVP_PKEY
*pkey
= NULL
;
163 int i
= 0, badops
= 0, newreq
= 0, verbose
= 0, pkey_type
= -1;
165 BIO
*in
= NULL
, *out
= NULL
;
166 int informat
, outformat
, verify
= 0, noout
= 0, text
= 0, keyform
=
168 int nodes
= 0, kludge
= 0, newhdr
= 0, subject
= 0, pubkey
= 0;
169 char *infile
, *outfile
, *prog
, *keyfile
= NULL
, *template =
170 NULL
, *keyout
= NULL
;
171 #ifndef OPENSSL_NO_ENGINE
174 char *extensions
= NULL
;
175 char *req_exts
= NULL
;
176 const EVP_CIPHER
*cipher
= NULL
;
177 ASN1_INTEGER
*serial
= NULL
;
180 char *passargin
= NULL
, *passargout
= NULL
;
181 char *passin
= NULL
, *passout
= NULL
;
185 const EVP_MD
*md_alg
= NULL
, *digest
= NULL
;
186 unsigned long chtype
= MBSTRING_ASC
;
193 #ifndef OPENSSL_NO_DES
194 cipher
= EVP_des_ede3_cbc();
199 if ((bio_err
= BIO_new(BIO_s_file())) != NULL
)
200 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
204 informat
= FORMAT_PEM
;
205 outformat
= FORMAT_PEM
;
211 if (strcmp(*argv
, "-inform") == 0) {
214 informat
= str2fmt(*(++argv
));
215 } else if (strcmp(*argv
, "-outform") == 0) {
218 outformat
= str2fmt(*(++argv
));
220 #ifndef OPENSSL_NO_ENGINE
221 else if (strcmp(*argv
, "-engine") == 0) {
225 } else if (strcmp(*argv
, "-keygen_engine") == 0) {
228 gen_eng
= ENGINE_by_id(*(++argv
));
229 if (gen_eng
== NULL
) {
230 BIO_printf(bio_err
, "Can't find keygen engine %s\n", *argv
);
235 else if (strcmp(*argv
, "-key") == 0) {
239 } else if (strcmp(*argv
, "-pubkey") == 0) {
241 } else if (strcmp(*argv
, "-new") == 0) {
243 } else if (strcmp(*argv
, "-config") == 0) {
246 template = *(++argv
);
247 } else if (strcmp(*argv
, "-keyform") == 0) {
250 keyform
= str2fmt(*(++argv
));
251 } else if (strcmp(*argv
, "-in") == 0) {
255 } else if (strcmp(*argv
, "-out") == 0) {
259 } else if (strcmp(*argv
, "-keyout") == 0) {
263 } else if (strcmp(*argv
, "-passin") == 0) {
266 passargin
= *(++argv
);
267 } else if (strcmp(*argv
, "-passout") == 0) {
270 passargout
= *(++argv
);
271 } else if (strcmp(*argv
, "-rand") == 0) {
275 } else if (strcmp(*argv
, "-newkey") == 0) {
280 } else if (strcmp(*argv
, "-pkeyopt") == 0) {
284 pkeyopts
= sk_OPENSSL_STRING_new_null();
285 if (!pkeyopts
|| !sk_OPENSSL_STRING_push(pkeyopts
, *(++argv
)))
287 } else if (strcmp(*argv
, "-sigopt") == 0) {
291 sigopts
= sk_OPENSSL_STRING_new_null();
292 if (!sigopts
|| !sk_OPENSSL_STRING_push(sigopts
, *(++argv
)))
294 } else if (strcmp(*argv
, "-batch") == 0)
296 else if (strcmp(*argv
, "-newhdr") == 0)
298 else if (strcmp(*argv
, "-modulus") == 0)
300 else if (strcmp(*argv
, "-verify") == 0)
302 else if (strcmp(*argv
, "-nodes") == 0)
304 else if (strcmp(*argv
, "-noout") == 0)
306 else if (strcmp(*argv
, "-verbose") == 0)
308 else if (strcmp(*argv
, "-utf8") == 0)
309 chtype
= MBSTRING_UTF8
;
310 else if (strcmp(*argv
, "-nameopt") == 0) {
313 if (!set_name_ex(&nmflag
, *(++argv
)))
315 } else if (strcmp(*argv
, "-reqopt") == 0) {
318 if (!set_cert_ex(&reqflag
, *(++argv
)))
320 } else if (strcmp(*argv
, "-subject") == 0)
322 else if (strcmp(*argv
, "-text") == 0)
324 else if (strcmp(*argv
, "-x509") == 0)
326 else if (strcmp(*argv
, "-asn1-kludge") == 0)
328 else if (strcmp(*argv
, "-no-asn1-kludge") == 0)
330 else if (strcmp(*argv
, "-subj") == 0) {
334 } else if (strcmp(*argv
, "-multivalue-rdn") == 0)
336 else if (strcmp(*argv
, "-days") == 0) {
339 days
= atoi(*(++argv
));
342 } else if (strcmp(*argv
, "-set_serial") == 0) {
345 serial
= s2i_ASN1_INTEGER(NULL
, *(++argv
));
348 } else if (strcmp(*argv
, "-extensions") == 0) {
351 extensions
= *(++argv
);
352 } else if (strcmp(*argv
, "-reqexts") == 0) {
355 req_exts
= *(++argv
);
356 } else if ((md_alg
= EVP_get_digestbyname(&((*argv
)[1]))) != NULL
) {
360 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
370 BIO_printf(bio_err
, "%s [options] <infile >outfile\n", prog
);
371 BIO_printf(bio_err
, "where options are\n");
372 BIO_printf(bio_err
, " -inform arg input format - DER or PEM\n");
373 BIO_printf(bio_err
, " -outform arg output format - DER or PEM\n");
374 BIO_printf(bio_err
, " -in arg input file\n");
375 BIO_printf(bio_err
, " -out arg output file\n");
376 BIO_printf(bio_err
, " -text text form of request\n");
377 BIO_printf(bio_err
, " -pubkey output public key\n");
378 BIO_printf(bio_err
, " -noout do not output REQ\n");
379 BIO_printf(bio_err
, " -verify verify signature on REQ\n");
380 BIO_printf(bio_err
, " -modulus RSA modulus\n");
381 BIO_printf(bio_err
, " -nodes don't encrypt the output key\n");
382 #ifndef OPENSSL_NO_ENGINE
384 " -engine e use engine e, possibly a hardware device\n");
386 BIO_printf(bio_err
, " -subject output the request's subject\n");
387 BIO_printf(bio_err
, " -passin private key password source\n");
389 " -key file use the private key contained in file\n");
390 BIO_printf(bio_err
, " -keyform arg key file format\n");
391 BIO_printf(bio_err
, " -keyout arg file to send the key to\n");
392 BIO_printf(bio_err
, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR
,
393 LIST_SEPARATOR_CHAR
);
395 " load the file (or the files in the directory) into\n");
396 BIO_printf(bio_err
, " the random number generator\n");
398 " -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
400 " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
401 #ifndef OPENSSL_NO_ECDSA
403 " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
406 " -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
407 BIO_printf(bio_err
, " -config file request template file.\n");
409 " -subj arg set or modify request subject\n");
411 " -multivalue-rdn enable support for multivalued RDNs\n");
412 BIO_printf(bio_err
, " -new new request.\n");
414 " -batch do not ask anything during request generation\n");
416 " -x509 output a x509 structure instead of a cert. req.\n");
418 " -days number of days a certificate generated by -x509 is valid for.\n");
420 " -set_serial serial number to use for a certificate generated by -x509.\n");
422 " -newhdr output \"NEW\" in the header lines\n");
424 " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
426 " have been reported as requiring\n");
428 " -extensions .. specify certificate extension section (override value in config file)\n");
430 " -reqexts .. specify request extension section (override value in config file)\n");
432 " -utf8 input characters are UTF8 (default ASCII)\n");
434 " -nameopt arg - various certificate name options\n");
436 " -reqopt arg - various request text options\n\n");
440 ERR_load_crypto_strings();
441 if (!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
)) {
442 BIO_printf(bio_err
, "Error getting passwords\n");
445 #ifndef MONOLITH /* else this has happened in openssl.c
446 * (global `config') */
447 /* Lets load up our environment a little */
448 p
= getenv("OPENSSL_CONF");
450 p
= getenv("SSLEAY_CONF");
452 p
= to_free
= make_config_name();
453 default_config_file
= p
;
454 config
= NCONF_new(NULL
);
455 i
= NCONF_load(config
, p
, &errline
);
458 if (template != NULL
) {
462 BIO_printf(bio_err
, "Using configuration from %s\n", template);
463 req_conf
= NCONF_new(NULL
);
464 i
= NCONF_load(req_conf
, template, &errline
);
466 BIO_printf(bio_err
, "error on line %ld of %s\n", errline
,
473 if (req_conf
== NULL
) {
474 BIO_printf(bio_err
, "Unable to load config info from %s\n",
475 default_config_file
);
479 BIO_printf(bio_err
, "Using configuration from %s\n",
480 default_config_file
);
483 if (req_conf
!= NULL
) {
484 if (!load_config(bio_err
, req_conf
))
486 p
= NCONF_get_string(req_conf
, NULL
, "oid_file");
492 oid_bio
= BIO_new_file(p
, "r");
493 if (oid_bio
== NULL
) {
495 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
496 ERR_print_errors(bio_err);
499 OBJ_create_objects(oid_bio
);
504 if (!add_oid_section(bio_err
, req_conf
))
507 if (md_alg
== NULL
) {
508 p
= NCONF_get_string(req_conf
, SECTION
, "default_md");
512 if ((md_alg
= EVP_get_digestbyname(p
)) != NULL
)
518 extensions
= NCONF_get_string(req_conf
, SECTION
, V3_EXTENSIONS
);
523 /* Check syntax of file */
525 X509V3_set_ctx_test(&ctx
);
526 X509V3_set_nconf(&ctx
, req_conf
);
527 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, extensions
, NULL
)) {
529 "Error Loading extension section %s\n", extensions
);
535 passin
= NCONF_get_string(req_conf
, SECTION
, "input_password");
541 passout
= NCONF_get_string(req_conf
, SECTION
, "output_password");
546 p
= NCONF_get_string(req_conf
, SECTION
, STRING_MASK
);
550 if (p
&& !ASN1_STRING_set_default_mask_asc(p
)) {
551 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", p
);
555 if (chtype
!= MBSTRING_UTF8
) {
556 p
= NCONF_get_string(req_conf
, SECTION
, UTF8_IN
);
559 else if (!strcmp(p
, "yes"))
560 chtype
= MBSTRING_UTF8
;
564 req_exts
= NCONF_get_string(req_conf
, SECTION
, REQ_EXTENSIONS
);
569 /* Check syntax of file */
571 X509V3_set_ctx_test(&ctx
);
572 X509V3_set_nconf(&ctx
, req_conf
);
573 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, req_exts
, NULL
)) {
575 "Error Loading request extension section %s\n",
581 in
= BIO_new(BIO_s_file());
582 out
= BIO_new(BIO_s_file());
583 if ((in
== NULL
) || (out
== NULL
))
586 #ifndef OPENSSL_NO_ENGINE
587 e
= setup_engine(bio_err
, engine
, 0);
590 if (keyfile
!= NULL
) {
591 pkey
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
595 * load_key() has already printed an appropriate message
599 char *randfile
= NCONF_get_string(req_conf
, SECTION
, "RANDFILE");
600 if (randfile
== NULL
)
602 app_RAND_load_file(randfile
, bio_err
, 0);
606 if (newreq
&& (pkey
== NULL
)) {
607 char *randfile
= NCONF_get_string(req_conf
, SECTION
, "RANDFILE");
608 if (randfile
== NULL
)
610 app_RAND_load_file(randfile
, bio_err
, 0);
612 app_RAND_load_files(inrand
);
614 if (!NCONF_get_number(req_conf
, SECTION
, BITS
, &newkey
)) {
615 newkey
= DEFAULT_KEY_LENGTH
;
619 genctx
= set_keygen_ctx(bio_err
, keyalg
, &pkey_type
, &newkey
,
620 &keyalgstr
, gen_eng
);
625 if (newkey
< MIN_KEY_LENGTH
626 && (pkey_type
== EVP_PKEY_RSA
|| pkey_type
== EVP_PKEY_DSA
)) {
627 BIO_printf(bio_err
, "private key length is too short,\n");
628 BIO_printf(bio_err
, "it needs to be at least %d bits, not %ld\n",
629 MIN_KEY_LENGTH
, newkey
);
634 genctx
= set_keygen_ctx(bio_err
, NULL
, &pkey_type
, &newkey
,
635 &keyalgstr
, gen_eng
);
642 for (i
= 0; i
< sk_OPENSSL_STRING_num(pkeyopts
); i
++) {
643 genopt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
644 if (pkey_ctrl_string(genctx
, genopt
) <= 0) {
645 BIO_printf(bio_err
, "parameter error \"%s\"\n", genopt
);
646 ERR_print_errors(bio_err
);
652 BIO_printf(bio_err
, "Generating a %ld bit %s private key\n",
655 EVP_PKEY_CTX_set_cb(genctx
, genpkey_cb
);
656 EVP_PKEY_CTX_set_app_data(genctx
, bio_err
);
658 if (EVP_PKEY_keygen(genctx
, &pkey
) <= 0) {
659 BIO_puts(bio_err
, "Error Generating Key\n");
663 EVP_PKEY_CTX_free(genctx
);
666 app_RAND_write_file(randfile
, bio_err
);
668 if (keyout
== NULL
) {
669 keyout
= NCONF_get_string(req_conf
, SECTION
, KEYFILE
);
674 if (keyout
== NULL
) {
675 BIO_printf(bio_err
, "writing new private key to stdout\n");
676 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
677 #ifdef OPENSSL_SYS_VMS
679 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
680 out
= BIO_push(tmpbio
, out
);
684 BIO_printf(bio_err
, "writing new private key to '%s'\n", keyout
);
685 if (BIO_write_filename(out
, keyout
) <= 0) {
691 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_rsa_key");
694 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_key");
698 if ((p
!= NULL
) && (strcmp(p
, "no") == 0))
705 if (!PEM_write_bio_PrivateKey(out
, pkey
, cipher
,
706 NULL
, 0, NULL
, passout
)) {
707 if ((ERR_GET_REASON(ERR_peek_error()) ==
708 PEM_R_PROBLEMS_GETTING_PASSWORD
) && (i
< 3)) {
715 BIO_printf(bio_err
, "-----\n");
720 * Since we are using a pre-existing certificate request, the kludge
721 * 'format' info should not be changed.
725 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
727 if (BIO_read_filename(in
, infile
) <= 0) {
733 if (informat
== FORMAT_ASN1
)
734 req
= d2i_X509_REQ_bio(in
, NULL
);
735 else if (informat
== FORMAT_PEM
)
736 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
739 "bad input format specified for X509 request\n");
743 BIO_printf(bio_err
, "unable to load X509 request\n");
748 if (newreq
|| x509
) {
750 BIO_printf(bio_err
, "you need to specify a private key\n");
755 req
= X509_REQ_new();
760 i
= make_REQ(req
, pkey
, subj
, multirdn
, !x509
, chtype
);
761 subj
= NULL
; /* done processing '-subj' option */
763 && !sk_X509_ATTRIBUTE_num(req
->req_info
->attributes
)) {
764 sk_X509_ATTRIBUTE_free(req
->req_info
->attributes
);
765 req
->req_info
->attributes
= NULL
;
768 BIO_printf(bio_err
, "problems making Certificate Request\n");
775 if ((x509ss
= X509_new()) == NULL
)
778 /* Set version to V3 */
779 if (extensions
&& !X509_set_version(x509ss
, 2))
782 if (!X509_set_serialNumber(x509ss
, serial
))
785 if (!rand_serial(NULL
, X509_get_serialNumber(x509ss
)))
789 if (!X509_set_issuer_name(x509ss
, X509_REQ_get_subject_name(req
)))
791 if (!X509_gmtime_adj(X509_get_notBefore(x509ss
), 0))
793 if (!X509_time_adj_ex(X509_get_notAfter(x509ss
), days
, 0, NULL
))
795 if (!X509_set_subject_name
796 (x509ss
, X509_REQ_get_subject_name(req
)))
798 tmppkey
= X509_REQ_get_pubkey(req
);
799 if (!tmppkey
|| !X509_set_pubkey(x509ss
, tmppkey
))
801 EVP_PKEY_free(tmppkey
);
803 /* Set up V3 context struct */
805 X509V3_set_ctx(&ext_ctx
, x509ss
, x509ss
, NULL
, NULL
, 0);
806 X509V3_set_nconf(&ext_ctx
, req_conf
);
809 if (extensions
&& !X509V3_EXT_add_nconf(req_conf
,
810 &ext_ctx
, extensions
,
812 BIO_printf(bio_err
, "Error Loading extension section %s\n",
817 i
= do_X509_sign(bio_err
, x509ss
, pkey
, digest
, sigopts
);
819 ERR_print_errors(bio_err
);
825 /* Set up V3 context struct */
827 X509V3_set_ctx(&ext_ctx
, NULL
, NULL
, req
, NULL
, 0);
828 X509V3_set_nconf(&ext_ctx
, req_conf
);
831 if (req_exts
&& !X509V3_EXT_REQ_add_nconf(req_conf
,
834 BIO_printf(bio_err
, "Error Loading extension section %s\n",
838 i
= do_X509_REQ_sign(bio_err
, req
, pkey
, digest
, sigopts
);
840 ERR_print_errors(bio_err
);
847 BIO_printf(bio_err
, "Cannot modify certificate subject\n");
853 BIO_printf(bio_err
, "Modifying Request's Subject\n");
854 print_name(bio_err
, "old subject=",
855 X509_REQ_get_subject_name(req
), nmflag
);
858 if (build_subject(req
, subj
, chtype
, multirdn
) == 0) {
859 BIO_printf(bio_err
, "ERROR: cannot modify subject\n");
864 req
->req_info
->enc
.modified
= 1;
867 print_name(bio_err
, "new subject=",
868 X509_REQ_get_subject_name(req
), nmflag
);
872 if (verify
&& !x509
) {
876 pkey
= X509_REQ_get_pubkey(req
);
882 i
= X509_REQ_verify(req
, pkey
);
891 BIO_printf(bio_err
, "verify failure\n");
892 ERR_print_errors(bio_err
);
893 } else /* if (i > 0) */
894 BIO_printf(bio_err
, "verify OK\n");
897 if (noout
&& !text
&& !modulus
&& !subject
&& !pubkey
) {
902 if (outfile
== NULL
) {
903 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
904 #ifdef OPENSSL_SYS_VMS
906 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
907 out
= BIO_push(tmpbio
, out
);
911 if ((keyout
!= NULL
) && (strcmp(outfile
, keyout
) == 0))
912 i
= (int)BIO_append_filename(out
, outfile
);
914 i
= (int)BIO_write_filename(out
, outfile
);
923 tpubkey
= X509_REQ_get_pubkey(req
);
924 if (tpubkey
== NULL
) {
925 BIO_printf(bio_err
, "Error getting public key\n");
926 ERR_print_errors(bio_err
);
929 PEM_write_bio_PUBKEY(out
, tpubkey
);
930 EVP_PKEY_free(tpubkey
);
935 X509_print_ex(out
, x509ss
, nmflag
, reqflag
);
937 X509_REQ_print_ex(out
, req
, nmflag
, reqflag
);
942 print_name(out
, "subject=", X509_get_subject_name(x509ss
),
945 print_name(out
, "subject=", X509_REQ_get_subject_name(req
),
953 tpubkey
= X509_get_pubkey(x509ss
);
955 tpubkey
= X509_REQ_get_pubkey(req
);
956 if (tpubkey
== NULL
) {
957 fprintf(stdout
, "Modulus=unavailable\n");
960 fprintf(stdout
, "Modulus=");
961 #ifndef OPENSSL_NO_RSA
962 if (EVP_PKEY_base_id(tpubkey
) == EVP_PKEY_RSA
)
963 BN_print(out
, tpubkey
->pkey
.rsa
->n
);
966 fprintf(stdout
, "Wrong Algorithm type");
967 EVP_PKEY_free(tpubkey
);
968 fprintf(stdout
, "\n");
971 if (!noout
&& !x509
) {
972 if (outformat
== FORMAT_ASN1
)
973 i
= i2d_X509_REQ_bio(out
, req
);
974 else if (outformat
== FORMAT_PEM
) {
976 i
= PEM_write_bio_X509_REQ_NEW(out
, req
);
978 i
= PEM_write_bio_X509_REQ(out
, req
);
980 BIO_printf(bio_err
, "bad output format specified for outfile\n");
984 BIO_printf(bio_err
, "unable to write X509 request\n");
988 if (!noout
&& x509
&& (x509ss
!= NULL
)) {
989 if (outformat
== FORMAT_ASN1
)
990 i
= i2d_X509_bio(out
, x509ss
);
991 else if (outformat
== FORMAT_PEM
)
992 i
= PEM_write_bio_X509(out
, x509ss
);
994 BIO_printf(bio_err
, "bad output format specified for outfile\n");
998 BIO_printf(bio_err
, "unable to write X509 certificate\n");
1006 OPENSSL_free(to_free
);
1009 ERR_print_errors(bio_err
);
1011 if ((req_conf
!= NULL
) && (req_conf
!= config
))
1012 NCONF_free(req_conf
);
1015 EVP_PKEY_free(pkey
);
1017 EVP_PKEY_CTX_free(genctx
);
1019 sk_OPENSSL_STRING_free(pkeyopts
);
1021 sk_OPENSSL_STRING_free(sigopts
);
1022 #ifndef OPENSSL_NO_ENGINE
1024 ENGINE_free(gen_eng
);
1027 OPENSSL_free(keyalgstr
);
1030 ASN1_INTEGER_free(serial
);
1031 if (passargin
&& passin
)
1032 OPENSSL_free(passin
);
1033 if (passargout
&& passout
)
1034 OPENSSL_free(passout
);
1040 static int make_REQ(X509_REQ
*req
, EVP_PKEY
*pkey
, char *subj
, int multirdn
,
1041 int attribs
, unsigned long chtype
)
1045 STACK_OF(CONF_VALUE
) *dn_sk
, *attr_sk
= NULL
;
1046 char *tmp
, *dn_sect
, *attr_sect
;
1048 tmp
= NCONF_get_string(req_conf
, SECTION
, PROMPT
);
1051 if ((tmp
!= NULL
) && !strcmp(tmp
, "no"))
1054 dn_sect
= NCONF_get_string(req_conf
, SECTION
, DISTINGUISHED_NAME
);
1055 if (dn_sect
== NULL
) {
1056 BIO_printf(bio_err
, "unable to find '%s' in config\n",
1057 DISTINGUISHED_NAME
);
1060 dn_sk
= NCONF_get_section(req_conf
, dn_sect
);
1061 if (dn_sk
== NULL
) {
1062 BIO_printf(bio_err
, "unable to get '%s' section\n", dn_sect
);
1066 attr_sect
= NCONF_get_string(req_conf
, SECTION
, ATTRIBUTES
);
1067 if (attr_sect
== NULL
) {
1071 attr_sk
= NCONF_get_section(req_conf
, attr_sect
);
1072 if (attr_sk
== NULL
) {
1073 BIO_printf(bio_err
, "unable to get '%s' section\n", attr_sect
);
1078 /* setup version number */
1079 if (!X509_REQ_set_version(req
, 0L))
1080 goto err
; /* version 1 */
1083 i
= build_subject(req
, subj
, chtype
, multirdn
);
1085 i
= auto_info(req
, dn_sk
, attr_sk
, attribs
, chtype
);
1087 i
= prompt_info(req
, dn_sk
, dn_sect
, attr_sk
, attr_sect
, attribs
,
1092 if (!X509_REQ_set_pubkey(req
, pkey
))
1101 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
1102 * where characters may be escaped by \
1104 static int build_subject(X509_REQ
*req
, char *subject
, unsigned long chtype
,
1109 if (!(n
= parse_name(subject
, chtype
, multirdn
)))
1112 if (!X509_REQ_set_subject_name(req
, n
)) {
1120 static int prompt_info(X509_REQ
*req
,
1121 STACK_OF(CONF_VALUE
) *dn_sk
, char *dn_sect
,
1122 STACK_OF(CONF_VALUE
) *attr_sk
, char *attr_sect
,
1123 int attribs
, unsigned long chtype
)
1134 subj
= X509_REQ_get_subject_name(req
);
1138 "You are about to be asked to enter information that will be incorporated\n");
1139 BIO_printf(bio_err
, "into your certificate request.\n");
1141 "What you are about to enter is what is called a Distinguished Name or a DN.\n");
1143 "There are quite a few fields but you can leave some blank\n");
1145 "For some fields there will be a default value,\n");
1147 "If you enter '.', the field will be left blank.\n");
1148 BIO_printf(bio_err
, "-----\n");
1151 if (sk_CONF_VALUE_num(dn_sk
)) {
1155 if (sk_CONF_VALUE_num(dn_sk
) <= i
)
1158 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1161 if (!check_end(type
, "_min") || !check_end(type
, "_max") ||
1162 !check_end(type
, "_default") || !check_end(type
, "_value"))
1165 * Skip past any leading X. X: X, etc to allow for multiple
1168 for (p
= v
->name
; *p
; p
++)
1169 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1180 /* If OBJ not recognised ignore it */
1181 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1183 if (BIO_snprintf(buf
, sizeof buf
, "%s_default", v
->name
)
1184 >= (int)sizeof(buf
)) {
1185 BIO_printf(bio_err
, "Name '%s' too long\n", v
->name
);
1189 if ((def
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1194 BIO_snprintf(buf
, sizeof buf
, "%s_value", v
->name
);
1195 if ((value
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1200 BIO_snprintf(buf
, sizeof buf
, "%s_min", v
->name
);
1201 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_min
)) {
1206 BIO_snprintf(buf
, sizeof buf
, "%s_max", v
->name
);
1207 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_max
)) {
1212 if (!add_DN_object(subj
, v
->value
, def
, value
, nid
,
1213 n_min
, n_max
, chtype
, mval
))
1216 if (X509_NAME_entry_count(subj
) == 0) {
1218 "error, no objects specified in config file\n");
1223 if ((attr_sk
!= NULL
) && (sk_CONF_VALUE_num(attr_sk
) > 0)
1226 "\nPlease enter the following 'extra' attributes\n");
1228 "to be sent with your certificate request\n");
1234 if ((attr_sk
== NULL
) || (sk_CONF_VALUE_num(attr_sk
) <= i
))
1237 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1239 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1242 if (BIO_snprintf(buf
, sizeof buf
, "%s_default", type
)
1243 >= (int)sizeof(buf
)) {
1244 BIO_printf(bio_err
, "Name '%s' too long\n", v
->name
);
1248 if ((def
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1254 BIO_snprintf(buf
, sizeof buf
, "%s_value", type
);
1255 if ((value
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1261 BIO_snprintf(buf
, sizeof buf
, "%s_min", type
);
1262 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_min
)) {
1267 BIO_snprintf(buf
, sizeof buf
, "%s_max", type
);
1268 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_max
)) {
1273 if (!add_attribute_object(req
,
1274 v
->value
, def
, value
, nid
, n_min
,
1280 BIO_printf(bio_err
, "No template, please set one up.\n");
1288 static int auto_info(X509_REQ
*req
, STACK_OF(CONF_VALUE
) *dn_sk
,
1289 STACK_OF(CONF_VALUE
) *attr_sk
, int attribs
,
1290 unsigned long chtype
)
1298 subj
= X509_REQ_get_subject_name(req
);
1300 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1302 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1306 * Skip past any leading X. X: X, etc to allow for multiple instances
1308 for (p
= v
->name
; *p
; p
++)
1309 #ifndef CHARSET_EBCDIC
1310 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1312 if ((*p
== os_toascii
[':']) || (*p
== os_toascii
[','])
1313 || (*p
== os_toascii
['.'])) {
1320 #ifndef CHARSET_EBCDIC
1323 if (*p
== os_toascii
['+'])
1330 if (!X509_NAME_add_entry_by_txt(subj
, type
, chtype
,
1331 (unsigned char *)v
->value
, -1, -1,
1337 if (!X509_NAME_entry_count(subj
)) {
1338 BIO_printf(bio_err
, "error, no objects specified in config file\n");
1342 for (i
= 0; i
< sk_CONF_VALUE_num(attr_sk
); i
++) {
1343 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1344 if (!X509_REQ_add1_attr_by_txt(req
, v
->name
, chtype
,
1345 (unsigned char *)v
->value
, -1))
1352 static int add_DN_object(X509_NAME
*n
, char *text
, const char *def
,
1353 char *value
, int nid
, int n_min
, int n_max
,
1354 unsigned long chtype
, int mval
)
1357 MS_STATIC
char buf
[1024];
1360 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1361 (void)BIO_flush(bio_err
);
1362 if (value
!= NULL
) {
1363 BUF_strlcpy(buf
, value
, sizeof buf
);
1364 BUF_strlcat(buf
, "\n", sizeof buf
);
1365 BIO_printf(bio_err
, "%s\n", value
);
1369 if (!fgets(buf
, sizeof buf
, stdin
))
1379 else if (buf
[0] == '\n') {
1380 if ((def
== NULL
) || (def
[0] == '\0'))
1382 BUF_strlcpy(buf
, def
, sizeof buf
);
1383 BUF_strlcat(buf
, "\n", sizeof buf
);
1384 } else if ((buf
[0] == '.') && (buf
[1] == '\n'))
1388 if (buf
[i
- 1] != '\n') {
1389 BIO_printf(bio_err
, "weird input :-(\n");
1393 #ifdef CHARSET_EBCDIC
1394 ebcdic2ascii(buf
, buf
, i
);
1396 if (!req_check_len(i
, n_min
, n_max
)) {
1402 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
1403 (unsigned char *)buf
, -1, -1, mval
))
1410 static int add_attribute_object(X509_REQ
*req
, char *text
, const char *def
,
1411 char *value
, int nid
, int n_min
,
1412 int n_max
, unsigned long chtype
)
1415 static char buf
[1024];
1419 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1420 (void)BIO_flush(bio_err
);
1421 if (value
!= NULL
) {
1422 BUF_strlcpy(buf
, value
, sizeof buf
);
1423 BUF_strlcat(buf
, "\n", sizeof buf
);
1424 BIO_printf(bio_err
, "%s\n", value
);
1428 if (!fgets(buf
, sizeof buf
, stdin
))
1438 else if (buf
[0] == '\n') {
1439 if ((def
== NULL
) || (def
[0] == '\0'))
1441 BUF_strlcpy(buf
, def
, sizeof buf
);
1442 BUF_strlcat(buf
, "\n", sizeof buf
);
1443 } else if ((buf
[0] == '.') && (buf
[1] == '\n'))
1447 if (buf
[i
- 1] != '\n') {
1448 BIO_printf(bio_err
, "weird input :-(\n");
1452 #ifdef CHARSET_EBCDIC
1453 ebcdic2ascii(buf
, buf
, i
);
1455 if (!req_check_len(i
, n_min
, n_max
)) {
1461 if (!X509_REQ_add1_attr_by_NID(req
, nid
, chtype
,
1462 (unsigned char *)buf
, -1)) {
1463 BIO_printf(bio_err
, "Error adding attribute\n");
1464 ERR_print_errors(bio_err
);
1473 static int req_check_len(int len
, int n_min
, int n_max
)
1475 if ((n_min
> 0) && (len
< n_min
)) {
1477 "string is too short, it needs to be at least %d bytes long\n",
1481 if ((n_max
>= 0) && (len
> n_max
)) {
1483 "string is too long, it needs to be less than %d bytes long\n",
1490 /* Check if the end of a string matches 'end' */
1491 static int check_end(const char *str
, const char *end
)
1499 tmp
= str
+ slen
- elen
;
1500 return strcmp(tmp
, end
);
1503 static EVP_PKEY_CTX
*set_keygen_ctx(BIO
*err
, const char *gstr
,
1504 int *pkey_type
, long *pkeylen
,
1505 char **palgnam
, ENGINE
*keygen_engine
)
1507 EVP_PKEY_CTX
*gctx
= NULL
;
1508 EVP_PKEY
*param
= NULL
;
1511 const char *paramfile
= NULL
;
1514 *pkey_type
= EVP_PKEY_RSA
;
1516 } else if (gstr
[0] >= '0' && gstr
[0] <= '9') {
1517 *pkey_type
= EVP_PKEY_RSA
;
1518 keylen
= atol(gstr
);
1520 } else if (!strncmp(gstr
, "param:", 6))
1521 paramfile
= gstr
+ 6;
1523 const char *p
= strchr(gstr
, ':');
1526 const EVP_PKEY_ASN1_METHOD
*ameth
;
1533 * The lookup of a the string will cover all engines so keep a note
1534 * of the implementation.
1537 ameth
= EVP_PKEY_asn1_find_str(&tmpeng
, gstr
, len
);
1540 BIO_printf(err
, "Unknown algorithm %.*s\n", len
, gstr
);
1544 EVP_PKEY_asn1_get0_info(NULL
, pkey_type
, NULL
, NULL
, NULL
, ameth
);
1545 #ifndef OPENSSL_NO_ENGINE
1547 ENGINE_finish(tmpeng
);
1549 if (*pkey_type
== EVP_PKEY_RSA
) {
1551 keylen
= atol(p
+ 1);
1560 pbio
= BIO_new_file(paramfile
, "r");
1562 BIO_printf(err
, "Can't open parameter file %s\n", paramfile
);
1565 param
= PEM_read_bio_Parameters(pbio
, NULL
);
1569 (void)BIO_reset(pbio
);
1570 x
= PEM_read_bio_X509(pbio
, NULL
, NULL
, NULL
);
1572 param
= X509_get_pubkey(x
);
1580 BIO_printf(err
, "Error reading parameter file %s\n", paramfile
);
1583 if (*pkey_type
== -1)
1584 *pkey_type
= EVP_PKEY_id(param
);
1585 else if (*pkey_type
!= EVP_PKEY_base_id(param
)) {
1586 BIO_printf(err
, "Key Type does not match parameters\n");
1587 EVP_PKEY_free(param
);
1593 const EVP_PKEY_ASN1_METHOD
*ameth
;
1596 ameth
= EVP_PKEY_asn1_find(&tmpeng
, *pkey_type
);
1598 BIO_puts(err
, "Internal error: can't find key algorithm\n");
1601 EVP_PKEY_asn1_get0_info(NULL
, NULL
, NULL
, NULL
, &anam
, ameth
);
1602 *palgnam
= BUF_strdup(anam
);
1603 #ifndef OPENSSL_NO_ENGINE
1605 ENGINE_finish(tmpeng
);
1610 gctx
= EVP_PKEY_CTX_new(param
, keygen_engine
);
1611 *pkeylen
= EVP_PKEY_bits(param
);
1612 EVP_PKEY_free(param
);
1614 gctx
= EVP_PKEY_CTX_new_id(*pkey_type
, keygen_engine
);
1617 BIO_puts(err
, "Error allocating keygen context\n");
1618 ERR_print_errors(err
);
1622 if (EVP_PKEY_keygen_init(gctx
) <= 0) {
1623 BIO_puts(err
, "Error initializing keygen context\n");
1624 ERR_print_errors(err
);
1627 #ifndef OPENSSL_NO_RSA
1628 if ((*pkey_type
== EVP_PKEY_RSA
) && (keylen
!= -1)) {
1629 if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx
, keylen
) <= 0) {
1630 BIO_puts(err
, "Error setting RSA keysize\n");
1631 ERR_print_errors(err
);
1632 EVP_PKEY_CTX_free(gctx
);
1641 static int genpkey_cb(EVP_PKEY_CTX
*ctx
)
1644 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
1646 p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);
1655 BIO_write(b
, &c
, 1);
1660 static int do_sign_init(BIO
*err
, EVP_MD_CTX
*ctx
, EVP_PKEY
*pkey
,
1661 const EVP_MD
*md
, STACK_OF(OPENSSL_STRING
) *sigopts
)
1663 EVP_PKEY_CTX
*pkctx
= NULL
;
1665 EVP_MD_CTX_init(ctx
);
1666 if (!EVP_DigestSignInit(ctx
, &pkctx
, md
, NULL
, pkey
))
1668 for (i
= 0; i
< sk_OPENSSL_STRING_num(sigopts
); i
++) {
1669 char *sigopt
= sk_OPENSSL_STRING_value(sigopts
, i
);
1670 if (pkey_ctrl_string(pkctx
, sigopt
) <= 0) {
1671 BIO_printf(err
, "parameter error \"%s\"\n", sigopt
);
1672 ERR_print_errors(bio_err
);
1679 int do_X509_sign(BIO
*err
, X509
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1680 STACK_OF(OPENSSL_STRING
) *sigopts
)
1684 EVP_MD_CTX_init(&mctx
);
1685 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1687 rv
= X509_sign_ctx(x
, &mctx
);
1688 EVP_MD_CTX_cleanup(&mctx
);
1689 return rv
> 0 ? 1 : 0;
1692 int do_X509_REQ_sign(BIO
*err
, X509_REQ
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1693 STACK_OF(OPENSSL_STRING
) *sigopts
)
1697 EVP_MD_CTX_init(&mctx
);
1698 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1700 rv
= X509_REQ_sign_ctx(x
, &mctx
);
1701 EVP_MD_CTX_cleanup(&mctx
);
1702 return rv
> 0 ? 1 : 0;
1705 int do_X509_CRL_sign(BIO
*err
, X509_CRL
*x
, EVP_PKEY
*pkey
, const EVP_MD
*md
,
1706 STACK_OF(OPENSSL_STRING
) *sigopts
)
1710 EVP_MD_CTX_init(&mctx
);
1711 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1713 rv
= X509_CRL_sign_ctx(x
, &mctx
);
1714 EVP_MD_CTX_cleanup(&mctx
);
1715 return rv
> 0 ? 1 : 0;