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.
 
 
 
 
 
 

218 lines
7.0 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 "internal.h"
  16. /* kMaxDepth is a just a sanity limit. The code should be such that the length
  17. * of the input being processes always decreases. None the less, a very large
  18. * input could otherwise cause the stack to overflow. */
  19. static const unsigned kMaxDepth = 2048;
  20. /* cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found|
  21. * depending on whether an indefinite length element was found. The value of
  22. * |in| is not changed. It returns one on success (i.e. |*ber_found| was set)
  23. * and zero on error. */
  24. static int cbs_find_ber(CBS *orig_in, char *ber_found, unsigned depth) {
  25. CBS in;
  26. if (depth > kMaxDepth) {
  27. return 0;
  28. }
  29. CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
  30. *ber_found = 0;
  31. while (CBS_len(&in) > 0) {
  32. CBS contents;
  33. unsigned tag;
  34. size_t header_len;
  35. if (!CBS_get_any_asn1_element(&in, &contents, &tag, &header_len)) {
  36. return 0;
  37. }
  38. if (CBS_len(&contents) == header_len &&
  39. header_len > 0 &&
  40. CBS_data(&contents)[header_len-1] == 0x80) {
  41. *ber_found = 1;
  42. return 1;
  43. }
  44. if (tag & CBS_ASN1_CONSTRUCTED) {
  45. if (!CBS_skip(&contents, header_len) ||
  46. !cbs_find_ber(&contents, ber_found, depth + 1)) {
  47. return 0;
  48. }
  49. }
  50. }
  51. return 1;
  52. }
  53. /* is_primitive_type returns true if |tag| likely a primitive type. Normally
  54. * one can just test the "constructed" bit in the tag but, in BER, even
  55. * primitive tags can have the constructed bit if they have indefinite
  56. * length. */
  57. static char is_primitive_type(unsigned tag) {
  58. return (tag & 0xc0) == 0 &&
  59. (tag & 0x1f) != (CBS_ASN1_SEQUENCE & 0x1f) &&
  60. (tag & 0x1f) != (CBS_ASN1_SET & 0x1f);
  61. }
  62. /* is_eoc returns true if |header_len| and |contents|, as returned by
  63. * |CBS_get_any_asn1_element|, indicate an "end of contents" (EOC) value. */
  64. static char is_eoc(size_t header_len, CBS *contents) {
  65. return header_len == 2 && CBS_len(contents) == 2 &&
  66. memcmp(CBS_data(contents), "\x00\x00", 2) == 0;
  67. }
  68. /* cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
  69. * |squash_header| is set then the top-level of elements from |in| will not
  70. * have their headers written. This is used when concatenating the fragments of
  71. * an indefinite length, primitive value. If |looking_for_eoc| is set then any
  72. * EOC elements found will cause the function to return after consuming it.
  73. * It returns one on success and zero on error. */
  74. static int cbs_convert_ber(CBS *in, CBB *out, char squash_header,
  75. char looking_for_eoc, unsigned depth) {
  76. if (depth > kMaxDepth) {
  77. return 0;
  78. }
  79. while (CBS_len(in) > 0) {
  80. CBS contents;
  81. unsigned tag;
  82. size_t header_len;
  83. CBB *out_contents, out_contents_storage;
  84. if (!CBS_get_any_asn1_element(in, &contents, &tag, &header_len)) {
  85. return 0;
  86. }
  87. out_contents = out;
  88. if (CBS_len(&contents) == header_len) {
  89. if (is_eoc(header_len, &contents)) {
  90. return looking_for_eoc;
  91. }
  92. if (header_len > 0 && CBS_data(&contents)[header_len - 1] == 0x80) {
  93. /* This is an indefinite length element. If it's a SEQUENCE or SET then
  94. * we just need to write the out the contents as normal, but with a
  95. * concrete length prefix.
  96. *
  97. * If it's a something else then the contents will be a series of BER
  98. * elements of the same type which need to be concatenated. */
  99. const char context_specific = (tag & 0xc0) == 0x80;
  100. char squash_child_headers = is_primitive_type(tag);
  101. /* This is a hack, but it sufficies to handle NSS's output. If we find
  102. * an indefinite length, context-specific tag with a definite, primtive
  103. * tag inside it, then we assume that the context-specific tag is
  104. * implicit and the tags within are fragments of a primitive type that
  105. * need to be concatenated. */
  106. if (context_specific && (tag & CBS_ASN1_CONSTRUCTED)) {
  107. CBS in_copy, inner_contents;
  108. unsigned inner_tag;
  109. size_t inner_header_len;
  110. CBS_init(&in_copy, CBS_data(in), CBS_len(in));
  111. if (!CBS_get_any_asn1_element(&in_copy, &inner_contents, &inner_tag,
  112. &inner_header_len)) {
  113. return 0;
  114. }
  115. if (CBS_len(&inner_contents) > inner_header_len &&
  116. is_primitive_type(inner_tag)) {
  117. squash_child_headers = 1;
  118. }
  119. }
  120. if (!squash_header) {
  121. unsigned out_tag = tag;
  122. if (squash_child_headers) {
  123. out_tag &= ~CBS_ASN1_CONSTRUCTED;
  124. }
  125. if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) {
  126. return 0;
  127. }
  128. out_contents = &out_contents_storage;
  129. }
  130. if (!cbs_convert_ber(in, out_contents,
  131. squash_child_headers,
  132. 1 /* looking for eoc */, depth + 1)) {
  133. return 0;
  134. }
  135. if (out_contents != out && !CBB_flush(out)) {
  136. return 0;
  137. }
  138. continue;
  139. }
  140. }
  141. if (!squash_header) {
  142. if (!CBB_add_asn1(out, &out_contents_storage, tag)) {
  143. return 0;
  144. }
  145. out_contents = &out_contents_storage;
  146. }
  147. if (!CBS_skip(&contents, header_len)) {
  148. return 0;
  149. }
  150. if (tag & CBS_ASN1_CONSTRUCTED) {
  151. if (!cbs_convert_ber(&contents, out_contents, 0 /* don't squash header */,
  152. 0 /* not looking for eoc */, depth + 1)) {
  153. return 0;
  154. }
  155. } else {
  156. if (!CBB_add_bytes(out_contents, CBS_data(&contents),
  157. CBS_len(&contents))) {
  158. return 0;
  159. }
  160. }
  161. if (out_contents != out && !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. CBB_init(&cbb, CBS_len(in));
  181. if (!cbs_convert_ber(in, &cbb, 0, 0, 0)) {
  182. CBB_cleanup(&cbb);
  183. return 0;
  184. }
  185. return CBB_finish(&cbb, out, out_len);
  186. }