forked from google/boringssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cc
161 lines (138 loc) · 3.54 KB
/
parser.cc
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "parser.h"
#include "parse_values.h"
BSSL_NAMESPACE_BEGIN
namespace der {
Parser::Parser() { CBS_init(&cbs_, nullptr, 0); }
Parser::Parser(Input input) { CBS_init(&cbs_, input.data(), input.size()); }
bool Parser::PeekTagAndValue(CBS_ASN1_TAG *tag, Input *out) {
CBS peeker = cbs_;
CBS tmp_out;
size_t header_len;
CBS_ASN1_TAG tag_value;
if (!CBS_get_any_asn1_element(&peeker, &tmp_out, &tag_value, &header_len) ||
!CBS_skip(&tmp_out, header_len)) {
return false;
}
advance_len_ = CBS_len(&tmp_out) + header_len;
*tag = tag_value;
*out = Input(CBS_data(&tmp_out), CBS_len(&tmp_out));
return true;
}
bool Parser::Advance() {
if (advance_len_ == 0) {
return false;
}
bool ret = !!CBS_skip(&cbs_, advance_len_);
advance_len_ = 0;
return ret;
}
bool Parser::HasMore() { return CBS_len(&cbs_) > 0; }
bool Parser::ReadRawTLV(Input *out) {
CBS tmp_out;
if (!CBS_get_any_asn1_element(&cbs_, &tmp_out, nullptr, nullptr)) {
return false;
}
*out = Input(CBS_data(&tmp_out), CBS_len(&tmp_out));
return true;
}
bool Parser::ReadTagAndValue(CBS_ASN1_TAG *tag, Input *out) {
if (!PeekTagAndValue(tag, out)) {
return false;
}
BSSL_CHECK(Advance());
return true;
}
bool Parser::ReadOptionalTag(CBS_ASN1_TAG tag, std::optional<Input> *out) {
if (!HasMore()) {
*out = std::nullopt;
return true;
}
CBS_ASN1_TAG actual_tag;
Input value;
if (!PeekTagAndValue(&actual_tag, &value)) {
return false;
}
if (actual_tag == tag) {
BSSL_CHECK(Advance());
*out = value;
} else {
advance_len_ = 0;
*out = std::nullopt;
}
return true;
}
bool Parser::ReadOptionalTag(CBS_ASN1_TAG tag, Input *out, bool *present) {
std::optional<Input> tmp_out;
if (!ReadOptionalTag(tag, &tmp_out)) {
return false;
}
*present = tmp_out.has_value();
*out = tmp_out.value_or(der::Input());
return true;
}
bool Parser::SkipOptionalTag(CBS_ASN1_TAG tag, bool *present) {
Input out;
return ReadOptionalTag(tag, &out, present);
}
bool Parser::ReadTag(CBS_ASN1_TAG tag, Input *out) {
CBS_ASN1_TAG actual_tag;
Input value;
if (!PeekTagAndValue(&actual_tag, &value) || actual_tag != tag) {
return false;
}
BSSL_CHECK(Advance());
*out = value;
return true;
}
bool Parser::SkipTag(CBS_ASN1_TAG tag) {
Input out;
return ReadTag(tag, &out);
}
// Type-specific variants of ReadTag
bool Parser::ReadConstructed(CBS_ASN1_TAG tag, Parser *out) {
if (!(tag & CBS_ASN1_CONSTRUCTED)) {
return false;
}
Input data;
if (!ReadTag(tag, &data)) {
return false;
}
*out = Parser(data);
return true;
}
bool Parser::ReadSequence(Parser *out) {
return ReadConstructed(CBS_ASN1_SEQUENCE, out);
}
bool Parser::ReadUint8(uint8_t *out) {
Input encoded_int;
if (!ReadTag(CBS_ASN1_INTEGER, &encoded_int)) {
return false;
}
return ParseUint8(encoded_int, out);
}
bool Parser::ReadUint64(uint64_t *out) {
Input encoded_int;
if (!ReadTag(CBS_ASN1_INTEGER, &encoded_int)) {
return false;
}
return ParseUint64(encoded_int, out);
}
std::optional<BitString> Parser::ReadBitString() {
Input value;
if (!ReadTag(CBS_ASN1_BITSTRING, &value)) {
return std::nullopt;
}
return ParseBitString(value);
}
bool Parser::ReadGeneralizedTime(GeneralizedTime *out) {
Input value;
if (!ReadTag(CBS_ASN1_GENERALIZEDTIME, &value)) {
return false;
}
return ParseGeneralizedTime(value, out);
}
} // namespace der
BSSL_NAMESPACE_END