Coverage Report

Created: 2025-08-11 07:04

/src/openssl35/crypto/property/property_parse.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2025 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 "internal/e_os.h"
22
23
DEFINE_STACK_OF(OSSL_PROPERTY_DEFINITION)
24
25
static const char *skip_space(const char *s)
26
235k
{
27
291k
    while (ossl_isspace(*s))
28
55.9k
        s++;
29
235k
    return s;
30
235k
}
31
32
static int match_ch(const char *t[], char m)
33
264k
{
34
264k
    const char *s = *t;
35
36
264k
    if (*s == m) {
37
106k
        *t = skip_space(s + 1);
38
106k
        return 1;
39
106k
    }
40
158k
    return 0;
41
264k
}
42
43
43.3k
#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
43.3k
{
47
43.3k
    const char *s = *t;
48
49
43.3k
    if (OPENSSL_strncasecmp(s, m, m_len) == 0) {
50
358
        *t = skip_space(s + m_len);
51
358
        return 1;
52
358
    }
53
43.0k
    return 0;
54
43.3k
}
55
56
static int parse_name(OSSL_LIB_CTX *ctx, const char *t[], int create,
57
                      OSSL_PROPERTY_IDX *idx)
58
80.8k
{
59
80.8k
    char name[100];
60
80.8k
    int err = 0;
61
80.8k
    size_t i = 0;
62
80.8k
    const char *s = *t;
63
80.8k
    int user_name = 0;
64
65
87.9k
    for (;;) {
66
87.9k
        if (!ossl_isalpha(*s)) {
67
928
            ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_IDENTIFIER,
68
928
                           "HERE-->%s", *t);
69
928
            return 0;
70
928
        }
71
416k
        do {
72
416k
            if (i < sizeof(name) - 1)
73
377k
                name[i++] = ossl_tolower(*s);
74
38.7k
            else
75
38.7k
                err = 1;
76
416k
        } while (*++s == '_' || ossl_isalnum(*s));
77
87.0k
        if (*s != '.')
78
79.9k
            break;
79
7.10k
        user_name = 1;
80
7.10k
        if (i < sizeof(name) - 1)
81
6.80k
            name[i++] = *s;
82
298
        else
83
298
            err = 1;
84
7.10k
        s++;
85
7.10k
    }
86
79.9k
    name[i] = '\0';
87
79.9k
    if (err) {
88
98
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NAME_TOO_LONG, "HERE-->%s", *t);
89
98
        return 0;
90
98
    }
91
79.8k
    *t = skip_space(s);
92
79.8k
    *idx = ossl_property_name(ctx, name, user_name && create);
93
79.8k
    return 1;
94
79.9k
}
95
96
static int parse_number(const char *t[], OSSL_PROPERTY_DEFINITION *res)
97
885
{
98
885
    const char *s = *t;
99
885
    int64_t v = 0;
100
101
3.45k
    do {
102
3.45k
        if (!ossl_isdigit(*s)) {
103
37
            ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT,
104
37
                           "HERE-->%s", *t);
105
37
            return 0;
106
37
        }
107
        /* overflow check */
108
3.41k
        if (v > ((INT64_MAX - (*s - '0')) / 10)) {
109
78
            ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
110
78
                           "Property %s overflows", *t);
111
78
            return 0;
112
78
        }
113
3.33k
        v = v * 10 + (*s++ - '0');
114
3.33k
    } while (ossl_isdigit(*s));
115
770
    if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
116
142
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT,
117
142
                       "HERE-->%s", *t);
118
142
        return 0;
119
142
    }
120
628
    *t = skip_space(s);
121
628
    res->type = OSSL_PROPERTY_TYPE_NUMBER;
122
628
    res->v.int_val = v;
123
628
    return 1;
124
770
}
125
126
static int parse_hex(const char *t[], OSSL_PROPERTY_DEFINITION *res)
127
386
{
128
386
    const char *s = *t;
129
386
    int64_t v = 0;
130
386
    int sval;
131
132
2.65k
    do {
133
2.65k
        if (ossl_isdigit(*s)) {
134
1.69k
            sval = *s - '0';
135
1.69k
        } else if (ossl_isxdigit(*s)) {
136
938
            sval = ossl_tolower(*s) - 'a' + 10;
137
938
        } 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
2.63k
        if (v > ((INT64_MAX - sval) / 16)) {
144
83
            ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
145
83
                           "Property %s overflows", *t);
146
83
            return 0;
147
83
        }
148
149
2.54k
        v <<= 4;
150
2.54k
        v += sval;
151
2.54k
    } while (ossl_isxdigit(*++s));
152
282
    if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
153
55
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT,
154
55
                       "HERE-->%s", *t);
155
55
        return 0;
156
55
    }
157
227
    *t = skip_space(s);
158
227
    res->type = OSSL_PROPERTY_TYPE_NUMBER;
159
227
    res->v.int_val = v;
160
227
    return 1;
161
282
}
162
163
static int parse_oct(const char *t[], OSSL_PROPERTY_DEFINITION *res)
164
256
{
165
256
    const char *s = *t;
166
256
    int64_t v = 0;
167
168
2.98k
    do {
169
2.98k
        if (*s == '9' || *s == '8' || !ossl_isdigit(*s)) {
170
33
            ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT,
171
33
                           "HERE-->%s", *t);
172
33
            return 0;
173
33
        }
174
2.94k
        if (v > ((INT64_MAX - (*s - '0')) / 8)) {
175
52
            ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
176
52
                           "Property %s overflows", *t);
177
52
            return 0;
178
52
        }
179
180
2.89k
        v = (v << 3) + (*s - '0');
181
2.89k
    } while (ossl_isdigit(*++s) && *s != '9' && *s != '8');
182
171
    if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
183
88
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT,
184
88
                       "HERE-->%s", *t);
185
88
        return 0;
186
88
    }
187
83
    *t = skip_space(s);
188
83
    res->type = OSSL_PROPERTY_TYPE_NUMBER;
189
83
    res->v.int_val = v;
190
83
    return 1;
191
171
}
192
193
static int parse_string(OSSL_LIB_CTX *ctx, const char *t[], char delim,
194
                        OSSL_PROPERTY_DEFINITION *res, const int create)
195
207
{
196
207
    char v[1000];
197
207
    const char *s = *t;
198
207
    size_t i = 0;
199
207
    int err = 0;
200
201
82.4k
    while (*s != '\0' && *s != delim) {
202
82.2k
        if (i < sizeof(v) - 1)
203
77.3k
            v[i++] = *s;
204
4.90k
        else
205
4.90k
            err = 1;
206
82.2k
        s++;
207
82.2k
    }
208
207
    if (*s == '\0') {
209
106
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_MATCHING_STRING_DELIMITER,
210
106
                       "HERE-->%c%s", delim, *t);
211
106
        return 0;
212
106
    }
213
101
    v[i] = '\0';
214
101
    if (err) {
215
21
        ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
216
80
    } else {
217
80
        res->v.str_val = ossl_property_value(ctx, v, create);
218
80
    }
219
101
    *t = skip_space(s + 1);
220
101
    res->type = OSSL_PROPERTY_TYPE_STRING;
221
101
    return !err;
222
207
}
223
224
static int parse_unquoted(OSSL_LIB_CTX *ctx, const char *t[],
225
                          OSSL_PROPERTY_DEFINITION *res, const int create)
226
34.6k
{
227
34.6k
    char v[1000];
228
34.6k
    const char *s = *t;
229
34.6k
    size_t i = 0;
230
34.6k
    int err = 0;
231
232
34.6k
    if (*s == '\0' || *s == ',')
233
0
        return 0;
234
1.11M
    while (ossl_isprint(*s) && !ossl_isspace(*s) && *s != ',') {
235
1.08M
        if (i < sizeof(v) - 1)
236
839k
            v[i++] = ossl_tolower(*s);
237
241k
        else
238
241k
            err = 1;
239
1.08M
        s++;
240
1.08M
    }
241
34.6k
    if (!ossl_isspace(*s) && *s != '\0' && *s != ',') {
242
111
        ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_ASCII_CHARACTER,
243
111
                       "HERE-->%s", s);
244
111
        return 0;
245
111
    }
246
34.5k
    v[i] = 0;
247
34.5k
    if (err)
248
516
        ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
249
34.0k
    else if ((res->v.str_val = ossl_property_value(ctx, v, create)) == 0)
250
6.09k
        err = 1;
251
34.5k
    *t = skip_space(s);
252
34.5k
    res->type = OSSL_PROPERTY_TYPE_STRING;
253
34.5k
    return !err;
254
34.6k
}
255
256
static int parse_value(OSSL_LIB_CTX *ctx, const char *t[],
257
                       OSSL_PROPERTY_DEFINITION *res, int create)
258
36.4k
{
259
36.4k
    const char *s = *t;
260
36.4k
    int r = 0;
261
262
36.4k
    if (*s == '"' || *s == '\'') {
263
207
        s++;
264
207
        r = parse_string(ctx, &s, s[-1], res, create);
265
36.2k
    } else if (*s == '+') {
266
48
        s++;
267
48
        r = parse_number(&s, res);
268
36.2k
    } else if (*s == '-') {
269
154
        s++;
270
154
        r = parse_number(&s, res);
271
154
        res->v.int_val = -res->v.int_val;
272
36.0k
    } else if (*s == '0' && s[1] == 'x') {
273
386
        s += 2;
274
386
        r = parse_hex(&s, res);
275
35.6k
    } else if (*s == '0' && ossl_isdigit(s[1])) {
276
256
        s++;
277
256
        r = parse_oct(&s, res);
278
35.4k
    } else if (ossl_isdigit(*s)) {
279
683
        return parse_number(t, res);
280
34.7k
    } else if (ossl_isalpha(*s))
281
34.6k
        return parse_unquoted(ctx, t, res, create);
282
1.17k
    if (r)
283
470
        *t = s;
284
1.17k
    return r;
285
36.4k
}
286
287
static int pd_compare(const OSSL_PROPERTY_DEFINITION *const *p1,
288
                      const OSSL_PROPERTY_DEFINITION *const *p2)
289
52.2k
{
290
52.2k
    const OSSL_PROPERTY_DEFINITION *pd1 = *p1;
291
52.2k
    const OSSL_PROPERTY_DEFINITION *pd2 = *p2;
292
293
52.2k
    if (pd1->name_idx < pd2->name_idx)
294
29.5k
        return -1;
295
22.6k
    if (pd1->name_idx > pd2->name_idx)
296
9.44k
        return 1;
297
13.2k
    return 0;
298
22.6k
}
299
300
static void pd_free(OSSL_PROPERTY_DEFINITION *pd)
301
79.8k
{
302
79.8k
    OPENSSL_free(pd);
303
79.8k
}
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.0k
{
315
10.0k
    const int n = sk_OSSL_PROPERTY_DEFINITION_num(sk);
316
10.0k
    OSSL_PROPERTY_LIST *r;
317
10.0k
    OSSL_PROPERTY_IDX prev_name_idx = 0;
318
10.0k
    int i;
319
320
10.0k
    r = OPENSSL_malloc(sizeof(*r)
321
10.0k
                       + (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0]));
322
10.0k
    if (r != NULL) {
323
10.0k
        sk_OSSL_PROPERTY_DEFINITION_sort(sk);
324
325
10.0k
        r->has_optional = 0;
326
38.9k
        for (i = 0; i < n; i++) {
327
29.1k
            r->properties[i] = *sk_OSSL_PROPERTY_DEFINITION_value(sk, i);
328
29.1k
            r->has_optional |= r->properties[i].optional;
329
330
            /* Check for duplicated names */
331
29.1k
            if (i > 0 && r->properties[i].name_idx == prev_name_idx) {
332
306
                OPENSSL_free(r);
333
306
                ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED,
334
306
                               "Duplicated name `%s'",
335
306
                               ossl_property_name_str(ctx, prev_name_idx));
336
306
                return NULL;
337
306
            }
338
28.8k
            prev_name_idx = r->properties[i].name_idx;
339
28.8k
        }
340
9.77k
        r->num_properties = n;
341
9.77k
    }
342
9.77k
    return r;
343
10.0k
}
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
5.59k
{
407
5.59k
    STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
408
5.59k
    OSSL_PROPERTY_LIST *res = NULL;
409
5.59k
    OSSL_PROPERTY_DEFINITION *prop = NULL;
410
5.59k
    int done;
411
412
5.59k
    if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
413
0
        return NULL;
414
415
5.59k
    s = skip_space(s);
416
5.59k
    done = *s == '\0';
417
57.5k
    while (!done) {
418
52.9k
        prop = OPENSSL_malloc(sizeof(*prop));
419
52.9k
        if (prop == NULL)
420
0
            goto err;
421
52.9k
        memset(&prop->v, 0, sizeof(prop->v));
422
423
52.9k
        if (match_ch(&s, '-')) {
424
378
            prop->oper = OSSL_PROPERTY_OVERRIDE;
425
378
            prop->optional = 0;
426
378
            if (!parse_name(ctx, &s, 1, &prop->name_idx))
427
54
                goto err;
428
324
            goto skip_value;
429
378
        }
430
52.5k
        prop->optional = match_ch(&s, '?');
431
52.5k
        if (!parse_name(ctx, &s, 1, &prop->name_idx))
432
972
            goto err;
433
434
51.6k
        if (match_ch(&s, '=')) {
435
8.23k
            prop->oper = OSSL_PROPERTY_OPER_EQ;
436
43.3k
        } else if (MATCH(&s, "!=")) {
437
358
            prop->oper = OSSL_PROPERTY_OPER_NE;
438
43.0k
        } else {
439
            /* A name alone is a Boolean comparison for true */
440
43.0k
            prop->oper = OSSL_PROPERTY_OPER_EQ;
441
43.0k
            prop->type = OSSL_PROPERTY_TYPE_STRING;
442
43.0k
            prop->v.str_val = OSSL_PROPERTY_TRUE;
443
43.0k
            goto skip_value;
444
43.0k
        }
445
8.59k
        if (!parse_value(ctx, &s, prop, create_values))
446
7.55k
            prop->type = OSSL_PROPERTY_TYPE_VALUE_UNDEFINED;
447
448
51.9k
skip_value:
449
51.9k
        if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
450
0
            goto err;
451
51.9k
        prop = NULL;
452
51.9k
        done = !match_ch(&s, ',');
453
51.9k
    }
454
4.57k
    if (*s != '\0') {
455
1.80k
        ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS,
456
1.80k
                       "HERE-->%s", s);
457
1.80k
        goto err;
458
1.80k
    }
459
2.76k
    res = stack_to_property_list(ctx, sk);
460
461
5.59k
err:
462
5.59k
    OPENSSL_free(prop);
463
5.59k
    sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
464
5.59k
    return res;
465
2.76k
}
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.46k
{
474
2.46k
    const OSSL_PROPERTY_DEFINITION *const q = query->properties;
475
2.46k
    const OSSL_PROPERTY_DEFINITION *const d = defn->properties;
476
2.46k
    int i = 0, j = 0, matches = 0;
477
2.46k
    OSSL_PROPERTY_OPER oper;
478
479
2.97k
    while (i < query->num_properties) {
480
634
        if ((oper = q[i].oper) == OSSL_PROPERTY_OVERRIDE) {
481
157
            i++;
482
157
            continue;
483
157
        }
484
477
        if (j < defn->num_properties) {
485
323
            if (q[i].name_idx > d[j].name_idx) {  /* skip defn, not in query */
486
100
                j++;
487
100
                continue;
488
100
            }
489
223
            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
223
        }
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
133
            if (oper == OSSL_PROPERTY_OPER_NE)
511
96
                matches++;
512
37
            else if (!q[i].optional)
513
10
                return -1;
514
230
        } else if (q[i].type != OSSL_PROPERTY_TYPE_STRING
515
230
                   || (oper == OSSL_PROPERTY_OPER_EQ
516
184
                       && q[i].v.str_val != OSSL_PROPERTY_FALSE)
517
230
                   || (oper == OSSL_PROPERTY_OPER_NE
518
192
                       && q[i].v.str_val == OSSL_PROPERTY_FALSE)) {
519
192
            if (!q[i].optional)
520
108
                return -1;
521
192
        } else {
522
38
            matches++;
523
38
        }
524
245
        i++;
525
245
    }
526
2.34k
    return matches;
527
2.46k
}
528
529
void ossl_property_free(OSSL_PROPERTY_LIST *p)
530
18.8k
{
531
18.8k
    OPENSSL_free(p);
532
18.8k
}
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
571
0
    return r;
572
0
}
573
574
int ossl_property_parse_init(OSSL_LIB_CTX *ctx)
575
377
{
576
377
    static const char *const predefined_names[] = {
577
377
        "provider",     /* Name of provider (default, legacy, fips) */
578
377
        "version",      /* Version number of this provider */
579
377
        "fips",         /* FIPS validated or FIPS supporting algorithm */
580
377
        "output",       /* Output type for encoders */
581
377
        "input",        /* Input type for decoders */
582
377
        "structure",    /* Structure name for encoders and decoders */
583
377
    };
584
377
    size_t i;
585
586
2.63k
    for (i = 0; i < OSSL_NELEM(predefined_names); i++)
587
2.26k
        if (ossl_property_name(ctx, predefined_names[i], 1) == 0)
588
0
            goto err;
589
590
    /*
591
     * Pre-populate the two Boolean values. We must do them before any other
592
     * values and in this order so that we get the same index as the global
593
     * OSSL_PROPERTY_TRUE and OSSL_PROPERTY_FALSE values
594
     */
595
377
    if ((ossl_property_value(ctx, "yes", 1) != OSSL_PROPERTY_TRUE)
596
377
        || (ossl_property_value(ctx, "no", 1) != OSSL_PROPERTY_FALSE))
597
0
        goto err;
598
599
377
    return 1;
600
0
err:
601
0
    return 0;
602
377
}
603
604
static void put_char(char ch, char **buf, size_t *remain, size_t *needed)
605
0
{
606
0
    if (*remain == 0) {
607
0
        ++*needed;
608
0
        return;
609
0
    }
610
0
    if (*remain == 1)
611
0
        **buf = '\0';
612
0
    else
613
0
        **buf = ch;
614
0
    ++*buf;
615
0
    ++*needed;
616
0
    --*remain;
617
0
}
618
619
static void put_str(const char *str, char **buf, size_t *remain, size_t *needed)
620
0
{
621
0
    size_t olen, len, i;
622
0
    char quote = '\0';
623
0
    int quotes;
624
625
0
    len = olen = strlen(str);
626
0
    *needed += len;
627
628
    /*
629
     * Check to see if we need quotes or not.
630
     * Characters that are legal in a PropertyName don't need quoting.
631
     * We simply assume all others require quotes.
632
     */
633
0
    for (i = 0; i < len; i++)
634
0
        if (!ossl_isalnum(str[i]) && str[i] != '.' && str[i] != '_') {
635
            /* Default to single quotes ... */
636
0
            if (quote == '\0')
637
0
                quote = '\'';
638
            /* ... but use double quotes if a single is present */
639
0
            if (str[i] == '\'')
640
0
                quote = '"';
641
0
        }
642
643
0
    quotes = quote != '\0';
644
0
    if (*remain == 0) {
645
0
        *needed += 2 * quotes;
646
0
        return;
647
0
    }
648
649
0
    if (quotes)
650
0
        put_char(quote, buf, remain, needed);
651
652
0
    if (*remain < len + 1 + quotes)
653
0
        len = *remain - 1;
654
655
0
    if (len > 0) {
656
0
        memcpy(*buf, str, len);
657
0
        *buf += len;
658
0
        *remain -= len;
659
0
    }
660
661
0
    if (quotes)
662
0
        put_char(quote, buf, remain, needed);
663
664
0
    if (len < olen && *remain == 1) {
665
0
        **buf = '\0';
666
0
        ++*buf;
667
0
        --*remain;
668
0
    }
669
0
}
670
671
static void put_num(int64_t val, char **buf, size_t *remain, size_t *needed)
672
0
{
673
0
    int64_t tmpval = val;
674
0
    size_t len = 1;
675
676
0
    if (tmpval < 0) {
677
0
        len++;
678
0
        tmpval = -tmpval;
679
0
    }
680
0
    for (; tmpval > 9; len++, tmpval /= 10);
681
682
0
    *needed += len;
683
684
0
    if (*remain == 0)
685
0
        return;
686
687
0
    BIO_snprintf(*buf, *remain, "%lld", (long long int)val);
688
0
    if (*remain < len) {
689
0
        *buf += *remain;
690
0
        *remain = 0;
691
0
    } else {
692
0
        *buf += len;
693
0
        *remain -= len;
694
0
    }
695
0
}
696
697
size_t ossl_property_list_to_string(OSSL_LIB_CTX *ctx,
698
                                    const OSSL_PROPERTY_LIST *list, char *buf,
699
                                    size_t bufsize)
700
0
{
701
0
    int i;
702
0
    const OSSL_PROPERTY_DEFINITION *prop = NULL;
703
0
    size_t needed = 0;
704
0
    const char *val;
705
706
0
    if (list == NULL) {
707
0
        if (bufsize > 0)
708
0
            *buf = '\0';
709
0
        return 1;
710
0
    }
711
0
    if (list->num_properties != 0)
712
0
        prop = &list->properties[list->num_properties - 1];
713
0
    for (i = 0; i < list->num_properties; i++, prop--) {
714
        /* Skip invalid names */
715
0
        if (prop->name_idx == 0)
716
0
            continue;
717
718
0
        if (needed > 0)
719
0
            put_char(',', &buf, &bufsize, &needed);
720
721
0
        if (prop->optional)
722
0
            put_char('?', &buf, &bufsize, &needed);
723
0
        else if (prop->oper == OSSL_PROPERTY_OVERRIDE)
724
0
            put_char('-', &buf, &bufsize, &needed);
725
726
0
        val = ossl_property_name_str(ctx, prop->name_idx);
727
0
        if (val == NULL)
728
0
            return 0;
729
0
        put_str(val, &buf, &bufsize, &needed);
730
731
0
        switch (prop->oper) {
732
0
            case OSSL_PROPERTY_OPER_NE:
733
0
                put_char('!', &buf, &bufsize, &needed);
734
                /* fall through */
735
0
            case OSSL_PROPERTY_OPER_EQ:
736
0
                put_char('=', &buf, &bufsize, &needed);
737
                /* put value */
738
0
                switch (prop->type) {
739
0
                case OSSL_PROPERTY_TYPE_STRING:
740
0
                    val = ossl_property_value_str(ctx, prop->v.str_val);
741
0
                    if (val == NULL)
742
0
                        return 0;
743
0
                    put_str(val, &buf, &bufsize, &needed);
744
0
                    break;
745
746
0
                case OSSL_PROPERTY_TYPE_NUMBER:
747
0
                    put_num(prop->v.int_val, &buf, &bufsize, &needed);
748
0
                    break;
749
750
0
                default:
751
0
                    return 0;
752
0
                }
753
0
                break;
754
0
            default:
755
                /* do nothing */
756
0
                break;
757
0
        }
758
0
    }
759
760
0
    put_char('\0', &buf, &bufsize, &needed);
761
0
    return needed;
762
0
}