param_build.c 11 KB

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