GOST2012 TLS ClientCertificateType Identifiers support
[openssl.git] / apps / lib / app_provider.c
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "apps.h"
11 #include <openssl/err.h>
12 #include <openssl/provider.h>
13 #include <openssl/safestack.h>
14
15 DEFINE_STACK_OF(OSSL_PROVIDER)
16
17 /*
18  * See comments in opt_verify for explanation of this.
19  */
20 enum prov_range { OPT_PROV_ENUM };
21
22 static STACK_OF(OSSL_PROVIDER) *app_providers = NULL;
23
24 static int opt_provider_load(const char *provider)
25 {
26     OSSL_PROVIDER *prov;
27
28     prov = OSSL_PROVIDER_load(NULL, provider);
29     if (prov == NULL) {
30         opt_printf_stderr("%s: unable to load provider %s\n",
31                           opt_getprog(), provider);
32         return 0;
33     }
34     if (app_providers == NULL)
35         app_providers = sk_OSSL_PROVIDER_new_null();
36     if (app_providers == NULL
37         || !sk_OSSL_PROVIDER_push(app_providers, prov)) {
38         app_providers_cleanup();
39         return 0;
40     }
41     return 1;
42 }
43
44 static void provider_free(OSSL_PROVIDER *prov)
45 {
46     OSSL_PROVIDER_unload(prov);
47 }
48
49 void app_providers_cleanup(void)
50 {
51     sk_OSSL_PROVIDER_pop_free(app_providers, provider_free);
52     app_providers = NULL;
53 }
54
55 static int opt_provider_path(const char *path)
56 {
57     if (path != NULL && *path == '\0')
58         path = NULL;
59     return OSSL_PROVIDER_set_default_search_path(NULL, path);
60 }
61
62 int opt_provider(int opt)
63 {
64     switch ((enum prov_range)opt) {
65     case OPT_PROV__FIRST:
66     case OPT_PROV__LAST:
67         return 1;
68     case OPT_PROV_PROVIDER:
69         return opt_provider_load(opt_arg());
70     case OPT_PROV_PROVIDER_PATH:
71         return opt_provider_path(opt_arg());
72     }
73     return 0;
74 }