25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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