/src/openssl30/crypto/property/property_parse.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
4 | | * |
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 |
9 | | */ |
10 | | |
11 | | #include <string.h> |
12 | | #include <stdio.h> |
13 | | #include <stdarg.h> |
14 | | #include <openssl/err.h> |
15 | | #include "internal/propertyerr.h" |
16 | | #include "internal/property.h" |
17 | | #include "internal/numbers.h" |
18 | | #include "crypto/ctype.h" |
19 | | #include "internal/nelem.h" |
20 | | #include "property_local.h" |
21 | | #include "e_os.h" |
22 | | |
23 | | DEFINE_STACK_OF(OSSL_PROPERTY_DEFINITION) |
24 | | |
25 | | static const char *skip_space(const char *s) |
26 | 6.13M | { |
27 | 6.13M | while (ossl_isspace(*s)) |
28 | 6.17k | s++; |
29 | 6.13M | return s; |
30 | 6.13M | } |
31 | | |
32 | | static int match_ch(const char *t[], char m) |
33 | 12.0M | { |
34 | 12.0M | const char *s = *t; |
35 | | |
36 | 12.0M | if (*s == m) { |
37 | 3.06M | *t = skip_space(s + 1); |
38 | 3.06M | return 1; |
39 | 3.06M | } |
40 | 9.01M | return 0; |
41 | 12.0M | } |
42 | | |
43 | 2.98M | #define MATCH(s, m) match(s, m, sizeof(m) - 1) |
44 | | |
45 | | static int match(const char *t[], const char m[], size_t m_len) |
46 | 2.98M | { |
47 | 2.98M | const char *s = *t; |
48 | | |
49 | 2.98M | if (OPENSSL_strncasecmp(s, m, m_len) == 0) { |
50 | 353 | *t = skip_space(s + m_len); |
51 | 353 | return 1; |
52 | 353 | } |
53 | 2.98M | return 0; |
54 | 2.98M | } |
55 | | |
56 | | static int parse_name(OSSL_LIB_CTX *ctx, const char *t[], int create, |
57 | | OSSL_PROPERTY_IDX *idx) |
58 | 3.02M | { |
59 | 3.02M | char name[100]; |
60 | 3.02M | int err = 0; |
61 | 3.02M | size_t i = 0; |
62 | 3.02M | const char *s = *t; |
63 | 3.02M | int user_name = 0; |
64 | | |
65 | 3.03M | for (;;) { |
66 | 3.03M | if (!ossl_isalpha(*s)) { |
67 | 317 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_IDENTIFIER, |
68 | 317 | "HERE-->%s", *t); |
69 | 317 | return 0; |
70 | 317 | } |
71 | 4.71M | do { |
72 | 4.71M | if (i < sizeof(name) - 1) |
73 | 4.71M | name[i++] = ossl_tolower(*s); |
74 | 442 | else |
75 | 442 | err = 1; |
76 | 4.71M | } while (*++s == '_' || ossl_isalnum(*s)); |
77 | 3.03M | if (*s != '.') |
78 | 3.02M | break; |
79 | 9.50k | user_name = 1; |
80 | 9.50k | if (i < sizeof(name) - 1) |
81 | 9.31k | name[i++] = *s; |
82 | 196 | else |
83 | 196 | err = 1; |
84 | 9.50k | s++; |
85 | 9.50k | } |
86 | 3.02M | name[i] = '\0'; |
87 | 3.02M | if (err) { |
88 | 32 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NAME_TOO_LONG, "HERE-->%s", *t); |
89 | 32 | return 0; |
90 | 32 | } |
91 | 3.02M | *t = skip_space(s); |
92 | 3.02M | *idx = ossl_property_name(ctx, name, user_name && create); |
93 | 3.02M | return 1; |
94 | 3.02M | } |
95 | | |
96 | | static int parse_number(const char *t[], OSSL_PROPERTY_DEFINITION *res) |
97 | 3.51k | { |
98 | 3.51k | const char *s = *t; |
99 | 3.51k | int64_t v = 0; |
100 | | |
101 | 6.45k | do { |
102 | 6.45k | if (!ossl_isdigit(*s)) { |
103 | 28 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT, |
104 | 28 | "HERE-->%s", *t); |
105 | 28 | return 0; |
106 | 28 | } |
107 | | /* overflow check */ |
108 | 6.42k | if (v > ((INT64_MAX - (*s - '0')) / 10)) { |
109 | 102 | ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, |
110 | 102 | "Property %s overflows", *t); |
111 | 102 | return 0; |
112 | 102 | } |
113 | 6.32k | v = v * 10 + (*s++ - '0'); |
114 | 6.32k | } while (ossl_isdigit(*s)); |
115 | 3.38k | if (!ossl_isspace(*s) && *s != '\0' && *s != ',') { |
116 | 87 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT, |
117 | 87 | "HERE-->%s", *t); |
118 | 87 | return 0; |
119 | 87 | } |
120 | 3.29k | *t = skip_space(s); |
121 | 3.29k | res->type = OSSL_PROPERTY_TYPE_NUMBER; |
122 | 3.29k | res->v.int_val = v; |
123 | 3.29k | return 1; |
124 | 3.38k | } |
125 | | |
126 | | static int parse_hex(const char *t[], OSSL_PROPERTY_DEFINITION *res) |
127 | 1.22k | { |
128 | 1.22k | const char *s = *t; |
129 | 1.22k | int64_t v = 0; |
130 | 1.22k | int sval; |
131 | | |
132 | 7.75k | do { |
133 | 7.75k | if (ossl_isdigit(*s)) { |
134 | 6.13k | sval = *s - '0'; |
135 | 6.13k | } else if (ossl_isxdigit(*s)) { |
136 | 1.58k | sval = ossl_tolower(*s) - 'a' + 10; |
137 | 1.58k | } else { |
138 | 35 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT, |
139 | 35 | "%s", *t); |
140 | 35 | return 0; |
141 | 35 | } |
142 | | |
143 | 7.71k | if (v > ((INT64_MAX - sval) / 16)) { |
144 | 27 | ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, |
145 | 27 | "Property %s overflows", *t); |
146 | 27 | return 0; |
147 | 27 | } |
148 | | |
149 | 7.69k | v <<= 4; |
150 | 7.69k | v += sval; |
151 | 7.69k | } while (ossl_isxdigit(*++s)); |
152 | 1.16k | if (!ossl_isspace(*s) && *s != '\0' && *s != ',') { |
153 | 105 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT, |
154 | 105 | "HERE-->%s", *t); |
155 | 105 | return 0; |
156 | 105 | } |
157 | 1.06k | *t = skip_space(s); |
158 | 1.06k | res->type = OSSL_PROPERTY_TYPE_NUMBER; |
159 | 1.06k | res->v.int_val = v; |
160 | 1.06k | return 1; |
161 | 1.16k | } |
162 | | |
163 | | static int parse_oct(const char *t[], OSSL_PROPERTY_DEFINITION *res) |
164 | 393 | { |
165 | 393 | const char *s = *t; |
166 | 393 | int64_t v = 0; |
167 | | |
168 | 1.46k | do { |
169 | 1.46k | if (*s == '9' || *s == '8' || !ossl_isdigit(*s)) { |
170 | 14 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT, |
171 | 14 | "HERE-->%s", *t); |
172 | 14 | return 0; |
173 | 14 | } |
174 | 1.45k | if (v > ((INT64_MAX - (*s - '0')) / 8)) { |
175 | 21 | ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, |
176 | 21 | "Property %s overflows", *t); |
177 | 21 | return 0; |
178 | 21 | } |
179 | | |
180 | 1.42k | v = (v << 3) + (*s - '0'); |
181 | 1.42k | } while (ossl_isdigit(*++s) && *s != '9' && *s != '8'); |
182 | 358 | if (!ossl_isspace(*s) && *s != '\0' && *s != ',') { |
183 | 26 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT, |
184 | 26 | "HERE-->%s", *t); |
185 | 26 | return 0; |
186 | 26 | } |
187 | 332 | *t = skip_space(s); |
188 | 332 | res->type = OSSL_PROPERTY_TYPE_NUMBER; |
189 | 332 | res->v.int_val = v; |
190 | 332 | return 1; |
191 | 358 | } |
192 | | |
193 | | static int parse_string(OSSL_LIB_CTX *ctx, const char *t[], char delim, |
194 | | OSSL_PROPERTY_DEFINITION *res, const int create) |
195 | 163 | { |
196 | 163 | char v[1000]; |
197 | 163 | const char *s = *t; |
198 | 163 | size_t i = 0; |
199 | 163 | int err = 0; |
200 | | |
201 | 73.9k | while (*s != '\0' && *s != delim) { |
202 | 73.7k | if (i < sizeof(v) - 1) |
203 | 28.4k | v[i++] = *s; |
204 | 45.3k | else |
205 | 45.3k | err = 1; |
206 | 73.7k | s++; |
207 | 73.7k | } |
208 | 163 | if (*s == '\0') { |
209 | 59 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_MATCHING_STRING_DELIMITER, |
210 | 59 | "HERE-->%c%s", delim, *t); |
211 | 59 | return 0; |
212 | 59 | } |
213 | 104 | v[i] = '\0'; |
214 | 104 | if (err) { |
215 | 9 | ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t); |
216 | 95 | } else { |
217 | 95 | res->v.str_val = ossl_property_value(ctx, v, create); |
218 | 95 | } |
219 | 104 | *t = skip_space(s + 1); |
220 | 104 | res->type = OSSL_PROPERTY_TYPE_STRING; |
221 | 104 | return !err; |
222 | 163 | } |
223 | | |
224 | | static int parse_unquoted(OSSL_LIB_CTX *ctx, const char *t[], |
225 | | OSSL_PROPERTY_DEFINITION *res, const int create) |
226 | 32.2k | { |
227 | 32.2k | char v[1000]; |
228 | 32.2k | const char *s = *t; |
229 | 32.2k | size_t i = 0; |
230 | 32.2k | int err = 0; |
231 | | |
232 | 32.2k | if (*s == '\0' || *s == ',') |
233 | 0 | return 0; |
234 | 2.02M | while (ossl_isprint(*s) && !ossl_isspace(*s) && *s != ',') { |
235 | 1.99M | if (i < sizeof(v) - 1) |
236 | 1.67M | v[i++] = ossl_tolower(*s); |
237 | 316k | else |
238 | 316k | err = 1; |
239 | 1.99M | s++; |
240 | 1.99M | } |
241 | 32.2k | if (!ossl_isspace(*s) && *s != '\0' && *s != ',') { |
242 | 121 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_ASCII_CHARACTER, |
243 | 121 | "HERE-->%s", s); |
244 | 121 | return 0; |
245 | 121 | } |
246 | 32.0k | v[i] = 0; |
247 | 32.0k | if (err) |
248 | 1.41k | ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t); |
249 | 30.6k | else if ((res->v.str_val = ossl_property_value(ctx, v, create)) == 0) |
250 | 15.2k | err = 1; |
251 | 32.0k | *t = skip_space(s); |
252 | 32.0k | res->type = OSSL_PROPERTY_TYPE_STRING; |
253 | 32.0k | return !err; |
254 | 32.2k | } |
255 | | |
256 | | static int parse_value(OSSL_LIB_CTX *ctx, const char *t[], |
257 | | OSSL_PROPERTY_DEFINITION *res, int create) |
258 | 39.1k | { |
259 | 39.1k | const char *s = *t; |
260 | 39.1k | int r = 0; |
261 | | |
262 | 39.1k | if (*s == '"' || *s == '\'') { |
263 | 163 | s++; |
264 | 163 | r = parse_string(ctx, &s, s[-1], res, create); |
265 | 39.0k | } else if (*s == '+') { |
266 | 238 | s++; |
267 | 238 | r = parse_number(&s, res); |
268 | 38.7k | } else if (*s == '-') { |
269 | 986 | s++; |
270 | 986 | r = parse_number(&s, res); |
271 | 986 | res->v.int_val = -res->v.int_val; |
272 | 37.8k | } else if (*s == '0' && s[1] == 'x') { |
273 | 1.22k | s += 2; |
274 | 1.22k | r = parse_hex(&s, res); |
275 | 36.5k | } else if (*s == '0' && ossl_isdigit(s[1])) { |
276 | 393 | s++; |
277 | 393 | r = parse_oct(&s, res); |
278 | 36.1k | } else if (ossl_isdigit(*s)) { |
279 | 2.28k | return parse_number(t, res); |
280 | 33.9k | } else if (ossl_isalpha(*s)) |
281 | 32.2k | return parse_unquoted(ctx, t, res, create); |
282 | 4.70k | if (r) |
283 | 2.66k | *t = s; |
284 | 4.70k | return r; |
285 | 39.1k | } |
286 | | |
287 | | static int pd_compare(const OSSL_PROPERTY_DEFINITION *const *p1, |
288 | | const OSSL_PROPERTY_DEFINITION *const *p2) |
289 | 11.6M | { |
290 | 11.6M | const OSSL_PROPERTY_DEFINITION *pd1 = *p1; |
291 | 11.6M | const OSSL_PROPERTY_DEFINITION *pd2 = *p2; |
292 | | |
293 | 11.6M | if (pd1->name_idx < pd2->name_idx) |
294 | 19.0k | return -1; |
295 | 11.6M | if (pd1->name_idx > pd2->name_idx) |
296 | 600k | return 1; |
297 | 11.0M | return 0; |
298 | 11.6M | } |
299 | | |
300 | | static void pd_free(OSSL_PROPERTY_DEFINITION *pd) |
301 | 3.02M | { |
302 | 3.02M | OPENSSL_free(pd); |
303 | 3.02M | } |
304 | | |
305 | | /* |
306 | | * Convert a stack of property definitions and queries into a fixed array. |
307 | | * The items are sorted for efficient query. The stack is not freed. |
308 | | * This function also checks for duplicated names and returns an error if |
309 | | * any exist. |
310 | | */ |
311 | | static OSSL_PROPERTY_LIST * |
312 | | stack_to_property_list(OSSL_LIB_CTX *ctx, |
313 | | STACK_OF(OSSL_PROPERTY_DEFINITION) *sk) |
314 | 5.86k | { |
315 | 5.86k | const int n = sk_OSSL_PROPERTY_DEFINITION_num(sk); |
316 | 5.86k | OSSL_PROPERTY_LIST *r; |
317 | 5.86k | OSSL_PROPERTY_IDX prev_name_idx = 0; |
318 | 5.86k | int i; |
319 | | |
320 | 5.86k | r = OPENSSL_malloc(sizeof(*r) |
321 | 5.86k | + (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0])); |
322 | 5.86k | if (r != NULL) { |
323 | 5.86k | sk_OSSL_PROPERTY_DEFINITION_sort(sk); |
324 | | |
325 | 5.86k | r->has_optional = 0; |
326 | 22.2k | for (i = 0; i < n; i++) { |
327 | 16.7k | r->properties[i] = *sk_OSSL_PROPERTY_DEFINITION_value(sk, i); |
328 | 16.7k | r->has_optional |= r->properties[i].optional; |
329 | | |
330 | | /* Check for duplicated names */ |
331 | 16.7k | if (i > 0 && r->properties[i].name_idx == prev_name_idx) { |
332 | 412 | OPENSSL_free(r); |
333 | 412 | ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, |
334 | 412 | "Duplicated name `%s'", |
335 | 412 | ossl_property_name_str(ctx, prev_name_idx)); |
336 | 412 | return NULL; |
337 | 412 | } |
338 | 16.3k | prev_name_idx = r->properties[i].name_idx; |
339 | 16.3k | } |
340 | 5.44k | r->num_properties = n; |
341 | 5.44k | } |
342 | 5.44k | return r; |
343 | 5.86k | } |
344 | | |
345 | | OSSL_PROPERTY_LIST *ossl_parse_property(OSSL_LIB_CTX *ctx, const char *defn) |
346 | 4.04k | { |
347 | 4.04k | OSSL_PROPERTY_DEFINITION *prop = NULL; |
348 | 4.04k | OSSL_PROPERTY_LIST *res = NULL; |
349 | 4.04k | STACK_OF(OSSL_PROPERTY_DEFINITION) *sk; |
350 | 4.04k | const char *s = defn; |
351 | 4.04k | int done; |
352 | | |
353 | 4.04k | if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL) |
354 | 0 | return NULL; |
355 | | |
356 | 4.04k | s = skip_space(s); |
357 | 4.04k | done = *s == '\0'; |
358 | 19.4k | while (!done) { |
359 | 15.4k | const char *start = s; |
360 | | |
361 | 15.4k | prop = OPENSSL_malloc(sizeof(*prop)); |
362 | 15.4k | if (prop == NULL) |
363 | 0 | goto err; |
364 | 15.4k | memset(&prop->v, 0, sizeof(prop->v)); |
365 | 15.4k | prop->optional = 0; |
366 | 15.4k | if (!parse_name(ctx, &s, 1, &prop->name_idx)) |
367 | 0 | goto err; |
368 | 15.4k | prop->oper = OSSL_PROPERTY_OPER_EQ; |
369 | 15.4k | if (prop->name_idx == 0) { |
370 | 0 | ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, |
371 | 0 | "Unknown name HERE-->%s", start); |
372 | 0 | goto err; |
373 | 0 | } |
374 | 15.4k | if (match_ch(&s, '=')) { |
375 | 15.4k | if (!parse_value(ctx, &s, prop, 1)) { |
376 | 0 | ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_VALUE, |
377 | 0 | "HERE-->%s", start); |
378 | 0 | goto err; |
379 | 0 | } |
380 | 15.4k | } else { |
381 | | /* A name alone means a true Boolean */ |
382 | 0 | prop->type = OSSL_PROPERTY_TYPE_STRING; |
383 | 0 | prop->v.str_val = OSSL_PROPERTY_TRUE; |
384 | 0 | } |
385 | | |
386 | 15.4k | if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop)) |
387 | 0 | goto err; |
388 | 15.4k | prop = NULL; |
389 | 15.4k | done = !match_ch(&s, ','); |
390 | 15.4k | } |
391 | 4.04k | if (*s != '\0') { |
392 | 0 | ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS, |
393 | 0 | "HERE-->%s", s); |
394 | 0 | goto err; |
395 | 0 | } |
396 | 4.04k | res = stack_to_property_list(ctx, sk); |
397 | | |
398 | 4.04k | err: |
399 | 4.04k | OPENSSL_free(prop); |
400 | 4.04k | sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free); |
401 | 4.04k | return res; |
402 | 4.04k | } |
403 | | |
404 | | OSSL_PROPERTY_LIST *ossl_parse_query(OSSL_LIB_CTX *ctx, const char *s, |
405 | | int create_values) |
406 | 3.04k | { |
407 | 3.04k | STACK_OF(OSSL_PROPERTY_DEFINITION) *sk; |
408 | 3.04k | OSSL_PROPERTY_LIST *res = NULL; |
409 | 3.04k | OSSL_PROPERTY_DEFINITION *prop = NULL; |
410 | 3.04k | int done; |
411 | | |
412 | 3.04k | if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL) |
413 | 0 | return NULL; |
414 | | |
415 | 3.04k | s = skip_space(s); |
416 | 3.04k | done = *s == '\0'; |
417 | 3.01M | while (!done) { |
418 | 3.01M | prop = OPENSSL_malloc(sizeof(*prop)); |
419 | 3.01M | if (prop == NULL) |
420 | 0 | goto err; |
421 | 3.01M | memset(&prop->v, 0, sizeof(prop->v)); |
422 | | |
423 | 3.01M | if (match_ch(&s, '-')) { |
424 | 271 | prop->oper = OSSL_PROPERTY_OVERRIDE; |
425 | 271 | prop->optional = 0; |
426 | 271 | if (!parse_name(ctx, &s, 1, &prop->name_idx)) |
427 | 33 | goto err; |
428 | 238 | goto skip_value; |
429 | 271 | } |
430 | 3.01M | prop->optional = match_ch(&s, '?'); |
431 | 3.01M | if (!parse_name(ctx, &s, 1, &prop->name_idx)) |
432 | 316 | goto err; |
433 | | |
434 | 3.01M | if (match_ch(&s, '=')) { |
435 | 23.4k | prop->oper = OSSL_PROPERTY_OPER_EQ; |
436 | 2.98M | } else if (MATCH(&s, "!=")) { |
437 | 353 | prop->oper = OSSL_PROPERTY_OPER_NE; |
438 | 2.98M | } else { |
439 | | /* A name alone is a Boolean comparison for true */ |
440 | 2.98M | prop->oper = OSSL_PROPERTY_OPER_EQ; |
441 | 2.98M | prop->type = OSSL_PROPERTY_TYPE_STRING; |
442 | 2.98M | prop->v.str_val = OSSL_PROPERTY_TRUE; |
443 | 2.98M | goto skip_value; |
444 | 2.98M | } |
445 | 23.7k | if (!parse_value(ctx, &s, prop, create_values)) |
446 | 18.9k | prop->type = OSSL_PROPERTY_TYPE_VALUE_UNDEFINED; |
447 | | |
448 | 3.01M | skip_value: |
449 | 3.01M | if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop)) |
450 | 0 | goto err; |
451 | 3.01M | prop = NULL; |
452 | 3.01M | done = !match_ch(&s, ','); |
453 | 3.01M | } |
454 | 2.69k | if (*s != '\0') { |
455 | 880 | ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS, |
456 | 880 | "HERE-->%s", s); |
457 | 880 | goto err; |
458 | 880 | } |
459 | 1.81k | res = stack_to_property_list(ctx, sk); |
460 | | |
461 | 3.04k | err: |
462 | 3.04k | OPENSSL_free(prop); |
463 | 3.04k | sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free); |
464 | 3.04k | return res; |
465 | 1.81k | } |
466 | | |
467 | | /* |
468 | | * Compare a query against a definition. |
469 | | * Return the number of clauses matched or -1 if a mandatory clause is false. |
470 | | */ |
471 | | int ossl_property_match_count(const OSSL_PROPERTY_LIST *query, |
472 | | const OSSL_PROPERTY_LIST *defn) |
473 | 1.40k | { |
474 | 1.40k | const OSSL_PROPERTY_DEFINITION *const q = query->properties; |
475 | 1.40k | const OSSL_PROPERTY_DEFINITION *const d = defn->properties; |
476 | 1.40k | int i = 0, j = 0, matches = 0; |
477 | 1.40k | OSSL_PROPERTY_OPER oper; |
478 | | |
479 | 1.71k | while (i < query->num_properties) { |
480 | 499 | if ((oper = q[i].oper) == OSSL_PROPERTY_OVERRIDE) { |
481 | 48 | i++; |
482 | 48 | continue; |
483 | 48 | } |
484 | 451 | if (j < defn->num_properties) { |
485 | 313 | if (q[i].name_idx > d[j].name_idx) { /* skip defn, not in query */ |
486 | 70 | j++; |
487 | 70 | continue; |
488 | 70 | } |
489 | 243 | if (q[i].name_idx == d[j].name_idx) { /* both in defn and query */ |
490 | 18 | const int eq = q[i].type == d[j].type |
491 | 18 | && memcmp(&q[i].v, &d[j].v, sizeof(q[i].v)) == 0; |
492 | | |
493 | 18 | if ((eq && oper == OSSL_PROPERTY_OPER_EQ) |
494 | 18 | || (!eq && oper == OSSL_PROPERTY_OPER_NE)) |
495 | 11 | matches++; |
496 | 7 | else if (!q[i].optional) |
497 | 2 | return -1; |
498 | 16 | i++; |
499 | 16 | j++; |
500 | 16 | continue; |
501 | 18 | } |
502 | 243 | } |
503 | | |
504 | | /* |
505 | | * Handle the cases of a missing value and a query with no corresponding |
506 | | * definition. The former fails for any comparison except inequality, |
507 | | * the latter is treated as a comparison against the Boolean false. |
508 | | */ |
509 | 363 | if (q[i].type == OSSL_PROPERTY_TYPE_VALUE_UNDEFINED) { |
510 | 64 | if (oper == OSSL_PROPERTY_OPER_NE) |
511 | 11 | matches++; |
512 | 53 | else if (!q[i].optional) |
513 | 32 | return -1; |
514 | 299 | } else if (q[i].type != OSSL_PROPERTY_TYPE_STRING |
515 | 299 | || (oper == OSSL_PROPERTY_OPER_EQ |
516 | 177 | && q[i].v.str_val != OSSL_PROPERTY_FALSE) |
517 | 299 | || (oper == OSSL_PROPERTY_OPER_NE |
518 | 282 | && q[i].v.str_val == OSSL_PROPERTY_FALSE)) { |
519 | 282 | if (!q[i].optional) |
520 | 155 | return -1; |
521 | 282 | } else { |
522 | 17 | matches++; |
523 | 17 | } |
524 | 176 | i++; |
525 | 176 | } |
526 | 1.21k | return matches; |
527 | 1.40k | } |
528 | | |
529 | | void ossl_property_free(OSSL_PROPERTY_LIST *p) |
530 | 7.19k | { |
531 | 7.19k | OPENSSL_free(p); |
532 | 7.19k | } |
533 | | |
534 | | /* |
535 | | * Merge two property lists. |
536 | | * If there is a common name, the one from the first list is used. |
537 | | */ |
538 | | OSSL_PROPERTY_LIST *ossl_property_merge(const OSSL_PROPERTY_LIST *a, |
539 | | const OSSL_PROPERTY_LIST *b) |
540 | 0 | { |
541 | 0 | const OSSL_PROPERTY_DEFINITION *const ap = a->properties; |
542 | 0 | const OSSL_PROPERTY_DEFINITION *const bp = b->properties; |
543 | 0 | const OSSL_PROPERTY_DEFINITION *copy; |
544 | 0 | OSSL_PROPERTY_LIST *r; |
545 | 0 | int i, j, n; |
546 | 0 | const int t = a->num_properties + b->num_properties; |
547 | |
|
548 | 0 | r = OPENSSL_malloc(sizeof(*r) |
549 | 0 | + (t == 0 ? 0 : t - 1) * sizeof(r->properties[0])); |
550 | 0 | if (r == NULL) |
551 | 0 | return NULL; |
552 | | |
553 | 0 | r->has_optional = 0; |
554 | 0 | for (i = j = n = 0; i < a->num_properties || j < b->num_properties; n++) { |
555 | 0 | if (i >= a->num_properties) { |
556 | 0 | copy = &bp[j++]; |
557 | 0 | } else if (j >= b->num_properties) { |
558 | 0 | copy = &ap[i++]; |
559 | 0 | } else if (ap[i].name_idx <= bp[j].name_idx) { |
560 | 0 | if (ap[i].name_idx == bp[j].name_idx) |
561 | 0 | j++; |
562 | 0 | copy = &ap[i++]; |
563 | 0 | } else { |
564 | 0 | copy = &bp[j++]; |
565 | 0 | } |
566 | 0 | memcpy(r->properties + n, copy, sizeof(r->properties[0])); |
567 | 0 | r->has_optional |= copy->optional; |
568 | 0 | } |
569 | 0 | r->num_properties = n; |
570 | 0 | if (n != t) |
571 | 0 | r = OPENSSL_realloc(r, sizeof(*r) + (n - 1) * sizeof(r->properties[0])); |
572 | 0 | return r; |
573 | 0 | } |
574 | | |
575 | | int ossl_property_parse_init(OSSL_LIB_CTX *ctx) |
576 | 185 | { |
577 | 185 | static const char *const predefined_names[] = { |
578 | 185 | "provider", /* Name of provider (default, legacy, fips) */ |
579 | 185 | "version", /* Version number of this provider */ |
580 | 185 | "fips", /* FIPS validated or FIPS supporting algorithm */ |
581 | 185 | "output", /* Output type for encoders */ |
582 | 185 | "input", /* Input type for decoders */ |
583 | 185 | "structure", /* Structure name for encoders and decoders */ |
584 | 185 | }; |
585 | 185 | size_t i; |
586 | | |
587 | 1.29k | for (i = 0; i < OSSL_NELEM(predefined_names); i++) |
588 | 1.11k | if (ossl_property_name(ctx, predefined_names[i], 1) == 0) |
589 | 0 | goto err; |
590 | | |
591 | | /* |
592 | | * Pre-populate the two Boolean values. We must do them before any other |
593 | | * values and in this order so that we get the same index as the global |
594 | | * OSSL_PROPERTY_TRUE and OSSL_PROPERTY_FALSE values |
595 | | */ |
596 | 185 | if ((ossl_property_value(ctx, "yes", 1) != OSSL_PROPERTY_TRUE) |
597 | 185 | || (ossl_property_value(ctx, "no", 1) != OSSL_PROPERTY_FALSE)) |
598 | 0 | goto err; |
599 | | |
600 | 185 | return 1; |
601 | 0 | err: |
602 | 0 | return 0; |
603 | 185 | } |
604 | | |
605 | | static void put_char(char ch, char **buf, size_t *remain, size_t *needed) |
606 | 0 | { |
607 | 0 | if (*remain == 0) { |
608 | 0 | ++*needed; |
609 | 0 | return; |
610 | 0 | } |
611 | 0 | if (*remain == 1) |
612 | 0 | **buf = '\0'; |
613 | 0 | else |
614 | 0 | **buf = ch; |
615 | 0 | ++*buf; |
616 | 0 | ++*needed; |
617 | 0 | --*remain; |
618 | 0 | } |
619 | | |
620 | | static void put_str(const char *str, char **buf, size_t *remain, size_t *needed) |
621 | 0 | { |
622 | 0 | size_t olen, len, i; |
623 | 0 | char quote = '\0'; |
624 | 0 | int quotes; |
625 | |
|
626 | 0 | len = olen = strlen(str); |
627 | 0 | *needed += len; |
628 | | |
629 | | /* |
630 | | * Check to see if we need quotes or not. |
631 | | * Characters that are legal in a PropertyName don't need quoting. |
632 | | * We simply assume all others require quotes. |
633 | | */ |
634 | 0 | for (i = 0; i < len; i++) |
635 | 0 | if (!ossl_isalnum(str[i]) && str[i] != '.' && str[i] != '_') { |
636 | | /* Default to single quotes ... */ |
637 | 0 | if (quote == '\0') |
638 | 0 | quote = '\''; |
639 | | /* ... but use double quotes if a single is present */ |
640 | 0 | if (str[i] == '\'') |
641 | 0 | quote = '"'; |
642 | 0 | } |
643 | |
|
644 | 0 | quotes = quote != '\0'; |
645 | 0 | if (*remain == 0) { |
646 | 0 | *needed += 2 * quotes; |
647 | 0 | return; |
648 | 0 | } |
649 | | |
650 | 0 | if (quotes) |
651 | 0 | put_char(quote, buf, remain, needed); |
652 | |
|
653 | 0 | if (*remain < len + 1 + quotes) |
654 | 0 | len = *remain - 1; |
655 | |
|
656 | 0 | if (len > 0) { |
657 | 0 | memcpy(*buf, str, len); |
658 | 0 | *buf += len; |
659 | 0 | *remain -= len; |
660 | 0 | } |
661 | |
|
662 | 0 | if (quotes) |
663 | 0 | put_char(quote, buf, remain, needed); |
664 | |
|
665 | 0 | if (len < olen && *remain == 1) { |
666 | 0 | **buf = '\0'; |
667 | 0 | ++*buf; |
668 | 0 | --*remain; |
669 | 0 | } |
670 | 0 | } |
671 | | |
672 | | static void put_num(int64_t val, char **buf, size_t *remain, size_t *needed) |
673 | 0 | { |
674 | 0 | int64_t tmpval = val; |
675 | 0 | size_t len = 1; |
676 | |
|
677 | 0 | if (tmpval < 0) { |
678 | 0 | len++; |
679 | 0 | tmpval = -tmpval; |
680 | 0 | } |
681 | 0 | for (; tmpval > 9; len++, tmpval /= 10); |
682 | |
|
683 | 0 | *needed += len; |
684 | |
|
685 | 0 | if (*remain == 0) |
686 | 0 | return; |
687 | | |
688 | 0 | BIO_snprintf(*buf, *remain, "%lld", (long long int)val); |
689 | 0 | if (*remain < len) { |
690 | 0 | *buf += *remain; |
691 | 0 | *remain = 0; |
692 | 0 | } else { |
693 | 0 | *buf += len; |
694 | 0 | *remain -= len; |
695 | 0 | } |
696 | 0 | } |
697 | | |
698 | | size_t ossl_property_list_to_string(OSSL_LIB_CTX *ctx, |
699 | | const OSSL_PROPERTY_LIST *list, char *buf, |
700 | | size_t bufsize) |
701 | 0 | { |
702 | 0 | int i; |
703 | 0 | const OSSL_PROPERTY_DEFINITION *prop = NULL; |
704 | 0 | size_t needed = 0; |
705 | 0 | const char *val; |
706 | |
|
707 | 0 | if (list == NULL) { |
708 | 0 | if (bufsize > 0) |
709 | 0 | *buf = '\0'; |
710 | 0 | return 1; |
711 | 0 | } |
712 | 0 | if (list->num_properties != 0) |
713 | 0 | prop = &list->properties[list->num_properties - 1]; |
714 | 0 | for (i = 0; i < list->num_properties; i++, prop--) { |
715 | | /* Skip invalid names */ |
716 | 0 | if (prop->name_idx == 0) |
717 | 0 | continue; |
718 | | |
719 | 0 | if (needed > 0) |
720 | 0 | put_char(',', &buf, &bufsize, &needed); |
721 | |
|
722 | 0 | if (prop->optional) |
723 | 0 | put_char('?', &buf, &bufsize, &needed); |
724 | 0 | else if (prop->oper == OSSL_PROPERTY_OVERRIDE) |
725 | 0 | put_char('-', &buf, &bufsize, &needed); |
726 | |
|
727 | 0 | val = ossl_property_name_str(ctx, prop->name_idx); |
728 | 0 | if (val == NULL) |
729 | 0 | return 0; |
730 | 0 | put_str(val, &buf, &bufsize, &needed); |
731 | |
|
732 | 0 | switch (prop->oper) { |
733 | 0 | case OSSL_PROPERTY_OPER_NE: |
734 | 0 | put_char('!', &buf, &bufsize, &needed); |
735 | | /* fall through */ |
736 | 0 | case OSSL_PROPERTY_OPER_EQ: |
737 | 0 | put_char('=', &buf, &bufsize, &needed); |
738 | | /* put value */ |
739 | 0 | switch (prop->type) { |
740 | 0 | case OSSL_PROPERTY_TYPE_STRING: |
741 | 0 | val = ossl_property_value_str(ctx, prop->v.str_val); |
742 | 0 | if (val == NULL) |
743 | 0 | return 0; |
744 | 0 | put_str(val, &buf, &bufsize, &needed); |
745 | 0 | break; |
746 | | |
747 | 0 | case OSSL_PROPERTY_TYPE_NUMBER: |
748 | 0 | put_num(prop->v.int_val, &buf, &bufsize, &needed); |
749 | 0 | break; |
750 | | |
751 | 0 | default: |
752 | 0 | return 0; |
753 | 0 | } |
754 | 0 | break; |
755 | 0 | default: |
756 | | /* do nothing */ |
757 | 0 | break; |
758 | 0 | } |
759 | 0 | } |
760 | | |
761 | 0 | put_char('\0', &buf, &bufsize, &needed); |
762 | 0 | return needed; |
763 | 0 | } |