1 // Copyright 2015 The Chromium Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "parser.h"
16
17 #include "parse_values.h"
18
19 BSSL_NAMESPACE_BEGIN
20 namespace der {
21
Parser()22 Parser::Parser() { CBS_init(&cbs_, nullptr, 0); }
23
Parser(Input input)24 Parser::Parser(Input input) { CBS_init(&cbs_, input.data(), input.size()); }
25
PeekTagAndValue(CBS_ASN1_TAG * tag,Input * out)26 bool Parser::PeekTagAndValue(CBS_ASN1_TAG *tag, Input *out) {
27 CBS peeker = cbs_;
28 CBS tmp_out;
29 size_t header_len;
30 CBS_ASN1_TAG tag_value;
31 if (!CBS_get_any_asn1_element(&peeker, &tmp_out, &tag_value, &header_len) ||
32 !CBS_skip(&tmp_out, header_len)) {
33 return false;
34 }
35 advance_len_ = CBS_len(&tmp_out) + header_len;
36 *tag = tag_value;
37 *out = Input(CBS_data(&tmp_out), CBS_len(&tmp_out));
38 return true;
39 }
40
Advance()41 bool Parser::Advance() {
42 if (advance_len_ == 0) {
43 return false;
44 }
45 bool ret = !!CBS_skip(&cbs_, advance_len_);
46 advance_len_ = 0;
47 return ret;
48 }
49
HasMore()50 bool Parser::HasMore() { return CBS_len(&cbs_) > 0; }
51
ReadRawTLV(Input * out)52 bool Parser::ReadRawTLV(Input *out) {
53 CBS tmp_out;
54 if (!CBS_get_any_asn1_element(&cbs_, &tmp_out, nullptr, nullptr)) {
55 return false;
56 }
57 *out = Input(CBS_data(&tmp_out), CBS_len(&tmp_out));
58 return true;
59 }
60
ReadTagAndValue(CBS_ASN1_TAG * tag,Input * out)61 bool Parser::ReadTagAndValue(CBS_ASN1_TAG *tag, Input *out) {
62 if (!PeekTagAndValue(tag, out)) {
63 return false;
64 }
65 BSSL_CHECK(Advance());
66 return true;
67 }
68
ReadOptionalTag(CBS_ASN1_TAG tag,std::optional<Input> * out)69 bool Parser::ReadOptionalTag(CBS_ASN1_TAG tag, std::optional<Input> *out) {
70 if (!HasMore()) {
71 *out = std::nullopt;
72 return true;
73 }
74 CBS_ASN1_TAG actual_tag;
75 Input value;
76 if (!PeekTagAndValue(&actual_tag, &value)) {
77 return false;
78 }
79 if (actual_tag == tag) {
80 BSSL_CHECK(Advance());
81 *out = value;
82 } else {
83 advance_len_ = 0;
84 *out = std::nullopt;
85 }
86 return true;
87 }
88
ReadOptionalTag(CBS_ASN1_TAG tag,Input * out,bool * present)89 bool Parser::ReadOptionalTag(CBS_ASN1_TAG tag, Input *out, bool *present) {
90 std::optional<Input> tmp_out;
91 if (!ReadOptionalTag(tag, &tmp_out)) {
92 return false;
93 }
94 *present = tmp_out.has_value();
95 *out = tmp_out.value_or(der::Input());
96 return true;
97 }
98
SkipOptionalTag(CBS_ASN1_TAG tag,bool * present)99 bool Parser::SkipOptionalTag(CBS_ASN1_TAG tag, bool *present) {
100 Input out;
101 return ReadOptionalTag(tag, &out, present);
102 }
103
ReadTag(CBS_ASN1_TAG tag,Input * out)104 bool Parser::ReadTag(CBS_ASN1_TAG tag, Input *out) {
105 CBS_ASN1_TAG actual_tag;
106 Input value;
107 if (!PeekTagAndValue(&actual_tag, &value) || actual_tag != tag) {
108 return false;
109 }
110 BSSL_CHECK(Advance());
111 *out = value;
112 return true;
113 }
114
SkipTag(CBS_ASN1_TAG tag)115 bool Parser::SkipTag(CBS_ASN1_TAG tag) {
116 Input out;
117 return ReadTag(tag, &out);
118 }
119
120 // Type-specific variants of ReadTag
121
ReadConstructed(CBS_ASN1_TAG tag,Parser * out)122 bool Parser::ReadConstructed(CBS_ASN1_TAG tag, Parser *out) {
123 if (!(tag & CBS_ASN1_CONSTRUCTED)) {
124 return false;
125 }
126 Input data;
127 if (!ReadTag(tag, &data)) {
128 return false;
129 }
130 *out = Parser(data);
131 return true;
132 }
133
ReadSequence(Parser * out)134 bool Parser::ReadSequence(Parser *out) {
135 return ReadConstructed(CBS_ASN1_SEQUENCE, out);
136 }
137
ReadUint8(uint8_t * out)138 bool Parser::ReadUint8(uint8_t *out) {
139 Input encoded_int;
140 if (!ReadTag(CBS_ASN1_INTEGER, &encoded_int)) {
141 return false;
142 }
143 return ParseUint8(encoded_int, out);
144 }
145
ReadUint64(uint64_t * out)146 bool Parser::ReadUint64(uint64_t *out) {
147 Input encoded_int;
148 if (!ReadTag(CBS_ASN1_INTEGER, &encoded_int)) {
149 return false;
150 }
151 return ParseUint64(encoded_int, out);
152 }
153
ReadBitString()154 std::optional<BitString> Parser::ReadBitString() {
155 Input value;
156 if (!ReadTag(CBS_ASN1_BITSTRING, &value)) {
157 return std::nullopt;
158 }
159 return ParseBitString(value);
160 }
161
ReadGeneralizedTime(GeneralizedTime * out)162 bool Parser::ReadGeneralizedTime(GeneralizedTime *out) {
163 Input value;
164 if (!ReadTag(CBS_ASN1_GENERALIZEDTIME, &value)) {
165 return false;
166 }
167 return ParseGeneralizedTime(value, out);
168 }
169
170 } // namespace der
171 BSSL_NAMESPACE_END
172