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