2 * Copyright 2019 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 "internal/ctype.h"
18 #include "internal/nelem.h"
19 #include "property_lcl.h"
23 PROPERTY_TYPE_STRING, PROPERTY_TYPE_NUMBER,
24 PROPERTY_TYPE_VALUE_UNDEFINED
28 PROPERTY_OPER_EQ, PROPERTY_OPER_NE, PROPERTY_OVERRIDE
32 OSSL_PROPERTY_IDX name_idx;
36 int64_t int_val; /* Signed integer */
37 OSSL_PROPERTY_IDX str_val; /* String */
39 } PROPERTY_DEFINITION;
41 struct ossl_property_list_st {
43 PROPERTY_DEFINITION properties[1];
46 static OSSL_PROPERTY_IDX ossl_property_true, ossl_property_false;
48 DEFINE_STACK_OF(PROPERTY_DEFINITION)
50 static const char *skip_space(const char *s)
52 while (ossl_isspace(*s))
57 static int match_ch(const char *t[], char m)
62 *t = skip_space(s + 1);
68 #define MATCH(s, m) match(s, m, sizeof(m) - 1)
70 static int match(const char *t[], const char m[], size_t m_len)
74 if (strncasecmp(s, m, m_len) == 0) {
75 *t = skip_space(s + m_len);
81 static int parse_name(OPENSSL_CTX *ctx, const char *t[], int create,
82 OSSL_PROPERTY_IDX *idx)
91 if (!ossl_isalpha(*s)) {
92 PROPerr(PROP_F_PARSE_NAME, PROP_R_NOT_AN_IDENTIFIER);
96 if (i < sizeof(name) - 1)
97 name[i++] = ossl_tolower(*s);
100 } while (*++s == '_' || ossl_isalnum(*s));
104 if (i < sizeof(name) - 1)
113 *idx = ossl_property_name(ctx, name, user_name && create);
116 PROPerr(PROP_F_PARSE_NAME, PROP_R_NAME_TOO_LONG);
120 static int parse_number(const char *t[], PROPERTY_DEFINITION *res)
125 if (!ossl_isdigit(*s))
128 v = v * 10 + (*s++ - '0');
129 } while (ossl_isdigit(*s));
130 if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
131 PROPerr(PROP_F_PARSE_NUMBER, PROP_R_NOT_A_DECIMAL_DIGIT);
135 res->type = PROPERTY_TYPE_NUMBER;
140 static int parse_hex(const char *t[], PROPERTY_DEFINITION *res)
145 if (!ossl_isxdigit(*s))
149 if (ossl_isdigit(*s))
152 v += ossl_tolower(*s) - 'a';
153 } while (ossl_isxdigit(*++s));
154 if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
155 PROPerr(PROP_F_PARSE_HEX, PROP_R_NOT_AN_HEXADECIMAL_DIGIT);
159 res->type = PROPERTY_TYPE_NUMBER;
164 static int parse_oct(const char *t[], PROPERTY_DEFINITION *res)
169 if (*s == '9' || *s == '8' || !ossl_isdigit(*s))
172 v = (v << 3) + (*s - '0');
173 } while (ossl_isdigit(*++s) && *s != '9' && *s != '8');
174 if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
175 PROPerr(PROP_F_PARSE_OCT, PROP_R_NOT_AN_OCTAL_DIGIT);
179 res->type = PROPERTY_TYPE_NUMBER;
184 static int parse_string(OPENSSL_CTX *ctx, const char *t[], char delim,
185 PROPERTY_DEFINITION *res, const int create)
192 while (*s != '\0' && *s != delim) {
193 if (i < sizeof(v) - 1)
200 PROPerr(PROP_F_PARSE_STRING,
201 PROP_R_NO_MATCHING_STRING_DELIMETER);
205 *t = skip_space(s + 1);
207 PROPerr(PROP_F_PARSE_STRING, PROP_R_STRING_TOO_LONG);
209 res->v.str_val = ossl_property_value(ctx, v, create);
210 res->type = PROPERTY_TYPE_STRING;
214 static int parse_unquoted(OPENSSL_CTX *ctx, const char *t[],
215 PROPERTY_DEFINITION *res, const int create)
222 if (*s == '\0' || *s == ',')
224 while (ossl_isprint(*s) && !ossl_isspace(*s) && *s != ',') {
225 if (i < sizeof(v) - 1)
226 v[i++] = ossl_tolower(*s);
231 if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
232 PROPerr(PROP_F_PARSE_UNQUOTED, PROP_R_NOT_AN_ASCII_CHARACTER);
238 PROPerr(PROP_F_PARSE_UNQUOTED, PROP_R_STRING_TOO_LONG);
240 res->v.str_val = ossl_property_value(ctx, v, create);
241 res->type = PROPERTY_TYPE_STRING;
245 static int parse_value(OPENSSL_CTX *ctx, const char *t[],
246 PROPERTY_DEFINITION *res, int create)
251 if (*s == '"' || *s == '\'') {
253 r = parse_string(ctx, &s, s[-1], res, create);
254 } else if (*s == '+') {
256 r = parse_number(&s, res);
257 } else if (*s == '-') {
259 r = parse_number(&s, res);
260 res->v.int_val = -res->v.int_val;
261 } else if (*s == '0' && s[1] == 'x') {
263 r = parse_hex(&s, res);
264 } else if (*s == '0' && ossl_isdigit(s[1])) {
266 r = parse_oct(&s, res);
267 } else if (ossl_isdigit(*s)) {
268 return parse_number(t, res);
269 } else if (ossl_isalpha(*s))
270 return parse_unquoted(ctx, t, res, create);
276 static int pd_compare(const PROPERTY_DEFINITION *const *p1,
277 const PROPERTY_DEFINITION *const *p2)
279 const PROPERTY_DEFINITION *pd1 = *p1;
280 const PROPERTY_DEFINITION *pd2 = *p2;
282 if (pd1->name_idx < pd2->name_idx)
284 if (pd1->name_idx > pd2->name_idx)
289 static void pd_free(PROPERTY_DEFINITION *pd)
295 * Convert a stack of property definitions and queries into a fixed array.
296 * The items are sorted for efficient query. The stack is not freed.
298 static OSSL_PROPERTY_LIST *stack_to_property_list(STACK_OF(PROPERTY_DEFINITION)
301 const int n = sk_PROPERTY_DEFINITION_num(sk);
302 OSSL_PROPERTY_LIST *r;
305 r = OPENSSL_malloc(sizeof(*r)
306 + (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0]));
308 sk_PROPERTY_DEFINITION_sort(sk);
310 for (i = 0; i < n; i++)
311 r->properties[i] = *sk_PROPERTY_DEFINITION_value(sk, i);
317 OSSL_PROPERTY_LIST *ossl_parse_property(OPENSSL_CTX *ctx, const char *defn)
319 PROPERTY_DEFINITION *prop = NULL;
320 OSSL_PROPERTY_LIST *res = NULL;
321 STACK_OF(PROPERTY_DEFINITION) *sk;
322 const char *s = defn;
325 if (s == NULL || (sk = sk_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
331 prop = OPENSSL_malloc(sizeof(*prop));
334 memset(&prop->v, 0, sizeof(prop->v));
335 if (!parse_name(ctx, &s, 1, &prop->name_idx))
337 prop->oper = PROPERTY_OPER_EQ;
338 if (prop->name_idx == 0) {
339 PROPerr(PROP_F_OSSL_PARSE_PROPERTY, PROP_R_PARSE_FAILED);
342 if (match_ch(&s, '=')) {
343 if (!parse_value(ctx, &s, prop, 1)) {
344 PROPerr(PROP_F_OSSL_PARSE_PROPERTY, PROP_R_NO_VALUE);
348 /* A name alone means a true Boolean */
349 prop->type = PROPERTY_TYPE_STRING;
350 prop->v.str_val = ossl_property_true;
353 if (!sk_PROPERTY_DEFINITION_push(sk, prop))
356 done = !match_ch(&s, ',');
359 PROPerr(PROP_F_OSSL_PARSE_PROPERTY, PROP_R_TRAILING_CHARACTERS);
362 res = stack_to_property_list(sk);
366 sk_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
370 OSSL_PROPERTY_LIST *ossl_parse_query(OPENSSL_CTX *ctx, const char *s)
372 STACK_OF(PROPERTY_DEFINITION) *sk;
373 OSSL_PROPERTY_LIST *res = NULL;
374 PROPERTY_DEFINITION *prop = NULL;
377 if (s == NULL || (sk = sk_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
383 prop = OPENSSL_malloc(sizeof(*prop));
386 memset(&prop->v, 0, sizeof(prop->v));
388 if (match_ch(&s, '-')) {
389 prop->oper = PROPERTY_OVERRIDE;
390 if (!parse_name(ctx, &s, 0, &prop->name_idx))
394 if (!parse_name(ctx, &s, 0, &prop->name_idx))
397 if (match_ch(&s, '=')) {
398 prop->oper = PROPERTY_OPER_EQ;
399 } else if (MATCH(&s, "!=")) {
400 prop->oper = PROPERTY_OPER_NE;
402 /* A name alone is a Boolean comparison for true */
403 prop->oper = PROPERTY_OPER_EQ;
404 prop->type = PROPERTY_TYPE_STRING;
405 prop->v.str_val = ossl_property_true;
408 if (!parse_value(ctx, &s, prop, 0))
409 prop->type = PROPERTY_TYPE_VALUE_UNDEFINED;
412 if (!sk_PROPERTY_DEFINITION_push(sk, prop))
415 done = !match_ch(&s, ',');
418 PROPerr(PROP_F_OSSL_PARSE_QUERY, PROP_R_TRAILING_CHARACTERS);
421 res = stack_to_property_list(sk);
425 sk_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
429 int ossl_property_match(const OSSL_PROPERTY_LIST *query,
430 const OSSL_PROPERTY_LIST *defn)
432 const PROPERTY_DEFINITION *const q = query->properties;
433 const PROPERTY_DEFINITION *const d = defn->properties;
437 while (i < query->n) {
438 if ((oper = q[i].oper) == PROPERTY_OVERRIDE) {
443 if (q[i].name_idx > d[j].name_idx) { /* skip defn, not in query */
447 if (q[i].name_idx == d[j].name_idx) { /* both in defn and query */
448 const int eq = q[i].type == d[j].type
449 && memcmp(&q[i].v, &d[j].v, sizeof(q[i].v)) == 0;
451 if ((eq && oper != PROPERTY_OPER_EQ)
452 || (!eq && oper != PROPERTY_OPER_NE))
461 * Handle the cases of a missing value and a query with no corresponding
462 * definition. The former fails for any comparision except inequality,
463 * the latter is treated as a comparison against the Boolean false.
465 if (q[i].type == PROPERTY_TYPE_VALUE_UNDEFINED) {
466 if (oper != PROPERTY_OPER_NE)
468 } else if (q[i].type != PROPERTY_TYPE_STRING
469 || (oper == PROPERTY_OPER_EQ
470 && q[i].v.str_val != ossl_property_false)
471 || (oper == PROPERTY_OPER_NE
472 && q[i].v.str_val == ossl_property_false)) {
480 void ossl_property_free(OSSL_PROPERTY_LIST *p)
486 * Merge two property lists.
487 * If there is a common name, the one from the first list is used.
489 OSSL_PROPERTY_LIST *ossl_property_merge(const OSSL_PROPERTY_LIST *a,
490 const OSSL_PROPERTY_LIST *b)
492 const PROPERTY_DEFINITION *const ap = a->properties;
493 const PROPERTY_DEFINITION *const bp = b->properties;
494 const PROPERTY_DEFINITION *copy;
495 OSSL_PROPERTY_LIST *r;
497 const int t = a->n + b->n;
499 r = OPENSSL_malloc(sizeof(*r)
500 + (t == 0 ? 0 : t - 1) * sizeof(r->properties[0]));
504 for (i = j = n = 0; i < a->n || j < b->n; n++) {
507 } else if (j >= b->n) {
509 } else if (ap[i].name_idx <= bp[j].name_idx) {
510 if (ap[i].name_idx == bp[j].name_idx)
516 memcpy(r->properties + n, copy, sizeof(r->properties[0]));
520 r = OPENSSL_realloc(r, sizeof(*r) + (n - 1) * sizeof(r->properties[0]));
524 int ossl_property_parse_init(OPENSSL_CTX *ctx)
526 static const char *const predefined_names[] = {
527 "default", /* Being provided by the default built-in provider */
528 "legacy", /* Provided by the legacy provider */
529 "provider", /* Name of provider (default, fips) */
530 "version", /* Version number of this provider */
531 "fips", /* FIPS supporting provider */
532 "engine", /* An old style engine masquerading as a provider */
536 for (i = 0; i < OSSL_NELEM(predefined_names); i++)
537 if (ossl_property_name(ctx, predefined_names[i], 1) == 0)
540 /* Pre-populate the two Boolean values */
541 if ((ossl_property_true = ossl_property_value(ctx, "yes", 1)) == 0
542 || (ossl_property_false = ossl_property_value(ctx, "no", 1)) == 0)