Parameter building utilities.
[oweals/openssl.git] / crypto / param_build.c
1 /*
2  * Copyright 2019 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/err.h>
13 #include <openssl/cryptoerr.h>
14 #include <openssl/params.h>
15 #include "internal/cryptlib.h"
16 #include "internal/param_build.h"
17
18 typedef union {
19     OSSL_UNION_ALIGN;
20 } OSSL_PARAM_BLD_BLOCK;
21
22 #define ALIGN_SIZE  sizeof(OSSL_PARAM_BLD_BLOCK)
23
24 static size_t bytes_to_blocks(size_t bytes)
25 {
26     return (bytes + ALIGN_SIZE - 1) / ALIGN_SIZE;
27 }
28
29 static OSSL_PARAM_BLD_DEF *param_push(OSSL_PARAM_BLD *bld, const char *key,
30                                       int size, size_t alloc, int type,
31                                       int secure)
32 {
33     OSSL_PARAM_BLD_DEF *pd;
34
35     if (bld->curr >= OSSL_PARAM_BLD_MAX) {
36         CRYPTOerr(CRYPTO_F_PARAM_PUSH, CRYPTO_R_TOO_MANY_RECORDS);
37         return NULL;
38     }
39     pd = bld->params + bld->curr++;
40     memset(pd, 0, sizeof(*pd));
41     pd->key = key;
42     pd->type = type;
43     pd->size = size;
44     pd->alloc_blocks = bytes_to_blocks(size);
45     if ((pd->secure = secure) != 0)
46         bld->secure_blocks += pd->alloc_blocks;
47     else
48         bld->total_blocks += pd->alloc_blocks;
49     return pd;
50 }
51
52 static int param_push_num(OSSL_PARAM_BLD *bld, const char *key,
53                           void *num, size_t size, int type)
54 {
55     OSSL_PARAM_BLD_DEF *pd = param_push(bld, key, size, size, type, 0);
56
57     if (pd == NULL)
58         return 0;
59     if (size > sizeof(pd->num)) {
60         CRYPTOerr(CRYPTO_F_PARAM_PUSH_NUM, CRYPTO_R_TOO_MANY_BYTES);
61         return 0;
62     }
63     memcpy(&pd->num, num, size);
64     return 1;
65 }
66
67 void ossl_param_bld_init(OSSL_PARAM_BLD *bld)
68 {
69     memset(bld, 0, sizeof(*bld));
70 }
71
72 int ossl_param_bld_push_int(OSSL_PARAM_BLD *bld, const char *key, int num)
73 {
74     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
75 }
76
77 int ossl_param_bld_push_uint(OSSL_PARAM_BLD *bld, const char *key,
78                              unsigned int num)
79 {
80     return param_push_num(bld, key, &num, sizeof(num),
81                           OSSL_PARAM_UNSIGNED_INTEGER);
82 }
83
84 int ossl_param_bld_push_long(OSSL_PARAM_BLD *bld, const char *key,
85                              long int num)
86 {
87     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
88 }
89
90 int ossl_param_bld_push_ulong(OSSL_PARAM_BLD *bld, const char *key,
91                               unsigned long int num)
92 {
93     return param_push_num(bld, key, &num, sizeof(num),
94                           OSSL_PARAM_UNSIGNED_INTEGER);
95 }
96
97 int ossl_param_bld_push_int32(OSSL_PARAM_BLD *bld, const char *key,
98                               int32_t num)
99 {
100     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
101 }
102
103 int ossl_param_bld_push_uint32(OSSL_PARAM_BLD *bld, const char *key,
104                                uint32_t num)
105 {
106     return param_push_num(bld, key, &num, sizeof(num),
107                           OSSL_PARAM_UNSIGNED_INTEGER);
108 }
109
110 int ossl_param_bld_push_int64(OSSL_PARAM_BLD *bld, const char *key,
111                               int64_t num)
112 {
113     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
114 }
115
116 int ossl_param_bld_push_uint64(OSSL_PARAM_BLD *bld, const char *key,
117                                uint64_t num)
118 {
119     return param_push_num(bld, key, &num, sizeof(num),
120                           OSSL_PARAM_UNSIGNED_INTEGER);
121 }
122
123 int ossl_param_bld_push_size_t(OSSL_PARAM_BLD *bld, const char *key,
124                                size_t num)
125 {
126     return param_push_num(bld, key, &num, sizeof(num),
127                           OSSL_PARAM_UNSIGNED_INTEGER);
128 }
129
130 int ossl_param_bld_push_double(OSSL_PARAM_BLD *bld, const char *key,
131                                double num)
132 {
133     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL);
134 }
135
136 int ossl_param_bld_push_BN(OSSL_PARAM_BLD *bld, const char *key,
137                            const BIGNUM *bn)
138 {
139     int sz = -1, secure = 0;
140     OSSL_PARAM_BLD_DEF *pd;
141
142     if (bn != NULL) {
143         sz = BN_num_bytes(bn);
144         if (sz < 0) {
145             CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_BN,
146                       CRYPTO_R_ZERO_LENGTH_NUMBER);
147             return 0;
148         }
149         if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE)
150             secure = 1;
151     }
152     pd = param_push(bld, key, sz, sz >= 0 ? sz : 0,
153                     OSSL_PARAM_UNSIGNED_INTEGER, secure);
154     if (pd == NULL)
155         return 0;
156     pd->bn = bn;
157     return 1;
158 }
159
160 int ossl_param_bld_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
161                                     char *buf, size_t bsize)
162 {
163     OSSL_PARAM_BLD_DEF *pd;
164
165     if (bsize == 0) {
166         bsize = strlen(buf) + 1;
167     } else if (bsize > INT_MAX) {
168         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_UTF8_STRING,
169                   CRYPTO_R_STRING_TOO_LONG);
170         return 0;
171     }
172     pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_UTF8_STRING, 0);
173     if (pd == NULL)
174         return 0;
175     pd->string = buf;
176     return 1;
177 }
178
179 int ossl_param_bld_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
180                                  char *buf, size_t bsize)
181 {
182     OSSL_PARAM_BLD_DEF *pd;
183
184     if (bsize == 0) {
185         bsize = strlen(buf) + 1;
186     } else if (bsize > INT_MAX) {
187         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_UTF8_PTR,
188                   CRYPTO_R_STRING_TOO_LONG);
189         return 0;
190     }
191     pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_UTF8_PTR, 0);
192     if (pd == NULL)
193         return 0;
194     pd->string = buf;
195     return 1;
196 }
197
198 int ossl_param_bld_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
199                                      void *buf, size_t bsize)
200 {
201     OSSL_PARAM_BLD_DEF *pd;
202
203     if (bsize > INT_MAX) {
204         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_OCTET_STRING,
205                   CRYPTO_R_STRING_TOO_LONG);
206         return 0;
207     }
208     pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_OCTET_STRING, 0);
209     if (pd == NULL)
210         return 0;
211     pd->string = buf;
212     return 1;
213 }
214
215 int ossl_param_bld_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
216                                   void *buf, size_t bsize)
217 {
218     OSSL_PARAM_BLD_DEF *pd;
219
220     if (bsize > INT_MAX) {
221         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_PUSH_OCTET_PTR,
222                   CRYPTO_R_STRING_TOO_LONG);
223         return 0;
224     }
225     pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_OCTET_PTR, 0);
226     if (pd == NULL)
227         return 0;
228     pd->string = buf;
229     return 1;
230 }
231
232 static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param,
233                                      OSSL_PARAM_BLD_BLOCK *blk,
234                                      OSSL_PARAM_BLD_BLOCK *secure)
235 {
236     size_t i;
237     OSSL_PARAM_BLD_DEF *pd;
238     void *p;
239
240     for (i = 0; i < bld->curr; i++) {
241         pd = bld->params + i;
242         param[i].key = pd->key;
243         param[i].data_type = pd->type;
244         param[i].data_size = pd->size;
245         param[i].return_size = 0;
246
247         if (pd->secure) {
248             p = secure;
249             secure += pd->alloc_blocks;
250         } else {
251             p = blk;
252             blk += pd->alloc_blocks;
253         }
254         param[i].data = p;
255         if (pd->bn != NULL) {
256             /* BIGNUM */
257             BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size);
258         } else if (pd->type == OSSL_PARAM_OCTET_PTR
259                    || pd->type == OSSL_PARAM_UTF8_PTR) {
260             /* PTR */
261             *(void **)p = pd->string;
262         } else if (pd->type == OSSL_PARAM_OCTET_STRING
263                    || pd->type == OSSL_PARAM_UTF8_STRING) {
264             if (pd->string != NULL)
265                 memcpy(p, pd->string, pd->size);
266             else
267                 memset(p, 0, pd->size);
268         } else {
269             /* Number, but could also be a NULL BIGNUM */
270             if (pd->size > sizeof(pd->num))
271                 memset(p, 0, pd->size);
272             else if (pd->size > 0)
273                 memcpy(p, &pd->num, pd->size);
274         }
275     }
276     param[i] = OSSL_PARAM_construct_end();
277     return param;
278 }
279
280 OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld, void **secure)
281 {
282     OSSL_PARAM_BLD_BLOCK *blk, *s = NULL;
283     OSSL_PARAM *param;
284     const size_t p_blks = bytes_to_blocks((bld->curr + 1) * sizeof(*param));
285     const size_t total = ALIGN_SIZE * (p_blks + bld->total_blocks);
286
287     if (bld->secure_blocks > 0) {
288         if (secure == NULL) {
289             CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM,
290                       CRYPTO_R_INVALID_NULL_ARGUMENT);
291             return NULL;
292         }
293         s = OPENSSL_secure_malloc(bld->secure_blocks * ALIGN_SIZE);
294         if (s == NULL) {
295             CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM,
296                       CRYPTO_R_SECURE_MALLOC_FAILURE);
297             return NULL;
298         }
299     }
300     param = OPENSSL_malloc(total);
301     if (param == NULL) {
302         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM, ERR_R_MALLOC_FAILURE);
303         OPENSSL_secure_free(s);
304         return NULL;
305     }
306     if (secure != NULL)
307         *secure = s;
308     blk = p_blks + (OSSL_PARAM_BLD_BLOCK *)(param);
309     param_bld_convert(bld, param, blk, s);
310     return param;
311 }
312
313 OSSL_PARAM *ossl_param_bld_to_param_ex(OSSL_PARAM_BLD *bld, OSSL_PARAM *params,
314                                        size_t param_n, void *data,
315                                        size_t data_n, void *secure,
316                                        size_t secure_n)
317 {
318     if (params == NULL || data == NULL) {
319         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM_EX,
320                   CRYPTO_R_INVALID_NULL_ARGUMENT);
321         return NULL;
322     }
323     if (param_n < bld->curr + 1) {
324         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM_EX,
325                   CRYPTO_R_INSUFFICIENT_PARAM_SIZE);
326         return NULL;
327     }
328     if (data_n < ALIGN_SIZE * bld->total_blocks) {
329         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM_EX,
330                   CRYPTO_R_INSUFFICIENT_DATA_SPACE);
331         return NULL;
332     }
333     if (bld->secure_blocks > 0 && secure_n < ALIGN_SIZE * bld->secure_blocks) {
334         CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM_EX,
335                   CRYPTO_R_INSUFFICIENT_SECURE_DATA_SPACE);
336         return NULL;
337     }
338     param_bld_convert(bld, params, (OSSL_PARAM_BLD_BLOCK *)data,
339                       (OSSL_PARAM_BLD_BLOCK *)secure);
340     return params;
341 }