Coverage Report

Created: 2025-08-11 07:04

/src/openssl32/crypto/params.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <string.h>
12
#include <openssl/params.h>
13
#include <openssl/err.h>
14
#include "internal/thread_once.h"
15
#include "internal/numbers.h"
16
#include "internal/endian.h"
17
#include "internal/params.h"
18
#include "internal/packet.h"
19
20
/* Shortcuts for raising errors that are widely used */
21
#define err_unsigned_negative \
22
3.20M
    ERR_raise(ERR_LIB_CRYPTO, \
23
0
              CRYPTO_R_PARAM_UNSIGNED_INTEGER_NEGATIVE_VALUE_UNSUPPORTED)
24
#define err_out_of_range      \
25
1.15M
    ERR_raise(ERR_LIB_CRYPTO, \
26
0
              CRYPTO_R_PARAM_VALUE_TOO_LARGE_FOR_DESTINATION)
27
#define err_inexact           \
28
0
    ERR_raise(ERR_LIB_CRYPTO, \
29
0
              CRYPTO_R_PARAM_CANNOT_BE_REPRESENTED_EXACTLY)
30
#define err_not_integer       \
31
0
    ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_NOT_INTEGER_TYPE)
32
#define err_too_small         \
33
76
    ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER)
34
#define err_bad_type          \
35
161M
    ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_OF_INCOMPATIBLE_TYPE)
36
#define err_null_argument     \
37
25
    ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER)
38
#define err_unsupported_real  \
39
0
    ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PARAM_UNSUPPORTED_FLOATING_POINT_FORMAT)
40
41
#ifndef OPENSSL_SYS_UEFI
42
/*
43
 * Return the number of bits in the mantissa of a double.  This is used to
44
 * shift a larger integral value to determine if it will exactly fit into a
45
 * double.
46
 */
47
static unsigned int real_shift(void)
48
0
{
49
0
    return sizeof(double) == 4 ? 24 : 53;
50
0
}
51
#endif
52
53
OSSL_PARAM *OSSL_PARAM_locate(OSSL_PARAM *p, const char *key)
54
214M
{
55
214M
    if (p != NULL && key != NULL)
56
594M
        for (; p->key != NULL; p++)
57
556M
            if (strcmp(key, p->key) == 0)
58
177M
                return p;
59
37.7M
    return NULL;
60
214M
}
61
62
const OSSL_PARAM *OSSL_PARAM_locate_const(const OSSL_PARAM *p, const char *key)
63
211M
{
64
211M
    return OSSL_PARAM_locate((OSSL_PARAM *)p, key);
65
211M
}
66
67
static OSSL_PARAM ossl_param_construct(const char *key, unsigned int data_type,
68
                                       void *data, size_t data_size)
69
280M
{
70
280M
    OSSL_PARAM res;
71
72
280M
    res.key = key;
73
280M
    res.data_type = data_type;
74
280M
    res.data = data;
75
280M
    res.data_size = data_size;
76
280M
    res.return_size = OSSL_PARAM_UNMODIFIED;
77
280M
    return res;
78
280M
}
79
80
int OSSL_PARAM_modified(const OSSL_PARAM *p)
81
872k
{
82
872k
    return p != NULL && p->return_size != OSSL_PARAM_UNMODIFIED;
83
872k
}
84
85
void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p)
86
0
{
87
0
    if (p != NULL)
88
0
        while (p->key != NULL)
89
0
            p++->return_size = OSSL_PARAM_UNMODIFIED;
90
0
}
91
92
/* Return non-zero if the signed number is negative */
93
static int is_negative(const void *number, size_t s)
94
0
{
95
0
    const unsigned char *n = number;
96
0
    DECLARE_IS_ENDIAN;
97
98
0
    return 0x80 & (IS_BIG_ENDIAN ? n[0] : n[s - 1]);
99
0
}
100
101
/* Check that all the bytes specified match the expected sign byte */
102
static int check_sign_bytes(const unsigned char *p, size_t n, unsigned char s)
103
0
{
104
0
    size_t i;
105
106
0
    for (i = 0; i < n; i++)
107
0
        if (p[i] != s)
108
0
            return 0;
109
0
    return 1;
110
0
}
111
112
/*
113
 * Copy an integer to another integer.
114
 * Handle different length integers and signed and unsigned integers.
115
 * Both integers are in native byte ordering.
116
 */
117
static int copy_integer(unsigned char *dest, size_t dest_len,
118
                        const unsigned char *src, size_t src_len,
119
                        unsigned char pad, int signed_int)
120
0
{
121
0
    size_t n;
122
0
    DECLARE_IS_ENDIAN;
123
124
0
    if (IS_BIG_ENDIAN) {
125
0
        if (src_len < dest_len) {
126
0
            n = dest_len - src_len;
127
0
            memset(dest, pad, n);
128
0
            memcpy(dest + n, src, src_len);
129
0
        } else {
130
0
            n = src_len - dest_len;
131
0
            if (!check_sign_bytes(src, n, pad)
132
                    /*
133
                     * Shortening a signed value must retain the correct sign.
134
                     * Avoiding this kind of thing: -253 = 0xff03 -> 0x03 = 3
135
                     */
136
0
                    || (signed_int && ((pad ^ src[n]) & 0x80) != 0)) {
137
0
                err_out_of_range;
138
0
                return 0;
139
0
            }
140
0
            memcpy(dest, src + n, dest_len);
141
0
        }
142
0
    } else /* IS_LITTLE_ENDIAN */ {
143
0
        if (src_len < dest_len) {
144
0
            n = dest_len - src_len;
145
0
            memset(dest + src_len, pad, n);
146
0
            memcpy(dest, src, src_len);
147
0
        } else {
148
0
            n = src_len - dest_len;
149
0
            if (!check_sign_bytes(src + dest_len, n, pad)
150
                    /*
151
                     * Shortening a signed value must retain the correct sign.
152
                     * Avoiding this kind of thing: 130 = 0x0082 -> 0x82 = -126
153
                     */
154
0
                    || (signed_int && ((pad ^ src[dest_len - 1]) & 0x80) != 0)) {
155
0
                err_out_of_range;
156
0
                return 0;
157
0
            }
158
0
            memcpy(dest, src, dest_len);
159
0
        }
160
0
    }
161
0
    return 1;
162
0
}
163
164
/* Copy a signed number to a signed number of possibly different length */
165
static int signed_from_signed(void *dest, size_t dest_len,
166
                              const void *src, size_t src_len)
167
0
{
168
0
    return copy_integer(dest, dest_len, src, src_len,
169
0
                        is_negative(src, src_len) ? 0xff : 0, 1);
170
0
}
171
172
/* Copy an unsigned number to a signed number of possibly different length */
173
static int signed_from_unsigned(void *dest, size_t dest_len,
174
                                const void *src, size_t src_len)
175
0
{
176
0
    return copy_integer(dest, dest_len, src, src_len, 0, 1);
177
0
}
178
179
/* Copy a signed number to an unsigned number of possibly different length */
180
static int unsigned_from_signed(void *dest, size_t dest_len,
181
                                const void *src, size_t src_len)
182
0
{
183
0
    if (is_negative(src, src_len)) {
184
0
        err_unsigned_negative;
185
0
        return 0;
186
0
    }
187
0
    return copy_integer(dest, dest_len, src, src_len, 0, 0);
188
0
}
189
190
/* Copy an unsigned number to an unsigned number of possibly different length */
191
static int unsigned_from_unsigned(void *dest, size_t dest_len,
192
                                  const void *src, size_t src_len)
193
0
{
194
0
    return copy_integer(dest, dest_len, src, src_len, 0, 0);
195
0
}
196
197
/* General purpose get integer parameter call that handles odd sizes */
198
static int general_get_int(const OSSL_PARAM *p, void *val, size_t val_size)
199
0
{
200
0
    if (p->data == NULL) {
201
0
        err_null_argument;
202
0
        return 0;
203
0
    }
204
0
    if (p->data_type == OSSL_PARAM_INTEGER)
205
0
        return signed_from_signed(val, val_size, p->data, p->data_size);
206
0
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
207
0
        return signed_from_unsigned(val, val_size, p->data, p->data_size);
208
0
    err_not_integer;
209
0
    return 0;
210
0
}
211
212
/* General purpose set integer parameter call that handles odd sizes */
213
static int general_set_int(OSSL_PARAM *p, void *val, size_t val_size)
214
0
{
215
0
    int r = 0;
216
217
0
    p->return_size = val_size; /* Expected size */
218
0
    if (p->data == NULL)
219
0
        return 1;
220
0
    if (p->data_type == OSSL_PARAM_INTEGER)
221
0
        r = signed_from_signed(p->data, p->data_size, val, val_size);
222
0
    else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
223
0
        r = unsigned_from_signed(p->data, p->data_size, val, val_size);
224
0
    else
225
0
        err_not_integer;
226
0
    p->return_size = r ? p->data_size : val_size;
227
0
    return r;
228
0
}
229
230
/* General purpose get unsigned integer parameter call that handles odd sizes */
231
static int general_get_uint(const OSSL_PARAM *p, void *val, size_t val_size)
232
0
{
233
234
0
    if (p->data == NULL) {
235
0
        err_null_argument;
236
0
        return 0;
237
0
    }
238
0
    if (p->data_type == OSSL_PARAM_INTEGER)
239
0
        return unsigned_from_signed(val, val_size, p->data, p->data_size);
240
0
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
241
0
        return unsigned_from_unsigned(val, val_size, p->data, p->data_size);
242
0
    err_not_integer;
243
0
    return 0;
244
0
}
245
246
/* General purpose set unsigned integer parameter call that handles odd sizes */
247
static int general_set_uint(OSSL_PARAM *p, void *val, size_t val_size)
248
0
{
249
0
    int r = 0;
250
251
0
    p->return_size = val_size; /* Expected size */
252
0
    if (p->data == NULL)
253
0
        return 1;
254
0
    if (p->data_type == OSSL_PARAM_INTEGER)
255
0
        r = signed_from_unsigned(p->data, p->data_size, val, val_size);
256
0
    else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER)
257
0
        r = unsigned_from_unsigned(p->data, p->data_size, val, val_size);
258
0
    else
259
0
        err_not_integer;
260
0
    p->return_size = r ? p->data_size : val_size;
261
0
    return r;
262
0
}
263
264
int OSSL_PARAM_get_int(const OSSL_PARAM *p, int *val)
265
34.7M
{
266
34.7M
#ifndef OPENSSL_SMALL_FOOTPRINT
267
34.7M
    switch (sizeof(int)) {
268
34.7M
    case sizeof(int32_t):
269
34.7M
        return OSSL_PARAM_get_int32(p, (int32_t *)val);
270
0
    case sizeof(int64_t):
271
0
        return OSSL_PARAM_get_int64(p, (int64_t *)val);
272
34.7M
    }
273
0
#endif
274
0
    return general_get_int(p, val, sizeof(*val));
275
34.7M
}
276
277
int OSSL_PARAM_set_int(OSSL_PARAM *p, int val)
278
2.60M
{
279
2.60M
#ifndef OPENSSL_SMALL_FOOTPRINT
280
2.60M
    switch (sizeof(int)) {
281
2.60M
    case sizeof(int32_t):
282
2.60M
        return OSSL_PARAM_set_int32(p, (int32_t)val);
283
0
    case sizeof(int64_t):
284
0
        return OSSL_PARAM_set_int64(p, (int64_t)val);
285
2.60M
    }
286
0
#endif
287
0
    return general_set_int(p, &val, sizeof(val));
288
2.60M
}
289
290
OSSL_PARAM OSSL_PARAM_construct_int(const char *key, int *buf)
291
6.89M
{
292
6.89M
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int));
293
6.89M
}
294
295
int OSSL_PARAM_get_uint(const OSSL_PARAM *p, unsigned int *val)
296
18.9M
{
297
18.9M
#ifndef OPENSSL_SMALL_FOOTPRINT
298
18.9M
    switch (sizeof(unsigned int)) {
299
18.9M
    case sizeof(uint32_t):
300
18.9M
        return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
301
0
    case sizeof(uint64_t):
302
0
        return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
303
18.9M
    }
304
0
#endif
305
0
    return general_get_uint(p, val, sizeof(*val));
306
18.9M
}
307
308
int OSSL_PARAM_set_uint(OSSL_PARAM *p, unsigned int val)
309
141k
{
310
141k
#ifndef OPENSSL_SMALL_FOOTPRINT
311
141k
    switch (sizeof(unsigned int)) {
312
141k
    case sizeof(uint32_t):
313
141k
        return OSSL_PARAM_set_uint32(p, (uint32_t)val);
314
0
    case sizeof(uint64_t):
315
0
        return OSSL_PARAM_set_uint64(p, (uint64_t)val);
316
141k
    }
317
0
#endif
318
0
    return general_set_uint(p, &val, sizeof(val));
319
141k
}
320
321
OSSL_PARAM OSSL_PARAM_construct_uint(const char *key, unsigned int *buf)
322
67.9k
{
323
67.9k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
324
67.9k
                                sizeof(unsigned int));
325
67.9k
}
326
327
int OSSL_PARAM_get_long(const OSSL_PARAM *p, long int *val)
328
0
{
329
0
#ifndef OPENSSL_SMALL_FOOTPRINT
330
0
    switch (sizeof(long int)) {
331
0
    case sizeof(int32_t):
332
0
        return OSSL_PARAM_get_int32(p, (int32_t *)val);
333
0
    case sizeof(int64_t):
334
0
        return OSSL_PARAM_get_int64(p, (int64_t *)val);
335
0
    }
336
0
#endif
337
0
    return general_get_int(p, val, sizeof(*val));
338
0
}
339
340
int OSSL_PARAM_set_long(OSSL_PARAM *p, long int val)
341
0
{
342
0
#ifndef OPENSSL_SMALL_FOOTPRINT
343
0
    switch (sizeof(long int)) {
344
0
    case sizeof(int32_t):
345
0
        return OSSL_PARAM_set_int32(p, (int32_t)val);
346
0
    case sizeof(int64_t):
347
0
        return OSSL_PARAM_set_int64(p, (int64_t)val);
348
0
    }
349
0
#endif
350
0
    return general_set_int(p, &val, sizeof(val));
351
0
}
352
353
OSSL_PARAM OSSL_PARAM_construct_long(const char *key, long int *buf)
354
0
{
355
0
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(long int));
356
0
}
357
358
int OSSL_PARAM_get_ulong(const OSSL_PARAM *p, unsigned long int *val)
359
0
{
360
0
#ifndef OPENSSL_SMALL_FOOTPRINT
361
0
    switch (sizeof(unsigned long int)) {
362
0
    case sizeof(uint32_t):
363
0
        return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
364
0
    case sizeof(uint64_t):
365
0
        return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
366
0
    }
367
0
#endif
368
0
    return general_get_uint(p, val, sizeof(*val));
369
0
}
370
371
int OSSL_PARAM_set_ulong(OSSL_PARAM *p, unsigned long int val)
372
0
{
373
0
#ifndef OPENSSL_SMALL_FOOTPRINT
374
0
    switch (sizeof(unsigned long int)) {
375
0
    case sizeof(uint32_t):
376
0
        return OSSL_PARAM_set_uint32(p, (uint32_t)val);
377
0
    case sizeof(uint64_t):
378
0
        return OSSL_PARAM_set_uint64(p, (uint64_t)val);
379
0
    }
380
0
#endif
381
0
    return general_set_uint(p, &val, sizeof(val));
382
0
}
383
384
OSSL_PARAM OSSL_PARAM_construct_ulong(const char *key, unsigned long int *buf)
385
0
{
386
0
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
387
0
                                sizeof(unsigned long int));
388
0
}
389
390
int OSSL_PARAM_get_int32(const OSSL_PARAM *p, int32_t *val)
391
32.3M
{
392
32.3M
    if (val == NULL || p == NULL) {
393
0
        err_null_argument;
394
0
        return 0;
395
0
    }
396
397
32.3M
    if (p->data == NULL) {
398
0
        err_null_argument;
399
0
        return 0;
400
0
    }
401
402
32.3M
    if (p->data_type == OSSL_PARAM_INTEGER) {
403
31.2M
#ifndef OPENSSL_SMALL_FOOTPRINT
404
31.2M
        int64_t i64;
405
406
31.2M
        switch (p->data_size) {
407
31.2M
        case sizeof(int32_t):
408
31.2M
            *val = *(const int32_t *)p->data;
409
31.2M
            return 1;
410
0
        case sizeof(int64_t):
411
0
            i64 = *(const int64_t *)p->data;
412
0
            if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
413
0
                *val = (int32_t)i64;
414
0
                return 1;
415
0
            }
416
0
            err_out_of_range;
417
0
            return 0;
418
31.2M
        }
419
0
#endif
420
0
        return general_get_int(p, val, sizeof(*val));
421
422
31.2M
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
423
1.07M
#ifndef OPENSSL_SMALL_FOOTPRINT
424
1.07M
        uint32_t u32;
425
1.07M
        uint64_t u64;
426
427
1.07M
        switch (p->data_size) {
428
0
        case sizeof(uint32_t):
429
0
            u32 = *(const uint32_t *)p->data;
430
0
            if (u32 <= INT32_MAX) {
431
0
                *val = (int32_t)u32;
432
0
                return 1;
433
0
            }
434
0
            err_out_of_range;
435
0
            return 0;
436
1.07M
        case sizeof(uint64_t):
437
1.07M
            u64 = *(const uint64_t *)p->data;
438
1.07M
            if (u64 <= INT32_MAX) {
439
1.07M
                *val = (int32_t)u64;
440
1.07M
                return 1;
441
1.07M
            }
442
0
            err_out_of_range;
443
0
            return 0;
444
1.07M
        }
445
0
#endif
446
0
        return general_get_int(p, val, sizeof(*val));
447
448
1.07M
    } else if (p->data_type == OSSL_PARAM_REAL) {
449
0
#ifndef OPENSSL_SYS_UEFI
450
0
        double d;
451
452
0
        switch (p->data_size) {
453
0
        case sizeof(double):
454
0
            d = *(const double *)p->data;
455
0
            if (d >= INT32_MIN && d <= INT32_MAX && d == (int32_t)d) {
456
0
                *val = (int32_t)d;
457
0
                return 1;
458
0
            }
459
0
            err_out_of_range;
460
0
            return 0;
461
0
        }
462
0
        err_unsupported_real;
463
0
        return 0;
464
0
#endif
465
0
    }
466
0
    err_bad_type;
467
0
    return 0;
468
32.3M
}
469
470
int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
471
2.27M
{
472
2.27M
    if (p == NULL) {
473
0
        err_null_argument;
474
0
        return 0;
475
0
    }
476
2.27M
    p->return_size = 0;
477
2.27M
    if (p->data_type == OSSL_PARAM_INTEGER) {
478
2.27M
#ifndef OPENSSL_SMALL_FOOTPRINT
479
2.27M
        p->return_size = sizeof(int32_t); /* Minimum expected size */
480
2.27M
        if (p->data == NULL)
481
0
            return 1;
482
2.27M
        switch (p->data_size) {
483
2.27M
        case sizeof(int32_t):
484
2.27M
            *(int32_t *)p->data = val;
485
2.27M
            return 1;
486
0
        case sizeof(int64_t):
487
0
            p->return_size = sizeof(int64_t);
488
0
            *(int64_t *)p->data = (int64_t)val;
489
0
            return 1;
490
2.27M
        }
491
0
#endif
492
0
        return general_set_int(p, &val, sizeof(val));
493
2.27M
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
494
88
#ifndef OPENSSL_SMALL_FOOTPRINT
495
88
        p->return_size = sizeof(uint32_t); /* Minimum expected size */
496
88
        if (p->data == NULL)
497
0
            return 1;
498
88
        switch (p->data_size) {
499
68
        case sizeof(uint32_t):
500
68
            *(uint32_t *)p->data = (uint32_t)val;
501
68
            return 1;
502
20
        case sizeof(uint64_t):
503
20
            p->return_size = sizeof(uint64_t);
504
20
            *(uint64_t *)p->data = (uint64_t)val;
505
20
            return 1;
506
88
        }
507
0
#endif
508
0
        return general_set_int(p, &val, sizeof(val));
509
88
    } else if (p->data_type == OSSL_PARAM_REAL) {
510
0
#ifndef OPENSSL_SYS_UEFI
511
0
        uint32_t u32;
512
0
        unsigned int shift;
513
514
0
        p->return_size = sizeof(double);
515
0
        if (p->data == NULL)
516
0
            return 1;
517
0
        switch (p->data_size) {
518
0
        case sizeof(double):
519
0
            shift = real_shift();
520
0
            if (shift < 8 * sizeof(val) - 1) {
521
0
                u32 = val < 0 ? -val : val;
522
0
                if ((u32 >> shift) != 0) {
523
0
                    err_inexact;
524
0
                    return 0;
525
0
                }
526
0
            }
527
0
            *(double *)p->data = (double)val;
528
0
            return 1;
529
0
        }
530
0
        err_unsupported_real;
531
0
        return 0;
532
0
#endif
533
0
    }
534
0
    err_bad_type;
535
0
    return 0;
536
2.27M
}
537
538
OSSL_PARAM OSSL_PARAM_construct_int32(const char *key, int32_t *buf)
539
0
{
540
0
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf,
541
0
                                sizeof(int32_t));
542
0
}
543
544
int OSSL_PARAM_get_uint32(const OSSL_PARAM *p, uint32_t *val)
545
18.4M
{
546
18.4M
    if (val == NULL || p == NULL) {
547
0
        err_null_argument;
548
0
        return 0;
549
0
    }
550
551
18.4M
    if (p->data == NULL) {
552
0
        err_null_argument;
553
0
        return 0;
554
0
    }
555
556
18.4M
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
557
15.2M
#ifndef OPENSSL_SMALL_FOOTPRINT
558
15.2M
        uint64_t u64;
559
560
15.2M
        switch (p->data_size) {
561
15.2M
        case sizeof(uint32_t):
562
15.2M
            *val = *(const uint32_t *)p->data;
563
15.2M
            return 1;
564
15.0k
        case sizeof(uint64_t):
565
15.0k
            u64 = *(const uint64_t *)p->data;
566
15.0k
            if (u64 <= UINT32_MAX) {
567
15.0k
                *val = (uint32_t)u64;
568
15.0k
                return 1;
569
15.0k
            }
570
0
            err_out_of_range;
571
0
            return 0;
572
15.2M
        }
573
0
#endif
574
0
        return general_get_uint(p, val, sizeof(*val));
575
15.2M
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
576
3.20M
#ifndef OPENSSL_SMALL_FOOTPRINT
577
3.20M
        int32_t i32;
578
3.20M
        int64_t i64;
579
580
3.20M
        switch (p->data_size) {
581
3.20M
        case sizeof(int32_t):
582
3.20M
            i32 = *(const int32_t *)p->data;
583
3.20M
            if (i32 >= 0) {
584
3.20M
                *val = i32;
585
3.20M
                return 1;
586
3.20M
            }
587
0
            err_unsigned_negative;
588
0
            return 0;
589
0
        case sizeof(int64_t):
590
0
            i64 = *(const int64_t *)p->data;
591
0
            if (i64 >= 0 && i64 <= UINT32_MAX) {
592
0
                *val = (uint32_t)i64;
593
0
                return 1;
594
0
            }
595
0
            if (i64 < 0)
596
0
                err_unsigned_negative;
597
0
            else
598
0
                err_out_of_range;
599
0
            return 0;
600
3.20M
        }
601
0
#endif
602
0
        return general_get_uint(p, val, sizeof(*val));
603
3.20M
    } else if (p->data_type == OSSL_PARAM_REAL) {
604
0
#ifndef OPENSSL_SYS_UEFI
605
0
        double d;
606
607
0
        switch (p->data_size) {
608
0
        case sizeof(double):
609
0
            d = *(const double *)p->data;
610
0
            if (d >= 0 && d <= UINT32_MAX && d == (uint32_t)d) {
611
0
                *val = (uint32_t)d;
612
0
                return 1;
613
0
            }
614
0
            err_inexact;
615
0
            return 0;
616
0
        }
617
0
        err_unsupported_real;
618
0
        return 0;
619
0
#endif
620
0
    }
621
0
    err_bad_type;
622
0
    return 0;
623
18.4M
}
624
625
int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
626
130k
{
627
130k
    if (p == NULL) {
628
0
        err_null_argument;
629
0
        return 0;
630
0
    }
631
130k
    p->return_size = 0;
632
633
130k
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
634
130k
#ifndef OPENSSL_SMALL_FOOTPRINT
635
130k
        p->return_size = sizeof(uint32_t); /* Minimum expected size */
636
130k
        if (p->data == NULL)
637
0
            return 1;
638
130k
        switch (p->data_size) {
639
45.8k
        case sizeof(uint32_t):
640
45.8k
            *(uint32_t *)p->data = val;
641
45.8k
            return 1;
642
84.3k
        case sizeof(uint64_t):
643
84.3k
            p->return_size = sizeof(uint64_t);
644
84.3k
            *(uint64_t *)p->data = val;
645
84.3k
            return 1;
646
130k
        }
647
0
#endif
648
0
        return general_set_uint(p, &val, sizeof(val));
649
130k
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
650
0
#ifndef OPENSSL_SMALL_FOOTPRINT
651
0
        p->return_size = sizeof(int32_t); /* Minimum expected size */
652
0
        if (p->data == NULL)
653
0
            return 1;
654
0
        switch (p->data_size) {
655
0
        case sizeof(int32_t):
656
0
            if (val <= INT32_MAX) {
657
0
                *(int32_t *)p->data = (int32_t)val;
658
0
                return 1;
659
0
            }
660
0
            err_out_of_range;
661
0
            return 0;
662
0
        case sizeof(int64_t):
663
0
            p->return_size = sizeof(int64_t);
664
0
            *(int64_t *)p->data = (int64_t)val;
665
0
            return 1;
666
0
        }
667
0
#endif
668
0
        return general_set_uint(p, &val, sizeof(val));
669
0
    } else if (p->data_type == OSSL_PARAM_REAL) {
670
0
#ifndef OPENSSL_SYS_UEFI
671
0
        unsigned int shift;
672
673
0
        p->return_size = sizeof(double);
674
0
        if (p->data == NULL)
675
0
            return 1;
676
0
        switch (p->data_size) {
677
0
        case sizeof(double):
678
0
            shift = real_shift();
679
0
            if (shift < 8 * sizeof(val) && (val >> shift) != 0) {
680
0
                err_inexact;
681
0
                return 0;
682
0
            }
683
0
            *(double *)p->data = (double)val;
684
0
            return 1;
685
0
        }
686
0
        err_unsupported_real;
687
0
        return 0;
688
0
#endif
689
0
    }
690
0
    err_bad_type;
691
0
    return 0;
692
130k
}
693
694
OSSL_PARAM OSSL_PARAM_construct_uint32(const char *key, uint32_t *buf)
695
872k
{
696
872k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
697
872k
                                sizeof(uint32_t));
698
872k
}
699
700
int OSSL_PARAM_get_int64(const OSSL_PARAM *p, int64_t *val)
701
334
{
702
334
    if (val == NULL || p == NULL) {
703
0
        err_null_argument;
704
0
        return 0;
705
0
    }
706
707
334
    if (p->data == NULL) {
708
0
        err_null_argument;
709
0
        return 0;
710
0
    }
711
712
334
    if (p->data_type == OSSL_PARAM_INTEGER) {
713
26
#ifndef OPENSSL_SMALL_FOOTPRINT
714
26
        switch (p->data_size) {
715
0
        case sizeof(int32_t):
716
0
            *val = *(const int32_t *)p->data;
717
0
            return 1;
718
26
        case sizeof(int64_t):
719
26
            *val = *(const int64_t *)p->data;
720
26
            return 1;
721
26
        }
722
0
#endif
723
0
        return general_get_int(p, val, sizeof(*val));
724
308
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
725
308
#ifndef OPENSSL_SMALL_FOOTPRINT
726
308
        uint64_t u64;
727
728
308
        switch (p->data_size) {
729
0
        case sizeof(uint32_t):
730
0
            *val = *(const uint32_t *)p->data;
731
0
            return 1;
732
308
        case sizeof(uint64_t):
733
308
            u64 = *(const uint64_t *)p->data;
734
308
            if (u64 <= INT64_MAX) {
735
308
                *val = (int64_t)u64;
736
308
                return 1;
737
308
            }
738
0
            err_out_of_range;
739
0
            return 0;
740
308
        }
741
0
#endif
742
0
        return general_get_int(p, val, sizeof(*val));
743
308
    } else if (p->data_type == OSSL_PARAM_REAL) {
744
0
#ifndef OPENSSL_SYS_UEFI
745
0
        double d;
746
747
0
        switch (p->data_size) {
748
0
        case sizeof(double):
749
0
            d = *(const double *)p->data;
750
0
            if (d >= INT64_MIN
751
                    /*
752
                     * By subtracting 65535 (2^16-1) we cancel the low order
753
                     * 15 bits of INT64_MAX to avoid using imprecise floating
754
                     * point values.
755
                     */
756
0
                    && d < (double)(INT64_MAX - 65535) + 65536.0
757
0
                    && d == (int64_t)d) {
758
0
                *val = (int64_t)d;
759
0
                return 1;
760
0
            }
761
0
            err_inexact;
762
0
            return 0;
763
0
        }
764
0
        err_unsupported_real;
765
0
        return 0;
766
0
#endif
767
0
    }
768
0
    err_bad_type;
769
0
    return 0;
770
334
}
771
772
int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
773
0
{
774
0
    if (p == NULL) {
775
0
        err_null_argument;
776
0
        return 0;
777
0
    }
778
0
    p->return_size = 0;
779
0
    if (p->data_type == OSSL_PARAM_INTEGER) {
780
0
#ifndef OPENSSL_SMALL_FOOTPRINT
781
0
        p->return_size = sizeof(int64_t); /* Expected size */
782
0
        if (p->data == NULL)
783
0
            return 1;
784
0
        switch (p->data_size) {
785
0
        case sizeof(int32_t):
786
0
            if (val >= INT32_MIN && val <= INT32_MAX) {
787
0
                p->return_size = sizeof(int32_t);
788
0
                *(int32_t *)p->data = (int32_t)val;
789
0
                return 1;
790
0
            }
791
0
            err_out_of_range;
792
0
            return 0;
793
0
        case sizeof(int64_t):
794
0
            *(int64_t *)p->data = val;
795
0
            return 1;
796
0
        }
797
0
#endif
798
0
        return general_set_int(p, &val, sizeof(val));
799
0
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
800
0
#ifndef OPENSSL_SMALL_FOOTPRINT
801
0
        p->return_size = sizeof(uint64_t); /* Expected size */
802
0
        if (p->data == NULL)
803
0
            return 1;
804
0
        switch (p->data_size) {
805
0
        case sizeof(uint32_t):
806
0
            if (val <= UINT32_MAX) {
807
0
                p->return_size = sizeof(uint32_t);
808
0
                *(uint32_t *)p->data = (uint32_t)val;
809
0
                return 1;
810
0
            }
811
0
            err_out_of_range;
812
0
            return 0;
813
0
        case sizeof(uint64_t):
814
0
            *(uint64_t *)p->data = (uint64_t)val;
815
0
            return 1;
816
0
        }
817
0
#endif
818
0
        return general_set_int(p, &val, sizeof(val));
819
0
    } else if (p->data_type == OSSL_PARAM_REAL) {
820
0
#ifndef OPENSSL_SYS_UEFI
821
0
        uint64_t u64;
822
823
0
        p->return_size = sizeof(double);
824
0
        if (p->data == NULL)
825
0
            return 1;
826
0
        switch (p->data_size) {
827
0
        case sizeof(double):
828
0
            u64 = val < 0 ? -val : val;
829
0
            if ((u64 >> real_shift()) == 0) {
830
0
                *(double *)p->data = (double)val;
831
0
                return 1;
832
0
            }
833
0
            err_inexact;
834
0
            return 0;
835
0
        }
836
0
        err_unsupported_real;
837
0
        return 0;
838
0
#endif
839
0
    }
840
0
    err_bad_type;
841
0
    return 0;
842
0
}
843
844
OSSL_PARAM OSSL_PARAM_construct_int64(const char *key, int64_t *buf)
845
0
{
846
0
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(int64_t));
847
0
}
848
849
int OSSL_PARAM_get_uint64(const OSSL_PARAM *p, uint64_t *val)
850
103M
{
851
103M
    if (val == NULL || p == NULL) {
852
0
        err_null_argument;
853
0
        return 0;
854
0
    }
855
856
103M
    if (p->data == NULL) {
857
0
        err_null_argument;
858
0
        return 0;
859
0
    }
860
861
103M
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
862
103M
#ifndef OPENSSL_SMALL_FOOTPRINT
863
103M
        switch (p->data_size) {
864
0
        case sizeof(uint32_t):
865
0
            *val = *(const uint32_t *)p->data;
866
0
            return 1;
867
103M
        case sizeof(uint64_t):
868
103M
            *val = *(const uint64_t *)p->data;
869
103M
            return 1;
870
103M
        }
871
0
#endif
872
0
        return general_get_uint(p, val, sizeof(*val));
873
103M
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
874
0
#ifndef OPENSSL_SMALL_FOOTPRINT
875
0
        int32_t i32;
876
0
        int64_t i64;
877
878
0
        switch (p->data_size) {
879
0
        case sizeof(int32_t):
880
0
            i32 = *(const int32_t *)p->data;
881
0
            if (i32 >= 0) {
882
0
                *val = (uint64_t)i32;
883
0
                return 1;
884
0
            }
885
0
            err_unsigned_negative;
886
0
            return 0;
887
0
        case sizeof(int64_t):
888
0
            i64 = *(const int64_t *)p->data;
889
0
            if (i64 >= 0) {
890
0
                *val = (uint64_t)i64;
891
0
                return 1;
892
0
            }
893
0
            err_unsigned_negative;
894
0
            return 0;
895
0
        }
896
0
#endif
897
0
        return general_get_uint(p, val, sizeof(*val));
898
0
    } else if (p->data_type == OSSL_PARAM_REAL) {
899
0
#ifndef OPENSSL_SYS_UEFI
900
0
        double d;
901
902
0
        switch (p->data_size) {
903
0
        case sizeof(double):
904
0
            d = *(const double *)p->data;
905
0
            if (d >= 0
906
                    /*
907
                     * By subtracting 65535 (2^16-1) we cancel the low order
908
                     * 15 bits of UINT64_MAX to avoid using imprecise floating
909
                     * point values.
910
                     */
911
0
                    && d < (double)(UINT64_MAX - 65535) + 65536.0
912
0
                    && d == (uint64_t)d) {
913
0
                *val = (uint64_t)d;
914
0
                return 1;
915
0
            }
916
0
            err_inexact;
917
0
            return 0;
918
0
        }
919
0
        err_unsupported_real;
920
0
        return 0;
921
0
#endif
922
0
    }
923
0
    err_bad_type;
924
0
    return 0;
925
103M
}
926
927
int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
928
3.66M
{
929
3.66M
    if (p == NULL) {
930
0
        err_null_argument;
931
0
        return 0;
932
0
    }
933
3.66M
    p->return_size = 0;
934
935
3.66M
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
936
3.60M
#ifndef OPENSSL_SMALL_FOOTPRINT
937
3.60M
        p->return_size = sizeof(uint64_t); /* Expected size */
938
3.60M
        if (p->data == NULL)
939
0
            return 1;
940
3.60M
        switch (p->data_size) {
941
0
        case sizeof(uint32_t):
942
0
            if (val <= UINT32_MAX) {
943
0
                p->return_size = sizeof(uint32_t);
944
0
                *(uint32_t *)p->data = (uint32_t)val;
945
0
                return 1;
946
0
            }
947
0
            err_out_of_range;
948
0
            return 0;
949
3.60M
        case sizeof(uint64_t):
950
3.60M
            *(uint64_t *)p->data = val;
951
3.60M
            return 1;
952
3.60M
        }
953
0
#endif
954
0
        return general_set_uint(p, &val, sizeof(val));
955
3.60M
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
956
65.1k
#ifndef OPENSSL_SMALL_FOOTPRINT
957
65.1k
        p->return_size = sizeof(int64_t); /* Expected size */
958
65.1k
        if (p->data == NULL)
959
0
            return 1;
960
65.1k
        switch (p->data_size) {
961
65.1k
        case sizeof(int32_t):
962
65.1k
            if (val <= INT32_MAX) {
963
65.1k
                p->return_size = sizeof(int32_t);
964
65.1k
                *(int32_t *)p->data = (int32_t)val;
965
65.1k
                return 1;
966
65.1k
            }
967
0
            err_out_of_range;
968
0
            return 0;
969
0
        case sizeof(int64_t):
970
0
            if (val <= INT64_MAX) {
971
0
                *(int64_t *)p->data = (int64_t)val;
972
0
                return 1;
973
0
            }
974
0
            err_out_of_range;
975
0
            return 0;
976
65.1k
        }
977
0
#endif
978
0
        return general_set_uint(p, &val, sizeof(val));
979
65.1k
    } else if (p->data_type == OSSL_PARAM_REAL) {
980
0
#ifndef OPENSSL_SYS_UEFI
981
0
        p->return_size = sizeof(double);
982
0
        switch (p->data_size) {
983
0
        case sizeof(double):
984
0
            if ((val >> real_shift()) == 0) {
985
0
                *(double *)p->data = (double)val;
986
0
                return 1;
987
0
            }
988
0
            err_inexact;
989
0
            return 0;
990
0
        }
991
0
        err_unsupported_real;
992
0
        return 0;
993
0
#endif
994
0
    }
995
0
    err_bad_type;
996
0
    return 0;
997
3.66M
}
998
999
OSSL_PARAM OSSL_PARAM_construct_uint64(const char *key, uint64_t *buf)
1000
916k
{
1001
916k
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1002
916k
                                sizeof(uint64_t));
1003
916k
}
1004
1005
int OSSL_PARAM_get_size_t(const OSSL_PARAM *p, size_t *val)
1006
254M
{
1007
254M
#ifndef OPENSSL_SMALL_FOOTPRINT
1008
254M
    switch (sizeof(size_t)) {
1009
0
    case sizeof(uint32_t):
1010
0
        return OSSL_PARAM_get_uint32(p, (uint32_t *)val);
1011
254M
    case sizeof(uint64_t):
1012
254M
        return OSSL_PARAM_get_uint64(p, (uint64_t *)val);
1013
254M
    }
1014
0
#endif
1015
0
    return general_get_uint(p, val, sizeof(*val));
1016
254M
}
1017
1018
int OSSL_PARAM_set_size_t(OSSL_PARAM *p, size_t val)
1019
3.96M
{
1020
3.96M
#ifndef OPENSSL_SMALL_FOOTPRINT
1021
3.96M
    switch (sizeof(size_t)) {
1022
0
    case sizeof(uint32_t):
1023
0
        return OSSL_PARAM_set_uint32(p, (uint32_t)val);
1024
3.96M
    case sizeof(uint64_t):
1025
3.96M
        return OSSL_PARAM_set_uint64(p, (uint64_t)val);
1026
3.96M
    }
1027
0
#endif
1028
0
    return general_set_uint(p, &val, sizeof(val));
1029
3.96M
}
1030
1031
OSSL_PARAM OSSL_PARAM_construct_size_t(const char *key, size_t *buf)
1032
258M
{
1033
258M
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER, buf,
1034
258M
                                sizeof(size_t));
1035
258M
}
1036
1037
int OSSL_PARAM_get_time_t(const OSSL_PARAM *p, time_t *val)
1038
336
{
1039
336
#ifndef OPENSSL_SMALL_FOOTPRINT
1040
336
    switch (sizeof(time_t)) {
1041
0
    case sizeof(int32_t):
1042
0
        return OSSL_PARAM_get_int32(p, (int32_t *)val);
1043
336
    case sizeof(int64_t):
1044
336
        return OSSL_PARAM_get_int64(p, (int64_t *)val);
1045
336
    }
1046
0
#endif
1047
0
    return general_get_int(p, val, sizeof(*val));
1048
336
}
1049
1050
int OSSL_PARAM_set_time_t(OSSL_PARAM *p, time_t val)
1051
0
{
1052
0
#ifndef OPENSSL_SMALL_FOOTPRINT
1053
0
    switch (sizeof(time_t)) {
1054
0
    case sizeof(int32_t):
1055
0
        return OSSL_PARAM_set_int32(p, (int32_t)val);
1056
0
    case sizeof(int64_t):
1057
0
        return OSSL_PARAM_set_int64(p, (int64_t)val);
1058
0
    }
1059
0
#endif
1060
0
    return general_set_int(p, &val, sizeof(val));
1061
0
}
1062
1063
OSSL_PARAM OSSL_PARAM_construct_time_t(const char *key, time_t *buf)
1064
172
{
1065
172
    return ossl_param_construct(key, OSSL_PARAM_INTEGER, buf, sizeof(time_t));
1066
172
}
1067
1068
int OSSL_PARAM_get_BN(const OSSL_PARAM *p, BIGNUM **val)
1069
858k
{
1070
858k
    BIGNUM *b = NULL;
1071
1072
858k
    if (val == NULL || p == NULL || p->data == NULL) {
1073
0
        err_null_argument;
1074
0
        return 0;
1075
0
    }
1076
1077
858k
    switch (p->data_type) {
1078
858k
    case OSSL_PARAM_UNSIGNED_INTEGER:
1079
858k
        b = BN_native2bn(p->data, (int)p->data_size, *val);
1080
858k
        break;
1081
0
    case OSSL_PARAM_INTEGER:
1082
0
        b = BN_signed_native2bn(p->data, (int)p->data_size, *val);
1083
0
        break;
1084
0
    default:
1085
0
        err_bad_type;
1086
0
        break;
1087
858k
    }
1088
1089
858k
    if (b == NULL) {
1090
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_BN_LIB);
1091
0
        return 0;
1092
0
    }
1093
1094
858k
    *val = b;
1095
858k
    return 1;
1096
858k
}
1097
1098
int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
1099
0
{
1100
0
    size_t bytes;
1101
1102
0
    if (p == NULL) {
1103
0
        err_null_argument;
1104
0
        return 0;
1105
0
    }
1106
0
    p->return_size = 0;
1107
0
    if (val == NULL) {
1108
0
        err_null_argument;
1109
0
        return 0;
1110
0
    }
1111
0
    if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && BN_is_negative(val)) {
1112
0
        err_bad_type;
1113
0
        return 0;
1114
0
    }
1115
1116
0
    bytes = (size_t)BN_num_bytes(val);
1117
    /* We add 1 byte for signed numbers, to make space for a sign extension */
1118
0
    if (p->data_type == OSSL_PARAM_INTEGER)
1119
0
        bytes++;
1120
    /* We make sure that at least one byte is used, so zero is properly set */
1121
0
    if (bytes == 0)
1122
0
        bytes++;
1123
1124
0
    p->return_size = bytes;
1125
0
    if (p->data == NULL)
1126
0
        return 1;
1127
0
    if (p->data_size >= bytes) {
1128
0
        p->return_size = p->data_size;
1129
1130
0
        switch (p->data_type) {
1131
0
        case OSSL_PARAM_UNSIGNED_INTEGER:
1132
0
            if (BN_bn2nativepad(val, p->data, p->data_size) >= 0)
1133
0
                return 1;
1134
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1135
0
            break;
1136
0
        case OSSL_PARAM_INTEGER:
1137
0
            if (BN_signed_bn2native(val, p->data, p->data_size) >= 0)
1138
0
                return 1;
1139
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INTEGER_OVERFLOW);
1140
0
            break;
1141
0
        default:
1142
0
            err_bad_type;
1143
0
            break;
1144
0
        }
1145
0
        return 0;
1146
0
    }
1147
0
    err_too_small;
1148
0
    return 0;
1149
0
}
1150
1151
OSSL_PARAM OSSL_PARAM_construct_BN(const char *key, unsigned char *buf,
1152
                                   size_t bsize)
1153
0
{
1154
0
    return ossl_param_construct(key, OSSL_PARAM_UNSIGNED_INTEGER,
1155
0
                                buf, bsize);
1156
0
}
1157
1158
#ifndef OPENSSL_SYS_UEFI
1159
int OSSL_PARAM_get_double(const OSSL_PARAM *p, double *val)
1160
0
{
1161
0
    int64_t i64;
1162
0
    uint64_t u64;
1163
1164
0
    if (val == NULL || p == NULL || p->data == NULL) {
1165
0
        err_null_argument;
1166
0
        return 0;
1167
0
    }
1168
1169
0
    if (p->data_type == OSSL_PARAM_REAL) {
1170
0
        switch (p->data_size) {
1171
0
        case sizeof(double):
1172
0
            *val = *(const double *)p->data;
1173
0
            return 1;
1174
0
        }
1175
0
        err_unsupported_real;
1176
0
        return 0;
1177
0
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1178
0
        switch (p->data_size) {
1179
0
        case sizeof(uint32_t):
1180
0
            *val = *(const uint32_t *)p->data;
1181
0
            return 1;
1182
0
        case sizeof(uint64_t):
1183
0
            u64 = *(const uint64_t *)p->data;
1184
0
            if ((u64 >> real_shift()) == 0) {
1185
0
                *val = (double)u64;
1186
0
                return 1;
1187
0
            }
1188
0
            err_inexact;
1189
0
            return 0;
1190
0
        }
1191
0
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
1192
0
        switch (p->data_size) {
1193
0
        case sizeof(int32_t):
1194
0
            *val = *(const int32_t *)p->data;
1195
0
            return 1;
1196
0
        case sizeof(int64_t):
1197
0
            i64 = *(const int64_t *)p->data;
1198
0
            u64 = i64 < 0 ? -i64 : i64;
1199
0
            if ((u64 >> real_shift()) == 0) {
1200
0
                *val = 0.0 + i64;
1201
0
                return 1;
1202
0
            }
1203
0
            err_inexact;
1204
0
            return 0;
1205
0
        }
1206
0
    }
1207
0
    err_bad_type;
1208
0
    return 0;
1209
0
}
1210
1211
int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
1212
0
{
1213
0
    if (p == NULL) {
1214
0
        err_null_argument;
1215
0
        return 0;
1216
0
    }
1217
0
    p->return_size = 0;
1218
1219
0
    if (p->data_type == OSSL_PARAM_REAL) {
1220
0
        p->return_size = sizeof(double);
1221
0
        if (p->data == NULL)
1222
0
            return 1;
1223
0
        switch (p->data_size) {
1224
0
        case sizeof(double):
1225
0
            *(double *)p->data = val;
1226
0
            return 1;
1227
0
        }
1228
0
        err_unsupported_real;
1229
0
        return 0;
1230
0
    } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
1231
0
        p->return_size = sizeof(double);
1232
0
        if (p->data == NULL)
1233
0
            return 1;
1234
0
        if (val != (uint64_t)val) {
1235
0
            err_inexact;
1236
0
            return 0;
1237
0
        }
1238
0
        switch (p->data_size) {
1239
0
        case sizeof(uint32_t):
1240
0
            if (val >= 0 && val <= UINT32_MAX) {
1241
0
                p->return_size = sizeof(uint32_t);
1242
0
                *(uint32_t *)p->data = (uint32_t)val;
1243
0
                return 1;
1244
0
            }
1245
0
            err_out_of_range;
1246
0
            return 0;
1247
0
        case sizeof(uint64_t):
1248
0
            if (val >= 0
1249
                    /*
1250
                     * By subtracting 65535 (2^16-1) we cancel the low order
1251
                     * 15 bits of UINT64_MAX to avoid using imprecise floating
1252
                     * point values.
1253
                     */
1254
0
                    && val < (double)(UINT64_MAX - 65535) + 65536.0) {
1255
0
                p->return_size = sizeof(uint64_t);
1256
0
                *(uint64_t *)p->data = (uint64_t)val;
1257
0
                return 1;
1258
0
            }
1259
0
            err_out_of_range;
1260
0
            return 0;
1261
0
        }
1262
0
    } else if (p->data_type == OSSL_PARAM_INTEGER) {
1263
0
        p->return_size = sizeof(double);
1264
0
        if (p->data == NULL)
1265
0
            return 1;
1266
0
        if (val != (int64_t)val) {
1267
0
            err_inexact;
1268
0
            return 0;
1269
0
        }
1270
0
        switch (p->data_size) {
1271
0
        case sizeof(int32_t):
1272
0
            if (val >= INT32_MIN && val <= INT32_MAX) {
1273
0
                p->return_size = sizeof(int32_t);
1274
0
                *(int32_t *)p->data = (int32_t)val;
1275
0
                return 1;
1276
0
            }
1277
0
            err_out_of_range;
1278
0
            return 0;
1279
0
        case sizeof(int64_t):
1280
0
            if (val >= INT64_MIN
1281
                    /*
1282
                     * By subtracting 65535 (2^16-1) we cancel the low order
1283
                     * 15 bits of INT64_MAX to avoid using imprecise floating
1284
                     * point values.
1285
                     */
1286
0
                    && val < (double)(INT64_MAX - 65535) + 65536.0) {
1287
0
                p->return_size = sizeof(int64_t);
1288
0
                *(int64_t *)p->data = (int64_t)val;
1289
0
                return 1;
1290
0
            }
1291
0
            err_out_of_range;
1292
0
            return 0;
1293
0
        }
1294
0
    }
1295
0
    err_bad_type;
1296
0
    return 0;
1297
0
}
1298
1299
OSSL_PARAM OSSL_PARAM_construct_double(const char *key, double *buf)
1300
0
{
1301
0
    return ossl_param_construct(key, OSSL_PARAM_REAL, buf, sizeof(double));
1302
0
}
1303
#endif
1304
1305
static int get_string_internal(const OSSL_PARAM *p, void **val,
1306
                               size_t *max_len, size_t *used_len,
1307
                               unsigned int type)
1308
5.59M
{
1309
5.59M
    size_t sz, alloc_sz;
1310
1311
5.59M
    if ((val == NULL && used_len == NULL) || p == NULL) {
1312
0
        err_null_argument;
1313
0
        return 0;
1314
0
    }
1315
5.59M
    if (p->data_type != type) {
1316
0
        err_bad_type;
1317
0
        return 0;
1318
0
    }
1319
1320
5.59M
    sz = p->data_size;
1321
    /*
1322
     * If the input size is 0, or the input string needs NUL byte
1323
     * termination, allocate an extra byte.
1324
     */
1325
5.59M
    alloc_sz = sz + (type == OSSL_PARAM_UTF8_STRING || sz == 0);
1326
1327
5.59M
    if (used_len != NULL)
1328
4.48M
        *used_len = sz;
1329
1330
5.59M
    if (p->data == NULL) {
1331
25
        err_null_argument;
1332
25
        return 0;
1333
25
    }
1334
1335
5.59M
    if (val == NULL)
1336
0
        return 1;
1337
1338
5.59M
    if (*val == NULL) {
1339
4.71M
        char *const q = OPENSSL_malloc(alloc_sz);
1340
1341
4.71M
        if (q == NULL)
1342
0
            return 0;
1343
4.71M
        *val = q;
1344
4.71M
        *max_len = alloc_sz;
1345
4.71M
    }
1346
1347
5.59M
    if (*max_len < sz) {
1348
76
        err_too_small;
1349
76
        return 0;
1350
76
    }
1351
5.59M
    memcpy(*val, p->data, sz);
1352
5.59M
    return 1;
1353
5.59M
}
1354
1355
int OSSL_PARAM_get_utf8_string(const OSSL_PARAM *p, char **val, size_t max_len)
1356
1.37M
{
1357
1.37M
    int ret = get_string_internal(p, (void **)val, &max_len, NULL,
1358
1.37M
                                  OSSL_PARAM_UTF8_STRING);
1359
1360
    /*
1361
     * We try to ensure that the copied string is terminated with a
1362
     * NUL byte.  That should be easy, just place a NUL byte at
1363
     * |((char*)*val)[p->data_size]|.
1364
     * Unfortunately, we have seen cases where |p->data_size| doesn't
1365
     * correctly reflect the length of the string, and just happens
1366
     * to be out of bounds according to |max_len|, so in that case, we
1367
     * make the extra step of trying to find the true length of the
1368
     * string that |p->data| points at, and use that as an index to
1369
     * place the NUL byte in |*val|.
1370
     */
1371
1.37M
    size_t data_length = p->data_size;
1372
1373
1.37M
    if (ret == 0)
1374
0
        return 0;
1375
1.37M
    if (data_length >= max_len)
1376
0
        data_length = OPENSSL_strnlen(p->data, data_length);
1377
1.37M
    if (data_length >= max_len) {
1378
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_NO_SPACE_FOR_TERMINATING_NULL);
1379
0
        return 0;            /* No space for a terminating NUL byte */
1380
0
    }
1381
1.37M
    (*val)[data_length] = '\0';
1382
1383
1.37M
    return ret;
1384
1.37M
}
1385
1386
int OSSL_PARAM_get_octet_string(const OSSL_PARAM *p, void **val, size_t max_len,
1387
                                size_t *used_len)
1388
4.57M
{
1389
4.57M
    return get_string_internal(p, val, &max_len, used_len,
1390
4.57M
                               OSSL_PARAM_OCTET_STRING);
1391
4.57M
}
1392
1393
static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
1394
                               unsigned int type)
1395
950k
{
1396
950k
    p->return_size = len;
1397
950k
    if (p->data == NULL)
1398
52.0k
        return 1;
1399
898k
    if (p->data_type != type) {
1400
0
        err_bad_type;
1401
0
        return 0;
1402
0
    }
1403
898k
    if (p->data_size < len) {
1404
0
        err_too_small;
1405
0
        return 0;
1406
0
    }
1407
1408
898k
    memcpy(p->data, val, len);
1409
    /* If possible within the size of p->data, add a NUL terminator byte */
1410
898k
    if (type == OSSL_PARAM_UTF8_STRING && p->data_size > len)
1411
32.5k
        ((char *)p->data)[len] = '\0';
1412
898k
    return 1;
1413
898k
}
1414
1415
int OSSL_PARAM_set_utf8_string(OSSL_PARAM *p, const char *val)
1416
40.7k
{
1417
40.7k
    if (p == NULL) {
1418
0
        err_null_argument;
1419
0
        return 0;
1420
0
    }
1421
1422
40.7k
    p->return_size = 0;
1423
40.7k
    if (val == NULL) {
1424
0
        err_null_argument;
1425
0
        return 0;
1426
0
    }
1427
40.7k
    return set_string_internal(p, val, strlen(val), OSSL_PARAM_UTF8_STRING);
1428
40.7k
}
1429
1430
int OSSL_PARAM_set_octet_string(OSSL_PARAM *p, const void *val,
1431
                                size_t len)
1432
934k
{
1433
934k
    if (p == NULL) {
1434
0
        err_null_argument;
1435
0
        return 0;
1436
0
    }
1437
1438
934k
    p->return_size = 0;
1439
934k
    if (val == NULL) {
1440
0
        err_null_argument;
1441
0
        return 0;
1442
0
    }
1443
934k
    return set_string_internal(p, val, len, OSSL_PARAM_OCTET_STRING);
1444
934k
}
1445
1446
OSSL_PARAM OSSL_PARAM_construct_utf8_string(const char *key, char *buf,
1447
                                            size_t bsize)
1448
4.36M
{
1449
4.36M
    if (buf != NULL && bsize == 0)
1450
4.29M
        bsize = strlen(buf);
1451
4.36M
    return ossl_param_construct(key, OSSL_PARAM_UTF8_STRING, buf, bsize);
1452
4.36M
}
1453
1454
OSSL_PARAM OSSL_PARAM_construct_octet_string(const char *key, void *buf,
1455
                                             size_t bsize)
1456
8.04M
{
1457
8.04M
    return ossl_param_construct(key, OSSL_PARAM_OCTET_STRING, buf, bsize);
1458
8.04M
}
1459
1460
static int get_ptr_internal(const OSSL_PARAM *p, const void **val,
1461
                            size_t *used_len, unsigned int type)
1462
1.09M
{
1463
1.09M
    if (val == NULL || p == NULL) {
1464
0
        err_null_argument;
1465
0
        return 0;
1466
0
    }
1467
1.09M
    if (p->data_type != type) {
1468
1.09M
        err_bad_type;
1469
1.09M
        return 0;
1470
1.09M
    }
1471
0
    if (used_len != NULL)
1472
0
        *used_len = p->data_size;
1473
0
    *val = *(const void **)p->data;
1474
0
    return 1;
1475
1.09M
}
1476
1477
int OSSL_PARAM_get_utf8_ptr(const OSSL_PARAM *p, const char **val)
1478
1.40M
{
1479
1.40M
    return get_ptr_internal(p, (const void **)val, NULL, OSSL_PARAM_UTF8_PTR);
1480
1.40M
}
1481
1482
int OSSL_PARAM_get_octet_ptr(const OSSL_PARAM *p, const void **val,
1483
                             size_t *used_len)
1484
72.8k
{
1485
72.8k
    return get_ptr_internal(p, val, used_len, OSSL_PARAM_OCTET_PTR);
1486
72.8k
}
1487
1488
static int set_ptr_internal(OSSL_PARAM *p, const void *val,
1489
                            unsigned int type, size_t len)
1490
336k
{
1491
336k
    p->return_size = len;
1492
336k
    if (p->data_type != type) {
1493
0
        err_bad_type;
1494
0
        return 0;
1495
0
    }
1496
336k
    if (p->data != NULL)
1497
336k
        *(const void **)p->data = val;
1498
336k
    return 1;
1499
336k
}
1500
1501
int OSSL_PARAM_set_utf8_ptr(OSSL_PARAM *p, const char *val)
1502
0
{
1503
0
    if (p == NULL) {
1504
0
        err_null_argument;
1505
0
        return 0;
1506
0
    }
1507
0
    p->return_size = 0;
1508
0
    return set_ptr_internal(p, val, OSSL_PARAM_UTF8_PTR,
1509
0
                            val == NULL ? 0 : strlen(val));
1510
0
}
1511
1512
int OSSL_PARAM_set_octet_ptr(OSSL_PARAM *p, const void *val,
1513
                             size_t used_len)
1514
336k
{
1515
336k
    if (p == NULL) {
1516
0
        err_null_argument;
1517
0
        return 0;
1518
0
    }
1519
336k
    p->return_size = 0;
1520
336k
    return set_ptr_internal(p, val, OSSL_PARAM_OCTET_PTR, used_len);
1521
336k
}
1522
1523
OSSL_PARAM OSSL_PARAM_construct_utf8_ptr(const char *key, char **buf,
1524
                                         size_t bsize)
1525
0
{
1526
0
    return ossl_param_construct(key, OSSL_PARAM_UTF8_PTR, buf, bsize);
1527
0
}
1528
1529
OSSL_PARAM OSSL_PARAM_construct_octet_ptr(const char *key, void **buf,
1530
                                          size_t bsize)
1531
337k
{
1532
337k
    return ossl_param_construct(key, OSSL_PARAM_OCTET_PTR, buf, bsize);
1533
337k
}
1534
1535
/*
1536
 * Extract the parameter into an allocated buffer.
1537
 * Any existing allocation in *out is cleared and freed.
1538
 *
1539
 * Returns 1 on success, 0 on failure and -1 if there are no matching params.
1540
 *
1541
 * *out and *out_len are guaranteed to be untouched if this function
1542
 * doesn't return success.
1543
 */
1544
int ossl_param_get1_octet_string(const OSSL_PARAM *params, const char *name,
1545
                                 unsigned char **out, size_t *out_len)
1546
410
{
1547
410
    const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1548
410
    void *buf = NULL;
1549
410
    size_t len = 0;
1550
1551
410
    if (p == NULL)
1552
0
        return -1;
1553
1554
410
    if (p->data != NULL
1555
410
            && p->data_size > 0
1556
410
            && !OSSL_PARAM_get_octet_string(p, &buf, 0, &len))
1557
0
        return 0;
1558
1559
410
    OPENSSL_clear_free(*out, *out_len);
1560
410
    *out = buf;
1561
410
    *out_len = len;
1562
410
    return 1;
1563
410
}
1564
1565
static int setbuf_fromparams(const OSSL_PARAM *p, const char *name,
1566
                             unsigned char *out, size_t *outlen)
1567
283
{
1568
283
    int ret = 0;
1569
283
    WPACKET pkt;
1570
1571
283
    if (out == NULL) {
1572
160
        if (!WPACKET_init_null(&pkt, 0))
1573
0
            return 0;
1574
160
    } else {
1575
123
        if (!WPACKET_init_static_len(&pkt, out, *outlen, 0))
1576
0
            return 0;
1577
123
    }
1578
1579
566
    for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, name)) {
1580
283
        if (p->data_type != OSSL_PARAM_OCTET_STRING)
1581
0
            goto err;
1582
283
        if (p->data != NULL
1583
283
                && p->data_size != 0
1584
283
                && !WPACKET_memcpy(&pkt, p->data, p->data_size))
1585
0
            goto err;
1586
283
    }
1587
283
    if (!WPACKET_get_total_written(&pkt, outlen)
1588
283
            || !WPACKET_finish(&pkt))
1589
0
        goto err;
1590
283
    ret = 1;
1591
283
err:
1592
283
    WPACKET_cleanup(&pkt);
1593
283
    return ret;
1594
283
}
1595
1596
int ossl_param_get1_concat_octet_string(const OSSL_PARAM *params, const char *name,
1597
                                        unsigned char **out,
1598
                                        size_t *out_len, size_t maxsize)
1599
37.7k
{
1600
37.7k
    const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
1601
37.7k
    unsigned char *res;
1602
37.7k
    size_t sz = 0;
1603
1604
37.7k
    if (p == NULL)
1605
37.5k
        return -1;
1606
1607
    /* Calculate the total size */
1608
160
    if (!setbuf_fromparams(p, name, NULL, &sz))
1609
0
        return 0;
1610
1611
    /* Check that it's not oversized */
1612
160
    if (maxsize > 0 && sz > maxsize)
1613
0
        return 0;
1614
1615
    /* Special case zero length */
1616
160
    if (sz == 0) {
1617
37
        if ((res = OPENSSL_zalloc(1)) == NULL)
1618
0
            return 0;
1619
37
        goto fin;
1620
37
    }
1621
1622
    /* Allocate the buffer */
1623
123
    res = OPENSSL_malloc(sz);
1624
123
    if (res == NULL)
1625
0
        return 0;
1626
1627
    /* Concat one or more OSSL_KDF_PARAM_INFO fields */
1628
123
    if (!setbuf_fromparams(p, name, res, &sz)) {
1629
0
        OPENSSL_clear_free(res, sz);
1630
0
        return 0;
1631
0
    }
1632
1633
160
 fin:
1634
160
    OPENSSL_clear_free(*out, *out_len);
1635
160
    *out = res;
1636
160
    *out_len = sz;
1637
160
    return 1;
1638
123
}
1639
1640
OSSL_PARAM OSSL_PARAM_construct_end(void)
1641
261M
{
1642
261M
    OSSL_PARAM end = OSSL_PARAM_END;
1643
1644
261M
    return end;
1645
261M
}
1646
1647
static int get_string_ptr_internal(const OSSL_PARAM *p, const void **val,
1648
                                   size_t *used_len, unsigned int type)
1649
1.09M
{
1650
1.09M
    if (val == NULL || p == NULL) {
1651
0
        err_null_argument;
1652
0
        return 0;
1653
0
    }
1654
1.09M
    if (p->data_type != type) {
1655
0
        err_bad_type;
1656
0
        return 0;
1657
0
    }
1658
1.09M
    if (used_len != NULL)
1659
72.8k
        *used_len = p->data_size;
1660
1.09M
    *val = p->data;
1661
1.09M
    return 1;
1662
1.09M
}
1663
1664
int OSSL_PARAM_get_utf8_string_ptr(const OSSL_PARAM *p, const char **val)
1665
1.40M
{
1666
1.40M
    int rv;
1667
1668
1.40M
    ERR_set_mark();
1669
1.40M
    rv = OSSL_PARAM_get_utf8_ptr(p, val);
1670
1.40M
    ERR_pop_to_mark();
1671
1672
1.40M
    return rv || get_string_ptr_internal(p, (const void **)val, NULL,
1673
1.40M
                                         OSSL_PARAM_UTF8_STRING);
1674
1.40M
}
1675
1676
int OSSL_PARAM_get_octet_string_ptr(const OSSL_PARAM *p, const void **val,
1677
                                    size_t *used_len)
1678
72.8k
{
1679
72.8k
    int rv;
1680
1681
72.8k
    ERR_set_mark();
1682
72.8k
    rv = OSSL_PARAM_get_octet_ptr(p, val, used_len);
1683
72.8k
    ERR_pop_to_mark();
1684
1685
72.8k
    return rv || get_string_ptr_internal(p, val, used_len,
1686
72.8k
                                         OSSL_PARAM_OCTET_STRING);
1687
72.8k
}