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