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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
// 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 "net/der/parser.h"
#include "net/der/input.h"
#include "net/der/parse_values.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net::der::test {
TEST(ParserTest, ConsumesAllBytesOfTLV) {
const uint8_t der[] = {0x04 /* OCTET STRING */, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
ASSERT_EQ(kOctetString, tag);
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, CanReadRawTLV) {
const uint8_t der[] = {0x02, 0x01, 0x01};
Parser parser((Input(der)));
Input tlv;
ASSERT_TRUE(parser.ReadRawTLV(&tlv));
ByteReader tlv_reader(tlv);
size_t tlv_len = tlv_reader.BytesLeft();
ASSERT_EQ(3u, tlv_len);
Input tlv_data;
ASSERT_TRUE(tlv_reader.ReadBytes(tlv_len, &tlv_data));
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, IgnoresContentsOfInnerValues) {
// This is a SEQUENCE which has one member. The member is another SEQUENCE
// with an invalid encoding - its length is too long.
const uint8_t der[] = {0x30, 0x02, 0x30, 0x7e};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
}
TEST(ParserTest, FailsIfLengthOverlapsAnotherTLV) {
// This DER encoding has 2 top-level TLV tuples. The first is a SEQUENCE;
// the second is an INTEGER. The SEQUENCE contains an INTEGER, but its length
// is longer than what it has contents for.
const uint8_t der[] = {0x30, 0x02, 0x02, 0x01, 0x02, 0x01, 0x01};
Parser parser((Input(der)));
Parser inner_sequence;
ASSERT_TRUE(parser.ReadSequence(&inner_sequence));
uint64_t int_value;
ASSERT_TRUE(parser.ReadUint64(&int_value));
ASSERT_EQ(1u, int_value);
ASSERT_FALSE(parser.HasMore());
// Try to read the INTEGER from the SEQUENCE, which should fail.
Tag tag;
Input value;
ASSERT_FALSE(inner_sequence.ReadTagAndValue(&tag, &value));
}
TEST(ParserTest, ReadOptionalTagPresent) {
// DER encoding of 2 top-level TLV values:
// INTEGER { 1 }
// OCTET_STRING { `02` }
const uint8_t der[] = {0x02, 0x01, 0x01, 0x04, 0x01, 0x02};
Parser parser((Input(der)));
Input value;
bool present;
ASSERT_TRUE(parser.ReadOptionalTag(kInteger, &value, &present));
ASSERT_TRUE(present);
const uint8_t expected_int_value[] = {0x01};
ASSERT_EQ(Input(expected_int_value), value);
Tag tag;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
ASSERT_EQ(kOctetString, tag);
const uint8_t expected_octet_string_value[] = {0x02};
ASSERT_EQ(Input(expected_octet_string_value), value);
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, ReadOptionalTag2Present) {
// DER encoding of 2 top-level TLV values:
// INTEGER { 1 }
// OCTET_STRING { `02` }
const uint8_t der[] = {0x02, 0x01, 0x01, 0x04, 0x01, 0x02};
Parser parser((Input(der)));
absl::optional<Input> optional_value;
ASSERT_TRUE(parser.ReadOptionalTag(kInteger, &optional_value));
ASSERT_TRUE(optional_value.has_value());
const uint8_t expected_int_value[] = {0x01};
ASSERT_EQ(Input(expected_int_value), *optional_value);
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
ASSERT_EQ(kOctetString, tag);
const uint8_t expected_octet_string_value[] = {0x02};
ASSERT_EQ(Input(expected_octet_string_value), value);
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, ReadOptionalTagNotPresent) {
// DER encoding of 1 top-level TLV value:
// OCTET_STRING { `02` }
const uint8_t der[] = {0x04, 0x01, 0x02};
Parser parser((Input(der)));
Input value;
bool present;
ASSERT_TRUE(parser.ReadOptionalTag(kInteger, &value, &present));
ASSERT_FALSE(present);
Tag tag;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
ASSERT_EQ(kOctetString, tag);
const uint8_t expected_octet_string_value[] = {0x02};
ASSERT_EQ(Input(expected_octet_string_value), value);
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, ReadOptionalTag2NotPresent) {
// DER encoding of 1 top-level TLV value:
// OCTET_STRING { `02` }
const uint8_t der[] = {0x04, 0x01, 0x02};
Parser parser((Input(der)));
absl::optional<Input> optional_value;
ASSERT_TRUE(parser.ReadOptionalTag(kInteger, &optional_value));
ASSERT_FALSE(optional_value.has_value());
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
ASSERT_EQ(kOctetString, tag);
const uint8_t expected_octet_string_value[] = {0x02};
ASSERT_EQ(Input(expected_octet_string_value), value);
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, CanSkipOptionalTagAtEndOfInput) {
const uint8_t der[] = {0x02 /* INTEGER */, 0x01, 0x01};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
bool present;
ASSERT_TRUE(parser.ReadOptionalTag(kInteger, &value, &present));
ASSERT_FALSE(present);
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, SkipOptionalTagDoesntConsumePresentNonMatchingTLVs) {
const uint8_t der[] = {0x02 /* INTEGER */, 0x01, 0x01};
Parser parser((Input(der)));
bool present;
ASSERT_TRUE(parser.SkipOptionalTag(kOctetString, &present));
ASSERT_FALSE(present);
ASSERT_TRUE(parser.SkipOptionalTag(kInteger, &present));
ASSERT_TRUE(present);
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, TagNumbersAboveThirtySupported) {
// Context-specific class, tag number 31, length 0.
const uint8_t der[] = {0x9f, 0x1f, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
EXPECT_EQ(kTagContextSpecific | 31u, tag);
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, ParseTags) {
{
// Universal primitive tag, tag number 4.
const uint8_t der[] = {0x04, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
EXPECT_EQ(kOctetString, tag);
}
{
// Universal constructed tag, tag number 16.
const uint8_t der[] = {0x30, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
EXPECT_EQ(kSequence, tag);
}
{
// Application primitive tag, tag number 1.
const uint8_t der[] = {0x41, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
EXPECT_EQ(kTagApplication | 1, tag);
}
{
// Context-specific constructed tag, tag number 30.
const uint8_t der[] = {0xbe, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
EXPECT_EQ(kTagContextSpecific | kTagConstructed | 30, tag);
}
{
// Private primitive tag, tag number 15.
const uint8_t der[] = {0xcf, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_TRUE(parser.ReadTagAndValue(&tag, &value));
EXPECT_EQ(kTagPrivate | 15, tag);
}
}
TEST(ParserTest, IncompleteEncodingTagOnly) {
const uint8_t der[] = {0x01};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_FALSE(parser.ReadTagAndValue(&tag, &value));
ASSERT_TRUE(parser.HasMore());
}
TEST(ParserTest, IncompleteEncodingLengthTruncated) {
// Tag: octet string; length: long form, should have 2 total octets, but
// the last one is missing. (There's also no value.)
const uint8_t der[] = {0x04, 0x81};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_FALSE(parser.ReadTagAndValue(&tag, &value));
ASSERT_TRUE(parser.HasMore());
}
TEST(ParserTest, IncompleteEncodingValueShorterThanLength) {
// Tag: octet string; length: 2; value: first octet 'T', second octet missing.
const uint8_t der[] = {0x04, 0x02, 0x84};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_FALSE(parser.ReadTagAndValue(&tag, &value));
ASSERT_TRUE(parser.HasMore());
}
TEST(ParserTest, LengthMustBeEncodedWithMinimumNumberOfOctets) {
const uint8_t der[] = {0x01, 0x81, 0x01, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_FALSE(parser.ReadTagAndValue(&tag, &value));
ASSERT_TRUE(parser.HasMore());
}
TEST(ParserTest, LengthMustNotHaveLeadingZeroes) {
// Tag: octet string; length: 3 bytes of length encoding a value of 128
// (it should be encoded in only 2 bytes). Value: 128 bytes of 0.
const uint8_t der[] = {
0x04, 0x83, 0x80, 0x81, 0x80, // group the 0s separately
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Parser parser((Input(der)));
Tag tag;
Input value;
ASSERT_FALSE(parser.ReadTagAndValue(&tag, &value));
ASSERT_TRUE(parser.HasMore());
}
TEST(ParserTest, ReadConstructedFailsForNonConstructedTags) {
// Tag number is for SEQUENCE, but the constructed bit isn't set.
const uint8_t der[] = {0x10, 0x00};
Parser parser((Input(der)));
Tag expected_tag = 0x10;
Parser sequence_parser;
ASSERT_FALSE(parser.ReadConstructed(expected_tag, &sequence_parser));
// Check that we didn't fail above because of a tag mismatch or an improperly
// encoded TLV.
Input value;
ASSERT_TRUE(parser.ReadTag(expected_tag, &value));
ASSERT_FALSE(parser.HasMore());
}
TEST(ParserTest, CannotAdvanceAfterReadOptionalTag) {
const uint8_t der[] = {0x02, 0x01, 0x01};
Parser parser((Input(der)));
Input value;
bool present;
ASSERT_TRUE(parser.ReadOptionalTag(0x04, &value, &present));
ASSERT_FALSE(present);
ASSERT_FALSE(parser.Advance());
}
// Reads a valid BIT STRING with 1 unused bit.
TEST(ParserTest, ReadBitString) {
const uint8_t der[] = {0x03, 0x03, 0x01, 0xAA, 0xBE};
Parser parser((Input(der)));
absl::optional<BitString> bit_string = parser.ReadBitString();
ASSERT_TRUE(bit_string.has_value());
EXPECT_FALSE(parser.HasMore());
EXPECT_EQ(1u, bit_string->unused_bits());
ASSERT_EQ(2u, bit_string->bytes().Length());
EXPECT_EQ(0xAA, bit_string->bytes()[0]);
EXPECT_EQ(0xBE, bit_string->bytes()[1]);
}
// Tries reading a BIT STRING. This should fail because the tag is not for a
// BIT STRING.
TEST(ParserTest, ReadBitStringBadTag) {
const uint8_t der[] = {0x05, 0x03, 0x01, 0xAA, 0xBE};
Parser parser((Input(der)));
absl::optional<BitString> bit_string = parser.ReadBitString();
EXPECT_FALSE(bit_string.has_value());
}
} // namespace net::der::test
|