cmdline app: add provider commandline options.
[oweals/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
14 /*
15  * See comments in opt_verify for explanation of this.
16  */
17 enum prov_range { OPT_PROV_ENUM };
18
19 static int opt_provider_load(const char *provider)
20 {
21     OSSL_PROVIDER *prov;
22
23     prov = OSSL_PROVIDER_load(NULL, provider);
24     if (prov == NULL) {
25         opt_printf_stderr("%s: unable to load provider %s\n",
26                           opt_getprog(), provider);
27         return 0;
28     }
29     return 1;
30 }
31
32 static int opt_provider_path(const char *path)
33 {
34     if (path != NULL && *path == '\0')
35         path = NULL;
36     return OSSL_PROVIDER_set_default_search_path(NULL, path);
37 }
38
39 int opt_provider(int opt)
40 {
41     switch ((enum prov_range)opt) {
42     case OPT_PROV__FIRST:
43     case OPT_PROV__LAST:
44         return 1;
45     case OPT_PROV_PROVIDER:
46         return opt_provider_load(opt_arg());
47     case OPT_PROV_PROVIDER_PATH:
48         return opt_provider_path(opt_arg());
49     }
50     return 0;
51 }