2
0

params_from_text.c 5.7 KB

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