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
0
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
0
  switch (tag & ~CBS_ASN1_CONSTRUCTED) {
36
0
    case CBS_ASN1_OCTETSTRING:
37
0
    case CBS_ASN1_UTF8STRING:
38
0
    case CBS_ASN1_NUMERICSTRING:
39
0
    case CBS_ASN1_PRINTABLESTRING:
40
0
    case CBS_ASN1_T61STRING:
41
0
    case CBS_ASN1_VIDEOTEXSTRING:
42
0
    case CBS_ASN1_IA5STRING:
43
0
    case CBS_ASN1_GRAPHICSTRING:
44
0
    case CBS_ASN1_VISIBLESTRING:
45
0
    case CBS_ASN1_GENERALSTRING:
46
0
    case CBS_ASN1_UNIVERSALSTRING:
47
0
    case CBS_ASN1_BMPSTRING:
48
0
      return 1;
49
0
    default:
50
0
      return 0;
51
0
  }
52
0
}
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
0
static int cbs_find_ber(const CBS *orig_in, int *ber_found, uint32_t depth) {
59
0
  CBS in;
60
61
0
  if (depth > kMaxDepth) {
62
0
    return 0;
63
0
  }
64
65
0
  CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
66
0
  *ber_found = 0;
67
68
0
  while (CBS_len(&in) > 0) {
69
0
    CBS contents;
70
0
    CBS_ASN1_TAG tag;
71
0
    size_t header_len;
72
0
    int indefinite;
73
0
    if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len,
74
0
                                      ber_found, &indefinite)) {
75
0
      return 0;
76
0
    }
77
0
    if (*ber_found) {
78
0
      return 1;
79
0
    }
80
0
    if (tag & CBS_ASN1_CONSTRUCTED) {
81
0
      if (is_string_type(tag)) {
82
        // Constructed strings are only legal in BER and require conversion.
83
0
        *ber_found = 1;
84
0
        return 1;
85
0
      }
86
0
      if (!CBS_skip(&contents, header_len) ||
87
0
          !cbs_find_ber(&contents, ber_found, depth + 1)) {
88
0
        return 0;
89
0
      }
90
0
    }
91
0
  }
92
93
0
  return 1;
94
0
}
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
0
static int cbs_get_eoc(CBS *cbs) {
99
0
  if (CBS_len(cbs) >= 2 &&
100
0
      CBS_data(cbs)[0] == 0 && CBS_data(cbs)[1] == 0) {
101
0
    return CBS_skip(cbs, 2);
102
0
  }
103
0
  return 0;
104
0
}
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
0
                           int looking_for_eoc, uint32_t depth) {
115
0
  assert(!(string_tag & CBS_ASN1_CONSTRUCTED));
116
117
0
  if (depth > kMaxDepth) {
118
0
    return 0;
119
0
  }
120
121
0
  while (CBS_len(in) > 0) {
122
0
    if (looking_for_eoc && cbs_get_eoc(in)) {
123
0
      return 1;
124
0
    }
125
126
0
    CBS contents;
127
0
    CBS_ASN1_TAG tag, child_string_tag = string_tag;
128
0
    size_t header_len;
129
0
    int indefinite;
130
0
    CBB *out_contents, out_contents_storage;
131
0
    if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len,
132
                                      /*out_ber_found=*/NULL, &indefinite)) {
133
0
      return 0;
134
0
    }
135
136
0
    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
0
      if ((tag & ~CBS_ASN1_CONSTRUCTED) != string_tag) {
141
0
        return 0;
142
0
      }
143
0
      out_contents = out;
144
0
    } else {
145
0
      CBS_ASN1_TAG out_tag = tag;
146
0
      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
0
        out_tag &= ~CBS_ASN1_CONSTRUCTED;
150
0
        child_string_tag = out_tag;
151
0
      }
152
0
      if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) {
153
0
        return 0;
154
0
      }
155
0
      out_contents = &out_contents_storage;
156
0
    }
157
158
0
    if (indefinite) {
159
0
      if (!cbs_convert_ber(in, out_contents, child_string_tag,
160
0
                           /*looking_for_eoc=*/1, depth + 1) ||
161
0
          !CBB_flush(out)) {
162
0
        return 0;
163
0
      }
164
0
      continue;
165
0
    }
166
167
0
    if (!CBS_skip(&contents, header_len)) {
168
0
      return 0;
169
0
    }
170
171
0
    if (tag & CBS_ASN1_CONSTRUCTED) {
172
      // Recurse into children.
173
0
      if (!cbs_convert_ber(&contents, out_contents, child_string_tag,
174
0
                           /*looking_for_eoc=*/0, depth + 1)) {
175
0
        return 0;
176
0
      }
177
0
    } else {
178
      // Copy primitive contents as-is.
179
0
      if (!CBB_add_bytes(out_contents, CBS_data(&contents),
180
0
                         CBS_len(&contents))) {
181
0
        return 0;
182
0
      }
183
0
    }
184
185
0
    if (!CBB_flush(out)) {
186
0
      return 0;
187
0
    }
188
0
  }
189
190
0
  return looking_for_eoc == 0;
191
0
}
192
193
0
int CBS_asn1_ber_to_der(CBS *in, CBS *out, uint8_t **out_storage) {
194
0
  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
0
  int conversion_needed;
199
0
  if (!cbs_find_ber(in, &conversion_needed, 0)) {
200
0
    return 0;
201
0
  }
202
203
0
  if (!conversion_needed) {
204
0
    if (!CBS_get_any_asn1_element(in, out, NULL, NULL)) {
205
0
      return 0;
206
0
    }
207
0
    *out_storage = NULL;
208
0
    return 1;
209
0
  }
210
211
0
  size_t len;
212
0
  if (!CBB_init(&cbb, CBS_len(in)) ||
213
0
      !cbs_convert_ber(in, &cbb, 0, 0, 0) ||
214
0
      !CBB_finish(&cbb, out_storage, &len)) {
215
0
    CBB_cleanup(&cbb);
216
0
    return 0;
217
0
  }
218
219
0
  CBS_init(out, *out_storage, len);
220
0
  return 1;
221
0
}
222
223
int CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage,
224
                                 CBS_ASN1_TAG outer_tag,
225
0
                                 CBS_ASN1_TAG inner_tag) {
226
0
  assert(!(outer_tag & CBS_ASN1_CONSTRUCTED));
227
0
  assert(!(inner_tag & CBS_ASN1_CONSTRUCTED));
228
0
  assert(is_string_type(inner_tag));
229
230
0
  if (CBS_peek_asn1_tag(in, outer_tag)) {
231
    // Normal implicitly-tagged string.
232
0
    *out_storage = NULL;
233
0
    return CBS_get_asn1(in, out, outer_tag);
234
0
  }
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
0
  CBB result;
240
0
  CBS child;
241
0
  if (!CBB_init(&result, CBS_len(in)) ||
242
0
      !CBS_get_asn1(in, &child, outer_tag | CBS_ASN1_CONSTRUCTED)) {
243
0
    goto err;
244
0
  }
245
246
0
  while (CBS_len(&child) > 0) {
247
0
    CBS chunk;
248
0
    if (!CBS_get_asn1(&child, &chunk, inner_tag) ||
249
0
        !CBB_add_bytes(&result, CBS_data(&chunk), CBS_len(&chunk))) {
250
0
      goto err;
251
0
    }
252
0
  }
253
254
0
  uint8_t *data;
255
0
  size_t len;
256
0
  if (!CBB_finish(&result, &data, &len)) {
257
0
    goto err;
258
0
  }
259
260
0
  CBS_init(out, data, len);
261
0
  *out_storage = data;
262
0
  return 1;
263
264
0
err:
265
0
  CBB_cleanup(&result);
266
0
  return 0;
267
0
}