1 /* $OpenBSD: req.c,v 1.14 2017/01/20 08:57:12 deraadt Exp $ */
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.]
59 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
60 * deprecated functions for openssl-internal code */
61 #ifdef OPENSSL_NO_DEPRECATED
62 #undef OPENSSL_NO_DEPRECATED
73 #include <openssl/asn1.h>
74 #include <openssl/bio.h>
75 #include <openssl/bn.h>
76 #include <openssl/conf.h>
77 #include <openssl/err.h>
78 #include <openssl/evp.h>
79 #include <openssl/objects.h>
80 #include <openssl/pem.h>
81 #include <openssl/x509.h>
82 #include <openssl/x509v3.h>
84 #include <openssl/dsa.h>
86 #include <openssl/rsa.h>
90 #define BITS "default_bits"
91 #define KEYFILE "default_keyfile"
92 #define PROMPT "prompt"
93 #define DISTINGUISHED_NAME "distinguished_name"
94 #define ATTRIBUTES "attributes"
95 #define V3_EXTENSIONS "x509_extensions"
96 #define REQ_EXTENSIONS "req_extensions"
97 #define STRING_MASK "string_mask"
98 #define UTF8_IN "utf8"
100 #define DEFAULT_KEY_LENGTH 2048
101 #define MIN_KEY_LENGTH 384
104 /* -inform arg - input format - default PEM (DER or PEM)
105 * -outform arg - output format - default PEM
106 * -in arg - input file - default stdin
107 * -out arg - output file - default stdout
108 * -verify - check request signature
109 * -noout - don't print stuff out.
110 * -text - print out human readable text.
111 * -nodes - no des encryption
112 * -config file - Load configuration file.
113 * -key file - make a request using key in file (or use it for verification).
114 * -keyform arg - key file format.
115 * -newkey - make a key and a request.
116 * -modulus - print RSA modulus.
117 * -pubkey - output Public Key.
118 * -x509 - output a self signed X509 structure instead.
119 * -asn1-kludge - output new certificate request in a format that some CA's
120 * require. This format is wrong
123 static int make_REQ(X509_REQ
* req
, EVP_PKEY
* pkey
, char *dn
, int multirdn
,
124 int attribs
, unsigned long chtype
);
125 static int build_subject(X509_REQ
* req
, char *subj
, unsigned long chtype
,
127 static int prompt_info(X509_REQ
* req
,
128 STACK_OF(CONF_VALUE
) * dn_sk
, char *dn_sect
,
129 STACK_OF(CONF_VALUE
) * attr_sk
, char *attr_sect
, int attribs
,
130 unsigned long chtype
);
131 static int auto_info(X509_REQ
* req
, STACK_OF(CONF_VALUE
) * sk
,
132 STACK_OF(CONF_VALUE
) * attr
, int attribs
,
133 unsigned long chtype
);
134 static int add_attribute_object(X509_REQ
* req
, char *text
, const char *def
,
135 char *value
, int nid
, int n_min
,
136 int n_max
, unsigned long chtype
);
137 static int add_DN_object(X509_NAME
* n
, char *text
, const char *def
, char *value
,
138 int nid
, int n_min
, int n_max
, unsigned long chtype
, int mval
);
139 static int genpkey_cb(EVP_PKEY_CTX
* ctx
);
140 static int req_check_len(int len
, int n_min
, int n_max
);
141 static int check_end(const char *str
, const char *end
);
142 static EVP_PKEY_CTX
*set_keygen_ctx(BIO
* err
, const char *gstr
, int *pkey_type
,
143 long *pkeylen
, char **palgnam
);
144 static CONF
*req_conf
= NULL
;
145 static int batch
= 0;
148 req_main(int argc
, char **argv
)
150 unsigned long nmflag
= 0, reqflag
= 0;
151 int ex
= 1, x509
= 0, days
= 30;
153 X509_REQ
*req
= NULL
;
154 EVP_PKEY_CTX
*genctx
= NULL
;
155 const char *keyalg
= NULL
;
156 char *keyalgstr
= NULL
;
157 STACK_OF(OPENSSL_STRING
) * pkeyopts
= NULL
, *sigopts
= NULL
;
158 EVP_PKEY
*pkey
= NULL
;
159 int i
= 0, badops
= 0, newreq
= 0, verbose
= 0, pkey_type
= -1;
161 BIO
*in
= NULL
, *out
= NULL
;
162 int informat
, outformat
, verify
= 0, noout
= 0, text
= 0, keyform
= FORMAT_PEM
;
163 int nodes
= 0, kludge
= 0, newhdr
= 0, subject
= 0, pubkey
= 0;
164 char *infile
, *outfile
, *prog
, *keyfile
= NULL
, *template = NULL
,
166 char *extensions
= NULL
;
167 char *req_exts
= NULL
;
168 const EVP_CIPHER
*cipher
= NULL
;
169 ASN1_INTEGER
*serial
= NULL
;
171 char *passargin
= NULL
, *passargout
= NULL
;
172 char *passin
= NULL
, *passout
= NULL
;
176 const EVP_MD
*md_alg
= NULL
, *digest
= NULL
;
177 unsigned long chtype
= MBSTRING_ASC
;
179 if (single_execution
) {
180 if (pledge("stdio cpath wpath rpath tty", NULL
) == -1) {
187 cipher
= EVP_aes_256_cbc();
188 digest
= EVP_sha256();
192 informat
= FORMAT_PEM
;
193 outformat
= FORMAT_PEM
;
199 if (strcmp(*argv
, "-inform") == 0) {
202 informat
= str2fmt(*(++argv
));
203 } else if (strcmp(*argv
, "-outform") == 0) {
206 outformat
= str2fmt(*(++argv
));
208 else if (strcmp(*argv
, "-key") == 0) {
212 } else if (strcmp(*argv
, "-pubkey") == 0) {
214 } else if (strcmp(*argv
, "-new") == 0) {
216 } else if (strcmp(*argv
, "-config") == 0) {
219 template = *(++argv
);
220 } else if (strcmp(*argv
, "-keyform") == 0) {
223 keyform
= str2fmt(*(++argv
));
224 } else if (strcmp(*argv
, "-in") == 0) {
228 } else if (strcmp(*argv
, "-out") == 0) {
232 } else if (strcmp(*argv
, "-keyout") == 0) {
236 } else if (strcmp(*argv
, "-passin") == 0) {
239 passargin
= *(++argv
);
240 } else if (strcmp(*argv
, "-passout") == 0) {
243 passargout
= *(++argv
);
244 } else if (strcmp(*argv
, "-newkey") == 0) {
249 } else if (strcmp(*argv
, "-pkeyopt") == 0) {
253 pkeyopts
= sk_OPENSSL_STRING_new_null();
254 if (!pkeyopts
|| !sk_OPENSSL_STRING_push(pkeyopts
, *(++argv
)))
256 } else if (strcmp(*argv
, "-sigopt") == 0) {
260 sigopts
= sk_OPENSSL_STRING_new_null();
261 if (!sigopts
|| !sk_OPENSSL_STRING_push(sigopts
, *(++argv
)))
263 } else if (strcmp(*argv
, "-batch") == 0)
265 else if (strcmp(*argv
, "-newhdr") == 0)
267 else if (strcmp(*argv
, "-modulus") == 0)
269 else if (strcmp(*argv
, "-verify") == 0)
271 else if (strcmp(*argv
, "-nodes") == 0)
273 else if (strcmp(*argv
, "-noout") == 0)
275 else if (strcmp(*argv
, "-verbose") == 0)
277 else if (strcmp(*argv
, "-utf8") == 0)
278 chtype
= MBSTRING_UTF8
;
279 else if (strcmp(*argv
, "-nameopt") == 0) {
282 if (!set_name_ex(&nmflag
, *(++argv
)))
284 } else if (strcmp(*argv
, "-reqopt") == 0) {
287 if (!set_cert_ex(&reqflag
, *(++argv
)))
289 } else if (strcmp(*argv
, "-subject") == 0)
291 else if (strcmp(*argv
, "-text") == 0)
293 else if (strcmp(*argv
, "-x509") == 0)
295 else if (strcmp(*argv
, "-asn1-kludge") == 0)
297 else if (strcmp(*argv
, "-no-asn1-kludge") == 0)
299 else if (strcmp(*argv
, "-subj") == 0) {
303 } else if (strcmp(*argv
, "-multivalue-rdn") == 0)
305 else if (strcmp(*argv
, "-days") == 0) {
310 days
= strtonum(*(++argv
), 1, INT_MAX
, &errstr
);
312 BIO_printf(bio_err
, "bad -days %s, using 0: %s\n",
316 } else if (strcmp(*argv
, "-set_serial") == 0) {
319 serial
= s2i_ASN1_INTEGER(NULL
, *(++argv
));
322 } else if (strcmp(*argv
, "-extensions") == 0) {
325 extensions
= *(++argv
);
326 } else if (strcmp(*argv
, "-reqexts") == 0) {
329 req_exts
= *(++argv
);
330 } else if ((md_alg
= EVP_get_digestbyname(&((*argv
)[1]))) != NULL
) {
334 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
344 BIO_printf(bio_err
, "%s [options] <infile >outfile\n", prog
);
345 BIO_printf(bio_err
, "where options are\n");
346 BIO_printf(bio_err
, " -inform arg input format - DER or PEM\n");
347 BIO_printf(bio_err
, " -outform arg output format - DER or PEM\n");
348 BIO_printf(bio_err
, " -in arg input file\n");
349 BIO_printf(bio_err
, " -out arg output file\n");
350 BIO_printf(bio_err
, " -text text form of request\n");
351 BIO_printf(bio_err
, " -pubkey output public key\n");
352 BIO_printf(bio_err
, " -noout do not output REQ\n");
353 BIO_printf(bio_err
, " -verify verify signature on REQ\n");
354 BIO_printf(bio_err
, " -modulus RSA modulus\n");
355 BIO_printf(bio_err
, " -nodes don't encrypt the output key\n");
356 BIO_printf(bio_err
, " -subject output the request's subject\n");
357 BIO_printf(bio_err
, " -passin private key password source\n");
358 BIO_printf(bio_err
, " -key file use the private key contained in file\n");
359 BIO_printf(bio_err
, " -keyform arg key file format\n");
360 BIO_printf(bio_err
, " -keyout arg file to send the key to\n");
361 BIO_printf(bio_err
, " -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
362 BIO_printf(bio_err
, " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
363 BIO_printf(bio_err
, " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
364 BIO_printf(bio_err
, " -[digest] Digest to sign with (md5, sha1, md4)\n");
365 BIO_printf(bio_err
, " -config file request template file.\n");
366 BIO_printf(bio_err
, " -subj arg set or modify request subject\n");
367 BIO_printf(bio_err
, " -multivalue-rdn enable support for multivalued RDNs\n");
368 BIO_printf(bio_err
, " -new new request.\n");
369 BIO_printf(bio_err
, " -batch do not ask anything during request generation\n");
370 BIO_printf(bio_err
, " -x509 output a x509 structure instead of a cert. req.\n");
371 BIO_printf(bio_err
, " -days number of days a certificate generated by -x509 is valid for.\n");
372 BIO_printf(bio_err
, " -set_serial serial number to use for a certificate generated by -x509.\n");
373 BIO_printf(bio_err
, " -newhdr output \"NEW\" in the header lines\n");
374 BIO_printf(bio_err
, " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
375 BIO_printf(bio_err
, " have been reported as requiring\n");
376 BIO_printf(bio_err
, " -extensions .. specify certificate extension section (override value in config file)\n");
377 BIO_printf(bio_err
, " -reqexts .. specify request extension section (override value in config file)\n");
378 BIO_printf(bio_err
, " -utf8 input characters are UTF8 (default ASCII)\n");
379 BIO_printf(bio_err
, " -nameopt arg - various certificate name options\n");
380 BIO_printf(bio_err
, " -reqopt arg - various request text options\n\n");
384 if (!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
)) {
385 BIO_printf(bio_err
, "Error getting passwords\n");
388 if (template != NULL
) {
392 BIO_printf(bio_err
, "Using configuration from %s\n", template);
393 req_conf
= NCONF_new(NULL
);
394 i
= NCONF_load(req_conf
, template, &errline
);
396 BIO_printf(bio_err
, "error on line %ld of %s\n", errline
, template);
402 if (req_conf
== NULL
) {
403 BIO_printf(bio_err
, "Unable to load config info from %s\n", default_config_file
);
407 BIO_printf(bio_err
, "Using configuration from %s\n",
408 default_config_file
);
411 if (req_conf
!= NULL
) {
412 if (!load_config(bio_err
, req_conf
))
414 p
= NCONF_get_string(req_conf
, NULL
, "oid_file");
420 oid_bio
= BIO_new_file(p
, "r");
421 if (oid_bio
== NULL
) {
423 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
424 ERR_print_errors(bio_err);
427 OBJ_create_objects(oid_bio
);
432 if (!add_oid_section(bio_err
, req_conf
))
435 if (md_alg
== NULL
) {
436 p
= NCONF_get_string(req_conf
, SECTION
, "default_md");
440 if ((md_alg
= EVP_get_digestbyname(p
)) != NULL
)
445 extensions
= NCONF_get_string(req_conf
, SECTION
, V3_EXTENSIONS
);
450 /* Check syntax of file */
452 X509V3_set_ctx_test(&ctx
);
453 X509V3_set_nconf(&ctx
, req_conf
);
454 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, extensions
, NULL
)) {
456 "Error Loading extension section %s\n", extensions
);
461 passin
= NCONF_get_string(req_conf
, SECTION
, "input_password");
466 passout
= NCONF_get_string(req_conf
, SECTION
, "output_password");
470 p
= NCONF_get_string(req_conf
, SECTION
, STRING_MASK
);
474 if (p
&& !ASN1_STRING_set_default_mask_asc(p
)) {
475 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", p
);
478 if (chtype
!= MBSTRING_UTF8
) {
479 p
= NCONF_get_string(req_conf
, SECTION
, UTF8_IN
);
482 else if (!strcmp(p
, "yes"))
483 chtype
= MBSTRING_UTF8
;
486 req_exts
= NCONF_get_string(req_conf
, SECTION
, REQ_EXTENSIONS
);
491 /* Check syntax of file */
493 X509V3_set_ctx_test(&ctx
);
494 X509V3_set_nconf(&ctx
, req_conf
);
495 if (!X509V3_EXT_add_nconf(req_conf
, &ctx
, req_exts
, NULL
)) {
497 "Error Loading request extension section %s\n",
502 in
= BIO_new(BIO_s_file());
503 out
= BIO_new(BIO_s_file());
504 if ((in
== NULL
) || (out
== NULL
))
507 if (keyfile
!= NULL
) {
508 pkey
= load_key(bio_err
, keyfile
, keyform
, 0, passin
,
512 * load_key() has already printed an appropriate
518 if (newreq
&& (pkey
== NULL
)) {
519 if (!NCONF_get_number(req_conf
, SECTION
, BITS
, &newkey
)) {
520 newkey
= DEFAULT_KEY_LENGTH
;
523 genctx
= set_keygen_ctx(bio_err
, keyalg
, &pkey_type
, &newkey
,
528 if (newkey
< MIN_KEY_LENGTH
&& (pkey_type
== EVP_PKEY_RSA
|| pkey_type
== EVP_PKEY_DSA
)) {
529 BIO_printf(bio_err
, "private key length is too short,\n");
530 BIO_printf(bio_err
, "it needs to be at least %d bits, not %ld\n", MIN_KEY_LENGTH
, newkey
);
534 genctx
= set_keygen_ctx(bio_err
, NULL
, &pkey_type
, &newkey
,
541 for (i
= 0; i
< sk_OPENSSL_STRING_num(pkeyopts
); i
++) {
542 genopt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
543 if (pkey_ctrl_string(genctx
, genopt
) <= 0) {
545 "parameter error \"%s\"\n",
547 ERR_print_errors(bio_err
);
552 BIO_printf(bio_err
, "Generating a %ld bit %s private key\n",
555 EVP_PKEY_CTX_set_cb(genctx
, genpkey_cb
);
556 EVP_PKEY_CTX_set_app_data(genctx
, bio_err
);
558 if (EVP_PKEY_keygen(genctx
, &pkey
) <= 0) {
559 BIO_puts(bio_err
, "Error Generating Key\n");
562 EVP_PKEY_CTX_free(genctx
);
565 if (keyout
== NULL
) {
566 keyout
= NCONF_get_string(req_conf
, SECTION
, KEYFILE
);
570 if (keyout
== NULL
) {
571 BIO_printf(bio_err
, "writing new private key to stdout\n");
572 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
574 BIO_printf(bio_err
, "writing new private key to '%s'\n", keyout
);
575 if (BIO_write_filename(out
, keyout
) <= 0) {
581 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_rsa_key");
584 p
= NCONF_get_string(req_conf
, SECTION
, "encrypt_key");
588 if ((p
!= NULL
) && (strcmp(p
, "no") == 0))
595 if (!PEM_write_bio_PrivateKey(out
, pkey
, cipher
,
596 NULL
, 0, NULL
, passout
)) {
597 if ((ERR_GET_REASON(ERR_peek_error()) ==
598 PEM_R_PROBLEMS_GETTING_PASSWORD
) && (i
< 3)) {
605 BIO_printf(bio_err
, "-----\n");
609 * Since we are using a pre-existing certificate request, the
610 * kludge 'format' info should not be changed.
614 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
616 if (BIO_read_filename(in
, infile
) <= 0) {
622 if (informat
== FORMAT_ASN1
)
623 req
= d2i_X509_REQ_bio(in
, NULL
);
624 else if (informat
== FORMAT_PEM
)
625 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
627 BIO_printf(bio_err
, "bad input format specified for X509 request\n");
631 BIO_printf(bio_err
, "unable to load X509 request\n");
635 if (newreq
|| x509
) {
637 BIO_printf(bio_err
, "you need to specify a private key\n");
641 req
= X509_REQ_new();
645 i
= make_REQ(req
, pkey
, subj
, multirdn
, !x509
, chtype
);
646 subj
= NULL
; /* done processing '-subj' option */
647 if ((kludge
> 0) && !sk_X509_ATTRIBUTE_num(req
->req_info
->attributes
)) {
648 sk_X509_ATTRIBUTE_free(req
->req_info
->attributes
);
649 req
->req_info
->attributes
= NULL
;
652 BIO_printf(bio_err
, "problems making Certificate Request\n");
659 if ((x509ss
= X509_new()) == NULL
)
662 /* Set version to V3 */
663 if (extensions
&& !X509_set_version(x509ss
, 2))
666 if (!X509_set_serialNumber(x509ss
, serial
))
669 if (!rand_serial(NULL
,
670 X509_get_serialNumber(x509ss
)))
674 if (!X509_set_issuer_name(x509ss
, X509_REQ_get_subject_name(req
)))
676 if (!X509_gmtime_adj(X509_get_notBefore(x509ss
), 0))
678 if (!X509_time_adj_ex(X509_get_notAfter(x509ss
), days
, 0, NULL
))
680 if (!X509_set_subject_name(x509ss
, X509_REQ_get_subject_name(req
)))
682 tmppkey
= X509_REQ_get_pubkey(req
);
683 if (!tmppkey
|| !X509_set_pubkey(x509ss
, tmppkey
))
685 EVP_PKEY_free(tmppkey
);
687 /* Set up V3 context struct */
689 X509V3_set_ctx(&ext_ctx
, x509ss
, x509ss
, NULL
, NULL
, 0);
690 X509V3_set_nconf(&ext_ctx
, req_conf
);
693 if (extensions
&& !X509V3_EXT_add_nconf(req_conf
,
694 &ext_ctx
, extensions
, x509ss
)) {
696 "Error Loading extension section %s\n",
700 i
= do_X509_sign(bio_err
, x509ss
, pkey
, digest
, sigopts
);
702 ERR_print_errors(bio_err
);
708 /* Set up V3 context struct */
710 X509V3_set_ctx(&ext_ctx
, NULL
, NULL
, req
, NULL
, 0);
711 X509V3_set_nconf(&ext_ctx
, req_conf
);
714 if (req_exts
&& !X509V3_EXT_REQ_add_nconf(req_conf
,
715 &ext_ctx
, req_exts
, req
)) {
717 "Error Loading extension section %s\n",
721 i
= do_X509_REQ_sign(bio_err
, req
, pkey
, digest
, sigopts
);
723 ERR_print_errors(bio_err
);
729 BIO_printf(bio_err
, "Cannot modifiy certificate subject\n");
734 BIO_printf(bio_err
, "Modifying Request's Subject\n");
735 print_name(bio_err
, "old subject=", X509_REQ_get_subject_name(req
), nmflag
);
737 if (build_subject(req
, subj
, chtype
, multirdn
) == 0) {
738 BIO_printf(bio_err
, "ERROR: cannot modify subject\n");
742 req
->req_info
->enc
.modified
= 1;
745 print_name(bio_err
, "new subject=", X509_REQ_get_subject_name(req
), nmflag
);
748 if (verify
&& !x509
) {
752 pkey
= X509_REQ_get_pubkey(req
);
757 i
= X509_REQ_verify(req
, pkey
);
765 BIO_printf(bio_err
, "verify failure\n");
766 ERR_print_errors(bio_err
);
767 } else /* if (i > 0) */
768 BIO_printf(bio_err
, "verify OK\n");
770 if (noout
&& !text
&& !modulus
&& !subject
&& !pubkey
) {
774 if (outfile
== NULL
) {
775 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
777 if ((keyout
!= NULL
) && (strcmp(outfile
, keyout
) == 0))
778 i
= (int) BIO_append_filename(out
, outfile
);
780 i
= (int) BIO_write_filename(out
, outfile
);
789 tpubkey
= X509_REQ_get_pubkey(req
);
790 if (tpubkey
== NULL
) {
791 BIO_printf(bio_err
, "Error getting public key\n");
792 ERR_print_errors(bio_err
);
795 PEM_write_bio_PUBKEY(out
, tpubkey
);
796 EVP_PKEY_free(tpubkey
);
800 X509_print_ex(out
, x509ss
, nmflag
, reqflag
);
802 X509_REQ_print_ex(out
, req
, nmflag
, reqflag
);
806 print_name(out
, "subject=", X509_get_subject_name(x509ss
), nmflag
);
808 print_name(out
, "subject=", X509_REQ_get_subject_name(req
), nmflag
);
814 tpubkey
= X509_get_pubkey(x509ss
);
816 tpubkey
= X509_REQ_get_pubkey(req
);
817 if (tpubkey
== NULL
) {
818 fprintf(stdout
, "Modulus=unavailable\n");
821 fprintf(stdout
, "Modulus=");
822 if (EVP_PKEY_base_id(tpubkey
) == EVP_PKEY_RSA
)
823 BN_print(out
, tpubkey
->pkey
.rsa
->n
);
825 fprintf(stdout
, "Wrong Algorithm type");
826 EVP_PKEY_free(tpubkey
);
827 fprintf(stdout
, "\n");
829 if (!noout
&& !x509
) {
830 if (outformat
== FORMAT_ASN1
)
831 i
= i2d_X509_REQ_bio(out
, req
);
832 else if (outformat
== FORMAT_PEM
) {
834 i
= PEM_write_bio_X509_REQ_NEW(out
, req
);
836 i
= PEM_write_bio_X509_REQ(out
, req
);
838 BIO_printf(bio_err
, "bad output format specified for outfile\n");
842 BIO_printf(bio_err
, "unable to write X509 request\n");
846 if (!noout
&& x509
&& (x509ss
!= NULL
)) {
847 if (outformat
== FORMAT_ASN1
)
848 i
= i2d_X509_bio(out
, x509ss
);
849 else if (outformat
== FORMAT_PEM
)
850 i
= PEM_write_bio_X509(out
, x509ss
);
852 BIO_printf(bio_err
, "bad output format specified for outfile\n");
856 BIO_printf(bio_err
, "unable to write X509 certificate\n");
863 ERR_print_errors(bio_err
);
865 if ((req_conf
!= NULL
) && (req_conf
!= config
))
866 NCONF_free(req_conf
);
871 EVP_PKEY_CTX_free(genctx
);
873 sk_OPENSSL_STRING_free(pkeyopts
);
875 sk_OPENSSL_STRING_free(sigopts
);
879 ASN1_INTEGER_free(serial
);
880 if (passargin
&& passin
)
882 if (passargout
&& passout
)
890 make_REQ(X509_REQ
* req
, EVP_PKEY
* pkey
, char *subj
, int multirdn
,
891 int attribs
, unsigned long chtype
)
895 STACK_OF(CONF_VALUE
) * dn_sk
, *attr_sk
= NULL
;
896 char *tmp
, *dn_sect
, *attr_sect
;
898 tmp
= NCONF_get_string(req_conf
, SECTION
, PROMPT
);
901 if ((tmp
!= NULL
) && !strcmp(tmp
, "no"))
904 dn_sect
= NCONF_get_string(req_conf
, SECTION
, DISTINGUISHED_NAME
);
905 if (dn_sect
== NULL
) {
906 BIO_printf(bio_err
, "unable to find '%s' in config\n",
910 dn_sk
= NCONF_get_section(req_conf
, dn_sect
);
912 BIO_printf(bio_err
, "unable to get '%s' section\n", dn_sect
);
915 attr_sect
= NCONF_get_string(req_conf
, SECTION
, ATTRIBUTES
);
916 if (attr_sect
== NULL
) {
920 attr_sk
= NCONF_get_section(req_conf
, attr_sect
);
921 if (attr_sk
== NULL
) {
922 BIO_printf(bio_err
, "unable to get '%s' section\n", attr_sect
);
927 /* setup version number */
928 if (!X509_REQ_set_version(req
, 0L))
929 goto err
; /* version 1 */
932 i
= auto_info(req
, dn_sk
, attr_sk
, attribs
, chtype
);
935 i
= build_subject(req
, subj
, chtype
, multirdn
);
937 i
= prompt_info(req
, dn_sk
, dn_sect
, attr_sk
, attr_sect
, attribs
, chtype
);
942 if (!X509_REQ_set_pubkey(req
, pkey
))
951 * subject is expected to be in the format /type0=value0/type1=value1/type2=...
952 * where characters may be escaped by \
955 build_subject(X509_REQ
* req
, char *subject
, unsigned long chtype
, int multirdn
)
959 if (!(n
= parse_name(subject
, chtype
, multirdn
)))
962 if (!X509_REQ_set_subject_name(req
, n
)) {
972 prompt_info(X509_REQ
* req
,
973 STACK_OF(CONF_VALUE
) * dn_sk
, char *dn_sect
,
974 STACK_OF(CONF_VALUE
) * attr_sk
, char *attr_sect
, int attribs
,
975 unsigned long chtype
)
986 subj
= X509_REQ_get_subject_name(req
);
989 BIO_printf(bio_err
, "You are about to be asked to enter information that will be incorporated\n");
990 BIO_printf(bio_err
, "into your certificate request.\n");
991 BIO_printf(bio_err
, "What you are about to enter is what is called a Distinguished Name or a DN.\n");
992 BIO_printf(bio_err
, "There are quite a few fields but you can leave some blank\n");
993 BIO_printf(bio_err
, "For some fields there will be a default value,\n");
994 BIO_printf(bio_err
, "If you enter '.', the field will be left blank.\n");
995 BIO_printf(bio_err
, "-----\n");
997 if (sk_CONF_VALUE_num(dn_sk
)) {
1002 if (sk_CONF_VALUE_num(dn_sk
) <= i
)
1005 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1008 if (!check_end(type
, "_min") || !check_end(type
, "_max") ||
1009 !check_end(type
, "_default") ||
1010 !check_end(type
, "_value"))
1013 * Skip past any leading X. X: X, etc to allow for
1014 * multiple instances
1016 for (p
= v
->name
; *p
; p
++)
1017 if ((*p
== ':') || (*p
== ',') ||
1029 /* If OBJ not recognised ignore it */
1030 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1032 ret
= snprintf(buf
, sizeof buf
, "%s_default", v
->name
);
1033 if (ret
== -1 || ret
>= sizeof(buf
)) {
1034 BIO_printf(bio_err
, "Name '%s' too long for default\n",
1038 if ((def
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1042 ret
= snprintf(buf
, sizeof buf
, "%s_value", v
->name
);
1043 if (ret
== -1 || ret
>= sizeof(buf
)) {
1044 BIO_printf(bio_err
, "Name '%s' too long for value\n",
1048 if ((value
= NCONF_get_string(req_conf
, dn_sect
, buf
)) == NULL
) {
1052 ret
= snprintf(buf
, sizeof buf
, "%s_min", v
->name
);
1053 if (ret
== -1 || ret
>= sizeof(buf
)) {
1054 BIO_printf(bio_err
, "Name '%s' too long for min\n",
1058 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_min
)) {
1062 ret
= snprintf(buf
, sizeof buf
, "%s_max", v
->name
);
1063 if (ret
== -1 || ret
>= sizeof(buf
)) {
1064 BIO_printf(bio_err
, "Name '%s' too long for max\n",
1068 if (!NCONF_get_number(req_conf
, dn_sect
, buf
, &n_max
)) {
1072 if (!add_DN_object(subj
, v
->value
, def
, value
, nid
,
1073 n_min
, n_max
, chtype
, mval
))
1076 if (X509_NAME_entry_count(subj
) == 0) {
1077 BIO_printf(bio_err
, "error, no objects specified in config file\n");
1081 if ((attr_sk
!= NULL
) && (sk_CONF_VALUE_num(attr_sk
) > 0) &&
1084 "\nPlease enter the following 'extra' attributes\n");
1086 "to be sent with your certificate request\n");
1092 if ((attr_sk
== NULL
) ||
1093 (sk_CONF_VALUE_num(attr_sk
) <= i
))
1096 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1098 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
)
1100 ret
= snprintf(buf
, sizeof buf
, "%s_default", type
);
1101 if (ret
== -1 || ret
>= sizeof(buf
)) {
1102 BIO_printf(bio_err
, "Name '%s' too long for default\n",
1106 if ((def
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1111 ret
= snprintf(buf
, sizeof buf
, "%s_value", type
);
1112 if (ret
== -1 || ret
>= sizeof(buf
)) {
1113 BIO_printf(bio_err
, "Name '%s' too long for value\n",
1117 if ((value
= NCONF_get_string(req_conf
, attr_sect
, buf
))
1122 ret
= snprintf(buf
, sizeof buf
, "%s_min", type
);
1123 if (ret
== -1 || ret
>= sizeof(buf
)) {
1124 BIO_printf(bio_err
, "Name '%s' too long for min\n",
1128 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_min
)) {
1132 ret
= snprintf(buf
, sizeof buf
, "%s_max", type
);
1133 if (ret
== -1 || ret
>= sizeof(buf
)) {
1134 BIO_printf(bio_err
, "Name '%s' too long for max\n",
1138 if (!NCONF_get_number(req_conf
, attr_sect
, buf
, &n_max
)) {
1142 if (!add_attribute_object(req
,
1143 v
->value
, def
, value
, nid
, n_min
, n_max
, chtype
))
1148 BIO_printf(bio_err
, "No template, please set one up.\n");
1157 auto_info(X509_REQ
* req
, STACK_OF(CONF_VALUE
) * dn_sk
,
1158 STACK_OF(CONF_VALUE
) * attr_sk
, int attribs
, unsigned long chtype
)
1166 subj
= X509_REQ_get_subject_name(req
);
1168 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
1170 v
= sk_CONF_VALUE_value(dn_sk
, i
);
1174 * Skip past any leading X. X: X, etc to allow for multiple
1177 for (p
= v
->name
; *p
; p
++)
1178 if ((*p
== ':') || (*p
== ',') || (*p
== '.')) {
1189 if (!X509_NAME_add_entry_by_txt(subj
, type
, chtype
,
1190 (unsigned char *) v
->value
, -1, -1, mval
))
1195 if (!X509_NAME_entry_count(subj
)) {
1196 BIO_printf(bio_err
, "error, no objects specified in config file\n");
1200 for (i
= 0; i
< sk_CONF_VALUE_num(attr_sk
); i
++) {
1201 v
= sk_CONF_VALUE_value(attr_sk
, i
);
1202 if (!X509_REQ_add1_attr_by_txt(req
, v
->name
, chtype
,
1203 (unsigned char *) v
->value
, -1))
1212 add_DN_object(X509_NAME
* n
, char *text
, const char *def
, char *value
,
1213 int nid
, int n_min
, int n_max
, unsigned long chtype
, int mval
)
1219 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1220 (void) BIO_flush(bio_err
);
1221 if (value
!= NULL
) {
1222 strlcpy(buf
, value
, sizeof buf
);
1223 strlcat(buf
, "\n", sizeof buf
);
1224 BIO_printf(bio_err
, "%s\n", value
);
1228 if (!fgets(buf
, sizeof buf
, stdin
))
1238 else if (buf
[0] == '\n') {
1239 if ((def
== NULL
) || (def
[0] == '\0'))
1241 strlcpy(buf
, def
, sizeof buf
);
1242 strlcat(buf
, "\n", sizeof buf
);
1243 } else if ((buf
[0] == '.') && (buf
[1] == '\n'))
1247 if (buf
[i
- 1] != '\n') {
1248 BIO_printf(bio_err
, "weird input :-(\n");
1252 if (!req_check_len(i
, n_min
, n_max
))
1254 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
1255 (unsigned char *) buf
, -1, -1, mval
))
1263 add_attribute_object(X509_REQ
* req
, char *text
, const char *def
,
1264 char *value
, int nid
, int n_min
,
1265 int n_max
, unsigned long chtype
)
1268 static char buf
[1024];
1272 BIO_printf(bio_err
, "%s [%s]:", text
, def
);
1273 (void) BIO_flush(bio_err
);
1274 if (value
!= NULL
) {
1275 strlcpy(buf
, value
, sizeof buf
);
1276 strlcat(buf
, "\n", sizeof buf
);
1277 BIO_printf(bio_err
, "%s\n", value
);
1281 if (!fgets(buf
, sizeof buf
, stdin
))
1291 else if (buf
[0] == '\n') {
1292 if ((def
== NULL
) || (def
[0] == '\0'))
1294 strlcpy(buf
, def
, sizeof buf
);
1295 strlcat(buf
, "\n", sizeof buf
);
1296 } else if ((buf
[0] == '.') && (buf
[1] == '\n'))
1300 if (buf
[i
- 1] != '\n') {
1301 BIO_printf(bio_err
, "weird input :-(\n");
1305 if (!req_check_len(i
, n_min
, n_max
))
1308 if (!X509_REQ_add1_attr_by_NID(req
, nid
, chtype
,
1309 (unsigned char *) buf
, -1)) {
1310 BIO_printf(bio_err
, "Error adding attribute\n");
1311 ERR_print_errors(bio_err
);
1320 req_check_len(int len
, int n_min
, int n_max
)
1322 if ((n_min
> 0) && (len
< n_min
)) {
1323 BIO_printf(bio_err
, "string is too short, it needs to be at least %d bytes long\n", n_min
);
1326 if ((n_max
>= 0) && (len
> n_max
)) {
1327 BIO_printf(bio_err
, "string is too long, it needs to be less than %d bytes long\n", n_max
);
1333 /* Check if the end of a string matches 'end' */
1335 check_end(const char *str
, const char *end
)
1343 tmp
= str
+ slen
- elen
;
1344 return strcmp(tmp
, end
);
1347 static EVP_PKEY_CTX
*
1348 set_keygen_ctx(BIO
* err
, const char *gstr
, int *pkey_type
,
1349 long *pkeylen
, char **palgnam
)
1351 EVP_PKEY_CTX
*gctx
= NULL
;
1352 EVP_PKEY
*param
= NULL
;
1355 const char *paramfile
= NULL
;
1359 *pkey_type
= EVP_PKEY_RSA
;
1361 } else if (gstr
[0] >= '0' && gstr
[0] <= '9') {
1362 *pkey_type
= EVP_PKEY_RSA
;
1363 keylen
= strtonum(gstr
, 0, LONG_MAX
, &errstr
);
1365 BIO_printf(err
, "bad algorithm %s: %s\n", gstr
, errstr
);
1369 } else if (!strncmp(gstr
, "param:", 6))
1370 paramfile
= gstr
+ 6;
1372 const char *p
= strchr(gstr
, ':');
1374 const EVP_PKEY_ASN1_METHOD
*ameth
;
1381 ameth
= EVP_PKEY_asn1_find_str(NULL
, gstr
, len
);
1384 BIO_printf(err
, "Unknown algorithm %.*s\n", len
, gstr
);
1387 EVP_PKEY_asn1_get0_info(NULL
, pkey_type
, NULL
, NULL
, NULL
,
1389 if (*pkey_type
== EVP_PKEY_RSA
) {
1391 keylen
= strtonum(p
+ 1, 0, LONG_MAX
, &errstr
);
1393 BIO_printf(err
, "bad algorithm %s: %s\n",
1405 pbio
= BIO_new_file(paramfile
, "r");
1407 BIO_printf(err
, "Can't open parameter file %s\n",
1411 param
= PEM_read_bio_Parameters(pbio
, NULL
);
1415 (void) BIO_reset(pbio
);
1416 x
= PEM_read_bio_X509(pbio
, NULL
, NULL
, NULL
);
1418 param
= X509_get_pubkey(x
);
1425 BIO_printf(err
, "Error reading parameter file %s\n",
1429 if (*pkey_type
== -1)
1430 *pkey_type
= EVP_PKEY_id(param
);
1431 else if (*pkey_type
!= EVP_PKEY_base_id(param
)) {
1432 BIO_printf(err
, "Key Type does not match parameters\n");
1433 EVP_PKEY_free(param
);
1438 const EVP_PKEY_ASN1_METHOD
*ameth
;
1440 ameth
= EVP_PKEY_asn1_find(NULL
, *pkey_type
);
1442 BIO_puts(err
, "Internal error: can't find key algorithm\n");
1445 EVP_PKEY_asn1_get0_info(NULL
, NULL
, NULL
, NULL
, &anam
, ameth
);
1446 *palgnam
= strdup(anam
);
1449 gctx
= EVP_PKEY_CTX_new(param
, NULL
);
1450 *pkeylen
= EVP_PKEY_bits(param
);
1451 EVP_PKEY_free(param
);
1453 gctx
= EVP_PKEY_CTX_new_id(*pkey_type
, NULL
);
1456 BIO_puts(err
, "Error allocating keygen context\n");
1457 ERR_print_errors(err
);
1460 if (EVP_PKEY_keygen_init(gctx
) <= 0) {
1461 BIO_puts(err
, "Error initializing keygen context\n");
1462 ERR_print_errors(err
);
1465 if ((*pkey_type
== EVP_PKEY_RSA
) && (keylen
!= -1)) {
1466 if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx
, keylen
) <= 0) {
1467 BIO_puts(err
, "Error setting RSA keysize\n");
1468 ERR_print_errors(err
);
1469 EVP_PKEY_CTX_free(gctx
);
1478 genpkey_cb(EVP_PKEY_CTX
* ctx
)
1481 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
1483 p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);
1492 BIO_write(b
, &c
, 1);
1493 (void) BIO_flush(b
);
1498 do_sign_init(BIO
* err
, EVP_MD_CTX
* ctx
, EVP_PKEY
* pkey
,
1499 const EVP_MD
* md
, STACK_OF(OPENSSL_STRING
) * sigopts
)
1501 EVP_PKEY_CTX
*pkctx
= NULL
;
1503 EVP_MD_CTX_init(ctx
);
1504 if (!EVP_DigestSignInit(ctx
, &pkctx
, md
, NULL
, pkey
))
1506 for (i
= 0; i
< sk_OPENSSL_STRING_num(sigopts
); i
++) {
1507 char *sigopt
= sk_OPENSSL_STRING_value(sigopts
, i
);
1508 if (pkey_ctrl_string(pkctx
, sigopt
) <= 0) {
1509 BIO_printf(err
, "parameter error \"%s\"\n", sigopt
);
1510 ERR_print_errors(bio_err
);
1518 do_X509_sign(BIO
* err
, X509
* x
, EVP_PKEY
* pkey
, const EVP_MD
* md
,
1519 STACK_OF(OPENSSL_STRING
) * sigopts
)
1523 EVP_MD_CTX_init(&mctx
);
1524 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1526 rv
= X509_sign_ctx(x
, &mctx
);
1527 EVP_MD_CTX_cleanup(&mctx
);
1528 return rv
> 0 ? 1 : 0;
1533 do_X509_REQ_sign(BIO
* err
, X509_REQ
* x
, EVP_PKEY
* pkey
, const EVP_MD
* md
,
1534 STACK_OF(OPENSSL_STRING
) * sigopts
)
1538 EVP_MD_CTX_init(&mctx
);
1539 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1541 rv
= X509_REQ_sign_ctx(x
, &mctx
);
1542 EVP_MD_CTX_cleanup(&mctx
);
1543 return rv
> 0 ? 1 : 0;
1549 do_X509_CRL_sign(BIO
* err
, X509_CRL
* x
, EVP_PKEY
* pkey
, const EVP_MD
* md
,
1550 STACK_OF(OPENSSL_STRING
) * sigopts
)
1554 EVP_MD_CTX_init(&mctx
);
1555 rv
= do_sign_init(err
, &mctx
, pkey
, md
, sigopts
);
1557 rv
= X509_CRL_sign_ctx(x
, &mctx
);
1558 EVP_MD_CTX_cleanup(&mctx
);
1559 return rv
> 0 ? 1 : 0;