req.c revision 1.1.1.2 1 1.1 christos /*
2 1.1 christos * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3 1.1 christos *
4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use
5 1.1 christos * this file except in compliance with the License. You can obtain a copy
6 1.1 christos * in the file LICENSE in the source distribution or at
7 1.1 christos * https://www.openssl.org/source/license.html
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include <stdio.h>
11 1.1 christos #include <stdlib.h>
12 1.1 christos #include <time.h>
13 1.1 christos #include <string.h>
14 1.1 christos #include <ctype.h>
15 1.1 christos #include "apps.h"
16 1.1 christos #include "progs.h"
17 1.1 christos #include <openssl/core_names.h>
18 1.1 christos #include <openssl/bio.h>
19 1.1 christos #include <openssl/evp.h>
20 1.1 christos #include <openssl/conf.h>
21 1.1 christos #include <openssl/err.h>
22 1.1 christos #include <openssl/asn1.h>
23 1.1 christos #include <openssl/x509.h>
24 1.1 christos #include <openssl/x509v3.h>
25 1.1 christos #include <openssl/objects.h>
26 1.1 christos #include <openssl/pem.h>
27 1.1 christos #include <openssl/bn.h>
28 1.1 christos #include <openssl/lhash.h>
29 1.1 christos #include <openssl/rsa.h>
30 1.1 christos #ifndef OPENSSL_NO_DSA
31 1.1.1.2 christos #include <openssl/dsa.h>
32 1.1 christos #endif
33 1.1.1.2 christos #include "internal/e_os.h" /* For isatty() */
34 1.1 christos
35 1.1.1.2 christos #define BITS "default_bits"
36 1.1.1.2 christos #define KEYFILE "default_keyfile"
37 1.1.1.2 christos #define PROMPT "prompt"
38 1.1 christos #define DISTINGUISHED_NAME "distinguished_name"
39 1.1.1.2 christos #define ATTRIBUTES "attributes"
40 1.1.1.2 christos #define V3_EXTENSIONS "x509_extensions"
41 1.1.1.2 christos #define REQ_EXTENSIONS "req_extensions"
42 1.1.1.2 christos #define STRING_MASK "string_mask"
43 1.1.1.2 christos #define UTF8_IN "utf8"
44 1.1 christos
45 1.1 christos #define DEFAULT_KEY_LENGTH 2048
46 1.1.1.2 christos #define MIN_KEY_LENGTH 512
47 1.1.1.2 christos #define DEFAULT_DAYS 30 /* default certificate validity period in days */
48 1.1.1.2 christos #define UNSET_DAYS -2 /* -1 may be used for testing expiration checks */
49 1.1.1.2 christos #define EXT_COPY_UNSET -1
50 1.1 christos
51 1.1 christos static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, X509_NAME *fsubj,
52 1.1.1.2 christos int mutlirdn, int attribs, unsigned long chtype);
53 1.1 christos static int prompt_info(X509_REQ *req,
54 1.1.1.2 christos STACK_OF(CONF_VALUE) *dn_sk, const char *dn_sect,
55 1.1.1.2 christos STACK_OF(CONF_VALUE) *attr_sk, const char *attr_sect,
56 1.1.1.2 christos int attribs, unsigned long chtype);
57 1.1 christos static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
58 1.1.1.2 christos STACK_OF(CONF_VALUE) *attr, int attribs,
59 1.1.1.2 christos unsigned long chtype);
60 1.1 christos static int add_attribute_object(X509_REQ *req, char *text, const char *def,
61 1.1.1.2 christos char *value, int nid, int n_min, int n_max,
62 1.1.1.2 christos unsigned long chtype);
63 1.1 christos static int add_DN_object(X509_NAME *n, char *text, const char *def,
64 1.1.1.2 christos char *value, int nid, int n_min, int n_max,
65 1.1.1.2 christos unsigned long chtype, int mval);
66 1.1 christos static int build_data(char *text, const char *def, char *value,
67 1.1.1.2 christos int n_min, int n_max, char *buf, const int buf_size,
68 1.1.1.2 christos const char *desc1, const char *desc2);
69 1.1 christos static int req_check_len(int len, int n_min, int n_max);
70 1.1 christos static int check_end(const char *str, const char *end);
71 1.1 christos static int join(char buf[], size_t buf_size, const char *name,
72 1.1.1.2 christos const char *tail, const char *desc);
73 1.1 christos static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
74 1.1.1.2 christos char **pkeytype, long *pkeylen,
75 1.1.1.2 christos ENGINE *keygen_engine);
76 1.1 christos
77 1.1 christos static const char *section = "req";
78 1.1 christos static CONF *req_conf = NULL;
79 1.1 christos static CONF *addext_conf = NULL;
80 1.1 christos static int batch = 0;
81 1.1 christos
82 1.1 christos typedef enum OPTION_choice {
83 1.1 christos OPT_COMMON,
84 1.1 christos OPT_CIPHER,
85 1.1.1.2 christos OPT_INFORM,
86 1.1.1.2 christos OPT_OUTFORM,
87 1.1.1.2 christos OPT_ENGINE,
88 1.1.1.2 christos OPT_KEYGEN_ENGINE,
89 1.1.1.2 christos OPT_KEY,
90 1.1.1.2 christos OPT_PUBKEY,
91 1.1.1.2 christos OPT_NEW,
92 1.1.1.2 christos OPT_CONFIG,
93 1.1.1.2 christos OPT_KEYFORM,
94 1.1.1.2 christos OPT_IN,
95 1.1.1.2 christos OPT_OUT,
96 1.1.1.2 christos OPT_KEYOUT,
97 1.1.1.2 christos OPT_PASSIN,
98 1.1.1.2 christos OPT_PASSOUT,
99 1.1.1.2 christos OPT_NEWKEY,
100 1.1.1.2 christos OPT_PKEYOPT,
101 1.1.1.2 christos OPT_SIGOPT,
102 1.1.1.2 christos OPT_VFYOPT,
103 1.1.1.2 christos OPT_BATCH,
104 1.1.1.2 christos OPT_NEWHDR,
105 1.1.1.2 christos OPT_MODULUS,
106 1.1.1.2 christos OPT_VERIFY,
107 1.1.1.2 christos OPT_NOENC,
108 1.1.1.2 christos OPT_NODES,
109 1.1.1.2 christos OPT_NOOUT,
110 1.1.1.2 christos OPT_VERBOSE,
111 1.1.1.2 christos OPT_UTF8,
112 1.1.1.2 christos OPT_NAMEOPT,
113 1.1.1.2 christos OPT_REQOPT,
114 1.1.1.2 christos OPT_SUBJ,
115 1.1.1.2 christos OPT_SUBJECT,
116 1.1.1.2 christos OPT_TEXT,
117 1.1.1.2 christos OPT_X509,
118 1.1.1.2 christos OPT_X509V1,
119 1.1.1.2 christos OPT_CA,
120 1.1.1.2 christos OPT_CAKEY,
121 1.1.1.2 christos OPT_MULTIVALUE_RDN,
122 1.1.1.2 christos OPT_NOT_BEFORE,
123 1.1.1.2 christos OPT_NOT_AFTER,
124 1.1.1.2 christos OPT_DAYS,
125 1.1.1.2 christos OPT_SET_SERIAL,
126 1.1.1.2 christos OPT_COPY_EXTENSIONS,
127 1.1.1.2 christos OPT_EXTENSIONS,
128 1.1.1.2 christos OPT_REQEXTS,
129 1.1.1.2 christos OPT_ADDEXT,
130 1.1.1.2 christos OPT_PRECERT,
131 1.1.1.2 christos OPT_MD,
132 1.1.1.2 christos OPT_SECTION,
133 1.1.1.2 christos OPT_QUIET,
134 1.1.1.2 christos OPT_R_ENUM,
135 1.1.1.2 christos OPT_PROV_ENUM
136 1.1 christos } OPTION_CHOICE;
137 1.1 christos
138 1.1 christos const OPTIONS req_options[] = {
139 1.1 christos OPT_SECTION("General"),
140 1.1.1.2 christos { "help", OPT_HELP, '-', "Display this summary" },
141 1.1.1.2 christos { "cipher", OPT_CIPHER, 's', "Specify the cipher for private key encryption" },
142 1.1 christos #ifndef OPENSSL_NO_ENGINE
143 1.1.1.2 christos { "engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device" },
144 1.1.1.2 christos { "keygen_engine", OPT_KEYGEN_ENGINE, 's',
145 1.1.1.2 christos "Specify engine to be used for key generation operations" },
146 1.1 christos #endif
147 1.1.1.2 christos { "in", OPT_IN, '<', "X.509 request input file (default stdin)" },
148 1.1.1.2 christos { "inform", OPT_INFORM, 'F',
149 1.1.1.2 christos "CSR input format to use (PEM or DER; by default try PEM first)" },
150 1.1.1.2 christos { "verify", OPT_VERIFY, '-', "Verify self-signature on the request" },
151 1.1 christos
152 1.1 christos OPT_SECTION("Certificate"),
153 1.1.1.2 christos { "new", OPT_NEW, '-', "New request" },
154 1.1.1.2 christos { "config", OPT_CONFIG, '<', "Request template file" },
155 1.1.1.2 christos { "section", OPT_SECTION, 's', "Config section to use (default \"req\")" },
156 1.1.1.2 christos { "utf8", OPT_UTF8, '-', "Input characters are UTF8 (default ASCII)" },
157 1.1.1.2 christos { "nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options" },
158 1.1.1.2 christos { "reqopt", OPT_REQOPT, 's', "Various request text options" },
159 1.1.1.2 christos { "text", OPT_TEXT, '-', "Text form of request" },
160 1.1.1.2 christos { "x509", OPT_X509, '-',
161 1.1.1.2 christos "Output an X.509 certificate structure instead of a cert request" },
162 1.1.1.2 christos { "x509v1", OPT_X509V1, '-', "Request cert generation with X.509 version 1" },
163 1.1.1.2 christos { "CA", OPT_CA, '<', "Issuer cert to use for signing a cert, implies -x509" },
164 1.1.1.2 christos { "CAkey", OPT_CAKEY, 's',
165 1.1.1.2 christos "Issuer private key to use with -CA; default is -CA arg" },
166 1.1.1.2 christos { OPT_MORE_STR, 1, 1, "(Required by some CA's)" },
167 1.1.1.2 christos { "subj", OPT_SUBJ, 's', "Set or modify subject of request or cert" },
168 1.1.1.2 christos { "subject", OPT_SUBJECT, '-',
169 1.1.1.2 christos "Print the subject of the output request or cert" },
170 1.1.1.2 christos { "multivalue-rdn", OPT_MULTIVALUE_RDN, '-',
171 1.1.1.2 christos "Deprecated; multi-valued RDNs support is always on." },
172 1.1.1.2 christos { "not_before", OPT_NOT_BEFORE, 's',
173 1.1.1.2 christos "[CC]YYMMDDHHMMSSZ value for notBefore certificate field" },
174 1.1.1.2 christos { "not_after", OPT_NOT_AFTER, 's',
175 1.1.1.2 christos "[CC]YYMMDDHHMMSSZ value for notAfter certificate field, overrides -days" },
176 1.1.1.2 christos { "days", OPT_DAYS, 'p', "Number of days certificate is valid for" },
177 1.1.1.2 christos { "set_serial", OPT_SET_SERIAL, 's', "Serial number to use" },
178 1.1.1.2 christos { "copy_extensions", OPT_COPY_EXTENSIONS, 's',
179 1.1.1.2 christos "copy extensions from request when using -x509" },
180 1.1.1.2 christos { "extensions", OPT_EXTENSIONS, 's',
181 1.1.1.2 christos "Cert or request extension section (override value in config file)" },
182 1.1.1.2 christos { "reqexts", OPT_REQEXTS, 's', "An alias for -extensions" },
183 1.1.1.2 christos { "addext", OPT_ADDEXT, 's',
184 1.1.1.2 christos "Additional cert extension key=value pair (may be given more than once)" },
185 1.1.1.2 christos { "precert", OPT_PRECERT, '-', "Add a poison extension to generated cert (implies -new)" },
186 1.1 christos
187 1.1 christos OPT_SECTION("Keys and Signing"),
188 1.1.1.2 christos { "key", OPT_KEY, 's', "Key for signing, and to include unless -in given" },
189 1.1.1.2 christos { "keyform", OPT_KEYFORM, 'f', "Key file format (ENGINE, other values ignored)" },
190 1.1.1.2 christos { "pubkey", OPT_PUBKEY, '-', "Output public key" },
191 1.1.1.2 christos { "keyout", OPT_KEYOUT, '>', "File to write private key to" },
192 1.1.1.2 christos { "passin", OPT_PASSIN, 's', "Private key and certificate password source" },
193 1.1.1.2 christos { "passout", OPT_PASSOUT, 's', "Output file pass phrase source" },
194 1.1.1.2 christos { "newkey", OPT_NEWKEY, 's',
195 1.1.1.2 christos "Generate new key with [<alg>:]<nbits> or <alg>[:<file>] or param:<file>" },
196 1.1.1.2 christos { "pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value" },
197 1.1.1.2 christos { "sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form" },
198 1.1.1.2 christos { "vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form" },
199 1.1.1.2 christos { "", OPT_MD, '-', "Any supported digest" },
200 1.1 christos
201 1.1 christos OPT_SECTION("Output"),
202 1.1.1.2 christos { "out", OPT_OUT, '>', "Output file" },
203 1.1.1.2 christos { "outform", OPT_OUTFORM, 'F', "Output format - DER or PEM" },
204 1.1.1.2 christos { "batch", OPT_BATCH, '-',
205 1.1.1.2 christos "Do not ask anything during request generation" },
206 1.1.1.2 christos { "verbose", OPT_VERBOSE, '-', "Verbose output" },
207 1.1.1.2 christos { "quiet", OPT_QUIET, '-', "Terse output" },
208 1.1.1.2 christos { "noenc", OPT_NOENC, '-', "Don't encrypt private keys" },
209 1.1.1.2 christos { "nodes", OPT_NODES, '-', "Don't encrypt private keys; deprecated" },
210 1.1.1.2 christos { "noout", OPT_NOOUT, '-', "Do not output REQ" },
211 1.1.1.2 christos { "newhdr", OPT_NEWHDR, '-', "Output \"NEW\" in the header lines" },
212 1.1.1.2 christos { "modulus", OPT_MODULUS, '-', "RSA modulus" },
213 1.1 christos
214 1.1 christos OPT_R_OPTIONS,
215 1.1 christos OPT_PROV_OPTIONS,
216 1.1.1.2 christos { NULL }
217 1.1 christos };
218 1.1 christos
219 1.1 christos /*
220 1.1 christos * An LHASH of strings, where each string is an extension name.
221 1.1 christos */
222 1.1 christos static unsigned long ext_name_hash(const OPENSSL_STRING *a)
223 1.1 christos {
224 1.1 christos return OPENSSL_LH_strhash((const char *)a);
225 1.1 christos }
226 1.1 christos
227 1.1 christos static int ext_name_cmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b)
228 1.1 christos {
229 1.1 christos return strcmp((const char *)a, (const char *)b);
230 1.1 christos }
231 1.1 christos
232 1.1 christos static void exts_cleanup(OPENSSL_STRING *x)
233 1.1 christos {
234 1.1 christos OPENSSL_free((char *)x);
235 1.1 christos }
236 1.1 christos
237 1.1 christos /*
238 1.1 christos * Is the |kv| key already duplicated?
239 1.1 christos * Return 0 if unique, -1 on runtime error, -2 on syntax error; 1 if found.
240 1.1 christos */
241 1.1 christos static int duplicated(LHASH_OF(OPENSSL_STRING) *addexts, char *kv)
242 1.1 christos {
243 1.1 christos char *p;
244 1.1 christos size_t off;
245 1.1 christos
246 1.1 christos /* Check syntax. */
247 1.1 christos /* Skip leading whitespace, make a copy. */
248 1.1 christos while (isspace(_UC(*kv)))
249 1.1 christos kv++;
250 1.1 christos if ((p = strchr(kv, '=')) == NULL) {
251 1.1 christos BIO_printf(bio_err, "Parse error on -addext: missing '='\n");
252 1.1 christos return -2;
253 1.1 christos }
254 1.1 christos off = p - kv;
255 1.1 christos if ((kv = OPENSSL_strdup(kv)) == NULL)
256 1.1 christos return -1;
257 1.1 christos
258 1.1 christos /* Skip trailing space before the equal sign. */
259 1.1 christos for (p = kv + off; p > kv; --p)
260 1.1 christos if (!isspace(_UC(p[-1])))
261 1.1 christos break;
262 1.1 christos if (p == kv) {
263 1.1 christos BIO_printf(bio_err, "Parse error on -addext: missing key\n");
264 1.1 christos OPENSSL_free(kv);
265 1.1 christos return -2;
266 1.1 christos }
267 1.1 christos *p = '\0';
268 1.1 christos
269 1.1 christos /* Finally have a clean "key"; see if it's there [by attempt to add it]. */
270 1.1 christos p = (char *)lh_OPENSSL_STRING_insert(addexts, (OPENSSL_STRING *)kv);
271 1.1 christos if (p != NULL) {
272 1.1 christos BIO_printf(bio_err, "Duplicate extension name: %s\n", kv);
273 1.1 christos OPENSSL_free(p);
274 1.1 christos return 1;
275 1.1 christos } else if (lh_OPENSSL_STRING_error(addexts)) {
276 1.1 christos OPENSSL_free(kv);
277 1.1 christos return -1;
278 1.1 christos }
279 1.1 christos
280 1.1 christos return 0;
281 1.1 christos }
282 1.1 christos
283 1.1 christos int req_main(int argc, char **argv)
284 1.1 christos {
285 1.1 christos ASN1_INTEGER *serial = NULL;
286 1.1 christos BIO *out = NULL;
287 1.1 christos ENGINE *e = NULL, *gen_eng = NULL;
288 1.1 christos EVP_PKEY *pkey = NULL, *CAkey = NULL;
289 1.1 christos EVP_PKEY_CTX *genctx = NULL;
290 1.1 christos STACK_OF(OPENSSL_STRING) *pkeyopts = NULL, *sigopts = NULL, *vfyopts = NULL;
291 1.1 christos LHASH_OF(OPENSSL_STRING) *addexts = NULL;
292 1.1 christos X509 *new_x509 = NULL, *CAcert = NULL;
293 1.1 christos X509_REQ *req = NULL;
294 1.1 christos const EVP_CIPHER *cipher = NULL;
295 1.1 christos int ext_copy = EXT_COPY_UNSET;
296 1.1 christos BIO *addext_bio = NULL;
297 1.1 christos char *extsect = NULL;
298 1.1 christos const char *infile = NULL, *CAfile = NULL, *CAkeyfile = NULL;
299 1.1 christos char *outfile = NULL, *keyfile = NULL, *digest = NULL;
300 1.1 christos char *keyalgstr = NULL, *p, *prog, *passargin = NULL, *passargout = NULL;
301 1.1 christos char *passin = NULL, *passout = NULL;
302 1.1 christos char *nofree_passin = NULL, *nofree_passout = NULL;
303 1.1 christos char *subj = NULL;
304 1.1 christos X509_NAME *fsubj = NULL;
305 1.1 christos char *template = default_config_file, *keyout = NULL;
306 1.1 christos const char *keyalg = NULL;
307 1.1 christos OPTION_CHOICE o;
308 1.1 christos char *not_before = NULL, *not_after = NULL;
309 1.1 christos int days = UNSET_DAYS;
310 1.1 christos int ret = 1, gen_x509 = 0, i = 0, newreq = 0, verbose = 0, progress = 1;
311 1.1 christos int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, keyform = FORMAT_UNDEF;
312 1.1 christos int modulus = 0, multirdn = 1, verify = 0, noout = 0, text = 0;
313 1.1 christos int noenc = 0, newhdr = 0, subject = 0, pubkey = 0, precert = 0, x509v1 = 0;
314 1.1 christos long newkey_len = -1;
315 1.1 christos unsigned long chtype = MBSTRING_ASC, reqflag = 0;
316 1.1 christos
317 1.1 christos cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
318 1.1 christos
319 1.1 christos opt_set_unknown_name("digest");
320 1.1 christos prog = opt_init(argc, argv, req_options);
321 1.1 christos while ((o = opt_next()) != OPT_EOF) {
322 1.1 christos switch (o) {
323 1.1 christos case OPT_EOF:
324 1.1 christos case OPT_ERR:
325 1.1.1.2 christos opthelp:
326 1.1 christos BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
327 1.1 christos goto end;
328 1.1 christos case OPT_HELP:
329 1.1 christos opt_help(req_options);
330 1.1 christos ret = 0;
331 1.1 christos goto end;
332 1.1 christos case OPT_INFORM:
333 1.1 christos if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
334 1.1 christos goto opthelp;
335 1.1 christos break;
336 1.1 christos case OPT_OUTFORM:
337 1.1 christos if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
338 1.1 christos goto opthelp;
339 1.1 christos break;
340 1.1 christos case OPT_ENGINE:
341 1.1 christos e = setup_engine(opt_arg(), 0);
342 1.1 christos break;
343 1.1 christos case OPT_KEYGEN_ENGINE:
344 1.1 christos #ifndef OPENSSL_NO_ENGINE
345 1.1 christos gen_eng = setup_engine(opt_arg(), 0);
346 1.1 christos if (gen_eng == NULL) {
347 1.1 christos BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv);
348 1.1 christos goto opthelp;
349 1.1 christos }
350 1.1 christos #endif
351 1.1 christos break;
352 1.1 christos case OPT_KEY:
353 1.1 christos keyfile = opt_arg();
354 1.1 christos break;
355 1.1 christos case OPT_PUBKEY:
356 1.1 christos pubkey = 1;
357 1.1 christos break;
358 1.1 christos case OPT_NEW:
359 1.1 christos newreq = 1;
360 1.1 christos break;
361 1.1 christos case OPT_CONFIG:
362 1.1 christos template = opt_arg();
363 1.1 christos break;
364 1.1 christos case OPT_SECTION:
365 1.1 christos section = opt_arg();
366 1.1 christos break;
367 1.1 christos case OPT_KEYFORM:
368 1.1 christos if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
369 1.1 christos goto opthelp;
370 1.1 christos break;
371 1.1 christos case OPT_IN:
372 1.1 christos infile = opt_arg();
373 1.1 christos break;
374 1.1 christos case OPT_OUT:
375 1.1 christos outfile = opt_arg();
376 1.1 christos break;
377 1.1 christos case OPT_KEYOUT:
378 1.1 christos keyout = opt_arg();
379 1.1 christos break;
380 1.1 christos case OPT_PASSIN:
381 1.1 christos passargin = opt_arg();
382 1.1 christos break;
383 1.1 christos case OPT_PASSOUT:
384 1.1 christos passargout = opt_arg();
385 1.1 christos break;
386 1.1 christos case OPT_R_CASES:
387 1.1 christos if (!opt_rand(o))
388 1.1 christos goto end;
389 1.1 christos break;
390 1.1 christos case OPT_PROV_CASES:
391 1.1 christos if (!opt_provider(o))
392 1.1 christos goto end;
393 1.1 christos break;
394 1.1 christos case OPT_NEWKEY:
395 1.1 christos keyalg = opt_arg();
396 1.1 christos newreq = 1;
397 1.1 christos break;
398 1.1 christos case OPT_PKEYOPT:
399 1.1 christos if (pkeyopts == NULL)
400 1.1 christos pkeyopts = sk_OPENSSL_STRING_new_null();
401 1.1 christos if (pkeyopts == NULL
402 1.1.1.2 christos || !sk_OPENSSL_STRING_push(pkeyopts, opt_arg()))
403 1.1 christos goto opthelp;
404 1.1 christos break;
405 1.1 christos case OPT_SIGOPT:
406 1.1 christos if (!sigopts)
407 1.1 christos sigopts = sk_OPENSSL_STRING_new_null();
408 1.1 christos if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
409 1.1 christos goto opthelp;
410 1.1 christos break;
411 1.1 christos case OPT_VFYOPT:
412 1.1 christos if (!vfyopts)
413 1.1 christos vfyopts = sk_OPENSSL_STRING_new_null();
414 1.1 christos if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
415 1.1 christos goto opthelp;
416 1.1 christos break;
417 1.1 christos case OPT_BATCH:
418 1.1 christos batch = 1;
419 1.1 christos break;
420 1.1 christos case OPT_NEWHDR:
421 1.1 christos newhdr = 1;
422 1.1 christos break;
423 1.1 christos case OPT_MODULUS:
424 1.1 christos modulus = 1;
425 1.1 christos break;
426 1.1 christos case OPT_VERIFY:
427 1.1 christos verify = 1;
428 1.1 christos break;
429 1.1 christos case OPT_NODES:
430 1.1 christos case OPT_NOENC:
431 1.1 christos noenc = 1;
432 1.1 christos break;
433 1.1 christos case OPT_NOOUT:
434 1.1 christos noout = 1;
435 1.1 christos break;
436 1.1 christos case OPT_VERBOSE:
437 1.1 christos verbose = 1;
438 1.1 christos progress = 1;
439 1.1 christos break;
440 1.1 christos case OPT_QUIET:
441 1.1 christos verbose = 0;
442 1.1 christos progress = 0;
443 1.1 christos break;
444 1.1 christos case OPT_UTF8:
445 1.1 christos chtype = MBSTRING_UTF8;
446 1.1 christos break;
447 1.1 christos case OPT_NAMEOPT:
448 1.1 christos if (!set_nameopt(opt_arg()))
449 1.1 christos goto opthelp;
450 1.1 christos break;
451 1.1 christos case OPT_REQOPT:
452 1.1 christos if (!set_cert_ex(&reqflag, opt_arg()))
453 1.1 christos goto opthelp;
454 1.1 christos break;
455 1.1 christos case OPT_TEXT:
456 1.1 christos text = 1;
457 1.1 christos break;
458 1.1 christos case OPT_X509V1:
459 1.1 christos x509v1 = 1;
460 1.1 christos /* fall thru */
461 1.1 christos case OPT_X509:
462 1.1 christos gen_x509 = 1;
463 1.1 christos break;
464 1.1 christos case OPT_CA:
465 1.1 christos CAfile = opt_arg();
466 1.1 christos gen_x509 = 1;
467 1.1 christos break;
468 1.1 christos case OPT_CAKEY:
469 1.1 christos CAkeyfile = opt_arg();
470 1.1 christos break;
471 1.1 christos case OPT_NOT_BEFORE:
472 1.1 christos not_before = opt_arg();
473 1.1 christos break;
474 1.1 christos case OPT_NOT_AFTER:
475 1.1 christos not_after = opt_arg();
476 1.1 christos break;
477 1.1 christos case OPT_DAYS:
478 1.1 christos days = atoi(opt_arg());
479 1.1 christos if (days <= UNSET_DAYS) {
480 1.1 christos BIO_printf(bio_err, "%s: -days parameter arg must be >= -1\n",
481 1.1.1.2 christos prog);
482 1.1 christos goto end;
483 1.1 christos }
484 1.1 christos break;
485 1.1 christos case OPT_SET_SERIAL:
486 1.1 christos if (serial != NULL) {
487 1.1 christos BIO_printf(bio_err, "Serial number supplied twice\n");
488 1.1 christos goto opthelp;
489 1.1 christos }
490 1.1 christos serial = s2i_ASN1_INTEGER(NULL, opt_arg());
491 1.1 christos if (serial == NULL)
492 1.1 christos goto opthelp;
493 1.1 christos break;
494 1.1 christos case OPT_SUBJECT:
495 1.1 christos subject = 1;
496 1.1 christos break;
497 1.1 christos case OPT_SUBJ:
498 1.1 christos subj = opt_arg();
499 1.1 christos break;
500 1.1 christos case OPT_MULTIVALUE_RDN:
501 1.1 christos /* obsolete */
502 1.1 christos break;
503 1.1 christos case OPT_COPY_EXTENSIONS:
504 1.1 christos if (!set_ext_copy(&ext_copy, opt_arg())) {
505 1.1 christos BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n",
506 1.1.1.2 christos opt_arg());
507 1.1 christos goto end;
508 1.1 christos }
509 1.1 christos break;
510 1.1 christos case OPT_EXTENSIONS:
511 1.1 christos case OPT_REQEXTS:
512 1.1 christos extsect = opt_arg();
513 1.1 christos break;
514 1.1 christos case OPT_ADDEXT:
515 1.1 christos p = opt_arg();
516 1.1 christos if (addexts == NULL) {
517 1.1 christos addexts = lh_OPENSSL_STRING_new(ext_name_hash, ext_name_cmp);
518 1.1 christos addext_bio = BIO_new(BIO_s_mem());
519 1.1 christos if (addexts == NULL || addext_bio == NULL)
520 1.1 christos goto end;
521 1.1 christos }
522 1.1 christos i = duplicated(addexts, p);
523 1.1 christos if (i == 1)
524 1.1 christos goto end;
525 1.1 christos if (i == -1)
526 1.1 christos BIO_printf(bio_err, "Internal error handling -addext %s\n", p);
527 1.1 christos if (i < 0 || BIO_printf(addext_bio, "%s\n", p) < 0)
528 1.1 christos goto end;
529 1.1 christos break;
530 1.1 christos case OPT_PRECERT:
531 1.1 christos newreq = precert = 1;
532 1.1 christos break;
533 1.1 christos case OPT_CIPHER:
534 1.1 christos cipher = EVP_get_cipherbyname(opt_arg());
535 1.1 christos if (cipher == NULL) {
536 1.1 christos BIO_printf(bio_err, "Unknown cipher: %s\n", opt_arg());
537 1.1 christos goto opthelp;
538 1.1 christos }
539 1.1 christos break;
540 1.1 christos case OPT_MD:
541 1.1 christos digest = opt_unknown();
542 1.1 christos break;
543 1.1 christos }
544 1.1 christos }
545 1.1 christos
546 1.1 christos /* No extra arguments. */
547 1.1 christos if (!opt_check_rest_arg(NULL))
548 1.1 christos goto opthelp;
549 1.1 christos
550 1.1 christos if (!app_RAND_load())
551 1.1 christos goto end;
552 1.1 christos
553 1.1 christos if (!gen_x509) {
554 1.1 christos if (days != UNSET_DAYS)
555 1.1 christos BIO_printf(bio_err, "Warning: Ignoring -days without -x509; not generating a certificate\n");
556 1.1 christos if (not_before != NULL)
557 1.1 christos BIO_printf(bio_err, "Warning: Ignoring -not_before without -x509; not generating a certificate\n");
558 1.1 christos if (not_after != NULL)
559 1.1 christos BIO_printf(bio_err, "Warning: Ignoring -not_after without -x509; not generating a certificate\n");
560 1.1 christos if (ext_copy == EXT_COPY_NONE)
561 1.1 christos BIO_printf(bio_err, "Warning: Ignoring -copy_extensions 'none' when -x509 is not given\n");
562 1.1 christos }
563 1.1 christos if (infile == NULL) {
564 1.1 christos if (gen_x509)
565 1.1 christos newreq = 1;
566 1.1 christos else if (!newreq && isatty(fileno_stdin()))
567 1.1 christos BIO_printf(bio_err,
568 1.1.1.2 christos "Warning: Will read cert request from stdin since no -in option is given\n");
569 1.1 christos }
570 1.1 christos
571 1.1 christos if (!app_passwd(passargin, passargout, &passin, &passout)) {
572 1.1 christos BIO_printf(bio_err, "Error getting passwords\n");
573 1.1 christos goto end;
574 1.1 christos }
575 1.1 christos
576 1.1 christos if ((req_conf = app_load_config_verbose(template, verbose)) == NULL)
577 1.1 christos goto end;
578 1.1 christos if (addext_bio != NULL) {
579 1.1 christos if (verbose)
580 1.1 christos BIO_printf(bio_err,
581 1.1.1.2 christos "Using additional configuration from -addext options\n");
582 1.1 christos if ((addext_conf = app_load_config_bio(addext_bio, NULL)) == NULL)
583 1.1 christos goto end;
584 1.1 christos }
585 1.1 christos if (template != default_config_file && !app_load_modules(req_conf))
586 1.1 christos goto end;
587 1.1 christos
588 1.1 christos if (req_conf != NULL) {
589 1.1 christos p = app_conf_try_string(req_conf, NULL, "oid_file");
590 1.1 christos if (p != NULL) {
591 1.1 christos BIO *oid_bio = BIO_new_file(p, "r");
592 1.1 christos
593 1.1 christos if (oid_bio == NULL) {
594 1.1 christos if (verbose)
595 1.1 christos BIO_printf(bio_err,
596 1.1.1.2 christos "Problems opening '%s' for extra OIDs\n", p);
597 1.1 christos } else {
598 1.1 christos OBJ_create_objects(oid_bio);
599 1.1 christos BIO_free(oid_bio);
600 1.1 christos }
601 1.1 christos }
602 1.1 christos }
603 1.1 christos if (!add_oid_section(req_conf))
604 1.1 christos goto end;
605 1.1 christos
606 1.1 christos /* Check that any specified digest is fetchable */
607 1.1 christos if (digest != NULL) {
608 1.1 christos if (!opt_check_md(digest))
609 1.1 christos goto opthelp;
610 1.1 christos } else {
611 1.1 christos /* No digest specified, default to configuration */
612 1.1 christos p = app_conf_try_string(req_conf, section, "default_md");
613 1.1 christos if (p != NULL)
614 1.1 christos digest = p;
615 1.1 christos }
616 1.1 christos
617 1.1 christos if (extsect == NULL)
618 1.1 christos extsect = app_conf_try_string(req_conf, section,
619 1.1.1.2 christos gen_x509 ? V3_EXTENSIONS : REQ_EXTENSIONS);
620 1.1 christos if (extsect != NULL) {
621 1.1 christos /* Check syntax of extension section in config file */
622 1.1 christos X509V3_CTX ctx;
623 1.1 christos
624 1.1 christos X509V3_set_ctx_test(&ctx);
625 1.1 christos X509V3_set_nconf(&ctx, req_conf);
626 1.1 christos if (!X509V3_EXT_add_nconf(req_conf, &ctx, extsect, NULL)) {
627 1.1 christos BIO_printf(bio_err,
628 1.1.1.2 christos "Error checking %s extension section %s\n",
629 1.1.1.2 christos gen_x509 ? "x509" : "request", extsect);
630 1.1 christos goto end;
631 1.1 christos }
632 1.1 christos }
633 1.1 christos if (addext_conf != NULL) {
634 1.1 christos /* Check syntax of command line extensions */
635 1.1 christos X509V3_CTX ctx;
636 1.1 christos
637 1.1 christos X509V3_set_ctx_test(&ctx);
638 1.1 christos X509V3_set_nconf(&ctx, req_conf);
639 1.1 christos if (!X509V3_EXT_add_nconf(addext_conf, &ctx, "default", NULL)) {
640 1.1 christos BIO_printf(bio_err, "Error checking extensions defined using -addext\n");
641 1.1 christos goto end;
642 1.1 christos }
643 1.1 christos }
644 1.1 christos
645 1.1 christos if (passin == NULL)
646 1.1.1.2 christos passin = nofree_passin = app_conf_try_string(req_conf, section, "input_password");
647 1.1 christos
648 1.1 christos if (passout == NULL)
649 1.1.1.2 christos passout = nofree_passout = app_conf_try_string(req_conf, section, "output_password");
650 1.1 christos
651 1.1 christos p = app_conf_try_string(req_conf, section, STRING_MASK);
652 1.1 christos if (p != NULL && !ASN1_STRING_set_default_mask_asc(p)) {
653 1.1 christos BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
654 1.1 christos goto end;
655 1.1 christos }
656 1.1 christos
657 1.1 christos if (chtype != MBSTRING_UTF8) {
658 1.1 christos p = app_conf_try_string(req_conf, section, UTF8_IN);
659 1.1 christos if (p != NULL && strcmp(p, "yes") == 0)
660 1.1 christos chtype = MBSTRING_UTF8;
661 1.1 christos }
662 1.1 christos
663 1.1 christos if (keyfile != NULL) {
664 1.1 christos pkey = load_key(keyfile, keyform, 0, passin, e, "private key");
665 1.1 christos if (pkey == NULL)
666 1.1 christos goto end;
667 1.1 christos app_RAND_load_conf(req_conf, section);
668 1.1 christos }
669 1.1 christos if (keyalg != NULL && pkey != NULL) {
670 1.1 christos BIO_printf(bio_err,
671 1.1.1.2 christos "Warning: Not generating key via given -newkey option since -key is given\n");
672 1.1 christos /* Better throw an error in this case */
673 1.1 christos }
674 1.1 christos if (newreq && pkey == NULL) {
675 1.1 christos app_RAND_load_conf(req_conf, section);
676 1.1 christos
677 1.1 christos if (!app_conf_try_number(req_conf, section, BITS, &newkey_len))
678 1.1 christos newkey_len = DEFAULT_KEY_LENGTH;
679 1.1 christos
680 1.1 christos genctx = set_keygen_ctx(keyalg, &keyalgstr, &newkey_len, gen_eng);
681 1.1 christos if (genctx == NULL)
682 1.1 christos goto end;
683 1.1 christos
684 1.1 christos if (newkey_len < MIN_KEY_LENGTH
685 1.1 christos && (EVP_PKEY_CTX_is_a(genctx, "RSA")
686 1.1 christos || EVP_PKEY_CTX_is_a(genctx, "RSA-PSS")
687 1.1 christos || EVP_PKEY_CTX_is_a(genctx, "DSA"))) {
688 1.1 christos BIO_printf(bio_err, "Private key length too short, needs to be at least %d bits, not %ld.\n",
689 1.1.1.2 christos MIN_KEY_LENGTH, newkey_len);
690 1.1 christos goto end;
691 1.1 christos }
692 1.1 christos
693 1.1 christos if (newkey_len > OPENSSL_RSA_MAX_MODULUS_BITS
694 1.1 christos && (EVP_PKEY_CTX_is_a(genctx, "RSA")
695 1.1 christos || EVP_PKEY_CTX_is_a(genctx, "RSA-PSS")))
696 1.1 christos BIO_printf(bio_err,
697 1.1.1.2 christos "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
698 1.1.1.2 christos " Your key size is %ld! Larger key size may behave not as expected.\n",
699 1.1.1.2 christos OPENSSL_RSA_MAX_MODULUS_BITS, newkey_len);
700 1.1 christos
701 1.1 christos #ifndef OPENSSL_NO_DSA
702 1.1 christos if (EVP_PKEY_CTX_is_a(genctx, "DSA")
703 1.1.1.2 christos && newkey_len > OPENSSL_DSA_MAX_MODULUS_BITS)
704 1.1 christos BIO_printf(bio_err,
705 1.1.1.2 christos "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
706 1.1.1.2 christos " Your key size is %ld! Larger key size may behave not as expected.\n",
707 1.1.1.2 christos OPENSSL_DSA_MAX_MODULUS_BITS, newkey_len);
708 1.1 christos #endif
709 1.1 christos
710 1.1 christos if (pkeyopts != NULL) {
711 1.1 christos char *genopt;
712 1.1 christos for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
713 1.1 christos genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
714 1.1 christos if (pkey_ctrl_string(genctx, genopt) <= 0) {
715 1.1 christos BIO_printf(bio_err, "Key parameter error \"%s\"\n", genopt);
716 1.1 christos goto end;
717 1.1 christos }
718 1.1 christos }
719 1.1 christos }
720 1.1 christos
721 1.1 christos EVP_PKEY_CTX_set_app_data(genctx, bio_err);
722 1.1 christos if (progress)
723 1.1 christos EVP_PKEY_CTX_set_cb(genctx, progress_cb);
724 1.1 christos
725 1.1 christos pkey = app_keygen(genctx, keyalgstr, newkey_len, verbose);
726 1.1 christos if (pkey == NULL)
727 1.1 christos goto end;
728 1.1 christos
729 1.1 christos EVP_PKEY_CTX_free(genctx);
730 1.1 christos genctx = NULL;
731 1.1 christos }
732 1.1 christos if (keyout == NULL && keyfile == NULL)
733 1.1 christos keyout = app_conf_try_string(req_conf, section, KEYFILE);
734 1.1 christos
735 1.1 christos if (pkey != NULL && (keyfile == NULL || keyout != NULL)) {
736 1.1 christos if (verbose) {
737 1.1 christos BIO_printf(bio_err, "Writing private key to ");
738 1.1 christos if (keyout == NULL)
739 1.1 christos BIO_printf(bio_err, "stdout\n");
740 1.1 christos else
741 1.1 christos BIO_printf(bio_err, "'%s'\n", keyout);
742 1.1 christos }
743 1.1.1.2 christos out = bio_open_owner(keyout, outformat, 1);
744 1.1 christos if (out == NULL)
745 1.1 christos goto end;
746 1.1 christos
747 1.1 christos p = app_conf_try_string(req_conf, section, "encrypt_rsa_key");
748 1.1 christos if (p == NULL)
749 1.1 christos p = app_conf_try_string(req_conf, section, "encrypt_key");
750 1.1 christos if (p != NULL && strcmp(p, "no") == 0)
751 1.1 christos cipher = NULL;
752 1.1 christos if (noenc)
753 1.1 christos cipher = NULL;
754 1.1 christos
755 1.1 christos i = 0;
756 1.1.1.2 christos loop:
757 1.1 christos if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
758 1.1.1.2 christos NULL, 0, NULL, passout)) {
759 1.1.1.2 christos if ((ERR_GET_REASON(ERR_peek_error()) == PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) {
760 1.1 christos ERR_clear_error();
761 1.1 christos i++;
762 1.1 christos goto loop;
763 1.1 christos }
764 1.1 christos goto end;
765 1.1 christos }
766 1.1 christos BIO_free_all(out);
767 1.1 christos out = NULL;
768 1.1 christos BIO_printf(bio_err, "-----\n");
769 1.1 christos }
770 1.1 christos
771 1.1 christos /*
772 1.1 christos * subj is expected to be in the format /type0=value0/type1=value1/type2=...
773 1.1 christos * where characters may be escaped by \
774 1.1 christos */
775 1.1 christos if (subj != NULL
776 1.1.1.2 christos && (fsubj = parse_name(subj, chtype, multirdn, "subject")) == NULL)
777 1.1 christos goto end;
778 1.1 christos
779 1.1 christos if (!newreq) {
780 1.1 christos if (keyfile != NULL)
781 1.1 christos BIO_printf(bio_err,
782 1.1.1.2 christos "Warning: Not placing -key in cert or request since request is used\n");
783 1.1 christos req = load_csr_autofmt(infile /* if NULL, reads from stdin */,
784 1.1.1.2 christos informat, vfyopts, "X509 request");
785 1.1 christos if (req == NULL)
786 1.1 christos goto end;
787 1.1 christos } else if (infile != NULL) {
788 1.1 christos BIO_printf(bio_err,
789 1.1.1.2 christos "Warning: Ignoring -in option since -new or -newkey or -precert is given\n");
790 1.1 christos /* Better throw an error in this case, as done in the x509 app */
791 1.1 christos }
792 1.1 christos
793 1.1 christos if (CAkeyfile == NULL)
794 1.1 christos CAkeyfile = CAfile;
795 1.1 christos if (CAkeyfile != NULL) {
796 1.1 christos if (CAfile == NULL) {
797 1.1 christos BIO_printf(bio_err,
798 1.1.1.2 christos "Warning: Ignoring -CAkey option since no -CA option is given\n");
799 1.1 christos } else {
800 1.1 christos if ((CAkey = load_key(CAkeyfile, FORMAT_UNDEF,
801 1.1.1.2 christos 0, passin, e,
802 1.1.1.2 christos CAkeyfile != CAfile
803 1.1.1.2 christos ? "issuer private key from -CAkey arg"
804 1.1.1.2 christos : "issuer private key from -CA arg"))
805 1.1.1.2 christos == NULL)
806 1.1 christos goto end;
807 1.1 christos }
808 1.1 christos }
809 1.1 christos if (CAfile != NULL) {
810 1.1 christos if ((CAcert = load_cert_pass(CAfile, FORMAT_UNDEF, 1, passin,
811 1.1.1.2 christos "issuer cert from -CA arg"))
812 1.1.1.2 christos == NULL)
813 1.1 christos goto end;
814 1.1 christos if (!X509_check_private_key(CAcert, CAkey)) {
815 1.1 christos BIO_printf(bio_err,
816 1.1.1.2 christos "Issuer CA certificate and key do not match\n");
817 1.1 christos goto end;
818 1.1 christos }
819 1.1 christos }
820 1.1 christos if (newreq || gen_x509) {
821 1.1 christos if (CAcert == NULL && pkey == NULL) {
822 1.1 christos BIO_printf(bio_err, "Must provide a signature key using -key or"
823 1.1.1.2 christos " provide -CA / -CAkey\n");
824 1.1 christos goto end;
825 1.1 christos }
826 1.1 christos
827 1.1 christos if (req == NULL) {
828 1.1 christos req = X509_REQ_new_ex(app_get0_libctx(), app_get0_propq());
829 1.1 christos if (req == NULL) {
830 1.1 christos goto end;
831 1.1 christos }
832 1.1 christos
833 1.1 christos if (!make_REQ(req, pkey, fsubj, multirdn, !gen_x509, chtype)) {
834 1.1 christos BIO_printf(bio_err, "Error making certificate request\n");
835 1.1 christos goto end;
836 1.1 christos }
837 1.1 christos /* Note that -x509 can take over -key and -subj option values. */
838 1.1 christos }
839 1.1 christos if (gen_x509) {
840 1.1 christos EVP_PKEY *pub_key = X509_REQ_get0_pubkey(req);
841 1.1 christos EVP_PKEY *issuer_key = CAcert != NULL ? CAkey : pkey;
842 1.1 christos X509V3_CTX ext_ctx;
843 1.1.1.2 christos X509_NAME *issuer = CAcert != NULL ? X509_get_subject_name(CAcert) : X509_REQ_get_subject_name(req);
844 1.1.1.2 christos X509_NAME *n_subj = fsubj != NULL ? fsubj : X509_REQ_get_subject_name(req);
845 1.1 christos
846 1.1 christos if (CAcert != NULL && keyfile != NULL)
847 1.1 christos BIO_printf(bio_err,
848 1.1.1.2 christos "Warning: Not using -key or -newkey for signing since -CA option is given\n");
849 1.1 christos
850 1.1 christos if ((new_x509 = X509_new_ex(app_get0_libctx(),
851 1.1.1.2 christos app_get0_propq()))
852 1.1.1.2 christos == NULL)
853 1.1 christos goto end;
854 1.1 christos
855 1.1 christos if (serial != NULL) {
856 1.1 christos if (!X509_set_serialNumber(new_x509, serial))
857 1.1 christos goto end;
858 1.1 christos } else {
859 1.1 christos if (!rand_serial(NULL, X509_get_serialNumber(new_x509)))
860 1.1 christos goto end;
861 1.1 christos }
862 1.1 christos
863 1.1 christos if (!X509_set_issuer_name(new_x509, issuer))
864 1.1 christos goto end;
865 1.1 christos if (days == UNSET_DAYS)
866 1.1 christos days = DEFAULT_DAYS;
867 1.1 christos else if (not_after != NULL)
868 1.1.1.2 christos BIO_printf(bio_err, "Warning: -not_after option overriding -days option\n");
869 1.1 christos if (!set_cert_times(new_x509, not_before, not_after, days, 1))
870 1.1 christos goto end;
871 1.1 christos if (!X509_set_subject_name(new_x509, n_subj))
872 1.1 christos goto end;
873 1.1 christos if (!pub_key || !X509_set_pubkey(new_x509, pub_key))
874 1.1 christos goto end;
875 1.1 christos if (ext_copy == EXT_COPY_UNSET) {
876 1.1 christos if (infile != NULL)
877 1.1 christos BIO_printf(bio_err, "Warning: No -copy_extensions given; ignoring any extensions in the request\n");
878 1.1 christos } else if (!copy_extensions(new_x509, req, ext_copy)) {
879 1.1 christos BIO_printf(bio_err, "Error copying extensions from request\n");
880 1.1 christos goto end;
881 1.1 christos }
882 1.1 christos
883 1.1 christos /* Set up V3 context struct */
884 1.1 christos X509V3_set_ctx(&ext_ctx, CAcert != NULL ? CAcert : new_x509,
885 1.1.1.2 christos new_x509, NULL, NULL, X509V3_CTX_REPLACE);
886 1.1 christos /* prepare fallback for AKID, but only if issuer cert == new_x509 */
887 1.1 christos if (CAcert == NULL) {
888 1.1 christos if (!X509V3_set_issuer_pkey(&ext_ctx, issuer_key))
889 1.1 christos goto end;
890 1.1 christos if (!cert_matches_key(new_x509, issuer_key))
891 1.1 christos BIO_printf(bio_err,
892 1.1.1.2 christos "Warning: Signature key and public key of cert do not match\n");
893 1.1 christos }
894 1.1 christos X509V3_set_nconf(&ext_ctx, req_conf);
895 1.1 christos
896 1.1 christos /* Add extensions */
897 1.1 christos if (extsect != NULL
898 1.1 christos && !X509V3_EXT_add_nconf(req_conf, &ext_ctx, extsect, new_x509)) {
899 1.1 christos BIO_printf(bio_err, "Error adding x509 extensions from section %s\n",
900 1.1.1.2 christos extsect);
901 1.1 christos goto end;
902 1.1 christos }
903 1.1 christos if (addext_conf != NULL
904 1.1 christos && !X509V3_EXT_add_nconf(addext_conf, &ext_ctx, "default",
905 1.1.1.2 christos new_x509)) {
906 1.1 christos BIO_printf(bio_err, "Error adding x509 extensions defined via -addext\n");
907 1.1 christos goto end;
908 1.1 christos }
909 1.1 christos
910 1.1 christos /* If a pre-cert was requested, we need to add a poison extension */
911 1.1 christos if (precert) {
912 1.1 christos if (X509_add1_ext_i2d(new_x509, NID_ct_precert_poison,
913 1.1.1.2 christos NULL, 1, 0)
914 1.1.1.2 christos != 1) {
915 1.1 christos BIO_printf(bio_err, "Error adding poison extension\n");
916 1.1 christos goto end;
917 1.1 christos }
918 1.1 christos }
919 1.1 christos
920 1.1 christos i = do_X509_sign(new_x509, x509v1, issuer_key, digest, sigopts,
921 1.1.1.2 christos &ext_ctx);
922 1.1 christos if (!i)
923 1.1 christos goto end;
924 1.1 christos } else {
925 1.1 christos X509V3_CTX ext_ctx;
926 1.1 christos
927 1.1 christos if (precert) {
928 1.1 christos BIO_printf(bio_err,
929 1.1.1.2 christos "Warning: Ignoring -precert flag since no cert is produced\n");
930 1.1 christos }
931 1.1 christos /* Set up V3 context struct */
932 1.1 christos X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, X509V3_CTX_REPLACE);
933 1.1 christos X509V3_set_nconf(&ext_ctx, req_conf);
934 1.1 christos
935 1.1 christos /* Add extensions */
936 1.1 christos if (extsect != NULL
937 1.1 christos && !X509V3_EXT_REQ_add_nconf(req_conf, &ext_ctx, extsect, req)) {
938 1.1 christos BIO_printf(bio_err, "Error adding request extensions from section %s\n",
939 1.1.1.2 christos extsect);
940 1.1 christos goto end;
941 1.1 christos }
942 1.1 christos if (addext_conf != NULL
943 1.1 christos && !X509V3_EXT_REQ_add_nconf(addext_conf, &ext_ctx, "default",
944 1.1.1.2 christos req)) {
945 1.1 christos BIO_printf(bio_err, "Error adding request extensions defined via -addext\n");
946 1.1 christos goto end;
947 1.1 christos }
948 1.1 christos i = do_X509_REQ_sign(req, pkey, digest, sigopts);
949 1.1 christos if (!i)
950 1.1 christos goto end;
951 1.1 christos }
952 1.1 christos }
953 1.1 christos
954 1.1 christos if (subj != NULL && !newreq && !gen_x509) {
955 1.1 christos if (verbose) {
956 1.1 christos BIO_printf(out, "Modifying subject of certificate request\n");
957 1.1 christos print_name(out, "Old subject=", X509_REQ_get_subject_name(req));
958 1.1 christos }
959 1.1 christos
960 1.1 christos if (!X509_REQ_set_subject_name(req, fsubj)) {
961 1.1 christos BIO_printf(bio_err, "Error modifying subject of certificate request\n");
962 1.1 christos goto end;
963 1.1 christos }
964 1.1 christos
965 1.1 christos if (verbose) {
966 1.1 christos print_name(out, "New subject=", X509_REQ_get_subject_name(req));
967 1.1 christos }
968 1.1 christos }
969 1.1 christos
970 1.1 christos if (verify) {
971 1.1 christos EVP_PKEY *tpubkey = pkey;
972 1.1 christos
973 1.1 christos if (tpubkey == NULL) {
974 1.1 christos tpubkey = X509_REQ_get0_pubkey(req);
975 1.1 christos if (tpubkey == NULL)
976 1.1 christos goto end;
977 1.1 christos }
978 1.1 christos
979 1.1 christos i = do_X509_REQ_verify(req, tpubkey, vfyopts);
980 1.1 christos
981 1.1 christos if (i < 0)
982 1.1 christos goto end;
983 1.1 christos if (i == 0) {
984 1.1 christos BIO_printf(bio_err, "Certificate request self-signature verify failure\n");
985 1.1.1.2 christos goto end;
986 1.1 christos } else /* i > 0 */
987 1.1 christos BIO_printf(bio_out, "Certificate request self-signature verify OK\n");
988 1.1 christos }
989 1.1 christos
990 1.1 christos if (noout && !text && !modulus && !subject && !pubkey) {
991 1.1 christos ret = 0;
992 1.1 christos goto end;
993 1.1 christos }
994 1.1 christos
995 1.1 christos out = bio_open_default(outfile,
996 1.1.1.2 christos keyout != NULL && outfile != NULL && strcmp(keyout, outfile) == 0 ? 'a' : 'w',
997 1.1.1.2 christos outformat);
998 1.1 christos if (out == NULL)
999 1.1 christos goto end;
1000 1.1 christos
1001 1.1 christos if (pubkey) {
1002 1.1 christos EVP_PKEY *tpubkey = X509_REQ_get0_pubkey(req);
1003 1.1 christos
1004 1.1 christos if (tpubkey == NULL) {
1005 1.1 christos BIO_printf(bio_err, "Error getting public key\n");
1006 1.1 christos goto end;
1007 1.1 christos }
1008 1.1 christos PEM_write_bio_PUBKEY(out, tpubkey);
1009 1.1 christos }
1010 1.1 christos
1011 1.1 christos if (text) {
1012 1.1 christos if (gen_x509)
1013 1.1 christos ret = X509_print_ex(out, new_x509, get_nameopt(), reqflag);
1014 1.1 christos else
1015 1.1 christos ret = X509_REQ_print_ex(out, req, get_nameopt(), reqflag);
1016 1.1 christos
1017 1.1 christos if (ret == 0) {
1018 1.1 christos if (gen_x509)
1019 1.1 christos BIO_printf(bio_err, "Error printing certificate\n");
1020 1.1 christos else
1021 1.1 christos BIO_printf(bio_err, "Error printing certificate request\n");
1022 1.1 christos goto end;
1023 1.1 christos }
1024 1.1 christos }
1025 1.1 christos
1026 1.1 christos if (subject) {
1027 1.1.1.2 christos print_name(out, "subject=", gen_x509 ? X509_get_subject_name(new_x509) : X509_REQ_get_subject_name(req));
1028 1.1 christos }
1029 1.1 christos
1030 1.1 christos if (modulus) {
1031 1.1 christos EVP_PKEY *tpubkey;
1032 1.1 christos
1033 1.1 christos if (gen_x509)
1034 1.1 christos tpubkey = X509_get0_pubkey(new_x509);
1035 1.1 christos else
1036 1.1 christos tpubkey = X509_REQ_get0_pubkey(req);
1037 1.1 christos if (tpubkey == NULL) {
1038 1.1 christos BIO_puts(bio_err, "Modulus is unavailable\n");
1039 1.1 christos goto end;
1040 1.1 christos }
1041 1.1 christos BIO_puts(out, "Modulus=");
1042 1.1 christos if (EVP_PKEY_is_a(tpubkey, "RSA") || EVP_PKEY_is_a(tpubkey, "RSA-PSS")) {
1043 1.1 christos BIGNUM *n = NULL;
1044 1.1 christos
1045 1.1 christos if (!EVP_PKEY_get_bn_param(tpubkey, "n", &n))
1046 1.1 christos goto end;
1047 1.1 christos BN_print(out, n);
1048 1.1 christos BN_free(n);
1049 1.1 christos } else {
1050 1.1 christos BIO_puts(out, "Wrong Algorithm type");
1051 1.1 christos }
1052 1.1 christos BIO_puts(out, "\n");
1053 1.1 christos }
1054 1.1 christos
1055 1.1 christos if (!noout && !gen_x509) {
1056 1.1 christos if (outformat == FORMAT_ASN1)
1057 1.1 christos i = i2d_X509_REQ_bio(out, req);
1058 1.1 christos else if (newhdr)
1059 1.1 christos i = PEM_write_bio_X509_REQ_NEW(out, req);
1060 1.1 christos else
1061 1.1 christos i = PEM_write_bio_X509_REQ(out, req);
1062 1.1 christos if (!i) {
1063 1.1 christos BIO_printf(bio_err, "Unable to write certificate request\n");
1064 1.1 christos goto end;
1065 1.1 christos }
1066 1.1 christos }
1067 1.1 christos if (!noout && gen_x509 && new_x509 != NULL) {
1068 1.1 christos if (outformat == FORMAT_ASN1)
1069 1.1 christos i = i2d_X509_bio(out, new_x509);
1070 1.1 christos else
1071 1.1 christos i = PEM_write_bio_X509(out, new_x509);
1072 1.1 christos if (!i) {
1073 1.1 christos BIO_printf(bio_err, "Unable to write X509 certificate\n");
1074 1.1 christos goto end;
1075 1.1 christos }
1076 1.1 christos }
1077 1.1 christos ret = 0;
1078 1.1.1.2 christos end:
1079 1.1 christos if (ret) {
1080 1.1 christos ERR_print_errors(bio_err);
1081 1.1 christos }
1082 1.1 christos NCONF_free(req_conf);
1083 1.1 christos NCONF_free(addext_conf);
1084 1.1 christos BIO_free(addext_bio);
1085 1.1 christos BIO_free_all(out);
1086 1.1 christos EVP_PKEY_free(pkey);
1087 1.1 christos EVP_PKEY_CTX_free(genctx);
1088 1.1 christos sk_OPENSSL_STRING_free(pkeyopts);
1089 1.1 christos sk_OPENSSL_STRING_free(sigopts);
1090 1.1 christos sk_OPENSSL_STRING_free(vfyopts);
1091 1.1 christos lh_OPENSSL_STRING_doall(addexts, exts_cleanup);
1092 1.1 christos lh_OPENSSL_STRING_free(addexts);
1093 1.1 christos #ifndef OPENSSL_NO_ENGINE
1094 1.1 christos release_engine(gen_eng);
1095 1.1 christos #endif
1096 1.1 christos OPENSSL_free(keyalgstr);
1097 1.1 christos X509_REQ_free(req);
1098 1.1 christos X509_NAME_free(fsubj);
1099 1.1 christos X509_free(new_x509);
1100 1.1 christos X509_free(CAcert);
1101 1.1 christos EVP_PKEY_free(CAkey);
1102 1.1 christos ASN1_INTEGER_free(serial);
1103 1.1 christos release_engine(e);
1104 1.1 christos if (passin != nofree_passin)
1105 1.1 christos OPENSSL_free(passin);
1106 1.1 christos if (passout != nofree_passout)
1107 1.1 christos OPENSSL_free(passout);
1108 1.1 christos return ret;
1109 1.1 christos }
1110 1.1 christos
1111 1.1 christos static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, X509_NAME *fsubj,
1112 1.1.1.2 christos int multirdn, int attribs, unsigned long chtype)
1113 1.1 christos {
1114 1.1 christos int ret = 0, i;
1115 1.1 christos char no_prompt = 0;
1116 1.1 christos STACK_OF(CONF_VALUE) *dn_sk = NULL, *attr_sk = NULL;
1117 1.1 christos char *tmp, *dn_sect, *attr_sect;
1118 1.1 christos
1119 1.1 christos tmp = app_conf_try_string(req_conf, section, PROMPT);
1120 1.1 christos if (tmp != NULL && strcmp(tmp, "no") == 0)
1121 1.1 christos no_prompt = 1;
1122 1.1 christos
1123 1.1 christos dn_sect = app_conf_try_string(req_conf, section, DISTINGUISHED_NAME);
1124 1.1 christos if (dn_sect != NULL) {
1125 1.1 christos dn_sk = NCONF_get_section(req_conf, dn_sect);
1126 1.1 christos if (dn_sk == NULL) {
1127 1.1 christos BIO_printf(bio_err, "Unable to get '%s' section\n", dn_sect);
1128 1.1 christos goto err;
1129 1.1 christos }
1130 1.1 christos }
1131 1.1 christos
1132 1.1 christos attr_sect = app_conf_try_string(req_conf, section, ATTRIBUTES);
1133 1.1 christos if (attr_sect != NULL) {
1134 1.1 christos attr_sk = NCONF_get_section(req_conf, attr_sect);
1135 1.1 christos if (attr_sk == NULL) {
1136 1.1 christos BIO_printf(bio_err, "Unable to get '%s' section\n", attr_sect);
1137 1.1 christos goto err;
1138 1.1 christos }
1139 1.1 christos }
1140 1.1 christos
1141 1.1 christos /* so far there is only version 1 */
1142 1.1 christos if (!X509_REQ_set_version(req, X509_REQ_VERSION_1))
1143 1.1 christos goto err;
1144 1.1 christos
1145 1.1 christos if (fsubj != NULL)
1146 1.1 christos i = X509_REQ_set_subject_name(req, fsubj);
1147 1.1 christos else if (no_prompt)
1148 1.1 christos i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
1149 1.1 christos else
1150 1.1 christos i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs,
1151 1.1.1.2 christos chtype);
1152 1.1 christos if (!i)
1153 1.1 christos goto err;
1154 1.1 christos
1155 1.1 christos if (!X509_REQ_set_pubkey(req, pkey))
1156 1.1 christos goto err;
1157 1.1 christos
1158 1.1 christos ret = 1;
1159 1.1.1.2 christos err:
1160 1.1 christos return ret;
1161 1.1 christos }
1162 1.1 christos
1163 1.1 christos static int prompt_info(X509_REQ *req,
1164 1.1.1.2 christos STACK_OF(CONF_VALUE) *dn_sk, const char *dn_sect,
1165 1.1.1.2 christos STACK_OF(CONF_VALUE) *attr_sk, const char *attr_sect,
1166 1.1.1.2 christos int attribs, unsigned long chtype)
1167 1.1 christos {
1168 1.1 christos int i;
1169 1.1 christos char *p, *q;
1170 1.1 christos char buf[100];
1171 1.1 christos int nid, mval;
1172 1.1 christos long n_min, n_max;
1173 1.1 christos char *type, *value;
1174 1.1 christos const char *def;
1175 1.1 christos CONF_VALUE *v;
1176 1.1 christos X509_NAME *subj = X509_REQ_get_subject_name(req);
1177 1.1 christos
1178 1.1 christos if (!batch) {
1179 1.1 christos BIO_printf(bio_err,
1180 1.1.1.2 christos "You are about to be asked to enter information that will be incorporated\n");
1181 1.1 christos BIO_printf(bio_err, "into your certificate request.\n");
1182 1.1 christos BIO_printf(bio_err,
1183 1.1.1.2 christos "What you are about to enter is what is called a Distinguished Name or a DN.\n");
1184 1.1 christos BIO_printf(bio_err,
1185 1.1.1.2 christos "There are quite a few fields but you can leave some blank\n");
1186 1.1 christos BIO_printf(bio_err,
1187 1.1.1.2 christos "For some fields there will be a default value,\n");
1188 1.1 christos BIO_printf(bio_err,
1189 1.1.1.2 christos "If you enter '.', the field will be left blank.\n");
1190 1.1 christos BIO_printf(bio_err, "-----\n");
1191 1.1 christos }
1192 1.1 christos
1193 1.1 christos if (sk_CONF_VALUE_num(dn_sk)) {
1194 1.1 christos i = -1;
1195 1.1.1.2 christos start:
1196 1.1 christos for (;;) {
1197 1.1 christos i++;
1198 1.1 christos if (sk_CONF_VALUE_num(dn_sk) <= i)
1199 1.1 christos break;
1200 1.1 christos
1201 1.1 christos v = sk_CONF_VALUE_value(dn_sk, i);
1202 1.1 christos p = q = NULL;
1203 1.1 christos type = v->name;
1204 1.1.1.2 christos if (!check_end(type, "_min") || !check_end(type, "_max") || !check_end(type, "_default") || !check_end(type, "_value"))
1205 1.1 christos continue;
1206 1.1 christos /*
1207 1.1 christos * Skip past any leading X. X: X, etc to allow for multiple
1208 1.1 christos * instances
1209 1.1 christos */
1210 1.1 christos for (p = v->name; *p; p++)
1211 1.1 christos if ((*p == ':') || (*p == ',') || (*p == '.')) {
1212 1.1 christos p++;
1213 1.1 christos if (*p)
1214 1.1 christos type = p;
1215 1.1 christos break;
1216 1.1 christos }
1217 1.1 christos if (*type == '+') {
1218 1.1 christos mval = -1;
1219 1.1 christos type++;
1220 1.1 christos } else {
1221 1.1 christos mval = 0;
1222 1.1 christos }
1223 1.1 christos /* If OBJ not recognised ignore it */
1224 1.1 christos if ((nid = OBJ_txt2nid(type)) == NID_undef)
1225 1.1 christos goto start;
1226 1.1 christos if (!join(buf, sizeof(buf), v->name, "_default", "Name"))
1227 1.1 christos return 0;
1228 1.1 christos if ((def = app_conf_try_string(req_conf, dn_sect, buf)) == NULL)
1229 1.1 christos def = "";
1230 1.1 christos
1231 1.1 christos if (!join(buf, sizeof(buf), v->name, "_value", "Name"))
1232 1.1 christos return 0;
1233 1.1 christos if ((value = app_conf_try_string(req_conf, dn_sect, buf)) == NULL)
1234 1.1 christos value = NULL;
1235 1.1 christos
1236 1.1 christos if (!join(buf, sizeof(buf), v->name, "_min", "Name"))
1237 1.1 christos return 0;
1238 1.1 christos if (!app_conf_try_number(req_conf, dn_sect, buf, &n_min))
1239 1.1 christos n_min = -1;
1240 1.1 christos
1241 1.1 christos if (!join(buf, sizeof(buf), v->name, "_max", "Name"))
1242 1.1 christos return 0;
1243 1.1 christos if (!app_conf_try_number(req_conf, dn_sect, buf, &n_max))
1244 1.1 christos n_max = -1;
1245 1.1 christos
1246 1.1 christos if (!add_DN_object(subj, v->value, def, value, nid,
1247 1.1.1.2 christos n_min, n_max, chtype, mval))
1248 1.1 christos return 0;
1249 1.1 christos }
1250 1.1 christos if (X509_NAME_entry_count(subj) == 0) {
1251 1.1 christos BIO_printf(bio_err, "Error: No objects specified in config file\n");
1252 1.1 christos return 0;
1253 1.1 christos }
1254 1.1 christos
1255 1.1 christos if (attribs) {
1256 1.1 christos if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0)
1257 1.1 christos && (!batch)) {
1258 1.1 christos BIO_printf(bio_err,
1259 1.1.1.2 christos "\nPlease enter the following 'extra' attributes\n");
1260 1.1 christos BIO_printf(bio_err,
1261 1.1.1.2 christos "to be sent with your certificate request\n");
1262 1.1 christos }
1263 1.1 christos
1264 1.1 christos i = -1;
1265 1.1.1.2 christos start2:
1266 1.1 christos for (;;) {
1267 1.1 christos i++;
1268 1.1 christos if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i))
1269 1.1 christos break;
1270 1.1 christos
1271 1.1 christos v = sk_CONF_VALUE_value(attr_sk, i);
1272 1.1 christos type = v->name;
1273 1.1 christos if ((nid = OBJ_txt2nid(type)) == NID_undef)
1274 1.1 christos goto start2;
1275 1.1 christos
1276 1.1 christos if (!join(buf, sizeof(buf), type, "_default", "Name"))
1277 1.1 christos return 0;
1278 1.1 christos def = app_conf_try_string(req_conf, attr_sect, buf);
1279 1.1 christos if (def == NULL)
1280 1.1 christos def = "";
1281 1.1 christos
1282 1.1 christos if (!join(buf, sizeof(buf), type, "_value", "Name"))
1283 1.1 christos return 0;
1284 1.1 christos value = app_conf_try_string(req_conf, attr_sect, buf);
1285 1.1 christos
1286 1.1 christos if (!join(buf, sizeof(buf), type, "_min", "Name"))
1287 1.1 christos return 0;
1288 1.1 christos if (!app_conf_try_number(req_conf, attr_sect, buf, &n_min))
1289 1.1 christos n_min = -1;
1290 1.1 christos
1291 1.1 christos if (!join(buf, sizeof(buf), type, "_max", "Name"))
1292 1.1 christos return 0;
1293 1.1 christos if (!app_conf_try_number(req_conf, attr_sect, buf, &n_max))
1294 1.1 christos n_max = -1;
1295 1.1 christos
1296 1.1 christos if (!add_attribute_object(req,
1297 1.1.1.2 christos v->value, def, value, nid, n_min,
1298 1.1.1.2 christos n_max, chtype))
1299 1.1 christos return 0;
1300 1.1 christos }
1301 1.1 christos }
1302 1.1 christos } else {
1303 1.1 christos BIO_printf(bio_err, "No template, please set one up.\n");
1304 1.1 christos return 0;
1305 1.1 christos }
1306 1.1 christos
1307 1.1 christos return 1;
1308 1.1 christos }
1309 1.1 christos
1310 1.1 christos static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
1311 1.1.1.2 christos STACK_OF(CONF_VALUE) *attr_sk, int attribs,
1312 1.1.1.2 christos unsigned long chtype)
1313 1.1 christos {
1314 1.1 christos int i, spec_char, plus_char;
1315 1.1 christos char *p, *q;
1316 1.1 christos char *type;
1317 1.1 christos CONF_VALUE *v;
1318 1.1 christos X509_NAME *subj;
1319 1.1 christos
1320 1.1 christos subj = X509_REQ_get_subject_name(req);
1321 1.1 christos
1322 1.1 christos for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1323 1.1 christos int mval;
1324 1.1 christos v = sk_CONF_VALUE_value(dn_sk, i);
1325 1.1 christos p = q = NULL;
1326 1.1 christos type = v->name;
1327 1.1 christos /*
1328 1.1 christos * Skip past any leading X. X: X, etc to allow for multiple instances
1329 1.1 christos */
1330 1.1 christos for (p = v->name; *p; p++) {
1331 1.1 christos #ifndef CHARSET_EBCDIC
1332 1.1 christos spec_char = (*p == ':' || *p == ',' || *p == '.');
1333 1.1 christos #else
1334 1.1 christos spec_char = (*p == os_toascii[':'] || *p == os_toascii[',']
1335 1.1.1.2 christos || *p == os_toascii['.']);
1336 1.1 christos #endif
1337 1.1 christos if (spec_char) {
1338 1.1 christos p++;
1339 1.1 christos if (*p)
1340 1.1 christos type = p;
1341 1.1 christos break;
1342 1.1 christos }
1343 1.1 christos }
1344 1.1 christos #ifndef CHARSET_EBCDIC
1345 1.1 christos plus_char = (*type == '+');
1346 1.1 christos #else
1347 1.1 christos plus_char = (*type == os_toascii['+']);
1348 1.1 christos #endif
1349 1.1 christos if (plus_char) {
1350 1.1 christos type++;
1351 1.1 christos mval = -1;
1352 1.1 christos } else {
1353 1.1 christos mval = 0;
1354 1.1 christos }
1355 1.1 christos if (!X509_NAME_add_entry_by_txt(subj, type, chtype,
1356 1.1.1.2 christos (unsigned char *)v->value, -1, -1,
1357 1.1.1.2 christos mval))
1358 1.1 christos return 0;
1359 1.1 christos }
1360 1.1 christos
1361 1.1 christos if (!X509_NAME_entry_count(subj)) {
1362 1.1 christos BIO_printf(bio_err, "Error: No objects specified in config file\n");
1363 1.1 christos return 0;
1364 1.1 christos }
1365 1.1 christos if (attribs) {
1366 1.1 christos for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
1367 1.1 christos v = sk_CONF_VALUE_value(attr_sk, i);
1368 1.1 christos if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
1369 1.1.1.2 christos (unsigned char *)v->value, -1))
1370 1.1 christos return 0;
1371 1.1 christos }
1372 1.1 christos }
1373 1.1 christos return 1;
1374 1.1 christos }
1375 1.1 christos
1376 1.1 christos static int add_DN_object(X509_NAME *n, char *text, const char *def,
1377 1.1.1.2 christos char *value, int nid, int n_min, int n_max,
1378 1.1.1.2 christos unsigned long chtype, int mval)
1379 1.1 christos {
1380 1.1 christos int ret = 0;
1381 1.1 christos char buf[1024];
1382 1.1 christos
1383 1.1 christos ret = build_data(text, def, value, n_min, n_max, buf, sizeof(buf),
1384 1.1.1.2 christos "DN value", "DN default");
1385 1.1 christos if ((ret == 0) || (ret == 1))
1386 1.1 christos return ret;
1387 1.1 christos ret = 1;
1388 1.1 christos
1389 1.1 christos if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
1390 1.1.1.2 christos (unsigned char *)buf, -1, -1, mval))
1391 1.1 christos ret = 0;
1392 1.1 christos
1393 1.1 christos return ret;
1394 1.1 christos }
1395 1.1 christos
1396 1.1 christos static int add_attribute_object(X509_REQ *req, char *text, const char *def,
1397 1.1.1.2 christos char *value, int nid, int n_min,
1398 1.1.1.2 christos int n_max, unsigned long chtype)
1399 1.1 christos {
1400 1.1 christos int ret = 0;
1401 1.1 christos char buf[1024];
1402 1.1 christos
1403 1.1 christos ret = build_data(text, def, value, n_min, n_max, buf, sizeof(buf),
1404 1.1.1.2 christos "Attribute value", "Attribute default");
1405 1.1 christos if ((ret == 0) || (ret == 1))
1406 1.1 christos return ret;
1407 1.1 christos ret = 1;
1408 1.1 christos
1409 1.1 christos if (!X509_REQ_add1_attr_by_NID(req, nid, chtype,
1410 1.1.1.2 christos (unsigned char *)buf, -1)) {
1411 1.1 christos BIO_printf(bio_err, "Error adding attribute\n");
1412 1.1 christos ret = 0;
1413 1.1 christos }
1414 1.1 christos
1415 1.1 christos return ret;
1416 1.1 christos }
1417 1.1 christos
1418 1.1 christos static int build_data(char *text, const char *def, char *value,
1419 1.1.1.2 christos int n_min, int n_max, char *buf, const int buf_size,
1420 1.1.1.2 christos const char *desc1, const char *desc2)
1421 1.1 christos {
1422 1.1 christos int i;
1423 1.1.1.2 christos start:
1424 1.1 christos if (!batch)
1425 1.1 christos BIO_printf(bio_err, "%s [%s]:", text, def);
1426 1.1 christos (void)BIO_flush(bio_err);
1427 1.1 christos if (value != NULL) {
1428 1.1 christos if (!join(buf, buf_size, value, "\n", desc1))
1429 1.1 christos return 0;
1430 1.1 christos BIO_printf(bio_err, "%s\n", value);
1431 1.1 christos } else {
1432 1.1 christos buf[0] = '\0';
1433 1.1 christos if (!batch) {
1434 1.1 christos if (!fgets(buf, buf_size, stdin))
1435 1.1 christos return 0;
1436 1.1 christos } else {
1437 1.1 christos buf[0] = '\n';
1438 1.1 christos buf[1] = '\0';
1439 1.1 christos }
1440 1.1 christos }
1441 1.1 christos
1442 1.1 christos if (buf[0] == '\0')
1443 1.1 christos return 0;
1444 1.1 christos if (buf[0] == '\n') {
1445 1.1 christos if ((def == NULL) || (def[0] == '\0'))
1446 1.1 christos return 1;
1447 1.1 christos if (!join(buf, buf_size, def, "\n", desc2))
1448 1.1 christos return 0;
1449 1.1 christos } else if ((buf[0] == '.') && (buf[1] == '\n')) {
1450 1.1 christos return 1;
1451 1.1 christos }
1452 1.1 christos
1453 1.1 christos i = strlen(buf);
1454 1.1 christos if (buf[i - 1] != '\n') {
1455 1.1 christos BIO_printf(bio_err, "Missing newline at end of input\n");
1456 1.1 christos return 0;
1457 1.1 christos }
1458 1.1 christos buf[--i] = '\0';
1459 1.1 christos #ifdef CHARSET_EBCDIC
1460 1.1 christos ebcdic2ascii(buf, buf, i);
1461 1.1 christos #endif
1462 1.1 christos if (!req_check_len(i, n_min, n_max)) {
1463 1.1 christos if (batch || value)
1464 1.1 christos return 0;
1465 1.1 christos goto start;
1466 1.1 christos }
1467 1.1 christos return 2;
1468 1.1 christos }
1469 1.1 christos
1470 1.1 christos static int req_check_len(int len, int n_min, int n_max)
1471 1.1 christos {
1472 1.1 christos if (n_min > 0 && len < n_min) {
1473 1.1 christos BIO_printf(bio_err,
1474 1.1.1.2 christos "String too short, must be at least %d bytes long\n", n_min);
1475 1.1 christos return 0;
1476 1.1 christos }
1477 1.1 christos if (n_max >= 0 && len > n_max) {
1478 1.1 christos BIO_printf(bio_err,
1479 1.1.1.2 christos "String too long, must be at most %d bytes long\n", n_max);
1480 1.1 christos return 0;
1481 1.1 christos }
1482 1.1 christos return 1;
1483 1.1 christos }
1484 1.1 christos
1485 1.1 christos /* Check if the end of a string matches 'end' */
1486 1.1 christos static int check_end(const char *str, const char *end)
1487 1.1 christos {
1488 1.1 christos size_t elen, slen;
1489 1.1 christos const char *tmp;
1490 1.1 christos
1491 1.1 christos elen = strlen(end);
1492 1.1 christos slen = strlen(str);
1493 1.1 christos if (elen > slen)
1494 1.1 christos return 1;
1495 1.1 christos tmp = str + slen - elen;
1496 1.1 christos return strcmp(tmp, end);
1497 1.1 christos }
1498 1.1 christos
1499 1.1 christos /*
1500 1.1 christos * Merge the two strings together into the result buffer checking for
1501 1.1 christos * overflow and producing an error message if there is.
1502 1.1 christos */
1503 1.1 christos static int join(char buf[], size_t buf_size, const char *name,
1504 1.1.1.2 christos const char *tail, const char *desc)
1505 1.1 christos {
1506 1.1 christos const size_t name_len = strlen(name), tail_len = strlen(tail);
1507 1.1 christos
1508 1.1 christos if (name_len + tail_len + 1 > buf_size) {
1509 1.1 christos BIO_printf(bio_err, "%s '%s' too long\n", desc, name);
1510 1.1 christos return 0;
1511 1.1 christos }
1512 1.1 christos memcpy(buf, name, name_len);
1513 1.1 christos memcpy(buf + name_len, tail, tail_len + 1);
1514 1.1 christos return 1;
1515 1.1 christos }
1516 1.1 christos
1517 1.1 christos static EVP_PKEY_CTX *set_keygen_ctx(const char *gstr,
1518 1.1.1.2 christos char **pkeytype, long *pkeylen,
1519 1.1.1.2 christos ENGINE *keygen_engine)
1520 1.1 christos {
1521 1.1 christos EVP_PKEY_CTX *gctx = NULL;
1522 1.1 christos EVP_PKEY *param = NULL;
1523 1.1 christos long keylen = -1;
1524 1.1 christos BIO *pbio = NULL;
1525 1.1 christos const char *keytype = NULL;
1526 1.1 christos size_t keytypelen = 0;
1527 1.1 christos int expect_paramfile = 0;
1528 1.1 christos const char *paramfile = NULL;
1529 1.1 christos
1530 1.1 christos /* Treat the first part of gstr, and only that */
1531 1.1 christos if (gstr == NULL) {
1532 1.1 christos /*
1533 1.1 christos * Special case: when no string given, default to RSA and the
1534 1.1 christos * key length given by |*pkeylen|.
1535 1.1 christos */
1536 1.1 christos keytype = "RSA";
1537 1.1 christos keylen = *pkeylen;
1538 1.1 christos } else if (gstr[0] >= '0' && gstr[0] <= '9') {
1539 1.1 christos /* Special case: only keylength given from string, so default to RSA */
1540 1.1 christos keytype = "RSA";
1541 1.1 christos /* The second part treatment will do the rest */
1542 1.1 christos } else {
1543 1.1 christos const char *p = strchr(gstr, ':');
1544 1.1 christos int len;
1545 1.1 christos
1546 1.1 christos if (p != NULL)
1547 1.1 christos len = p - gstr;
1548 1.1 christos else
1549 1.1 christos len = strlen(gstr);
1550 1.1 christos
1551 1.1 christos if (strncmp(gstr, "param", len) == 0) {
1552 1.1 christos expect_paramfile = 1;
1553 1.1 christos if (p == NULL) {
1554 1.1 christos BIO_printf(bio_err,
1555 1.1.1.2 christos "Parameter file requested but no path given: %s\n",
1556 1.1.1.2 christos gstr);
1557 1.1 christos return NULL;
1558 1.1 christos }
1559 1.1 christos } else {
1560 1.1 christos keytype = gstr;
1561 1.1 christos keytypelen = len;
1562 1.1 christos }
1563 1.1 christos
1564 1.1 christos if (p != NULL)
1565 1.1 christos gstr = gstr + len + 1;
1566 1.1 christos else
1567 1.1 christos gstr = NULL;
1568 1.1 christos }
1569 1.1 christos
1570 1.1 christos /* Treat the second part of gstr, if there is one */
1571 1.1 christos if (gstr != NULL) {
1572 1.1 christos /* If the second part starts with a digit, we assume it's a size */
1573 1.1 christos if (!expect_paramfile && gstr[0] >= '0' && gstr[0] <= '9')
1574 1.1 christos keylen = atol(gstr);
1575 1.1 christos else
1576 1.1 christos paramfile = gstr;
1577 1.1 christos }
1578 1.1 christos
1579 1.1 christos if (paramfile != NULL) {
1580 1.1 christos pbio = BIO_new_file(paramfile, "r");
1581 1.1 christos if (pbio == NULL) {
1582 1.1 christos BIO_printf(bio_err, "Cannot open parameter file %s\n", paramfile);
1583 1.1 christos return NULL;
1584 1.1 christos }
1585 1.1 christos param = PEM_read_bio_Parameters(pbio, NULL);
1586 1.1 christos
1587 1.1 christos if (param == NULL) {
1588 1.1 christos X509 *x;
1589 1.1 christos
1590 1.1 christos (void)BIO_reset(pbio);
1591 1.1 christos x = PEM_read_bio_X509(pbio, NULL, NULL, NULL);
1592 1.1 christos if (x != NULL) {
1593 1.1 christos param = X509_get_pubkey(x);
1594 1.1 christos X509_free(x);
1595 1.1 christos }
1596 1.1 christos }
1597 1.1 christos
1598 1.1 christos BIO_free(pbio);
1599 1.1 christos
1600 1.1 christos if (param == NULL) {
1601 1.1 christos BIO_printf(bio_err, "Error reading parameter file %s\n", paramfile);
1602 1.1 christos return NULL;
1603 1.1 christos }
1604 1.1 christos if (keytype == NULL) {
1605 1.1 christos keytype = EVP_PKEY_get0_type_name(param);
1606 1.1 christos if (keytype == NULL) {
1607 1.1 christos EVP_PKEY_free(param);
1608 1.1 christos BIO_puts(bio_err, "Unable to determine key type\n");
1609 1.1 christos return NULL;
1610 1.1 christos }
1611 1.1 christos }
1612 1.1 christos }
1613 1.1 christos
1614 1.1 christos if (keytypelen > 0)
1615 1.1 christos *pkeytype = OPENSSL_strndup(keytype, keytypelen);
1616 1.1 christos else
1617 1.1 christos *pkeytype = OPENSSL_strdup(keytype);
1618 1.1 christos
1619 1.1 christos if (*pkeytype == NULL) {
1620 1.1 christos BIO_printf(bio_err, "Out of memory\n");
1621 1.1 christos EVP_PKEY_free(param);
1622 1.1 christos return NULL;
1623 1.1 christos }
1624 1.1 christos
1625 1.1 christos if (keylen >= 0)
1626 1.1 christos *pkeylen = keylen;
1627 1.1 christos
1628 1.1 christos if (param != NULL) {
1629 1.1 christos if (!EVP_PKEY_is_a(param, *pkeytype)) {
1630 1.1 christos BIO_printf(bio_err, "Key type does not match parameters\n");
1631 1.1 christos EVP_PKEY_free(param);
1632 1.1 christos return NULL;
1633 1.1 christos }
1634 1.1 christos
1635 1.1 christos if (keygen_engine != NULL)
1636 1.1 christos gctx = EVP_PKEY_CTX_new(param, keygen_engine);
1637 1.1 christos else
1638 1.1 christos gctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
1639 1.1.1.2 christos param, app_get0_propq());
1640 1.1 christos *pkeylen = EVP_PKEY_get_bits(param);
1641 1.1 christos EVP_PKEY_free(param);
1642 1.1 christos } else {
1643 1.1 christos if (keygen_engine != NULL) {
1644 1.1 christos int pkey_id = get_legacy_pkey_id(app_get0_libctx(), *pkeytype,
1645 1.1.1.2 christos keygen_engine);
1646 1.1 christos
1647 1.1 christos if (pkey_id != NID_undef)
1648 1.1 christos gctx = EVP_PKEY_CTX_new_id(pkey_id, keygen_engine);
1649 1.1 christos } else {
1650 1.1 christos gctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(),
1651 1.1.1.2 christos *pkeytype, app_get0_propq());
1652 1.1 christos }
1653 1.1 christos }
1654 1.1 christos
1655 1.1 christos if (gctx == NULL) {
1656 1.1 christos BIO_puts(bio_err, "Error allocating keygen context\n");
1657 1.1 christos return NULL;
1658 1.1 christos }
1659 1.1 christos
1660 1.1 christos if (EVP_PKEY_keygen_init(gctx) <= 0) {
1661 1.1 christos BIO_puts(bio_err, "Error initializing keygen context\n");
1662 1.1 christos EVP_PKEY_CTX_free(gctx);
1663 1.1 christos return NULL;
1664 1.1 christos }
1665 1.1.1.2 christos if (keylen == -1 && (EVP_PKEY_CTX_is_a(gctx, "RSA") || EVP_PKEY_CTX_is_a(gctx, "RSA-PSS")))
1666 1.1 christos keylen = *pkeylen;
1667 1.1 christos
1668 1.1 christos if (keylen != -1) {
1669 1.1 christos OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1670 1.1 christos size_t bits = keylen;
1671 1.1 christos
1672 1.1.1.2 christos params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_BITS, &bits);
1673 1.1 christos if (EVP_PKEY_CTX_set_params(gctx, params) <= 0) {
1674 1.1 christos BIO_puts(bio_err, "Error setting keysize\n");
1675 1.1 christos EVP_PKEY_CTX_free(gctx);
1676 1.1 christos return NULL;
1677 1.1 christos }
1678 1.1 christos }
1679 1.1 christos
1680 1.1 christos return gctx;
1681 1.1 christos }
1682