Coverage Report

Created: 2025-08-28 07:07

/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
370k
{
27
407k
    while (ossl_isspace(*s))
28
37.2k
        s++;
29
370k
    return s;
30
370k
}
31
32
static int match_ch(const char *t[], char m)
33
511k
{
34
511k
    const char *s = *t;
35
36
511k
    if (*s == m) {
37
175k
        *t = skip_space(s + 1);
38
175k
        return 1;
39
175k
    }
40
336k
    return 0;
41
511k
}
42
43
100k
#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
100k
{
47
100k
    const char *s = *t;
48
49
100k
    if (OPENSSL_strncasecmp(s, m, m_len) == 0) {
50
405
        *t = skip_space(s + m_len);
51
405
        return 1;
52
405
    }
53
100k
    return 0;
54
100k
}
55
56
static int parse_name(OSSL_LIB_CTX *ctx, const char *t[], int create,
57
                      OSSL_PROPERTY_IDX *idx)
58
143k
{
59
143k
    char name[100];
60
143k
    int err = 0;
61
143k
    size_t i = 0;
62
143k
    const char *s = *t;
63
143k
    int user_name = 0;
64
65
156k
    for (;;) {
66
156k
        if (!ossl_isalpha(*s)) {
67
1.09k
            ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_IDENTIFIER,
68
1.09k
                           "HERE-->%s", *t);
69
1.09k
            return 0;
70
1.09k
        }
71
584k
        do {
72
584k
            if (i < sizeof(name) - 1)
73
541k
                name[i++] = ossl_tolower(*s);
74
43.3k
            else
75
43.3k
                err = 1;
76
584k
        } while (*++s == '_' || ossl_isalnum(*s));
77
155k
        if (*s != '.')
78
142k
            break;
79
12.7k
        user_name = 1;
80
12.7k
        if (i < sizeof(name) - 1)
81
12.4k
            name[i++] = *s;
82
307
        else
83
307
            err = 1;
84
12.7k
        s++;
85
12.7k
    }
86
142k
    name[i] = '\0';
87
142k
    if (err) {
88
104
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NAME_TOO_LONG, "HERE-->%s", *t);
89
104
        return 0;
90
104
    }
91
142k
    *t = skip_space(s);
92
142k
    *idx = ossl_property_name(ctx, name, user_name && create);
93
142k
    return 1;
94
142k
}
95
96
static int parse_number(const char *t[], OSSL_PROPERTY_DEFINITION *res)
97
1.80k
{
98
1.80k
    const char *s = *t;
99
1.80k
    int64_t v = 0;
100
101
4.76k
    do {
102
4.76k
        if (!ossl_isdigit(*s)) {
103
46
            ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT,
104
46
                           "HERE-->%s", *t);
105
46
            return 0;
106
46
        }
107
        /* overflow check */
108
4.72k
        if (v > ((INT64_MAX - (*s - '0')) / 10)) {
109
75
            ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
110
75
                           "Property %s overflows", *t);
111
75
            return 0;
112
75
        }
113
4.64k
        v = v * 10 + (*s++ - '0');
114
4.64k
    } while (ossl_isdigit(*s));
115
1.68k
    if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
116
221
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT,
117
221
                       "HERE-->%s", *t);
118
221
        return 0;
119
221
    }
120
1.46k
    *t = skip_space(s);
121
1.46k
    res->type = OSSL_PROPERTY_TYPE_NUMBER;
122
1.46k
    res->v.int_val = v;
123
1.46k
    return 1;
124
1.68k
}
125
126
static int parse_hex(const char *t[], OSSL_PROPERTY_DEFINITION *res)
127
613
{
128
613
    const char *s = *t;
129
613
    int64_t v = 0;
130
613
    int sval;
131
132
3.46k
    do {
133
3.46k
        if (ossl_isdigit(*s)) {
134
2.52k
            sval = *s - '0';
135
2.52k
        } else if (ossl_isxdigit(*s)) {
136
917
            sval = ossl_tolower(*s) - 'a' + 10;
137
917
        } else {
138
21
            ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT,
139
21
                           "%s", *t);
140
21
            return 0;
141
21
        }
142
143
3.44k
        if (v > ((INT64_MAX - sval) / 16)) {
144
98
            ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
145
98
                           "Property %s overflows", *t);
146
98
            return 0;
147
98
        }
148
149
3.34k
        v <<= 4;
150
3.34k
        v += sval;
151
3.34k
    } while (ossl_isxdigit(*++s));
152
494
    if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
153
71
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT,
154
71
                       "HERE-->%s", *t);
155
71
        return 0;
156
71
    }
157
423
    *t = skip_space(s);
158
423
    res->type = OSSL_PROPERTY_TYPE_NUMBER;
159
423
    res->v.int_val = v;
160
423
    return 1;
161
494
}
162
163
static int parse_oct(const char *t[], OSSL_PROPERTY_DEFINITION *res)
164
273
{
165
273
    const char *s = *t;
166
273
    int64_t v = 0;
167
168
2.98k
    do {
169
2.98k
        if (*s == '9' || *s == '8' || !ossl_isdigit(*s)) {
170
42
            ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT,
171
42
                           "HERE-->%s", *t);
172
42
            return 0;
173
42
        }
174
2.94k
        if (v > ((INT64_MAX - (*s - '0')) / 8)) {
175
43
            ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
176
43
                           "Property %s overflows", *t);
177
43
            return 0;
178
43
        }
179
180
2.90k
        v = (v << 3) + (*s - '0');
181
2.90k
    } while (ossl_isdigit(*++s) && *s != '9' && *s != '8');
182
188
    if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
183
83
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT,
184
83
                       "HERE-->%s", *t);
185
83
        return 0;
186
83
    }
187
105
    *t = skip_space(s);
188
105
    res->type = OSSL_PROPERTY_TYPE_NUMBER;
189
105
    res->v.int_val = v;
190
105
    return 1;
191
188
}
192
193
static int parse_string(OSSL_LIB_CTX *ctx, const char *t[], char delim,
194
                        OSSL_PROPERTY_DEFINITION *res, const int create)
195
234
{
196
234
    char v[1000];
197
234
    const char *s = *t;
198
234
    size_t i = 0;
199
234
    int err = 0;
200
201
93.8k
    while (*s != '\0' && *s != delim) {
202
93.6k
        if (i < sizeof(v) - 1)
203
87.3k
            v[i++] = *s;
204
6.33k
        else
205
6.33k
            err = 1;
206
93.6k
        s++;
207
93.6k
    }
208
234
    if (*s == '\0') {
209
122
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_MATCHING_STRING_DELIMITER,
210
122
                       "HERE-->%c%s", delim, *t);
211
122
        return 0;
212
122
    }
213
112
    v[i] = '\0';
214
112
    if (err) {
215
26
        ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
216
86
    } else {
217
86
        res->v.str_val = ossl_property_value(ctx, v, create);
218
86
    }
219
112
    *t = skip_space(s + 1);
220
112
    res->type = OSSL_PROPERTY_TYPE_STRING;
221
112
    return !err;
222
234
}
223
224
static int parse_unquoted(OSSL_LIB_CTX *ctx, const char *t[],
225
                          OSSL_PROPERTY_DEFINITION *res, const int create)
226
36.6k
{
227
36.6k
    char v[1000];
228
36.6k
    const char *s = *t;
229
36.6k
    size_t i = 0;
230
36.6k
    int err = 0;
231
232
36.6k
    if (*s == '\0' || *s == ',')
233
0
        return 0;
234
1.28M
    while (ossl_isprint(*s) && !ossl_isspace(*s) && *s != ',') {
235
1.25M
        if (i < sizeof(v) - 1)
236
950k
            v[i++] = ossl_tolower(*s);
237
300k
        else
238
300k
            err = 1;
239
1.25M
        s++;
240
1.25M
    }
241
36.6k
    if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
242
147
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_ASCII_CHARACTER,
243
147
                       "HERE-->%s", s);
244
147
        return 0;
245
147
    }
246
36.4k
    v[i] = 0;
247
36.4k
    if (err)
248
574
        ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
249
35.9k
    else if ((res->v.str_val = ossl_property_value(ctx, v, create)) == 0)
250
8.00k
        err = 1;
251
36.4k
    *t = skip_space(s);
252
36.4k
    res->type = OSSL_PROPERTY_TYPE_STRING;
253
36.4k
    return !err;
254
36.6k
}
255
256
static int parse_value(OSSL_LIB_CTX *ctx, const char *t[],
257
                       OSSL_PROPERTY_DEFINITION *res, int create)
258
39.7k
{
259
39.7k
    const char *s = *t;
260
39.7k
    int r = 0;
261
262
39.7k
    if (*s == '"' || *s == '\'') {
263
234
        s++;
264
234
        r = parse_string(ctx, &s, s[-1], res, create);
265
39.4k
    } else if (*s == '+') {
266
40
        s++;
267
40
        r = parse_number(&s, res);
268
39.4k
    } else if (*s == '-') {
269
247
        s++;
270
247
        r = parse_number(&s, res);
271
247
        res->v.int_val = -res->v.int_val;
272
39.2k
    } else if (*s == '0' && s[1] == 'x') {
273
613
        s += 2;
274
613
        r = parse_hex(&s, res);
275
38.5k
    } else if (*s == '0' && ossl_isdigit(s[1])) {
276
273
        s++;
277
273
        r = parse_oct(&s, res);
278
38.3k
    } else if (ossl_isdigit(*s)) {
279
1.52k
        return parse_number(t, res);
280
36.8k
    } else if (ossl_isalpha(*s))
281
36.6k
        return parse_unquoted(ctx, t, res, create);
282
1.56k
    if (r)
283
736
        *t = s;
284
1.56k
    return r;
285
39.7k
}
286
287
static int pd_compare(const OSSL_PROPERTY_DEFINITION *const *p1,
288
                      const OSSL_PROPERTY_DEFINITION *const *p2)
289
78.2k
{
290
78.2k
    const OSSL_PROPERTY_DEFINITION *pd1 = *p1;
291
78.2k
    const OSSL_PROPERTY_DEFINITION *pd2 = *p2;
292
293
78.2k
    if (pd1->name_idx < pd2->name_idx)
294
30.3k
        return -1;
295
47.9k
    if (pd1->name_idx > pd2->name_idx)
296
16.5k
        return 1;
297
31.3k
    return 0;
298
47.9k
}
299
300
static void pd_free(OSSL_PROPERTY_DEFINITION *pd)
301
142k
{
302
142k
    OPENSSL_free(pd);
303
142k
}
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
10.2k
{
315
10.2k
    const int n = sk_OSSL_PROPERTY_DEFINITION_num(sk);
316
10.2k
    OSSL_PROPERTY_LIST *r;
317
10.2k
    OSSL_PROPERTY_IDX prev_name_idx = 0;
318
10.2k
    int i;
319
320
10.2k
    r = OPENSSL_malloc(sizeof(*r)
321
10.2k
                       + (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0]));
322
10.2k
    if (r != NULL) {
323
10.2k
        sk_OSSL_PROPERTY_DEFINITION_sort(sk);
324
325
10.2k
        r->has_optional = 0;
326
39.4k
        for (i = 0; i < n; i++) {
327
29.5k
            r->properties[i] = *sk_OSSL_PROPERTY_DEFINITION_value(sk, i);
328
29.5k
            r->has_optional |= r->properties[i].optional;
329
330
            /* Check for duplicated names */
331
29.5k
            if (i > 0 && r->properties[i].name_idx == prev_name_idx) {
332
397
                OPENSSL_free(r);
333
397
                ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
334
397
                               "Duplicated name `%s'",
335
397
                               ossl_property_name_str(ctx, prev_name_idx));
336
397
                return NULL;
337
397
            }
338
29.1k
            prev_name_idx = r->properties[i].name_idx;
339
29.1k
        }
340
9.86k
        r->num_properties = n;
341
9.86k
    }
342
9.86k
    return r;
343
10.2k
}
344
345
OSSL_PROPERTY_LIST *ossl_parse_property(OSSL_LIB_CTX *ctx, const char *defn)
346
7.31k
{
347
7.31k
    OSSL_PROPERTY_DEFINITION *prop = NULL;
348
7.31k
    OSSL_PROPERTY_LIST *res = NULL;
349
7.31k
    STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
350
7.31k
    const char *s = defn;
351
7.31k
    int done;
352
353
7.31k
    if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
354
0
        return NULL;
355
356
7.31k
    s = skip_space(s);
357
7.31k
    done = *s == '\0';
358
35.2k
    while (!done) {
359
27.8k
        const char *start = s;
360
361
27.8k
        prop = OPENSSL_malloc(sizeof(*prop));
362
27.8k
        if (prop == NULL)
363
0
            goto err;
364
27.8k
        memset(&prop->v, 0, sizeof(prop->v));
365
27.8k
        prop->optional = 0;
366
27.8k
        if (!parse_name(ctx, &s, 1, &prop->name_idx))
367
0
            goto err;
368
27.8k
        prop->oper = OSSL_PROPERTY_OPER_EQ;
369
27.8k
        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
27.8k
        if (match_ch(&s, '=')) {
375
27.8k
            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
27.8k
        } 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
27.8k
        if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
387
0
            goto err;
388
27.8k
        prop = NULL;
389
27.8k
        done = !match_ch(&s, ',');
390
27.8k
    }
391
7.31k
    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
7.31k
    res = stack_to_property_list(ctx, sk);
397
398
7.31k
err:
399
7.31k
    OPENSSL_free(prop);
400
7.31k
    sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
401
7.31k
    return res;
402
7.31k
}
403
404
OSSL_PROPERTY_LIST *ossl_parse_query(OSSL_LIB_CTX *ctx, const char *s,
405
                                     int create_values)
406
6.21k
{
407
6.21k
    STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
408
6.21k
    OSSL_PROPERTY_LIST *res = NULL;
409
6.21k
    OSSL_PROPERTY_DEFINITION *prop = NULL;
410
6.21k
    int done;
411
412
6.21k
    if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
413
0
        return NULL;
414
415
6.21k
    s = skip_space(s);
416
6.21k
    done = *s == '\0';
417
121k
    while (!done) {
418
116k
        prop = OPENSSL_malloc(sizeof(*prop));
419
116k
        if (prop == NULL)
420
0
            goto err;
421
116k
        memset(&prop->v, 0, sizeof(prop->v));
422
423
116k
        if (match_ch(&s, '-')) {
424
2.88k
            prop->oper = OSSL_PROPERTY_OVERRIDE;
425
2.88k
            prop->optional = 0;
426
2.88k
            if (!parse_name(ctx, &s, 1, &prop->name_idx))
427
56
                goto err;
428
2.82k
            goto skip_value;
429
2.88k
        }
430
113k
        prop->optional = match_ch(&s, '?');
431
113k
        if (!parse_name(ctx, &s, 1, &prop->name_idx))
432
1.14k
            goto err;
433
434
112k
        if (match_ch(&s, '=')) {
435
11.4k
            prop->oper = OSSL_PROPERTY_OPER_EQ;
436
100k
        } else if (MATCH(&s, "!=")) {
437
405
            prop->oper = OSSL_PROPERTY_OPER_NE;
438
100k
        } else {
439
            /* A name alone is a Boolean comparison for true */
440
100k
            prop->oper = OSSL_PROPERTY_OPER_EQ;
441
100k
            prop->type = OSSL_PROPERTY_TYPE_STRING;
442
100k
            prop->v.str_val = OSSL_PROPERTY_TRUE;
443
100k
            goto skip_value;
444
100k
        }
445
11.8k
        if (!parse_value(ctx, &s, prop, create_values))
446
9.73k
            prop->type = OSSL_PROPERTY_TYPE_VALUE_UNDEFINED;
447
448
114k
skip_value:
449
114k
        if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
450
0
            goto err;
451
114k
        prop = NULL;
452
114k
        done = !match_ch(&s, ',');
453
114k
    }
454
5.01k
    if (*s != '\0') {
455
2.07k
        ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS,
456
2.07k
                       "HERE-->%s", s);
457
2.07k
        goto err;
458
2.07k
    }
459
2.94k
    res = stack_to_property_list(ctx, sk);
460
461
6.21k
err:
462
6.21k
    OPENSSL_free(prop);
463
6.21k
    sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
464
6.21k
    return res;
465
2.94k
}
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
2.54k
{
474
2.54k
    const OSSL_PROPERTY_DEFINITION *const q = query->properties;
475
2.54k
    const OSSL_PROPERTY_DEFINITION *const d = defn->properties;
476
2.54k
    int i = 0, j = 0, matches = 0;
477
2.54k
    OSSL_PROPERTY_OPER oper;
478
479
3.06k
    while (i < query->num_properties) {
480
674
        if ((oper = q[i].oper) == OSSL_PROPERTY_OVERRIDE) {
481
126
            i++;
482
126
            continue;
483
126
        }
484
548
        if (j < defn->num_properties) {
485
376
            if (q[i].name_idx > d[j].name_idx) {  /* skip defn, not in query */
486
108
                j++;
487
108
                continue;
488
108
            }
489
268
            if (q[i].name_idx == d[j].name_idx) { /* both in defn and query */
490
14
                const int eq = q[i].type == d[j].type
491
14
                               && memcmp(&q[i].v, &d[j].v, sizeof(q[i].v)) == 0;
492
493
14
                if ((eq && oper == OSSL_PROPERTY_OPER_EQ)
494
14
                    || (!eq && oper == OSSL_PROPERTY_OPER_NE))
495
3
                    matches++;
496
11
                else if (!q[i].optional)
497
4
                    return -1;
498
10
                i++;
499
10
                j++;
500
10
                continue;
501
14
            }
502
268
        }
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
426
        if (q[i].type == OSSL_PROPERTY_TYPE_VALUE_UNDEFINED) {
510
150
            if (oper == OSSL_PROPERTY_OPER_NE)
511
109
                matches++;
512
41
            else if (!q[i].optional)
513
14
                return -1;
514
276
        } else if (q[i].type != OSSL_PROPERTY_TYPE_STRING
515
276
                   || (oper == OSSL_PROPERTY_OPER_EQ
516
217
                       && q[i].v.str_val != OSSL_PROPERTY_FALSE)
517
276
                   || (oper == OSSL_PROPERTY_OPER_NE
518
235
                       && q[i].v.str_val == OSSL_PROPERTY_FALSE)) {
519
235
            if (!q[i].optional)
520
134
                return -1;
521
235
        } else {
522
41
            matches++;
523
41
        }
524
278
        i++;
525
278
    }
526
2.39k
    return matches;
527
2.54k
}
528
529
void ossl_property_free(OSSL_PROPERTY_LIST *p)
530
20.2k
{
531
20.2k
    OPENSSL_free(p);
532
20.2k
}
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
377
{
577
377
    static const char *const predefined_names[] = {
578
377
        "provider",     /* Name of provider (default, legacy, fips) */
579
377
        "version",      /* Version number of this provider */
580
377
        "fips",         /* FIPS validated or FIPS supporting algorithm */
581
377
        "output",       /* Output type for encoders */
582
377
        "input",        /* Input type for decoders */
583
377
        "structure",    /* Structure name for encoders and decoders */
584
377
    };
585
377
    size_t i;
586
587
2.63k
    for (i = 0; i < OSSL_NELEM(predefined_names); i++)
588
2.26k
        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
377
    if ((ossl_property_value(ctx, "yes", 1) != OSSL_PROPERTY_TRUE)
597
377
        || (ossl_property_value(ctx, "no", 1) != OSSL_PROPERTY_FALSE))
598
0
        goto err;
599
600
377
    return 1;
601
0
err:
602
0
    return 0;
603
377
}
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
}