]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
a661b653 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
a661b653 | 8 | */ |
d02b48c6 | 9 | |
fc7dae52 | 10 | #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS) |
0f113f3e MC |
11 | /* |
12 | * On VMS, you need to define this to get the declaration of fileno(). The | |
13 | * value 2 is to make sure no function defined in POSIX-2 is left undefined. | |
68d39f3c | 14 | */ |
0f113f3e | 15 | # define _POSIX_C_SOURCE 2 |
83d8fa7d | 16 | #endif |
75dd6c1a | 17 | |
d02b48c6 RE |
18 | #include <stdio.h> |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
b379fe6c | 21 | #include <sys/types.h> |
a412b891 RL |
22 | #ifndef OPENSSL_NO_POSIX_IO |
23 | # include <sys/stat.h> | |
24 | # include <fcntl.h> | |
25 | #endif | |
d652a095 | 26 | #include <ctype.h> |
a1ad253f | 27 | #include <errno.h> |
90ae4673 RL |
28 | #include <openssl/err.h> |
29 | #include <openssl/x509.h> | |
535d79da | 30 | #include <openssl/x509v3.h> |
90ae4673 RL |
31 | #include <openssl/pem.h> |
32 | #include <openssl/pkcs12.h> | |
2fe5adc3 | 33 | #include <openssl/ui.h> |
90ae4673 | 34 | #include <openssl/safestack.h> |
0b13e9f0 | 35 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 36 | # include <openssl/engine.h> |
0b13e9f0 | 37 | #endif |
3eeaab4b | 38 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 39 | # include <openssl/rsa.h> |
3eeaab4b | 40 | #endif |
f0eae953 | 41 | #include <openssl/bn.h> |
7e1b7485 | 42 | #include <openssl/ssl.h> |
d610d27f | 43 | #include "apps.h" |
d610d27f | 44 | |
a1ad253f AP |
45 | #ifdef _WIN32 |
46 | static int WIN32_rename(const char *from, const char *to); | |
0f113f3e | 47 | # define rename(from,to) WIN32_rename((from),(to)) |
a1ad253f AP |
48 | #endif |
49 | ||
d0cf719e DMSP |
50 | #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) |
51 | # include <conio.h> | |
52 | #endif | |
53 | ||
54 | #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32) | |
55 | # define _kbhit kbhit | |
56 | #endif | |
57 | ||
62ca1565 DO |
58 | #define PASS_SOURCE_SIZE_MAX 4 |
59 | ||
852c2ed2 RS |
60 | DEFINE_STACK_OF(CONF) |
61 | DEFINE_STACK_OF(CONF_VALUE) | |
62 | DEFINE_STACK_OF(X509) | |
63 | DEFINE_STACK_OF(X509_CRL) | |
64 | DEFINE_STACK_OF(X509_INFO) | |
65 | DEFINE_STACK_OF(X509_EXTENSION) | |
66 | DEFINE_STACK_OF(X509_POLICY_NODE) | |
67 | DEFINE_STACK_OF(GENERAL_NAME) | |
68 | DEFINE_STACK_OF(DIST_POINT) | |
69 | DEFINE_STACK_OF_STRING() | |
70 | ||
8ca533e3 | 71 | typedef struct { |
0f113f3e MC |
72 | const char *name; |
73 | unsigned long flag; | |
74 | unsigned long mask; | |
8ca533e3 DSH |
75 | } NAME_EX_TBL; |
76 | ||
0f113f3e MC |
77 | static int set_table_opts(unsigned long *flags, const char *arg, |
78 | const NAME_EX_TBL * in_tbl); | |
79 | static int set_multi_opts(unsigned long *flags, const char *arg, | |
80 | const NAME_EX_TBL * in_tbl); | |
8ca533e3 | 81 | |
d02b48c6 | 82 | int app_init(long mesgwin); |
0f113f3e | 83 | |
7e1b7485 | 84 | int chopup_args(ARGS *arg, char *buf) |
0f113f3e | 85 | { |
7e1b7485 | 86 | int quoted; |
4c9b0a03 | 87 | char c = '\0', *p = NULL; |
0f113f3e | 88 | |
7e1b7485 RS |
89 | arg->argc = 0; |
90 | if (arg->size == 0) { | |
91 | arg->size = 20; | |
b4faea50 | 92 | arg->argv = app_malloc(sizeof(*arg->argv) * arg->size, "argv space"); |
0f113f3e | 93 | } |
0f113f3e | 94 | |
7e1b7485 RS |
95 | for (p = buf;;) { |
96 | /* Skip whitespace. */ | |
18295f0c | 97 | while (*p && isspace(_UC(*p))) |
0f113f3e | 98 | p++; |
12a765a5 | 99 | if (*p == '\0') |
0f113f3e MC |
100 | break; |
101 | ||
102 | /* The start of something good :-) */ | |
7e1b7485 | 103 | if (arg->argc >= arg->size) { |
7c0ef843 | 104 | char **tmp; |
7e1b7485 | 105 | arg->size += 20; |
7c0ef843 DSH |
106 | tmp = OPENSSL_realloc(arg->argv, sizeof(*arg->argv) * arg->size); |
107 | if (tmp == NULL) | |
0f113f3e | 108 | return 0; |
7c0ef843 | 109 | arg->argv = tmp; |
0f113f3e | 110 | } |
7e1b7485 RS |
111 | quoted = *p == '\'' || *p == '"'; |
112 | if (quoted) | |
113 | c = *p++; | |
114 | arg->argv[arg->argc++] = p; | |
0f113f3e MC |
115 | |
116 | /* now look for the end of this */ | |
7e1b7485 RS |
117 | if (quoted) { |
118 | while (*p && *p != c) | |
0f113f3e | 119 | p++; |
7e1b7485 | 120 | *p++ = '\0'; |
0f113f3e | 121 | } else { |
18295f0c | 122 | while (*p && !isspace(_UC(*p))) |
0f113f3e | 123 | p++; |
7e1b7485 RS |
124 | if (*p) |
125 | *p++ = '\0'; | |
0f113f3e | 126 | } |
0f113f3e | 127 | } |
7e1b7485 | 128 | arg->argv[arg->argc] = NULL; |
208fb891 | 129 | return 1; |
0f113f3e | 130 | } |
d02b48c6 RE |
131 | |
132 | #ifndef APP_INIT | |
6b691a5c | 133 | int app_init(long mesgwin) |
0f113f3e | 134 | { |
208fb891 | 135 | return 1; |
0f113f3e | 136 | } |
d02b48c6 | 137 | #endif |
53b1899e | 138 | |
fd3397fc RL |
139 | int ctx_set_verify_locations(SSL_CTX *ctx, |
140 | const char *CAfile, int noCAfile, | |
141 | const char *CApath, int noCApath, | |
142 | const char *CAstore, int noCAstore) | |
7e1b7485 | 143 | { |
fd3397fc | 144 | if (CAfile == NULL && CApath == NULL && CAstore == NULL) { |
2b6bcb70 MC |
145 | if (!noCAfile && SSL_CTX_set_default_verify_file(ctx) <= 0) |
146 | return 0; | |
147 | if (!noCApath && SSL_CTX_set_default_verify_dir(ctx) <= 0) | |
148 | return 0; | |
fd3397fc RL |
149 | if (!noCAstore && SSL_CTX_set_default_verify_store(ctx) <= 0) |
150 | return 0; | |
2b6bcb70 MC |
151 | |
152 | return 1; | |
153 | } | |
fd3397fc RL |
154 | |
155 | if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile)) | |
156 | return 0; | |
157 | if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath)) | |
158 | return 0; | |
159 | if (CAstore != NULL && !SSL_CTX_load_verify_store(ctx, CAstore)) | |
160 | return 0; | |
161 | return 1; | |
7e1b7485 RS |
162 | } |
163 | ||
b5369582 RP |
164 | #ifndef OPENSSL_NO_CT |
165 | ||
dd696a55 RP |
166 | int ctx_set_ctlog_list_file(SSL_CTX *ctx, const char *path) |
167 | { | |
2234212c | 168 | if (path == NULL) |
328f36c5 | 169 | return SSL_CTX_set_default_ctlog_list_file(ctx); |
dd696a55 RP |
170 | |
171 | return SSL_CTX_set_ctlog_list_file(ctx, path); | |
172 | } | |
173 | ||
b5369582 RP |
174 | #endif |
175 | ||
b5c4209b DB |
176 | static unsigned long nmflag = 0; |
177 | static char nmflag_set = 0; | |
178 | ||
179 | int set_nameopt(const char *arg) | |
954ef7ef | 180 | { |
b5c4209b DB |
181 | int ret = set_name_ex(&nmflag, arg); |
182 | ||
183 | if (ret) | |
184 | nmflag_set = 1; | |
185 | ||
186 | return ret; | |
187 | } | |
54a656ef | 188 | |
b5c4209b DB |
189 | unsigned long get_nameopt(void) |
190 | { | |
191 | return (nmflag_set) ? nmflag : XN_FLAG_ONELINE; | |
192 | } | |
954ef7ef | 193 | |
b5c4209b DB |
194 | int dump_cert_text(BIO *out, X509 *x) |
195 | { | |
196 | print_name(out, "subject=", X509_get_subject_name(x), get_nameopt()); | |
197 | BIO_puts(out, "\n"); | |
198 | print_name(out, "issuer=", X509_get_issuer_name(x), get_nameopt()); | |
0f113f3e | 199 | BIO_puts(out, "\n"); |
54a656ef | 200 | |
0f113f3e | 201 | return 0; |
954ef7ef | 202 | } |
a3fe382e | 203 | |
229446df RS |
204 | int wrap_password_callback(char *buf, int bufsiz, int verify, void *userdata) |
205 | { | |
206 | return password_callback(buf, bufsiz, verify, (PW_CB_DATA *)userdata); | |
207 | } | |
208 | ||
a43ce58f | 209 | |
cc696296 | 210 | static char *app_get_pass(const char *arg, int keepbio); |
a3fe382e | 211 | |
cc696296 | 212 | int app_passwd(const char *arg1, const char *arg2, char **pass1, char **pass2) |
a3fe382e | 213 | { |
229446df RS |
214 | int same = arg1 != NULL && arg2 != NULL && strcmp(arg1, arg2) == 0; |
215 | ||
2234212c | 216 | if (arg1 != NULL) { |
7e1b7485 | 217 | *pass1 = app_get_pass(arg1, same); |
2234212c | 218 | if (*pass1 == NULL) |
0f113f3e | 219 | return 0; |
2234212c | 220 | } else if (pass1 != NULL) { |
0f113f3e | 221 | *pass1 = NULL; |
2234212c PY |
222 | } |
223 | if (arg2 != NULL) { | |
7e1b7485 | 224 | *pass2 = app_get_pass(arg2, same ? 2 : 0); |
2234212c | 225 | if (*pass2 == NULL) |
0f113f3e | 226 | return 0; |
2234212c | 227 | } else if (pass2 != NULL) { |
0f113f3e | 228 | *pass2 = NULL; |
2234212c | 229 | } |
0f113f3e | 230 | return 1; |
a3fe382e DSH |
231 | } |
232 | ||
cc696296 | 233 | static char *app_get_pass(const char *arg, int keepbio) |
a3fe382e | 234 | { |
0f113f3e | 235 | static BIO *pwdbio = NULL; |
229446df | 236 | char *tmp, tpass[APP_PASS_LEN]; |
0f113f3e | 237 | int i; |
86885c28 | 238 | |
62ca1565 | 239 | /* PASS_SOURCE_SIZE_MAX = max number of chars before ':' in below strings */ |
86885c28 | 240 | if (strncmp(arg, "pass:", 5) == 0) |
7644a9ae | 241 | return OPENSSL_strdup(arg + 5); |
86885c28 | 242 | if (strncmp(arg, "env:", 4) == 0) { |
0f113f3e | 243 | tmp = getenv(arg + 4); |
2234212c | 244 | if (tmp == NULL) { |
229446df | 245 | BIO_printf(bio_err, "No environment variable %s\n", arg + 4); |
0f113f3e MC |
246 | return NULL; |
247 | } | |
7644a9ae | 248 | return OPENSSL_strdup(tmp); |
0f113f3e | 249 | } |
2234212c | 250 | if (!keepbio || pwdbio == NULL) { |
86885c28 | 251 | if (strncmp(arg, "file:", 5) == 0) { |
0f113f3e | 252 | pwdbio = BIO_new_file(arg + 5, "r"); |
2234212c | 253 | if (pwdbio == NULL) { |
7e1b7485 | 254 | BIO_printf(bio_err, "Can't open file %s\n", arg + 5); |
0f113f3e MC |
255 | return NULL; |
256 | } | |
eff7cb41 | 257 | #if !defined(_WIN32) |
0f113f3e MC |
258 | /* |
259 | * Under _WIN32, which covers even Win64 and CE, file | |
260 | * descriptors referenced by BIO_s_fd are not inherited | |
261 | * by child process and therefore below is not an option. | |
262 | * It could have been an option if bss_fd.c was operating | |
263 | * on real Windows descriptors, such as those obtained | |
264 | * with CreateFile. | |
265 | */ | |
86885c28 | 266 | } else if (strncmp(arg, "fd:", 3) == 0) { |
0f113f3e MC |
267 | BIO *btmp; |
268 | i = atoi(arg + 3); | |
269 | if (i >= 0) | |
270 | pwdbio = BIO_new_fd(i, BIO_NOCLOSE); | |
271 | if ((i < 0) || !pwdbio) { | |
7e1b7485 | 272 | BIO_printf(bio_err, "Can't access file descriptor %s\n", arg + 3); |
0f113f3e MC |
273 | return NULL; |
274 | } | |
275 | /* | |
276 | * Can't do BIO_gets on an fd BIO so add a buffering BIO | |
277 | */ | |
278 | btmp = BIO_new(BIO_f_buffer()); | |
279 | pwdbio = BIO_push(btmp, pwdbio); | |
280 | #endif | |
86885c28 | 281 | } else if (strcmp(arg, "stdin") == 0) { |
a60994df | 282 | pwdbio = dup_bio_in(FORMAT_TEXT); |
12a765a5 | 283 | if (pwdbio == NULL) { |
7e1b7485 | 284 | BIO_printf(bio_err, "Can't open BIO for stdin\n"); |
0f113f3e MC |
285 | return NULL; |
286 | } | |
287 | } else { | |
62ca1565 DO |
288 | /* argument syntax error; do not reveal too much about arg */ |
289 | tmp = strchr(arg, ':'); | |
290 | if (tmp == NULL || tmp - arg > PASS_SOURCE_SIZE_MAX) | |
291 | BIO_printf(bio_err, | |
292 | "Invalid password argument, missing ':' within the first %d chars\n", | |
293 | PASS_SOURCE_SIZE_MAX + 1); | |
294 | else | |
295 | BIO_printf(bio_err, | |
296 | "Invalid password argument, starting with \"%.*s\"\n", | |
297 | (int)(tmp - arg + 1), arg); | |
0f113f3e MC |
298 | return NULL; |
299 | } | |
300 | } | |
301 | i = BIO_gets(pwdbio, tpass, APP_PASS_LEN); | |
302 | if (keepbio != 1) { | |
303 | BIO_free_all(pwdbio); | |
304 | pwdbio = NULL; | |
305 | } | |
306 | if (i <= 0) { | |
7e1b7485 | 307 | BIO_printf(bio_err, "Error reading password from BIO\n"); |
0f113f3e MC |
308 | return NULL; |
309 | } | |
310 | tmp = strchr(tpass, '\n'); | |
2234212c | 311 | if (tmp != NULL) |
0f113f3e | 312 | *tmp = 0; |
7644a9ae | 313 | return OPENSSL_strdup(tpass); |
a3fe382e | 314 | } |
90ae4673 | 315 | |
bfa470a4 | 316 | CONF *app_load_config_bio(BIO *in, const char *filename) |
cc01d217 RS |
317 | { |
318 | long errorline = -1; | |
319 | CONF *conf; | |
320 | int i; | |
cc01d217 RS |
321 | |
322 | conf = NCONF_new(NULL); | |
323 | i = NCONF_load_bio(conf, in, &errorline); | |
cc01d217 RS |
324 | if (i > 0) |
325 | return conf; | |
326 | ||
bfa470a4 RL |
327 | if (errorline <= 0) { |
328 | BIO_printf(bio_err, "%s: Can't load ", opt_getprog()); | |
329 | } else { | |
330 | BIO_printf(bio_err, "%s: Error on line %ld of ", opt_getprog(), | |
331 | errorline); | |
332 | } | |
333 | if (filename != NULL) | |
334 | BIO_printf(bio_err, "config file \"%s\"\n", filename); | |
cc01d217 | 335 | else |
bfa470a4 RL |
336 | BIO_printf(bio_err, "config input"); |
337 | ||
cc01d217 RS |
338 | NCONF_free(conf); |
339 | return NULL; | |
340 | } | |
2234212c | 341 | |
296f54ee RL |
342 | CONF *app_load_config(const char *filename) |
343 | { | |
344 | BIO *in; | |
345 | CONF *conf; | |
346 | ||
bdd58d98 | 347 | in = bio_open_default(filename, 'r', FORMAT_TEXT); |
296f54ee RL |
348 | if (in == NULL) |
349 | return NULL; | |
350 | ||
bfa470a4 | 351 | conf = app_load_config_bio(in, filename); |
296f54ee RL |
352 | BIO_free(in); |
353 | return conf; | |
354 | } | |
2234212c | 355 | |
296f54ee RL |
356 | CONF *app_load_config_quiet(const char *filename) |
357 | { | |
358 | BIO *in; | |
359 | CONF *conf; | |
360 | ||
bdd58d98 | 361 | in = bio_open_default_quiet(filename, 'r', FORMAT_TEXT); |
296f54ee RL |
362 | if (in == NULL) |
363 | return NULL; | |
364 | ||
bfa470a4 | 365 | conf = app_load_config_bio(in, filename); |
296f54ee RL |
366 | BIO_free(in); |
367 | return conf; | |
368 | } | |
369 | ||
370 | int app_load_modules(const CONF *config) | |
371 | { | |
372 | CONF *to_free = NULL; | |
373 | ||
374 | if (config == NULL) | |
dccd20d1 | 375 | config = to_free = app_load_config_quiet(default_config_file); |
296f54ee | 376 | if (config == NULL) |
dccd20d1 | 377 | return 1; |
296f54ee RL |
378 | |
379 | if (CONF_modules_load(config, NULL, 0) <= 0) { | |
380 | BIO_printf(bio_err, "Error configuring OpenSSL modules\n"); | |
381 | ERR_print_errors(bio_err); | |
382 | NCONF_free(to_free); | |
383 | return 0; | |
384 | } | |
385 | NCONF_free(to_free); | |
386 | return 1; | |
387 | } | |
cc01d217 | 388 | |
7e1b7485 | 389 | int add_oid_section(CONF *conf) |
0f113f3e MC |
390 | { |
391 | char *p; | |
392 | STACK_OF(CONF_VALUE) *sktmp; | |
393 | CONF_VALUE *cnf; | |
394 | int i; | |
75ebbd9a RS |
395 | |
396 | if ((p = NCONF_get_string(conf, NULL, "oid_section")) == NULL) { | |
0f113f3e MC |
397 | ERR_clear_error(); |
398 | return 1; | |
399 | } | |
75ebbd9a | 400 | if ((sktmp = NCONF_get_section(conf, p)) == NULL) { |
7e1b7485 | 401 | BIO_printf(bio_err, "problem loading oid section %s\n", p); |
0f113f3e MC |
402 | return 0; |
403 | } | |
404 | for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { | |
405 | cnf = sk_CONF_VALUE_value(sktmp, i); | |
406 | if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) { | |
7e1b7485 | 407 | BIO_printf(bio_err, "problem creating object %s=%s\n", |
0f113f3e MC |
408 | cnf->name, cnf->value); |
409 | return 0; | |
410 | } | |
411 | } | |
412 | return 1; | |
431b0cce RL |
413 | } |
414 | ||
7e1b7485 | 415 | static int load_pkcs12(BIO *in, const char *desc, |
229446df | 416 | pem_password_cb *pem_cb, PW_CB_DATA *cb_data, |
0f113f3e MC |
417 | EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca) |
418 | { | |
419 | const char *pass; | |
420 | char tpass[PEM_BUFSIZE]; | |
421 | int len, ret = 0; | |
422 | PKCS12 *p12; | |
423 | p12 = d2i_PKCS12_bio(in, NULL); | |
424 | if (p12 == NULL) { | |
7e1b7485 | 425 | BIO_printf(bio_err, "Error loading PKCS12 file for %s\n", desc); |
0f113f3e MC |
426 | goto die; |
427 | } | |
428 | /* See if an empty password will do */ | |
2234212c | 429 | if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0)) { |
0f113f3e | 430 | pass = ""; |
2234212c | 431 | } else { |
12a765a5 | 432 | if (pem_cb == NULL) |
0f113f3e MC |
433 | pem_cb = (pem_password_cb *)password_callback; |
434 | len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data); | |
435 | if (len < 0) { | |
7e1b7485 | 436 | BIO_printf(bio_err, "Passphrase callback error for %s\n", desc); |
0f113f3e MC |
437 | goto die; |
438 | } | |
439 | if (len < PEM_BUFSIZE) | |
440 | tpass[len] = 0; | |
441 | if (!PKCS12_verify_mac(p12, tpass, len)) { | |
7e1b7485 | 442 | BIO_printf(bio_err, |
0f113f3e MC |
443 | "Mac verify error (wrong password?) in PKCS12 file for %s\n", |
444 | desc); | |
445 | goto die; | |
446 | } | |
447 | pass = tpass; | |
448 | } | |
449 | ret = PKCS12_parse(p12, pass, pkey, cert, ca); | |
450 | die: | |
e0e920b1 | 451 | PKCS12_free(p12); |
0f113f3e MC |
452 | return ret; |
453 | } | |
e90fadda | 454 | |
a773b52a | 455 | X509 *load_cert(const char *file, int format, const char *cert_descrip) |
0f113f3e MC |
456 | { |
457 | X509 *x = NULL; | |
458 | BIO *cert; | |
459 | ||
460 | if (format == FORMAT_HTTP) { | |
29f178bd DDO |
461 | #if !defined(OPENSSL_NO_SOCK) |
462 | x = X509_load_http(file, NULL, NULL, 0 /* timeout */); | |
5d322287 | 463 | #endif |
0f113f3e MC |
464 | return x; |
465 | } | |
466 | ||
0f113f3e | 467 | if (file == NULL) { |
7e1b7485 | 468 | unbuffer(stdin); |
a60994df | 469 | cert = dup_bio_in(format); |
2234212c | 470 | } else { |
bdd58d98 | 471 | cert = bio_open_default(file, 'r', format); |
2234212c | 472 | } |
7e1b7485 RS |
473 | if (cert == NULL) |
474 | goto end; | |
0f113f3e | 475 | |
2234212c | 476 | if (format == FORMAT_ASN1) { |
0f113f3e | 477 | x = d2i_X509_bio(cert, NULL); |
2234212c | 478 | } else if (format == FORMAT_PEM) { |
0f113f3e MC |
479 | x = PEM_read_bio_X509_AUX(cert, NULL, |
480 | (pem_password_cb *)password_callback, NULL); | |
2234212c | 481 | } else if (format == FORMAT_PKCS12) { |
7e1b7485 | 482 | if (!load_pkcs12(cert, cert_descrip, NULL, NULL, NULL, &x, NULL)) |
0f113f3e MC |
483 | goto end; |
484 | } else { | |
7e1b7485 | 485 | BIO_printf(bio_err, "bad input format specified for %s\n", cert_descrip); |
0f113f3e MC |
486 | goto end; |
487 | } | |
488 | end: | |
489 | if (x == NULL) { | |
7e1b7485 RS |
490 | BIO_printf(bio_err, "unable to load certificate\n"); |
491 | ERR_print_errors(bio_err); | |
0f113f3e | 492 | } |
25aaa98a | 493 | BIO_free(cert); |
26a7d938 | 494 | return x; |
0f113f3e | 495 | } |
90ae4673 | 496 | |
0090a686 | 497 | X509_CRL *load_crl(const char *infile, int format) |
0f113f3e MC |
498 | { |
499 | X509_CRL *x = NULL; | |
500 | BIO *in = NULL; | |
501 | ||
502 | if (format == FORMAT_HTTP) { | |
29f178bd DDO |
503 | #if !defined(OPENSSL_NO_SOCK) |
504 | x = X509_CRL_load_http(infile, NULL, NULL, 0 /* timeout */); | |
5d322287 | 505 | #endif |
0f113f3e MC |
506 | return x; |
507 | } | |
508 | ||
bdd58d98 | 509 | in = bio_open_default(infile, 'r', format); |
7e1b7485 | 510 | if (in == NULL) |
0f113f3e | 511 | goto end; |
2234212c | 512 | if (format == FORMAT_ASN1) { |
0f113f3e | 513 | x = d2i_X509_CRL_bio(in, NULL); |
2234212c | 514 | } else if (format == FORMAT_PEM) { |
0f113f3e | 515 | x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); |
2234212c | 516 | } else { |
0f113f3e MC |
517 | BIO_printf(bio_err, "bad input format specified for input crl\n"); |
518 | goto end; | |
519 | } | |
520 | if (x == NULL) { | |
521 | BIO_printf(bio_err, "unable to load CRL\n"); | |
522 | ERR_print_errors(bio_err); | |
523 | goto end; | |
524 | } | |
fdb78f3d | 525 | |
0f113f3e MC |
526 | end: |
527 | BIO_free(in); | |
26a7d938 | 528 | return x; |
0f113f3e | 529 | } |
fdb78f3d | 530 | |
7e1b7485 | 531 | EVP_PKEY *load_key(const char *file, int format, int maybe_stdin, |
0f113f3e MC |
532 | const char *pass, ENGINE *e, const char *key_descrip) |
533 | { | |
534 | BIO *key = NULL; | |
535 | EVP_PKEY *pkey = NULL; | |
536 | PW_CB_DATA cb_data; | |
537 | ||
538 | cb_data.password = pass; | |
539 | cb_data.prompt_info = file; | |
540 | ||
541 | if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) { | |
7e1b7485 | 542 | BIO_printf(bio_err, "no keyfile specified\n"); |
0f113f3e MC |
543 | goto end; |
544 | } | |
0f113f3e | 545 | if (format == FORMAT_ENGINE) { |
2234212c | 546 | if (e == NULL) { |
7e1b7485 | 547 | BIO_printf(bio_err, "no engine specified\n"); |
2234212c | 548 | } else { |
0c20802c | 549 | #ifndef OPENSSL_NO_ENGINE |
49e476a5 | 550 | if (ENGINE_init(e)) { |
a43ce58f SL |
551 | pkey = ENGINE_load_private_key(e, file, |
552 | (UI_METHOD *)get_ui_method(), | |
553 | &cb_data); | |
49e476a5 RL |
554 | ENGINE_finish(e); |
555 | } | |
0c20802c | 556 | if (pkey == NULL) { |
7e1b7485 RS |
557 | BIO_printf(bio_err, "cannot load %s from engine\n", key_descrip); |
558 | ERR_print_errors(bio_err); | |
0f113f3e | 559 | } |
0c20802c VD |
560 | #else |
561 | BIO_printf(bio_err, "engines not supported\n"); | |
562 | #endif | |
0f113f3e MC |
563 | } |
564 | goto end; | |
565 | } | |
0f113f3e | 566 | if (file == NULL && maybe_stdin) { |
7e1b7485 | 567 | unbuffer(stdin); |
a60994df | 568 | key = dup_bio_in(format); |
2234212c | 569 | } else { |
bdd58d98 | 570 | key = bio_open_default(file, 'r', format); |
2234212c | 571 | } |
7e1b7485 | 572 | if (key == NULL) |
0f113f3e | 573 | goto end; |
0f113f3e MC |
574 | if (format == FORMAT_ASN1) { |
575 | pkey = d2i_PrivateKey_bio(key, NULL); | |
576 | } else if (format == FORMAT_PEM) { | |
229446df | 577 | pkey = PEM_read_bio_PrivateKey(key, NULL, wrap_password_callback, &cb_data); |
2234212c | 578 | } else if (format == FORMAT_PKCS12) { |
229446df | 579 | if (!load_pkcs12(key, key_descrip, wrap_password_callback, &cb_data, |
0f113f3e MC |
580 | &pkey, NULL, NULL)) |
581 | goto end; | |
00a37b5a | 582 | #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4) |
2234212c | 583 | } else if (format == FORMAT_MSBLOB) { |
0f113f3e | 584 | pkey = b2i_PrivateKey_bio(key); |
2234212c | 585 | } else if (format == FORMAT_PVK) { |
229446df | 586 | pkey = b2i_PVK_bio(key, wrap_password_callback, &cb_data); |
0f113f3e | 587 | #endif |
2234212c | 588 | } else { |
7e1b7485 | 589 | BIO_printf(bio_err, "bad input format specified for key file\n"); |
0f113f3e MC |
590 | goto end; |
591 | } | |
90ae4673 | 592 | end: |
25aaa98a | 593 | BIO_free(key); |
0f113f3e | 594 | if (pkey == NULL) { |
7e1b7485 RS |
595 | BIO_printf(bio_err, "unable to load %s\n", key_descrip); |
596 | ERR_print_errors(bio_err); | |
0f113f3e | 597 | } |
26a7d938 | 598 | return pkey; |
0f113f3e | 599 | } |
90ae4673 | 600 | |
7e1b7485 | 601 | EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin, |
0f113f3e MC |
602 | const char *pass, ENGINE *e, const char *key_descrip) |
603 | { | |
604 | BIO *key = NULL; | |
605 | EVP_PKEY *pkey = NULL; | |
606 | PW_CB_DATA cb_data; | |
607 | ||
608 | cb_data.password = pass; | |
609 | cb_data.prompt_info = file; | |
610 | ||
611 | if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) { | |
7e1b7485 | 612 | BIO_printf(bio_err, "no keyfile specified\n"); |
0f113f3e MC |
613 | goto end; |
614 | } | |
0f113f3e | 615 | if (format == FORMAT_ENGINE) { |
2234212c | 616 | if (e == NULL) { |
0f113f3e | 617 | BIO_printf(bio_err, "no engine specified\n"); |
2234212c | 618 | } else { |
0c20802c | 619 | #ifndef OPENSSL_NO_ENGINE |
a43ce58f SL |
620 | pkey = ENGINE_load_public_key(e, file, (UI_METHOD *)get_ui_method(), |
621 | &cb_data); | |
0c20802c VD |
622 | if (pkey == NULL) { |
623 | BIO_printf(bio_err, "cannot load %s from engine\n", key_descrip); | |
624 | ERR_print_errors(bio_err); | |
625 | } | |
626 | #else | |
627 | BIO_printf(bio_err, "engines not supported\n"); | |
628 | #endif | |
629 | } | |
0f113f3e MC |
630 | goto end; |
631 | } | |
0f113f3e | 632 | if (file == NULL && maybe_stdin) { |
7e1b7485 | 633 | unbuffer(stdin); |
a60994df | 634 | key = dup_bio_in(format); |
2234212c | 635 | } else { |
bdd58d98 | 636 | key = bio_open_default(file, 'r', format); |
2234212c | 637 | } |
7e1b7485 | 638 | if (key == NULL) |
0f113f3e | 639 | goto end; |
0f113f3e MC |
640 | if (format == FORMAT_ASN1) { |
641 | pkey = d2i_PUBKEY_bio(key, NULL); | |
2234212c | 642 | } else if (format == FORMAT_ASN1RSA) { |
0c20802c | 643 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
644 | RSA *rsa; |
645 | rsa = d2i_RSAPublicKey_bio(key, NULL); | |
646 | if (rsa) { | |
647 | pkey = EVP_PKEY_new(); | |
96487cdd | 648 | if (pkey != NULL) |
0f113f3e MC |
649 | EVP_PKEY_set1_RSA(pkey, rsa); |
650 | RSA_free(rsa); | |
651 | } else | |
0c20802c VD |
652 | #else |
653 | BIO_printf(bio_err, "RSA keys not supported\n"); | |
654 | #endif | |
0f113f3e MC |
655 | pkey = NULL; |
656 | } else if (format == FORMAT_PEMRSA) { | |
0c20802c | 657 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
658 | RSA *rsa; |
659 | rsa = PEM_read_bio_RSAPublicKey(key, NULL, | |
660 | (pem_password_cb *)password_callback, | |
661 | &cb_data); | |
96487cdd | 662 | if (rsa != NULL) { |
0f113f3e | 663 | pkey = EVP_PKEY_new(); |
96487cdd | 664 | if (pkey != NULL) |
0f113f3e MC |
665 | EVP_PKEY_set1_RSA(pkey, rsa); |
666 | RSA_free(rsa); | |
667 | } else | |
0c20802c VD |
668 | #else |
669 | BIO_printf(bio_err, "RSA keys not supported\n"); | |
670 | #endif | |
0f113f3e | 671 | pkey = NULL; |
2234212c | 672 | } else if (format == FORMAT_PEM) { |
0f113f3e MC |
673 | pkey = PEM_read_bio_PUBKEY(key, NULL, |
674 | (pem_password_cb *)password_callback, | |
675 | &cb_data); | |
d4f0339c | 676 | #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) |
2234212c | 677 | } else if (format == FORMAT_MSBLOB) { |
0f113f3e | 678 | pkey = b2i_PublicKey_bio(key); |
d4f0339c | 679 | #endif |
2234212c | 680 | } |
bd08a2bd | 681 | end: |
25aaa98a | 682 | BIO_free(key); |
0f113f3e | 683 | if (pkey == NULL) |
7e1b7485 | 684 | BIO_printf(bio_err, "unable to load %s\n", key_descrip); |
26a7d938 | 685 | return pkey; |
0f113f3e | 686 | } |
bd08a2bd | 687 | |
7e1b7485 | 688 | static int load_certs_crls(const char *file, int format, |
a773b52a | 689 | const char *pass, const char *desc, |
0f113f3e MC |
690 | STACK_OF(X509) **pcerts, |
691 | STACK_OF(X509_CRL) **pcrls) | |
692 | { | |
693 | int i; | |
694 | BIO *bio; | |
695 | STACK_OF(X509_INFO) *xis = NULL; | |
696 | X509_INFO *xi; | |
697 | PW_CB_DATA cb_data; | |
698 | int rv = 0; | |
699 | ||
700 | cb_data.password = pass; | |
701 | cb_data.prompt_info = file; | |
702 | ||
703 | if (format != FORMAT_PEM) { | |
7e1b7485 | 704 | BIO_printf(bio_err, "bad input format specified for %s\n", desc); |
0f113f3e MC |
705 | return 0; |
706 | } | |
707 | ||
bdd58d98 | 708 | bio = bio_open_default(file, 'r', FORMAT_PEM); |
7e1b7485 | 709 | if (bio == NULL) |
0f113f3e | 710 | return 0; |
0f113f3e MC |
711 | |
712 | xis = PEM_X509_INFO_read_bio(bio, NULL, | |
713 | (pem_password_cb *)password_callback, | |
714 | &cb_data); | |
715 | ||
716 | BIO_free(bio); | |
717 | ||
2234212c | 718 | if (pcerts != NULL && *pcerts == NULL) { |
0f113f3e | 719 | *pcerts = sk_X509_new_null(); |
2234212c | 720 | if (*pcerts == NULL) |
0f113f3e MC |
721 | goto end; |
722 | } | |
723 | ||
2234212c | 724 | if (pcrls != NULL && *pcrls == NULL) { |
0f113f3e | 725 | *pcrls = sk_X509_CRL_new_null(); |
2234212c | 726 | if (*pcrls == NULL) |
0f113f3e MC |
727 | goto end; |
728 | } | |
729 | ||
730 | for (i = 0; i < sk_X509_INFO_num(xis); i++) { | |
731 | xi = sk_X509_INFO_value(xis, i); | |
2234212c | 732 | if (xi->x509 != NULL && pcerts != NULL) { |
0f113f3e MC |
733 | if (!sk_X509_push(*pcerts, xi->x509)) |
734 | goto end; | |
735 | xi->x509 = NULL; | |
736 | } | |
2234212c | 737 | if (xi->crl != NULL && pcrls != NULL) { |
0f113f3e MC |
738 | if (!sk_X509_CRL_push(*pcrls, xi->crl)) |
739 | goto end; | |
740 | xi->crl = NULL; | |
741 | } | |
742 | } | |
743 | ||
2234212c | 744 | if (pcerts != NULL && sk_X509_num(*pcerts) > 0) |
0f113f3e MC |
745 | rv = 1; |
746 | ||
2234212c | 747 | if (pcrls != NULL && sk_X509_CRL_num(*pcrls) > 0) |
0f113f3e MC |
748 | rv = 1; |
749 | ||
750 | end: | |
751 | ||
222561fe | 752 | sk_X509_INFO_pop_free(xis, X509_INFO_free); |
0f113f3e MC |
753 | |
754 | if (rv == 0) { | |
2234212c | 755 | if (pcerts != NULL) { |
0f113f3e MC |
756 | sk_X509_pop_free(*pcerts, X509_free); |
757 | *pcerts = NULL; | |
758 | } | |
2234212c | 759 | if (pcrls != NULL) { |
0f113f3e MC |
760 | sk_X509_CRL_pop_free(*pcrls, X509_CRL_free); |
761 | *pcrls = NULL; | |
762 | } | |
7e1b7485 | 763 | BIO_printf(bio_err, "unable to load %s\n", |
0f113f3e | 764 | pcerts ? "certificates" : "CRLs"); |
7e1b7485 | 765 | ERR_print_errors(bio_err); |
0f113f3e MC |
766 | } |
767 | return rv; | |
768 | } | |
90ae4673 | 769 | |
68dc6824 RS |
770 | void* app_malloc(int sz, const char *what) |
771 | { | |
772 | void *vp = OPENSSL_malloc(sz); | |
773 | ||
774 | if (vp == NULL) { | |
775 | BIO_printf(bio_err, "%s: Could not allocate %d bytes for %s\n", | |
776 | opt_getprog(), sz, what); | |
777 | ERR_print_errors(bio_err); | |
778 | exit(1); | |
779 | } | |
780 | return vp; | |
781 | } | |
782 | ||
0996dc54 | 783 | /* |
6b4a77f5 | 784 | * Initialize or extend, if *certs != NULL, a certificate stack. |
0996dc54 VD |
785 | */ |
786 | int load_certs(const char *file, STACK_OF(X509) **certs, int format, | |
a773b52a | 787 | const char *pass, const char *desc) |
0f113f3e | 788 | { |
a773b52a | 789 | return load_certs_crls(file, format, pass, desc, certs, NULL); |
0f113f3e | 790 | } |
245d2ee3 | 791 | |
0996dc54 | 792 | /* |
6b4a77f5 | 793 | * Initialize or extend, if *crls != NULL, a certificate stack. |
0996dc54 VD |
794 | */ |
795 | int load_crls(const char *file, STACK_OF(X509_CRL) **crls, int format, | |
a773b52a | 796 | const char *pass, const char *desc) |
0f113f3e | 797 | { |
a773b52a | 798 | return load_certs_crls(file, format, pass, desc, NULL, crls); |
0f113f3e MC |
799 | } |
800 | ||
801 | #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16) | |
8ca533e3 | 802 | /* Return error for unknown extensions */ |
0f113f3e | 803 | #define X509V3_EXT_DEFAULT 0 |
8ca533e3 | 804 | /* Print error for unknown extensions */ |
0f113f3e | 805 | #define X509V3_EXT_ERROR_UNKNOWN (1L << 16) |
8ca533e3 | 806 | /* ASN1 parse unknown extensions */ |
0f113f3e | 807 | #define X509V3_EXT_PARSE_UNKNOWN (2L << 16) |
8ca533e3 | 808 | /* BIO_dump unknown extensions */ |
0f113f3e | 809 | #define X509V3_EXT_DUMP_UNKNOWN (3L << 16) |
8ca533e3 | 810 | |
535d79da | 811 | #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \ |
0f113f3e | 812 | X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION) |
535d79da | 813 | |
8ca533e3 DSH |
814 | int set_cert_ex(unsigned long *flags, const char *arg) |
815 | { | |
0f113f3e MC |
816 | static const NAME_EX_TBL cert_tbl[] = { |
817 | {"compatible", X509_FLAG_COMPAT, 0xffffffffl}, | |
818 | {"ca_default", X509_FLAG_CA, 0xffffffffl}, | |
819 | {"no_header", X509_FLAG_NO_HEADER, 0}, | |
820 | {"no_version", X509_FLAG_NO_VERSION, 0}, | |
821 | {"no_serial", X509_FLAG_NO_SERIAL, 0}, | |
822 | {"no_signame", X509_FLAG_NO_SIGNAME, 0}, | |
823 | {"no_validity", X509_FLAG_NO_VALIDITY, 0}, | |
824 | {"no_subject", X509_FLAG_NO_SUBJECT, 0}, | |
825 | {"no_issuer", X509_FLAG_NO_ISSUER, 0}, | |
826 | {"no_pubkey", X509_FLAG_NO_PUBKEY, 0}, | |
827 | {"no_extensions", X509_FLAG_NO_EXTENSIONS, 0}, | |
828 | {"no_sigdump", X509_FLAG_NO_SIGDUMP, 0}, | |
829 | {"no_aux", X509_FLAG_NO_AUX, 0}, | |
830 | {"no_attributes", X509_FLAG_NO_ATTRIBUTES, 0}, | |
831 | {"ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK}, | |
832 | {"ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
833 | {"ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
834 | {"ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
835 | {NULL, 0, 0} | |
836 | }; | |
837 | return set_multi_opts(flags, arg, cert_tbl); | |
8ca533e3 | 838 | } |
a657546f DSH |
839 | |
840 | int set_name_ex(unsigned long *flags, const char *arg) | |
841 | { | |
0f113f3e MC |
842 | static const NAME_EX_TBL ex_tbl[] = { |
843 | {"esc_2253", ASN1_STRFLGS_ESC_2253, 0}, | |
bc776510 | 844 | {"esc_2254", ASN1_STRFLGS_ESC_2254, 0}, |
0f113f3e MC |
845 | {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0}, |
846 | {"esc_msb", ASN1_STRFLGS_ESC_MSB, 0}, | |
847 | {"use_quote", ASN1_STRFLGS_ESC_QUOTE, 0}, | |
848 | {"utf8", ASN1_STRFLGS_UTF8_CONVERT, 0}, | |
849 | {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0}, | |
850 | {"show_type", ASN1_STRFLGS_SHOW_TYPE, 0}, | |
851 | {"dump_all", ASN1_STRFLGS_DUMP_ALL, 0}, | |
852 | {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0}, | |
853 | {"dump_der", ASN1_STRFLGS_DUMP_DER, 0}, | |
854 | {"compat", XN_FLAG_COMPAT, 0xffffffffL}, | |
855 | {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK}, | |
856 | {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK}, | |
857 | {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK}, | |
858 | {"sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK}, | |
859 | {"dn_rev", XN_FLAG_DN_REV, 0}, | |
860 | {"nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK}, | |
861 | {"sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK}, | |
862 | {"lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK}, | |
863 | {"align", XN_FLAG_FN_ALIGN, 0}, | |
864 | {"oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK}, | |
865 | {"space_eq", XN_FLAG_SPC_EQ, 0}, | |
866 | {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0}, | |
867 | {"RFC2253", XN_FLAG_RFC2253, 0xffffffffL}, | |
868 | {"oneline", XN_FLAG_ONELINE, 0xffffffffL}, | |
869 | {"multiline", XN_FLAG_MULTILINE, 0xffffffffL}, | |
870 | {"ca_default", XN_FLAG_MULTILINE, 0xffffffffL}, | |
871 | {NULL, 0, 0} | |
872 | }; | |
03706afa DSH |
873 | if (set_multi_opts(flags, arg, ex_tbl) == 0) |
874 | return 0; | |
3190d1dc RL |
875 | if (*flags != XN_FLAG_COMPAT |
876 | && (*flags & XN_FLAG_SEP_MASK) == 0) | |
03706afa DSH |
877 | *flags |= XN_FLAG_SEP_CPLUS_SPC; |
878 | return 1; | |
535d79da DSH |
879 | } |
880 | ||
791bd0cd DSH |
881 | int set_ext_copy(int *copy_type, const char *arg) |
882 | { | |
86885c28 | 883 | if (strcasecmp(arg, "none") == 0) |
0f113f3e | 884 | *copy_type = EXT_COPY_NONE; |
86885c28 | 885 | else if (strcasecmp(arg, "copy") == 0) |
0f113f3e | 886 | *copy_type = EXT_COPY_ADD; |
86885c28 | 887 | else if (strcasecmp(arg, "copyall") == 0) |
0f113f3e MC |
888 | *copy_type = EXT_COPY_ALL; |
889 | else | |
890 | return 0; | |
891 | return 1; | |
791bd0cd DSH |
892 | } |
893 | ||
894 | int copy_extensions(X509 *x, X509_REQ *req, int copy_type) | |
895 | { | |
0f113f3e MC |
896 | STACK_OF(X509_EXTENSION) *exts = NULL; |
897 | X509_EXTENSION *ext, *tmpext; | |
898 | ASN1_OBJECT *obj; | |
899 | int i, idx, ret = 0; | |
900 | if (!x || !req || (copy_type == EXT_COPY_NONE)) | |
901 | return 1; | |
902 | exts = X509_REQ_get_extensions(req); | |
903 | ||
904 | for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { | |
905 | ext = sk_X509_EXTENSION_value(exts, i); | |
906 | obj = X509_EXTENSION_get_object(ext); | |
907 | idx = X509_get_ext_by_OBJ(x, obj, -1); | |
908 | /* Does extension exist? */ | |
909 | if (idx != -1) { | |
910 | /* If normal copy don't override existing extension */ | |
911 | if (copy_type == EXT_COPY_ADD) | |
912 | continue; | |
913 | /* Delete all extensions of same type */ | |
914 | do { | |
915 | tmpext = X509_get_ext(x, idx); | |
916 | X509_delete_ext(x, idx); | |
917 | X509_EXTENSION_free(tmpext); | |
918 | idx = X509_get_ext_by_OBJ(x, obj, -1); | |
919 | } while (idx != -1); | |
920 | } | |
921 | if (!X509_add_ext(x, ext, -1)) | |
922 | goto end; | |
923 | } | |
924 | ||
925 | ret = 1; | |
926 | ||
927 | end: | |
928 | ||
929 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); | |
930 | ||
931 | return ret; | |
932 | } | |
933 | ||
934 | static int set_multi_opts(unsigned long *flags, const char *arg, | |
935 | const NAME_EX_TBL * in_tbl) | |
936 | { | |
937 | STACK_OF(CONF_VALUE) *vals; | |
938 | CONF_VALUE *val; | |
939 | int i, ret = 1; | |
940 | if (!arg) | |
941 | return 0; | |
942 | vals = X509V3_parse_list(arg); | |
943 | for (i = 0; i < sk_CONF_VALUE_num(vals); i++) { | |
944 | val = sk_CONF_VALUE_value(vals, i); | |
945 | if (!set_table_opts(flags, val->name, in_tbl)) | |
946 | ret = 0; | |
947 | } | |
948 | sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); | |
949 | return ret; | |
950 | } | |
951 | ||
952 | static int set_table_opts(unsigned long *flags, const char *arg, | |
953 | const NAME_EX_TBL * in_tbl) | |
954 | { | |
955 | char c; | |
956 | const NAME_EX_TBL *ptbl; | |
957 | c = arg[0]; | |
958 | ||
959 | if (c == '-') { | |
960 | c = 0; | |
961 | arg++; | |
962 | } else if (c == '+') { | |
963 | c = 1; | |
964 | arg++; | |
2234212c | 965 | } else { |
0f113f3e | 966 | c = 1; |
2234212c | 967 | } |
0f113f3e MC |
968 | |
969 | for (ptbl = in_tbl; ptbl->name; ptbl++) { | |
86885c28 | 970 | if (strcasecmp(arg, ptbl->name) == 0) { |
0f113f3e MC |
971 | *flags &= ~ptbl->mask; |
972 | if (c) | |
973 | *flags |= ptbl->flag; | |
974 | else | |
975 | *flags &= ~ptbl->flag; | |
976 | return 1; | |
977 | } | |
978 | } | |
979 | return 0; | |
980 | } | |
981 | ||
8cc86b81 | 982 | void print_name(BIO *out, const char *title, const X509_NAME *nm, |
0f113f3e MC |
983 | unsigned long lflags) |
984 | { | |
985 | char *buf; | |
986 | char mline = 0; | |
987 | int indent = 0; | |
988 | ||
989 | if (title) | |
990 | BIO_puts(out, title); | |
991 | if ((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { | |
992 | mline = 1; | |
993 | indent = 4; | |
994 | } | |
995 | if (lflags == XN_FLAG_COMPAT) { | |
996 | buf = X509_NAME_oneline(nm, 0, 0); | |
997 | BIO_puts(out, buf); | |
998 | BIO_puts(out, "\n"); | |
999 | OPENSSL_free(buf); | |
1000 | } else { | |
1001 | if (mline) | |
1002 | BIO_puts(out, "\n"); | |
1003 | X509_NAME_print_ex(out, nm, indent, lflags); | |
1004 | BIO_puts(out, "\n"); | |
1005 | } | |
a657546f DSH |
1006 | } |
1007 | ||
2ac6115d | 1008 | void print_bignum_var(BIO *out, const BIGNUM *in, const char *var, |
7e1b7485 | 1009 | int len, unsigned char *buffer) |
81f169e9 | 1010 | { |
7e1b7485 | 1011 | BIO_printf(out, " static unsigned char %s_%d[] = {", var, len); |
2234212c | 1012 | if (BN_is_zero(in)) { |
06deb932 | 1013 | BIO_printf(out, "\n 0x00"); |
2234212c | 1014 | } else { |
7e1b7485 RS |
1015 | int i, l; |
1016 | ||
1017 | l = BN_bn2bin(in, buffer); | |
1018 | for (i = 0; i < l; i++) { | |
06deb932 | 1019 | BIO_printf(out, (i % 10) == 0 ? "\n " : " "); |
7e1b7485 | 1020 | if (i < l - 1) |
06deb932 | 1021 | BIO_printf(out, "0x%02X,", buffer[i]); |
7e1b7485 RS |
1022 | else |
1023 | BIO_printf(out, "0x%02X", buffer[i]); | |
1024 | } | |
1025 | } | |
1026 | BIO_printf(out, "\n };\n"); | |
1027 | } | |
2234212c | 1028 | |
7e1b7485 RS |
1029 | void print_array(BIO *out, const char* title, int len, const unsigned char* d) |
1030 | { | |
1031 | int i; | |
1032 | ||
1033 | BIO_printf(out, "unsigned char %s[%d] = {", title, len); | |
1034 | for (i = 0; i < len; i++) { | |
1035 | if ((i % 10) == 0) | |
1036 | BIO_printf(out, "\n "); | |
1037 | if (i < len - 1) | |
1038 | BIO_printf(out, "0x%02X, ", d[i]); | |
1039 | else | |
1040 | BIO_printf(out, "0x%02X", d[i]); | |
1041 | } | |
1042 | BIO_printf(out, "\n};\n"); | |
1043 | } | |
1044 | ||
fd3397fc RL |
1045 | X509_STORE *setup_verify(const char *CAfile, int noCAfile, |
1046 | const char *CApath, int noCApath, | |
1047 | const char *CAstore, int noCAstore) | |
7e1b7485 RS |
1048 | { |
1049 | X509_STORE *store = X509_STORE_new(); | |
0f113f3e | 1050 | X509_LOOKUP *lookup; |
7e1b7485 | 1051 | |
96487cdd | 1052 | if (store == NULL) |
0f113f3e | 1053 | goto end; |
2b6bcb70 | 1054 | |
e8aa8b6c | 1055 | if (CAfile != NULL || !noCAfile) { |
2b6bcb70 MC |
1056 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); |
1057 | if (lookup == NULL) | |
0f113f3e | 1058 | goto end; |
fd3397fc | 1059 | if (CAfile != NULL) { |
2b6bcb70 MC |
1060 | if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM)) { |
1061 | BIO_printf(bio_err, "Error loading file %s\n", CAfile); | |
1062 | goto end; | |
1063 | } | |
2234212c | 1064 | } else { |
2b6bcb70 | 1065 | X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT); |
2234212c | 1066 | } |
2b6bcb70 | 1067 | } |
0f113f3e | 1068 | |
e8aa8b6c | 1069 | if (CApath != NULL || !noCApath) { |
2b6bcb70 MC |
1070 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir()); |
1071 | if (lookup == NULL) | |
0f113f3e | 1072 | goto end; |
fd3397fc | 1073 | if (CApath != NULL) { |
2b6bcb70 MC |
1074 | if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) { |
1075 | BIO_printf(bio_err, "Error loading directory %s\n", CApath); | |
1076 | goto end; | |
1077 | } | |
2234212c | 1078 | } else { |
2b6bcb70 | 1079 | X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT); |
2234212c | 1080 | } |
2b6bcb70 | 1081 | } |
0f113f3e | 1082 | |
fd3397fc RL |
1083 | if (CAstore != NULL || !noCAstore) { |
1084 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_store()); | |
1085 | if (lookup == NULL) | |
1086 | goto end; | |
1087 | if (!X509_LOOKUP_add_store(lookup, CAstore)) { | |
1088 | if (CAstore != NULL) | |
1089 | BIO_printf(bio_err, "Error loading store URI %s\n", CAstore); | |
1090 | goto end; | |
1091 | } | |
1092 | } | |
1093 | ||
0f113f3e MC |
1094 | ERR_clear_error(); |
1095 | return store; | |
1096 | end: | |
1097 | X509_STORE_free(store); | |
1098 | return NULL; | |
81f169e9 | 1099 | } |
531d630b | 1100 | |
0b13e9f0 | 1101 | #ifndef OPENSSL_NO_ENGINE |
e1a00d7d | 1102 | /* Try to load an engine in a shareable library */ |
a773b52a | 1103 | static ENGINE *try_load_engine(const char *engine) |
0f113f3e MC |
1104 | { |
1105 | ENGINE *e = ENGINE_by_id("dynamic"); | |
1106 | if (e) { | |
1107 | if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0) | |
1108 | || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) { | |
1109 | ENGINE_free(e); | |
1110 | e = NULL; | |
1111 | } | |
1112 | } | |
1113 | return e; | |
1114 | } | |
907c6c86 | 1115 | #endif |
e1a00d7d | 1116 | |
7e1b7485 | 1117 | ENGINE *setup_engine(const char *engine, int debug) |
0f113f3e MC |
1118 | { |
1119 | ENGINE *e = NULL; | |
1120 | ||
907c6c86 | 1121 | #ifndef OPENSSL_NO_ENGINE |
2234212c | 1122 | if (engine != NULL) { |
0f113f3e | 1123 | if (strcmp(engine, "auto") == 0) { |
7e1b7485 | 1124 | BIO_printf(bio_err, "enabling auto ENGINE support\n"); |
0f113f3e MC |
1125 | ENGINE_register_all_complete(); |
1126 | return NULL; | |
1127 | } | |
1128 | if ((e = ENGINE_by_id(engine)) == NULL | |
a773b52a | 1129 | && (e = try_load_engine(engine)) == NULL) { |
7e1b7485 RS |
1130 | BIO_printf(bio_err, "invalid engine \"%s\"\n", engine); |
1131 | ERR_print_errors(bio_err); | |
0f113f3e MC |
1132 | return NULL; |
1133 | } | |
1134 | if (debug) { | |
7e1b7485 | 1135 | ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, 0, bio_err, 0); |
531d630b | 1136 | } |
a43ce58f SL |
1137 | ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, (void *)get_ui_method(), |
1138 | 0, 1); | |
56e36bda | 1139 | if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) { |
7e1b7485 RS |
1140 | BIO_printf(bio_err, "can't use that engine\n"); |
1141 | ERR_print_errors(bio_err); | |
0f113f3e MC |
1142 | ENGINE_free(e); |
1143 | return NULL; | |
1144 | } | |
1145 | ||
7e1b7485 | 1146 | BIO_printf(bio_err, "engine \"%s\" set.\n", ENGINE_get_id(e)); |
0f113f3e | 1147 | } |
907c6c86 | 1148 | #endif |
0f113f3e MC |
1149 | return e; |
1150 | } | |
3647bee2 | 1151 | |
dd1abd44 RL |
1152 | void release_engine(ENGINE *e) |
1153 | { | |
1154 | #ifndef OPENSSL_NO_ENGINE | |
1155 | if (e != NULL) | |
1156 | /* Free our "structural" reference. */ | |
1157 | ENGINE_free(e); | |
1158 | #endif | |
1159 | } | |
1160 | ||
c869da88 | 1161 | static unsigned long index_serial_hash(const OPENSSL_CSTRING *a) |
0f113f3e MC |
1162 | { |
1163 | const char *n; | |
f85b68cd | 1164 | |
0f113f3e MC |
1165 | n = a[DB_serial]; |
1166 | while (*n == '0') | |
1167 | n++; | |
739a1eb1 | 1168 | return OPENSSL_LH_strhash(n); |
0f113f3e | 1169 | } |
f85b68cd | 1170 | |
0f113f3e MC |
1171 | static int index_serial_cmp(const OPENSSL_CSTRING *a, |
1172 | const OPENSSL_CSTRING *b) | |
1173 | { | |
1174 | const char *aa, *bb; | |
f85b68cd | 1175 | |
0f113f3e MC |
1176 | for (aa = a[DB_serial]; *aa == '0'; aa++) ; |
1177 | for (bb = b[DB_serial]; *bb == '0'; bb++) ; | |
26a7d938 | 1178 | return strcmp(aa, bb); |
0f113f3e | 1179 | } |
f85b68cd RL |
1180 | |
1181 | static int index_name_qual(char **a) | |
0f113f3e MC |
1182 | { |
1183 | return (a[0][0] == 'V'); | |
1184 | } | |
f85b68cd | 1185 | |
c869da88 | 1186 | static unsigned long index_name_hash(const OPENSSL_CSTRING *a) |
0f113f3e | 1187 | { |
739a1eb1 | 1188 | return OPENSSL_LH_strhash(a[DB_name]); |
0f113f3e | 1189 | } |
f85b68cd | 1190 | |
c869da88 | 1191 | int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b) |
0f113f3e | 1192 | { |
26a7d938 | 1193 | return strcmp(a[DB_name], b[DB_name]); |
0f113f3e | 1194 | } |
f85b68cd | 1195 | |
c869da88 DSH |
1196 | static IMPLEMENT_LHASH_HASH_FN(index_serial, OPENSSL_CSTRING) |
1197 | static IMPLEMENT_LHASH_COMP_FN(index_serial, OPENSSL_CSTRING) | |
1198 | static IMPLEMENT_LHASH_HASH_FN(index_name, OPENSSL_CSTRING) | |
1199 | static IMPLEMENT_LHASH_COMP_FN(index_name, OPENSSL_CSTRING) | |
f85b68cd RL |
1200 | #undef BSIZE |
1201 | #define BSIZE 256 | |
cc696296 | 1202 | BIGNUM *load_serial(const char *serialfile, int create, ASN1_INTEGER **retai) |
0f113f3e MC |
1203 | { |
1204 | BIO *in = NULL; | |
1205 | BIGNUM *ret = NULL; | |
68b00c23 | 1206 | char buf[1024]; |
0f113f3e MC |
1207 | ASN1_INTEGER *ai = NULL; |
1208 | ||
1209 | ai = ASN1_INTEGER_new(); | |
1210 | if (ai == NULL) | |
1211 | goto err; | |
1212 | ||
7e1b7485 RS |
1213 | in = BIO_new_file(serialfile, "r"); |
1214 | if (in == NULL) { | |
0f113f3e MC |
1215 | if (!create) { |
1216 | perror(serialfile); | |
1217 | goto err; | |
0f113f3e | 1218 | } |
7e1b7485 RS |
1219 | ERR_clear_error(); |
1220 | ret = BN_new(); | |
1221 | if (ret == NULL || !rand_serial(ret, ai)) | |
1222 | BIO_printf(bio_err, "Out of memory\n"); | |
0f113f3e MC |
1223 | } else { |
1224 | if (!a2i_ASN1_INTEGER(in, ai, buf, 1024)) { | |
1225 | BIO_printf(bio_err, "unable to load number from %s\n", | |
1226 | serialfile); | |
1227 | goto err; | |
1228 | } | |
1229 | ret = ASN1_INTEGER_to_BN(ai, NULL); | |
1230 | if (ret == NULL) { | |
1231 | BIO_printf(bio_err, | |
1232 | "error converting number from bin to BIGNUM\n"); | |
1233 | goto err; | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | if (ret && retai) { | |
1238 | *retai = ai; | |
1239 | ai = NULL; | |
1240 | } | |
f85b68cd | 1241 | err: |
ca3a82c3 | 1242 | BIO_free(in); |
2ace7450 | 1243 | ASN1_INTEGER_free(ai); |
26a7d938 | 1244 | return ret; |
0f113f3e MC |
1245 | } |
1246 | ||
cc696296 | 1247 | int save_serial(const char *serialfile, const char *suffix, const BIGNUM *serial, |
0f113f3e MC |
1248 | ASN1_INTEGER **retai) |
1249 | { | |
1250 | char buf[1][BSIZE]; | |
1251 | BIO *out = NULL; | |
1252 | int ret = 0; | |
1253 | ASN1_INTEGER *ai = NULL; | |
1254 | int j; | |
1255 | ||
1256 | if (suffix == NULL) | |
1257 | j = strlen(serialfile); | |
1258 | else | |
1259 | j = strlen(serialfile) + strlen(suffix) + 1; | |
1260 | if (j >= BSIZE) { | |
1261 | BIO_printf(bio_err, "file name too long\n"); | |
1262 | goto err; | |
1263 | } | |
1264 | ||
1265 | if (suffix == NULL) | |
7644a9ae | 1266 | OPENSSL_strlcpy(buf[0], serialfile, BSIZE); |
0f113f3e | 1267 | else { |
4c771796 | 1268 | #ifndef OPENSSL_SYS_VMS |
cbe29648 | 1269 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", serialfile, suffix); |
4c771796 | 1270 | #else |
cbe29648 | 1271 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", serialfile, suffix); |
4c771796 | 1272 | #endif |
0f113f3e | 1273 | } |
7e1b7485 | 1274 | out = BIO_new_file(buf[0], "w"); |
0f113f3e MC |
1275 | if (out == NULL) { |
1276 | ERR_print_errors(bio_err); | |
1277 | goto err; | |
1278 | } | |
0f113f3e MC |
1279 | |
1280 | if ((ai = BN_to_ASN1_INTEGER(serial, NULL)) == NULL) { | |
1281 | BIO_printf(bio_err, "error converting serial to ASN.1 format\n"); | |
1282 | goto err; | |
1283 | } | |
1284 | i2a_ASN1_INTEGER(out, ai); | |
1285 | BIO_puts(out, "\n"); | |
1286 | ret = 1; | |
1287 | if (retai) { | |
1288 | *retai = ai; | |
1289 | ai = NULL; | |
1290 | } | |
1291 | err: | |
ca3a82c3 | 1292 | BIO_free_all(out); |
2ace7450 | 1293 | ASN1_INTEGER_free(ai); |
26a7d938 | 1294 | return ret; |
0f113f3e | 1295 | } |
f85b68cd | 1296 | |
cc696296 F |
1297 | int rotate_serial(const char *serialfile, const char *new_suffix, |
1298 | const char *old_suffix) | |
0f113f3e | 1299 | { |
bde136c8 | 1300 | char buf[2][BSIZE]; |
0f113f3e MC |
1301 | int i, j; |
1302 | ||
1303 | i = strlen(serialfile) + strlen(old_suffix); | |
1304 | j = strlen(serialfile) + strlen(new_suffix); | |
1305 | if (i > j) | |
1306 | j = i; | |
1307 | if (j + 1 >= BSIZE) { | |
1308 | BIO_printf(bio_err, "file name too long\n"); | |
1309 | goto err; | |
1310 | } | |
4c771796 | 1311 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1312 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", serialfile, new_suffix); |
1313 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.%s", serialfile, old_suffix); | |
4c771796 | 1314 | #else |
cbe29648 RS |
1315 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", serialfile, new_suffix); |
1316 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-%s", serialfile, old_suffix); | |
a1ad253f | 1317 | #endif |
0f113f3e | 1318 | if (rename(serialfile, buf[1]) < 0 && errno != ENOENT |
4c771796 | 1319 | #ifdef ENOTDIR |
0f113f3e MC |
1320 | && errno != ENOTDIR |
1321 | #endif | |
1322 | ) { | |
1323 | BIO_printf(bio_err, | |
1324 | "unable to rename %s to %s\n", serialfile, buf[1]); | |
1325 | perror("reason"); | |
1326 | goto err; | |
1327 | } | |
0f113f3e MC |
1328 | if (rename(buf[0], serialfile) < 0) { |
1329 | BIO_printf(bio_err, | |
1330 | "unable to rename %s to %s\n", buf[0], serialfile); | |
1331 | perror("reason"); | |
1332 | rename(buf[1], serialfile); | |
1333 | goto err; | |
1334 | } | |
1335 | return 1; | |
4c771796 | 1336 | err: |
0f113f3e MC |
1337 | return 0; |
1338 | } | |
4c771796 | 1339 | |
64674bcc | 1340 | int rand_serial(BIGNUM *b, ASN1_INTEGER *ai) |
0f113f3e MC |
1341 | { |
1342 | BIGNUM *btmp; | |
1343 | int ret = 0; | |
23a1d5e9 | 1344 | |
ffb46830 | 1345 | btmp = b == NULL ? BN_new() : b; |
96487cdd | 1346 | if (btmp == NULL) |
0f113f3e MC |
1347 | return 0; |
1348 | ||
ffb46830 | 1349 | if (!BN_rand(btmp, SERIAL_RAND_BITS, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) |
0f113f3e MC |
1350 | goto error; |
1351 | if (ai && !BN_to_ASN1_INTEGER(btmp, ai)) | |
1352 | goto error; | |
1353 | ||
1354 | ret = 1; | |
1355 | ||
1356 | error: | |
1357 | ||
23a1d5e9 | 1358 | if (btmp != b) |
0f113f3e MC |
1359 | BN_free(btmp); |
1360 | ||
1361 | return ret; | |
1362 | } | |
64674bcc | 1363 | |
cc696296 | 1364 | CA_DB *load_index(const char *dbfile, DB_ATTR *db_attr) |
0f113f3e MC |
1365 | { |
1366 | CA_DB *retdb = NULL; | |
1367 | TXT_DB *tmpdb = NULL; | |
7e1b7485 | 1368 | BIO *in; |
0f113f3e | 1369 | CONF *dbattr_conf = NULL; |
cc01d217 | 1370 | char buf[BSIZE]; |
c7d5ea26 VD |
1371 | #ifndef OPENSSL_NO_POSIX_IO |
1372 | FILE *dbfp; | |
1373 | struct stat dbst; | |
1374 | #endif | |
0f113f3e | 1375 | |
7e1b7485 | 1376 | in = BIO_new_file(dbfile, "r"); |
0f113f3e MC |
1377 | if (in == NULL) { |
1378 | ERR_print_errors(bio_err); | |
1379 | goto err; | |
1380 | } | |
c7d5ea26 VD |
1381 | |
1382 | #ifndef OPENSSL_NO_POSIX_IO | |
1383 | BIO_get_fp(in, &dbfp); | |
1384 | if (fstat(fileno(dbfp), &dbst) == -1) { | |
ff988500 RS |
1385 | ERR_raise_data(ERR_LIB_SYS, errno, |
1386 | "calling fstat(%s)", dbfile); | |
c7d5ea26 VD |
1387 | ERR_print_errors(bio_err); |
1388 | goto err; | |
1389 | } | |
1390 | #endif | |
1391 | ||
0f113f3e MC |
1392 | if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL) |
1393 | goto err; | |
f85b68cd RL |
1394 | |
1395 | #ifndef OPENSSL_SYS_VMS | |
cbe29648 | 1396 | BIO_snprintf(buf, sizeof(buf), "%s.attr", dbfile); |
f85b68cd | 1397 | #else |
cbe29648 | 1398 | BIO_snprintf(buf, sizeof(buf), "%s-attr", dbfile); |
0f113f3e | 1399 | #endif |
ac454d8d | 1400 | dbattr_conf = app_load_config_quiet(buf); |
0f113f3e | 1401 | |
b4faea50 | 1402 | retdb = app_malloc(sizeof(*retdb), "new DB"); |
0f113f3e MC |
1403 | retdb->db = tmpdb; |
1404 | tmpdb = NULL; | |
1405 | if (db_attr) | |
1406 | retdb->attributes = *db_attr; | |
1407 | else { | |
1408 | retdb->attributes.unique_subject = 1; | |
1409 | } | |
1410 | ||
1411 | if (dbattr_conf) { | |
1412 | char *p = NCONF_get_string(dbattr_conf, NULL, "unique_subject"); | |
1413 | if (p) { | |
0f113f3e MC |
1414 | retdb->attributes.unique_subject = parse_yesno(p, 1); |
1415 | } | |
1416 | } | |
f85b68cd | 1417 | |
c7d5ea26 VD |
1418 | retdb->dbfname = OPENSSL_strdup(dbfile); |
1419 | #ifndef OPENSSL_NO_POSIX_IO | |
1420 | retdb->dbst = dbst; | |
1421 | #endif | |
1422 | ||
f85b68cd | 1423 | err: |
efa7dd64 | 1424 | NCONF_free(dbattr_conf); |
895cba19 | 1425 | TXT_DB_free(tmpdb); |
ca3a82c3 | 1426 | BIO_free_all(in); |
0f113f3e MC |
1427 | return retdb; |
1428 | } | |
f85b68cd | 1429 | |
a4107d73 VD |
1430 | /* |
1431 | * Returns > 0 on success, <= 0 on error | |
1432 | */ | |
f85b68cd | 1433 | int index_index(CA_DB *db) |
0f113f3e MC |
1434 | { |
1435 | if (!TXT_DB_create_index(db->db, DB_serial, NULL, | |
1436 | LHASH_HASH_FN(index_serial), | |
1437 | LHASH_COMP_FN(index_serial))) { | |
1438 | BIO_printf(bio_err, | |
1439 | "error creating serial number index:(%ld,%ld,%ld)\n", | |
1440 | db->db->error, db->db->arg1, db->db->arg2); | |
1441 | return 0; | |
1442 | } | |
1443 | ||
1444 | if (db->attributes.unique_subject | |
1445 | && !TXT_DB_create_index(db->db, DB_name, index_name_qual, | |
1446 | LHASH_HASH_FN(index_name), | |
1447 | LHASH_COMP_FN(index_name))) { | |
1448 | BIO_printf(bio_err, "error creating name index:(%ld,%ld,%ld)\n", | |
1449 | db->db->error, db->db->arg1, db->db->arg2); | |
1450 | return 0; | |
1451 | } | |
1452 | return 1; | |
1453 | } | |
f85b68cd | 1454 | |
7d727231 | 1455 | int save_index(const char *dbfile, const char *suffix, CA_DB *db) |
0f113f3e MC |
1456 | { |
1457 | char buf[3][BSIZE]; | |
7e1b7485 | 1458 | BIO *out; |
0f113f3e MC |
1459 | int j; |
1460 | ||
0f113f3e MC |
1461 | j = strlen(dbfile) + strlen(suffix); |
1462 | if (j + 6 >= BSIZE) { | |
1463 | BIO_printf(bio_err, "file name too long\n"); | |
1464 | goto err; | |
1465 | } | |
f85b68cd | 1466 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1467 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s.attr", dbfile); |
1468 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.attr.%s", dbfile, suffix); | |
1469 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", dbfile, suffix); | |
f85b68cd | 1470 | #else |
cbe29648 RS |
1471 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s-attr", dbfile); |
1472 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-attr-%s", dbfile, suffix); | |
1473 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", dbfile, suffix); | |
63b6fe2b | 1474 | #endif |
7e1b7485 RS |
1475 | out = BIO_new_file(buf[0], "w"); |
1476 | if (out == NULL) { | |
0f113f3e MC |
1477 | perror(dbfile); |
1478 | BIO_printf(bio_err, "unable to open '%s'\n", dbfile); | |
1479 | goto err; | |
1480 | } | |
1481 | j = TXT_DB_write(out, db->db); | |
7e1b7485 | 1482 | BIO_free(out); |
0f113f3e MC |
1483 | if (j <= 0) |
1484 | goto err; | |
1485 | ||
7e1b7485 | 1486 | out = BIO_new_file(buf[1], "w"); |
7e1b7485 | 1487 | if (out == NULL) { |
0f113f3e MC |
1488 | perror(buf[2]); |
1489 | BIO_printf(bio_err, "unable to open '%s'\n", buf[2]); | |
1490 | goto err; | |
1491 | } | |
1492 | BIO_printf(out, "unique_subject = %s\n", | |
1493 | db->attributes.unique_subject ? "yes" : "no"); | |
1494 | BIO_free(out); | |
1495 | ||
1496 | return 1; | |
f85b68cd | 1497 | err: |
0f113f3e MC |
1498 | return 0; |
1499 | } | |
f85b68cd | 1500 | |
0f113f3e MC |
1501 | int rotate_index(const char *dbfile, const char *new_suffix, |
1502 | const char *old_suffix) | |
1503 | { | |
1504 | char buf[5][BSIZE]; | |
1505 | int i, j; | |
1506 | ||
1507 | i = strlen(dbfile) + strlen(old_suffix); | |
1508 | j = strlen(dbfile) + strlen(new_suffix); | |
1509 | if (i > j) | |
1510 | j = i; | |
1511 | if (j + 6 >= BSIZE) { | |
1512 | BIO_printf(bio_err, "file name too long\n"); | |
1513 | goto err; | |
1514 | } | |
f85b68cd | 1515 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1516 | j = BIO_snprintf(buf[4], sizeof(buf[4]), "%s.attr", dbfile); |
1517 | j = BIO_snprintf(buf[3], sizeof(buf[3]), "%s.attr.%s", dbfile, old_suffix); | |
1518 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s.attr.%s", dbfile, new_suffix); | |
1519 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.%s", dbfile, old_suffix); | |
1520 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", dbfile, new_suffix); | |
f85b68cd | 1521 | #else |
cbe29648 RS |
1522 | j = BIO_snprintf(buf[4], sizeof(buf[4]), "%s-attr", dbfile); |
1523 | j = BIO_snprintf(buf[3], sizeof(buf[3]), "%s-attr-%s", dbfile, old_suffix); | |
1524 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s-attr-%s", dbfile, new_suffix); | |
1525 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-%s", dbfile, old_suffix); | |
1526 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", dbfile, new_suffix); | |
63b6fe2b | 1527 | #endif |
0f113f3e | 1528 | if (rename(dbfile, buf[1]) < 0 && errno != ENOENT |
a1ad253f | 1529 | #ifdef ENOTDIR |
0f113f3e | 1530 | && errno != ENOTDIR |
a1ad253f | 1531 | #endif |
0f113f3e MC |
1532 | ) { |
1533 | BIO_printf(bio_err, "unable to rename %s to %s\n", dbfile, buf[1]); | |
1534 | perror("reason"); | |
1535 | goto err; | |
1536 | } | |
0f113f3e MC |
1537 | if (rename(buf[0], dbfile) < 0) { |
1538 | BIO_printf(bio_err, "unable to rename %s to %s\n", buf[0], dbfile); | |
1539 | perror("reason"); | |
1540 | rename(buf[1], dbfile); | |
1541 | goto err; | |
1542 | } | |
0f113f3e | 1543 | if (rename(buf[4], buf[3]) < 0 && errno != ENOENT |
a1ad253f | 1544 | #ifdef ENOTDIR |
0f113f3e MC |
1545 | && errno != ENOTDIR |
1546 | #endif | |
1547 | ) { | |
1548 | BIO_printf(bio_err, "unable to rename %s to %s\n", buf[4], buf[3]); | |
1549 | perror("reason"); | |
1550 | rename(dbfile, buf[0]); | |
1551 | rename(buf[1], dbfile); | |
1552 | goto err; | |
1553 | } | |
0f113f3e MC |
1554 | if (rename(buf[2], buf[4]) < 0) { |
1555 | BIO_printf(bio_err, "unable to rename %s to %s\n", buf[2], buf[4]); | |
1556 | perror("reason"); | |
1557 | rename(buf[3], buf[4]); | |
1558 | rename(dbfile, buf[0]); | |
1559 | rename(buf[1], dbfile); | |
1560 | goto err; | |
1561 | } | |
1562 | return 1; | |
f85b68cd | 1563 | err: |
0f113f3e MC |
1564 | return 0; |
1565 | } | |
f85b68cd RL |
1566 | |
1567 | void free_index(CA_DB *db) | |
0f113f3e MC |
1568 | { |
1569 | if (db) { | |
895cba19 | 1570 | TXT_DB_free(db->db); |
c7d5ea26 | 1571 | OPENSSL_free(db->dbfname); |
0f113f3e MC |
1572 | OPENSSL_free(db); |
1573 | } | |
1574 | } | |
6d5ffb59 | 1575 | |
ff990440 | 1576 | int parse_yesno(const char *str, int def) |
0f113f3e | 1577 | { |
0f113f3e MC |
1578 | if (str) { |
1579 | switch (*str) { | |
1580 | case 'f': /* false */ | |
1581 | case 'F': /* FALSE */ | |
1582 | case 'n': /* no */ | |
1583 | case 'N': /* NO */ | |
1584 | case '0': /* 0 */ | |
1bb2daea | 1585 | return 0; |
0f113f3e MC |
1586 | case 't': /* true */ |
1587 | case 'T': /* TRUE */ | |
1588 | case 'y': /* yes */ | |
1589 | case 'Y': /* YES */ | |
1590 | case '1': /* 1 */ | |
1bb2daea | 1591 | return 1; |
0f113f3e MC |
1592 | } |
1593 | } | |
1bb2daea | 1594 | return def; |
0f113f3e | 1595 | } |
03ddbdd9 | 1596 | |
6d5ffb59 | 1597 | /* |
db4c08f0 | 1598 | * name is expected to be in the format /type0=value0/type1=value1/type2=... |
6d5ffb59 RL |
1599 | * where characters may be escaped by \ |
1600 | */ | |
db4c08f0 | 1601 | X509_NAME *parse_name(const char *cp, long chtype, int canmulti) |
0f113f3e | 1602 | { |
db4c08f0 RS |
1603 | int nextismulti = 0; |
1604 | char *work; | |
1605 | X509_NAME *n; | |
0f113f3e | 1606 | |
2167640b EC |
1607 | if (*cp++ != '/') { |
1608 | BIO_printf(bio_err, | |
1609 | "name is expected to be in the format " | |
1610 | "/type0=value0/type1=value1/type2=... where characters may " | |
1611 | "be escaped by \\. This name is not in that format: '%s'\n", | |
1612 | --cp); | |
db4c08f0 | 1613 | return NULL; |
2167640b | 1614 | } |
db4c08f0 RS |
1615 | |
1616 | n = X509_NAME_new(); | |
1617 | if (n == NULL) | |
1618 | return NULL; | |
a3ed492f | 1619 | work = OPENSSL_strdup(cp); |
52958608 DO |
1620 | if (work == NULL) { |
1621 | BIO_printf(bio_err, "%s: Error copying name input\n", opt_getprog()); | |
db4c08f0 | 1622 | goto err; |
52958608 | 1623 | } |
db4c08f0 RS |
1624 | |
1625 | while (*cp) { | |
1626 | char *bp = work; | |
1627 | char *typestr = bp; | |
1628 | unsigned char *valstr; | |
1629 | int nid; | |
1630 | int ismulti = nextismulti; | |
1631 | nextismulti = 0; | |
1632 | ||
1633 | /* Collect the type */ | |
1634 | while (*cp && *cp != '=') | |
1635 | *bp++ = *cp++; | |
1636 | if (*cp == '\0') { | |
0f113f3e | 1637 | BIO_printf(bio_err, |
52958608 | 1638 | "%s: Hit end of string before finding the '='\n", |
db4c08f0 RS |
1639 | opt_getprog()); |
1640 | goto err; | |
0f113f3e | 1641 | } |
db4c08f0 RS |
1642 | *bp++ = '\0'; |
1643 | ++cp; | |
1644 | ||
1645 | /* Collect the value. */ | |
1646 | valstr = (unsigned char *)bp; | |
1647 | for (; *cp && *cp != '/'; *bp++ = *cp++) { | |
1648 | if (canmulti && *cp == '+') { | |
1649 | nextismulti = 1; | |
0f113f3e | 1650 | break; |
db4c08f0 RS |
1651 | } |
1652 | if (*cp == '\\' && *++cp == '\0') { | |
1653 | BIO_printf(bio_err, | |
52958608 DO |
1654 | "%s: escape character at end of string\n", |
1655 | opt_getprog()); | |
db4c08f0 RS |
1656 | goto err; |
1657 | } | |
0f113f3e MC |
1658 | } |
1659 | *bp++ = '\0'; | |
0f113f3e | 1660 | |
db4c08f0 RS |
1661 | /* If not at EOS (must be + or /), move forward. */ |
1662 | if (*cp) | |
1663 | ++cp; | |
0f113f3e | 1664 | |
db4c08f0 RS |
1665 | /* Parse */ |
1666 | nid = OBJ_txt2nid(typestr); | |
1667 | if (nid == NID_undef) { | |
1668 | BIO_printf(bio_err, "%s: Skipping unknown attribute \"%s\"\n", | |
52958608 | 1669 | opt_getprog(), typestr); |
0f113f3e MC |
1670 | continue; |
1671 | } | |
3d362f19 BK |
1672 | if (*valstr == '\0') { |
1673 | BIO_printf(bio_err, | |
1674 | "%s: No value provided for Subject Attribute %s, skipped\n", | |
1675 | opt_getprog(), typestr); | |
1676 | continue; | |
1677 | } | |
db4c08f0 RS |
1678 | if (!X509_NAME_add_entry_by_NID(n, nid, chtype, |
1679 | valstr, strlen((char *)valstr), | |
52958608 DO |
1680 | -1, ismulti ? -1 : 0)) { |
1681 | BIO_printf(bio_err, "%s: Error adding name attribute \"/%s=%s\"\n", | |
1682 | opt_getprog(), typestr ,valstr); | |
db4c08f0 | 1683 | goto err; |
52958608 | 1684 | } |
0f113f3e MC |
1685 | } |
1686 | ||
a3ed492f | 1687 | OPENSSL_free(work); |
0f113f3e MC |
1688 | return n; |
1689 | ||
db4c08f0 | 1690 | err: |
0f113f3e | 1691 | X509_NAME_free(n); |
a3ed492f | 1692 | OPENSSL_free(work); |
0f113f3e | 1693 | return NULL; |
6d5ffb59 RL |
1694 | } |
1695 | ||
0f113f3e MC |
1696 | /* |
1697 | * Read whole contents of a BIO into an allocated memory buffer and return | |
1698 | * it. | |
a9164153 DSH |
1699 | */ |
1700 | ||
1701 | int bio_to_mem(unsigned char **out, int maxlen, BIO *in) | |
0f113f3e MC |
1702 | { |
1703 | BIO *mem; | |
1704 | int len, ret; | |
1705 | unsigned char tbuf[1024]; | |
bde136c8 | 1706 | |
0f113f3e | 1707 | mem = BIO_new(BIO_s_mem()); |
96487cdd | 1708 | if (mem == NULL) |
0f113f3e MC |
1709 | return -1; |
1710 | for (;;) { | |
1711 | if ((maxlen != -1) && maxlen < 1024) | |
1712 | len = maxlen; | |
1713 | else | |
1714 | len = 1024; | |
1715 | len = BIO_read(in, tbuf, len); | |
0c20802c VD |
1716 | if (len < 0) { |
1717 | BIO_free(mem); | |
1718 | return -1; | |
1719 | } | |
1720 | if (len == 0) | |
0f113f3e MC |
1721 | break; |
1722 | if (BIO_write(mem, tbuf, len) != len) { | |
1723 | BIO_free(mem); | |
1724 | return -1; | |
1725 | } | |
1726 | maxlen -= len; | |
1727 | ||
1728 | if (maxlen == 0) | |
1729 | break; | |
1730 | } | |
1731 | ret = BIO_get_mem_data(mem, (char **)out); | |
1732 | BIO_set_flags(mem, BIO_FLAGS_MEM_RDONLY); | |
1733 | BIO_free(mem); | |
1734 | return ret; | |
1735 | } | |
a9164153 | 1736 | |
0c20802c | 1737 | int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value) |
0f113f3e MC |
1738 | { |
1739 | int rv; | |
1740 | char *stmp, *vtmp = NULL; | |
7644a9ae | 1741 | stmp = OPENSSL_strdup(value); |
0f113f3e MC |
1742 | if (!stmp) |
1743 | return -1; | |
1744 | vtmp = strchr(stmp, ':'); | |
1745 | if (vtmp) { | |
1746 | *vtmp = 0; | |
1747 | vtmp++; | |
1748 | } | |
1749 | rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp); | |
1750 | OPENSSL_free(stmp); | |
1751 | return rv; | |
1752 | } | |
a2318e86 | 1753 | |
ecf3a1fb | 1754 | static void nodes_print(const char *name, STACK_OF(X509_POLICY_NODE) *nodes) |
0f113f3e MC |
1755 | { |
1756 | X509_POLICY_NODE *node; | |
1757 | int i; | |
ecf3a1fb RS |
1758 | |
1759 | BIO_printf(bio_err, "%s Policies:", name); | |
0f113f3e | 1760 | if (nodes) { |
ecf3a1fb | 1761 | BIO_puts(bio_err, "\n"); |
0f113f3e MC |
1762 | for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++) { |
1763 | node = sk_X509_POLICY_NODE_value(nodes, i); | |
ecf3a1fb | 1764 | X509_POLICY_NODE_print(bio_err, node, 2); |
0f113f3e | 1765 | } |
2234212c | 1766 | } else { |
ecf3a1fb | 1767 | BIO_puts(bio_err, " <empty>\n"); |
2234212c | 1768 | } |
0f113f3e | 1769 | } |
c431798e | 1770 | |
ecf3a1fb | 1771 | void policies_print(X509_STORE_CTX *ctx) |
0f113f3e MC |
1772 | { |
1773 | X509_POLICY_TREE *tree; | |
1774 | int explicit_policy; | |
0f113f3e MC |
1775 | tree = X509_STORE_CTX_get0_policy_tree(ctx); |
1776 | explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx); | |
1777 | ||
ecf3a1fb | 1778 | BIO_printf(bio_err, "Require explicit Policy: %s\n", |
0f113f3e MC |
1779 | explicit_policy ? "True" : "False"); |
1780 | ||
ecf3a1fb RS |
1781 | nodes_print("Authority", X509_policy_tree_get0_policies(tree)); |
1782 | nodes_print("User", X509_policy_tree_get0_user_policies(tree)); | |
0f113f3e | 1783 | } |
ffa10187 | 1784 | |
3a83462d MC |
1785 | /*- |
1786 | * next_protos_parse parses a comma separated list of strings into a string | |
71fa4513 BL |
1787 | * in a format suitable for passing to SSL_CTX_set_next_protos_advertised. |
1788 | * outlen: (output) set to the length of the resulting buffer on success. | |
1789 | * err: (maybe NULL) on failure, an error message line is written to this BIO. | |
8483a003 | 1790 | * in: a NUL terminated string like "abc,def,ghi" |
71fa4513 | 1791 | * |
8483a003 | 1792 | * returns: a malloc'd buffer or NULL on failure. |
71fa4513 | 1793 | */ |
817cd0d5 | 1794 | unsigned char *next_protos_parse(size_t *outlen, const char *in) |
0f113f3e MC |
1795 | { |
1796 | size_t len; | |
1797 | unsigned char *out; | |
1798 | size_t i, start = 0; | |
e78253f2 | 1799 | size_t skipped = 0; |
0f113f3e MC |
1800 | |
1801 | len = strlen(in); | |
e78253f2 | 1802 | if (len == 0 || len >= 65535) |
0f113f3e MC |
1803 | return NULL; |
1804 | ||
e78253f2 | 1805 | out = app_malloc(len + 1, "NPN buffer"); |
0f113f3e MC |
1806 | for (i = 0; i <= len; ++i) { |
1807 | if (i == len || in[i] == ',') { | |
e78253f2 VD |
1808 | /* |
1809 | * Zero-length ALPN elements are invalid on the wire, we could be | |
1810 | * strict and reject the entire string, but just ignoring extra | |
1811 | * commas seems harmless and more friendly. | |
1812 | * | |
1813 | * Every comma we skip in this way puts the input buffer another | |
1814 | * byte ahead of the output buffer, so all stores into the output | |
1815 | * buffer need to be decremented by the number commas skipped. | |
1816 | */ | |
1817 | if (i == start) { | |
1818 | ++start; | |
1819 | ++skipped; | |
1820 | continue; | |
1821 | } | |
0f113f3e MC |
1822 | if (i - start > 255) { |
1823 | OPENSSL_free(out); | |
1824 | return NULL; | |
1825 | } | |
e78253f2 | 1826 | out[start-skipped] = (unsigned char)(i - start); |
0f113f3e | 1827 | start = i + 1; |
2234212c | 1828 | } else { |
e78253f2 | 1829 | out[i + 1 - skipped] = in[i]; |
2234212c | 1830 | } |
0f113f3e MC |
1831 | } |
1832 | ||
e78253f2 VD |
1833 | if (len <= skipped) { |
1834 | OPENSSL_free(out); | |
1835 | return NULL; | |
1836 | } | |
1837 | ||
1838 | *outlen = len + 1 - skipped; | |
0f113f3e MC |
1839 | return out; |
1840 | } | |
71fa4513 | 1841 | |
a70da5b3 | 1842 | void print_cert_checks(BIO *bio, X509 *x, |
0f113f3e MC |
1843 | const char *checkhost, |
1844 | const char *checkemail, const char *checkip) | |
1845 | { | |
1846 | if (x == NULL) | |
1847 | return; | |
1848 | if (checkhost) { | |
1849 | BIO_printf(bio, "Hostname %s does%s match certificate\n", | |
7e1b7485 RS |
1850 | checkhost, |
1851 | X509_check_host(x, checkhost, 0, 0, NULL) == 1 | |
1852 | ? "" : " NOT"); | |
0f113f3e MC |
1853 | } |
1854 | ||
1855 | if (checkemail) { | |
1856 | BIO_printf(bio, "Email %s does%s match certificate\n", | |
7e1b7485 RS |
1857 | checkemail, X509_check_email(x, checkemail, 0, 0) |
1858 | ? "" : " NOT"); | |
0f113f3e MC |
1859 | } |
1860 | ||
1861 | if (checkip) { | |
1862 | BIO_printf(bio, "IP %s does%s match certificate\n", | |
1863 | checkip, X509_check_ip_asc(x, checkip, 0) ? "" : " NOT"); | |
1864 | } | |
1865 | } | |
a70da5b3 | 1866 | |
0090a686 DSH |
1867 | /* Get first http URL from a DIST_POINT structure */ |
1868 | ||
1869 | static const char *get_dp_url(DIST_POINT *dp) | |
0f113f3e MC |
1870 | { |
1871 | GENERAL_NAMES *gens; | |
1872 | GENERAL_NAME *gen; | |
1873 | int i, gtype; | |
1874 | ASN1_STRING *uri; | |
1875 | if (!dp->distpoint || dp->distpoint->type != 0) | |
1876 | return NULL; | |
1877 | gens = dp->distpoint->name.fullname; | |
1878 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | |
1879 | gen = sk_GENERAL_NAME_value(gens, i); | |
1880 | uri = GENERAL_NAME_get0_value(gen, >ype); | |
1881 | if (gtype == GEN_URI && ASN1_STRING_length(uri) > 6) { | |
17ebf85a | 1882 | const char *uptr = (const char *)ASN1_STRING_get0_data(uri); |
86885c28 | 1883 | if (strncmp(uptr, "http://", 7) == 0) |
0f113f3e MC |
1884 | return uptr; |
1885 | } | |
1886 | } | |
1887 | return NULL; | |
1888 | } | |
1889 | ||
1890 | /* | |
1891 | * Look through a CRLDP structure and attempt to find an http URL to | |
1892 | * downloads a CRL from. | |
0090a686 DSH |
1893 | */ |
1894 | ||
1895 | static X509_CRL *load_crl_crldp(STACK_OF(DIST_POINT) *crldp) | |
0f113f3e MC |
1896 | { |
1897 | int i; | |
1898 | const char *urlptr = NULL; | |
1899 | for (i = 0; i < sk_DIST_POINT_num(crldp); i++) { | |
1900 | DIST_POINT *dp = sk_DIST_POINT_value(crldp, i); | |
1901 | urlptr = get_dp_url(dp); | |
1902 | if (urlptr) | |
1903 | return load_crl(urlptr, FORMAT_HTTP); | |
1904 | } | |
1905 | return NULL; | |
1906 | } | |
1907 | ||
1908 | /* | |
0aa87e86 DDO |
1909 | * Example of downloading CRLs from CRLDP: |
1910 | * not usable for real world as it always downloads and doesn't cache anything. | |
0090a686 DSH |
1911 | */ |
1912 | ||
8cc86b81 DDO |
1913 | static STACK_OF(X509_CRL) *crls_http_cb(const X509_STORE_CTX *ctx, |
1914 | const X509_NAME *nm) | |
0f113f3e MC |
1915 | { |
1916 | X509 *x; | |
1917 | STACK_OF(X509_CRL) *crls = NULL; | |
1918 | X509_CRL *crl; | |
1919 | STACK_OF(DIST_POINT) *crldp; | |
7e1b7485 RS |
1920 | |
1921 | crls = sk_X509_CRL_new_null(); | |
1922 | if (!crls) | |
1923 | return NULL; | |
0f113f3e MC |
1924 | x = X509_STORE_CTX_get_current_cert(ctx); |
1925 | crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL); | |
1926 | crl = load_crl_crldp(crldp); | |
1927 | sk_DIST_POINT_pop_free(crldp, DIST_POINT_free); | |
fe2b7dfd MC |
1928 | if (!crl) { |
1929 | sk_X509_CRL_free(crls); | |
0f113f3e | 1930 | return NULL; |
fe2b7dfd | 1931 | } |
0f113f3e MC |
1932 | sk_X509_CRL_push(crls, crl); |
1933 | /* Try to download delta CRL */ | |
1934 | crldp = X509_get_ext_d2i(x, NID_freshest_crl, NULL, NULL); | |
1935 | crl = load_crl_crldp(crldp); | |
1936 | sk_DIST_POINT_pop_free(crldp, DIST_POINT_free); | |
1937 | if (crl) | |
1938 | sk_X509_CRL_push(crls, crl); | |
1939 | return crls; | |
1940 | } | |
0090a686 DSH |
1941 | |
1942 | void store_setup_crl_download(X509_STORE *st) | |
0f113f3e MC |
1943 | { |
1944 | X509_STORE_set_lookup_crls_cb(st, crls_http_cb); | |
1945 | } | |
0090a686 | 1946 | |
29f178bd DDO |
1947 | #ifndef OPENSSL_NO_SOCK |
1948 | static const char *tls_error_hint(void) | |
1949 | { | |
1950 | unsigned long err = ERR_peek_error(); | |
1951 | ||
1952 | if (ERR_GET_LIB(err) != ERR_LIB_SSL) | |
1953 | err = ERR_peek_last_error(); | |
1954 | if (ERR_GET_LIB(err) != ERR_LIB_SSL) | |
1955 | return NULL; | |
1956 | ||
1957 | switch (ERR_GET_REASON(err)) { | |
1958 | case SSL_R_WRONG_VERSION_NUMBER: | |
1959 | return "The server does not support (a suitable version of) TLS"; | |
1960 | case SSL_R_UNKNOWN_PROTOCOL: | |
1961 | return "The server does not support HTTPS"; | |
1962 | case SSL_R_CERTIFICATE_VERIFY_FAILED: | |
1963 | return "Cannot authenticate server via its TLS certificate, likely due to mismatch with our trusted TLS certs or missing revocation status"; | |
1964 | case SSL_AD_REASON_OFFSET + TLS1_AD_UNKNOWN_CA: | |
1965 | return "Server did not accept our TLS certificate, likely due to mismatch with server's trust anchor or missing revocation status"; | |
1966 | case SSL_AD_REASON_OFFSET + SSL3_AD_HANDSHAKE_FAILURE: | |
1967 | return "TLS handshake failure. Possibly the server requires our TLS certificate but did not receive it"; | |
1968 | default: /* no error or no hint available for error */ | |
1969 | return NULL; | |
1970 | } | |
1971 | } | |
1972 | ||
1973 | /* HTTP callback function that supports TLS connection also via HTTPS proxy */ | |
1974 | BIO *app_http_tls_cb(BIO *hbio, void *arg, int connect, int detail) | |
1975 | { | |
1976 | APP_HTTP_TLS_INFO *info = (APP_HTTP_TLS_INFO *)arg; | |
1977 | SSL_CTX *ssl_ctx = info->ssl_ctx; | |
1978 | SSL *ssl; | |
1979 | BIO *sbio = NULL; | |
1980 | ||
1981 | if (connect && detail) { /* connecting with TLS */ | |
1982 | if ((info->use_proxy | |
1983 | && !OSSL_HTTP_proxy_connect(hbio, info->server, info->port, | |
1984 | NULL, NULL, /* no proxy credentials */ | |
1985 | info->timeout, bio_err, opt_getprog())) | |
1986 | || (sbio = BIO_new(BIO_f_ssl())) == NULL) { | |
1987 | return NULL; | |
1988 | } | |
1989 | if (ssl_ctx == NULL || (ssl = SSL_new(ssl_ctx)) == NULL) { | |
1990 | BIO_free(sbio); | |
1991 | return NULL; | |
1992 | } | |
1993 | ||
1994 | SSL_set_tlsext_host_name(ssl, info->server); | |
1995 | ||
1996 | SSL_set_connect_state(ssl); | |
1997 | BIO_set_ssl(sbio, ssl, BIO_CLOSE); | |
1998 | ||
1999 | hbio = BIO_push(sbio, hbio); | |
2000 | } else if (!connect && !detail) { /* disconnecting after error */ | |
2001 | const char *hint = tls_error_hint(); | |
2002 | if (hint != NULL) | |
afe554c2 | 2003 | ERR_add_error_data(2, " : ", hint); |
29f178bd DDO |
2004 | /* |
2005 | * If we pop sbio and BIO_free() it this may lead to libssl double free. | |
2006 | * Rely on BIO_free_all() done by OSSL_HTTP_transfer() in http_client.c | |
2007 | */ | |
2008 | } | |
2009 | return hbio; | |
2010 | } | |
2011 | ||
2012 | ASN1_VALUE *app_http_get_asn1(const char *url, const char *proxy, | |
afe554c2 | 2013 | const char *no_proxy, SSL_CTX *ssl_ctx, |
29f178bd DDO |
2014 | const STACK_OF(CONF_VALUE) *headers, |
2015 | long timeout, const char *expected_content_type, | |
2016 | const ASN1_ITEM *it) | |
2017 | { | |
2018 | APP_HTTP_TLS_INFO info; | |
2019 | char *server; | |
2020 | char *port; | |
2021 | int use_ssl; | |
2022 | ASN1_VALUE *resp = NULL; | |
2023 | ||
2024 | if (url == NULL || it == NULL) { | |
2025 | HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER); | |
2026 | return NULL; | |
2027 | } | |
2028 | ||
2029 | if (!OSSL_HTTP_parse_url(url, &server, &port, NULL /* ppath */, &use_ssl)) | |
2030 | return NULL; | |
2031 | if (use_ssl && ssl_ctx == NULL) { | |
2032 | HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER); | |
2033 | ERR_add_error_data(1, "missing SSL_CTX"); | |
2034 | goto end; | |
2035 | } | |
2036 | ||
2037 | info.server = server; | |
2038 | info.port = port; | |
2039 | info.use_proxy = proxy != NULL; | |
2040 | info.timeout = timeout; | |
2041 | info.ssl_ctx = ssl_ctx; | |
afe554c2 | 2042 | resp = OSSL_HTTP_get_asn1(url, proxy, no_proxy, |
29f178bd DDO |
2043 | NULL, NULL, app_http_tls_cb, &info, |
2044 | headers, 0 /* maxline */, 0 /* max_resp_len */, | |
2045 | timeout, expected_content_type, it); | |
2046 | end: | |
2047 | OPENSSL_free(server); | |
2048 | OPENSSL_free(port); | |
2049 | return resp; | |
2050 | ||
2051 | } | |
2052 | ||
2053 | ASN1_VALUE *app_http_post_asn1(const char *host, const char *port, | |
2054 | const char *path, const char *proxy, | |
afe554c2 | 2055 | const char *no_proxy, SSL_CTX *ssl_ctx, |
29f178bd DDO |
2056 | const STACK_OF(CONF_VALUE) *headers, |
2057 | const char *content_type, | |
2058 | ASN1_VALUE *req, const ASN1_ITEM *req_it, | |
2059 | long timeout, const ASN1_ITEM *rsp_it) | |
2060 | { | |
2061 | APP_HTTP_TLS_INFO info; | |
2062 | ||
2063 | info.server = host; | |
2064 | info.port = port; | |
2065 | info.use_proxy = proxy != NULL; | |
2066 | info.timeout = timeout; | |
2067 | info.ssl_ctx = ssl_ctx; | |
2068 | return OSSL_HTTP_post_asn1(host, port, path, ssl_ctx != NULL, | |
afe554c2 | 2069 | proxy, no_proxy, |
29f178bd DDO |
2070 | NULL, NULL, app_http_tls_cb, &info, |
2071 | headers, content_type, req, req_it, | |
2072 | 0 /* maxline */, | |
2073 | 0 /* max_resp_len */, timeout, NULL, rsp_it); | |
2074 | } | |
2075 | ||
2076 | #endif | |
2077 | ||
0a39d8f2 AP |
2078 | /* |
2079 | * Platform-specific sections | |
2080 | */ | |
ffa10187 | 2081 | #if defined(_WIN32) |
a1ad253f AP |
2082 | # ifdef fileno |
2083 | # undef fileno | |
2084 | # define fileno(a) (int)_fileno(a) | |
2085 | # endif | |
2086 | ||
2087 | # include <windows.h> | |
2088 | # include <tchar.h> | |
2089 | ||
2090 | static int WIN32_rename(const char *from, const char *to) | |
0f113f3e MC |
2091 | { |
2092 | TCHAR *tfrom = NULL, *tto; | |
2093 | DWORD err; | |
2094 | int ret = 0; | |
2095 | ||
2096 | if (sizeof(TCHAR) == 1) { | |
2097 | tfrom = (TCHAR *)from; | |
2098 | tto = (TCHAR *)to; | |
2099 | } else { /* UNICODE path */ | |
2100 | ||
2101 | size_t i, flen = strlen(from) + 1, tlen = strlen(to) + 1; | |
b1ad95e3 | 2102 | tfrom = malloc(sizeof(*tfrom) * (flen + tlen)); |
0f113f3e MC |
2103 | if (tfrom == NULL) |
2104 | goto err; | |
2105 | tto = tfrom + flen; | |
2106 | # if !defined(_WIN32_WCE) || _WIN32_WCE>=101 | |
2107 | if (!MultiByteToWideChar(CP_ACP, 0, from, flen, (WCHAR *)tfrom, flen)) | |
2108 | # endif | |
2109 | for (i = 0; i < flen; i++) | |
2110 | tfrom[i] = (TCHAR)from[i]; | |
2111 | # if !defined(_WIN32_WCE) || _WIN32_WCE>=101 | |
2112 | if (!MultiByteToWideChar(CP_ACP, 0, to, tlen, (WCHAR *)tto, tlen)) | |
2113 | # endif | |
2114 | for (i = 0; i < tlen; i++) | |
2115 | tto[i] = (TCHAR)to[i]; | |
2116 | } | |
2117 | ||
2118 | if (MoveFile(tfrom, tto)) | |
2119 | goto ok; | |
2120 | err = GetLastError(); | |
2121 | if (err == ERROR_ALREADY_EXISTS || err == ERROR_FILE_EXISTS) { | |
2122 | if (DeleteFile(tto) && MoveFile(tfrom, tto)) | |
2123 | goto ok; | |
2124 | err = GetLastError(); | |
2125 | } | |
2126 | if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) | |
2127 | errno = ENOENT; | |
2128 | else if (err == ERROR_ACCESS_DENIED) | |
2129 | errno = EACCES; | |
2130 | else | |
2131 | errno = EINVAL; /* we could map more codes... */ | |
2132 | err: | |
2133 | ret = -1; | |
2134 | ok: | |
2135 | if (tfrom != NULL && tfrom != (TCHAR *)from) | |
2136 | free(tfrom); | |
2137 | return ret; | |
2138 | } | |
0a39d8f2 AP |
2139 | #endif |
2140 | ||
2141 | /* app_tminterval section */ | |
2142 | #if defined(_WIN32) | |
0f113f3e MC |
2143 | double app_tminterval(int stop, int usertime) |
2144 | { | |
2145 | FILETIME now; | |
2146 | double ret = 0; | |
2147 | static ULARGE_INTEGER tmstart; | |
2148 | static int warning = 1; | |
2149 | # ifdef _WIN32_WINNT | |
2150 | static HANDLE proc = NULL; | |
2151 | ||
2152 | if (proc == NULL) { | |
2153 | if (check_winnt()) | |
2154 | proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, | |
2155 | GetCurrentProcessId()); | |
2156 | if (proc == NULL) | |
2157 | proc = (HANDLE) - 1; | |
2158 | } | |
2159 | ||
2160 | if (usertime && proc != (HANDLE) - 1) { | |
2161 | FILETIME junk; | |
2162 | GetProcessTimes(proc, &junk, &junk, &junk, &now); | |
2163 | } else | |
2164 | # endif | |
2165 | { | |
2166 | SYSTEMTIME systime; | |
9135fddb | 2167 | |
0f113f3e MC |
2168 | if (usertime && warning) { |
2169 | BIO_printf(bio_err, "To get meaningful results, run " | |
2170 | "this program on idle system.\n"); | |
2171 | warning = 0; | |
2172 | } | |
2173 | GetSystemTime(&systime); | |
2174 | SystemTimeToFileTime(&systime, &now); | |
2175 | } | |
9135fddb | 2176 | |
0f113f3e MC |
2177 | if (stop == TM_START) { |
2178 | tmstart.u.LowPart = now.dwLowDateTime; | |
2179 | tmstart.u.HighPart = now.dwHighDateTime; | |
2180 | } else { | |
2181 | ULARGE_INTEGER tmstop; | |
9135fddb | 2182 | |
0f113f3e MC |
2183 | tmstop.u.LowPart = now.dwLowDateTime; |
2184 | tmstop.u.HighPart = now.dwHighDateTime; | |
9135fddb | 2185 | |
0f113f3e MC |
2186 | ret = (__int64)(tmstop.QuadPart - tmstart.QuadPart) * 1e-7; |
2187 | } | |
9135fddb | 2188 | |
26a7d938 | 2189 | return ret; |
0f113f3e | 2190 | } |
5c8b7b4c | 2191 | #elif defined(OPENSSL_SYS_VXWORKS) |
0f113f3e | 2192 | # include <time.h> |
0a39d8f2 | 2193 | |
0f113f3e MC |
2194 | double app_tminterval(int stop, int usertime) |
2195 | { | |
2196 | double ret = 0; | |
2197 | # ifdef CLOCK_REALTIME | |
2198 | static struct timespec tmstart; | |
2199 | struct timespec now; | |
2200 | # else | |
2201 | static unsigned long tmstart; | |
2202 | unsigned long now; | |
2203 | # endif | |
2204 | static int warning = 1; | |
2205 | ||
2206 | if (usertime && warning) { | |
2207 | BIO_printf(bio_err, "To get meaningful results, run " | |
2208 | "this program on idle system.\n"); | |
2209 | warning = 0; | |
2210 | } | |
2211 | # ifdef CLOCK_REALTIME | |
2212 | clock_gettime(CLOCK_REALTIME, &now); | |
2213 | if (stop == TM_START) | |
2214 | tmstart = now; | |
2215 | else | |
2216 | ret = ((now.tv_sec + now.tv_nsec * 1e-9) | |
2217 | - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9)); | |
2218 | # else | |
2219 | now = tickGet(); | |
2220 | if (stop == TM_START) | |
2221 | tmstart = now; | |
2222 | else | |
2223 | ret = (now - tmstart) / (double)sysClkRateGet(); | |
2224 | # endif | |
26a7d938 | 2225 | return ret; |
0f113f3e | 2226 | } |
0a39d8f2 | 2227 | |
0f113f3e MC |
2228 | #elif defined(OPENSSL_SYSTEM_VMS) |
2229 | # include <time.h> | |
2230 | # include <times.h> | |
0a39d8f2 | 2231 | |
0f113f3e MC |
2232 | double app_tminterval(int stop, int usertime) |
2233 | { | |
2234 | static clock_t tmstart; | |
2235 | double ret = 0; | |
2236 | clock_t now; | |
2237 | # ifdef __TMS | |
2238 | struct tms rus; | |
2239 | ||
2240 | now = times(&rus); | |
2241 | if (usertime) | |
2242 | now = rus.tms_utime; | |
2243 | # else | |
2244 | if (usertime) | |
2245 | now = clock(); /* sum of user and kernel times */ | |
2246 | else { | |
2247 | struct timeval tv; | |
2248 | gettimeofday(&tv, NULL); | |
2249 | now = (clock_t)((unsigned long long)tv.tv_sec * CLK_TCK + | |
2250 | (unsigned long long)tv.tv_usec * (1000000 / CLK_TCK) | |
2251 | ); | |
2252 | } | |
2253 | # endif | |
2254 | if (stop == TM_START) | |
2255 | tmstart = now; | |
2256 | else | |
2257 | ret = (now - tmstart) / (double)(CLK_TCK); | |
0a39d8f2 | 2258 | |
26a7d938 | 2259 | return ret; |
0f113f3e | 2260 | } |
0a39d8f2 | 2261 | |
0f113f3e MC |
2262 | #elif defined(_SC_CLK_TCK) /* by means of unistd.h */ |
2263 | # include <sys/times.h> | |
0a39d8f2 | 2264 | |
0f113f3e MC |
2265 | double app_tminterval(int stop, int usertime) |
2266 | { | |
2267 | double ret = 0; | |
2268 | struct tms rus; | |
2269 | clock_t now = times(&rus); | |
2270 | static clock_t tmstart; | |
2271 | ||
2272 | if (usertime) | |
2273 | now = rus.tms_utime; | |
2274 | ||
2234212c | 2275 | if (stop == TM_START) { |
0f113f3e | 2276 | tmstart = now; |
2234212c | 2277 | } else { |
0f113f3e MC |
2278 | long int tck = sysconf(_SC_CLK_TCK); |
2279 | ret = (now - tmstart) / (double)tck; | |
2280 | } | |
2281 | ||
26a7d938 | 2282 | return ret; |
0f113f3e | 2283 | } |
0a39d8f2 | 2284 | |
0f113f3e MC |
2285 | #else |
2286 | # include <sys/time.h> | |
2287 | # include <sys/resource.h> | |
0a39d8f2 | 2288 | |
0f113f3e MC |
2289 | double app_tminterval(int stop, int usertime) |
2290 | { | |
2291 | double ret = 0; | |
2292 | struct rusage rus; | |
2293 | struct timeval now; | |
2294 | static struct timeval tmstart; | |
2295 | ||
2296 | if (usertime) | |
2297 | getrusage(RUSAGE_SELF, &rus), now = rus.ru_utime; | |
2298 | else | |
2299 | gettimeofday(&now, NULL); | |
2300 | ||
2301 | if (stop == TM_START) | |
2302 | tmstart = now; | |
2303 | else | |
2304 | ret = ((now.tv_sec + now.tv_usec * 1e-6) | |
2305 | - (tmstart.tv_sec + tmstart.tv_usec * 1e-6)); | |
2306 | ||
2307 | return ret; | |
2308 | } | |
0a39d8f2 | 2309 | #endif |
a1ad253f | 2310 | |
7e1b7485 RS |
2311 | int app_access(const char* name, int flag) |
2312 | { | |
2313 | #ifdef _WIN32 | |
2314 | return _access(name, flag); | |
2315 | #else | |
2316 | return access(name, flag); | |
2317 | #endif | |
2318 | } | |
2319 | ||
ffa10187 | 2320 | int app_isdir(const char *name) |
0f113f3e | 2321 | { |
a43ce58f | 2322 | return opt_isdir(name); |
0f113f3e | 2323 | } |
ffa10187 | 2324 | |
0a39d8f2 | 2325 | /* raw_read|write section */ |
51e5133d RL |
2326 | #if defined(__VMS) |
2327 | # include "vms_term_sock.h" | |
2328 | static int stdin_sock = -1; | |
2329 | ||
2330 | static void close_stdin_sock(void) | |
2331 | { | |
2332 | TerminalSocket (TERM_SOCK_DELETE, &stdin_sock); | |
2333 | } | |
2334 | ||
2335 | int fileno_stdin(void) | |
2336 | { | |
2337 | if (stdin_sock == -1) { | |
2338 | TerminalSocket(TERM_SOCK_CREATE, &stdin_sock); | |
2339 | atexit(close_stdin_sock); | |
2340 | } | |
2341 | ||
2342 | return stdin_sock; | |
2343 | } | |
2344 | #else | |
2345 | int fileno_stdin(void) | |
2346 | { | |
2347 | return fileno(stdin); | |
2348 | } | |
2349 | #endif | |
2350 | ||
2351 | int fileno_stdout(void) | |
2352 | { | |
2353 | return fileno(stdout); | |
2354 | } | |
2355 | ||
ffa10187 | 2356 | #if defined(_WIN32) && defined(STD_INPUT_HANDLE) |
0f113f3e MC |
2357 | int raw_read_stdin(void *buf, int siz) |
2358 | { | |
2359 | DWORD n; | |
2360 | if (ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, siz, &n, NULL)) | |
26a7d938 | 2361 | return n; |
0f113f3e | 2362 | else |
26a7d938 | 2363 | return -1; |
0f113f3e | 2364 | } |
51e5133d | 2365 | #elif defined(__VMS) |
2234212c | 2366 | # include <sys/socket.h> |
a19228b7 | 2367 | |
51e5133d RL |
2368 | int raw_read_stdin(void *buf, int siz) |
2369 | { | |
2370 | return recv(fileno_stdin(), buf, siz, 0); | |
2371 | } | |
ffa10187 | 2372 | #else |
0f113f3e MC |
2373 | int raw_read_stdin(void *buf, int siz) |
2374 | { | |
51e5133d | 2375 | return read(fileno_stdin(), buf, siz); |
0f113f3e | 2376 | } |
ffa10187 AP |
2377 | #endif |
2378 | ||
2379 | #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE) | |
0f113f3e MC |
2380 | int raw_write_stdout(const void *buf, int siz) |
2381 | { | |
2382 | DWORD n; | |
2383 | if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, siz, &n, NULL)) | |
26a7d938 | 2384 | return n; |
0f113f3e | 2385 | else |
26a7d938 | 2386 | return -1; |
0f113f3e | 2387 | } |
ffa10187 | 2388 | #else |
0f113f3e MC |
2389 | int raw_write_stdout(const void *buf, int siz) |
2390 | { | |
51e5133d | 2391 | return write(fileno_stdout(), buf, siz); |
0f113f3e | 2392 | } |
ffa10187 | 2393 | #endif |
a412b891 RL |
2394 | |
2395 | /* | |
a43ce58f | 2396 | * Centralized handling of input and output files with format specification |
a412b891 RL |
2397 | * The format is meant to show what the input and output is supposed to be, |
2398 | * and is therefore a show of intent more than anything else. However, it | |
a43ce58f | 2399 | * does impact behavior on some platforms, such as differentiating between |
a412b891 RL |
2400 | * text and binary input/output on non-Unix platforms |
2401 | */ | |
a60994df | 2402 | BIO *dup_bio_in(int format) |
a412b891 | 2403 | { |
a60994df | 2404 | return BIO_new_fp(stdin, |
a43ce58f | 2405 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
a60994df RL |
2406 | } |
2407 | ||
2408 | BIO *dup_bio_out(int format) | |
2409 | { | |
2410 | BIO *b = BIO_new_fp(stdout, | |
a43ce58f | 2411 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
71bb86f0 RL |
2412 | void *prefix = NULL; |
2413 | ||
a412b891 | 2414 | #ifdef OPENSSL_SYS_VMS |
a43ce58f | 2415 | if (FMT_istext(format)) |
a60994df | 2416 | b = BIO_push(BIO_new(BIO_f_linebuffer()), b); |
149bd5d6 | 2417 | #endif |
71bb86f0 | 2418 | |
682b444f RL |
2419 | if (FMT_istext(format) |
2420 | && (prefix = getenv("HARNESS_OSSL_PREFIX")) != NULL) { | |
e79ae962 RL |
2421 | b = BIO_push(BIO_new(BIO_f_prefix()), b); |
2422 | BIO_set_prefix(b, prefix); | |
71bb86f0 RL |
2423 | } |
2424 | ||
149bd5d6 RL |
2425 | return b; |
2426 | } | |
2427 | ||
2428 | BIO *dup_bio_err(int format) | |
2429 | { | |
2430 | BIO *b = BIO_new_fp(stderr, | |
a43ce58f | 2431 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
149bd5d6 | 2432 | #ifdef OPENSSL_SYS_VMS |
a43ce58f | 2433 | if (FMT_istext(format)) |
149bd5d6 | 2434 | b = BIO_push(BIO_new(BIO_f_linebuffer()), b); |
a412b891 RL |
2435 | #endif |
2436 | return b; | |
2437 | } | |
2438 | ||
2439 | void unbuffer(FILE *fp) | |
2440 | { | |
90dbd250 RL |
2441 | /* |
2442 | * On VMS, setbuf() will only take 32-bit pointers, and a compilation | |
2443 | * with /POINTER_SIZE=64 will give off a MAYLOSEDATA2 warning here. | |
2444 | * However, we trust that the C RTL will never give us a FILE pointer | |
2445 | * above the first 4 GB of memory, so we simply turn off the warning | |
2446 | * temporarily. | |
2447 | */ | |
2448 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) | |
2449 | # pragma environment save | |
2450 | # pragma message disable maylosedata2 | |
2451 | #endif | |
a412b891 | 2452 | setbuf(fp, NULL); |
90dbd250 RL |
2453 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
2454 | # pragma environment restore | |
2455 | #endif | |
a412b891 RL |
2456 | } |
2457 | ||
2458 | static const char *modestr(char mode, int format) | |
2459 | { | |
2460 | OPENSSL_assert(mode == 'a' || mode == 'r' || mode == 'w'); | |
2461 | ||
2462 | switch (mode) { | |
2463 | case 'a': | |
a43ce58f | 2464 | return FMT_istext(format) ? "a" : "ab"; |
a412b891 | 2465 | case 'r': |
a43ce58f | 2466 | return FMT_istext(format) ? "r" : "rb"; |
a412b891 | 2467 | case 'w': |
a43ce58f | 2468 | return FMT_istext(format) ? "w" : "wb"; |
a412b891 RL |
2469 | } |
2470 | /* The assert above should make sure we never reach this point */ | |
2471 | return NULL; | |
2472 | } | |
2473 | ||
2474 | static const char *modeverb(char mode) | |
2475 | { | |
2476 | switch (mode) { | |
2477 | case 'a': | |
2478 | return "appending"; | |
2479 | case 'r': | |
2480 | return "reading"; | |
2481 | case 'w': | |
2482 | return "writing"; | |
2483 | } | |
2484 | return "(doing something)"; | |
2485 | } | |
2486 | ||
2487 | /* | |
2488 | * Open a file for writing, owner-read-only. | |
2489 | */ | |
2490 | BIO *bio_open_owner(const char *filename, int format, int private) | |
2491 | { | |
2492 | FILE *fp = NULL; | |
2493 | BIO *b = NULL; | |
1cd5cc36 | 2494 | int fd = -1, bflags, mode, textmode; |
a412b891 RL |
2495 | |
2496 | if (!private || filename == NULL || strcmp(filename, "-") == 0) | |
2497 | return bio_open_default(filename, 'w', format); | |
2498 | ||
2499 | mode = O_WRONLY; | |
2500 | #ifdef O_CREAT | |
2501 | mode |= O_CREAT; | |
2502 | #endif | |
2503 | #ifdef O_TRUNC | |
2504 | mode |= O_TRUNC; | |
2505 | #endif | |
a43ce58f | 2506 | textmode = FMT_istext(format); |
1cd5cc36 | 2507 | if (!textmode) { |
a412b891 RL |
2508 | #ifdef O_BINARY |
2509 | mode |= O_BINARY; | |
2510 | #elif defined(_O_BINARY) | |
2511 | mode |= _O_BINARY; | |
2512 | #endif | |
2513 | } | |
2514 | ||
fbd03b09 RL |
2515 | #ifdef OPENSSL_SYS_VMS |
2516 | /* VMS doesn't have O_BINARY, it just doesn't make sense. But, | |
2517 | * it still needs to know that we're going binary, or fdopen() | |
2518 | * will fail with "invalid argument"... so we tell VMS what the | |
2519 | * context is. | |
2520 | */ | |
2521 | if (!textmode) | |
2522 | fd = open(filename, mode, 0600, "ctx=bin"); | |
2523 | else | |
2524 | #endif | |
2525 | fd = open(filename, mode, 0600); | |
a412b891 RL |
2526 | if (fd < 0) |
2527 | goto err; | |
2528 | fp = fdopen(fd, modestr('w', format)); | |
2529 | if (fp == NULL) | |
2530 | goto err; | |
2531 | bflags = BIO_CLOSE; | |
1cd5cc36 | 2532 | if (textmode) |
a412b891 RL |
2533 | bflags |= BIO_FP_TEXT; |
2534 | b = BIO_new_fp(fp, bflags); | |
2535 | if (b) | |
2536 | return b; | |
2537 | ||
2538 | err: | |
2539 | BIO_printf(bio_err, "%s: Can't open \"%s\" for writing, %s\n", | |
2540 | opt_getprog(), filename, strerror(errno)); | |
2541 | ERR_print_errors(bio_err); | |
2542 | /* If we have fp, then fdopen took over fd, so don't close both. */ | |
2543 | if (fp) | |
2544 | fclose(fp); | |
2545 | else if (fd >= 0) | |
2546 | close(fd); | |
2547 | return NULL; | |
2548 | } | |
2549 | ||
2550 | static BIO *bio_open_default_(const char *filename, char mode, int format, | |
2551 | int quiet) | |
2552 | { | |
2553 | BIO *ret; | |
2554 | ||
2555 | if (filename == NULL || strcmp(filename, "-") == 0) { | |
a60994df | 2556 | ret = mode == 'r' ? dup_bio_in(format) : dup_bio_out(format); |
a412b891 RL |
2557 | if (quiet) { |
2558 | ERR_clear_error(); | |
2559 | return ret; | |
2560 | } | |
2561 | if (ret != NULL) | |
2562 | return ret; | |
2563 | BIO_printf(bio_err, | |
2564 | "Can't open %s, %s\n", | |
2565 | mode == 'r' ? "stdin" : "stdout", strerror(errno)); | |
2566 | } else { | |
2567 | ret = BIO_new_file(filename, modestr(mode, format)); | |
2568 | if (quiet) { | |
2569 | ERR_clear_error(); | |
2570 | return ret; | |
2571 | } | |
2572 | if (ret != NULL) | |
2573 | return ret; | |
2574 | BIO_printf(bio_err, | |
2575 | "Can't open %s for %s, %s\n", | |
2576 | filename, modeverb(mode), strerror(errno)); | |
2577 | } | |
2578 | ERR_print_errors(bio_err); | |
2579 | return NULL; | |
2580 | } | |
2581 | ||
2582 | BIO *bio_open_default(const char *filename, char mode, int format) | |
2583 | { | |
2584 | return bio_open_default_(filename, mode, format, 0); | |
2585 | } | |
2586 | ||
2587 | BIO *bio_open_default_quiet(const char *filename, char mode, int format) | |
2588 | { | |
2589 | return bio_open_default_(filename, mode, format, 1); | |
2590 | } | |
2591 | ||
e1b9840e MC |
2592 | void wait_for_async(SSL *s) |
2593 | { | |
d6e03b70 MC |
2594 | /* On Windows select only works for sockets, so we simply don't wait */ |
2595 | #ifndef OPENSSL_SYS_WINDOWS | |
ff75a257 | 2596 | int width = 0; |
e1b9840e | 2597 | fd_set asyncfds; |
ff75a257 MC |
2598 | OSSL_ASYNC_FD *fds; |
2599 | size_t numfds; | |
0a345252 | 2600 | size_t i; |
e1b9840e | 2601 | |
ff75a257 MC |
2602 | if (!SSL_get_all_async_fds(s, NULL, &numfds)) |
2603 | return; | |
2604 | if (numfds == 0) | |
e1b9840e | 2605 | return; |
589902b2 | 2606 | fds = app_malloc(sizeof(OSSL_ASYNC_FD) * numfds, "allocate async fds"); |
ff75a257 MC |
2607 | if (!SSL_get_all_async_fds(s, fds, &numfds)) { |
2608 | OPENSSL_free(fds); | |
0a345252 | 2609 | return; |
ff75a257 | 2610 | } |
e1b9840e | 2611 | |
e1b9840e | 2612 | FD_ZERO(&asyncfds); |
0a345252 P |
2613 | for (i = 0; i < numfds; i++) { |
2614 | if (width <= (int)fds[i]) | |
2615 | width = (int)fds[i] + 1; | |
2616 | openssl_fdset((int)fds[i], &asyncfds); | |
ff75a257 | 2617 | } |
e1b9840e | 2618 | select(width, (void *)&asyncfds, NULL, NULL, NULL); |
0a345252 | 2619 | OPENSSL_free(fds); |
d6e03b70 | 2620 | #endif |
e1b9840e | 2621 | } |
75dd6c1a MC |
2622 | |
2623 | /* if OPENSSL_SYS_WINDOWS is defined then so is OPENSSL_SYS_MSDOS */ | |
2624 | #if defined(OPENSSL_SYS_MSDOS) | |
2625 | int has_stdin_waiting(void) | |
2626 | { | |
2627 | # if defined(OPENSSL_SYS_WINDOWS) | |
2628 | HANDLE inhand = GetStdHandle(STD_INPUT_HANDLE); | |
2629 | DWORD events = 0; | |
2630 | INPUT_RECORD inputrec; | |
2631 | DWORD insize = 1; | |
2632 | BOOL peeked; | |
2633 | ||
2634 | if (inhand == INVALID_HANDLE_VALUE) { | |
2635 | return 0; | |
2636 | } | |
2637 | ||
2638 | peeked = PeekConsoleInput(inhand, &inputrec, insize, &events); | |
2639 | if (!peeked) { | |
2640 | /* Probably redirected input? _kbhit() does not work in this case */ | |
2641 | if (!feof(stdin)) { | |
2642 | return 1; | |
2643 | } | |
2644 | return 0; | |
2645 | } | |
2646 | # endif | |
2647 | return _kbhit(); | |
2648 | } | |
2649 | #endif | |
17ebf85a DSH |
2650 | |
2651 | /* Corrupt a signature by modifying final byte */ | |
a0754084 | 2652 | void corrupt_signature(const ASN1_STRING *signature) |
17ebf85a | 2653 | { |
a0754084 DSH |
2654 | unsigned char *s = signature->data; |
2655 | s[signature->length - 1] ^= 0x1; | |
17ebf85a | 2656 | } |
dc047d31 DSH |
2657 | |
2658 | int set_cert_times(X509 *x, const char *startdate, const char *enddate, | |
2659 | int days) | |
2660 | { | |
dc047d31 | 2661 | if (startdate == NULL || strcmp(startdate, "today") == 0) { |
0b7347ef DSH |
2662 | if (X509_gmtime_adj(X509_getm_notBefore(x), 0) == NULL) |
2663 | return 0; | |
2664 | } else { | |
04e62715 | 2665 | if (!ASN1_TIME_set_string_X509(X509_getm_notBefore(x), startdate)) |
0b7347ef | 2666 | return 0; |
dc047d31 | 2667 | } |
dc047d31 | 2668 | if (enddate == NULL) { |
0b7347ef DSH |
2669 | if (X509_time_adj_ex(X509_getm_notAfter(x), days, 0, NULL) |
2670 | == NULL) | |
2671 | return 0; | |
04e62715 | 2672 | } else if (!ASN1_TIME_set_string_X509(X509_getm_notAfter(x), enddate)) { |
0b7347ef | 2673 | return 0; |
dc047d31 | 2674 | } |
0b7347ef | 2675 | return 1; |
dc047d31 | 2676 | } |
20967afb RS |
2677 | |
2678 | void make_uppercase(char *string) | |
2679 | { | |
2680 | int i; | |
2681 | ||
2682 | for (i = 0; string[i] != '\0'; i++) | |
2683 | string[i] = toupper((unsigned char)string[i]); | |
2684 | } | |
a43ce58f SL |
2685 | |
2686 | int opt_printf_stderr(const char *fmt, ...) | |
2687 | { | |
2688 | va_list ap; | |
2689 | int ret; | |
2690 | ||
2691 | va_start(ap, fmt); | |
2692 | ret = BIO_vprintf(bio_err, fmt, ap); | |
2693 | va_end(ap); | |
2694 | return ret; | |
2695 | } | |
95214b43 SL |
2696 | |
2697 | OSSL_PARAM *app_params_new_from_opts(STACK_OF(OPENSSL_STRING) *opts, | |
2698 | const OSSL_PARAM *paramdefs) | |
2699 | { | |
2700 | OSSL_PARAM *params = NULL; | |
2701 | size_t sz = (size_t)sk_OPENSSL_STRING_num(opts); | |
2702 | size_t params_n; | |
2703 | char *opt = "", *stmp, *vtmp = NULL; | |
e1dcac22 | 2704 | int found = 1; |
95214b43 SL |
2705 | |
2706 | if (opts == NULL) | |
2707 | return NULL; | |
2708 | ||
2709 | params = OPENSSL_zalloc(sizeof(OSSL_PARAM) * (sz + 1)); | |
2710 | if (params == NULL) | |
2711 | return NULL; | |
2712 | ||
2713 | for (params_n = 0; params_n < sz; params_n++) { | |
2714 | opt = sk_OPENSSL_STRING_value(opts, (int)params_n); | |
2715 | if ((stmp = OPENSSL_strdup(opt)) == NULL | |
2716 | || (vtmp = strchr(stmp, ':')) == NULL) | |
2717 | goto err; | |
2718 | /* Replace ':' with 0 to terminate the string pointed to by stmp */ | |
2719 | *vtmp = 0; | |
2720 | /* Skip over the separator so that vmtp points to the value */ | |
2721 | vtmp++; | |
2722 | if (!OSSL_PARAM_allocate_from_text(¶ms[params_n], paramdefs, | |
e1dcac22 | 2723 | stmp, vtmp, strlen(vtmp), &found)) |
95214b43 SL |
2724 | goto err; |
2725 | OPENSSL_free(stmp); | |
2726 | } | |
2727 | params[params_n] = OSSL_PARAM_construct_end(); | |
2728 | return params; | |
2729 | err: | |
2730 | OPENSSL_free(stmp); | |
e1dcac22 P |
2731 | BIO_printf(bio_err, "Parameter %s '%s'\n", found ? "error" : "unknown", |
2732 | opt); | |
95214b43 SL |
2733 | ERR_print_errors(bio_err); |
2734 | app_params_free(params); | |
2735 | return NULL; | |
2736 | } | |
2737 | ||
2738 | void app_params_free(OSSL_PARAM *params) | |
2739 | { | |
2740 | int i; | |
2741 | ||
2742 | if (params != NULL) { | |
2743 | for (i = 0; params[i].key != NULL; ++i) | |
2744 | OPENSSL_free(params[i].data); | |
2745 | OPENSSL_free(params); | |
2746 | } | |
2747 | } |