1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
/*
* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.openssl.org/source/license.html
* or in the file LICENSE in the source distribution.
*/
/*
* This program tests the use of OSSL_PARAM, currently in raw form.
*/
#include <string.h>
#include <openssl/bn.h>
#include <openssl/core.h>
#include <openssl/params.h>
#include "internal/nelem.h"
#include "testutil.h"
/*-
* PROVIDER SECTION
* ================
*
* Even though it's not necessarily ONLY providers doing this part,
* they are naturally going to be the most common users of
* set_params and get_params functions.
*/
/*
* In real use cases, setters and getters would take an object with
* which the parameters are associated. This structure is a cheap
* simulation.
*/
struct object_st {
/*
* Documented as a native integer, of the size given by sizeof(int).
* Assumed data type OSSL_PARAM_INTEGER
*/
int p1;
/*
* Documented as a native double, of the size given by sizeof(double).
* Assumed data type OSSL_PARAM_REAL
*/
double p2;
/*
* Documented as an arbitrarly large unsigned integer.
* The data size must be large enough to accommodate.
* Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
*/
BIGNUM *p3;
/*
* Documented as a C string.
* The data size must be large enough to accommodate.
* Assumed data type OSSL_PARAM_UTF8_STRING
*/
char *p4;
size_t p4_l;
/*
* Documented as a C string.
* Assumed data type OSSL_PARAM_UTF8_STRING
*/
char p5[256];
size_t p5_l;
/*
* Documented as a pointer to a constant C string.
* Assumed data type OSSL_PARAM_UTF8_PTR
*/
const char *p6;
size_t p6_l;
};
#define p1_init 42 /* The ultimate answer */
#define p2_init 6.283 /* Magic number */
/* Stolen from evp_data, BLAKE2s256 test */
#define p3_init \
"4142434445464748494a4b4c4d4e4f50" \
"5152535455565758595a616263646566" \
"6768696a6b6c6d6e6f70717273747576" \
"7778797a30313233343536373839"
#define p4_init "BLAKE2s256" /* Random string */
#define p5_init "Hellow World" /* Random string */
#define p6_init OPENSSL_FULL_VERSION_STR /* Static string */
static void cleanup_object(void *vobj)
{
struct object_st *obj = vobj;
BN_free(obj->p3);
obj->p3 = NULL;
OPENSSL_free(obj->p4);
obj->p4 = NULL;
OPENSSL_free(obj);
}
static void *init_object(void)
{
struct object_st *obj = OPENSSL_zalloc(sizeof(*obj));
obj->p1 = p1_init;
obj->p2 = p2_init;
if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
goto fail;
if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
goto fail;
strcpy(obj->p5, p5_init);
obj->p6 = p6_init;
return obj;
fail:
cleanup_object(obj);
obj = NULL;
return NULL;
}
/*
* RAW provider, which handles the parameters in a very raw manner,
* with no fancy API and very minimal checking. The application that
* calls these to set or request parameters MUST get its OSSL_PARAM
* array right.
*/
static int raw_set_params(void *vobj, const OSSL_PARAM *params)
{
struct object_st *obj = vobj;
for (; params->key != NULL; params++)
if (strcmp(params->key, "p1") == 0) {
obj->p1 = *(int *)params->data;
} else if (strcmp(params->key, "p2") == 0) {
obj->p2 = *(double *)params->data;
} else if (strcmp(params->key, "p3") == 0) {
BN_free(obj->p3);
if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
params->data_size, NULL)))
return 0;
} else if (strcmp(params->key, "p4") == 0) {
OPENSSL_free(obj->p4);
if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
params->data_size)))
return 0;
} else if (strcmp(params->key, "p5") == 0) {
strncpy(obj->p5, params->data, params->data_size);
obj->p5_l = strlen(obj->p5) + 1;
} else if (strcmp(params->key, "p6") == 0) {
obj->p6 = *(const char **)params->data;
obj->p6_l = params->data_size;
}
return 1;
}
static int raw_get_params(void *vobj, OSSL_PARAM *params)
{
struct object_st *obj = vobj;
for (; params->key != NULL; params++)
if (strcmp(params->key, "p1") == 0) {
params->return_size = sizeof(obj->p1);
*(int *)params->data = obj->p1;
} else if (strcmp(params->key, "p2") == 0) {
params->return_size = sizeof(obj->p2);
*(double *)params->data = obj->p2;
} else if (strcmp(params->key, "p3") == 0) {
size_t bytes = BN_num_bytes(obj->p3);
params->return_size = bytes;
if (!TEST_size_t_ge(params->data_size, bytes))
return 0;
BN_bn2nativepad(obj->p3, params->data, bytes);
} else if (strcmp(params->key, "p4") == 0) {
size_t bytes = strlen(obj->p4) + 1;
params->return_size = bytes;
if (!TEST_size_t_ge(params->data_size, bytes))
return 0;
strcpy(params->data, obj->p4);
} else if (strcmp(params->key, "p5") == 0) {
size_t bytes = strlen(obj->p5) + 1;
params->return_size = bytes;
if (!TEST_size_t_ge(params->data_size, bytes))
return 0;
strcpy(params->data, obj->p5);
} else if (strcmp(params->key, "p6") == 0) {
/*
* We COULD also use OPENSSL_FULL_VERSION_STR directly and
* use sizeof(OPENSSL_FULL_VERSION_STR) instead of calling
* strlen().
* The caller wouldn't know the difference.
*/
size_t bytes = strlen(obj->p6) + 1;
params->return_size = bytes;
*(const char **)params->data = obj->p6;
|