params_from_text.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2019-2021 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 "internal/common.h" /* for HAS_PREFIX */
  11. #include <openssl/ebcdic.h>
  12. #include <openssl/err.h>
  13. #include <openssl/params.h>
  14. /*
  15. * When processing text to params, we're trying to be smart with numbers.
  16. * Instead of handling each specific separate integer type, we use a bignum
  17. * and ensure that it isn't larger than the expected size, and we then make
  18. * sure it is the expected size... if there is one given.
  19. * (if the size can be arbitrary, then we give whatever we have)
  20. */
  21. static int prepare_from_text(const OSSL_PARAM *paramdefs, const char *key,
  22. const char *value, size_t value_n,
  23. /* Output parameters */
  24. const OSSL_PARAM **paramdef, int *ishex,
  25. size_t *buf_n, BIGNUM **tmpbn, int *found)
  26. {
  27. const OSSL_PARAM *p;
  28. size_t buf_bits;
  29. int r;
  30. /*
  31. * ishex is used to translate legacy style string controls in hex format
  32. * to octet string parameters.
  33. */
  34. *ishex = CHECK_AND_SKIP_PREFIX(key, "hex");
  35. p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
  36. if (found != NULL)
  37. *found = p != NULL;
  38. if (p == NULL)
  39. return 0;
  40. switch (p->data_type) {
  41. case OSSL_PARAM_INTEGER:
  42. case OSSL_PARAM_UNSIGNED_INTEGER:
  43. if (*ishex)
  44. r = BN_hex2bn(tmpbn, value);
  45. else
  46. r = BN_asc2bn(tmpbn, value);
  47. if (r == 0 || *tmpbn == NULL)
  48. return 0;
  49. if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
  50. && BN_is_negative(*tmpbn)) {
  51. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INVALID_NEGATIVE_VALUE);
  52. return 0;
  53. }
  54. /*
  55. * 2's complement negate, part 1
  56. *
  57. * BN_bn2nativepad puts the absolute value of the number in the
  58. * buffer, i.e. if it's negative, we need to deal with it. We do
  59. * it by subtracting 1 here and inverting the bytes in
  60. * construct_from_text() below.
  61. * To subtract 1 from an absolute value of a negative number we
  62. * actually have to add 1: -3 - 1 = -4, |-3| = 3 + 1 = 4.
  63. */
  64. if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
  65. && !BN_add_word(*tmpbn, 1)) {
  66. return 0;
  67. }
  68. buf_bits = (size_t)BN_num_bits(*tmpbn);
  69. /*
  70. * Compensate for cases where the most significant bit in
  71. * the resulting OSSL_PARAM buffer will be set after the
  72. * BN_bn2nativepad() call, as the implied sign may not be
  73. * correct after the second part of the 2's complement
  74. * negation has been performed.
  75. * We fix these cases by extending the buffer by one byte
  76. * (8 bits), which will give some padding. The second part
  77. * of the 2's complement negation will do the rest.
  78. */
  79. if (p->data_type == OSSL_PARAM_INTEGER && buf_bits % 8 == 0)
  80. buf_bits += 8;
  81. *buf_n = (buf_bits + 7) / 8;
  82. /*
  83. * A zero data size means "arbitrary size", so only do the
  84. * range checking if a size is specified.
  85. */
  86. if (p->data_size > 0) {
  87. if (buf_bits > p->data_size * 8) {
  88. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
  89. /* Since this is a different error, we don't break */
  90. return 0;
  91. }
  92. /* Change actual size to become the desired size. */
  93. *buf_n = p->data_size;
  94. }
  95. break;
  96. case OSSL_PARAM_UTF8_STRING:
  97. if (*ishex) {
  98. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
  99. return 0;
  100. }
  101. *buf_n = strlen(value) + 1;
  102. break;
  103. case OSSL_PARAM_OCTET_STRING:
  104. if (*ishex) {
  105. size_t hexdigits = strlen(value);
  106. if ((hexdigits % 2) != 0) {
  107. /* We don't accept an odd number of hex digits */
  108. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
  109. return 0;
  110. }
  111. *buf_n = hexdigits >> 1;
  112. } else {
  113. *buf_n = value_n;
  114. }
  115. break;
  116. }
  117. return 1;
  118. }
  119. static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
  120. const char *value, size_t value_n, int ishex,
  121. void *buf, size_t buf_n, BIGNUM *tmpbn)
  122. {
  123. if (buf == NULL)
  124. return 0;
  125. if (buf_n > 0) {
  126. switch (paramdef->data_type) {
  127. case OSSL_PARAM_INTEGER:
  128. case OSSL_PARAM_UNSIGNED_INTEGER:
  129. /*
  130. {
  131. if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
  132. BN_free(a);
  133. break;
  134. }
  135. */
  136. BN_bn2nativepad(tmpbn, buf, buf_n);
  137. /*
  138. * 2's complement negation, part two.
  139. *
  140. * Because we did the first part on the BIGNUM itself, we can just
  141. * invert all the bytes here and be done with it.
  142. */
  143. if (paramdef->data_type == OSSL_PARAM_INTEGER
  144. && BN_is_negative(tmpbn)) {
  145. unsigned char *cp;
  146. size_t i = buf_n;
  147. for (cp = buf; i-- > 0; cp++)
  148. *cp ^= 0xFF;
  149. }
  150. break;
  151. case OSSL_PARAM_UTF8_STRING:
  152. #ifdef CHARSET_EBCDIC
  153. ebcdic2ascii(buf, value, buf_n);
  154. #else
  155. strncpy(buf, value, buf_n);
  156. #endif
  157. /* Don't count the terminating NUL byte as data */
  158. buf_n--;
  159. break;
  160. case OSSL_PARAM_OCTET_STRING:
  161. if (ishex) {
  162. size_t l = 0;
  163. if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':'))
  164. return 0;
  165. } else {
  166. memcpy(buf, value, buf_n);
  167. }
  168. break;
  169. }
  170. }
  171. *to = *paramdef;
  172. to->data = buf;
  173. to->data_size = buf_n;
  174. to->return_size = OSSL_PARAM_UNMODIFIED;
  175. return 1;
  176. }
  177. int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
  178. const OSSL_PARAM *paramdefs,
  179. const char *key, const char *value,
  180. size_t value_n, int *found)
  181. {
  182. const OSSL_PARAM *paramdef = NULL;
  183. int ishex = 0;
  184. void *buf = NULL;
  185. size_t buf_n = 0;
  186. BIGNUM *tmpbn = NULL;
  187. int ok = 0;
  188. if (to == NULL || paramdefs == NULL)
  189. return 0;
  190. if (!prepare_from_text(paramdefs, key, value, value_n,
  191. &paramdef, &ishex, &buf_n, &tmpbn, found))
  192. goto err;
  193. if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL)
  194. goto err;
  195. ok = construct_from_text(to, paramdef, value, value_n, ishex,
  196. buf, buf_n, tmpbn);
  197. BN_free(tmpbn);
  198. if (!ok)
  199. OPENSSL_free(buf);
  200. return ok;
  201. err:
  202. BN_free(tmpbn);
  203. return 0;
  204. }