params_from_text.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. *buf_n = strlen(value) >> 1;
  106. } else {
  107. *buf_n = value_n;
  108. }
  109. break;
  110. }
  111. return 1;
  112. }
  113. static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
  114. const char *value, size_t value_n, int ishex,
  115. void *buf, size_t buf_n, BIGNUM *tmpbn)
  116. {
  117. if (buf == NULL)
  118. return 0;
  119. if (buf_n > 0) {
  120. switch (paramdef->data_type) {
  121. case OSSL_PARAM_INTEGER:
  122. case OSSL_PARAM_UNSIGNED_INTEGER:
  123. /*
  124. {
  125. if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
  126. BN_free(a);
  127. break;
  128. }
  129. */
  130. BN_bn2nativepad(tmpbn, buf, buf_n);
  131. /*
  132. * 2's complement negation, part two.
  133. *
  134. * Because we did the first part on the BIGNUM itself, we can just
  135. * invert all the bytes here and be done with it.
  136. */
  137. if (paramdef->data_type == OSSL_PARAM_INTEGER
  138. && BN_is_negative(tmpbn)) {
  139. unsigned char *cp;
  140. size_t i = buf_n;
  141. for (cp = buf; i-- > 0; cp++)
  142. *cp ^= 0xFF;
  143. }
  144. break;
  145. case OSSL_PARAM_UTF8_STRING:
  146. #ifdef CHARSET_EBCDIC
  147. ebcdic2ascii(buf, value, buf_n);
  148. #else
  149. strncpy(buf, value, buf_n);
  150. #endif
  151. /* Don't count the terminating NUL byte as data */
  152. buf_n--;
  153. break;
  154. case OSSL_PARAM_OCTET_STRING:
  155. if (ishex) {
  156. size_t l = 0;
  157. if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':'))
  158. return 0;
  159. } else {
  160. memcpy(buf, value, buf_n);
  161. }
  162. break;
  163. }
  164. }
  165. *to = *paramdef;
  166. to->data = buf;
  167. to->data_size = buf_n;
  168. to->return_size = OSSL_PARAM_UNMODIFIED;
  169. return 1;
  170. }
  171. int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
  172. const OSSL_PARAM *paramdefs,
  173. const char *key, const char *value,
  174. size_t value_n, int *found)
  175. {
  176. const OSSL_PARAM *paramdef = NULL;
  177. int ishex = 0;
  178. void *buf = NULL;
  179. size_t buf_n = 0;
  180. BIGNUM *tmpbn = NULL;
  181. int ok = 0;
  182. if (to == NULL || paramdefs == NULL)
  183. return 0;
  184. if (!prepare_from_text(paramdefs, key, value, value_n,
  185. &paramdef, &ishex, &buf_n, &tmpbn, found))
  186. goto err;
  187. if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) {
  188. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  189. goto err;
  190. }
  191. ok = construct_from_text(to, paramdef, value, value_n, ishex,
  192. buf, buf_n, tmpbn);
  193. BN_free(tmpbn);
  194. if (!ok)
  195. OPENSSL_free(buf);
  196. return ok;
  197. err:
  198. BN_free(tmpbn);
  199. return 0;
  200. }