ber.c.grpc_back 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl_grpc/bytestring.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include "internal.h"
  18. #include "../internal.h"
  19. // kMaxDepth is a just a sanity limit. The code should be such that the length
  20. // of the input being processes always decreases. None the less, a very large
  21. // input could otherwise cause the stack to overflow.
  22. static const unsigned kMaxDepth = 2048;
  23. // is_string_type returns one if |tag| is a string type and zero otherwise. It
  24. // ignores the constructed bit.
  25. static int is_string_type(unsigned tag) {
  26. // While BER supports constructed BIT STRINGS, OpenSSL misparses them. To
  27. // avoid acting on an ambiguous input, we do not support constructed BIT
  28. // STRINGS. See https://github.com/openssl/openssl/issues/12810.
  29. switch (tag & ~CBS_ASN1_CONSTRUCTED) {
  30. case CBS_ASN1_OCTETSTRING:
  31. case CBS_ASN1_UTF8STRING:
  32. case CBS_ASN1_NUMERICSTRING:
  33. case CBS_ASN1_PRINTABLESTRING:
  34. case CBS_ASN1_T61STRING:
  35. case CBS_ASN1_VIDEOTEXSTRING:
  36. case CBS_ASN1_IA5STRING:
  37. case CBS_ASN1_GRAPHICSTRING:
  38. case CBS_ASN1_VISIBLESTRING:
  39. case CBS_ASN1_GENERALSTRING:
  40. case CBS_ASN1_UNIVERSALSTRING:
  41. case CBS_ASN1_BMPSTRING:
  42. return 1;
  43. default:
  44. return 0;
  45. }
  46. }
  47. // cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found|
  48. // depending on whether an indefinite length element or constructed string was
  49. // found. The value of |orig_in| is not changed. It returns one on success (i.e.
  50. // |*ber_found| was set) and zero on error.
  51. static int cbs_find_ber(const CBS *orig_in, int *ber_found, unsigned depth) {
  52. CBS in;
  53. if (depth > kMaxDepth) {
  54. return 0;
  55. }
  56. CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
  57. *ber_found = 0;
  58. while (CBS_len(&in) > 0) {
  59. CBS contents;
  60. unsigned tag;
  61. size_t header_len;
  62. if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len,
  63. ber_found)) {
  64. return 0;
  65. }
  66. if (*ber_found) {
  67. return 1;
  68. }
  69. if (tag & CBS_ASN1_CONSTRUCTED) {
  70. if (is_string_type(tag)) {
  71. // Constructed strings are only legal in BER and require conversion.
  72. *ber_found = 1;
  73. return 1;
  74. }
  75. if (!CBS_skip(&contents, header_len) ||
  76. !cbs_find_ber(&contents, ber_found, depth + 1)) {
  77. return 0;
  78. }
  79. }
  80. }
  81. return 1;
  82. }
  83. // is_eoc returns true if |header_len| and |contents|, as returned by
  84. // |CBS_get_any_ber_asn1_element|, indicate an "end of contents" (EOC) value.
  85. static char is_eoc(size_t header_len, CBS *contents) {
  86. return header_len == 2 && CBS_len(contents) == 2 &&
  87. OPENSSL_memcmp(CBS_data(contents), "\x00\x00", 2) == 0;
  88. }
  89. // cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
  90. // |string_tag| is non-zero, then all elements must match |string_tag| up to the
  91. // constructed bit and primitive element bodies are written to |out| without
  92. // element headers. This is used when concatenating the fragments of a
  93. // constructed string. If |looking_for_eoc| is set then any EOC elements found
  94. // will cause the function to return after consuming it. It returns one on
  95. // success and zero on error.
  96. static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
  97. char looking_for_eoc, unsigned depth) {
  98. assert(!(string_tag & CBS_ASN1_CONSTRUCTED));
  99. if (depth > kMaxDepth) {
  100. return 0;
  101. }
  102. while (CBS_len(in) > 0) {
  103. CBS contents;
  104. unsigned tag, child_string_tag = string_tag;
  105. size_t header_len;
  106. int ber_found;
  107. CBB *out_contents, out_contents_storage;
  108. if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len,
  109. &ber_found)) {
  110. return 0;
  111. }
  112. if (is_eoc(header_len, &contents)) {
  113. return looking_for_eoc;
  114. }
  115. if (string_tag != 0) {
  116. // This is part of a constructed string. All elements must match
  117. // |string_tag| up to the constructed bit and get appended to |out|
  118. // without a child element.
  119. if ((tag & ~CBS_ASN1_CONSTRUCTED) != string_tag) {
  120. return 0;
  121. }
  122. out_contents = out;
  123. } else {
  124. unsigned out_tag = tag;
  125. if ((tag & CBS_ASN1_CONSTRUCTED) && is_string_type(tag)) {
  126. // If a constructed string, clear the constructed bit and inform
  127. // children to concatenate bodies.
  128. out_tag &= ~CBS_ASN1_CONSTRUCTED;
  129. child_string_tag = out_tag;
  130. }
  131. if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) {
  132. return 0;
  133. }
  134. out_contents = &out_contents_storage;
  135. }
  136. if (CBS_len(&contents) == header_len && header_len > 0 &&
  137. CBS_data(&contents)[header_len - 1] == 0x80) {
  138. // This is an indefinite length element.
  139. if (!cbs_convert_ber(in, out_contents, child_string_tag,
  140. 1 /* looking for eoc */, depth + 1) ||
  141. !CBB_flush(out)) {
  142. return 0;
  143. }
  144. continue;
  145. }
  146. if (!CBS_skip(&contents, header_len)) {
  147. return 0;
  148. }
  149. if (tag & CBS_ASN1_CONSTRUCTED) {
  150. // Recurse into children.
  151. if (!cbs_convert_ber(&contents, out_contents, child_string_tag,
  152. 0 /* not looking for eoc */, depth + 1)) {
  153. return 0;
  154. }
  155. } else {
  156. // Copy primitive contents as-is.
  157. if (!CBB_add_bytes(out_contents, CBS_data(&contents),
  158. CBS_len(&contents))) {
  159. return 0;
  160. }
  161. }
  162. if (!CBB_flush(out)) {
  163. return 0;
  164. }
  165. }
  166. return looking_for_eoc == 0;
  167. }
  168. int CBS_asn1_ber_to_der(CBS *in, CBS *out, uint8_t **out_storage) {
  169. CBB cbb;
  170. // First, do a quick walk to find any indefinite-length elements. Most of the
  171. // time we hope that there aren't any and thus we can quickly return.
  172. int conversion_needed;
  173. if (!cbs_find_ber(in, &conversion_needed, 0)) {
  174. return 0;
  175. }
  176. if (!conversion_needed) {
  177. if (!CBS_get_any_asn1_element(in, out, NULL, NULL)) {
  178. return 0;
  179. }
  180. *out_storage = NULL;
  181. return 1;
  182. }
  183. size_t len;
  184. if (!CBB_init(&cbb, CBS_len(in)) ||
  185. !cbs_convert_ber(in, &cbb, 0, 0, 0) ||
  186. !CBB_finish(&cbb, out_storage, &len)) {
  187. CBB_cleanup(&cbb);
  188. return 0;
  189. }
  190. CBS_init(out, *out_storage, len);
  191. return 1;
  192. }
  193. int CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage,
  194. unsigned outer_tag, unsigned inner_tag) {
  195. assert(!(outer_tag & CBS_ASN1_CONSTRUCTED));
  196. assert(!(inner_tag & CBS_ASN1_CONSTRUCTED));
  197. assert(is_string_type(inner_tag));
  198. if (CBS_peek_asn1_tag(in, outer_tag)) {
  199. // Normal implicitly-tagged string.
  200. *out_storage = NULL;
  201. return CBS_get_asn1(in, out, outer_tag);
  202. }
  203. // Otherwise, try to parse an implicitly-tagged constructed string.
  204. // |CBS_asn1_ber_to_der| is assumed to have run, so only allow one level deep
  205. // of nesting.
  206. CBB result;
  207. CBS child;
  208. if (!CBB_init(&result, CBS_len(in)) ||
  209. !CBS_get_asn1(in, &child, outer_tag | CBS_ASN1_CONSTRUCTED)) {
  210. goto err;
  211. }
  212. while (CBS_len(&child) > 0) {
  213. CBS chunk;
  214. if (!CBS_get_asn1(&child, &chunk, inner_tag) ||
  215. !CBB_add_bytes(&result, CBS_data(&chunk), CBS_len(&chunk))) {
  216. goto err;
  217. }
  218. }
  219. uint8_t *data;
  220. size_t len;
  221. if (!CBB_finish(&result, &data, &len)) {
  222. goto err;
  223. }
  224. CBS_init(out, data, len);
  225. *out_storage = data;
  226. return 1;
  227. err:
  228. CBB_cleanup(&result);
  229. return 0;
  230. }