ber.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/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. switch (tag & ~CBS_ASN1_CONSTRUCTED) {
  27. case CBS_ASN1_BITSTRING:
  28. case CBS_ASN1_OCTETSTRING:
  29. case CBS_ASN1_UTF8STRING:
  30. case CBS_ASN1_NUMERICSTRING:
  31. case CBS_ASN1_PRINTABLESTRING:
  32. case CBS_ASN1_T61STRING:
  33. case CBS_ASN1_VIDEOTEXSTRING:
  34. case CBS_ASN1_IA5STRING:
  35. case CBS_ASN1_GRAPHICSTRING:
  36. case CBS_ASN1_VISIBLESTRING:
  37. case CBS_ASN1_GENERALSTRING:
  38. case CBS_ASN1_UNIVERSALSTRING:
  39. case CBS_ASN1_BMPSTRING:
  40. return 1;
  41. default:
  42. return 0;
  43. }
  44. }
  45. // cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found|
  46. // depending on whether an indefinite length element or constructed string was
  47. // found. The value of |orig_in| is not changed. It returns one on success (i.e.
  48. // |*ber_found| was set) and zero on error.
  49. static int cbs_find_ber(const CBS *orig_in, char *ber_found, unsigned depth) {
  50. CBS in;
  51. if (depth > kMaxDepth) {
  52. return 0;
  53. }
  54. CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
  55. *ber_found = 0;
  56. while (CBS_len(&in) > 0) {
  57. CBS contents;
  58. unsigned tag;
  59. size_t header_len;
  60. if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len)) {
  61. return 0;
  62. }
  63. if (CBS_len(&contents) == header_len &&
  64. header_len > 0 &&
  65. CBS_data(&contents)[header_len-1] == 0x80) {
  66. // Found an indefinite-length element.
  67. *ber_found = 1;
  68. return 1;
  69. }
  70. if (tag & CBS_ASN1_CONSTRUCTED) {
  71. if (is_string_type(tag)) {
  72. // Constructed strings are only legal in BER and require conversion.
  73. *ber_found = 1;
  74. return 1;
  75. }
  76. if (!CBS_skip(&contents, header_len) ||
  77. !cbs_find_ber(&contents, ber_found, depth + 1)) {
  78. return 0;
  79. }
  80. }
  81. }
  82. return 1;
  83. }
  84. // is_eoc returns true if |header_len| and |contents|, as returned by
  85. // |CBS_get_any_ber_asn1_element|, indicate an "end of contents" (EOC) value.
  86. static char is_eoc(size_t header_len, CBS *contents) {
  87. return header_len == 2 && CBS_len(contents) == 2 &&
  88. OPENSSL_memcmp(CBS_data(contents), "\x00\x00", 2) == 0;
  89. }
  90. // cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
  91. // |string_tag| is non-zero, then all elements must match |string_tag| up to the
  92. // constructed bit and primitive element bodies are written to |out| without
  93. // element headers. This is used when concatenating the fragments of a
  94. // constructed string. If |looking_for_eoc| is set then any EOC elements found
  95. // will cause the function to return after consuming it. It returns one on
  96. // success and zero on error.
  97. static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
  98. char looking_for_eoc, unsigned depth) {
  99. assert(!(string_tag & CBS_ASN1_CONSTRUCTED));
  100. if (depth > kMaxDepth) {
  101. return 0;
  102. }
  103. while (CBS_len(in) > 0) {
  104. CBS contents;
  105. unsigned tag, child_string_tag = string_tag;
  106. size_t header_len;
  107. CBB *out_contents, out_contents_storage;
  108. if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len)) {
  109. return 0;
  110. }
  111. if (is_eoc(header_len, &contents)) {
  112. return looking_for_eoc;
  113. }
  114. if (string_tag != 0) {
  115. // This is part of a constructed string. All elements must match
  116. // |string_tag| up to the constructed bit and get appended to |out|
  117. // without a child element.
  118. if ((tag & ~CBS_ASN1_CONSTRUCTED) != string_tag) {
  119. return 0;
  120. }
  121. out_contents = out;
  122. } else {
  123. unsigned out_tag = tag;
  124. if ((tag & CBS_ASN1_CONSTRUCTED) && is_string_type(tag)) {
  125. // If a constructed string, clear the constructed bit and inform
  126. // children to concatenate bodies.
  127. out_tag &= ~CBS_ASN1_CONSTRUCTED;
  128. child_string_tag = out_tag;
  129. }
  130. if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) {
  131. return 0;
  132. }
  133. out_contents = &out_contents_storage;
  134. }
  135. if (CBS_len(&contents) == header_len && header_len > 0 &&
  136. CBS_data(&contents)[header_len - 1] == 0x80) {
  137. // This is an indefinite length element.
  138. if (!cbs_convert_ber(in, out_contents, child_string_tag,
  139. 1 /* looking for eoc */, depth + 1) ||
  140. !CBB_flush(out)) {
  141. return 0;
  142. }
  143. continue;
  144. }
  145. if (!CBS_skip(&contents, header_len)) {
  146. return 0;
  147. }
  148. if (tag & CBS_ASN1_CONSTRUCTED) {
  149. // Recurse into children.
  150. if (!cbs_convert_ber(&contents, out_contents, child_string_tag,
  151. 0 /* not looking for eoc */, depth + 1)) {
  152. return 0;
  153. }
  154. } else {
  155. // Copy primitive contents as-is.
  156. if (!CBB_add_bytes(out_contents, CBS_data(&contents),
  157. CBS_len(&contents))) {
  158. return 0;
  159. }
  160. }
  161. if (!CBB_flush(out)) {
  162. return 0;
  163. }
  164. }
  165. return looking_for_eoc == 0;
  166. }
  167. int CBS_asn1_ber_to_der(CBS *in, uint8_t **out, size_t *out_len) {
  168. CBB cbb;
  169. // First, do a quick walk to find any indefinite-length elements. Most of the
  170. // time we hope that there aren't any and thus we can quickly return.
  171. char conversion_needed;
  172. if (!cbs_find_ber(in, &conversion_needed, 0)) {
  173. return 0;
  174. }
  175. if (!conversion_needed) {
  176. *out = NULL;
  177. *out_len = 0;
  178. return 1;
  179. }
  180. if (!CBB_init(&cbb, CBS_len(in)) ||
  181. !cbs_convert_ber(in, &cbb, 0, 0, 0) ||
  182. !CBB_finish(&cbb, out, out_len)) {
  183. CBB_cleanup(&cbb);
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. int CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage,
  189. unsigned outer_tag, unsigned inner_tag) {
  190. assert(!(outer_tag & CBS_ASN1_CONSTRUCTED));
  191. assert(!(inner_tag & CBS_ASN1_CONSTRUCTED));
  192. assert(is_string_type(inner_tag));
  193. if (CBS_peek_asn1_tag(in, outer_tag)) {
  194. // Normal implicitly-tagged string.
  195. *out_storage = NULL;
  196. return CBS_get_asn1(in, out, outer_tag);
  197. }
  198. // Otherwise, try to parse an implicitly-tagged constructed string.
  199. // |CBS_asn1_ber_to_der| is assumed to have run, so only allow one level deep
  200. // of nesting.
  201. CBB result;
  202. CBS child;
  203. if (!CBB_init(&result, CBS_len(in)) ||
  204. !CBS_get_asn1(in, &child, outer_tag | CBS_ASN1_CONSTRUCTED)) {
  205. goto err;
  206. }
  207. while (CBS_len(&child) > 0) {
  208. CBS chunk;
  209. if (!CBS_get_asn1(&child, &chunk, inner_tag) ||
  210. !CBB_add_bytes(&result, CBS_data(&chunk), CBS_len(&chunk))) {
  211. goto err;
  212. }
  213. }
  214. uint8_t *data;
  215. size_t len;
  216. if (!CBB_finish(&result, &data, &len)) {
  217. goto err;
  218. }
  219. CBS_init(out, data, len);
  220. *out_storage = data;
  221. return 1;
  222. err:
  223. CBB_cleanup(&result);
  224. return 0;
  225. }