Coverage Report

Created: 2024-11-21 07:03

/src/openssl/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 "internal/e_os.h"
22
23
DEFINE_STACK_OF(OSSL_PROPERTY_DEFINITION)
24
25
static const char *skip_space(const char *s)
26
286
{
27
286
    while (ossl_isspace(*s))
28
0
        s++;
29
286
    return s;
30
286
}
31
32
static int match_ch(const char *t[], char m)
33
8
{
34
8
    const char *s = *t;
35
36
8
    if (*s == m) {
37
4
        *t = skip_space(s + 1);
38
4
        return 1;
39
4
    }
40
4
    return 0;
41
8
}
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
4
{
59
4
    char name[100];
60
4
    int err = 0;
61
4
    size_t i = 0;
62
4
    const char *s = *t;
63
4
    int user_name = 0;
64
65
4
    for (;;) {
66
4
        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
24
        do {
72
24
            if (i < sizeof(name) - 1)
73
24
                name[i++] = ossl_tolower(*s);
74
0
            else
75
0
                err = 1;
76
24
        } while (*++s == '_' || ossl_isalnum(*s));
77
4
        if (*s != '.')
78
4
            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
4
    name[i] = '\0';
87
4
    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
4
    *t = skip_space(s);
92
4
    *idx = ossl_property_name(ctx, name, user_name && create);
93
4
    return 1;
94
4
}
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
2
{
227
2
    char v[1000];
228
2
    const char *s = *t;
229
2
    size_t i = 0;
230
2
    int err = 0;
231
232
2
    if (*s == '\0' || *s == ',')
233
0
        return 0;
234
16
    while (ossl_isprint(*s) && !ossl_isspace(*s) && *s != ',') {
235
14
        if (i < sizeof(v) - 1)
236
14
            v[i++] = ossl_tolower(*s);
237
0
        else
238
0
            err = 1;
239
14
        s++;
240
14
    }
241
2
    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
2
    v[i] = 0;
247
2
    if (err)
248
0
        ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t);
249
2
    else if ((res->v.str_val = ossl_property_value(ctx, v, create)) == 0)
250
0
        err = 1;
251
2
    *t = skip_space(s);
252
2
    res->type = OSSL_PROPERTY_TYPE_STRING;
253
2
    return !err;
254
2
}
255
256
static int parse_value(OSSL_LIB_CTX *ctx, const char *t[],
257
                       OSSL_PROPERTY_DEFINITION *res, int create)
258
2
{
259
2
    const char *s = *t;
260
2
    int r = 0;
261
262
2
    if (*s == '"' || *s == '\'') {
263
0
        s++;
264
0
        r = parse_string(ctx, &s, s[-1], res, create);
265
2
    } else if (*s == '+') {
266
0
        s++;
267
0
        r = parse_number(&s, res);
268
2
    } else if (*s == '-') {
269
0
        s++;
270
0
        r = parse_number(&s, res);
271
0
        res->v.int_val = -res->v.int_val;
272
2
    } else if (*s == '0' && s[1] == 'x') {
273
0
        s += 2;
274
0
        r = parse_hex(&s, res);
275
2
    } else if (*s == '0' && ossl_isdigit(s[1])) {
276
0
        s++;
277
0
        r = parse_oct(&s, res);
278
2
    } else if (ossl_isdigit(*s)) {
279
0
        return parse_number(t, res);
280
2
    } else if (ossl_isalpha(*s))
281
2
        return parse_unquoted(ctx, t, res, create);
282
0
    if (r)
283
0
        *t = s;
284
0
    return r;
285
2
}
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
4
{
302
4
    OPENSSL_free(pd);
303
4
}
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
276
{
315
276
    const int n = sk_OSSL_PROPERTY_DEFINITION_num(sk);
316
276
    OSSL_PROPERTY_LIST *r;
317
276
    OSSL_PROPERTY_IDX prev_name_idx = 0;
318
276
    int i;
319
320
276
    r = OPENSSL_malloc(sizeof(*r)
321
276
                       + (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0]));
322
276
    if (r != NULL) {
323
276
        sk_OSSL_PROPERTY_DEFINITION_sort(sk);
324
325
276
        r->has_optional = 0;
326
280
        for (i = 0; i < n; i++) {
327
4
            r->properties[i] = *sk_OSSL_PROPERTY_DEFINITION_value(sk, i);
328
4
            r->has_optional |= r->properties[i].optional;
329
330
            /* Check for duplicated names */
331
4
            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
4
            prev_name_idx = r->properties[i].name_idx;
339
4
        }
340
276
        r->num_properties = n;
341
276
    }
342
276
    return r;
343
276
}
344
345
OSSL_PROPERTY_LIST *ossl_parse_property(OSSL_LIB_CTX *ctx, const char *defn)
346
2
{
347
2
    OSSL_PROPERTY_DEFINITION *prop = NULL;
348
2
    OSSL_PROPERTY_LIST *res = NULL;
349
2
    STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
350
2
    const char *s = defn;
351
2
    int done;
352
353
2
    if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
354
0
        return NULL;
355
356
2
    s = skip_space(s);
357
2
    done = *s == '\0';
358
4
    while (!done) {
359
2
        const char *start = s;
360
361
2
        prop = OPENSSL_malloc(sizeof(*prop));
362
2
        if (prop == NULL)
363
0
            goto err;
364
2
        memset(&prop->v, 0, sizeof(prop->v));
365
2
        prop->optional = 0;
366
2
        if (!parse_name(ctx, &s, 1, &prop->name_idx))
367
0
            goto err;
368
2
        prop->oper = OSSL_PROPERTY_OPER_EQ;
369
2
        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
2
        if (match_ch(&s, '=')) {
375
2
            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
2
        } 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
2
        if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
387
0
            goto err;
388
2
        prop = NULL;
389
2
        done = !match_ch(&s, ',');
390
2
    }
391
2
    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
2
    res = stack_to_property_list(ctx, sk);
397
398
2
err:
399
2
    OPENSSL_free(prop);
400
2
    sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
401
2
    return res;
402
2
}
403
404
OSSL_PROPERTY_LIST *ossl_parse_query(OSSL_LIB_CTX *ctx, const char *s,
405
                                     int create_values)
406
274
{
407
274
    STACK_OF(OSSL_PROPERTY_DEFINITION) *sk;
408
274
    OSSL_PROPERTY_LIST *res = NULL;
409
274
    OSSL_PROPERTY_DEFINITION *prop = NULL;
410
274
    int done;
411
412
274
    if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL)
413
0
        return NULL;
414
415
274
    s = skip_space(s);
416
274
    done = *s == '\0';
417
276
    while (!done) {
418
2
        prop = OPENSSL_malloc(sizeof(*prop));
419
2
        if (prop == NULL)
420
0
            goto err;
421
2
        memset(&prop->v, 0, sizeof(prop->v));
422
423
2
        if (match_ch(&s, '-')) {
424
2
            prop->oper = OSSL_PROPERTY_OVERRIDE;
425
2
            prop->optional = 0;
426
2
            if (!parse_name(ctx, &s, 1, &prop->name_idx))
427
0
                goto err;
428
2
            goto skip_value;
429
2
        }
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
2
skip_value:
449
2
        if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop))
450
0
            goto err;
451
2
        prop = NULL;
452
2
        done = !match_ch(&s, ',');
453
2
    }
454
274
    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
274
    res = stack_to_property_list(ctx, sk);
460
461
274
err:
462
274
    OPENSSL_free(prop);
463
274
    sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free);
464
274
    return res;
465
274
}
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
274
{
474
274
    const OSSL_PROPERTY_DEFINITION *const q = query->properties;
475
274
    const OSSL_PROPERTY_DEFINITION *const d = defn->properties;
476
274
    int i = 0, j = 0, matches = 0;
477
274
    OSSL_PROPERTY_OPER oper;
478
479
276
    while (i < query->num_properties) {
480
2
        if ((oper = q[i].oper) == OSSL_PROPERTY_OVERRIDE) {
481
2
            i++;
482
2
            continue;
483
2
        }
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
274
    return matches;
527
274
}
528
529
void ossl_property_free(OSSL_PROPERTY_LIST *p)
530
280
{
531
280
    OPENSSL_free(p);
532
280
}
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
4
{
577
4
    static const char *const predefined_names[] = {
578
4
        "provider",     /* Name of provider (default, legacy, fips) */
579
4
        "version",      /* Version number of this provider */
580
4
        "fips",         /* FIPS validated or FIPS supporting algorithm */
581
4
        "output",       /* Output type for encoders */
582
4
        "input",        /* Input type for decoders */
583
4
        "structure",    /* Structure name for encoders and decoders */
584
4
    };
585
4
    size_t i;
586
587
28
    for (i = 0; i < OSSL_NELEM(predefined_names); i++)
588
24
        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
4
    if ((ossl_property_value(ctx, "yes", 1) != OSSL_PROPERTY_TRUE)
597
4
        || (ossl_property_value(ctx, "no", 1) != OSSL_PROPERTY_FALSE))
598
0
        goto err;
599
600
4
    return 1;
601
0
err:
602
0
    return 0;
603
4
}
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
}