params_from_text.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <string.h>
  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 = strncmp(key, "hex", 3) == 0;
  35. if (*ishex)
  36. key += 3;
  37. p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
  38. if (found != NULL)
  39. *found = p != NULL;
  40. if (p == NULL)
  41. return 0;
  42. switch (p->data_type) {
  43. case OSSL_PARAM_INTEGER:
  44. case OSSL_PARAM_UNSIGNED_INTEGER:
  45. if (*ishex)
  46. r = BN_hex2bn(tmpbn, value);
  47. else
  48. r = BN_asc2bn(tmpbn, value);
  49. if (r == 0 || *tmpbn == NULL)
  50. return 0;
  51. /*
  52. * 2s complement negate, part 1
  53. *
  54. * BN_bn2nativepad puts the absolute value of the number in the
  55. * buffer, i.e. if it's negative, we need to deal with it. We do
  56. * it by subtracting 1 here and inverting the bytes in
  57. * construct_from_text() below.
  58. * To subtract 1 from an absolute value of a negative number we
  59. * actually have to add 1: -3 - 1 = -4, |-3| = 3 + 1 = 4.
  60. */
  61. if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
  62. && !BN_add_word(*tmpbn, 1)) {
  63. return 0;
  64. }
  65. buf_bits = (size_t)BN_num_bits(*tmpbn);
  66. *buf_n = (buf_bits + 7) / 8;
  67. /*
  68. * A zero data size means "arbitrary size", so only do the
  69. * range checking if a size is specified.
  70. */
  71. if (p->data_size > 0) {
  72. if (buf_bits > p->data_size * 8
  73. || (p->data_type == OSSL_PARAM_INTEGER
  74. && buf_bits == p->data_size * 8)) {
  75. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
  76. /* Since this is a different error, we don't break */
  77. return 0;
  78. }
  79. /* Change actual size to become the desired size. */
  80. *buf_n = p->data_size;
  81. }
  82. break;
  83. case OSSL_PARAM_UTF8_STRING:
  84. if (*ishex) {
  85. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
  86. return 0;
  87. }
  88. *buf_n = strlen(value) + 1;
  89. break;
  90. case OSSL_PARAM_OCTET_STRING:
  91. if (*ishex) {
  92. *buf_n = strlen(value) >> 1;
  93. } else {
  94. *buf_n = value_n;
  95. }
  96. break;
  97. }
  98. return 1;
  99. }
  100. static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
  101. const char *value, size_t value_n, int ishex,
  102. void *buf, size_t buf_n, BIGNUM *tmpbn)
  103. {
  104. if (buf == NULL)
  105. return 0;
  106. if (buf_n > 0) {
  107. switch (paramdef->data_type) {
  108. case OSSL_PARAM_INTEGER:
  109. case OSSL_PARAM_UNSIGNED_INTEGER:
  110. /*
  111. {
  112. if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
  113. BN_free(a);
  114. break;
  115. }
  116. */
  117. BN_bn2nativepad(tmpbn, buf, buf_n);
  118. /*
  119. * 2s complement negate, part two.
  120. *
  121. * Because we did the first part on the BIGNUM itself, we can just
  122. * invert all the bytes here and be done with it.
  123. */
  124. if (paramdef->data_type == OSSL_PARAM_INTEGER
  125. && BN_is_negative(tmpbn)) {
  126. unsigned char *cp;
  127. size_t i = buf_n;
  128. for (cp = buf; i-- > 0; cp++)
  129. *cp ^= 0xFF;
  130. }
  131. break;
  132. case OSSL_PARAM_UTF8_STRING:
  133. #ifdef CHARSET_EBCDIC
  134. ebcdic2ascii(buf, value, buf_n);
  135. #else
  136. strncpy(buf, value, buf_n);
  137. #endif
  138. /* Don't count the terminating NUL byte as data */
  139. buf_n--;
  140. break;
  141. case OSSL_PARAM_OCTET_STRING:
  142. if (ishex) {
  143. size_t l = 0;
  144. if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':'))
  145. return 0;
  146. } else {
  147. memcpy(buf, value, buf_n);
  148. }
  149. break;
  150. }
  151. }
  152. *to = *paramdef;
  153. to->data = buf;
  154. to->data_size = buf_n;
  155. to->return_size = OSSL_PARAM_UNMODIFIED;
  156. return 1;
  157. }
  158. int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
  159. const OSSL_PARAM *paramdefs,
  160. const char *key, const char *value,
  161. size_t value_n, int *found)
  162. {
  163. const OSSL_PARAM *paramdef = NULL;
  164. int ishex = 0;
  165. void *buf = NULL;
  166. size_t buf_n = 0;
  167. BIGNUM *tmpbn = NULL;
  168. int ok = 0;
  169. if (to == NULL || paramdefs == NULL)
  170. return 0;
  171. if (!prepare_from_text(paramdefs, key, value, value_n,
  172. &paramdef, &ishex, &buf_n, &tmpbn, found))
  173. goto err;
  174. if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) {
  175. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  176. goto err;
  177. }
  178. ok = construct_from_text(to, paramdef, value, value_n, ishex,
  179. buf, buf_n, tmpbn);
  180. BN_free(tmpbn);
  181. if (!ok)
  182. OPENSSL_free(buf);
  183. return ok;
  184. err:
  185. BN_free(tmpbn);
  186. return 0;
  187. }