2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
14 #include <openssl/err.h>
15 #include "internal/propertyerr.h"
16 #include "internal/property.h"
17 #include "crypto/ctype.h"
18 #include "internal/nelem.h"
19 #include "property_local.h"
20 #include "internal/e_os.h"
22 DEFINE_STACK_OF(OSSL_PROPERTY_DEFINITION)
24 static const char *skip_space(const char *s)
26 while (ossl_isspace(*s))
31 static int match_ch(const char *t[], char m)
36 *t = skip_space(s + 1);
42 #define MATCH(s, m) match(s, m, sizeof(m) - 1)
44 static int match(const char *t[], const char m[], size_t m_len)
48 if (OPENSSL_strncasecmp(s, m, m_len) == 0) {
49 *t = skip_space(s + m_len);
55 static int parse_name(OSSL_LIB_CTX *ctx, const char *t[], int create,
56 OSSL_PROPERTY_IDX *idx)
65 if (!ossl_isalpha(*s)) {
66 ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_IDENTIFIER,
71 if (i < sizeof(name) - 1)
72 name[i++] = ossl_tolower(*s);
75 } while (*++s == '_' || ossl_isalnum(*s));
79 if (i < sizeof(name) - 1)
87 ERR_raise_data(ERR_LIB_PROP, PROP_R_NAME_TOO_LONG, "HERE-->%s", *t);
91 *idx = ossl_property_name(ctx, name, user_name && create);
95 static int parse_number(const char *t[], OSSL_PROPERTY_DEFINITION *res)
100 if (!ossl_isdigit(*s))
103 v = v * 10 + (*s++ - '0');
104 } while (ossl_isdigit(*s));
105 if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
106 ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT,
111 res->type = OSSL_PROPERTY_TYPE_NUMBER;
116 static int parse_hex(const char *t[], OSSL_PROPERTY_DEFINITION *res)
121 if (!ossl_isxdigit(*s))
125 if (ossl_isdigit(*s))
128 v += ossl_tolower(*s) - 'a';
129 } while (ossl_isxdigit(*++s));
130 if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
131 ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT,
136 res->type = OSSL_PROPERTY_TYPE_NUMBER;
141 static int parse_oct(const char *t[], OSSL_PROPERTY_DEFINITION *res)
146 if (*s == '9' || *s == '8' || !ossl_isdigit(*s))
149 v = (v << 3) + (*s - '0');
150 } while (ossl_isdigit(*++s) && *s != '9' && *s != '8');
151 if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
152 ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT,
157 res->type = OSSL_PROPERTY_TYPE_NUMBER;
162 static int parse_string(OSSL_LIB_CTX *ctx, const char *t[], char delim,
163 OSSL_PROPERTY_DEFINITION *res, const int create)
170 while (*s != '\0' && *s != delim) {
171 if (i < sizeof(v) - 1)
178 ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_MATCHING_STRING_DELIMITER,
179 "HERE-->%c%s", delim, *t);
184 ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
186 res->v.str_val = ossl_property_value(ctx, v, create);
188 *t = skip_space(s + 1);
189 res->type = OSSL_PROPERTY_TYPE_STRING;
193 static int parse_unquoted(OSSL_LIB_CTX *ctx, const char *t[],
194 OSSL_PROPERTY_DEFINITION *res, const int create)
201 if (*s == '\0' || *s == ',')
203 while (ossl_isprint(*s) && !ossl_isspace(*s) && *s != ',') {
204 if (i < sizeof(v) - 1)
205 v[i++] = ossl_tolower(*s);
210 if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
211 ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_ASCII_CHARACTER,
217 ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
219 res->v.str_val = ossl_property_value(ctx, v, create);
222 res->type = OSSL_PROPERTY_TYPE_STRING;
226 static int parse_value(OSSL_LIB_CTX *ctx, const char *t[],
227 OSSL_PROPERTY_DEFINITION *res, int create)
232 if (*s == '"' || *s == '\'') {
234 r = parse_string(ctx, &s, s[-1], res, create);
235 } else if (*s == '+') {
237 r = parse_number(&s, res);
238 } else if (*s == '-') {
240 r = parse_number(&s, res);
241 res->v.int_val = -res->v.int_val;
242 } else if (*s == '0' && s[1] == 'x') {
244 r = parse_hex(&s, res);
245 } else if (*s == '0' && ossl_isdigit(s[1])) {
247 r = parse_oct(&s, res);
248 } else if (ossl_isdigit(*s)) {
249 return parse_number(t, res);
250 } else if (ossl_isalpha(*s))
251 return parse_unquoted(ctx, t, res, create);
257 static int pd_compare(const OSSL_PROPERTY_DEFINITION *const *p1,
258 const OSSL_PROPERTY_DEFINITION *const *p2)
260 const OSSL_PROPERTY_DEFINITION *pd1 = *p1;
261 const OSSL_PROPERTY_DEFINITION *pd2 = *p2;
263 if (pd1->name_idx < pd2->name_idx)
265 if (pd1->name_idx > pd2->name_idx)
270 static void pd_free(OSSL_PROPERTY_DEFINITION *pd)
276 * Convert a stack of property definitions and queries into a fixed array.
277 * The items are sorted for efficient query. The stack is not freed.
278 * This function also checks for duplicated names and returns an error if
281 static OSSL_PROPERTY_LIST *
282 stack_to_property_list(OSSL_LIB_CTX *ctx,
283 STACK_OF(OSSL_PROPERTY_DEFINITION) *sk)
285 const int n = sk_OSSL_PROPERTY_DEFINITION_num(sk);
286 OSSL_PROPERTY_LIST *r;
287 OSSL_PROPERTY_IDX prev_name_idx = 0;
290 r = OPENSSL_malloc(sizeof(*r)
291 + (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0]));
293 sk_OSSL_PROPERTY_DEFINITION_sort(sk);
296 for (i = 0; i < n; i++) {
297 r->properties[i] = *sk_OSSL_PROPERTY_DEFINITION_value(sk, i);
298 r->has_optional |= r->properties[i].optional;
300 /* Check for duplicated names */
301 if (i > 0 && r->properties[i].name_idx == prev_name_idx) {
303 ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
304 "Duplicated name `%s'",
305 ossl_property_name_str(ctx, prev_name_idx));
308 prev_name_idx = r->properties[i].name_idx;
310 r->num_properties = n;
315 OSSL_PROPERTY_LIST *ossl_parse_property(OSSL_LIB_CTX *ctx, const char *defn)
317 OSSL_PROPERTY_DEFINITION *prop = NULL;
318 OSSL_PROPERTY_LIST *res = NULL;
319 STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
320 const char *s = defn;
323 if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
329 const char *start = s;
331 prop = OPENSSL_malloc(sizeof(*prop));
334 memset(&prop->v, 0, sizeof(prop->v));
336 if (!parse_name(ctx, &s, 1, &prop->name_idx))
338 prop->oper = OSSL_PROPERTY_OPER_EQ;
339 if (prop->name_idx == 0) {
340 ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
341 "Unknown name HERE-->%s", start);
344 if (match_ch(&s, '=')) {
345 if (!parse_value(ctx, &s, prop, 1)) {
346 ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_VALUE,
351 /* A name alone means a true Boolean */
352 prop->type = OSSL_PROPERTY_TYPE_STRING;
353 prop->v.str_val = OSSL_PROPERTY_TRUE;
356 if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
359 done = !match_ch(&s, ',');
362 ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS,
366 res = stack_to_property_list(ctx, sk);
370 sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
374 OSSL_PROPERTY_LIST *ossl_parse_query(OSSL_LIB_CTX *ctx, const char *s,
377 STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
378 OSSL_PROPERTY_LIST *res = NULL;
379 OSSL_PROPERTY_DEFINITION *prop = NULL;
382 if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
388 prop = OPENSSL_malloc(sizeof(*prop));
391 memset(&prop->v, 0, sizeof(prop->v));
393 if (match_ch(&s, '-')) {
394 prop->oper = OSSL_PROPERTY_OVERRIDE;
396 if (!parse_name(ctx, &s, 1, &prop->name_idx))
400 prop->optional = match_ch(&s, '?');
401 if (!parse_name(ctx, &s, 1, &prop->name_idx))
404 if (match_ch(&s, '=')) {
405 prop->oper = OSSL_PROPERTY_OPER_EQ;
406 } else if (MATCH(&s, "!=")) {
407 prop->oper = OSSL_PROPERTY_OPER_NE;
409 /* A name alone is a Boolean comparison for true */
410 prop->oper = OSSL_PROPERTY_OPER_EQ;
411 prop->type = OSSL_PROPERTY_TYPE_STRING;
412 prop->v.str_val = OSSL_PROPERTY_TRUE;
415 if (!parse_value(ctx, &s, prop, create_values))
416 prop->type = OSSL_PROPERTY_TYPE_VALUE_UNDEFINED;
419 if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
422 done = !match_ch(&s, ',');
425 ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS,
429 res = stack_to_property_list(ctx, sk);
433 sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
438 * Compare a query against a definition.
439 * Return the number of clauses matched or -1 if a mandatory clause is false.
441 int ossl_property_match_count(const OSSL_PROPERTY_LIST *query,
442 const OSSL_PROPERTY_LIST *defn)
444 const OSSL_PROPERTY_DEFINITION *const q = query->properties;
445 const OSSL_PROPERTY_DEFINITION *const d = defn->properties;
446 int i = 0, j = 0, matches = 0;
447 OSSL_PROPERTY_OPER oper;
449 while (i < query->num_properties) {
450 if ((oper = q[i].oper) == OSSL_PROPERTY_OVERRIDE) {
454 if (j < defn->num_properties) {
455 if (q[i].name_idx > d[j].name_idx) { /* skip defn, not in query */
459 if (q[i].name_idx == d[j].name_idx) { /* both in defn and query */
460 const int eq = q[i].type == d[j].type
461 && memcmp(&q[i].v, &d[j].v, sizeof(q[i].v)) == 0;
463 if ((eq && oper == OSSL_PROPERTY_OPER_EQ)
464 || (!eq && oper == OSSL_PROPERTY_OPER_NE))
466 else if (!q[i].optional)
475 * Handle the cases of a missing value and a query with no corresponding
476 * definition. The former fails for any comparison except inequality,
477 * the latter is treated as a comparison against the Boolean false.
479 if (q[i].type == OSSL_PROPERTY_TYPE_VALUE_UNDEFINED) {
480 if (oper == OSSL_PROPERTY_OPER_NE)
482 else if (!q[i].optional)
484 } else if (q[i].type != OSSL_PROPERTY_TYPE_STRING
485 || (oper == OSSL_PROPERTY_OPER_EQ
486 && q[i].v.str_val != OSSL_PROPERTY_FALSE)
487 || (oper == OSSL_PROPERTY_OPER_NE
488 && q[i].v.str_val == OSSL_PROPERTY_FALSE)) {
499 void ossl_property_free(OSSL_PROPERTY_LIST *p)
505 * Merge two property lists.
506 * If there is a common name, the one from the first list is used.
508 OSSL_PROPERTY_LIST *ossl_property_merge(const OSSL_PROPERTY_LIST *a,
509 const OSSL_PROPERTY_LIST *b)
511 const OSSL_PROPERTY_DEFINITION *const ap = a->properties;
512 const OSSL_PROPERTY_DEFINITION *const bp = b->properties;
513 const OSSL_PROPERTY_DEFINITION *copy;
514 OSSL_PROPERTY_LIST *r;
516 const int t = a->num_properties + b->num_properties;
518 r = OPENSSL_malloc(sizeof(*r)
519 + (t == 0 ? 0 : t - 1) * sizeof(r->properties[0]));
524 for (i = j = n = 0; i < a->num_properties || j < b->num_properties; n++) {
525 if (i >= a->num_properties) {
527 } else if (j >= b->num_properties) {
529 } else if (ap[i].name_idx <= bp[j].name_idx) {
530 if (ap[i].name_idx == bp[j].name_idx)
536 memcpy(r->properties + n, copy, sizeof(r->properties[0]));
537 r->has_optional |= copy->optional;
539 r->num_properties = n;
541 r = OPENSSL_realloc(r, sizeof(*r) + (n - 1) * sizeof(r->properties[0]));
545 int ossl_property_parse_init(OSSL_LIB_CTX *ctx)
547 static const char *const predefined_names[] = {
548 "provider", /* Name of provider (default, legacy, fips) */
549 "version", /* Version number of this provider */
550 "fips", /* FIPS validated or FIPS supporting algorithm */
551 "output", /* Output type for encoders */
552 "input", /* Input type for decoders */
553 "structure", /* Structure name for encoders and decoders */
557 for (i = 0; i < OSSL_NELEM(predefined_names); i++)
558 if (ossl_property_name(ctx, predefined_names[i], 1) == 0)
562 * Pre-populate the two Boolean values. We must do them before any other
563 * values and in this order so that we get the same index as the global
564 * OSSL_PROPERTY_TRUE and OSSL_PROPERTY_FALSE values
566 if ((ossl_property_value(ctx, "yes", 1) != OSSL_PROPERTY_TRUE)
567 || (ossl_property_value(ctx, "no", 1) != OSSL_PROPERTY_FALSE))
575 static void put_char(char ch, char **buf, size_t *remain, size_t *needed)
590 static void put_str(const char *str, char **buf, size_t *remain, size_t *needed)
594 len = olen = strlen(str);
600 if (*remain < len + 1)
604 strncpy(*buf, str, len);
609 if (len < olen && *remain == 1) {
616 static void put_num(int64_t val, char **buf, size_t *remain, size_t *needed)
618 int64_t tmpval = val;
625 for (; tmpval > 9; len++, tmpval /= 10);
632 BIO_snprintf(*buf, *remain, "%lld", (long long int)val);
642 size_t ossl_property_list_to_string(OSSL_LIB_CTX *ctx,
643 const OSSL_PROPERTY_LIST *list, char *buf,
647 const OSSL_PROPERTY_DEFINITION *prop = NULL;
656 if (list->num_properties != 0)
657 prop = &list->properties[list->num_properties - 1];
658 for (i = 0; i < list->num_properties; i++, prop--) {
659 /* Skip invalid names */
660 if (prop->name_idx == 0)
664 put_char(',', &buf, &bufsize, &needed);
667 put_char('?', &buf, &bufsize, &needed);
668 else if (prop->oper == OSSL_PROPERTY_OVERRIDE)
669 put_char('-', &buf, &bufsize, &needed);
671 val = ossl_property_name_str(ctx, prop->name_idx);
674 put_str(val, &buf, &bufsize, &needed);
676 switch (prop->oper) {
677 case OSSL_PROPERTY_OPER_NE:
678 put_char('!', &buf, &bufsize, &needed);
680 case OSSL_PROPERTY_OPER_EQ:
681 put_char('=', &buf, &bufsize, &needed);
683 switch (prop->type) {
684 case OSSL_PROPERTY_TYPE_STRING:
685 val = ossl_property_value_str(ctx, prop->v.str_val);
688 put_str(val, &buf, &bufsize, &needed);
691 case OSSL_PROPERTY_TYPE_NUMBER:
692 put_num(prop->v.int_val, &buf, &bufsize, &needed);
705 put_char('\0', &buf, &bufsize, &needed);