Coverage Report

Created: 2025-06-13 06:56

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