1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include "internal/propertyerr.h"
11e1051a39Sopenharmony_ci#include "internal/property.h"
12e1051a39Sopenharmony_ci#include "property_local.h"
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_cistatic int property_idx_cmp(const void *keyp, const void *compare)
15e1051a39Sopenharmony_ci{
16e1051a39Sopenharmony_ci    OSSL_PROPERTY_IDX key = *(const OSSL_PROPERTY_IDX *)keyp;
17e1051a39Sopenharmony_ci    const OSSL_PROPERTY_DEFINITION *defn =
18e1051a39Sopenharmony_ci            (const OSSL_PROPERTY_DEFINITION *)compare;
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ci    return key - defn->name_idx;
21e1051a39Sopenharmony_ci}
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ciconst OSSL_PROPERTY_DEFINITION *
24e1051a39Sopenharmony_ciossl_property_find_property(const OSSL_PROPERTY_LIST *list,
25e1051a39Sopenharmony_ci                            OSSL_LIB_CTX *libctx, const char *name)
26e1051a39Sopenharmony_ci{
27e1051a39Sopenharmony_ci    OSSL_PROPERTY_IDX name_idx;
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci    if (list == NULL || name == NULL
30e1051a39Sopenharmony_ci        || (name_idx = ossl_property_name(libctx, name, 0)) == 0)
31e1051a39Sopenharmony_ci        return NULL;
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ci    return ossl_bsearch(&name_idx, list->properties, list->num_properties,
34e1051a39Sopenharmony_ci                        sizeof(*list->properties), &property_idx_cmp, 0);
35e1051a39Sopenharmony_ci}
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_ciOSSL_PROPERTY_TYPE ossl_property_get_type(const OSSL_PROPERTY_DEFINITION *prop)
38e1051a39Sopenharmony_ci{
39e1051a39Sopenharmony_ci    return prop->type;
40e1051a39Sopenharmony_ci}
41e1051a39Sopenharmony_ci
42e1051a39Sopenharmony_ciconst char *ossl_property_get_string_value(OSSL_LIB_CTX *libctx,
43e1051a39Sopenharmony_ci                                           const OSSL_PROPERTY_DEFINITION *prop)
44e1051a39Sopenharmony_ci{
45e1051a39Sopenharmony_ci    const char *value = NULL;
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ci    if (prop != NULL && prop->type == OSSL_PROPERTY_TYPE_STRING)
48e1051a39Sopenharmony_ci        value = ossl_property_value_str(libctx, prop->v.str_val);
49e1051a39Sopenharmony_ci    return value;
50e1051a39Sopenharmony_ci}
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ciint64_t ossl_property_get_number_value(const OSSL_PROPERTY_DEFINITION *prop)
53e1051a39Sopenharmony_ci{
54e1051a39Sopenharmony_ci    int64_t value = 0;
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci    if (prop != NULL && prop->type == OSSL_PROPERTY_TYPE_NUMBER)
57e1051a39Sopenharmony_ci        value = prop->v.int_val;
58e1051a39Sopenharmony_ci    return value;
59e1051a39Sopenharmony_ci}
60e1051a39Sopenharmony_ci
61e1051a39Sopenharmony_ci/* Does a property query have any optional clauses */
62e1051a39Sopenharmony_ciint ossl_property_has_optional(const OSSL_PROPERTY_LIST *query)
63e1051a39Sopenharmony_ci{
64e1051a39Sopenharmony_ci    return query->has_optional ? 1 : 0;
65e1051a39Sopenharmony_ci}
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ciint ossl_property_is_enabled(OSSL_LIB_CTX *ctx,  const char *property_name,
68e1051a39Sopenharmony_ci                             const OSSL_PROPERTY_LIST *prop_list)
69e1051a39Sopenharmony_ci{
70e1051a39Sopenharmony_ci    const OSSL_PROPERTY_DEFINITION *prop;
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_ci    prop = ossl_property_find_property(prop_list, ctx, property_name);
73e1051a39Sopenharmony_ci    /* Do a separate check for override as it does not set type */
74e1051a39Sopenharmony_ci    if (prop == NULL || prop->optional || prop->oper == OSSL_PROPERTY_OVERRIDE)
75e1051a39Sopenharmony_ci        return 0;
76e1051a39Sopenharmony_ci    return (prop->type == OSSL_PROPERTY_TYPE_STRING
77e1051a39Sopenharmony_ci            && ((prop->oper == OSSL_PROPERTY_OPER_EQ
78e1051a39Sopenharmony_ci                     && prop->v.str_val == OSSL_PROPERTY_TRUE)
79e1051a39Sopenharmony_ci                 || (prop->oper == OSSL_PROPERTY_OPER_NE
80e1051a39Sopenharmony_ci                     && prop->v.str_val != OSSL_PROPERTY_TRUE)));
81e1051a39Sopenharmony_ci}
82e1051a39Sopenharmony_ci
83