11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "internal/propertyerr.h"
111cb0ef41Sopenharmony_ci#include "internal/property.h"
121cb0ef41Sopenharmony_ci#include "property_local.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cistatic int property_idx_cmp(const void *keyp, const void *compare)
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci    OSSL_PROPERTY_IDX key = *(const OSSL_PROPERTY_IDX *)keyp;
171cb0ef41Sopenharmony_ci    const OSSL_PROPERTY_DEFINITION *defn =
181cb0ef41Sopenharmony_ci            (const OSSL_PROPERTY_DEFINITION *)compare;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    return key - defn->name_idx;
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst OSSL_PROPERTY_DEFINITION *
241cb0ef41Sopenharmony_ciossl_property_find_property(const OSSL_PROPERTY_LIST *list,
251cb0ef41Sopenharmony_ci                            OSSL_LIB_CTX *libctx, const char *name)
261cb0ef41Sopenharmony_ci{
271cb0ef41Sopenharmony_ci    OSSL_PROPERTY_IDX name_idx;
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    if (list == NULL || name == NULL
301cb0ef41Sopenharmony_ci        || (name_idx = ossl_property_name(libctx, name, 0)) == 0)
311cb0ef41Sopenharmony_ci        return NULL;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    return ossl_bsearch(&name_idx, list->properties, list->num_properties,
341cb0ef41Sopenharmony_ci                        sizeof(*list->properties), &property_idx_cmp, 0);
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciOSSL_PROPERTY_TYPE ossl_property_get_type(const OSSL_PROPERTY_DEFINITION *prop)
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci    return prop->type;
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciconst char *ossl_property_get_string_value(OSSL_LIB_CTX *libctx,
431cb0ef41Sopenharmony_ci                                           const OSSL_PROPERTY_DEFINITION *prop)
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci    const char *value = NULL;
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    if (prop != NULL && prop->type == OSSL_PROPERTY_TYPE_STRING)
481cb0ef41Sopenharmony_ci        value = ossl_property_value_str(libctx, prop->v.str_val);
491cb0ef41Sopenharmony_ci    return value;
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciint64_t ossl_property_get_number_value(const OSSL_PROPERTY_DEFINITION *prop)
531cb0ef41Sopenharmony_ci{
541cb0ef41Sopenharmony_ci    int64_t value = 0;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    if (prop != NULL && prop->type == OSSL_PROPERTY_TYPE_NUMBER)
571cb0ef41Sopenharmony_ci        value = prop->v.int_val;
581cb0ef41Sopenharmony_ci    return value;
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci/* Does a property query have any optional clauses */
621cb0ef41Sopenharmony_ciint ossl_property_has_optional(const OSSL_PROPERTY_LIST *query)
631cb0ef41Sopenharmony_ci{
641cb0ef41Sopenharmony_ci    return query->has_optional ? 1 : 0;
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciint ossl_property_is_enabled(OSSL_LIB_CTX *ctx,  const char *property_name,
681cb0ef41Sopenharmony_ci                             const OSSL_PROPERTY_LIST *prop_list)
691cb0ef41Sopenharmony_ci{
701cb0ef41Sopenharmony_ci    const OSSL_PROPERTY_DEFINITION *prop;
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    prop = ossl_property_find_property(prop_list, ctx, property_name);
731cb0ef41Sopenharmony_ci    /* Do a separate check for override as it does not set type */
741cb0ef41Sopenharmony_ci    if (prop == NULL || prop->optional || prop->oper == OSSL_PROPERTY_OVERRIDE)
751cb0ef41Sopenharmony_ci        return 0;
761cb0ef41Sopenharmony_ci    return (prop->type == OSSL_PROPERTY_TYPE_STRING
771cb0ef41Sopenharmony_ci            && ((prop->oper == OSSL_PROPERTY_OPER_EQ
781cb0ef41Sopenharmony_ci                     && prop->v.str_val == OSSL_PROPERTY_TRUE)
791cb0ef41Sopenharmony_ci                 || (prop->oper == OSSL_PROPERTY_OPER_NE
801cb0ef41Sopenharmony_ci                     && prop->v.str_val != OSSL_PROPERTY_TRUE)));
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
83