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
|
/*
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. 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 can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <string.h>
#include <openssl/err.h>
#include <openssl/cryptoerr.h>
#include <openssl/params.h>
#include <openssl/types.h>
#include <openssl/safestack.h>
#include "internal/cryptlib.h"
#include "openssl/param_build.h"
/*
* Special internal param type to indicate the end of an allocate OSSL_PARAM
* array.
*/
#define OSSL_PARAM_ALLOCATED_END 127
typedef struct {
const char *key;
int type;
int secure;
size_t size;
size_t alloc_blocks;
const BIGNUM *bn;
const void *string;
union {
/*
* These fields are never directly addressed, but their sizes are
* imporant so that all native types can be copied here without overrun.
*/
ossl_intmax_t i;
ossl_uintmax_t u;
double d;
} num;
} OSSL_PARAM_BLD_DEF;
DEFINE_STACK_OF(OSSL_PARAM_BLD_DEF)
struct ossl_param_bld_st {
size_t total_blocks;
size_t secure_blocks;
STACK_OF(OSSL_PARAM_BLD_DEF) *params;
};
typedef union {
OSSL_UNION_ALIGN;
} OSSL_PARAM_BLD_BLOCK;
#define ALIGN_SIZE sizeof(OSSL_PARAM_BLD_BLOCK)
static size_t bytes_to_blocks(size_t bytes)
{
return (bytes + ALIGN_SIZE - 1) / ALIGN_SIZE;
}
static OSSL_PARAM_BLD_DEF *param_push(OSSL_PARAM_BLD *bld, const char *key,
int size, size_t alloc, int type,
int secure)
{
OSSL_PARAM_BLD_DEF *pd = OPENSSL_zalloc(sizeof(*pd));
if (pd == NULL) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
return NULL;
}
pd->key = key;
pd->type = type;
pd->size = size;
pd->alloc_blocks = bytes_to_blocks(size);
if ((pd->secure = secure) != 0)
bld->secure_blocks += pd->alloc_blocks;
else
bld->total_blocks += pd->alloc_blocks;
if (sk_OSSL_PARAM_BLD_DEF_push(bld->params, pd) <= 0) {
OPENSSL_free(pd);
pd = NULL;
}
return pd;
}
static int param_push_num(OSSL_PARAM_BLD *bld, const char *key,
void *num, size_t size, int type)
{
OSSL_PARAM_BLD_DEF *pd = param_push(bld, key, size, size, type, 0);
if (pd == NULL)
return 0;
if (size > sizeof(pd->num)) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES);
return 0;
}
memcpy(&pd->num, num, size);
return 1;
}
OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void)
{
OSSL_PARAM_BLD *r = OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD));
if (r != NULL) {
r->params = sk_OSSL_PARAM_BLD_DEF_new_null();
if (r->params == NULL) {
OPENSSL_free(r);
r = NULL;
}
}
return r;
}
static void free_all_params(OSSL_PARAM_BLD *bld)
{
int i, n = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
for (i = 0; i < n; i++)
OPENSSL_free(sk_OSSL_PARAM_BLD_DEF_pop(bld->params));
}
void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld)
{
if (bld == NULL)
return;
free_all_params(bld);
sk_OSSL_PARAM_BLD_DEF_free(bld->params);
OPENSSL_free(bld);
}
int OSSL_PARAM_BLD_push_int(OSSL_PARAM_BLD *bld, const char *key, int num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
}
int OSSL_PARAM_BLD_push_uint(OSSL_PARAM_BLD *bld, const char *key,
unsigned int num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
int OSSL_PARAM_BLD_push_long(OSSL_PARAM_BLD *bld, const char *key,
long int num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
}
int OSSL_PARAM_BLD_push_ulong(OSSL_PARAM_BLD *bld, const char *key,
unsigned long int num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
int OSSL_PARAM_BLD_push_int32(OSSL_PARAM_BLD *bld, const char *key,
int32_t num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
}
int OSSL_PARAM_BLD_push_uint32(OSSL_PARAM_BLD *bld, const char *key,
uint32_t num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
int OSSL_PARAM_BLD_push_int64(OSSL_PARAM_BLD *bld, const char *key,
int64_t num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
}
int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD *bld, const char *key,
uint64_t num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key,
size_t num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
int OSSL_PARAM_BLD_push_time_t(OSSL_PARAM_BLD *bld, const char *key,
time_t num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_INTEGER);
}
int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key,
double num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL);
}
int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
const BIGNUM *bn)
{
return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn,
bn == NULL ? 0 : BN_num_bytes(bn));
}
int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
const BIGNUM *bn, size_t sz)
{
int n, secure = 0;
OSSL_PARAM_BLD_DEF *pd;
if (bn != NULL) {
n = BN_num_bytes(bn);
if (n < 0) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ZERO_LENGTH_NUMBER);
return 0;
}
if (sz < (size_t)n) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
return 0;
}
if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE)
secure = 1;
}
pd = param_push(bld, key, sz, sz, OSSL_PARAM_UNSIGNED_INTEGER, secure);
if (pd == NULL)
return 0;
pd->bn = bn;
return 1;
}
int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
const char *buf, size_t bsize)
{
OSSL_PARAM_BLD_DEF *pd;
if (bsize == 0) {
bsize = strlen(buf) + 1;
} else if (bsize > INT_MAX) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
return 0;
}
pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_UTF8_STRING, 0);
if (pd == NULL)
return 0;
pd->string = buf;
return 1;
}
int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
char *buf, size_t bsize)
{
OSSL_PARAM_BLD_DEF *pd;
if (bsize == 0) {
bsize = strlen(buf) + 1;
} else if (bsize > INT_MAX) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
return 0;
}
pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_UTF8_PTR, 0);
if (pd == NULL)
return 0;
pd->string = buf;
return 1;
}
int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
const void *buf, size_t bsize)
{
OSSL_PARAM_BLD_DEF *pd;
if (bsize > INT_MAX) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
return 0;
}
pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_OCTET_STRING, 0);
if (pd == NULL)
return 0;
pd->string = buf;
return 1;
}
int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
void *buf, size_t bsize)
{
OSSL_PARAM_BLD_DEF *pd;
if (bsize > INT_MAX) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG);
return 0;
}
pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_OCTET_PTR, 0);
if (pd == NULL)
return 0;
pd->string = buf;
return 1;
}
static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param,
OSSL_PARAM_BLD_BLOCK *blk,
OSSL_PARAM_BLD_BLOCK *secure)
{
int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
OSSL_PARAM_BLD_DEF *pd;
void *p;
for (i = 0; i < num; i++) {
pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i);
param[i].key = pd->key;
param[i].data_type = pd->type;
param[i].data_size = pd->size;
param[i].return_size = OSSL_PARAM_UNMODIFIED;
if (pd->secure) {
p = secure;
secure += pd->alloc_blocks;
} else {
p = blk;
blk += pd->alloc_blocks;
}
param[i].data = p;
if (pd->bn != NULL) {
/* BIGNUM */
BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size);
} else if (pd->type == OSSL_PARAM_OCTET_PTR
|| pd->type == OSSL_PARAM_UTF8_PTR) {
/* PTR */
*(const void **)p = pd->string;
} else if (pd->type == OSSL_PARAM_OCTET_STRING
|| pd->type == OSSL_PARAM_UTF8_STRING) {
if (pd->string != NULL)
memcpy(p, pd->string, pd->size);
else
memset(p, 0, pd->size);
} else {
/* Number, but could also be a NULL BIGNUM */
if (pd->size > sizeof(pd->num))
memset(p, 0, pd->size);
else if (pd->size > 0)
memcpy(p, &pd->num, pd->size);
}
}
param[i] = OSSL_PARAM_construct_end();
return param + i;
}
OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld)
{
OSSL_PARAM_BLD_BLOCK *blk, *s = NULL;
OSSL_PARAM *params, *last;
const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
const size_t p_blks = bytes_to_blocks((1 + num) * sizeof(*params));
const size_t total = ALIGN_SIZE * (p_blks + bld->total_blocks);
const size_t ss = ALIGN_SIZE * bld->secure_blocks;
if (ss > 0) {
s = OPENSSL_secure_malloc(ss);
if (s == NULL) {
ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_SECURE_MALLOC_FAILURE);
return NULL;
}
}
params = OPENSSL_malloc(total);
if (params == NULL) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
OPENSSL_secure_free(s);
return NULL;
}
blk = p_blks + (OSSL_PARAM_BLD_BLOCK *)(params);
last = param_bld_convert(bld, params, blk, s);
last->data_size = ss;
last->data = s;
last->data_type = OSSL_PARAM_ALLOCATED_END;
/* Reset builder for reuse */
bld->total_blocks = 0;
bld->secure_blocks = 0;
free_all_params(bld);
return params;
}
void OSSL_PARAM_BLD_free_params(OSSL_PARAM *params)
{
if (params != NULL) {
OSSL_PARAM *p;
for (p = params; p->key != NULL; p++)
;
if (p->data_type == OSSL_PARAM_ALLOCATED_END)
OPENSSL_secure_clear_free(p->data, p->data_size);
OPENSSL_free(params);
}
}
|