Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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