2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
12 #include <openssl/err.h>
13 #include <openssl/cryptoerr.h>
14 #include <openssl/params.h>
15 #include <openssl/types.h>
16 #include "internal/cryptlib.h"
17 #include "openssl/param_build.h"
20 * The number of OSSL_PARAM elements a builder will allow.
22 #define OSSL_PARAM_BLD_MAX 25
25 * Special internal param type to indicate the end of an allocate OSSL_PARAM
28 #define OSSL_PARAM_ALLOCATED_END 127
40 * These fields are never directly addressed, but their sizes are
41 * imporant so that all native types can be copied here without overrun.
49 struct ossl_param_bld_st
{
53 OSSL_PARAM_BLD_DEF params
[OSSL_PARAM_BLD_MAX
];
58 } OSSL_PARAM_BLD_BLOCK
;
60 #define ALIGN_SIZE sizeof(OSSL_PARAM_BLD_BLOCK)
62 static size_t bytes_to_blocks(size_t bytes
)
64 return (bytes
+ ALIGN_SIZE
- 1) / ALIGN_SIZE
;
67 static OSSL_PARAM_BLD_DEF
*param_push(OSSL_PARAM_BLD
*bld
, const char *key
,
68 int size
, size_t alloc
, int type
,
71 OSSL_PARAM_BLD_DEF
*pd
;
73 if (bld
->curr
>= OSSL_PARAM_BLD_MAX
) {
74 CRYPTOerr(CRYPTO_F_PARAM_PUSH
, CRYPTO_R_TOO_MANY_RECORDS
);
77 pd
= bld
->params
+ bld
->curr
++;
78 memset(pd
, 0, sizeof(*pd
));
82 pd
->alloc_blocks
= bytes_to_blocks(size
);
83 if ((pd
->secure
= secure
) != 0)
84 bld
->secure_blocks
+= pd
->alloc_blocks
;
86 bld
->total_blocks
+= pd
->alloc_blocks
;
90 static int param_push_num(OSSL_PARAM_BLD
*bld
, const char *key
,
91 void *num
, size_t size
, int type
)
93 OSSL_PARAM_BLD_DEF
*pd
= param_push(bld
, key
, size
, size
, type
, 0);
97 if (size
> sizeof(pd
->num
)) {
98 CRYPTOerr(CRYPTO_F_PARAM_PUSH_NUM
, CRYPTO_R_TOO_MANY_BYTES
);
101 memcpy(&pd
->num
, num
, size
);
105 OSSL_PARAM_BLD
*OSSL_PARAM_BLD_new(void)
107 return OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD
));
110 void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD
*bld
)
115 int OSSL_PARAM_BLD_push_int(OSSL_PARAM_BLD
*bld
, const char *key
, int num
)
117 return param_push_num(bld
, key
, &num
, sizeof(num
), OSSL_PARAM_INTEGER
);
120 int OSSL_PARAM_BLD_push_uint(OSSL_PARAM_BLD
*bld
, const char *key
,
123 return param_push_num(bld
, key
, &num
, sizeof(num
),
124 OSSL_PARAM_UNSIGNED_INTEGER
);
127 int OSSL_PARAM_BLD_push_long(OSSL_PARAM_BLD
*bld
, const char *key
,
130 return param_push_num(bld
, key
, &num
, sizeof(num
), OSSL_PARAM_INTEGER
);
133 int OSSL_PARAM_BLD_push_ulong(OSSL_PARAM_BLD
*bld
, const char *key
,
134 unsigned long int num
)
136 return param_push_num(bld
, key
, &num
, sizeof(num
),
137 OSSL_PARAM_UNSIGNED_INTEGER
);
140 int OSSL_PARAM_BLD_push_int32(OSSL_PARAM_BLD
*bld
, const char *key
,
143 return param_push_num(bld
, key
, &num
, sizeof(num
), OSSL_PARAM_INTEGER
);
146 int OSSL_PARAM_BLD_push_uint32(OSSL_PARAM_BLD
*bld
, const char *key
,
149 return param_push_num(bld
, key
, &num
, sizeof(num
),
150 OSSL_PARAM_UNSIGNED_INTEGER
);
153 int OSSL_PARAM_BLD_push_int64(OSSL_PARAM_BLD
*bld
, const char *key
,
156 return param_push_num(bld
, key
, &num
, sizeof(num
), OSSL_PARAM_INTEGER
);
159 int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD
*bld
, const char *key
,
162 return param_push_num(bld
, key
, &num
, sizeof(num
),
163 OSSL_PARAM_UNSIGNED_INTEGER
);
166 int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD
*bld
, const char *key
,
169 return param_push_num(bld
, key
, &num
, sizeof(num
),
170 OSSL_PARAM_UNSIGNED_INTEGER
);
173 int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD
*bld
, const char *key
,
176 return param_push_num(bld
, key
, &num
, sizeof(num
), OSSL_PARAM_REAL
);
179 int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD
*bld
, const char *key
,
182 return OSSL_PARAM_BLD_push_BN_pad(bld
, key
, bn
,
183 bn
== NULL
? 0 : BN_num_bytes(bn
));
186 int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD
*bld
, const char *key
,
187 const BIGNUM
*bn
, size_t sz
)
190 OSSL_PARAM_BLD_DEF
*pd
;
193 n
= BN_num_bytes(bn
);
195 CRYPTOerr(0, CRYPTO_R_ZERO_LENGTH_NUMBER
);
198 if (sz
< (size_t)n
) {
199 CRYPTOerr(0, CRYPTO_R_TOO_SMALL_BUFFER
);
202 if (BN_get_flags(bn
, BN_FLG_SECURE
) == BN_FLG_SECURE
)
205 pd
= param_push(bld
, key
, sz
, sz
, OSSL_PARAM_UNSIGNED_INTEGER
, secure
);
212 int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD
*bld
, const char *key
,
213 const char *buf
, size_t bsize
)
215 OSSL_PARAM_BLD_DEF
*pd
;
218 bsize
= strlen(buf
) + 1;
219 } else if (bsize
> INT_MAX
) {
220 CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_UTF8_STRING
,
221 CRYPTO_R_STRING_TOO_LONG
);
224 pd
= param_push(bld
, key
, bsize
, bsize
, OSSL_PARAM_UTF8_STRING
, 0);
231 int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD
*bld
, const char *key
,
232 char *buf
, size_t bsize
)
234 OSSL_PARAM_BLD_DEF
*pd
;
237 bsize
= strlen(buf
) + 1;
238 } else if (bsize
> INT_MAX
) {
239 CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_UTF8_PTR
,
240 CRYPTO_R_STRING_TOO_LONG
);
243 pd
= param_push(bld
, key
, bsize
, sizeof(buf
), OSSL_PARAM_UTF8_PTR
, 0);
250 int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD
*bld
, const char *key
,
251 const void *buf
, size_t bsize
)
253 OSSL_PARAM_BLD_DEF
*pd
;
255 if (bsize
> INT_MAX
) {
256 CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_OCTET_STRING
,
257 CRYPTO_R_STRING_TOO_LONG
);
260 pd
= param_push(bld
, key
, bsize
, bsize
, OSSL_PARAM_OCTET_STRING
, 0);
267 int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD
*bld
, const char *key
,
268 void *buf
, size_t bsize
)
270 OSSL_PARAM_BLD_DEF
*pd
;
272 if (bsize
> INT_MAX
) {
273 CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_OCTET_PTR
,
274 CRYPTO_R_STRING_TOO_LONG
);
277 pd
= param_push(bld
, key
, bsize
, sizeof(buf
), OSSL_PARAM_OCTET_PTR
, 0);
284 static OSSL_PARAM
*param_bld_convert(OSSL_PARAM_BLD
*bld
, OSSL_PARAM
*param
,
285 OSSL_PARAM_BLD_BLOCK
*blk
,
286 OSSL_PARAM_BLD_BLOCK
*secure
)
289 OSSL_PARAM_BLD_DEF
*pd
;
292 for (i
= 0; i
< bld
->curr
; i
++) {
293 pd
= bld
->params
+ i
;
294 param
[i
].key
= pd
->key
;
295 param
[i
].data_type
= pd
->type
;
296 param
[i
].data_size
= pd
->size
;
297 param
[i
].return_size
= 0;
301 secure
+= pd
->alloc_blocks
;
304 blk
+= pd
->alloc_blocks
;
307 if (pd
->bn
!= NULL
) {
309 BN_bn2nativepad(pd
->bn
, (unsigned char *)p
, pd
->size
);
310 } else if (pd
->type
== OSSL_PARAM_OCTET_PTR
311 || pd
->type
== OSSL_PARAM_UTF8_PTR
) {
313 *(const void **)p
= pd
->string
;
314 } else if (pd
->type
== OSSL_PARAM_OCTET_STRING
315 || pd
->type
== OSSL_PARAM_UTF8_STRING
) {
316 if (pd
->string
!= NULL
)
317 memcpy(p
, pd
->string
, pd
->size
);
319 memset(p
, 0, pd
->size
);
321 /* Number, but could also be a NULL BIGNUM */
322 if (pd
->size
> sizeof(pd
->num
))
323 memset(p
, 0, pd
->size
);
324 else if (pd
->size
> 0)
325 memcpy(p
, &pd
->num
, pd
->size
);
328 param
[i
] = OSSL_PARAM_construct_end();
332 OSSL_PARAM
*OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD
*bld
)
334 OSSL_PARAM_BLD_BLOCK
*blk
, *s
= NULL
;
335 OSSL_PARAM
*params
, *last
;
336 const size_t p_blks
= bytes_to_blocks((1 + bld
->curr
) * sizeof(*params
));
337 const size_t total
= ALIGN_SIZE
* (p_blks
+ bld
->total_blocks
);
338 const size_t ss
= ALIGN_SIZE
* bld
->secure_blocks
;
341 s
= OPENSSL_secure_malloc(ss
);
343 CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM
,
344 CRYPTO_R_SECURE_MALLOC_FAILURE
);
349 params
= OPENSSL_malloc(total
);
350 if (params
== NULL
) {
351 CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM
, ERR_R_MALLOC_FAILURE
);
353 OPENSSL_secure_free(s
);
356 blk
= p_blks
+ (OSSL_PARAM_BLD_BLOCK
*)(params
);
357 last
= param_bld_convert(bld
, params
, blk
, s
);
358 last
->data_size
= ss
;
360 last
->data_type
= OSSL_PARAM_ALLOCATED_END
;
362 /* Reset for reuse */
363 memset(bld
, 0, sizeof(*bld
));
367 void OSSL_PARAM_BLD_free_params(OSSL_PARAM
*params
)
369 if (params
!= NULL
) {
372 for (p
= params
; p
->key
!= NULL
; p
++)
374 if (p
->data_type
== OSSL_PARAM_ALLOCATED_END
)
375 OPENSSL_secure_clear_free(p
->data
, p
->data_size
);
376 OPENSSL_free(params
);