OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_DER_PARSER_H_ | 5 #ifndef NET_DER_PARSER_H_ |
6 #define NET_DER_PARSER_H_ | 6 #define NET_DER_PARSER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
14 #include "net/der/input.h" | 14 #include "net/der/input.h" |
15 #include "net/der/tag.h" | 15 #include "net/der/tag.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 namespace der { | 19 namespace der { |
20 | 20 |
21 class BitString; | 21 class BitString; |
| 22 struct GeneralizedTime; |
22 | 23 |
23 // Parses a DER-encoded ASN.1 structure. DER (distinguished encoding rules) | 24 // Parses a DER-encoded ASN.1 structure. DER (distinguished encoding rules) |
24 // encodes each data value with a tag, length, and value (TLV). The tag | 25 // encodes each data value with a tag, length, and value (TLV). The tag |
25 // indicates the type of the ASN.1 value. Depending on the type of the value, | 26 // indicates the type of the ASN.1 value. Depending on the type of the value, |
26 // it could contain arbitrary bytes, so the length of the value is encoded | 27 // it could contain arbitrary bytes, so the length of the value is encoded |
27 // after the tag and before the value to indicate how many bytes of value | 28 // after the tag and before the value to indicate how many bytes of value |
28 // follow. DER also defines how the values are encoded for particular types. | 29 // follow. DER also defines how the values are encoded for particular types. |
29 // | 30 // |
30 // This Parser places a few restrictions on the DER encoding it can parse. The | 31 // This Parser places a few restrictions on the DER encoding it can parse. The |
31 // largest restriction is that it only supports tags which have a tag number | 32 // largest restriction is that it only supports tags which have a tag number |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 // encoding for a specific type. | 140 // encoding for a specific type. |
140 | 141 |
141 // Reads the current TLV from the input, checks that the tag matches |tag| | 142 // Reads the current TLV from the input, checks that the tag matches |tag| |
142 // and is a constructed tag, and creates a new Parser from the value. | 143 // and is a constructed tag, and creates a new Parser from the value. |
143 bool ReadConstructed(Tag tag, Parser* out) WARN_UNUSED_RESULT; | 144 bool ReadConstructed(Tag tag, Parser* out) WARN_UNUSED_RESULT; |
144 | 145 |
145 // A more specific form of ReadConstructed that expects the current tag | 146 // A more specific form of ReadConstructed that expects the current tag |
146 // to be 0x30 (SEQUENCE). | 147 // to be 0x30 (SEQUENCE). |
147 bool ReadSequence(Parser* out) WARN_UNUSED_RESULT; | 148 bool ReadSequence(Parser* out) WARN_UNUSED_RESULT; |
148 | 149 |
| 150 // Expects the current tag to be kInteger, and calls ParseUint8 on the |
| 151 // current value. Note that DER-encoded integers are arbitrary precision, |
| 152 // so this method will fail for valid input that represents an integer |
| 153 // outside the range of an uint8_t. |
| 154 // |
| 155 // Note that on failure the Parser is left in an undefined state (the |
| 156 // input may or may not have been advanced). |
| 157 bool ReadUint8(uint8_t* out) WARN_UNUSED_RESULT; |
| 158 |
149 // Expects the current tag to be kInteger, and calls ParseUint64 on the | 159 // Expects the current tag to be kInteger, and calls ParseUint64 on the |
150 // current value. Note that DER-encoded integers are arbitrary precision, | 160 // current value. Note that DER-encoded integers are arbitrary precision, |
151 // so this method will fail for valid input that represents an integer | 161 // so this method will fail for valid input that represents an integer |
152 // outside the range of an int64_t. | 162 // outside the range of an uint64_t. |
153 // | 163 // |
154 // Note that on failure the Parser is left in an undefined state (the | 164 // Note that on failure the Parser is left in an undefined state (the |
155 // input may or may not have been advanced). | 165 // input may or may not have been advanced). |
156 bool ReadUint64(uint64_t* out) WARN_UNUSED_RESULT; | 166 bool ReadUint64(uint64_t* out) WARN_UNUSED_RESULT; |
157 | 167 |
158 // Reads a BIT STRING. On success fills |out| and returns true. | 168 // Reads a BIT STRING. On success fills |out| and returns true. |
159 // | 169 // |
160 // Note that on failure the Parser is left in an undefined state (the | 170 // Note that on failure the Parser is left in an undefined state (the |
161 // input may or may not have been advanced). | 171 // input may or may not have been advanced). |
162 bool ReadBitString(BitString* out) WARN_UNUSED_RESULT; | 172 bool ReadBitString(BitString* out) WARN_UNUSED_RESULT; |
163 | 173 |
| 174 // Reads a GeneralizeTime. On success fills |out| and returns true. |
| 175 // |
| 176 // Note that on failure the Parser is left in an undefined state (the |
| 177 // input may or may not have been advanced). |
| 178 bool ReadGeneralizedTime(GeneralizedTime* out) WARN_UNUSED_RESULT; |
| 179 |
164 // Lower level methods. The previous methods couple reading data from the | 180 // Lower level methods. The previous methods couple reading data from the |
165 // input with advancing the Parser's internal pointer to the next TLV; these | 181 // input with advancing the Parser's internal pointer to the next TLV; these |
166 // lower level methods decouple those two steps into methods that read from | 182 // lower level methods decouple those two steps into methods that read from |
167 // the current TLV and a method that advances the internal pointer to the | 183 // the current TLV and a method that advances the internal pointer to the |
168 // next TLV. | 184 // next TLV. |
169 | 185 |
170 // Reads the current TLV from the input, putting the tag in |tag| and the raw | 186 // Reads the current TLV from the input, putting the tag in |tag| and the raw |
171 // value in |out|, but does not advance the input. Returns true if the tag | 187 // value in |out|, but does not advance the input. Returns true if the tag |
172 // and length are successfully read and the output exists. | 188 // and length are successfully read and the output exists. |
173 bool PeekTagAndValue(Tag* tag, Input* out) WARN_UNUSED_RESULT; | 189 bool PeekTagAndValue(Tag* tag, Input* out) WARN_UNUSED_RESULT; |
174 | 190 |
175 // Advances the input to the next TLV. This method only needs to be called | 191 // Advances the input to the next TLV. This method only needs to be called |
176 // after PeekTagAndValue; all other methods will advance the input if they | 192 // after PeekTagAndValue; all other methods will advance the input if they |
177 // read something. | 193 // read something. |
178 bool Advance(); | 194 bool Advance(); |
179 | 195 |
180 private: | 196 private: |
181 ByteReader input_; | 197 ByteReader input_; |
182 Mark advance_mark_; | 198 Mark advance_mark_; |
183 | 199 |
184 DISALLOW_COPY(Parser); | 200 DISALLOW_COPY(Parser); |
185 }; | 201 }; |
186 | 202 |
187 } // namespace der | 203 } // namespace der |
188 | 204 |
189 } // namespace net | 205 } // namespace net |
190 | 206 |
191 #endif // NET_DER_PARSER_H_ | 207 #endif // NET_DER_PARSER_H_ |
OLD | NEW |