Path: blob/main/crypto/openssl/providers/fips/fipsindicator.c
48261 views
/*1* Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/indicator.h>10#include <openssl/params.h>11#include <openssl/core_names.h>12#include "internal/common.h" /* for ossl_assert() */13#include "fips/fipsindicator.h"1415void ossl_FIPS_IND_init(OSSL_FIPS_IND *ind)16{17int i;1819ossl_FIPS_IND_set_approved(ind); /* Assume we are approved by default */20for (i = 0; i < OSSL_FIPS_IND_SETTABLE_MAX; i++)21ind->settable[i] = OSSL_FIPS_IND_STATE_UNKNOWN;22}2324void ossl_FIPS_IND_set_approved(OSSL_FIPS_IND *ind)25{26ind->approved = 1;27}2829void ossl_FIPS_IND_copy(OSSL_FIPS_IND *dst, const OSSL_FIPS_IND *src)30{31*dst = *src;32}3334void ossl_FIPS_IND_set_settable(OSSL_FIPS_IND *ind, int id, int state)35{36if (!ossl_assert(id < OSSL_FIPS_IND_SETTABLE_MAX))37return;38if (!ossl_assert(state == OSSL_FIPS_IND_STATE_STRICT39|| state == OSSL_FIPS_IND_STATE_TOLERANT))40return;41ind->settable[id] = state;42}4344int ossl_FIPS_IND_get_settable(const OSSL_FIPS_IND *ind, int id)45{46if (!ossl_assert(id < OSSL_FIPS_IND_SETTABLE_MAX))47return OSSL_FIPS_IND_STATE_UNKNOWN;48return ind->settable[id];49}5051/*52* This should only be called when a strict FIPS algorithm check fails.53* It assumes that we are in strict mode by default.54* If the logic here is not sufficient for all cases, then additional55* ossl_FIPS_IND_on_unapproved() functions may be required.56*/57int ossl_FIPS_IND_on_unapproved(OSSL_FIPS_IND *ind, int id,58OSSL_LIB_CTX *libctx,59const char *algname, const char *opname,60OSSL_FIPS_IND_CHECK_CB *config_check_fn)61{62/* Set to unapproved. Once unapproved mode is set this will not be reset */63ind->approved = 0;6465/*66* We only trigger the indicator callback if the ctx variable is cleared OR67* the configurable item is cleared. If the values are unknown they are68* assumed to be strict.69*/70if (ossl_FIPS_IND_get_settable(ind, id) == OSSL_FIPS_IND_STATE_TOLERANT71|| (config_check_fn != NULL72&& config_check_fn(libctx) == OSSL_FIPS_IND_STATE_TOLERANT)) {73return ossl_FIPS_IND_callback(libctx, algname, opname);74}75/* Strict mode gets here: This returns an error */76return 0;77}7879int ossl_FIPS_IND_set_ctx_param(OSSL_FIPS_IND *ind, int id,80const OSSL_PARAM params[], const char *name)81{82int in = 0;83const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);8485if (p != NULL) {86if (!OSSL_PARAM_get_int(p, &in))87return 0;88ossl_FIPS_IND_set_settable(ind, id, in);89}90return 1;91}9293int ossl_FIPS_IND_get_ctx_param(const OSSL_FIPS_IND *ind, OSSL_PARAM params[])94{95OSSL_PARAM *p = OSSL_PARAM_locate(params, OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR);9697return p == NULL || OSSL_PARAM_set_int(p, ind->approved);98}99100/*101* Can be used during application testing to log that an indicator was102* triggered. The callback will return 1 if the application wants an error103* to occur based on the indicator type and description.104*/105int ossl_FIPS_IND_callback(OSSL_LIB_CTX *libctx, const char *type,106const char *desc)107{108OSSL_INDICATOR_CALLBACK *cb = NULL;109110OSSL_INDICATOR_get_callback(libctx, &cb);111if (cb == NULL)112return 1;113114return cb(type, desc, NULL);115}116117118