1ebfedea0SLionel Sambuc /* apps/req.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc
59*0a6a1f1dSLionel Sambuc /*
60*0a6a1f1dSLionel Sambuc * Until the key-gen callbacks are modified to use newer prototypes, we allow
61*0a6a1f1dSLionel Sambuc * deprecated functions for openssl-internal code
62*0a6a1f1dSLionel Sambuc */
63ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_DEPRECATED
64ebfedea0SLionel Sambuc # undef OPENSSL_NO_DEPRECATED
65ebfedea0SLionel Sambuc #endif
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc #include <stdio.h>
68ebfedea0SLionel Sambuc #include <stdlib.h>
69ebfedea0SLionel Sambuc #include <time.h>
70ebfedea0SLionel Sambuc #include <string.h>
71ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_STDIO
72ebfedea0SLionel Sambuc # define APPS_WIN16
73ebfedea0SLionel Sambuc #endif
74ebfedea0SLionel Sambuc #include "apps.h"
75ebfedea0SLionel Sambuc #include <openssl/bio.h>
76ebfedea0SLionel Sambuc #include <openssl/evp.h>
77ebfedea0SLionel Sambuc #include <openssl/conf.h>
78ebfedea0SLionel Sambuc #include <openssl/err.h>
79ebfedea0SLionel Sambuc #include <openssl/asn1.h>
80ebfedea0SLionel Sambuc #include <openssl/x509.h>
81ebfedea0SLionel Sambuc #include <openssl/x509v3.h>
82ebfedea0SLionel Sambuc #include <openssl/objects.h>
83ebfedea0SLionel Sambuc #include <openssl/pem.h>
84ebfedea0SLionel Sambuc #include <openssl/bn.h>
85ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
86ebfedea0SLionel Sambuc # include <openssl/rsa.h>
87ebfedea0SLionel Sambuc #endif
88ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA
89ebfedea0SLionel Sambuc # include <openssl/dsa.h>
90ebfedea0SLionel Sambuc #endif
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc #define SECTION "req"
93ebfedea0SLionel Sambuc
94ebfedea0SLionel Sambuc #define BITS "default_bits"
95ebfedea0SLionel Sambuc #define KEYFILE "default_keyfile"
96ebfedea0SLionel Sambuc #define PROMPT "prompt"
97ebfedea0SLionel Sambuc #define DISTINGUISHED_NAME "distinguished_name"
98ebfedea0SLionel Sambuc #define ATTRIBUTES "attributes"
99ebfedea0SLionel Sambuc #define V3_EXTENSIONS "x509_extensions"
100ebfedea0SLionel Sambuc #define REQ_EXTENSIONS "req_extensions"
101ebfedea0SLionel Sambuc #define STRING_MASK "string_mask"
102ebfedea0SLionel Sambuc #define UTF8_IN "utf8"
103ebfedea0SLionel Sambuc
104ebfedea0SLionel Sambuc #define DEFAULT_KEY_LENGTH 512
105ebfedea0SLionel Sambuc #define MIN_KEY_LENGTH 384
106ebfedea0SLionel Sambuc
107ebfedea0SLionel Sambuc #undef PROG
108ebfedea0SLionel Sambuc #define PROG req_main
109ebfedea0SLionel Sambuc
110*0a6a1f1dSLionel Sambuc /*-
111*0a6a1f1dSLionel Sambuc * -inform arg - input format - default PEM (DER or PEM)
112ebfedea0SLionel Sambuc * -outform arg - output format - default PEM
113ebfedea0SLionel Sambuc * -in arg - input file - default stdin
114ebfedea0SLionel Sambuc * -out arg - output file - default stdout
115ebfedea0SLionel Sambuc * -verify - check request signature
116ebfedea0SLionel Sambuc * -noout - don't print stuff out.
117ebfedea0SLionel Sambuc * -text - print out human readable text.
118ebfedea0SLionel Sambuc * -nodes - no des encryption
119ebfedea0SLionel Sambuc * -config file - Load configuration file.
120ebfedea0SLionel Sambuc * -key file - make a request using key in file (or use it for verification).
121ebfedea0SLionel Sambuc * -keyform arg - key file format.
122ebfedea0SLionel Sambuc * -rand file(s) - load the file(s) into the PRNG.
123ebfedea0SLionel Sambuc * -newkey - make a key and a request.
124ebfedea0SLionel Sambuc * -modulus - print RSA modulus.
125ebfedea0SLionel Sambuc * -pubkey - output Public Key.
126ebfedea0SLionel Sambuc * -x509 - output a self signed X509 structure instead.
127ebfedea0SLionel Sambuc * -asn1-kludge - output new certificate request in a format that some CA's
128ebfedea0SLionel Sambuc * require. This format is wrong
129ebfedea0SLionel Sambuc */
130ebfedea0SLionel Sambuc
131ebfedea0SLionel Sambuc static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *dn, int mutlirdn,
132ebfedea0SLionel Sambuc int attribs, unsigned long chtype);
133ebfedea0SLionel Sambuc static int build_subject(X509_REQ *req, char *subj, unsigned long chtype,
134ebfedea0SLionel Sambuc int multirdn);
135ebfedea0SLionel Sambuc static int prompt_info(X509_REQ *req,
136ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
137*0a6a1f1dSLionel Sambuc STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect,
138*0a6a1f1dSLionel Sambuc int attribs, unsigned long chtype);
139ebfedea0SLionel Sambuc static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
140ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *attr, int attribs,
141ebfedea0SLionel Sambuc unsigned long chtype);
142ebfedea0SLionel Sambuc static int add_attribute_object(X509_REQ *req, char *text, const char *def,
143*0a6a1f1dSLionel Sambuc char *value, int nid, int n_min, int n_max,
144*0a6a1f1dSLionel Sambuc unsigned long chtype);
145*0a6a1f1dSLionel Sambuc static int add_DN_object(X509_NAME *n, char *text, const char *def,
146*0a6a1f1dSLionel Sambuc char *value, int nid, int n_min, int n_max,
147*0a6a1f1dSLionel Sambuc unsigned long chtype, int mval);
148ebfedea0SLionel Sambuc static int genpkey_cb(EVP_PKEY_CTX *ctx);
149ebfedea0SLionel Sambuc static int req_check_len(int len, int n_min, int n_max);
150ebfedea0SLionel Sambuc static int check_end(const char *str, const char *end);
151*0a6a1f1dSLionel Sambuc static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
152*0a6a1f1dSLionel Sambuc int *pkey_type, long *pkeylen,
153*0a6a1f1dSLionel Sambuc char **palgnam, ENGINE *keygen_engine);
154ebfedea0SLionel Sambuc #ifndef MONOLITH
155ebfedea0SLionel Sambuc static char *default_config_file = NULL;
156ebfedea0SLionel Sambuc #endif
157ebfedea0SLionel Sambuc static CONF *req_conf = NULL;
158ebfedea0SLionel Sambuc static int batch = 0;
159ebfedea0SLionel Sambuc
160ebfedea0SLionel Sambuc int MAIN(int, char **);
161ebfedea0SLionel Sambuc
MAIN(int argc,char ** argv)162ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
163ebfedea0SLionel Sambuc {
164ebfedea0SLionel Sambuc ENGINE *e = NULL, *gen_eng = NULL;
165ebfedea0SLionel Sambuc unsigned long nmflag = 0, reqflag = 0;
166ebfedea0SLionel Sambuc int ex = 1, x509 = 0, days = 30;
167ebfedea0SLionel Sambuc X509 *x509ss = NULL;
168ebfedea0SLionel Sambuc X509_REQ *req = NULL;
169ebfedea0SLionel Sambuc EVP_PKEY_CTX *genctx = NULL;
170ebfedea0SLionel Sambuc const char *keyalg = NULL;
171ebfedea0SLionel Sambuc char *keyalgstr = NULL;
172ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *pkeyopts = NULL, *sigopts = NULL;
173ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
174ebfedea0SLionel Sambuc int i = 0, badops = 0, newreq = 0, verbose = 0, pkey_type = -1;
175ebfedea0SLionel Sambuc long newkey = -1;
176ebfedea0SLionel Sambuc BIO *in = NULL, *out = NULL;
177*0a6a1f1dSLionel Sambuc int informat, outformat, verify = 0, noout = 0, text = 0, keyform =
178*0a6a1f1dSLionel Sambuc FORMAT_PEM;
179ebfedea0SLionel Sambuc int nodes = 0, kludge = 0, newhdr = 0, subject = 0, pubkey = 0;
180*0a6a1f1dSLionel Sambuc char *infile, *outfile, *prog, *keyfile = NULL, *template =
181*0a6a1f1dSLionel Sambuc NULL, *keyout = NULL;
182ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
183ebfedea0SLionel Sambuc char *engine = NULL;
184ebfedea0SLionel Sambuc #endif
185ebfedea0SLionel Sambuc char *extensions = NULL;
186ebfedea0SLionel Sambuc char *req_exts = NULL;
187ebfedea0SLionel Sambuc const EVP_CIPHER *cipher = NULL;
188ebfedea0SLionel Sambuc ASN1_INTEGER *serial = NULL;
189ebfedea0SLionel Sambuc int modulus = 0;
190ebfedea0SLionel Sambuc char *inrand = NULL;
191ebfedea0SLionel Sambuc char *passargin = NULL, *passargout = NULL;
192ebfedea0SLionel Sambuc char *passin = NULL, *passout = NULL;
193ebfedea0SLionel Sambuc char *p;
194ebfedea0SLionel Sambuc char *subj = NULL;
195ebfedea0SLionel Sambuc int multirdn = 0;
196ebfedea0SLionel Sambuc const EVP_MD *md_alg = NULL, *digest = NULL;
197ebfedea0SLionel Sambuc unsigned long chtype = MBSTRING_ASC;
198ebfedea0SLionel Sambuc #ifndef MONOLITH
199ebfedea0SLionel Sambuc char *to_free;
200ebfedea0SLionel Sambuc long errline;
201ebfedea0SLionel Sambuc #endif
202ebfedea0SLionel Sambuc
203ebfedea0SLionel Sambuc req_conf = NULL;
204ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DES
205ebfedea0SLionel Sambuc cipher = EVP_des_ede3_cbc();
206ebfedea0SLionel Sambuc #endif
207ebfedea0SLionel Sambuc apps_startup();
208ebfedea0SLionel Sambuc
209ebfedea0SLionel Sambuc if (bio_err == NULL)
210ebfedea0SLionel Sambuc if ((bio_err = BIO_new(BIO_s_file())) != NULL)
211ebfedea0SLionel Sambuc BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
212ebfedea0SLionel Sambuc
213ebfedea0SLionel Sambuc infile = NULL;
214ebfedea0SLionel Sambuc outfile = NULL;
215ebfedea0SLionel Sambuc informat = FORMAT_PEM;
216ebfedea0SLionel Sambuc outformat = FORMAT_PEM;
217ebfedea0SLionel Sambuc
218ebfedea0SLionel Sambuc prog = argv[0];
219ebfedea0SLionel Sambuc argc--;
220ebfedea0SLionel Sambuc argv++;
221*0a6a1f1dSLionel Sambuc while (argc >= 1) {
222*0a6a1f1dSLionel Sambuc if (strcmp(*argv, "-inform") == 0) {
223*0a6a1f1dSLionel Sambuc if (--argc < 1)
224*0a6a1f1dSLionel Sambuc goto bad;
225ebfedea0SLionel Sambuc informat = str2fmt(*(++argv));
226*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-outform") == 0) {
227*0a6a1f1dSLionel Sambuc if (--argc < 1)
228*0a6a1f1dSLionel Sambuc goto bad;
229ebfedea0SLionel Sambuc outformat = str2fmt(*(++argv));
230ebfedea0SLionel Sambuc }
231ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
232*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-engine") == 0) {
233*0a6a1f1dSLionel Sambuc if (--argc < 1)
234*0a6a1f1dSLionel Sambuc goto bad;
235ebfedea0SLionel Sambuc engine = *(++argv);
236*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-keygen_engine") == 0) {
237*0a6a1f1dSLionel Sambuc if (--argc < 1)
238*0a6a1f1dSLionel Sambuc goto bad;
239ebfedea0SLionel Sambuc gen_eng = ENGINE_by_id(*(++argv));
240*0a6a1f1dSLionel Sambuc if (gen_eng == NULL) {
241ebfedea0SLionel Sambuc BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv);
242ebfedea0SLionel Sambuc goto end;
243ebfedea0SLionel Sambuc }
244ebfedea0SLionel Sambuc }
245ebfedea0SLionel Sambuc #endif
246*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-key") == 0) {
247*0a6a1f1dSLionel Sambuc if (--argc < 1)
248*0a6a1f1dSLionel Sambuc goto bad;
249ebfedea0SLionel Sambuc keyfile = *(++argv);
250*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-pubkey") == 0) {
251ebfedea0SLionel Sambuc pubkey = 1;
252*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-new") == 0) {
253ebfedea0SLionel Sambuc newreq = 1;
254*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-config") == 0) {
255*0a6a1f1dSLionel Sambuc if (--argc < 1)
256*0a6a1f1dSLionel Sambuc goto bad;
257ebfedea0SLionel Sambuc template = *(++argv);
258*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-keyform") == 0) {
259*0a6a1f1dSLionel Sambuc if (--argc < 1)
260*0a6a1f1dSLionel Sambuc goto bad;
261ebfedea0SLionel Sambuc keyform = str2fmt(*(++argv));
262*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-in") == 0) {
263*0a6a1f1dSLionel Sambuc if (--argc < 1)
264*0a6a1f1dSLionel Sambuc goto bad;
265ebfedea0SLionel Sambuc infile = *(++argv);
266*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-out") == 0) {
267*0a6a1f1dSLionel Sambuc if (--argc < 1)
268*0a6a1f1dSLionel Sambuc goto bad;
269ebfedea0SLionel Sambuc outfile = *(++argv);
270*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-keyout") == 0) {
271*0a6a1f1dSLionel Sambuc if (--argc < 1)
272*0a6a1f1dSLionel Sambuc goto bad;
273ebfedea0SLionel Sambuc keyout = *(++argv);
274*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-passin") == 0) {
275*0a6a1f1dSLionel Sambuc if (--argc < 1)
276*0a6a1f1dSLionel Sambuc goto bad;
277ebfedea0SLionel Sambuc passargin = *(++argv);
278*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-passout") == 0) {
279*0a6a1f1dSLionel Sambuc if (--argc < 1)
280*0a6a1f1dSLionel Sambuc goto bad;
281ebfedea0SLionel Sambuc passargout = *(++argv);
282*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-rand") == 0) {
283*0a6a1f1dSLionel Sambuc if (--argc < 1)
284*0a6a1f1dSLionel Sambuc goto bad;
285ebfedea0SLionel Sambuc inrand = *(++argv);
286*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-newkey") == 0) {
287ebfedea0SLionel Sambuc if (--argc < 1)
288ebfedea0SLionel Sambuc goto bad;
289ebfedea0SLionel Sambuc keyalg = *(++argv);
290ebfedea0SLionel Sambuc newreq = 1;
291*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-pkeyopt") == 0) {
292ebfedea0SLionel Sambuc if (--argc < 1)
293ebfedea0SLionel Sambuc goto bad;
294ebfedea0SLionel Sambuc if (!pkeyopts)
295ebfedea0SLionel Sambuc pkeyopts = sk_OPENSSL_STRING_new_null();
296ebfedea0SLionel Sambuc if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, *(++argv)))
297ebfedea0SLionel Sambuc goto bad;
298*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-sigopt") == 0) {
299ebfedea0SLionel Sambuc if (--argc < 1)
300ebfedea0SLionel Sambuc goto bad;
301ebfedea0SLionel Sambuc if (!sigopts)
302ebfedea0SLionel Sambuc sigopts = sk_OPENSSL_STRING_new_null();
303ebfedea0SLionel Sambuc if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
304ebfedea0SLionel Sambuc goto bad;
305*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-batch") == 0)
306ebfedea0SLionel Sambuc batch = 1;
307ebfedea0SLionel Sambuc else if (strcmp(*argv, "-newhdr") == 0)
308ebfedea0SLionel Sambuc newhdr = 1;
309ebfedea0SLionel Sambuc else if (strcmp(*argv, "-modulus") == 0)
310ebfedea0SLionel Sambuc modulus = 1;
311ebfedea0SLionel Sambuc else if (strcmp(*argv, "-verify") == 0)
312ebfedea0SLionel Sambuc verify = 1;
313ebfedea0SLionel Sambuc else if (strcmp(*argv, "-nodes") == 0)
314ebfedea0SLionel Sambuc nodes = 1;
315ebfedea0SLionel Sambuc else if (strcmp(*argv, "-noout") == 0)
316ebfedea0SLionel Sambuc noout = 1;
317ebfedea0SLionel Sambuc else if (strcmp(*argv, "-verbose") == 0)
318ebfedea0SLionel Sambuc verbose = 1;
319ebfedea0SLionel Sambuc else if (strcmp(*argv, "-utf8") == 0)
320ebfedea0SLionel Sambuc chtype = MBSTRING_UTF8;
321*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-nameopt") == 0) {
322*0a6a1f1dSLionel Sambuc if (--argc < 1)
323*0a6a1f1dSLionel Sambuc goto bad;
324*0a6a1f1dSLionel Sambuc if (!set_name_ex(&nmflag, *(++argv)))
325*0a6a1f1dSLionel Sambuc goto bad;
326*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-reqopt") == 0) {
327*0a6a1f1dSLionel Sambuc if (--argc < 1)
328*0a6a1f1dSLionel Sambuc goto bad;
329*0a6a1f1dSLionel Sambuc if (!set_cert_ex(&reqflag, *(++argv)))
330*0a6a1f1dSLionel Sambuc goto bad;
331*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-subject") == 0)
332ebfedea0SLionel Sambuc subject = 1;
333ebfedea0SLionel Sambuc else if (strcmp(*argv, "-text") == 0)
334ebfedea0SLionel Sambuc text = 1;
335ebfedea0SLionel Sambuc else if (strcmp(*argv, "-x509") == 0)
336ebfedea0SLionel Sambuc x509 = 1;
337ebfedea0SLionel Sambuc else if (strcmp(*argv, "-asn1-kludge") == 0)
338ebfedea0SLionel Sambuc kludge = 1;
339ebfedea0SLionel Sambuc else if (strcmp(*argv, "-no-asn1-kludge") == 0)
340ebfedea0SLionel Sambuc kludge = 0;
341*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-subj") == 0) {
342*0a6a1f1dSLionel Sambuc if (--argc < 1)
343*0a6a1f1dSLionel Sambuc goto bad;
344ebfedea0SLionel Sambuc subj = *(++argv);
345*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-multivalue-rdn") == 0)
346ebfedea0SLionel Sambuc multirdn = 1;
347*0a6a1f1dSLionel Sambuc else if (strcmp(*argv, "-days") == 0) {
348*0a6a1f1dSLionel Sambuc if (--argc < 1)
349*0a6a1f1dSLionel Sambuc goto bad;
350ebfedea0SLionel Sambuc days = atoi(*(++argv));
351*0a6a1f1dSLionel Sambuc if (days == 0)
352*0a6a1f1dSLionel Sambuc days = 30;
353*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-set_serial") == 0) {
354*0a6a1f1dSLionel Sambuc if (--argc < 1)
355*0a6a1f1dSLionel Sambuc goto bad;
356ebfedea0SLionel Sambuc serial = s2i_ASN1_INTEGER(NULL, *(++argv));
357*0a6a1f1dSLionel Sambuc if (!serial)
358*0a6a1f1dSLionel Sambuc goto bad;
359*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-extensions") == 0) {
360*0a6a1f1dSLionel Sambuc if (--argc < 1)
361*0a6a1f1dSLionel Sambuc goto bad;
362ebfedea0SLionel Sambuc extensions = *(++argv);
363*0a6a1f1dSLionel Sambuc } else if (strcmp(*argv, "-reqexts") == 0) {
364*0a6a1f1dSLionel Sambuc if (--argc < 1)
365*0a6a1f1dSLionel Sambuc goto bad;
366ebfedea0SLionel Sambuc req_exts = *(++argv);
367*0a6a1f1dSLionel Sambuc } else if ((md_alg = EVP_get_digestbyname(&((*argv)[1]))) != NULL) {
368ebfedea0SLionel Sambuc /* ok */
369ebfedea0SLionel Sambuc digest = md_alg;
370*0a6a1f1dSLionel Sambuc } else {
371ebfedea0SLionel Sambuc BIO_printf(bio_err, "unknown option %s\n", *argv);
372ebfedea0SLionel Sambuc badops = 1;
373ebfedea0SLionel Sambuc break;
374ebfedea0SLionel Sambuc }
375ebfedea0SLionel Sambuc argc--;
376ebfedea0SLionel Sambuc argv++;
377ebfedea0SLionel Sambuc }
378ebfedea0SLionel Sambuc
379*0a6a1f1dSLionel Sambuc if (badops) {
380ebfedea0SLionel Sambuc bad:
381ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
382ebfedea0SLionel Sambuc BIO_printf(bio_err, "where options are\n");
383ebfedea0SLionel Sambuc BIO_printf(bio_err, " -inform arg input format - DER or PEM\n");
384ebfedea0SLionel Sambuc BIO_printf(bio_err, " -outform arg output format - DER or PEM\n");
385ebfedea0SLionel Sambuc BIO_printf(bio_err, " -in arg input file\n");
386ebfedea0SLionel Sambuc BIO_printf(bio_err, " -out arg output file\n");
387ebfedea0SLionel Sambuc BIO_printf(bio_err, " -text text form of request\n");
388ebfedea0SLionel Sambuc BIO_printf(bio_err, " -pubkey output public key\n");
389ebfedea0SLionel Sambuc BIO_printf(bio_err, " -noout do not output REQ\n");
390ebfedea0SLionel Sambuc BIO_printf(bio_err, " -verify verify signature on REQ\n");
391ebfedea0SLionel Sambuc BIO_printf(bio_err, " -modulus RSA modulus\n");
392ebfedea0SLionel Sambuc BIO_printf(bio_err, " -nodes don't encrypt the output key\n");
393ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
394*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
395*0a6a1f1dSLionel Sambuc " -engine e use engine e, possibly a hardware device\n");
396ebfedea0SLionel Sambuc #endif
397ebfedea0SLionel Sambuc BIO_printf(bio_err, " -subject output the request's subject\n");
398ebfedea0SLionel Sambuc BIO_printf(bio_err, " -passin private key password source\n");
399*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
400*0a6a1f1dSLionel Sambuc " -key file use the private key contained in file\n");
401ebfedea0SLionel Sambuc BIO_printf(bio_err, " -keyform arg key file format\n");
402ebfedea0SLionel Sambuc BIO_printf(bio_err, " -keyout arg file to send the key to\n");
403*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
404*0a6a1f1dSLionel Sambuc LIST_SEPARATOR_CHAR);
405*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
406*0a6a1f1dSLionel Sambuc " load the file (or the files in the directory) into\n");
407ebfedea0SLionel Sambuc BIO_printf(bio_err, " the random number generator\n");
408*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
409*0a6a1f1dSLionel Sambuc " -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
410*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
411*0a6a1f1dSLionel Sambuc " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
412ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDSA
413*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
414*0a6a1f1dSLionel Sambuc " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
415ebfedea0SLionel Sambuc #endif
416*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
417*0a6a1f1dSLionel Sambuc " -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
418ebfedea0SLionel Sambuc BIO_printf(bio_err, " -config file request template file.\n");
419*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
420*0a6a1f1dSLionel Sambuc " -subj arg set or modify request subject\n");
421*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
422*0a6a1f1dSLionel Sambuc " -multivalue-rdn enable support for multivalued RDNs\n");
423ebfedea0SLionel Sambuc BIO_printf(bio_err, " -new new request.\n");
424*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
425*0a6a1f1dSLionel Sambuc " -batch do not ask anything during request generation\n");
426*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
427*0a6a1f1dSLionel Sambuc " -x509 output a x509 structure instead of a cert. req.\n");
428*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
429*0a6a1f1dSLionel Sambuc " -days number of days a certificate generated by -x509 is valid for.\n");
430*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
431*0a6a1f1dSLionel Sambuc " -set_serial serial number to use for a certificate generated by -x509.\n");
432*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
433*0a6a1f1dSLionel Sambuc " -newhdr output \"NEW\" in the header lines\n");
434*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
435*0a6a1f1dSLionel Sambuc " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n");
436*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
437*0a6a1f1dSLionel Sambuc " have been reported as requiring\n");
438*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
439*0a6a1f1dSLionel Sambuc " -extensions .. specify certificate extension section (override value in config file)\n");
440*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
441*0a6a1f1dSLionel Sambuc " -reqexts .. specify request extension section (override value in config file)\n");
442*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
443*0a6a1f1dSLionel Sambuc " -utf8 input characters are UTF8 (default ASCII)\n");
444*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
445*0a6a1f1dSLionel Sambuc " -nameopt arg - various certificate name options\n");
446*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
447*0a6a1f1dSLionel Sambuc " -reqopt arg - various request text options\n\n");
448ebfedea0SLionel Sambuc goto end;
449ebfedea0SLionel Sambuc }
450ebfedea0SLionel Sambuc
451ebfedea0SLionel Sambuc ERR_load_crypto_strings();
452ebfedea0SLionel Sambuc if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
453ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error getting passwords\n");
454ebfedea0SLionel Sambuc goto end;
455ebfedea0SLionel Sambuc }
456*0a6a1f1dSLionel Sambuc #ifndef MONOLITH /* else this has happened in openssl.c
457*0a6a1f1dSLionel Sambuc * (global `config') */
458ebfedea0SLionel Sambuc /* Lets load up our environment a little */
459ebfedea0SLionel Sambuc p = getenv("OPENSSL_CONF");
460ebfedea0SLionel Sambuc if (p == NULL)
461ebfedea0SLionel Sambuc p = getenv("SSLEAY_CONF");
462ebfedea0SLionel Sambuc if (p == NULL)
463ebfedea0SLionel Sambuc p = to_free = make_config_name();
464ebfedea0SLionel Sambuc default_config_file = p;
465ebfedea0SLionel Sambuc config = NCONF_new(NULL);
466ebfedea0SLionel Sambuc i = NCONF_load(config, p, &errline);
467ebfedea0SLionel Sambuc #endif
468ebfedea0SLionel Sambuc
469*0a6a1f1dSLionel Sambuc if (template != NULL) {
470ebfedea0SLionel Sambuc long errline = -1;
471ebfedea0SLionel Sambuc
472ebfedea0SLionel Sambuc if (verbose)
473ebfedea0SLionel Sambuc BIO_printf(bio_err, "Using configuration from %s\n", template);
474ebfedea0SLionel Sambuc req_conf = NCONF_new(NULL);
475ebfedea0SLionel Sambuc i = NCONF_load(req_conf, template, &errline);
476*0a6a1f1dSLionel Sambuc if (i == 0) {
477*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "error on line %ld of %s\n", errline,
478*0a6a1f1dSLionel Sambuc template);
479ebfedea0SLionel Sambuc goto end;
480ebfedea0SLionel Sambuc }
481*0a6a1f1dSLionel Sambuc } else {
482ebfedea0SLionel Sambuc req_conf = config;
483ebfedea0SLionel Sambuc
484*0a6a1f1dSLionel Sambuc if (req_conf == NULL) {
485*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Unable to load config info from %s\n",
486*0a6a1f1dSLionel Sambuc default_config_file);
487ebfedea0SLionel Sambuc if (newreq)
488ebfedea0SLionel Sambuc goto end;
489*0a6a1f1dSLionel Sambuc } else if (verbose)
490ebfedea0SLionel Sambuc BIO_printf(bio_err, "Using configuration from %s\n",
491ebfedea0SLionel Sambuc default_config_file);
492ebfedea0SLionel Sambuc }
493ebfedea0SLionel Sambuc
494*0a6a1f1dSLionel Sambuc if (req_conf != NULL) {
495ebfedea0SLionel Sambuc if (!load_config(bio_err, req_conf))
496ebfedea0SLionel Sambuc goto end;
497ebfedea0SLionel Sambuc p = NCONF_get_string(req_conf, NULL, "oid_file");
498ebfedea0SLionel Sambuc if (p == NULL)
499ebfedea0SLionel Sambuc ERR_clear_error();
500*0a6a1f1dSLionel Sambuc if (p != NULL) {
501ebfedea0SLionel Sambuc BIO *oid_bio;
502ebfedea0SLionel Sambuc
503ebfedea0SLionel Sambuc oid_bio = BIO_new_file(p, "r");
504*0a6a1f1dSLionel Sambuc if (oid_bio == NULL) {
505*0a6a1f1dSLionel Sambuc /*-
506ebfedea0SLionel Sambuc BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
507ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
508ebfedea0SLionel Sambuc */
509*0a6a1f1dSLionel Sambuc } else {
510ebfedea0SLionel Sambuc OBJ_create_objects(oid_bio);
511ebfedea0SLionel Sambuc BIO_free(oid_bio);
512ebfedea0SLionel Sambuc }
513ebfedea0SLionel Sambuc }
514ebfedea0SLionel Sambuc }
515*0a6a1f1dSLionel Sambuc if (!add_oid_section(bio_err, req_conf))
516*0a6a1f1dSLionel Sambuc goto end;
517ebfedea0SLionel Sambuc
518*0a6a1f1dSLionel Sambuc if (md_alg == NULL) {
519ebfedea0SLionel Sambuc p = NCONF_get_string(req_conf, SECTION, "default_md");
520ebfedea0SLionel Sambuc if (p == NULL)
521ebfedea0SLionel Sambuc ERR_clear_error();
522*0a6a1f1dSLionel Sambuc if (p != NULL) {
523ebfedea0SLionel Sambuc if ((md_alg = EVP_get_digestbyname(p)) != NULL)
524ebfedea0SLionel Sambuc digest = md_alg;
525ebfedea0SLionel Sambuc }
526ebfedea0SLionel Sambuc }
527ebfedea0SLionel Sambuc
528*0a6a1f1dSLionel Sambuc if (!extensions) {
529ebfedea0SLionel Sambuc extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
530ebfedea0SLionel Sambuc if (!extensions)
531ebfedea0SLionel Sambuc ERR_clear_error();
532ebfedea0SLionel Sambuc }
533ebfedea0SLionel Sambuc if (extensions) {
534ebfedea0SLionel Sambuc /* Check syntax of file */
535ebfedea0SLionel Sambuc X509V3_CTX ctx;
536ebfedea0SLionel Sambuc X509V3_set_ctx_test(&ctx);
537ebfedea0SLionel Sambuc X509V3_set_nconf(&ctx, req_conf);
538ebfedea0SLionel Sambuc if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
539ebfedea0SLionel Sambuc BIO_printf(bio_err,
540ebfedea0SLionel Sambuc "Error Loading extension section %s\n", extensions);
541ebfedea0SLionel Sambuc goto end;
542ebfedea0SLionel Sambuc }
543ebfedea0SLionel Sambuc }
544ebfedea0SLionel Sambuc
545*0a6a1f1dSLionel Sambuc if (!passin) {
546ebfedea0SLionel Sambuc passin = NCONF_get_string(req_conf, SECTION, "input_password");
547ebfedea0SLionel Sambuc if (!passin)
548ebfedea0SLionel Sambuc ERR_clear_error();
549ebfedea0SLionel Sambuc }
550ebfedea0SLionel Sambuc
551*0a6a1f1dSLionel Sambuc if (!passout) {
552ebfedea0SLionel Sambuc passout = NCONF_get_string(req_conf, SECTION, "output_password");
553ebfedea0SLionel Sambuc if (!passout)
554ebfedea0SLionel Sambuc ERR_clear_error();
555ebfedea0SLionel Sambuc }
556ebfedea0SLionel Sambuc
557ebfedea0SLionel Sambuc p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
558ebfedea0SLionel Sambuc if (!p)
559ebfedea0SLionel Sambuc ERR_clear_error();
560ebfedea0SLionel Sambuc
561ebfedea0SLionel Sambuc if (p && !ASN1_STRING_set_default_mask_asc(p)) {
562ebfedea0SLionel Sambuc BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
563ebfedea0SLionel Sambuc goto end;
564ebfedea0SLionel Sambuc }
565ebfedea0SLionel Sambuc
566*0a6a1f1dSLionel Sambuc if (chtype != MBSTRING_UTF8) {
567ebfedea0SLionel Sambuc p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
568ebfedea0SLionel Sambuc if (!p)
569ebfedea0SLionel Sambuc ERR_clear_error();
570ebfedea0SLionel Sambuc else if (!strcmp(p, "yes"))
571ebfedea0SLionel Sambuc chtype = MBSTRING_UTF8;
572ebfedea0SLionel Sambuc }
573ebfedea0SLionel Sambuc
574*0a6a1f1dSLionel Sambuc if (!req_exts) {
575ebfedea0SLionel Sambuc req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
576ebfedea0SLionel Sambuc if (!req_exts)
577ebfedea0SLionel Sambuc ERR_clear_error();
578ebfedea0SLionel Sambuc }
579ebfedea0SLionel Sambuc if (req_exts) {
580ebfedea0SLionel Sambuc /* Check syntax of file */
581ebfedea0SLionel Sambuc X509V3_CTX ctx;
582ebfedea0SLionel Sambuc X509V3_set_ctx_test(&ctx);
583ebfedea0SLionel Sambuc X509V3_set_nconf(&ctx, req_conf);
584ebfedea0SLionel Sambuc if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
585ebfedea0SLionel Sambuc BIO_printf(bio_err,
586ebfedea0SLionel Sambuc "Error Loading request extension section %s\n",
587ebfedea0SLionel Sambuc req_exts);
588ebfedea0SLionel Sambuc goto end;
589ebfedea0SLionel Sambuc }
590ebfedea0SLionel Sambuc }
591ebfedea0SLionel Sambuc
592ebfedea0SLionel Sambuc in = BIO_new(BIO_s_file());
593ebfedea0SLionel Sambuc out = BIO_new(BIO_s_file());
594ebfedea0SLionel Sambuc if ((in == NULL) || (out == NULL))
595ebfedea0SLionel Sambuc goto end;
596ebfedea0SLionel Sambuc
597ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
598ebfedea0SLionel Sambuc e = setup_engine(bio_err, engine, 0);
599ebfedea0SLionel Sambuc #endif
600ebfedea0SLionel Sambuc
601*0a6a1f1dSLionel Sambuc if (keyfile != NULL) {
602ebfedea0SLionel Sambuc pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
603ebfedea0SLionel Sambuc "Private Key");
604*0a6a1f1dSLionel Sambuc if (!pkey) {
605*0a6a1f1dSLionel Sambuc /*
606*0a6a1f1dSLionel Sambuc * load_key() has already printed an appropriate message
607*0a6a1f1dSLionel Sambuc */
608ebfedea0SLionel Sambuc goto end;
609*0a6a1f1dSLionel Sambuc } else {
610ebfedea0SLionel Sambuc char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
611ebfedea0SLionel Sambuc if (randfile == NULL)
612ebfedea0SLionel Sambuc ERR_clear_error();
613ebfedea0SLionel Sambuc app_RAND_load_file(randfile, bio_err, 0);
614ebfedea0SLionel Sambuc }
615ebfedea0SLionel Sambuc }
616ebfedea0SLionel Sambuc
617*0a6a1f1dSLionel Sambuc if (newreq && (pkey == NULL)) {
618ebfedea0SLionel Sambuc char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
619ebfedea0SLionel Sambuc if (randfile == NULL)
620ebfedea0SLionel Sambuc ERR_clear_error();
621ebfedea0SLionel Sambuc app_RAND_load_file(randfile, bio_err, 0);
622ebfedea0SLionel Sambuc if (inrand)
623ebfedea0SLionel Sambuc app_RAND_load_files(inrand);
624ebfedea0SLionel Sambuc
625*0a6a1f1dSLionel Sambuc if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
626*0a6a1f1dSLionel Sambuc newkey = DEFAULT_KEY_LENGTH;
627*0a6a1f1dSLionel Sambuc }
628*0a6a1f1dSLionel Sambuc
629*0a6a1f1dSLionel Sambuc if (keyalg) {
630ebfedea0SLionel Sambuc genctx = set_keygen_ctx(bio_err, keyalg, &pkey_type, &newkey,
631ebfedea0SLionel Sambuc &keyalgstr, gen_eng);
632ebfedea0SLionel Sambuc if (!genctx)
633ebfedea0SLionel Sambuc goto end;
634ebfedea0SLionel Sambuc }
635ebfedea0SLionel Sambuc
636*0a6a1f1dSLionel Sambuc if (newkey < MIN_KEY_LENGTH
637*0a6a1f1dSLionel Sambuc && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) {
638ebfedea0SLionel Sambuc BIO_printf(bio_err, "private key length is too short,\n");
639*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n",
640*0a6a1f1dSLionel Sambuc MIN_KEY_LENGTH, newkey);
641ebfedea0SLionel Sambuc goto end;
642ebfedea0SLionel Sambuc }
643ebfedea0SLionel Sambuc
644*0a6a1f1dSLionel Sambuc if (!genctx) {
645ebfedea0SLionel Sambuc genctx = set_keygen_ctx(bio_err, NULL, &pkey_type, &newkey,
646ebfedea0SLionel Sambuc &keyalgstr, gen_eng);
647ebfedea0SLionel Sambuc if (!genctx)
648ebfedea0SLionel Sambuc goto end;
649ebfedea0SLionel Sambuc }
650ebfedea0SLionel Sambuc
651*0a6a1f1dSLionel Sambuc if (pkeyopts) {
652ebfedea0SLionel Sambuc char *genopt;
653*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
654ebfedea0SLionel Sambuc genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
655*0a6a1f1dSLionel Sambuc if (pkey_ctrl_string(genctx, genopt) <= 0) {
656*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "parameter error \"%s\"\n", genopt);
657ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
658ebfedea0SLionel Sambuc goto end;
659ebfedea0SLionel Sambuc }
660ebfedea0SLionel Sambuc }
661ebfedea0SLionel Sambuc }
662ebfedea0SLionel Sambuc
663ebfedea0SLionel Sambuc BIO_printf(bio_err, "Generating a %ld bit %s private key\n",
664ebfedea0SLionel Sambuc newkey, keyalgstr);
665ebfedea0SLionel Sambuc
666ebfedea0SLionel Sambuc EVP_PKEY_CTX_set_cb(genctx, genpkey_cb);
667ebfedea0SLionel Sambuc EVP_PKEY_CTX_set_app_data(genctx, bio_err);
668ebfedea0SLionel Sambuc
669*0a6a1f1dSLionel Sambuc if (EVP_PKEY_keygen(genctx, &pkey) <= 0) {
670ebfedea0SLionel Sambuc BIO_puts(bio_err, "Error Generating Key\n");
671ebfedea0SLionel Sambuc goto end;
672ebfedea0SLionel Sambuc }
673ebfedea0SLionel Sambuc
674ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(genctx);
675ebfedea0SLionel Sambuc genctx = NULL;
676ebfedea0SLionel Sambuc
677ebfedea0SLionel Sambuc app_RAND_write_file(randfile, bio_err);
678ebfedea0SLionel Sambuc
679*0a6a1f1dSLionel Sambuc if (keyout == NULL) {
680ebfedea0SLionel Sambuc keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
681ebfedea0SLionel Sambuc if (keyout == NULL)
682ebfedea0SLionel Sambuc ERR_clear_error();
683ebfedea0SLionel Sambuc }
684ebfedea0SLionel Sambuc
685*0a6a1f1dSLionel Sambuc if (keyout == NULL) {
686ebfedea0SLionel Sambuc BIO_printf(bio_err, "writing new private key to stdout\n");
687ebfedea0SLionel Sambuc BIO_set_fp(out, stdout, BIO_NOCLOSE);
688ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
689ebfedea0SLionel Sambuc {
690ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
691ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
692ebfedea0SLionel Sambuc }
693ebfedea0SLionel Sambuc #endif
694*0a6a1f1dSLionel Sambuc } else {
695ebfedea0SLionel Sambuc BIO_printf(bio_err, "writing new private key to '%s'\n", keyout);
696*0a6a1f1dSLionel Sambuc if (BIO_write_filename(out, keyout) <= 0) {
697ebfedea0SLionel Sambuc perror(keyout);
698ebfedea0SLionel Sambuc goto end;
699ebfedea0SLionel Sambuc }
700ebfedea0SLionel Sambuc }
701ebfedea0SLionel Sambuc
702ebfedea0SLionel Sambuc p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key");
703*0a6a1f1dSLionel Sambuc if (p == NULL) {
704ebfedea0SLionel Sambuc ERR_clear_error();
705ebfedea0SLionel Sambuc p = NCONF_get_string(req_conf, SECTION, "encrypt_key");
706ebfedea0SLionel Sambuc if (p == NULL)
707ebfedea0SLionel Sambuc ERR_clear_error();
708ebfedea0SLionel Sambuc }
709ebfedea0SLionel Sambuc if ((p != NULL) && (strcmp(p, "no") == 0))
710ebfedea0SLionel Sambuc cipher = NULL;
711*0a6a1f1dSLionel Sambuc if (nodes)
712*0a6a1f1dSLionel Sambuc cipher = NULL;
713ebfedea0SLionel Sambuc
714ebfedea0SLionel Sambuc i = 0;
715ebfedea0SLionel Sambuc loop:
716ebfedea0SLionel Sambuc if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
717*0a6a1f1dSLionel Sambuc NULL, 0, NULL, passout)) {
718ebfedea0SLionel Sambuc if ((ERR_GET_REASON(ERR_peek_error()) ==
719*0a6a1f1dSLionel Sambuc PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
720ebfedea0SLionel Sambuc ERR_clear_error();
721ebfedea0SLionel Sambuc i++;
722ebfedea0SLionel Sambuc goto loop;
723ebfedea0SLionel Sambuc }
724ebfedea0SLionel Sambuc goto end;
725ebfedea0SLionel Sambuc }
726ebfedea0SLionel Sambuc BIO_printf(bio_err, "-----\n");
727ebfedea0SLionel Sambuc }
728ebfedea0SLionel Sambuc
729*0a6a1f1dSLionel Sambuc if (!newreq) {
730*0a6a1f1dSLionel Sambuc /*
731*0a6a1f1dSLionel Sambuc * Since we are using a pre-existing certificate request, the kludge
732*0a6a1f1dSLionel Sambuc * 'format' info should not be changed.
733*0a6a1f1dSLionel Sambuc */
734ebfedea0SLionel Sambuc kludge = -1;
735ebfedea0SLionel Sambuc if (infile == NULL)
736ebfedea0SLionel Sambuc BIO_set_fp(in, stdin, BIO_NOCLOSE);
737*0a6a1f1dSLionel Sambuc else {
738*0a6a1f1dSLionel Sambuc if (BIO_read_filename(in, infile) <= 0) {
739ebfedea0SLionel Sambuc perror(infile);
740ebfedea0SLionel Sambuc goto end;
741ebfedea0SLionel Sambuc }
742ebfedea0SLionel Sambuc }
743ebfedea0SLionel Sambuc
744ebfedea0SLionel Sambuc if (informat == FORMAT_ASN1)
745ebfedea0SLionel Sambuc req = d2i_X509_REQ_bio(in, NULL);
746ebfedea0SLionel Sambuc else if (informat == FORMAT_PEM)
747ebfedea0SLionel Sambuc req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
748*0a6a1f1dSLionel Sambuc else {
749*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
750*0a6a1f1dSLionel Sambuc "bad input format specified for X509 request\n");
751ebfedea0SLionel Sambuc goto end;
752ebfedea0SLionel Sambuc }
753*0a6a1f1dSLionel Sambuc if (req == NULL) {
754ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to load X509 request\n");
755ebfedea0SLionel Sambuc goto end;
756ebfedea0SLionel Sambuc }
757ebfedea0SLionel Sambuc }
758ebfedea0SLionel Sambuc
759*0a6a1f1dSLionel Sambuc if (newreq || x509) {
760*0a6a1f1dSLionel Sambuc if (pkey == NULL) {
761ebfedea0SLionel Sambuc BIO_printf(bio_err, "you need to specify a private key\n");
762ebfedea0SLionel Sambuc goto end;
763ebfedea0SLionel Sambuc }
764ebfedea0SLionel Sambuc
765*0a6a1f1dSLionel Sambuc if (req == NULL) {
766ebfedea0SLionel Sambuc req = X509_REQ_new();
767*0a6a1f1dSLionel Sambuc if (req == NULL) {
768ebfedea0SLionel Sambuc goto end;
769ebfedea0SLionel Sambuc }
770ebfedea0SLionel Sambuc
771ebfedea0SLionel Sambuc i = make_REQ(req, pkey, subj, multirdn, !x509, chtype);
772ebfedea0SLionel Sambuc subj = NULL; /* done processing '-subj' option */
773*0a6a1f1dSLionel Sambuc if ((kludge > 0)
774*0a6a1f1dSLionel Sambuc && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) {
775ebfedea0SLionel Sambuc sk_X509_ATTRIBUTE_free(req->req_info->attributes);
776ebfedea0SLionel Sambuc req->req_info->attributes = NULL;
777ebfedea0SLionel Sambuc }
778*0a6a1f1dSLionel Sambuc if (!i) {
779ebfedea0SLionel Sambuc BIO_printf(bio_err, "problems making Certificate Request\n");
780ebfedea0SLionel Sambuc goto end;
781ebfedea0SLionel Sambuc }
782ebfedea0SLionel Sambuc }
783*0a6a1f1dSLionel Sambuc if (x509) {
784ebfedea0SLionel Sambuc EVP_PKEY *tmppkey;
785ebfedea0SLionel Sambuc X509V3_CTX ext_ctx;
786*0a6a1f1dSLionel Sambuc if ((x509ss = X509_new()) == NULL)
787*0a6a1f1dSLionel Sambuc goto end;
788ebfedea0SLionel Sambuc
789ebfedea0SLionel Sambuc /* Set version to V3 */
790*0a6a1f1dSLionel Sambuc if (extensions && !X509_set_version(x509ss, 2))
791*0a6a1f1dSLionel Sambuc goto end;
792*0a6a1f1dSLionel Sambuc if (serial) {
793*0a6a1f1dSLionel Sambuc if (!X509_set_serialNumber(x509ss, serial))
794*0a6a1f1dSLionel Sambuc goto end;
795*0a6a1f1dSLionel Sambuc } else {
796*0a6a1f1dSLionel Sambuc if (!rand_serial(NULL, X509_get_serialNumber(x509ss)))
797ebfedea0SLionel Sambuc goto end;
798ebfedea0SLionel Sambuc }
799ebfedea0SLionel Sambuc
800*0a6a1f1dSLionel Sambuc if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
801*0a6a1f1dSLionel Sambuc goto end;
802*0a6a1f1dSLionel Sambuc if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0))
803*0a6a1f1dSLionel Sambuc goto end;
804*0a6a1f1dSLionel Sambuc if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL))
805*0a6a1f1dSLionel Sambuc goto end;
806*0a6a1f1dSLionel Sambuc if (!X509_set_subject_name
807*0a6a1f1dSLionel Sambuc (x509ss, X509_REQ_get_subject_name(req)))
808*0a6a1f1dSLionel Sambuc goto end;
809ebfedea0SLionel Sambuc tmppkey = X509_REQ_get_pubkey(req);
810*0a6a1f1dSLionel Sambuc if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey))
811*0a6a1f1dSLionel Sambuc goto end;
812ebfedea0SLionel Sambuc EVP_PKEY_free(tmppkey);
813ebfedea0SLionel Sambuc
814ebfedea0SLionel Sambuc /* Set up V3 context struct */
815ebfedea0SLionel Sambuc
816ebfedea0SLionel Sambuc X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
817ebfedea0SLionel Sambuc X509V3_set_nconf(&ext_ctx, req_conf);
818ebfedea0SLionel Sambuc
819ebfedea0SLionel Sambuc /* Add extensions */
820ebfedea0SLionel Sambuc if (extensions && !X509V3_EXT_add_nconf(req_conf,
821*0a6a1f1dSLionel Sambuc &ext_ctx, extensions,
822*0a6a1f1dSLionel Sambuc x509ss)) {
823*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Error Loading extension section %s\n",
824ebfedea0SLionel Sambuc extensions);
825ebfedea0SLionel Sambuc goto end;
826ebfedea0SLionel Sambuc }
827ebfedea0SLionel Sambuc
828ebfedea0SLionel Sambuc i = do_X509_sign(bio_err, x509ss, pkey, digest, sigopts);
829*0a6a1f1dSLionel Sambuc if (!i) {
830ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
831ebfedea0SLionel Sambuc goto end;
832ebfedea0SLionel Sambuc }
833*0a6a1f1dSLionel Sambuc } else {
834ebfedea0SLionel Sambuc X509V3_CTX ext_ctx;
835ebfedea0SLionel Sambuc
836ebfedea0SLionel Sambuc /* Set up V3 context struct */
837ebfedea0SLionel Sambuc
838ebfedea0SLionel Sambuc X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
839ebfedea0SLionel Sambuc X509V3_set_nconf(&ext_ctx, req_conf);
840ebfedea0SLionel Sambuc
841ebfedea0SLionel Sambuc /* Add extensions */
842ebfedea0SLionel Sambuc if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
843*0a6a1f1dSLionel Sambuc &ext_ctx, req_exts,
844*0a6a1f1dSLionel Sambuc req)) {
845*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Error Loading extension section %s\n",
846ebfedea0SLionel Sambuc req_exts);
847ebfedea0SLionel Sambuc goto end;
848ebfedea0SLionel Sambuc }
849ebfedea0SLionel Sambuc i = do_X509_REQ_sign(bio_err, req, pkey, digest, sigopts);
850*0a6a1f1dSLionel Sambuc if (!i) {
851ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
852ebfedea0SLionel Sambuc goto end;
853ebfedea0SLionel Sambuc }
854ebfedea0SLionel Sambuc }
855ebfedea0SLionel Sambuc }
856ebfedea0SLionel Sambuc
857*0a6a1f1dSLionel Sambuc if (subj && x509) {
858ebfedea0SLionel Sambuc BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
859ebfedea0SLionel Sambuc goto end;
860ebfedea0SLionel Sambuc }
861ebfedea0SLionel Sambuc
862*0a6a1f1dSLionel Sambuc if (subj && !x509) {
863*0a6a1f1dSLionel Sambuc if (verbose) {
864ebfedea0SLionel Sambuc BIO_printf(bio_err, "Modifying Request's Subject\n");
865*0a6a1f1dSLionel Sambuc print_name(bio_err, "old subject=",
866*0a6a1f1dSLionel Sambuc X509_REQ_get_subject_name(req), nmflag);
867ebfedea0SLionel Sambuc }
868ebfedea0SLionel Sambuc
869*0a6a1f1dSLionel Sambuc if (build_subject(req, subj, chtype, multirdn) == 0) {
870ebfedea0SLionel Sambuc BIO_printf(bio_err, "ERROR: cannot modify subject\n");
871ebfedea0SLionel Sambuc ex = 1;
872ebfedea0SLionel Sambuc goto end;
873ebfedea0SLionel Sambuc }
874ebfedea0SLionel Sambuc
875ebfedea0SLionel Sambuc req->req_info->enc.modified = 1;
876ebfedea0SLionel Sambuc
877*0a6a1f1dSLionel Sambuc if (verbose) {
878*0a6a1f1dSLionel Sambuc print_name(bio_err, "new subject=",
879*0a6a1f1dSLionel Sambuc X509_REQ_get_subject_name(req), nmflag);
880ebfedea0SLionel Sambuc }
881ebfedea0SLionel Sambuc }
882ebfedea0SLionel Sambuc
883*0a6a1f1dSLionel Sambuc if (verify && !x509) {
884ebfedea0SLionel Sambuc int tmp = 0;
885ebfedea0SLionel Sambuc
886*0a6a1f1dSLionel Sambuc if (pkey == NULL) {
887ebfedea0SLionel Sambuc pkey = X509_REQ_get_pubkey(req);
888ebfedea0SLionel Sambuc tmp = 1;
889*0a6a1f1dSLionel Sambuc if (pkey == NULL)
890*0a6a1f1dSLionel Sambuc goto end;
891ebfedea0SLionel Sambuc }
892ebfedea0SLionel Sambuc
893ebfedea0SLionel Sambuc i = X509_REQ_verify(req, pkey);
894ebfedea0SLionel Sambuc if (tmp) {
895ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
896ebfedea0SLionel Sambuc pkey = NULL;
897ebfedea0SLionel Sambuc }
898ebfedea0SLionel Sambuc
899*0a6a1f1dSLionel Sambuc if (i < 0) {
900ebfedea0SLionel Sambuc goto end;
901*0a6a1f1dSLionel Sambuc } else if (i == 0) {
902ebfedea0SLionel Sambuc BIO_printf(bio_err, "verify failure\n");
903ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
904*0a6a1f1dSLionel Sambuc } else /* if (i > 0) */
905ebfedea0SLionel Sambuc BIO_printf(bio_err, "verify OK\n");
906ebfedea0SLionel Sambuc }
907ebfedea0SLionel Sambuc
908*0a6a1f1dSLionel Sambuc if (noout && !text && !modulus && !subject && !pubkey) {
909ebfedea0SLionel Sambuc ex = 0;
910ebfedea0SLionel Sambuc goto end;
911ebfedea0SLionel Sambuc }
912ebfedea0SLionel Sambuc
913*0a6a1f1dSLionel Sambuc if (outfile == NULL) {
914ebfedea0SLionel Sambuc BIO_set_fp(out, stdout, BIO_NOCLOSE);
915ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
916ebfedea0SLionel Sambuc {
917ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
918ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
919ebfedea0SLionel Sambuc }
920ebfedea0SLionel Sambuc #endif
921*0a6a1f1dSLionel Sambuc } else {
922ebfedea0SLionel Sambuc if ((keyout != NULL) && (strcmp(outfile, keyout) == 0))
923ebfedea0SLionel Sambuc i = (int)BIO_append_filename(out, outfile);
924ebfedea0SLionel Sambuc else
925ebfedea0SLionel Sambuc i = (int)BIO_write_filename(out, outfile);
926*0a6a1f1dSLionel Sambuc if (!i) {
927ebfedea0SLionel Sambuc perror(outfile);
928ebfedea0SLionel Sambuc goto end;
929ebfedea0SLionel Sambuc }
930ebfedea0SLionel Sambuc }
931ebfedea0SLionel Sambuc
932*0a6a1f1dSLionel Sambuc if (pubkey) {
933ebfedea0SLionel Sambuc EVP_PKEY *tpubkey;
934ebfedea0SLionel Sambuc tpubkey = X509_REQ_get_pubkey(req);
935*0a6a1f1dSLionel Sambuc if (tpubkey == NULL) {
936ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error getting public key\n");
937ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
938ebfedea0SLionel Sambuc goto end;
939ebfedea0SLionel Sambuc }
940ebfedea0SLionel Sambuc PEM_write_bio_PUBKEY(out, tpubkey);
941ebfedea0SLionel Sambuc EVP_PKEY_free(tpubkey);
942ebfedea0SLionel Sambuc }
943ebfedea0SLionel Sambuc
944*0a6a1f1dSLionel Sambuc if (text) {
945ebfedea0SLionel Sambuc if (x509)
946ebfedea0SLionel Sambuc X509_print_ex(out, x509ss, nmflag, reqflag);
947ebfedea0SLionel Sambuc else
948ebfedea0SLionel Sambuc X509_REQ_print_ex(out, req, nmflag, reqflag);
949ebfedea0SLionel Sambuc }
950ebfedea0SLionel Sambuc
951*0a6a1f1dSLionel Sambuc if (subject) {
952ebfedea0SLionel Sambuc if (x509)
953*0a6a1f1dSLionel Sambuc print_name(out, "subject=", X509_get_subject_name(x509ss),
954*0a6a1f1dSLionel Sambuc nmflag);
955ebfedea0SLionel Sambuc else
956*0a6a1f1dSLionel Sambuc print_name(out, "subject=", X509_REQ_get_subject_name(req),
957*0a6a1f1dSLionel Sambuc nmflag);
958ebfedea0SLionel Sambuc }
959ebfedea0SLionel Sambuc
960*0a6a1f1dSLionel Sambuc if (modulus) {
961ebfedea0SLionel Sambuc EVP_PKEY *tpubkey;
962ebfedea0SLionel Sambuc
963ebfedea0SLionel Sambuc if (x509)
964ebfedea0SLionel Sambuc tpubkey = X509_get_pubkey(x509ss);
965ebfedea0SLionel Sambuc else
966ebfedea0SLionel Sambuc tpubkey = X509_REQ_get_pubkey(req);
967*0a6a1f1dSLionel Sambuc if (tpubkey == NULL) {
968ebfedea0SLionel Sambuc fprintf(stdout, "Modulus=unavailable\n");
969ebfedea0SLionel Sambuc goto end;
970ebfedea0SLionel Sambuc }
971ebfedea0SLionel Sambuc fprintf(stdout, "Modulus=");
972ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
973ebfedea0SLionel Sambuc if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA)
974ebfedea0SLionel Sambuc BN_print(out, tpubkey->pkey.rsa->n);
975ebfedea0SLionel Sambuc else
976ebfedea0SLionel Sambuc #endif
977ebfedea0SLionel Sambuc fprintf(stdout, "Wrong Algorithm type");
978ebfedea0SLionel Sambuc EVP_PKEY_free(tpubkey);
979ebfedea0SLionel Sambuc fprintf(stdout, "\n");
980ebfedea0SLionel Sambuc }
981ebfedea0SLionel Sambuc
982*0a6a1f1dSLionel Sambuc if (!noout && !x509) {
983ebfedea0SLionel Sambuc if (outformat == FORMAT_ASN1)
984ebfedea0SLionel Sambuc i = i2d_X509_REQ_bio(out, req);
985ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM) {
986*0a6a1f1dSLionel Sambuc if (newhdr)
987*0a6a1f1dSLionel Sambuc i = PEM_write_bio_X509_REQ_NEW(out, req);
988*0a6a1f1dSLionel Sambuc else
989*0a6a1f1dSLionel Sambuc i = PEM_write_bio_X509_REQ(out, req);
990ebfedea0SLionel Sambuc } else {
991ebfedea0SLionel Sambuc BIO_printf(bio_err, "bad output format specified for outfile\n");
992ebfedea0SLionel Sambuc goto end;
993ebfedea0SLionel Sambuc }
994*0a6a1f1dSLionel Sambuc if (!i) {
995ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to write X509 request\n");
996ebfedea0SLionel Sambuc goto end;
997ebfedea0SLionel Sambuc }
998ebfedea0SLionel Sambuc }
999*0a6a1f1dSLionel Sambuc if (!noout && x509 && (x509ss != NULL)) {
1000ebfedea0SLionel Sambuc if (outformat == FORMAT_ASN1)
1001ebfedea0SLionel Sambuc i = i2d_X509_bio(out, x509ss);
1002ebfedea0SLionel Sambuc else if (outformat == FORMAT_PEM)
1003ebfedea0SLionel Sambuc i = PEM_write_bio_X509(out, x509ss);
1004ebfedea0SLionel Sambuc else {
1005ebfedea0SLionel Sambuc BIO_printf(bio_err, "bad output format specified for outfile\n");
1006ebfedea0SLionel Sambuc goto end;
1007ebfedea0SLionel Sambuc }
1008*0a6a1f1dSLionel Sambuc if (!i) {
1009ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to write X509 certificate\n");
1010ebfedea0SLionel Sambuc goto end;
1011ebfedea0SLionel Sambuc }
1012ebfedea0SLionel Sambuc }
1013ebfedea0SLionel Sambuc ex = 0;
1014ebfedea0SLionel Sambuc end:
1015ebfedea0SLionel Sambuc #ifndef MONOLITH
1016ebfedea0SLionel Sambuc if (to_free)
1017ebfedea0SLionel Sambuc OPENSSL_free(to_free);
1018ebfedea0SLionel Sambuc #endif
1019*0a6a1f1dSLionel Sambuc if (ex) {
1020ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
1021ebfedea0SLionel Sambuc }
1022*0a6a1f1dSLionel Sambuc if ((req_conf != NULL) && (req_conf != config))
1023*0a6a1f1dSLionel Sambuc NCONF_free(req_conf);
1024ebfedea0SLionel Sambuc BIO_free(in);
1025ebfedea0SLionel Sambuc BIO_free_all(out);
1026ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
1027ebfedea0SLionel Sambuc if (genctx)
1028ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(genctx);
1029ebfedea0SLionel Sambuc if (pkeyopts)
1030ebfedea0SLionel Sambuc sk_OPENSSL_STRING_free(pkeyopts);
1031ebfedea0SLionel Sambuc if (sigopts)
1032ebfedea0SLionel Sambuc sk_OPENSSL_STRING_free(sigopts);
1033ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
1034ebfedea0SLionel Sambuc if (gen_eng)
1035ebfedea0SLionel Sambuc ENGINE_free(gen_eng);
1036ebfedea0SLionel Sambuc #endif
1037ebfedea0SLionel Sambuc if (keyalgstr)
1038ebfedea0SLionel Sambuc OPENSSL_free(keyalgstr);
1039ebfedea0SLionel Sambuc X509_REQ_free(req);
1040ebfedea0SLionel Sambuc X509_free(x509ss);
1041ebfedea0SLionel Sambuc ASN1_INTEGER_free(serial);
1042*0a6a1f1dSLionel Sambuc if (passargin && passin)
1043*0a6a1f1dSLionel Sambuc OPENSSL_free(passin);
1044*0a6a1f1dSLionel Sambuc if (passargout && passout)
1045*0a6a1f1dSLionel Sambuc OPENSSL_free(passout);
1046ebfedea0SLionel Sambuc OBJ_cleanup();
1047ebfedea0SLionel Sambuc apps_shutdown();
1048ebfedea0SLionel Sambuc OPENSSL_EXIT(ex);
1049ebfedea0SLionel Sambuc }
1050ebfedea0SLionel Sambuc
make_REQ(X509_REQ * req,EVP_PKEY * pkey,char * subj,int multirdn,int attribs,unsigned long chtype)1051ebfedea0SLionel Sambuc static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
1052ebfedea0SLionel Sambuc int attribs, unsigned long chtype)
1053ebfedea0SLionel Sambuc {
1054ebfedea0SLionel Sambuc int ret = 0, i;
1055ebfedea0SLionel Sambuc char no_prompt = 0;
1056ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
1057ebfedea0SLionel Sambuc char *tmp, *dn_sect, *attr_sect;
1058ebfedea0SLionel Sambuc
1059ebfedea0SLionel Sambuc tmp = NCONF_get_string(req_conf, SECTION, PROMPT);
1060ebfedea0SLionel Sambuc if (tmp == NULL)
1061ebfedea0SLionel Sambuc ERR_clear_error();
1062*0a6a1f1dSLionel Sambuc if ((tmp != NULL) && !strcmp(tmp, "no"))
1063*0a6a1f1dSLionel Sambuc no_prompt = 1;
1064ebfedea0SLionel Sambuc
1065ebfedea0SLionel Sambuc dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME);
1066*0a6a1f1dSLionel Sambuc if (dn_sect == NULL) {
1067ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to find '%s' in config\n",
1068ebfedea0SLionel Sambuc DISTINGUISHED_NAME);
1069ebfedea0SLionel Sambuc goto err;
1070ebfedea0SLionel Sambuc }
1071ebfedea0SLionel Sambuc dn_sk = NCONF_get_section(req_conf, dn_sect);
1072*0a6a1f1dSLionel Sambuc if (dn_sk == NULL) {
1073ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect);
1074ebfedea0SLionel Sambuc goto err;
1075ebfedea0SLionel Sambuc }
1076ebfedea0SLionel Sambuc
1077ebfedea0SLionel Sambuc attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES);
1078*0a6a1f1dSLionel Sambuc if (attr_sect == NULL) {
1079ebfedea0SLionel Sambuc ERR_clear_error();
1080ebfedea0SLionel Sambuc attr_sk = NULL;
1081*0a6a1f1dSLionel Sambuc } else {
1082ebfedea0SLionel Sambuc attr_sk = NCONF_get_section(req_conf, attr_sect);
1083*0a6a1f1dSLionel Sambuc if (attr_sk == NULL) {
1084ebfedea0SLionel Sambuc BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect);
1085ebfedea0SLionel Sambuc goto err;
1086ebfedea0SLionel Sambuc }
1087ebfedea0SLionel Sambuc }
1088ebfedea0SLionel Sambuc
1089ebfedea0SLionel Sambuc /* setup version number */
1090*0a6a1f1dSLionel Sambuc if (!X509_REQ_set_version(req, 0L))
1091*0a6a1f1dSLionel Sambuc goto err; /* version 1 */
1092ebfedea0SLionel Sambuc
1093ebfedea0SLionel Sambuc if (no_prompt)
1094ebfedea0SLionel Sambuc i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
1095*0a6a1f1dSLionel Sambuc else {
1096ebfedea0SLionel Sambuc if (subj)
1097ebfedea0SLionel Sambuc i = build_subject(req, subj, chtype, multirdn);
1098ebfedea0SLionel Sambuc else
1099*0a6a1f1dSLionel Sambuc i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs,
1100*0a6a1f1dSLionel Sambuc chtype);
1101ebfedea0SLionel Sambuc }
1102*0a6a1f1dSLionel Sambuc if (!i)
1103*0a6a1f1dSLionel Sambuc goto err;
1104ebfedea0SLionel Sambuc
1105*0a6a1f1dSLionel Sambuc if (!X509_REQ_set_pubkey(req, pkey))
1106*0a6a1f1dSLionel Sambuc goto err;
1107ebfedea0SLionel Sambuc
1108ebfedea0SLionel Sambuc ret = 1;
1109ebfedea0SLionel Sambuc err:
1110ebfedea0SLionel Sambuc return (ret);
1111ebfedea0SLionel Sambuc }
1112ebfedea0SLionel Sambuc
1113ebfedea0SLionel Sambuc /*
1114ebfedea0SLionel Sambuc * subject is expected to be in the format /type0=value0/type1=value1/type2=...
1115ebfedea0SLionel Sambuc * where characters may be escaped by \
1116ebfedea0SLionel Sambuc */
build_subject(X509_REQ * req,char * subject,unsigned long chtype,int multirdn)1117*0a6a1f1dSLionel Sambuc static int build_subject(X509_REQ *req, char *subject, unsigned long chtype,
1118*0a6a1f1dSLionel Sambuc int multirdn)
1119ebfedea0SLionel Sambuc {
1120ebfedea0SLionel Sambuc X509_NAME *n;
1121ebfedea0SLionel Sambuc
1122ebfedea0SLionel Sambuc if (!(n = parse_name(subject, chtype, multirdn)))
1123ebfedea0SLionel Sambuc return 0;
1124ebfedea0SLionel Sambuc
1125*0a6a1f1dSLionel Sambuc if (!X509_REQ_set_subject_name(req, n)) {
1126ebfedea0SLionel Sambuc X509_NAME_free(n);
1127ebfedea0SLionel Sambuc return 0;
1128ebfedea0SLionel Sambuc }
1129ebfedea0SLionel Sambuc X509_NAME_free(n);
1130ebfedea0SLionel Sambuc return 1;
1131ebfedea0SLionel Sambuc }
1132ebfedea0SLionel Sambuc
prompt_info(X509_REQ * req,STACK_OF (CONF_VALUE)* dn_sk,char * dn_sect,STACK_OF (CONF_VALUE)* attr_sk,char * attr_sect,int attribs,unsigned long chtype)1133ebfedea0SLionel Sambuc static int prompt_info(X509_REQ *req,
1134ebfedea0SLionel Sambuc STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
1135*0a6a1f1dSLionel Sambuc STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect,
1136*0a6a1f1dSLionel Sambuc int attribs, unsigned long chtype)
1137ebfedea0SLionel Sambuc {
1138ebfedea0SLionel Sambuc int i;
1139ebfedea0SLionel Sambuc char *p, *q;
1140ebfedea0SLionel Sambuc char buf[100];
1141ebfedea0SLionel Sambuc int nid, mval;
1142ebfedea0SLionel Sambuc long n_min, n_max;
1143ebfedea0SLionel Sambuc char *type, *value;
1144ebfedea0SLionel Sambuc const char *def;
1145ebfedea0SLionel Sambuc CONF_VALUE *v;
1146ebfedea0SLionel Sambuc X509_NAME *subj;
1147ebfedea0SLionel Sambuc subj = X509_REQ_get_subject_name(req);
1148ebfedea0SLionel Sambuc
1149*0a6a1f1dSLionel Sambuc if (!batch) {
1150*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1151*0a6a1f1dSLionel Sambuc "You are about to be asked to enter information that will be incorporated\n");
1152ebfedea0SLionel Sambuc BIO_printf(bio_err, "into your certificate request.\n");
1153*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1154*0a6a1f1dSLionel Sambuc "What you are about to enter is what is called a Distinguished Name or a DN.\n");
1155*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1156*0a6a1f1dSLionel Sambuc "There are quite a few fields but you can leave some blank\n");
1157*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1158*0a6a1f1dSLionel Sambuc "For some fields there will be a default value,\n");
1159*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1160*0a6a1f1dSLionel Sambuc "If you enter '.', the field will be left blank.\n");
1161ebfedea0SLionel Sambuc BIO_printf(bio_err, "-----\n");
1162ebfedea0SLionel Sambuc }
1163ebfedea0SLionel Sambuc
1164*0a6a1f1dSLionel Sambuc if (sk_CONF_VALUE_num(dn_sk)) {
1165ebfedea0SLionel Sambuc i = -1;
1166*0a6a1f1dSLionel Sambuc start:for (;;) {
1167ebfedea0SLionel Sambuc i++;
1168*0a6a1f1dSLionel Sambuc if (sk_CONF_VALUE_num(dn_sk) <= i)
1169*0a6a1f1dSLionel Sambuc break;
1170ebfedea0SLionel Sambuc
1171ebfedea0SLionel Sambuc v = sk_CONF_VALUE_value(dn_sk, i);
1172ebfedea0SLionel Sambuc p = q = NULL;
1173ebfedea0SLionel Sambuc type = v->name;
1174ebfedea0SLionel Sambuc if (!check_end(type, "_min") || !check_end(type, "_max") ||
1175*0a6a1f1dSLionel Sambuc !check_end(type, "_default") || !check_end(type, "_value"))
1176*0a6a1f1dSLionel Sambuc continue;
1177*0a6a1f1dSLionel Sambuc /*
1178*0a6a1f1dSLionel Sambuc * Skip past any leading X. X: X, etc to allow for multiple
1179*0a6a1f1dSLionel Sambuc * instances
1180ebfedea0SLionel Sambuc */
1181ebfedea0SLionel Sambuc for (p = v->name; *p; p++)
1182*0a6a1f1dSLionel Sambuc if ((*p == ':') || (*p == ',') || (*p == '.')) {
1183ebfedea0SLionel Sambuc p++;
1184*0a6a1f1dSLionel Sambuc if (*p)
1185*0a6a1f1dSLionel Sambuc type = p;
1186ebfedea0SLionel Sambuc break;
1187ebfedea0SLionel Sambuc }
1188*0a6a1f1dSLionel Sambuc if (*type == '+') {
1189ebfedea0SLionel Sambuc mval = -1;
1190ebfedea0SLionel Sambuc type++;
1191*0a6a1f1dSLionel Sambuc } else
1192ebfedea0SLionel Sambuc mval = 0;
1193ebfedea0SLionel Sambuc /* If OBJ not recognised ignore it */
1194*0a6a1f1dSLionel Sambuc if ((nid = OBJ_txt2nid(type)) == NID_undef)
1195*0a6a1f1dSLionel Sambuc goto start;
1196ebfedea0SLionel Sambuc if (BIO_snprintf(buf, sizeof buf, "%s_default", v->name)
1197*0a6a1f1dSLionel Sambuc >= (int)sizeof(buf)) {
1198ebfedea0SLionel Sambuc BIO_printf(bio_err, "Name '%s' too long\n", v->name);
1199ebfedea0SLionel Sambuc return 0;
1200ebfedea0SLionel Sambuc }
1201ebfedea0SLionel Sambuc
1202*0a6a1f1dSLionel Sambuc if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
1203ebfedea0SLionel Sambuc ERR_clear_error();
1204ebfedea0SLionel Sambuc def = "";
1205ebfedea0SLionel Sambuc }
1206ebfedea0SLionel Sambuc
1207ebfedea0SLionel Sambuc BIO_snprintf(buf, sizeof buf, "%s_value", v->name);
1208*0a6a1f1dSLionel Sambuc if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) {
1209ebfedea0SLionel Sambuc ERR_clear_error();
1210ebfedea0SLionel Sambuc value = NULL;
1211ebfedea0SLionel Sambuc }
1212ebfedea0SLionel Sambuc
1213ebfedea0SLionel Sambuc BIO_snprintf(buf, sizeof buf, "%s_min", v->name);
1214*0a6a1f1dSLionel Sambuc if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) {
1215ebfedea0SLionel Sambuc ERR_clear_error();
1216ebfedea0SLionel Sambuc n_min = -1;
1217ebfedea0SLionel Sambuc }
1218ebfedea0SLionel Sambuc
1219ebfedea0SLionel Sambuc BIO_snprintf(buf, sizeof buf, "%s_max", v->name);
1220*0a6a1f1dSLionel Sambuc if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) {
1221ebfedea0SLionel Sambuc ERR_clear_error();
1222ebfedea0SLionel Sambuc n_max = -1;
1223ebfedea0SLionel Sambuc }
1224ebfedea0SLionel Sambuc
1225ebfedea0SLionel Sambuc if (!add_DN_object(subj, v->value, def, value, nid,
1226ebfedea0SLionel Sambuc n_min, n_max, chtype, mval))
1227ebfedea0SLionel Sambuc return 0;
1228ebfedea0SLionel Sambuc }
1229*0a6a1f1dSLionel Sambuc if (X509_NAME_entry_count(subj) == 0) {
1230*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1231*0a6a1f1dSLionel Sambuc "error, no objects specified in config file\n");
1232ebfedea0SLionel Sambuc return 0;
1233ebfedea0SLionel Sambuc }
1234ebfedea0SLionel Sambuc
1235*0a6a1f1dSLionel Sambuc if (attribs) {
1236*0a6a1f1dSLionel Sambuc if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0)
1237*0a6a1f1dSLionel Sambuc && (!batch)) {
1238*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1239*0a6a1f1dSLionel Sambuc "\nPlease enter the following 'extra' attributes\n");
1240*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1241*0a6a1f1dSLionel Sambuc "to be sent with your certificate request\n");
1242ebfedea0SLionel Sambuc }
1243ebfedea0SLionel Sambuc
1244ebfedea0SLionel Sambuc i = -1;
1245*0a6a1f1dSLionel Sambuc start2: for (;;) {
1246ebfedea0SLionel Sambuc i++;
1247*0a6a1f1dSLionel Sambuc if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i))
1248ebfedea0SLionel Sambuc break;
1249ebfedea0SLionel Sambuc
1250ebfedea0SLionel Sambuc v = sk_CONF_VALUE_value(attr_sk, i);
1251ebfedea0SLionel Sambuc type = v->name;
1252ebfedea0SLionel Sambuc if ((nid = OBJ_txt2nid(type)) == NID_undef)
1253ebfedea0SLionel Sambuc goto start2;
1254ebfedea0SLionel Sambuc
1255ebfedea0SLionel Sambuc if (BIO_snprintf(buf, sizeof buf, "%s_default", type)
1256*0a6a1f1dSLionel Sambuc >= (int)sizeof(buf)) {
1257ebfedea0SLionel Sambuc BIO_printf(bio_err, "Name '%s' too long\n", v->name);
1258ebfedea0SLionel Sambuc return 0;
1259ebfedea0SLionel Sambuc }
1260ebfedea0SLionel Sambuc
1261ebfedea0SLionel Sambuc if ((def = NCONF_get_string(req_conf, attr_sect, buf))
1262*0a6a1f1dSLionel Sambuc == NULL) {
1263ebfedea0SLionel Sambuc ERR_clear_error();
1264ebfedea0SLionel Sambuc def = "";
1265ebfedea0SLionel Sambuc }
1266ebfedea0SLionel Sambuc
1267ebfedea0SLionel Sambuc BIO_snprintf(buf, sizeof buf, "%s_value", type);
1268ebfedea0SLionel Sambuc if ((value = NCONF_get_string(req_conf, attr_sect, buf))
1269*0a6a1f1dSLionel Sambuc == NULL) {
1270ebfedea0SLionel Sambuc ERR_clear_error();
1271ebfedea0SLionel Sambuc value = NULL;
1272ebfedea0SLionel Sambuc }
1273ebfedea0SLionel Sambuc
1274ebfedea0SLionel Sambuc BIO_snprintf(buf, sizeof buf, "%s_min", type);
1275*0a6a1f1dSLionel Sambuc if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) {
1276ebfedea0SLionel Sambuc ERR_clear_error();
1277ebfedea0SLionel Sambuc n_min = -1;
1278ebfedea0SLionel Sambuc }
1279ebfedea0SLionel Sambuc
1280ebfedea0SLionel Sambuc BIO_snprintf(buf, sizeof buf, "%s_max", type);
1281*0a6a1f1dSLionel Sambuc if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) {
1282ebfedea0SLionel Sambuc ERR_clear_error();
1283ebfedea0SLionel Sambuc n_max = -1;
1284ebfedea0SLionel Sambuc }
1285ebfedea0SLionel Sambuc
1286ebfedea0SLionel Sambuc if (!add_attribute_object(req,
1287*0a6a1f1dSLionel Sambuc v->value, def, value, nid, n_min,
1288*0a6a1f1dSLionel Sambuc n_max, chtype))
1289ebfedea0SLionel Sambuc return 0;
1290ebfedea0SLionel Sambuc }
1291ebfedea0SLionel Sambuc }
1292*0a6a1f1dSLionel Sambuc } else {
1293ebfedea0SLionel Sambuc BIO_printf(bio_err, "No template, please set one up.\n");
1294ebfedea0SLionel Sambuc return 0;
1295ebfedea0SLionel Sambuc }
1296ebfedea0SLionel Sambuc
1297ebfedea0SLionel Sambuc return 1;
1298ebfedea0SLionel Sambuc
1299ebfedea0SLionel Sambuc }
1300ebfedea0SLionel Sambuc
auto_info(X509_REQ * req,STACK_OF (CONF_VALUE)* dn_sk,STACK_OF (CONF_VALUE)* attr_sk,int attribs,unsigned long chtype)1301ebfedea0SLionel Sambuc static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
1302*0a6a1f1dSLionel Sambuc STACK_OF(CONF_VALUE) *attr_sk, int attribs,
1303*0a6a1f1dSLionel Sambuc unsigned long chtype)
1304ebfedea0SLionel Sambuc {
1305ebfedea0SLionel Sambuc int i;
1306ebfedea0SLionel Sambuc char *p, *q;
1307ebfedea0SLionel Sambuc char *type;
1308ebfedea0SLionel Sambuc CONF_VALUE *v;
1309ebfedea0SLionel Sambuc X509_NAME *subj;
1310ebfedea0SLionel Sambuc
1311ebfedea0SLionel Sambuc subj = X509_REQ_get_subject_name(req);
1312ebfedea0SLionel Sambuc
1313*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1314ebfedea0SLionel Sambuc int mval;
1315ebfedea0SLionel Sambuc v = sk_CONF_VALUE_value(dn_sk, i);
1316ebfedea0SLionel Sambuc p = q = NULL;
1317ebfedea0SLionel Sambuc type = v->name;
1318*0a6a1f1dSLionel Sambuc /*
1319*0a6a1f1dSLionel Sambuc * Skip past any leading X. X: X, etc to allow for multiple instances
1320ebfedea0SLionel Sambuc */
1321ebfedea0SLionel Sambuc for (p = v->name; *p; p++)
1322ebfedea0SLionel Sambuc #ifndef CHARSET_EBCDIC
1323ebfedea0SLionel Sambuc if ((*p == ':') || (*p == ',') || (*p == '.')) {
1324ebfedea0SLionel Sambuc #else
1325*0a6a1f1dSLionel Sambuc if ((*p == os_toascii[':']) || (*p == os_toascii[','])
1326*0a6a1f1dSLionel Sambuc || (*p == os_toascii['.'])) {
1327ebfedea0SLionel Sambuc #endif
1328ebfedea0SLionel Sambuc p++;
1329*0a6a1f1dSLionel Sambuc if (*p)
1330*0a6a1f1dSLionel Sambuc type = p;
1331ebfedea0SLionel Sambuc break;
1332ebfedea0SLionel Sambuc }
1333ebfedea0SLionel Sambuc #ifndef CHARSET_EBCDIC
1334ebfedea0SLionel Sambuc if (*p == '+')
1335ebfedea0SLionel Sambuc #else
1336ebfedea0SLionel Sambuc if (*p == os_toascii['+'])
1337ebfedea0SLionel Sambuc #endif
1338ebfedea0SLionel Sambuc {
1339ebfedea0SLionel Sambuc p++;
1340ebfedea0SLionel Sambuc mval = -1;
1341*0a6a1f1dSLionel Sambuc } else
1342ebfedea0SLionel Sambuc mval = 0;
1343ebfedea0SLionel Sambuc if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
1344*0a6a1f1dSLionel Sambuc (unsigned char *)v->value, -1, -1,
1345*0a6a1f1dSLionel Sambuc mval))
1346*0a6a1f1dSLionel Sambuc return 0;
1347ebfedea0SLionel Sambuc
1348ebfedea0SLionel Sambuc }
1349ebfedea0SLionel Sambuc
1350*0a6a1f1dSLionel Sambuc if (!X509_NAME_entry_count(subj)) {
1351ebfedea0SLionel Sambuc BIO_printf(bio_err, "error, no objects specified in config file\n");
1352ebfedea0SLionel Sambuc return 0;
1353ebfedea0SLionel Sambuc }
1354*0a6a1f1dSLionel Sambuc if (attribs) {
1355*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
1356ebfedea0SLionel Sambuc v = sk_CONF_VALUE_value(attr_sk, i);
1357ebfedea0SLionel Sambuc if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
1358*0a6a1f1dSLionel Sambuc (unsigned char *)v->value, -1))
1359*0a6a1f1dSLionel Sambuc return 0;
1360ebfedea0SLionel Sambuc }
1361ebfedea0SLionel Sambuc }
1362ebfedea0SLionel Sambuc return 1;
1363ebfedea0SLionel Sambuc }
1364ebfedea0SLionel Sambuc
1365*0a6a1f1dSLionel Sambuc static int add_DN_object(X509_NAME *n, char *text, const char *def,
1366*0a6a1f1dSLionel Sambuc char *value, int nid, int n_min, int n_max,
1367*0a6a1f1dSLionel Sambuc unsigned long chtype, int mval)
1368ebfedea0SLionel Sambuc {
1369ebfedea0SLionel Sambuc int i, ret = 0;
1370ebfedea0SLionel Sambuc MS_STATIC char buf[1024];
1371ebfedea0SLionel Sambuc start:
1372*0a6a1f1dSLionel Sambuc if (!batch)
1373*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "%s [%s]:", text, def);
1374ebfedea0SLionel Sambuc (void)BIO_flush(bio_err);
1375*0a6a1f1dSLionel Sambuc if (value != NULL) {
1376ebfedea0SLionel Sambuc BUF_strlcpy(buf, value, sizeof buf);
1377ebfedea0SLionel Sambuc BUF_strlcat(buf, "\n", sizeof buf);
1378ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s\n", value);
1379*0a6a1f1dSLionel Sambuc } else {
1380ebfedea0SLionel Sambuc buf[0] = '\0';
1381*0a6a1f1dSLionel Sambuc if (!batch) {
1382ebfedea0SLionel Sambuc if (!fgets(buf, sizeof buf, stdin))
1383ebfedea0SLionel Sambuc return 0;
1384*0a6a1f1dSLionel Sambuc } else {
1385ebfedea0SLionel Sambuc buf[0] = '\n';
1386ebfedea0SLionel Sambuc buf[1] = '\0';
1387ebfedea0SLionel Sambuc }
1388ebfedea0SLionel Sambuc }
1389ebfedea0SLionel Sambuc
1390*0a6a1f1dSLionel Sambuc if (buf[0] == '\0')
1391*0a6a1f1dSLionel Sambuc return (0);
1392*0a6a1f1dSLionel Sambuc else if (buf[0] == '\n') {
1393ebfedea0SLionel Sambuc if ((def == NULL) || (def[0] == '\0'))
1394ebfedea0SLionel Sambuc return (1);
1395ebfedea0SLionel Sambuc BUF_strlcpy(buf, def, sizeof buf);
1396ebfedea0SLionel Sambuc BUF_strlcat(buf, "\n", sizeof buf);
1397*0a6a1f1dSLionel Sambuc } else if ((buf[0] == '.') && (buf[1] == '\n'))
1398*0a6a1f1dSLionel Sambuc return (1);
1399ebfedea0SLionel Sambuc
1400ebfedea0SLionel Sambuc i = strlen(buf);
1401*0a6a1f1dSLionel Sambuc if (buf[i - 1] != '\n') {
1402ebfedea0SLionel Sambuc BIO_printf(bio_err, "weird input :-(\n");
1403ebfedea0SLionel Sambuc return (0);
1404ebfedea0SLionel Sambuc }
1405ebfedea0SLionel Sambuc buf[--i] = '\0';
1406ebfedea0SLionel Sambuc #ifdef CHARSET_EBCDIC
1407ebfedea0SLionel Sambuc ebcdic2ascii(buf, buf, i);
1408ebfedea0SLionel Sambuc #endif
1409*0a6a1f1dSLionel Sambuc if (!req_check_len(i, n_min, n_max)) {
1410*0a6a1f1dSLionel Sambuc if (batch || value)
1411*0a6a1f1dSLionel Sambuc return 0;
1412*0a6a1f1dSLionel Sambuc goto start;
1413*0a6a1f1dSLionel Sambuc }
1414*0a6a1f1dSLionel Sambuc
1415ebfedea0SLionel Sambuc if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
1416*0a6a1f1dSLionel Sambuc (unsigned char *)buf, -1, -1, mval))
1417*0a6a1f1dSLionel Sambuc goto err;
1418ebfedea0SLionel Sambuc ret = 1;
1419ebfedea0SLionel Sambuc err:
1420ebfedea0SLionel Sambuc return (ret);
1421ebfedea0SLionel Sambuc }
1422ebfedea0SLionel Sambuc
1423ebfedea0SLionel Sambuc static int add_attribute_object(X509_REQ *req, char *text, const char *def,
1424ebfedea0SLionel Sambuc char *value, int nid, int n_min,
1425ebfedea0SLionel Sambuc int n_max, unsigned long chtype)
1426ebfedea0SLionel Sambuc {
1427ebfedea0SLionel Sambuc int i;
1428ebfedea0SLionel Sambuc static char buf[1024];
1429ebfedea0SLionel Sambuc
1430ebfedea0SLionel Sambuc start:
1431*0a6a1f1dSLionel Sambuc if (!batch)
1432*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "%s [%s]:", text, def);
1433ebfedea0SLionel Sambuc (void)BIO_flush(bio_err);
1434*0a6a1f1dSLionel Sambuc if (value != NULL) {
1435ebfedea0SLionel Sambuc BUF_strlcpy(buf, value, sizeof buf);
1436ebfedea0SLionel Sambuc BUF_strlcat(buf, "\n", sizeof buf);
1437ebfedea0SLionel Sambuc BIO_printf(bio_err, "%s\n", value);
1438*0a6a1f1dSLionel Sambuc } else {
1439ebfedea0SLionel Sambuc buf[0] = '\0';
1440*0a6a1f1dSLionel Sambuc if (!batch) {
1441ebfedea0SLionel Sambuc if (!fgets(buf, sizeof buf, stdin))
1442ebfedea0SLionel Sambuc return 0;
1443*0a6a1f1dSLionel Sambuc } else {
1444ebfedea0SLionel Sambuc buf[0] = '\n';
1445ebfedea0SLionel Sambuc buf[1] = '\0';
1446ebfedea0SLionel Sambuc }
1447ebfedea0SLionel Sambuc }
1448ebfedea0SLionel Sambuc
1449*0a6a1f1dSLionel Sambuc if (buf[0] == '\0')
1450*0a6a1f1dSLionel Sambuc return (0);
1451*0a6a1f1dSLionel Sambuc else if (buf[0] == '\n') {
1452ebfedea0SLionel Sambuc if ((def == NULL) || (def[0] == '\0'))
1453ebfedea0SLionel Sambuc return (1);
1454ebfedea0SLionel Sambuc BUF_strlcpy(buf, def, sizeof buf);
1455ebfedea0SLionel Sambuc BUF_strlcat(buf, "\n", sizeof buf);
1456*0a6a1f1dSLionel Sambuc } else if ((buf[0] == '.') && (buf[1] == '\n'))
1457*0a6a1f1dSLionel Sambuc return (1);
1458ebfedea0SLionel Sambuc
1459ebfedea0SLionel Sambuc i = strlen(buf);
1460*0a6a1f1dSLionel Sambuc if (buf[i - 1] != '\n') {
1461ebfedea0SLionel Sambuc BIO_printf(bio_err, "weird input :-(\n");
1462ebfedea0SLionel Sambuc return (0);
1463ebfedea0SLionel Sambuc }
1464ebfedea0SLionel Sambuc buf[--i] = '\0';
1465ebfedea0SLionel Sambuc #ifdef CHARSET_EBCDIC
1466ebfedea0SLionel Sambuc ebcdic2ascii(buf, buf, i);
1467ebfedea0SLionel Sambuc #endif
1468*0a6a1f1dSLionel Sambuc if (!req_check_len(i, n_min, n_max)) {
1469*0a6a1f1dSLionel Sambuc if (batch || value)
1470*0a6a1f1dSLionel Sambuc return 0;
1471*0a6a1f1dSLionel Sambuc goto start;
1472*0a6a1f1dSLionel Sambuc }
1473ebfedea0SLionel Sambuc
1474ebfedea0SLionel Sambuc if (!X509_REQ_add1_attr_by_NID(req, nid, chtype,
1475ebfedea0SLionel Sambuc (unsigned char *)buf, -1)) {
1476ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error adding attribute\n");
1477ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
1478ebfedea0SLionel Sambuc goto err;
1479ebfedea0SLionel Sambuc }
1480ebfedea0SLionel Sambuc
1481ebfedea0SLionel Sambuc return (1);
1482ebfedea0SLionel Sambuc err:
1483ebfedea0SLionel Sambuc return (0);
1484ebfedea0SLionel Sambuc }
1485ebfedea0SLionel Sambuc
1486ebfedea0SLionel Sambuc static int req_check_len(int len, int n_min, int n_max)
1487ebfedea0SLionel Sambuc {
1488*0a6a1f1dSLionel Sambuc if ((n_min > 0) && (len < n_min)) {
1489*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1490*0a6a1f1dSLionel Sambuc "string is too short, it needs to be at least %d bytes long\n",
1491*0a6a1f1dSLionel Sambuc n_min);
1492ebfedea0SLionel Sambuc return (0);
1493ebfedea0SLionel Sambuc }
1494*0a6a1f1dSLionel Sambuc if ((n_max >= 0) && (len > n_max)) {
1495*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
1496*0a6a1f1dSLionel Sambuc "string is too long, it needs to be less than %d bytes long\n",
1497*0a6a1f1dSLionel Sambuc n_max);
1498ebfedea0SLionel Sambuc return (0);
1499ebfedea0SLionel Sambuc }
1500ebfedea0SLionel Sambuc return (1);
1501ebfedea0SLionel Sambuc }
1502ebfedea0SLionel Sambuc
1503ebfedea0SLionel Sambuc /* Check if the end of a string matches 'end' */
1504ebfedea0SLionel Sambuc static int check_end(const char *str, const char *end)
1505ebfedea0SLionel Sambuc {
1506ebfedea0SLionel Sambuc int elen, slen;
1507ebfedea0SLionel Sambuc const char *tmp;
1508ebfedea0SLionel Sambuc elen = strlen(end);
1509ebfedea0SLionel Sambuc slen = strlen(str);
1510*0a6a1f1dSLionel Sambuc if (elen > slen)
1511*0a6a1f1dSLionel Sambuc return 1;
1512ebfedea0SLionel Sambuc tmp = str + slen - elen;
1513ebfedea0SLionel Sambuc return strcmp(tmp, end);
1514ebfedea0SLionel Sambuc }
1515ebfedea0SLionel Sambuc
1516*0a6a1f1dSLionel Sambuc static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr,
1517*0a6a1f1dSLionel Sambuc int *pkey_type, long *pkeylen,
1518*0a6a1f1dSLionel Sambuc char **palgnam, ENGINE *keygen_engine)
1519ebfedea0SLionel Sambuc {
1520ebfedea0SLionel Sambuc EVP_PKEY_CTX *gctx = NULL;
1521ebfedea0SLionel Sambuc EVP_PKEY *param = NULL;
1522ebfedea0SLionel Sambuc long keylen = -1;
1523ebfedea0SLionel Sambuc BIO *pbio = NULL;
1524ebfedea0SLionel Sambuc const char *paramfile = NULL;
1525ebfedea0SLionel Sambuc
1526*0a6a1f1dSLionel Sambuc if (gstr == NULL) {
1527ebfedea0SLionel Sambuc *pkey_type = EVP_PKEY_RSA;
1528ebfedea0SLionel Sambuc keylen = *pkeylen;
1529*0a6a1f1dSLionel Sambuc } else if (gstr[0] >= '0' && gstr[0] <= '9') {
1530ebfedea0SLionel Sambuc *pkey_type = EVP_PKEY_RSA;
1531ebfedea0SLionel Sambuc keylen = atol(gstr);
1532ebfedea0SLionel Sambuc *pkeylen = keylen;
1533*0a6a1f1dSLionel Sambuc } else if (!strncmp(gstr, "param:", 6))
1534ebfedea0SLionel Sambuc paramfile = gstr + 6;
1535*0a6a1f1dSLionel Sambuc else {
1536ebfedea0SLionel Sambuc const char *p = strchr(gstr, ':');
1537ebfedea0SLionel Sambuc int len;
1538ebfedea0SLionel Sambuc ENGINE *tmpeng;
1539ebfedea0SLionel Sambuc const EVP_PKEY_ASN1_METHOD *ameth;
1540ebfedea0SLionel Sambuc
1541ebfedea0SLionel Sambuc if (p)
1542ebfedea0SLionel Sambuc len = p - gstr;
1543ebfedea0SLionel Sambuc else
1544ebfedea0SLionel Sambuc len = strlen(gstr);
1545*0a6a1f1dSLionel Sambuc /*
1546*0a6a1f1dSLionel Sambuc * The lookup of a the string will cover all engines so keep a note
1547*0a6a1f1dSLionel Sambuc * of the implementation.
1548ebfedea0SLionel Sambuc */
1549ebfedea0SLionel Sambuc
1550ebfedea0SLionel Sambuc ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len);
1551ebfedea0SLionel Sambuc
1552*0a6a1f1dSLionel Sambuc if (!ameth) {
1553ebfedea0SLionel Sambuc BIO_printf(err, "Unknown algorithm %.*s\n", len, gstr);
1554ebfedea0SLionel Sambuc return NULL;
1555ebfedea0SLionel Sambuc }
1556ebfedea0SLionel Sambuc
1557*0a6a1f1dSLionel Sambuc EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, ameth);
1558ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
1559ebfedea0SLionel Sambuc if (tmpeng)
1560ebfedea0SLionel Sambuc ENGINE_finish(tmpeng);
1561ebfedea0SLionel Sambuc #endif
1562*0a6a1f1dSLionel Sambuc if (*pkey_type == EVP_PKEY_RSA) {
1563*0a6a1f1dSLionel Sambuc if (p) {
1564ebfedea0SLionel Sambuc keylen = atol(p + 1);
1565ebfedea0SLionel Sambuc *pkeylen = keylen;
1566*0a6a1f1dSLionel Sambuc } else
1567*0a6a1f1dSLionel Sambuc keylen = *pkeylen;
1568*0a6a1f1dSLionel Sambuc } else if (p)
1569ebfedea0SLionel Sambuc paramfile = p + 1;
1570ebfedea0SLionel Sambuc }
1571ebfedea0SLionel Sambuc
1572*0a6a1f1dSLionel Sambuc if (paramfile) {
1573ebfedea0SLionel Sambuc pbio = BIO_new_file(paramfile, "r");
1574*0a6a1f1dSLionel Sambuc if (!pbio) {
1575*0a6a1f1dSLionel Sambuc BIO_printf(err, "Can't open parameter file %s\n", paramfile);
1576ebfedea0SLionel Sambuc return NULL;
1577ebfedea0SLionel Sambuc }
1578ebfedea0SLionel Sambuc param = PEM_read_bio_Parameters(pbio, NULL);
1579ebfedea0SLionel Sambuc
1580*0a6a1f1dSLionel Sambuc if (!param) {
1581ebfedea0SLionel Sambuc X509 *x;
1582ebfedea0SLionel Sambuc (void)BIO_reset(pbio);
1583ebfedea0SLionel Sambuc x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
1584*0a6a1f1dSLionel Sambuc if (x) {
1585ebfedea0SLionel Sambuc param = X509_get_pubkey(x);
1586ebfedea0SLionel Sambuc X509_free(x);
1587ebfedea0SLionel Sambuc }
1588ebfedea0SLionel Sambuc }
1589ebfedea0SLionel Sambuc
1590ebfedea0SLionel Sambuc BIO_free(pbio);
1591ebfedea0SLionel Sambuc
1592*0a6a1f1dSLionel Sambuc if (!param) {
1593*0a6a1f1dSLionel Sambuc BIO_printf(err, "Error reading parameter file %s\n", paramfile);
1594ebfedea0SLionel Sambuc return NULL;
1595ebfedea0SLionel Sambuc }
1596ebfedea0SLionel Sambuc if (*pkey_type == -1)
1597ebfedea0SLionel Sambuc *pkey_type = EVP_PKEY_id(param);
1598*0a6a1f1dSLionel Sambuc else if (*pkey_type != EVP_PKEY_base_id(param)) {
1599ebfedea0SLionel Sambuc BIO_printf(err, "Key Type does not match parameters\n");
1600ebfedea0SLionel Sambuc EVP_PKEY_free(param);
1601ebfedea0SLionel Sambuc return NULL;
1602ebfedea0SLionel Sambuc }
1603ebfedea0SLionel Sambuc }
1604ebfedea0SLionel Sambuc
1605*0a6a1f1dSLionel Sambuc if (palgnam) {
1606ebfedea0SLionel Sambuc const EVP_PKEY_ASN1_METHOD *ameth;
1607ebfedea0SLionel Sambuc ENGINE *tmpeng;
1608ebfedea0SLionel Sambuc const char *anam;
1609ebfedea0SLionel Sambuc ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type);
1610*0a6a1f1dSLionel Sambuc if (!ameth) {
1611ebfedea0SLionel Sambuc BIO_puts(err, "Internal error: can't find key algorithm\n");
1612ebfedea0SLionel Sambuc return NULL;
1613ebfedea0SLionel Sambuc }
1614ebfedea0SLionel Sambuc EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth);
1615ebfedea0SLionel Sambuc *palgnam = BUF_strdup(anam);
1616ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
1617ebfedea0SLionel Sambuc if (tmpeng)
1618ebfedea0SLionel Sambuc ENGINE_finish(tmpeng);
1619ebfedea0SLionel Sambuc #endif
1620ebfedea0SLionel Sambuc }
1621ebfedea0SLionel Sambuc
1622*0a6a1f1dSLionel Sambuc if (param) {
1623ebfedea0SLionel Sambuc gctx = EVP_PKEY_CTX_new(param, keygen_engine);
1624ebfedea0SLionel Sambuc *pkeylen = EVP_PKEY_bits(param);
1625ebfedea0SLionel Sambuc EVP_PKEY_free(param);
1626*0a6a1f1dSLionel Sambuc } else
1627ebfedea0SLionel Sambuc gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine);
1628ebfedea0SLionel Sambuc
1629*0a6a1f1dSLionel Sambuc if (!gctx) {
1630ebfedea0SLionel Sambuc BIO_puts(err, "Error allocating keygen context\n");
1631ebfedea0SLionel Sambuc ERR_print_errors(err);
1632ebfedea0SLionel Sambuc return NULL;
1633ebfedea0SLionel Sambuc }
1634ebfedea0SLionel Sambuc
1635*0a6a1f1dSLionel Sambuc if (EVP_PKEY_keygen_init(gctx) <= 0) {
1636ebfedea0SLionel Sambuc BIO_puts(err, "Error initializing keygen context\n");
1637ebfedea0SLionel Sambuc ERR_print_errors(err);
1638ebfedea0SLionel Sambuc return NULL;
1639ebfedea0SLionel Sambuc }
1640ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1641*0a6a1f1dSLionel Sambuc if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) {
1642*0a6a1f1dSLionel Sambuc if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) {
1643ebfedea0SLionel Sambuc BIO_puts(err, "Error setting RSA keysize\n");
1644ebfedea0SLionel Sambuc ERR_print_errors(err);
1645ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(gctx);
1646ebfedea0SLionel Sambuc return NULL;
1647ebfedea0SLionel Sambuc }
1648ebfedea0SLionel Sambuc }
1649ebfedea0SLionel Sambuc #endif
1650ebfedea0SLionel Sambuc
1651ebfedea0SLionel Sambuc return gctx;
1652ebfedea0SLionel Sambuc }
1653ebfedea0SLionel Sambuc
1654ebfedea0SLionel Sambuc static int genpkey_cb(EVP_PKEY_CTX *ctx)
1655ebfedea0SLionel Sambuc {
1656ebfedea0SLionel Sambuc char c = '*';
1657ebfedea0SLionel Sambuc BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
1658ebfedea0SLionel Sambuc int p;
1659ebfedea0SLionel Sambuc p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
1660*0a6a1f1dSLionel Sambuc if (p == 0)
1661*0a6a1f1dSLionel Sambuc c = '.';
1662*0a6a1f1dSLionel Sambuc if (p == 1)
1663*0a6a1f1dSLionel Sambuc c = '+';
1664*0a6a1f1dSLionel Sambuc if (p == 2)
1665*0a6a1f1dSLionel Sambuc c = '*';
1666*0a6a1f1dSLionel Sambuc if (p == 3)
1667*0a6a1f1dSLionel Sambuc c = '\n';
1668ebfedea0SLionel Sambuc BIO_write(b, &c, 1);
1669ebfedea0SLionel Sambuc (void)BIO_flush(b);
1670ebfedea0SLionel Sambuc #ifdef LINT
1671ebfedea0SLionel Sambuc p = n;
1672ebfedea0SLionel Sambuc #endif
1673ebfedea0SLionel Sambuc return 1;
1674ebfedea0SLionel Sambuc }
1675ebfedea0SLionel Sambuc
1676ebfedea0SLionel Sambuc static int do_sign_init(BIO *err, EVP_MD_CTX *ctx, EVP_PKEY *pkey,
1677ebfedea0SLionel Sambuc const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts)
1678ebfedea0SLionel Sambuc {
1679ebfedea0SLionel Sambuc EVP_PKEY_CTX *pkctx = NULL;
1680ebfedea0SLionel Sambuc int i;
1681ebfedea0SLionel Sambuc EVP_MD_CTX_init(ctx);
1682ebfedea0SLionel Sambuc if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey))
1683ebfedea0SLionel Sambuc return 0;
1684*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
1685ebfedea0SLionel Sambuc char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
1686*0a6a1f1dSLionel Sambuc if (pkey_ctrl_string(pkctx, sigopt) <= 0) {
1687ebfedea0SLionel Sambuc BIO_printf(err, "parameter error \"%s\"\n", sigopt);
1688ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
1689ebfedea0SLionel Sambuc return 0;
1690ebfedea0SLionel Sambuc }
1691ebfedea0SLionel Sambuc }
1692ebfedea0SLionel Sambuc return 1;
1693ebfedea0SLionel Sambuc }
1694ebfedea0SLionel Sambuc
1695ebfedea0SLionel Sambuc int do_X509_sign(BIO *err, X509 *x, EVP_PKEY *pkey, const EVP_MD *md,
1696ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts)
1697ebfedea0SLionel Sambuc {
1698ebfedea0SLionel Sambuc int rv;
1699ebfedea0SLionel Sambuc EVP_MD_CTX mctx;
1700ebfedea0SLionel Sambuc EVP_MD_CTX_init(&mctx);
1701ebfedea0SLionel Sambuc rv = do_sign_init(err, &mctx, pkey, md, sigopts);
1702ebfedea0SLionel Sambuc if (rv > 0)
1703ebfedea0SLionel Sambuc rv = X509_sign_ctx(x, &mctx);
1704ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx);
1705ebfedea0SLionel Sambuc return rv > 0 ? 1 : 0;
1706ebfedea0SLionel Sambuc }
1707ebfedea0SLionel Sambuc
1708ebfedea0SLionel Sambuc int do_X509_REQ_sign(BIO *err, X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md,
1709ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts)
1710ebfedea0SLionel Sambuc {
1711ebfedea0SLionel Sambuc int rv;
1712ebfedea0SLionel Sambuc EVP_MD_CTX mctx;
1713ebfedea0SLionel Sambuc EVP_MD_CTX_init(&mctx);
1714ebfedea0SLionel Sambuc rv = do_sign_init(err, &mctx, pkey, md, sigopts);
1715ebfedea0SLionel Sambuc if (rv > 0)
1716ebfedea0SLionel Sambuc rv = X509_REQ_sign_ctx(x, &mctx);
1717ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx);
1718ebfedea0SLionel Sambuc return rv > 0 ? 1 : 0;
1719ebfedea0SLionel Sambuc }
1720ebfedea0SLionel Sambuc
1721ebfedea0SLionel Sambuc int do_X509_CRL_sign(BIO *err, X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md,
1722ebfedea0SLionel Sambuc STACK_OF(OPENSSL_STRING) *sigopts)
1723ebfedea0SLionel Sambuc {
1724ebfedea0SLionel Sambuc int rv;
1725ebfedea0SLionel Sambuc EVP_MD_CTX mctx;
1726ebfedea0SLionel Sambuc EVP_MD_CTX_init(&mctx);
1727ebfedea0SLionel Sambuc rv = do_sign_init(err, &mctx, pkey, md, sigopts);
1728ebfedea0SLionel Sambuc if (rv > 0)
1729ebfedea0SLionel Sambuc rv = X509_CRL_sign_ctx(x, &mctx);
1730ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx);
1731ebfedea0SLionel Sambuc return rv > 0 ? 1 : 0;
1732ebfedea0SLionel Sambuc }
1733