PROV: Adapt all our providers to use the new PROV_CTX structure
[openssl.git] / providers / fips / fipsprov.c
1 /*
2  * Copyright 2019-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 <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/kdf.h>
19
20 /* TODO(3.0): Needed for dummy_evp_call(). To be removed */
21 #include <openssl/sha.h>
22 #include <openssl/rand_drbg.h>
23 #include <openssl/ec.h>
24 #include <openssl/fips_names.h>
25
26 #include "internal/cryptlib.h"
27 #include "internal/property.h"
28 #include "internal/nelem.h"
29 #include "openssl/param_build.h"
30 #include "crypto/evp.h"
31 #include "prov/implementations.h"
32 #include "prov/provider_ctx.h"
33 #include "prov/providercommon.h"
34 #include "prov/provider_util.h"
35 #include "self_test.h"
36
37 /*
38  * Forward declarations to ensure that interface functions are correctly
39  * defined.
40  */
41 static OSSL_provider_teardown_fn fips_teardown;
42 static OSSL_provider_gettable_params_fn fips_gettable_params;
43 static OSSL_provider_get_params_fn fips_get_params;
44 static OSSL_provider_query_operation_fn fips_query;
45
46 #define ALGC(NAMES, FUNC, CHECK) { { NAMES, "provider=fips,fips=yes", FUNC }, CHECK }
47 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
48
49 extern OSSL_core_thread_start_fn *c_thread_start;
50
51 /*
52  * TODO(3.0): Should these be stored in the provider side provctx? Could they
53  * ever be different from one init to the next? Unfortunately we can't do this
54  * at the moment because c_put_error/c_add_error_vdata do not provide
55  * us with the OPENSSL_CTX as a parameter.
56  */
57
58 static SELF_TEST_POST_PARAMS selftest_params;
59
60 /* Functions provided by the core */
61 static OSSL_core_gettable_params_fn *c_gettable_params;
62 static OSSL_core_get_params_fn *c_get_params;
63 OSSL_core_thread_start_fn *c_thread_start;
64 static OSSL_core_new_error_fn *c_new_error;
65 static OSSL_core_set_error_debug_fn *c_set_error_debug;
66 static OSSL_core_vset_error_fn *c_vset_error;
67 static OSSL_core_set_error_mark_fn *c_set_error_mark;
68 static OSSL_core_clear_last_error_mark_fn *c_clear_last_error_mark;
69 static OSSL_core_pop_error_to_mark_fn *c_pop_error_to_mark;
70 static OSSL_CRYPTO_malloc_fn *c_CRYPTO_malloc;
71 static OSSL_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
72 static OSSL_CRYPTO_free_fn *c_CRYPTO_free;
73 static OSSL_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
74 static OSSL_CRYPTO_realloc_fn *c_CRYPTO_realloc;
75 static OSSL_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
76 static OSSL_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
77 static OSSL_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
78 static OSSL_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
79 static OSSL_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
80 static OSSL_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
81 static OSSL_BIO_vsnprintf_fn *c_BIO_vsnprintf;
82
83 typedef struct fips_global_st {
84     const OSSL_PROVIDER *prov;
85 } FIPS_GLOBAL;
86
87 static void *fips_prov_ossl_ctx_new(OPENSSL_CTX *libctx)
88 {
89     FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
90
91     return fgbl;
92 }
93
94 static void fips_prov_ossl_ctx_free(void *fgbl)
95 {
96     OPENSSL_free(fgbl);
97 }
98
99 static const OPENSSL_CTX_METHOD fips_prov_ossl_ctx_method = {
100     fips_prov_ossl_ctx_new,
101     fips_prov_ossl_ctx_free,
102 };
103
104
105 /* Parameters we provide to the core */
106 static const OSSL_PARAM fips_param_types[] = {
107     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
108     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
109     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
110     OSSL_PARAM_END
111 };
112
113 /*
114  * Parameters to retrieve from the core provider - required for self testing.
115  * NOTE: inside core_get_params() these will be loaded from config items
116  * stored inside prov->parameters (except for OSSL_PROV_PARAM_MODULE_FILENAME).
117  */
118 static OSSL_PARAM core_params[] =
119 {
120     OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_MODULE_FILENAME,
121                         selftest_params.module_filename,
122                         sizeof(selftest_params.module_filename)),
123     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC,
124                         selftest_params.module_checksum_data,
125                         sizeof(selftest_params.module_checksum_data)),
126     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_MAC,
127                         selftest_params.indicator_checksum_data,
128                         sizeof(selftest_params.indicator_checksum_data)),
129     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
130                         selftest_params.indicator_data,
131                         sizeof(selftest_params.indicator_data)),
132     OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
133                         selftest_params.indicator_version,
134                         sizeof(selftest_params.indicator_version)),
135     OSSL_PARAM_END
136 };
137
138 /* TODO(3.0): To be removed */
139 static int dummy_evp_call(OPENSSL_CTX *libctx)
140 {
141     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
142     EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL);
143     EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, NULL);
144     unsigned char dgst[SHA256_DIGEST_LENGTH];
145     unsigned int dgstlen;
146     int ret = 0;
147     BN_CTX *bnctx = NULL;
148     BIGNUM *a = NULL, *b = NULL;
149     unsigned char randbuf[128];
150     RAND_DRBG *drbg = OPENSSL_CTX_get0_public_drbg(libctx);
151 #ifndef OPENSSL_NO_EC
152     EC_KEY *key = NULL;
153 #endif
154
155     static const char msg[] = "Hello World!";
156     static const unsigned char exptd[] = {
157         0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
158         0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
159         0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
160     };
161
162     if (ctx == NULL || sha256 == NULL || drbg == NULL || kdf == NULL)
163         goto err;
164
165     if (!EVP_DigestInit_ex(ctx, sha256, NULL))
166         goto err;
167     if (!EVP_DigestUpdate(ctx, msg, sizeof(msg) - 1))
168         goto err;
169     if (!EVP_DigestFinal(ctx, dgst, &dgstlen))
170         goto err;
171     if (dgstlen != sizeof(exptd) || memcmp(dgst, exptd, sizeof(exptd)) != 0)
172         goto err;
173
174     bnctx = BN_CTX_new_ex(libctx);
175     if (bnctx == NULL)
176         goto err;
177     BN_CTX_start(bnctx);
178     a = BN_CTX_get(bnctx);
179     b = BN_CTX_get(bnctx);
180     if (b == NULL)
181         goto err;
182     BN_zero(a);
183     if (!BN_one(b)
184         || !BN_add(a, a, b)
185         || BN_cmp(a, b) != 0)
186         goto err;
187
188     if (RAND_DRBG_bytes(drbg, randbuf, sizeof(randbuf)) <= 0)
189         goto err;
190
191     if (!BN_rand_ex(a, 256, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, bnctx))
192         goto err;
193
194 #ifndef OPENSSL_NO_EC
195     /* Do some dummy EC calls */
196     key = EC_KEY_new_by_curve_name_ex(libctx, NID_X9_62_prime256v1);
197     if (key == NULL)
198         goto err;
199
200     if (!EC_KEY_generate_key(key))
201         goto err;
202 #endif
203
204     ret = 1;
205  err:
206     BN_CTX_end(bnctx);
207     BN_CTX_free(bnctx);
208
209     EVP_KDF_free(kdf);
210     EVP_MD_CTX_free(ctx);
211     EVP_MD_free(sha256);
212
213 #ifndef OPENSSL_NO_EC
214     EC_KEY_free(key);
215 #endif
216     return ret;
217 }
218
219 static const OSSL_PARAM *fips_gettable_params(void *provctx)
220 {
221     return fips_param_types;
222 }
223
224 static int fips_get_params(void *provctx, OSSL_PARAM params[])
225 {
226     OSSL_PARAM *p;
227
228     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
229     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
230         return 0;
231     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
232     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
233         return 0;
234     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
235     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
236         return 0;
237
238     return 1;
239 }
240
241 /* FIPS specific version of the function of the same name in provlib.c */
242 const char *ossl_prov_util_nid_to_name(int nid)
243 {
244     /* We don't have OBJ_nid2n() in FIPS_MODULE so we have an explicit list */
245
246     switch (nid) {
247     /* Digests */
248     case NID_sha1:
249         return "SHA1";
250     case NID_sha224:
251         return "SHA-224";
252     case NID_sha256:
253         return "SHA-256";
254     case NID_sha384:
255         return "SHA-384";
256     case NID_sha512:
257         return "SHA-512";
258     case NID_sha512_224:
259         return "SHA-512/224";
260     case NID_sha512_256:
261         return "SHA-512/256";
262     case NID_sha3_224:
263         return "SHA3-224";
264     case NID_sha3_256:
265         return "SHA3-256";
266     case NID_sha3_384:
267         return "SHA3-384";
268     case NID_sha3_512:
269         return "SHA3-512";
270
271     /* Ciphers */
272     case NID_aes_256_ecb:
273         return "AES-256-ECB";
274     case NID_aes_192_ecb:
275         return "AES-192-ECB";
276     case NID_aes_128_ecb:
277         return "AES-128-ECB";
278     case NID_aes_256_cbc:
279         return "AES-256-CBC";
280     case NID_aes_192_cbc:
281         return "AES-192-CBC";
282     case NID_aes_128_cbc:
283         return "AES-128-CBC";
284     case NID_aes_256_ctr:
285         return "AES-256-CTR";
286     case NID_aes_192_ctr:
287         return "AES-192-CTR";
288     case NID_aes_128_ctr:
289         return "AES-128-CTR";
290     case NID_aes_256_xts:
291         return "AES-256-XTS";
292     case NID_aes_128_xts:
293         return "AES-128-XTS";
294     case NID_aes_256_gcm:
295         return "AES-256-GCM";
296     case NID_aes_192_gcm:
297         return "AES-192-GCM";
298     case NID_aes_128_gcm:
299         return "AES-128-GCM";
300     case NID_aes_256_ccm:
301         return "AES-256-CCM";
302     case NID_aes_192_ccm:
303         return "AES-192-CCM";
304     case NID_aes_128_ccm:
305         return "AES-128-CCM";
306     case NID_id_aes256_wrap:
307         return "AES-256-WRAP";
308     case NID_id_aes192_wrap:
309         return "AES-192-WRAP";
310     case NID_id_aes128_wrap:
311         return "AES-128-WRAP";
312     case NID_id_aes256_wrap_pad:
313         return "AES-256-WRAP-PAD";
314     case NID_id_aes192_wrap_pad:
315         return "AES-192-WRAP-PAD";
316     case NID_id_aes128_wrap_pad:
317         return "AES-128-WRAP-PAD";
318     case NID_des_ede3_ecb:
319         return "DES-EDE3";
320     case NID_des_ede3_cbc:
321         return "DES-EDE3-CBC";
322     case NID_aes_256_cbc_hmac_sha256:
323         return "AES-256-CBC-HMAC-SHA256";
324     case NID_aes_128_cbc_hmac_sha256:
325         return "AES-128-CBC-HMAC-SHA256";
326     case NID_aes_256_cbc_hmac_sha1:
327         return "AES-256-CBC-HMAC-SHA1";
328     case NID_aes_128_cbc_hmac_sha1:
329         return "AES-128-CBC-HMAC-SHA1";
330     default:
331         break;
332     }
333
334     return NULL;
335 }
336
337 /*
338  * For the algorithm names, we use the following formula for our primary
339  * names:
340  *
341  *     ALGNAME[VERSION?][-SUBNAME[VERSION?]?][-SIZE?][-MODE?]
342  *
343  *     VERSION is only present if there are multiple versions of
344  *     an alg (MD2, MD4, MD5).  It may be omitted if there is only
345  *     one version (if a subsequent version is released in the future,
346  *     we can always change the canonical name, and add the old name
347  *     as an alias).
348  *
349  *     SUBNAME may be present where we are combining multiple
350  *     algorithms together, e.g. MD5-SHA1.
351  *
352  *     SIZE is only present if multiple versions of an algorithm exist
353  *     with different sizes (e.g. AES-128-CBC, AES-256-CBC)
354  *
355  *     MODE is only present where applicable.
356  *
357  * We add diverse other names where applicable, such as the names that
358  * NIST uses, or that are used for ASN.1 OBJECT IDENTIFIERs, or names
359  * we have used historically.
360  */
361 static const OSSL_ALGORITHM fips_digests[] = {
362     /* Our primary name:NiST name[:our older names] */
363     { "SHA1:SHA-1", "provider=fips,fips=yes", sha1_functions },
364     { "SHA2-224:SHA-224:SHA224", "provider=fips,fips=yes", sha224_functions },
365     { "SHA2-256:SHA-256:SHA256", "provider=fips,fips=yes", sha256_functions },
366     { "SHA2-384:SHA-384:SHA384", "provider=fips,fips=yes", sha384_functions },
367     { "SHA2-512:SHA-512:SHA512", "provider=fips,fips=yes", sha512_functions },
368     { "SHA2-512/224:SHA-512/224:SHA512-224", "provider=fips,fips=yes",
369       sha512_224_functions },
370     { "SHA2-512/256:SHA-512/256:SHA512-256", "provider=fips,fips=yes",
371       sha512_256_functions },
372
373     /* We agree with NIST here, so one name only */
374     { "SHA3-224", "provider=fips,fips=yes", sha3_224_functions },
375     { "SHA3-256", "provider=fips,fips=yes", sha3_256_functions },
376     { "SHA3-384", "provider=fips,fips=yes", sha3_384_functions },
377     { "SHA3-512", "provider=fips,fips=yes", sha3_512_functions },
378
379     /* Non-FIPS algorithm to support oneshot_hash in the Ed448 code */
380     { "SHAKE-256:SHAKE256", "provider=fips,fips=no", shake_256_functions },
381     { NULL, NULL, NULL }
382 };
383
384 static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = {
385     /* Our primary name[:ASN.1 OID name][:our older names] */
386     ALG("AES-256-ECB", aes256ecb_functions),
387     ALG("AES-192-ECB", aes192ecb_functions),
388     ALG("AES-128-ECB", aes128ecb_functions),
389     ALG("AES-256-CBC", aes256cbc_functions),
390     ALG("AES-192-CBC", aes192cbc_functions),
391     ALG("AES-128-CBC", aes128cbc_functions),
392     ALG("AES-256-CTR", aes256ctr_functions),
393     ALG("AES-192-CTR", aes192ctr_functions),
394     ALG("AES-128-CTR", aes128ctr_functions),
395     ALG("AES-256-XTS", aes256xts_functions),
396     ALG("AES-128-XTS", aes128xts_functions),
397     ALG("AES-256-GCM:id-aes256-GCM", aes256gcm_functions),
398     ALG("AES-192-GCM:id-aes192-GCM", aes192gcm_functions),
399     ALG("AES-128-GCM:id-aes128-GCM", aes128gcm_functions),
400     ALG("AES-256-CCM:id-aes256-CCM", aes256ccm_functions),
401     ALG("AES-192-CCM:id-aes192-CCM", aes192ccm_functions),
402     ALG("AES-128-CCM:id-aes128-CCM", aes128ccm_functions),
403     ALG("AES-256-WRAP:id-aes256-wrap:AES256-WRAP", aes256wrap_functions),
404     ALG("AES-192-WRAP:id-aes192-wrap:AES192-WRAP", aes192wrap_functions),
405     ALG("AES-128-WRAP:id-aes128-wrap:AES128-WRAP", aes128wrap_functions),
406     ALG("AES-256-WRAP-PAD:id-aes256-wrap-pad:AES256-WRAP-PAD",
407         aes256wrappad_functions),
408     ALG("AES-192-WRAP-PAD:id-aes192-wrap-pad:AES192-WRAP-PAD",
409         aes192wrappad_functions),
410     ALG("AES-128-WRAP-PAD:id-aes128-wrap-pad:AES128-WRAP-PAD",
411         aes128wrappad_functions),
412     ALGC("AES-128-CBC-HMAC-SHA1", aes128cbc_hmac_sha1_functions,
413          cipher_capable_aes_cbc_hmac_sha1),
414     ALGC("AES-256-CBC-HMAC-SHA1", aes256cbc_hmac_sha1_functions,
415          cipher_capable_aes_cbc_hmac_sha1),
416     ALGC("AES-128-CBC-HMAC-SHA256", aes128cbc_hmac_sha256_functions,
417          cipher_capable_aes_cbc_hmac_sha256),
418     ALGC("AES-256-CBC-HMAC-SHA256", aes256cbc_hmac_sha256_functions,
419          cipher_capable_aes_cbc_hmac_sha256),
420 #ifndef OPENSSL_NO_DES
421     ALG("DES-EDE3-ECB:DES-EDE3", tdes_ede3_ecb_functions),
422     ALG("DES-EDE3-CBC:DES3", tdes_ede3_cbc_functions),
423 #endif  /* OPENSSL_NO_DES */
424     { { NULL, NULL, NULL }, NULL }
425 };
426 static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)];
427
428 static const OSSL_ALGORITHM fips_macs[] = {
429     { "GMAC", "provider=fips,fips=yes", gmac_functions },
430     { "HMAC", "provider=fips,fips=yes", hmac_functions },
431     { NULL, NULL, NULL }
432 };
433
434 static const OSSL_ALGORITHM fips_kdfs[] = {
435     { "HKDF", "provider=fips,fips=yes", kdf_hkdf_functions },
436     { "SSKDF", "provider=fips,fips=yes", kdf_sskdf_functions },
437     { "PBKDF2", "provider=fips,fips=yes", kdf_pbkdf2_functions },
438     { "TLS1-PRF", "provider=fips,fips=yes", kdf_tls1_prf_functions },
439     { NULL, NULL, NULL }
440 };
441
442 static const OSSL_ALGORITHM fips_keyexch[] = {
443 #ifndef OPENSSL_NO_DH
444     { "DH:dhKeyAgreement", "provider=fips,fips=yes", dh_keyexch_functions },
445 #endif
446 #ifndef OPENSSL_NO_EC
447     { "ECDH", "provider=fips,fips=yes", ecdh_keyexch_functions },
448     { "X25519", "provider=fips,fips=no", x25519_keyexch_functions },
449     { "X448", "provider=fips,fips=no", x448_keyexch_functions },
450 #endif
451     { NULL, NULL, NULL }
452 };
453
454 static const OSSL_ALGORITHM fips_signature[] = {
455 #ifndef OPENSSL_NO_DSA
456     { "DSA:dsaEncryption", "provider=fips,fips=yes", dsa_signature_functions },
457 #endif
458     { "RSA:rsaEncryption", "provider=fips,fips=yes", rsa_signature_functions },
459 #ifndef OPENSSL_NO_EC
460     { "ED25519", "provider=fips,fips=no", ed25519_signature_functions },
461     { "ED448", "provider=fips,fips=no", ed448_signature_functions },
462     { "ECDSA", "provider=fips,fips=yes", ecdsa_signature_functions },
463 #endif
464     { NULL, NULL, NULL }
465 };
466
467 static const OSSL_ALGORITHM fips_asym_cipher[] = {
468     { "RSA:rsaEncryption", "provider=fips,fips=yes", rsa_asym_cipher_functions },
469     { NULL, NULL, NULL }
470 };
471
472 static const OSSL_ALGORITHM fips_keymgmt[] = {
473 #ifndef OPENSSL_NO_DH
474     { "DH:dhKeyAgreement", "provider=fips,fips=yes", dh_keymgmt_functions },
475 #endif
476 #ifndef OPENSSL_NO_DSA
477     { "DSA", "provider=fips,fips=yes", dsa_keymgmt_functions },
478 #endif
479     { "RSA:rsaEncryption", "provider=fips,fips=yes", rsa_keymgmt_functions },
480 #ifndef OPENSSL_NO_EC
481     { "EC:id-ecPublicKey", "provider=fips,fips=yes", ec_keymgmt_functions },
482     { "X25519", "provider=fips,fips=no", x25519_keymgmt_functions },
483     { "X448", "provider=fips,fips=no", x448_keymgmt_functions },
484     { "ED25519", "provider=fips,fips=no", ed25519_keymgmt_functions },
485     { "ED448", "provider=fips,fips=no", ed448_keymgmt_functions },
486 #endif
487     { NULL, NULL, NULL }
488 };
489
490 static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id,
491                                         int *no_cache)
492 {
493     *no_cache = 0;
494     switch (operation_id) {
495     case OSSL_OP_DIGEST:
496         return fips_digests;
497     case OSSL_OP_CIPHER:
498         ossl_prov_cache_exported_algorithms(fips_ciphers, exported_fips_ciphers);
499         return exported_fips_ciphers;
500     case OSSL_OP_MAC:
501         return fips_macs;
502     case OSSL_OP_KDF:
503         return fips_kdfs;
504     case OSSL_OP_KEYMGMT:
505         return fips_keymgmt;
506     case OSSL_OP_KEYEXCH:
507         return fips_keyexch;
508     case OSSL_OP_SIGNATURE:
509         return fips_signature;
510     case OSSL_OP_ASYM_CIPHER:
511         return fips_asym_cipher;
512     }
513     return NULL;
514 }
515
516 static void fips_teardown(void *provctx)
517 {
518     OPENSSL_CTX_free(PROV_LIBRARY_CONTEXT_OF(provctx));
519     PROV_CTX_free(provctx);
520 }
521
522 static void fips_intern_teardown(void *provctx)
523 {
524     /*
525      * We know that the library context is the same as for the outer provider,
526      * so no need to destroy it here.
527      */
528     PROV_CTX_free(provctx);
529 }
530
531 /* Functions we provide to the core */
532 static const OSSL_DISPATCH fips_dispatch_table[] = {
533     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown },
534     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
535     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
536     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
537     { 0, NULL }
538 };
539
540 /* Functions we provide to ourself */
541 static const OSSL_DISPATCH intern_dispatch_table[] = {
542     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown },
543     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
544     { 0, NULL }
545 };
546
547
548 int OSSL_provider_init(const OSSL_PROVIDER *provider,
549                        const OSSL_DISPATCH *in,
550                        const OSSL_DISPATCH **out,
551                        void **provctx)
552 {
553     FIPS_GLOBAL *fgbl;
554     OPENSSL_CTX *libctx = NULL;
555     OSSL_self_test_cb_fn *stcbfn = NULL;
556     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
557
558     for (; in->function_id != 0; in++) {
559         switch (in->function_id) {
560         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
561             c_get_libctx = OSSL_get_core_get_library_context(in);
562             break;
563         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
564             c_gettable_params = OSSL_get_core_gettable_params(in);
565             break;
566         case OSSL_FUNC_CORE_GET_PARAMS:
567             c_get_params = OSSL_get_core_get_params(in);
568             break;
569         case OSSL_FUNC_CORE_THREAD_START:
570             c_thread_start = OSSL_get_core_thread_start(in);
571             break;
572         case OSSL_FUNC_CORE_NEW_ERROR:
573             c_new_error = OSSL_get_core_new_error(in);
574             break;
575         case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
576             c_set_error_debug = OSSL_get_core_set_error_debug(in);
577             break;
578         case OSSL_FUNC_CORE_VSET_ERROR:
579             c_vset_error = OSSL_get_core_vset_error(in);
580             break;
581         case OSSL_FUNC_CORE_SET_ERROR_MARK:
582             c_set_error_mark = OSSL_get_core_set_error_mark(in);
583             break;
584         case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
585             c_clear_last_error_mark = OSSL_get_core_clear_last_error_mark(in);
586             break;
587         case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
588             c_pop_error_to_mark = OSSL_get_core_pop_error_to_mark(in);
589             break;
590         case OSSL_FUNC_CRYPTO_MALLOC:
591             c_CRYPTO_malloc = OSSL_get_CRYPTO_malloc(in);
592             break;
593         case OSSL_FUNC_CRYPTO_ZALLOC:
594             c_CRYPTO_zalloc = OSSL_get_CRYPTO_zalloc(in);
595             break;
596         case OSSL_FUNC_CRYPTO_FREE:
597             c_CRYPTO_free = OSSL_get_CRYPTO_free(in);
598             break;
599         case OSSL_FUNC_CRYPTO_CLEAR_FREE:
600             c_CRYPTO_clear_free = OSSL_get_CRYPTO_clear_free(in);
601             break;
602         case OSSL_FUNC_CRYPTO_REALLOC:
603             c_CRYPTO_realloc = OSSL_get_CRYPTO_realloc(in);
604             break;
605         case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
606             c_CRYPTO_clear_realloc = OSSL_get_CRYPTO_clear_realloc(in);
607             break;
608         case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
609             c_CRYPTO_secure_malloc = OSSL_get_CRYPTO_secure_malloc(in);
610             break;
611         case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
612             c_CRYPTO_secure_zalloc = OSSL_get_CRYPTO_secure_zalloc(in);
613             break;
614         case OSSL_FUNC_CRYPTO_SECURE_FREE:
615             c_CRYPTO_secure_free = OSSL_get_CRYPTO_secure_free(in);
616             break;
617         case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
618             c_CRYPTO_secure_clear_free = OSSL_get_CRYPTO_secure_clear_free(in);
619             break;
620         case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
621             c_CRYPTO_secure_allocated = OSSL_get_CRYPTO_secure_allocated(in);
622             break;
623         case OSSL_FUNC_BIO_NEW_FILE:
624             selftest_params.bio_new_file_cb = OSSL_get_BIO_new_file(in);
625             break;
626         case OSSL_FUNC_BIO_NEW_MEMBUF:
627             selftest_params.bio_new_buffer_cb = OSSL_get_BIO_new_membuf(in);
628             break;
629         case OSSL_FUNC_BIO_READ_EX:
630             selftest_params.bio_read_ex_cb = OSSL_get_BIO_read_ex(in);
631             break;
632         case OSSL_FUNC_BIO_FREE:
633             selftest_params.bio_free_cb = OSSL_get_BIO_free(in);
634             break;
635         case OSSL_FUNC_BIO_VSNPRINTF:
636             c_BIO_vsnprintf = OSSL_get_BIO_vsnprintf(in);
637             break;
638         case OSSL_FUNC_SELF_TEST_CB: {
639             stcbfn = OSSL_get_self_test_cb(in);
640             break;
641         }
642         default:
643             /* Just ignore anything we don't understand */
644             break;
645         }
646     }
647
648     if (stcbfn != NULL && c_get_libctx != NULL) {
649         stcbfn(c_get_libctx(provider), &selftest_params.cb,
650                &selftest_params.cb_arg);
651     }
652     else {
653         selftest_params.cb = NULL;
654         selftest_params.cb_arg = NULL;
655     }
656
657     if (!c_get_params(provider, core_params))
658         return 0;
659
660     /*  Create a context. */
661     if ((*provctx = PROV_CTX_new()) == NULL
662         || (libctx = OPENSSL_CTX_new()) == NULL) {
663         /*
664          * We free libctx separately here and only here because it hasn't
665          * been attached to *provctx.  All other error paths below rely
666          * solely on fips_teardown.
667          */
668         OPENSSL_CTX_free(libctx);
669         goto err;
670     }
671     PROV_CTX_set0_library_context(*provctx, libctx);
672     PROV_CTX_set0_provider(*provctx, provider);
673
674     if ((fgbl = openssl_ctx_get_data(libctx, OPENSSL_CTX_FIPS_PROV_INDEX,
675                                      &fips_prov_ossl_ctx_method)) == NULL)
676         goto err;
677
678     fgbl->prov = provider;
679
680     selftest_params.libctx = libctx;
681     if (!SELF_TEST_post(&selftest_params, 0))
682         goto err;
683
684     /*
685      * TODO(3.0): Remove me. This is just a dummy call to demonstrate making
686      * EVP calls from within the FIPS module.
687      */
688     if (!dummy_evp_call(libctx))
689         goto err;
690
691     *out = fips_dispatch_table;
692
693     return 1;
694  err:
695     fips_teardown(*provctx);
696     *provctx = NULL;
697     return 0;
698 }
699
700 /*
701  * The internal init function used when the FIPS module uses EVP to call
702  * another algorithm also in the FIPS module. This is a recursive call that has
703  * been made from within the FIPS module itself. To make this work, we populate
704  * the provider context of this inner instance with the same library context
705  * that was used in the EVP call that initiated this recursive call.
706  */
707 OSSL_provider_init_fn fips_intern_provider_init;
708 int fips_intern_provider_init(const OSSL_PROVIDER *provider,
709                               const OSSL_DISPATCH *in,
710                               const OSSL_DISPATCH **out,
711                               void **provctx)
712 {
713     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
714
715     for (; in->function_id != 0; in++) {
716         switch (in->function_id) {
717         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
718             c_get_libctx = OSSL_get_core_get_library_context(in);
719             break;
720         default:
721             break;
722         }
723     }
724
725     if (c_get_libctx == NULL)
726         return 0;
727
728     if ((*provctx = PROV_CTX_new()) == NULL)
729         return 0;
730     PROV_CTX_set0_library_context(*provctx, c_get_libctx(provider));
731     PROV_CTX_set0_provider(*provctx, provider);
732
733     *out = intern_dispatch_table;
734     return 1;
735 }
736
737 void ERR_new(void)
738 {
739     c_new_error(NULL);
740 }
741
742 void ERR_set_debug(const char *file, int line, const char *func)
743 {
744     c_set_error_debug(NULL, file, line, func);
745 }
746
747 void ERR_set_error(int lib, int reason, const char *fmt, ...)
748 {
749     va_list args;
750
751     va_start(args, fmt);
752     c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
753     va_end(args);
754 }
755
756 void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
757 {
758     c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
759 }
760
761 int ERR_set_mark(void)
762 {
763     return c_set_error_mark(NULL);
764 }
765
766 int ERR_clear_last_mark(void)
767 {
768     return c_clear_last_error_mark(NULL);
769 }
770
771 int ERR_pop_to_mark(void)
772 {
773     return c_pop_error_to_mark(NULL);
774 }
775
776 /*
777  * This must take a library context, since it's called from the depths
778  * of crypto/initthread.c code, where it's (correctly) assumed that the
779  * passed caller argument is an OPENSSL_CTX pointer (since the same routine
780  * is also called from other parts of libcrypto, which all pass around a
781  * OPENSSL_CTX pointer)
782  */
783 const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *libctx)
784 {
785     FIPS_GLOBAL *fgbl = openssl_ctx_get_data(libctx,
786                                              OPENSSL_CTX_FIPS_PROV_INDEX,
787                                              &fips_prov_ossl_ctx_method);
788
789     if (fgbl == NULL)
790         return NULL;
791
792     return fgbl->prov;
793 }
794
795 void *CRYPTO_malloc(size_t num, const char *file, int line)
796 {
797     return c_CRYPTO_malloc(num, file, line);
798 }
799
800 void *CRYPTO_zalloc(size_t num, const char *file, int line)
801 {
802     return c_CRYPTO_zalloc(num, file, line);
803 }
804
805 void CRYPTO_free(void *ptr, const char *file, int line)
806 {
807     c_CRYPTO_free(ptr, file, line);
808 }
809
810 void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
811 {
812     c_CRYPTO_clear_free(ptr, num, file, line);
813 }
814
815 void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
816 {
817     return c_CRYPTO_realloc(addr, num, file, line);
818 }
819
820 void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
821                            const char *file, int line)
822 {
823     return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
824 }
825
826 void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
827 {
828     return c_CRYPTO_secure_malloc(num, file, line);
829 }
830
831 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
832 {
833     return c_CRYPTO_secure_zalloc(num, file, line);
834 }
835
836 void CRYPTO_secure_free(void *ptr, const char *file, int line)
837 {
838     c_CRYPTO_secure_free(ptr, file, line);
839 }
840
841 void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
842 {
843     c_CRYPTO_secure_clear_free(ptr, num, file, line);
844 }
845
846 int CRYPTO_secure_allocated(const void *ptr)
847 {
848     return c_CRYPTO_secure_allocated(ptr);
849 }
850
851 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
852 {
853     va_list args;
854     int ret;
855
856     va_start(args, format);
857     ret = c_BIO_vsnprintf(buf, n, format, args);
858     va_end(args);
859     return ret;
860 }