Mac Partial Swap: Fix sub-layer rounding issue
[chromium-blink-merge.git] / net / der / parse_values.cc
blob445903250fb1c504b78eb83f5742d23c6217d816
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "base/numerics/safe_math.h"
7 #include "net/der/parse_values.h"
9 namespace net {
11 namespace der {
13 namespace {
15 bool ParseBoolInternal(const Input& in, bool* out, bool relaxed) {
16 // According to ITU-T X.690 section 8.2, a bool is encoded as a single octet
17 // where the octet of all zeroes is FALSE and a non-zero value for the octet
18 // is TRUE.
19 if (in.Length() != 1)
20 return false;
21 ByteReader data(in);
22 uint8_t byte;
23 if (!data.ReadByte(&byte))
24 return false;
25 if (byte == 0) {
26 *out = false;
27 return true;
29 // ITU-T X.690 section 11.1 specifies that for DER, the TRUE value must be
30 // encoded as an octet of all ones.
31 if (byte == 0xff || relaxed) {
32 *out = true;
33 return true;
35 return false;
38 // Reads a positive decimal number with |digits| digits and stores it in
39 // |*out|. This function does not check that the type of |*out| is large
40 // enough to hold 10^digits - 1; the caller must choose an appropriate type
41 // based on the number of digits they wish to parse.
42 template <typename UINT>
43 bool DecimalStringToUint(ByteReader& in, size_t digits, UINT* out) {
44 UINT value = 0;
45 for (size_t i = 0; i < digits; ++i) {
46 uint8_t digit;
47 if (!in.ReadByte(&digit)) {
48 return false;
50 if (digit < '0' || digit > '9') {
51 return false;
53 value = (value * 10) + (digit - '0');
55 *out = value;
56 return true;
59 // Checks that the values in a GeneralizedTime struct are valid. This involves
60 // checking that the year is 4 digits, the month is between 1 and 12, the day
61 // is a day that exists in that month (following current leap year rules),
62 // hours are between 0 and 23, minutes between 0 and 59, and seconds between
63 // 0 and 60 (to allow for leap seconds; no validation is done that a leap
64 // second is on a day that could be a leap second).
65 bool ValidateGeneralizedTime(const GeneralizedTime& time) {
66 if (time.month < 1 || time.month > 12)
67 return false;
68 if (time.day < 1)
69 return false;
70 if (time.hours < 0 || time.hours > 23)
71 return false;
72 if (time.minutes < 0 || time.minutes > 59)
73 return false;
74 // Leap seconds are allowed.
75 if (time.seconds < 0 || time.seconds > 60)
76 return false;
78 // validate upper bound for day of month
79 switch (time.month) {
80 case 4:
81 case 6:
82 case 9:
83 case 11:
84 if (time.day > 30)
85 return false;
86 break;
87 case 1:
88 case 3:
89 case 5:
90 case 7:
91 case 8:
92 case 10:
93 case 12:
94 if (time.day > 31)
95 return false;
96 break;
97 case 2:
98 if (time.year % 4 == 0 &&
99 (time.year % 100 != 0 || time.year % 400 == 0)) {
100 if (time.day > 29)
101 return false;
102 } else {
103 if (time.day > 28)
104 return false;
106 break;
107 default:
108 NOTREACHED();
109 return false;
111 return true;
114 } // namespace
116 bool ParseBool(const Input& in, bool* out) {
117 return ParseBoolInternal(in, out, false /* relaxed */);
120 // BER interprets any non-zero value as true, while DER requires a bool to
121 // have either all bits zero (false) or all bits one (true). To support
122 // malformed certs, we recognized the BER encoding instead of failing to
123 // parse.
124 bool ParseBoolRelaxed(const Input& in, bool* out) {
125 return ParseBoolInternal(in, out, true /* relaxed */);
128 bool ParseUint64(const Input& in, uint64_t* out) {
129 ByteReader reader(in);
130 size_t bytes_read = 0;
131 uint8_t data;
132 uint64_t value = 0;
133 // Note that for simplicity, this check only admits integers up to 2^63-1.
134 if (in.Length() > sizeof(uint64_t) || in.Length() == 0)
135 return false;
136 while (reader.ReadByte(&data)) {
137 if (bytes_read == 0 && (data & 0x80)) {
138 return false;
140 value <<= 8;
141 value |= data;
142 bytes_read++;
144 // ITU-T X.690 section 8.3.2 specifies that an integer value must be encoded
145 // in the smallest number of octets. If the encoding consists of more than
146 // one octet, then the bits of the first octet and the most significant bit
147 // of the second octet must not be all zeroes or all ones.
148 // Because this function only parses unsigned integers, there's no need to
149 // check for the all ones case.
150 if (bytes_read > 1) {
151 ByteReader first_bytes_reader(in);
152 uint8_t first_byte;
153 uint8_t second_byte;
154 if (!first_bytes_reader.ReadByte(&first_byte) ||
155 !first_bytes_reader.ReadByte(&second_byte)) {
156 return false;
158 if (first_byte == 0 && !(second_byte & 0x80)) {
159 return false;
162 *out = value;
163 return true;
166 BitString::BitString(const Input& bytes, uint8_t unused_bits)
167 : bytes_(bytes), unused_bits_(unused_bits) {
168 DCHECK_LT(unused_bits, 8);
169 DCHECK(unused_bits == 0 || bytes.Length() != 0);
172 bool ParseBitString(const Input& in, BitString* out) {
173 ByteReader reader(in);
175 // From ITU-T X.690, section 8.6.2.2 (applies to BER, CER, DER):
177 // The initial octet shall encode, as an unsigned binary integer with
178 // bit 1 as the least significant bit, the number of unused bits in the final
179 // subsequent octet. The number shall be in the range zero to seven.
180 uint8_t unused_bits;
181 if (!reader.ReadByte(&unused_bits))
182 return false;
183 if (unused_bits > 7)
184 return false;
186 Input bytes;
187 if (!reader.ReadBytes(reader.BytesLeft(), &bytes))
188 return false; // Not reachable.
190 // Ensure that unused bits in the last byte are set to 0.
191 if (unused_bits > 0) {
192 // From ITU-T X.690, section 8.6.2.3 (applies to BER, CER, DER):
194 // If the bitstring is empty, there shall be no subsequent octets,
195 // and the initial octet shall be zero.
196 if (bytes.Length() == 0)
197 return false;
198 uint8_t last_byte = bytes.UnsafeData()[bytes.Length() - 1];
200 // From ITU-T X.690, section 11.2.1 (applies to CER and DER, but not BER):
202 // Each unused bit in the final octet of the encoding of a bit string value
203 // shall be set to zero.
204 uint8_t mask = 0xFF >> (8 - unused_bits);
205 if ((mask & last_byte) != 0)
206 return false;
209 *out = BitString(bytes, unused_bits);
210 return true;
213 bool operator<(const GeneralizedTime& lhs, const GeneralizedTime& rhs) {
214 if (lhs.year != rhs.year)
215 return lhs.year < rhs.year;
216 if (lhs.month != rhs.month)
217 return lhs.month < rhs.month;
218 if (lhs.day != rhs.day)
219 return lhs.day < rhs.day;
220 if (lhs.hours != rhs.hours)
221 return lhs.hours < rhs.hours;
222 if (lhs.minutes != rhs.minutes)
223 return lhs.minutes < rhs.minutes;
224 return lhs.seconds < rhs.seconds;
227 // A UTC Time in DER encoding should be YYMMDDHHMMSSZ, but some CAs encode
228 // the time following BER rules, which allows for YYMMDDHHMMZ. If the length
229 // is 11, assume it's YYMMDDHHMMZ, and in converting it to a GeneralizedTime,
230 // add in the seconds (set to 0).
231 bool ParseUTCTimeRelaxed(const Input& in, GeneralizedTime* value) {
232 ByteReader reader(in);
233 GeneralizedTime time;
234 if (!DecimalStringToUint(reader, 2, &time.year) ||
235 !DecimalStringToUint(reader, 2, &time.month) ||
236 !DecimalStringToUint(reader, 2, &time.day) ||
237 !DecimalStringToUint(reader, 2, &time.hours) ||
238 !DecimalStringToUint(reader, 2, &time.minutes)) {
239 return false;
242 // Try to read the 'Z' at the end. If we read something else, then for it to
243 // be valid the next bytes should be seconds (and then followed by 'Z').
244 uint8_t zulu;
245 ByteReader zulu_reader = reader;
246 if (!zulu_reader.ReadByte(&zulu))
247 return false;
248 if (zulu == 'Z' && !zulu_reader.HasMore()) {
249 time.seconds = 0;
250 *value = time;
251 } else {
252 if (!DecimalStringToUint(reader, 2, &time.seconds))
253 return false;
254 if (!reader.ReadByte(&zulu) || zulu != 'Z' || reader.HasMore())
255 return false;
258 if (time.year < 50) {
259 time.year += 2000;
260 } else {
261 time.year += 1900;
263 if (!ValidateGeneralizedTime(time))
264 return false;
265 *value = time;
266 return true;
269 bool ParseUTCTime(const Input& in, GeneralizedTime* value) {
270 ByteReader reader(in);
271 GeneralizedTime time;
272 if (!DecimalStringToUint(reader, 2, &time.year) ||
273 !DecimalStringToUint(reader, 2, &time.month) ||
274 !DecimalStringToUint(reader, 2, &time.day) ||
275 !DecimalStringToUint(reader, 2, &time.hours) ||
276 !DecimalStringToUint(reader, 2, &time.minutes) ||
277 !DecimalStringToUint(reader, 2, &time.seconds)) {
278 return false;
280 uint8_t zulu;
281 if (!reader.ReadByte(&zulu) || zulu != 'Z' || reader.HasMore())
282 return false;
283 if (time.year < 50) {
284 time.year += 2000;
285 } else {
286 time.year += 1900;
288 if (!ValidateGeneralizedTime(time))
289 return false;
290 *value = time;
291 return true;
294 bool ParseGeneralizedTime(const Input& in, GeneralizedTime* value) {
295 ByteReader reader(in);
296 GeneralizedTime time;
297 if (!DecimalStringToUint(reader, 4, &time.year) ||
298 !DecimalStringToUint(reader, 2, &time.month) ||
299 !DecimalStringToUint(reader, 2, &time.day) ||
300 !DecimalStringToUint(reader, 2, &time.hours) ||
301 !DecimalStringToUint(reader, 2, &time.minutes) ||
302 !DecimalStringToUint(reader, 2, &time.seconds)) {
303 return false;
305 uint8_t zulu;
306 if (!reader.ReadByte(&zulu) || zulu != 'Z' || reader.HasMore())
307 return false;
308 if (!ValidateGeneralizedTime(time))
309 return false;
310 *value = time;
311 return true;
314 } // namespace der
316 } // namespace net