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);
218 else if ((res->v.str_val = ossl_property_value(ctx, v, create)) == 0)
221 res->type = OSSL_PROPERTY_TYPE_STRING;
225 static int parse_value(OSSL_LIB_CTX *ctx, const char *t[],
226 OSSL_PROPERTY_DEFINITION *res, int create)
231 if (*s == '"' || *s == '\'') {
233 r = parse_string(ctx, &s, s[-1], res, create);
234 } else if (*s == '+') {
236 r = parse_number(&s, res);
237 } else if (*s == '-') {
239 r = parse_number(&s, res);
240 res->v.int_val = -res->v.int_val;
241 } else if (*s == '0' && s[1] == 'x') {
243 r = parse_hex(&s, res);
244 } else if (*s == '0' && ossl_isdigit(s[1])) {
246 r = parse_oct(&s, res);
247 } else if (ossl_isdigit(*s)) {
248 return parse_number(t, res);
249 } else if (ossl_isalpha(*s))
250 return parse_unquoted(ctx, t, res, create);
256 static int pd_compare(const OSSL_PROPERTY_DEFINITION *const *p1,
257 const OSSL_PROPERTY_DEFINITION *const *p2)
259 const OSSL_PROPERTY_DEFINITION *pd1 = *p1;
260 const OSSL_PROPERTY_DEFINITION *pd2 = *p2;
262 if (pd1->name_idx < pd2->name_idx)
264 if (pd1->name_idx > pd2->name_idx)
269 static void pd_free(OSSL_PROPERTY_DEFINITION *pd)
275 * Convert a stack of property definitions and queries into a fixed array.
276 * The items are sorted for efficient query. The stack is not freed.
277 * This function also checks for duplicated names and returns an error if
280 static OSSL_PROPERTY_LIST *
281 stack_to_property_list(OSSL_LIB_CTX *ctx,
282 STACK_OF(OSSL_PROPERTY_DEFINITION) *sk)
284 const int n = sk_OSSL_PROPERTY_DEFINITION_num(sk);
285 OSSL_PROPERTY_LIST *r;
286 OSSL_PROPERTY_IDX prev_name_idx = 0;
289 r = OPENSSL_malloc(sizeof(*r)
290 + (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0]));
292 sk_OSSL_PROPERTY_DEFINITION_sort(sk);
295 for (i = 0; i < n; i++) {
296 r->properties[i] = *sk_OSSL_PROPERTY_DEFINITION_value(sk, i);
297 r->has_optional |= r->properties[i].optional;
299 /* Check for duplicated names */
300 if (i > 0 && r->properties[i].name_idx == prev_name_idx) {
302 ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
303 "Duplicated name `%s'",
304 ossl_property_name_str(ctx, prev_name_idx));
307 prev_name_idx = r->properties[i].name_idx;
309 r->num_properties = n;
314 OSSL_PROPERTY_LIST *ossl_parse_property(OSSL_LIB_CTX *ctx, const char *defn)
316 OSSL_PROPERTY_DEFINITION *prop = NULL;
317 OSSL_PROPERTY_LIST *res = NULL;
318 STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
319 const char *s = defn;
322 if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
328 const char *start = s;
330 prop = OPENSSL_malloc(sizeof(*prop));
333 memset(&prop->v, 0, sizeof(prop->v));
335 if (!parse_name(ctx, &s, 1, &prop->name_idx))
337 prop->oper = OSSL_PROPERTY_OPER_EQ;
338 if (prop->name_idx == 0) {
339 ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
340 "Unknown name HERE-->%s", start);
343 if (match_ch(&s, '=')) {
344 if (!parse_value(ctx, &s, prop, 1)) {
345 ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_VALUE,
350 /* A name alone means a true Boolean */
351 prop->type = OSSL_PROPERTY_TYPE_STRING;
352 prop->v.str_val = OSSL_PROPERTY_TRUE;
355 if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
358 done = !match_ch(&s, ',');
361 ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS,
365 res = stack_to_property_list(ctx, sk);
369 sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
373 OSSL_PROPERTY_LIST *ossl_parse_query(OSSL_LIB_CTX *ctx, const char *s,
376 STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
377 OSSL_PROPERTY_LIST *res = NULL;
378 OSSL_PROPERTY_DEFINITION *prop = NULL;
381 if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
387 prop = OPENSSL_malloc(sizeof(*prop));
390 memset(&prop->v, 0, sizeof(prop->v));
392 if (match_ch(&s, '-')) {
393 prop->oper = OSSL_PROPERTY_OVERRIDE;
395 if (!parse_name(ctx, &s, 1, &prop->name_idx))
399 prop->optional = match_ch(&s, '?');
400 if (!parse_name(ctx, &s, 1, &prop->name_idx))
403 if (match_ch(&s, '=')) {
404 prop->oper = OSSL_PROPERTY_OPER_EQ;
405 } else if (MATCH(&s, "!=")) {
406 prop->oper = OSSL_PROPERTY_OPER_NE;
408 /* A name alone is a Boolean comparison for true */
409 prop->oper = OSSL_PROPERTY_OPER_EQ;
410 prop->type = OSSL_PROPERTY_TYPE_STRING;
411 prop->v.str_val = OSSL_PROPERTY_TRUE;
414 if (!parse_value(ctx, &s, prop, create_values))
415 prop->type = OSSL_PROPERTY_TYPE_VALUE_UNDEFINED;
418 if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
421 done = !match_ch(&s, ',');
424 ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS,
428 res = stack_to_property_list(ctx, sk);
432 sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
437 * Compare a query against a definition.
438 * Return the number of clauses matched or -1 if a mandatory clause is false.
440 int ossl_property_match_count(const OSSL_PROPERTY_LIST *query,
441 const OSSL_PROPERTY_LIST *defn)
443 const OSSL_PROPERTY_DEFINITION *const q = query->properties;
444 const OSSL_PROPERTY_DEFINITION *const d = defn->properties;
445 int i = 0, j = 0, matches = 0;
446 OSSL_PROPERTY_OPER oper;
448 while (i < query->num_properties) {
449 if ((oper = q[i].oper) == OSSL_PROPERTY_OVERRIDE) {
453 if (j < defn->num_properties) {
454 if (q[i].name_idx > d[j].name_idx) { /* skip defn, not in query */
458 if (q[i].name_idx == d[j].name_idx) { /* both in defn and query */
459 const int eq = q[i].type == d[j].type
460 && memcmp(&q[i].v, &d[j].v, sizeof(q[i].v)) == 0;
462 if ((eq && oper == OSSL_PROPERTY_OPER_EQ)
463 || (!eq && oper == OSSL_PROPERTY_OPER_NE))
465 else if (!q[i].optional)
474 * Handle the cases of a missing value and a query with no corresponding
475 * definition. The former fails for any comparison except inequality,
476 * the latter is treated as a comparison against the Boolean false.
478 if (q[i].type == OSSL_PROPERTY_TYPE_VALUE_UNDEFINED) {
479 if (oper == OSSL_PROPERTY_OPER_NE)
481 else if (!q[i].optional)
483 } else if (q[i].type != OSSL_PROPERTY_TYPE_STRING
484 || (oper == OSSL_PROPERTY_OPER_EQ
485 && q[i].v.str_val != OSSL_PROPERTY_FALSE)
486 || (oper == OSSL_PROPERTY_OPER_NE
487 && q[i].v.str_val == OSSL_PROPERTY_FALSE)) {
498 void ossl_property_free(OSSL_PROPERTY_LIST *p)
504 * Merge two property lists.
505 * If there is a common name, the one from the first list is used.
507 OSSL_PROPERTY_LIST *ossl_property_merge(const OSSL_PROPERTY_LIST *a,
508 const OSSL_PROPERTY_LIST *b)
510 const OSSL_PROPERTY_DEFINITION *const ap = a->properties;
511 const OSSL_PROPERTY_DEFINITION *const bp = b->properties;
512 const OSSL_PROPERTY_DEFINITION *copy;
513 OSSL_PROPERTY_LIST *r;
515 const int t = a->num_properties + b->num_properties;
517 r = OPENSSL_malloc(sizeof(*r)
518 + (t == 0 ? 0 : t - 1) * sizeof(r->properties[0]));
523 for (i = j = n = 0; i < a->num_properties || j < b->num_properties; n++) {
524 if (i >= a->num_properties) {
526 } else if (j >= b->num_properties) {
528 } else if (ap[i].name_idx <= bp[j].name_idx) {
529 if (ap[i].name_idx == bp[j].name_idx)
535 memcpy(r->properties + n, copy, sizeof(r->properties[0]));
536 r->has_optional |= copy->optional;
538 r->num_properties = n;
540 r = OPENSSL_realloc(r, sizeof(*r) + (n - 1) * sizeof(r->properties[0]));
544 int ossl_property_parse_init(OSSL_LIB_CTX *ctx)
546 static const char *const predefined_names[] = {
547 "provider", /* Name of provider (default, legacy, fips) */
548 "version", /* Version number of this provider */
549 "fips", /* FIPS validated or FIPS supporting algorithm */
550 "output", /* Output type for encoders */
551 "input", /* Input type for decoders */
552 "structure", /* Structure name for encoders and decoders */
556 for (i = 0; i < OSSL_NELEM(predefined_names); i++)
557 if (ossl_property_name(ctx, predefined_names[i], 1) == 0)
561 * Pre-populate the two Boolean values. We must do them before any other
562 * values and in this order so that we get the same index as the global
563 * OSSL_PROPERTY_TRUE and OSSL_PROPERTY_FALSE values
565 if ((ossl_property_value(ctx, "yes", 1) != OSSL_PROPERTY_TRUE)
566 || (ossl_property_value(ctx, "no", 1) != OSSL_PROPERTY_FALSE))
574 static void put_char(char ch, char **buf, size_t *remain, size_t *needed)
589 static void put_str(const char *str, char **buf, size_t *remain, size_t *needed)
593 len = olen = strlen(str);
599 if (*remain < len + 1)
603 memcpy(*buf, str, len);
608 if (len < olen && *remain == 1) {
615 static void put_num(int64_t val, char **buf, size_t *remain, size_t *needed)
617 int64_t tmpval = val;
624 for (; tmpval > 9; len++, tmpval /= 10);
631 BIO_snprintf(*buf, *remain, "%lld", (long long int)val);
641 size_t ossl_property_list_to_string(OSSL_LIB_CTX *ctx,
642 const OSSL_PROPERTY_LIST *list, char *buf,
646 const OSSL_PROPERTY_DEFINITION *prop = NULL;
655 if (list->num_properties != 0)
656 prop = &list->properties[list->num_properties - 1];
657 for (i = 0; i < list->num_properties; i++, prop--) {
658 /* Skip invalid names */
659 if (prop->name_idx == 0)
663 put_char(',', &buf, &bufsize, &needed);
666 put_char('?', &buf, &bufsize, &needed);
667 else if (prop->oper == OSSL_PROPERTY_OVERRIDE)
668 put_char('-', &buf, &bufsize, &needed);
670 val = ossl_property_name_str(ctx, prop->name_idx);
673 put_str(val, &buf, &bufsize, &needed);
675 switch (prop->oper) {
676 case OSSL_PROPERTY_OPER_NE:
677 put_char('!', &buf, &bufsize, &needed);
679 case OSSL_PROPERTY_OPER_EQ:
680 put_char('=', &buf, &bufsize, &needed);
682 switch (prop->type) {
683 case OSSL_PROPERTY_TYPE_STRING:
684 val = ossl_property_value_str(ctx, prop->v.str_val);
687 put_str(val, &buf, &bufsize, &needed);
690 case OSSL_PROPERTY_TYPE_NUMBER:
691 put_num(prop->v.int_val, &buf, &bufsize, &needed);
704 put_char('\0', &buf, &bufsize, &needed);