params_from_text.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/params.h>
  13. /*
  14. * When processing text to params, we're trying to be smart with numbers.
  15. * Instead of handling each specific separate integer type, we use a bignum
  16. * and ensure that it isn't larger than the expected size, and we then make
  17. * sure it is the expected size... if there is one given.
  18. * (if the size can be arbitrary, then we give whatever we have)
  19. */
  20. static int prepare_from_text(const OSSL_PARAM *paramdefs, const char *key,
  21. const char *value, size_t value_n,
  22. /* Output parameters */
  23. const OSSL_PARAM **paramdef, int *ishex,
  24. size_t *buf_n, BIGNUM **tmpbn)
  25. {
  26. const OSSL_PARAM *p;
  27. /*
  28. * ishex is used to translate legacy style string controls in hex format
  29. * to octet string parameters.
  30. */
  31. *ishex = strncmp(key, "hex", 3) == 0;
  32. if (*ishex)
  33. key += 3;
  34. p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
  35. if (p == NULL)
  36. return 0;
  37. switch (p->data_type) {
  38. case OSSL_PARAM_INTEGER:
  39. case OSSL_PARAM_UNSIGNED_INTEGER:
  40. if (*ishex)
  41. BN_hex2bn(tmpbn, value);
  42. else
  43. BN_dec2bn(tmpbn, value);
  44. if (*tmpbn == NULL)
  45. return 0;
  46. /*
  47. * 2s complement negate, part 1
  48. *
  49. * BN_bn2nativepad puts the absolute value of the number in the
  50. * buffer, i.e. if it's negative, we need to deal with it. We do
  51. * it by subtracting 1 here and inverting the bytes in
  52. * construct_from_text() below.
  53. */
  54. if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
  55. && !BN_sub_word(*tmpbn, 1)) {
  56. return 0;
  57. }
  58. *buf_n = BN_num_bytes(*tmpbn);
  59. /*
  60. * TODO(v3.0) is this the right way to do this? This code expects
  61. * a zero data size to simply mean "arbitrary size".
  62. */
  63. if (p->data_size > 0) {
  64. if (*buf_n >= p->data_size) {
  65. CRYPTOerr(0, CRYPTO_R_TOO_SMALL_BUFFER);
  66. /* Since this is a different error, we don't break */
  67. return 0;
  68. }
  69. /* Change actual size to become the desired size. */
  70. *buf_n = p->data_size;
  71. }
  72. break;
  73. case OSSL_PARAM_UTF8_STRING:
  74. if (*ishex) {
  75. CRYPTOerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  76. return 0;
  77. }
  78. *buf_n = strlen(value) + 1;
  79. break;
  80. case OSSL_PARAM_OCTET_STRING:
  81. if (*ishex) {
  82. *buf_n = strlen(value) >> 1;
  83. } else {
  84. *buf_n = value_n;
  85. }
  86. break;
  87. }
  88. return 1;
  89. }
  90. static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
  91. const char *value, size_t value_n, int ishex,
  92. void *buf, size_t buf_n, BIGNUM *tmpbn)
  93. {
  94. if (buf == NULL)
  95. return 0;
  96. if (buf_n > 0) {
  97. switch (paramdef->data_type) {
  98. case OSSL_PARAM_INTEGER:
  99. case OSSL_PARAM_UNSIGNED_INTEGER:
  100. /*
  101. {
  102. if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
  103. BN_free(a);
  104. break;
  105. }
  106. */
  107. BN_bn2nativepad(tmpbn, buf, buf_n);
  108. /*
  109. * 2s complement negate, part two.
  110. *
  111. * Because we did the first part on the BIGNUM itself, we can just
  112. * invert all the bytes here and be done with it.
  113. */
  114. if (paramdef->data_type == OSSL_PARAM_INTEGER
  115. && BN_is_negative(tmpbn)) {
  116. unsigned char *cp;
  117. size_t i = buf_n;
  118. for (cp = buf; i-- > 0; cp++)
  119. *cp ^= 0xFF;
  120. }
  121. break;
  122. case OSSL_PARAM_UTF8_STRING:
  123. strncpy(buf, value, buf_n);
  124. break;
  125. case OSSL_PARAM_OCTET_STRING:
  126. if (ishex) {
  127. size_t l = 0;
  128. if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value))
  129. return 0;
  130. } else {
  131. memcpy(buf, value, buf_n);
  132. }
  133. break;
  134. }
  135. }
  136. *to = *paramdef;
  137. to->data = buf;
  138. to->data_size = buf_n;
  139. to->return_size = 0;
  140. return 1;
  141. }
  142. int OSSL_PARAM_construct_from_text(OSSL_PARAM *to,
  143. const OSSL_PARAM *paramdefs,
  144. const char *key, const char *value,
  145. size_t value_n,
  146. void *buf, size_t *buf_n)
  147. {
  148. const OSSL_PARAM *paramdef = NULL;
  149. int ishex = 0;
  150. BIGNUM *tmpbn = NULL;
  151. int ok = 0;
  152. if (to == NULL || paramdefs == NULL)
  153. return 0;
  154. if (!prepare_from_text(paramdefs, key, value, value_n,
  155. &paramdef, &ishex, buf_n, &tmpbn))
  156. return 0;
  157. /*
  158. * The user gets the expected buffer size back even if the buffer isn't
  159. * allocated.
  160. */
  161. if (buf == NULL)
  162. return 1;
  163. ok = construct_from_text(to, paramdef, value, value_n, ishex,
  164. buf, *buf_n, tmpbn);
  165. BN_free(tmpbn);
  166. return ok;
  167. }
  168. int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
  169. const OSSL_PARAM *paramdefs,
  170. const char *key, const char *value,
  171. size_t value_n)
  172. {
  173. const OSSL_PARAM *paramdef = NULL;
  174. int ishex = 0;
  175. void *buf = NULL;
  176. size_t buf_n = 0;
  177. BIGNUM *tmpbn = NULL;
  178. int ok = 0;
  179. if (to == NULL || paramdefs == NULL)
  180. return 0;
  181. if (!prepare_from_text(paramdefs, key, value, value_n,
  182. &paramdef, &ishex, &buf_n, &tmpbn))
  183. return 0;
  184. if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) {
  185. CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
  186. return 0;
  187. }
  188. ok = construct_from_text(to, paramdef, value, value_n, ishex,
  189. buf, buf_n, tmpbn);
  190. BN_free(tmpbn);
  191. if (!ok)
  192. OPENSSL_free(buf);
  193. return ok;
  194. }