params_from_text.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /*
  29. * ishex is used to translate legacy style string controls in hex format
  30. * to octet string parameters.
  31. */
  32. *ishex = strncmp(key, "hex", 3) == 0;
  33. if (*ishex)
  34. key += 3;
  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. BN_hex2bn(tmpbn, value);
  45. else
  46. BN_dec2bn(tmpbn, value);
  47. if (*tmpbn == NULL)
  48. return 0;
  49. /*
  50. * 2s complement negate, part 1
  51. *
  52. * BN_bn2nativepad puts the absolute value of the number in the
  53. * buffer, i.e. if it's negative, we need to deal with it. We do
  54. * it by subtracting 1 here and inverting the bytes in
  55. * construct_from_text() below.
  56. */
  57. if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
  58. && !BN_sub_word(*tmpbn, 1)) {
  59. return 0;
  60. }
  61. *buf_n = BN_num_bytes(*tmpbn);
  62. /*
  63. * TODO(v3.0) is this the right way to do this? This code expects
  64. * a zero data size to simply mean "arbitrary size".
  65. */
  66. if (p->data_size > 0) {
  67. if (*buf_n >= p->data_size) {
  68. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
  69. /* Since this is a different error, we don't break */
  70. return 0;
  71. }
  72. /* Change actual size to become the desired size. */
  73. *buf_n = p->data_size;
  74. }
  75. break;
  76. case OSSL_PARAM_UTF8_STRING:
  77. if (*ishex) {
  78. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
  79. return 0;
  80. }
  81. *buf_n = strlen(value) + 1;
  82. break;
  83. case OSSL_PARAM_OCTET_STRING:
  84. if (*ishex) {
  85. *buf_n = strlen(value) >> 1;
  86. } else {
  87. *buf_n = value_n;
  88. }
  89. break;
  90. }
  91. return 1;
  92. }
  93. static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
  94. const char *value, size_t value_n, int ishex,
  95. void *buf, size_t buf_n, BIGNUM *tmpbn)
  96. {
  97. if (buf == NULL)
  98. return 0;
  99. if (buf_n > 0) {
  100. switch (paramdef->data_type) {
  101. case OSSL_PARAM_INTEGER:
  102. case OSSL_PARAM_UNSIGNED_INTEGER:
  103. /*
  104. {
  105. if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
  106. BN_free(a);
  107. break;
  108. }
  109. */
  110. BN_bn2nativepad(tmpbn, buf, buf_n);
  111. /*
  112. * 2s complement negate, part two.
  113. *
  114. * Because we did the first part on the BIGNUM itself, we can just
  115. * invert all the bytes here and be done with it.
  116. */
  117. if (paramdef->data_type == OSSL_PARAM_INTEGER
  118. && BN_is_negative(tmpbn)) {
  119. unsigned char *cp;
  120. size_t i = buf_n;
  121. for (cp = buf; i-- > 0; cp++)
  122. *cp ^= 0xFF;
  123. }
  124. break;
  125. case OSSL_PARAM_UTF8_STRING:
  126. #ifdef CHARSET_EBCDIC
  127. ebcdic2ascii(buf, value, buf_n);
  128. #else
  129. strncpy(buf, value, buf_n);
  130. #endif
  131. break;
  132. case OSSL_PARAM_OCTET_STRING:
  133. if (ishex) {
  134. size_t l = 0;
  135. if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':'))
  136. return 0;
  137. } else {
  138. memcpy(buf, value, buf_n);
  139. }
  140. break;
  141. }
  142. }
  143. *to = *paramdef;
  144. to->data = buf;
  145. to->data_size = buf_n;
  146. to->return_size = OSSL_PARAM_UNMODIFIED;
  147. return 1;
  148. }
  149. int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
  150. const OSSL_PARAM *paramdefs,
  151. const char *key, const char *value,
  152. size_t value_n, int *found)
  153. {
  154. const OSSL_PARAM *paramdef = NULL;
  155. int ishex = 0;
  156. void *buf = NULL;
  157. size_t buf_n = 0;
  158. BIGNUM *tmpbn = NULL;
  159. int ok = 0;
  160. if (to == NULL || paramdefs == NULL)
  161. return 0;
  162. if (!prepare_from_text(paramdefs, key, value, value_n,
  163. &paramdef, &ishex, &buf_n, &tmpbn, found))
  164. return 0;
  165. if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) {
  166. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  167. return 0;
  168. }
  169. ok = construct_from_text(to, paramdef, value, value_n, ishex,
  170. buf, buf_n, tmpbn);
  171. BN_free(tmpbn);
  172. if (!ok)
  173. OPENSSL_free(buf);
  174. return ok;
  175. }