rand_unix.c: Ensure requests to KERN_ARND don't exceed 256 bytes.
[openssl.git] / providers / nullprov.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 <string.h>
11 #include <stdio.h>
12 #include <openssl/core.h>
13 #include <openssl/core_numbers.h>
14 #include <openssl/core_names.h>
15 #include <openssl/params.h>
16 #include "prov/implementations.h"
17
18 OSSL_provider_init_fn ossl_null_provider_init;
19
20 /* Functions provided by the core */
21 static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
22 static OSSL_core_get_params_fn *c_get_params = NULL;
23
24 /* Parameters we provide to the core */
25 static const OSSL_ITEM null_param_types[] = {
26     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
27     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
28     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
29     { 0, NULL }
30 };
31
32 static const OSSL_ITEM *null_gettable_params(const OSSL_PROVIDER *prov)
33 {
34     return null_param_types;
35 }
36
37 static int null_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
38 {
39     OSSL_PARAM *p;
40
41     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
42     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Null Provider"))
43         return 0;
44     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
45     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
46         return 0;
47     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
48     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
49         return 0;
50
51     return 1;
52 }
53
54 static const OSSL_ALGORITHM *null_query(OSSL_PROVIDER *prov,
55                                           int operation_id,
56                                           int *no_cache)
57 {
58     *no_cache = 0;
59     return NULL;
60 }
61
62 /* Functions we provide to the core */
63 static const OSSL_DISPATCH null_dispatch_table[] = {
64     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))null_gettable_params },
65     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))null_get_params },
66     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))null_query },
67     { 0, NULL }
68 };
69
70 int ossl_null_provider_init(const OSSL_PROVIDER *provider,
71                             const OSSL_DISPATCH *in,
72                             const OSSL_DISPATCH **out,
73                             void **provctx)
74 {
75     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
76
77     for (; in->function_id != 0; in++) {
78         switch (in->function_id) {
79         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
80             c_gettable_params = OSSL_get_core_gettable_params(in);
81             break;
82         case OSSL_FUNC_CORE_GET_PARAMS:
83             c_get_params = OSSL_get_core_get_params(in);
84             break;
85         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
86             c_get_libctx = OSSL_get_core_get_library_context(in);
87             break;
88         /* Just ignore anything we don't understand */
89         default:
90             break;
91         }
92     }
93
94     if (c_get_libctx == NULL)
95         return 0;
96
97     *out = null_dispatch_table;
98
99     /*
100      * We want to make sure that all calls from this provider that requires
101      * a library context use the same context as the one used to call our
102      * functions.  We do that by passing it along as the provider context.
103      */
104     *provctx = c_get_libctx(provider);
105     return 1;
106 }