Coverage Report

Created: 2023-09-25 06:45

/src/openssl30/crypto/params.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2023 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 <openssl/params.h>
13
#include "internal/thread_once.h"
14
#include "internal/numbers.h"
15
#include "internal/endian.h"
16
17
#ifndef OPENSSL_SYS_UEFI
18
/*
19
 * Return the number of bits in the mantissa of a double.  This is used to
20
 * shift a larger integral value to determine if it will exactly fit into a
21
 * double.
22
 */
23
static unsigned int real_shift(void)
24
0
{
25
0
    return sizeof(double) == 4 ? 24 : 53;
26
0
}
27
#endif
28
29
OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
30
24.4M
{
31
24.4M
    if (p != NULL && key != NULL)
32
104M
        for (; p->key != NULL; p++)
33
95.8M
            if (strcmp(key, p->key) == 0)
34
15.7M
                return p;
35
8.75M
    return NULL;
36
24.4M
}
37
38
const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
39
18.3M
{
40
18.3M
    return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
41
18.3M
}
42
43
static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
44
                                       void *data, size_t data_size)
45
4.15M
{
46
4.15M
    OSSL_PARAM res;
47
48
4.15M
    res.key = key;
49
4.15M
    res.data_type = data_type;
50
4.15M
    res.data = data;
51
4.15M
    res.data_size = data_size;
52
4.15M
    res.return_size = OSSL_PARAM_UNMODIFIED;
53
4.15M
    return res;
54
4.15M
}
55
56
int OSSL_PARAM_modified(const OSSL_PARAM *p)
57
94.0k
{
58
94.0k
    return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
59
94.0k
}
60
61
void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
62
0
{
63
0
    if (p != NULL)
64
0
        while (p->key != NULL)
65
0
            p++->return_size = OSSL_PARAM_UNMODIFIED;
66
0
}
67
68
/* Return non-zero if the signed number is negative */
69
static int is_negative(const void *number, size_t s)
70
0
{
71
0
    const unsigned char *n = number;
72
0
    DECLARE_IS_ENDIAN;
73
74
0
    return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
75
0
}
76
77
/* Check that all the bytes specified match the expected sign byte */
78
static int check_sign_bytes(const unsigned char *p, size_t n, unsigned char s)
79
0
{
80
0
    size_t i;
81
82
0
    for (i = 0; i < n; i++)
83
0
        if (p[i] != s)
84
0
            return 0;
85
0
    return 1;
86
0
}
87
88
/*
89
 * Copy an integer to another integer.
90
 * Handle different length integers and signed and unsigned integers.
91
 * Both integers are in native byte ordering.
92
 */
93
static int copy_integer(unsigned char *dest, size_t dest_len,
94
                        const unsigned char *src, size_t src_len,
95
                        unsigned char pad, int signed_int)
96
0
{
97
0
    size_t n;
98
0
    DECLARE_IS_ENDIAN;
99
100
0
    if (IS_BIG_ENDIAN) {
101
0
        if (src_len < dest_len) {
102
0
            n = dest_len - src_len;
103
0
            memset(dest, pad, n);
104
0
            memcpy(dest + n, src, src_len);
105
0
        } else {
106
0
            n = src_len - dest_len;
107
0
            if (!check_sign_bytes(src, n, pad)
108
                    /*
109
                     * Shortening a signed value must retain the correct sign.
110
                     * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
111
                     */
112
0
                    || (signed_int && ((pad ^ src[n]) & 0x80) != 0))
113
0
                return 0;
114
0
            memcpy(dest, src + n, dest_len);
115
0
        }
116
0
    } else /* IS_LITTLE_ENDIAN */ {
117
0
        if (src_len < dest_len) {
118
0
            n = dest_len - src_len;
119
0
            memset(dest + src_len, pad, n);
120
0
            memcpy(dest, src, src_len);
121
0
        } else {
122
0
            n = src_len - dest_len;
123
0
            if (!check_sign_bytes(src + dest_len, n, pad)
124
                    /*
125
                     * Shortening a signed value must retain the correct sign.
126
                     * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
127
                     */
128
0
                    || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0))
129
0
                return 0;
130
0
            memcpy(dest, src, dest_len);
131
0
        }
132
0
    }
133
0
    return 1;
134
0
}
135
136
/* Copy a signed number to a signed number of possibly different length */
137
static int signed_from_signed(void *dest, size_t dest_len,
138
                              const void *src, size_t src_len)
139
0
{
140
0
    return copy_integer(dest, dest_len, src, src_len,
141
0
                        is_negative(src, src_len) ? 0xff : 0, 1);
142
0
}
143
144
/* Copy an unsigned number to a signed number of possibly different length */
145
static int signed_from_unsigned(void *dest, size_t dest_len,
146
                                const void *src, size_t src_len)
147
0
{
148
0
    return copy_integer(dest, dest_len, src, src_len, 0, 1);
149
0
}
150
151
/* Copy a signed number to an unsigned number of possibly different length */
152
static int unsigned_from_signed(void *dest, size_t dest_len,
153
                                const void *src, size_t src_len)
154
0
{
155
0
    if (is_negative(src, src_len))
156
0
        return 0;
157
0
    return copy_integer(dest, dest_len, src, src_len, 0, 0);
158
0
}
159
160
/* Copy an unsigned number to an unsigned number of possibly different length */
161
static int unsigned_from_unsigned(void *dest, size_t dest_len,
162
                                  const void *src, size_t src_len)
163
0
{
164
0
    return copy_integer(dest, dest_len, src, src_len, 0, 0);
165
0
}
166
167
/* General purpose get integer parameter call that handles odd sizes */
168
static int general_get_int(const OSSL_PARAM *p, void *val, size_t val_size)
169
0
{
170
0
    if (p->data_type == OSSL_PARAM_INTEGER)
171
0
        return signed_from_signed(val, val_size, p->data, p->data_size);
172
0
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
173
0
        return signed_from_unsigned(val, val_size, p->data, p->data_size);
174
0
    return 0;
175
0
}
176
177
/* General purpose set integer parameter call that handles odd sizes */
178
static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
179
0
{
180
0
    int r = 0;
181
182
0
    p->return_size = val_size; /* Expected size */
183
0
    if (p->data == NULL)
184
0
        return 1;
185
0
    if (p->data_type == OSSL_PARAM_INTEGER)
186
0
        r = signed_from_signed(p->data, p->data_size, val, val_size);
187
0
    else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
188
0
        r = unsigned_from_signed(p->data, p->data_size, val, val_size);
189
0
    p->return_size = r ? p->data_size : val_size;
190
0
    return r;
191
0
}
192
193
/* General purpose get unsigned integer parameter call that handles odd sizes */
194
static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
195
0
{
196
0
    if (p->data_type == OSSL_PARAM_INTEGER)
197
0
        return unsigned_from_signed(val, val_size, p->data, p->data_size);
198
0
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
199
0
        return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
200
0
    return 0;
201
0
}
202
203
/* General purpose set unsigned integer parameter call that handles odd sizes */
204
static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
205
0
{
206
0
    int r = 0;
207
208
0
    p->return_size = val_size; /* Expected size */
209
0
    if (p->data == NULL)
210
0
        return 1;
211
0
    if (p->data_type == OSSL_PARAM_INTEGER)
212
0
        r = signed_from_unsigned(p->data, p->data_size, val, val_size);
213
0
    else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
214
0
        r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
215
0
    p->return_size = r ? p->data_size : val_size;
216
0
    return r;
217
0
}
218
219
int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
220
5.31M
{
221
5.31M
#ifndef OPENSSL_SMALL_FOOTPRINT
222
5.31M
    switch (sizeof(int)) {
223
5.31M
    case sizeof(int32_t):
224
5.31M
        return OSSL_PARAM_get_int32(p, (int32_t *)val);
225
0
    case sizeof(int64_t):
226
0
        return OSSL_PARAM_get_int64(p, (int64_t *)val);
227
5.31M
    }
228
0
#endif
229
0
    return general_get_int(p, val, sizeof(*val));
230
5.31M
}
231
232
int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
233
711k
{
234
711k
#ifndef OPENSSL_SMALL_FOOTPRINT
235
711k
    switch (sizeof(int)) {
236
711k
    case sizeof(int32_t):
237
711k
        return OSSL_PARAM_set_int32(p, (int32_t)val);
238
0
    case sizeof(int64_t):
239
0
        return OSSL_PARAM_set_int64(p, (int64_t)val);
240
711k
    }
241
0
#endif
242
0
    return general_set_int(p, &val, sizeof(val));
243
711k
}
244
245
OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
246
1.39M
{
247
1.39M
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
248
1.39M
}
249
250
int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
251
2.51M
{
252
2.51M
#ifndef OPENSSL_SMALL_FOOTPRINT
253
2.51M
    switch (sizeof(unsigned int)) {
254
2.51M
    case sizeof(uint32_t):
255
2.51M
        return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
256
0
    case sizeof(uint64_t):
257
0
        return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
258
2.51M
    }
259
0
#endif
260
0
    return general_get_uint(p, val, sizeof(*val));
261
2.51M
}
262
263
int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
264
16.7k
{
265
16.7k
#ifndef OPENSSL_SMALL_FOOTPRINT
266
16.7k
    switch (sizeof(unsigned int)) {
267
16.7k
    case sizeof(uint32_t):
268
16.7k
        return OSSL_PARAM_set_uint32(p, (uint32_t)val);
269
0
    case sizeof(uint64_t):
270
0
        return OSSL_PARAM_set_uint64(p, (uint64_t)val);
271
16.7k
    }
272
0
#endif
273
0
    return general_set_uint(p, &val, sizeof(val));
274
16.7k
}
275
276
OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
277
18.8k
{
278
18.8k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
279
18.8k
                                sizeof(unsigned int));
280
18.8k
}
281
282
int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
283
0
{
284
0
#ifndef OPENSSL_SMALL_FOOTPRINT
285
0
    switch (sizeof(long int)) {
286
0
    case sizeof(int32_t):
287
0
        return OSSL_PARAM_get_int32(p, (int32_t *)val);
288
0
    case sizeof(int64_t):
289
0
        return OSSL_PARAM_get_int64(p, (int64_t *)val);
290
0
    }
291
0
#endif
292
0
    return general_get_int(p, val, sizeof(*val));
293
0
}
294
295
int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
296
0
{
297
0
#ifndef OPENSSL_SMALL_FOOTPRINT
298
0
    switch (sizeof(long int)) {
299
0
    case sizeof(int32_t):
300
0
        return OSSL_PARAM_set_int32(p, (int32_t)val);
301
0
    case sizeof(int64_t):
302
0
        return OSSL_PARAM_set_int64(p, (int64_t)val);
303
0
    }
304
0
#endif
305
0
    return general_set_int(p, &val, sizeof(val));
306
0
}
307
308
OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
309
0
{
310
0
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
311
0
}
312
313
int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
314
0
{
315
0
#ifndef OPENSSL_SMALL_FOOTPRINT
316
0
    switch (sizeof(unsigned long int)) {
317
0
    case sizeof(uint32_t):
318
0
        return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
319
0
    case sizeof(uint64_t):
320
0
        return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
321
0
    }
322
0
#endif
323
0
    return general_get_uint(p, val, sizeof(*val));
324
0
}
325
326
int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
327
0
{
328
0
#ifndef OPENSSL_SMALL_FOOTPRINT
329
0
    switch (sizeof(unsigned long int)) {
330
0
    case sizeof(uint32_t):
331
0
        return OSSL_PARAM_set_uint32(p, (uint32_t)val);
332
0
    case sizeof(uint64_t):
333
0
        return OSSL_PARAM_set_uint64(p, (uint64_t)val);
334
0
    }
335
0
#endif
336
0
    return general_set_uint(p, &val, sizeof(val));
337
0
}
338
339
OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
340
0
{
341
0
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
342
0
                                sizeof(unsigned long int));
343
0
}
344
345
int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
346
2.49M
{
347
2.49M
    if (val == NULL || p == NULL )
348
0
        return 0;
349
350
2.49M
    if (p->data_type == OSSL_PARAM_INTEGER) {
351
2.45M
#ifndef OPENSSL_SMALL_FOOTPRINT
352
2.45M
        int64_t i64;
353
354
2.45M
        switch (p->data_size) {
355
2.45M
        case sizeof(int32_t):
356
2.45M
            *val = *(const int32_t *)p->data;
357
2.45M
            return 1;
358
0
        case sizeof(int64_t):
359
0
            i64 = *(const int64_t *)p->data;
360
0
            if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
361
0
                *val = (int32_t)i64;
362
0
                return 1;
363
0
            }
364
0
            return 0;
365
2.45M
        }
366
0
#endif
367
0
        return general_get_int(p, val, sizeof(*val));
368
369
2.45M
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
370
39.4k
#ifndef OPENSSL_SMALL_FOOTPRINT
371
39.4k
        uint32_t u32;
372
39.4k
        uint64_t u64;
373
374
39.4k
        switch (p->data_size) {
375
0
        case sizeof(uint32_t):
376
0
            u32 = *(const uint32_t *)p->data;
377
0
            if (u32 <= INT32_MAX) {
378
0
                *val = (int32_t)u32;
379
0
                return 1;
380
0
            }
381
0
            return 0;
382
39.4k
        case sizeof(uint64_t):
383
39.4k
            u64 = *(const uint64_t *)p->data;
384
39.4k
            if (u64 <= INT32_MAX) {
385
39.4k
                *val = (int32_t)u64;
386
39.4k
                return 1;
387
39.4k
            }
388
0
            return 0;
389
39.4k
        }
390
0
#endif
391
0
        return general_get_int(p, val, sizeof(*val));
392
393
39.4k
    } else if (p->data_type == OSSL_PARAM_REAL) {
394
0
#ifndef OPENSSL_SYS_UEFI
395
0
        double d;
396
397
0
        switch (p->data_size) {
398
0
        case sizeof(double):
399
0
            d = *(const double *)p->data;
400
0
            if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
401
0
                *val = (int32_t)d;
402
0
                return 1;
403
0
            }
404
0
            break;
405
0
        }
406
0
#endif
407
0
    }
408
0
    return 0;
409
2.49M
}
410
411
int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
412
347k
{
413
347k
    if (p == NULL)
414
0
        return 0;
415
347k
    p->return_size = 0;
416
347k
    if (p->data_type == OSSL_PARAM_INTEGER) {
417
347k
#ifndef OPENSSL_SMALL_FOOTPRINT
418
347k
        p->return_size = sizeof(int32_t); /* Minimum expected size */
419
347k
        if (p->data == NULL)
420
0
            return 1;
421
347k
        switch (p->data_size) {
422
347k
        case sizeof(int32_t):
423
347k
            *(int32_t *)p->data = val;
424
347k
            return 1;
425
0
        case sizeof(int64_t):
426
0
            p->return_size = sizeof(int64_t);
427
0
            *(int64_t *)p->data = (int64_t)val;
428
0
            return 1;
429
347k
        }
430
0
#endif
431
0
        return general_set_int(p, &val, sizeof(val));
432
347k
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
433
5
#ifndef OPENSSL_SMALL_FOOTPRINT
434
5
        p->return_size = sizeof(uint32_t); /* Minimum expected size */
435
5
        if (p->data == NULL)
436
0
            return 1;
437
5
        switch (p->data_size) {
438
5
        case sizeof(uint32_t):
439
5
            *(uint32_t *)p->data = (uint32_t)val;
440
5
            return 1;
441
0
        case sizeof(uint64_t):
442
0
            p->return_size = sizeof(uint64_t);
443
0
            *(uint64_t *)p->data = (uint64_t)val;
444
0
            return 1;
445
5
        }
446
0
#endif
447
0
        return general_set_int(p, &val, sizeof(val));
448
5
    } else if (p->data_type == OSSL_PARAM_REAL) {
449
0
#ifndef OPENSSL_SYS_UEFI
450
0
        p->return_size = sizeof(double);
451
0
        if (p->data == NULL)
452
0
            return 1;
453
0
        switch (p->data_size) {
454
0
        case sizeof(double):
455
0
            *(double *)p->data = (double)val;
456
0
            return 1;
457
0
        }
458
0
#endif
459
0
    }
460
0
    return 0;
461
347k
}
462
463
OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
464
0
{
465
0
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
466
0
                                sizeof(int32_t));
467
0
}
468
469
int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
470
1.18M
{
471
1.18M
    if (val == NULL || p == NULL)
472
0
        return 0;
473
474
1.18M
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
475
1.18M
#ifndef OPENSSL_SMALL_FOOTPRINT
476
1.18M
        uint64_t u64;
477
478
1.18M
        switch (p->data_size) {
479
1.18M
        case sizeof(uint32_t):
480
1.18M
            *val = *(const uint32_t *)p->data;
481
1.18M
            return 1;
482
0
        case sizeof(uint64_t):
483
0
            u64 = *(const uint64_t *)p->data;
484
0
            if (u64 <= UINT32_MAX) {
485
0
                *val = (uint32_t)u64;
486
0
                return 1;
487
0
            }
488
0
            return 0;
489
1.18M
        }
490
0
#endif
491
0
        return general_get_uint(p, val, sizeof(*val));
492
1.18M
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
493
1.11k
#ifndef OPENSSL_SMALL_FOOTPRINT
494
1.11k
        int32_t i32;
495
1.11k
        int64_t i64;
496
497
1.11k
        switch (p->data_size) {
498
1.11k
        case sizeof(int32_t):
499
1.11k
            i32 = *(const int32_t *)p->data;
500
1.11k
            if (i32 >= 0) {
501
1.11k
                *val = i32;
502
1.11k
                return 1;
503
1.11k
            }
504
0
            return 0;
505
0
        case sizeof(int64_t):
506
0
            i64 = *(const int64_t *)p->data;
507
0
            if (i64 >= 0 && i64 <= UINT32_MAX) {
508
0
                *val = (uint32_t)i64;
509
0
                return 1;
510
0
            }
511
0
            return 0;
512
1.11k
        }
513
0
#endif
514
0
        return general_get_uint(p, val, sizeof(*val));
515
1.11k
    } else if (p->data_type == OSSL_PARAM_REAL) {
516
0
#ifndef OPENSSL_SYS_UEFI
517
0
        double d;
518
519
0
        switch (p->data_size) {
520
0
        case sizeof(double):
521
0
            d = *(const double *)p->data;
522
0
            if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
523
0
                *val = (uint32_t)d;
524
0
                return 1;
525
0
            }
526
0
            break;
527
0
        }
528
0
#endif
529
0
    }
530
0
    return 0;
531
1.18M
}
532
533
int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
534
8.77k
{
535
8.77k
    if (p == NULL)
536
0
        return 0;
537
8.77k
    p->return_size = 0;
538
539
8.77k
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
540
8.77k
#ifndef OPENSSL_SMALL_FOOTPRINT
541
8.77k
        p->return_size = sizeof(uint32_t); /* Minimum expected size */
542
8.77k
        if (p->data == NULL)
543
0
            return 1;
544
8.77k
        switch (p->data_size) {
545
8.77k
        case sizeof(uint32_t):
546
8.77k
            *(uint32_t *)p->data = val;
547
8.77k
            return 1;
548
0
        case sizeof(uint64_t):
549
0
            p->return_size = sizeof(uint64_t);
550
0
            *(uint64_t *)p->data = val;
551
0
            return 1;
552
8.77k
        }
553
0
#endif
554
0
        return general_set_uint(p, &val, sizeof(val));
555
8.77k
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
556
0
#ifndef OPENSSL_SMALL_FOOTPRINT
557
0
        p->return_size = sizeof(int32_t); /* Minimum expected size */
558
0
        if (p->data == NULL)
559
0
            return 1;
560
0
        switch (p->data_size) {
561
0
        case sizeof(int32_t):
562
0
            if (val <= INT32_MAX) {
563
0
                *(int32_t *)p->data = (int32_t)val;
564
0
                return 1;
565
0
            }
566
0
            return 0;
567
0
        case sizeof(int64_t):
568
0
            p->return_size = sizeof(int64_t);
569
0
            *(int64_t *)p->data = (int64_t)val;
570
0
            return 1;
571
0
        }
572
0
#endif
573
0
        return general_set_uint(p, &val, sizeof(val));
574
0
    } else if (p->data_type == OSSL_PARAM_REAL) {
575
0
#ifndef OPENSSL_SYS_UEFI
576
0
        p->return_size = sizeof(double);
577
0
        if (p->data == NULL)
578
0
            return 1;
579
0
        switch (p->data_size) {
580
0
        case sizeof(double):
581
0
            *(double *)p->data = (double)val;
582
0
            return 1;
583
0
        }
584
0
#endif
585
0
    }
586
0
    return 0;
587
8.77k
}
588
589
OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
590
80.3k
{
591
80.3k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
592
80.3k
                                sizeof(uint32_t));
593
80.3k
}
594
595
int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
596
2
{
597
2
    if (val == NULL || p == NULL )
598
0
        return 0;
599
600
2
    if (p->data_type == OSSL_PARAM_INTEGER) {
601
2
#ifndef OPENSSL_SMALL_FOOTPRINT
602
2
        switch (p->data_size) {
603
0
        case sizeof(int32_t):
604
0
            *val = *(const int32_t *)p->data;
605
0
            return 1;
606
2
        case sizeof(int64_t):
607
2
            *val = *(const int64_t *)p->data;
608
2
            return 1;
609
2
        }
610
0
#endif
611
0
        return general_get_int(p, val, sizeof(*val));
612
2
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
613
0
#ifndef OPENSSL_SMALL_FOOTPRINT
614
0
        uint64_t u64;
615
616
0
        switch (p->data_size) {
617
0
        case sizeof(uint32_t):
618
0
            *val = *(const uint32_t *)p->data;
619
0
            return 1;
620
0
        case sizeof(uint64_t):
621
0
            u64 = *(const uint64_t *)p->data;
622
0
            if (u64 <= INT64_MAX) {
623
0
                *val = (int64_t)u64;
624
0
                return 1;
625
0
            }
626
0
            return 0;
627
0
        }
628
0
#endif
629
0
        return general_get_int(p, val, sizeof(*val));
630
0
    } else if (p->data_type == OSSL_PARAM_REAL) {
631
0
#ifndef OPENSSL_SYS_UEFI
632
0
        double d;
633
634
0
        switch (p->data_size) {
635
0
        case sizeof(double):
636
0
            d = *(const double *)p->data;
637
0
            if (d >= INT64_MIN
638
                    /*
639
                     * By subtracting 65535 (2^16-1) we cancel the low order
640
                     * 15 bits of INT64_MAX to avoid using imprecise floating
641
                     * point values.
642
                     */
643
0
                    && d < (double)(INT64_MAX - 65535) + 65536.0
644
0
                    && d == (int64_t)d) {
645
0
                *val = (int64_t)d;
646
0
                return 1;
647
0
            }
648
0
            break;
649
0
        }
650
0
#endif
651
0
    }
652
0
    return 0;
653
2
}
654
655
int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
656
0
{
657
0
    if (p == NULL)
658
0
        return 0;
659
0
    p->return_size = 0;
660
0
    if (p->data_type == OSSL_PARAM_INTEGER) {
661
0
#ifndef OPENSSL_SMALL_FOOTPRINT
662
0
        p->return_size = sizeof(int64_t); /* Expected size */
663
0
        if (p->data == NULL)
664
0
            return 1;
665
0
        switch (p->data_size) {
666
0
        case sizeof(int32_t):
667
0
            if (val >= INT32_MIN && val <= INT32_MAX) {
668
0
                p->return_size = sizeof(int32_t);
669
0
                *(int32_t *)p->data = (int32_t)val;
670
0
                return 1;
671
0
            }
672
0
            return 0;
673
0
        case sizeof(int64_t):
674
0
            *(int64_t *)p->data = val;
675
0
            return 1;
676
0
        }
677
0
#endif
678
0
        return general_set_int(p, &val, sizeof(val));
679
0
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
680
0
#ifndef OPENSSL_SMALL_FOOTPRINT
681
0
        p->return_size = sizeof(uint64_t); /* Expected size */
682
0
        if (p->data == NULL)
683
0
            return 1;
684
0
        switch (p->data_size) {
685
0
        case sizeof(uint32_t):
686
0
            if (val <= UINT32_MAX) {
687
0
                p->return_size = sizeof(uint32_t);
688
0
                *(uint32_t *)p->data = (uint32_t)val;
689
0
                return 1;
690
0
            }
691
0
            return 0;
692
0
        case sizeof(uint64_t):
693
0
            *(uint64_t *)p->data = (uint64_t)val;
694
0
            return 1;
695
0
        }
696
0
#endif
697
0
        return general_set_int(p, &val, sizeof(val));
698
0
    } else if (p->data_type == OSSL_PARAM_REAL) {
699
0
#ifndef OPENSSL_SYS_UEFI
700
0
        uint64_t u64;
701
702
0
        p->return_size = sizeof(double);
703
0
        if (p->data == NULL)
704
0
            return 1;
705
0
        switch (p->data_size) {
706
0
        case sizeof(double):
707
0
            u64 = val < 0 ? -val : val;
708
0
            if ((u64 >> real_shift()) == 0) {
709
0
                *(double *)p->data = (double)val;
710
0
                return 1;
711
0
            }
712
0
            break;
713
0
        }
714
0
#endif
715
0
    }
716
0
    return 0;
717
0
}
718
719
OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
720
0
{
721
0
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
722
0
}
723
724
int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
725
2.96k
{
726
2.96k
    if (val == NULL || p == NULL)
727
0
        return 0;
728
729
2.96k
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
730
2.96k
#ifndef OPENSSL_SMALL_FOOTPRINT
731
2.96k
        switch (p->data_size) {
732
0
        case sizeof(uint32_t):
733
0
            *val = *(const uint32_t *)p->data;
734
0
            return 1;
735
2.96k
        case sizeof(uint64_t):
736
2.96k
            *val = *(const uint64_t *)p->data;
737
2.96k
            return 1;
738
2.96k
        }
739
0
#endif
740
0
        return general_get_uint(p, val, sizeof(*val));
741
2.96k
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
742
0
#ifndef OPENSSL_SMALL_FOOTPRINT
743
0
        int32_t i32;
744
0
        int64_t i64;
745
746
0
        switch (p->data_size) {
747
0
        case sizeof(int32_t):
748
0
            i32 = *(const int32_t *)p->data;
749
0
            if (i32 >= 0) {
750
0
                *val = (uint64_t)i32;
751
0
                return 1;
752
0
            }
753
0
            return 0;
754
0
        case sizeof(int64_t):
755
0
            i64 = *(const int64_t *)p->data;
756
0
            if (i64 >= 0) {
757
0
                *val = (uint64_t)i64;
758
0
                return 1;
759
0
            }
760
0
            return 0;
761
0
        }
762
0
#endif
763
0
        return general_get_uint(p, val, sizeof(*val));
764
0
    } else if (p->data_type == OSSL_PARAM_REAL) {
765
0
#ifndef OPENSSL_SYS_UEFI
766
0
        double d;
767
768
0
        switch (p->data_size) {
769
0
        case sizeof(double):
770
0
            d = *(const double *)p->data;
771
0
            if (d >= 0
772
                    /*
773
                     * By subtracting 65535 (2^16-1) we cancel the low order
774
                     * 15 bits of UINT64_MAX to avoid using imprecise floating
775
                     * point values.
776
                     */
777
0
                    && d < (double)(UINT64_MAX - 65535) + 65536.0
778
0
                    && d == (uint64_t)d) {
779
0
                *val = (uint64_t)d;
780
0
                return 1;
781
0
            }
782
0
            break;
783
0
        }
784
0
#endif
785
0
    }
786
0
    return 0;
787
2.96k
}
788
789
int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
790
271k
{
791
271k
    if (p == NULL)
792
0
        return 0;
793
271k
    p->return_size = 0;
794
795
271k
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
796
271k
#ifndef OPENSSL_SMALL_FOOTPRINT
797
271k
        p->return_size = sizeof(uint64_t); /* Expected size */
798
271k
        if (p->data == NULL)
799
0
            return 1;
800
271k
        switch (p->data_size) {
801
0
        case sizeof(uint32_t):
802
0
            if (val <= UINT32_MAX) {
803
0
                p->return_size = sizeof(uint32_t);
804
0
                *(uint32_t *)p->data = (uint32_t)val;
805
0
                return 1;
806
0
            }
807
0
            return 0;
808
271k
        case sizeof(uint64_t):
809
271k
            *(uint64_t *)p->data = val;
810
271k
            return 1;
811
271k
        }
812
0
#endif
813
0
        return general_set_uint(p, &val, sizeof(val));
814
271k
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
815
0
#ifndef OPENSSL_SMALL_FOOTPRINT
816
0
        p->return_size = sizeof(int64_t); /* Expected size */
817
0
        if (p->data == NULL)
818
0
            return 1;
819
0
        switch (p->data_size) {
820
0
        case sizeof(int32_t):
821
0
            if (val <= INT32_MAX) {
822
0
                p->return_size = sizeof(int32_t);
823
0
                *(int32_t *)p->data = (int32_t)val;
824
0
                return 1;
825
0
            }
826
0
            return 0;
827
0
        case sizeof(int64_t):
828
0
            if (val <= INT64_MAX) {
829
0
                *(int64_t *)p->data = (int64_t)val;
830
0
                return 1;
831
0
            }
832
0
            return 0;
833
0
        }
834
0
#endif
835
0
        return general_set_uint(p, &val, sizeof(val));
836
0
    } else if (p->data_type == OSSL_PARAM_REAL) {
837
0
#ifndef OPENSSL_SYS_UEFI
838
0
        p->return_size = sizeof(double);
839
0
        switch (p->data_size) {
840
0
        case sizeof(double):
841
0
            if ((val >> real_shift()) == 0) {
842
0
                *(double *)p->data = (double)val;
843
0
                return 1;
844
0
            }
845
0
            break;
846
0
        }
847
0
#endif
848
0
    }
849
0
    return 0;
850
271k
}
851
852
OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
853
79.8k
{
854
79.8k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
855
79.8k
                                sizeof(uint64_t));
856
79.8k
}
857
858
int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
859
86.7k
{
860
86.7k
#ifndef OPENSSL_SMALL_FOOTPRINT
861
86.7k
    switch (sizeof(size_t)) {
862
0
    case sizeof(uint32_t):
863
0
        return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
864
86.7k
    case sizeof(uint64_t):
865
86.7k
        return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
866
86.7k
    }
867
0
#endif
868
0
    return general_get_uint(p, val, sizeof(*val));
869
86.7k
}
870
871
int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
872
596k
{
873
596k
#ifndef OPENSSL_SMALL_FOOTPRINT
874
596k
    switch (sizeof(size_t)) {
875
0
    case sizeof(uint32_t):
876
0
        return OSSL_PARAM_set_uint32(p, (uint32_t)val);
877
596k
    case sizeof(uint64_t):
878
596k
        return OSSL_PARAM_set_uint64(p, (uint64_t)val);
879
596k
    }
880
0
#endif
881
0
    return general_set_uint(p, &val, sizeof(val));
882
596k
}
883
884
OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
885
686k
{
886
686k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
887
686k
                                sizeof(size_t));
888
686k
}
889
890
int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
891
4
{
892
4
#ifndef OPENSSL_SMALL_FOOTPRINT
893
4
    switch (sizeof(time_t)) {
894
0
    case sizeof(int32_t):
895
0
        return OSSL_PARAM_get_int32(p, (int32_t *)val);
896
4
    case sizeof(int64_t):
897
4
        return OSSL_PARAM_get_int64(p, (int64_t *)val);
898
4
    }
899
0
#endif
900
0
    return general_get_int(p, val, sizeof(*val));
901
4
}
902
903
int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
904
0
{
905
0
#ifndef OPENSSL_SMALL_FOOTPRINT
906
0
    switch (sizeof(time_t)) {
907
0
    case sizeof(int32_t):
908
0
        return OSSL_PARAM_set_int32(p, (int32_t)val);
909
0
    case sizeof(int64_t):
910
0
        return OSSL_PARAM_set_int64(p, (int64_t)val);
911
0
    }
912
0
#endif
913
0
    return general_set_int(p, &val, sizeof(val));
914
0
}
915
916
OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
917
32
{
918
32
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
919
32
}
920
921
int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
922
102k
{
923
102k
    BIGNUM *b;
924
925
102k
    if (val == NULL
926
102k
        || p == NULL
927
102k
        || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
928
0
        return 0;
929
930
102k
    b = BN_native2bn(p->data, (int)p->data_size, *val);
931
102k
    if (b != NULL) {
932
102k
        *val = b;
933
102k
        return 1;
934
102k
    }
935
0
    return 0;
936
102k
}
937
938
int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
939
0
{
940
0
    size_t bytes;
941
942
0
    if (p == NULL)
943
0
        return 0;
944
0
    p->return_size = 0;
945
0
    if (val == NULL || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
946
0
        return 0;
947
948
    /* For the moment, only positive values are permitted */
949
0
    if (BN_is_negative(val))
950
0
        return 0;
951
952
0
    bytes = (size_t)BN_num_bytes(val);
953
    /* We make sure that at least one byte is used, so zero is properly set */
954
0
    if (bytes == 0)
955
0
        bytes++;
956
957
0
    p->return_size = bytes;
958
0
    if (p->data == NULL)
959
0
        return 1;
960
0
    if (p->data_size >= bytes) {
961
0
        p->return_size = p->data_size;
962
0
        return BN_bn2nativepad(val, p->data, p->data_size) >= 0;
963
0
    }
964
0
    return 0;
965
0
}
966
967
OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
968
                                   size_t bsize)
969
0
{
970
0
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
971
0
                                buf, bsize);
972
0
}
973
974
#ifndef OPENSSL_SYS_UEFI
975
int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
976
0
{
977
0
    int64_t i64;
978
0
    uint64_t u64;
979
980
0
    if (val == NULL || p == NULL)
981
0
        return 0;
982
983
0
    if (p->data_type == OSSL_PARAM_REAL) {
984
0
        switch (p->data_size) {
985
0
        case sizeof(double):
986
0
            *val = *(const double *)p->data;
987
0
            return 1;
988
0
        }
989
0
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
990
0
        switch (p->data_size) {
991
0
        case sizeof(uint32_t):
992
0
            *val = *(const uint32_t *)p->data;
993
0
            return 1;
994
0
        case sizeof(uint64_t):
995
0
            u64 = *(const uint64_t *)p->data;
996
0
            if ((u64 >> real_shift()) == 0) {
997
0
                *val = (double)u64;
998
0
                return 1;
999
0
            }
1000
0
            break;
1001
0
        }
1002
0
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
1003
0
        switch (p->data_size) {
1004
0
        case sizeof(int32_t):
1005
0
            *val = *(const int32_t *)p->data;
1006
0
            return 1;
1007
0
        case sizeof(int64_t):
1008
0
            i64 = *(const int64_t *)p->data;
1009
0
            u64 = i64 < 0 ? -i64 : i64;
1010
0
            if ((u64 >> real_shift()) == 0) {
1011
0
                *val = 0.0 + i64;
1012
0
                return 1;
1013
0
            }
1014
0
            break;
1015
0
        }
1016
0
    }
1017
0
    return 0;
1018
0
}
1019
1020
int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1021
0
{
1022
0
    if (p == NULL)
1023
0
        return 0;
1024
0
    p->return_size = 0;
1025
1026
0
    if (p->data_type == OSSL_PARAM_REAL) {
1027
0
        p->return_size = sizeof(double);
1028
0
        if (p->data == NULL)
1029
0
            return 1;
1030
0
        switch (p->data_size) {
1031
0
        case sizeof(double):
1032
0
            *(double *)p->data = val;
1033
0
            return 1;
1034
0
        }
1035
0
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
1036
0
               && val == (uint64_t)val) {
1037
0
        p->return_size = sizeof(double);
1038
0
        if (p->data == NULL)
1039
0
            return 1;
1040
0
        switch (p->data_size) {
1041
0
        case sizeof(uint32_t):
1042
0
            if (val >= 0 && val <= UINT32_MAX) {
1043
0
                p->return_size = sizeof(uint32_t);
1044
0
                *(uint32_t *)p->data = (uint32_t)val;
1045
0
                return 1;
1046
0
            }
1047
0
            break;
1048
0
        case sizeof(uint64_t):
1049
0
            if (val >= 0
1050
                    /*
1051
                     * By subtracting 65535 (2^16-1) we cancel the low order
1052
                     * 15 bits of UINT64_MAX to avoid using imprecise floating
1053
                     * point values.
1054
                     */
1055
0
                    && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1056
0
                p->return_size = sizeof(uint64_t);
1057
0
                *(uint64_t *)p->data = (uint64_t)val;
1058
0
                return 1;
1059
0
            }
1060
0
            break;            }
1061
0
    } else if (p->data_type == OSSL_PARAM_INTEGER && val == (int64_t)val) {
1062
0
        p->return_size = sizeof(double);
1063
0
        if (p->data == NULL)
1064
0
            return 1;
1065
0
        switch (p->data_size) {
1066
0
        case sizeof(int32_t):
1067
0
            if (val >= INT32_MIN && val <= INT32_MAX) {
1068
0
                p->return_size = sizeof(int32_t);
1069
0
                *(int32_t *)p->data = (int32_t)val;
1070
0
                return 1;
1071
0
            }
1072
0
            break;
1073
0
        case sizeof(int64_t):
1074
0
            if (val >= INT64_MIN
1075
                    /*
1076
                     * By subtracting 65535 (2^16-1) we cancel the low order
1077
                     * 15 bits of INT64_MAX to avoid using imprecise floating
1078
                     * point values.
1079
                     */
1080
0
                    && val < (double)(INT64_MAX - 65535) + 65536.0) {
1081
0
                p->return_size = sizeof(int64_t);
1082
0
                *(int64_t *)p->data = (int64_t)val;
1083
0
                return 1;
1084
0
            }
1085
0
            break;
1086
0
        }
1087
0
    }
1088
0
    return 0;
1089
0
}
1090
1091
OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1092
0
{
1093
0
    return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1094
0
}
1095
#endif
1096
1097
static int get_string_internal(const OSSL_PARAM *p, void **val,
1098
                               size_t *max_len, size_t *used_len,
1099
                               unsigned int type)
1100
377k
{
1101
377k
    size_t sz, alloc_sz;
1102
1103
377k
    if ((val == NULL && used_len == NULL) || p == NULL || p->data_type != type)
1104
0
        return 0;
1105
1106
377k
    sz = p->data_size;
1107
    /*
1108
     * If the input size is 0, or the input string needs NUL byte
1109
     * termination, allocate an extra byte.
1110
     */
1111
377k
    alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1112
1113
377k
    if (used_len != NULL)
1114
63.2k
        *used_len = sz;
1115
1116
377k
    if (p->data == NULL)
1117
0
        return 0;
1118
1119
377k
    if (val == NULL)
1120
0
        return 1;
1121
1122
377k
    if (*val == NULL) {
1123
350k
        char *const q = OPENSSL_malloc(alloc_sz);
1124
1125
350k
        if (q == NULL)
1126
0
            return 0;
1127
350k
        *val = q;
1128
350k
        *max_len = alloc_sz;
1129
350k
    }
1130
1131
377k
    if (*max_len < sz)
1132
0
        return 0;
1133
377k
    memcpy(*val, p->data, sz);
1134
377k
    return 1;
1135
377k
}
1136
1137
int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1138
589k
{
1139
589k
    int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1140
589k
                                  OSSL_PARAM_UTF8_STRING);
1141
1142
    /*
1143
     * We try to ensure that the copied string is terminated with a
1144
     * NUL byte.  That should be easy, just place a NUL byte at
1145
     * |((char*)*val)[p->data_size]|.
1146
     * Unfortunately, we have seen cases where |p->data_size| doesn't
1147
     * correctly reflect the length of the string, and just happens
1148
     * to be out of bounds according to |max_len|, so in that case, we
1149
     * make the extra step of trying to find the true length of the
1150
     * string that |p->data| points at, and use that as an index to
1151
     * place the NUL byte in |*val|.
1152
     */
1153
589k
    size_t data_length = p->data_size;
1154
1155
589k
    if (ret == 0)
1156
0
        return 0;
1157
589k
    if (data_length >= max_len)
1158
0
        data_length = OPENSSL_strnlen(p->data, data_length);
1159
589k
    if (data_length >= max_len)
1160
0
        return 0;            /* No space for a terminating NUL byte */
1161
589k
    (*val)[data_length] = '\0';
1162
1163
589k
    return ret;
1164
589k
}
1165
1166
int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1167
                                size_t *used_len)
1168
142k
{
1169
142k
    return get_string_internal(p, val, &max_len, used_len,
1170
142k
                               OSSL_PARAM_OCTET_STRING);
1171
142k
}
1172
1173
static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1174
                               unsigned int type)
1175
25.2k
{
1176
25.2k
    p->return_size = len;
1177
25.2k
    if (p->data == NULL)
1178
8.01k
        return 1;
1179
17.2k
    if (p->data_type != type || p->data_size < len)
1180
0
        return 0;
1181
1182
17.2k
    memcpy(p->data, val, len);
1183
    /* If possible within the size of p->data, add a NUL terminator byte */
1184
17.2k
    if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1185
8.24k
        ((char *)p->data)[len] = '\0';
1186
17.2k
    return 1;
1187
17.2k
}
1188
1189
int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1190
16.9k
{
1191
16.9k
    if (p == NULL)
1192
0
        return 0;
1193
1194
16.9k
    p->return_size = 0;
1195
16.9k
    if (val == NULL)
1196
0
        return 0;
1197
16.9k
    return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1198
16.9k
}
1199
1200
int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1201
                                size_t len)
1202
34.7k
{
1203
34.7k
    if (p == NULL)
1204
0
        return 0;
1205
1206
34.7k
    p->return_size = 0;
1207
34.7k
    if (val == NULL)
1208
0
        return 0;
1209
34.7k
    return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1210
34.7k
}
1211
1212
OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1213
                                            size_t bsize)
1214
1.05M
{
1215
1.05M
    if (buf != NULL && bsize == 0)
1216
1.04M
        bsize = strlen(buf);
1217
1.05M
    return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1218
1.05M
}
1219
1220
OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1221
                                             size_t bsize)
1222
813k
{
1223
813k
    return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1224
813k
}
1225
1226
static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1227
                            size_t *used_len, unsigned int type)
1228
419k
{
1229
419k
    if (val == NULL || p == NULL || p->data_type != type)
1230
419k
        return 0;
1231
0
    if (used_len != NULL)
1232
0
        *used_len = p->data_size;
1233
0
    *val = *(const void **)p->data;
1234
0
    return 1;
1235
419k
}
1236
1237
int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1238
770k
{
1239
770k
    return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1240
770k
}
1241
1242
int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1243
                             size_t *used_len)
1244
0
{
1245
0
    return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1246
0
}
1247
1248
static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1249
                            unsigned int type, size_t len)
1250
28.0k
{
1251
28.0k
    p->return_size = len;
1252
28.0k
    if (p->data_type != type)
1253
0
        return 0;
1254
28.0k
    if (p->data != NULL)
1255
28.0k
        *(const void **)p->data = val;
1256
28.0k
    return 1;
1257
28.0k
}
1258
1259
int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1260
0
{
1261
0
    if (p == NULL)
1262
0
        return 0;
1263
0
    p->return_size = 0;
1264
0
    return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1265
0
                            val == NULL ? 0 : strlen(val));
1266
0
}
1267
1268
int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1269
                             size_t used_len)
1270
28.0k
{
1271
28.0k
    if (p == NULL)
1272
0
        return 0;
1273
28.0k
    p->return_size = 0;
1274
28.0k
    return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1275
28.0k
}
1276
1277
OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1278
                                         size_t bsize)
1279
0
{
1280
0
    return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1281
0
}
1282
1283
OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1284
                                          size_t bsize)
1285
28.0k
{
1286
28.0k
    return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1287
28.0k
}
1288
1289
OSSL_PARAM OSSL_PARAM_construct_end(void)
1290
1.18M
{
1291
1.18M
    OSSL_PARAM end = OSSL_PARAM_END;
1292
1293
1.18M
    return end;
1294
1.18M
}
1295
1296
static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1297
                                   size_t *used_len, unsigned int type)
1298
419k
{
1299
419k
    if (val == NULL || p == NULL || p->data_type != type)
1300
0
        return 0;
1301
419k
    if (used_len != NULL)
1302
0
        *used_len = p->data_size;
1303
419k
    *val = p->data;
1304
419k
    return 1;
1305
419k
}
1306
1307
int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1308
770k
{
1309
770k
    return OSSL_PARAM_get_utf8_ptr(p, val)
1310
770k
        || get_string_ptr_internal(p, (const void **)val, NULL,
1311
770k
                                   OSSL_PARAM_UTF8_STRING);
1312
770k
}
1313
1314
int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1315
                                    size_t *used_len)
1316
0
{
1317
0
    return OSSL_PARAM_get_octet_ptr(p, val, used_len)
1318
0
        || get_string_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_STRING);
1319
0
}