summaryrefslogtreecommitdiffstats
path: root/net/der/parser.cc
blob: 0cb2600f5e6919606df2f422299e49cfd009f6cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "base/numerics/safe_math.h"
#include "net/der/parse_values.h"
#include "net/der/parser.h"

namespace net {

namespace der {

Parser::Parser() : input_(Input()), advance_mark_(Mark::NullMark()) {
}

Parser::Parser(const Input& input)
    : input_(input), advance_mark_(Mark::NullMark()) {
}

bool Parser::PeekTagAndValue(Tag* tag, Input* out) {
  ByteReader reader = input_;

  // Don't support tags > 30.
  uint8_t tag_byte;
  if (!reader.ReadByte(&tag_byte))
    return false;

  // ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a
  // tag number no greater than 30. This parser only supports tag numbers up
  // to 30.
  // If the tag number is 31 (0x1F, the largest value that fits in the allotted
  // bytes), then the tag is more than one byte long and the continuation bytes
  // contain the real tag number. We only support tag numbers < 31 (and thus
  // single-byte tags).
  if ((tag_byte & kTagNumberMask) == 31)
    return false;

  // Parse length. The format for the length encoding is specified in
  // ITU-T X.690 section 8.1.3.
  size_t value_len = 0;  // Number of bytes used to encode just the value.

  uint8_t length_first_byte;
  if (!reader.ReadByte(&length_first_byte))
    return false;
  if ((length_first_byte & 0x80) == 0) {
    // Short form for length - it's only one byte long.
    value_len = length_first_byte & 0x7f;
  } else {
    // Long form for length - it's encoded across multiple bytes.
    if (length_first_byte == 0xff) {
      // ITU-T X.690 clause 8.1.3.5.c specifies the value 0xff shall not be
      // used.
      return false;
    }
    // The high bit indicated that this is the long form, while the next 7 bits
    // encode the number of subsequent octets used to encode the length
    // (ITU-T X.690 clause 8.1.3.5.b).
    size_t length_len = length_first_byte & 0x7f;
    if (length_len == 0) {
      // ITU-T X.690 section 10.1 (DER length forms) requires encoding the
      // length with the minimum number of octets. Besides, it makes no sense
      // for the length to be encoded in 0 octets.
      return false;
    }
    if (length_len > sizeof(value_len)) {
      // The length is encoded in multiple octets, with the first octet
      // indicating how many octets follow. Those octets need to be combined
      // to form a size_t, so the number of octets to follow (length_len)
      // must be small enough so that they fit in a size_t.
      return false;
    }
    uint8_t length_byte;
    for (size_t i = 0; i < length_len; i++) {
      if (!reader.ReadByte(&length_byte))
        return false;
      // A first length byte of all zeroes means the length was not encoded in
      // minimum length.
      if (i == 0 && length_byte == 0)
        return false;
      value_len <<= 8;
      value_len += length_byte;
    }
    if (value_len < 0x80) {
      // If value_len is < 0x80, then it could have been encoded in a single
      // byte, meaning it was not encoded in minimum length.
      return false;
    }
  }

  if (!reader.ReadBytes(value_len, out))
    return false;
  advance_mark_ = reader.NewMark();
  *tag = tag_byte;
  return true;
}

bool Parser::Advance() {
  if (advance_mark_.IsEmpty())
    return false;
  if (!input_.AdvanceToMark(advance_mark_))
    return false;
  advance_mark_ = Mark::NullMark();
  return true;
}

bool Parser::HasMore() {
  return input_.HasMore();
}

bool Parser::ReadRawTLV(Input* out) {
  Tag tag;
  Input value;
  if (!PeekTagAndValue(&tag, &value))
    return false;
  if (!input_.ReadToMark(advance_mark_, out))
    return false;
  advance_mark_ = Mark::NullMark();

  return true;
}

bool Parser::ReadTagAndValue(Tag* tag, Input* out) {
  if (!PeekTagAndValue(tag, out))
    return false;
  CHECK(Advance());
  return true;
}

bool Parser::ReadOptionalTag(Tag tag, Input* out, bool* present) {
  if (!HasMore()) {
    *present = false;
    return true;
  }

  Tag read_tag;
  Input value;
  if (!PeekTagAndValue(&read_tag, &value))
    return false;
  *present = false;
  if (read_tag == tag) {
    *present = true;
    *out = value;
    CHECK(Advance());
  } else {
    advance_mark_ = Mark::NullMark();
  }
  return true;
}

bool Parser::SkipOptionalTag(Tag tag, bool* present) {
  Input out;
  return ReadOptionalTag(tag, &out, present);
}

bool Parser::ReadTag(Tag tag, Input* out) {
  bool present;
  return ReadOptionalTag(tag, out, &present) && present;
}

bool Parser::SkipTag(Tag tag) {
  Input out;
  return ReadTag(tag, &out);
}

// Type-specific variants of ReadTag

bool Parser::ReadConstructed(Tag tag, Parser* out) {
  if (!IsConstructed(tag))
    return false;
  Input data;
  if (!ReadTag(tag, &data))
    return false;
  *out = Parser(data);
  return true;
}

bool Parser::ReadSequence(Parser* out) {
  return ReadConstructed(kSequence, out);
}

bool Parser::ReadUint8(uint8_t* out) {
  Input encoded_int;
  if (!ReadTag(kInteger, &encoded_int))
    return false;
  return ParseUint8(encoded_int, out);
}

bool Parser::ReadUint64(uint64_t* out) {
  Input encoded_int;
  if (!ReadTag(kInteger, &encoded_int))
    return false;
  return ParseUint64(encoded_int, out);
}

bool Parser::ReadBitString(BitString* bit_string) {
  Input value;
  if (!ReadTag(kBitString, &value))
    return false;
  return ParseBitString(value, bit_string);
}

bool Parser::ReadGeneralizedTime(GeneralizedTime* out) {
  Input value;
  if (!ReadTag(kGeneralizedTime, &value))
    return false;
  return ParseGeneralizedTime(value, out);
}

}  // namespace der

}  // namespace net