Coverage Report

Created: 2023-06-08 06:40

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