You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

265 lines
7.7 KiB

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