/src/openssl30/crypto/param_build.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/err.h> | 
| 13 |  | #include <openssl/cryptoerr.h> | 
| 14 |  | #include <openssl/params.h> | 
| 15 |  | #include <openssl/types.h> | 
| 16 |  | #include <openssl/safestack.h> | 
| 17 |  | #include "internal/param_build_set.h" | 
| 18 |  |  | 
| 19 |  | /* | 
| 20 |  |  * Special internal param type to indicate the end of an allocate OSSL_PARAM | 
| 21 |  |  * array. | 
| 22 |  |  */ | 
| 23 |  |  | 
| 24 |  | typedef struct { | 
| 25 |  |     const char *key; | 
| 26 |  |     int type; | 
| 27 |  |     int secure; | 
| 28 |  |     size_t size; | 
| 29 |  |     size_t alloc_blocks; | 
| 30 |  |     const BIGNUM *bn; | 
| 31 |  |     const void *string; | 
| 32 |  |     union { | 
| 33 |  |         /* | 
| 34 |  |          * These fields are never directly addressed, but their sizes are | 
| 35 |  |          * imporant so that all native types can be copied here without overrun. | 
| 36 |  |          */ | 
| 37 |  |         ossl_intmax_t i; | 
| 38 |  |         ossl_uintmax_t u; | 
| 39 |  |         double d; | 
| 40 |  |     } num; | 
| 41 |  | } OSSL_PARAM_BLD_DEF; | 
| 42 |  |  | 
| 43 |  | DEFINE_STACK_OF(OSSL_PARAM_BLD_DEF) | 
| 44 |  |  | 
| 45 |  | struct ossl_param_bld_st { | 
| 46 |  |     size_t total_blocks; | 
| 47 |  |     size_t secure_blocks; | 
| 48 |  |     STACK_OF(OSSL_PARAM_BLD_DEF) *params; | 
| 49 |  | }; | 
| 50 |  |  | 
| 51 |  | static OSSL_PARAM_BLD_DEF *param_push(OSSL_PARAM_BLD *bld, const char *key, | 
| 52 |  |                                       int size, size_t alloc, int type, | 
| 53 |  |                                       int secure) | 
| 54 | 0 | { | 
| 55 | 0 |     OSSL_PARAM_BLD_DEF *pd = OPENSSL_zalloc(sizeof(*pd)); | 
| 56 |  | 
 | 
| 57 | 0 |     if (pd == NULL) { | 
| 58 | 0 |         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); | 
| 59 | 0 |         return NULL; | 
| 60 | 0 |     } | 
| 61 | 0 |     pd->key = key; | 
| 62 | 0 |     pd->type = type; | 
| 63 | 0 |     pd->size = size; | 
| 64 | 0 |     pd->alloc_blocks = ossl_param_bytes_to_blocks(alloc); | 
| 65 | 0 |     if ((pd->secure = secure) != 0) | 
| 66 | 0 |         bld->secure_blocks += pd->alloc_blocks; | 
| 67 | 0 |     else | 
| 68 | 0 |         bld->total_blocks += pd->alloc_blocks; | 
| 69 | 0 |     if (sk_OSSL_PARAM_BLD_DEF_push(bld->params, pd) <= 0) { | 
| 70 | 0 |         OPENSSL_free(pd); | 
| 71 | 0 |         pd = NULL; | 
| 72 | 0 |     } | 
| 73 | 0 |     return pd; | 
| 74 | 0 | } | 
| 75 |  |  | 
| 76 |  | static int param_push_num(OSSL_PARAM_BLD *bld, const char *key, | 
| 77 |  |                           void *num, size_t size, int type) | 
| 78 | 0 | { | 
| 79 | 0 |     OSSL_PARAM_BLD_DEF *pd = param_push(bld, key, size, size, type, 0); | 
| 80 |  | 
 | 
| 81 | 0 |     if (pd == NULL) | 
| 82 | 0 |         return 0; | 
| 83 | 0 |     if (size > sizeof(pd->num)) { | 
| 84 | 0 |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_BYTES); | 
| 85 | 0 |         return 0; | 
| 86 | 0 |     } | 
| 87 | 0 |     memcpy(&pd->num, num, size); | 
| 88 | 0 |     return 1; | 
| 89 | 0 | } | 
| 90 |  |  | 
| 91 |  | OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void) | 
| 92 | 0 | { | 
| 93 | 0 |     OSSL_PARAM_BLD *r = OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD)); | 
| 94 |  | 
 | 
| 95 | 0 |     if (r != NULL) { | 
| 96 | 0 |         r->params = sk_OSSL_PARAM_BLD_DEF_new_null(); | 
| 97 | 0 |         if (r->params == NULL) { | 
| 98 | 0 |             OPENSSL_free(r); | 
| 99 | 0 |             r = NULL; | 
| 100 | 0 |         } | 
| 101 | 0 |     } | 
| 102 | 0 |     return r; | 
| 103 | 0 | } | 
| 104 |  |  | 
| 105 |  | static void free_all_params(OSSL_PARAM_BLD *bld) | 
| 106 | 0 | { | 
| 107 | 0 |     int i, n = sk_OSSL_PARAM_BLD_DEF_num(bld->params); | 
| 108 |  | 
 | 
| 109 | 0 |     for (i = 0; i < n; i++) | 
| 110 | 0 |         OPENSSL_free(sk_OSSL_PARAM_BLD_DEF_pop(bld->params)); | 
| 111 | 0 | } | 
| 112 |  |  | 
| 113 |  | void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld) | 
| 114 | 0 | { | 
| 115 | 0 |     if (bld == NULL) | 
| 116 | 0 |         return; | 
| 117 | 0 |     free_all_params(bld); | 
| 118 | 0 |     sk_OSSL_PARAM_BLD_DEF_free(bld->params); | 
| 119 | 0 |     OPENSSL_free(bld); | 
| 120 | 0 | } | 
| 121 |  |  | 
| 122 |  | int OSSL_PARAM_BLD_push_int(OSSL_PARAM_BLD *bld, const char *key, int num) | 
| 123 | 0 | { | 
| 124 | 0 |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); | 
| 125 | 0 | } | 
| 126 |  |  | 
| 127 |  | int OSSL_PARAM_BLD_push_uint(OSSL_PARAM_BLD *bld, const char *key, | 
| 128 |  |                              unsigned int num) | 
| 129 | 0 | { | 
| 130 | 0 |     return param_push_num(bld, key, &num, sizeof(num), | 
| 131 | 0 |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
| 132 | 0 | } | 
| 133 |  |  | 
| 134 |  | int OSSL_PARAM_BLD_push_long(OSSL_PARAM_BLD *bld, const char *key, | 
| 135 |  |                              long int num) | 
| 136 | 0 | { | 
| 137 | 0 |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); | 
| 138 | 0 | } | 
| 139 |  |  | 
| 140 |  | int OSSL_PARAM_BLD_push_ulong(OSSL_PARAM_BLD *bld, const char *key, | 
| 141 |  |                               unsigned long int num) | 
| 142 | 0 | { | 
| 143 | 0 |     return param_push_num(bld, key, &num, sizeof(num), | 
| 144 | 0 |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
| 145 | 0 | } | 
| 146 |  |  | 
| 147 |  | int OSSL_PARAM_BLD_push_int32(OSSL_PARAM_BLD *bld, const char *key, | 
| 148 |  |                               int32_t num) | 
| 149 | 0 | { | 
| 150 | 0 |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); | 
| 151 | 0 | } | 
| 152 |  |  | 
| 153 |  | int OSSL_PARAM_BLD_push_uint32(OSSL_PARAM_BLD *bld, const char *key, | 
| 154 |  |                                uint32_t num) | 
| 155 | 0 | { | 
| 156 | 0 |     return param_push_num(bld, key, &num, sizeof(num), | 
| 157 | 0 |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
| 158 | 0 | } | 
| 159 |  |  | 
| 160 |  | int OSSL_PARAM_BLD_push_int64(OSSL_PARAM_BLD *bld, const char *key, | 
| 161 |  |                               int64_t num) | 
| 162 | 0 | { | 
| 163 | 0 |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER); | 
| 164 | 0 | } | 
| 165 |  |  | 
| 166 |  | int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD *bld, const char *key, | 
| 167 |  |                                uint64_t num) | 
| 168 | 0 | { | 
| 169 | 0 |     return param_push_num(bld, key, &num, sizeof(num), | 
| 170 | 0 |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
| 171 | 0 | } | 
| 172 |  |  | 
| 173 |  | int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key, | 
| 174 |  |                                size_t num) | 
| 175 | 0 | { | 
| 176 | 0 |     return param_push_num(bld, key, &num, sizeof(num), | 
| 177 | 0 |                           OSSL_PARAM_UNSIGNED_INTEGER); | 
| 178 | 0 | } | 
| 179 |  |  | 
| 180 |  | int OSSL_PARAM_BLD_push_time_t(OSSL_PARAM_BLD *bld, const char *key, | 
| 181 |  |                                time_t num) | 
| 182 | 0 | { | 
| 183 | 0 |     return param_push_num(bld, key, &num, sizeof(num), | 
| 184 | 0 |                           OSSL_PARAM_INTEGER); | 
| 185 | 0 | } | 
| 186 |  |  | 
| 187 |  | int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key, | 
| 188 |  |                                double num) | 
| 189 | 0 | { | 
| 190 | 0 |     return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL); | 
| 191 | 0 | } | 
| 192 |  |  | 
| 193 |  | int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key, | 
| 194 |  |                            const BIGNUM *bn) | 
| 195 | 0 | { | 
| 196 | 0 |     return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn, | 
| 197 | 0 |                                       bn == NULL ? 0 : BN_num_bytes(bn)); | 
| 198 | 0 | } | 
| 199 |  |  | 
| 200 |  | int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key, | 
| 201 |  |                                const BIGNUM *bn, size_t sz) | 
| 202 | 0 | { | 
| 203 | 0 |     int n, secure = 0; | 
| 204 | 0 |     OSSL_PARAM_BLD_DEF *pd; | 
| 205 |  | 
 | 
| 206 | 0 |     if (bn != NULL) { | 
| 207 | 0 |         if (BN_is_negative(bn)) { | 
| 208 | 0 |             ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_UNSUPPORTED, | 
| 209 | 0 |                            "Negative big numbers are unsupported for OSSL_PARAM"); | 
| 210 | 0 |             return 0; | 
| 211 | 0 |         } | 
| 212 |  |  | 
| 213 | 0 |         n = BN_num_bytes(bn); | 
| 214 | 0 |         if (n < 0) { | 
| 215 | 0 |             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ZERO_LENGTH_NUMBER); | 
| 216 | 0 |             return 0; | 
| 217 | 0 |         } | 
| 218 | 0 |         if (sz < (size_t)n) { | 
| 219 | 0 |             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); | 
| 220 | 0 |             return 0; | 
| 221 | 0 |         } | 
| 222 | 0 |         if (BN_get_flags(bn, BN_FLG_SECURE) == BN_FLG_SECURE) | 
| 223 | 0 |             secure = 1; | 
| 224 |  |  | 
| 225 |  |         /* The BIGNUM is zero, we must transfer at least one byte */ | 
| 226 | 0 |         if (sz == 0) | 
| 227 | 0 |             sz++; | 
| 228 | 0 |     } | 
| 229 | 0 |     pd = param_push(bld, key, sz, sz, OSSL_PARAM_UNSIGNED_INTEGER, secure); | 
| 230 | 0 |     if (pd == NULL) | 
| 231 | 0 |         return 0; | 
| 232 | 0 |     pd->bn = bn; | 
| 233 | 0 |     return 1; | 
| 234 | 0 | } | 
| 235 |  |  | 
| 236 |  | int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key, | 
| 237 |  |                                     const char *buf, size_t bsize) | 
| 238 | 0 | { | 
| 239 | 0 |     OSSL_PARAM_BLD_DEF *pd; | 
| 240 | 0 |     int secure; | 
| 241 |  | 
 | 
| 242 | 0 |     if (bsize == 0) { | 
| 243 | 0 |         bsize = strlen(buf); | 
| 244 | 0 |     } else if (bsize > INT_MAX) { | 
| 245 | 0 |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); | 
| 246 | 0 |         return 0; | 
| 247 | 0 |     } | 
| 248 | 0 |     secure = CRYPTO_secure_allocated(buf); | 
| 249 | 0 |     pd = param_push(bld, key, bsize, bsize + 1, OSSL_PARAM_UTF8_STRING, secure); | 
| 250 | 0 |     if (pd == NULL) | 
| 251 | 0 |         return 0; | 
| 252 | 0 |     pd->string = buf; | 
| 253 | 0 |     return 1; | 
| 254 | 0 | } | 
| 255 |  |  | 
| 256 |  | int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key, | 
| 257 |  |                                  char *buf, size_t bsize) | 
| 258 | 0 | { | 
| 259 | 0 |     OSSL_PARAM_BLD_DEF *pd; | 
| 260 |  | 
 | 
| 261 | 0 |     if (bsize == 0) { | 
| 262 | 0 |         bsize = strlen(buf); | 
| 263 | 0 |     } else if (bsize > INT_MAX) { | 
| 264 | 0 |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); | 
| 265 | 0 |         return 0; | 
| 266 | 0 |     } | 
| 267 | 0 |     pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_UTF8_PTR, 0); | 
| 268 | 0 |     if (pd == NULL) | 
| 269 | 0 |         return 0; | 
| 270 | 0 |     pd->string = buf; | 
| 271 | 0 |     return 1; | 
| 272 | 0 | } | 
| 273 |  |  | 
| 274 |  | int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key, | 
| 275 |  |                                      const void *buf, size_t bsize) | 
| 276 | 0 | { | 
| 277 | 0 |     OSSL_PARAM_BLD_DEF *pd; | 
| 278 | 0 |     int secure; | 
| 279 |  | 
 | 
| 280 | 0 |     if (bsize > INT_MAX) { | 
| 281 | 0 |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); | 
| 282 | 0 |         return 0; | 
| 283 | 0 |     } | 
| 284 | 0 |     secure = CRYPTO_secure_allocated(buf); | 
| 285 | 0 |     pd = param_push(bld, key, bsize, bsize, OSSL_PARAM_OCTET_STRING, secure); | 
| 286 | 0 |     if (pd == NULL) | 
| 287 | 0 |         return 0; | 
| 288 | 0 |     pd->string = buf; | 
| 289 | 0 |     return 1; | 
| 290 | 0 | } | 
| 291 |  |  | 
| 292 |  | int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key, | 
| 293 |  |                                   void *buf, size_t bsize) | 
| 294 | 0 | { | 
| 295 | 0 |     OSSL_PARAM_BLD_DEF *pd; | 
| 296 |  | 
 | 
| 297 | 0 |     if (bsize > INT_MAX) { | 
| 298 | 0 |         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_STRING_TOO_LONG); | 
| 299 | 0 |         return 0; | 
| 300 | 0 |     } | 
| 301 | 0 |     pd = param_push(bld, key, bsize, sizeof(buf), OSSL_PARAM_OCTET_PTR, 0); | 
| 302 | 0 |     if (pd == NULL) | 
| 303 | 0 |         return 0; | 
| 304 | 0 |     pd->string = buf; | 
| 305 | 0 |     return 1; | 
| 306 | 0 | } | 
| 307 |  |  | 
| 308 |  | static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param, | 
| 309 |  |                                      OSSL_PARAM_ALIGNED_BLOCK *blk, | 
| 310 |  |                                      OSSL_PARAM_ALIGNED_BLOCK *secure) | 
| 311 | 0 | { | 
| 312 | 0 |     int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params); | 
| 313 | 0 |     OSSL_PARAM_BLD_DEF *pd; | 
| 314 | 0 |     void *p; | 
| 315 |  | 
 | 
| 316 | 0 |     for (i = 0; i < num; i++) { | 
| 317 | 0 |         pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i); | 
| 318 | 0 |         param[i].key = pd->key; | 
| 319 | 0 |         param[i].data_type = pd->type; | 
| 320 | 0 |         param[i].data_size = pd->size; | 
| 321 | 0 |         param[i].return_size = OSSL_PARAM_UNMODIFIED; | 
| 322 |  | 
 | 
| 323 | 0 |         if (pd->secure) { | 
| 324 | 0 |             p = secure; | 
| 325 | 0 |             secure += pd->alloc_blocks; | 
| 326 | 0 |         } else { | 
| 327 | 0 |             p = blk; | 
| 328 | 0 |             blk += pd->alloc_blocks; | 
| 329 | 0 |         } | 
| 330 | 0 |         param[i].data = p; | 
| 331 | 0 |         if (pd->bn != NULL) { | 
| 332 |  |             /* BIGNUM */ | 
| 333 | 0 |             BN_bn2nativepad(pd->bn, (unsigned char *)p, pd->size); | 
| 334 | 0 |         } else if (pd->type == OSSL_PARAM_OCTET_PTR | 
| 335 | 0 |                    || pd->type == OSSL_PARAM_UTF8_PTR) { | 
| 336 |  |             /* PTR */ | 
| 337 | 0 |             *(const void **)p = pd->string; | 
| 338 | 0 |         } else if (pd->type == OSSL_PARAM_OCTET_STRING | 
| 339 | 0 |                    || pd->type == OSSL_PARAM_UTF8_STRING) { | 
| 340 | 0 |             if (pd->string != NULL) | 
| 341 | 0 |                 memcpy(p, pd->string, pd->size); | 
| 342 | 0 |             else | 
| 343 | 0 |                 memset(p, 0, pd->size); | 
| 344 | 0 |             if (pd->type == OSSL_PARAM_UTF8_STRING) | 
| 345 | 0 |                 ((char *)p)[pd->size] = '\0'; | 
| 346 | 0 |         } else { | 
| 347 |  |             /* Number, but could also be a NULL BIGNUM */ | 
| 348 | 0 |             if (pd->size > sizeof(pd->num)) | 
| 349 | 0 |                 memset(p, 0, pd->size); | 
| 350 | 0 |             else if (pd->size > 0) | 
| 351 | 0 |                 memcpy(p, &pd->num, pd->size); | 
| 352 | 0 |         } | 
| 353 | 0 |     } | 
| 354 | 0 |     param[i] = OSSL_PARAM_construct_end(); | 
| 355 | 0 |     return param + i; | 
| 356 | 0 | } | 
| 357 |  |  | 
| 358 |  | OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld) | 
| 359 | 0 | { | 
| 360 | 0 |     OSSL_PARAM_ALIGNED_BLOCK *blk, *s = NULL; | 
| 361 | 0 |     OSSL_PARAM *params, *last; | 
| 362 | 0 |     const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params); | 
| 363 | 0 |     const size_t p_blks = ossl_param_bytes_to_blocks((1 + num) * sizeof(*params)); | 
| 364 | 0 |     const size_t total = OSSL_PARAM_ALIGN_SIZE * (p_blks + bld->total_blocks); | 
| 365 | 0 |     const size_t ss = OSSL_PARAM_ALIGN_SIZE * bld->secure_blocks; | 
| 366 |  | 
 | 
| 367 | 0 |     if (ss > 0) { | 
| 368 | 0 |         s = OPENSSL_secure_malloc(ss); | 
| 369 | 0 |         if (s == NULL) { | 
| 370 | 0 |             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_SECURE_MALLOC_FAILURE); | 
| 371 | 0 |             return NULL; | 
| 372 | 0 |         } | 
| 373 | 0 |     } | 
| 374 | 0 |     params = OPENSSL_malloc(total); | 
| 375 | 0 |     if (params == NULL) { | 
| 376 | 0 |         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); | 
| 377 | 0 |         OPENSSL_secure_free(s); | 
| 378 | 0 |         return NULL; | 
| 379 | 0 |     } | 
| 380 | 0 |     blk = p_blks + (OSSL_PARAM_ALIGNED_BLOCK *)(params); | 
| 381 | 0 |     last = param_bld_convert(bld, params, blk, s); | 
| 382 | 0 |     ossl_param_set_secure_block(last, s, ss); | 
| 383 |  |  | 
| 384 |  |     /* Reset builder for reuse */ | 
| 385 | 0 |     bld->total_blocks = 0; | 
| 386 | 0 |     bld->secure_blocks = 0; | 
| 387 | 0 |     free_all_params(bld); | 
| 388 | 0 |     return params; | 
| 389 | 0 | } |