xref: /freebsd/crypto/openssl/crypto/property/property_query.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2021 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 "internal/propertyerr.h"
11 #include "internal/property.h"
12 #include "property_local.h"
13 
property_idx_cmp(const void * keyp,const void * compare)14 static int property_idx_cmp(const void *keyp, const void *compare)
15 {
16     OSSL_PROPERTY_IDX key = *(const OSSL_PROPERTY_IDX *)keyp;
17     const OSSL_PROPERTY_DEFINITION *defn = (const OSSL_PROPERTY_DEFINITION *)compare;
18 
19     return key - defn->name_idx;
20 }
21 
22 const OSSL_PROPERTY_DEFINITION *
ossl_property_find_property(const OSSL_PROPERTY_LIST * list,OSSL_LIB_CTX * libctx,const char * name)23 ossl_property_find_property(const OSSL_PROPERTY_LIST *list,
24     OSSL_LIB_CTX *libctx, const char *name)
25 {
26     OSSL_PROPERTY_IDX name_idx;
27 
28     if (list == NULL || name == NULL
29         || (name_idx = ossl_property_name(libctx, name, 0)) == 0)
30         return NULL;
31 
32     return ossl_bsearch(&name_idx, list->properties, list->num_properties,
33         sizeof(*list->properties), &property_idx_cmp, 0);
34 }
35 
ossl_property_get_type(const OSSL_PROPERTY_DEFINITION * prop)36 OSSL_PROPERTY_TYPE ossl_property_get_type(const OSSL_PROPERTY_DEFINITION *prop)
37 {
38     return prop->type;
39 }
40 
ossl_property_get_string_value(OSSL_LIB_CTX * libctx,const OSSL_PROPERTY_DEFINITION * prop)41 const char *ossl_property_get_string_value(OSSL_LIB_CTX *libctx,
42     const OSSL_PROPERTY_DEFINITION *prop)
43 {
44     const char *value = NULL;
45 
46     if (prop != NULL && prop->type == OSSL_PROPERTY_TYPE_STRING)
47         value = ossl_property_value_str(libctx, prop->v.str_val);
48     return value;
49 }
50 
ossl_property_get_number_value(const OSSL_PROPERTY_DEFINITION * prop)51 int64_t ossl_property_get_number_value(const OSSL_PROPERTY_DEFINITION *prop)
52 {
53     int64_t value = 0;
54 
55     if (prop != NULL && prop->type == OSSL_PROPERTY_TYPE_NUMBER)
56         value = prop->v.int_val;
57     return value;
58 }
59 
60 /* Does a property query have any optional clauses */
ossl_property_has_optional(const OSSL_PROPERTY_LIST * query)61 int ossl_property_has_optional(const OSSL_PROPERTY_LIST *query)
62 {
63     return query->has_optional ? 1 : 0;
64 }
65 
ossl_property_is_enabled(OSSL_LIB_CTX * ctx,const char * property_name,const OSSL_PROPERTY_LIST * prop_list)66 int ossl_property_is_enabled(OSSL_LIB_CTX *ctx, const char *property_name,
67     const OSSL_PROPERTY_LIST *prop_list)
68 {
69     const OSSL_PROPERTY_DEFINITION *prop;
70 
71     prop = ossl_property_find_property(prop_list, ctx, property_name);
72     /* Do a separate check for override as it does not set type */
73     if (prop == NULL || prop->optional || prop->oper == OSSL_PROPERTY_OVERRIDE)
74         return 0;
75     return (prop->type == OSSL_PROPERTY_TYPE_STRING
76         && ((prop->oper == OSSL_PROPERTY_OPER_EQ
77                 && prop->v.str_val == OSSL_PROPERTY_TRUE)
78             || (prop->oper == OSSL_PROPERTY_OPER_NE
79                 && prop->v.str_val != OSSL_PROPERTY_TRUE)));
80 }
81