Home | History | Annotate | Line # | Download | only in lib
      1  1.1  christos /*
      2  1.4  christos  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  *
      4  1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos  * in the file LICENSE in the source distribution or at
      7  1.1  christos  * https://www.openssl.org/source/license.html
      8  1.1  christos  */
      9  1.1  christos 
     10  1.1  christos #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
     11  1.1  christos /*
     12  1.1  christos  * On VMS, you need to define this to get the declaration of fileno().  The
     13  1.1  christos  * value 2 is to make sure no function defined in POSIX-2 is left undefined.
     14  1.1  christos  */
     15  1.1  christos # define _POSIX_C_SOURCE 2
     16  1.1  christos #endif
     17  1.1  christos 
     18  1.1  christos #ifndef OPENSSL_NO_ENGINE
     19  1.1  christos /* We need to use some deprecated APIs */
     20  1.1  christos # define OPENSSL_SUPPRESS_DEPRECATED
     21  1.1  christos # include <openssl/engine.h>
     22  1.1  christos #endif
     23  1.1  christos 
     24  1.1  christos #include <stdio.h>
     25  1.1  christos #include <stdlib.h>
     26  1.1  christos #include <string.h>
     27  1.1  christos #include <sys/types.h>
     28  1.1  christos #ifndef OPENSSL_NO_POSIX_IO
     29  1.1  christos # include <sys/stat.h>
     30  1.1  christos # include <fcntl.h>
     31  1.1  christos #endif
     32  1.1  christos #include <ctype.h>
     33  1.1  christos #include <errno.h>
     34  1.1  christos #include <openssl/err.h>
     35  1.1  christos #include <openssl/x509.h>
     36  1.1  christos #include <openssl/x509v3.h>
     37  1.1  christos #include <openssl/http.h>
     38  1.1  christos #include <openssl/pem.h>
     39  1.1  christos #include <openssl/store.h>
     40  1.1  christos #include <openssl/pkcs12.h>
     41  1.1  christos #include <openssl/ui.h>
     42  1.1  christos #include <openssl/safestack.h>
     43  1.1  christos #include <openssl/rsa.h>
     44  1.1  christos #include <openssl/rand.h>
     45  1.1  christos #include <openssl/bn.h>
     46  1.1  christos #include <openssl/ssl.h>
     47  1.1  christos #include <openssl/store.h>
     48  1.1  christos #include <openssl/core_names.h>
     49  1.1  christos #include "s_apps.h"
     50  1.1  christos #include "apps.h"
     51  1.1  christos 
     52  1.1  christos #ifdef _WIN32
     53  1.1  christos static int WIN32_rename(const char *from, const char *to);
     54  1.1  christos # define rename(from,to) WIN32_rename((from),(to))
     55  1.1  christos #endif
     56  1.1  christos 
     57  1.1  christos #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
     58  1.1  christos # include <conio.h>
     59  1.1  christos #endif
     60  1.1  christos 
     61  1.1  christos #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32) || defined(__BORLANDC__)
     62  1.1  christos # define _kbhit kbhit
     63  1.1  christos #endif
     64  1.1  christos 
     65  1.1  christos static BIO *bio_open_default_(const char *filename, char mode, int format,
     66  1.1  christos                               int quiet);
     67  1.1  christos 
     68  1.1  christos #define PASS_SOURCE_SIZE_MAX 4
     69  1.1  christos 
     70  1.1  christos DEFINE_STACK_OF(CONF)
     71  1.1  christos 
     72  1.1  christos typedef struct {
     73  1.1  christos     const char *name;
     74  1.1  christos     unsigned long flag;
     75  1.1  christos     unsigned long mask;
     76  1.1  christos } NAME_EX_TBL;
     77  1.1  christos 
     78  1.1  christos static int set_table_opts(unsigned long *flags, const char *arg,
     79  1.1  christos                           const NAME_EX_TBL * in_tbl);
     80  1.1  christos static int set_multi_opts(unsigned long *flags, const char *arg,
     81  1.1  christos                           const NAME_EX_TBL * in_tbl);
     82  1.1  christos static
     83  1.1  christos int load_key_certs_crls_suppress(const char *uri, int format, int maybe_stdin,
     84  1.1  christos                                  const char *pass, const char *desc,
     85  1.1  christos                                  EVP_PKEY **ppkey, EVP_PKEY **ppubkey,
     86  1.1  christos                                  EVP_PKEY **pparams,
     87  1.1  christos                                  X509 **pcert, STACK_OF(X509) **pcerts,
     88  1.1  christos                                  X509_CRL **pcrl, STACK_OF(X509_CRL) **pcrls,
     89  1.1  christos                                  int suppress_decode_errors);
     90  1.1  christos 
     91  1.1  christos int app_init(long mesgwin);
     92  1.1  christos 
     93  1.1  christos int chopup_args(ARGS *arg, char *buf)
     94  1.1  christos {
     95  1.1  christos     int quoted;
     96  1.1  christos     char c = '\0', *p = NULL;
     97  1.1  christos 
     98  1.1  christos     arg->argc = 0;
     99  1.1  christos     if (arg->size == 0) {
    100  1.1  christos         arg->size = 20;
    101  1.1  christos         arg->argv = app_malloc(sizeof(*arg->argv) * arg->size, "argv space");
    102  1.1  christos     }
    103  1.1  christos 
    104  1.1  christos     for (p = buf;;) {
    105  1.1  christos         /* Skip whitespace. */
    106  1.1  christos         while (*p && isspace(_UC(*p)))
    107  1.1  christos             p++;
    108  1.1  christos         if (*p == '\0')
    109  1.1  christos             break;
    110  1.1  christos 
    111  1.1  christos         /* The start of something good :-) */
    112  1.1  christos         if (arg->argc >= arg->size) {
    113  1.1  christos             char **tmp;
    114  1.1  christos             arg->size += 20;
    115  1.1  christos             tmp = OPENSSL_realloc(arg->argv, sizeof(*arg->argv) * arg->size);
    116  1.1  christos             if (tmp == NULL)
    117  1.1  christos                 return 0;
    118  1.1  christos             arg->argv = tmp;
    119  1.1  christos         }
    120  1.1  christos         quoted = *p == '\'' || *p == '"';
    121  1.1  christos         if (quoted)
    122  1.1  christos             c = *p++;
    123  1.1  christos         arg->argv[arg->argc++] = p;
    124  1.1  christos 
    125  1.1  christos         /* now look for the end of this */
    126  1.1  christos         if (quoted) {
    127  1.1  christos             while (*p && *p != c)
    128  1.1  christos                 p++;
    129  1.1  christos             *p++ = '\0';
    130  1.1  christos         } else {
    131  1.1  christos             while (*p && !isspace(_UC(*p)))
    132  1.1  christos                 p++;
    133  1.1  christos             if (*p)
    134  1.1  christos                 *p++ = '\0';
    135  1.1  christos         }
    136  1.1  christos     }
    137  1.1  christos     arg->argv[arg->argc] = NULL;
    138  1.1  christos     return 1;
    139  1.1  christos }
    140  1.1  christos 
    141  1.1  christos #ifndef APP_INIT
    142  1.1  christos int app_init(long mesgwin)
    143  1.1  christos {
    144  1.1  christos     return 1;
    145  1.1  christos }
    146  1.1  christos #endif
    147  1.1  christos 
    148  1.1  christos int ctx_set_verify_locations(SSL_CTX *ctx,
    149  1.1  christos                              const char *CAfile, int noCAfile,
    150  1.1  christos                              const char *CApath, int noCApath,
    151  1.1  christos                              const char *CAstore, int noCAstore)
    152  1.1  christos {
    153  1.1  christos     if (CAfile == NULL && CApath == NULL && CAstore == NULL) {
    154  1.1  christos         if (!noCAfile && SSL_CTX_set_default_verify_file(ctx) <= 0)
    155  1.1  christos             return 0;
    156  1.1  christos         if (!noCApath && SSL_CTX_set_default_verify_dir(ctx) <= 0)
    157  1.1  christos             return 0;
    158  1.1  christos         if (!noCAstore && SSL_CTX_set_default_verify_store(ctx) <= 0)
    159  1.1  christos             return 0;
    160  1.1  christos 
    161  1.1  christos         return 1;
    162  1.1  christos     }
    163  1.1  christos 
    164  1.1  christos     if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
    165  1.1  christos         return 0;
    166  1.1  christos     if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
    167  1.1  christos         return 0;
    168  1.1  christos     if (CAstore != NULL && !SSL_CTX_load_verify_store(ctx, CAstore))
    169  1.1  christos         return 0;
    170  1.1  christos     return 1;
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos #ifndef OPENSSL_NO_CT
    174  1.1  christos 
    175  1.1  christos int ctx_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
    176  1.1  christos {
    177  1.1  christos     if (path == NULL)
    178  1.1  christos         return SSL_CTX_set_default_ctlog_list_file(ctx);
    179  1.1  christos 
    180  1.1  christos     return SSL_CTX_set_ctlog_list_file(ctx, path);
    181  1.1  christos }
    182  1.1  christos 
    183  1.1  christos #endif
    184  1.1  christos 
    185  1.1  christos static unsigned long nmflag = 0;
    186  1.1  christos static char nmflag_set = 0;
    187  1.1  christos 
    188  1.1  christos int set_nameopt(const char *arg)
    189  1.1  christos {
    190  1.1  christos     int ret = set_name_ex(&nmflag, arg);
    191  1.1  christos 
    192  1.1  christos     if (ret)
    193  1.1  christos         nmflag_set = 1;
    194  1.1  christos 
    195  1.1  christos     return ret;
    196  1.1  christos }
    197  1.1  christos 
    198  1.1  christos unsigned long get_nameopt(void)
    199  1.1  christos {
    200  1.1  christos     return (nmflag_set) ? nmflag : XN_FLAG_ONELINE;
    201  1.1  christos }
    202  1.1  christos 
    203  1.1  christos void dump_cert_text(BIO *out, X509 *x)
    204  1.1  christos {
    205  1.1  christos     print_name(out, "subject=", X509_get_subject_name(x));
    206  1.1  christos     print_name(out, "issuer=", X509_get_issuer_name(x));
    207  1.1  christos }
    208  1.1  christos 
    209  1.1  christos int wrap_password_callback(char *buf, int bufsiz, int verify, void *userdata)
    210  1.1  christos {
    211  1.1  christos     return password_callback(buf, bufsiz, verify, (PW_CB_DATA *)userdata);
    212  1.1  christos }
    213  1.1  christos 
    214  1.1  christos 
    215  1.1  christos static char *app_get_pass(const char *arg, int keepbio);
    216  1.1  christos 
    217  1.1  christos char *get_passwd(const char *pass, const char *desc)
    218  1.1  christos {
    219  1.1  christos     char *result = NULL;
    220  1.1  christos 
    221  1.1  christos     if (desc == NULL)
    222  1.1  christos         desc = "<unknown>";
    223  1.1  christos     if (!app_passwd(pass, NULL, &result, NULL))
    224  1.1  christos         BIO_printf(bio_err, "Error getting password for %s\n", desc);
    225  1.1  christos     if (pass != NULL && result == NULL) {
    226  1.1  christos         BIO_printf(bio_err,
    227  1.1  christos                    "Trying plain input string (better precede with 'pass:')\n");
    228  1.1  christos         result = OPENSSL_strdup(pass);
    229  1.1  christos         if (result == NULL)
    230  1.1  christos             BIO_printf(bio_err, "Out of memory getting password for %s\n", desc);
    231  1.1  christos     }
    232  1.1  christos     return result;
    233  1.1  christos }
    234  1.1  christos 
    235  1.1  christos int app_passwd(const char *arg1, const char *arg2, char **pass1, char **pass2)
    236  1.1  christos {
    237  1.1  christos     int same = arg1 != NULL && arg2 != NULL && strcmp(arg1, arg2) == 0;
    238  1.1  christos 
    239  1.1  christos     if (arg1 != NULL) {
    240  1.1  christos         *pass1 = app_get_pass(arg1, same);
    241  1.1  christos         if (*pass1 == NULL)
    242  1.1  christos             return 0;
    243  1.1  christos     } else if (pass1 != NULL) {
    244  1.1  christos         *pass1 = NULL;
    245  1.1  christos     }
    246  1.1  christos     if (arg2 != NULL) {
    247  1.1  christos         *pass2 = app_get_pass(arg2, same ? 2 : 0);
    248  1.1  christos         if (*pass2 == NULL)
    249  1.1  christos             return 0;
    250  1.1  christos     } else if (pass2 != NULL) {
    251  1.1  christos         *pass2 = NULL;
    252  1.1  christos     }
    253  1.1  christos     return 1;
    254  1.1  christos }
    255  1.1  christos 
    256  1.1  christos static char *app_get_pass(const char *arg, int keepbio)
    257  1.1  christos {
    258  1.1  christos     static BIO *pwdbio = NULL;
    259  1.1  christos     char *tmp, tpass[APP_PASS_LEN];
    260  1.1  christos     int i;
    261  1.1  christos 
    262  1.1  christos     /* PASS_SOURCE_SIZE_MAX = max number of chars before ':' in below strings */
    263  1.1  christos     if (strncmp(arg, "pass:", 5) == 0)
    264  1.1  christos         return OPENSSL_strdup(arg + 5);
    265  1.1  christos     if (strncmp(arg, "env:", 4) == 0) {
    266  1.1  christos         tmp = getenv(arg + 4);
    267  1.1  christos         if (tmp == NULL) {
    268  1.1  christos             BIO_printf(bio_err, "No environment variable %s\n", arg + 4);
    269  1.1  christos             return NULL;
    270  1.1  christos         }
    271  1.1  christos         return OPENSSL_strdup(tmp);
    272  1.1  christos     }
    273  1.1  christos     if (!keepbio || pwdbio == NULL) {
    274  1.1  christos         if (strncmp(arg, "file:", 5) == 0) {
    275  1.1  christos             pwdbio = BIO_new_file(arg + 5, "r");
    276  1.1  christos             if (pwdbio == NULL) {
    277  1.1  christos                 BIO_printf(bio_err, "Can't open file %s\n", arg + 5);
    278  1.1  christos                 return NULL;
    279  1.1  christos             }
    280  1.1  christos #if !defined(_WIN32)
    281  1.1  christos             /*
    282  1.1  christos              * Under _WIN32, which covers even Win64 and CE, file
    283  1.1  christos              * descriptors referenced by BIO_s_fd are not inherited
    284  1.1  christos              * by child process and therefore below is not an option.
    285  1.1  christos              * It could have been an option if bss_fd.c was operating
    286  1.1  christos              * on real Windows descriptors, such as those obtained
    287  1.1  christos              * with CreateFile.
    288  1.1  christos              */
    289  1.1  christos         } else if (strncmp(arg, "fd:", 3) == 0) {
    290  1.1  christos             BIO *btmp;
    291  1.1  christos             i = atoi(arg + 3);
    292  1.1  christos             if (i >= 0)
    293  1.1  christos                 pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
    294  1.1  christos             if ((i < 0) || pwdbio == NULL) {
    295  1.1  christos                 BIO_printf(bio_err, "Can't access file descriptor %s\n", arg + 3);
    296  1.1  christos                 return NULL;
    297  1.1  christos             }
    298  1.1  christos             /*
    299  1.1  christos              * Can't do BIO_gets on an fd BIO so add a buffering BIO
    300  1.1  christos              */
    301  1.1  christos             btmp = BIO_new(BIO_f_buffer());
    302  1.1  christos             if (btmp == NULL) {
    303  1.1  christos                 BIO_free_all(pwdbio);
    304  1.1  christos                 pwdbio = NULL;
    305  1.1  christos                 BIO_printf(bio_err, "Out of memory\n");
    306  1.1  christos                 return NULL;
    307  1.1  christos             }
    308  1.1  christos             pwdbio = BIO_push(btmp, pwdbio);
    309  1.1  christos #endif
    310  1.1  christos         } else if (strcmp(arg, "stdin") == 0) {
    311  1.1  christos             unbuffer(stdin);
    312  1.1  christos             pwdbio = dup_bio_in(FORMAT_TEXT);
    313  1.1  christos             if (pwdbio == NULL) {
    314  1.1  christos                 BIO_printf(bio_err, "Can't open BIO for stdin\n");
    315  1.1  christos                 return NULL;
    316  1.1  christos             }
    317  1.1  christos         } else {
    318  1.1  christos             /* argument syntax error; do not reveal too much about arg */
    319  1.1  christos             tmp = strchr(arg, ':');
    320  1.1  christos             if (tmp == NULL || tmp - arg > PASS_SOURCE_SIZE_MAX)
    321  1.1  christos                 BIO_printf(bio_err,
    322  1.1  christos                            "Invalid password argument, missing ':' within the first %d chars\n",
    323  1.1  christos                            PASS_SOURCE_SIZE_MAX + 1);
    324  1.1  christos             else
    325  1.1  christos                 BIO_printf(bio_err,
    326  1.1  christos                            "Invalid password argument, starting with \"%.*s\"\n",
    327  1.1  christos                            (int)(tmp - arg + 1), arg);
    328  1.1  christos             return NULL;
    329  1.1  christos         }
    330  1.1  christos     }
    331  1.1  christos     i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
    332  1.1  christos     if (keepbio != 1) {
    333  1.1  christos         BIO_free_all(pwdbio);
    334  1.1  christos         pwdbio = NULL;
    335  1.1  christos     }
    336  1.1  christos     if (i <= 0) {
    337  1.1  christos         BIO_printf(bio_err, "Error reading password from BIO\n");
    338  1.1  christos         return NULL;
    339  1.1  christos     }
    340  1.1  christos     tmp = strchr(tpass, '\n');
    341  1.1  christos     if (tmp != NULL)
    342  1.1  christos         *tmp = 0;
    343  1.1  christos     return OPENSSL_strdup(tpass);
    344  1.1  christos }
    345  1.1  christos 
    346  1.1  christos CONF *app_load_config_bio(BIO *in, const char *filename)
    347  1.1  christos {
    348  1.1  christos     long errorline = -1;
    349  1.1  christos     CONF *conf;
    350  1.1  christos     int i;
    351  1.1  christos 
    352  1.1  christos     conf = NCONF_new_ex(app_get0_libctx(), NULL);
    353  1.1  christos     i = NCONF_load_bio(conf, in, &errorline);
    354  1.1  christos     if (i > 0)
    355  1.1  christos         return conf;
    356  1.1  christos 
    357  1.1  christos     if (errorline <= 0) {
    358  1.1  christos         BIO_printf(bio_err, "%s: Can't load ", opt_getprog());
    359  1.1  christos     } else {
    360  1.1  christos         BIO_printf(bio_err, "%s: Error on line %ld of ", opt_getprog(),
    361  1.1  christos                    errorline);
    362  1.1  christos     }
    363  1.1  christos     if (filename != NULL)
    364  1.1  christos         BIO_printf(bio_err, "config file \"%s\"\n", filename);
    365  1.1  christos     else
    366  1.1  christos         BIO_printf(bio_err, "config input");
    367  1.1  christos 
    368  1.1  christos     NCONF_free(conf);
    369  1.1  christos     return NULL;
    370  1.1  christos }
    371  1.1  christos 
    372  1.1  christos CONF *app_load_config_verbose(const char *filename, int verbose)
    373  1.1  christos {
    374  1.1  christos     if (verbose) {
    375  1.1  christos         if (*filename == '\0')
    376  1.1  christos             BIO_printf(bio_err, "No configuration used\n");
    377  1.1  christos         else
    378  1.1  christos             BIO_printf(bio_err, "Using configuration from %s\n", filename);
    379  1.1  christos     }
    380  1.1  christos     return app_load_config_internal(filename, 0);
    381  1.1  christos }
    382  1.1  christos 
    383  1.1  christos CONF *app_load_config_internal(const char *filename, int quiet)
    384  1.1  christos {
    385  1.1  christos     BIO *in;
    386  1.1  christos     CONF *conf;
    387  1.1  christos 
    388  1.1  christos     if (filename == NULL || *filename != '\0') {
    389  1.1  christos         if ((in = bio_open_default_(filename, 'r', FORMAT_TEXT, quiet)) == NULL)
    390  1.1  christos             return NULL;
    391  1.1  christos         conf = app_load_config_bio(in, filename);
    392  1.1  christos         BIO_free(in);
    393  1.1  christos     } else {
    394  1.1  christos         /* Return empty config if filename is empty string. */
    395  1.1  christos         conf = NCONF_new_ex(app_get0_libctx(), NULL);
    396  1.1  christos     }
    397  1.1  christos     return conf;
    398  1.1  christos }
    399  1.1  christos 
    400  1.1  christos int app_load_modules(const CONF *config)
    401  1.1  christos {
    402  1.1  christos     CONF *to_free = NULL;
    403  1.1  christos 
    404  1.1  christos     if (config == NULL)
    405  1.1  christos         config = to_free = app_load_config_quiet(default_config_file);
    406  1.1  christos     if (config == NULL)
    407  1.1  christos         return 1;
    408  1.1  christos 
    409  1.1  christos     if (CONF_modules_load(config, NULL, 0) <= 0) {
    410  1.1  christos         BIO_printf(bio_err, "Error configuring OpenSSL modules\n");
    411  1.1  christos         ERR_print_errors(bio_err);
    412  1.1  christos         NCONF_free(to_free);
    413  1.1  christos         return 0;
    414  1.1  christos     }
    415  1.1  christos     NCONF_free(to_free);
    416  1.1  christos     return 1;
    417  1.1  christos }
    418  1.1  christos 
    419  1.1  christos int add_oid_section(CONF *conf)
    420  1.1  christos {
    421  1.1  christos     char *p;
    422  1.1  christos     STACK_OF(CONF_VALUE) *sktmp;
    423  1.1  christos     CONF_VALUE *cnf;
    424  1.1  christos     int i;
    425  1.1  christos 
    426  1.1  christos     if ((p = NCONF_get_string(conf, NULL, "oid_section")) == NULL) {
    427  1.1  christos         ERR_clear_error();
    428  1.1  christos         return 1;
    429  1.1  christos     }
    430  1.1  christos     if ((sktmp = NCONF_get_section(conf, p)) == NULL) {
    431  1.1  christos         BIO_printf(bio_err, "problem loading oid section %s\n", p);
    432  1.1  christos         return 0;
    433  1.1  christos     }
    434  1.1  christos     for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
    435  1.1  christos         cnf = sk_CONF_VALUE_value(sktmp, i);
    436  1.1  christos         if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
    437  1.1  christos             BIO_printf(bio_err, "problem creating object %s=%s\n",
    438  1.1  christos                        cnf->name, cnf->value);
    439  1.1  christos             return 0;
    440  1.1  christos         }
    441  1.1  christos     }
    442  1.1  christos     return 1;
    443  1.1  christos }
    444  1.1  christos 
    445  1.1  christos CONF *app_load_config_modules(const char *configfile)
    446  1.1  christos {
    447  1.1  christos     CONF *conf = NULL;
    448  1.1  christos 
    449  1.1  christos     if (configfile != NULL) {
    450  1.1  christos         if ((conf = app_load_config_verbose(configfile, 1)) == NULL)
    451  1.1  christos             return NULL;
    452  1.1  christos         if (configfile != default_config_file && !app_load_modules(conf)) {
    453  1.1  christos             NCONF_free(conf);
    454  1.1  christos             conf = NULL;
    455  1.1  christos         }
    456  1.1  christos     }
    457  1.1  christos     return conf;
    458  1.1  christos }
    459  1.1  christos 
    460  1.1  christos #define IS_HTTP(uri) ((uri) != NULL \
    461  1.1  christos         && strncmp(uri, OSSL_HTTP_PREFIX, strlen(OSSL_HTTP_PREFIX)) == 0)
    462  1.1  christos #define IS_HTTPS(uri) ((uri) != NULL \
    463  1.1  christos         && strncmp(uri, OSSL_HTTPS_PREFIX, strlen(OSSL_HTTPS_PREFIX)) == 0)
    464  1.1  christos 
    465  1.1  christos X509 *load_cert_pass(const char *uri, int format, int maybe_stdin,
    466  1.1  christos                      const char *pass, const char *desc)
    467  1.1  christos {
    468  1.1  christos     X509 *cert = NULL;
    469  1.1  christos 
    470  1.1  christos     if (desc == NULL)
    471  1.1  christos         desc = "certificate";
    472  1.1  christos     if (IS_HTTPS(uri))
    473  1.1  christos         BIO_printf(bio_err, "Loading %s over HTTPS is unsupported\n", desc);
    474  1.1  christos     else if (IS_HTTP(uri))
    475  1.1  christos         cert = X509_load_http(uri, NULL, NULL, 0 /* timeout */);
    476  1.1  christos     else
    477  1.1  christos         (void)load_key_certs_crls(uri, format, maybe_stdin, pass, desc,
    478  1.1  christos                                   NULL, NULL, NULL, &cert, NULL, NULL, NULL);
    479  1.1  christos     if (cert == NULL) {
    480  1.1  christos         BIO_printf(bio_err, "Unable to load %s\n", desc);
    481  1.1  christos         ERR_print_errors(bio_err);
    482  1.1  christos     }
    483  1.1  christos     return cert;
    484  1.1  christos }
    485  1.1  christos 
    486  1.1  christos X509_CRL *load_crl(const char *uri, int format, int maybe_stdin,
    487  1.1  christos                    const char *desc)
    488  1.1  christos {
    489  1.1  christos     X509_CRL *crl = NULL;
    490  1.1  christos 
    491  1.1  christos     if (desc == NULL)
    492  1.1  christos         desc = "CRL";
    493  1.1  christos     if (IS_HTTPS(uri))
    494  1.1  christos         BIO_printf(bio_err, "Loading %s over HTTPS is unsupported\n", desc);
    495  1.1  christos     else if (IS_HTTP(uri))
    496  1.1  christos         crl = X509_CRL_load_http(uri, NULL, NULL, 0 /* timeout */);
    497  1.1  christos     else
    498  1.1  christos         (void)load_key_certs_crls(uri, format, maybe_stdin, NULL, desc,
    499  1.1  christos                                   NULL, NULL,  NULL, NULL, NULL, &crl, NULL);
    500  1.1  christos     if (crl == NULL) {
    501  1.1  christos         BIO_printf(bio_err, "Unable to load %s\n", desc);
    502  1.1  christos         ERR_print_errors(bio_err);
    503  1.1  christos     }
    504  1.1  christos     return crl;
    505  1.1  christos }
    506  1.1  christos 
    507  1.1  christos X509_REQ *load_csr(const char *file, int format, const char *desc)
    508  1.1  christos {
    509  1.1  christos     X509_REQ *req = NULL;
    510  1.1  christos     BIO *in;
    511  1.1  christos 
    512  1.1  christos     if (format == FORMAT_UNDEF)
    513  1.1  christos         format = FORMAT_PEM;
    514  1.1  christos     if (desc == NULL)
    515  1.1  christos         desc = "CSR";
    516  1.1  christos     in = bio_open_default(file, 'r', format);
    517  1.1  christos     if (in == NULL)
    518  1.1  christos         goto end;
    519  1.1  christos 
    520  1.1  christos     if (format == FORMAT_ASN1)
    521  1.1  christos         req = d2i_X509_REQ_bio(in, NULL);
    522  1.1  christos     else if (format == FORMAT_PEM)
    523  1.1  christos         req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL);
    524  1.1  christos     else
    525  1.1  christos         print_format_error(format, OPT_FMT_PEMDER);
    526  1.1  christos 
    527  1.1  christos  end:
    528  1.1  christos     if (req == NULL) {
    529  1.1  christos         BIO_printf(bio_err, "Unable to load %s\n", desc);
    530  1.1  christos         ERR_print_errors(bio_err);
    531  1.1  christos     }
    532  1.1  christos     BIO_free(in);
    533  1.1  christos     return req;
    534  1.1  christos }
    535  1.1  christos 
    536  1.1  christos void cleanse(char *str)
    537  1.1  christos {
    538  1.1  christos     if (str != NULL)
    539  1.1  christos         OPENSSL_cleanse(str, strlen(str));
    540  1.1  christos }
    541  1.1  christos 
    542  1.1  christos void clear_free(char *str)
    543  1.1  christos {
    544  1.1  christos     if (str != NULL)
    545  1.1  christos         OPENSSL_clear_free(str, strlen(str));
    546  1.1  christos }
    547  1.1  christos 
    548  1.1  christos EVP_PKEY *load_key(const char *uri, int format, int may_stdin,
    549  1.1  christos                    const char *pass, ENGINE *e, const char *desc)
    550  1.1  christos {
    551  1.1  christos     EVP_PKEY *pkey = NULL;
    552  1.1  christos     char *allocated_uri = NULL;
    553  1.1  christos 
    554  1.1  christos     if (desc == NULL)
    555  1.1  christos         desc = "private key";
    556  1.1  christos 
    557  1.1  christos     if (format == FORMAT_ENGINE) {
    558  1.1  christos         uri = allocated_uri = make_engine_uri(e, uri, desc);
    559  1.1  christos     }
    560  1.1  christos     (void)load_key_certs_crls(uri, format, may_stdin, pass, desc,
    561  1.1  christos                               &pkey, NULL, NULL, NULL, NULL, NULL, NULL);
    562  1.1  christos 
    563  1.1  christos     OPENSSL_free(allocated_uri);
    564  1.1  christos     return pkey;
    565  1.1  christos }
    566  1.1  christos 
    567  1.1  christos EVP_PKEY *load_pubkey(const char *uri, int format, int maybe_stdin,
    568  1.1  christos                       const char *pass, ENGINE *e, const char *desc)
    569  1.1  christos {
    570  1.1  christos     EVP_PKEY *pkey = NULL;
    571  1.1  christos     char *allocated_uri = NULL;
    572  1.1  christos 
    573  1.1  christos     if (desc == NULL)
    574  1.1  christos         desc = "public key";
    575  1.1  christos 
    576  1.1  christos     if (format == FORMAT_ENGINE) {
    577  1.1  christos         uri = allocated_uri = make_engine_uri(e, uri, desc);
    578  1.1  christos     }
    579  1.1  christos     (void)load_key_certs_crls(uri, format, maybe_stdin, pass, desc,
    580  1.1  christos                               NULL, &pkey, NULL, NULL, NULL, NULL, NULL);
    581  1.1  christos 
    582  1.1  christos     OPENSSL_free(allocated_uri);
    583  1.1  christos     return pkey;
    584  1.1  christos }
    585  1.1  christos 
    586  1.1  christos EVP_PKEY *load_keyparams_suppress(const char *uri, int format, int maybe_stdin,
    587  1.1  christos                                  const char *keytype, const char *desc,
    588  1.1  christos                                  int suppress_decode_errors)
    589  1.1  christos {
    590  1.1  christos     EVP_PKEY *params = NULL;
    591  1.1  christos 
    592  1.1  christos     if (desc == NULL)
    593  1.1  christos         desc = "key parameters";
    594  1.1  christos 
    595  1.1  christos     (void)load_key_certs_crls_suppress(uri, format, maybe_stdin, NULL, desc,
    596  1.1  christos                                        NULL, NULL, &params, NULL, NULL, NULL,
    597  1.1  christos                                        NULL, suppress_decode_errors);
    598  1.1  christos     if (params != NULL && keytype != NULL && !EVP_PKEY_is_a(params, keytype)) {
    599  1.1  christos         if (!suppress_decode_errors) {
    600  1.1  christos             BIO_printf(bio_err,
    601  1.1  christos                        "Unable to load %s from %s (unexpected parameters type)\n",
    602  1.1  christos                        desc, uri);
    603  1.1  christos             ERR_print_errors(bio_err);
    604  1.1  christos         }
    605  1.1  christos         EVP_PKEY_free(params);
    606  1.1  christos         params = NULL;
    607  1.1  christos     }
    608  1.1  christos     return params;
    609  1.1  christos }
    610  1.1  christos 
    611  1.1  christos EVP_PKEY *load_keyparams(const char *uri, int format, int maybe_stdin,
    612  1.1  christos                          const char *keytype, const char *desc)
    613  1.1  christos {
    614  1.1  christos     return load_keyparams_suppress(uri, format, maybe_stdin, keytype, desc, 0);
    615  1.1  christos }
    616  1.1  christos 
    617  1.7  christos void app_bail_out(const char *fmt, ...)
    618  1.1  christos {
    619  1.1  christos     va_list args;
    620  1.1  christos 
    621  1.1  christos     va_start(args, fmt);
    622  1.1  christos     BIO_vprintf(bio_err, fmt, args);
    623  1.1  christos     va_end(args);
    624  1.1  christos     ERR_print_errors(bio_err);
    625  1.1  christos     exit(EXIT_FAILURE);
    626  1.1  christos }
    627  1.1  christos 
    628  1.1  christos void *app_malloc(size_t sz, const char *what)
    629  1.1  christos {
    630  1.1  christos     void *vp = OPENSSL_malloc(sz);
    631  1.1  christos 
    632  1.1  christos     if (vp == NULL)
    633  1.1  christos         app_bail_out("%s: Could not allocate %zu bytes for %s\n",
    634  1.1  christos                      opt_getprog(), sz, what);
    635  1.1  christos     return vp;
    636  1.1  christos }
    637  1.1  christos 
    638  1.1  christos char *next_item(char *opt) /* in list separated by comma and/or space */
    639  1.1  christos {
    640  1.1  christos     /* advance to separator (comma or whitespace), if any */
    641  1.5  christos     while (*opt != ',' && !isspace(_UC(*opt)) && *opt != '\0')
    642  1.1  christos         opt++;
    643  1.1  christos     if (*opt != '\0') {
    644  1.1  christos         /* terminate current item */
    645  1.1  christos         *opt++ = '\0';
    646  1.1  christos         /* skip over any whitespace after separator */
    647  1.5  christos         while (isspace(_UC(*opt)))
    648  1.1  christos             opt++;
    649  1.1  christos     }
    650  1.1  christos     return *opt == '\0' ? NULL : opt; /* NULL indicates end of input */
    651  1.1  christos }
    652  1.1  christos 
    653  1.1  christos static void warn_cert_msg(const char *uri, X509 *cert, const char *msg)
    654  1.1  christos {
    655  1.1  christos     char *subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
    656  1.1  christos 
    657  1.1  christos     BIO_printf(bio_err, "Warning: certificate from '%s' with subject '%s' %s\n",
    658  1.1  christos                uri, subj, msg);
    659  1.1  christos     OPENSSL_free(subj);
    660  1.1  christos }
    661  1.1  christos 
    662  1.1  christos static void warn_cert(const char *uri, X509 *cert, int warn_EE,
    663  1.1  christos                       X509_VERIFY_PARAM *vpm)
    664  1.1  christos {
    665  1.1  christos     uint32_t ex_flags = X509_get_extension_flags(cert);
    666  1.1  christos     int res = X509_cmp_timeframe(vpm, X509_get0_notBefore(cert),
    667  1.1  christos                                  X509_get0_notAfter(cert));
    668  1.1  christos 
    669  1.1  christos     if (res != 0)
    670  1.1  christos         warn_cert_msg(uri, cert, res > 0 ? "has expired" : "not yet valid");
    671  1.1  christos     if (warn_EE && (ex_flags & EXFLAG_V1) == 0 && (ex_flags & EXFLAG_CA) == 0)
    672  1.1  christos         warn_cert_msg(uri, cert, "is not a CA cert");
    673  1.1  christos }
    674  1.1  christos 
    675  1.1  christos static void warn_certs(const char *uri, STACK_OF(X509) *certs, int warn_EE,
    676  1.1  christos                        X509_VERIFY_PARAM *vpm)
    677  1.1  christos {
    678  1.1  christos     int i;
    679  1.1  christos 
    680  1.1  christos     for (i = 0; i < sk_X509_num(certs); i++)
    681  1.1  christos         warn_cert(uri, sk_X509_value(certs, i), warn_EE, vpm);
    682  1.1  christos }
    683  1.1  christos 
    684  1.1  christos int load_cert_certs(const char *uri,
    685  1.1  christos                     X509 **pcert, STACK_OF(X509) **pcerts,
    686  1.1  christos                     int exclude_http, const char *pass, const char *desc,
    687  1.1  christos                     X509_VERIFY_PARAM *vpm)
    688  1.1  christos {
    689  1.1  christos     int ret = 0;
    690  1.1  christos     char *pass_string;
    691  1.1  christos 
    692  1.1  christos     if (exclude_http && (OPENSSL_strncasecmp(uri, "http://", 7) == 0
    693  1.1  christos                          || OPENSSL_strncasecmp(uri, "https://", 8) == 0)) {
    694  1.1  christos         BIO_printf(bio_err, "error: HTTP retrieval not allowed for %s\n", desc);
    695  1.1  christos         return ret;
    696  1.1  christos     }
    697  1.1  christos     pass_string = get_passwd(pass, desc);
    698  1.1  christos     ret = load_key_certs_crls(uri, FORMAT_UNDEF, 0, pass_string, desc,
    699  1.1  christos                               NULL, NULL, NULL,
    700  1.1  christos                               pcert, pcerts, NULL, NULL);
    701  1.1  christos     clear_free(pass_string);
    702  1.1  christos 
    703  1.1  christos     if (ret) {
    704  1.1  christos         if (pcert != NULL)
    705  1.1  christos             warn_cert(uri, *pcert, 0, vpm);
    706  1.1  christos         if (pcerts != NULL)
    707  1.1  christos             warn_certs(uri, *pcerts, 1, vpm);
    708  1.1  christos     } else {
    709  1.1  christos         if (pcerts != NULL) {
    710  1.1  christos             sk_X509_pop_free(*pcerts, X509_free);
    711  1.1  christos             *pcerts = NULL;
    712  1.1  christos         }
    713  1.1  christos     }
    714  1.1  christos     return ret;
    715  1.1  christos }
    716  1.1  christos 
    717  1.1  christos STACK_OF(X509) *load_certs_multifile(char *files, const char *pass,
    718  1.1  christos                                      const char *desc, X509_VERIFY_PARAM *vpm)
    719  1.1  christos {
    720  1.1  christos     STACK_OF(X509) *certs = NULL;
    721  1.1  christos     STACK_OF(X509) *result = sk_X509_new_null();
    722  1.1  christos 
    723  1.1  christos     if (files == NULL)
    724  1.1  christos         goto err;
    725  1.1  christos     if (result == NULL)
    726  1.1  christos         goto oom;
    727  1.1  christos 
    728  1.1  christos     while (files != NULL) {
    729  1.1  christos         char *next = next_item(files);
    730  1.1  christos 
    731  1.1  christos         if (!load_cert_certs(files, NULL, &certs, 0, pass, desc, vpm))
    732  1.1  christos             goto err;
    733  1.1  christos         if (!X509_add_certs(result, certs,
    734  1.1  christos                             X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
    735  1.1  christos             goto oom;
    736  1.1  christos         sk_X509_pop_free(certs, X509_free);
    737  1.1  christos         certs = NULL;
    738  1.1  christos         files = next;
    739  1.1  christos     }
    740  1.1  christos     return result;
    741  1.1  christos 
    742  1.1  christos  oom:
    743  1.1  christos     BIO_printf(bio_err, "out of memory\n");
    744  1.1  christos  err:
    745  1.1  christos     sk_X509_pop_free(certs, X509_free);
    746  1.1  christos     sk_X509_pop_free(result, X509_free);
    747  1.1  christos     return NULL;
    748  1.1  christos }
    749  1.1  christos 
    750  1.1  christos static X509_STORE *sk_X509_to_store(X509_STORE *store /* may be NULL */,
    751  1.1  christos                                     const STACK_OF(X509) *certs /* may NULL */)
    752  1.1  christos {
    753  1.1  christos     int i;
    754  1.1  christos 
    755  1.1  christos     if (store == NULL)
    756  1.1  christos         store = X509_STORE_new();
    757  1.1  christos     if (store == NULL)
    758  1.1  christos         return NULL;
    759  1.1  christos     for (i = 0; i < sk_X509_num(certs); i++) {
    760  1.1  christos         if (!X509_STORE_add_cert(store, sk_X509_value(certs, i))) {
    761  1.1  christos             X509_STORE_free(store);
    762  1.1  christos             return NULL;
    763  1.1  christos         }
    764  1.1  christos     }
    765  1.1  christos     return store;
    766  1.1  christos }
    767  1.1  christos 
    768  1.1  christos /*
    769  1.1  christos  * Create cert store structure with certificates read from given file(s).
    770  1.1  christos  * Returns pointer to created X509_STORE on success, NULL on error.
    771  1.1  christos  */
    772  1.1  christos X509_STORE *load_certstore(char *input, const char *pass, const char *desc,
    773  1.1  christos                            X509_VERIFY_PARAM *vpm)
    774  1.1  christos {
    775  1.1  christos     X509_STORE *store = NULL;
    776  1.1  christos     STACK_OF(X509) *certs = NULL;
    777  1.1  christos 
    778  1.1  christos     while (input != NULL) {
    779  1.1  christos         char *next = next_item(input);
    780  1.1  christos         int ok;
    781  1.1  christos 
    782  1.1  christos         if (!load_cert_certs(input, NULL, &certs, 1, pass, desc, vpm)) {
    783  1.1  christos             X509_STORE_free(store);
    784  1.1  christos             return NULL;
    785  1.1  christos         }
    786  1.1  christos         ok = (store = sk_X509_to_store(store, certs)) != NULL;
    787  1.1  christos         sk_X509_pop_free(certs, X509_free);
    788  1.1  christos         certs = NULL;
    789  1.1  christos         if (!ok)
    790  1.1  christos             return NULL;
    791  1.1  christos         input = next;
    792  1.1  christos     }
    793  1.1  christos     return store;
    794  1.1  christos }
    795  1.1  christos 
    796  1.1  christos /*
    797  1.1  christos  * Initialize or extend, if *certs != NULL, a certificate stack.
    798  1.1  christos  * The caller is responsible for freeing *certs if its value is left not NULL.
    799  1.1  christos  */
    800  1.1  christos int load_certs(const char *uri, int maybe_stdin, STACK_OF(X509) **certs,
    801  1.1  christos                const char *pass, const char *desc)
    802  1.1  christos {
    803  1.1  christos     int was_NULL = *certs == NULL;
    804  1.1  christos     int ret = load_key_certs_crls(uri, FORMAT_UNDEF, maybe_stdin,
    805  1.1  christos                                   pass, desc, NULL, NULL,
    806  1.1  christos                                   NULL, NULL, certs, NULL, NULL);
    807  1.1  christos 
    808  1.1  christos     if (!ret && was_NULL) {
    809  1.1  christos         sk_X509_pop_free(*certs, X509_free);
    810  1.1  christos         *certs = NULL;
    811  1.1  christos     }
    812  1.1  christos     return ret;
    813  1.1  christos }
    814  1.1  christos 
    815  1.1  christos /*
    816  1.1  christos  * Initialize or extend, if *crls != NULL, a certificate stack.
    817  1.1  christos  * The caller is responsible for freeing *crls if its value is left not NULL.
    818  1.1  christos  */
    819  1.1  christos int load_crls(const char *uri, STACK_OF(X509_CRL) **crls,
    820  1.1  christos               const char *pass, const char *desc)
    821  1.1  christos {
    822  1.1  christos     int was_NULL = *crls == NULL;
    823  1.1  christos     int ret = load_key_certs_crls(uri, FORMAT_UNDEF, 0, pass, desc,
    824  1.1  christos                                   NULL, NULL, NULL,
    825  1.1  christos                                   NULL, NULL, NULL, crls);
    826  1.1  christos 
    827  1.1  christos     if (!ret && was_NULL) {
    828  1.1  christos         sk_X509_CRL_pop_free(*crls, X509_CRL_free);
    829  1.1  christos         *crls = NULL;
    830  1.1  christos     }
    831  1.1  christos     return ret;
    832  1.1  christos }
    833  1.1  christos 
    834  1.1  christos static const char *format2string(int format)
    835  1.1  christos {
    836  1.1  christos     switch(format) {
    837  1.1  christos     case FORMAT_PEM:
    838  1.1  christos         return "PEM";
    839  1.1  christos     case FORMAT_ASN1:
    840  1.1  christos         return "DER";
    841  1.1  christos     }
    842  1.1  christos     return NULL;
    843  1.1  christos }
    844  1.1  christos 
    845  1.1  christos /* Set type expectation, but clear it if objects of different types expected. */
    846  1.1  christos #define SET_EXPECT(expect, val) ((expect) = (expect) < 0 ? (val) : ((expect) == (val) ? (val) : 0))
    847  1.1  christos /*
    848  1.1  christos  * Load those types of credentials for which the result pointer is not NULL.
    849  1.1  christos  * Reads from stdio if uri is NULL and maybe_stdin is nonzero.
    850  1.1  christos  * For non-NULL ppkey, pcert, and pcrl the first suitable value found is loaded.
    851  1.1  christos  * If pcerts is non-NULL and *pcerts == NULL then a new cert list is allocated.
    852  1.1  christos  * If pcerts is non-NULL then all available certificates are appended to *pcerts
    853  1.1  christos  * except any certificate assigned to *pcert.
    854  1.1  christos  * If pcrls is non-NULL and *pcrls == NULL then a new list of CRLs is allocated.
    855  1.1  christos  * If pcrls is non-NULL then all available CRLs are appended to *pcerts
    856  1.1  christos  * except any CRL assigned to *pcrl.
    857  1.1  christos  * In any case (also on error) the caller is responsible for freeing all members
    858  1.1  christos  * of *pcerts and *pcrls (as far as they are not NULL).
    859  1.1  christos  */
    860  1.1  christos static
    861  1.1  christos int load_key_certs_crls_suppress(const char *uri, int format, int maybe_stdin,
    862  1.1  christos                                  const char *pass, const char *desc,
    863  1.1  christos                                  EVP_PKEY **ppkey, EVP_PKEY **ppubkey,
    864  1.1  christos                                  EVP_PKEY **pparams,
    865  1.1  christos                                  X509 **pcert, STACK_OF(X509) **pcerts,
    866  1.1  christos                                  X509_CRL **pcrl, STACK_OF(X509_CRL) **pcrls,
    867  1.1  christos                                  int suppress_decode_errors)
    868  1.1  christos {
    869  1.1  christos     PW_CB_DATA uidata;
    870  1.1  christos     OSSL_STORE_CTX *ctx = NULL;
    871  1.1  christos     OSSL_LIB_CTX *libctx = app_get0_libctx();
    872  1.1  christos     const char *propq = app_get0_propq();
    873  1.1  christos     int ncerts = 0;
    874  1.1  christos     int ncrls = 0;
    875  1.1  christos     const char *failed =
    876  1.1  christos         ppkey != NULL ? "key" : ppubkey != NULL ? "public key" :
    877  1.1  christos         pparams != NULL ? "params" : pcert != NULL ? "cert" :
    878  1.1  christos         pcrl != NULL ? "CRL" : pcerts != NULL ? "certs" :
    879  1.1  christos         pcrls != NULL ? "CRLs" : NULL;
    880  1.1  christos     int cnt_expectations = 0;
    881  1.1  christos     int expect = -1;
    882  1.1  christos     const char *input_type;
    883  1.1  christos     OSSL_PARAM itp[2];
    884  1.1  christos     const OSSL_PARAM *params = NULL;
    885  1.1  christos 
    886  1.1  christos     if (ppkey != NULL) {
    887  1.1  christos         *ppkey = NULL;
    888  1.1  christos         cnt_expectations++;
    889  1.1  christos         SET_EXPECT(expect, OSSL_STORE_INFO_PKEY);
    890  1.1  christos     }
    891  1.1  christos     if (ppubkey != NULL) {
    892  1.1  christos         *ppubkey = NULL;
    893  1.1  christos         cnt_expectations++;
    894  1.1  christos         SET_EXPECT(expect, OSSL_STORE_INFO_PUBKEY);
    895  1.1  christos     }
    896  1.1  christos     if (pparams != NULL) {
    897  1.1  christos         *pparams = NULL;
    898  1.1  christos         cnt_expectations++;
    899  1.1  christos         SET_EXPECT(expect, OSSL_STORE_INFO_PARAMS);
    900  1.1  christos     }
    901  1.1  christos     if (pcert != NULL) {
    902  1.1  christos         *pcert = NULL;
    903  1.1  christos         cnt_expectations++;
    904  1.1  christos         SET_EXPECT(expect, OSSL_STORE_INFO_CERT);
    905  1.1  christos     }
    906  1.1  christos     if (pcerts != NULL) {
    907  1.1  christos         if (*pcerts == NULL && (*pcerts = sk_X509_new_null()) == NULL) {
    908  1.1  christos             BIO_printf(bio_err, "Out of memory loading");
    909  1.1  christos             goto end;
    910  1.1  christos         }
    911  1.1  christos         cnt_expectations++;
    912  1.1  christos         SET_EXPECT(expect, OSSL_STORE_INFO_CERT);
    913  1.1  christos     }
    914  1.1  christos     if (pcrl != NULL) {
    915  1.1  christos         *pcrl = NULL;
    916  1.1  christos         cnt_expectations++;
    917  1.1  christos         SET_EXPECT(expect, OSSL_STORE_INFO_CRL);
    918  1.1  christos     }
    919  1.1  christos     if (pcrls != NULL) {
    920  1.1  christos         if (*pcrls == NULL && (*pcrls = sk_X509_CRL_new_null()) == NULL) {
    921  1.1  christos             BIO_printf(bio_err, "Out of memory loading");
    922  1.1  christos             goto end;
    923  1.1  christos         }
    924  1.1  christos         cnt_expectations++;
    925  1.1  christos         SET_EXPECT(expect, OSSL_STORE_INFO_CRL);
    926  1.1  christos     }
    927  1.1  christos     if (cnt_expectations == 0) {
    928  1.1  christos         BIO_printf(bio_err, "Internal error: nothing to load from %s\n",
    929  1.1  christos                    uri != NULL ? uri : "<stdin>");
    930  1.1  christos         return 0;
    931  1.1  christos     }
    932  1.1  christos 
    933  1.1  christos     uidata.password = pass;
    934  1.1  christos     uidata.prompt_info = uri;
    935  1.1  christos 
    936  1.1  christos     if ((input_type = format2string(format)) != NULL) {
    937  1.1  christos        itp[0] = OSSL_PARAM_construct_utf8_string(OSSL_STORE_PARAM_INPUT_TYPE,
    938  1.1  christos                                                  (char *)input_type, 0);
    939  1.1  christos        itp[1] = OSSL_PARAM_construct_end();
    940  1.1  christos        params = itp;
    941  1.1  christos     }
    942  1.1  christos 
    943  1.1  christos     if (uri == NULL) {
    944  1.1  christos         BIO *bio;
    945  1.1  christos 
    946  1.1  christos         if (!maybe_stdin) {
    947  1.5  christos             BIO_printf(bio_err, "No filename or uri specified for loading\n");
    948  1.1  christos             goto end;
    949  1.1  christos         }
    950  1.1  christos         uri = "<stdin>";
    951  1.1  christos         unbuffer(stdin);
    952  1.1  christos         bio = BIO_new_fp(stdin, 0);
    953  1.1  christos         if (bio != NULL) {
    954  1.1  christos             ctx = OSSL_STORE_attach(bio, "file", libctx, propq,
    955  1.1  christos                                     get_ui_method(), &uidata, params,
    956  1.1  christos                                     NULL, NULL);
    957  1.1  christos             BIO_free(bio);
    958  1.1  christos         }
    959  1.1  christos     } else {
    960  1.1  christos         ctx = OSSL_STORE_open_ex(uri, libctx, propq, get_ui_method(), &uidata,
    961  1.1  christos                                  params, NULL, NULL);
    962  1.1  christos     }
    963  1.1  christos     if (ctx == NULL) {
    964  1.1  christos         BIO_printf(bio_err, "Could not open file or uri for loading");
    965  1.1  christos         goto end;
    966  1.1  christos     }
    967  1.5  christos     if (expect > 0 && !OSSL_STORE_expect(ctx, expect)) {
    968  1.5  christos         BIO_printf(bio_err, "Internal error trying to load");
    969  1.1  christos         goto end;
    970  1.5  christos     }
    971  1.1  christos 
    972  1.1  christos     failed = NULL;
    973  1.1  christos     while (cnt_expectations > 0 && !OSSL_STORE_eof(ctx)) {
    974  1.1  christos         OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
    975  1.1  christos         int type, ok = 1;
    976  1.1  christos 
    977  1.1  christos         /*
    978  1.1  christos          * This can happen (for example) if we attempt to load a file with
    979  1.1  christos          * multiple different types of things in it - but the thing we just
    980  1.1  christos          * tried to load wasn't one of the ones we wanted, e.g. if we're trying
    981  1.1  christos          * to load a certificate but the file has both the private key and the
    982  1.1  christos          * certificate in it. We just retry until eof.
    983  1.1  christos          */
    984  1.1  christos         if (info == NULL) {
    985  1.1  christos             continue;
    986  1.1  christos         }
    987  1.1  christos 
    988  1.1  christos         type = OSSL_STORE_INFO_get_type(info);
    989  1.1  christos         switch (type) {
    990  1.1  christos         case OSSL_STORE_INFO_PKEY:
    991  1.1  christos             if (ppkey != NULL && *ppkey == NULL) {
    992  1.1  christos                 ok = (*ppkey = OSSL_STORE_INFO_get1_PKEY(info)) != NULL;
    993  1.1  christos                 cnt_expectations -= ok;
    994  1.1  christos             }
    995  1.1  christos             /*
    996  1.1  christos              * An EVP_PKEY with private parts also holds the public parts,
    997  1.1  christos              * so if the caller asked for a public key, and we got a private
    998  1.1  christos              * key, we can still pass it back.
    999  1.1  christos              */
   1000  1.1  christos             if (ok && ppubkey != NULL && *ppubkey == NULL) {
   1001  1.1  christos                 ok = ((*ppubkey = OSSL_STORE_INFO_get1_PKEY(info)) != NULL);
   1002  1.1  christos                 cnt_expectations -= ok;
   1003  1.1  christos             }
   1004  1.1  christos             break;
   1005  1.1  christos         case OSSL_STORE_INFO_PUBKEY:
   1006  1.1  christos             if (ppubkey != NULL && *ppubkey == NULL) {
   1007  1.1  christos                 ok = ((*ppubkey = OSSL_STORE_INFO_get1_PUBKEY(info)) != NULL);
   1008  1.1  christos                 cnt_expectations -= ok;
   1009  1.1  christos             }
   1010  1.1  christos             break;
   1011  1.1  christos         case OSSL_STORE_INFO_PARAMS:
   1012  1.1  christos             if (pparams != NULL && *pparams == NULL) {
   1013  1.1  christos                 ok = ((*pparams = OSSL_STORE_INFO_get1_PARAMS(info)) != NULL);
   1014  1.1  christos                 cnt_expectations -= ok;
   1015  1.1  christos             }
   1016  1.1  christos             break;
   1017  1.1  christos         case OSSL_STORE_INFO_CERT:
   1018  1.1  christos             if (pcert != NULL && *pcert == NULL) {
   1019  1.1  christos                 ok = (*pcert = OSSL_STORE_INFO_get1_CERT(info)) != NULL;
   1020  1.1  christos                 cnt_expectations -= ok;
   1021  1.1  christos             }
   1022  1.1  christos             else if (pcerts != NULL)
   1023  1.1  christos                 ok = X509_add_cert(*pcerts,
   1024  1.1  christos                                    OSSL_STORE_INFO_get1_CERT(info),
   1025  1.1  christos                                    X509_ADD_FLAG_DEFAULT);
   1026  1.1  christos             ncerts += ok;
   1027  1.1  christos             break;
   1028  1.1  christos         case OSSL_STORE_INFO_CRL:
   1029  1.1  christos             if (pcrl != NULL && *pcrl == NULL) {
   1030  1.1  christos                 ok = (*pcrl = OSSL_STORE_INFO_get1_CRL(info)) != NULL;
   1031  1.1  christos                 cnt_expectations -= ok;
   1032  1.1  christos             }
   1033  1.1  christos             else if (pcrls != NULL)
   1034  1.1  christos                 ok = sk_X509_CRL_push(*pcrls, OSSL_STORE_INFO_get1_CRL(info));
   1035  1.1  christos             ncrls += ok;
   1036  1.1  christos             break;
   1037  1.1  christos         default:
   1038  1.1  christos             /* skip any other type */
   1039  1.1  christos             break;
   1040  1.1  christos         }
   1041  1.1  christos         OSSL_STORE_INFO_free(info);
   1042  1.1  christos         if (!ok) {
   1043  1.1  christos             failed = info == NULL ? NULL : OSSL_STORE_INFO_type_string(type);
   1044  1.1  christos             BIO_printf(bio_err, "Error reading");
   1045  1.1  christos             break;
   1046  1.1  christos         }
   1047  1.1  christos     }
   1048  1.1  christos 
   1049  1.1  christos  end:
   1050  1.1  christos     OSSL_STORE_close(ctx);
   1051  1.1  christos     if (failed == NULL) {
   1052  1.1  christos         int any = 0;
   1053  1.1  christos 
   1054  1.1  christos         if ((ppkey != NULL && *ppkey == NULL)
   1055  1.1  christos             || (ppubkey != NULL && *ppubkey == NULL)) {
   1056  1.1  christos             failed = "key";
   1057  1.1  christos         } else if (pparams != NULL && *pparams == NULL) {
   1058  1.1  christos             failed = "params";
   1059  1.1  christos         } else if ((pcert != NULL || pcerts != NULL) && ncerts == 0) {
   1060  1.1  christos             if (pcert == NULL)
   1061  1.1  christos                 any = 1;
   1062  1.1  christos             failed = "cert";
   1063  1.1  christos         } else if ((pcrl != NULL || pcrls != NULL) && ncrls == 0) {
   1064  1.1  christos             if (pcrl == NULL)
   1065  1.1  christos                 any = 1;
   1066  1.1  christos             failed = "CRL";
   1067  1.1  christos         }
   1068  1.1  christos         if (!suppress_decode_errors) {
   1069  1.1  christos             if (failed != NULL)
   1070  1.1  christos                 BIO_printf(bio_err, "Could not read");
   1071  1.1  christos             if (any)
   1072  1.1  christos                 BIO_printf(bio_err, " any");
   1073  1.1  christos         }
   1074  1.1  christos     }
   1075  1.1  christos     if (!suppress_decode_errors && failed != NULL) {
   1076  1.1  christos         if (desc != NULL && strstr(desc, failed) != NULL) {
   1077  1.1  christos             BIO_printf(bio_err, " %s", desc);
   1078  1.1  christos         } else {
   1079  1.1  christos             BIO_printf(bio_err, " %s", failed);
   1080  1.1  christos             if (desc != NULL)
   1081  1.1  christos                 BIO_printf(bio_err, " of %s", desc);
   1082  1.1  christos         }
   1083  1.1  christos         if (uri != NULL)
   1084  1.1  christos             BIO_printf(bio_err, " from %s", uri);
   1085  1.1  christos         BIO_printf(bio_err, "\n");
   1086  1.1  christos         ERR_print_errors(bio_err);
   1087  1.1  christos     }
   1088  1.1  christos     if (suppress_decode_errors || failed == NULL)
   1089  1.1  christos         /* clear any spurious errors */
   1090  1.1  christos         ERR_clear_error();
   1091  1.1  christos     return failed == NULL;
   1092  1.1  christos }
   1093  1.1  christos 
   1094  1.1  christos int load_key_certs_crls(const char *uri, int format, int maybe_stdin,
   1095  1.1  christos                         const char *pass, const char *desc,
   1096  1.1  christos                         EVP_PKEY **ppkey, EVP_PKEY **ppubkey,
   1097  1.1  christos                         EVP_PKEY **pparams,
   1098  1.1  christos                         X509 **pcert, STACK_OF(X509) **pcerts,
   1099  1.1  christos                         X509_CRL **pcrl, STACK_OF(X509_CRL) **pcrls)
   1100  1.1  christos {
   1101  1.1  christos     return load_key_certs_crls_suppress(uri, format, maybe_stdin, pass, desc,
   1102  1.1  christos                                         ppkey, ppubkey, pparams, pcert, pcerts,
   1103  1.1  christos                                         pcrl, pcrls, 0);
   1104  1.1  christos }
   1105  1.1  christos 
   1106  1.1  christos #define X509V3_EXT_UNKNOWN_MASK         (0xfL << 16)
   1107  1.1  christos /* Return error for unknown extensions */
   1108  1.1  christos #define X509V3_EXT_DEFAULT              0
   1109  1.1  christos /* Print error for unknown extensions */
   1110  1.1  christos #define X509V3_EXT_ERROR_UNKNOWN        (1L << 16)
   1111  1.1  christos /* ASN1 parse unknown extensions */
   1112  1.1  christos #define X509V3_EXT_PARSE_UNKNOWN        (2L << 16)
   1113  1.1  christos /* BIO_dump unknown extensions */
   1114  1.1  christos #define X509V3_EXT_DUMP_UNKNOWN         (3L << 16)
   1115  1.1  christos 
   1116  1.1  christos #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
   1117  1.1  christos                          X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
   1118  1.1  christos 
   1119  1.1  christos int set_cert_ex(unsigned long *flags, const char *arg)
   1120  1.1  christos {
   1121  1.1  christos     static const NAME_EX_TBL cert_tbl[] = {
   1122  1.1  christos         {"compatible", X509_FLAG_COMPAT, 0xffffffffl},
   1123  1.1  christos         {"ca_default", X509_FLAG_CA, 0xffffffffl},
   1124  1.1  christos         {"no_header", X509_FLAG_NO_HEADER, 0},
   1125  1.1  christos         {"no_version", X509_FLAG_NO_VERSION, 0},
   1126  1.1  christos         {"no_serial", X509_FLAG_NO_SERIAL, 0},
   1127  1.1  christos         {"no_signame", X509_FLAG_NO_SIGNAME, 0},
   1128  1.1  christos         {"no_validity", X509_FLAG_NO_VALIDITY, 0},
   1129  1.1  christos         {"no_subject", X509_FLAG_NO_SUBJECT, 0},
   1130  1.1  christos         {"no_issuer", X509_FLAG_NO_ISSUER, 0},
   1131  1.1  christos         {"no_pubkey", X509_FLAG_NO_PUBKEY, 0},
   1132  1.1  christos         {"no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
   1133  1.1  christos         {"no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
   1134  1.1  christos         {"no_aux", X509_FLAG_NO_AUX, 0},
   1135  1.1  christos         {"no_attributes", X509_FLAG_NO_ATTRIBUTES, 0},
   1136  1.1  christos         {"ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
   1137  1.1  christos         {"ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
   1138  1.1  christos         {"ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
   1139  1.1  christos         {"ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
   1140  1.1  christos         {NULL, 0, 0}
   1141  1.1  christos     };
   1142  1.1  christos     return set_multi_opts(flags, arg, cert_tbl);
   1143  1.1  christos }
   1144  1.1  christos 
   1145  1.1  christos int set_name_ex(unsigned long *flags, const char *arg)
   1146  1.1  christos {
   1147  1.1  christos     static const NAME_EX_TBL ex_tbl[] = {
   1148  1.1  christos         {"esc_2253", ASN1_STRFLGS_ESC_2253, 0},
   1149  1.1  christos         {"esc_2254", ASN1_STRFLGS_ESC_2254, 0},
   1150  1.1  christos         {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
   1151  1.1  christos         {"esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
   1152  1.1  christos         {"use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
   1153  1.1  christos         {"utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
   1154  1.1  christos         {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
   1155  1.1  christos         {"show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
   1156  1.1  christos         {"dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
   1157  1.1  christos         {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
   1158  1.1  christos         {"dump_der", ASN1_STRFLGS_DUMP_DER, 0},
   1159  1.1  christos         {"compat", XN_FLAG_COMPAT, 0xffffffffL},
   1160  1.1  christos         {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
   1161  1.1  christos         {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
   1162  1.1  christos         {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
   1163  1.1  christos         {"sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
   1164  1.1  christos         {"dn_rev", XN_FLAG_DN_REV, 0},
   1165  1.1  christos         {"nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
   1166  1.1  christos         {"sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
   1167  1.1  christos         {"lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
   1168  1.1  christos         {"align", XN_FLAG_FN_ALIGN, 0},
   1169  1.1  christos         {"oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
   1170  1.1  christos         {"space_eq", XN_FLAG_SPC_EQ, 0},
   1171  1.1  christos         {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
   1172  1.1  christos         {"RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
   1173  1.1  christos         {"oneline", XN_FLAG_ONELINE, 0xffffffffL},
   1174  1.1  christos         {"multiline", XN_FLAG_MULTILINE, 0xffffffffL},
   1175  1.1  christos         {"ca_default", XN_FLAG_MULTILINE, 0xffffffffL},
   1176  1.1  christos         {NULL, 0, 0}
   1177  1.1  christos     };
   1178  1.1  christos     if (set_multi_opts(flags, arg, ex_tbl) == 0)
   1179  1.1  christos         return 0;
   1180  1.1  christos     if (*flags != XN_FLAG_COMPAT
   1181  1.1  christos         && (*flags & XN_FLAG_SEP_MASK) == 0)
   1182  1.1  christos         *flags |= XN_FLAG_SEP_CPLUS_SPC;
   1183  1.1  christos     return 1;
   1184  1.1  christos }
   1185  1.1  christos 
   1186  1.1  christos int set_dateopt(unsigned long *dateopt, const char *arg)
   1187  1.1  christos {
   1188  1.1  christos     if (OPENSSL_strcasecmp(arg, "rfc_822") == 0)
   1189  1.1  christos         *dateopt = ASN1_DTFLGS_RFC822;
   1190  1.1  christos     else if (OPENSSL_strcasecmp(arg, "iso_8601") == 0)
   1191  1.1  christos         *dateopt = ASN1_DTFLGS_ISO8601;
   1192  1.1  christos     else
   1193  1.1  christos         return 0;
   1194  1.1  christos     return 1;
   1195  1.1  christos }
   1196  1.1  christos 
   1197  1.1  christos int set_ext_copy(int *copy_type, const char *arg)
   1198  1.1  christos {
   1199  1.1  christos     if (OPENSSL_strcasecmp(arg, "none") == 0)
   1200  1.1  christos         *copy_type = EXT_COPY_NONE;
   1201  1.1  christos     else if (OPENSSL_strcasecmp(arg, "copy") == 0)
   1202  1.1  christos         *copy_type = EXT_COPY_ADD;
   1203  1.1  christos     else if (OPENSSL_strcasecmp(arg, "copyall") == 0)
   1204  1.1  christos         *copy_type = EXT_COPY_ALL;
   1205  1.1  christos     else
   1206  1.1  christos         return 0;
   1207  1.1  christos     return 1;
   1208  1.1  christos }
   1209  1.1  christos 
   1210  1.1  christos int copy_extensions(X509 *x, X509_REQ *req, int copy_type)
   1211  1.1  christos {
   1212  1.1  christos     STACK_OF(X509_EXTENSION) *exts;
   1213  1.1  christos     int i, ret = 0;
   1214  1.1  christos 
   1215  1.1  christos     if (x == NULL || req == NULL)
   1216  1.1  christos         return 0;
   1217  1.1  christos     if (copy_type == EXT_COPY_NONE)
   1218  1.1  christos         return 1;
   1219  1.1  christos     exts = X509_REQ_get_extensions(req);
   1220  1.1  christos 
   1221  1.1  christos     for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
   1222  1.1  christos         X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
   1223  1.1  christos         ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext);
   1224  1.1  christos         int idx = X509_get_ext_by_OBJ(x, obj, -1);
   1225  1.1  christos 
   1226  1.1  christos         /* Does extension exist in target? */
   1227  1.1  christos         if (idx != -1) {
   1228  1.1  christos             /* If normal copy don't override existing extension */
   1229  1.1  christos             if (copy_type == EXT_COPY_ADD)
   1230  1.1  christos                 continue;
   1231  1.1  christos             /* Delete all extensions of same type */
   1232  1.1  christos             do {
   1233  1.1  christos                 X509_EXTENSION_free(X509_delete_ext(x, idx));
   1234  1.1  christos                 idx = X509_get_ext_by_OBJ(x, obj, -1);
   1235  1.1  christos             } while (idx != -1);
   1236  1.1  christos         }
   1237  1.1  christos         if (!X509_add_ext(x, ext, -1))
   1238  1.1  christos             goto end;
   1239  1.1  christos     }
   1240  1.1  christos     ret = 1;
   1241  1.1  christos 
   1242  1.1  christos  end:
   1243  1.1  christos     sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
   1244  1.1  christos     return ret;
   1245  1.1  christos }
   1246  1.1  christos 
   1247  1.1  christos static int set_multi_opts(unsigned long *flags, const char *arg,
   1248  1.1  christos                           const NAME_EX_TBL * in_tbl)
   1249  1.1  christos {
   1250  1.1  christos     STACK_OF(CONF_VALUE) *vals;
   1251  1.1  christos     CONF_VALUE *val;
   1252  1.1  christos     int i, ret = 1;
   1253  1.1  christos     if (!arg)
   1254  1.1  christos         return 0;
   1255  1.1  christos     vals = X509V3_parse_list(arg);
   1256  1.1  christos     for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
   1257  1.1  christos         val = sk_CONF_VALUE_value(vals, i);
   1258  1.1  christos         if (!set_table_opts(flags, val->name, in_tbl))
   1259  1.1  christos             ret = 0;
   1260  1.1  christos     }
   1261  1.1  christos     sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
   1262  1.1  christos     return ret;
   1263  1.1  christos }
   1264  1.1  christos 
   1265  1.1  christos static int set_table_opts(unsigned long *flags, const char *arg,
   1266  1.1  christos                           const NAME_EX_TBL * in_tbl)
   1267  1.1  christos {
   1268  1.1  christos     char c;
   1269  1.1  christos     const NAME_EX_TBL *ptbl;
   1270  1.1  christos     c = arg[0];
   1271  1.1  christos 
   1272  1.1  christos     if (c == '-') {
   1273  1.1  christos         c = 0;
   1274  1.1  christos         arg++;
   1275  1.1  christos     } else if (c == '+') {
   1276  1.1  christos         c = 1;
   1277  1.1  christos         arg++;
   1278  1.1  christos     } else {
   1279  1.1  christos         c = 1;
   1280  1.1  christos     }
   1281  1.1  christos 
   1282  1.1  christos     for (ptbl = in_tbl; ptbl->name; ptbl++) {
   1283  1.1  christos         if (OPENSSL_strcasecmp(arg, ptbl->name) == 0) {
   1284  1.1  christos             *flags &= ~ptbl->mask;
   1285  1.1  christos             if (c)
   1286  1.1  christos                 *flags |= ptbl->flag;
   1287  1.1  christos             else
   1288  1.1  christos                 *flags &= ~ptbl->flag;
   1289  1.1  christos             return 1;
   1290  1.1  christos         }
   1291  1.1  christos     }
   1292  1.1  christos     return 0;
   1293  1.1  christos }
   1294  1.1  christos 
   1295  1.1  christos void print_name(BIO *out, const char *title, const X509_NAME *nm)
   1296  1.1  christos {
   1297  1.1  christos     char *buf;
   1298  1.1  christos     char mline = 0;
   1299  1.1  christos     int indent = 0;
   1300  1.1  christos     unsigned long lflags = get_nameopt();
   1301  1.1  christos 
   1302  1.1  christos     if (out == NULL)
   1303  1.1  christos         return;
   1304  1.1  christos     if (title != NULL)
   1305  1.1  christos         BIO_puts(out, title);
   1306  1.1  christos     if ((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
   1307  1.1  christos         mline = 1;
   1308  1.1  christos         indent = 4;
   1309  1.1  christos     }
   1310  1.1  christos     if (lflags == XN_FLAG_COMPAT) {
   1311  1.1  christos         buf = X509_NAME_oneline(nm, 0, 0);
   1312  1.1  christos         BIO_puts(out, buf);
   1313  1.1  christos         BIO_puts(out, "\n");
   1314  1.1  christos         OPENSSL_free(buf);
   1315  1.1  christos     } else {
   1316  1.1  christos         if (mline)
   1317  1.1  christos             BIO_puts(out, "\n");
   1318  1.1  christos         X509_NAME_print_ex(out, nm, indent, lflags);
   1319  1.1  christos         BIO_puts(out, "\n");
   1320  1.1  christos     }
   1321  1.1  christos }
   1322  1.1  christos 
   1323  1.1  christos void print_bignum_var(BIO *out, const BIGNUM *in, const char *var,
   1324  1.1  christos                       int len, unsigned char *buffer)
   1325  1.1  christos {
   1326  1.1  christos     BIO_printf(out, "    static unsigned char %s_%d[] = {", var, len);
   1327  1.1  christos     if (BN_is_zero(in)) {
   1328  1.1  christos         BIO_printf(out, "\n        0x00");
   1329  1.1  christos     } else {
   1330  1.1  christos         int i, l;
   1331  1.1  christos 
   1332  1.1  christos         l = BN_bn2bin(in, buffer);
   1333  1.1  christos         for (i = 0; i < l; i++) {
   1334  1.1  christos             BIO_printf(out, (i % 10) == 0 ? "\n        " : " ");
   1335  1.1  christos             if (i < l - 1)
   1336  1.1  christos                 BIO_printf(out, "0x%02X,", buffer[i]);
   1337  1.1  christos             else
   1338  1.1  christos                 BIO_printf(out, "0x%02X", buffer[i]);
   1339  1.1  christos         }
   1340  1.1  christos     }
   1341  1.1  christos     BIO_printf(out, "\n    };\n");
   1342  1.1  christos }
   1343  1.1  christos 
   1344  1.1  christos void print_array(BIO *out, const char* title, int len, const unsigned char* d)
   1345  1.1  christos {
   1346  1.1  christos     int i;
   1347  1.1  christos 
   1348  1.1  christos     BIO_printf(out, "unsigned char %s[%d] = {", title, len);
   1349  1.1  christos     for (i = 0; i < len; i++) {
   1350  1.1  christos         if ((i % 10) == 0)
   1351  1.1  christos             BIO_printf(out, "\n    ");
   1352  1.1  christos         if (i < len - 1)
   1353  1.1  christos             BIO_printf(out, "0x%02X, ", d[i]);
   1354  1.1  christos         else
   1355  1.1  christos             BIO_printf(out, "0x%02X", d[i]);
   1356  1.1  christos     }
   1357  1.1  christos     BIO_printf(out, "\n};\n");
   1358  1.1  christos }
   1359  1.1  christos 
   1360  1.1  christos X509_STORE *setup_verify(const char *CAfile, int noCAfile,
   1361  1.1  christos                          const char *CApath, int noCApath,
   1362  1.1  christos                          const char *CAstore, int noCAstore)
   1363  1.1  christos {
   1364  1.1  christos     X509_STORE *store = X509_STORE_new();
   1365  1.1  christos     X509_LOOKUP *lookup;
   1366  1.1  christos     OSSL_LIB_CTX *libctx = app_get0_libctx();
   1367  1.1  christos     const char *propq = app_get0_propq();
   1368  1.1  christos 
   1369  1.1  christos     if (store == NULL)
   1370  1.1  christos         goto end;
   1371  1.1  christos 
   1372  1.1  christos     if (CAfile != NULL || !noCAfile) {
   1373  1.1  christos         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
   1374  1.1  christos         if (lookup == NULL)
   1375  1.1  christos             goto end;
   1376  1.1  christos         if (CAfile != NULL) {
   1377  1.1  christos             if (X509_LOOKUP_load_file_ex(lookup, CAfile, X509_FILETYPE_PEM,
   1378  1.1  christos                                           libctx, propq) <= 0) {
   1379  1.1  christos                 BIO_printf(bio_err, "Error loading file %s\n", CAfile);
   1380  1.1  christos                 goto end;
   1381  1.1  christos             }
   1382  1.1  christos         } else {
   1383  1.1  christos             X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT,
   1384  1.1  christos                                      libctx, propq);
   1385  1.1  christos         }
   1386  1.1  christos     }
   1387  1.1  christos 
   1388  1.1  christos     if (CApath != NULL || !noCApath) {
   1389  1.1  christos         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
   1390  1.1  christos         if (lookup == NULL)
   1391  1.1  christos             goto end;
   1392  1.1  christos         if (CApath != NULL) {
   1393  1.1  christos             if (X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM) <= 0) {
   1394  1.1  christos                 BIO_printf(bio_err, "Error loading directory %s\n", CApath);
   1395  1.1  christos                 goto end;
   1396  1.1  christos             }
   1397  1.1  christos         } else {
   1398  1.1  christos             X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
   1399  1.1  christos         }
   1400  1.1  christos     }
   1401  1.1  christos 
   1402  1.1  christos     if (CAstore != NULL || !noCAstore) {
   1403  1.1  christos         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_store());
   1404  1.1  christos         if (lookup == NULL)
   1405  1.1  christos             goto end;
   1406  1.1  christos         if (!X509_LOOKUP_add_store_ex(lookup, CAstore, libctx, propq)) {
   1407  1.1  christos             if (CAstore != NULL)
   1408  1.1  christos                 BIO_printf(bio_err, "Error loading store URI %s\n", CAstore);
   1409  1.1  christos             goto end;
   1410  1.1  christos         }
   1411  1.1  christos     }
   1412  1.1  christos 
   1413  1.1  christos     ERR_clear_error();
   1414  1.1  christos     return store;
   1415  1.1  christos  end:
   1416  1.1  christos     ERR_print_errors(bio_err);
   1417  1.1  christos     X509_STORE_free(store);
   1418  1.1  christos     return NULL;
   1419  1.1  christos }
   1420  1.1  christos 
   1421  1.1  christos static unsigned long index_serial_hash(const OPENSSL_CSTRING *a)
   1422  1.1  christos {
   1423  1.1  christos     const char *n;
   1424  1.1  christos 
   1425  1.1  christos     n = a[DB_serial];
   1426  1.1  christos     while (*n == '0')
   1427  1.1  christos         n++;
   1428  1.1  christos     return OPENSSL_LH_strhash(n);
   1429  1.1  christos }
   1430  1.1  christos 
   1431  1.1  christos static int index_serial_cmp(const OPENSSL_CSTRING *a,
   1432  1.1  christos                             const OPENSSL_CSTRING *b)
   1433  1.1  christos {
   1434  1.1  christos     const char *aa, *bb;
   1435  1.1  christos 
   1436  1.1  christos     for (aa = a[DB_serial]; *aa == '0'; aa++) ;
   1437  1.1  christos     for (bb = b[DB_serial]; *bb == '0'; bb++) ;
   1438  1.1  christos     return strcmp(aa, bb);
   1439  1.1  christos }
   1440  1.1  christos 
   1441  1.1  christos static int index_name_qual(char **a)
   1442  1.1  christos {
   1443  1.1  christos     return (a[0][0] == 'V');
   1444  1.1  christos }
   1445  1.1  christos 
   1446  1.1  christos static unsigned long index_name_hash(const OPENSSL_CSTRING *a)
   1447  1.1  christos {
   1448  1.1  christos     return OPENSSL_LH_strhash(a[DB_name]);
   1449  1.1  christos }
   1450  1.1  christos 
   1451  1.1  christos int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b)
   1452  1.1  christos {
   1453  1.1  christos     return strcmp(a[DB_name], b[DB_name]);
   1454  1.1  christos }
   1455  1.1  christos 
   1456  1.1  christos static IMPLEMENT_LHASH_HASH_FN(index_serial, OPENSSL_CSTRING)
   1457  1.1  christos static IMPLEMENT_LHASH_COMP_FN(index_serial, OPENSSL_CSTRING)
   1458  1.1  christos static IMPLEMENT_LHASH_HASH_FN(index_name, OPENSSL_CSTRING)
   1459  1.1  christos static IMPLEMENT_LHASH_COMP_FN(index_name, OPENSSL_CSTRING)
   1460  1.1  christos #undef BSIZE
   1461  1.1  christos #define BSIZE 256
   1462  1.1  christos BIGNUM *load_serial(const char *serialfile, int *exists, int create,
   1463  1.1  christos                     ASN1_INTEGER **retai)
   1464  1.1  christos {
   1465  1.1  christos     BIO *in = NULL;
   1466  1.1  christos     BIGNUM *ret = NULL;
   1467  1.1  christos     char buf[1024];
   1468  1.1  christos     ASN1_INTEGER *ai = NULL;
   1469  1.1  christos 
   1470  1.1  christos     ai = ASN1_INTEGER_new();
   1471  1.1  christos     if (ai == NULL)
   1472  1.1  christos         goto err;
   1473  1.1  christos 
   1474  1.1  christos     in = BIO_new_file(serialfile, "r");
   1475  1.1  christos     if (exists != NULL)
   1476  1.1  christos         *exists = in != NULL;
   1477  1.1  christos     if (in == NULL) {
   1478  1.1  christos         if (!create) {
   1479  1.1  christos             perror(serialfile);
   1480  1.1  christos             goto err;
   1481  1.1  christos         }
   1482  1.1  christos         ERR_clear_error();
   1483  1.1  christos         ret = BN_new();
   1484  1.1  christos         if (ret == NULL) {
   1485  1.1  christos             BIO_printf(bio_err, "Out of memory\n");
   1486  1.1  christos         } else if (!rand_serial(ret, ai)) {
   1487  1.1  christos             BIO_printf(bio_err, "Error creating random number to store in %s\n",
   1488  1.1  christos                        serialfile);
   1489  1.1  christos             BN_free(ret);
   1490  1.1  christos             ret = NULL;
   1491  1.1  christos         }
   1492  1.1  christos     } else {
   1493  1.1  christos         if (!a2i_ASN1_INTEGER(in, ai, buf, 1024)) {
   1494  1.1  christos             BIO_printf(bio_err, "Unable to load number from %s\n",
   1495  1.1  christos                        serialfile);
   1496  1.1  christos             goto err;
   1497  1.1  christos         }
   1498  1.1  christos         ret = ASN1_INTEGER_to_BN(ai, NULL);
   1499  1.1  christos         if (ret == NULL) {
   1500  1.1  christos             BIO_printf(bio_err, "Error converting number from bin to BIGNUM\n");
   1501  1.1  christos             goto err;
   1502  1.1  christos         }
   1503  1.1  christos     }
   1504  1.1  christos 
   1505  1.1  christos     if (ret != NULL && retai != NULL) {
   1506  1.1  christos         *retai = ai;
   1507  1.1  christos         ai = NULL;
   1508  1.1  christos     }
   1509  1.1  christos  err:
   1510  1.1  christos     if (ret == NULL)
   1511  1.1  christos         ERR_print_errors(bio_err);
   1512  1.1  christos     BIO_free(in);
   1513  1.1  christos     ASN1_INTEGER_free(ai);
   1514  1.1  christos     return ret;
   1515  1.1  christos }
   1516  1.1  christos 
   1517  1.1  christos int save_serial(const char *serialfile, const char *suffix, const BIGNUM *serial,
   1518  1.1  christos                 ASN1_INTEGER **retai)
   1519  1.1  christos {
   1520  1.1  christos     char buf[1][BSIZE];
   1521  1.1  christos     BIO *out = NULL;
   1522  1.1  christos     int ret = 0;
   1523  1.1  christos     ASN1_INTEGER *ai = NULL;
   1524  1.1  christos     int j;
   1525  1.1  christos 
   1526  1.1  christos     if (suffix == NULL)
   1527  1.1  christos         j = strlen(serialfile);
   1528  1.1  christos     else
   1529  1.1  christos         j = strlen(serialfile) + strlen(suffix) + 1;
   1530  1.1  christos     if (j >= BSIZE) {
   1531  1.1  christos         BIO_printf(bio_err, "File name too long\n");
   1532  1.1  christos         goto err;
   1533  1.1  christos     }
   1534  1.1  christos 
   1535  1.1  christos     if (suffix == NULL)
   1536  1.1  christos         OPENSSL_strlcpy(buf[0], serialfile, BSIZE);
   1537  1.1  christos     else {
   1538  1.1  christos #ifndef OPENSSL_SYS_VMS
   1539  1.1  christos         j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", serialfile, suffix);
   1540  1.1  christos #else
   1541  1.1  christos         j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", serialfile, suffix);
   1542  1.1  christos #endif
   1543  1.1  christos     }
   1544  1.1  christos     out = BIO_new_file(buf[0], "w");
   1545  1.1  christos     if (out == NULL) {
   1546  1.1  christos         goto err;
   1547  1.1  christos     }
   1548  1.1  christos 
   1549  1.1  christos     if ((ai = BN_to_ASN1_INTEGER(serial, NULL)) == NULL) {
   1550  1.1  christos         BIO_printf(bio_err, "error converting serial to ASN.1 format\n");
   1551  1.1  christos         goto err;
   1552  1.1  christos     }
   1553  1.1  christos     i2a_ASN1_INTEGER(out, ai);
   1554  1.1  christos     BIO_puts(out, "\n");
   1555  1.1  christos     ret = 1;
   1556  1.1  christos     if (retai) {
   1557  1.1  christos         *retai = ai;
   1558  1.1  christos         ai = NULL;
   1559  1.1  christos     }
   1560  1.1  christos  err:
   1561  1.1  christos     if (!ret)
   1562  1.1  christos         ERR_print_errors(bio_err);
   1563  1.1  christos     BIO_free_all(out);
   1564  1.1  christos     ASN1_INTEGER_free(ai);
   1565  1.1  christos     return ret;
   1566  1.1  christos }
   1567  1.1  christos 
   1568  1.1  christos int rotate_serial(const char *serialfile, const char *new_suffix,
   1569  1.1  christos                   const char *old_suffix)
   1570  1.1  christos {
   1571  1.1  christos     char buf[2][BSIZE];
   1572  1.1  christos     int i, j;
   1573  1.1  christos 
   1574  1.1  christos     i = strlen(serialfile) + strlen(old_suffix);
   1575  1.1  christos     j = strlen(serialfile) + strlen(new_suffix);
   1576  1.1  christos     if (i > j)
   1577  1.1  christos         j = i;
   1578  1.1  christos     if (j + 1 >= BSIZE) {
   1579  1.1  christos         BIO_printf(bio_err, "File name too long\n");
   1580  1.1  christos         goto err;
   1581  1.1  christos     }
   1582  1.1  christos #ifndef OPENSSL_SYS_VMS
   1583  1.1  christos     j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", serialfile, new_suffix);
   1584  1.1  christos     j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.%s", serialfile, old_suffix);
   1585  1.1  christos #else
   1586  1.1  christos     j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", serialfile, new_suffix);
   1587  1.1  christos     j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-%s", serialfile, old_suffix);
   1588  1.1  christos #endif
   1589  1.1  christos     if (rename(serialfile, buf[1]) < 0 && errno != ENOENT
   1590  1.1  christos #ifdef ENOTDIR
   1591  1.1  christos         && errno != ENOTDIR
   1592  1.1  christos #endif
   1593  1.1  christos         ) {
   1594  1.1  christos         BIO_printf(bio_err,
   1595  1.1  christos                    "Unable to rename %s to %s\n", serialfile, buf[1]);
   1596  1.1  christos         perror("reason");
   1597  1.1  christos         goto err;
   1598  1.1  christos     }
   1599  1.1  christos     if (rename(buf[0], serialfile) < 0) {
   1600  1.1  christos         BIO_printf(bio_err,
   1601  1.1  christos                    "Unable to rename %s to %s\n", buf[0], serialfile);
   1602  1.1  christos         perror("reason");
   1603  1.1  christos         rename(buf[1], serialfile);
   1604  1.1  christos         goto err;
   1605  1.1  christos     }
   1606  1.1  christos     return 1;
   1607  1.1  christos  err:
   1608  1.1  christos     ERR_print_errors(bio_err);
   1609  1.1  christos     return 0;
   1610  1.1  christos }
   1611  1.1  christos 
   1612  1.1  christos int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
   1613  1.1  christos {
   1614  1.1  christos     BIGNUM *btmp;
   1615  1.1  christos     int ret = 0;
   1616  1.1  christos 
   1617  1.1  christos     btmp = b == NULL ? BN_new() : b;
   1618  1.1  christos     if (btmp == NULL)
   1619  1.1  christos         return 0;
   1620  1.1  christos 
   1621  1.1  christos     if (!BN_rand(btmp, SERIAL_RAND_BITS, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
   1622  1.1  christos         goto error;
   1623  1.1  christos     if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
   1624  1.1  christos         goto error;
   1625  1.1  christos 
   1626  1.1  christos     ret = 1;
   1627  1.1  christos 
   1628  1.1  christos  error:
   1629  1.1  christos 
   1630  1.1  christos     if (btmp != b)
   1631  1.1  christos         BN_free(btmp);
   1632  1.1  christos 
   1633  1.1  christos     return ret;
   1634  1.1  christos }
   1635  1.1  christos 
   1636  1.1  christos CA_DB *load_index(const char *dbfile, DB_ATTR *db_attr)
   1637  1.1  christos {
   1638  1.1  christos     CA_DB *retdb = NULL;
   1639  1.1  christos     TXT_DB *tmpdb = NULL;
   1640  1.1  christos     BIO *in;
   1641  1.1  christos     CONF *dbattr_conf = NULL;
   1642  1.1  christos     char buf[BSIZE];
   1643  1.1  christos #ifndef OPENSSL_NO_POSIX_IO
   1644  1.1  christos     FILE *dbfp;
   1645  1.1  christos     struct stat dbst;
   1646  1.1  christos #endif
   1647  1.1  christos 
   1648  1.1  christos     in = BIO_new_file(dbfile, "r");
   1649  1.1  christos     if (in == NULL)
   1650  1.1  christos         goto err;
   1651  1.1  christos 
   1652  1.1  christos #ifndef OPENSSL_NO_POSIX_IO
   1653  1.1  christos     BIO_get_fp(in, &dbfp);
   1654  1.1  christos     if (fstat(fileno(dbfp), &dbst) == -1) {
   1655  1.1  christos         ERR_raise_data(ERR_LIB_SYS, errno,
   1656  1.1  christos                        "calling fstat(%s)", dbfile);
   1657  1.1  christos         goto err;
   1658  1.1  christos     }
   1659  1.1  christos #endif
   1660  1.1  christos 
   1661  1.1  christos     if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
   1662  1.1  christos         goto err;
   1663  1.1  christos 
   1664  1.1  christos #ifndef OPENSSL_SYS_VMS
   1665  1.1  christos     BIO_snprintf(buf, sizeof(buf), "%s.attr", dbfile);
   1666  1.1  christos #else
   1667  1.1  christos     BIO_snprintf(buf, sizeof(buf), "%s-attr", dbfile);
   1668  1.1  christos #endif
   1669  1.1  christos     dbattr_conf = app_load_config_quiet(buf);
   1670  1.1  christos 
   1671  1.1  christos     retdb = app_malloc(sizeof(*retdb), "new DB");
   1672  1.1  christos     retdb->db = tmpdb;
   1673  1.1  christos     tmpdb = NULL;
   1674  1.1  christos     if (db_attr)
   1675  1.1  christos         retdb->attributes = *db_attr;
   1676  1.1  christos     else {
   1677  1.1  christos         retdb->attributes.unique_subject = 1;
   1678  1.1  christos     }
   1679  1.1  christos 
   1680  1.1  christos     if (dbattr_conf) {
   1681  1.1  christos         char *p = NCONF_get_string(dbattr_conf, NULL, "unique_subject");
   1682  1.1  christos         if (p) {
   1683  1.1  christos             retdb->attributes.unique_subject = parse_yesno(p, 1);
   1684  1.5  christos         } else {
   1685  1.5  christos             ERR_clear_error();
   1686  1.1  christos         }
   1687  1.5  christos 
   1688  1.1  christos     }
   1689  1.1  christos 
   1690  1.1  christos     retdb->dbfname = OPENSSL_strdup(dbfile);
   1691  1.1  christos #ifndef OPENSSL_NO_POSIX_IO
   1692  1.1  christos     retdb->dbst = dbst;
   1693  1.1  christos #endif
   1694  1.1  christos 
   1695  1.1  christos  err:
   1696  1.1  christos     ERR_print_errors(bio_err);
   1697  1.1  christos     NCONF_free(dbattr_conf);
   1698  1.1  christos     TXT_DB_free(tmpdb);
   1699  1.1  christos     BIO_free_all(in);
   1700  1.1  christos     return retdb;
   1701  1.1  christos }
   1702  1.1  christos 
   1703  1.1  christos /*
   1704  1.1  christos  * Returns > 0 on success, <= 0 on error
   1705  1.1  christos  */
   1706  1.1  christos int index_index(CA_DB *db)
   1707  1.1  christos {
   1708  1.1  christos     if (!TXT_DB_create_index(db->db, DB_serial, NULL,
   1709  1.1  christos                              LHASH_HASH_FN(index_serial),
   1710  1.1  christos                              LHASH_COMP_FN(index_serial))) {
   1711  1.1  christos         BIO_printf(bio_err,
   1712  1.1  christos                    "Error creating serial number index:(%ld,%ld,%ld)\n",
   1713  1.1  christos                    db->db->error, db->db->arg1, db->db->arg2);
   1714  1.1  christos         goto err;
   1715  1.1  christos     }
   1716  1.1  christos 
   1717  1.1  christos     if (db->attributes.unique_subject
   1718  1.1  christos         && !TXT_DB_create_index(db->db, DB_name, index_name_qual,
   1719  1.1  christos                                 LHASH_HASH_FN(index_name),
   1720  1.1  christos                                 LHASH_COMP_FN(index_name))) {
   1721  1.1  christos         BIO_printf(bio_err, "Error creating name index:(%ld,%ld,%ld)\n",
   1722  1.1  christos                    db->db->error, db->db->arg1, db->db->arg2);
   1723  1.1  christos         goto err;
   1724  1.1  christos     }
   1725  1.1  christos     return 1;
   1726  1.1  christos  err:
   1727  1.1  christos     ERR_print_errors(bio_err);
   1728  1.1  christos     return 0;
   1729  1.1  christos }
   1730  1.1  christos 
   1731  1.1  christos int save_index(const char *dbfile, const char *suffix, CA_DB *db)
   1732  1.1  christos {
   1733  1.1  christos     char buf[3][BSIZE];
   1734  1.1  christos     BIO *out;
   1735  1.1  christos     int j;
   1736  1.1  christos 
   1737  1.1  christos     j = strlen(dbfile) + strlen(suffix);
   1738  1.1  christos     if (j + 6 >= BSIZE) {
   1739  1.1  christos         BIO_printf(bio_err, "File name too long\n");
   1740  1.1  christos         goto err;
   1741  1.1  christos     }
   1742  1.1  christos #ifndef OPENSSL_SYS_VMS
   1743  1.1  christos     j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s.attr", dbfile);
   1744  1.1  christos     j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.attr.%s", dbfile, suffix);
   1745  1.1  christos     j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", dbfile, suffix);
   1746  1.1  christos #else
   1747  1.1  christos     j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s-attr", dbfile);
   1748  1.1  christos     j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-attr-%s", dbfile, suffix);
   1749  1.1  christos     j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", dbfile, suffix);
   1750  1.1  christos #endif
   1751  1.1  christos     out = BIO_new_file(buf[0], "w");
   1752  1.1  christos     if (out == NULL) {
   1753  1.1  christos         perror(dbfile);
   1754  1.1  christos         BIO_printf(bio_err, "Unable to open '%s'\n", dbfile);
   1755  1.1  christos         goto err;
   1756  1.1  christos     }
   1757  1.1  christos     j = TXT_DB_write(out, db->db);
   1758  1.1  christos     BIO_free(out);
   1759  1.1  christos     if (j <= 0)
   1760  1.1  christos         goto err;
   1761  1.1  christos 
   1762  1.1  christos     out = BIO_new_file(buf[1], "w");
   1763  1.1  christos     if (out == NULL) {
   1764  1.1  christos         perror(buf[2]);
   1765  1.1  christos         BIO_printf(bio_err, "Unable to open '%s'\n", buf[2]);
   1766  1.1  christos         goto err;
   1767  1.1  christos     }
   1768  1.1  christos     BIO_printf(out, "unique_subject = %s\n",
   1769  1.1  christos                db->attributes.unique_subject ? "yes" : "no");
   1770  1.1  christos     BIO_free(out);
   1771  1.1  christos 
   1772  1.1  christos     return 1;
   1773  1.1  christos  err:
   1774  1.1  christos     ERR_print_errors(bio_err);
   1775  1.1  christos     return 0;
   1776  1.1  christos }
   1777  1.1  christos 
   1778  1.1  christos int rotate_index(const char *dbfile, const char *new_suffix,
   1779  1.1  christos                  const char *old_suffix)
   1780  1.1  christos {
   1781  1.1  christos     char buf[5][BSIZE];
   1782  1.1  christos     int i, j;
   1783  1.1  christos 
   1784  1.1  christos     i = strlen(dbfile) + strlen(old_suffix);
   1785  1.1  christos     j = strlen(dbfile) + strlen(new_suffix);
   1786  1.1  christos     if (i > j)
   1787  1.1  christos         j = i;
   1788  1.1  christos     if (j + 6 >= BSIZE) {
   1789  1.1  christos         BIO_printf(bio_err, "File name too long\n");
   1790  1.1  christos         goto err;
   1791  1.1  christos     }
   1792  1.1  christos #ifndef OPENSSL_SYS_VMS
   1793  1.1  christos     j = BIO_snprintf(buf[4], sizeof(buf[4]), "%s.attr", dbfile);
   1794  1.1  christos     j = BIO_snprintf(buf[3], sizeof(buf[3]), "%s.attr.%s", dbfile, old_suffix);
   1795  1.1  christos     j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s.attr.%s", dbfile, new_suffix);
   1796  1.1  christos     j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.%s", dbfile, old_suffix);
   1797  1.1  christos     j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", dbfile, new_suffix);
   1798  1.1  christos #else
   1799  1.1  christos     j = BIO_snprintf(buf[4], sizeof(buf[4]), "%s-attr", dbfile);
   1800  1.1  christos     j = BIO_snprintf(buf[3], sizeof(buf[3]), "%s-attr-%s", dbfile, old_suffix);
   1801  1.1  christos     j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s-attr-%s", dbfile, new_suffix);
   1802  1.1  christos     j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-%s", dbfile, old_suffix);
   1803  1.1  christos     j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", dbfile, new_suffix);
   1804  1.1  christos #endif
   1805  1.1  christos     if (rename(dbfile, buf[1]) < 0 && errno != ENOENT
   1806  1.1  christos #ifdef ENOTDIR
   1807  1.1  christos         && errno != ENOTDIR
   1808  1.1  christos #endif
   1809  1.1  christos         ) {
   1810  1.1  christos         BIO_printf(bio_err, "Unable to rename %s to %s\n", dbfile, buf[1]);
   1811  1.1  christos         perror("reason");
   1812  1.1  christos         goto err;
   1813  1.1  christos     }
   1814  1.1  christos     if (rename(buf[0], dbfile) < 0) {
   1815  1.1  christos         BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[0], dbfile);
   1816  1.1  christos         perror("reason");
   1817  1.1  christos         rename(buf[1], dbfile);
   1818  1.1  christos         goto err;
   1819  1.1  christos     }
   1820  1.1  christos     if (rename(buf[4], buf[3]) < 0 && errno != ENOENT
   1821  1.1  christos #ifdef ENOTDIR
   1822  1.1  christos         && errno != ENOTDIR
   1823  1.1  christos #endif
   1824  1.1  christos         ) {
   1825  1.1  christos         BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[4], buf[3]);
   1826  1.1  christos         perror("reason");
   1827  1.1  christos         rename(dbfile, buf[0]);
   1828  1.1  christos         rename(buf[1], dbfile);
   1829  1.1  christos         goto err;
   1830  1.1  christos     }
   1831  1.1  christos     if (rename(buf[2], buf[4]) < 0) {
   1832  1.1  christos         BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[2], buf[4]);
   1833  1.1  christos         perror("reason");
   1834  1.1  christos         rename(buf[3], buf[4]);
   1835  1.1  christos         rename(dbfile, buf[0]);
   1836  1.1  christos         rename(buf[1], dbfile);
   1837  1.1  christos         goto err;
   1838  1.1  christos     }
   1839  1.1  christos     return 1;
   1840  1.1  christos  err:
   1841  1.1  christos     ERR_print_errors(bio_err);
   1842  1.1  christos     return 0;
   1843  1.1  christos }
   1844  1.1  christos 
   1845  1.1  christos void free_index(CA_DB *db)
   1846  1.1  christos {
   1847  1.1  christos     if (db) {
   1848  1.1  christos         TXT_DB_free(db->db);
   1849  1.1  christos         OPENSSL_free(db->dbfname);
   1850  1.1  christos         OPENSSL_free(db);
   1851  1.1  christos     }
   1852  1.1  christos }
   1853  1.1  christos 
   1854  1.1  christos int parse_yesno(const char *str, int def)
   1855  1.1  christos {
   1856  1.1  christos     if (str) {
   1857  1.1  christos         switch (*str) {
   1858  1.1  christos         case 'f':              /* false */
   1859  1.1  christos         case 'F':              /* FALSE */
   1860  1.1  christos         case 'n':              /* no */
   1861  1.1  christos         case 'N':              /* NO */
   1862  1.1  christos         case '0':              /* 0 */
   1863  1.1  christos             return 0;
   1864  1.1  christos         case 't':              /* true */
   1865  1.1  christos         case 'T':              /* TRUE */
   1866  1.1  christos         case 'y':              /* yes */
   1867  1.1  christos         case 'Y':              /* YES */
   1868  1.1  christos         case '1':              /* 1 */
   1869  1.1  christos             return 1;
   1870  1.1  christos         }
   1871  1.1  christos     }
   1872  1.1  christos     return def;
   1873  1.1  christos }
   1874  1.1  christos 
   1875  1.1  christos /*
   1876  1.1  christos  * name is expected to be in the format /type0=value0/type1=value1/type2=...
   1877  1.1  christos  * where + can be used instead of / to form multi-valued RDNs if canmulti
   1878  1.1  christos  * and characters may be escaped by \
   1879  1.1  christos  */
   1880  1.1  christos X509_NAME *parse_name(const char *cp, int chtype, int canmulti,
   1881  1.1  christos                       const char *desc)
   1882  1.1  christos {
   1883  1.1  christos     int nextismulti = 0;
   1884  1.1  christos     char *work;
   1885  1.1  christos     X509_NAME *n;
   1886  1.1  christos 
   1887  1.1  christos     if (*cp++ != '/') {
   1888  1.1  christos         BIO_printf(bio_err,
   1889  1.1  christos                    "%s: %s name is expected to be in the format "
   1890  1.1  christos                    "/type0=value0/type1=value1/type2=... where characters may "
   1891  1.1  christos                    "be escaped by \\. This name is not in that format: '%s'\n",
   1892  1.1  christos                    opt_getprog(), desc, --cp);
   1893  1.1  christos         return NULL;
   1894  1.1  christos     }
   1895  1.1  christos 
   1896  1.1  christos     n = X509_NAME_new();
   1897  1.1  christos     if (n == NULL) {
   1898  1.1  christos         BIO_printf(bio_err, "%s: Out of memory\n", opt_getprog());
   1899  1.1  christos         return NULL;
   1900  1.1  christos     }
   1901  1.1  christos     work = OPENSSL_strdup(cp);
   1902  1.1  christos     if (work == NULL) {
   1903  1.1  christos         BIO_printf(bio_err, "%s: Error copying %s name input\n",
   1904  1.1  christos                    opt_getprog(), desc);
   1905  1.1  christos         goto err;
   1906  1.1  christos     }
   1907  1.1  christos 
   1908  1.1  christos     while (*cp != '\0') {
   1909  1.1  christos         char *bp = work;
   1910  1.1  christos         char *typestr = bp;
   1911  1.1  christos         unsigned char *valstr;
   1912  1.1  christos         int nid;
   1913  1.1  christos         int ismulti = nextismulti;
   1914  1.1  christos         nextismulti = 0;
   1915  1.1  christos 
   1916  1.1  christos         /* Collect the type */
   1917  1.1  christos         while (*cp != '\0' && *cp != '=')
   1918  1.1  christos             *bp++ = *cp++;
   1919  1.1  christos         *bp++ = '\0';
   1920  1.1  christos         if (*cp == '\0') {
   1921  1.1  christos             BIO_printf(bio_err,
   1922  1.1  christos                        "%s: Missing '=' after RDN type string '%s' in %s name string\n",
   1923  1.1  christos                        opt_getprog(), typestr, desc);
   1924  1.1  christos             goto err;
   1925  1.1  christos         }
   1926  1.1  christos         ++cp;
   1927  1.1  christos 
   1928  1.1  christos         /* Collect the value. */
   1929  1.1  christos         valstr = (unsigned char *)bp;
   1930  1.1  christos         for (; *cp != '\0' && *cp != '/'; *bp++ = *cp++) {
   1931  1.1  christos             /* unescaped '+' symbol string signals further member of multiRDN */
   1932  1.1  christos             if (canmulti && *cp == '+') {
   1933  1.1  christos                 nextismulti = 1;
   1934  1.1  christos                 break;
   1935  1.1  christos             }
   1936  1.1  christos             if (*cp == '\\' && *++cp == '\0') {
   1937  1.1  christos                 BIO_printf(bio_err,
   1938  1.1  christos                            "%s: Escape character at end of %s name string\n",
   1939  1.1  christos                            opt_getprog(), desc);
   1940  1.1  christos                 goto err;
   1941  1.1  christos             }
   1942  1.1  christos         }
   1943  1.1  christos         *bp++ = '\0';
   1944  1.1  christos 
   1945  1.1  christos         /* If not at EOS (must be + or /), move forward. */
   1946  1.1  christos         if (*cp != '\0')
   1947  1.1  christos             ++cp;
   1948  1.1  christos 
   1949  1.1  christos         /* Parse */
   1950  1.1  christos         nid = OBJ_txt2nid(typestr);
   1951  1.1  christos         if (nid == NID_undef) {
   1952  1.1  christos             BIO_printf(bio_err,
   1953  1.5  christos                        "%s warning: Skipping unknown %s name attribute \"%s\"\n",
   1954  1.1  christos                        opt_getprog(), desc, typestr);
   1955  1.1  christos             if (ismulti)
   1956  1.1  christos                 BIO_printf(bio_err,
   1957  1.5  christos                            "%s hint: a '+' in a value string needs be escaped using '\\' else a new member of a multi-valued RDN is expected\n",
   1958  1.5  christos                            opt_getprog());
   1959  1.1  christos             continue;
   1960  1.1  christos         }
   1961  1.1  christos         if (*valstr == '\0') {
   1962  1.1  christos             BIO_printf(bio_err,
   1963  1.5  christos                        "%s warning: No value provided for %s name attribute \"%s\", skipped\n",
   1964  1.1  christos                        opt_getprog(), desc, typestr);
   1965  1.1  christos             continue;
   1966  1.1  christos         }
   1967  1.1  christos         if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
   1968  1.1  christos                                         valstr, strlen((char *)valstr),
   1969  1.1  christos                                         -1, ismulti ? -1 : 0)) {
   1970  1.1  christos             ERR_print_errors(bio_err);
   1971  1.1  christos             BIO_printf(bio_err,
   1972  1.1  christos                        "%s: Error adding %s name attribute \"/%s=%s\"\n",
   1973  1.1  christos                        opt_getprog(), desc, typestr ,valstr);
   1974  1.1  christos             goto err;
   1975  1.1  christos         }
   1976  1.1  christos     }
   1977  1.1  christos 
   1978  1.1  christos     OPENSSL_free(work);
   1979  1.1  christos     return n;
   1980  1.1  christos 
   1981  1.1  christos  err:
   1982  1.1  christos     X509_NAME_free(n);
   1983  1.1  christos     OPENSSL_free(work);
   1984  1.1  christos     return NULL;
   1985  1.1  christos }
   1986  1.1  christos 
   1987  1.1  christos /*
   1988  1.1  christos  * Read whole contents of a BIO into an allocated memory buffer and return
   1989  1.1  christos  * it.
   1990  1.1  christos  */
   1991  1.1  christos 
   1992  1.1  christos int bio_to_mem(unsigned char **out, int maxlen, BIO *in)
   1993  1.1  christos {
   1994  1.1  christos     BIO *mem;
   1995  1.1  christos     int len, ret;
   1996  1.1  christos     unsigned char tbuf[1024];
   1997  1.1  christos 
   1998  1.1  christos     mem = BIO_new(BIO_s_mem());
   1999  1.1  christos     if (mem == NULL)
   2000  1.1  christos         return -1;
   2001  1.1  christos     for (;;) {
   2002  1.1  christos         if ((maxlen != -1) && maxlen < 1024)
   2003  1.1  christos             len = maxlen;
   2004  1.1  christos         else
   2005  1.1  christos             len = 1024;
   2006  1.1  christos         len = BIO_read(in, tbuf, len);
   2007  1.1  christos         if (len < 0) {
   2008  1.1  christos             BIO_free(mem);
   2009  1.1  christos             return -1;
   2010  1.1  christos         }
   2011  1.1  christos         if (len == 0)
   2012  1.1  christos             break;
   2013  1.1  christos         if (BIO_write(mem, tbuf, len) != len) {
   2014  1.1  christos             BIO_free(mem);
   2015  1.1  christos             return -1;
   2016  1.1  christos         }
   2017  1.5  christos         if (maxlen != -1)
   2018  1.5  christos             maxlen -= len;
   2019  1.1  christos 
   2020  1.1  christos         if (maxlen == 0)
   2021  1.1  christos             break;
   2022  1.1  christos     }
   2023  1.1  christos     ret = BIO_get_mem_data(mem, (char **)out);
   2024  1.1  christos     BIO_set_flags(mem, BIO_FLAGS_MEM_RDONLY);
   2025  1.1  christos     BIO_free(mem);
   2026  1.1  christos     return ret;
   2027  1.1  christos }
   2028  1.1  christos 
   2029  1.1  christos int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value)
   2030  1.1  christos {
   2031  1.1  christos     int rv = 0;
   2032  1.1  christos     char *stmp, *vtmp = NULL;
   2033  1.1  christos 
   2034  1.1  christos     stmp = OPENSSL_strdup(value);
   2035  1.1  christos     if (stmp == NULL)
   2036  1.1  christos         return -1;
   2037  1.1  christos     vtmp = strchr(stmp, ':');
   2038  1.1  christos     if (vtmp == NULL)
   2039  1.1  christos         goto err;
   2040  1.1  christos 
   2041  1.1  christos     *vtmp = 0;
   2042  1.1  christos     vtmp++;
   2043  1.1  christos     rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp);
   2044  1.1  christos 
   2045  1.1  christos  err:
   2046  1.1  christos     OPENSSL_free(stmp);
   2047  1.1  christos     return rv;
   2048  1.1  christos }
   2049  1.1  christos 
   2050  1.1  christos static void nodes_print(const char *name, STACK_OF(X509_POLICY_NODE) *nodes)
   2051  1.1  christos {
   2052  1.1  christos     X509_POLICY_NODE *node;
   2053  1.1  christos     int i;
   2054  1.1  christos 
   2055  1.1  christos     BIO_printf(bio_err, "%s Policies:", name);
   2056  1.1  christos     if (nodes) {
   2057  1.1  christos         BIO_puts(bio_err, "\n");
   2058  1.1  christos         for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++) {
   2059  1.1  christos             node = sk_X509_POLICY_NODE_value(nodes, i);
   2060  1.1  christos             X509_POLICY_NODE_print(bio_err, node, 2);
   2061  1.1  christos         }
   2062  1.1  christos     } else {
   2063  1.1  christos         BIO_puts(bio_err, " <empty>\n");
   2064  1.1  christos     }
   2065  1.1  christos }
   2066  1.1  christos 
   2067  1.1  christos void policies_print(X509_STORE_CTX *ctx)
   2068  1.1  christos {
   2069  1.1  christos     X509_POLICY_TREE *tree;
   2070  1.1  christos     int explicit_policy;
   2071  1.1  christos     tree = X509_STORE_CTX_get0_policy_tree(ctx);
   2072  1.1  christos     explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx);
   2073  1.1  christos 
   2074  1.1  christos     BIO_printf(bio_err, "Require explicit Policy: %s\n",
   2075  1.1  christos                explicit_policy ? "True" : "False");
   2076  1.1  christos 
   2077  1.1  christos     nodes_print("Authority", X509_policy_tree_get0_policies(tree));
   2078  1.1  christos     nodes_print("User", X509_policy_tree_get0_user_policies(tree));
   2079  1.1  christos }
   2080  1.1  christos 
   2081  1.1  christos /*-
   2082  1.1  christos  * next_protos_parse parses a comma separated list of strings into a string
   2083  1.1  christos  * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
   2084  1.1  christos  *   outlen: (output) set to the length of the resulting buffer on success.
   2085  1.1  christos  *   err: (maybe NULL) on failure, an error message line is written to this BIO.
   2086  1.1  christos  *   in: a NUL terminated string like "abc,def,ghi"
   2087  1.1  christos  *
   2088  1.1  christos  *   returns: a malloc'd buffer or NULL on failure.
   2089  1.1  christos  */
   2090  1.1  christos unsigned char *next_protos_parse(size_t *outlen, const char *in)
   2091  1.1  christos {
   2092  1.1  christos     size_t len;
   2093  1.1  christos     unsigned char *out;
   2094  1.1  christos     size_t i, start = 0;
   2095  1.1  christos     size_t skipped = 0;
   2096  1.1  christos 
   2097  1.1  christos     len = strlen(in);
   2098  1.1  christos     if (len == 0 || len >= 65535)
   2099  1.1  christos         return NULL;
   2100  1.1  christos 
   2101  1.1  christos     out = app_malloc(len + 1, "NPN buffer");
   2102  1.1  christos     for (i = 0; i <= len; ++i) {
   2103  1.1  christos         if (i == len || in[i] == ',') {
   2104  1.1  christos             /*
   2105  1.1  christos              * Zero-length ALPN elements are invalid on the wire, we could be
   2106  1.1  christos              * strict and reject the entire string, but just ignoring extra
   2107  1.1  christos              * commas seems harmless and more friendly.
   2108  1.1  christos              *
   2109  1.1  christos              * Every comma we skip in this way puts the input buffer another
   2110  1.1  christos              * byte ahead of the output buffer, so all stores into the output
   2111  1.1  christos              * buffer need to be decremented by the number commas skipped.
   2112  1.1  christos              */
   2113  1.1  christos             if (i == start) {
   2114  1.1  christos                 ++start;
   2115  1.1  christos                 ++skipped;
   2116  1.1  christos                 continue;
   2117  1.1  christos             }
   2118  1.1  christos             if (i - start > 255) {
   2119  1.1  christos                 OPENSSL_free(out);
   2120  1.1  christos                 return NULL;
   2121  1.1  christos             }
   2122  1.1  christos             out[start-skipped] = (unsigned char)(i - start);
   2123  1.1  christos             start = i + 1;
   2124  1.1  christos         } else {
   2125  1.1  christos             out[i + 1 - skipped] = in[i];
   2126  1.1  christos         }
   2127  1.1  christos     }
   2128  1.1  christos 
   2129  1.1  christos     if (len <= skipped) {
   2130  1.1  christos         OPENSSL_free(out);
   2131  1.1  christos         return NULL;
   2132  1.1  christos     }
   2133  1.1  christos 
   2134  1.1  christos     *outlen = len + 1 - skipped;
   2135  1.1  christos     return out;
   2136  1.1  christos }
   2137  1.1  christos 
   2138  1.1  christos void print_cert_checks(BIO *bio, X509 *x,
   2139  1.1  christos                        const char *checkhost,
   2140  1.1  christos                        const char *checkemail, const char *checkip)
   2141  1.1  christos {
   2142  1.1  christos     if (x == NULL)
   2143  1.1  christos         return;
   2144  1.1  christos     if (checkhost) {
   2145  1.1  christos         BIO_printf(bio, "Hostname %s does%s match certificate\n",
   2146  1.1  christos                    checkhost,
   2147  1.1  christos                    X509_check_host(x, checkhost, 0, 0, NULL) == 1
   2148  1.1  christos                        ? "" : " NOT");
   2149  1.1  christos     }
   2150  1.1  christos 
   2151  1.1  christos     if (checkemail) {
   2152  1.1  christos         BIO_printf(bio, "Email %s does%s match certificate\n",
   2153  1.1  christos                    checkemail, X509_check_email(x, checkemail, 0, 0)
   2154  1.1  christos                    ? "" : " NOT");
   2155  1.1  christos     }
   2156  1.1  christos 
   2157  1.1  christos     if (checkip) {
   2158  1.1  christos         BIO_printf(bio, "IP %s does%s match certificate\n",
   2159  1.1  christos                    checkip, X509_check_ip_asc(x, checkip, 0) ? "" : " NOT");
   2160  1.1  christos     }
   2161  1.1  christos }
   2162  1.1  christos 
   2163  1.1  christos static int do_pkey_ctx_init(EVP_PKEY_CTX *pkctx, STACK_OF(OPENSSL_STRING) *opts)
   2164  1.1  christos {
   2165  1.1  christos     int i;
   2166  1.1  christos 
   2167  1.1  christos     if (opts == NULL)
   2168  1.1  christos         return 1;
   2169  1.1  christos 
   2170  1.1  christos     for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
   2171  1.1  christos         char *opt = sk_OPENSSL_STRING_value(opts, i);
   2172  1.1  christos         if (pkey_ctrl_string(pkctx, opt) <= 0) {
   2173  1.1  christos             BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
   2174  1.1  christos             ERR_print_errors(bio_err);
   2175  1.1  christos             return 0;
   2176  1.1  christos         }
   2177  1.1  christos     }
   2178  1.1  christos 
   2179  1.1  christos     return 1;
   2180  1.1  christos }
   2181  1.1  christos 
   2182  1.1  christos static int do_x509_init(X509 *x, STACK_OF(OPENSSL_STRING) *opts)
   2183  1.1  christos {
   2184  1.1  christos     int i;
   2185  1.1  christos 
   2186  1.1  christos     if (opts == NULL)
   2187  1.1  christos         return 1;
   2188  1.1  christos 
   2189  1.1  christos     for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
   2190  1.1  christos         char *opt = sk_OPENSSL_STRING_value(opts, i);
   2191  1.1  christos         if (x509_ctrl_string(x, opt) <= 0) {
   2192  1.1  christos             BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
   2193  1.1  christos             ERR_print_errors(bio_err);
   2194  1.1  christos             return 0;
   2195  1.1  christos         }
   2196  1.1  christos     }
   2197  1.1  christos 
   2198  1.1  christos     return 1;
   2199  1.1  christos }
   2200  1.1  christos 
   2201  1.1  christos static int do_x509_req_init(X509_REQ *x, STACK_OF(OPENSSL_STRING) *opts)
   2202  1.1  christos {
   2203  1.1  christos     int i;
   2204  1.1  christos 
   2205  1.1  christos     if (opts == NULL)
   2206  1.1  christos         return 1;
   2207  1.1  christos 
   2208  1.1  christos     for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
   2209  1.1  christos         char *opt = sk_OPENSSL_STRING_value(opts, i);
   2210  1.1  christos         if (x509_req_ctrl_string(x, opt) <= 0) {
   2211  1.1  christos             BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
   2212  1.1  christos             ERR_print_errors(bio_err);
   2213  1.1  christos             return 0;
   2214  1.1  christos         }
   2215  1.1  christos     }
   2216  1.1  christos 
   2217  1.1  christos     return 1;
   2218  1.1  christos }
   2219  1.1  christos 
   2220  1.1  christos static int do_sign_init(EVP_MD_CTX *ctx, EVP_PKEY *pkey,
   2221  1.1  christos                         const char *md, STACK_OF(OPENSSL_STRING) *sigopts)
   2222  1.1  christos {
   2223  1.1  christos     EVP_PKEY_CTX *pkctx = NULL;
   2224  1.1  christos     char def_md[80];
   2225  1.1  christos 
   2226  1.1  christos     if (ctx == NULL)
   2227  1.1  christos         return 0;
   2228  1.1  christos     /*
   2229  1.1  christos      * EVP_PKEY_get_default_digest_name() returns 2 if the digest is mandatory
   2230  1.1  christos      * for this algorithm.
   2231  1.1  christos      */
   2232  1.1  christos     if (EVP_PKEY_get_default_digest_name(pkey, def_md, sizeof(def_md)) == 2
   2233  1.1  christos             && strcmp(def_md, "UNDEF") == 0) {
   2234  1.1  christos         /* The signing algorithm requires there to be no digest */
   2235  1.1  christos         md = NULL;
   2236  1.1  christos     }
   2237  1.1  christos 
   2238  1.1  christos     return EVP_DigestSignInit_ex(ctx, &pkctx, md, app_get0_libctx(),
   2239  1.1  christos                                  app_get0_propq(), pkey, NULL)
   2240  1.1  christos         && do_pkey_ctx_init(pkctx, sigopts);
   2241  1.1  christos }
   2242  1.1  christos 
   2243  1.1  christos static int adapt_keyid_ext(X509 *cert, X509V3_CTX *ext_ctx,
   2244  1.1  christos                            const char *name, const char *value, int add_default)
   2245  1.1  christos {
   2246  1.1  christos     const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(cert);
   2247  1.1  christos     X509_EXTENSION *new_ext = X509V3_EXT_nconf(NULL, ext_ctx, name, value);
   2248  1.1  christos     int idx, rv = 0;
   2249  1.1  christos 
   2250  1.1  christos     if (new_ext == NULL)
   2251  1.1  christos         return rv;
   2252  1.1  christos 
   2253  1.1  christos     idx = X509v3_get_ext_by_OBJ(exts, X509_EXTENSION_get_object(new_ext), -1);
   2254  1.1  christos     if (idx >= 0) {
   2255  1.1  christos         X509_EXTENSION *found_ext = X509v3_get_ext(exts, idx);
   2256  1.1  christos         ASN1_OCTET_STRING *data = X509_EXTENSION_get_data(found_ext);
   2257  1.1  christos         int disabled = ASN1_STRING_length(data) <= 2; /* config said "none" */
   2258  1.1  christos 
   2259  1.1  christos         if (disabled) {
   2260  1.1  christos             X509_delete_ext(cert, idx);
   2261  1.1  christos             X509_EXTENSION_free(found_ext);
   2262  1.1  christos         } /* else keep existing key identifier, which might be outdated */
   2263  1.1  christos         rv = 1;
   2264  1.1  christos     } else  {
   2265  1.1  christos         rv = !add_default || X509_add_ext(cert, new_ext, -1);
   2266  1.1  christos     }
   2267  1.1  christos     X509_EXTENSION_free(new_ext);
   2268  1.1  christos     return rv;
   2269  1.1  christos }
   2270  1.1  christos 
   2271  1.1  christos /* Ensure RFC 5280 compliance, adapt keyIDs as needed, and sign the cert info */
   2272  1.1  christos int do_X509_sign(X509 *cert, EVP_PKEY *pkey, const char *md,
   2273  1.1  christos                  STACK_OF(OPENSSL_STRING) *sigopts, X509V3_CTX *ext_ctx)
   2274  1.1  christos {
   2275  1.1  christos     const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(cert);
   2276  1.1  christos     EVP_MD_CTX *mctx = EVP_MD_CTX_new();
   2277  1.1  christos     int self_sign;
   2278  1.1  christos     int rv = 0;
   2279  1.1  christos 
   2280  1.1  christos     if (sk_X509_EXTENSION_num(exts /* may be NULL */) > 0) {
   2281  1.1  christos         /* Prevent X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3 */
   2282  1.1  christos         if (!X509_set_version(cert, X509_VERSION_3))
   2283  1.1  christos             goto end;
   2284  1.1  christos 
   2285  1.1  christos         /*
   2286  1.1  christos          * Add default SKID before such that default AKID can make use of it
   2287  1.1  christos          * in case the certificate is self-signed
   2288  1.1  christos          */
   2289  1.1  christos         /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */
   2290  1.1  christos         if (!adapt_keyid_ext(cert, ext_ctx, "subjectKeyIdentifier", "hash", 1))
   2291  1.1  christos             goto end;
   2292  1.1  christos         /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */
   2293  1.1  christos         ERR_set_mark();
   2294  1.1  christos         self_sign = X509_check_private_key(cert, pkey);
   2295  1.1  christos         ERR_pop_to_mark();
   2296  1.1  christos         if (!adapt_keyid_ext(cert, ext_ctx, "authorityKeyIdentifier",
   2297  1.1  christos                              "keyid, issuer", !self_sign))
   2298  1.1  christos             goto end;
   2299  1.1  christos     }
   2300  1.1  christos 
   2301  1.1  christos     if (mctx != NULL && do_sign_init(mctx, pkey, md, sigopts) > 0)
   2302  1.1  christos         rv = (X509_sign_ctx(cert, mctx) > 0);
   2303  1.1  christos  end:
   2304  1.1  christos     EVP_MD_CTX_free(mctx);
   2305  1.1  christos     return rv;
   2306  1.1  christos }
   2307  1.1  christos 
   2308  1.1  christos /* Sign the certificate request info */
   2309  1.1  christos int do_X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const char *md,
   2310  1.1  christos                      STACK_OF(OPENSSL_STRING) *sigopts)
   2311  1.1  christos {
   2312  1.1  christos     int rv = 0;
   2313  1.1  christos     EVP_MD_CTX *mctx = EVP_MD_CTX_new();
   2314  1.1  christos 
   2315  1.1  christos     if (do_sign_init(mctx, pkey, md, sigopts) > 0)
   2316  1.1  christos         rv = (X509_REQ_sign_ctx(x, mctx) > 0);
   2317  1.1  christos     EVP_MD_CTX_free(mctx);
   2318  1.1  christos     return rv;
   2319  1.1  christos }
   2320  1.1  christos 
   2321  1.1  christos /* Sign the CRL info */
   2322  1.1  christos int do_X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const char *md,
   2323  1.1  christos                      STACK_OF(OPENSSL_STRING) *sigopts)
   2324  1.1  christos {
   2325  1.1  christos     int rv = 0;
   2326  1.1  christos     EVP_MD_CTX *mctx = EVP_MD_CTX_new();
   2327  1.1  christos 
   2328  1.1  christos     if (do_sign_init(mctx, pkey, md, sigopts) > 0)
   2329  1.1  christos         rv = (X509_CRL_sign_ctx(x, mctx) > 0);
   2330  1.1  christos     EVP_MD_CTX_free(mctx);
   2331  1.1  christos     return rv;
   2332  1.1  christos }
   2333  1.1  christos 
   2334  1.1  christos /*
   2335  1.1  christos  * do_X509_verify returns 1 if the signature is valid,
   2336  1.1  christos  * 0 if the signature check fails, or -1 if error occurs.
   2337  1.1  christos  */
   2338  1.1  christos int do_X509_verify(X509 *x, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *vfyopts)
   2339  1.1  christos {
   2340  1.1  christos     int rv = 0;
   2341  1.1  christos 
   2342  1.1  christos     if (do_x509_init(x, vfyopts) > 0)
   2343  1.1  christos         rv = X509_verify(x, pkey);
   2344  1.1  christos     else
   2345  1.1  christos         rv = -1;
   2346  1.1  christos     return rv;
   2347  1.1  christos }
   2348  1.1  christos 
   2349  1.1  christos /*
   2350  1.1  christos  * do_X509_REQ_verify returns 1 if the signature is valid,
   2351  1.1  christos  * 0 if the signature check fails, or -1 if error occurs.
   2352  1.1  christos  */
   2353  1.1  christos int do_X509_REQ_verify(X509_REQ *x, EVP_PKEY *pkey,
   2354  1.1  christos                        STACK_OF(OPENSSL_STRING) *vfyopts)
   2355  1.1  christos {
   2356  1.1  christos     int rv = 0;
   2357  1.1  christos 
   2358  1.1  christos     if (do_x509_req_init(x, vfyopts) > 0)
   2359  1.1  christos         rv = X509_REQ_verify_ex(x, pkey,
   2360  1.1  christos                                  app_get0_libctx(), app_get0_propq());
   2361  1.1  christos     else
   2362  1.1  christos         rv = -1;
   2363  1.1  christos     return rv;
   2364  1.1  christos }
   2365  1.1  christos 
   2366  1.1  christos /* Get first http URL from a DIST_POINT structure */
   2367  1.1  christos 
   2368  1.1  christos static const char *get_dp_url(DIST_POINT *dp)
   2369  1.1  christos {
   2370  1.1  christos     GENERAL_NAMES *gens;
   2371  1.1  christos     GENERAL_NAME *gen;
   2372  1.1  christos     int i, gtype;
   2373  1.1  christos     ASN1_STRING *uri;
   2374  1.1  christos     if (!dp->distpoint || dp->distpoint->type != 0)
   2375  1.1  christos         return NULL;
   2376  1.1  christos     gens = dp->distpoint->name.fullname;
   2377  1.1  christos     for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
   2378  1.1  christos         gen = sk_GENERAL_NAME_value(gens, i);
   2379  1.1  christos         uri = GENERAL_NAME_get0_value(gen, &gtype);
   2380  1.1  christos         if (gtype == GEN_URI && ASN1_STRING_length(uri) > 6) {
   2381  1.1  christos             const char *uptr = (const char *)ASN1_STRING_get0_data(uri);
   2382  1.1  christos 
   2383  1.1  christos             if (IS_HTTP(uptr)) /* can/should not use HTTPS here */
   2384  1.1  christos                 return uptr;
   2385  1.1  christos         }
   2386  1.1  christos     }
   2387  1.1  christos     return NULL;
   2388  1.1  christos }
   2389  1.1  christos 
   2390  1.1  christos /*
   2391  1.1  christos  * Look through a CRLDP structure and attempt to find an http URL to
   2392  1.1  christos  * downloads a CRL from.
   2393  1.1  christos  */
   2394  1.1  christos 
   2395  1.1  christos static X509_CRL *load_crl_crldp(STACK_OF(DIST_POINT) *crldp)
   2396  1.1  christos {
   2397  1.1  christos     int i;
   2398  1.1  christos     const char *urlptr = NULL;
   2399  1.1  christos     for (i = 0; i < sk_DIST_POINT_num(crldp); i++) {
   2400  1.1  christos         DIST_POINT *dp = sk_DIST_POINT_value(crldp, i);
   2401  1.1  christos         urlptr = get_dp_url(dp);
   2402  1.1  christos         if (urlptr != NULL)
   2403  1.1  christos             return load_crl(urlptr, FORMAT_UNDEF, 0, "CRL via CDP");
   2404  1.1  christos     }
   2405  1.1  christos     return NULL;
   2406  1.1  christos }
   2407  1.1  christos 
   2408  1.1  christos /*
   2409  1.1  christos  * Example of downloading CRLs from CRLDP:
   2410  1.1  christos  * not usable for real world as it always downloads and doesn't cache anything.
   2411  1.1  christos  */
   2412  1.1  christos 
   2413  1.1  christos static STACK_OF(X509_CRL) *crls_http_cb(const X509_STORE_CTX *ctx,
   2414  1.1  christos                                         const X509_NAME *nm)
   2415  1.1  christos {
   2416  1.1  christos     X509 *x;
   2417  1.1  christos     STACK_OF(X509_CRL) *crls = NULL;
   2418  1.1  christos     X509_CRL *crl;
   2419  1.1  christos     STACK_OF(DIST_POINT) *crldp;
   2420  1.1  christos 
   2421  1.1  christos     crls = sk_X509_CRL_new_null();
   2422  1.1  christos     if (!crls)
   2423  1.1  christos         return NULL;
   2424  1.1  christos     x = X509_STORE_CTX_get_current_cert(ctx);
   2425  1.1  christos     crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL);
   2426  1.1  christos     crl = load_crl_crldp(crldp);
   2427  1.1  christos     sk_DIST_POINT_pop_free(crldp, DIST_POINT_free);
   2428  1.1  christos     if (!crl) {
   2429  1.1  christos         sk_X509_CRL_free(crls);
   2430  1.1  christos         return NULL;
   2431  1.1  christos     }
   2432  1.1  christos     sk_X509_CRL_push(crls, crl);
   2433  1.1  christos     /* Try to download delta CRL */
   2434  1.1  christos     crldp = X509_get_ext_d2i(x, NID_freshest_crl, NULL, NULL);
   2435  1.1  christos     crl = load_crl_crldp(crldp);
   2436  1.1  christos     sk_DIST_POINT_pop_free(crldp, DIST_POINT_free);
   2437  1.1  christos     if (crl)
   2438  1.1  christos         sk_X509_CRL_push(crls, crl);
   2439  1.1  christos     return crls;
   2440  1.1  christos }
   2441  1.1  christos 
   2442  1.1  christos void store_setup_crl_download(X509_STORE *st)
   2443  1.1  christos {
   2444  1.1  christos     X509_STORE_set_lookup_crls_cb(st, crls_http_cb);
   2445  1.1  christos }
   2446  1.1  christos 
   2447  1.1  christos #ifndef OPENSSL_NO_SOCK
   2448  1.1  christos static const char *tls_error_hint(void)
   2449  1.1  christos {
   2450  1.1  christos     unsigned long err = ERR_peek_error();
   2451  1.1  christos 
   2452  1.1  christos     if (ERR_GET_LIB(err) != ERR_LIB_SSL)
   2453  1.1  christos         err = ERR_peek_last_error();
   2454  1.1  christos     if (ERR_GET_LIB(err) != ERR_LIB_SSL)
   2455  1.1  christos         return NULL;
   2456  1.1  christos 
   2457  1.1  christos     switch (ERR_GET_REASON(err)) {
   2458  1.1  christos     case SSL_R_WRONG_VERSION_NUMBER:
   2459  1.1  christos         return "The server does not support (a suitable version of) TLS";
   2460  1.1  christos     case SSL_R_UNKNOWN_PROTOCOL:
   2461  1.1  christos         return "The server does not support HTTPS";
   2462  1.1  christos     case SSL_R_CERTIFICATE_VERIFY_FAILED:
   2463  1.1  christos         return "Cannot authenticate server via its TLS certificate, likely due to mismatch with our trusted TLS certs or missing revocation status";
   2464  1.1  christos     case SSL_AD_REASON_OFFSET + TLS1_AD_UNKNOWN_CA:
   2465  1.1  christos         return "Server did not accept our TLS certificate, likely due to mismatch with server's trust anchor or missing revocation status";
   2466  1.1  christos     case SSL_AD_REASON_OFFSET + SSL3_AD_HANDSHAKE_FAILURE:
   2467  1.1  christos         return "TLS handshake failure. Possibly the server requires our TLS certificate but did not receive it";
   2468  1.1  christos     default: /* no error or no hint available for error */
   2469  1.1  christos         return NULL;
   2470  1.1  christos     }
   2471  1.1  christos }
   2472  1.1  christos 
   2473  1.1  christos /* HTTP callback function that supports TLS connection also via HTTPS proxy */
   2474  1.1  christos BIO *app_http_tls_cb(BIO *bio, void *arg, int connect, int detail)
   2475  1.1  christos {
   2476  1.1  christos     APP_HTTP_TLS_INFO *info = (APP_HTTP_TLS_INFO *)arg;
   2477  1.1  christos     SSL_CTX *ssl_ctx = info->ssl_ctx;
   2478  1.1  christos 
   2479  1.1  christos     if (ssl_ctx == NULL) /* not using TLS */
   2480  1.1  christos         return bio;
   2481  1.1  christos     if (connect) {
   2482  1.1  christos         SSL *ssl;
   2483  1.1  christos         BIO *sbio = NULL;
   2484  1.4  christos         X509_STORE *ts = SSL_CTX_get_cert_store(ssl_ctx);
   2485  1.4  christos         X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
   2486  1.4  christos         const char *host = vpm == NULL ? NULL :
   2487  1.4  christos             X509_VERIFY_PARAM_get0_host(vpm, 0 /* first hostname */);
   2488  1.1  christos 
   2489  1.1  christos         /* adapt after fixing callback design flaw, see #17088 */
   2490  1.1  christos         if ((info->use_proxy
   2491  1.1  christos              && !OSSL_HTTP_proxy_connect(bio, info->server, info->port,
   2492  1.1  christos                                          NULL, NULL, /* no proxy credentials */
   2493  1.1  christos                                          info->timeout, bio_err, opt_getprog()))
   2494  1.1  christos                 || (sbio = BIO_new(BIO_f_ssl())) == NULL) {
   2495  1.1  christos             return NULL;
   2496  1.1  christos         }
   2497  1.1  christos         if (ssl_ctx == NULL || (ssl = SSL_new(ssl_ctx)) == NULL) {
   2498  1.1  christos             BIO_free(sbio);
   2499  1.1  christos             return NULL;
   2500  1.1  christos         }
   2501  1.1  christos 
   2502  1.4  christos         if (vpm != NULL)
   2503  1.4  christos             SSL_set_tlsext_host_name(ssl, host /* may be NULL */);
   2504  1.1  christos 
   2505  1.1  christos         SSL_set_connect_state(ssl);
   2506  1.1  christos         BIO_set_ssl(sbio, ssl, BIO_CLOSE);
   2507  1.1  christos 
   2508  1.1  christos         bio = BIO_push(sbio, bio);
   2509  1.1  christos     }
   2510  1.1  christos     if (!connect) {
   2511  1.1  christos         const char *hint;
   2512  1.1  christos         BIO *cbio;
   2513  1.1  christos 
   2514  1.1  christos         if (!detail) { /* disconnecting after error */
   2515  1.1  christos             hint = tls_error_hint();
   2516  1.1  christos             if (hint != NULL)
   2517  1.1  christos                 ERR_add_error_data(2, " : ", hint);
   2518  1.1  christos         }
   2519  1.1  christos         if (ssl_ctx != NULL) {
   2520  1.1  christos             (void)ERR_set_mark();
   2521  1.1  christos             BIO_ssl_shutdown(bio);
   2522  1.1  christos             cbio = BIO_pop(bio); /* connect+HTTP BIO */
   2523  1.1  christos             BIO_free(bio); /* SSL BIO */
   2524  1.1  christos             (void)ERR_pop_to_mark(); /* hide SSL_R_READ_BIO_NOT_SET etc. */
   2525  1.1  christos             bio = cbio;
   2526  1.1  christos         }
   2527  1.1  christos     }
   2528  1.1  christos     return bio;
   2529  1.1  christos }
   2530  1.1  christos 
   2531  1.1  christos void APP_HTTP_TLS_INFO_free(APP_HTTP_TLS_INFO *info)
   2532  1.1  christos {
   2533  1.1  christos     if (info != NULL) {
   2534  1.1  christos         SSL_CTX_free(info->ssl_ctx);
   2535  1.1  christos         OPENSSL_free(info);
   2536  1.1  christos     }
   2537  1.1  christos }
   2538  1.1  christos 
   2539  1.1  christos ASN1_VALUE *app_http_get_asn1(const char *url, const char *proxy,
   2540  1.1  christos                               const char *no_proxy, SSL_CTX *ssl_ctx,
   2541  1.1  christos                               const STACK_OF(CONF_VALUE) *headers,
   2542  1.1  christos                               long timeout, const char *expected_content_type,
   2543  1.1  christos                               const ASN1_ITEM *it)
   2544  1.1  christos {
   2545  1.1  christos     APP_HTTP_TLS_INFO info;
   2546  1.1  christos     char *server;
   2547  1.1  christos     char *port;
   2548  1.1  christos     int use_ssl;
   2549  1.1  christos     BIO *mem;
   2550  1.1  christos     ASN1_VALUE *resp = NULL;
   2551  1.1  christos 
   2552  1.1  christos     if (url == NULL || it == NULL) {
   2553  1.1  christos         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
   2554  1.1  christos         return NULL;
   2555  1.1  christos     }
   2556  1.1  christos 
   2557  1.1  christos     if (!OSSL_HTTP_parse_url(url, &use_ssl, NULL /* userinfo */, &server, &port,
   2558  1.1  christos                              NULL /* port_num, */, NULL, NULL, NULL))
   2559  1.1  christos         return NULL;
   2560  1.1  christos     if (use_ssl && ssl_ctx == NULL) {
   2561  1.1  christos         ERR_raise_data(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER,
   2562  1.1  christos                        "missing SSL_CTX");
   2563  1.1  christos         goto end;
   2564  1.1  christos     }
   2565  1.1  christos     if (!use_ssl && ssl_ctx != NULL) {
   2566  1.1  christos         ERR_raise_data(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT,
   2567  1.1  christos                        "SSL_CTX given but use_ssl == 0");
   2568  1.1  christos         goto end;
   2569  1.1  christos     }
   2570  1.1  christos 
   2571  1.1  christos     info.server = server;
   2572  1.1  christos     info.port = port;
   2573  1.1  christos     info.use_proxy = /* workaround for callback design flaw, see #17088 */
   2574  1.1  christos         OSSL_HTTP_adapt_proxy(proxy, no_proxy, server, use_ssl) != NULL;
   2575  1.1  christos     info.timeout = timeout;
   2576  1.1  christos     info.ssl_ctx = ssl_ctx;
   2577  1.1  christos     mem = OSSL_HTTP_get(url, proxy, no_proxy, NULL /* bio */, NULL /* rbio */,
   2578  1.1  christos                         app_http_tls_cb, &info, 0 /* buf_size */, headers,
   2579  1.1  christos                         expected_content_type, 1 /* expect_asn1 */,
   2580  1.1  christos                         OSSL_HTTP_DEFAULT_MAX_RESP_LEN, timeout);
   2581  1.1  christos     resp = ASN1_item_d2i_bio(it, mem, NULL);
   2582  1.1  christos     BIO_free(mem);
   2583  1.1  christos 
   2584  1.1  christos  end:
   2585  1.1  christos     OPENSSL_free(server);
   2586  1.1  christos     OPENSSL_free(port);
   2587  1.1  christos     return resp;
   2588  1.1  christos 
   2589  1.1  christos }
   2590  1.1  christos 
   2591  1.1  christos ASN1_VALUE *app_http_post_asn1(const char *host, const char *port,
   2592  1.1  christos                                const char *path, const char *proxy,
   2593  1.1  christos                                const char *no_proxy, SSL_CTX *ssl_ctx,
   2594  1.1  christos                                const STACK_OF(CONF_VALUE) *headers,
   2595  1.1  christos                                const char *content_type,
   2596  1.1  christos                                ASN1_VALUE *req, const ASN1_ITEM *req_it,
   2597  1.1  christos                                const char *expected_content_type,
   2598  1.1  christos                                long timeout, const ASN1_ITEM *rsp_it)
   2599  1.1  christos {
   2600  1.1  christos     int use_ssl = ssl_ctx != NULL;
   2601  1.1  christos     APP_HTTP_TLS_INFO info;
   2602  1.1  christos     BIO *rsp, *req_mem = ASN1_item_i2d_mem_bio(req_it, req);
   2603  1.1  christos     ASN1_VALUE *res;
   2604  1.1  christos 
   2605  1.1  christos     if (req_mem == NULL)
   2606  1.1  christos         return NULL;
   2607  1.1  christos 
   2608  1.1  christos     info.server = host;
   2609  1.1  christos     info.port = port;
   2610  1.1  christos     info.use_proxy = /* workaround for callback design flaw, see #17088 */
   2611  1.1  christos         OSSL_HTTP_adapt_proxy(proxy, no_proxy, host, use_ssl) != NULL;
   2612  1.1  christos     info.timeout = timeout;
   2613  1.1  christos     info.ssl_ctx = ssl_ctx;
   2614  1.1  christos     rsp = OSSL_HTTP_transfer(NULL, host, port, path, use_ssl,
   2615  1.1  christos                              proxy, no_proxy, NULL /* bio */, NULL /* rbio */,
   2616  1.1  christos                              app_http_tls_cb, &info,
   2617  1.1  christos                              0 /* buf_size */, headers, content_type, req_mem,
   2618  1.1  christos                              expected_content_type, 1 /* expect_asn1 */,
   2619  1.1  christos                              OSSL_HTTP_DEFAULT_MAX_RESP_LEN, timeout,
   2620  1.1  christos                              0 /* keep_alive */);
   2621  1.1  christos     BIO_free(req_mem);
   2622  1.1  christos     res = ASN1_item_d2i_bio(rsp_it, rsp, NULL);
   2623  1.1  christos     BIO_free(rsp);
   2624  1.1  christos     return res;
   2625  1.1  christos }
   2626  1.1  christos 
   2627  1.1  christos #endif
   2628  1.1  christos 
   2629  1.1  christos /*
   2630  1.1  christos  * Platform-specific sections
   2631  1.1  christos  */
   2632  1.1  christos #if defined(_WIN32)
   2633  1.1  christos # ifdef fileno
   2634  1.1  christos #  undef fileno
   2635  1.1  christos #  define fileno(a) (int)_fileno(a)
   2636  1.1  christos # endif
   2637  1.1  christos 
   2638  1.1  christos # include <windows.h>
   2639  1.1  christos # include <tchar.h>
   2640  1.1  christos 
   2641  1.1  christos static int WIN32_rename(const char *from, const char *to)
   2642  1.1  christos {
   2643  1.1  christos     TCHAR *tfrom = NULL, *tto;
   2644  1.1  christos     DWORD err;
   2645  1.1  christos     int ret = 0;
   2646  1.1  christos 
   2647  1.1  christos     if (sizeof(TCHAR) == 1) {
   2648  1.1  christos         tfrom = (TCHAR *)from;
   2649  1.1  christos         tto = (TCHAR *)to;
   2650  1.1  christos     } else {                    /* UNICODE path */
   2651  1.1  christos 
   2652  1.1  christos         size_t i, flen = strlen(from) + 1, tlen = strlen(to) + 1;
   2653  1.1  christos         tfrom = malloc(sizeof(*tfrom) * (flen + tlen));
   2654  1.1  christos         if (tfrom == NULL)
   2655  1.1  christos             goto err;
   2656  1.1  christos         tto = tfrom + flen;
   2657  1.1  christos # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
   2658  1.1  christos         if (!MultiByteToWideChar(CP_ACP, 0, from, flen, (WCHAR *)tfrom, flen))
   2659  1.1  christos # endif
   2660  1.1  christos             for (i = 0; i < flen; i++)
   2661  1.1  christos                 tfrom[i] = (TCHAR)from[i];
   2662  1.1  christos # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
   2663  1.1  christos         if (!MultiByteToWideChar(CP_ACP, 0, to, tlen, (WCHAR *)tto, tlen))
   2664  1.1  christos # endif
   2665  1.1  christos             for (i = 0; i < tlen; i++)
   2666  1.1  christos                 tto[i] = (TCHAR)to[i];
   2667  1.1  christos     }
   2668  1.1  christos 
   2669  1.1  christos     if (MoveFile(tfrom, tto))
   2670  1.1  christos         goto ok;
   2671  1.1  christos     err = GetLastError();
   2672  1.1  christos     if (err == ERROR_ALREADY_EXISTS || err == ERROR_FILE_EXISTS) {
   2673  1.1  christos         if (DeleteFile(tto) && MoveFile(tfrom, tto))
   2674  1.1  christos             goto ok;
   2675  1.1  christos         err = GetLastError();
   2676  1.1  christos     }
   2677  1.1  christos     if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
   2678  1.1  christos         errno = ENOENT;
   2679  1.1  christos     else if (err == ERROR_ACCESS_DENIED)
   2680  1.1  christos         errno = EACCES;
   2681  1.1  christos     else
   2682  1.1  christos         errno = EINVAL;         /* we could map more codes... */
   2683  1.1  christos  err:
   2684  1.1  christos     ret = -1;
   2685  1.1  christos  ok:
   2686  1.1  christos     if (tfrom != NULL && tfrom != (TCHAR *)from)
   2687  1.1  christos         free(tfrom);
   2688  1.1  christos     return ret;
   2689  1.1  christos }
   2690  1.1  christos #endif
   2691  1.1  christos 
   2692  1.1  christos /* app_tminterval section */
   2693  1.1  christos #if defined(_WIN32)
   2694  1.1  christos double app_tminterval(int stop, int usertime)
   2695  1.1  christos {
   2696  1.1  christos     FILETIME now;
   2697  1.1  christos     double ret = 0;
   2698  1.1  christos     static ULARGE_INTEGER tmstart;
   2699  1.1  christos     static int warning = 1;
   2700  1.1  christos # ifdef _WIN32_WINNT
   2701  1.1  christos     static HANDLE proc = NULL;
   2702  1.1  christos 
   2703  1.1  christos     if (proc == NULL) {
   2704  1.1  christos         if (check_winnt())
   2705  1.1  christos             proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
   2706  1.1  christos                                GetCurrentProcessId());
   2707  1.1  christos         if (proc == NULL)
   2708  1.1  christos             proc = (HANDLE) - 1;
   2709  1.1  christos     }
   2710  1.1  christos 
   2711  1.1  christos     if (usertime && proc != (HANDLE) - 1) {
   2712  1.1  christos         FILETIME junk;
   2713  1.1  christos         GetProcessTimes(proc, &junk, &junk, &junk, &now);
   2714  1.1  christos     } else
   2715  1.1  christos # endif
   2716  1.1  christos     {
   2717  1.1  christos         SYSTEMTIME systime;
   2718  1.1  christos 
   2719  1.1  christos         if (usertime && warning) {
   2720  1.1  christos             BIO_printf(bio_err, "To get meaningful results, run "
   2721  1.1  christos                        "this program on idle system.\n");
   2722  1.1  christos             warning = 0;
   2723  1.1  christos         }
   2724  1.1  christos         GetSystemTime(&systime);
   2725  1.1  christos         SystemTimeToFileTime(&systime, &now);
   2726  1.1  christos     }
   2727  1.1  christos 
   2728  1.1  christos     if (stop == TM_START) {
   2729  1.1  christos         tmstart.u.LowPart = now.dwLowDateTime;
   2730  1.1  christos         tmstart.u.HighPart = now.dwHighDateTime;
   2731  1.1  christos     } else {
   2732  1.1  christos         ULARGE_INTEGER tmstop;
   2733  1.1  christos 
   2734  1.1  christos         tmstop.u.LowPart = now.dwLowDateTime;
   2735  1.1  christos         tmstop.u.HighPart = now.dwHighDateTime;
   2736  1.1  christos 
   2737  1.1  christos         ret = (__int64)(tmstop.QuadPart - tmstart.QuadPart) * 1e-7;
   2738  1.1  christos     }
   2739  1.1  christos 
   2740  1.1  christos     return ret;
   2741  1.1  christos }
   2742  1.1  christos #elif defined(OPENSSL_SYS_VXWORKS)
   2743  1.1  christos # include <time.h>
   2744  1.1  christos 
   2745  1.1  christos double app_tminterval(int stop, int usertime)
   2746  1.1  christos {
   2747  1.1  christos     double ret = 0;
   2748  1.1  christos # ifdef CLOCK_REALTIME
   2749  1.1  christos     static struct timespec tmstart;
   2750  1.1  christos     struct timespec now;
   2751  1.1  christos # else
   2752  1.1  christos     static unsigned long tmstart;
   2753  1.1  christos     unsigned long now;
   2754  1.1  christos # endif
   2755  1.1  christos     static int warning = 1;
   2756  1.1  christos 
   2757  1.1  christos     if (usertime && warning) {
   2758  1.1  christos         BIO_printf(bio_err, "To get meaningful results, run "
   2759  1.1  christos                    "this program on idle system.\n");
   2760  1.1  christos         warning = 0;
   2761  1.1  christos     }
   2762  1.1  christos # ifdef CLOCK_REALTIME
   2763  1.1  christos     clock_gettime(CLOCK_REALTIME, &now);
   2764  1.1  christos     if (stop == TM_START)
   2765  1.1  christos         tmstart = now;
   2766  1.1  christos     else
   2767  1.1  christos         ret = ((now.tv_sec + now.tv_nsec * 1e-9)
   2768  1.1  christos                - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9));
   2769  1.1  christos # else
   2770  1.1  christos     now = tickGet();
   2771  1.1  christos     if (stop == TM_START)
   2772  1.1  christos         tmstart = now;
   2773  1.1  christos     else
   2774  1.1  christos         ret = (now - tmstart) / (double)sysClkRateGet();
   2775  1.1  christos # endif
   2776  1.1  christos     return ret;
   2777  1.1  christos }
   2778  1.1  christos 
   2779  1.1  christos #elif defined(_SC_CLK_TCK)      /* by means of unistd.h */
   2780  1.1  christos # include <sys/times.h>
   2781  1.1  christos 
   2782  1.1  christos double app_tminterval(int stop, int usertime)
   2783  1.1  christos {
   2784  1.1  christos     double ret = 0;
   2785  1.1  christos     struct tms rus;
   2786  1.1  christos     clock_t now = times(&rus);
   2787  1.1  christos     static clock_t tmstart;
   2788  1.1  christos 
   2789  1.1  christos     if (usertime)
   2790  1.1  christos         now = rus.tms_utime;
   2791  1.1  christos 
   2792  1.1  christos     if (stop == TM_START) {
   2793  1.1  christos         tmstart = now;
   2794  1.1  christos     } else {
   2795  1.1  christos         long int tck = sysconf(_SC_CLK_TCK);
   2796  1.1  christos         ret = (now - tmstart) / (double)tck;
   2797  1.1  christos     }
   2798  1.1  christos 
   2799  1.1  christos     return ret;
   2800  1.1  christos }
   2801  1.1  christos 
   2802  1.1  christos #else
   2803  1.1  christos # include <sys/time.h>
   2804  1.1  christos # include <sys/resource.h>
   2805  1.1  christos 
   2806  1.1  christos double app_tminterval(int stop, int usertime)
   2807  1.1  christos {
   2808  1.1  christos     double ret = 0;
   2809  1.1  christos     struct rusage rus;
   2810  1.1  christos     struct timeval now;
   2811  1.1  christos     static struct timeval tmstart;
   2812  1.1  christos 
   2813  1.1  christos     if (usertime)
   2814  1.1  christos         getrusage(RUSAGE_SELF, &rus), now = rus.ru_utime;
   2815  1.1  christos     else
   2816  1.1  christos         gettimeofday(&now, NULL);
   2817  1.1  christos 
   2818  1.1  christos     if (stop == TM_START)
   2819  1.1  christos         tmstart = now;
   2820  1.1  christos     else
   2821  1.1  christos         ret = ((now.tv_sec + now.tv_usec * 1e-6)
   2822  1.1  christos                - (tmstart.tv_sec + tmstart.tv_usec * 1e-6));
   2823  1.1  christos 
   2824  1.1  christos     return ret;
   2825  1.1  christos }
   2826  1.1  christos #endif
   2827  1.1  christos 
   2828  1.1  christos int app_access(const char* name, int flag)
   2829  1.1  christos {
   2830  1.1  christos #ifdef _WIN32
   2831  1.1  christos     return _access(name, flag);
   2832  1.1  christos #else
   2833  1.1  christos     return access(name, flag);
   2834  1.1  christos #endif
   2835  1.1  christos }
   2836  1.1  christos 
   2837  1.1  christos int app_isdir(const char *name)
   2838  1.1  christos {
   2839  1.1  christos     return opt_isdir(name);
   2840  1.1  christos }
   2841  1.1  christos 
   2842  1.1  christos /* raw_read|write section */
   2843  1.1  christos #if defined(__VMS)
   2844  1.1  christos # include "vms_term_sock.h"
   2845  1.1  christos static int stdin_sock = -1;
   2846  1.1  christos 
   2847  1.1  christos static void close_stdin_sock(void)
   2848  1.1  christos {
   2849  1.1  christos     TerminalSocket (TERM_SOCK_DELETE, &stdin_sock);
   2850  1.1  christos }
   2851  1.1  christos 
   2852  1.1  christos int fileno_stdin(void)
   2853  1.1  christos {
   2854  1.1  christos     if (stdin_sock == -1) {
   2855  1.1  christos         TerminalSocket(TERM_SOCK_CREATE, &stdin_sock);
   2856  1.1  christos         atexit(close_stdin_sock);
   2857  1.1  christos     }
   2858  1.1  christos 
   2859  1.1  christos     return stdin_sock;
   2860  1.1  christos }
   2861  1.1  christos #else
   2862  1.1  christos int fileno_stdin(void)
   2863  1.1  christos {
   2864  1.1  christos     return fileno(stdin);
   2865  1.1  christos }
   2866  1.1  christos #endif
   2867  1.1  christos 
   2868  1.1  christos int fileno_stdout(void)
   2869  1.1  christos {
   2870  1.1  christos     return fileno(stdout);
   2871  1.1  christos }
   2872  1.1  christos 
   2873  1.1  christos #if defined(_WIN32) && defined(STD_INPUT_HANDLE)
   2874  1.1  christos int raw_read_stdin(void *buf, int siz)
   2875  1.1  christos {
   2876  1.1  christos     DWORD n;
   2877  1.1  christos     if (ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, siz, &n, NULL))
   2878  1.1  christos         return n;
   2879  1.1  christos     else
   2880  1.1  christos         return -1;
   2881  1.1  christos }
   2882  1.1  christos #elif defined(__VMS)
   2883  1.1  christos # include <sys/socket.h>
   2884  1.1  christos 
   2885  1.1  christos int raw_read_stdin(void *buf, int siz)
   2886  1.1  christos {
   2887  1.1  christos     return recv(fileno_stdin(), buf, siz, 0);
   2888  1.1  christos }
   2889  1.1  christos #else
   2890  1.1  christos # if defined(__TANDEM)
   2891  1.1  christos #  if defined(OPENSSL_TANDEM_FLOSS)
   2892  1.1  christos #   include <floss.h(floss_read)>
   2893  1.1  christos #  endif
   2894  1.1  christos # endif
   2895  1.1  christos int raw_read_stdin(void *buf, int siz)
   2896  1.1  christos {
   2897  1.1  christos     return read(fileno_stdin(), buf, siz);
   2898  1.1  christos }
   2899  1.1  christos #endif
   2900  1.1  christos 
   2901  1.1  christos #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE)
   2902  1.1  christos int raw_write_stdout(const void *buf, int siz)
   2903  1.1  christos {
   2904  1.1  christos     DWORD n;
   2905  1.1  christos     if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, siz, &n, NULL))
   2906  1.1  christos         return n;
   2907  1.1  christos     else
   2908  1.1  christos         return -1;
   2909  1.1  christos }
   2910  1.1  christos #elif defined(OPENSSL_SYS_TANDEM) && defined(OPENSSL_THREADS) && defined(_SPT_MODEL_)
   2911  1.1  christos # if defined(__TANDEM)
   2912  1.1  christos #  if defined(OPENSSL_TANDEM_FLOSS)
   2913  1.1  christos #   include <floss.h(floss_write)>
   2914  1.1  christos #  endif
   2915  1.1  christos # endif
   2916  1.1  christos int raw_write_stdout(const void *buf,int siz)
   2917  1.1  christos {
   2918  1.1  christos 	return write(fileno(stdout),(void*)buf,siz);
   2919  1.1  christos }
   2920  1.1  christos #else
   2921  1.1  christos # if defined(__TANDEM)
   2922  1.1  christos #  if defined(OPENSSL_TANDEM_FLOSS)
   2923  1.1  christos #   include <floss.h(floss_write)>
   2924  1.1  christos #  endif
   2925  1.1  christos # endif
   2926  1.1  christos int raw_write_stdout(const void *buf, int siz)
   2927  1.1  christos {
   2928  1.1  christos     return write(fileno_stdout(), buf, siz);
   2929  1.1  christos }
   2930  1.1  christos #endif
   2931  1.1  christos 
   2932  1.1  christos /*
   2933  1.1  christos  * Centralized handling of input and output files with format specification
   2934  1.1  christos  * The format is meant to show what the input and output is supposed to be,
   2935  1.1  christos  * and is therefore a show of intent more than anything else.  However, it
   2936  1.1  christos  * does impact behavior on some platforms, such as differentiating between
   2937  1.1  christos  * text and binary input/output on non-Unix platforms
   2938  1.1  christos  */
   2939  1.1  christos BIO *dup_bio_in(int format)
   2940  1.1  christos {
   2941  1.1  christos     return BIO_new_fp(stdin,
   2942  1.1  christos                       BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0));
   2943  1.1  christos }
   2944  1.1  christos 
   2945  1.1  christos BIO *dup_bio_out(int format)
   2946  1.1  christos {
   2947  1.1  christos     BIO *b = BIO_new_fp(stdout,
   2948  1.1  christos                         BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0));
   2949  1.1  christos     void *prefix = NULL;
   2950  1.1  christos 
   2951  1.1  christos     if (b == NULL)
   2952  1.1  christos         return NULL;
   2953  1.1  christos 
   2954  1.1  christos #ifdef OPENSSL_SYS_VMS
   2955  1.1  christos     if (FMT_istext(format))
   2956  1.1  christos         b = BIO_push(BIO_new(BIO_f_linebuffer()), b);
   2957  1.1  christos #endif
   2958  1.1  christos 
   2959  1.1  christos     if (FMT_istext(format)
   2960  1.1  christos         && (prefix = getenv("HARNESS_OSSL_PREFIX")) != NULL) {
   2961  1.1  christos         b = BIO_push(BIO_new(BIO_f_prefix()), b);
   2962  1.1  christos         BIO_set_prefix(b, prefix);
   2963  1.1  christos     }
   2964  1.1  christos 
   2965  1.1  christos     return b;
   2966  1.1  christos }
   2967  1.1  christos 
   2968  1.1  christos BIO *dup_bio_err(int format)
   2969  1.1  christos {
   2970  1.1  christos     BIO *b = BIO_new_fp(stderr,
   2971  1.1  christos                         BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0));
   2972  1.1  christos #ifdef OPENSSL_SYS_VMS
   2973  1.1  christos     if (b != NULL && FMT_istext(format))
   2974  1.1  christos         b = BIO_push(BIO_new(BIO_f_linebuffer()), b);
   2975  1.1  christos #endif
   2976  1.1  christos     return b;
   2977  1.1  christos }
   2978  1.1  christos 
   2979  1.1  christos void unbuffer(FILE *fp)
   2980  1.1  christos {
   2981  1.1  christos /*
   2982  1.1  christos  * On VMS, setbuf() will only take 32-bit pointers, and a compilation
   2983  1.1  christos  * with /POINTER_SIZE=64 will give off a MAYLOSEDATA2 warning here.
   2984  1.1  christos  * However, we trust that the C RTL will never give us a FILE pointer
   2985  1.1  christos  * above the first 4 GB of memory, so we simply turn off the warning
   2986  1.1  christos  * temporarily.
   2987  1.1  christos  */
   2988  1.1  christos #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
   2989  1.1  christos # pragma environment save
   2990  1.1  christos # pragma message disable maylosedata2
   2991  1.1  christos #endif
   2992  1.1  christos     setbuf(fp, NULL);
   2993  1.1  christos #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
   2994  1.1  christos # pragma environment restore
   2995  1.1  christos #endif
   2996  1.1  christos }
   2997  1.1  christos 
   2998  1.1  christos static const char *modestr(char mode, int format)
   2999  1.1  christos {
   3000  1.1  christos     OPENSSL_assert(mode == 'a' || mode == 'r' || mode == 'w');
   3001  1.1  christos 
   3002  1.1  christos     switch (mode) {
   3003  1.1  christos     case 'a':
   3004  1.1  christos         return FMT_istext(format) ? "a" : "ab";
   3005  1.1  christos     case 'r':
   3006  1.1  christos         return FMT_istext(format) ? "r" : "rb";
   3007  1.1  christos     case 'w':
   3008  1.1  christos         return FMT_istext(format) ? "w" : "wb";
   3009  1.1  christos     }
   3010  1.1  christos     /* The assert above should make sure we never reach this point */
   3011  1.1  christos     return NULL;
   3012  1.1  christos }
   3013  1.1  christos 
   3014  1.1  christos static const char *modeverb(char mode)
   3015  1.1  christos {
   3016  1.1  christos     switch (mode) {
   3017  1.1  christos     case 'a':
   3018  1.1  christos         return "appending";
   3019  1.1  christos     case 'r':
   3020  1.1  christos         return "reading";
   3021  1.1  christos     case 'w':
   3022  1.1  christos         return "writing";
   3023  1.1  christos     }
   3024  1.1  christos     return "(doing something)";
   3025  1.1  christos }
   3026  1.1  christos 
   3027  1.1  christos /*
   3028  1.1  christos  * Open a file for writing, owner-read-only.
   3029  1.1  christos  */
   3030  1.1  christos BIO *bio_open_owner(const char *filename, int format, int private)
   3031  1.1  christos {
   3032  1.1  christos     FILE *fp = NULL;
   3033  1.1  christos     BIO *b = NULL;
   3034  1.1  christos     int textmode, bflags;
   3035  1.1  christos #ifndef OPENSSL_NO_POSIX_IO
   3036  1.1  christos     int fd = -1, mode;
   3037  1.1  christos #endif
   3038  1.1  christos 
   3039  1.1  christos     if (!private || filename == NULL || strcmp(filename, "-") == 0)
   3040  1.1  christos         return bio_open_default(filename, 'w', format);
   3041  1.1  christos 
   3042  1.1  christos     textmode = FMT_istext(format);
   3043  1.1  christos #ifndef OPENSSL_NO_POSIX_IO
   3044  1.1  christos     mode = O_WRONLY;
   3045  1.1  christos # ifdef O_CREAT
   3046  1.1  christos     mode |= O_CREAT;
   3047  1.1  christos # endif
   3048  1.1  christos # ifdef O_TRUNC
   3049  1.1  christos     mode |= O_TRUNC;
   3050  1.1  christos # endif
   3051  1.1  christos     if (!textmode) {
   3052  1.1  christos # ifdef O_BINARY
   3053  1.1  christos         mode |= O_BINARY;
   3054  1.1  christos # elif defined(_O_BINARY)
   3055  1.1  christos         mode |= _O_BINARY;
   3056  1.1  christos # endif
   3057  1.1  christos     }
   3058  1.1  christos 
   3059  1.1  christos # ifdef OPENSSL_SYS_VMS
   3060  1.1  christos     /* VMS doesn't have O_BINARY, it just doesn't make sense.  But,
   3061  1.1  christos      * it still needs to know that we're going binary, or fdopen()
   3062  1.1  christos      * will fail with "invalid argument"...  so we tell VMS what the
   3063  1.1  christos      * context is.
   3064  1.1  christos      */
   3065  1.1  christos     if (!textmode)
   3066  1.1  christos         fd = open(filename, mode, 0600, "ctx=bin");
   3067  1.1  christos     else
   3068  1.1  christos # endif
   3069  1.1  christos         fd = open(filename, mode, 0600);
   3070  1.1  christos     if (fd < 0)
   3071  1.1  christos         goto err;
   3072  1.1  christos     fp = fdopen(fd, modestr('w', format));
   3073  1.1  christos #else   /* OPENSSL_NO_POSIX_IO */
   3074  1.1  christos     /* Have stdio but not Posix IO, do the best we can */
   3075  1.1  christos     fp = fopen(filename, modestr('w', format));
   3076  1.1  christos #endif  /* OPENSSL_NO_POSIX_IO */
   3077  1.1  christos     if (fp == NULL)
   3078  1.1  christos         goto err;
   3079  1.1  christos     bflags = BIO_CLOSE;
   3080  1.1  christos     if (textmode)
   3081  1.1  christos         bflags |= BIO_FP_TEXT;
   3082  1.1  christos     b = BIO_new_fp(fp, bflags);
   3083  1.1  christos     if (b != NULL)
   3084  1.1  christos         return b;
   3085  1.1  christos 
   3086  1.1  christos  err:
   3087  1.1  christos     BIO_printf(bio_err, "%s: Can't open \"%s\" for writing, %s\n",
   3088  1.1  christos                opt_getprog(), filename, strerror(errno));
   3089  1.1  christos     ERR_print_errors(bio_err);
   3090  1.1  christos     /* If we have fp, then fdopen took over fd, so don't close both. */
   3091  1.1  christos     if (fp != NULL)
   3092  1.1  christos         fclose(fp);
   3093  1.1  christos #ifndef OPENSSL_NO_POSIX_IO
   3094  1.1  christos     else if (fd >= 0)
   3095  1.1  christos         close(fd);
   3096  1.1  christos #endif
   3097  1.1  christos     return NULL;
   3098  1.1  christos }
   3099  1.1  christos 
   3100  1.1  christos static BIO *bio_open_default_(const char *filename, char mode, int format,
   3101  1.1  christos                               int quiet)
   3102  1.1  christos {
   3103  1.1  christos     BIO *ret;
   3104  1.1  christos 
   3105  1.1  christos     if (filename == NULL || strcmp(filename, "-") == 0) {
   3106  1.1  christos         ret = mode == 'r' ? dup_bio_in(format) : dup_bio_out(format);
   3107  1.1  christos         if (quiet) {
   3108  1.1  christos             ERR_clear_error();
   3109  1.1  christos             return ret;
   3110  1.1  christos         }
   3111  1.1  christos         if (ret != NULL)
   3112  1.1  christos             return ret;
   3113  1.1  christos         BIO_printf(bio_err,
   3114  1.1  christos                    "Can't open %s, %s\n",
   3115  1.1  christos                    mode == 'r' ? "stdin" : "stdout", strerror(errno));
   3116  1.1  christos     } else {
   3117  1.1  christos         ret = BIO_new_file(filename, modestr(mode, format));
   3118  1.1  christos         if (quiet) {
   3119  1.1  christos             ERR_clear_error();
   3120  1.1  christos             return ret;
   3121  1.1  christos         }
   3122  1.1  christos         if (ret != NULL)
   3123  1.1  christos             return ret;
   3124  1.1  christos         BIO_printf(bio_err,
   3125  1.1  christos                    "Can't open \"%s\" for %s, %s\n",
   3126  1.1  christos                    filename, modeverb(mode), strerror(errno));
   3127  1.1  christos     }
   3128  1.1  christos     ERR_print_errors(bio_err);
   3129  1.1  christos     return NULL;
   3130  1.1  christos }
   3131  1.1  christos 
   3132  1.1  christos BIO *bio_open_default(const char *filename, char mode, int format)
   3133  1.1  christos {
   3134  1.1  christos     return bio_open_default_(filename, mode, format, 0);
   3135  1.1  christos }
   3136  1.1  christos 
   3137  1.1  christos BIO *bio_open_default_quiet(const char *filename, char mode, int format)
   3138  1.1  christos {
   3139  1.1  christos     return bio_open_default_(filename, mode, format, 1);
   3140  1.1  christos }
   3141  1.1  christos 
   3142  1.1  christos void wait_for_async(SSL *s)
   3143  1.1  christos {
   3144  1.1  christos     /* On Windows select only works for sockets, so we simply don't wait  */
   3145  1.1  christos #ifndef OPENSSL_SYS_WINDOWS
   3146  1.1  christos     int width = 0;
   3147  1.1  christos     fd_set asyncfds;
   3148  1.1  christos     OSSL_ASYNC_FD *fds;
   3149  1.1  christos     size_t numfds;
   3150  1.1  christos     size_t i;
   3151  1.1  christos 
   3152  1.1  christos     if (!SSL_get_all_async_fds(s, NULL, &numfds))
   3153  1.1  christos         return;
   3154  1.1  christos     if (numfds == 0)
   3155  1.1  christos         return;
   3156  1.1  christos     fds = app_malloc(sizeof(OSSL_ASYNC_FD) * numfds, "allocate async fds");
   3157  1.1  christos     if (!SSL_get_all_async_fds(s, fds, &numfds)) {
   3158  1.1  christos         OPENSSL_free(fds);
   3159  1.1  christos         return;
   3160  1.1  christos     }
   3161  1.1  christos 
   3162  1.1  christos     FD_ZERO(&asyncfds);
   3163  1.1  christos     for (i = 0; i < numfds; i++) {
   3164  1.1  christos         if (width <= (int)fds[i])
   3165  1.1  christos             width = (int)fds[i] + 1;
   3166  1.1  christos         openssl_fdset((int)fds[i], &asyncfds);
   3167  1.1  christos     }
   3168  1.1  christos     select(width, (void *)&asyncfds, NULL, NULL, NULL);
   3169  1.1  christos     OPENSSL_free(fds);
   3170  1.1  christos #endif
   3171  1.1  christos }
   3172  1.1  christos 
   3173  1.1  christos /* if OPENSSL_SYS_WINDOWS is defined then so is OPENSSL_SYS_MSDOS */
   3174  1.1  christos #if defined(OPENSSL_SYS_MSDOS)
   3175  1.1  christos int has_stdin_waiting(void)
   3176  1.1  christos {
   3177  1.1  christos # if defined(OPENSSL_SYS_WINDOWS)
   3178  1.1  christos     HANDLE inhand = GetStdHandle(STD_INPUT_HANDLE);
   3179  1.1  christos     DWORD events = 0;
   3180  1.1  christos     INPUT_RECORD inputrec;
   3181  1.1  christos     DWORD insize = 1;
   3182  1.1  christos     BOOL peeked;
   3183  1.1  christos 
   3184  1.1  christos     if (inhand == INVALID_HANDLE_VALUE) {
   3185  1.1  christos         return 0;
   3186  1.1  christos     }
   3187  1.1  christos 
   3188  1.1  christos     peeked = PeekConsoleInput(inhand, &inputrec, insize, &events);
   3189  1.1  christos     if (!peeked) {
   3190  1.1  christos         /* Probably redirected input? _kbhit() does not work in this case */
   3191  1.1  christos         if (!feof(stdin)) {
   3192  1.1  christos             return 1;
   3193  1.1  christos         }
   3194  1.1  christos         return 0;
   3195  1.1  christos     }
   3196  1.1  christos # endif
   3197  1.1  christos     return _kbhit();
   3198  1.1  christos }
   3199  1.1  christos #endif
   3200  1.1  christos 
   3201  1.1  christos /* Corrupt a signature by modifying final byte */
   3202  1.1  christos void corrupt_signature(const ASN1_STRING *signature)
   3203  1.1  christos {
   3204  1.1  christos         unsigned char *s = signature->data;
   3205  1.1  christos         s[signature->length - 1] ^= 0x1;
   3206  1.1  christos }
   3207  1.1  christos 
   3208  1.1  christos int set_cert_times(X509 *x, const char *startdate, const char *enddate,
   3209  1.1  christos                    int days)
   3210  1.1  christos {
   3211  1.1  christos     if (startdate == NULL || strcmp(startdate, "today") == 0) {
   3212  1.1  christos         if (X509_gmtime_adj(X509_getm_notBefore(x), 0) == NULL)
   3213  1.1  christos             return 0;
   3214  1.1  christos     } else {
   3215  1.1  christos         if (!ASN1_TIME_set_string_X509(X509_getm_notBefore(x), startdate))
   3216  1.1  christos             return 0;
   3217  1.1  christos     }
   3218  1.1  christos     if (enddate == NULL) {
   3219  1.1  christos         if (X509_time_adj_ex(X509_getm_notAfter(x), days, 0, NULL)
   3220  1.1  christos             == NULL)
   3221  1.1  christos             return 0;
   3222  1.1  christos     } else if (!ASN1_TIME_set_string_X509(X509_getm_notAfter(x), enddate)) {
   3223  1.1  christos         return 0;
   3224  1.1  christos     }
   3225  1.1  christos     return 1;
   3226  1.1  christos }
   3227  1.1  christos 
   3228  1.1  christos int set_crl_lastupdate(X509_CRL *crl, const char *lastupdate)
   3229  1.1  christos {
   3230  1.1  christos     int ret = 0;
   3231  1.1  christos     ASN1_TIME *tm = ASN1_TIME_new();
   3232  1.1  christos 
   3233  1.1  christos     if (tm == NULL)
   3234  1.1  christos         goto end;
   3235  1.1  christos 
   3236  1.1  christos     if (lastupdate == NULL) {
   3237  1.1  christos         if (X509_gmtime_adj(tm, 0) == NULL)
   3238  1.1  christos             goto end;
   3239  1.1  christos     } else {
   3240  1.1  christos         if (!ASN1_TIME_set_string_X509(tm, lastupdate))
   3241  1.1  christos             goto end;
   3242  1.1  christos     }
   3243  1.1  christos 
   3244  1.1  christos     if (!X509_CRL_set1_lastUpdate(crl, tm))
   3245  1.1  christos         goto end;
   3246  1.1  christos 
   3247  1.1  christos     ret = 1;
   3248  1.1  christos end:
   3249  1.1  christos     ASN1_TIME_free(tm);
   3250  1.1  christos     return ret;
   3251  1.1  christos }
   3252  1.1  christos 
   3253  1.1  christos int set_crl_nextupdate(X509_CRL *crl, const char *nextupdate,
   3254  1.1  christos                        long days, long hours, long secs)
   3255  1.1  christos {
   3256  1.1  christos     int ret = 0;
   3257  1.1  christos     ASN1_TIME *tm = ASN1_TIME_new();
   3258  1.1  christos 
   3259  1.1  christos     if (tm == NULL)
   3260  1.1  christos         goto end;
   3261  1.1  christos 
   3262  1.1  christos     if (nextupdate == NULL) {
   3263  1.1  christos         if (X509_time_adj_ex(tm, days, hours * 60 * 60 + secs, NULL) == NULL)
   3264  1.1  christos             goto end;
   3265  1.1  christos     } else {
   3266  1.1  christos         if (!ASN1_TIME_set_string_X509(tm, nextupdate))
   3267  1.1  christos             goto end;
   3268  1.1  christos     }
   3269  1.1  christos 
   3270  1.1  christos     if (!X509_CRL_set1_nextUpdate(crl, tm))
   3271  1.1  christos         goto end;
   3272  1.1  christos 
   3273  1.1  christos     ret = 1;
   3274  1.1  christos end:
   3275  1.1  christos     ASN1_TIME_free(tm);
   3276  1.1  christos     return ret;
   3277  1.1  christos }
   3278  1.1  christos 
   3279  1.1  christos void make_uppercase(char *string)
   3280  1.1  christos {
   3281  1.1  christos     int i;
   3282  1.1  christos 
   3283  1.1  christos     for (i = 0; string[i] != '\0'; i++)
   3284  1.1  christos         string[i] = toupper((unsigned char)string[i]);
   3285  1.1  christos }
   3286  1.1  christos 
   3287  1.1  christos /* This function is defined here due to visibility of bio_err */
   3288  1.1  christos int opt_printf_stderr(const char *fmt, ...)
   3289  1.1  christos {
   3290  1.1  christos     va_list ap;
   3291  1.1  christos     int ret;
   3292  1.1  christos 
   3293  1.1  christos     va_start(ap, fmt);
   3294  1.1  christos     ret = BIO_vprintf(bio_err, fmt, ap);
   3295  1.1  christos     va_end(ap);
   3296  1.1  christos     return ret;
   3297  1.1  christos }
   3298  1.1  christos 
   3299  1.1  christos OSSL_PARAM *app_params_new_from_opts(STACK_OF(OPENSSL_STRING) *opts,
   3300  1.1  christos                                      const OSSL_PARAM *paramdefs)
   3301  1.1  christos {
   3302  1.1  christos     OSSL_PARAM *params = NULL;
   3303  1.1  christos     size_t sz = (size_t)sk_OPENSSL_STRING_num(opts);
   3304  1.1  christos     size_t params_n;
   3305  1.1  christos     char *opt = "", *stmp, *vtmp = NULL;
   3306  1.1  christos     int found = 1;
   3307  1.1  christos 
   3308  1.1  christos     if (opts == NULL)
   3309  1.1  christos         return NULL;
   3310  1.1  christos 
   3311  1.1  christos     params = OPENSSL_zalloc(sizeof(OSSL_PARAM) * (sz + 1));
   3312  1.1  christos     if (params == NULL)
   3313  1.1  christos         return NULL;
   3314  1.1  christos 
   3315  1.1  christos     for (params_n = 0; params_n < sz; params_n++) {
   3316  1.1  christos         opt = sk_OPENSSL_STRING_value(opts, (int)params_n);
   3317  1.1  christos         if ((stmp = OPENSSL_strdup(opt)) == NULL
   3318  1.1  christos             || (vtmp = strchr(stmp, ':')) == NULL)
   3319  1.1  christos             goto err;
   3320  1.1  christos         /* Replace ':' with 0 to terminate the string pointed to by stmp */
   3321  1.1  christos         *vtmp = 0;
   3322  1.1  christos         /* Skip over the separator so that vmtp points to the value */
   3323  1.1  christos         vtmp++;
   3324  1.1  christos         if (!OSSL_PARAM_allocate_from_text(&params[params_n], paramdefs,
   3325  1.1  christos                                            stmp, vtmp, strlen(vtmp), &found))
   3326  1.1  christos             goto err;
   3327  1.1  christos         OPENSSL_free(stmp);
   3328  1.1  christos     }
   3329  1.1  christos     params[params_n] = OSSL_PARAM_construct_end();
   3330  1.1  christos     return params;
   3331  1.1  christos err:
   3332  1.1  christos     OPENSSL_free(stmp);
   3333  1.1  christos     BIO_printf(bio_err, "Parameter %s '%s'\n", found ? "error" : "unknown",
   3334  1.1  christos                opt);
   3335  1.1  christos     ERR_print_errors(bio_err);
   3336  1.1  christos     app_params_free(params);
   3337  1.1  christos     return NULL;
   3338  1.1  christos }
   3339  1.1  christos 
   3340  1.1  christos void app_params_free(OSSL_PARAM *params)
   3341  1.1  christos {
   3342  1.1  christos     int i;
   3343  1.1  christos 
   3344  1.1  christos     if (params != NULL) {
   3345  1.1  christos         for (i = 0; params[i].key != NULL; ++i)
   3346  1.1  christos             OPENSSL_free(params[i].data);
   3347  1.1  christos         OPENSSL_free(params);
   3348  1.1  christos     }
   3349  1.1  christos }
   3350  1.1  christos 
   3351  1.1  christos EVP_PKEY *app_keygen(EVP_PKEY_CTX *ctx, const char *alg, int bits, int verbose)
   3352  1.1  christos {
   3353  1.1  christos     EVP_PKEY *res = NULL;
   3354  1.1  christos 
   3355  1.1  christos     if (verbose && alg != NULL) {
   3356  1.1  christos         BIO_printf(bio_err, "Generating %s key", alg);
   3357  1.1  christos         if (bits > 0)
   3358  1.1  christos             BIO_printf(bio_err, " with %d bits\n", bits);
   3359  1.1  christos         else
   3360  1.1  christos             BIO_printf(bio_err, "\n");
   3361  1.1  christos     }
   3362  1.1  christos     if (!RAND_status())
   3363  1.1  christos         BIO_printf(bio_err, "Warning: generating random key material may take a long time\n"
   3364  1.1  christos                    "if the system has a poor entropy source\n");
   3365  1.1  christos     if (EVP_PKEY_keygen(ctx, &res) <= 0)
   3366  1.5  christos         BIO_printf(bio_err, "%s: Error generating %s key\n", opt_getprog(),
   3367  1.5  christos                    alg != NULL ? alg : "asymmetric");
   3368  1.1  christos     return res;
   3369  1.1  christos }
   3370  1.1  christos 
   3371  1.1  christos EVP_PKEY *app_paramgen(EVP_PKEY_CTX *ctx, const char *alg)
   3372  1.1  christos {
   3373  1.1  christos     EVP_PKEY *res = NULL;
   3374  1.1  christos 
   3375  1.1  christos     if (!RAND_status())
   3376  1.1  christos         BIO_printf(bio_err, "Warning: generating random key parameters may take a long time\n"
   3377  1.1  christos                    "if the system has a poor entropy source\n");
   3378  1.1  christos     if (EVP_PKEY_paramgen(ctx, &res) <= 0)
   3379  1.5  christos         BIO_printf(bio_err, "%s: Generating %s key parameters failed\n",
   3380  1.5  christos                    opt_getprog(), alg != NULL ? alg : "asymmetric");
   3381  1.1  christos     return res;
   3382  1.1  christos }
   3383  1.1  christos 
   3384  1.1  christos /*
   3385  1.1  christos  * Return non-zero if the legacy path is still an option.
   3386  1.1  christos  * This decision is based on the global command line operations and the
   3387  1.1  christos  * behaviour thus far.
   3388  1.1  christos  */
   3389  1.1  christos int opt_legacy_okay(void)
   3390  1.1  christos {
   3391  1.1  christos     int provider_options = opt_provider_option_given();
   3392  1.1  christos     int libctx = app_get0_libctx() != NULL || app_get0_propq() != NULL;
   3393  1.1  christos     /*
   3394  1.1  christos      * Having a provider option specified or a custom library context or
   3395  1.1  christos      * property query, is a sure sign we're not using legacy.
   3396  1.1  christos      */
   3397  1.1  christos     if (provider_options || libctx)
   3398  1.1  christos         return 0;
   3399  1.1  christos     return 1;
   3400  1.1  christos }
   3401