| 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 <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <vector> |
| 20 |
| 21 | #include <gtest/gtest.h> |
| 22 |
| 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/crypto.h> |
| 25 | #include <openssl/span.h> |
| 26 |
| 27 | #include "../internal.h" |
| 28 | #include "../test/test_util.h" |
| 29 | #include "internal.h" |
| 30 |
| 31 |
| 32 | namespace { |
| 33 |
| 34 | TEST(CBSTest, Skip) { |
| 35 | static const uint8_t kData[] = {1, 2, 3}; |
| 36 | CBS data; |
| 37 |
| 38 | CBS_init(&data, kData, sizeof(kData)); |
| 39 | EXPECT_EQ(3u, CBS_len(&data)); |
| 40 | EXPECT_TRUE(CBS_skip(&data, 1)); |
| 41 | EXPECT_EQ(2u, CBS_len(&data)); |
| 42 | EXPECT_TRUE(CBS_skip(&data, 2)); |
| 43 | EXPECT_EQ(0u, CBS_len(&data)); |
| 44 | EXPECT_FALSE(CBS_skip(&data, 1)); |
| 45 | } |
| 46 |
| 47 | TEST(CBSTest, GetUint) { |
| 48 | static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 49 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; |
| 50 | uint8_t u8; |
| 51 | uint16_t u16; |
| 52 | uint32_t u32; |
| 53 | uint64_t u64; |
| 54 | CBS data; |
| 55 |
| 56 | CBS_init(&data, kData, sizeof(kData)); |
| 57 | ASSERT_TRUE(CBS_get_u8(&data, &u8)); |
| 58 | EXPECT_EQ(1u, u8); |
| 59 | ASSERT_TRUE(CBS_get_u16(&data, &u16)); |
| 60 | EXPECT_EQ(0x203u, u16); |
| 61 | ASSERT_TRUE(CBS_get_u24(&data, &u32)); |
| 62 | EXPECT_EQ(0x40506u, u32); |
| 63 | ASSERT_TRUE(CBS_get_u32(&data, &u32)); |
| 64 | EXPECT_EQ(0x708090au, u32); |
| 65 | ASSERT_TRUE(CBS_get_u64(&data, &u64)); |
| 66 | EXPECT_EQ(0xb0c0d0e0f101112u, u64); |
| 67 | ASSERT_TRUE(CBS_get_last_u8(&data, &u8)); |
| 68 | EXPECT_EQ(0x14u, u8); |
| 69 | ASSERT_TRUE(CBS_get_last_u8(&data, &u8)); |
| 70 | EXPECT_EQ(0x13u, u8); |
| 71 | EXPECT_FALSE(CBS_get_u8(&data, &u8)); |
| 72 | EXPECT_FALSE(CBS_get_last_u8(&data, &u8)); |
| 73 |
| 74 | CBS_init(&data, kData, sizeof(kData)); |
| 75 | ASSERT_TRUE(CBS_get_u16le(&data, &u16)); |
| 76 | EXPECT_EQ(0x0201u, u16); |
| 77 | ASSERT_TRUE(CBS_get_u32le(&data, &u32)); |
| 78 | EXPECT_EQ(0x06050403u, u32); |
| 79 | ASSERT_TRUE(CBS_get_u64le(&data, &u64)); |
| 80 | EXPECT_EQ(0x0e0d0c0b0a090807u, u64); |
| 81 | } |
| 82 |
| 83 | TEST(CBSTest, GetPrefixed) { |
| 84 | static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1}; |
| 85 | uint8_t u8; |
| 86 | uint16_t u16; |
| 87 | uint32_t u32; |
| 88 | CBS data, prefixed; |
| 89 |
| 90 | CBS_init(&data, kData, sizeof(kData)); |
| 91 | ASSERT_TRUE(CBS_get_u8_length_prefixed(&data, &prefixed)); |
| 92 | EXPECT_EQ(1u, CBS_len(&prefixed)); |
| 93 | ASSERT_TRUE(CBS_get_u8(&prefixed, &u8)); |
| 94 | EXPECT_EQ(2u, u8); |
| 95 | ASSERT_TRUE(CBS_get_u16_length_prefixed(&data, &prefixed)); |
| 96 | EXPECT_EQ(2u, CBS_len(&prefixed)); |
| 97 | ASSERT_TRUE(CBS_get_u16(&prefixed, &u16)); |
| 98 | EXPECT_EQ(0x304u, u16); |
| 99 | ASSERT_TRUE(CBS_get_u24_length_prefixed(&data, &prefixed)); |
| 100 | EXPECT_EQ(3u, CBS_len(&prefixed)); |
| 101 | ASSERT_TRUE(CBS_get_u24(&prefixed, &u32)); |
| 102 | EXPECT_EQ(0x30201u, u32); |
| 103 | } |
| 104 |
| 105 | TEST(CBSTest, GetPrefixedBad) { |
| 106 | static const uint8_t kData1[] = {2, 1}; |
| 107 | static const uint8_t kData2[] = {0, 2, 1}; |
| 108 | static const uint8_t kData3[] = {0, 0, 2, 1}; |
| 109 | CBS data, prefixed; |
| 110 |
| 111 | CBS_init(&data, kData1, sizeof(kData1)); |
| 112 | EXPECT_FALSE(CBS_get_u8_length_prefixed(&data, &prefixed)); |
| 113 |
| 114 | CBS_init(&data, kData2, sizeof(kData2)); |
| 115 | EXPECT_FALSE(CBS_get_u16_length_prefixed(&data, &prefixed)); |
| 116 |
| 117 | CBS_init(&data, kData3, sizeof(kData3)); |
| 118 | EXPECT_FALSE(CBS_get_u24_length_prefixed(&data, &prefixed)); |
| 119 | } |
| 120 |
| 121 | TEST(CBSTest, GetUntilFirst) { |
| 122 | static const uint8_t kData[] = {0, 1, 2, 3, 0, 1, 2, 3}; |
| 123 | CBS data; |
| 124 | CBS_init(&data, kData, sizeof(kData)); |
| 125 |
| 126 | CBS prefix; |
| 127 | EXPECT_FALSE(CBS_get_until_first(&data, &prefix, 4)); |
| 128 | EXPECT_EQ(CBS_data(&data), kData); |
| 129 | EXPECT_EQ(CBS_len(&data), sizeof(kData)); |
| 130 |
| 131 | ASSERT_TRUE(CBS_get_until_first(&data, &prefix, 0)); |
| 132 | EXPECT_EQ(CBS_len(&prefix), 0u); |
| 133 | EXPECT_EQ(CBS_data(&data), kData); |
| 134 | EXPECT_EQ(CBS_len(&data), sizeof(kData)); |
| 135 |
| 136 | ASSERT_TRUE(CBS_get_until_first(&data, &prefix, 2)); |
| 137 | EXPECT_EQ(CBS_data(&prefix), kData); |
| 138 | EXPECT_EQ(CBS_len(&prefix), 2u); |
| 139 | EXPECT_EQ(CBS_data(&data), kData + 2); |
| 140 | EXPECT_EQ(CBS_len(&data), sizeof(kData) - 2); |
| 141 | } |
| 142 |
| 143 | TEST(CBSTest, GetASN1) { |
| 144 | static const uint8_t kData1[] = {0x30, 2, 1, 2}; |
| 145 | static const uint8_t kData2[] = {0x30, 3, 1, 2}; |
| 146 | static const uint8_t kData3[] = {0x30, 0x80}; |
| 147 | static const uint8_t kData4[] = {0x30, 0x81, 1, 1}; |
| 148 | static const uint8_t kData5[4 + 0x80] = {0x30, 0x82, 0, 0x80}; |
| 149 | static const uint8_t kData6[] = {0xa1, 3, 0x4, 1, 1}; |
| 150 | static const uint8_t kData7[] = {0xa1, 3, 0x4, 2, 1}; |
| 151 | static const uint8_t kData8[] = {0xa1, 3, 0x2, 1, 1}; |
| 152 | static const uint8_t kData9[] = {0xa1, 3, 0x2, 1, 0xff}; |
| 153 |
| 154 | CBS data, contents; |
| 155 | int present; |
| 156 | uint64_t value; |
| 157 |
| 158 | CBS_init(&data, kData1, sizeof(kData1)); |
| 159 | EXPECT_FALSE(CBS_peek_asn1_tag(&data, CBS_ASN1_BOOLEAN)); |
| 160 | EXPECT_TRUE(CBS_peek_asn1_tag(&data, CBS_ASN1_SEQUENCE)); |
| 161 |
| 162 | ASSERT_TRUE(CBS_get_asn1(&data, &contents, CBS_ASN1_SEQUENCE)); |
| 163 | EXPECT_EQ(Bytes("\x01\x02"), Bytes(CBS_data(&contents), CBS_len(&contents))); |
| 164 |
| 165 | CBS_init(&data, kData2, sizeof(kData2)); |
| 166 | // data is truncated |
| 167 | EXPECT_FALSE(CBS_get_asn1(&data, &contents, CBS_ASN1_SEQUENCE)); |
| 168 |
| 169 | CBS_init(&data, kData3, sizeof(kData3)); |
| 170 | // zero byte length of length |
| 171 | EXPECT_FALSE(CBS_get_asn1(&data, &contents, CBS_ASN1_SEQUENCE)); |
| 172 |
| 173 | CBS_init(&data, kData4, sizeof(kData4)); |
| 174 | // long form mistakenly used. |
| 175 | EXPECT_FALSE(CBS_get_asn1(&data, &contents, CBS_ASN1_SEQUENCE)); |
| 176 |
| 177 | CBS_init(&data, kData5, sizeof(kData5)); |
| 178 | // length takes too many bytes. |
| 179 | EXPECT_FALSE(CBS_get_asn1(&data, &contents, CBS_ASN1_SEQUENCE)); |
| 180 |
| 181 | CBS_init(&data, kData1, sizeof(kData1)); |
| 182 | // wrong tag. |
| 183 | EXPECT_FALSE(CBS_get_asn1(&data, &contents, 0x31)); |
| 184 |
| 185 | CBS_init(&data, NULL, 0); |
| 186 | // peek at empty data. |
| 187 | EXPECT_FALSE(CBS_peek_asn1_tag(&data, CBS_ASN1_SEQUENCE)); |
| 188 |
| 189 | CBS_init(&data, NULL, 0); |
| 190 | // optional elements at empty data. |
| 191 | ASSERT_TRUE(CBS_get_optional_asn1( |
| 192 | &data, &contents, &present, |
| 193 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)); |
| 194 | EXPECT_FALSE(present); |
| 195 | ASSERT_TRUE(CBS_get_optional_asn1_octet_string( |
| 196 | &data, &contents, &present, |
| 197 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)); |
| 198 | EXPECT_FALSE(present); |
| 199 | EXPECT_EQ(0u, CBS_len(&contents)); |
| 200 | ASSERT_TRUE(CBS_get_optional_asn1_octet_string( |
| 201 | &data, &contents, NULL, |
| 202 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)); |
| 203 | EXPECT_EQ(0u, CBS_len(&contents)); |
| 204 | ASSERT_TRUE(CBS_get_optional_asn1_uint64( |
| 205 | &data, &value, CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, 42)); |
| 206 | EXPECT_EQ(42u, value); |
| 207 |
| 208 | CBS_init(&data, kData6, sizeof(kData6)); |
| 209 | // optional element. |
| 210 | ASSERT_TRUE(CBS_get_optional_asn1( |
| 211 | &data, &contents, &present, |
| 212 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)); |
| 213 | EXPECT_FALSE(present); |
| 214 | ASSERT_TRUE(CBS_get_optional_asn1( |
| 215 | &data, &contents, &present, |
| 216 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)); |
| 217 | EXPECT_TRUE(present); |
| 218 | EXPECT_EQ(Bytes("\x04\x01\x01"), |
| 219 | Bytes(CBS_data(&contents), CBS_len(&contents))); |
| 220 |
| 221 | CBS_init(&data, kData6, sizeof(kData6)); |
| 222 | // optional octet string. |
| 223 | ASSERT_TRUE(CBS_get_optional_asn1_octet_string( |
| 224 | &data, &contents, &present, |
| 225 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)); |
| 226 | EXPECT_FALSE(present); |
| 227 | EXPECT_EQ(0u, CBS_len(&contents)); |
| 228 | ASSERT_TRUE(CBS_get_optional_asn1_octet_string( |
| 229 | &data, &contents, &present, |
| 230 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)); |
| 231 | EXPECT_TRUE(present); |
| 232 | EXPECT_EQ(Bytes("\x01"), Bytes(CBS_data(&contents), CBS_len(&contents))); |
| 233 |
| 234 | CBS_init(&data, kData7, sizeof(kData7)); |
| 235 | // invalid optional octet string. |
| 236 | EXPECT_FALSE(CBS_get_optional_asn1_octet_string( |
| 237 | &data, &contents, &present, |
| 238 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)); |
| 239 |
| 240 | CBS_init(&data, kData8, sizeof(kData8)); |
| 241 | // optional integer. |
| 242 | ASSERT_TRUE(CBS_get_optional_asn1_uint64( |
| 243 | &data, &value, CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, 42)); |
| 244 | EXPECT_EQ(42u, value); |
| 245 | ASSERT_TRUE(CBS_get_optional_asn1_uint64( |
| 246 | &data, &value, CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1, 42)); |
| 247 | EXPECT_EQ(1u, value); |
| 248 |
| 249 | CBS_init(&data, kData9, sizeof(kData9)); |
| 250 | // invalid optional integer. |
| 251 | EXPECT_FALSE(CBS_get_optional_asn1_uint64( |
| 252 | &data, &value, CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1, 42)); |
| 253 |
| 254 | CBS_ASN1_TAG tag; |
| 255 | CBS_init(&data, kData1, sizeof(kData1)); |
| 256 | ASSERT_TRUE(CBS_get_any_asn1(&data, &contents, &tag)); |
| 257 | EXPECT_EQ(CBS_ASN1_SEQUENCE, tag); |
| 258 | EXPECT_EQ(Bytes("\x01\x02"), Bytes(CBS_data(&contents), CBS_len(&contents))); |
| 259 |
| 260 | CBS_init(&data, kData1, sizeof(kData1)); |
| 261 | // We should be able to ignore the contents and get the tag. |
| 262 | ASSERT_TRUE(CBS_get_any_asn1(&data, NULL, &tag)); |
| 263 | EXPECT_EQ(CBS_ASN1_SEQUENCE, tag); |
| 264 | // We should be able to ignore the tag and get the contents. |
| 265 | CBS_init(&data, kData1, sizeof(kData1)); |
| 266 | ASSERT_TRUE(CBS_get_any_asn1(&data, &contents, NULL)); |
| 267 | EXPECT_EQ(Bytes("\x01\x02"), Bytes(CBS_data(&contents), CBS_len(&contents))); |
| 268 | // We should be able to ignore both the tag and contents. |
| 269 | CBS_init(&data, kData1, sizeof(kData1)); |
| 270 | ASSERT_TRUE(CBS_get_any_asn1(&data, NULL, NULL)); |
| 271 |
| 272 | size_t header_len; |
| 273 | CBS_init(&data, kData1, sizeof(kData1)); |
| 274 | ASSERT_TRUE(CBS_get_any_asn1_element(&data, &contents, &tag, &header_len)); |
| 275 | EXPECT_EQ(CBS_ASN1_SEQUENCE, tag); |
| 276 | EXPECT_EQ(2u, header_len); |
| 277 | EXPECT_EQ(Bytes("\x30\x02\x01\x02"), |
| 278 | Bytes(CBS_data(&contents), CBS_len(&contents))); |
| 279 | } |
| 280 |
| 281 | TEST(CBSTest, ParseASN1Tag) { |
| 282 | const struct { |
| 283 | bool ok; |
| 284 | CBS_ASN1_TAG tag; |
| 285 | std::vector<uint8_t> in; |
| 286 | } kTests[] = { |
| 287 | {true, CBS_ASN1_SEQUENCE, {0x30, 0}}, |
| 288 | {true, CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 4, {0xa4, 0}}, |
| 289 | {true, CBS_ASN1_APPLICATION | 30, {0x5e, 0}}, |
| 290 | {true, CBS_ASN1_APPLICATION | 31, {0x5f, 0x1f, 0}}, |
| 291 | {true, CBS_ASN1_APPLICATION | 32, {0x5f, 0x20, 0}}, |
| 292 | {true, |
| 293 | CBS_ASN1_PRIVATE | CBS_ASN1_CONSTRUCTED | 0x1fffffff, |
| 294 | {0xff, 0x81, 0xff, 0xff, 0xff, 0x7f, 0}}, |
| 295 | // Tag number fits in |uint32_t| but not |CBS_ASN1_TAG_NUMBER_MASK|. |
| 296 | {false, 0, {0xff, 0x82, 0xff, 0xff, 0xff, 0x7f, 0}}, |
| 297 | // Tag number does not fit in |uint32_t|. |
| 298 | {false, 0, {0xff, 0x90, 0x80, 0x80, 0x80, 0, 0}}, |
| 299 | // Tag number is not minimally-encoded |
| 300 | {false, 0, {0x5f, 0x80, 0x1f, 0}}, |
| 301 | // Tag number should have used short form. |
| 302 | {false, 0, {0x5f, 0x80, 0x1e, 0}}, |
| 303 | }; |
| 304 | for (const auto &t : kTests) { |
| 305 | SCOPED_TRACE(Bytes(t.in)); |
| 306 | CBS_ASN1_TAG tag; |
| 307 | CBS cbs, child; |
| 308 | CBS_init(&cbs, t.in.data(), t.in.size()); |
| 309 | ASSERT_EQ(t.ok, !!CBS_get_any_asn1(&cbs, &child, &tag)); |
| 310 | if (t.ok) { |
| 311 | EXPECT_EQ(t.tag, tag); |
| 312 | EXPECT_EQ(0u, CBS_len(&child)); |
| 313 | EXPECT_EQ(0u, CBS_len(&cbs)); |
| 314 |
| 315 | CBS_init(&cbs, t.in.data(), t.in.size()); |
| 316 | EXPECT_TRUE(CBS_peek_asn1_tag(&cbs, t.tag)); |
| 317 | EXPECT_FALSE(CBS_peek_asn1_tag(&cbs, t.tag + 1)); |
| 318 |
| 319 | EXPECT_TRUE(CBS_get_asn1(&cbs, &child, t.tag)); |
| 320 | EXPECT_EQ(0u, CBS_len(&child)); |
| 321 | EXPECT_EQ(0u, CBS_len(&cbs)); |
| 322 |
| 323 | CBS_init(&cbs, t.in.data(), t.in.size()); |
| 324 | EXPECT_FALSE(CBS_get_asn1(&cbs, &child, t.tag + 1)); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 |
| 329 | TEST(CBSTest, GetOptionalASN1Bool) { |
| 330 | static const uint8_t kTrue[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0xff}; |
| 331 | static const uint8_t kFalse[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x00}; |
| 332 | static const uint8_t kInvalid[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x01}; |
| 333 |
| 334 | CBS data; |
| 335 | CBS_init(&data, NULL, 0); |
| 336 | int val = 2; |
| 337 | ASSERT_TRUE(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0)); |
| 338 | EXPECT_EQ(0, val); |
| 339 |
| 340 | CBS_init(&data, kTrue, sizeof(kTrue)); |
| 341 | val = 2; |
| 342 | ASSERT_TRUE(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0)); |
| 343 | EXPECT_EQ(1, val); |
| 344 |
| 345 | CBS_init(&data, kFalse, sizeof(kFalse)); |
| 346 | val = 2; |
| 347 | ASSERT_TRUE(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1)); |
| 348 | EXPECT_EQ(0, val); |
| 349 |
| 350 | CBS_init(&data, kInvalid, sizeof(kInvalid)); |
| 351 | EXPECT_FALSE(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1)); |
| 352 | } |
| 353 |
| 354 | // Test that CBB_init may be used on an uninitialized input. |
| 355 | TEST(CBBTest, InitUninitialized) { |
| 356 | CBB cbb; |
| 357 | ASSERT_TRUE(CBB_init(&cbb, 100)); |
| 358 | CBB_cleanup(&cbb); |
| 359 | } |
| 360 |
| 361 | TEST(CBBTest, Basic) { |
| 362 | static const uint8_t kExpected[] = { |
| 363 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, |
| 364 | 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, |
| 365 | 0x03, 0x02, 0x0a, 0x09, 0x08, 0x07, 0x12, 0x11, 0x10, 0x0f, |
| 366 | 0x0e, 0x0d, 0x0c, 0x0b, 0x00, 0x00, 0x00, 0x00}; |
| 367 | uint8_t *buf; |
| 368 | size_t buf_len; |
| 369 |
| 370 | bssl::ScopedCBB cbb; |
| 371 | ASSERT_TRUE(CBB_init(cbb.get(), 100)); |
| 372 | cbb.Reset(); |
| 373 |
| 374 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 375 | ASSERT_TRUE(CBB_add_zeros(cbb.get(), 0)); |
| 376 | ASSERT_TRUE(CBB_add_u8(cbb.get(), 1)); |
| 377 | ASSERT_TRUE(CBB_add_u16(cbb.get(), 0x203)); |
| 378 | ASSERT_TRUE(CBB_add_u24(cbb.get(), 0x40506)); |
| 379 | ASSERT_TRUE(CBB_add_u32(cbb.get(), 0x708090a)); |
| 380 | ASSERT_TRUE(CBB_add_u64(cbb.get(), 0xb0c0d0e0f101112)); |
| 381 | ASSERT_TRUE(CBB_add_bytes(cbb.get(), (const uint8_t *)"\x13\x14", 2)); |
| 382 | ASSERT_TRUE(CBB_add_u16le(cbb.get(), 0x203)); |
| 383 | ASSERT_TRUE(CBB_add_u32le(cbb.get(), 0x708090a)); |
| 384 | ASSERT_TRUE(CBB_add_u64le(cbb.get(), 0xb0c0d0e0f101112)); |
| 385 | ASSERT_TRUE(CBB_add_zeros(cbb.get(), 4)); |
| 386 | ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len)); |
| 387 |
| 388 | bssl::UniquePtr<uint8_t> scoper(buf); |
| 389 | EXPECT_EQ(Bytes(kExpected), Bytes(buf, buf_len)); |
| 390 | } |
| 391 |
| 392 | TEST(CBBTest, Fixed) { |
| 393 | CBB cbb; |
| 394 | uint8_t buf[1]; |
| 395 | uint8_t *out_buf; |
| 396 | size_t out_size; |
| 397 |
| 398 | ASSERT_TRUE(CBB_init_fixed(&cbb, NULL, 0)); |
| 399 | ASSERT_TRUE(CBB_finish(&cbb, &out_buf, &out_size)); |
| 400 | EXPECT_EQ(NULL, out_buf); |
| 401 | EXPECT_EQ(0u, out_size); |
| 402 |
| 403 | ASSERT_TRUE(CBB_init_fixed(&cbb, buf, 1)); |
| 404 | ASSERT_TRUE(CBB_add_u8(&cbb, 1)); |
| 405 | ASSERT_TRUE(CBB_finish(&cbb, &out_buf, &out_size)); |
| 406 | EXPECT_EQ(buf, out_buf); |
| 407 | EXPECT_EQ(1u, out_size); |
| 408 | EXPECT_EQ(1u, buf[0]); |
| 409 |
| 410 | ASSERT_TRUE(CBB_init_fixed(&cbb, buf, 1)); |
| 411 | ASSERT_TRUE(CBB_add_u8(&cbb, 1)); |
| 412 | EXPECT_FALSE(CBB_add_u8(&cbb, 2)); |
| 413 | // We do not need |CBB_cleanup| or |bssl::ScopedCBB| here because a fixed |
| 414 | // |CBB| has no allocations. Leak-checking tools will confirm there was |
| 415 | // nothing to clean up. |
| 416 |
| 417 | // However, it should be harmless to call |CBB_cleanup|. |
| 418 | CBB cbb2; |
| 419 | ASSERT_TRUE(CBB_init_fixed(&cbb2, buf, 1)); |
| 420 | ASSERT_TRUE(CBB_add_u8(&cbb2, 1)); |
| 421 | EXPECT_FALSE(CBB_add_u8(&cbb2, 2)); |
| 422 | CBB_cleanup(&cbb2); |
| 423 | } |
| 424 |
| 425 | // Test that calling CBB_finish on a child does nothing. |
| 426 | TEST(CBBTest, FinishChild) { |
| 427 | CBB child; |
| 428 | uint8_t *out_buf; |
| 429 | size_t out_size; |
| 430 |
| 431 | bssl::ScopedCBB cbb; |
| 432 | ASSERT_TRUE(CBB_init(cbb.get(), 16)); |
| 433 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &child)); |
| 434 |
| 435 | EXPECT_FALSE(CBB_finish(&child, &out_buf, &out_size)); |
| 436 |
| 437 | ASSERT_TRUE(CBB_finish(cbb.get(), &out_buf, &out_size)); |
| 438 | bssl::UniquePtr<uint8_t> scoper(out_buf); |
| 439 | ASSERT_EQ(1u, out_size); |
| 440 | EXPECT_EQ(0u, out_buf[0]); |
| 441 | } |
| 442 |
| 443 | TEST(CBBTest, Prefixed) { |
| 444 | static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3, |
| 445 | 4, 5, 6, 5, 4, 1, 0, 1, 2}; |
| 446 | uint8_t *buf; |
| 447 | size_t buf_len; |
| 448 | bssl::ScopedCBB cbb; |
| 449 | CBB contents, inner_contents, inner_inner_contents; |
| 450 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 451 | EXPECT_EQ(0u, CBB_len(cbb.get())); |
| 452 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &contents)); |
| 453 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &contents)); |
| 454 | ASSERT_TRUE(CBB_add_u8(&contents, 1)); |
| 455 | EXPECT_EQ(1u, CBB_len(&contents)); |
| 456 | ASSERT_TRUE(CBB_flush(cbb.get())); |
| 457 | EXPECT_EQ(3u, CBB_len(cbb.get())); |
| 458 | ASSERT_TRUE(CBB_add_u16_length_prefixed(cbb.get(), &contents)); |
| 459 | ASSERT_TRUE(CBB_add_u16(&contents, 0x203)); |
| 460 | ASSERT_TRUE(CBB_add_u24_length_prefixed(cbb.get(), &contents)); |
| 461 | ASSERT_TRUE(CBB_add_u24(&contents, 0x40506)); |
| 462 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &contents)); |
| 463 | ASSERT_TRUE(CBB_add_u8_length_prefixed(&contents, &inner_contents)); |
| 464 | ASSERT_TRUE(CBB_add_u8(&inner_contents, 1)); |
| 465 | ASSERT_TRUE( |
| 466 | CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents)); |
| 467 | ASSERT_TRUE(CBB_add_u8(&inner_inner_contents, 2)); |
| 468 | ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len)); |
| 469 |
| 470 | bssl::UniquePtr<uint8_t> scoper(buf); |
| 471 | EXPECT_EQ(Bytes(kExpected), Bytes(buf, buf_len)); |
| 472 | } |
| 473 |
| 474 | TEST(CBBTest, DiscardChild) { |
| 475 | bssl::ScopedCBB cbb; |
| 476 | CBB contents, inner_contents, inner_inner_contents; |
| 477 |
| 478 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 479 | ASSERT_TRUE(CBB_add_u8(cbb.get(), 0xaa)); |
| 480 |
| 481 | // Discarding |cbb|'s children preserves the byte written. |
| 482 | CBB_discard_child(cbb.get()); |
| 483 |
| 484 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &contents)); |
| 485 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &contents)); |
| 486 | ASSERT_TRUE(CBB_add_u8(&contents, 0xbb)); |
| 487 | ASSERT_TRUE(CBB_add_u16_length_prefixed(cbb.get(), &contents)); |
| 488 | ASSERT_TRUE(CBB_add_u16(&contents, 0xcccc)); |
| 489 | ASSERT_TRUE(CBB_add_u24_length_prefixed(cbb.get(), &contents)); |
| 490 | ASSERT_TRUE(CBB_add_u24(&contents, 0xdddddd)); |
| 491 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &contents)); |
| 492 | ASSERT_TRUE(CBB_add_u8(&contents, 0xff)); |
| 493 | ASSERT_TRUE(CBB_add_u8_length_prefixed(&contents, &inner_contents)); |
| 494 | ASSERT_TRUE(CBB_add_u8(&inner_contents, 0x42)); |
| 495 | ASSERT_TRUE( |
| 496 | CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents)); |
| 497 | ASSERT_TRUE(CBB_add_u8(&inner_inner_contents, 0x99)); |
| 498 |
| 499 | // Discard everything from |inner_contents| down. |
| 500 | CBB_discard_child(&contents); |
| 501 |
| 502 | uint8_t *buf; |
| 503 | size_t buf_len; |
| 504 | ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len)); |
| 505 | bssl::UniquePtr<uint8_t> scoper(buf); |
| 506 |
| 507 | static const uint8_t kExpected[] = { |
| 508 | // clang-format off |
| 509 | 0xaa, |
| 510 | 0, |
| 511 | 1, 0xbb, |
| 512 | 0, 2, 0xcc, 0xcc, |
| 513 | 0, 0, 3, 0xdd, 0xdd, 0xdd, |
| 514 | 1, 0xff, |
| 515 | // clang-format on |
| 516 | }; |
| 517 | EXPECT_EQ(Bytes(kExpected), Bytes(buf, buf_len)); |
| 518 | } |
| 519 |
| 520 | TEST(CBBTest, Misuse) { |
| 521 | bssl::ScopedCBB cbb; |
| 522 | CBB child, contents; |
| 523 | uint8_t *buf; |
| 524 | size_t buf_len; |
| 525 |
| 526 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 527 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &child)); |
| 528 | ASSERT_TRUE(CBB_add_u8(&child, 1)); |
| 529 | ASSERT_TRUE(CBB_add_u8(cbb.get(), 2)); |
| 530 |
| 531 | // Since we wrote to |cbb|, |child| is now invalid and attempts to write to |
| 532 | // it should fail. |
| 533 | EXPECT_FALSE(CBB_add_u8(&child, 1)); |
| 534 | EXPECT_FALSE(CBB_add_u16(&child, 1)); |
| 535 | EXPECT_FALSE(CBB_add_u24(&child, 1)); |
| 536 | EXPECT_FALSE(CBB_add_u8_length_prefixed(&child, &contents)); |
| 537 | EXPECT_FALSE(CBB_add_u16_length_prefixed(&child, &contents)); |
| 538 | EXPECT_FALSE(CBB_add_asn1(&child, &contents, 1)); |
| 539 | EXPECT_FALSE(CBB_add_bytes(&child, (const uint8_t *)"a", 1)); |
| 540 |
| 541 | ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len)); |
| 542 | bssl::UniquePtr<uint8_t> scoper(buf); |
| 543 |
| 544 | EXPECT_EQ(Bytes("\x01\x01\x02"), Bytes(buf, buf_len)); |
| 545 | } |
| 546 |
| 547 | TEST(CBBTest, ASN1) { |
| 548 | static const uint8_t kExpected[] = { |
| 549 | // clang-format off |
| 550 | // SEQUENCE { 1 2 3 } |
| 551 | 0x30, 3, 1, 2, 3, |
| 552 | // [4 CONSTRUCTED] { 4 5 6 } |
| 553 | 0xa4, 3, 4, 5, 6, |
| 554 | // [APPLICATION 30 PRIMITIVE] { 7 8 9 } |
| 555 | 0x5e, 3, 7, 8, 9, |
| 556 | // [APPLICATION 31 PRIMITIVE] { 10 11 12 } |
| 557 | 0x5f, 0x1f, 3, 10, 11, 12, |
| 558 | // [PRIVATE 2^29-1 CONSTRUCTED] { 13 14 15 } |
| 559 | 0xff, 0x81, 0xff, 0xff, 0xff, 0x7f, 3, 13, 14, 15, |
| 560 | // clang-format on |
| 561 | }; |
| 562 | uint8_t *buf; |
| 563 | size_t buf_len; |
| 564 | bssl::ScopedCBB cbb; |
| 565 | CBB contents, inner_contents; |
| 566 |
| 567 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 568 | ASSERT_TRUE(CBB_add_asn1(cbb.get(), &contents, CBS_ASN1_SEQUENCE)); |
| 569 | ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x01\x02\x03", 3)); |
| 570 | ASSERT_TRUE( |
| 571 | CBB_add_asn1(cbb.get(), &contents, |
| 572 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 4)); |
| 573 | ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x04\x05\x06", 3)); |
| 574 | ASSERT_TRUE(CBB_add_asn1(cbb.get(), &contents, CBS_ASN1_APPLICATION | 30)); |
| 575 | ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x07\x08\x09", 3)); |
| 576 | ASSERT_TRUE(CBB_add_asn1(cbb.get(), &contents, CBS_ASN1_APPLICATION | 31)); |
| 577 | ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x0a\x0b\x0c", 3)); |
| 578 | ASSERT_TRUE( |
| 579 | CBB_add_asn1(cbb.get(), &contents, |
| 580 | CBS_ASN1_PRIVATE | CBS_ASN1_CONSTRUCTED | 0x1fffffff)); |
| 581 | ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x0d\x0e\x0f", 3)); |
| 582 | ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len)); |
| 583 | bssl::UniquePtr<uint8_t> scoper(buf); |
| 584 |
| 585 | EXPECT_EQ(Bytes(kExpected), Bytes(buf, buf_len)); |
| 586 |
| 587 | std::vector<uint8_t> test_data(100000, 0x42); |
| 588 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 589 | ASSERT_TRUE(CBB_add_asn1(cbb.get(), &contents, CBS_ASN1_SEQUENCE)); |
| 590 | ASSERT_TRUE(CBB_add_bytes(&contents, test_data.data(), 130)); |
| 591 | ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len)); |
| 592 | scoper.reset(buf); |
| 593 |
| 594 | ASSERT_EQ(3u + 130u, buf_len); |
| 595 | EXPECT_EQ(Bytes("\x30\x81\x82"), Bytes(buf, 3)); |
| 596 | EXPECT_EQ(Bytes(test_data.data(), 130), Bytes(buf + 3, 130)); |
| 597 |
| 598 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 599 | ASSERT_TRUE(CBB_add_asn1(cbb.get(), &contents, CBS_ASN1_SEQUENCE)); |
| 600 | ASSERT_TRUE(CBB_add_bytes(&contents, test_data.data(), 1000)); |
| 601 | ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len)); |
| 602 | scoper.reset(buf); |
| 603 |
| 604 | ASSERT_EQ(4u + 1000u, buf_len); |
| 605 | EXPECT_EQ(Bytes("\x30\x82\x03\xe8"), Bytes(buf, 4)); |
| 606 | EXPECT_EQ(Bytes(test_data.data(), 1000), Bytes(buf + 4, 1000)); |
| 607 |
| 608 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 609 | ASSERT_TRUE(CBB_add_asn1(cbb.get(), &contents, CBS_ASN1_SEQUENCE)); |
| 610 | ASSERT_TRUE(CBB_add_asn1(&contents, &inner_contents, CBS_ASN1_SEQUENCE)); |
| 611 | ASSERT_TRUE(CBB_add_bytes(&inner_contents, test_data.data(), 100000)); |
| 612 | ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len)); |
| 613 | scoper.reset(buf); |
| 614 |
| 615 | ASSERT_EQ(5u + 5u + 100000u, buf_len); |
| 616 | EXPECT_EQ(Bytes("\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0"), Bytes(buf, 10)); |
| 617 | EXPECT_EQ(Bytes(test_data.data(), test_data.size()), Bytes(buf + 10, 100000)); |
| 618 | } |
| 619 |
| 620 | static void ExpectBerConvert(const char *name, |
| 621 | bssl::Span<const uint8_t> der_expected, |
| 622 | bssl::Span<const uint8_t> ber) { |
| 623 | SCOPED_TRACE(name); |
| 624 | CBS in, out; |
| 625 | uint8_t *storage; |
| 626 |
| 627 | CBS_init(&in, ber.data(), ber.size()); |
| 628 | ASSERT_TRUE(CBS_asn1_ber_to_der(&in, &out, &storage)); |
| 629 | bssl::UniquePtr<uint8_t> scoper(storage); |
| 630 |
| 631 | EXPECT_EQ(Bytes(der_expected), Bytes(CBS_data(&out), CBS_len(&out))); |
| 632 | if (storage != nullptr) { |
| 633 | EXPECT_NE(Bytes(der_expected), Bytes(ber)); |
| 634 | } else { |
| 635 | EXPECT_EQ(Bytes(der_expected), Bytes(ber)); |
| 636 | } |
| 637 | } |
| 638 |
| 639 | TEST(CBSTest, BerConvert) { |
| 640 | static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00}; |
| 641 |
| 642 | // kNonMinimalLengthBER has a non-minimally encoded length. |
| 643 | static const uint8_t kNonMinimalLengthBER[] = {0x02, 0x82, 0x00, 0x01, 0x01}; |
| 644 | static const uint8_t kNonMinimalLengthDER[] = {0x02, 0x01, 0x01}; |
| 645 |
| 646 | // kIndefBER contains a SEQUENCE with an indefinite length. |
| 647 | static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00}; |
| 648 | static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02}; |
| 649 |
| 650 | // kIndefBER2 contains a constructed [APPLICATION 31] with an indefinite |
| 651 | // length. |
| 652 | static const uint8_t kIndefBER2[] = {0x7f, 0x1f, 0x80, 0x01, |
| 653 | 0x01, 0x02, 0x00, 0x00}; |
| 654 | static const uint8_t kIndefDER2[] = {0x7f, 0x1f, 0x03, 0x01, 0x01, 0x02}; |
| 655 |
| 656 | // kOctetStringBER contains an indefinite length OCTET STRING with two parts. |
| 657 | // These parts need to be concatenated in DER form. |
| 658 | static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0, 1, |
| 659 | 0x04, 0x02, 2, 3, 0x00, 0x00}; |
| 660 | static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3}; |
| 661 |
| 662 | // kNSSBER is part of a PKCS#12 message generated by NSS that uses indefinite |
| 663 | // length elements extensively. |
| 664 | static const uint8_t kNSSBER[] = { |
| 665 | 0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48, |
| 666 | 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04, |
| 667 | 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39, |
| 668 | 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, |
| 669 | 0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, |
| 670 | 0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, |
| 671 | 0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, |
| 672 | 0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00, |
| 673 | }; |
| 674 |
| 675 | static const uint8_t kNSSDER[] = { |
| 676 | 0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86, |
| 677 | 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04, |
| 678 | 0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06, |
| 679 | 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84, |
| 680 | 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8, |
| 681 | 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38, |
| 682 | 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0, |
| 683 | 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, |
| 684 | }; |
| 685 |
| 686 | // kConstructedStringBER contains a deeply-nested constructed OCTET STRING. |
| 687 | // The BER conversion collapses this to one level deep, but not completely. |
| 688 | static const uint8_t kConstructedStringBER[] = { |
| 689 | 0xa0, 0x10, 0x24, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, |
| 690 | 0x01, 0x24, 0x06, 0x04, 0x01, 0x02, 0x04, 0x01, 0x03, |
| 691 | }; |
| 692 | static const uint8_t kConstructedStringDER[] = { |
| 693 | 0xa0, 0x08, 0x04, 0x02, 0x00, 0x01, 0x04, 0x02, 0x02, 0x03, |
| 694 | }; |
| 695 |
| 696 | // kWrappedIndefBER contains indefinite-length SEQUENCE, wrapped |
| 697 | // and followed by valid DER. This tests that we correctly identify BER nested |
| 698 | // inside DER. |
| 699 | // |
| 700 | // SEQUENCE { |
| 701 | // SEQUENCE { |
| 702 | // SEQUENCE indefinite {} |
| 703 | // } |
| 704 | // SEQUENCE {} |
| 705 | // } |
| 706 | static const uint8_t kWrappedIndefBER[] = {0x30, 0x08, 0x30, 0x04, 0x30, |
| 707 | 0x80, 0x00, 0x00, 0x30, 0x00}; |
| 708 | static const uint8_t kWrappedIndefDER[] = {0x30, 0x06, 0x30, 0x02, |
| 709 | 0x30, 0x00, 0x30, 0x00}; |
| 710 |
| 711 | // kWrappedConstructedStringBER contains a constructed OCTET STRING, wrapped |
| 712 | // and followed by valid DER. This tests that we correctly identify BER nested |
| 713 | // inside DER. |
| 714 | // |
| 715 | // SEQUENCE { |
| 716 | // SEQUENCE { |
| 717 | // [OCTET_STRING CONSTRUCTED] { |
| 718 | // OCTET_STRING {} |
| 719 | // } |
| 720 | // } |
| 721 | // SEQUENCE {} |
| 722 | // } |
| 723 | static const uint8_t kWrappedConstructedStringBER[] = { |
| 724 | 0x30, 0x08, 0x30, 0x04, 0x24, 0x02, 0x04, 0x00, 0x30, 0x00}; |
| 725 | static const uint8_t kWrappedConstructedStringDER[] = { |
| 726 | 0x30, 0x06, 0x30, 0x02, 0x04, 0x00, 0x30, 0x00}; |
| 727 |
| 728 | // kConstructedBitString contains a BER constructed BIT STRING. These are not |
| 729 | // supported and thus are left unchanged. |
| 730 | static const uint8_t kConstructedBitStringBER[] = { |
| 731 | 0x23, 0x0a, 0x03, 0x03, 0x00, 0x12, 0x34, 0x03, 0x03, 0x00, 0x56, 0x78}; |
| 732 |
| 733 | ExpectBerConvert("kSimpleBER", kSimpleBER, kSimpleBER); |
| 734 | ExpectBerConvert("kNonMinimalLengthBER", kNonMinimalLengthDER, |
| 735 | kNonMinimalLengthBER); |
| 736 | ExpectBerConvert("kIndefBER", kIndefDER, kIndefBER); |
| 737 | ExpectBerConvert("kIndefBER2", kIndefDER2, kIndefBER2); |
| 738 | ExpectBerConvert("kOctetStringBER", kOctetStringDER, kOctetStringBER); |
| 739 | ExpectBerConvert("kNSSBER", kNSSDER, kNSSBER); |
| 740 | ExpectBerConvert("kConstructedStringBER", kConstructedStringDER, |
| 741 | kConstructedStringBER); |
| 742 | ExpectBerConvert("kConstructedBitStringBER", kConstructedBitStringBER, |
| 743 | kConstructedBitStringBER); |
| 744 | ExpectBerConvert("kWrappedIndefBER", kWrappedIndefDER, kWrappedIndefBER); |
| 745 | ExpectBerConvert("kWrappedConstructedStringBER", kWrappedConstructedStringDER, |
| 746 | kWrappedConstructedStringBER); |
| 747 |
| 748 | // indef_overflow is 200 levels deep of an indefinite-length-encoded SEQUENCE. |
| 749 | // This will exceed our recursion limits and fail to be converted. |
| 750 | std::vector<uint8_t> indef_overflow; |
| 751 | for (int i = 0; i < 200; i++) { |
| 752 | indef_overflow.push_back(0x30); |
| 753 | indef_overflow.push_back(0x80); |
| 754 | } |
| 755 | for (int i = 0; i < 200; i++) { |
| 756 | indef_overflow.push_back(0x00); |
| 757 | indef_overflow.push_back(0x00); |
| 758 | } |
| 759 | CBS in, out; |
| 760 | CBS_init(&in, indef_overflow.data(), indef_overflow.size()); |
| 761 | uint8_t *storage; |
| 762 | ASSERT_FALSE(CBS_asn1_ber_to_der(&in, &out, &storage)); |
| 763 | } |
| 764 |
| 765 | struct BERTest { |
| 766 | const char *in_hex; |
| 767 | bool ok; |
| 768 | bool ber_found; |
| 769 | bool indefinite; |
| 770 | CBS_ASN1_TAG tag; |
| 771 | }; |
| 772 |
| 773 | static const BERTest kBERTests[] = { |
| 774 | // Trivial cases, also valid DER. |
| 775 | {"0100", true, false, false, 1}, |
| 776 | {"020101", true, false, false, 2}, |
| 777 |
| 778 | // Non-minimally encoded lengths. |
| 779 | {"02810101", true, true, false, 2}, |
| 780 | {"0282000101", true, true, false, 2}, |
| 781 | {"028300000101", true, true, false, 2}, |
| 782 | {"02840000000101", true, true, false, 2}, |
| 783 | // Technically valid BER, but not handled. |
| 784 | {"02850000000101", false, false, false, 0}, |
| 785 |
| 786 | // Indefinite length, but not constructed. |
| 787 | {"0280", false, false, false, 0}, |
| 788 | // Indefinite length. |
| 789 | {"2280", true, true, true, CBS_ASN1_CONSTRUCTED | 2}, |
| 790 | // Indefinite length with multi-byte tag. |
| 791 | {"bf1f80", true, true, true, |
| 792 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 31}, |
| 793 | // Invalid extended tag zero (X.690 8.1.2.4.2.c) |
| 794 | {"3f0000", false, false, false, 0}, |
| 795 | // Should be a low-number tag form, even in BER. |
| 796 | {"1f0100", false, false, false, 0}, |
| 797 | {"1f4000", true, false, false, 0x40}, |
| 798 | // Non-minimal tags are invalid, even in BER. |
| 799 | {"1f804000", false, false, false, 0}, |
| 800 |
| 801 | // EOCs and other forms of tag [UNIVERSAL 0] are rejected as elements. |
| 802 | {"0000", false, false, false, 0}, |
| 803 | {"000100", false, false, false, 0}, |
| 804 | {"00800000", false, false, false, 0}, |
| 805 | {"2000", false, false, false, 0}, |
| 806 | }; |
| 807 |
| 808 | TEST(CBSTest, BERElementTest) { |
| 809 | for (const auto &test : kBERTests) { |
| 810 | SCOPED_TRACE(test.in_hex); |
| 811 |
| 812 | std::vector<uint8_t> in_bytes; |
| 813 | ASSERT_TRUE(DecodeHex(&in_bytes, test.in_hex)); |
| 814 | CBS in(in_bytes); |
| 815 | CBS out; |
| 816 | CBS_ASN1_TAG tag; |
| 817 | size_t header_len; |
| 818 | int ber_found; |
| 819 | int indefinite; |
| 820 | int ok = CBS_get_any_ber_asn1_element(&in, &out, &tag, &header_len, |
| 821 | &ber_found, &indefinite); |
| 822 | ASSERT_TRUE((ok == 1) == test.ok); |
| 823 | if (!test.ok) { |
| 824 | continue; |
| 825 | } |
| 826 |
| 827 | EXPECT_EQ(test.ber_found ? 1 : 0, ber_found); |
| 828 | EXPECT_EQ(test.indefinite ? 1 : 0, indefinite); |
| 829 | EXPECT_LE(header_len, in_bytes.size()); |
| 830 | EXPECT_EQ(CBS_len(&out), in_bytes.size()); |
| 831 | EXPECT_EQ(CBS_len(&in), 0u); |
| 832 | EXPECT_EQ(Bytes(out), Bytes(in_bytes)); |
| 833 | EXPECT_EQ(tag, test.tag); |
| 834 | } |
| 835 | } |
| 836 |
| 837 | struct ImplicitStringTest { |
| 838 | const char *in; |
| 839 | size_t in_len; |
| 840 | bool ok; |
| 841 | const char *out; |
| 842 | size_t out_len; |
| 843 | }; |
| 844 |
| 845 | static const ImplicitStringTest kImplicitStringTests[] = { |
| 846 | // A properly-encoded string. |
| 847 | {"\x80\x03\x61\x61\x61", 5, true, "aaa", 3}, |
| 848 | // An implicit-tagged string. |
| 849 | {"\xa0\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11, true, "aaa", 3}, |
| 850 | // |CBS_get_asn1_implicit_string| only accepts one level deep of nesting. |
| 851 | {"\xa0\x0b\x24\x06\x04\x01\x61\x04\x01\x61\x04\x01\x61", 13, false, nullptr, |
| 852 | 0}, |
| 853 | // The outer tag must match. |
| 854 | {"\x81\x03\x61\x61\x61", 5, false, nullptr, 0}, |
| 855 | {"\xa1\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11, false, nullptr, 0}, |
| 856 | // The inner tag must match. |
| 857 | {"\xa1\x09\x0c\x01\x61\x0c\x01\x61\x0c\x01\x61", 11, false, nullptr, 0}, |
| 858 | }; |
| 859 |
| 860 | TEST(CBSTest, ImplicitString) { |
| 861 | for (const auto &test : kImplicitStringTests) { |
| 862 | SCOPED_TRACE(Bytes(test.in, test.in_len)); |
| 863 | uint8_t *storage = nullptr; |
| 864 | CBS in, out; |
| 865 | CBS_init(&in, reinterpret_cast<const uint8_t *>(test.in), test.in_len); |
| 866 | int ok = CBS_get_asn1_implicit_string(&in, &out, &storage, |
| 867 | CBS_ASN1_CONTEXT_SPECIFIC | 0, |
| 868 | CBS_ASN1_OCTETSTRING); |
| 869 | bssl::UniquePtr<uint8_t> scoper(storage); |
| 870 | EXPECT_EQ(test.ok, static_cast<bool>(ok)); |
| 871 |
| 872 | if (ok) { |
| 873 | EXPECT_EQ(Bytes(test.out, test.out_len), |
| 874 | Bytes(CBS_data(&out), CBS_len(&out))); |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 |
| 879 | struct ASN1Uint64Test { |
| 880 | uint64_t value; |
| 881 | const char *encoding; |
| 882 | size_t encoding_len; |
| 883 | }; |
| 884 |
| 885 | static const ASN1Uint64Test kASN1Uint64Tests[] = { |
| 886 | {0, "\x02\x01\x00", 3}, |
| 887 | {1, "\x02\x01\x01", 3}, |
| 888 | {127, "\x02\x01\x7f", 3}, |
| 889 | {128, "\x02\x02\x00\x80", 4}, |
| 890 | {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7}, |
| 891 | {UINT64_C(0x0102030405060708), "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", |
| 892 | 10}, |
| 893 | {UINT64_C(0xffffffffffffffff), |
| 894 | "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11}, |
| 895 | }; |
| 896 |
| 897 | struct ASN1InvalidUint64Test { |
| 898 | const char *encoding; |
| 899 | size_t encoding_len; |
| 900 | bool overflow; |
| 901 | }; |
| 902 |
| 903 | static const ASN1InvalidUint64Test kASN1InvalidUint64Tests[] = { |
| 904 | // Bad tag. |
| 905 | {"\x03\x01\x00", 3, false}, |
| 906 | // Empty contents. |
| 907 | {"\x02\x00", 2, false}, |
| 908 | // Negative number. |
| 909 | {"\x02\x01\x80", 3, false}, |
| 910 | // Overflow. |
| 911 | {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11, true}, |
| 912 | // Leading zeros. |
| 913 | {"\x02\x02\x00\x01", 4, false}, |
| 914 | }; |
| 915 |
| 916 | struct ASN1Uint64WithTagTest { |
| 917 | CBS_ASN1_TAG tag; |
| 918 | uint64_t value; |
| 919 | const char *encoding; |
| 920 | size_t encoding_len; |
| 921 | }; |
| 922 |
| 923 | static const ASN1Uint64WithTagTest kASN1Uint64WithTagTests[]{ |
| 924 | {CBS_ASN1_CONTEXT_SPECIFIC, 0, "\x80\x01\x00", 3}, |
| 925 | {CBS_ASN1_CONTEXT_SPECIFIC | 1, 1, "\x81\x01\x01", 3}, |
| 926 | {CBS_ASN1_INTEGER, 127, "\x02\x01\x7f", 3}, |
| 927 | {CBS_ASN1_CONTEXT_SPECIFIC, 128, "\x80\x02\x00\x80", 4}, |
| 928 | {CBS_ASN1_CONTEXT_SPECIFIC, UINT64_C(0x0102030405060708), |
| 929 | "\x80\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10}, |
| 930 | {CBS_ASN1_CONTEXT_SPECIFIC, (0xffffffffffffffff), |
| 931 | "\x80\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11}, |
| 932 | }; |
| 933 |
| 934 | TEST(CBSTest, ASN1Uint64) { |
| 935 | for (const ASN1Uint64Test &test : kASN1Uint64Tests) { |
| 936 | SCOPED_TRACE(Bytes(test.encoding, test.encoding_len)); |
| 937 | SCOPED_TRACE(test.value); |
| 938 | CBS cbs; |
| 939 | uint64_t value; |
| 940 | uint8_t *out; |
| 941 | size_t len; |
| 942 |
| 943 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 944 | ASSERT_TRUE(CBS_get_asn1_uint64(&cbs, &value)); |
| 945 | EXPECT_EQ(0u, CBS_len(&cbs)); |
| 946 | EXPECT_EQ(test.value, value); |
| 947 |
| 948 | CBS child; |
| 949 | int is_negative; |
| 950 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 951 | ASSERT_TRUE(CBS_get_asn1(&cbs, &child, CBS_ASN1_INTEGER)); |
| 952 | EXPECT_TRUE(CBS_is_valid_asn1_integer(&child, &is_negative)); |
| 953 | EXPECT_EQ(0, is_negative); |
| 954 | EXPECT_TRUE(CBS_is_unsigned_asn1_integer(&child)); |
| 955 |
| 956 | { |
| 957 | bssl::ScopedCBB cbb; |
| 958 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 959 | ASSERT_TRUE(CBB_add_asn1_uint64(cbb.get(), test.value)); |
| 960 | ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len)); |
| 961 | bssl::UniquePtr<uint8_t> scoper(out); |
| 962 | EXPECT_EQ(Bytes(test.encoding, test.encoding_len), Bytes(out, len)); |
| 963 | } |
| 964 |
| 965 | { |
| 966 | // Overwrite the tag. |
| 967 | bssl::ScopedCBB cbb; |
| 968 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 969 | ASSERT_TRUE(CBB_add_asn1_uint64_with_tag(cbb.get(), test.value, |
| 970 | CBS_ASN1_CONTEXT_SPECIFIC | 1)); |
| 971 | ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len)); |
| 972 | bssl::UniquePtr<uint8_t> scoper(out); |
| 973 | std::vector<uint8_t> expected(test.encoding, |
| 974 | test.encoding + test.encoding_len); |
| 975 | expected[0] = 0x81; |
| 976 | EXPECT_EQ(Bytes(expected), Bytes(out, len)); |
| 977 | } |
| 978 | } |
| 979 |
| 980 | for (const ASN1InvalidUint64Test &test : kASN1InvalidUint64Tests) { |
| 981 | SCOPED_TRACE(Bytes(test.encoding, test.encoding_len)); |
| 982 | CBS cbs; |
| 983 | uint64_t value; |
| 984 |
| 985 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 986 | EXPECT_FALSE(CBS_get_asn1_uint64(&cbs, &value)); |
| 987 |
| 988 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 989 | CBS child; |
| 990 | if (CBS_get_asn1(&cbs, &child, CBS_ASN1_INTEGER)) { |
| 991 | EXPECT_EQ(test.overflow, !!CBS_is_unsigned_asn1_integer(&child)); |
| 992 | } |
| 993 | } |
| 994 |
| 995 | for (const ASN1Uint64WithTagTest &test : kASN1Uint64WithTagTests) { |
| 996 | SCOPED_TRACE(Bytes(test.encoding, test.encoding_len)); |
| 997 | SCOPED_TRACE(test.value); |
| 998 | CBS cbs; |
| 999 | uint64_t value; |
| 1000 | uint8_t *out; |
| 1001 | size_t len; |
| 1002 |
| 1003 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 1004 | ASSERT_TRUE(CBS_get_asn1_uint64_with_tag(&cbs, &value, test.tag)); |
| 1005 | EXPECT_EQ(0u, CBS_len(&cbs)); |
| 1006 | EXPECT_EQ(test.value, value); |
| 1007 |
| 1008 | CBS child; |
| 1009 | int is_negative; |
| 1010 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 1011 | ASSERT_TRUE(CBS_get_asn1(&cbs, &child, test.tag)); |
| 1012 | EXPECT_TRUE(CBS_is_valid_asn1_integer(&child, &is_negative)); |
| 1013 | EXPECT_EQ(0, is_negative); |
| 1014 | EXPECT_TRUE(CBS_is_unsigned_asn1_integer(&child)); |
| 1015 |
| 1016 | { |
| 1017 | bssl::ScopedCBB cbb; |
| 1018 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1019 | ASSERT_TRUE(CBB_add_asn1_uint64_with_tag(cbb.get(), test.value, test.tag)); |
| 1020 | ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len)); |
| 1021 | bssl::UniquePtr<uint8_t> scoper(out); |
| 1022 | EXPECT_EQ(Bytes(test.encoding, test.encoding_len), Bytes(out, len)); |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 |
| 1027 | struct ASN1Int64Test { |
| 1028 | int64_t value; |
| 1029 | const char *encoding; |
| 1030 | size_t encoding_len; |
| 1031 | }; |
| 1032 |
| 1033 | static const ASN1Int64Test kASN1Int64Tests[] = { |
| 1034 | {0, "\x02\x01\x00", 3}, |
| 1035 | {1, "\x02\x01\x01", 3}, |
| 1036 | {-1, "\x02\x01\xff", 3}, |
| 1037 | {127, "\x02\x01\x7f", 3}, |
| 1038 | {-127, "\x02\x01\x81", 3}, |
| 1039 | {128, "\x02\x02\x00\x80", 4}, |
| 1040 | {-128, "\x02\x01\x80", 3}, |
| 1041 | {129, "\x02\x02\x00\x81", 4}, |
| 1042 | {-129, "\x02\x02\xff\x7f", 4}, |
| 1043 | {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7}, |
| 1044 | {INT64_C(0x0102030405060708), "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", |
| 1045 | 10}, |
| 1046 | {INT64_MIN, "\x02\x08\x80\x00\x00\x00\x00\x00\x00\x00", 10}, |
| 1047 | {INT64_MAX, "\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff", 10}, |
| 1048 | }; |
| 1049 |
| 1050 | struct ASN1InvalidInt64Test { |
| 1051 | const char *encoding; |
| 1052 | size_t encoding_len; |
| 1053 | bool overflow; |
| 1054 | }; |
| 1055 |
| 1056 | static const ASN1InvalidInt64Test kASN1InvalidInt64Tests[] = { |
| 1057 | // Bad tag. |
| 1058 | {"\x03\x01\x00", 3, false}, |
| 1059 | // Empty contents. |
| 1060 | {"\x02\x00", 2, false}, |
| 1061 | // Overflow. |
| 1062 | {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11, true}, |
| 1063 | // Underflow. |
| 1064 | {"\x02\x09\x08\xff\xff\xff\xff\xff\xff\xff\xff", 11, true}, |
| 1065 | // Leading zeros. |
| 1066 | {"\x02\x02\x00\x01", 4, false}, |
| 1067 | // Leading 0xff. |
| 1068 | {"\x02\x02\xff\xff", 4, false}, |
| 1069 | }; |
| 1070 |
| 1071 | struct ASN1Int64WithTagTest { |
| 1072 | CBS_ASN1_TAG tag; |
| 1073 | int64_t value; |
| 1074 | const char *encoding; |
| 1075 | size_t encoding_len; |
| 1076 | }; |
| 1077 |
| 1078 | static const ASN1Int64WithTagTest kASN1Int64WithTagTests[] = { |
| 1079 | {CBS_ASN1_CONTEXT_SPECIFIC, 0, "\x80\x01\x00", 3}, |
| 1080 | {CBS_ASN1_CONTEXT_SPECIFIC | 1, 1, "\x81\x01\x01", 3}, |
| 1081 | {CBS_ASN1_INTEGER, 1, "\x02\x01\x01", 3}, |
| 1082 | {CBS_ASN1_CONTEXT_SPECIFIC, INT64_MIN, |
| 1083 | "\x80\x08\x80\x00\x00\x00\x00\x00\x00\x00", 10}, |
| 1084 | {CBS_ASN1_CONTEXT_SPECIFIC, INT64_MAX, |
| 1085 | "\x80\x08\x7f\xff\xff\xff\xff\xff\xff\xff", 10}, |
| 1086 | }; |
| 1087 |
| 1088 |
| 1089 | TEST(CBSTest, ASN1Int64) { |
| 1090 | for (const ASN1Int64Test &test : kASN1Int64Tests) { |
| 1091 | SCOPED_TRACE(Bytes(test.encoding, test.encoding_len)); |
| 1092 | SCOPED_TRACE(test.value); |
| 1093 | CBS cbs; |
| 1094 | int64_t value; |
| 1095 | uint8_t *out; |
| 1096 | size_t len; |
| 1097 |
| 1098 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 1099 | ASSERT_TRUE(CBS_get_asn1_int64(&cbs, &value)); |
| 1100 | EXPECT_EQ(0u, CBS_len(&cbs)); |
| 1101 | EXPECT_EQ(test.value, value); |
| 1102 |
| 1103 | CBS child; |
| 1104 | int is_negative; |
| 1105 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 1106 | ASSERT_TRUE(CBS_get_asn1(&cbs, &child, CBS_ASN1_INTEGER)); |
| 1107 | EXPECT_TRUE(CBS_is_valid_asn1_integer(&child, &is_negative)); |
| 1108 | EXPECT_EQ(test.value < 0, !!is_negative); |
| 1109 | EXPECT_EQ(test.value >= 0, !!CBS_is_unsigned_asn1_integer(&child)); |
| 1110 |
| 1111 | { |
| 1112 | bssl::ScopedCBB cbb; |
| 1113 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1114 | ASSERT_TRUE(CBB_add_asn1_int64(cbb.get(), test.value)); |
| 1115 | ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len)); |
| 1116 | bssl::UniquePtr<uint8_t> scoper(out); |
| 1117 | EXPECT_EQ(Bytes(test.encoding, test.encoding_len), Bytes(out, len)); |
| 1118 | } |
| 1119 |
| 1120 | { |
| 1121 | // Overwrite the tag. |
| 1122 | bssl::ScopedCBB cbb; |
| 1123 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1124 | ASSERT_TRUE(CBB_add_asn1_int64_with_tag(cbb.get(), test.value, |
| 1125 | CBS_ASN1_CONTEXT_SPECIFIC | 1)); |
| 1126 | ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len)); |
| 1127 | bssl::UniquePtr<uint8_t> scoper(out); |
| 1128 | std::vector<uint8_t> expected(test.encoding, |
| 1129 | test.encoding + test.encoding_len); |
| 1130 | expected[0] = 0x81; |
| 1131 | EXPECT_EQ(Bytes(expected), Bytes(out, len)); |
| 1132 | } |
| 1133 | } |
| 1134 |
| 1135 | for (const ASN1InvalidInt64Test &test : kASN1InvalidInt64Tests) { |
| 1136 | SCOPED_TRACE(Bytes(test.encoding, test.encoding_len)); |
| 1137 | CBS cbs; |
| 1138 | int64_t value; |
| 1139 |
| 1140 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 1141 | EXPECT_FALSE(CBS_get_asn1_int64(&cbs, &value)); |
| 1142 |
| 1143 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 1144 | CBS child; |
| 1145 | if (CBS_get_asn1(&cbs, &child, CBS_ASN1_INTEGER)) { |
| 1146 | EXPECT_EQ(test.overflow, !!CBS_is_valid_asn1_integer(&child, NULL)); |
| 1147 | } |
| 1148 | } |
| 1149 |
| 1150 | for (const ASN1Int64WithTagTest &test : kASN1Int64WithTagTests) { |
| 1151 | SCOPED_TRACE(Bytes(test.encoding, test.encoding_len)); |
| 1152 | SCOPED_TRACE(test.value); |
| 1153 | CBS cbs; |
| 1154 | int64_t value; |
| 1155 | uint8_t *out; |
| 1156 | size_t len; |
| 1157 |
| 1158 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 1159 | ASSERT_TRUE(CBS_get_asn1_int64_with_tag(&cbs, &value, test.tag)); |
| 1160 | EXPECT_EQ(0u, CBS_len(&cbs)); |
| 1161 | EXPECT_EQ(test.value, value); |
| 1162 |
| 1163 | CBS child; |
| 1164 | int is_negative; |
| 1165 | CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len); |
| 1166 | ASSERT_TRUE(CBS_get_asn1(&cbs, &child, test.tag)); |
| 1167 | EXPECT_TRUE(CBS_is_valid_asn1_integer(&child, &is_negative)); |
| 1168 | EXPECT_EQ(test.value < 0, !!is_negative); |
| 1169 | EXPECT_EQ(test.value >= 0, !!CBS_is_unsigned_asn1_integer(&child)); |
| 1170 |
| 1171 | { |
| 1172 | bssl::ScopedCBB cbb; |
| 1173 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1174 | ASSERT_TRUE(CBB_add_asn1_int64_with_tag(cbb.get(), test.value, test.tag)); |
| 1175 | ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len)); |
| 1176 | bssl::UniquePtr<uint8_t> scoper(out); |
| 1177 | EXPECT_EQ(Bytes(test.encoding, test.encoding_len), Bytes(out, len)); |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 |
| 1182 | TEST(CBBTest, Zero) { |
| 1183 | CBB cbb; |
| 1184 | CBB_zero(&cbb); |
| 1185 | // Calling |CBB_cleanup| on a zero-state |CBB| must not crash. |
| 1186 | CBB_cleanup(&cbb); |
| 1187 | } |
| 1188 |
| 1189 | TEST(CBBTest, Reserve) { |
| 1190 | uint8_t buf[10]; |
| 1191 | uint8_t *ptr; |
| 1192 | size_t len; |
| 1193 | bssl::ScopedCBB cbb; |
| 1194 | ASSERT_TRUE(CBB_init_fixed(cbb.get(), buf, sizeof(buf))); |
| 1195 | // Too large. |
| 1196 | EXPECT_FALSE(CBB_reserve(cbb.get(), &ptr, 11)); |
| 1197 |
| 1198 | cbb.Reset(); |
| 1199 | ASSERT_TRUE(CBB_init_fixed(cbb.get(), buf, sizeof(buf))); |
| 1200 | // Successfully reserve the entire space. |
| 1201 | ASSERT_TRUE(CBB_reserve(cbb.get(), &ptr, 10)); |
| 1202 | EXPECT_EQ(buf, ptr); |
| 1203 | // Advancing under the maximum bytes is legal. |
| 1204 | ASSERT_TRUE(CBB_did_write(cbb.get(), 5)); |
| 1205 | ASSERT_TRUE(CBB_finish(cbb.get(), NULL, &len)); |
| 1206 | EXPECT_EQ(5u, len); |
| 1207 | } |
| 1208 |
| 1209 | // Test that CBB errors are sticky; once on operation on CBB fails, all |
| 1210 | // subsequent ones do. |
| 1211 | TEST(CBBTest, StickyError) { |
| 1212 | // Write an input that exceeds the limit for its length prefix. |
| 1213 | bssl::ScopedCBB cbb; |
| 1214 | CBB child; |
| 1215 | static const uint8_t kZeros[256] = {0}; |
| 1216 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1217 | ASSERT_TRUE(CBB_add_u8_length_prefixed(cbb.get(), &child)); |
| 1218 | ASSERT_TRUE(CBB_add_bytes(&child, kZeros, sizeof(kZeros))); |
| 1219 | ASSERT_FALSE(CBB_flush(cbb.get())); |
| 1220 |
| 1221 | // All future operations should fail. |
| 1222 | uint8_t *ptr; |
| 1223 | size_t len; |
| 1224 | EXPECT_FALSE(CBB_add_u8(cbb.get(), 0)); |
| 1225 | EXPECT_FALSE(CBB_finish(cbb.get(), &ptr, &len)); |
| 1226 |
| 1227 | // Write an input that cannot fit in a fixed CBB. |
| 1228 | cbb.Reset(); |
| 1229 | uint8_t buf; |
| 1230 | ASSERT_TRUE(CBB_init_fixed(cbb.get(), &buf, 1)); |
| 1231 | ASSERT_FALSE(CBB_add_bytes(cbb.get(), kZeros, sizeof(kZeros))); |
| 1232 |
| 1233 | // All future operations should fail. |
| 1234 | EXPECT_FALSE(CBB_add_u8(cbb.get(), 0)); |
| 1235 | EXPECT_FALSE(CBB_finish(cbb.get(), &ptr, &len)); |
| 1236 |
| 1237 | // Write a u32 that cannot fit in a u24. |
| 1238 | cbb.Reset(); |
| 1239 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1240 | ASSERT_FALSE(CBB_add_u24(cbb.get(), 1u << 24)); |
| 1241 |
| 1242 | // All future operations should fail. |
| 1243 | EXPECT_FALSE(CBB_add_u8(cbb.get(), 0)); |
| 1244 | EXPECT_FALSE(CBB_finish(cbb.get(), &ptr, &len)); |
| 1245 | } |
| 1246 |
| 1247 | TEST(CBSTest, BitString) { |
| 1248 | static const std::vector<uint8_t> kValidBitStrings[] = { |
| 1249 | {0x00}, // 0 bits |
| 1250 | {0x07, 0x80}, // 1 bit |
| 1251 | {0x04, 0xf0}, // 4 bits |
| 1252 | {0x00, 0xff}, // 8 bits |
| 1253 | {0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0}, // 42 bits |
| 1254 | }; |
| 1255 | for (const auto &test : kValidBitStrings) { |
| 1256 | SCOPED_TRACE(Bytes(test.data(), test.size())); |
| 1257 | CBS cbs; |
| 1258 | CBS_init(&cbs, test.data(), test.size()); |
| 1259 | EXPECT_TRUE(CBS_is_valid_asn1_bitstring(&cbs)); |
| 1260 | } |
| 1261 |
| 1262 | static const std::vector<uint8_t> kInvalidBitStrings[] = { |
| 1263 | // BIT STRINGs always have a leading byte. |
| 1264 | std::vector<uint8_t>{}, |
| 1265 | // It's not possible to take an unused bit off the empty string. |
| 1266 | {0x01}, |
| 1267 | // There can be at most 7 unused bits. |
| 1268 | {0x08, 0xff}, |
| 1269 | {0xff, 0xff}, |
| 1270 | // All unused bits must be cleared. |
| 1271 | {0x06, 0xff, 0xc1}, |
| 1272 | }; |
| 1273 | for (const auto &test : kInvalidBitStrings) { |
| 1274 | SCOPED_TRACE(Bytes(test.data(), test.size())); |
| 1275 | CBS cbs; |
| 1276 | CBS_init(&cbs, test.data(), test.size()); |
| 1277 | EXPECT_FALSE(CBS_is_valid_asn1_bitstring(&cbs)); |
| 1278 |
| 1279 | // CBS_asn1_bitstring_has_bit returns false on invalid inputs. |
| 1280 | EXPECT_FALSE(CBS_asn1_bitstring_has_bit(&cbs, 0)); |
| 1281 | } |
| 1282 |
| 1283 | static const struct { |
| 1284 | std::vector<uint8_t> in; |
| 1285 | unsigned bit; |
| 1286 | bool bit_set; |
| 1287 | } kBitTests[] = { |
| 1288 | // Basic tests. |
| 1289 | {{0x00}, 0, false}, |
| 1290 | {{0x07, 0x80}, 0, true}, |
| 1291 | {{0x06, 0x0f, 0x40}, 0, false}, |
| 1292 | {{0x06, 0x0f, 0x40}, 1, false}, |
| 1293 | {{0x06, 0x0f, 0x40}, 2, false}, |
| 1294 | {{0x06, 0x0f, 0x40}, 3, false}, |
| 1295 | {{0x06, 0x0f, 0x40}, 4, true}, |
| 1296 | {{0x06, 0x0f, 0x40}, 5, true}, |
| 1297 | {{0x06, 0x0f, 0x40}, 6, true}, |
| 1298 | {{0x06, 0x0f, 0x40}, 7, true}, |
| 1299 | {{0x06, 0x0f, 0x40}, 8, false}, |
| 1300 | {{0x06, 0x0f, 0x40}, 9, true}, |
| 1301 | // Out-of-bounds bits return 0. |
| 1302 | {{0x06, 0x0f, 0x40}, 10, false}, |
| 1303 | {{0x06, 0x0f, 0x40}, 15, false}, |
| 1304 | {{0x06, 0x0f, 0x40}, 16, false}, |
| 1305 | {{0x06, 0x0f, 0x40}, 1000, false}, |
| 1306 | }; |
| 1307 | for (const auto &test : kBitTests) { |
| 1308 | SCOPED_TRACE(Bytes(test.in.data(), test.in.size())); |
| 1309 | SCOPED_TRACE(test.bit); |
| 1310 | CBS cbs; |
| 1311 | CBS_init(&cbs, test.in.data(), test.in.size()); |
| 1312 | EXPECT_EQ(static_cast<int>(test.bit_set), |
| 1313 | CBS_asn1_bitstring_has_bit(&cbs, test.bit)); |
| 1314 | } |
| 1315 | } |
| 1316 |
| 1317 | TEST(CBBTest, AddOIDFromText) { |
| 1318 | const struct { |
| 1319 | const char *text; |
| 1320 | std::vector<uint8_t> der; |
| 1321 | } kValidOIDs[] = { |
| 1322 | // Some valid values. |
| 1323 | {"0.0", {0x00}}, |
| 1324 | {"0.2.3.4", {0x2, 0x3, 0x4}}, |
| 1325 | {"1.2.3.4", {0x2a, 0x3, 0x4}}, |
| 1326 | {"2.2.3.4", {0x52, 0x3, 0x4}}, |
| 1327 | {"1.2.840.113554.4.1.72585", |
| 1328 | {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09}}, |
| 1329 | // Test edge cases around the first component. |
| 1330 | {"0.39", {0x27}}, |
| 1331 | {"1.0", {0x28}}, |
| 1332 | {"1.39", {0x4f}}, |
| 1333 | {"2.0", {0x50}}, |
| 1334 | {"2.1", {0x51}}, |
| 1335 | {"2.40", {0x78}}, |
| 1336 | // Edge cases near an overflow. |
| 1337 | {"1.2.18446744073709551615", |
| 1338 | {0x2a, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}}, |
| 1339 | {"2.18446744073709551535", |
| 1340 | {0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}}, |
| 1341 | }; |
| 1342 |
| 1343 | const char *kInvalidTexts[] = { |
| 1344 | // Invalid second component. |
| 1345 | "0.40", |
| 1346 | "1.40", |
| 1347 | // Invalid first component. |
| 1348 | "3.1", |
| 1349 | // The empty string is not an OID. |
| 1350 | "", |
| 1351 | // No empty components. |
| 1352 | ".1.2.3.4.5", |
| 1353 | "1..2.3.4.5", |
| 1354 | "1.2.3.4.5.", |
| 1355 | // There must be at least two components. |
| 1356 | "1", |
| 1357 | // No extra leading zeros. |
| 1358 | "00.1.2.3.4", |
| 1359 | "01.1.2.3.4", |
| 1360 | // Overflow for both components or 40*A + B. |
| 1361 | "1.2.18446744073709551616", |
| 1362 | "2.18446744073709551536", |
| 1363 | }; |
| 1364 |
| 1365 | const struct { |
| 1366 | std::vector<uint8_t> der; |
| 1367 | // If true, |der| is valid but has a component that exceeds 2^64-1. |
| 1368 | bool overflow; |
| 1369 | } kInvalidDER[] = { |
| 1370 | // The empty string is not an OID. |
| 1371 | {{}, false}, |
| 1372 | // Non-minimal representation. |
| 1373 | {{0x80, 0x01}, false}, |
| 1374 | // Unterminated integer. |
| 1375 | {{0x01, 0x02, 0x83}, false}, |
| 1376 | // Overflow. This is the DER representation of |
| 1377 | // 1.2.840.113554.4.1.72585.18446744073709551616. (The final value is |
| 1378 | // 2^64.) |
| 1379 | {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, |
| 1380 | 0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00}, |
| 1381 | true}, |
| 1382 | }; |
| 1383 |
| 1384 | for (const auto &t : kValidOIDs) { |
| 1385 | SCOPED_TRACE(t.text); |
| 1386 |
| 1387 | bssl::ScopedCBB cbb; |
| 1388 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1389 | ASSERT_TRUE(CBB_add_asn1_oid_from_text(cbb.get(), t.text, strlen(t.text))); |
| 1390 | uint8_t *out; |
| 1391 | size_t len; |
| 1392 | ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len)); |
| 1393 | bssl::UniquePtr<uint8_t> free_out(out); |
| 1394 | EXPECT_EQ(Bytes(t.der), Bytes(out, len)); |
| 1395 |
| 1396 | CBS cbs; |
| 1397 | CBS_init(&cbs, t.der.data(), t.der.size()); |
| 1398 | bssl::UniquePtr<char> text(CBS_asn1_oid_to_text(&cbs)); |
| 1399 | ASSERT_TRUE(text.get()); |
| 1400 | EXPECT_STREQ(t.text, text.get()); |
| 1401 |
| 1402 | EXPECT_TRUE(CBS_is_valid_asn1_oid(&cbs)); |
| 1403 | } |
| 1404 |
| 1405 | for (const char *t : kInvalidTexts) { |
| 1406 | SCOPED_TRACE(t); |
| 1407 | bssl::ScopedCBB cbb; |
| 1408 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1409 | EXPECT_FALSE(CBB_add_asn1_oid_from_text(cbb.get(), t, strlen(t))); |
| 1410 | } |
| 1411 |
| 1412 | for (const auto &t : kInvalidDER) { |
| 1413 | SCOPED_TRACE(Bytes(t.der)); |
| 1414 | CBS cbs; |
| 1415 | CBS_init(&cbs, t.der.data(), t.der.size()); |
| 1416 | bssl::UniquePtr<char> text(CBS_asn1_oid_to_text(&cbs)); |
| 1417 | EXPECT_FALSE(text); |
| 1418 | EXPECT_EQ(t.overflow ? 1 : 0, CBS_is_valid_asn1_oid(&cbs)); |
| 1419 | } |
| 1420 | } |
| 1421 |
| 1422 | TEST(CBBTest, FlushASN1SetOf) { |
| 1423 | const struct { |
| 1424 | std::vector<uint8_t> in, out; |
| 1425 | } kValidInputs[] = { |
| 1426 | // No elements. |
| 1427 | {{}, {}}, |
| 1428 | // One element. |
| 1429 | {{0x30, 0x00}, {0x30, 0x00}}, |
| 1430 | // Two identical elements. |
| 1431 | {{0x30, 0x00, 0x30, 0x00}, {0x30, 0x00, 0x30, 0x00}}, |
| 1432 | // clang-format off |
| 1433 | {{0x30, 0x02, 0x00, 0x00, |
| 1434 | 0x30, 0x00, |
| 1435 | 0x01, 0x00, |
| 1436 | 0x30, 0x02, 0x00, 0x00, |
| 1437 | 0x30, 0x03, 0x00, 0x00, 0x00, |
| 1438 | 0x30, 0x00, |
| 1439 | 0x30, 0x03, 0x00, 0x00, 0x01, |
| 1440 | 0x30, 0x01, 0x00, |
| 1441 | 0x01, 0x01, 0x00}, |
| 1442 | {0x01, 0x00, |
| 1443 | 0x01, 0x01, 0x00, |
| 1444 | 0x30, 0x00, |
| 1445 | 0x30, 0x00, |
| 1446 | 0x30, 0x01, 0x00, |
| 1447 | 0x30, 0x02, 0x00, 0x00, |
| 1448 | 0x30, 0x02, 0x00, 0x00, |
| 1449 | 0x30, 0x03, 0x00, 0x00, 0x00, |
| 1450 | 0x30, 0x03, 0x00, 0x00, 0x01}}, |
| 1451 | // clang-format on |
| 1452 | }; |
| 1453 |
| 1454 | for (const auto &t : kValidInputs) { |
| 1455 | SCOPED_TRACE(Bytes(t.in)); |
| 1456 |
| 1457 | bssl::ScopedCBB cbb; |
| 1458 | CBB child; |
| 1459 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1460 | ASSERT_TRUE(CBB_add_asn1(cbb.get(), &child, CBS_ASN1_SET)); |
| 1461 | ASSERT_TRUE(CBB_add_bytes(&child, t.in.data(), t.in.size())); |
| 1462 | ASSERT_TRUE(CBB_flush_asn1_set_of(&child)); |
| 1463 | EXPECT_EQ(Bytes(t.out), Bytes(CBB_data(&child), CBB_len(&child))); |
| 1464 |
| 1465 | // Running it again should be idempotent. |
| 1466 | ASSERT_TRUE(CBB_flush_asn1_set_of(&child)); |
| 1467 | EXPECT_EQ(Bytes(t.out), Bytes(CBB_data(&child), CBB_len(&child))); |
| 1468 |
| 1469 | // The ASN.1 header remain intact. |
| 1470 | ASSERT_TRUE(CBB_flush(cbb.get())); |
| 1471 | EXPECT_EQ(0x31, CBB_data(cbb.get())[0]); |
| 1472 | } |
| 1473 |
| 1474 | const std::vector<uint8_t> kInvalidInputs[] = { |
| 1475 | {0x30}, |
| 1476 | {0x30, 0x01}, |
| 1477 | {0x30, 0x00, 0x30, 0x00, 0x30, 0x01}, |
| 1478 | }; |
| 1479 |
| 1480 | for (const auto &t : kInvalidInputs) { |
| 1481 | SCOPED_TRACE(Bytes(t)); |
| 1482 |
| 1483 | bssl::ScopedCBB cbb; |
| 1484 | CBB child; |
| 1485 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1486 | ASSERT_TRUE(CBB_add_asn1(cbb.get(), &child, CBS_ASN1_SET)); |
| 1487 | ASSERT_TRUE(CBB_add_bytes(&child, t.data(), t.size())); |
| 1488 | EXPECT_FALSE(CBB_flush_asn1_set_of(&child)); |
| 1489 | } |
| 1490 | } |
| 1491 |
| 1492 | template <class T> |
| 1493 | static std::vector<uint8_t> LiteralToBytes(const T *str) { |
| 1494 | std::vector<uint8_t> ret; |
| 1495 | for (; *str != 0; str++) { |
| 1496 | for (size_t i = 0; i < sizeof(T); i++) { |
| 1497 | ret.push_back(static_cast<uint8_t>(*str >> (8 * (sizeof(T) - 1 - i)))); |
| 1498 | } |
| 1499 | } |
| 1500 | return ret; |
| 1501 | } |
| 1502 |
| 1503 | static std::vector<uint32_t> LiteralToCodePoints(const char32_t *str) { |
| 1504 | std::vector<uint32_t> ret; |
| 1505 | for (; *str != 0; str++) { |
| 1506 | ret.push_back(static_cast<uint32_t>(*str)); |
| 1507 | } |
| 1508 | return ret; |
| 1509 | } |
| 1510 |
| 1511 | TEST(CBBTest, Unicode) { |
| 1512 | struct { |
| 1513 | int (*decode)(CBS *, uint32_t *); |
| 1514 | int (*encode)(CBB *, uint32_t); |
| 1515 | std::vector<uint8_t> in; |
| 1516 | std::vector<uint32_t> out; |
| 1517 | bool ok; |
| 1518 | } kTests[] = { |
| 1519 | {CBS_get_utf8, CBB_add_utf8, |
| 1520 | // This test string captures all four cases in UTF-8. |
| 1521 | LiteralToBytes(u8"Hello, 世界! ¡Hola, 🌎!"), |
| 1522 | LiteralToCodePoints(U"Hello, 世界! ¡Hola, 🌎!"), true}, |
| 1523 |
| 1524 | // Some invalid inputs adapted from |
| 1525 | // http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt |
| 1526 | // 2.1 First possible sequence of a certain length. (5- and 6-bit |
| 1527 | // sequences no longer exist.) |
| 1528 | {CBS_get_utf8, CBB_add_utf8, {0xf8, 0x88, 0x80, 0x80, 0x80}, {}, false}, |
| 1529 | {CBS_get_utf8, |
| 1530 | CBB_add_utf8, |
| 1531 | {0xfc, 0x84, 0x80, 0x80, 0x80, 0x80}, |
| 1532 | {}, |
| 1533 | false}, |
| 1534 | // 3.1 Unexpected continuation bytes. |
| 1535 | {CBS_get_utf8, CBB_add_utf8, {0x80}, {}, false}, |
| 1536 | {CBS_get_utf8, CBB_add_utf8, {0xbf}, {}, false}, |
| 1537 | // 3.2 Lonely start characters. |
| 1538 | {CBS_get_utf8, CBB_add_utf8, {0xc0, ' '}, {}, false}, |
| 1539 | {CBS_get_utf8, CBB_add_utf8, {0xe0, ' '}, {}, false}, |
| 1540 | {CBS_get_utf8, CBB_add_utf8, {0xf0, ' '}, {}, false}, |
| 1541 | // 3.3 Sequences with last continuation byte missing |
| 1542 | {CBS_get_utf8, CBB_add_utf8, {0xc0}, {}, false}, |
| 1543 | {CBS_get_utf8, CBB_add_utf8, {0xe0, 0x80}, {}, false}, |
| 1544 | {CBS_get_utf8, CBB_add_utf8, {0xf0, 0x80, 0x80}, {}, false}, |
| 1545 | // Variation of the above with unexpected spaces. |
| 1546 | {CBS_get_utf8, CBB_add_utf8, {0xe0, 0x80, ' '}, {}, false}, |
| 1547 | {CBS_get_utf8, CBB_add_utf8, {0xf0, 0x80, 0x80, ' '}, {}, false}, |
| 1548 | // 4.1 Examples of an overlong ASCII character |
| 1549 | {CBS_get_utf8, CBB_add_utf8, {0xc0, 0xaf}, {}, false}, |
| 1550 | {CBS_get_utf8, CBB_add_utf8, {0xe0, 0x80, 0xaf}, {}, false}, |
| 1551 | {CBS_get_utf8, CBB_add_utf8, {0xf0, 0x80, 0x80, 0xaf}, {}, false}, |
| 1552 | // 4.2 Maximum overlong sequences |
| 1553 | {CBS_get_utf8, CBB_add_utf8, {0xc1, 0xbf}, {}, false}, |
| 1554 | {CBS_get_utf8, CBB_add_utf8, {0xe0, 0x9f, 0xbf}, {}, false}, |
| 1555 | {CBS_get_utf8, CBB_add_utf8, {0xf0, 0x8f, 0xbf, 0xbf}, {}, false}, |
| 1556 | // 4.3 Overlong representation of the NUL character |
| 1557 | {CBS_get_utf8, CBB_add_utf8, {0xc0, 0x80}, {}, false}, |
| 1558 | {CBS_get_utf8, CBB_add_utf8, {0xe0, 0x80, 0x80}, {}, false}, |
| 1559 | {CBS_get_utf8, CBB_add_utf8, {0xf0, 0x80, 0x80, 0x80}, {}, false}, |
| 1560 | // 5.1 Single UTF-16 surrogates |
| 1561 | {CBS_get_utf8, CBB_add_utf8, {0xed, 0xa0, 0x80}, {}, false}, |
| 1562 | {CBS_get_utf8, CBB_add_utf8, {0xed, 0xad, 0xbf}, {}, false}, |
| 1563 | {CBS_get_utf8, CBB_add_utf8, {0xed, 0xae, 0x80}, {}, false}, |
| 1564 | {CBS_get_utf8, CBB_add_utf8, {0xed, 0xb0, 0x80}, {}, false}, |
| 1565 | {CBS_get_utf8, CBB_add_utf8, {0xed, 0xbe, 0x80}, {}, false}, |
| 1566 | {CBS_get_utf8, CBB_add_utf8, {0xed, 0xbf, 0xbf}, {}, false}, |
| 1567 | // 5.2 Paired UTF-16 surrogates |
| 1568 | {CBS_get_utf8, |
| 1569 | CBB_add_utf8, |
| 1570 | {0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80}, |
| 1571 | {}, |
| 1572 | false}, |
| 1573 | {CBS_get_utf8, |
| 1574 | CBB_add_utf8, |
| 1575 | {0xed, 0xa0, 0x80, 0xed, 0xbf, 0xbf}, |
| 1576 | {}, |
| 1577 | false}, |
| 1578 | {CBS_get_utf8, |
| 1579 | CBB_add_utf8, |
| 1580 | {0xed, 0xad, 0xbf, 0xed, 0xb0, 0x80}, |
| 1581 | {}, |
| 1582 | false}, |
| 1583 | {CBS_get_utf8, |
| 1584 | CBB_add_utf8, |
| 1585 | {0xed, 0xad, 0xbf, 0xed, 0xbf, 0xbf}, |
| 1586 | {}, |
| 1587 | false}, |
| 1588 | {CBS_get_utf8, |
| 1589 | CBB_add_utf8, |
| 1590 | {0xed, 0xae, 0x80, 0xed, 0xb0, 0x80}, |
| 1591 | {}, |
| 1592 | false}, |
| 1593 | {CBS_get_utf8, |
| 1594 | CBB_add_utf8, |
| 1595 | {0xed, 0xae, 0x80, 0xed, 0xbf, 0xbf}, |
| 1596 | {}, |
| 1597 | false}, |
| 1598 | {CBS_get_utf8, |
| 1599 | CBB_add_utf8, |
| 1600 | {0xed, 0xaf, 0xbf, 0xed, 0xb0, 0x80}, |
| 1601 | {}, |
| 1602 | false}, |
| 1603 | {CBS_get_utf8, |
| 1604 | CBB_add_utf8, |
| 1605 | {0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf}, |
| 1606 | {}, |
| 1607 | false}, |
| 1608 | // 5.3 Noncharacter code positions |
| 1609 | {CBS_get_utf8, CBB_add_utf8, {0xef, 0xbf, 0xbe}, {}, false}, |
| 1610 | {CBS_get_utf8, CBB_add_utf8, {0xef, 0xbf, 0xbf}, {}, false}, |
| 1611 | {CBS_get_utf8, CBB_add_utf8, {0xef, 0xb7, 0x90}, {}, false}, |
| 1612 | {CBS_get_utf8, CBB_add_utf8, {0xef, 0xb7, 0xaf}, {}, false}, |
| 1613 | {CBS_get_utf8, CBB_add_utf8, {0xf0, 0x9f, 0xbf, 0xbe}, {}, false}, |
| 1614 | {CBS_get_utf8, CBB_add_utf8, {0xf0, 0x9f, 0xbf, 0xbf}, {}, false}, |
| 1615 |
| 1616 | {CBS_get_latin1, CBB_add_latin1, LiteralToBytes("\xa1Hola!"), |
| 1617 | LiteralToCodePoints(U"¡Hola!"), true}, |
| 1618 |
| 1619 | // UCS-2 matches UTF-16 on the BMP. |
| 1620 | {CBS_get_ucs2_be, CBB_add_ucs2_be, LiteralToBytes(u"Hello, 世界!"), |
| 1621 | LiteralToCodePoints(U"Hello, 世界!"), true}, |
| 1622 | // It does not support characters beyond the BMP. |
| 1623 | {CBS_get_ucs2_be, CBB_add_ucs2_be, |
| 1624 | LiteralToBytes(u"Hello, 世界! ¡Hola, 🌎!"), |
| 1625 | LiteralToCodePoints(U"Hello, 世界! ¡Hola, "), false}, |
| 1626 | // Unpaired surrogates and non-characters are also rejected. |
| 1627 | {CBS_get_ucs2_be, CBB_add_ucs2_be, {0xd8, 0x00}, {}, false}, |
| 1628 | {CBS_get_ucs2_be, CBB_add_ucs2_be, {0xff, 0xfe}, {}, false}, |
| 1629 |
| 1630 | {CBS_get_utf32_be, CBB_add_utf32_be, |
| 1631 | LiteralToBytes(U"Hello, 世界! ¡Hola, 🌎!"), |
| 1632 | LiteralToCodePoints(U"Hello, 世界! ¡Hola, 🌎!"), true}, |
| 1633 | // Unpaired surrogates and non-characters are rejected. |
| 1634 | {CBS_get_utf32_be, CBB_add_utf32_be, {0x00, 0x00, 0xd8, 0x00}, {}, false}, |
| 1635 | {CBS_get_utf32_be, CBB_add_utf32_be, {0x00, 0x00, 0xff, 0xfe}, {}, false}, |
| 1636 |
| 1637 | // Test that the NUL character can be encoded. |
| 1638 | {CBS_get_latin1, CBB_add_latin1, {0}, {0}, true}, |
| 1639 | {CBS_get_utf8, CBB_add_utf8, {0}, {0}, true}, |
| 1640 | {CBS_get_ucs2_be, CBB_add_ucs2_be, {0, 0}, {0}, true}, |
| 1641 | {CBS_get_utf32_be, CBB_add_utf32_be, {0, 0, 0, 0}, {0}, true}, |
| 1642 | }; |
| 1643 | for (const auto &t : kTests) { |
| 1644 | SCOPED_TRACE(Bytes(t.in)); |
| 1645 |
| 1646 | // Test decoding. |
| 1647 | CBS cbs; |
| 1648 | CBS_init(&cbs, t.in.data(), t.in.size()); |
| 1649 | std::vector<uint32_t> out; |
| 1650 | bool ok = true; |
| 1651 | while (CBS_len(&cbs) != 0) { |
| 1652 | uint32_t u; |
| 1653 | if (!t.decode(&cbs, &u)) { |
| 1654 | ok = false; |
| 1655 | break; |
| 1656 | } |
| 1657 | out.push_back(u); |
| 1658 | } |
| 1659 | EXPECT_EQ(t.ok, ok); |
| 1660 | EXPECT_EQ(t.out, out); |
| 1661 |
| 1662 | // Test encoding. |
| 1663 | if (t.ok) { |
| 1664 | bssl::ScopedCBB cbb; |
| 1665 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1666 | for (uint32_t u : t.out) { |
| 1667 | ASSERT_TRUE(t.encode(cbb.get(), u)); |
| 1668 | } |
| 1669 | EXPECT_EQ(Bytes(t.in), Bytes(CBB_data(cbb.get()), CBB_len(cbb.get()))); |
| 1670 | } |
| 1671 | } |
| 1672 |
| 1673 | static const uint32_t kBadCodePoints[] = { |
| 1674 | // Surrogate pairs. |
| 1675 | 0xd800, |
| 1676 | 0xdfff, |
| 1677 | // Non-characters. |
| 1678 | 0xfffe, |
| 1679 | 0xffff, |
| 1680 | 0xfdd0, |
| 1681 | 0x1fffe, |
| 1682 | 0x1ffff, |
| 1683 | // Too big. |
| 1684 | 0x110000, |
| 1685 | }; |
| 1686 | bssl::ScopedCBB cbb; |
| 1687 | ASSERT_TRUE(CBB_init(cbb.get(), 0)); |
| 1688 | for (uint32_t v : kBadCodePoints) { |
| 1689 | SCOPED_TRACE(v); |
| 1690 | EXPECT_FALSE(CBB_add_utf8(cbb.get(), v)); |
| 1691 | EXPECT_FALSE(CBB_add_latin1(cbb.get(), v)); |
| 1692 | EXPECT_FALSE(CBB_add_ucs2_be(cbb.get(), v)); |
| 1693 | EXPECT_FALSE(CBB_add_utf32_be(cbb.get(), v)); |
| 1694 | } |
| 1695 |
| 1696 | // Additional values that are out of range. |
| 1697 | EXPECT_FALSE(CBB_add_latin1(cbb.get(), 0x100)); |
| 1698 | EXPECT_FALSE(CBB_add_ucs2_be(cbb.get(), 0x10000)); |
| 1699 |
| 1700 | EXPECT_EQ(1u, CBB_get_utf8_len(0)); |
| 1701 | EXPECT_EQ(1u, CBB_get_utf8_len(0x7f)); |
| 1702 | EXPECT_EQ(2u, CBB_get_utf8_len(0x80)); |
| 1703 | EXPECT_EQ(2u, CBB_get_utf8_len(0x7ff)); |
| 1704 | EXPECT_EQ(3u, CBB_get_utf8_len(0x800)); |
| 1705 | EXPECT_EQ(3u, CBB_get_utf8_len(0xffff)); |
| 1706 | EXPECT_EQ(4u, CBB_get_utf8_len(0x10000)); |
| 1707 | EXPECT_EQ(4u, CBB_get_utf8_len(0x10ffff)); |
| 1708 | } |
| 1709 |
| 1710 | TEST(CBSTest, BogusTime) { |
| 1711 | static const struct { |
| 1712 | const char *timestring; |
| 1713 | } kBogusTimeTests[] = { |
| 1714 | {""}, |
| 1715 | {"invalidtimesZ"}, |
| 1716 | {"Z"}, |
| 1717 | {"0000"}, |
| 1718 | {"9999Z"}, |
| 1719 | {"00000000000000000000000000000Z"}, |
| 1720 | {"19491231235959"}, |
| 1721 | {"500101000000.001Z"}, |
| 1722 | {"500101000000+6"}, |
| 1723 | {"-1970010100000Z"}, |
| 1724 | {"7a0101000000Z"}, |
| 1725 | {"20500101000000-6"}, |
| 1726 | {"20500101000000.001"}, |
| 1727 | {"20500229000000Z"}, |
| 1728 | {"220229000000Z"}, |
| 1729 | {"20500132000000Z"}, |
| 1730 | {"220132000000Z"}, |
| 1731 | {"20500332000000Z"}, |
| 1732 | {"220332000000Z"}, |
| 1733 | {"20500532000000Z"}, |
| 1734 | {"220532000000Z"}, |
| 1735 | {"20500732000000Z"}, |
| 1736 | {"220732000000Z"}, |
| 1737 | {"20500832000000Z"}, |
| 1738 | {"220832000000Z"}, |
| 1739 | {"20501032000000Z"}, |
| 1740 | {"221032000000Z"}, |
| 1741 | {"20501232000000Z"}, |
| 1742 | {"221232000000Z"}, |
| 1743 | {"20500431000000Z"}, |
| 1744 | {"220431000000Z"}, |
| 1745 | {"20500631000000Z"}, |
| 1746 | {"220631000000Z"}, |
| 1747 | {"20500931000000Z"}, |
| 1748 | {"220931000000Z"}, |
| 1749 | {"20501131000000Z"}, |
| 1750 | {"221131000000Z"}, |
| 1751 | {"20501100000000Z"}, |
| 1752 | {"221100000000Z"}, |
| 1753 | {"19500101000000+0600"}, |
| 1754 | }; |
| 1755 | for (const auto &t : kBogusTimeTests) { |
| 1756 | SCOPED_TRACE(t.timestring); |
| 1757 | CBS cbs; |
| 1758 | CBS_init(&cbs, (const uint8_t *)t.timestring, strlen(t.timestring)); |
| 1759 | EXPECT_FALSE(CBS_parse_generalized_time(&cbs, NULL, |
| 1760 | /*allow_timezone_offset=*/0)); |
| 1761 | EXPECT_FALSE(CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)); |
| 1762 | } |
| 1763 | static const struct { |
| 1764 | const char *timestring; |
| 1765 | } kUTCTZTests[] = { |
| 1766 | {"480711220333-0700"}, {"140704000000-0700"}, {"480222202332-0500"}, |
| 1767 | {"480726113216-0000"}, {"480726113216-2359"}, |
| 1768 | }; |
| 1769 | for (const auto &t : kUTCTZTests) { |
| 1770 | SCOPED_TRACE(t.timestring); |
| 1771 | CBS cbs; |
| 1772 | CBS_init(&cbs, (const uint8_t *)t.timestring, strlen(t.timestring)); |
| 1773 | EXPECT_FALSE(CBS_parse_generalized_time(&cbs, NULL, |
| 1774 | /*allow_timezone_offset=*/0)); |
| 1775 | EXPECT_FALSE(CBS_parse_generalized_time(&cbs, NULL, |
| 1776 | /*allow_timezone_offset=*/1)); |
| 1777 | EXPECT_TRUE(CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)); |
| 1778 | EXPECT_FALSE(CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/0)); |
| 1779 | } |
| 1780 | static const struct { |
| 1781 | const char *timestring; |
| 1782 | } kBogusUTCTZTests[] = { |
| 1783 | {"480711220333-0160"}, |
| 1784 | {"140704000000-9999"}, |
| 1785 | {"480222202332-2400"}, |
| 1786 | }; |
| 1787 | for (const auto &t : kBogusUTCTZTests) { |
| 1788 | SCOPED_TRACE(t.timestring); |
| 1789 | CBS cbs; |
| 1790 | CBS_init(&cbs, (const uint8_t *)t.timestring, strlen(t.timestring)); |
| 1791 | EXPECT_FALSE(CBS_parse_generalized_time(&cbs, NULL, |
| 1792 | /*allow_timezone_offset=*/0)); |
| 1793 | EXPECT_FALSE(CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)); |
| 1794 | } |
| 1795 | static const struct { |
| 1796 | const char *timestring; |
| 1797 | } kGenTZTests[] = { |
| 1798 | {"20480711220333-0000"}, |
| 1799 | {"20140704000000-0100"}, |
| 1800 | {"20460311174630-0300"}, |
| 1801 | {"20140704000000-2359"}, |
| 1802 | }; |
| 1803 | for (const auto &t : kGenTZTests) { |
| 1804 | SCOPED_TRACE(t.timestring); |
| 1805 | CBS cbs; |
| 1806 | CBS_init(&cbs, (const uint8_t *)t.timestring, strlen(t.timestring)); |
| 1807 | EXPECT_FALSE(CBS_parse_generalized_time(&cbs, NULL, |
| 1808 | /*allow_timezone_offset=*/0)); |
| 1809 | EXPECT_TRUE(CBS_parse_generalized_time(&cbs, NULL, |
| 1810 | /*allow_timezone_offset=*/1)); |
| 1811 | EXPECT_FALSE(CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)); |
| 1812 | EXPECT_FALSE(CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/0)); |
| 1813 | } |
| 1814 | static const struct { |
| 1815 | const char *timestring; |
| 1816 | } kBogusGenTZTests[] = { |
| 1817 | {"20480222202332-2400"}, |
| 1818 | {"20140704000000-9999"}, |
| 1819 | {"20480726113216-0160"}, |
| 1820 | }; |
| 1821 | for (const auto &t : kBogusGenTZTests) { |
| 1822 | SCOPED_TRACE(t.timestring); |
| 1823 | CBS cbs; |
| 1824 | CBS_init(&cbs, (const uint8_t *)t.timestring, strlen(t.timestring)); |
| 1825 | EXPECT_FALSE(CBS_parse_generalized_time(&cbs, NULL, |
| 1826 | /*allow_timezone_offset=*/0)); |
| 1827 | EXPECT_FALSE(CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)); |
| 1828 | } |
| 1829 | } |
| 1830 |
| 1831 | TEST(CBSTest, GetU64Decimal) { |
| 1832 | const struct { |
| 1833 | uint64_t val; |
| 1834 | const char *text; |
| 1835 | } kTests[] = { |
| 1836 | {0, "0"}, |
| 1837 | {1, "1"}, |
| 1838 | {123456, "123456"}, |
| 1839 | // 2^64 - 1 |
| 1840 | {UINT64_C(18446744073709551615), "18446744073709551615"}, |
| 1841 | }; |
| 1842 | for (const auto &t : kTests) { |
| 1843 | SCOPED_TRACE(t.text); |
| 1844 | CBS cbs; |
| 1845 | CBS_init(&cbs, reinterpret_cast<const uint8_t *>(t.text), strlen(t.text)); |
| 1846 | uint64_t v; |
| 1847 | ASSERT_TRUE(CBS_get_u64_decimal(&cbs, &v)); |
| 1848 | EXPECT_EQ(v, t.val); |
| 1849 | EXPECT_EQ(CBS_data(&cbs), |
| 1850 | reinterpret_cast<const uint8_t *>(t.text) + strlen(t.text)); |
| 1851 | EXPECT_EQ(CBS_len(&cbs), 0u); |
| 1852 |
| 1853 | std::string str(t.text); |
| 1854 | str += "Z"; |
| 1855 | CBS_init(&cbs, reinterpret_cast<const uint8_t *>(str.data()), str.size()); |
| 1856 | ASSERT_TRUE(CBS_get_u64_decimal(&cbs, &v)); |
| 1857 | EXPECT_EQ(v, t.val); |
| 1858 | EXPECT_EQ(CBS_data(&cbs), |
| 1859 | reinterpret_cast<const uint8_t *>(str.data()) + strlen(t.text)); |
| 1860 | EXPECT_EQ(CBS_len(&cbs), 1u); |
| 1861 | } |
| 1862 |
| 1863 | static const char *kInvalidTests[] = { |
| 1864 | "", |
| 1865 | "nope", |
| 1866 | "-1", |
| 1867 | // 2^64 |
| 1868 | "18446744073709551616", |
| 1869 | // Overflows at multiplying by 10. |
| 1870 | "18446744073709551620", |
| 1871 | }; |
| 1872 | for (const char *invalid : kInvalidTests) { |
| 1873 | SCOPED_TRACE(invalid); |
| 1874 | CBS cbs; |
| 1875 | CBS_init(&cbs, reinterpret_cast<const uint8_t *>(invalid), strlen(invalid)); |
| 1876 | uint64_t v; |
| 1877 | EXPECT_FALSE(CBS_get_u64_decimal(&cbs, &v)); |
| 1878 | } |
| 1879 | } |
| 1880 |
| 1881 | } // namespace |
| 1882 |