]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/fips/fipsprov.c
Make some EVP code available from within the FIPS module
[thirdparty/openssl.git] / providers / fips / fipsprov.c
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 <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 <openssl/err.h>
17 #include <openssl/evp.h>
18 /* TODO(3.0): Needed for dummy_evp_call(). To be removed */
19 #include <openssl/sha.h>
20 #include "internal/cryptlib.h"
21 #include "internal/property.h"
22 #include "internal/evp_int.h"
23
24 /* Functions provided by the core */
25 static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
26 static OSSL_core_get_params_fn *c_get_params = NULL;
27 static OSSL_core_put_error_fn *c_put_error = NULL;
28 static OSSL_core_add_error_vdata_fn *c_add_error_vdata = NULL;
29
30 /* Parameters we provide to the core */
31 static const OSSL_ITEM fips_param_types[] = {
32 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
33 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
34 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
35 { 0, NULL }
36 };
37
38 /* TODO(3.0): To be removed */
39 static int dummy_evp_call(OPENSSL_CTX *libctx)
40 {
41 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
42 EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL);
43 char msg[] = "Hello World!";
44 const unsigned char exptd[] = {
45 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
46 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
47 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
48 };
49 unsigned int dgstlen = 0;
50 unsigned char dgst[SHA256_DIGEST_LENGTH];
51 int ret = 0;
52
53 if (ctx == NULL || sha256 == NULL)
54 goto err;
55
56 if (!EVP_DigestInit_ex(ctx, sha256, NULL))
57 goto err;
58 if (!EVP_DigestUpdate(ctx, msg, sizeof(msg) - 1))
59 goto err;
60 if (!EVP_DigestFinal(ctx, dgst, &dgstlen))
61 goto err;
62 if (dgstlen != sizeof(exptd) || memcmp(dgst, exptd, sizeof(exptd)) != 0)
63 goto err;
64
65 ret = 1;
66 err:
67 EVP_MD_CTX_free(ctx);
68 EVP_MD_meth_free(sha256);
69 return ret;
70 }
71
72 static const OSSL_ITEM *fips_get_param_types(const OSSL_PROVIDER *prov)
73 {
74 return fips_param_types;
75 }
76
77 static int fips_get_params(const OSSL_PROVIDER *prov,
78 const OSSL_PARAM params[])
79 {
80 const OSSL_PARAM *p;
81
82 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
83 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
84 return 0;
85 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
86 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
87 return 0;
88 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
89 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
90 return 0;
91
92 return 1;
93 }
94
95 extern const OSSL_DISPATCH sha256_functions[];
96
97 static const OSSL_ALGORITHM fips_digests[] = {
98 { "SHA256", "fips=yes", sha256_functions },
99 { NULL, NULL, NULL }
100 };
101
102 static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov,
103 int operation_id,
104 int *no_cache)
105 {
106 *no_cache = 0;
107 switch (operation_id) {
108 case OSSL_OP_DIGEST:
109 return fips_digests;
110 }
111 return NULL;
112 }
113
114 /* Functions we provide to the core */
115 static const OSSL_DISPATCH fips_dispatch_table[] = {
116 /*
117 * To release our resources we just need to free the OPENSSL_CTX so we just
118 * use OPENSSL_CTX_free directly as our teardown function
119 */
120 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
121 { OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))fips_get_param_types },
122 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
123 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
124 { 0, NULL }
125 };
126
127 /* Functions we provide to ourself */
128 static const OSSL_DISPATCH intern_dispatch_table[] = {
129 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
130 { 0, NULL }
131 };
132
133
134 int OSSL_provider_init(const OSSL_PROVIDER *provider,
135 const OSSL_DISPATCH *in,
136 const OSSL_DISPATCH **out,
137 void **provctx)
138 {
139 OPENSSL_CTX *ctx;
140
141 for (; in->function_id != 0; in++) {
142 switch (in->function_id) {
143 case OSSL_FUNC_CORE_GET_PARAM_TYPES:
144 c_get_param_types = OSSL_get_core_get_param_types(in);
145 break;
146 case OSSL_FUNC_CORE_GET_PARAMS:
147 c_get_params = OSSL_get_core_get_params(in);
148 break;
149 case OSSL_FUNC_CORE_PUT_ERROR:
150 c_put_error = OSSL_get_core_put_error(in);
151 break;
152 case OSSL_FUNC_CORE_ADD_ERROR_VDATA:
153 c_add_error_vdata = OSSL_get_core_add_error_vdata(in);
154 break;
155 /* Just ignore anything we don't understand */
156 default:
157 break;
158 }
159 }
160
161 ctx = OPENSSL_CTX_new();
162 if (ctx == NULL)
163 return 0;
164
165 /*
166 * TODO(3.0): Remove me. This is just a dummy call to demonstrate making
167 * EVP calls from within the FIPS module.
168 */
169 if (!dummy_evp_call(ctx)) {
170 OPENSSL_CTX_free(ctx);
171 return 0;
172 }
173
174 *out = fips_dispatch_table;
175 *provctx = ctx;
176 return 1;
177 }
178
179 /*
180 * The internal init function used when the FIPS module uses EVP to call
181 * another algorithm also in the FIPS module.
182 */
183 OSSL_provider_init_fn fips_intern_provider_init;
184 int fips_intern_provider_init(const OSSL_PROVIDER *provider,
185 const OSSL_DISPATCH *in,
186 const OSSL_DISPATCH **out,
187 void **provctx)
188 {
189 *out = intern_dispatch_table;
190 return 1;
191 }
192
193 void ERR_put_error(int lib, int func, int reason, const char *file, int line)
194 {
195 /*
196 * TODO(3.0): This works for the FIPS module because we're going to be
197 * using lib/func/reason codes that libcrypto already knows about. This
198 * won't work for third party providers that have their own error mechanisms,
199 * so we'll need to come up with something else for them.
200 */
201 c_put_error(lib, func, reason, file, line);
202 }
203
204 void ERR_add_error_data(int num, ...)
205 {
206 va_list args;
207 va_start(args, num);
208 ERR_add_error_vdata(num, args);
209 va_end(args);
210 }
211
212 void ERR_add_error_vdata(int num, va_list args)
213 {
214 c_add_error_vdata(num, args);
215 }