Coverage Report

Created: 2025-06-13 06:58

/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
248M
{
31
248M
    if (p != NULL && key != NULL)
32
536M
        for (; p->key != NULL; p++)
33
506M
            if (strcmp(key, p->key) == 0)
34
217M
                return p;
35
30.8M
    return NULL;
36
248M
}
37
38
const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
39
225M
{
40
225M
    return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
41
225M
}
42
43
static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
44
                                       void *data, size_t data_size)
45
177M
{
46
177M
    OSSL_PARAM res;
47
48
177M
    res.key = key;
49
177M
    res.data_type = data_type;
50
177M
    res.data = data;
51
177M
    res.data_size = data_size;
52
177M
    res.return_size = OSSL_PARAM_UNMODIFIED;
53
177M
    return res;
54
177M
}
55
56
int OSSL_PARAM_modified(const OSSL_PARAM *p)
57
524k
{
58
524k
    return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
59
524k
}
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
19.4M
{
221
19.4M
#ifndef OPENSSL_SMALL_FOOTPRINT
222
19.4M
    switch (sizeof(int)) {
223
19.4M
    case sizeof(int32_t):
224
19.4M
        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
19.4M
    }
228
0
#endif
229
0
    return general_get_int(p, val, sizeof(*val));
230
19.4M
}
231
232
int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
233
2.12M
{
234
2.12M
#ifndef OPENSSL_SMALL_FOOTPRINT
235
2.12M
    switch (sizeof(int)) {
236
2.12M
    case sizeof(int32_t):
237
2.12M
        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
2.12M
    }
241
0
#endif
242
0
    return general_set_int(p, &val, sizeof(val));
243
2.12M
}
244
245
OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
246
4.70M
{
247
4.70M
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
248
4.70M
}
249
250
int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
251
10.6M
{
252
10.6M
#ifndef OPENSSL_SMALL_FOOTPRINT
253
10.6M
    switch (sizeof(unsigned int)) {
254
10.6M
    case sizeof(uint32_t):
255
10.6M
        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
10.6M
    }
259
0
#endif
260
0
    return general_get_uint(p, val, sizeof(*val));
261
10.6M
}
262
263
int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
264
113k
{
265
113k
#ifndef OPENSSL_SMALL_FOOTPRINT
266
113k
    switch (sizeof(unsigned int)) {
267
113k
    case sizeof(uint32_t):
268
113k
        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
113k
    }
272
0
#endif
273
0
    return general_set_uint(p, &val, sizeof(val));
274
113k
}
275
276
OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
277
91.0k
{
278
91.0k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
279
91.0k
                                sizeof(unsigned int));
280
91.0k
}
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.47M
{
347
2.47M
    if (val == NULL || p == NULL )
348
0
        return 0;
349
350
2.47M
    if (p->data_type == OSSL_PARAM_INTEGER) {
351
2.42M
#ifndef OPENSSL_SMALL_FOOTPRINT
352
2.42M
        int64_t i64;
353
354
2.42M
        switch (p->data_size) {
355
2.42M
        case sizeof(int32_t):
356
2.42M
            *val = *(const int32_t *)p->data;
357
2.42M
            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.42M
        }
366
0
#endif
367
0
        return general_get_int(p, val, sizeof(*val));
368
369
2.42M
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
370
47.6k
#ifndef OPENSSL_SMALL_FOOTPRINT
371
47.6k
        uint32_t u32;
372
47.6k
        uint64_t u64;
373
374
47.6k
        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
47.6k
        case sizeof(uint64_t):
383
47.6k
            u64 = *(const uint64_t *)p->data;
384
47.6k
            if (u64 <= INT32_MAX) {
385
47.6k
                *val = (int32_t)u64;
386
47.6k
                return 1;
387
47.6k
            }
388
0
            return 0;
389
47.6k
        }
390
0
#endif
391
0
        return general_get_int(p, val, sizeof(*val));
392
393
47.6k
    } 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.47M
}
410
411
int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
412
355k
{
413
355k
    if (p == NULL)
414
0
        return 0;
415
355k
    p->return_size = 0;
416
355k
    if (p->data_type == OSSL_PARAM_INTEGER) {
417
355k
#ifndef OPENSSL_SMALL_FOOTPRINT
418
355k
        p->return_size = sizeof(int32_t); /* Minimum expected size */
419
355k
        if (p->data == NULL)
420
0
            return 1;
421
355k
        switch (p->data_size) {
422
355k
        case sizeof(int32_t):
423
355k
            *(int32_t *)p->data = val;
424
355k
            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
355k
        }
430
0
#endif
431
0
        return general_set_int(p, &val, sizeof(val));
432
355k
    } 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
355k
}
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.16M
{
471
1.16M
    if (val == NULL || p == NULL)
472
0
        return 0;
473
474
1.16M
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
475
1.16M
#ifndef OPENSSL_SMALL_FOOTPRINT
476
1.16M
        uint64_t u64;
477
478
1.16M
        switch (p->data_size) {
479
1.16M
        case sizeof(uint32_t):
480
1.16M
            *val = *(const uint32_t *)p->data;
481
1.16M
            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.16M
        }
490
0
#endif
491
0
        return general_get_uint(p, val, sizeof(*val));
492
1.16M
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
493
1.44k
#ifndef OPENSSL_SMALL_FOOTPRINT
494
1.44k
        int32_t i32;
495
1.44k
        int64_t i64;
496
497
1.44k
        switch (p->data_size) {
498
1.44k
        case sizeof(int32_t):
499
1.44k
            i32 = *(const int32_t *)p->data;
500
1.44k
            if (i32 >= 0) {
501
1.44k
                *val = i32;
502
1.44k
                return 1;
503
1.44k
            }
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.44k
        }
513
0
#endif
514
0
        return general_get_uint(p, val, sizeof(*val));
515
1.44k
    } 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.16M
}
532
533
int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
534
16.6k
{
535
16.6k
    if (p == NULL)
536
0
        return 0;
537
16.6k
    p->return_size = 0;
538
539
16.6k
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
540
16.6k
#ifndef OPENSSL_SMALL_FOOTPRINT
541
16.6k
        p->return_size = sizeof(uint32_t); /* Minimum expected size */
542
16.6k
        if (p->data == NULL)
543
0
            return 1;
544
16.6k
        switch (p->data_size) {
545
16.6k
        case sizeof(uint32_t):
546
16.6k
            *(uint32_t *)p->data = val;
547
16.6k
            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
16.6k
        }
553
0
#endif
554
0
        return general_set_uint(p, &val, sizeof(val));
555
16.6k
    } 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
16.6k
}
588
589
OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
590
376k
{
591
376k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
592
376k
                                sizeof(uint32_t));
593
376k
}
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.57k
{
726
2.57k
    if (val == NULL || p == NULL)
727
0
        return 0;
728
729
2.57k
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
730
2.57k
#ifndef OPENSSL_SMALL_FOOTPRINT
731
2.57k
        switch (p->data_size) {
732
0
        case sizeof(uint32_t):
733
0
            *val = *(const uint32_t *)p->data;
734
0
            return 1;
735
2.57k
        case sizeof(uint64_t):
736
2.57k
            *val = *(const uint64_t *)p->data;
737
2.57k
            return 1;
738
2.57k
        }
739
0
#endif
740
0
        return general_get_uint(p, val, sizeof(*val));
741
2.57k
    } 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.57k
}
788
789
int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
790
318k
{
791
318k
    if (p == NULL)
792
0
        return 0;
793
318k
    p->return_size = 0;
794
795
318k
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
796
318k
#ifndef OPENSSL_SMALL_FOOTPRINT
797
318k
        p->return_size = sizeof(uint64_t); /* Expected size */
798
318k
        if (p->data == NULL)
799
0
            return 1;
800
318k
        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
318k
        case sizeof(uint64_t):
809
318k
            *(uint64_t *)p->data = val;
810
318k
            return 1;
811
318k
        }
812
0
#endif
813
0
        return general_set_uint(p, &val, sizeof(val));
814
318k
    } 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
318k
}
851
852
OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
853
397k
{
854
397k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
855
397k
                                sizeof(uint64_t));
856
397k
}
857
858
int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
859
161M
{
860
161M
#ifndef OPENSSL_SMALL_FOOTPRINT
861
161M
    switch (sizeof(size_t)) {
862
0
    case sizeof(uint32_t):
863
0
        return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
864
161M
    case sizeof(uint64_t):
865
161M
        return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
866
161M
    }
867
0
#endif
868
0
    return general_get_uint(p, val, sizeof(*val));
869
161M
}
870
871
int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
872
2.33M
{
873
2.33M
#ifndef OPENSSL_SMALL_FOOTPRINT
874
2.33M
    switch (sizeof(size_t)) {
875
0
    case sizeof(uint32_t):
876
0
        return OSSL_PARAM_set_uint32(p, (uint32_t)val);
877
2.33M
    case sizeof(uint64_t):
878
2.33M
        return OSSL_PARAM_set_uint64(p, (uint64_t)val);
879
2.33M
    }
880
0
#endif
881
0
    return general_set_uint(p, &val, sizeof(val));
882
2.33M
}
883
884
OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
885
163M
{
886
163M
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
887
163M
                                sizeof(size_t));
888
163M
}
889
890
int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
891
117
{
892
117
#ifndef OPENSSL_SMALL_FOOTPRINT
893
117
    switch (sizeof(time_t)) {
894
0
    case sizeof(int32_t):
895
0
        return OSSL_PARAM_get_int32(p, (int32_t *)val);
896
117
    case sizeof(int64_t):
897
117
        return OSSL_PARAM_get_int64(p, (int64_t *)val);
898
117
    }
899
0
#endif
900
0
    return general_get_int(p, val, sizeof(*val));
901
117
}
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
92
{
918
92
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
919
92
}
920
921
int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
922
100k
{
923
100k
    BIGNUM *b;
924
925
100k
    if (val == NULL
926
100k
        || p == NULL
927
100k
        || p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
928
0
        return 0;
929
930
100k
    b = BN_native2bn(p->data, (int)p->data_size, *val);
931
100k
    if (b != NULL) {
932
100k
        *val = b;
933
100k
        return 1;
934
100k
    }
935
0
    return 0;
936
100k
}
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
373k
{
1101
373k
    size_t sz, alloc_sz;
1102
1103
373k
    if ((val == NULL && used_len == NULL) || p == NULL || p->data_type != type)
1104
0
        return 0;
1105
1106
373k
    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
373k
    alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1112
1113
373k
    if (used_len != NULL)
1114
56.0k
        *used_len = sz;
1115
1116
373k
    if (p->data == NULL)
1117
0
        return 0;
1118
1119
373k
    if (val == NULL)
1120
0
        return 1;
1121
1122
373k
    if (*val == NULL) {
1123
351k
        char *const q = OPENSSL_malloc(alloc_sz);
1124
1125
351k
        if (q == NULL)
1126
0
            return 0;
1127
351k
        *val = q;
1128
351k
        *max_len = alloc_sz;
1129
351k
    }
1130
1131
373k
    if (*max_len < sz)
1132
0
        return 0;
1133
373k
    memcpy(*val, p->data, sz);
1134
373k
    return 1;
1135
373k
}
1136
1137
int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1138
1.28M
{
1139
1.28M
    int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1140
1.28M
                                  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
1.28M
    size_t data_length = p->data_size;
1154
1155
1.28M
    if (ret == 0)
1156
0
        return 0;
1157
1.28M
    if (data_length >= max_len)
1158
0
        data_length = OPENSSL_strnlen(p->data, data_length);
1159
1.28M
    if (data_length >= max_len)
1160
0
        return 0;            /* No space for a terminating NUL byte */
1161
1.28M
    (*val)[data_length] = '\0';
1162
1163
1.28M
    return ret;
1164
1.28M
}
1165
1166
int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1167
                                size_t *used_len)
1168
2.61M
{
1169
2.61M
    return get_string_internal(p, val, &max_len, used_len,
1170
2.61M
                               OSSL_PARAM_OCTET_STRING);
1171
2.61M
}
1172
1173
static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1174
                               unsigned int type)
1175
24.4k
{
1176
24.4k
    p->return_size = len;
1177
24.4k
    if (p->data == NULL)
1178
7.73k
        return 1;
1179
16.7k
    if (p->data_type != type || p->data_size < len)
1180
0
        return 0;
1181
1182
16.7k
    memcpy(p->data, val, len);
1183
    /* If possible within the size of p->data, add a NUL terminator byte */
1184
16.7k
    if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1185
8.22k
        ((char *)p->data)[len] = '\0';
1186
16.7k
    return 1;
1187
16.7k
}
1188
1189
int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1190
26.8k
{
1191
26.8k
    if (p == NULL)
1192
0
        return 0;
1193
1194
26.8k
    p->return_size = 0;
1195
26.8k
    if (val == NULL)
1196
0
        return 0;
1197
26.8k
    return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1198
26.8k
}
1199
1200
int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1201
                                size_t len)
1202
500k
{
1203
500k
    if (p == NULL)
1204
0
        return 0;
1205
1206
500k
    p->return_size = 0;
1207
500k
    if (val == NULL)
1208
0
        return 0;
1209
500k
    return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1210
500k
}
1211
1212
OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1213
                                            size_t bsize)
1214
3.35M
{
1215
3.35M
    if (buf != NULL && bsize == 0)
1216
3.31M
        bsize = strlen(buf);
1217
3.35M
    return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1218
3.35M
}
1219
1220
OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1221
                                             size_t bsize)
1222
5.12M
{
1223
5.12M
    return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1224
5.12M
}
1225
1226
static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1227
                            size_t *used_len, unsigned int type)
1228
421k
{
1229
421k
    if (val == NULL || p == NULL || p->data_type != type)
1230
421k
        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
421k
}
1236
1237
int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1238
1.23M
{
1239
1.23M
    return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1240
1.23M
}
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
140k
{
1251
140k
    p->return_size = len;
1252
140k
    if (p->data_type != type)
1253
0
        return 0;
1254
140k
    if (p->data != NULL)
1255
140k
        *(const void **)p->data = val;
1256
140k
    return 1;
1257
140k
}
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
140k
{
1271
140k
    if (p == NULL)
1272
0
        return 0;
1273
140k
    p->return_size = 0;
1274
140k
    return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1275
140k
}
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
140k
{
1286
140k
    return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1287
140k
}
1288
1289
OSSL_PARAM OSSL_PARAM_construct_end(void)
1290
165M
{
1291
165M
    OSSL_PARAM end = OSSL_PARAM_END;
1292
1293
165M
    return end;
1294
165M
}
1295
1296
static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1297
                                   size_t *used_len, unsigned int type)
1298
421k
{
1299
421k
    if (val == NULL || p == NULL || p->data_type != type)
1300
0
        return 0;
1301
421k
    if (used_len != NULL)
1302
0
        *used_len = p->data_size;
1303
421k
    *val = p->data;
1304
421k
    return 1;
1305
421k
}
1306
1307
int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1308
1.23M
{
1309
1.23M
    return OSSL_PARAM_get_utf8_ptr(p, val)
1310
1.23M
        || get_string_ptr_internal(p, (const void **)val, NULL,
1311
1.23M
                                   OSSL_PARAM_UTF8_STRING);
1312
1.23M
}
1313
1314
int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1315
                                    size_t *used_len)
1316
{
1317
    return OSSL_PARAM_get_octet_ptr(p, val, used_len)
1318
        || get_string_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_STRING);
1319
}