1/*
2 * Copyright 2019 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 <openssl/err.h>
11#include <openssl/cryptoerr.h>
12#include <openssl/provider.h>
13#include "internal/provider.h"
14
15OSSL_PROVIDER *OSSL_PROVIDER_load(OPENSSL_CTX *libctx, const char *name)
16{
17 OSSL_PROVIDER *prov = NULL;
18
19 /* Find it or create it */
20 if ((prov = ossl_provider_find(libctx, name, 0)) == NULL
21 && (prov = ossl_provider_new(libctx, name, NULL, 0)) == NULL)
22 return NULL;
23
24 if (!ossl_provider_activate(prov)) {
25 ossl_provider_free(prov);
26 return NULL;
27 }
28
29 return prov;
30}
31
32int OSSL_PROVIDER_unload(OSSL_PROVIDER *prov)
33{
34 ossl_provider_free(prov);
35 return 1;
36}
37
38int OSSL_PROVIDER_available(OPENSSL_CTX *libctx, const char *name)
39{
40 OSSL_PROVIDER *prov = NULL;
41 int available = 0;
42
43 /* Find it or create it */
44 prov = ossl_provider_find(libctx, name, 0);
45 available = ossl_provider_available(prov);
46 ossl_provider_free(prov);
47 return available;
48}
49
50const OSSL_PARAM *OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER *prov)
51{
52 return ossl_provider_gettable_params(prov);
53}
54
55int OSSL_PROVIDER_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
56{
57 return ossl_provider_get_params(prov, params);
58}
59
60int OSSL_PROVIDER_add_builtin(OPENSSL_CTX *libctx, const char *name,
61 OSSL_provider_init_fn *init_fn)
62{
63 OSSL_PROVIDER *prov = NULL;
64
65 if (name == NULL || init_fn == NULL) {
66 CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ADD_BUILTIN,
67 ERR_R_PASSED_NULL_PARAMETER);
68 return 0;
69 }
70
71 /* Create it */
72 if ((prov = ossl_provider_new(libctx, name, init_fn, 0)) == NULL)
73 return 0;
74
75 /*
76 * It's safely stored in the internal store at this point,
77 * free the returned extra reference
78 */
79 ossl_provider_free(prov);
80
81 return 1;
82}
83
84const char *OSSL_PROVIDER_name(const OSSL_PROVIDER *prov)
85{
86 return ossl_provider_name(prov);
87}
88