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