2018-11-16 09:44:30 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2018-11-16 09:44:30 +08:00
|
|
|
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include "internal/propertyerr.h"
|
|
|
|
#include "internal/property.h"
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/ctype.h"
|
2018-11-16 09:44:30 +08:00
|
|
|
#include "internal/nelem.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "property_local.h"
|
2022-02-04 22:13:01 +08:00
|
|
|
#include "internal/e_os.h"
|
2018-11-16 09:44:30 +08:00
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
DEFINE_STACK_OF(OSSL_PROPERTY_DEFINITION)
|
2018-11-16 09:44:30 +08:00
|
|
|
|
|
|
|
static const char *skip_space(const char *s)
|
|
|
|
{
|
|
|
|
while (ossl_isspace(*s))
|
|
|
|
s++;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int match_ch(const char *t[], char m)
|
|
|
|
{
|
|
|
|
const char *s = *t;
|
|
|
|
|
|
|
|
if (*s == m) {
|
|
|
|
*t = skip_space(s + 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MATCH(s, m) match(s, m, sizeof(m) - 1)
|
|
|
|
|
|
|
|
static int match(const char *t[], const char m[], size_t m_len)
|
|
|
|
{
|
|
|
|
const char *s = *t;
|
|
|
|
|
2022-04-12 18:30:08 +08:00
|
|
|
if (OPENSSL_strncasecmp(s, m, m_len) == 0) {
|
2018-11-16 09:44:30 +08:00
|
|
|
*t = skip_space(s + m_len);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
static int parse_name(OSSL_LIB_CTX *ctx, const char *t[], int create,
|
2019-05-01 18:02:43 +08:00
|
|
|
OSSL_PROPERTY_IDX *idx)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
char name[100];
|
|
|
|
int err = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
const char *s = *t;
|
|
|
|
int user_name = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!ossl_isalpha(*s)) {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_IDENTIFIER,
|
|
|
|
"HERE-->%s", *t);
|
2018-11-16 09:44:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (i < sizeof(name) - 1)
|
|
|
|
name[i++] = ossl_tolower(*s);
|
|
|
|
else
|
|
|
|
err = 1;
|
|
|
|
} while (*++s == '_' || ossl_isalnum(*s));
|
|
|
|
if (*s != '.')
|
|
|
|
break;
|
|
|
|
user_name = 1;
|
|
|
|
if (i < sizeof(name) - 1)
|
|
|
|
name[i++] = *s;
|
|
|
|
else
|
|
|
|
err = 1;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
name[i] = '\0';
|
2019-06-19 14:37:19 +08:00
|
|
|
if (err) {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_NAME_TOO_LONG, "HERE-->%s", *t);
|
2019-06-19 14:37:19 +08:00
|
|
|
return 0;
|
2018-11-16 09:44:30 +08:00
|
|
|
}
|
2019-06-19 14:37:19 +08:00
|
|
|
*t = skip_space(s);
|
|
|
|
*idx = ossl_property_name(ctx, name, user_name && create);
|
|
|
|
return 1;
|
2018-11-16 09:44:30 +08:00
|
|
|
}
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
static int parse_number(const char *t[], OSSL_PROPERTY_DEFINITION *res)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
const char *s = *t;
|
|
|
|
int64_t v = 0;
|
|
|
|
|
|
|
|
if (!ossl_isdigit(*s))
|
|
|
|
return 0;
|
|
|
|
do {
|
|
|
|
v = v * 10 + (*s++ - '0');
|
|
|
|
} while (ossl_isdigit(*s));
|
|
|
|
if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT,
|
|
|
|
"HERE-->%s", *t);
|
2018-11-16 09:44:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*t = skip_space(s);
|
2021-06-02 02:02:24 +08:00
|
|
|
res->type = OSSL_PROPERTY_TYPE_NUMBER;
|
2018-11-16 09:44:30 +08:00
|
|
|
res->v.int_val = v;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
static int parse_hex(const char *t[], OSSL_PROPERTY_DEFINITION *res)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
const char *s = *t;
|
|
|
|
int64_t v = 0;
|
|
|
|
|
|
|
|
if (!ossl_isxdigit(*s))
|
|
|
|
return 0;
|
|
|
|
do {
|
|
|
|
v <<= 4;
|
|
|
|
if (ossl_isdigit(*s))
|
|
|
|
v += *s - '0';
|
|
|
|
else
|
|
|
|
v += ossl_tolower(*s) - 'a';
|
|
|
|
} while (ossl_isxdigit(*++s));
|
|
|
|
if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT,
|
|
|
|
"HERE-->%s", *t);
|
2018-11-16 09:44:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*t = skip_space(s);
|
2021-06-02 02:02:24 +08:00
|
|
|
res->type = OSSL_PROPERTY_TYPE_NUMBER;
|
2018-11-16 09:44:30 +08:00
|
|
|
res->v.int_val = v;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
static int parse_oct(const char *t[], OSSL_PROPERTY_DEFINITION *res)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
const char *s = *t;
|
|
|
|
int64_t v = 0;
|
|
|
|
|
|
|
|
if (*s == '9' || *s == '8' || !ossl_isdigit(*s))
|
|
|
|
return 0;
|
|
|
|
do {
|
|
|
|
v = (v << 3) + (*s - '0');
|
|
|
|
} while (ossl_isdigit(*++s) && *s != '9' && *s != '8');
|
|
|
|
if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT,
|
|
|
|
"HERE-->%s", *t);
|
2018-11-16 09:44:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*t = skip_space(s);
|
2021-06-02 02:02:24 +08:00
|
|
|
res->type = OSSL_PROPERTY_TYPE_NUMBER;
|
2018-11-16 09:44:30 +08:00
|
|
|
res->v.int_val = v;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
static int parse_string(OSSL_LIB_CTX *ctx, const char *t[], char delim,
|
2021-06-02 02:02:24 +08:00
|
|
|
OSSL_PROPERTY_DEFINITION *res, const int create)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
char v[1000];
|
|
|
|
const char *s = *t;
|
|
|
|
size_t i = 0;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
while (*s != '\0' && *s != delim) {
|
|
|
|
if (i < sizeof(v) - 1)
|
|
|
|
v[i++] = *s;
|
|
|
|
else
|
|
|
|
err = 1;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (*s == '\0') {
|
2019-12-01 07:18:47 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_MATCHING_STRING_DELIMITER,
|
2019-07-24 19:37:42 +08:00
|
|
|
"HERE-->%c%s", delim, *t);
|
2018-11-16 09:44:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
v[i] = '\0';
|
2019-06-19 14:37:19 +08:00
|
|
|
if (err) {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
|
2019-06-19 14:37:19 +08:00
|
|
|
} else {
|
2019-05-01 18:02:43 +08:00
|
|
|
res->v.str_val = ossl_property_value(ctx, v, create);
|
2019-06-19 14:37:19 +08:00
|
|
|
}
|
|
|
|
*t = skip_space(s + 1);
|
2021-06-02 02:02:24 +08:00
|
|
|
res->type = OSSL_PROPERTY_TYPE_STRING;
|
2018-11-16 09:44:30 +08:00
|
|
|
return !err;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
static int parse_unquoted(OSSL_LIB_CTX *ctx, const char *t[],
|
2021-06-02 02:02:24 +08:00
|
|
|
OSSL_PROPERTY_DEFINITION *res, const int create)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
char v[1000];
|
|
|
|
const char *s = *t;
|
|
|
|
size_t i = 0;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (*s == '\0' || *s == ',')
|
|
|
|
return 0;
|
|
|
|
while (ossl_isprint(*s) && !ossl_isspace(*s) && *s != ',') {
|
|
|
|
if (i < sizeof(v) - 1)
|
|
|
|
v[i++] = ossl_tolower(*s);
|
|
|
|
else
|
|
|
|
err = 1;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_ASCII_CHARACTER,
|
|
|
|
"HERE-->%s", s);
|
2018-11-16 09:44:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
v[i] = 0;
|
2022-06-07 20:16:30 +08:00
|
|
|
if (err)
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
|
2022-06-07 20:16:30 +08:00
|
|
|
else if ((res->v.str_val = ossl_property_value(ctx, v, create)) == 0)
|
|
|
|
err = 1;
|
2019-06-19 14:37:19 +08:00
|
|
|
*t = skip_space(s);
|
2021-06-02 02:02:24 +08:00
|
|
|
res->type = OSSL_PROPERTY_TYPE_STRING;
|
2018-11-16 09:44:30 +08:00
|
|
|
return !err;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
static int parse_value(OSSL_LIB_CTX *ctx, const char *t[],
|
2021-06-02 02:02:24 +08:00
|
|
|
OSSL_PROPERTY_DEFINITION *res, int create)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
const char *s = *t;
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
if (*s == '"' || *s == '\'') {
|
|
|
|
s++;
|
2019-05-01 18:02:43 +08:00
|
|
|
r = parse_string(ctx, &s, s[-1], res, create);
|
2018-11-16 09:44:30 +08:00
|
|
|
} else if (*s == '+') {
|
|
|
|
s++;
|
|
|
|
r = parse_number(&s, res);
|
|
|
|
} else if (*s == '-') {
|
|
|
|
s++;
|
|
|
|
r = parse_number(&s, res);
|
|
|
|
res->v.int_val = -res->v.int_val;
|
|
|
|
} else if (*s == '0' && s[1] == 'x') {
|
|
|
|
s += 2;
|
|
|
|
r = parse_hex(&s, res);
|
|
|
|
} else if (*s == '0' && ossl_isdigit(s[1])) {
|
|
|
|
s++;
|
|
|
|
r = parse_oct(&s, res);
|
|
|
|
} else if (ossl_isdigit(*s)) {
|
|
|
|
return parse_number(t, res);
|
|
|
|
} else if (ossl_isalpha(*s))
|
2019-05-01 18:02:43 +08:00
|
|
|
return parse_unquoted(ctx, t, res, create);
|
2018-11-16 09:44:30 +08:00
|
|
|
if (r)
|
|
|
|
*t = s;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
static int pd_compare(const OSSL_PROPERTY_DEFINITION *const *p1,
|
|
|
|
const OSSL_PROPERTY_DEFINITION *const *p2)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
2021-06-02 02:02:24 +08:00
|
|
|
const OSSL_PROPERTY_DEFINITION *pd1 = *p1;
|
|
|
|
const OSSL_PROPERTY_DEFINITION *pd2 = *p2;
|
2018-11-16 09:44:30 +08:00
|
|
|
|
|
|
|
if (pd1->name_idx < pd2->name_idx)
|
|
|
|
return -1;
|
|
|
|
if (pd1->name_idx > pd2->name_idx)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
static void pd_free(OSSL_PROPERTY_DEFINITION *pd)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
OPENSSL_free(pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a stack of property definitions and queries into a fixed array.
|
|
|
|
* The items are sorted for efficient query. The stack is not freed.
|
2021-09-30 09:33:37 +08:00
|
|
|
* This function also checks for duplicated names and returns an error if
|
|
|
|
* any exist.
|
2018-11-16 09:44:30 +08:00
|
|
|
*/
|
2021-06-02 02:02:24 +08:00
|
|
|
static OSSL_PROPERTY_LIST *
|
2021-09-30 09:33:37 +08:00
|
|
|
stack_to_property_list(OSSL_LIB_CTX *ctx,
|
|
|
|
STACK_OF(OSSL_PROPERTY_DEFINITION) *sk)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
2021-06-02 02:02:24 +08:00
|
|
|
const int n = sk_OSSL_PROPERTY_DEFINITION_num(sk);
|
2018-11-16 09:44:30 +08:00
|
|
|
OSSL_PROPERTY_LIST *r;
|
2021-09-30 09:33:37 +08:00
|
|
|
OSSL_PROPERTY_IDX prev_name_idx = 0;
|
2018-11-16 09:44:30 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
r = OPENSSL_malloc(sizeof(*r)
|
2019-02-24 19:26:39 +08:00
|
|
|
+ (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0]));
|
2018-11-16 09:44:30 +08:00
|
|
|
if (r != NULL) {
|
2021-06-02 02:02:24 +08:00
|
|
|
sk_OSSL_PROPERTY_DEFINITION_sort(sk);
|
2018-11-16 09:44:30 +08:00
|
|
|
|
2019-05-16 12:50:18 +08:00
|
|
|
r->has_optional = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
2021-06-02 02:02:24 +08:00
|
|
|
r->properties[i] = *sk_OSSL_PROPERTY_DEFINITION_value(sk, i);
|
2019-05-16 12:50:18 +08:00
|
|
|
r->has_optional |= r->properties[i].optional;
|
2021-09-30 09:33:37 +08:00
|
|
|
|
|
|
|
/* Check for duplicated names */
|
|
|
|
if (i > 0 && r->properties[i].name_idx == prev_name_idx) {
|
|
|
|
OPENSSL_free(r);
|
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
|
|
|
|
"Duplicated name `%s'",
|
|
|
|
ossl_property_name_str(ctx, prev_name_idx));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
prev_name_idx = r->properties[i].name_idx;
|
2019-05-16 12:50:18 +08:00
|
|
|
}
|
2021-06-04 11:19:23 +08:00
|
|
|
r->num_properties = n;
|
2018-11-16 09:44:30 +08:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_PROPERTY_LIST *ossl_parse_property(OSSL_LIB_CTX *ctx, const char *defn)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
2021-06-02 02:02:24 +08:00
|
|
|
OSSL_PROPERTY_DEFINITION *prop = NULL;
|
2018-11-16 09:44:30 +08:00
|
|
|
OSSL_PROPERTY_LIST *res = NULL;
|
2021-06-02 02:02:24 +08:00
|
|
|
STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
|
2018-11-16 09:44:30 +08:00
|
|
|
const char *s = defn;
|
|
|
|
int done;
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
|
2018-11-16 09:44:30 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s = skip_space(s);
|
|
|
|
done = *s == '\0';
|
|
|
|
while (!done) {
|
2019-06-19 14:37:19 +08:00
|
|
|
const char *start = s;
|
|
|
|
|
2018-11-16 09:44:30 +08:00
|
|
|
prop = OPENSSL_malloc(sizeof(*prop));
|
|
|
|
if (prop == NULL)
|
|
|
|
goto err;
|
|
|
|
memset(&prop->v, 0, sizeof(prop->v));
|
2019-05-16 12:50:18 +08:00
|
|
|
prop->optional = 0;
|
2019-05-01 18:02:43 +08:00
|
|
|
if (!parse_name(ctx, &s, 1, &prop->name_idx))
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
2021-06-02 02:02:24 +08:00
|
|
|
prop->oper = OSSL_PROPERTY_OPER_EQ;
|
2018-11-16 09:44:30 +08:00
|
|
|
if (prop->name_idx == 0) {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
|
|
|
|
"Unknown name HERE-->%s", start);
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (match_ch(&s, '=')) {
|
2019-05-01 18:02:43 +08:00
|
|
|
if (!parse_value(ctx, &s, prop, 1)) {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_VALUE,
|
|
|
|
"HERE-->%s", start);
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* A name alone means a true Boolean */
|
2021-06-02 02:02:24 +08:00
|
|
|
prop->type = OSSL_PROPERTY_TYPE_STRING;
|
2021-11-05 21:29:41 +08:00
|
|
|
prop->v.str_val = OSSL_PROPERTY_TRUE;
|
2018-11-16 09:44:30 +08:00
|
|
|
}
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
prop = NULL;
|
|
|
|
done = !match_ch(&s, ',');
|
|
|
|
}
|
|
|
|
if (*s != '\0') {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS,
|
|
|
|
"HERE-->%s", s);
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2021-09-30 09:33:37 +08:00
|
|
|
res = stack_to_property_list(ctx, sk);
|
2018-11-16 09:44:30 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
OPENSSL_free(prop);
|
2021-06-02 02:02:24 +08:00
|
|
|
sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
|
2018-11-16 09:44:30 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:34:49 +08:00
|
|
|
OSSL_PROPERTY_LIST *ossl_parse_query(OSSL_LIB_CTX *ctx, const char *s,
|
|
|
|
int create_values)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
2021-06-02 02:02:24 +08:00
|
|
|
STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
|
2018-11-16 09:44:30 +08:00
|
|
|
OSSL_PROPERTY_LIST *res = NULL;
|
2021-06-02 02:02:24 +08:00
|
|
|
OSSL_PROPERTY_DEFINITION *prop = NULL;
|
2018-11-16 09:44:30 +08:00
|
|
|
int done;
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
|
2018-11-16 09:44:30 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s = skip_space(s);
|
|
|
|
done = *s == '\0';
|
|
|
|
while (!done) {
|
|
|
|
prop = OPENSSL_malloc(sizeof(*prop));
|
|
|
|
if (prop == NULL)
|
|
|
|
goto err;
|
|
|
|
memset(&prop->v, 0, sizeof(prop->v));
|
|
|
|
|
|
|
|
if (match_ch(&s, '-')) {
|
2021-06-02 02:02:24 +08:00
|
|
|
prop->oper = OSSL_PROPERTY_OVERRIDE;
|
2019-05-16 12:50:18 +08:00
|
|
|
prop->optional = 0;
|
2021-05-11 07:48:22 +08:00
|
|
|
if (!parse_name(ctx, &s, 1, &prop->name_idx))
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
goto skip_value;
|
|
|
|
}
|
2019-05-16 12:50:18 +08:00
|
|
|
prop->optional = match_ch(&s, '?');
|
2021-05-11 07:48:22 +08:00
|
|
|
if (!parse_name(ctx, &s, 1, &prop->name_idx))
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (match_ch(&s, '=')) {
|
2021-06-02 02:02:24 +08:00
|
|
|
prop->oper = OSSL_PROPERTY_OPER_EQ;
|
2018-11-16 09:44:30 +08:00
|
|
|
} else if (MATCH(&s, "!=")) {
|
2021-06-02 02:02:24 +08:00
|
|
|
prop->oper = OSSL_PROPERTY_OPER_NE;
|
2018-11-16 09:44:30 +08:00
|
|
|
} else {
|
|
|
|
/* A name alone is a Boolean comparison for true */
|
2021-06-02 02:02:24 +08:00
|
|
|
prop->oper = OSSL_PROPERTY_OPER_EQ;
|
|
|
|
prop->type = OSSL_PROPERTY_TYPE_STRING;
|
2021-11-05 21:29:41 +08:00
|
|
|
prop->v.str_val = OSSL_PROPERTY_TRUE;
|
2018-11-16 09:44:30 +08:00
|
|
|
goto skip_value;
|
|
|
|
}
|
2021-03-13 08:34:49 +08:00
|
|
|
if (!parse_value(ctx, &s, prop, create_values))
|
2021-06-02 02:02:24 +08:00
|
|
|
prop->type = OSSL_PROPERTY_TYPE_VALUE_UNDEFINED;
|
2018-11-16 09:44:30 +08:00
|
|
|
|
|
|
|
skip_value:
|
2021-06-02 02:02:24 +08:00
|
|
|
if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
prop = NULL;
|
|
|
|
done = !match_ch(&s, ',');
|
|
|
|
}
|
|
|
|
if (*s != '\0') {
|
2019-07-24 19:37:42 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS,
|
|
|
|
"HERE-->%s", s);
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2021-09-30 09:33:37 +08:00
|
|
|
res = stack_to_property_list(ctx, sk);
|
2018-11-16 09:44:30 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
OPENSSL_free(prop);
|
2021-06-02 02:02:24 +08:00
|
|
|
sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
|
2018-11-16 09:44:30 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-05-16 12:50:18 +08:00
|
|
|
/*
|
|
|
|
* Compare a query against a definition.
|
|
|
|
* Return the number of clauses matched or -1 if a mandatory clause is false.
|
|
|
|
*/
|
|
|
|
int ossl_property_match_count(const OSSL_PROPERTY_LIST *query,
|
|
|
|
const OSSL_PROPERTY_LIST *defn)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
2021-06-02 02:02:24 +08:00
|
|
|
const OSSL_PROPERTY_DEFINITION *const q = query->properties;
|
|
|
|
const OSSL_PROPERTY_DEFINITION *const d = defn->properties;
|
2019-05-16 12:50:18 +08:00
|
|
|
int i = 0, j = 0, matches = 0;
|
2021-06-02 02:02:24 +08:00
|
|
|
OSSL_PROPERTY_OPER oper;
|
2018-11-16 09:44:30 +08:00
|
|
|
|
2021-06-04 11:19:23 +08:00
|
|
|
while (i < query->num_properties) {
|
2021-06-02 02:02:24 +08:00
|
|
|
if ((oper = q[i].oper) == OSSL_PROPERTY_OVERRIDE) {
|
2018-11-16 09:44:30 +08:00
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-04 11:19:23 +08:00
|
|
|
if (j < defn->num_properties) {
|
2018-11-16 09:44:30 +08:00
|
|
|
if (q[i].name_idx > d[j].name_idx) { /* skip defn, not in query */
|
|
|
|
j++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (q[i].name_idx == d[j].name_idx) { /* both in defn and query */
|
|
|
|
const int eq = q[i].type == d[j].type
|
|
|
|
&& memcmp(&q[i].v, &d[j].v, sizeof(q[i].v)) == 0;
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
if ((eq && oper == OSSL_PROPERTY_OPER_EQ)
|
|
|
|
|| (!eq && oper == OSSL_PROPERTY_OPER_NE))
|
2019-05-16 12:50:18 +08:00
|
|
|
matches++;
|
|
|
|
else if (!q[i].optional)
|
|
|
|
return -1;
|
2018-11-16 09:44:30 +08:00
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle the cases of a missing value and a query with no corresponding
|
2019-12-01 07:18:47 +08:00
|
|
|
* definition. The former fails for any comparison except inequality,
|
2018-11-16 09:44:30 +08:00
|
|
|
* the latter is treated as a comparison against the Boolean false.
|
|
|
|
*/
|
2021-06-02 02:02:24 +08:00
|
|
|
if (q[i].type == OSSL_PROPERTY_TYPE_VALUE_UNDEFINED) {
|
|
|
|
if (oper == OSSL_PROPERTY_OPER_NE)
|
2019-05-16 12:50:18 +08:00
|
|
|
matches++;
|
|
|
|
else if (!q[i].optional)
|
|
|
|
return -1;
|
2021-06-02 02:02:24 +08:00
|
|
|
} else if (q[i].type != OSSL_PROPERTY_TYPE_STRING
|
|
|
|
|| (oper == OSSL_PROPERTY_OPER_EQ
|
2021-11-05 21:29:41 +08:00
|
|
|
&& q[i].v.str_val != OSSL_PROPERTY_FALSE)
|
2021-06-02 02:02:24 +08:00
|
|
|
|| (oper == OSSL_PROPERTY_OPER_NE
|
2021-11-05 21:29:41 +08:00
|
|
|
&& q[i].v.str_val == OSSL_PROPERTY_FALSE)) {
|
2019-05-16 12:50:18 +08:00
|
|
|
if (!q[i].optional)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
matches++;
|
2018-11-16 09:44:30 +08:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2019-05-16 12:50:18 +08:00
|
|
|
return matches;
|
2018-11-16 09:44:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ossl_property_free(OSSL_PROPERTY_LIST *p)
|
|
|
|
{
|
|
|
|
OPENSSL_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Merge two property lists.
|
|
|
|
* If there is a common name, the one from the first list is used.
|
|
|
|
*/
|
|
|
|
OSSL_PROPERTY_LIST *ossl_property_merge(const OSSL_PROPERTY_LIST *a,
|
|
|
|
const OSSL_PROPERTY_LIST *b)
|
|
|
|
{
|
2021-06-02 02:02:24 +08:00
|
|
|
const OSSL_PROPERTY_DEFINITION *const ap = a->properties;
|
|
|
|
const OSSL_PROPERTY_DEFINITION *const bp = b->properties;
|
|
|
|
const OSSL_PROPERTY_DEFINITION *copy;
|
2018-11-16 09:44:30 +08:00
|
|
|
OSSL_PROPERTY_LIST *r;
|
|
|
|
int i, j, n;
|
2021-06-04 11:19:23 +08:00
|
|
|
const int t = a->num_properties + b->num_properties;
|
2018-11-16 09:44:30 +08:00
|
|
|
|
|
|
|
r = OPENSSL_malloc(sizeof(*r)
|
|
|
|
+ (t == 0 ? 0 : t - 1) * sizeof(r->properties[0]));
|
|
|
|
if (r == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2020-06-15 08:07:50 +08:00
|
|
|
r->has_optional = 0;
|
2021-06-04 11:19:23 +08:00
|
|
|
for (i = j = n = 0; i < a->num_properties || j < b->num_properties; n++) {
|
|
|
|
if (i >= a->num_properties) {
|
2018-11-16 09:44:30 +08:00
|
|
|
copy = &bp[j++];
|
2021-06-04 11:19:23 +08:00
|
|
|
} else if (j >= b->num_properties) {
|
2018-11-16 09:44:30 +08:00
|
|
|
copy = &ap[i++];
|
|
|
|
} else if (ap[i].name_idx <= bp[j].name_idx) {
|
|
|
|
if (ap[i].name_idx == bp[j].name_idx)
|
|
|
|
j++;
|
|
|
|
copy = &ap[i++];
|
|
|
|
} else {
|
|
|
|
copy = &bp[j++];
|
|
|
|
}
|
|
|
|
memcpy(r->properties + n, copy, sizeof(r->properties[0]));
|
2020-06-15 08:07:50 +08:00
|
|
|
r->has_optional |= copy->optional;
|
2018-11-16 09:44:30 +08:00
|
|
|
}
|
2021-06-04 11:19:23 +08:00
|
|
|
r->num_properties = n;
|
2018-11-16 09:44:30 +08:00
|
|
|
if (n != t)
|
|
|
|
r = OPENSSL_realloc(r, sizeof(*r) + (n - 1) * sizeof(r->properties[0]));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
int ossl_property_parse_init(OSSL_LIB_CTX *ctx)
|
2018-11-16 09:44:30 +08:00
|
|
|
{
|
|
|
|
static const char *const predefined_names[] = {
|
2020-02-15 06:49:26 +08:00
|
|
|
"provider", /* Name of provider (default, legacy, fips) */
|
2018-11-16 09:44:30 +08:00
|
|
|
"version", /* Version number of this provider */
|
2020-02-15 06:49:26 +08:00
|
|
|
"fips", /* FIPS validated or FIPS supporting algorithm */
|
2020-09-14 14:29:45 +08:00
|
|
|
"output", /* Output type for encoders */
|
|
|
|
"input", /* Input type for decoders */
|
|
|
|
"structure", /* Structure name for encoders and decoders */
|
2018-11-16 09:44:30 +08:00
|
|
|
};
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < OSSL_NELEM(predefined_names); i++)
|
2019-05-01 18:02:43 +08:00
|
|
|
if (ossl_property_name(ctx, predefined_names[i], 1) == 0)
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
|
2021-11-05 21:29:41 +08:00
|
|
|
/*
|
|
|
|
* Pre-populate the two Boolean values. We must do them before any other
|
|
|
|
* values and in this order so that we get the same index as the global
|
|
|
|
* OSSL_PROPERTY_TRUE and OSSL_PROPERTY_FALSE values
|
|
|
|
*/
|
|
|
|
if ((ossl_property_value(ctx, "yes", 1) != OSSL_PROPERTY_TRUE)
|
|
|
|
|| (ossl_property_value(ctx, "no", 1) != OSSL_PROPERTY_FALSE))
|
2018-11-16 09:44:30 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
err:
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-07 23:42:53 +08:00
|
|
|
|
|
|
|
static void put_char(char ch, char **buf, size_t *remain, size_t *needed)
|
|
|
|
{
|
|
|
|
if (*remain == 0) {
|
|
|
|
++*needed;
|
|
|
|
return;
|
|
|
|
}
|
2021-10-26 15:16:18 +08:00
|
|
|
if (*remain == 1)
|
2021-05-07 23:42:53 +08:00
|
|
|
**buf = '\0';
|
|
|
|
else
|
|
|
|
**buf = ch;
|
|
|
|
++*buf;
|
|
|
|
++*needed;
|
|
|
|
--*remain;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_str(const char *str, char **buf, size_t *remain, size_t *needed)
|
|
|
|
{
|
|
|
|
size_t olen, len;
|
|
|
|
|
|
|
|
len = olen = strlen(str);
|
|
|
|
*needed += len;
|
|
|
|
|
|
|
|
if (*remain == 0)
|
|
|
|
return;
|
|
|
|
|
2021-10-26 15:16:18 +08:00
|
|
|
if (*remain < len + 1)
|
2021-05-07 23:42:53 +08:00
|
|
|
len = *remain - 1;
|
|
|
|
|
2021-10-26 15:16:18 +08:00
|
|
|
if (len > 0) {
|
2022-06-22 18:36:02 +08:00
|
|
|
memcpy(*buf, str, len);
|
2021-05-07 23:42:53 +08:00
|
|
|
*buf += len;
|
|
|
|
*remain -= len;
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:16:18 +08:00
|
|
|
if (len < olen && *remain == 1) {
|
2021-05-07 23:42:53 +08:00
|
|
|
**buf = '\0';
|
|
|
|
++*buf;
|
|
|
|
--*remain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 09:19:30 +08:00
|
|
|
static void put_num(int64_t val, char **buf, size_t *remain, size_t *needed)
|
2021-05-07 23:42:53 +08:00
|
|
|
{
|
2021-05-21 09:19:30 +08:00
|
|
|
int64_t tmpval = val;
|
2021-05-07 23:42:53 +08:00
|
|
|
size_t len = 1;
|
|
|
|
|
2021-05-21 09:19:30 +08:00
|
|
|
if (tmpval < 0) {
|
|
|
|
len++;
|
|
|
|
tmpval = -tmpval;
|
|
|
|
}
|
2021-05-07 23:42:53 +08:00
|
|
|
for (; tmpval > 9; len++, tmpval /= 10);
|
|
|
|
|
|
|
|
*needed += len;
|
|
|
|
|
|
|
|
if (*remain == 0)
|
|
|
|
return;
|
|
|
|
|
2021-05-21 09:19:30 +08:00
|
|
|
BIO_snprintf(*buf, *remain, "%lld", (long long int)val);
|
2021-05-07 23:42:53 +08:00
|
|
|
if (*remain < len) {
|
|
|
|
*buf += *remain;
|
|
|
|
*remain = 0;
|
|
|
|
} else {
|
|
|
|
*buf += len;
|
|
|
|
*remain -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ossl_property_list_to_string(OSSL_LIB_CTX *ctx,
|
|
|
|
const OSSL_PROPERTY_LIST *list, char *buf,
|
|
|
|
size_t bufsize)
|
|
|
|
{
|
|
|
|
int i;
|
2021-06-02 02:02:24 +08:00
|
|
|
const OSSL_PROPERTY_DEFINITION *prop = NULL;
|
2021-05-07 23:42:53 +08:00
|
|
|
size_t needed = 0;
|
|
|
|
const char *val;
|
|
|
|
|
|
|
|
if (list == NULL) {
|
|
|
|
if (bufsize > 0)
|
|
|
|
*buf = '\0';
|
|
|
|
return 1;
|
|
|
|
}
|
2021-06-04 11:19:23 +08:00
|
|
|
if (list->num_properties != 0)
|
|
|
|
prop = &list->properties[list->num_properties - 1];
|
|
|
|
for (i = 0; i < list->num_properties; i++, prop--) {
|
2021-05-07 23:42:53 +08:00
|
|
|
/* Skip invalid names */
|
|
|
|
if (prop->name_idx == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (needed > 0)
|
|
|
|
put_char(',', &buf, &bufsize, &needed);
|
|
|
|
|
|
|
|
if (prop->optional)
|
|
|
|
put_char('?', &buf, &bufsize, &needed);
|
2021-06-02 02:02:24 +08:00
|
|
|
else if (prop->oper == OSSL_PROPERTY_OVERRIDE)
|
2021-05-07 23:42:53 +08:00
|
|
|
put_char('-', &buf, &bufsize, &needed);
|
|
|
|
|
|
|
|
val = ossl_property_name_str(ctx, prop->name_idx);
|
|
|
|
if (val == NULL)
|
|
|
|
return 0;
|
|
|
|
put_str(val, &buf, &bufsize, &needed);
|
|
|
|
|
|
|
|
switch (prop->oper) {
|
2021-06-02 02:02:24 +08:00
|
|
|
case OSSL_PROPERTY_OPER_NE:
|
2021-05-07 23:42:53 +08:00
|
|
|
put_char('!', &buf, &bufsize, &needed);
|
|
|
|
/* fall through */
|
2021-06-02 02:02:24 +08:00
|
|
|
case OSSL_PROPERTY_OPER_EQ:
|
2021-05-07 23:42:53 +08:00
|
|
|
put_char('=', &buf, &bufsize, &needed);
|
|
|
|
/* put value */
|
|
|
|
switch (prop->type) {
|
2021-06-02 02:02:24 +08:00
|
|
|
case OSSL_PROPERTY_TYPE_STRING:
|
2021-05-07 23:42:53 +08:00
|
|
|
val = ossl_property_value_str(ctx, prop->v.str_val);
|
|
|
|
if (val == NULL)
|
|
|
|
return 0;
|
|
|
|
put_str(val, &buf, &bufsize, &needed);
|
|
|
|
break;
|
|
|
|
|
2021-06-02 02:02:24 +08:00
|
|
|
case OSSL_PROPERTY_TYPE_NUMBER:
|
2021-05-07 23:42:53 +08:00
|
|
|
put_num(prop->v.int_val, &buf, &bufsize, &needed);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* do nothing */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put_char('\0', &buf, &bufsize, &needed);
|
|
|
|
return needed;
|
|
|
|
}
|