ber.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /* kMaxDepth is a just a sanity limit. The code should be such that the length
  19. * of the input being processes always decreases. None the less, a very large
  20. * input could otherwise cause the stack to overflow. */
  21. static const unsigned kMaxDepth = 2048;
  22. /* is_string_type returns one if |tag| is a string type and zero otherwise. It
  23. * ignores the constructed bit. */
  24. static int is_string_type(unsigned tag) {
  25. if ((tag & 0xc0) != 0) {
  26. return 0;
  27. }
  28. switch (tag & 0x1f) {
  29. case CBS_ASN1_BITSTRING:
  30. case CBS_ASN1_OCTETSTRING:
  31. case CBS_ASN1_NUMERICSTRING:
  32. case CBS_ASN1_PRINTABLESTRING:
  33. case CBS_ASN1_T16STRING:
  34. case CBS_ASN1_VIDEOTEXSTRING:
  35. case CBS_ASN1_IA5STRING:
  36. case CBS_ASN1_GRAPHICSTRING:
  37. case CBS_ASN1_VISIBLESTRING:
  38. case CBS_ASN1_GENERALSTRING:
  39. case CBS_ASN1_UNIVERSALSTRING:
  40. case CBS_ASN1_BMPSTRING:
  41. return 1;
  42. default:
  43. return 0;
  44. }
  45. }
  46. /* cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found|
  47. * depending on whether an indefinite length element or constructed string was
  48. * found. The value of |orig_in| is not changed. It returns one on success (i.e.
  49. * |*ber_found| was set) and zero on error. */
  50. static int cbs_find_ber(const CBS *orig_in, char *ber_found, unsigned depth) {
  51. CBS in;
  52. if (depth > kMaxDepth) {
  53. return 0;
  54. }
  55. CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
  56. *ber_found = 0;
  57. while (CBS_len(&in) > 0) {
  58. CBS contents;
  59. unsigned tag;
  60. size_t header_len;
  61. if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len)) {
  62. return 0;
  63. }
  64. if (CBS_len(&contents) == header_len &&
  65. header_len > 0 &&
  66. CBS_data(&contents)[header_len-1] == 0x80) {
  67. /* Found an indefinite-length element. */
  68. *ber_found = 1;
  69. return 1;
  70. }
  71. if (tag & CBS_ASN1_CONSTRUCTED) {
  72. if (is_string_type(tag)) {
  73. /* Constructed strings are only legal in BER and require conversion. */
  74. *ber_found = 1;
  75. return 1;
  76. }
  77. if (!CBS_skip(&contents, header_len) ||
  78. !cbs_find_ber(&contents, ber_found, depth + 1)) {
  79. return 0;
  80. }
  81. }
  82. }
  83. return 1;
  84. }
  85. /* is_eoc returns true if |header_len| and |contents|, as returned by
  86. * |CBS_get_any_ber_asn1_element|, indicate an "end of contents" (EOC) value. */
  87. static char is_eoc(size_t header_len, CBS *contents) {
  88. return header_len == 2 && CBS_len(contents) == 2 &&
  89. memcmp(CBS_data(contents), "\x00\x00", 2) == 0;
  90. }
  91. /* cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
  92. * |string_tag| is non-zero, then all elements must match |string_tag| up to the
  93. * constructed bit and primitive element bodies are written to |out| without
  94. * element headers. This is used when concatenating the fragments of a
  95. * constructed string. If |looking_for_eoc| is set then any EOC elements found
  96. * will cause the function to return after consuming it. It returns one on
  97. * success and zero on error. */
  98. static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
  99. char looking_for_eoc, unsigned depth) {
  100. assert(!(string_tag & CBS_ASN1_CONSTRUCTED));
  101. if (depth > kMaxDepth) {
  102. return 0;
  103. }
  104. while (CBS_len(in) > 0) {
  105. CBS contents;
  106. unsigned tag, child_string_tag = string_tag;
  107. size_t header_len;
  108. CBB *out_contents, out_contents_storage;
  109. if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len)) {
  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, uint8_t **out, size_t *out_len) {
  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. char conversion_needed;
  173. if (!cbs_find_ber(in, &conversion_needed, 0)) {
  174. return 0;
  175. }
  176. if (!conversion_needed) {
  177. *out = NULL;
  178. *out_len = 0;
  179. return 1;
  180. }
  181. if (!CBB_init(&cbb, CBS_len(in)) ||
  182. !cbs_convert_ber(in, &cbb, 0, 0, 0) ||
  183. !CBB_finish(&cbb, out, out_len)) {
  184. CBB_cleanup(&cbb);
  185. return 0;
  186. }
  187. return 1;
  188. }
  189. int CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage,
  190. unsigned outer_tag, unsigned inner_tag) {
  191. assert(!(outer_tag & CBS_ASN1_CONSTRUCTED));
  192. assert(!(inner_tag & CBS_ASN1_CONSTRUCTED));
  193. assert(is_string_type(inner_tag));
  194. if (CBS_peek_asn1_tag(in, outer_tag)) {
  195. /* Normal implicitly-tagged string. */
  196. *out_storage = NULL;
  197. return CBS_get_asn1(in, out, outer_tag);
  198. }
  199. /* Otherwise, try to parse an implicitly-tagged constructed string.
  200. * |CBS_asn1_ber_to_der| is assumed to have run, so only allow one level deep
  201. * of nesting. */
  202. CBB result;
  203. CBS child;
  204. if (!CBB_init(&result, CBS_len(in)) ||
  205. !CBS_get_asn1(in, &child, outer_tag | CBS_ASN1_CONSTRUCTED)) {
  206. goto err;
  207. }
  208. while (CBS_len(&child) > 0) {
  209. CBS chunk;
  210. if (!CBS_get_asn1(&child, &chunk, inner_tag) ||
  211. !CBB_add_bytes(&result, CBS_data(&chunk), CBS_len(&chunk))) {
  212. goto err;
  213. }
  214. }
  215. uint8_t *data;
  216. size_t len;
  217. if (!CBB_finish(&result, &data, &len)) {
  218. goto err;
  219. }
  220. CBS_init(out, data, len);
  221. *out_storage = data;
  222. return 1;
  223. err:
  224. CBB_cleanup(&result);
  225. return 0;
  226. }