Coverage Report

Created: 2023-06-07 07:11

/src/boringssl/crypto/bytestring/ber.c
Line
Count
Source (jump to first uncovered line)
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 <assert.h>
18
#include <string.h>
19
20
#include "internal.h"
21
#include "../internal.h"
22
23
24
// kMaxDepth is a just a sanity limit. The code should be such that the length
25
// of the input being processes always decreases. None the less, a very large
26
// input could otherwise cause the stack to overflow.
27
static const uint32_t kMaxDepth = 2048;
28
29
// is_string_type returns one if |tag| is a string type and zero otherwise. It
30
// ignores the constructed bit.
31
218k
static int is_string_type(CBS_ASN1_TAG tag) {
32
  // While BER supports constructed BIT STRINGS, OpenSSL misparses them. To
33
  // avoid acting on an ambiguous input, we do not support constructed BIT
34
  // STRINGS. See https://github.com/openssl/openssl/issues/12810.
35
218k
  switch (tag & ~CBS_ASN1_CONSTRUCTED) {
36
1.61k
    case CBS_ASN1_OCTETSTRING:
37
2.43k
    case CBS_ASN1_UTF8STRING:
38
2.89k
    case CBS_ASN1_NUMERICSTRING:
39
3.32k
    case CBS_ASN1_PRINTABLESTRING:
40
3.66k
    case CBS_ASN1_T61STRING:
41
4.54k
    case CBS_ASN1_VIDEOTEXSTRING:
42
5.40k
    case CBS_ASN1_IA5STRING:
43
5.45k
    case CBS_ASN1_GRAPHICSTRING:
44
5.52k
    case CBS_ASN1_VISIBLESTRING:
45
6.18k
    case CBS_ASN1_GENERALSTRING:
46
6.43k
    case CBS_ASN1_UNIVERSALSTRING:
47
6.76k
    case CBS_ASN1_BMPSTRING:
48
6.76k
      return 1;
49
211k
    default:
50
211k
      return 0;
51
218k
  }
52
218k
}
53
54
// cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found|
55
// depending on whether an indefinite length element or constructed string was
56
// found. The value of |orig_in| is not changed. It returns one on success (i.e.
57
// |*ber_found| was set) and zero on error.
58
53.6k
static int cbs_find_ber(const CBS *orig_in, int *ber_found, uint32_t depth) {
59
53.6k
  CBS in;
60
61
53.6k
  if (depth > kMaxDepth) {
62
0
    return 0;
63
0
  }
64
65
53.6k
  CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
66
53.6k
  *ber_found = 0;
67
68
136k
  while (CBS_len(&in) > 0) {
69
93.2k
    CBS contents;
70
93.2k
    CBS_ASN1_TAG tag;
71
93.2k
    size_t header_len;
72
93.2k
    int indefinite;
73
93.2k
    if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len,
74
93.2k
                                      ber_found, &indefinite)) {
75
714
      return 0;
76
714
    }
77
92.5k
    if (*ber_found) {
78
5.56k
      return 1;
79
5.56k
    }
80
86.9k
    if (tag & CBS_ASN1_CONSTRUCTED) {
81
47.0k
      if (is_string_type(tag)) {
82
        // Constructed strings are only legal in BER and require conversion.
83
3.47k
        *ber_found = 1;
84
3.47k
        return 1;
85
3.47k
      }
86
43.5k
      if (!CBS_skip(&contents, header_len) ||
87
43.5k
          !cbs_find_ber(&contents, ber_found, depth + 1)) {
88
215
        return 0;
89
215
      }
90
43.5k
    }
91
86.9k
  }
92
93
43.6k
  return 1;
94
53.6k
}
95
96
// cbs_get_eoc returns one if |cbs| begins with an "end of contents" (EOC) value
97
// and zero otherwise. If an EOC was found, it advances |cbs| past it.
98
3.84M
static int cbs_get_eoc(CBS *cbs) {
99
3.84M
  if (CBS_len(cbs) >= 2 &&
100
3.84M
      CBS_data(cbs)[0] == 0 && CBS_data(cbs)[1] == 0) {
101
87.1k
    return CBS_skip(cbs, 2);
102
87.1k
  }
103
3.76M
  return 0;
104
3.84M
}
105
106
// cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
107
// |string_tag| is non-zero, then all elements must match |string_tag| up to the
108
// constructed bit and primitive element bodies are written to |out| without
109
// element headers. This is used when concatenating the fragments of a
110
// constructed string. If |looking_for_eoc| is set then any EOC elements found
111
// will cause the function to return after consuming it. It returns one on
112
// success and zero on error.
113
static int cbs_convert_ber(CBS *in, CBB *out, CBS_ASN1_TAG string_tag,
114
200k
                           int looking_for_eoc, uint32_t depth) {
115
200k
  assert(!(string_tag & CBS_ASN1_CONSTRUCTED));
116
117
200k
  if (depth > kMaxDepth) {
118
1
    return 0;
119
1
  }
120
121
3.98M
  while (CBS_len(in) > 0) {
122
3.93M
    if (looking_for_eoc && cbs_get_eoc(in)) {
123
87.1k
      return 1;
124
87.1k
    }
125
126
3.84M
    CBS contents;
127
3.84M
    CBS_ASN1_TAG tag, child_string_tag = string_tag;
128
3.84M
    size_t header_len;
129
3.84M
    int indefinite;
130
3.84M
    CBB *out_contents, out_contents_storage;
131
3.84M
    if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len,
132
                                      /*out_ber_found=*/NULL, &indefinite)) {
133
231
      return 0;
134
231
    }
135
136
3.84M
    if (string_tag != 0) {
137
      // This is part of a constructed string. All elements must match
138
      // |string_tag| up to the constructed bit and get appended to |out|
139
      // without a child element.
140
97.2k
      if ((tag & ~CBS_ASN1_CONSTRUCTED) != string_tag) {
141
90
        return 0;
142
90
      }
143
97.1k
      out_contents = out;
144
3.75M
    } else {
145
3.75M
      CBS_ASN1_TAG out_tag = tag;
146
3.75M
      if ((tag & CBS_ASN1_CONSTRUCTED) && is_string_type(tag)) {
147
        // If a constructed string, clear the constructed bit and inform
148
        // children to concatenate bodies.
149
2.49k
        out_tag &= ~CBS_ASN1_CONSTRUCTED;
150
2.49k
        child_string_tag = out_tag;
151
2.49k
      }
152
3.75M
      if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) {
153
0
        return 0;
154
0
      }
155
3.75M
      out_contents = &out_contents_storage;
156
3.75M
    }
157
158
3.84M
    if (indefinite) {
159
151k
      if (!cbs_convert_ber(in, out_contents, child_string_tag,
160
151k
                           /*looking_for_eoc=*/1, depth + 1) ||
161
151k
          !CBB_flush(out)) {
162
64.5k
        return 0;
163
64.5k
      }
164
87.1k
      continue;
165
151k
    }
166
167
3.69M
    if (!CBS_skip(&contents, header_len)) {
168
0
      return 0;
169
0
    }
170
171
3.69M
    if (tag & CBS_ASN1_CONSTRUCTED) {
172
      // Recurse into children.
173
44.7k
      if (!cbs_convert_ber(&contents, out_contents, child_string_tag,
174
44.7k
                           /*looking_for_eoc=*/0, depth + 1)) {
175
135
        return 0;
176
135
      }
177
3.65M
    } else {
178
      // Copy primitive contents as-is.
179
3.65M
      if (!CBB_add_bytes(out_contents, CBS_data(&contents),
180
3.65M
                         CBS_len(&contents))) {
181
0
        return 0;
182
0
      }
183
3.65M
    }
184
185
3.69M
    if (!CBB_flush(out)) {
186
0
      return 0;
187
0
    }
188
3.69M
  }
189
190
48.2k
  return looking_for_eoc == 0;
191
200k
}
192
193
10.0k
int CBS_asn1_ber_to_der(CBS *in, CBS *out, uint8_t **out_storage) {
194
10.0k
  CBB cbb;
195
196
  // First, do a quick walk to find any indefinite-length elements. Most of the
197
  // time we hope that there aren't any and thus we can quickly return.
198
10.0k
  int conversion_needed;
199
10.0k
  if (!cbs_find_ber(in, &conversion_needed, 0)) {
200
714
    return 0;
201
714
  }
202
203
9.35k
  if (!conversion_needed) {
204
5.37k
    if (!CBS_get_any_asn1_element(in, out, NULL, NULL)) {
205
131
      return 0;
206
131
    }
207
5.24k
    *out_storage = NULL;
208
5.24k
    return 1;
209
5.37k
  }
210
211
3.98k
  size_t len;
212
3.98k
  if (!CBB_init(&cbb, CBS_len(in)) ||
213
3.98k
      !cbs_convert_ber(in, &cbb, 0, 0, 0) ||
214
3.98k
      !CBB_finish(&cbb, out_storage, &len)) {
215
536
    CBB_cleanup(&cbb);
216
536
    return 0;
217
536
  }
218
219
3.44k
  CBS_init(out, *out_storage, len);
220
3.44k
  return 1;
221
3.98k
}
222
223
int CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage,
224
                                 CBS_ASN1_TAG outer_tag,
225
791
                                 CBS_ASN1_TAG inner_tag) {
226
791
  assert(!(outer_tag & CBS_ASN1_CONSTRUCTED));
227
791
  assert(!(inner_tag & CBS_ASN1_CONSTRUCTED));
228
791
  assert(is_string_type(inner_tag));
229
230
791
  if (CBS_peek_asn1_tag(in, outer_tag)) {
231
    // Normal implicitly-tagged string.
232
670
    *out_storage = NULL;
233
670
    return CBS_get_asn1(in, out, outer_tag);
234
670
  }
235
236
  // Otherwise, try to parse an implicitly-tagged constructed string.
237
  // |CBS_asn1_ber_to_der| is assumed to have run, so only allow one level deep
238
  // of nesting.
239
121
  CBB result;
240
121
  CBS child;
241
121
  if (!CBB_init(&result, CBS_len(in)) ||
242
121
      !CBS_get_asn1(in, &child, outer_tag | CBS_ASN1_CONSTRUCTED)) {
243
75
    goto err;
244
75
  }
245
246
410
  while (CBS_len(&child) > 0) {
247
369
    CBS chunk;
248
369
    if (!CBS_get_asn1(&child, &chunk, inner_tag) ||
249
369
        !CBB_add_bytes(&result, CBS_data(&chunk), CBS_len(&chunk))) {
250
5
      goto err;
251
5
    }
252
369
  }
253
254
41
  uint8_t *data;
255
41
  size_t len;
256
41
  if (!CBB_finish(&result, &data, &len)) {
257
0
    goto err;
258
0
  }
259
260
41
  CBS_init(out, data, len);
261
41
  *out_storage = data;
262
41
  return 1;
263
264
80
err:
265
80
  CBB_cleanup(&result);
266
80
  return 0;
267
41
}