Coverage Report

Created: 2023-06-08 06:41

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