| 1 |
// Copyright (c) 2016, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <limits.h> |
| 5 |
#include <stdio.h> |
| 6 |
|
| 7 |
#include <map> |
| 8 |
#include <string> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
#include <gtest/gtest.h> |
| 12 |
|
| 13 |
#include <openssl/asn1.h> |
| 14 |
#include <openssl/asn1t.h> |
| 15 |
#include <openssl/bio.h> |
| 16 |
#include <openssl/bytestring.h> |
| 17 |
#include <openssl/err.h> |
| 18 |
#include <openssl/mem.h> |
| 19 |
#include <openssl/obj.h> |
| 20 |
#include <openssl/span.h> |
| 21 |
#include <openssl/x509v3.h> |
| 22 |
|
| 23 |
#include "../test/test_util.h" |
| 24 |
#include "internal.h" |
| 25 |
|
| 26 |
#if defined(OPENSSL_THREADS) |
| 27 |
#include <thread> |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
// kTag128 is an ASN.1 structure with a universal tag with number 128. |
| 32 |
static const uint8_t kTag128[] = { |
| 33 |
0x1f, 0x81, 0x00, 0x01, 0x00, |
| 34 |
}; |
| 35 |
|
| 36 |
// kTag258 is an ASN.1 structure with a universal tag with number 258. |
| 37 |
static const uint8_t kTag258[] = { |
| 38 |
0x1f, 0x82, 0x02, 0x01, 0x00, |
| 39 |
}; |
| 40 |
|
| 41 |
static_assert(V_ASN1_NEG_INTEGER == 258, |
| 42 |
"V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it."); |
| 43 |
|
| 44 |
// kTagOverflow is an ASN.1 structure with a universal tag with number 2^35-1, |
| 45 |
// which will not fit in an int. |
| 46 |
static const uint8_t kTagOverflow[] = { |
| 47 |
0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00, |
| 48 |
}; |
| 49 |
|
| 50 |
TEST(ASN1Test, LargeTags) { |
| 51 |
const uint8_t *p = kTag258; |
| 52 |
bssl::UniquePtr<ASN1_TYPE> obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258))); |
| 53 |
EXPECT_FALSE(obj) << "Parsed value with illegal tag" << obj->type; |
| 54 |
ERR_clear_error(); |
| 55 |
|
| 56 |
p = kTagOverflow; |
| 57 |
obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTagOverflow))); |
| 58 |
EXPECT_FALSE(obj) << "Parsed value with tag overflow" << obj->type; |
| 59 |
ERR_clear_error(); |
| 60 |
|
| 61 |
p = kTag128; |
| 62 |
obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag128))); |
| 63 |
ASSERT_TRUE(obj); |
| 64 |
EXPECT_EQ(128, obj->type); |
| 65 |
const uint8_t kZero = 0; |
| 66 |
EXPECT_EQ(Bytes(&kZero, 1), Bytes(obj->value.asn1_string->data, |
| 67 |
obj->value.asn1_string->length)); |
| 68 |
} |
| 69 |
|
| 70 |
// |obj| and |i2d_func| require different template parameters because C++ may |
| 71 |
// deduce, say, |ASN1_STRING*| via |obj| and |const ASN1_STRING*| via |
| 72 |
// |i2d_func|. Template argument deduction then fails. The language is not able |
| 73 |
// to resolve this by observing that |const ASN1_STRING*| works for both. |
| 74 |
template <typename T, typename U> |
| 75 |
void TestSerialize(T obj, int (*i2d_func)(U a, uint8_t **pp), |
| 76 |
bssl::Span<const uint8_t> expected) { |
| 77 |
static_assert(std::is_convertible<T, U>::value, |
| 78 |
"incompatible parameter to i2d_func"); |
| 79 |
// Test the allocating version first. It is easiest to debug. |
| 80 |
uint8_t *ptr = nullptr; |
| 81 |
int len = i2d_func(obj, &ptr); |
| 82 |
ASSERT_GT(len, 0); |
| 83 |
EXPECT_EQ(Bytes(expected), Bytes(ptr, len)); |
| 84 |
OPENSSL_free(ptr); |
| 85 |
|
| 86 |
len = i2d_func(obj, nullptr); |
| 87 |
ASSERT_GT(len, 0); |
| 88 |
EXPECT_EQ(len, static_cast<int>(expected.size())); |
| 89 |
|
| 90 |
std::vector<uint8_t> buf(len); |
| 91 |
ptr = buf.data(); |
| 92 |
len = i2d_func(obj, &ptr); |
| 93 |
ASSERT_EQ(len, static_cast<int>(expected.size())); |
| 94 |
EXPECT_EQ(ptr, buf.data() + buf.size()); |
| 95 |
EXPECT_EQ(Bytes(expected), Bytes(buf)); |
| 96 |
} |
| 97 |
|
| 98 |
static bssl::UniquePtr<BIGNUM> BIGNUMPow2(unsigned bit) { |
| 99 |
bssl::UniquePtr<BIGNUM> bn(BN_new()); |
| 100 |
if (!bn || |
| 101 |
!BN_set_bit(bn.get(), bit)) { |
| 102 |
return nullptr; |
| 103 |
} |
| 104 |
return bn; |
| 105 |
} |
| 106 |
|
| 107 |
TEST(ASN1Test, Integer) { |
| 108 |
bssl::UniquePtr<BIGNUM> int64_min = BIGNUMPow2(63); |
| 109 |
ASSERT_TRUE(int64_min); |
| 110 |
BN_set_negative(int64_min.get(), 1); |
| 111 |
|
| 112 |
bssl::UniquePtr<BIGNUM> int64_max = BIGNUMPow2(63); |
| 113 |
ASSERT_TRUE(int64_max); |
| 114 |
ASSERT_TRUE(BN_sub_word(int64_max.get(), 1)); |
| 115 |
|
| 116 |
bssl::UniquePtr<BIGNUM> int32_min = BIGNUMPow2(31); |
| 117 |
ASSERT_TRUE(int32_min); |
| 118 |
BN_set_negative(int32_min.get(), 1); |
| 119 |
|
| 120 |
bssl::UniquePtr<BIGNUM> int32_max = BIGNUMPow2(31); |
| 121 |
ASSERT_TRUE(int32_max); |
| 122 |
ASSERT_TRUE(BN_sub_word(int32_max.get(), 1)); |
| 123 |
|
| 124 |
struct { |
| 125 |
// der is the DER encoding of the INTEGER, including the tag and length. |
| 126 |
std::vector<uint8_t> der; |
| 127 |
// type and data are the corresponding fields of the |ASN1_STRING| |
| 128 |
// representation. |
| 129 |
int type; |
| 130 |
std::vector<uint8_t> data; |
| 131 |
// bn_asc is the |BIGNUM| representation, as parsed by the |BN_asc2bn| |
| 132 |
// function. |
| 133 |
const char *bn_asc; |
| 134 |
} kTests[] = { |
| 135 |
// -2^64 - 1 |
| 136 |
{{0x02, 0x09, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 137 |
V_ASN1_NEG_INTEGER, |
| 138 |
{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, |
| 139 |
"-0x10000000000000001"}, |
| 140 |
// -2^64 |
| 141 |
{{0x02, 0x09, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 142 |
V_ASN1_NEG_INTEGER, |
| 143 |
{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 144 |
"-0x10000000000000000"}, |
| 145 |
// -2^64 + 1 |
| 146 |
{{0x02, 0x09, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, |
| 147 |
V_ASN1_NEG_INTEGER, |
| 148 |
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 149 |
"-0xffffffffffffffff"}, |
| 150 |
// -2^63 - 1 |
| 151 |
{{0x02, 0x09, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 152 |
V_ASN1_NEG_INTEGER, |
| 153 |
{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, |
| 154 |
"-0x8000000000000001"}, |
| 155 |
// -2^63 (INT64_MIN) |
| 156 |
{{0x02, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 157 |
V_ASN1_NEG_INTEGER, |
| 158 |
{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 159 |
"-0x8000000000000000"}, |
| 160 |
// -2^63 + 1 |
| 161 |
{{0x02, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, |
| 162 |
V_ASN1_NEG_INTEGER, |
| 163 |
{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 164 |
"-0x7fffffffffffffff"}, |
| 165 |
// -2^32 - 1 |
| 166 |
{{0x02, 0x05, 0xfe, 0xff, 0xff, 0xff, 0xff}, |
| 167 |
V_ASN1_NEG_INTEGER, |
| 168 |
{0x01, 0x00, 0x00, 0x00, 0x01}, |
| 169 |
"-0x100000001"}, |
| 170 |
// -2^32 |
| 171 |
{{0x02, 0x05, 0xff, 0x00, 0x00, 0x00, 0x00}, |
| 172 |
V_ASN1_NEG_INTEGER, |
| 173 |
{0x01, 0x00, 0x00, 0x00, 0x00}, |
| 174 |
"-0x100000000"}, |
| 175 |
// -2^32 + 1 |
| 176 |
{{0x02, 0x05, 0xff, 0x00, 0x00, 0x00, 0x01}, |
| 177 |
V_ASN1_NEG_INTEGER, |
| 178 |
{0xff, 0xff, 0xff, 0xff}, |
| 179 |
"-0xffffffff"}, |
| 180 |
// -2^31 - 1 |
| 181 |
{{0x02, 0x05, 0xff, 0x7f, 0xff, 0xff, 0xff}, |
| 182 |
V_ASN1_NEG_INTEGER, |
| 183 |
{0x80, 0x00, 0x00, 0x01}, |
| 184 |
"-0x80000001"}, |
| 185 |
// -2^31 (INT32_MIN) |
| 186 |
{{0x02, 0x04, 0x80, 0x00, 0x00, 0x00}, |
| 187 |
V_ASN1_NEG_INTEGER, |
| 188 |
{0x80, 0x00, 0x00, 0x00}, |
| 189 |
"-0x80000000"}, |
| 190 |
// -2^31 + 1 |
| 191 |
{{0x02, 0x04, 0x80, 0x00, 0x00, 0x01}, |
| 192 |
V_ASN1_NEG_INTEGER, |
| 193 |
{0x7f, 0xff, 0xff, 0xff}, |
| 194 |
"-0x7fffffff"}, |
| 195 |
// -257 |
| 196 |
{{0x02, 0x02, 0xfe, 0xff}, V_ASN1_NEG_INTEGER, {0x01, 0x01}, "-257"}, |
| 197 |
// -256 |
| 198 |
{{0x02, 0x02, 0xff, 0x00}, V_ASN1_NEG_INTEGER, {0x01, 0x00}, "-256"}, |
| 199 |
// -255 |
| 200 |
{{0x02, 0x02, 0xff, 0x01}, V_ASN1_NEG_INTEGER, {0xff}, "-255"}, |
| 201 |
// -129 |
| 202 |
{{0x02, 0x02, 0xff, 0x7f}, V_ASN1_NEG_INTEGER, {0x81}, "-129"}, |
| 203 |
// -128 |
| 204 |
{{0x02, 0x01, 0x80}, V_ASN1_NEG_INTEGER, {0x80}, "-128"}, |
| 205 |
// -127 |
| 206 |
{{0x02, 0x01, 0x81}, V_ASN1_NEG_INTEGER, {0x7f}, "-127"}, |
| 207 |
// -1 |
| 208 |
{{0x02, 0x01, 0xff}, V_ASN1_NEG_INTEGER, {0x01}, "-1"}, |
| 209 |
// 0 |
| 210 |
{{0x02, 0x01, 0x00}, V_ASN1_INTEGER, {}, "0"}, |
| 211 |
// 1 |
| 212 |
{{0x02, 0x01, 0x01}, V_ASN1_INTEGER, {0x01}, "1"}, |
| 213 |
// 127 |
| 214 |
{{0x02, 0x01, 0x7f}, V_ASN1_INTEGER, {0x7f}, "127"}, |
| 215 |
// 128 |
| 216 |
{{0x02, 0x02, 0x00, 0x80}, V_ASN1_INTEGER, {0x80}, "128"}, |
| 217 |
// 129 |
| 218 |
{{0x02, 0x02, 0x00, 0x81}, V_ASN1_INTEGER, {0x81}, "129"}, |
| 219 |
// 255 |
| 220 |
{{0x02, 0x02, 0x00, 0xff}, V_ASN1_INTEGER, {0xff}, "255"}, |
| 221 |
// 256 |
| 222 |
{{0x02, 0x02, 0x01, 0x00}, V_ASN1_INTEGER, {0x01, 0x00}, "256"}, |
| 223 |
// 257 |
| 224 |
{{0x02, 0x02, 0x01, 0x01}, V_ASN1_INTEGER, {0x01, 0x01}, "257"}, |
| 225 |
// 2^31 - 2 |
| 226 |
{{0x02, 0x04, 0x7f, 0xff, 0xff, 0xfe}, |
| 227 |
V_ASN1_INTEGER, |
| 228 |
{0x7f, 0xff, 0xff, 0xfe}, |
| 229 |
"0x7ffffffe"}, |
| 230 |
// 2^31 - 1 (INT32_MAX) |
| 231 |
{{0x02, 0x04, 0x7f, 0xff, 0xff, 0xff}, |
| 232 |
V_ASN1_INTEGER, |
| 233 |
{0x7f, 0xff, 0xff, 0xff}, |
| 234 |
"0x7fffffff"}, |
| 235 |
// 2^31 |
| 236 |
{{0x02, 0x05, 0x00, 0x80, 0x00, 0x00, 0x00}, |
| 237 |
V_ASN1_INTEGER, |
| 238 |
{0x80, 0x00, 0x00, 0x00}, |
| 239 |
"0x80000000"}, |
| 240 |
// 2^32 - 2 |
| 241 |
{{0x02, 0x05, 0x00, 0xff, 0xff, 0xff, 0xfe}, |
| 242 |
V_ASN1_INTEGER, |
| 243 |
{0xff, 0xff, 0xff, 0xfe}, |
| 244 |
"0xfffffffe"}, |
| 245 |
// 2^32 - 1 (UINT32_MAX) |
| 246 |
{{0x02, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff}, |
| 247 |
V_ASN1_INTEGER, |
| 248 |
{0xff, 0xff, 0xff, 0xff}, |
| 249 |
"0xffffffff"}, |
| 250 |
// 2^32 |
| 251 |
{{0x02, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 252 |
V_ASN1_INTEGER, |
| 253 |
{0x01, 0x00, 0x00, 0x00, 0x00}, |
| 254 |
"0x100000000"}, |
| 255 |
// 2^63 - 2 |
| 256 |
{{0x02, 0x08, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, |
| 257 |
V_ASN1_INTEGER, |
| 258 |
{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, |
| 259 |
"0x7ffffffffffffffe"}, |
| 260 |
// 2^63 - 1 (INT64_MAX) |
| 261 |
{{0x02, 0x08, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 262 |
V_ASN1_INTEGER, |
| 263 |
{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 264 |
"0x7fffffffffffffff"}, |
| 265 |
// 2^63 |
| 266 |
{{0x02, 0x09, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 267 |
V_ASN1_INTEGER, |
| 268 |
{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 269 |
"0x8000000000000000"}, |
| 270 |
// 2^64 - 2 |
| 271 |
{{0x02, 0x09, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, |
| 272 |
V_ASN1_INTEGER, |
| 273 |
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, |
| 274 |
"0xfffffffffffffffe"}, |
| 275 |
// 2^64 - 1 (UINT64_MAX) |
| 276 |
{{0x02, 0x09, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 277 |
V_ASN1_INTEGER, |
| 278 |
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 279 |
"0xffffffffffffffff"}, |
| 280 |
// 2^64 |
| 281 |
{{0x02, 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 282 |
V_ASN1_INTEGER, |
| 283 |
{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 284 |
"0x10000000000000000"}, |
| 285 |
// 2^64 + 1 |
| 286 |
{{0x02, 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, |
| 287 |
V_ASN1_INTEGER, |
| 288 |
{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, |
| 289 |
"0x10000000000000001"}, |
| 290 |
}; |
| 291 |
|
| 292 |
for (const auto &t : kTests) { |
| 293 |
SCOPED_TRACE(t.bn_asc); |
| 294 |
// Collect a map of different ways to construct the integer. The key is the |
| 295 |
// method used and is only retained to aid debugging. |
| 296 |
std::map<std::string, bssl::UniquePtr<ASN1_INTEGER>> objs; |
| 297 |
|
| 298 |
// Construct |ASN1_INTEGER| by setting the type and data manually. |
| 299 |
bssl::UniquePtr<ASN1_INTEGER> by_data(ASN1_STRING_type_new(t.type)); |
| 300 |
ASSERT_TRUE(by_data); |
| 301 |
ASSERT_TRUE(ASN1_STRING_set(by_data.get(), t.data.data(), t.data.size())); |
| 302 |
objs["data"] = std::move(by_data); |
| 303 |
|
| 304 |
// Construct |ASN1_INTEGER| from a |BIGNUM|. |
| 305 |
BIGNUM *bn_raw = nullptr; |
| 306 |
ASSERT_TRUE(BN_asc2bn(&bn_raw, t.bn_asc)); |
| 307 |
bssl::UniquePtr<BIGNUM> bn(bn_raw); |
| 308 |
bssl::UniquePtr<ASN1_INTEGER> by_bn(BN_to_ASN1_INTEGER(bn.get(), nullptr)); |
| 309 |
ASSERT_TRUE(by_bn); |
| 310 |
objs["bn"] = std::move(by_bn); |
| 311 |
|
| 312 |
// Construct |ASN1_INTEGER| from decoding. |
| 313 |
const uint8_t *ptr = t.der.data(); |
| 314 |
bssl::UniquePtr<ASN1_INTEGER> by_der( |
| 315 |
d2i_ASN1_INTEGER(nullptr, &ptr, t.der.size())); |
| 316 |
ASSERT_TRUE(by_der); |
| 317 |
EXPECT_EQ(ptr, t.der.data() + t.der.size()); |
| 318 |
objs["der"] = std::move(by_der); |
| 319 |
|
| 320 |
// Construct |ASN1_INTEGER| from various C types, if it fits. |
| 321 |
bool fits_in_long = false, fits_in_i64 = false, fits_in_u64 = false; |
| 322 |
uint64_t u64 = 0; |
| 323 |
int64_t i64 = 0; |
| 324 |
long l = 0; |
| 325 |
uint64_t abs_u64; |
| 326 |
if (BN_get_u64(bn.get(), &abs_u64)) { |
| 327 |
fits_in_u64 = !BN_is_negative(bn.get()); |
| 328 |
if (fits_in_u64) { |
| 329 |
u64 = abs_u64; |
| 330 |
bssl::UniquePtr<ASN1_INTEGER> by_u64(ASN1_INTEGER_new()); |
| 331 |
ASSERT_TRUE(by_u64); |
| 332 |
ASSERT_TRUE(ASN1_INTEGER_set_uint64(by_u64.get(), u64)); |
| 333 |
objs["u64"] = std::move(by_u64); |
| 334 |
} |
| 335 |
|
| 336 |
fits_in_i64 = BN_cmp(int64_min.get(), bn.get()) <= 0 && |
| 337 |
BN_cmp(bn.get(), int64_max.get()) <= 0; |
| 338 |
if (fits_in_i64) { |
| 339 |
if (BN_is_negative(bn.get())) { |
| 340 |
i64 = static_cast<int64_t>(0u - abs_u64); |
| 341 |
} else { |
| 342 |
i64 = static_cast<int64_t>(abs_u64); |
| 343 |
} |
| 344 |
bssl::UniquePtr<ASN1_INTEGER> by_i64(ASN1_INTEGER_new()); |
| 345 |
ASSERT_TRUE(by_i64); |
| 346 |
ASSERT_TRUE(ASN1_INTEGER_set_int64(by_i64.get(), i64)); |
| 347 |
objs["i64"] = std::move(by_i64); |
| 348 |
} |
| 349 |
|
| 350 |
if (sizeof(long) == 8) { |
| 351 |
fits_in_long = fits_in_i64; |
| 352 |
} else { |
| 353 |
ASSERT_EQ(4u, sizeof(long)); |
| 354 |
fits_in_long = BN_cmp(int32_min.get(), bn.get()) <= 0 && |
| 355 |
BN_cmp(bn.get(), int32_max.get()) <= 0; |
| 356 |
} |
| 357 |
if (fits_in_long) { |
| 358 |
l = static_cast<long>(i64); |
| 359 |
bssl::UniquePtr<ASN1_INTEGER> by_long(ASN1_INTEGER_new()); |
| 360 |
ASSERT_TRUE(by_long); |
| 361 |
ASSERT_TRUE(ASN1_INTEGER_set(by_long.get(), l)); |
| 362 |
objs["long"] = std::move(by_long); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
// Default construction should return the zero |ASN1_INTEGER|. |
| 367 |
if (BN_is_zero(bn.get())) { |
| 368 |
bssl::UniquePtr<ASN1_INTEGER> by_default(ASN1_INTEGER_new()); |
| 369 |
ASSERT_TRUE(by_default); |
| 370 |
objs["default"] = std::move(by_default); |
| 371 |
} |
| 372 |
|
| 373 |
// Test that every |ASN1_INTEGER| constructed behaves as expected. |
| 374 |
for (const auto &pair : objs) { |
| 375 |
// The fields should be as expected. |
| 376 |
SCOPED_TRACE(pair.first); |
| 377 |
const ASN1_INTEGER *obj = pair.second.get(); |
| 378 |
EXPECT_EQ(t.type, ASN1_STRING_type(obj)); |
| 379 |
EXPECT_EQ(Bytes(t.data), Bytes(ASN1_STRING_get0_data(obj), |
| 380 |
ASN1_STRING_length(obj))); |
| 381 |
|
| 382 |
// The object should encode correctly. |
| 383 |
TestSerialize(obj, i2d_ASN1_INTEGER, t.der); |
| 384 |
|
| 385 |
bssl::UniquePtr<BIGNUM> bn2(ASN1_INTEGER_to_BN(obj, nullptr)); |
| 386 |
ASSERT_TRUE(bn2); |
| 387 |
EXPECT_EQ(0, BN_cmp(bn.get(), bn2.get())); |
| 388 |
|
| 389 |
if (fits_in_u64) { |
| 390 |
uint64_t v; |
| 391 |
ASSERT_TRUE(ASN1_INTEGER_get_uint64(&v, obj)); |
| 392 |
EXPECT_EQ(v, u64); |
| 393 |
} else { |
| 394 |
uint64_t v; |
| 395 |
EXPECT_FALSE(ASN1_INTEGER_get_uint64(&v, obj)); |
| 396 |
} |
| 397 |
|
| 398 |
if (fits_in_i64) { |
| 399 |
int64_t v; |
| 400 |
ASSERT_TRUE(ASN1_INTEGER_get_int64(&v, obj)); |
| 401 |
EXPECT_EQ(v, i64); |
| 402 |
} else { |
| 403 |
int64_t v; |
| 404 |
EXPECT_FALSE(ASN1_INTEGER_get_int64(&v, obj)); |
| 405 |
} |
| 406 |
|
| 407 |
if (fits_in_long) { |
| 408 |
EXPECT_EQ(l, ASN1_INTEGER_get(obj)); |
| 409 |
} else { |
| 410 |
EXPECT_EQ(-1, ASN1_INTEGER_get(obj)); |
| 411 |
} |
| 412 |
|
| 413 |
// All variations of integers should compare as equal to each other, as |
| 414 |
// strings or integers. (Functions like |ASN1_TYPE_cmp| rely on |
| 415 |
// string-based comparison.) |
| 416 |
for (const auto &pair2 : objs) { |
| 417 |
SCOPED_TRACE(pair2.first); |
| 418 |
EXPECT_EQ(0, ASN1_INTEGER_cmp(obj, pair2.second.get())); |
| 419 |
EXPECT_EQ(0, ASN1_STRING_cmp(obj, pair2.second.get())); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
// Although our parsers will never output non-minimal |ASN1_INTEGER|s, it is |
| 424 |
// possible to construct them manually. They should encode correctly. |
| 425 |
std::vector<uint8_t> data = t.data; |
| 426 |
const int kMaxExtraBytes = 5; |
| 427 |
for (int i = 0; i < kMaxExtraBytes; i++) { |
| 428 |
data.insert(data.begin(), 0x00); |
| 429 |
SCOPED_TRACE(Bytes(data)); |
| 430 |
|
| 431 |
bssl::UniquePtr<ASN1_INTEGER> non_minimal(ASN1_STRING_type_new(t.type)); |
| 432 |
ASSERT_TRUE(non_minimal); |
| 433 |
ASSERT_TRUE(ASN1_STRING_set(non_minimal.get(), data.data(), data.size())); |
| 434 |
|
| 435 |
TestSerialize(non_minimal.get(), i2d_ASN1_INTEGER, t.der); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTests); i++) { |
| 440 |
SCOPED_TRACE(Bytes(kTests[i].der)); |
| 441 |
const uint8_t *ptr = kTests[i].der.data(); |
| 442 |
bssl::UniquePtr<ASN1_INTEGER> a( |
| 443 |
d2i_ASN1_INTEGER(nullptr, &ptr, kTests[i].der.size())); |
| 444 |
ASSERT_TRUE(a); |
| 445 |
for (size_t j = 0; j < OPENSSL_ARRAY_SIZE(kTests); j++) { |
| 446 |
SCOPED_TRACE(Bytes(kTests[j].der)); |
| 447 |
ptr = kTests[j].der.data(); |
| 448 |
bssl::UniquePtr<ASN1_INTEGER> b( |
| 449 |
d2i_ASN1_INTEGER(nullptr, &ptr, kTests[j].der.size())); |
| 450 |
ASSERT_TRUE(b); |
| 451 |
|
| 452 |
// |ASN1_INTEGER_cmp| should compare numerically. |ASN1_STRING_cmp| does |
| 453 |
// not but should preserve equality. |
| 454 |
if (i < j) { |
| 455 |
EXPECT_LT(ASN1_INTEGER_cmp(a.get(), b.get()), 0); |
| 456 |
EXPECT_NE(ASN1_STRING_cmp(a.get(), b.get()), 0); |
| 457 |
} else if (i > j) { |
| 458 |
EXPECT_GT(ASN1_INTEGER_cmp(a.get(), b.get()), 0); |
| 459 |
EXPECT_NE(ASN1_STRING_cmp(a.get(), b.get()), 0); |
| 460 |
} else { |
| 461 |
EXPECT_EQ(ASN1_INTEGER_cmp(a.get(), b.get()), 0); |
| 462 |
EXPECT_EQ(ASN1_STRING_cmp(a.get(), b.get()), 0); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
std::vector<uint8_t> kInvalidTests[] = { |
| 468 |
// The empty string is not an integer. |
| 469 |
{0x02, 0x00}, |
| 470 |
// Integers must be minimally-encoded. |
| 471 |
{0x02, 0x02, 0x00, 0x00}, |
| 472 |
{0x02, 0x02, 0x00, 0x7f}, |
| 473 |
{0x02, 0x02, 0xff, 0xff}, |
| 474 |
{0x02, 0x02, 0xff, 0x80}, |
| 475 |
}; |
| 476 |
for (const auto &invalid : kInvalidTests) { |
| 477 |
SCOPED_TRACE(Bytes(invalid)); |
| 478 |
|
| 479 |
const uint8_t *ptr = invalid.data(); |
| 480 |
bssl::UniquePtr<ASN1_INTEGER> integer( |
| 481 |
d2i_ASN1_INTEGER(nullptr, &ptr, invalid.size())); |
| 482 |
EXPECT_FALSE(integer); |
| 483 |
} |
| 484 |
|
| 485 |
// Callers expect |ASN1_INTEGER_get| and |ASN1_ENUMERATED_get| to return zero |
| 486 |
// given NULL. |
| 487 |
EXPECT_EQ(0, ASN1_INTEGER_get(nullptr)); |
| 488 |
EXPECT_EQ(0, ASN1_ENUMERATED_get(nullptr)); |
| 489 |
} |
| 490 |
|
| 491 |
// Although invalid, a negative zero should encode correctly. |
| 492 |
TEST(ASN1Test, NegativeZero) { |
| 493 |
bssl::UniquePtr<ASN1_INTEGER> neg_zero( |
| 494 |
ASN1_STRING_type_new(V_ASN1_NEG_INTEGER)); |
| 495 |
ASSERT_TRUE(neg_zero); |
| 496 |
EXPECT_EQ(0, ASN1_INTEGER_get(neg_zero.get())); |
| 497 |
|
| 498 |
static const uint8_t kDER[] = {0x02, 0x01, 0x00}; |
| 499 |
TestSerialize(neg_zero.get(), i2d_ASN1_INTEGER, kDER); |
| 500 |
} |
| 501 |
|
| 502 |
TEST(ASN1Test, SerializeObject) { |
| 503 |
static const uint8_t kDER[] = {0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 504 |
0xf7, 0x0d, 0x01, 0x01, 0x01}; |
| 505 |
const ASN1_OBJECT *obj = OBJ_nid2obj(NID_rsaEncryption); |
| 506 |
TestSerialize(obj, i2d_ASN1_OBJECT, kDER); |
| 507 |
} |
| 508 |
|
| 509 |
TEST(ASN1Test, Boolean) { |
| 510 |
static const uint8_t kTrue[] = {0x01, 0x01, 0xff}; |
| 511 |
TestSerialize(0xff, i2d_ASN1_BOOLEAN, kTrue); |
| 512 |
// Other constants are also correctly encoded as TRUE. |
| 513 |
TestSerialize(1, i2d_ASN1_BOOLEAN, kTrue); |
| 514 |
TestSerialize(0x100, i2d_ASN1_BOOLEAN, kTrue); |
| 515 |
|
| 516 |
const uint8_t *ptr = kTrue; |
| 517 |
EXPECT_EQ(0xff, d2i_ASN1_BOOLEAN(nullptr, &ptr, sizeof(kTrue))); |
| 518 |
EXPECT_EQ(ptr, kTrue + sizeof(kTrue)); |
| 519 |
|
| 520 |
static const uint8_t kFalse[] = {0x01, 0x01, 0x00}; |
| 521 |
TestSerialize(0x00, i2d_ASN1_BOOLEAN, kFalse); |
| 522 |
|
| 523 |
ptr = kFalse; |
| 524 |
EXPECT_EQ(0, d2i_ASN1_BOOLEAN(nullptr, &ptr, sizeof(kFalse))); |
| 525 |
EXPECT_EQ(ptr, kFalse + sizeof(kFalse)); |
| 526 |
|
| 527 |
const std::vector<uint8_t> kInvalidBooleans[] = { |
| 528 |
// No tag header. |
| 529 |
{}, |
| 530 |
// No length. |
| 531 |
{0x01}, |
| 532 |
// Truncated contents. |
| 533 |
{0x01, 0x01}, |
| 534 |
// Contents too short or too long. |
| 535 |
{0x01, 0x00}, |
| 536 |
{0x01, 0x02, 0x00, 0x00}, |
| 537 |
// Wrong tag number. |
| 538 |
{0x02, 0x01, 0x00}, |
| 539 |
// Wrong tag class. |
| 540 |
{0x81, 0x01, 0x00}, |
| 541 |
// Element is constructed. |
| 542 |
{0x21, 0x01, 0x00}, |
| 543 |
// Not a DER encoding of TRUE. |
| 544 |
{0x01, 0x01, 0x01}, |
| 545 |
// Non-minimal tag length. |
| 546 |
{0x01, 0x81, 0x01, 0xff}, |
| 547 |
}; |
| 548 |
for (const auto &invalid : kInvalidBooleans) { |
| 549 |
SCOPED_TRACE(Bytes(invalid)); |
| 550 |
ptr = invalid.data(); |
| 551 |
EXPECT_EQ(-1, d2i_ASN1_BOOLEAN(nullptr, &ptr, invalid.size())); |
| 552 |
ERR_clear_error(); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
// The templates go through a different codepath, so test them separately. |
| 557 |
TEST(ASN1Test, SerializeEmbeddedBoolean) { |
| 558 |
bssl::UniquePtr<BASIC_CONSTRAINTS> val(BASIC_CONSTRAINTS_new()); |
| 559 |
ASSERT_TRUE(val); |
| 560 |
|
| 561 |
// BasicConstraints defaults to FALSE, so the encoding should be empty. |
| 562 |
static const uint8_t kLeaf[] = {0x30, 0x00}; |
| 563 |
val->ca = 0; |
| 564 |
TestSerialize(val.get(), i2d_BASIC_CONSTRAINTS, kLeaf); |
| 565 |
|
| 566 |
// TRUE should always be encoded as 0xff, independent of what value the caller |
| 567 |
// placed in the |ASN1_BOOLEAN|. |
| 568 |
static const uint8_t kCA[] = {0x30, 0x03, 0x01, 0x01, 0xff}; |
| 569 |
val->ca = 0xff; |
| 570 |
TestSerialize(val.get(), i2d_BASIC_CONSTRAINTS, kCA); |
| 571 |
val->ca = 1; |
| 572 |
TestSerialize(val.get(), i2d_BASIC_CONSTRAINTS, kCA); |
| 573 |
val->ca = 0x100; |
| 574 |
TestSerialize(val.get(), i2d_BASIC_CONSTRAINTS, kCA); |
| 575 |
} |
| 576 |
|
| 577 |
TEST(ASN1Test, ASN1Type) { |
| 578 |
const struct { |
| 579 |
int type; |
| 580 |
std::vector<uint8_t> der; |
| 581 |
} kTests[] = { |
| 582 |
// BOOLEAN { TRUE } |
| 583 |
{V_ASN1_BOOLEAN, {0x01, 0x01, 0xff}}, |
| 584 |
// BOOLEAN { FALSE } |
| 585 |
{V_ASN1_BOOLEAN, {0x01, 0x01, 0x00}}, |
| 586 |
// OCTET_STRING { "a" } |
| 587 |
{V_ASN1_OCTET_STRING, {0x04, 0x01, 0x61}}, |
| 588 |
// OCTET_STRING { } |
| 589 |
{V_ASN1_OCTET_STRING, {0x04, 0x00}}, |
| 590 |
// BIT_STRING { `01` `00` } |
| 591 |
{V_ASN1_BIT_STRING, {0x03, 0x02, 0x01, 0x00}}, |
| 592 |
// INTEGER { -1 } |
| 593 |
{V_ASN1_INTEGER, {0x02, 0x01, 0xff}}, |
| 594 |
// OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.2 } |
| 595 |
{V_ASN1_OBJECT, |
| 596 |
{0x06, 0x0c, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, |
| 597 |
0x09, 0x02}}, |
| 598 |
// NULL {} |
| 599 |
{V_ASN1_NULL, {0x05, 0x00}}, |
| 600 |
// SEQUENCE {} |
| 601 |
{V_ASN1_SEQUENCE, {0x30, 0x00}}, |
| 602 |
// SET {} |
| 603 |
{V_ASN1_SET, {0x31, 0x00}}, |
| 604 |
// [0] { UTF8String { "a" } } |
| 605 |
{V_ASN1_OTHER, {0xa0, 0x03, 0x0c, 0x01, 0x61}}, |
| 606 |
}; |
| 607 |
for (const auto &t : kTests) { |
| 608 |
SCOPED_TRACE(Bytes(t.der)); |
| 609 |
|
| 610 |
// The input should successfully parse. |
| 611 |
const uint8_t *ptr = t.der.data(); |
| 612 |
bssl::UniquePtr<ASN1_TYPE> val(d2i_ASN1_TYPE(nullptr, &ptr, t.der.size())); |
| 613 |
ASSERT_TRUE(val); |
| 614 |
|
| 615 |
EXPECT_EQ(ASN1_TYPE_get(val.get()), t.type); |
| 616 |
EXPECT_EQ(val->type, t.type); |
| 617 |
TestSerialize(val.get(), i2d_ASN1_TYPE, t.der); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
// Test that reading |value.ptr| from a FALSE |ASN1_TYPE| behaves correctly. The |
| 622 |
// type historically supported this, so maintain the invariant in case external |
| 623 |
// code relies on it. |
| 624 |
TEST(ASN1Test, UnusedBooleanBits) { |
| 625 |
// OCTET_STRING { "a" } |
| 626 |
static const uint8_t kDER[] = {0x04, 0x01, 0x61}; |
| 627 |
const uint8_t *ptr = kDER; |
| 628 |
bssl::UniquePtr<ASN1_TYPE> val(d2i_ASN1_TYPE(nullptr, &ptr, sizeof(kDER))); |
| 629 |
ASSERT_TRUE(val); |
| 630 |
EXPECT_EQ(V_ASN1_OCTET_STRING, val->type); |
| 631 |
EXPECT_TRUE(val->value.ptr); |
| 632 |
|
| 633 |
// Set |val| to a BOOLEAN containing FALSE. |
| 634 |
ASN1_TYPE_set(val.get(), V_ASN1_BOOLEAN, NULL); |
| 635 |
EXPECT_EQ(V_ASN1_BOOLEAN, val->type); |
| 636 |
EXPECT_FALSE(val->value.ptr); |
| 637 |
} |
| 638 |
|
| 639 |
TEST(ASN1Test, ParseASN1Object) { |
| 640 |
// 1.2.840.113554.4.1.72585.2, an arbitrary unknown OID. |
| 641 |
static const uint8_t kOID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, |
| 642 |
0x04, 0x01, 0x84, 0xb7, 0x09, 0x02}; |
| 643 |
ASN1_OBJECT *obj = ASN1_OBJECT_create(NID_undef, kOID, sizeof(kOID), |
| 644 |
"short name", "long name"); |
| 645 |
ASSERT_TRUE(obj); |
| 646 |
|
| 647 |
// OBJECT_IDENTIFIER { 1.3.101.112 } |
| 648 |
static const uint8_t kDER[] = {0x06, 0x03, 0x2b, 0x65, 0x70}; |
| 649 |
const uint8_t *ptr = kDER; |
| 650 |
// Parse an |ASN1_OBJECT| with object reuse. |
| 651 |
EXPECT_TRUE(d2i_ASN1_OBJECT(&obj, &ptr, sizeof(kDER))); |
| 652 |
EXPECT_EQ(NID_ED25519, OBJ_obj2nid(obj)); |
| 653 |
ASN1_OBJECT_free(obj); |
| 654 |
|
| 655 |
// Repeat the test, this time overriding a static |ASN1_OBJECT|. It should |
| 656 |
// detect this and construct a new one. |
| 657 |
obj = OBJ_nid2obj(NID_rsaEncryption); |
| 658 |
ptr = kDER; |
| 659 |
EXPECT_TRUE(d2i_ASN1_OBJECT(&obj, &ptr, sizeof(kDER))); |
| 660 |
EXPECT_EQ(NID_ED25519, OBJ_obj2nid(obj)); |
| 661 |
ASN1_OBJECT_free(obj); |
| 662 |
|
| 663 |
const std::vector<uint8_t> kInvalidObjects[] = { |
| 664 |
// No tag header. |
| 665 |
{}, |
| 666 |
// No length. |
| 667 |
{0x06}, |
| 668 |
// Truncated contents. |
| 669 |
{0x06, 0x01}, |
| 670 |
// An OID may not be empty. |
| 671 |
{0x06, 0x00}, |
| 672 |
// The last byte may not be a continuation byte (high bit set). |
| 673 |
{0x06, 0x03, 0x2b, 0x65, 0xf0}, |
| 674 |
// Each component must be minimally-encoded. |
| 675 |
{0x06, 0x03, 0x2b, 0x65, 0x80, 0x70}, |
| 676 |
{0x06, 0x03, 0x80, 0x2b, 0x65, 0x70}, |
| 677 |
// Wrong tag number. |
| 678 |
{0x01, 0x03, 0x2b, 0x65, 0x70}, |
| 679 |
// Wrong tag class. |
| 680 |
{0x86, 0x03, 0x2b, 0x65, 0x70}, |
| 681 |
// Element is constructed. |
| 682 |
{0x26, 0x03, 0x2b, 0x65, 0x70}, |
| 683 |
// Non-minimal tag length. |
| 684 |
{0x06, 0x81, 0x03, 0x2b, 0x65, 0x70}, |
| 685 |
}; |
| 686 |
for (const auto &invalid : kInvalidObjects) { |
| 687 |
SCOPED_TRACE(Bytes(invalid)); |
| 688 |
ptr = invalid.data(); |
| 689 |
obj = d2i_ASN1_OBJECT(nullptr, &ptr, invalid.size()); |
| 690 |
EXPECT_FALSE(obj); |
| 691 |
ASN1_OBJECT_free(obj); |
| 692 |
ERR_clear_error(); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
TEST(ASN1Test, BitString) { |
| 697 |
const size_t kNotWholeBytes = static_cast<size_t>(-1); |
| 698 |
const struct { |
| 699 |
std::vector<uint8_t> in; |
| 700 |
size_t num_bytes; |
| 701 |
} kValidInputs[] = { |
| 702 |
// Empty bit string |
| 703 |
{{0x03, 0x01, 0x00}, 0}, |
| 704 |
// 0b1 |
| 705 |
{{0x03, 0x02, 0x07, 0x80}, kNotWholeBytes}, |
| 706 |
// 0b1010 |
| 707 |
{{0x03, 0x02, 0x04, 0xa0}, kNotWholeBytes}, |
| 708 |
// 0b1010101 |
| 709 |
{{0x03, 0x02, 0x01, 0xaa}, kNotWholeBytes}, |
| 710 |
// 0b10101010 |
| 711 |
{{0x03, 0x02, 0x00, 0xaa}, 1}, |
| 712 |
// Bits 0 and 63 are set |
| 713 |
{{0x03, 0x09, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, 8}, |
| 714 |
// 64 zero bits |
| 715 |
{{0x03, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8}, |
| 716 |
}; |
| 717 |
for (const auto &test : kValidInputs) { |
| 718 |
SCOPED_TRACE(Bytes(test.in)); |
| 719 |
// The input should parse and round-trip correctly. |
| 720 |
const uint8_t *ptr = test.in.data(); |
| 721 |
bssl::UniquePtr<ASN1_BIT_STRING> val( |
| 722 |
d2i_ASN1_BIT_STRING(nullptr, &ptr, test.in.size())); |
| 723 |
ASSERT_TRUE(val); |
| 724 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, test.in); |
| 725 |
|
| 726 |
// Check the byte count. |
| 727 |
size_t num_bytes; |
| 728 |
if (test.num_bytes == kNotWholeBytes) { |
| 729 |
EXPECT_FALSE(ASN1_BIT_STRING_num_bytes(val.get(), &num_bytes)); |
| 730 |
} else { |
| 731 |
ASSERT_TRUE(ASN1_BIT_STRING_num_bytes(val.get(), &num_bytes)); |
| 732 |
EXPECT_EQ(num_bytes, test.num_bytes); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
const std::vector<uint8_t> kInvalidInputs[] = { |
| 737 |
// Wrong tag |
| 738 |
{0x04, 0x01, 0x00}, |
| 739 |
// Missing leading byte |
| 740 |
{0x03, 0x00}, |
| 741 |
// Leading byte too high |
| 742 |
{0x03, 0x02, 0x08, 0x00}, |
| 743 |
{0x03, 0x02, 0xff, 0x00}, |
| 744 |
// Empty bit strings must have a zero leading byte. |
| 745 |
{0x03, 0x01, 0x01}, |
| 746 |
// Unused bits must all be zero. |
| 747 |
{0x03, 0x02, 0x06, 0xc1 /* 0b11000001 */}, |
| 748 |
}; |
| 749 |
for (const auto &test : kInvalidInputs) { |
| 750 |
SCOPED_TRACE(Bytes(test)); |
| 751 |
const uint8_t *ptr = test.data(); |
| 752 |
bssl::UniquePtr<ASN1_BIT_STRING> val( |
| 753 |
d2i_ASN1_BIT_STRING(nullptr, &ptr, test.size())); |
| 754 |
EXPECT_FALSE(val); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
TEST(ASN1Test, SetBit) { |
| 759 |
bssl::UniquePtr<ASN1_BIT_STRING> val(ASN1_BIT_STRING_new()); |
| 760 |
ASSERT_TRUE(val); |
| 761 |
static const uint8_t kBitStringEmpty[] = {0x03, 0x01, 0x00}; |
| 762 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitStringEmpty); |
| 763 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 0)); |
| 764 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 100)); |
| 765 |
|
| 766 |
// Set a few bits via |ASN1_BIT_STRING_set_bit|. |
| 767 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 0, 1)); |
| 768 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 1, 1)); |
| 769 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 2, 0)); |
| 770 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 3, 1)); |
| 771 |
static const uint8_t kBitString1101[] = {0x03, 0x02, 0x04, 0xd0}; |
| 772 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitString1101); |
| 773 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 0)); |
| 774 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 1)); |
| 775 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 2)); |
| 776 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 3)); |
| 777 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 4)); |
| 778 |
|
| 779 |
// Bits that were set may be cleared. |
| 780 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 1, 0)); |
| 781 |
static const uint8_t kBitString1001[] = {0x03, 0x02, 0x04, 0x90}; |
| 782 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitString1001); |
| 783 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 0)); |
| 784 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 1)); |
| 785 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 2)); |
| 786 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 3)); |
| 787 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 4)); |
| 788 |
|
| 789 |
// Clearing trailing bits truncates the string. |
| 790 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 3, 0)); |
| 791 |
static const uint8_t kBitString1[] = {0x03, 0x02, 0x07, 0x80}; |
| 792 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitString1); |
| 793 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 0)); |
| 794 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 1)); |
| 795 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 2)); |
| 796 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 3)); |
| 797 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 4)); |
| 798 |
|
| 799 |
// Bits may be set beyond the end of the string. |
| 800 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 63, 1)); |
| 801 |
static const uint8_t kBitStringLong[] = {0x03, 0x09, 0x00, 0x80, 0x00, 0x00, |
| 802 |
0x00, 0x00, 0x00, 0x00, 0x01}; |
| 803 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitStringLong); |
| 804 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 0)); |
| 805 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 62)); |
| 806 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 63)); |
| 807 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 64)); |
| 808 |
|
| 809 |
// The string can be truncated back down again. |
| 810 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 63, 0)); |
| 811 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitString1); |
| 812 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 0)); |
| 813 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 62)); |
| 814 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 63)); |
| 815 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 64)); |
| 816 |
|
| 817 |
// |ASN1_BIT_STRING_set_bit| also truncates when starting from a parsed |
| 818 |
// string. |
| 819 |
const uint8_t *ptr = kBitStringLong; |
| 820 |
val.reset(d2i_ASN1_BIT_STRING(nullptr, &ptr, sizeof(kBitStringLong))); |
| 821 |
ASSERT_TRUE(val); |
| 822 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitStringLong); |
| 823 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 63, 0)); |
| 824 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitString1); |
| 825 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 0)); |
| 826 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 62)); |
| 827 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 63)); |
| 828 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 64)); |
| 829 |
|
| 830 |
// A parsed bit string preserves trailing zero bits. |
| 831 |
static const uint8_t kBitString10010[] = {0x03, 0x02, 0x03, 0x90}; |
| 832 |
ptr = kBitString10010; |
| 833 |
val.reset(d2i_ASN1_BIT_STRING(nullptr, &ptr, sizeof(kBitString10010))); |
| 834 |
ASSERT_TRUE(val); |
| 835 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitString10010); |
| 836 |
// But |ASN1_BIT_STRING_set_bit| will truncate it even if otherwise a no-op. |
| 837 |
ASSERT_TRUE(ASN1_BIT_STRING_set_bit(val.get(), 0, 1)); |
| 838 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitString1001); |
| 839 |
EXPECT_EQ(1, ASN1_BIT_STRING_get_bit(val.get(), 0)); |
| 840 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 62)); |
| 841 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 63)); |
| 842 |
EXPECT_EQ(0, ASN1_BIT_STRING_get_bit(val.get(), 64)); |
| 843 |
|
| 844 |
// By default, a BIT STRING implicitly truncates trailing zeros. |
| 845 |
val.reset(ASN1_BIT_STRING_new()); |
| 846 |
ASSERT_TRUE(val); |
| 847 |
static const uint8_t kZeros[64] = {0}; |
| 848 |
ASSERT_TRUE(ASN1_STRING_set(val.get(), kZeros, sizeof(kZeros))); |
| 849 |
TestSerialize(val.get(), i2d_ASN1_BIT_STRING, kBitStringEmpty); |
| 850 |
} |
| 851 |
|
| 852 |
TEST(ASN1Test, StringToUTF8) { |
| 853 |
static const struct { |
| 854 |
std::vector<uint8_t> in; |
| 855 |
int type; |
| 856 |
const char *expected; |
| 857 |
} kTests[] = { |
| 858 |
// Non-minimal, two-byte UTF-8. |
| 859 |
{{0xc0, 0x81}, V_ASN1_UTF8STRING, nullptr}, |
| 860 |
// Non-minimal, three-byte UTF-8. |
| 861 |
{{0xe0, 0x80, 0x81}, V_ASN1_UTF8STRING, nullptr}, |
| 862 |
// Non-minimal, four-byte UTF-8. |
| 863 |
{{0xf0, 0x80, 0x80, 0x81}, V_ASN1_UTF8STRING, nullptr}, |
| 864 |
// Truncated, four-byte UTF-8. |
| 865 |
{{0xf0, 0x80, 0x80}, V_ASN1_UTF8STRING, nullptr}, |
| 866 |
// Low-surrogate value. |
| 867 |
{{0xed, 0xa0, 0x80}, V_ASN1_UTF8STRING, nullptr}, |
| 868 |
// High-surrogate value. |
| 869 |
{{0xed, 0xb0, 0x81}, V_ASN1_UTF8STRING, nullptr}, |
| 870 |
// Initial BOMs should be rejected from UCS-2 and UCS-4. |
| 871 |
{{0xfe, 0xff, 0, 88}, V_ASN1_BMPSTRING, nullptr}, |
| 872 |
{{0, 0, 0xfe, 0xff, 0, 0, 0, 88}, V_ASN1_UNIVERSALSTRING, nullptr}, |
| 873 |
// Otherwise, BOMs should pass through. |
| 874 |
{{0, 88, 0xfe, 0xff}, V_ASN1_BMPSTRING, "X\xef\xbb\xbf"}, |
| 875 |
{{0, 0, 0, 88, 0, 0, 0xfe, 0xff}, V_ASN1_UNIVERSALSTRING, |
| 876 |
"X\xef\xbb\xbf"}, |
| 877 |
// The maximum code-point should pass though. |
| 878 |
{{0, 16, 0xff, 0xfd}, V_ASN1_UNIVERSALSTRING, "\xf4\x8f\xbf\xbd"}, |
| 879 |
// Values outside the Unicode space should not. |
| 880 |
{{0, 17, 0, 0}, V_ASN1_UNIVERSALSTRING, nullptr}, |
| 881 |
// Non-characters should be rejected. |
| 882 |
{{0, 1, 0xff, 0xff}, V_ASN1_UNIVERSALSTRING, nullptr}, |
| 883 |
{{0, 1, 0xff, 0xfe}, V_ASN1_UNIVERSALSTRING, nullptr}, |
| 884 |
{{0, 0, 0xfd, 0xd5}, V_ASN1_UNIVERSALSTRING, nullptr}, |
| 885 |
// BMPString is UCS-2, not UTF-16, so surrogate pairs are invalid. |
| 886 |
{{0xd8, 0, 0xdc, 1}, V_ASN1_BMPSTRING, nullptr}, |
| 887 |
// INTEGERs are stored as strings, but cannot be converted to UTF-8. |
| 888 |
{{0x01}, V_ASN1_INTEGER, nullptr}, |
| 889 |
}; |
| 890 |
|
| 891 |
for (const auto &test : kTests) { |
| 892 |
SCOPED_TRACE(Bytes(test.in)); |
| 893 |
SCOPED_TRACE(test.type); |
| 894 |
bssl::UniquePtr<ASN1_STRING> s(ASN1_STRING_type_new(test.type)); |
| 895 |
ASSERT_TRUE(s); |
| 896 |
ASSERT_TRUE(ASN1_STRING_set(s.get(), test.in.data(), test.in.size())); |
| 897 |
|
| 898 |
uint8_t *utf8; |
| 899 |
const int utf8_len = ASN1_STRING_to_UTF8(&utf8, s.get()); |
| 900 |
EXPECT_EQ(utf8_len < 0, test.expected == nullptr); |
| 901 |
if (utf8_len >= 0) { |
| 902 |
if (test.expected != nullptr) { |
| 903 |
EXPECT_EQ(Bytes(test.expected), Bytes(utf8, utf8_len)); |
| 904 |
} |
| 905 |
OPENSSL_free(utf8); |
| 906 |
} else { |
| 907 |
ERR_clear_error(); |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
static std::string ASN1StringToStdString(const ASN1_STRING *str) { |
| 913 |
return std::string(ASN1_STRING_get0_data(str), |
| 914 |
ASN1_STRING_get0_data(str) + ASN1_STRING_length(str)); |
| 915 |
} |
| 916 |
|
| 917 |
static bool ASN1Time_check_posix(const ASN1_TIME *s, int64_t t) { |
| 918 |
struct tm stm, ttm; |
| 919 |
int day, sec; |
| 920 |
|
| 921 |
switch (ASN1_STRING_type(s)) { |
| 922 |
case V_ASN1_GENERALIZEDTIME: |
| 923 |
if (!asn1_generalizedtime_to_tm(&stm, s)) { |
| 924 |
return false; |
| 925 |
} |
| 926 |
break; |
| 927 |
case V_ASN1_UTCTIME: |
| 928 |
if (!asn1_utctime_to_tm(&stm, s, /*allow_timezone_offset=*/1)) { |
| 929 |
return false; |
| 930 |
} |
| 931 |
break; |
| 932 |
default: |
| 933 |
return false; |
| 934 |
} |
| 935 |
if (!OPENSSL_posix_to_tm(t, &ttm) || |
| 936 |
!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm)) { |
| 937 |
return false; |
| 938 |
} |
| 939 |
return day == 0 && sec ==0; |
| 940 |
} |
| 941 |
|
| 942 |
static std::string PrintStringToBIO(const ASN1_STRING *str, |
| 943 |
int (*print_func)(BIO *, |
| 944 |
const ASN1_STRING *)) { |
| 945 |
const uint8_t *data; |
| 946 |
size_t len; |
| 947 |
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 948 |
if (!bio || // |
| 949 |
!print_func(bio.get(), str) || |
| 950 |
!BIO_mem_contents(bio.get(), &data, &len)) { |
| 951 |
ADD_FAILURE() << "Could not print to BIO"; |
| 952 |
return ""; |
| 953 |
} |
| 954 |
return std::string(data, data + len); |
| 955 |
} |
| 956 |
|
| 957 |
// MSVC 2015 does not support compound literals e.g. using (struct tm){0,0,...}} |
| 958 |
// in the list of test vectors below. Note: this returns a copy of the stack |
| 959 |
// allocated value t. |
| 960 |
static struct tm make_tm(int sec, int min, int hour, int mday, int mon, int year, int wday, int yday, int isdst, long gmtoff, char* zone) { |
| 961 |
struct tm t; |
| 962 |
t.tm_sec = sec; |
| 963 |
t.tm_min = min; |
| 964 |
t.tm_hour = hour; |
| 965 |
t.tm_mday = mday; |
| 966 |
t.tm_mon = mon; |
| 967 |
t.tm_year = year; |
| 968 |
t.tm_wday = wday; |
| 969 |
t.tm_yday = yday; |
| 970 |
t.tm_isdst = isdst; |
| 971 |
#if defined(__GNU__) |
| 972 |
t.tm_gmtoff = gmtoff; |
| 973 |
t.tm_zone = zone; |
| 974 |
#endif |
| 975 |
return t; |
| 976 |
} |
| 977 |
|
| 978 |
TEST(ASN1Test, SetTime) { |
| 979 |
static const struct { |
| 980 |
int64_t time; |
| 981 |
const char *generalized; |
| 982 |
const char *utc; |
| 983 |
const char *printed; |
| 984 |
const struct tm expected_tm; |
| 985 |
// struct tm years are deltas from 1900 and months start at 0 |
| 986 |
// AWS-LC does not calculate or set year day, weekday, timezone, or daylight savings |
| 987 |
} kTests[] = { |
| 988 |
{-631152001, "19491231235959Z", nullptr, "Dec 31 23:59:59 1949 GMT", |
| 989 |
make_tm(59, 59, 23, 31, 11, 49, 0, 0, 0, 0, nullptr)}, |
| 990 |
{-631152000, "19500101000000Z", "500101000000Z", "Jan 1 00:00:00 1950 GMT", |
| 991 |
make_tm(0, 0, 0, 1, 0, 50, 0, 0, 0, 0, nullptr)}, |
| 992 |
{0, "19700101000000Z", "700101000000Z", "Jan 1 00:00:00 1970 GMT", |
| 993 |
make_tm(0, 0, 0, 1, 0, 70, 0, 0, 0, 0, nullptr)}, |
| 994 |
{981173106, "20010203040506Z", "010203040506Z", "Feb 3 04:05:06 2001 GMT", |
| 995 |
make_tm(6, 5, 4, 3, 1, 101, 0, 0, 0, 0, nullptr)}, |
| 996 |
{951804000, "20000229060000Z", "000229060000Z", "Feb 29 06:00:00 2000 GMT", |
| 997 |
make_tm(0, 0, 6, 29, 1, 100, 0, 0, 0, 0, nullptr)}, |
| 998 |
// NASA says this is the correct time for posterity. |
| 999 |
{-16751025, "19690621025615Z", "690621025615Z", "Jun 21 02:56:15 1969 GMT", |
| 1000 |
make_tm(15, 56, 2, 21, 5, 69, 0, 0, 0, 0, nullptr)}, |
| 1001 |
// -1 is sometimes used as an error value. Ensure we correctly handle it. |
| 1002 |
{-1, "19691231235959Z", "691231235959Z", "Dec 31 23:59:59 1969 GMT", |
| 1003 |
make_tm(59, 59, 23, 31, 11, 69, 0, 0, 0, 0, nullptr)}, |
| 1004 |
{2524607999, "20491231235959Z", "491231235959Z", "Dec 31 23:59:59 2049 GMT", |
| 1005 |
make_tm(59, 59, 23, 31, 11, 149, 0, 0, 0, 0, nullptr)}, |
| 1006 |
{2524608000, "20500101000000Z", nullptr, "Jan 1 00:00:00 2050 GMT", |
| 1007 |
make_tm(0, 0, 0, 1, 0, 150, 0, 0, 0, 0, nullptr)}, |
| 1008 |
// Test boundary conditions. |
| 1009 |
{-62167219200, "00000101000000Z", nullptr, "Jan 1 00:00:00 0 GMT", |
| 1010 |
make_tm(0, 0, 0, 1, 0, -1900, 0, 0, 0, 0, nullptr)}, |
| 1011 |
{-62167219201, nullptr, nullptr, nullptr, |
| 1012 |
make_tm(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr)}, |
| 1013 |
{253402300799, "99991231235959Z", nullptr, "Dec 31 23:59:59 9999 GMT", |
| 1014 |
make_tm(59, 59, 23, 31, 11, 8099, 0, 0, 0, 0, nullptr)}, |
| 1015 |
{253402300800, nullptr, nullptr, nullptr, |
| 1016 |
make_tm(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr)}, |
| 1017 |
}; |
| 1018 |
for (const auto &t : kTests) { |
| 1019 |
int64_t tt; |
| 1020 |
struct tm actual_time_t; |
| 1021 |
OPENSSL_memset(&actual_time_t, 0, sizeof(actual_time_t)); |
| 1022 |
SCOPED_TRACE(t.time); |
| 1023 |
|
| 1024 |
bssl::UniquePtr<ASN1_UTCTIME> utc(ASN1_UTCTIME_set(nullptr, t.time)); |
| 1025 |
|
| 1026 |
if (t.utc) { |
| 1027 |
ASSERT_TRUE(utc); |
| 1028 |
EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(utc.get())); |
| 1029 |
EXPECT_EQ(t.utc, ASN1StringToStdString(utc.get())); |
| 1030 |
EXPECT_TRUE(ASN1Time_check_posix(utc.get(), t.time)); |
| 1031 |
EXPECT_EQ(ASN1_TIME_to_posix(utc.get(), &tt), 1); |
| 1032 |
EXPECT_EQ(tt, t.time); |
| 1033 |
EXPECT_EQ(PrintStringToBIO(utc.get(), &ASN1_UTCTIME_print), t.printed); |
| 1034 |
EXPECT_EQ(PrintStringToBIO(utc.get(), &ASN1_TIME_print), t.printed); |
| 1035 |
EXPECT_EQ(ASN1_TIME_to_tm(utc.get(), &actual_time_t), 1); |
| 1036 |
EXPECT_EQ(OPENSSL_memcmp(&t.expected_tm, &actual_time_t, sizeof(actual_time_t)), 0); |
| 1037 |
} else { |
| 1038 |
EXPECT_FALSE(utc); |
| 1039 |
} |
| 1040 |
|
| 1041 |
bssl::UniquePtr<ASN1_GENERALIZEDTIME> generalized( |
| 1042 |
ASN1_GENERALIZEDTIME_set(nullptr, t.time)); |
| 1043 |
if (t.generalized) { |
| 1044 |
ASSERT_TRUE(generalized); |
| 1045 |
EXPECT_EQ(V_ASN1_GENERALIZEDTIME, ASN1_STRING_type(generalized.get())); |
| 1046 |
EXPECT_EQ(t.generalized, ASN1StringToStdString(generalized.get())); |
| 1047 |
EXPECT_TRUE(ASN1Time_check_posix(generalized.get(), t.time)); |
| 1048 |
EXPECT_EQ(ASN1_TIME_to_posix(generalized.get(), &tt), 1); |
| 1049 |
EXPECT_EQ(tt, t.time); |
| 1050 |
EXPECT_EQ( |
| 1051 |
PrintStringToBIO(generalized.get(), &ASN1_GENERALIZEDTIME_print), |
| 1052 |
t.printed); |
| 1053 |
EXPECT_EQ(PrintStringToBIO(generalized.get(), &ASN1_TIME_print), |
| 1054 |
t.printed); |
| 1055 |
EXPECT_EQ(ASN1_TIME_to_tm(generalized.get(), &actual_time_t), 1); |
| 1056 |
EXPECT_EQ(OPENSSL_memcmp(&t.expected_tm, &actual_time_t, sizeof(actual_time_t)), 0); |
| 1057 |
} else { |
| 1058 |
EXPECT_FALSE(generalized); |
| 1059 |
} |
| 1060 |
|
| 1061 |
bssl::UniquePtr<ASN1_TIME> choice(ASN1_TIME_set_posix(nullptr, t.time)); |
| 1062 |
if (t.generalized) { |
| 1063 |
ASSERT_TRUE(choice); |
| 1064 |
if (t.utc) { |
| 1065 |
EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(choice.get())); |
| 1066 |
EXPECT_EQ(t.utc, ASN1StringToStdString(choice.get())); |
| 1067 |
} else { |
| 1068 |
EXPECT_EQ(V_ASN1_GENERALIZEDTIME, ASN1_STRING_type(choice.get())); |
| 1069 |
EXPECT_EQ(t.generalized, ASN1StringToStdString(choice.get())); |
| 1070 |
} |
| 1071 |
EXPECT_TRUE(ASN1Time_check_posix(choice.get(), t.time)); |
| 1072 |
EXPECT_EQ(ASN1_TIME_to_posix(choice.get(), &tt), 1); |
| 1073 |
EXPECT_EQ(tt, t.time); |
| 1074 |
EXPECT_EQ(ASN1_TIME_to_tm(choice.get(), &actual_time_t), 1); |
| 1075 |
EXPECT_EQ(OPENSSL_memcmp(&t.expected_tm, &actual_time_t, sizeof(actual_time_t)), 0); |
| 1076 |
} else { |
| 1077 |
EXPECT_FALSE(choice); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
TEST(ASN1Test, ASN1_TIME_to_tm_default_behavior) { |
| 1083 |
struct tm actual_time_t; |
| 1084 |
OPENSSL_memset(&actual_time_t, 0, sizeof(actual_time_t)); |
| 1085 |
// null ASN1_TIME should use the current time |
| 1086 |
EXPECT_EQ(ASN1_TIME_to_tm(nullptr, &actual_time_t), 1); |
| 1087 |
// The current time should be some point before 1900 (year 0) |
| 1088 |
EXPECT_GE(actual_time_t.tm_year, 0); |
| 1089 |
|
| 1090 |
// null struct tm should just check that ASN1_TIME is valid |
| 1091 |
bssl::UniquePtr<ASN1_UTCTIME> date(ASN1_UTCTIME_set(nullptr, 1685142702)); |
| 1092 |
EXPECT_EQ(ASN1_TIME_to_tm(date.get(), nullptr), 1); |
| 1093 |
|
| 1094 |
// Make date an unknown type |
| 1095 |
date.get()->type = -100000000; |
| 1096 |
EXPECT_EQ(ASN1_TIME_to_tm(date.get(), nullptr), 0); |
| 1097 |
|
| 1098 |
EXPECT_EQ(ASN1_TIME_to_tm(nullptr, nullptr), 0); |
| 1099 |
} |
| 1100 |
|
| 1101 |
TEST(ASN1Test, TimeSetString) { |
| 1102 |
bssl::UniquePtr<ASN1_STRING> s(ASN1_STRING_new()); |
| 1103 |
ASSERT_TRUE(s); |
| 1104 |
|
| 1105 |
ASSERT_TRUE(ASN1_UTCTIME_set_string(s.get(), "700101000000Z")); |
| 1106 |
EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(s.get())); |
| 1107 |
EXPECT_EQ("700101000000Z", ASN1StringToStdString(s.get())); |
| 1108 |
|
| 1109 |
ASSERT_TRUE(ASN1_GENERALIZEDTIME_set_string(s.get(), "19700101000000Z")); |
| 1110 |
EXPECT_EQ(V_ASN1_GENERALIZEDTIME, ASN1_STRING_type(s.get())); |
| 1111 |
EXPECT_EQ("19700101000000Z", ASN1StringToStdString(s.get())); |
| 1112 |
|
| 1113 |
// |ASN1_TIME_set_string| accepts either format. It relies on there being no |
| 1114 |
// overlap between the two. |
| 1115 |
ASSERT_TRUE(ASN1_TIME_set_string(s.get(), "700101000000Z")); |
| 1116 |
EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(s.get())); |
| 1117 |
EXPECT_EQ("700101000000Z", ASN1StringToStdString(s.get())); |
| 1118 |
|
| 1119 |
ASSERT_TRUE(ASN1_TIME_set_string(s.get(), "19700101000000Z")); |
| 1120 |
EXPECT_EQ(V_ASN1_GENERALIZEDTIME, ASN1_STRING_type(s.get())); |
| 1121 |
EXPECT_EQ("19700101000000Z", ASN1StringToStdString(s.get())); |
| 1122 |
|
| 1123 |
// |ASN1_TIME_set_string_X509| behaves similarly except it additionally |
| 1124 |
// converts GeneralizedTime to UTCTime if it fits. |
| 1125 |
ASSERT_TRUE(ASN1_TIME_set_string_X509(s.get(), "700101000000Z")); |
| 1126 |
EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(s.get())); |
| 1127 |
EXPECT_EQ("700101000000Z", ASN1StringToStdString(s.get())); |
| 1128 |
|
| 1129 |
ASSERT_TRUE(ASN1_TIME_set_string_X509(s.get(), "19700101000000Z")); |
| 1130 |
EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(s.get())); |
| 1131 |
EXPECT_EQ("700101000000Z", ASN1StringToStdString(s.get())); |
| 1132 |
|
| 1133 |
ASSERT_TRUE(ASN1_TIME_set_string_X509(s.get(), "19500101000000Z")); |
| 1134 |
EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(s.get())); |
| 1135 |
EXPECT_EQ("500101000000Z", ASN1StringToStdString(s.get())); |
| 1136 |
|
| 1137 |
ASSERT_TRUE(ASN1_TIME_set_string_X509(s.get(), "19491231235959Z")); |
| 1138 |
EXPECT_EQ(V_ASN1_GENERALIZEDTIME, ASN1_STRING_type(s.get())); |
| 1139 |
EXPECT_EQ("19491231235959Z", ASN1StringToStdString(s.get())); |
| 1140 |
|
| 1141 |
ASSERT_TRUE(ASN1_TIME_set_string_X509(s.get(), "20491231235959Z")); |
| 1142 |
EXPECT_EQ(V_ASN1_UTCTIME, ASN1_STRING_type(s.get())); |
| 1143 |
EXPECT_EQ("491231235959Z", ASN1StringToStdString(s.get())); |
| 1144 |
|
| 1145 |
ASSERT_TRUE(ASN1_TIME_set_string_X509(s.get(), "20500101000000Z")); |
| 1146 |
EXPECT_EQ(V_ASN1_GENERALIZEDTIME, ASN1_STRING_type(s.get())); |
| 1147 |
EXPECT_EQ("20500101000000Z", ASN1StringToStdString(s.get())); |
| 1148 |
|
| 1149 |
// Invalid inputs are rejected. |
| 1150 |
EXPECT_FALSE(ASN1_UTCTIME_set_string(s.get(), "nope")); |
| 1151 |
EXPECT_FALSE(ASN1_UTCTIME_set_string(s.get(), "19700101000000Z")); |
| 1152 |
EXPECT_FALSE(ASN1_GENERALIZEDTIME_set_string(s.get(), "nope")); |
| 1153 |
EXPECT_FALSE(ASN1_GENERALIZEDTIME_set_string(s.get(), "700101000000Z")); |
| 1154 |
EXPECT_FALSE(ASN1_TIME_set_string(s.get(), "nope")); |
| 1155 |
|
| 1156 |
// If passed a null object, the functions validate the input without writing |
| 1157 |
// to anything. |
| 1158 |
EXPECT_TRUE(ASN1_UTCTIME_set_string(nullptr, "700101000000Z")); |
| 1159 |
EXPECT_TRUE(ASN1_TIME_set_string(nullptr, "700101000000Z")); |
| 1160 |
EXPECT_TRUE(ASN1_TIME_set_string_X509(nullptr, "700101000000Z")); |
| 1161 |
EXPECT_TRUE(ASN1_GENERALIZEDTIME_set_string(nullptr, "19700101000000Z")); |
| 1162 |
EXPECT_TRUE(ASN1_TIME_set_string(nullptr, "19700101000000Z")); |
| 1163 |
EXPECT_TRUE(ASN1_TIME_set_string_X509(nullptr, "19700101000000Z")); |
| 1164 |
// Test an input |ASN1_TIME_set_string_X509| won't convert to UTCTime. |
| 1165 |
EXPECT_TRUE(ASN1_GENERALIZEDTIME_set_string(nullptr, "20500101000000Z")); |
| 1166 |
EXPECT_TRUE(ASN1_TIME_set_string(nullptr, "20500101000000Z")); |
| 1167 |
EXPECT_TRUE(ASN1_TIME_set_string_X509(nullptr, "20500101000000Z")); |
| 1168 |
EXPECT_FALSE(ASN1_UTCTIME_set_string(nullptr, "nope")); |
| 1169 |
EXPECT_FALSE(ASN1_GENERALIZEDTIME_set_string(nullptr, "nope")); |
| 1170 |
EXPECT_FALSE(ASN1_TIME_set_string(nullptr, "nope")); |
| 1171 |
EXPECT_FALSE(ASN1_TIME_set_string_X509(nullptr, "nope")); |
| 1172 |
|
| 1173 |
// Timezone offsets are not allowed by DER. |
| 1174 |
EXPECT_FALSE(ASN1_UTCTIME_set_string(nullptr, "700101000000-0400")); |
| 1175 |
EXPECT_FALSE(ASN1_TIME_set_string(nullptr, "700101000000-0400")); |
| 1176 |
EXPECT_FALSE(ASN1_TIME_set_string_X509(nullptr, "700101000000-0400")); |
| 1177 |
EXPECT_FALSE(ASN1_GENERALIZEDTIME_set_string(nullptr, "19700101000000-0400")); |
| 1178 |
EXPECT_FALSE(ASN1_TIME_set_string(nullptr, "19700101000000-0400")); |
| 1179 |
EXPECT_FALSE(ASN1_TIME_set_string_X509(nullptr, "19700101000000-0400")); |
| 1180 |
} |
| 1181 |
|
| 1182 |
TEST(ASN1Test, AdjTime) { |
| 1183 |
struct tm tm1, tm2; |
| 1184 |
int days, secs; |
| 1185 |
|
| 1186 |
EXPECT_TRUE(OPENSSL_posix_to_tm(0, &tm1)); |
| 1187 |
EXPECT_TRUE(OPENSSL_posix_to_tm(0, &tm2)); |
| 1188 |
// Test values that are too large and should be rejected. |
| 1189 |
EXPECT_FALSE(OPENSSL_gmtime_adj(&tm1, INT_MIN, INT_MIN)); |
| 1190 |
EXPECT_FALSE(OPENSSL_gmtime_adj(&tm1, INT_MAX, INT_MAX)); |
| 1191 |
// Basic functionality. |
| 1192 |
EXPECT_TRUE(OPENSSL_gmtime_adj(&tm2, 1, 1)); |
| 1193 |
EXPECT_TRUE(OPENSSL_gmtime_diff(&days, &secs, &tm1, &tm2)); |
| 1194 |
EXPECT_EQ(days, 1); |
| 1195 |
EXPECT_EQ(secs, 1); |
| 1196 |
EXPECT_TRUE(OPENSSL_gmtime_diff(&days, &secs, &tm2, &tm1)); |
| 1197 |
EXPECT_EQ(days, -1); |
| 1198 |
EXPECT_EQ(secs, -1); |
| 1199 |
// Test a value of days that is very large, but valid. |
| 1200 |
EXPECT_TRUE(OPENSSL_gmtime_adj(&tm2, 2932800, 0)); |
| 1201 |
EXPECT_TRUE(OPENSSL_gmtime_diff(&days, &secs, &tm1, &tm2)); |
| 1202 |
EXPECT_EQ(days, 2932801); |
| 1203 |
EXPECT_EQ(secs, 1); |
| 1204 |
EXPECT_TRUE(OPENSSL_gmtime_diff(&days, &secs, &tm2, &tm1)); |
| 1205 |
EXPECT_EQ(days, -2932801); |
| 1206 |
EXPECT_EQ(secs, -1); |
| 1207 |
} |
| 1208 |
static std::vector<uint8_t> StringToVector(const std::string &str) { |
| 1209 |
return std::vector<uint8_t>(str.begin(), str.end()); |
| 1210 |
} |
| 1211 |
|
| 1212 |
TEST(ASN1Test, StringPrintEx) { |
| 1213 |
const struct { |
| 1214 |
int type; |
| 1215 |
std::vector<uint8_t> data; |
| 1216 |
int str_flags; |
| 1217 |
unsigned long flags; |
| 1218 |
std::string expected; |
| 1219 |
} kTests[] = { |
| 1220 |
// A string like "hello" is never escaped or quoted. |
| 1221 |
// |ASN1_STRFLGS_ESC_QUOTE| only introduces quotes when needed. Note |
| 1222 |
// OpenSSL interprets T61String as Latin-1. |
| 1223 |
{V_ASN1_T61STRING, StringToVector("hello"), 0, 0, "hello"}, |
| 1224 |
{V_ASN1_T61STRING, StringToVector("hello"), 0, |
| 1225 |
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB, |
| 1226 |
"hello"}, |
| 1227 |
{V_ASN1_T61STRING, StringToVector("hello"), 0, |
| 1228 |
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | |
| 1229 |
ASN1_STRFLGS_ESC_QUOTE, |
| 1230 |
"hello"}, |
| 1231 |
|
| 1232 |
// By default, 8-bit characters are printed without escaping. |
| 1233 |
{V_ASN1_T61STRING, |
| 1234 |
{0, '\n', 0x80, 0xff, ',', '+', '"', '\\', '<', '>', ';'}, |
| 1235 |
0, |
| 1236 |
0, |
| 1237 |
std::string(1, '\0') + "\n\x80\xff,+\"\\<>;"}, |
| 1238 |
|
| 1239 |
// Flags control different escapes. Note that any escape flag will cause |
| 1240 |
// blackslashes to be escaped. |
| 1241 |
{V_ASN1_T61STRING, |
| 1242 |
{0, '\n', 0x80, 0xff, ',', '+', '"', '\\', '<', '>', ';'}, |
| 1243 |
0, |
| 1244 |
ASN1_STRFLGS_ESC_2253, |
| 1245 |
std::string(1, '\0') + "\n\x80\xff\\,\\+\\\"\\\\\\<\\>\\;"}, |
| 1246 |
{V_ASN1_T61STRING, |
| 1247 |
{0, '\n', 0x80, 0xff, ',', '+', '"', '\\', '<', '>', ';'}, |
| 1248 |
0, |
| 1249 |
ASN1_STRFLGS_ESC_CTRL, |
| 1250 |
"\\00\\0A\x80\xff,+\"\\\\<>;"}, |
| 1251 |
{V_ASN1_T61STRING, |
| 1252 |
{0, '\n', 0x80, 0xff, ',', '+', '"', '\\', '<', '>', ';'}, |
| 1253 |
0, |
| 1254 |
ASN1_STRFLGS_ESC_MSB, |
| 1255 |
std::string(1, '\0') + "\n\\80\\FF,+\"\\\\<>;"}, |
| 1256 |
{V_ASN1_T61STRING, |
| 1257 |
{0, '\n', 0x80, 0xff, ',', '+', '"', '\\', '<', '>', ';'}, |
| 1258 |
0, |
| 1259 |
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB, |
| 1260 |
"\\00\\0A\\80\\FF\\,\\+\\\"\\\\\\<\\>\\;"}, |
| 1261 |
|
| 1262 |
// When quoted, fewer characters need to be escaped in RFC 2253. |
| 1263 |
{V_ASN1_T61STRING, |
| 1264 |
{0, '\n', 0x80, 0xff, ',', '+', '"', '\\', '<', '>', ';'}, |
| 1265 |
0, |
| 1266 |
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | |
| 1267 |
ASN1_STRFLGS_ESC_QUOTE, |
| 1268 |
"\"\\00\\0A\\80\\FF,+\\\"\\\\<>;\""}, |
| 1269 |
|
| 1270 |
// If no characters benefit from quotes, no quotes are added. |
| 1271 |
{V_ASN1_T61STRING, |
| 1272 |
{0, '\n', 0x80, 0xff, '"', '\\'}, |
| 1273 |
0, |
| 1274 |
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | |
| 1275 |
ASN1_STRFLGS_ESC_QUOTE, |
| 1276 |
"\\00\\0A\\80\\FF\\\"\\\\"}, |
| 1277 |
|
| 1278 |
// RFC 2253 only escapes spaces at the start and end of a string. |
| 1279 |
{V_ASN1_T61STRING, StringToVector(" "), 0, ASN1_STRFLGS_ESC_2253, |
| 1280 |
"\\ \\ "}, |
| 1281 |
{V_ASN1_T61STRING, StringToVector(" "), 0, |
| 1282 |
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_UTF8_CONVERT, "\\ \\ "}, |
| 1283 |
{V_ASN1_T61STRING, StringToVector(" "), 0, |
| 1284 |
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_QUOTE, "\" \""}, |
| 1285 |
|
| 1286 |
// RFC 2253 only escapes # at the start of a string. |
| 1287 |
{V_ASN1_T61STRING, StringToVector("###"), 0, ASN1_STRFLGS_ESC_2253, |
| 1288 |
"\\###"}, |
| 1289 |
{V_ASN1_T61STRING, StringToVector("###"), 0, |
| 1290 |
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_QUOTE, "\"###\""}, |
| 1291 |
|
| 1292 |
// By default, strings are decoded and Unicode code points are |
| 1293 |
// individually escaped. |
| 1294 |
{V_ASN1_UTF8STRING, StringToVector("a\xc2\x80\xc4\x80\xf0\x90\x80\x80"), |
| 1295 |
0, ASN1_STRFLGS_ESC_MSB, "a\\80\\U0100\\W00010000"}, |
| 1296 |
{V_ASN1_BMPSTRING, |
| 1297 |
{0x00, 'a', 0x00, 0x80, 0x01, 0x00}, |
| 1298 |
0, |
| 1299 |
ASN1_STRFLGS_ESC_MSB, |
| 1300 |
"a\\80\\U0100"}, |
| 1301 |
{V_ASN1_UNIVERSALSTRING, |
| 1302 |
{0x00, 0x00, 0x00, 'a', // |
| 1303 |
0x00, 0x00, 0x00, 0x80, // |
| 1304 |
0x00, 0x00, 0x01, 0x00, // |
| 1305 |
0x00, 0x01, 0x00, 0x00}, |
| 1306 |
0, |
| 1307 |
ASN1_STRFLGS_ESC_MSB, |
| 1308 |
"a\\80\\U0100\\W00010000"}, |
| 1309 |
|
| 1310 |
// |ASN1_STRFLGS_UTF8_CONVERT| normalizes everything to UTF-8 and then |
| 1311 |
// escapes individual bytes. |
| 1312 |
{V_ASN1_IA5STRING, StringToVector("a\x80"), 0, |
| 1313 |
ASN1_STRFLGS_ESC_MSB | ASN1_STRFLGS_UTF8_CONVERT, "a\\C2\\80"}, |
| 1314 |
{V_ASN1_T61STRING, StringToVector("a\x80"), 0, |
| 1315 |
ASN1_STRFLGS_ESC_MSB | ASN1_STRFLGS_UTF8_CONVERT, "a\\C2\\80"}, |
| 1316 |
{V_ASN1_UTF8STRING, StringToVector("a\xc2\x80\xc4\x80\xf0\x90\x80\x80"), |
| 1317 |
0, ASN1_STRFLGS_ESC_MSB | ASN1_STRFLGS_UTF8_CONVERT, |
| 1318 |
"a\\C2\\80\\C4\\80\\F0\\90\\80\\80"}, |
| 1319 |
{V_ASN1_BMPSTRING, |
| 1320 |
{0x00, 'a', 0x00, 0x80, 0x01, 0x00}, |
| 1321 |
0, |
| 1322 |
ASN1_STRFLGS_ESC_MSB | ASN1_STRFLGS_UTF8_CONVERT, |
| 1323 |
"a\\C2\\80\\C4\\80"}, |
| 1324 |
{V_ASN1_UNIVERSALSTRING, |
| 1325 |
{0x00, 0x00, 0x00, 'a', // |
| 1326 |
0x00, 0x00, 0x00, 0x80, // |
| 1327 |
0x00, 0x00, 0x01, 0x00, // |
| 1328 |
0x00, 0x01, 0x00, 0x00}, |
| 1329 |
0, |
| 1330 |
ASN1_STRFLGS_ESC_MSB | ASN1_STRFLGS_UTF8_CONVERT, |
| 1331 |
"a\\C2\\80\\C4\\80\\F0\\90\\80\\80"}, |
| 1332 |
|
| 1333 |
// The same as above, but without escaping the UTF-8 encoding. |
| 1334 |
{V_ASN1_IA5STRING, StringToVector("a\x80"), 0, ASN1_STRFLGS_UTF8_CONVERT, |
| 1335 |
"a\xc2\x80"}, |
| 1336 |
{V_ASN1_T61STRING, StringToVector("a\x80"), 0, ASN1_STRFLGS_UTF8_CONVERT, |
| 1337 |
"a\xc2\x80"}, |
| 1338 |
{V_ASN1_UTF8STRING, StringToVector("a\xc2\x80\xc4\x80\xf0\x90\x80\x80"), |
| 1339 |
0, ASN1_STRFLGS_UTF8_CONVERT, "a\xc2\x80\xc4\x80\xf0\x90\x80\x80"}, |
| 1340 |
{V_ASN1_BMPSTRING, |
| 1341 |
{0x00, 'a', 0x00, 0x80, 0x01, 0x00}, |
| 1342 |
0, |
| 1343 |
ASN1_STRFLGS_UTF8_CONVERT, |
| 1344 |
"a\xc2\x80\xc4\x80"}, |
| 1345 |
{V_ASN1_UNIVERSALSTRING, |
| 1346 |
{0x00, 0x00, 0x00, 'a', // |
| 1347 |
0x00, 0x00, 0x00, 0x80, // |
| 1348 |
0x00, 0x00, 0x01, 0x00, // |
| 1349 |
0x00, 0x01, 0x00, 0x00}, |
| 1350 |
0, |
| 1351 |
ASN1_STRFLGS_UTF8_CONVERT, |
| 1352 |
"a\xc2\x80\xc4\x80\xf0\x90\x80\x80"}, |
| 1353 |
|
| 1354 |
// Types that cannot be decoded are, by default, treated as a byte string. |
| 1355 |
{V_ASN1_OCTET_STRING, {0xff}, 0, 0, "\xff"}, |
| 1356 |
{-1, {0xff}, 0, 0, "\xff"}, |
| 1357 |
{100, {0xff}, 0, 0, "\xff"}, |
| 1358 |
|
| 1359 |
// |ASN1_STRFLGS_UTF8_CONVERT| still converts these bytes to UTF-8. |
| 1360 |
// |
| 1361 |
// TODO(davidben): This seems like a bug. Although it's unclear because |
| 1362 |
// the non-RFC-2253 options aren't especially sound. Can we just remove |
| 1363 |
// them? |
| 1364 |
{V_ASN1_OCTET_STRING, {0xff}, 0, ASN1_STRFLGS_UTF8_CONVERT, "\xc3\xbf"}, |
| 1365 |
{-1, {0xff}, 0, ASN1_STRFLGS_UTF8_CONVERT, "\xc3\xbf"}, |
| 1366 |
{100, {0xff}, 0, ASN1_STRFLGS_UTF8_CONVERT, "\xc3\xbf"}, |
| 1367 |
|
| 1368 |
// |ASN1_STRFLGS_IGNORE_TYPE| causes the string type to be ignored, so it |
| 1369 |
// is always treated as a byte string, even if it is not a valid encoding. |
| 1370 |
{V_ASN1_UTF8STRING, {0xff}, 0, ASN1_STRFLGS_IGNORE_TYPE, "\xff"}, |
| 1371 |
{V_ASN1_BMPSTRING, {0xff}, 0, ASN1_STRFLGS_IGNORE_TYPE, "\xff"}, |
| 1372 |
{V_ASN1_UNIVERSALSTRING, {0xff}, 0, ASN1_STRFLGS_IGNORE_TYPE, "\xff"}, |
| 1373 |
|
| 1374 |
// |ASN1_STRFLGS_SHOW_TYPE| prepends the type name. |
| 1375 |
{V_ASN1_UTF8STRING, {'a'}, 0, ASN1_STRFLGS_SHOW_TYPE, "UTF8STRING:a"}, |
| 1376 |
{-1, {'a'}, 0, ASN1_STRFLGS_SHOW_TYPE, "(unknown):a"}, |
| 1377 |
{100, {'a'}, 0, ASN1_STRFLGS_SHOW_TYPE, "(unknown):a"}, |
| 1378 |
|
| 1379 |
// |ASN1_STRFLGS_DUMP_ALL| and |ASN1_STRFLGS_DUMP_UNKNOWN| cause |
| 1380 |
// non-string types to be printed in hex, though without the DER wrapper |
| 1381 |
// by default. |
| 1382 |
{V_ASN1_UTF8STRING, StringToVector("\xe2\x98\x83"), 0, |
| 1383 |
ASN1_STRFLGS_DUMP_UNKNOWN, "\\U2603"}, |
| 1384 |
{V_ASN1_UTF8STRING, StringToVector("\xe2\x98\x83"), 0, |
| 1385 |
ASN1_STRFLGS_DUMP_ALL, "#E29883"}, |
| 1386 |
{V_ASN1_OCTET_STRING, StringToVector("\xe2\x98\x83"), 0, |
| 1387 |
ASN1_STRFLGS_DUMP_UNKNOWN, "#E29883"}, |
| 1388 |
{V_ASN1_OCTET_STRING, StringToVector("\xe2\x98\x83"), 0, |
| 1389 |
ASN1_STRFLGS_DUMP_ALL, "#E29883"}, |
| 1390 |
|
| 1391 |
// |ASN1_STRFLGS_DUMP_DER| includes the entire element. |
| 1392 |
{V_ASN1_UTF8STRING, StringToVector("\xe2\x98\x83"), 0, |
| 1393 |
ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER, "#0C03E29883"}, |
| 1394 |
{V_ASN1_OCTET_STRING, StringToVector("\xe2\x98\x83"), 0, |
| 1395 |
ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER, "#0403E29883"}, |
| 1396 |
{V_ASN1_BIT_STRING, |
| 1397 |
{0x80}, |
| 1398 |
ASN1_STRING_FLAG_BITS_LEFT | 4, |
| 1399 |
ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER, |
| 1400 |
"#03020480"}, |
| 1401 |
// INTEGER { 1 } |
| 1402 |
{V_ASN1_INTEGER, |
| 1403 |
{0x01}, |
| 1404 |
0, |
| 1405 |
ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER, |
| 1406 |
"#020101"}, |
| 1407 |
// INTEGER { -1 } |
| 1408 |
{V_ASN1_NEG_INTEGER, |
| 1409 |
{0x01}, |
| 1410 |
0, |
| 1411 |
ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER, |
| 1412 |
"#0201FF"}, |
| 1413 |
// ENUMERATED { 1 } |
| 1414 |
{V_ASN1_ENUMERATED, |
| 1415 |
{0x01}, |
| 1416 |
0, |
| 1417 |
ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER, |
| 1418 |
"#0A0101"}, |
| 1419 |
// ENUMERATED { -1 } |
| 1420 |
{V_ASN1_NEG_ENUMERATED, |
| 1421 |
{0x01}, |
| 1422 |
0, |
| 1423 |
ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER, |
| 1424 |
"#0A01FF"}, |
| 1425 |
}; |
| 1426 |
for (const auto &t : kTests) { |
| 1427 |
SCOPED_TRACE(t.type); |
| 1428 |
SCOPED_TRACE(Bytes(t.data)); |
| 1429 |
SCOPED_TRACE(t.str_flags); |
| 1430 |
SCOPED_TRACE(t.flags); |
| 1431 |
|
| 1432 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_type_new(t.type)); |
| 1433 |
ASSERT_TRUE(str); |
| 1434 |
ASSERT_TRUE(ASN1_STRING_set(str.get(), t.data.data(), t.data.size())); |
| 1435 |
str->flags = t.str_flags; |
| 1436 |
|
| 1437 |
// If the |BIO| is null, it should measure the size. |
| 1438 |
int len = ASN1_STRING_print_ex(nullptr, str.get(), t.flags); |
| 1439 |
EXPECT_EQ(len, static_cast<int>(t.expected.size())); |
| 1440 |
|
| 1441 |
// Measuring the size should also work for the |FILE| version |
| 1442 |
len = ASN1_STRING_print_ex_fp(nullptr, str.get(), t.flags); |
| 1443 |
EXPECT_EQ(len, static_cast<int>(t.expected.size())); |
| 1444 |
|
| 1445 |
// Actually print the string. |
| 1446 |
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 1447 |
ASSERT_TRUE(bio); |
| 1448 |
len = ASN1_STRING_print_ex(bio.get(), str.get(), t.flags); |
| 1449 |
EXPECT_EQ(len, static_cast<int>(t.expected.size())); |
| 1450 |
|
| 1451 |
const uint8_t *bio_contents; |
| 1452 |
size_t bio_len; |
| 1453 |
ASSERT_TRUE(BIO_mem_contents(bio.get(), &bio_contents, &bio_len)); |
| 1454 |
EXPECT_EQ(t.expected, std::string(bio_contents, bio_contents + bio_len)); |
| 1455 |
} |
| 1456 |
|
| 1457 |
const struct { |
| 1458 |
int type; |
| 1459 |
std::vector<uint8_t> data; |
| 1460 |
int str_flags; |
| 1461 |
unsigned long flags; |
| 1462 |
} kUnprintableTests[] = { |
| 1463 |
// When decoding strings, invalid codepoints are errors. |
| 1464 |
{V_ASN1_UTF8STRING, {0xff}, 0, ASN1_STRFLGS_ESC_MSB}, |
| 1465 |
{V_ASN1_BMPSTRING, {0xff}, 0, ASN1_STRFLGS_ESC_MSB}, |
| 1466 |
{V_ASN1_BMPSTRING, {0xff}, 0, ASN1_STRFLGS_ESC_MSB}, |
| 1467 |
{V_ASN1_UNIVERSALSTRING, {0xff}, 0, ASN1_STRFLGS_ESC_MSB}, |
| 1468 |
}; |
| 1469 |
for (const auto &t : kUnprintableTests) { |
| 1470 |
SCOPED_TRACE(t.type); |
| 1471 |
SCOPED_TRACE(Bytes(t.data)); |
| 1472 |
SCOPED_TRACE(t.str_flags); |
| 1473 |
SCOPED_TRACE(t.flags); |
| 1474 |
|
| 1475 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_type_new(t.type)); |
| 1476 |
ASSERT_TRUE(str); |
| 1477 |
ASSERT_TRUE(ASN1_STRING_set(str.get(), t.data.data(), t.data.size())); |
| 1478 |
str->flags = t.str_flags; |
| 1479 |
|
| 1480 |
// If the |BIO| is null, it should measure the size. |
| 1481 |
int len = ASN1_STRING_print_ex(nullptr, str.get(), t.flags); |
| 1482 |
EXPECT_EQ(len, -1); |
| 1483 |
ERR_clear_error(); |
| 1484 |
|
| 1485 |
// Measuring the size should also work for the |FILE| version |
| 1486 |
len = ASN1_STRING_print_ex_fp(nullptr, str.get(), t.flags); |
| 1487 |
EXPECT_EQ(len, -1); |
| 1488 |
ERR_clear_error(); |
| 1489 |
|
| 1490 |
// Actually print the string. |
| 1491 |
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 1492 |
ASSERT_TRUE(bio); |
| 1493 |
len = ASN1_STRING_print_ex(bio.get(), str.get(), t.flags); |
| 1494 |
EXPECT_EQ(len, -1); |
| 1495 |
ERR_clear_error(); |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
TEST(ASN1Test, MBString) { |
| 1500 |
const unsigned long kAll = B_ASN1_PRINTABLESTRING | B_ASN1_IA5STRING | |
| 1501 |
B_ASN1_T61STRING | B_ASN1_BMPSTRING | |
| 1502 |
B_ASN1_UNIVERSALSTRING | B_ASN1_UTF8STRING; |
| 1503 |
|
| 1504 |
const struct { |
| 1505 |
int format; |
| 1506 |
std::vector<uint8_t> in; |
| 1507 |
unsigned long mask; |
| 1508 |
int expected_type; |
| 1509 |
std::vector<uint8_t> expected_data; |
| 1510 |
int num_codepoints; |
| 1511 |
} kTests[] = { |
| 1512 |
// Given a choice of formats, we pick the smallest that fits. |
| 1513 |
{MBSTRING_UTF8, {}, kAll, V_ASN1_PRINTABLESTRING, {}, 0}, |
| 1514 |
{MBSTRING_UTF8, {'a'}, kAll, V_ASN1_PRINTABLESTRING, {'a'}, 1}, |
| 1515 |
{MBSTRING_UTF8, |
| 1516 |
{'a', 'A', '0', '\'', '(', ')', '+', ',', '-', '.', '/', ':', '=', '?'}, |
| 1517 |
kAll, |
| 1518 |
V_ASN1_PRINTABLESTRING, |
| 1519 |
{'a', 'A', '0', '\'', '(', ')', '+', ',', '-', '.', '/', ':', '=', '?'}, |
| 1520 |
14}, |
| 1521 |
{MBSTRING_UTF8, {'*'}, kAll, V_ASN1_IA5STRING, {'*'}, 1}, |
| 1522 |
{MBSTRING_UTF8, {'\n'}, kAll, V_ASN1_IA5STRING, {'\n'}, 1}, |
| 1523 |
{MBSTRING_UTF8, |
| 1524 |
{0xc2, 0x80 /* U+0080 */}, |
| 1525 |
kAll, |
| 1526 |
V_ASN1_T61STRING, |
| 1527 |
{0x80}, |
| 1528 |
1}, |
| 1529 |
{MBSTRING_UTF8, |
| 1530 |
{0xc4, 0x80 /* U+0100 */}, |
| 1531 |
kAll, |
| 1532 |
V_ASN1_BMPSTRING, |
| 1533 |
{0x01, 0x00}, |
| 1534 |
1}, |
| 1535 |
{MBSTRING_UTF8, |
| 1536 |
{0xf0, 0x90, 0x80, 0x80 /* U+10000 */}, |
| 1537 |
kAll, |
| 1538 |
V_ASN1_UNIVERSALSTRING, |
| 1539 |
{0x00, 0x01, 0x00, 0x00}, |
| 1540 |
1}, |
| 1541 |
{MBSTRING_UTF8, |
| 1542 |
{0xf0, 0x90, 0x80, 0x80 /* U+10000 */}, |
| 1543 |
kAll & ~B_ASN1_UNIVERSALSTRING, |
| 1544 |
V_ASN1_UTF8STRING, |
| 1545 |
{0xf0, 0x90, 0x80, 0x80}, |
| 1546 |
1}, |
| 1547 |
|
| 1548 |
// NUL is not printable. It should also not terminate iteration. |
| 1549 |
{MBSTRING_UTF8, {0}, kAll, V_ASN1_IA5STRING, {0}, 1}, |
| 1550 |
{MBSTRING_UTF8, {0, 'a'}, kAll, V_ASN1_IA5STRING, {0, 'a'}, 2}, |
| 1551 |
|
| 1552 |
// When a particular format is specified, we use it. |
| 1553 |
{MBSTRING_UTF8, |
| 1554 |
{'a'}, |
| 1555 |
B_ASN1_PRINTABLESTRING, |
| 1556 |
V_ASN1_PRINTABLESTRING, |
| 1557 |
{'a'}, |
| 1558 |
1}, |
| 1559 |
{MBSTRING_UTF8, {'a'}, B_ASN1_IA5STRING, V_ASN1_IA5STRING, {'a'}, 1}, |
| 1560 |
{MBSTRING_UTF8, {'a'}, B_ASN1_T61STRING, V_ASN1_T61STRING, {'a'}, 1}, |
| 1561 |
{MBSTRING_UTF8, {'a'}, B_ASN1_UTF8STRING, V_ASN1_UTF8STRING, {'a'}, 1}, |
| 1562 |
{MBSTRING_UTF8, |
| 1563 |
{'a'}, |
| 1564 |
B_ASN1_BMPSTRING, |
| 1565 |
V_ASN1_BMPSTRING, |
| 1566 |
{0x00, 'a'}, |
| 1567 |
1}, |
| 1568 |
{MBSTRING_UTF8, |
| 1569 |
{'a'}, |
| 1570 |
B_ASN1_UNIVERSALSTRING, |
| 1571 |
V_ASN1_UNIVERSALSTRING, |
| 1572 |
{0x00, 0x00, 0x00, 'a'}, |
| 1573 |
1}, |
| 1574 |
|
| 1575 |
// A long string with characters of many widths, to test sizes are |
| 1576 |
// measured in code points. |
| 1577 |
{MBSTRING_UTF8, |
| 1578 |
{ |
| 1579 |
'a', // |
| 1580 |
0xc2, 0x80, // U+0080 |
| 1581 |
0xc4, 0x80, // U+0100 |
| 1582 |
0xf0, 0x90, 0x80, 0x80, // U+10000 |
| 1583 |
}, |
| 1584 |
B_ASN1_UNIVERSALSTRING, |
| 1585 |
V_ASN1_UNIVERSALSTRING, |
| 1586 |
{ |
| 1587 |
0x00, 0x00, 0x00, 'a', // |
| 1588 |
0x00, 0x00, 0x00, 0x80, // |
| 1589 |
0x00, 0x00, 0x01, 0x00, // |
| 1590 |
0x00, 0x01, 0x00, 0x00, // |
| 1591 |
}, |
| 1592 |
4}, |
| 1593 |
}; |
| 1594 |
for (const auto &t : kTests) { |
| 1595 |
SCOPED_TRACE(t.format); |
| 1596 |
SCOPED_TRACE(Bytes(t.in)); |
| 1597 |
SCOPED_TRACE(t.mask); |
| 1598 |
|
| 1599 |
// Passing in nullptr should do a dry run. |
| 1600 |
EXPECT_EQ(t.expected_type, |
| 1601 |
ASN1_mbstring_copy(nullptr, t.in.data(), t.in.size(), t.format, |
| 1602 |
t.mask)); |
| 1603 |
|
| 1604 |
// Test allocating a new object. |
| 1605 |
ASN1_STRING *str = nullptr; |
| 1606 |
EXPECT_EQ( |
| 1607 |
t.expected_type, |
| 1608 |
ASN1_mbstring_copy(&str, t.in.data(), t.in.size(), t.format, t.mask)); |
| 1609 |
ASSERT_TRUE(str); |
| 1610 |
EXPECT_EQ(t.expected_type, ASN1_STRING_type(str)); |
| 1611 |
EXPECT_EQ(Bytes(t.expected_data), |
| 1612 |
Bytes(ASN1_STRING_get0_data(str), ASN1_STRING_length(str))); |
| 1613 |
|
| 1614 |
// Test writing into an existing object. |
| 1615 |
ASN1_STRING_free(str); |
| 1616 |
str = ASN1_STRING_new(); |
| 1617 |
ASSERT_TRUE(str); |
| 1618 |
ASN1_STRING *old_str = str; |
| 1619 |
EXPECT_EQ( |
| 1620 |
t.expected_type, |
| 1621 |
ASN1_mbstring_copy(&str, t.in.data(), t.in.size(), t.format, t.mask)); |
| 1622 |
ASSERT_EQ(old_str, str); |
| 1623 |
EXPECT_EQ(t.expected_type, ASN1_STRING_type(str)); |
| 1624 |
EXPECT_EQ(Bytes(t.expected_data), |
| 1625 |
Bytes(ASN1_STRING_get0_data(str), ASN1_STRING_length(str))); |
| 1626 |
ASN1_STRING_free(str); |
| 1627 |
str = nullptr; |
| 1628 |
|
| 1629 |
// minsize and maxsize should be enforced, even in a dry run. |
| 1630 |
EXPECT_EQ(t.expected_type, |
| 1631 |
ASN1_mbstring_ncopy(nullptr, t.in.data(), t.in.size(), t.format, |
| 1632 |
t.mask, /*minsize=*/t.num_codepoints, |
| 1633 |
/*maxsize=*/t.num_codepoints)); |
| 1634 |
|
| 1635 |
EXPECT_EQ(t.expected_type, |
| 1636 |
ASN1_mbstring_ncopy(&str, t.in.data(), t.in.size(), t.format, |
| 1637 |
t.mask, /*minsize=*/t.num_codepoints, |
| 1638 |
/*maxsize=*/t.num_codepoints)); |
| 1639 |
ASSERT_TRUE(str); |
| 1640 |
EXPECT_EQ(t.expected_type, ASN1_STRING_type(str)); |
| 1641 |
EXPECT_EQ(Bytes(t.expected_data), |
| 1642 |
Bytes(ASN1_STRING_get0_data(str), ASN1_STRING_length(str))); |
| 1643 |
ASN1_STRING_free(str); |
| 1644 |
str = nullptr; |
| 1645 |
|
| 1646 |
EXPECT_EQ(-1, ASN1_mbstring_ncopy( |
| 1647 |
nullptr, t.in.data(), t.in.size(), t.format, t.mask, |
| 1648 |
/*minsize=*/t.num_codepoints + 1, /*maxsize=*/0)); |
| 1649 |
ERR_clear_error(); |
| 1650 |
EXPECT_EQ(-1, ASN1_mbstring_ncopy( |
| 1651 |
&str, t.in.data(), t.in.size(), t.format, t.mask, |
| 1652 |
/*minsize=*/t.num_codepoints + 1, /*maxsize=*/0)); |
| 1653 |
EXPECT_FALSE(str); |
| 1654 |
ERR_clear_error(); |
| 1655 |
if (t.num_codepoints > 1) { |
| 1656 |
EXPECT_EQ(-1, ASN1_mbstring_ncopy( |
| 1657 |
nullptr, t.in.data(), t.in.size(), t.format, t.mask, |
| 1658 |
/*minsize=*/0, /*maxsize=*/t.num_codepoints - 1)); |
| 1659 |
ERR_clear_error(); |
| 1660 |
EXPECT_EQ(-1, ASN1_mbstring_ncopy( |
| 1661 |
&str, t.in.data(), t.in.size(), t.format, t.mask, |
| 1662 |
/*minsize=*/0, /*maxsize=*/t.num_codepoints - 1)); |
| 1663 |
EXPECT_FALSE(str); |
| 1664 |
ERR_clear_error(); |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
const struct { |
| 1669 |
int format; |
| 1670 |
std::vector<uint8_t> in; |
| 1671 |
unsigned long mask; |
| 1672 |
} kInvalidTests[] = { |
| 1673 |
// Invalid encodings are rejected. |
| 1674 |
{MBSTRING_UTF8, {0xff}, B_ASN1_UTF8STRING}, |
| 1675 |
{MBSTRING_BMP, {0xff}, B_ASN1_UTF8STRING}, |
| 1676 |
{MBSTRING_UNIV, {0xff}, B_ASN1_UTF8STRING}, |
| 1677 |
|
| 1678 |
// Lone surrogates are not code points. |
| 1679 |
{MBSTRING_UTF8, {0xed, 0xa0, 0x80}, B_ASN1_UTF8STRING}, |
| 1680 |
{MBSTRING_BMP, {0xd8, 0x00}, B_ASN1_UTF8STRING}, |
| 1681 |
{MBSTRING_UNIV, {0x00, 0x00, 0xd8, 0x00}, B_ASN1_UTF8STRING}, |
| 1682 |
|
| 1683 |
// The input does not fit in the allowed output types. |
| 1684 |
{MBSTRING_UTF8, {'\n'}, B_ASN1_PRINTABLESTRING}, |
| 1685 |
{MBSTRING_UTF8, |
| 1686 |
{0xc2, 0x80 /* U+0080 */}, |
| 1687 |
B_ASN1_PRINTABLESTRING | B_ASN1_IA5STRING}, |
| 1688 |
{MBSTRING_UTF8, |
| 1689 |
{0xc4, 0x80 /* U+0100 */}, |
| 1690 |
B_ASN1_PRINTABLESTRING | B_ASN1_IA5STRING | B_ASN1_T61STRING}, |
| 1691 |
{MBSTRING_UTF8, |
| 1692 |
{0xf0, 0x90, 0x80, 0x80 /* U+10000 */}, |
| 1693 |
B_ASN1_PRINTABLESTRING | B_ASN1_IA5STRING | B_ASN1_T61STRING | |
| 1694 |
B_ASN1_BMPSTRING}, |
| 1695 |
|
| 1696 |
// Unrecognized bits are ignored. |
| 1697 |
{MBSTRING_UTF8, {'\n'}, B_ASN1_PRINTABLESTRING | B_ASN1_SEQUENCE}, |
| 1698 |
}; |
| 1699 |
for (const auto &t : kInvalidTests) { |
| 1700 |
SCOPED_TRACE(t.format); |
| 1701 |
SCOPED_TRACE(Bytes(t.in)); |
| 1702 |
SCOPED_TRACE(t.mask); |
| 1703 |
|
| 1704 |
EXPECT_EQ(-1, ASN1_mbstring_copy(nullptr, t.in.data(), t.in.size(), |
| 1705 |
t.format, t.mask)); |
| 1706 |
ERR_clear_error(); |
| 1707 |
|
| 1708 |
ASN1_STRING *str = nullptr; |
| 1709 |
EXPECT_EQ(-1, ASN1_mbstring_copy(&str, t.in.data(), t.in.size(), |
| 1710 |
t.format, t.mask)); |
| 1711 |
ERR_clear_error(); |
| 1712 |
EXPECT_EQ(nullptr, str); |
| 1713 |
} |
| 1714 |
} |
| 1715 |
|
| 1716 |
TEST(ASN1Test, StringByNID) { |
| 1717 |
// |ASN1_mbstring_*| tests above test most of the interactions with |inform|, |
| 1718 |
// so all tests below use UTF-8. |
| 1719 |
const struct { |
| 1720 |
int nid; |
| 1721 |
std::string in; |
| 1722 |
int expected_type; |
| 1723 |
std::string expected; |
| 1724 |
} kTests[] = { |
| 1725 |
// Although DirectoryString and PKCS9String allow many types of strings, |
| 1726 |
// we prefer UTF8String. |
| 1727 |
{NID_commonName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1728 |
{NID_commonName, "\xe2\x98\x83", V_ASN1_UTF8STRING, "\xe2\x98\x83"}, |
| 1729 |
{NID_localityName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1730 |
{NID_stateOrProvinceName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1731 |
{NID_organizationName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1732 |
{NID_organizationalUnitName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1733 |
{NID_pkcs9_unstructuredName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1734 |
{NID_pkcs9_challengePassword, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1735 |
{NID_pkcs9_unstructuredAddress, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1736 |
{NID_givenName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1737 |
{NID_givenName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1738 |
{NID_givenName, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1739 |
{NID_surname, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1740 |
{NID_initials, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1741 |
{NID_name, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1742 |
|
| 1743 |
// Some attribute types use a particular string type. |
| 1744 |
{NID_countryName, "US", V_ASN1_PRINTABLESTRING, "US"}, |
| 1745 |
{NID_pkcs9_emailAddress, "[email protected]", V_ASN1_IA5STRING, |
| 1746 |
"[email protected]"}, |
| 1747 |
{NID_serialNumber, "1234", V_ASN1_PRINTABLESTRING, "1234"}, |
| 1748 |
{NID_friendlyName, "abc", V_ASN1_BMPSTRING, |
| 1749 |
std::string({'\0', 'a', '\0', 'b', '\0', 'c'})}, |
| 1750 |
{NID_dnQualifier, "US", V_ASN1_PRINTABLESTRING, "US"}, |
| 1751 |
{NID_domainComponent, "com", V_ASN1_IA5STRING, "com"}, |
| 1752 |
{NID_ms_csp_name, "abc", V_ASN1_BMPSTRING, |
| 1753 |
std::string({'\0', 'a', '\0', 'b', '\0', 'c'})}, |
| 1754 |
|
| 1755 |
// Unknown NIDs default to UTF8String. |
| 1756 |
{NID_rsaEncryption, "abc", V_ASN1_UTF8STRING, "abc"}, |
| 1757 |
}; |
| 1758 |
for (const auto &t : kTests) { |
| 1759 |
SCOPED_TRACE(t.nid); |
| 1760 |
SCOPED_TRACE(t.in); |
| 1761 |
|
| 1762 |
// Test allocating a new object. |
| 1763 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_set_by_NID( |
| 1764 |
nullptr, reinterpret_cast<const uint8_t *>(t.in.data()), t.in.size(), |
| 1765 |
MBSTRING_UTF8, t.nid)); |
| 1766 |
ASSERT_TRUE(str); |
| 1767 |
EXPECT_EQ(t.expected_type, ASN1_STRING_type(str.get())); |
| 1768 |
EXPECT_EQ(Bytes(t.expected), Bytes(ASN1_STRING_get0_data(str.get()), |
| 1769 |
ASN1_STRING_length(str.get()))); |
| 1770 |
|
| 1771 |
// Test writing into an existing object. |
| 1772 |
str.reset(ASN1_STRING_new()); |
| 1773 |
ASSERT_TRUE(str); |
| 1774 |
ASN1_STRING *old_str = str.get(); |
| 1775 |
ASSERT_TRUE(ASN1_STRING_set_by_NID( |
| 1776 |
&old_str, reinterpret_cast<const uint8_t *>(t.in.data()), t.in.size(), |
| 1777 |
MBSTRING_UTF8, t.nid)); |
| 1778 |
ASSERT_EQ(old_str, str.get()); |
| 1779 |
EXPECT_EQ(t.expected_type, ASN1_STRING_type(str.get())); |
| 1780 |
EXPECT_EQ(Bytes(t.expected), Bytes(ASN1_STRING_get0_data(str.get()), |
| 1781 |
ASN1_STRING_length(str.get()))); |
| 1782 |
} |
| 1783 |
|
| 1784 |
const struct { |
| 1785 |
int nid; |
| 1786 |
std::string in; |
| 1787 |
} kInvalidTests[] = { |
| 1788 |
// DirectoryString forbids empty inputs. |
| 1789 |
{NID_commonName, ""}, |
| 1790 |
{NID_localityName, ""}, |
| 1791 |
{NID_stateOrProvinceName, ""}, |
| 1792 |
{NID_organizationName, ""}, |
| 1793 |
{NID_organizationalUnitName, ""}, |
| 1794 |
{NID_pkcs9_unstructuredName, ""}, |
| 1795 |
{NID_pkcs9_challengePassword, ""}, |
| 1796 |
{NID_pkcs9_unstructuredAddress, ""}, |
| 1797 |
{NID_givenName, ""}, |
| 1798 |
{NID_givenName, ""}, |
| 1799 |
{NID_givenName, ""}, |
| 1800 |
{NID_surname, ""}, |
| 1801 |
{NID_initials, ""}, |
| 1802 |
{NID_name, ""}, |
| 1803 |
|
| 1804 |
// Test upper bounds from RFC 5280. |
| 1805 |
{NID_name, std::string(32769, 'a')}, |
| 1806 |
{NID_commonName, std::string(65, 'a')}, |
| 1807 |
{NID_localityName, std::string(129, 'a')}, |
| 1808 |
{NID_stateOrProvinceName, std::string(129, 'a')}, |
| 1809 |
{NID_organizationName, std::string(65, 'a')}, |
| 1810 |
{NID_organizationalUnitName, std::string(65, 'a')}, |
| 1811 |
{NID_pkcs9_emailAddress, std::string(256, 'a')}, |
| 1812 |
{NID_serialNumber, std::string(65, 'a')}, |
| 1813 |
|
| 1814 |
// X520countryName must be exactly two characters. |
| 1815 |
{NID_countryName, "A"}, |
| 1816 |
{NID_countryName, "AAA"}, |
| 1817 |
|
| 1818 |
// Some string types cannot represent all codepoints. |
| 1819 |
{NID_countryName, "\xe2\x98\x83"}, |
| 1820 |
{NID_pkcs9_emailAddress, "\xe2\x98\x83"}, |
| 1821 |
{NID_serialNumber, "\xe2\x98\x83"}, |
| 1822 |
{NID_dnQualifier, "\xe2\x98\x83"}, |
| 1823 |
{NID_domainComponent, "\xe2\x98\x83"}, |
| 1824 |
}; |
| 1825 |
for (const auto &t : kInvalidTests) { |
| 1826 |
SCOPED_TRACE(t.nid); |
| 1827 |
SCOPED_TRACE(t.in); |
| 1828 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_set_by_NID( |
| 1829 |
nullptr, reinterpret_cast<const uint8_t *>(t.in.data()), t.in.size(), |
| 1830 |
MBSTRING_UTF8, t.nid)); |
| 1831 |
EXPECT_FALSE(str); |
| 1832 |
ERR_clear_error(); |
| 1833 |
} |
| 1834 |
} |
| 1835 |
|
| 1836 |
TEST(ASN1Test, StringByCustomNID) { |
| 1837 |
// This test affects library-global state. We rely on nothing else in the test |
| 1838 |
// suite using these OIDs. |
| 1839 |
int nid1 = OBJ_create("1.2.840.113554.4.1.72585.1000", "custom OID 1000", |
| 1840 |
"custom OID 1000"); |
| 1841 |
ASSERT_NE(NID_undef, nid1); |
| 1842 |
int nid2 = OBJ_create("1.2.840.113554.4.1.72585.1001", "custom OID 1001", |
| 1843 |
"custom OID 1001"); |
| 1844 |
ASSERT_NE(NID_undef, nid2); |
| 1845 |
|
| 1846 |
// Values registered in the string table should be picked up. |
| 1847 |
ASSERT_TRUE(ASN1_STRING_TABLE_add(nid1, 5, 10, V_ASN1_PRINTABLESTRING, |
| 1848 |
STABLE_NO_MASK)); |
| 1849 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_set_by_NID( |
| 1850 |
nullptr, reinterpret_cast<const uint8_t *>("12345"), 5, MBSTRING_UTF8, |
| 1851 |
nid1)); |
| 1852 |
ASSERT_TRUE(str); |
| 1853 |
EXPECT_EQ(V_ASN1_PRINTABLESTRING, ASN1_STRING_type(str.get())); |
| 1854 |
EXPECT_EQ(Bytes("12345"), Bytes(ASN1_STRING_get0_data(str.get()), |
| 1855 |
ASN1_STRING_length(str.get()))); |
| 1856 |
|
| 1857 |
// Minimum and maximum lengths are enforced. |
| 1858 |
str.reset(ASN1_STRING_set_by_NID( |
| 1859 |
nullptr, reinterpret_cast<const uint8_t *>("1234"), 4, MBSTRING_UTF8, |
| 1860 |
nid1)); |
| 1861 |
EXPECT_FALSE(str); |
| 1862 |
ERR_clear_error(); |
| 1863 |
str.reset(ASN1_STRING_set_by_NID( |
| 1864 |
nullptr, reinterpret_cast<const uint8_t *>("12345678901"), 11, |
| 1865 |
MBSTRING_UTF8, nid1)); |
| 1866 |
EXPECT_FALSE(str); |
| 1867 |
ERR_clear_error(); |
| 1868 |
|
| 1869 |
// Without |STABLE_NO_MASK|, we always pick UTF8String. -1 means there is no |
| 1870 |
// length limit. |
| 1871 |
ASSERT_TRUE(ASN1_STRING_TABLE_add(nid2, -1, -1, DIRSTRING_TYPE, 0)); |
| 1872 |
str.reset(ASN1_STRING_set_by_NID(nullptr, |
| 1873 |
reinterpret_cast<const uint8_t *>("12345"), |
| 1874 |
5, MBSTRING_UTF8, nid2)); |
| 1875 |
ASSERT_TRUE(str); |
| 1876 |
EXPECT_EQ(V_ASN1_UTF8STRING, ASN1_STRING_type(str.get())); |
| 1877 |
EXPECT_EQ(Bytes("12345"), Bytes(ASN1_STRING_get0_data(str.get()), |
| 1878 |
ASN1_STRING_length(str.get()))); |
| 1879 |
|
| 1880 |
// Overriding existing entries, built-in or custom, is an error. |
| 1881 |
EXPECT_FALSE( |
| 1882 |
ASN1_STRING_TABLE_add(NID_countryName, -1, -1, DIRSTRING_TYPE, 0)); |
| 1883 |
EXPECT_FALSE(ASN1_STRING_TABLE_add(nid1, -1, -1, DIRSTRING_TYPE, 0)); |
| 1884 |
} |
| 1885 |
|
| 1886 |
#if defined(OPENSSL_THREADS) |
| 1887 |
TEST(ASN1Test, StringByCustomNIDThreads) { |
| 1888 |
// This test affects library-global state. We rely on nothing else in the test |
| 1889 |
// suite using these OIDs. |
| 1890 |
int nid1 = OBJ_create("1.2.840.113554.4.1.72585.1002", "custom OID 1002", |
| 1891 |
"custom OID 1002"); |
| 1892 |
ASSERT_NE(NID_undef, nid1); |
| 1893 |
int nid2 = OBJ_create("1.2.840.113554.4.1.72585.1003", "custom OID 1003", |
| 1894 |
"custom OID 1003"); |
| 1895 |
ASSERT_NE(NID_undef, nid2); |
| 1896 |
|
| 1897 |
std::vector<std::thread> threads; |
| 1898 |
threads.emplace_back([&] { |
| 1899 |
ASSERT_TRUE(ASN1_STRING_TABLE_add(nid1, 5, 10, V_ASN1_PRINTABLESTRING, |
| 1900 |
STABLE_NO_MASK)); |
| 1901 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_set_by_NID( |
| 1902 |
nullptr, reinterpret_cast<const uint8_t *>("12345"), 5, MBSTRING_UTF8, |
| 1903 |
nid1)); |
| 1904 |
ASSERT_TRUE(str); |
| 1905 |
EXPECT_EQ(V_ASN1_PRINTABLESTRING, ASN1_STRING_type(str.get())); |
| 1906 |
EXPECT_EQ(Bytes("12345"), Bytes(ASN1_STRING_get0_data(str.get()), |
| 1907 |
ASN1_STRING_length(str.get()))); |
| 1908 |
}); |
| 1909 |
threads.emplace_back([&] { |
| 1910 |
ASSERT_TRUE(ASN1_STRING_TABLE_add(nid2, 5, 10, V_ASN1_PRINTABLESTRING, |
| 1911 |
STABLE_NO_MASK)); |
| 1912 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_set_by_NID( |
| 1913 |
nullptr, reinterpret_cast<const uint8_t *>("12345"), 5, MBSTRING_UTF8, |
| 1914 |
nid2)); |
| 1915 |
ASSERT_TRUE(str); |
| 1916 |
EXPECT_EQ(V_ASN1_PRINTABLESTRING, ASN1_STRING_type(str.get())); |
| 1917 |
EXPECT_EQ(Bytes("12345"), Bytes(ASN1_STRING_get0_data(str.get()), |
| 1918 |
ASN1_STRING_length(str.get()))); |
| 1919 |
}); |
| 1920 |
for (auto &thread : threads) { |
| 1921 |
thread.join(); |
| 1922 |
} |
| 1923 |
} |
| 1924 |
#endif // OPENSSL_THREADS |
| 1925 |
|
| 1926 |
// Test that multi-string types correctly encode negative ENUMERATED. |
| 1927 |
// Multi-string types cannot contain INTEGER, so we only test ENUMERATED. |
| 1928 |
TEST(ASN1Test, NegativeEnumeratedMultistring) { |
| 1929 |
static const uint8_t kMinusOne[] = {0x0a, 0x01, 0xff}; // ENUMERATED { -1 } |
| 1930 |
// |ASN1_PRINTABLE| is a multi-string type that allows ENUMERATED. |
| 1931 |
const uint8_t *p = kMinusOne; |
| 1932 |
bssl::UniquePtr<ASN1_STRING> str( |
| 1933 |
d2i_ASN1_PRINTABLE(nullptr, &p, sizeof(kMinusOne))); |
| 1934 |
ASSERT_TRUE(str); |
| 1935 |
TestSerialize(str.get(), i2d_ASN1_PRINTABLE, kMinusOne); |
| 1936 |
} |
| 1937 |
|
| 1938 |
// Encoding a CHOICE type with an invalid selector should fail. |
| 1939 |
TEST(ASN1Test, InvalidChoice) { |
| 1940 |
bssl::UniquePtr<GENERAL_NAME> name(GENERAL_NAME_new()); |
| 1941 |
ASSERT_TRUE(name); |
| 1942 |
// CHOICE types are initialized with an invalid selector. |
| 1943 |
EXPECT_EQ(-1, name->type); |
| 1944 |
// |name| should fail to encode. |
| 1945 |
EXPECT_EQ(-1, i2d_GENERAL_NAME(name.get(), nullptr)); |
| 1946 |
|
| 1947 |
// The error should be propagated through types containing |name|. |
| 1948 |
bssl::UniquePtr<GENERAL_NAMES> names(GENERAL_NAMES_new()); |
| 1949 |
ASSERT_TRUE(names); |
| 1950 |
EXPECT_TRUE(bssl::PushToStack(names.get(), std::move(name))); |
| 1951 |
EXPECT_EQ(-1, i2d_GENERAL_NAMES(names.get(), nullptr)); |
| 1952 |
} |
| 1953 |
|
| 1954 |
// Encoding NID-only |ASN1_OBJECT|s should fail. |
| 1955 |
TEST(ASN1Test, InvalidObject) { |
| 1956 |
EXPECT_EQ(-1, i2d_ASN1_OBJECT(OBJ_nid2obj(NID_kx_ecdhe), nullptr)); |
| 1957 |
|
| 1958 |
bssl::UniquePtr<X509_ALGOR> alg(X509_ALGOR_new()); |
| 1959 |
ASSERT_TRUE(alg); |
| 1960 |
ASSERT_TRUE(X509_ALGOR_set0(alg.get(), OBJ_nid2obj(NID_kx_ecdhe), |
| 1961 |
V_ASN1_UNDEF, nullptr)); |
| 1962 |
EXPECT_EQ(-1, i2d_X509_ALGOR(alg.get(), nullptr)); |
| 1963 |
} |
| 1964 |
|
| 1965 |
// Encoding invalid |ASN1_TYPE|s should fail. |ASN1_TYPE|s are |
| 1966 |
// default-initialized to an invalid type. |
| 1967 |
TEST(ASN1Test, InvalidASN1Type) { |
| 1968 |
bssl::UniquePtr<ASN1_TYPE> obj(ASN1_TYPE_new()); |
| 1969 |
ASSERT_TRUE(obj); |
| 1970 |
EXPECT_EQ(-1, obj->type); |
| 1971 |
EXPECT_EQ(-1, i2d_ASN1_TYPE(obj.get(), nullptr)); |
| 1972 |
} |
| 1973 |
|
| 1974 |
// Encoding invalid MSTRING types should fail. An MSTRING is a CHOICE of |
| 1975 |
// string-like types. They are initialized to an invalid type. |
| 1976 |
TEST(ASN1Test, InvalidMSTRING) { |
| 1977 |
bssl::UniquePtr<ASN1_STRING> obj(ASN1_TIME_new()); |
| 1978 |
ASSERT_TRUE(obj); |
| 1979 |
EXPECT_EQ(-1, obj->type); |
| 1980 |
EXPECT_EQ(-1, i2d_ASN1_TIME(obj.get(), nullptr)); |
| 1981 |
|
| 1982 |
obj.reset(DIRECTORYSTRING_new()); |
| 1983 |
ASSERT_TRUE(obj); |
| 1984 |
EXPECT_EQ(-1, obj->type); |
| 1985 |
EXPECT_EQ(-1, i2d_DIRECTORYSTRING(obj.get(), nullptr)); |
| 1986 |
} |
| 1987 |
|
| 1988 |
TEST(ASN1Test, StringTableSorted) { |
| 1989 |
const ASN1_STRING_TABLE *table; |
| 1990 |
size_t table_len; |
| 1991 |
asn1_get_string_table_for_testing(&table, &table_len); |
| 1992 |
for (size_t i = 1; i < table_len; i++) { |
| 1993 |
EXPECT_LT(table[i-1].nid, table[i].nid); |
| 1994 |
} |
| 1995 |
} |
| 1996 |
|
| 1997 |
TEST(ASN1Test, Null) { |
| 1998 |
// An |ASN1_NULL| is an opaque, non-null pointer. It is an arbitrary signaling |
| 1999 |
// value and does not need to be freed. (If the pointer is null, this is an |
| 2000 |
// omitted OPTIONAL NULL.) |
| 2001 |
EXPECT_NE(nullptr, ASN1_NULL_new()); |
| 2002 |
|
| 2003 |
// It is safe to free either the non-null pointer or the null one. |
| 2004 |
ASN1_NULL_free(ASN1_NULL_new()); |
| 2005 |
ASN1_NULL_free(nullptr); |
| 2006 |
|
| 2007 |
// A NULL may be decoded. |
| 2008 |
static const uint8_t kNull[] = {0x05, 0x00}; |
| 2009 |
const uint8_t *ptr = kNull; |
| 2010 |
EXPECT_NE(nullptr, d2i_ASN1_NULL(nullptr, &ptr, sizeof(kNull))); |
| 2011 |
EXPECT_EQ(ptr, kNull + sizeof(kNull)); |
| 2012 |
|
| 2013 |
// It may also be re-encoded. |
| 2014 |
uint8_t *enc = nullptr; |
| 2015 |
int enc_len = i2d_ASN1_NULL(ASN1_NULL_new(), &enc); |
| 2016 |
ASSERT_GE(enc_len, 0); |
| 2017 |
EXPECT_EQ(Bytes(kNull), Bytes(enc, enc_len)); |
| 2018 |
OPENSSL_free(enc); |
| 2019 |
enc = nullptr; |
| 2020 |
|
| 2021 |
// Although the standalone representation of NULL is a non-null pointer, the |
| 2022 |
// |ASN1_TYPE| representation is a null pointer. |
| 2023 |
ptr = kNull; |
| 2024 |
bssl::UniquePtr<ASN1_TYPE> null_type( |
| 2025 |
d2i_ASN1_TYPE(nullptr, &ptr, sizeof(kNull))); |
| 2026 |
ASSERT_TRUE(null_type); |
| 2027 |
EXPECT_EQ(ptr, kNull + sizeof(kNull)); |
| 2028 |
EXPECT_EQ(V_ASN1_NULL, ASN1_TYPE_get(null_type.get())); |
| 2029 |
EXPECT_EQ(nullptr, null_type->value.ptr); |
| 2030 |
} |
| 2031 |
|
| 2032 |
TEST(ASN1Test, Pack) { |
| 2033 |
bssl::UniquePtr<BASIC_CONSTRAINTS> val(BASIC_CONSTRAINTS_new()); |
| 2034 |
ASSERT_TRUE(val); |
| 2035 |
val->ca = 0; |
| 2036 |
|
| 2037 |
// Test all three calling conventions. |
| 2038 |
static const uint8_t kExpected[] = {0x30, 0x00}; |
| 2039 |
bssl::UniquePtr<ASN1_STRING> str( |
| 2040 |
ASN1_item_pack(val.get(), ASN1_ITEM_rptr(BASIC_CONSTRAINTS), nullptr)); |
| 2041 |
ASSERT_TRUE(str); |
| 2042 |
EXPECT_EQ( |
| 2043 |
Bytes(ASN1_STRING_get0_data(str.get()), ASN1_STRING_length(str.get())), |
| 2044 |
Bytes(kExpected)); |
| 2045 |
|
| 2046 |
ASN1_STRING *raw = nullptr; |
| 2047 |
str.reset(ASN1_item_pack(val.get(), ASN1_ITEM_rptr(BASIC_CONSTRAINTS), &raw)); |
| 2048 |
ASSERT_TRUE(str); |
| 2049 |
EXPECT_EQ(raw, str.get()); |
| 2050 |
EXPECT_EQ( |
| 2051 |
Bytes(ASN1_STRING_get0_data(str.get()), ASN1_STRING_length(str.get())), |
| 2052 |
Bytes(kExpected)); |
| 2053 |
|
| 2054 |
str.reset(ASN1_STRING_new()); |
| 2055 |
ASSERT_TRUE(str); |
| 2056 |
raw = str.get(); |
| 2057 |
EXPECT_TRUE( |
| 2058 |
ASN1_item_pack(val.get(), ASN1_ITEM_rptr(BASIC_CONSTRAINTS), &raw)); |
| 2059 |
EXPECT_EQ(raw, str.get()); |
| 2060 |
EXPECT_EQ( |
| 2061 |
Bytes(ASN1_STRING_get0_data(str.get()), ASN1_STRING_length(str.get())), |
| 2062 |
Bytes(kExpected)); |
| 2063 |
} |
| 2064 |
|
| 2065 |
TEST(ASN1Test, Unpack) { |
| 2066 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_new()); |
| 2067 |
ASSERT_TRUE(str); |
| 2068 |
|
| 2069 |
static const uint8_t kValid[] = {0x30, 0x00}; |
| 2070 |
ASSERT_TRUE( |
| 2071 |
ASN1_STRING_set(str.get(), kValid, sizeof(kValid))); |
| 2072 |
bssl::UniquePtr<BASIC_CONSTRAINTS> val(static_cast<BASIC_CONSTRAINTS *>( |
| 2073 |
ASN1_item_unpack(str.get(), ASN1_ITEM_rptr(BASIC_CONSTRAINTS)))); |
| 2074 |
ASSERT_TRUE(val); |
| 2075 |
EXPECT_EQ(val->ca, 0); |
| 2076 |
EXPECT_EQ(val->pathlen, nullptr); |
| 2077 |
|
| 2078 |
static const uint8_t kInvalid[] = {0x31, 0x00}; |
| 2079 |
ASSERT_TRUE(ASN1_STRING_set(str.get(), kInvalid, sizeof(kInvalid))); |
| 2080 |
val.reset(static_cast<BASIC_CONSTRAINTS *>( |
| 2081 |
ASN1_item_unpack(str.get(), ASN1_ITEM_rptr(BASIC_CONSTRAINTS)))); |
| 2082 |
EXPECT_FALSE(val); |
| 2083 |
|
| 2084 |
static const uint8_t kTraiilingData[] = {0x30, 0x00, 0x00}; |
| 2085 |
ASSERT_TRUE( |
| 2086 |
ASN1_STRING_set(str.get(), kTraiilingData, sizeof(kTraiilingData))); |
| 2087 |
val.reset(static_cast<BASIC_CONSTRAINTS *>( |
| 2088 |
ASN1_item_unpack(str.get(), ASN1_ITEM_rptr(BASIC_CONSTRAINTS)))); |
| 2089 |
EXPECT_FALSE(val); |
| 2090 |
} |
| 2091 |
|
| 2092 |
TEST(ASN1Test, StringCmp) { |
| 2093 |
struct Input { |
| 2094 |
int type; |
| 2095 |
std::vector<uint8_t> data; |
| 2096 |
int flags; |
| 2097 |
bool equals_previous; |
| 2098 |
}; |
| 2099 |
// kInputs is a list of |ASN1_STRING| parameters, in sorted order. The input |
| 2100 |
// should be sorted by bit length, then data, then type. |
| 2101 |
const Input kInputs[] = { |
| 2102 |
{V_ASN1_BIT_STRING, {}, ASN1_STRING_FLAG_BITS_LEFT | 0, false}, |
| 2103 |
{V_ASN1_BIT_STRING, {}, 0, true}, |
| 2104 |
// When |ASN1_STRING_FLAG_BITS_LEFT| is unset, BIT STRINGs implicitly |
| 2105 |
// drop trailing zeros. |
| 2106 |
{V_ASN1_BIT_STRING, {0x00, 0x00, 0x00, 0x00}, 0, true}, |
| 2107 |
|
| 2108 |
{V_ASN1_OCTET_STRING, {}, 0, false}, |
| 2109 |
{V_ASN1_UTF8STRING, {}, 0, false}, |
| 2110 |
|
| 2111 |
// BIT STRINGs with padding bits (i.e. not part of the actual value) are |
| 2112 |
// shorter and thus sort earlier: |
| 2113 |
// 1-bit inputs. |
| 2114 |
{V_ASN1_BIT_STRING, {0x00}, ASN1_STRING_FLAG_BITS_LEFT | 7, false}, |
| 2115 |
{V_ASN1_BIT_STRING, {0x80}, ASN1_STRING_FLAG_BITS_LEFT | 7, false}, |
| 2116 |
// 2-bit inputs. |
| 2117 |
{V_ASN1_BIT_STRING, {0x00}, ASN1_STRING_FLAG_BITS_LEFT | 6, false}, |
| 2118 |
{V_ASN1_BIT_STRING, {0xc0}, ASN1_STRING_FLAG_BITS_LEFT | 6, false}, |
| 2119 |
// 3-bit inputs. |
| 2120 |
{V_ASN1_BIT_STRING, {0x00}, ASN1_STRING_FLAG_BITS_LEFT | 5, false}, |
| 2121 |
{V_ASN1_BIT_STRING, {0xe0}, ASN1_STRING_FLAG_BITS_LEFT | 5, false}, |
| 2122 |
// 4-bit inputs. |
| 2123 |
{V_ASN1_BIT_STRING, {0xf0}, ASN1_STRING_FLAG_BITS_LEFT | 4, false}, |
| 2124 |
{V_ASN1_BIT_STRING, {0xf0}, 0, true}, // 4 trailing zeros dropped. |
| 2125 |
{V_ASN1_BIT_STRING, {0xf0, 0x00}, 0, true}, // 12 trailing zeros dropped. |
| 2126 |
// 5-bit inputs. |
| 2127 |
{V_ASN1_BIT_STRING, {0x00}, ASN1_STRING_FLAG_BITS_LEFT | 3, false}, |
| 2128 |
{V_ASN1_BIT_STRING, {0xf0}, ASN1_STRING_FLAG_BITS_LEFT | 3, false}, |
| 2129 |
{V_ASN1_BIT_STRING, {0xf8}, ASN1_STRING_FLAG_BITS_LEFT | 3, false}, |
| 2130 |
// 6-bit inputs. |
| 2131 |
{V_ASN1_BIT_STRING, {0x00}, ASN1_STRING_FLAG_BITS_LEFT | 2, false}, |
| 2132 |
{V_ASN1_BIT_STRING, {0xf0}, ASN1_STRING_FLAG_BITS_LEFT | 2, false}, |
| 2133 |
{V_ASN1_BIT_STRING, {0xfc}, ASN1_STRING_FLAG_BITS_LEFT | 2, false}, |
| 2134 |
// 7-bit inputs. |
| 2135 |
{V_ASN1_BIT_STRING, {0x00}, ASN1_STRING_FLAG_BITS_LEFT | 1, false}, |
| 2136 |
{V_ASN1_BIT_STRING, {0xf0}, ASN1_STRING_FLAG_BITS_LEFT | 1, false}, |
| 2137 |
{V_ASN1_BIT_STRING, {0xfe}, ASN1_STRING_FLAG_BITS_LEFT | 1, false}, |
| 2138 |
|
| 2139 |
// 8-bit inputs. |
| 2140 |
{V_ASN1_BIT_STRING, {0x00}, ASN1_STRING_FLAG_BITS_LEFT | 0, false}, |
| 2141 |
{V_ASN1_OCTET_STRING, {0x00}, 0, false}, |
| 2142 |
{V_ASN1_UTF8STRING, {0x00}, 0, false}, |
| 2143 |
|
| 2144 |
{V_ASN1_BIT_STRING, {0x80}, ASN1_STRING_FLAG_BITS_LEFT | 0, false}, |
| 2145 |
{V_ASN1_OCTET_STRING, {0x80}, 0, false}, |
| 2146 |
{V_ASN1_UTF8STRING, {0x80}, 0, false}, |
| 2147 |
|
| 2148 |
{V_ASN1_BIT_STRING, {0xff}, ASN1_STRING_FLAG_BITS_LEFT | 0, false}, |
| 2149 |
{V_ASN1_BIT_STRING, {0xff}, 0, true}, // No trailing zeros to drop. |
| 2150 |
{V_ASN1_OCTET_STRING, {0xff}, 0, false}, |
| 2151 |
{V_ASN1_UTF8STRING, {0xff}, 0, false}, |
| 2152 |
|
| 2153 |
// Bytes are compared lexicographically. |
| 2154 |
{V_ASN1_BIT_STRING, {0x00, 0x00}, ASN1_STRING_FLAG_BITS_LEFT | 0, false}, |
| 2155 |
{V_ASN1_OCTET_STRING, {0x00, 0x00}, 0, false}, |
| 2156 |
{V_ASN1_UTF8STRING, {0x00, 0x00}, 0, false}, |
| 2157 |
|
| 2158 |
{V_ASN1_BIT_STRING, {0x00, 0xff}, ASN1_STRING_FLAG_BITS_LEFT | 0, false}, |
| 2159 |
{V_ASN1_OCTET_STRING, {0x00, 0xff}, 0, false}, |
| 2160 |
{V_ASN1_UTF8STRING, {0x00, 0xff}, 0, false}, |
| 2161 |
|
| 2162 |
{V_ASN1_BIT_STRING, {0xff, 0x00}, ASN1_STRING_FLAG_BITS_LEFT | 0, false}, |
| 2163 |
{V_ASN1_OCTET_STRING, {0xff, 0x00}, 0, false}, |
| 2164 |
{V_ASN1_UTF8STRING, {0xff, 0x00}, 0, false}, |
| 2165 |
}; |
| 2166 |
std::vector<bssl::UniquePtr<ASN1_STRING>> strs; |
| 2167 |
strs.reserve(OPENSSL_ARRAY_SIZE(kInputs)); |
| 2168 |
for (const auto &input : kInputs) { |
| 2169 |
strs.emplace_back(ASN1_STRING_type_new(input.type)); |
| 2170 |
ASSERT_TRUE(strs.back()); |
| 2171 |
ASSERT_TRUE(ASN1_STRING_set(strs.back().get(), input.data.data(), |
| 2172 |
input.data.size())); |
| 2173 |
strs.back()->flags = input.flags; |
| 2174 |
} |
| 2175 |
|
| 2176 |
for (size_t i = 0; i < strs.size(); i++) { |
| 2177 |
SCOPED_TRACE(i); |
| 2178 |
bool expect_equal = true; |
| 2179 |
for (size_t j = i; j < strs.size(); j++) { |
| 2180 |
SCOPED_TRACE(j); |
| 2181 |
if (j > i && !kInputs[j].equals_previous) { |
| 2182 |
expect_equal = false; |
| 2183 |
} |
| 2184 |
|
| 2185 |
const int cmp_i_j = ASN1_STRING_cmp(strs[i].get(), strs[j].get()); |
| 2186 |
const int cmp_j_i = ASN1_STRING_cmp(strs[j].get(), strs[i].get()); |
| 2187 |
if (expect_equal) { |
| 2188 |
EXPECT_EQ(cmp_i_j, 0); |
| 2189 |
EXPECT_EQ(cmp_j_i, 0); |
| 2190 |
} else if (i < j) { |
| 2191 |
EXPECT_LT(cmp_i_j, 0); |
| 2192 |
EXPECT_GT(cmp_j_i, 0); |
| 2193 |
} else { |
| 2194 |
EXPECT_GT(cmp_i_j, 0); |
| 2195 |
EXPECT_LT(cmp_j_i, 0); |
| 2196 |
} |
| 2197 |
} |
| 2198 |
} |
| 2199 |
} |
| 2200 |
|
| 2201 |
TEST(ASN1Test, PrintASN1Object) { |
| 2202 |
const struct { |
| 2203 |
std::vector<uint8_t> in; |
| 2204 |
const char *expected; |
| 2205 |
} kDataTests[] = { |
| 2206 |
// Known OIDs print as the name. |
| 2207 |
{{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01}, "rsaEncryption"}, |
| 2208 |
|
| 2209 |
// Unknown OIDs print in decimal. |
| 2210 |
{{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00}, |
| 2211 |
"1.2.840.113554.4.1.72585.0"}, |
| 2212 |
|
| 2213 |
// Inputs which cannot be parsed as OIDs print as "<INVALID>". |
| 2214 |
{{0xff}, "<INVALID>"}, |
| 2215 |
|
| 2216 |
// The function has an internal 80-byte buffer. Test inputs at that |
| 2217 |
// boundary. First, 78 characters. |
| 2218 |
{{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, |
| 2219 |
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2220 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2221 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, |
| 2222 |
"1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0." |
| 2223 |
"0.0.0.1"}, |
| 2224 |
// 79 characters. |
| 2225 |
{{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, |
| 2226 |
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2227 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2228 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a}, |
| 2229 |
"1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0." |
| 2230 |
"0.0.0.10"}, |
| 2231 |
// 80 characters. |
| 2232 |
{{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, |
| 2233 |
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2234 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2235 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64}, |
| 2236 |
"1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0." |
| 2237 |
"0.0.0.100"}, |
| 2238 |
// 81 characters. |
| 2239 |
{{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, |
| 2240 |
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2241 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2242 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87, 0x68}, |
| 2243 |
"1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0." |
| 2244 |
"0.0.0.1000"}, |
| 2245 |
// 82 characters. |
| 2246 |
{{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, |
| 2247 |
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2248 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2249 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xce, 0x10}, |
| 2250 |
"1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0." |
| 2251 |
"0.0.0.10000"}, |
| 2252 |
}; |
| 2253 |
for (const auto &t : kDataTests) { |
| 2254 |
SCOPED_TRACE(Bytes(t.in)); |
| 2255 |
bssl::UniquePtr<ASN1_OBJECT> obj(ASN1_OBJECT_create( |
| 2256 |
NID_undef, t.in.data(), t.in.size(), /*sn=*/nullptr, /*ln=*/nullptr)); |
| 2257 |
ASSERT_TRUE(obj); |
| 2258 |
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 2259 |
ASSERT_TRUE(bio); |
| 2260 |
|
| 2261 |
int len = i2a_ASN1_OBJECT(bio.get(), obj.get()); |
| 2262 |
EXPECT_EQ(len, static_cast<int>(strlen(t.expected))); |
| 2263 |
|
| 2264 |
const uint8_t *bio_data; |
| 2265 |
size_t bio_len; |
| 2266 |
BIO_mem_contents(bio.get(), &bio_data, &bio_len); |
| 2267 |
EXPECT_EQ(t.expected, |
| 2268 |
std::string(reinterpret_cast<const char *>(bio_data), bio_len)); |
| 2269 |
} |
| 2270 |
|
| 2271 |
// Test writing NULL. |
| 2272 |
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 2273 |
ASSERT_TRUE(bio); |
| 2274 |
int len = i2a_ASN1_OBJECT(bio.get(), nullptr); |
| 2275 |
EXPECT_EQ(len, 4); |
| 2276 |
const uint8_t *bio_data; |
| 2277 |
size_t bio_len; |
| 2278 |
BIO_mem_contents(bio.get(), &bio_data, &bio_len); |
| 2279 |
EXPECT_EQ("NULL", |
| 2280 |
std::string(reinterpret_cast<const char *>(bio_data), bio_len)); |
| 2281 |
} |
| 2282 |
|
| 2283 |
const struct GetObjectTestData { |
| 2284 |
std::vector<uint8_t> in; |
| 2285 |
const int expected_return; |
| 2286 |
const int expected_tag; |
| 2287 |
const int expected_class; |
| 2288 |
const int expected_length; |
| 2289 |
} kGetObjectTests[] = { |
| 2290 |
// OpenSSL succeeds for all test cases below. |
| 2291 |
// The header is valid, but there are not enough bytes for the length. |
| 2292 |
{{0x30, 0x01}, 0x80, 0, 0, 0}, |
| 2293 |
{{0x30, 0x80, 0x00, 0x00}, 0x21, 0x10, 0, 0}, |
| 2294 |
{{0x00, 0x00}, 0x00, 0x00, 0x00, 0}, |
| 2295 |
{{0x01, 0x00}, 0x00, 0x01, 0x00, 0}, |
| 2296 |
{{0x41, 0x00}, 0x00, 0x01, 0x40, 0}, |
| 2297 |
{{0x81, 0x00}, 0x00, 0x01, 0x80, 0}, |
| 2298 |
{{0xC1, 0x00}, 0x00, 0x01, 0xC0, 0}, |
| 2299 |
{{0x1F, 0x20, 0x00}, 0x00, 0x20, 0x00, 0}, |
| 2300 |
// Rejected to avoid ambiguity with V_ASN1_NEG. Ruby has a test case expecting this to succeed. |
| 2301 |
{{0x1F, 0xC0, 0x20, 0x00}, 0x80, 0x00, 0x00, 0}, |
| 2302 |
{{0x41, 0x02, 0xAB, 0xCD}, 0x00, 0x01, 0x40, 2}, |
| 2303 |
{{0x61, 0x00}, 0x20, 0x01, 0x40, 0}, |
| 2304 |
{{0x61, 0x80, 0xC2, 0x02, 0xAB, 0xCD, 0x00, 0x00}, 0x21, 0x01, 0x40, 0}, |
| 2305 |
}; |
| 2306 |
|
| 2307 |
static void verifyGetObject(const GetObjectTestData& t) { |
| 2308 |
long length; |
| 2309 |
int tag; |
| 2310 |
int tag_class; |
| 2311 |
|
| 2312 |
SCOPED_TRACE(Bytes(t.in)); |
| 2313 |
const uint8_t *ptr = t.in.data(); |
| 2314 |
EXPECT_EQ(t.expected_return, |
| 2315 |
ASN1_get_object(&ptr, &length, &tag, &tag_class, t.in.size())); |
| 2316 |
if (!(t.expected_return & 0x80)) { |
| 2317 |
EXPECT_EQ(t.expected_length, length); |
| 2318 |
EXPECT_EQ(t.expected_tag, tag); |
| 2319 |
EXPECT_EQ(t.expected_class, tag_class); |
| 2320 |
} |
| 2321 |
} |
| 2322 |
|
| 2323 |
TEST(ASN1Test, GetObject) { |
| 2324 |
for (const auto &t : kGetObjectTests) { |
| 2325 |
verifyGetObject(t); |
| 2326 |
} |
| 2327 |
|
| 2328 |
{ |
| 2329 |
GetObjectTestData test_case{ {0x41, 0x81, 0x80}, 0x00, 0x01, 0x40, 128 }; |
| 2330 |
for(int i = 0; i < 64; i++) { |
| 2331 |
test_case.in.push_back(0xAB); |
| 2332 |
test_case.in.push_back(0xCD); |
| 2333 |
} |
| 2334 |
verifyGetObject(test_case); |
| 2335 |
} |
| 2336 |
|
| 2337 |
{ |
| 2338 |
GetObjectTestData test_case{ {0x41, 0x82, 0x01, 0x00}, 0x00, 0x01, 0x40, 256 }; |
| 2339 |
for(int i = 0; i < 128; i++) { |
| 2340 |
test_case.in.push_back(0xAB); |
| 2341 |
test_case.in.push_back(0xCD); |
| 2342 |
} |
| 2343 |
verifyGetObject(test_case); |
| 2344 |
} |
| 2345 |
|
| 2346 |
} |
| 2347 |
|
| 2348 |
// The zero tag, constructed or primitive, is reserved and should rejected by |
| 2349 |
// the parser. |
| 2350 |
TEST(ASN1Test, ZeroTag) { |
| 2351 |
ExpectParse(d2i_ASN1_TYPE, {0x00, 0x00}, true); |
| 2352 |
ExpectParse(d2i_ASN1_TYPE, {0x00, 0x10, 0x00}, |
| 2353 |
false); // OpenSSL also rejects this. |
| 2354 |
// SEQUENCE { |
| 2355 |
// OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.1 } |
| 2356 |
// [UNIVERSAL 0 PRIMITIVE] { "a" } |
| 2357 |
// } |
| 2358 |
ExpectParse(d2i_X509_ALGOR, |
| 2359 |
{0x30, 0x11, 0x06, 0x0c, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, |
| 2360 |
0x01, 0x84, 0xb7, 0x09, 0x01, 0x00, 0x01, 0x61}, |
| 2361 |
true); |
| 2362 |
|
| 2363 |
|
| 2364 |
// The following test cases are rejected by OpenSSL with their type specific |
| 2365 |
// counterparts. They are parsable with |d2i_ASN1_TYPE| however, and we test |
| 2366 |
// that later. |
| 2367 |
const std::vector<uint8_t> zero_tag_sequence = {0x30, 0x02, 0x00, 0x00}; |
| 2368 |
const std::vector<uint8_t> zero_tag_set_any = {0x31, 0x02, 0x00, 0x00}; |
| 2369 |
// Taken from OpenSSL's test/asn1_decode_test.c. |
| 2370 |
const std::vector<uint8_t> openssl_t_invalid_zero = {0x31, 0x02, 0x00, 0x00}; |
| 2371 |
// SEQUENCE { |
| 2372 |
// OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.1 } |
| 2373 |
// [UNIVERSAL 0 PRIMITIVE] {} |
| 2374 |
// } |
| 2375 |
const std::vector<uint8_t> universal_0_primitive_empty = { |
| 2376 |
0x30, 0x10, 0x06, 0x0c, 0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 2377 |
0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x01, 0x00, 0x00}; |
| 2378 |
ExpectParse(d2i_ASN1_SEQUENCE_ANY, zero_tag_sequence, false); |
| 2379 |
ExpectParse(d2i_ASN1_SET_ANY, zero_tag_set_any, false); |
| 2380 |
ExpectParse(d2i_X509_ALGOR, universal_0_primitive_empty, false); |
| 2381 |
ExpectParse(d2i_ASN1_SEQUENCE_ANY, openssl_t_invalid_zero, false); |
| 2382 |
// Test that the equivalent test cases are parsable with |ASN1_TYPE| (like |
| 2383 |
// OpenSSL). |
| 2384 |
ExpectParse(d2i_ASN1_TYPE, zero_tag_sequence, true); |
| 2385 |
ExpectParse(d2i_ASN1_TYPE, zero_tag_set_any, true); |
| 2386 |
ExpectParse(d2i_ASN1_TYPE, universal_0_primitive_empty, true); |
| 2387 |
ExpectParse(d2i_ASN1_TYPE, openssl_t_invalid_zero, true); |
| 2388 |
|
| 2389 |
|
| 2390 |
// TODO: Change expectation of below to true. Below use BER constructed |
| 2391 |
// strings and will still fail until we revert a70edd4. |
| 2392 |
|
| 2393 |
// SEQUENCE { |
| 2394 |
// OBJECT_IDENTIFIER { 1.2.840.113554.4.1.72585.1 } |
| 2395 |
// [UNIVERSAL 0 CONSTRUCTED] {} |
| 2396 |
// } |
| 2397 |
ExpectParse(d2i_X509_ALGOR, |
| 2398 |
{0x30, 0x10, 0x06, 0x0c, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, |
| 2399 |
0x01, 0x84, 0xb7, 0x09, 0x01, 0x20, 0x00}, |
| 2400 |
true); |
| 2401 |
|
| 2402 |
ExpectParse(d2i_ASN1_TYPE, {0x20, 0x00}, true); |
| 2403 |
ExpectParse(d2i_ASN1_TYPE, {0x20, 0x00}, true); |
| 2404 |
} |
| 2405 |
|
| 2406 |
TEST(ASN1Test, IndefiniteLength) { |
| 2407 |
// Indefinite lengths are more common across container types. |
| 2408 |
ExpectParse(d2i_ASN1_SEQUENCE_ANY, {0x30, 0x80, 0x02, 0x01, 0x2a, 0x00, 0x00}, |
| 2409 |
true); |
| 2410 |
ExpectParse(d2i_ASN1_SET_ANY, |
| 2411 |
{0x31, 0x80, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x00, 0x00}, |
| 2412 |
true); |
| 2413 |
|
| 2414 |
// constructed [APPLICATION 31] with an invalid indefinite length ending. |
| 2415 |
ExpectParse(d2i_ASN1_TYPE, {0x7f, 0x1f, 0x80, 0x01, 0x01, 0x02, 0x00}, false); |
| 2416 |
// Nested constructed [APPLICATION 31] within another constructed |
| 2417 |
// [APPLICATION 31] with indefinite lengths. |
| 2418 |
ExpectParse(d2i_ASN1_TYPE, |
| 2419 |
{0x7f, 0x1f, 0x80, 0x7f, 0x1f, 0x80, 0x03, 0x01, 0x02, 0x00, 0x00, |
| 2420 |
0x00, 0x00}, |
| 2421 |
true); |
| 2422 |
|
| 2423 |
// The ones below use constructed form. This is indicated with (0x20 | 0x??) |
| 2424 |
// in the first byte. |
| 2425 |
ExpectParse( |
| 2426 |
d2i_ASN1_BIT_STRING, |
| 2427 |
{0x23, 0x80, 0x03, 0x02, 0x00, 0xFF, 0x03, 0x02, 0x00, 0xAA, 0x00, 0x00}, |
| 2428 |
true); |
| 2429 |
ExpectParse( |
| 2430 |
d2i_ASN1_OCTET_STRING, |
| 2431 |
{0x24, 0x80, 0x04, 0x02, 0x12, 0x34, 0x04, 0x02, 0x56, 0x78, 0x00, 0x00}, |
| 2432 |
true); |
| 2433 |
// |ASN1_INTEGER|s only have primitive encoding. They do not have constructed |
| 2434 |
// forms. |
| 2435 |
ExpectParse(d2i_ASN1_INTEGER, |
| 2436 |
{0x22, 0x80, 0x02, 0x01, 0x12, 0x02, 0x01, 0x34, 0x00, 0x00}, |
| 2437 |
false); |
| 2438 |
} |
| 2439 |
|
| 2440 |
// Exhaustively test POSIX time conversions for every day across the millenium. |
| 2441 |
TEST(ASN1Test, POSIXTime) { |
| 2442 |
const int kDaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 2443 |
|
| 2444 |
// Test the epoch explicitly, to confirm our baseline is correct. |
| 2445 |
struct tm civil_time; |
| 2446 |
ASSERT_TRUE(OPENSSL_posix_to_tm(0, &civil_time)); |
| 2447 |
ASSERT_EQ(civil_time.tm_year + 1900, 1970); |
| 2448 |
ASSERT_EQ(civil_time.tm_mon + 1, 1); |
| 2449 |
ASSERT_EQ(civil_time.tm_mday, 1); |
| 2450 |
ASSERT_EQ(civil_time.tm_hour, 0); |
| 2451 |
ASSERT_EQ(civil_time.tm_min, 0); |
| 2452 |
ASSERT_EQ(civil_time.tm_sec, 0); |
| 2453 |
|
| 2454 |
int64_t posix_time = -11676096000; // Sat, 01 Jan 1600 00:00:00 +0000 |
| 2455 |
for (int year = 1600; year < 3000; year++) { |
| 2456 |
SCOPED_TRACE(year); |
| 2457 |
bool is_leap_year = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; |
| 2458 |
for (int month = 1; month <= 12; month++) { |
| 2459 |
SCOPED_TRACE(month); |
| 2460 |
int days = kDaysInMonth[month - 1]; |
| 2461 |
if (month == 2 && is_leap_year) { |
| 2462 |
days++; |
| 2463 |
} |
| 2464 |
for (int day = 1; day <= days; day++) { |
| 2465 |
SCOPED_TRACE(day); |
| 2466 |
SCOPED_TRACE(posix_time); |
| 2467 |
|
| 2468 |
ASSERT_TRUE(OPENSSL_posix_to_tm(posix_time, &civil_time)); |
| 2469 |
ASSERT_EQ(civil_time.tm_year + 1900, year); |
| 2470 |
ASSERT_EQ(civil_time.tm_mon + 1, month); |
| 2471 |
ASSERT_EQ(civil_time.tm_mday, day); |
| 2472 |
ASSERT_EQ(civil_time.tm_hour, 0); |
| 2473 |
ASSERT_EQ(civil_time.tm_min, 0); |
| 2474 |
ASSERT_EQ(civil_time.tm_sec, 0); |
| 2475 |
|
| 2476 |
int64_t posix_time_computed; |
| 2477 |
ASSERT_TRUE(OPENSSL_tm_to_posix(&civil_time, &posix_time_computed)); |
| 2478 |
ASSERT_EQ(posix_time_computed, posix_time); |
| 2479 |
|
| 2480 |
// Advance to the next day. |
| 2481 |
posix_time += 24 * 60 * 60; |
| 2482 |
} |
| 2483 |
} |
| 2484 |
} |
| 2485 |
} |
| 2486 |
|
| 2487 |
TEST(ASN1Test, LargeString) { |
| 2488 |
bssl::UniquePtr<ASN1_STRING> str(ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); |
| 2489 |
ASSERT_TRUE(str); |
| 2490 |
// Very large strings should be rejected by |ASN1_STRING_set|. Strictly |
| 2491 |
// speaking, this is an invalid call because the buffer does not have that |
| 2492 |
// much size available. |ASN1_STRING_set| should cleanly fail before it |
| 2493 |
// crashes, and actually allocating 512 MiB in a test is likely to break. |
| 2494 |
char b = 0; |
| 2495 |
EXPECT_FALSE(ASN1_STRING_set(str.get(), &b, INT_MAX / 4)); |
| 2496 |
|
| 2497 |
#if defined(OPENSSL_64_BIT) |
| 2498 |
// |ASN1_STRING_set| should tolerate lengths that exceed |int| without |
| 2499 |
// overflow. |
| 2500 |
EXPECT_FALSE(ASN1_STRING_set(str.get(), &b, 1 + (ossl_ssize_t{1} << 48))); |
| 2501 |
#endif |
| 2502 |
} |
| 2503 |
|
| 2504 |
|
| 2505 |
// Wrapper functions are needed to get around Control Flow Integrity Sanitizers. |
| 2506 |
static int i2d_ASN1_TYPE_void(const void *a, unsigned char **out) { |
| 2507 |
return i2d_ASN1_TYPE((ASN1_TYPE *)a, out); |
| 2508 |
} |
| 2509 |
static void *d2i_ASN1_TYPE_void(void **a, const unsigned char **in, long len) { |
| 2510 |
return d2i_ASN1_TYPE((ASN1_TYPE **)a, in, len); |
| 2511 |
} |
| 2512 |
static int i2d_ECPrivateKey_void(const void *a, unsigned char **out) { |
| 2513 |
return i2d_ECPrivateKey((EC_KEY *)a, out); |
| 2514 |
} |
| 2515 |
static void *d2i_ECPrivateKey_void(void **a, const unsigned char **in, long len) { |
| 2516 |
return d2i_ECPrivateKey((EC_KEY **)a, in, len); |
| 2517 |
} |
| 2518 |
static int i2d_X509_PUBKEY_void(const void *a, unsigned char **out) { |
| 2519 |
return i2d_X509_PUBKEY((X509_PUBKEY *)a, out); |
| 2520 |
} |
| 2521 |
static void *d2i_X509_PUBKEY_void(void **a, const unsigned char **in, long len) { |
| 2522 |
return d2i_X509_PUBKEY((X509_PUBKEY **)a, in, len); |
| 2523 |
} |
| 2524 |
|
| 2525 |
TEST(ASN1Test, ASN1Dup) { |
| 2526 |
const uint8_t *tag = kTag128; |
| 2527 |
bssl::UniquePtr<ASN1_TYPE> asn1( |
| 2528 |
d2i_ASN1_TYPE(nullptr, &tag, sizeof(kTag128))); |
| 2529 |
ASSERT_TRUE(asn1); |
| 2530 |
EXPECT_EQ(128, asn1->type); |
| 2531 |
bssl::UniquePtr<ASN1_TYPE> asn1_copy((ASN1_TYPE *)ASN1_dup( |
| 2532 |
i2d_ASN1_TYPE_void, d2i_ASN1_TYPE_void, asn1.get())); |
| 2533 |
ASSERT_TRUE(asn1_copy); |
| 2534 |
EXPECT_EQ(ASN1_TYPE_cmp(asn1.get(), asn1_copy.get()), 0); |
| 2535 |
|
| 2536 |
bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); |
| 2537 |
ASSERT_TRUE(key); |
| 2538 |
ASSERT_TRUE(EC_KEY_generate_key(key.get())); |
| 2539 |
bssl::UniquePtr<EC_KEY> key_copy((EC_KEY *)ASN1_dup( |
| 2540 |
i2d_ECPrivateKey_void, d2i_ECPrivateKey_void, key.get())); |
| 2541 |
ASSERT_TRUE(key_copy); |
| 2542 |
EXPECT_EQ(BN_cmp(EC_KEY_get0_private_key(key.get()), |
| 2543 |
EC_KEY_get0_private_key(key_copy.get())), |
| 2544 |
0); |
| 2545 |
EXPECT_EQ(EC_GROUP_cmp(EC_KEY_get0_group(key.get()), |
| 2546 |
EC_KEY_get0_group(key_copy.get()), nullptr), |
| 2547 |
0); |
| 2548 |
EXPECT_EQ(EC_POINT_cmp(EC_KEY_get0_group(key_copy.get()), |
| 2549 |
EC_KEY_get0_public_key(key.get()), |
| 2550 |
EC_KEY_get0_public_key(key_copy.get()), nullptr), |
| 2551 |
0); |
| 2552 |
|
| 2553 |
bssl::UniquePtr<EVP_PKEY> evp_pkey(EVP_PKEY_new()); |
| 2554 |
OPENSSL_BEGIN_ALLOW_DEPRECATED |
| 2555 |
ASSERT_FALSE(EVP_PKEY_get0(evp_pkey.get())); |
| 2556 |
X509_PUBKEY *tmp_key = nullptr; |
| 2557 |
ASSERT_TRUE(evp_pkey); |
| 2558 |
ASSERT_TRUE(EVP_PKEY_set1_EC_KEY(evp_pkey.get(), key.get())); |
| 2559 |
ASSERT_EQ(key.get(), EVP_PKEY_get0(evp_pkey.get())); |
| 2560 |
OPENSSL_END_ALLOW_DEPRECATED |
| 2561 |
ASSERT_TRUE(X509_PUBKEY_set(&tmp_key, evp_pkey.get())); |
| 2562 |
bssl::UniquePtr<X509_PUBKEY> x509_pubkey(tmp_key); |
| 2563 |
bssl::UniquePtr<X509_PUBKEY> x509_pubkey_copy((X509_PUBKEY *)ASN1_dup( |
| 2564 |
i2d_X509_PUBKEY_void, d2i_X509_PUBKEY_void, x509_pubkey.get())); |
| 2565 |
ASSERT_TRUE(x509_pubkey_copy); |
| 2566 |
EXPECT_EQ( |
| 2567 |
ASN1_STRING_cmp(X509_PUBKEY_get0_public_key(x509_pubkey.get()), |
| 2568 |
X509_PUBKEY_get0_public_key(x509_pubkey_copy.get())), |
| 2569 |
0); |
| 2570 |
} |
| 2571 |
|
| 2572 |
static std::tuple<int, int, int, int, int, int> TimeToTuple(const tm &t) { |
| 2573 |
return std::make_tuple(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, |
| 2574 |
t.tm_sec); |
| 2575 |
} |
| 2576 |
|
| 2577 |
TEST(ASN1Test, TimeOverflow) { |
| 2578 |
// Input time is out of range and may overflow internal calculations to shift |
| 2579 |
// |tm_year| and |tm_mon| to a more normal value. |
| 2580 |
tm overflow_year; |
| 2581 |
OPENSSL_memset(&overflow_year, 0, sizeof(overflow_year)); |
| 2582 |
overflow_year.tm_year = INT_MAX - 1899; |
| 2583 |
overflow_year.tm_mday = 1; |
| 2584 |
tm overflow_month; |
| 2585 |
OPENSSL_memset(&overflow_month, 0, sizeof(overflow_month)); |
| 2586 |
overflow_month.tm_mon = INT_MAX; |
| 2587 |
overflow_month.tm_mday = 1; |
| 2588 |
int64_t posix_u64; |
| 2589 |
EXPECT_FALSE(OPENSSL_tm_to_posix(&overflow_year, &posix_u64)); |
| 2590 |
EXPECT_FALSE(OPENSSL_tm_to_posix(&overflow_month, &posix_u64)); |
| 2591 |
time_t posix; |
| 2592 |
EXPECT_FALSE(OPENSSL_timegm(&overflow_year, &posix)); |
| 2593 |
EXPECT_FALSE(OPENSSL_timegm(&overflow_month, &posix)); |
| 2594 |
EXPECT_FALSE( |
| 2595 |
OPENSSL_gmtime_adj(&overflow_year, /*offset_day=*/0, /*offset_sec=*/0)); |
| 2596 |
EXPECT_FALSE( |
| 2597 |
OPENSSL_gmtime_adj(&overflow_month, /*offset_day=*/0, /*offset_sec=*/0)); |
| 2598 |
int days, secs; |
| 2599 |
EXPECT_FALSE( |
| 2600 |
OPENSSL_gmtime_diff(&days, &secs, &overflow_year, &overflow_year)); |
| 2601 |
EXPECT_FALSE( |
| 2602 |
OPENSSL_gmtime_diff(&days, &secs, &overflow_month, &overflow_month)); |
| 2603 |
|
| 2604 |
// Input time is in range, but even adding one second puts it out of range. |
| 2605 |
tm max_time; |
| 2606 |
OPENSSL_memset(&max_time, 0, sizeof(max_time)); |
| 2607 |
max_time.tm_year = 9999 - 1900; |
| 2608 |
max_time.tm_mon = 12 - 1; |
| 2609 |
max_time.tm_mday = 31; |
| 2610 |
max_time.tm_hour = 23; |
| 2611 |
max_time.tm_min = 59; |
| 2612 |
max_time.tm_sec = 59; |
| 2613 |
tm copy = max_time; |
| 2614 |
EXPECT_TRUE(OPENSSL_gmtime_adj(©, /*offset_day=*/0, /*offset_sec=*/0)); |
| 2615 |
EXPECT_EQ(TimeToTuple(copy), TimeToTuple(max_time)); |
| 2616 |
EXPECT_FALSE(OPENSSL_gmtime_adj(©, /*offset_day=*/0, /*offset_sec=*/1)); |
| 2617 |
|
| 2618 |
// Likewise for the earliest representable time. |
| 2619 |
tm min_time; |
| 2620 |
OPENSSL_memset(&min_time, 0, sizeof(min_time)); |
| 2621 |
min_time.tm_year = 0 - 1900; |
| 2622 |
min_time.tm_mon = 1 - 1; |
| 2623 |
min_time.tm_mday = 1; |
| 2624 |
min_time.tm_hour = 0; |
| 2625 |
min_time.tm_min = 0; |
| 2626 |
min_time.tm_sec = 0; |
| 2627 |
copy = min_time; |
| 2628 |
EXPECT_TRUE(OPENSSL_gmtime_adj(©, /*offset_day=*/0, /*offset_sec=*/0)); |
| 2629 |
EXPECT_EQ(TimeToTuple(copy), TimeToTuple(min_time)); |
| 2630 |
EXPECT_FALSE(OPENSSL_gmtime_adj(©, /*offset_day=*/0, /*offset_sec=*/-1)); |
| 2631 |
|
| 2632 |
// Test we can offset between the minimum and maximum times. |
| 2633 |
const int64_t kValidTimeRange = 315569519999; |
| 2634 |
copy = min_time; |
| 2635 |
EXPECT_TRUE(OPENSSL_gmtime_adj(©, /*offset_day=*/0, kValidTimeRange)); |
| 2636 |
EXPECT_EQ(TimeToTuple(copy), TimeToTuple(max_time)); |
| 2637 |
EXPECT_TRUE(OPENSSL_gmtime_adj(©, /*offset_day=*/0, -kValidTimeRange)); |
| 2638 |
EXPECT_EQ(TimeToTuple(copy), TimeToTuple(min_time)); |
| 2639 |
|
| 2640 |
// The second offset may even exceed kValidTimeRange if it is canceled out by |
| 2641 |
// offset_day. |
| 2642 |
EXPECT_TRUE(OPENSSL_gmtime_adj(©, /*offset_day=*/-1, |
| 2643 |
kValidTimeRange + 24 * 3600)); |
| 2644 |
EXPECT_EQ(TimeToTuple(copy), TimeToTuple(max_time)); |
| 2645 |
EXPECT_TRUE(OPENSSL_gmtime_adj(©, /*offset_day=*/1, |
| 2646 |
-kValidTimeRange - 24 * 3600)); |
| 2647 |
EXPECT_EQ(TimeToTuple(copy), TimeToTuple(min_time)); |
| 2648 |
|
| 2649 |
// Make sure the internal calculations for |OPENSSL_gmtime_adj| stay in |
| 2650 |
// bounds. |
| 2651 |
copy = max_time; |
| 2652 |
EXPECT_FALSE(OPENSSL_gmtime_adj(©, INT_MAX, LONG_MAX)); |
| 2653 |
copy = min_time; |
| 2654 |
EXPECT_FALSE(OPENSSL_gmtime_adj(©, INT_MIN, LONG_MIN)); |
| 2655 |
} |
| 2656 |
|
| 2657 |
// The ASN.1 macros do not work on Windows shared library builds, where usage of |
| 2658 |
// |OPENSSL_EXPORT| is a bit stricter. |
| 2659 |
#if !defined(OPENSSL_WINDOWS) || !defined(BORINGSSL_SHARED_LIBRARY) |
| 2660 |
|
| 2661 |
typedef struct asn1_linked_list_st { |
| 2662 |
struct asn1_linked_list_st *next; |
| 2663 |
} ASN1_LINKED_LIST; |
| 2664 |
|
| 2665 |
DECLARE_ASN1_ITEM(ASN1_LINKED_LIST) |
| 2666 |
DECLARE_ASN1_FUNCTIONS(ASN1_LINKED_LIST) |
| 2667 |
|
| 2668 |
ASN1_SEQUENCE(ASN1_LINKED_LIST) = { |
| 2669 |
ASN1_OPT(ASN1_LINKED_LIST, next, ASN1_LINKED_LIST), |
| 2670 |
} ASN1_SEQUENCE_END(ASN1_LINKED_LIST) |
| 2671 |
|
| 2672 |
IMPLEMENT_ASN1_FUNCTIONS(ASN1_LINKED_LIST) |
| 2673 |
|
| 2674 |
static bool MakeLinkedList(bssl::UniquePtr<uint8_t> *out, size_t *out_len, |
| 2675 |
size_t count) { |
| 2676 |
bssl::ScopedCBB cbb; |
| 2677 |
std::vector<CBB> cbbs(count); |
| 2678 |
if (!CBB_init(cbb.get(), 2 * count) || |
| 2679 |
!CBB_add_asn1(cbb.get(), &cbbs[0], CBS_ASN1_SEQUENCE)) { |
| 2680 |
return false; |
| 2681 |
} |
| 2682 |
for (size_t i = 1; i < count; i++) { |
| 2683 |
if (!CBB_add_asn1(&cbbs[i - 1], &cbbs[i], CBS_ASN1_SEQUENCE)) { |
| 2684 |
return false; |
| 2685 |
} |
| 2686 |
} |
| 2687 |
uint8_t *ptr; |
| 2688 |
if (!CBB_finish(cbb.get(), &ptr, out_len)) { |
| 2689 |
return false; |
| 2690 |
} |
| 2691 |
out->reset(ptr); |
| 2692 |
return true; |
| 2693 |
} |
| 2694 |
|
| 2695 |
TEST(ASN1Test, Recursive) { |
| 2696 |
bssl::UniquePtr<uint8_t> data; |
| 2697 |
size_t len; |
| 2698 |
|
| 2699 |
// Sanity-check that MakeLinkedList can be parsed. |
| 2700 |
ASSERT_TRUE(MakeLinkedList(&data, &len, 5)); |
| 2701 |
const uint8_t *ptr = data.get(); |
| 2702 |
ASN1_LINKED_LIST *list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len); |
| 2703 |
EXPECT_TRUE(list); |
| 2704 |
ASN1_LINKED_LIST_free(list); |
| 2705 |
|
| 2706 |
// Excessively deep structures are rejected. |
| 2707 |
ASSERT_TRUE(MakeLinkedList(&data, &len, 100)); |
| 2708 |
ptr = data.get(); |
| 2709 |
list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len); |
| 2710 |
EXPECT_FALSE(list); |
| 2711 |
// Note checking the error queue here does not work. The error "stack trace" |
| 2712 |
// is too deep, so the |ASN1_R_NESTED_TOO_DEEP| entry drops off the queue. |
| 2713 |
ASN1_LINKED_LIST_free(list); |
| 2714 |
} |
| 2715 |
|
| 2716 |
static int i2d_ASN1_LINKED_LIST_void(const void *a, unsigned char **out) { |
| 2717 |
return i2d_ASN1_LINKED_LIST((ASN1_LINKED_LIST *)a, out); |
| 2718 |
} |
| 2719 |
|
| 2720 |
TEST(ASN1Test, BIO) { |
| 2721 |
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 2722 |
bssl::UniquePtr<uint8_t> data; |
| 2723 |
size_t len; |
| 2724 |
ASSERT_TRUE(MakeLinkedList(&data, &len, 5)); |
| 2725 |
const uint8_t *ptr = data.get(); |
| 2726 |
ASN1_LINKED_LIST *list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len); |
| 2727 |
EXPECT_TRUE(list); |
| 2728 |
|
| 2729 |
// Retrieve expected bytes. |
| 2730 |
uint8_t *expected = nullptr; |
| 2731 |
int expected_len = i2d_ASN1_LINKED_LIST(list, &expected); |
| 2732 |
ASSERT_GT(expected_len, 0); |
| 2733 |
|
| 2734 |
const uint8_t *out; |
| 2735 |
size_t out_len; |
| 2736 |
EXPECT_TRUE(ASN1_i2d_bio(i2d_ASN1_LINKED_LIST_void, bio.get(), list)); |
| 2737 |
ASSERT_TRUE(BIO_mem_contents(bio.get(), &out, &out_len)); |
| 2738 |
|
| 2739 |
EXPECT_EQ(Bytes(out, out_len), Bytes(expected, expected_len)); |
| 2740 |
OPENSSL_free(expected); |
| 2741 |
ASN1_LINKED_LIST_free(list); |
| 2742 |
} |
| 2743 |
|
| 2744 |
struct IMPLICIT_CHOICE { |
| 2745 |
ASN1_STRING *string; |
| 2746 |
}; |
| 2747 |
|
| 2748 |
DECLARE_ASN1_FUNCTIONS(IMPLICIT_CHOICE) |
| 2749 |
|
| 2750 |
ASN1_SEQUENCE(IMPLICIT_CHOICE) = { |
| 2751 |
ASN1_IMP(IMPLICIT_CHOICE, string, DIRECTORYSTRING, 0), |
| 2752 |
} ASN1_SEQUENCE_END(IMPLICIT_CHOICE) |
| 2753 |
|
| 2754 |
IMPLEMENT_ASN1_FUNCTIONS(IMPLICIT_CHOICE) |
| 2755 |
|
| 2756 |
// Test that the ASN.1 templates reject types with implicitly-tagged CHOICE |
| 2757 |
// types. |
| 2758 |
TEST(ASN1Test, ImplicitChoice) { |
| 2759 |
// Serializing a type with an implicitly tagged CHOICE should fail. |
| 2760 |
std::unique_ptr<IMPLICIT_CHOICE, decltype(&IMPLICIT_CHOICE_free)> obj( |
| 2761 |
IMPLICIT_CHOICE_new(), IMPLICIT_CHOICE_free); |
| 2762 |
EXPECT_EQ(-1, i2d_IMPLICIT_CHOICE(obj.get(), nullptr)); |
| 2763 |
|
| 2764 |
// An implicitly-tagged CHOICE is an error. Depending on the implementation, |
| 2765 |
// it may be misinterpreted as without the tag, or as clobbering the CHOICE |
| 2766 |
// tag. Test both inputs and ensure they fail. |
| 2767 |
|
| 2768 |
// SEQUENCE { UTF8String {} } |
| 2769 |
static const uint8_t kInput1[] = {0x30, 0x02, 0x0c, 0x00}; |
| 2770 |
const uint8_t *ptr = kInput1; |
| 2771 |
EXPECT_EQ(nullptr, d2i_IMPLICIT_CHOICE(nullptr, &ptr, sizeof(kInput1))); |
| 2772 |
|
| 2773 |
// SEQUENCE { [0 PRIMITIVE] {} } |
| 2774 |
static const uint8_t kInput2[] = {0x30, 0x02, 0x80, 0x00}; |
| 2775 |
ptr = kInput2; |
| 2776 |
EXPECT_EQ(nullptr, d2i_IMPLICIT_CHOICE(nullptr, &ptr, sizeof(kInput2))); |
| 2777 |
} |
| 2778 |
|
| 2779 |
struct REQUIRED_FIELD { |
| 2780 |
ASN1_INTEGER *value; |
| 2781 |
ASN1_INTEGER *value_imp; |
| 2782 |
ASN1_INTEGER *value_exp; |
| 2783 |
STACK_OF(ASN1_INTEGER) *seq; |
| 2784 |
STACK_OF(ASN1_INTEGER) *seq_imp; |
| 2785 |
STACK_OF(ASN1_INTEGER) *seq_exp; |
| 2786 |
ASN1_NULL *null; |
| 2787 |
ASN1_NULL *null_imp; |
| 2788 |
ASN1_NULL *null_exp; |
| 2789 |
}; |
| 2790 |
|
| 2791 |
DECLARE_ASN1_FUNCTIONS(REQUIRED_FIELD) |
| 2792 |
ASN1_SEQUENCE(REQUIRED_FIELD) = { |
| 2793 |
ASN1_SIMPLE(REQUIRED_FIELD, value, ASN1_INTEGER), |
| 2794 |
ASN1_IMP(REQUIRED_FIELD, value_imp, ASN1_INTEGER, 0), |
| 2795 |
ASN1_EXP(REQUIRED_FIELD, value_exp, ASN1_INTEGER, 1), |
| 2796 |
ASN1_SEQUENCE_OF(REQUIRED_FIELD, seq, ASN1_INTEGER), |
| 2797 |
ASN1_IMP_SEQUENCE_OF(REQUIRED_FIELD, seq_imp, ASN1_INTEGER, 2), |
| 2798 |
ASN1_EXP_SEQUENCE_OF(REQUIRED_FIELD, seq_exp, ASN1_INTEGER, 3), |
| 2799 |
ASN1_SIMPLE(REQUIRED_FIELD, null, ASN1_NULL), |
| 2800 |
ASN1_IMP(REQUIRED_FIELD, null_imp, ASN1_NULL, 4), |
| 2801 |
ASN1_EXP(REQUIRED_FIELD, null_exp, ASN1_NULL, 5), |
| 2802 |
} ASN1_SEQUENCE_END(REQUIRED_FIELD) |
| 2803 |
IMPLEMENT_ASN1_FUNCTIONS(REQUIRED_FIELD) |
| 2804 |
|
| 2805 |
// Test that structures with missing required fields cannot be serialized. Test |
| 2806 |
// the full combination of tagging and SEQUENCE OF. |
| 2807 |
TEST(ASN1Test, MissingRequiredField) { |
| 2808 |
EXPECT_EQ(-1, i2d_REQUIRED_FIELD(nullptr, nullptr)); |
| 2809 |
|
| 2810 |
std::unique_ptr<REQUIRED_FIELD, decltype(&REQUIRED_FIELD_free)> obj( |
| 2811 |
nullptr, REQUIRED_FIELD_free); |
| 2812 |
for (auto field : {&REQUIRED_FIELD::value, &REQUIRED_FIELD::value_imp, |
| 2813 |
&REQUIRED_FIELD::value_exp}) { |
| 2814 |
obj.reset(REQUIRED_FIELD_new()); |
| 2815 |
ASSERT_TRUE(obj); |
| 2816 |
ASN1_INTEGER_free((*obj).*field); |
| 2817 |
(*obj).*field = nullptr; |
| 2818 |
EXPECT_EQ(-1, i2d_REQUIRED_FIELD(obj.get(), nullptr)); |
| 2819 |
} |
| 2820 |
|
| 2821 |
for (auto field : {&REQUIRED_FIELD::seq, &REQUIRED_FIELD::seq_imp, |
| 2822 |
&REQUIRED_FIELD::seq_exp}) { |
| 2823 |
obj.reset(REQUIRED_FIELD_new()); |
| 2824 |
ASSERT_TRUE(obj); |
| 2825 |
sk_ASN1_INTEGER_pop_free((*obj).*field, ASN1_INTEGER_free); |
| 2826 |
(*obj).*field = nullptr; |
| 2827 |
EXPECT_EQ(-1, i2d_REQUIRED_FIELD(obj.get(), nullptr)); |
| 2828 |
} |
| 2829 |
|
| 2830 |
for (auto field : {&REQUIRED_FIELD::null, &REQUIRED_FIELD::null_imp, |
| 2831 |
&REQUIRED_FIELD::null_exp}) { |
| 2832 |
obj.reset(REQUIRED_FIELD_new()); |
| 2833 |
ASSERT_TRUE(obj); |
| 2834 |
(*obj).*field = nullptr; |
| 2835 |
EXPECT_EQ(-1, i2d_REQUIRED_FIELD(obj.get(), nullptr)); |
| 2836 |
} |
| 2837 |
} |
| 2838 |
|
| 2839 |
struct BOOLEANS { |
| 2840 |
ASN1_BOOLEAN required; |
| 2841 |
ASN1_BOOLEAN optional; |
| 2842 |
ASN1_BOOLEAN default_true; |
| 2843 |
ASN1_BOOLEAN default_false; |
| 2844 |
}; |
| 2845 |
|
| 2846 |
DECLARE_ASN1_FUNCTIONS(BOOLEANS) |
| 2847 |
ASN1_SEQUENCE(BOOLEANS) = { |
| 2848 |
ASN1_SIMPLE(BOOLEANS, required, ASN1_BOOLEAN), |
| 2849 |
ASN1_IMP_OPT(BOOLEANS, optional, ASN1_BOOLEAN, 1), |
| 2850 |
// Although not actually optional, |ASN1_TBOOLEAN| and |ASN1_FBOOLEAN| need |
| 2851 |
// to be marked optional in the template. |
| 2852 |
ASN1_IMP_OPT(BOOLEANS, default_true, ASN1_TBOOLEAN, 2), |
| 2853 |
ASN1_IMP_OPT(BOOLEANS, default_false, ASN1_FBOOLEAN, 3), |
| 2854 |
} ASN1_SEQUENCE_END(BOOLEANS) |
| 2855 |
IMPLEMENT_ASN1_FUNCTIONS(BOOLEANS) |
| 2856 |
|
| 2857 |
TEST(ASN1Test, OptionalAndDefaultBooleans) { |
| 2858 |
std::unique_ptr<BOOLEANS, decltype(&BOOLEANS_free)> obj(nullptr, |
| 2859 |
BOOLEANS_free); |
| 2860 |
|
| 2861 |
// A default-constructed object should use, respectively, omitted, omitted, |
| 2862 |
// TRUE, FALSE. |
| 2863 |
// |
| 2864 |
// TODO(davidben): Is the first one a bug? It seems more consistent for a |
| 2865 |
// required BOOLEAN default to FALSE. |FOO_new| typically default-initializes |
| 2866 |
// fields valid states. (Though there are exceptions. CHOICE, ANY, and OBJECT |
| 2867 |
// IDENTIFIER are default-initialized to something invalid.) |
| 2868 |
obj.reset(BOOLEANS_new()); |
| 2869 |
ASSERT_TRUE(obj); |
| 2870 |
EXPECT_EQ(obj->required, ASN1_BOOLEAN_NONE); |
| 2871 |
EXPECT_EQ(obj->optional, ASN1_BOOLEAN_NONE); |
| 2872 |
EXPECT_EQ(obj->default_true, ASN1_BOOLEAN_TRUE); |
| 2873 |
EXPECT_EQ(obj->default_false, ASN1_BOOLEAN_FALSE); |
| 2874 |
|
| 2875 |
// Trying to serialize this should fail, because |obj->required| is omitted. |
| 2876 |
EXPECT_EQ(-1, i2d_BOOLEANS(obj.get(), nullptr)); |
| 2877 |
|
| 2878 |
// Otherwise, this object is serializable. Most fields are omitted, due to |
| 2879 |
// them being optional or defaulted. |
| 2880 |
static const uint8_t kFieldsOmitted[] = {0x30, 0x03, 0x01, 0x01, 0x00}; |
| 2881 |
obj->required = 0; |
| 2882 |
TestSerialize(obj.get(), i2d_BOOLEANS, kFieldsOmitted); |
| 2883 |
|
| 2884 |
const uint8_t *der = kFieldsOmitted; |
| 2885 |
obj.reset(d2i_BOOLEANS(nullptr, &der, sizeof(kFieldsOmitted))); |
| 2886 |
ASSERT_TRUE(obj); |
| 2887 |
EXPECT_EQ(obj->required, ASN1_BOOLEAN_FALSE); |
| 2888 |
EXPECT_EQ(obj->optional, ASN1_BOOLEAN_NONE); |
| 2889 |
EXPECT_EQ(obj->default_true, ASN1_BOOLEAN_TRUE); |
| 2890 |
EXPECT_EQ(obj->default_false, ASN1_BOOLEAN_FALSE); |
| 2891 |
|
| 2892 |
// Include the optinonal fields instead. |
| 2893 |
static const uint8_t kFieldsIncluded[] = {0x30, 0x0c, 0x01, 0x01, 0xff, |
| 2894 |
0x81, 0x01, 0x00, 0x82, 0x01, |
| 2895 |
0x00, 0x83, 0x01, 0xff}; |
| 2896 |
obj->required = ASN1_BOOLEAN_TRUE; |
| 2897 |
obj->optional = ASN1_BOOLEAN_FALSE; |
| 2898 |
obj->default_true = ASN1_BOOLEAN_FALSE; |
| 2899 |
obj->default_false = ASN1_BOOLEAN_TRUE; |
| 2900 |
TestSerialize(obj.get(), i2d_BOOLEANS, kFieldsIncluded); |
| 2901 |
|
| 2902 |
der = kFieldsIncluded; |
| 2903 |
obj.reset(d2i_BOOLEANS(nullptr, &der, sizeof(kFieldsIncluded))); |
| 2904 |
ASSERT_TRUE(obj); |
| 2905 |
EXPECT_EQ(obj->required, ASN1_BOOLEAN_TRUE); |
| 2906 |
EXPECT_EQ(obj->optional, ASN1_BOOLEAN_FALSE); |
| 2907 |
EXPECT_EQ(obj->default_true, ASN1_BOOLEAN_FALSE); |
| 2908 |
EXPECT_EQ(obj->default_false, ASN1_BOOLEAN_TRUE); |
| 2909 |
|
| 2910 |
// TODO(https://crbug.com/boringssl/354): Reject explicit DEFAULTs. |
| 2911 |
} |
| 2912 |
|
| 2913 |
// EXPLICIT_BOOLEAN is a [1] EXPLICIT BOOLEAN. |
| 2914 |
ASN1_ITEM_TEMPLATE(EXPLICIT_BOOLEAN) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_EXPLICIT, |
| 2915 |
1, |
| 2916 |
EXPLICIT_BOOLEAN, |
| 2917 |
ASN1_BOOLEAN) |
| 2918 |
ASN1_ITEM_TEMPLATE_END(EXPLICIT_BOOLEAN) |
| 2919 |
|
| 2920 |
// EXPLICIT_OCTET_STRING is a [2] EXPLICIT OCTET STRING. |
| 2921 |
ASN1_ITEM_TEMPLATE(EXPLICIT_OCTET_STRING) = ASN1_EX_TEMPLATE_TYPE( |
| 2922 |
ASN1_TFLG_EXPLICIT, 2, EXPLICIT_OCTET_STRING, ASN1_OCTET_STRING) |
| 2923 |
ASN1_ITEM_TEMPLATE_END(EXPLICIT_OCTET_STRING) |
| 2924 |
|
| 2925 |
// DOUBLY_TAGGED is |
| 2926 |
// SEQUENCE { |
| 2927 |
// b [3] EXPLICIT [1] EXPLICIT BOOLEAN OPTIONAL, |
| 2928 |
// oct [4] EXPLICIT [2] EXPLICIT OCTET STRING OPTIONAL } |
| 2929 |
// with explicit tagging. |
| 2930 |
struct DOUBLY_TAGGED { |
| 2931 |
ASN1_BOOLEAN b; |
| 2932 |
ASN1_OCTET_STRING *oct; |
| 2933 |
}; |
| 2934 |
|
| 2935 |
DECLARE_ASN1_FUNCTIONS(DOUBLY_TAGGED) |
| 2936 |
ASN1_SEQUENCE(DOUBLY_TAGGED) = { |
| 2937 |
ASN1_EXP_OPT(DOUBLY_TAGGED, b, EXPLICIT_BOOLEAN, 3), |
| 2938 |
ASN1_EXP_OPT(DOUBLY_TAGGED, oct, EXPLICIT_OCTET_STRING, 4), |
| 2939 |
} ASN1_SEQUENCE_END(DOUBLY_TAGGED) |
| 2940 |
IMPLEMENT_ASN1_FUNCTIONS(DOUBLY_TAGGED) |
| 2941 |
|
| 2942 |
// Test that optional fields with two layers of explicit tagging are correctly |
| 2943 |
// handled. |
| 2944 |
TEST(ASN1Test, DoublyTagged) { |
| 2945 |
std::unique_ptr<DOUBLY_TAGGED, decltype(&DOUBLY_TAGGED_free)> obj( |
| 2946 |
nullptr, DOUBLY_TAGGED_free); |
| 2947 |
|
| 2948 |
// Both fields missing. |
| 2949 |
static const uint8_t kOmitted[] = {0x30, 0x00}; |
| 2950 |
const uint8_t *inp = kOmitted; |
| 2951 |
obj.reset(d2i_DOUBLY_TAGGED(nullptr, &inp, sizeof(kOmitted))); |
| 2952 |
ASSERT_TRUE(obj); |
| 2953 |
EXPECT_EQ(obj->b, -1); |
| 2954 |
EXPECT_FALSE(obj->oct); |
| 2955 |
TestSerialize(obj.get(), i2d_DOUBLY_TAGGED, kOmitted); |
| 2956 |
|
| 2957 |
// Both fields present, true and the empty string. |
| 2958 |
static const uint8_t kTrueEmpty[] = {0x30, 0x0d, 0xa3, 0x05, 0xa1, |
| 2959 |
0x03, 0x01, 0x01, 0xff, 0xa4, |
| 2960 |
0x04, 0xa2, 0x02, 0x04, 0x00}; |
| 2961 |
inp = kTrueEmpty; |
| 2962 |
obj.reset(d2i_DOUBLY_TAGGED(nullptr, &inp, sizeof(kTrueEmpty))); |
| 2963 |
ASSERT_TRUE(obj); |
| 2964 |
EXPECT_EQ(obj->b, 0xff); |
| 2965 |
ASSERT_TRUE(obj->oct); |
| 2966 |
EXPECT_EQ(ASN1_STRING_length(obj->oct), 0); |
| 2967 |
TestSerialize(obj.get(), i2d_DOUBLY_TAGGED, kTrueEmpty); |
| 2968 |
} |
| 2969 |
|
| 2970 |
#define CHOICE_TYPE_OCT 0 |
| 2971 |
#define CHOICE_TYPE_BOOL 1 |
| 2972 |
|
| 2973 |
struct CHOICE_TYPE { |
| 2974 |
int type; |
| 2975 |
union { |
| 2976 |
ASN1_OCTET_STRING *oct; |
| 2977 |
ASN1_BOOLEAN b; |
| 2978 |
} value; |
| 2979 |
}; |
| 2980 |
|
| 2981 |
DECLARE_ASN1_FUNCTIONS(CHOICE_TYPE) |
| 2982 |
ASN1_CHOICE(CHOICE_TYPE) = { |
| 2983 |
ASN1_SIMPLE(CHOICE_TYPE, value.oct, ASN1_OCTET_STRING), |
| 2984 |
ASN1_SIMPLE(CHOICE_TYPE, value.b, ASN1_BOOLEAN), |
| 2985 |
} ASN1_CHOICE_END(CHOICE_TYPE) |
| 2986 |
IMPLEMENT_ASN1_FUNCTIONS(CHOICE_TYPE) |
| 2987 |
|
| 2988 |
struct OPTIONAL_CHOICE { |
| 2989 |
CHOICE_TYPE *choice; |
| 2990 |
}; |
| 2991 |
|
| 2992 |
DECLARE_ASN1_FUNCTIONS(OPTIONAL_CHOICE) |
| 2993 |
ASN1_SEQUENCE(OPTIONAL_CHOICE) = { |
| 2994 |
ASN1_OPT(OPTIONAL_CHOICE, choice, CHOICE_TYPE), |
| 2995 |
} ASN1_SEQUENCE_END(OPTIONAL_CHOICE) |
| 2996 |
IMPLEMENT_ASN1_FUNCTIONS(OPTIONAL_CHOICE) |
| 2997 |
|
| 2998 |
TEST(ASN1Test, OptionalChoice) { |
| 2999 |
std::unique_ptr<OPTIONAL_CHOICE, decltype(&OPTIONAL_CHOICE_free)> obj( |
| 3000 |
nullptr, OPTIONAL_CHOICE_free); |
| 3001 |
|
| 3002 |
// Value omitted. |
| 3003 |
static const uint8_t kOmitted[] = {0x30, 0x00}; |
| 3004 |
const uint8_t *inp = kOmitted; |
| 3005 |
obj.reset(d2i_OPTIONAL_CHOICE(nullptr, &inp, sizeof(kOmitted))); |
| 3006 |
ASSERT_TRUE(obj); |
| 3007 |
EXPECT_FALSE(obj->choice); |
| 3008 |
TestSerialize(obj.get(), i2d_OPTIONAL_CHOICE, kOmitted); |
| 3009 |
|
| 3010 |
// Value is present as an OCTET STRING. |
| 3011 |
static const uint8_t kOct[] = {0x30, 0x02, 0x04, 0x00}; |
| 3012 |
inp = kOct; |
| 3013 |
obj.reset(d2i_OPTIONAL_CHOICE(nullptr, &inp, sizeof(kOct))); |
| 3014 |
ASSERT_TRUE(obj); |
| 3015 |
ASSERT_TRUE(obj->choice); |
| 3016 |
ASSERT_EQ(obj->choice->type, CHOICE_TYPE_OCT); |
| 3017 |
ASSERT_TRUE(obj->choice->value.oct); |
| 3018 |
EXPECT_EQ(ASN1_STRING_length(obj->choice->value.oct), 0); |
| 3019 |
TestSerialize(obj.get(), i2d_OPTIONAL_CHOICE, kOct); |
| 3020 |
|
| 3021 |
// Value is present as TRUE. |
| 3022 |
static const uint8_t kTrue[] = {0x30, 0x03, 0x01, 0x01, 0xff}; |
| 3023 |
inp = kTrue; |
| 3024 |
obj.reset(d2i_OPTIONAL_CHOICE(nullptr, &inp, sizeof(kTrue))); |
| 3025 |
ASSERT_TRUE(obj); |
| 3026 |
ASSERT_TRUE(obj->choice); |
| 3027 |
ASSERT_EQ(obj->choice->type, CHOICE_TYPE_BOOL); |
| 3028 |
EXPECT_EQ(obj->choice->value.b, ASN1_BOOLEAN_TRUE); |
| 3029 |
TestSerialize(obj.get(), i2d_OPTIONAL_CHOICE, kTrue); |
| 3030 |
} |
| 3031 |
|
| 3032 |
struct EMBED_X509_ALGOR { |
| 3033 |
X509_ALGOR *simple; |
| 3034 |
X509_ALGOR *opt; |
| 3035 |
STACK_OF(X509_ALGOR) *seq; |
| 3036 |
}; |
| 3037 |
|
| 3038 |
struct EMBED_X509_EXTENSION { |
| 3039 |
X509_EXTENSION *simple; |
| 3040 |
X509_EXTENSION *opt; |
| 3041 |
STACK_OF(X509_EXTENSION) *seq; |
| 3042 |
}; |
| 3043 |
|
| 3044 |
struct EMBED_X509_NAME { |
| 3045 |
X509_NAME *simple; |
| 3046 |
X509_NAME *opt; |
| 3047 |
STACK_OF(X509_NAME) *seq; |
| 3048 |
}; |
| 3049 |
|
| 3050 |
struct EMBED_X509 { |
| 3051 |
X509 *simple; |
| 3052 |
X509 *opt; |
| 3053 |
STACK_OF(X509) *seq; |
| 3054 |
}; |
| 3055 |
|
| 3056 |
DECLARE_ASN1_FUNCTIONS(EMBED_X509_ALGOR) |
| 3057 |
ASN1_SEQUENCE(EMBED_X509_ALGOR) = { |
| 3058 |
ASN1_SIMPLE(EMBED_X509_ALGOR, simple, X509_ALGOR), |
| 3059 |
ASN1_EXP_OPT(EMBED_X509_ALGOR, opt, X509_ALGOR, 0), |
| 3060 |
ASN1_IMP_SEQUENCE_OF_OPT(EMBED_X509_ALGOR, seq, X509_ALGOR, 1), |
| 3061 |
} ASN1_SEQUENCE_END(EMBED_X509_ALGOR) |
| 3062 |
IMPLEMENT_ASN1_FUNCTIONS(EMBED_X509_ALGOR) |
| 3063 |
|
| 3064 |
DECLARE_ASN1_FUNCTIONS(EMBED_X509_NAME) |
| 3065 |
ASN1_SEQUENCE(EMBED_X509_NAME) = { |
| 3066 |
ASN1_SIMPLE(EMBED_X509_NAME, simple, X509_NAME), |
| 3067 |
ASN1_EXP_OPT(EMBED_X509_NAME, opt, X509_NAME, 0), |
| 3068 |
ASN1_IMP_SEQUENCE_OF_OPT(EMBED_X509_NAME, seq, X509_NAME, 1), |
| 3069 |
} ASN1_SEQUENCE_END(EMBED_X509_NAME) |
| 3070 |
IMPLEMENT_ASN1_FUNCTIONS(EMBED_X509_NAME) |
| 3071 |
|
| 3072 |
DECLARE_ASN1_FUNCTIONS(EMBED_X509_EXTENSION) |
| 3073 |
ASN1_SEQUENCE(EMBED_X509_EXTENSION) = { |
| 3074 |
ASN1_SIMPLE(EMBED_X509_EXTENSION, simple, X509_EXTENSION), |
| 3075 |
ASN1_EXP_OPT(EMBED_X509_EXTENSION, opt, X509_EXTENSION, 0), |
| 3076 |
ASN1_IMP_SEQUENCE_OF_OPT(EMBED_X509_EXTENSION, seq, X509_EXTENSION, 1), |
| 3077 |
} ASN1_SEQUENCE_END(EMBED_X509_EXTENSION) |
| 3078 |
IMPLEMENT_ASN1_FUNCTIONS(EMBED_X509_EXTENSION) |
| 3079 |
|
| 3080 |
DECLARE_ASN1_FUNCTIONS(EMBED_X509) |
| 3081 |
ASN1_SEQUENCE(EMBED_X509) = { |
| 3082 |
ASN1_SIMPLE(EMBED_X509, simple, X509), |
| 3083 |
ASN1_EXP_OPT(EMBED_X509, opt, X509, 0), |
| 3084 |
ASN1_IMP_SEQUENCE_OF_OPT(EMBED_X509, seq, X509, 1), |
| 3085 |
} ASN1_SEQUENCE_END(EMBED_X509) |
| 3086 |
IMPLEMENT_ASN1_FUNCTIONS(EMBED_X509) |
| 3087 |
|
| 3088 |
template <typename EmbedT, typename T, typename MaybeConstT, typename StackT> |
| 3089 |
void TestEmbedType(bssl::Span<const uint8_t> inp, |
| 3090 |
int (*i2d)(MaybeConstT *, uint8_t **), |
| 3091 |
EmbedT *(*embed_new)(), void (*embed_free)(EmbedT *), |
| 3092 |
EmbedT *(*d2i_embed)(EmbedT **, const uint8_t **, long), |
| 3093 |
int (*i2d_embed)(EmbedT *, uint8_t **), |
| 3094 |
size_t (*sk_num)(const StackT *), |
| 3095 |
T *(*sk_value)(const StackT *, size_t)) { |
| 3096 |
std::unique_ptr<EmbedT, decltype(embed_free)> obj(nullptr, embed_free); |
| 3097 |
|
| 3098 |
// Test only the first field present. |
| 3099 |
bssl::ScopedCBB cbb; |
| 3100 |
ASSERT_TRUE(CBB_init(cbb.get(), 64)); |
| 3101 |
CBB seq; |
| 3102 |
ASSERT_TRUE(CBB_add_asn1(cbb.get(), &seq, CBS_ASN1_SEQUENCE)); |
| 3103 |
ASSERT_TRUE(CBB_add_bytes(&seq, inp.data(), inp.size())); |
| 3104 |
ASSERT_TRUE(CBB_flush(cbb.get())); |
| 3105 |
const uint8_t *ptr = CBB_data(cbb.get()); |
| 3106 |
obj.reset(d2i_embed(nullptr, &ptr, CBB_len(cbb.get()))); |
| 3107 |
ASSERT_TRUE(obj); |
| 3108 |
ASSERT_TRUE(obj->simple); |
| 3109 |
// Test the field was parsed correctly by reserializing it. |
| 3110 |
TestSerialize(obj->simple, i2d, inp); |
| 3111 |
EXPECT_FALSE(obj->opt); |
| 3112 |
EXPECT_FALSE(obj->seq); |
| 3113 |
TestSerialize(obj.get(), i2d_embed, |
| 3114 |
{CBB_data(cbb.get()), CBB_len(cbb.get())}); |
| 3115 |
|
| 3116 |
// Test all fields present. |
| 3117 |
cbb.Reset(); |
| 3118 |
ASSERT_TRUE(CBB_init(cbb.get(), 64)); |
| 3119 |
ASSERT_TRUE(CBB_add_asn1(cbb.get(), &seq, CBS_ASN1_SEQUENCE)); |
| 3120 |
ASSERT_TRUE(CBB_add_bytes(&seq, inp.data(), inp.size())); |
| 3121 |
CBB child; |
| 3122 |
ASSERT_TRUE(CBB_add_asn1( |
| 3123 |
&seq, &child, CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)); |
| 3124 |
ASSERT_TRUE(CBB_add_bytes(&child, inp.data(), inp.size())); |
| 3125 |
ASSERT_TRUE(CBB_add_asn1( |
| 3126 |
&seq, &child, CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)); |
| 3127 |
ASSERT_TRUE(CBB_add_bytes(&child, inp.data(), inp.size())); |
| 3128 |
ASSERT_TRUE(CBB_add_bytes(&child, inp.data(), inp.size())); |
| 3129 |
ASSERT_TRUE(CBB_flush(cbb.get())); |
| 3130 |
ptr = CBB_data(cbb.get()); |
| 3131 |
obj.reset(d2i_embed(nullptr, &ptr, CBB_len(cbb.get()))); |
| 3132 |
ASSERT_TRUE(obj); |
| 3133 |
ASSERT_TRUE(obj->simple); |
| 3134 |
TestSerialize(obj->simple, i2d, inp); |
| 3135 |
ASSERT_TRUE(obj->opt); |
| 3136 |
TestSerialize(obj->opt, i2d, inp); |
| 3137 |
ASSERT_EQ(sk_num(obj->seq), 2u); |
| 3138 |
TestSerialize(sk_value(obj->seq, 0), i2d, inp); |
| 3139 |
TestSerialize(sk_value(obj->seq, 1), i2d, inp); |
| 3140 |
TestSerialize(obj.get(), i2d_embed, |
| 3141 |
{CBB_data(cbb.get()), CBB_len(cbb.get())}); |
| 3142 |
} |
| 3143 |
|
| 3144 |
// Test that X.509 types defined in this library can be embedded into other |
| 3145 |
// types, as we rewrite them away from the templating system. |
| 3146 |
TEST(ASN1Test, EmbedTypes) { |
| 3147 |
static const uint8_t kTestAlg[] = {0x30, 0x09, 0x06, 0x07, 0x2a, 0x86, |
| 3148 |
0x48, 0xce, 0x3d, 0x04, 0x01}; |
| 3149 |
TestEmbedType(kTestAlg, i2d_X509_ALGOR, EMBED_X509_ALGOR_new, |
| 3150 |
EMBED_X509_ALGOR_free, d2i_EMBED_X509_ALGOR, |
| 3151 |
i2d_EMBED_X509_ALGOR, sk_X509_ALGOR_num, sk_X509_ALGOR_value); |
| 3152 |
|
| 3153 |
static const uint8_t kTestName[] = { |
| 3154 |
0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, |
| 3155 |
0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, |
| 3156 |
0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, |
| 3157 |
0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, |
| 3158 |
0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, |
| 3159 |
0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64}; |
| 3160 |
TestEmbedType(kTestName, i2d_X509_NAME, EMBED_X509_NAME_new, |
| 3161 |
EMBED_X509_NAME_free, d2i_EMBED_X509_NAME, i2d_EMBED_X509_NAME, |
| 3162 |
sk_X509_NAME_num, sk_X509_NAME_value); |
| 3163 |
|
| 3164 |
static const uint8_t kTestExtension[] = {0x30, 0x0c, 0x06, 0x03, 0x55, |
| 3165 |
0x1d, 0x13, 0x04, 0x05, 0x30, |
| 3166 |
0x03, 0x01, 0x01, 0xf}; |
| 3167 |
TestEmbedType(kTestExtension, i2d_X509_EXTENSION, EMBED_X509_EXTENSION_new, |
| 3168 |
EMBED_X509_EXTENSION_free, d2i_EMBED_X509_EXTENSION, |
| 3169 |
i2d_EMBED_X509_EXTENSION, sk_X509_EXTENSION_num, |
| 3170 |
sk_X509_EXTENSION_value); |
| 3171 |
|
| 3172 |
static const uint8_t kTestCert[] = { |
| 3173 |
0x30, 0x82, 0x01, 0xcf, 0x30, 0x82, 0x01, 0x76, 0xa0, 0x03, 0x02, 0x01, |
| 3174 |
0x02, 0x02, 0x09, 0x00, 0xd9, 0x4c, 0x04, 0xda, 0x49, 0x7d, 0xbf, 0xeb, |
| 3175 |
0x30, 0x09, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x30, |
| 3176 |
0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, |
| 3177 |
0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, |
| 3178 |
0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, |
| 3179 |
0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, |
| 3180 |
0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, |
| 3181 |
0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, |
| 3182 |
0x17, 0x0d, 0x31, 0x34, 0x30, 0x34, 0x32, 0x33, 0x32, 0x33, 0x32, 0x31, |
| 3183 |
0x35, 0x37, 0x5a, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x35, 0x32, 0x33, 0x32, |
| 3184 |
0x33, 0x32, 0x31, 0x35, 0x37, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, |
| 3185 |
0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, |
| 3186 |
0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, |
| 3187 |
0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, |
| 3188 |
0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, |
| 3189 |
0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, |
| 3190 |
0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, |
| 3191 |
0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, |
| 3192 |
0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, |
| 3193 |
0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, |
| 3194 |
0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, |
| 3195 |
0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, |
| 3196 |
0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, |
| 3197 |
0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1, |
| 3198 |
0xa3, 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, |
| 3199 |
0x16, 0x04, 0x14, 0xab, 0x84, 0xd2, 0xac, 0xab, 0x95, 0xf0, 0x82, 0x4e, |
| 3200 |
0x16, 0x78, 0x07, 0x55, 0x57, 0x5f, 0xe4, 0x26, 0x8d, 0x82, 0xd1, 0x30, |
| 3201 |
0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, |
| 3202 |
0xab, 0x84, 0xd2, 0xac, 0xab, 0x95, 0xf0, 0x82, 0x4e, 0x16, 0x78, 0x07, |
| 3203 |
0x55, 0x57, 0x5f, 0xe4, 0x26, 0x8d, 0x82, 0xd1, 0x30, 0x0c, 0x06, 0x03, |
| 3204 |
0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x09, |
| 3205 |
0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01, 0x03, 0x48, 0x00, |
| 3206 |
0x30, 0x45, 0x02, 0x21, 0x00, 0xf2, 0xa0, 0x35, 0x5e, 0x51, 0x3a, 0x36, |
| 3207 |
0xc3, 0x82, 0x79, 0x9b, 0xee, 0x27, 0x50, 0x85, 0x8e, 0x70, 0x06, 0x74, |
| 3208 |
0x95, 0x57, 0xd2, 0x29, 0x74, 0x00, 0xf4, 0xbe, 0x15, 0x87, 0x5d, 0xc4, |
| 3209 |
0x07, 0x02, 0x20, 0x7c, 0x1e, 0x79, 0x14, 0x6a, 0x21, 0x83, 0xf0, 0x7a, |
| 3210 |
0x74, 0x68, 0x79, 0x5f, 0x14, 0x99, 0x9a, 0x68, 0xb4, 0xf1, 0xcb, 0x9e, |
| 3211 |
0x15, 0x5e, 0xe6, 0x1f, 0x32, 0x52, 0x61, 0x5e, 0x75, 0xc9, 0x14}; |
| 3212 |
TestEmbedType(kTestCert, i2d_X509, EMBED_X509_new, EMBED_X509_free, |
| 3213 |
d2i_EMBED_X509, i2d_EMBED_X509, sk_X509_num, sk_X509_value); |
| 3214 |
} |
| 3215 |
|
| 3216 |
TEST(ASN1Test, A2iASN1Integer) { |
| 3217 |
// Test basic a2i_ASN1_INTEGER functionality - parsing hex strings from BIO |
| 3218 |
const struct { |
| 3219 |
const char *input; |
| 3220 |
std::vector<uint8_t> expected_data; |
| 3221 |
int expected_type; |
| 3222 |
} kBasicTests[] = { |
| 3223 |
// Simple hex values - note: leading "00" is stripped from the first line |
| 3224 |
{"01", {0x01}, V_ASN1_INTEGER}, |
| 3225 |
{"FF", {0xff}, V_ASN1_INTEGER}, |
| 3226 |
{"AABB", {0xaa, 0xbb}, V_ASN1_INTEGER}, |
| 3227 |
{"AABBCC", {0xaa, 0xbb, 0xcc}, V_ASN1_INTEGER}, |
| 3228 |
{"0123456789ABCDEF", {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, |
| 3229 |
V_ASN1_INTEGER}, |
| 3230 |
// Leading "00" is stripped; "0000FF" becomes "00FF" = {0x00, 0xff} |
| 3231 |
{"0000FF", {0x00, 0xff}, V_ASN1_INTEGER}, |
| 3232 |
}; |
| 3233 |
|
| 3234 |
for (const auto &t : kBasicTests) { |
| 3235 |
SCOPED_TRACE(t.input); |
| 3236 |
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(t.input, strlen(t.input))); |
| 3237 |
ASSERT_TRUE(bio); |
| 3238 |
|
| 3239 |
bssl::UniquePtr<ASN1_INTEGER> val(ASN1_INTEGER_new()); |
| 3240 |
ASSERT_TRUE(val); |
| 3241 |
|
| 3242 |
char buf[256]; |
| 3243 |
int ret = a2i_ASN1_INTEGER(bio.get(), val.get(), buf, sizeof(buf)); |
| 3244 |
ASSERT_EQ(ret, 1); |
| 3245 |
EXPECT_EQ(val->type, t.expected_type); |
| 3246 |
EXPECT_EQ(Bytes(val->data, val->length), Bytes(t.expected_data)); |
| 3247 |
} |
| 3248 |
|
| 3249 |
// "00" alone results in empty data after stripping leading "00" |
| 3250 |
{ |
| 3251 |
const char *input = "00"; |
| 3252 |
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(input, strlen(input))); |
| 3253 |
ASSERT_TRUE(bio); |
| 3254 |
bssl::UniquePtr<ASN1_INTEGER> val(ASN1_INTEGER_new()); |
| 3255 |
ASSERT_TRUE(val); |
| 3256 |
char buf[256]; |
| 3257 |
int ret = a2i_ASN1_INTEGER(bio.get(), val.get(), buf, sizeof(buf)); |
| 3258 |
ASSERT_EQ(ret, 1); |
| 3259 |
// After stripping "00", we have nothing left, so length is 0 |
| 3260 |
EXPECT_EQ(val->length, 0); |
| 3261 |
} |
| 3262 |
|
| 3263 |
// Test invalid inputs |
| 3264 |
const char *kInvalidTests[] = { |
| 3265 |
"", // Empty |
| 3266 |
"A", // Odd number of hex chars |
| 3267 |
"ABC", // Odd number of hex chars |
| 3268 |
"GG", // Invalid hex chars |
| 3269 |
}; |
| 3270 |
for (const char *input : kInvalidTests) { |
| 3271 |
SCOPED_TRACE(input); |
| 3272 |
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(input, strlen(input))); |
| 3273 |
ASSERT_TRUE(bio); |
| 3274 |
|
| 3275 |
bssl::UniquePtr<ASN1_INTEGER> val(ASN1_INTEGER_new()); |
| 3276 |
ASSERT_TRUE(val); |
| 3277 |
|
| 3278 |
char buf[256]; |
| 3279 |
int ret = a2i_ASN1_INTEGER(bio.get(), val.get(), buf, sizeof(buf)); |
| 3280 |
EXPECT_EQ(ret, 0); |
| 3281 |
ERR_clear_error(); |
| 3282 |
} |
| 3283 |
} |
| 3284 |
|
| 3285 |
TEST(ASN1Test, A2iASN1IntegerLineContinuation) { |
| 3286 |
// Test line continuation with backslash. |
| 3287 |
// i2a_ASN1_INTEGER outputs "\\\n" every 35 bytes (70 hex chars). |
| 3288 |
// a2i_ASN1_INTEGER should handle this continuation. |
| 3289 |
|
| 3290 |
// Simple continuation test: "AABB\\\nCCDD" should parse as 0xAABBCCDD |
| 3291 |
{ |
| 3292 |
const char *input = "AABB\\\nCCDD"; |
| 3293 |
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(input, strlen(input))); |
| 3294 |
ASSERT_TRUE(bio); |
| 3295 |
|
| 3296 |
bssl::UniquePtr<ASN1_INTEGER> val(ASN1_INTEGER_new()); |
| 3297 |
ASSERT_TRUE(val); |
| 3298 |
|
| 3299 |
char buf[256]; |
| 3300 |
int ret = a2i_ASN1_INTEGER(bio.get(), val.get(), buf, sizeof(buf)); |
| 3301 |
ASSERT_EQ(ret, 1); |
| 3302 |
static const uint8_t kExpected[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
| 3303 |
EXPECT_EQ(Bytes(val->data, val->length), Bytes(kExpected)); |
| 3304 |
} |
| 3305 |
|
| 3306 |
// Test with longer continuation |
| 3307 |
{ |
| 3308 |
const char *input = "112233445566\\\n778899AABB"; |
| 3309 |
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(input, strlen(input))); |
| 3310 |
ASSERT_TRUE(bio); |
| 3311 |
|
| 3312 |
bssl::UniquePtr<ASN1_INTEGER> val(ASN1_INTEGER_new()); |
| 3313 |
ASSERT_TRUE(val); |
| 3314 |
|
| 3315 |
char buf[256]; |
| 3316 |
int ret = a2i_ASN1_INTEGER(bio.get(), val.get(), buf, sizeof(buf)); |
| 3317 |
ASSERT_EQ(ret, 1); |
| 3318 |
static const uint8_t kExpected[] = {0x11, 0x22, 0x33, 0x44, 0x55, |
| 3319 |
0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb}; |
| 3320 |
EXPECT_EQ(Bytes(val->data, val->length), Bytes(kExpected)); |
| 3321 |
} |
| 3322 |
} |
| 3323 |
|
| 3324 |
TEST(ASN1Test, I2aA2iASN1IntegerRoundTrip) { |
| 3325 |
// Test round-trip: i2a_ASN1_INTEGER -> a2i_ASN1_INTEGER |
| 3326 |
// For values small enough to not trigger line continuation, this should work. |
| 3327 |
// |
| 3328 |
// Note: {0x00} is intentionally excluded because a2i_ASN1_INTEGER strips |
| 3329 |
// leading "00" from the first line, so "00" -> empty data. This is expected |
| 3330 |
// behavior, not a bug. |
| 3331 |
const std::vector<uint8_t> kTestValues[] = { |
| 3332 |
{0x01}, |
| 3333 |
{0x7f}, |
| 3334 |
{0x80}, |
| 3335 |
{0xff}, |
| 3336 |
{0x01, 0x00}, |
| 3337 |
{0xde, 0xad, 0xbe, 0xef}, |
| 3338 |
}; |
| 3339 |
|
| 3340 |
for (const auto &test_data : kTestValues) { |
| 3341 |
SCOPED_TRACE(Bytes(test_data)); |
| 3342 |
|
| 3343 |
// Create an ASN1_INTEGER with the test data |
| 3344 |
bssl::UniquePtr<ASN1_INTEGER> orig(ASN1_INTEGER_new()); |
| 3345 |
ASSERT_TRUE(orig); |
| 3346 |
ASSERT_TRUE(ASN1_STRING_set(orig.get(), test_data.data(), test_data.size())); |
| 3347 |
|
| 3348 |
// Serialize to hex via i2a_ASN1_INTEGER |
| 3349 |
bssl::UniquePtr<BIO> out_bio(BIO_new(BIO_s_mem())); |
| 3350 |
ASSERT_TRUE(out_bio); |
| 3351 |
int written = i2a_ASN1_INTEGER(out_bio.get(), orig.get()); |
| 3352 |
ASSERT_GT(written, 0); |
| 3353 |
|
| 3354 |
// Get the hex string |
| 3355 |
const uint8_t *hex_data = nullptr; |
| 3356 |
size_t hex_len = 0; |
| 3357 |
ASSERT_TRUE(BIO_mem_contents(out_bio.get(), &hex_data, &hex_len)); |
| 3358 |
|
| 3359 |
// Parse it back via a2i_ASN1_INTEGER |
| 3360 |
bssl::UniquePtr<BIO> in_bio( |
| 3361 |
BIO_new_mem_buf(hex_data, static_cast<int>(hex_len))); |
| 3362 |
ASSERT_TRUE(in_bio); |
| 3363 |
|
| 3364 |
bssl::UniquePtr<ASN1_INTEGER> parsed(ASN1_INTEGER_new()); |
| 3365 |
ASSERT_TRUE(parsed); |
| 3366 |
|
| 3367 |
char buf[256]; |
| 3368 |
int ret = a2i_ASN1_INTEGER(in_bio.get(), parsed.get(), buf, sizeof(buf)); |
| 3369 |
ASSERT_EQ(ret, 1); |
| 3370 |
|
| 3371 |
// The parsed value should match the original |
| 3372 |
EXPECT_EQ(Bytes(orig->data, orig->length), |
| 3373 |
Bytes(parsed->data, parsed->length)); |
| 3374 |
} |
| 3375 |
} |
| 3376 |
|
| 3377 |
TEST(ASN1Test, I2aA2iASN1IntegerRoundTripLong) { |
| 3378 |
// Test round-trip with a value long enough to trigger line continuation. |
| 3379 |
// i2a_ASN1_INTEGER adds "\\\n" every 35 bytes. |
| 3380 |
// We need > 35 bytes to trigger continuation. |
| 3381 |
|
| 3382 |
// Create a 40-byte value |
| 3383 |
std::vector<uint8_t> long_value(40); |
| 3384 |
for (size_t i = 0; i < long_value.size(); i++) { |
| 3385 |
long_value[i] = static_cast<uint8_t>(i + 1); |
| 3386 |
} |
| 3387 |
|
| 3388 |
bssl::UniquePtr<ASN1_INTEGER> orig(ASN1_INTEGER_new()); |
| 3389 |
ASSERT_TRUE(orig); |
| 3390 |
ASSERT_TRUE( |
| 3391 |
ASN1_STRING_set(orig.get(), long_value.data(), long_value.size())); |
| 3392 |
|
| 3393 |
// Serialize to hex via i2a_ASN1_INTEGER |
| 3394 |
bssl::UniquePtr<BIO> out_bio(BIO_new(BIO_s_mem())); |
| 3395 |
ASSERT_TRUE(out_bio); |
| 3396 |
int written = i2a_ASN1_INTEGER(out_bio.get(), orig.get()); |
| 3397 |
ASSERT_GT(written, 0); |
| 3398 |
|
| 3399 |
// Get the hex string (should contain "\\\n" continuation) |
| 3400 |
const uint8_t *hex_data = nullptr; |
| 3401 |
size_t hex_len = 0; |
| 3402 |
ASSERT_TRUE(BIO_mem_contents(out_bio.get(), &hex_data, &hex_len)); |
| 3403 |
|
| 3404 |
// Verify that continuation is present |
| 3405 |
std::string hex_str(reinterpret_cast<const char *>(hex_data), hex_len); |
| 3406 |
EXPECT_NE(hex_str.find("\\\n"), std::string::npos) |
| 3407 |
<< "Expected line continuation in output: " << hex_str; |
| 3408 |
|
| 3409 |
// Parse it back via a2i_ASN1_INTEGER |
| 3410 |
bssl::UniquePtr<BIO> in_bio( |
| 3411 |
BIO_new_mem_buf(hex_data, static_cast<int>(hex_len))); |
| 3412 |
ASSERT_TRUE(in_bio); |
| 3413 |
|
| 3414 |
bssl::UniquePtr<ASN1_INTEGER> parsed(ASN1_INTEGER_new()); |
| 3415 |
ASSERT_TRUE(parsed); |
| 3416 |
|
| 3417 |
char buf[256]; |
| 3418 |
int ret = a2i_ASN1_INTEGER(in_bio.get(), parsed.get(), buf, sizeof(buf)); |
| 3419 |
ASSERT_EQ(ret, 1); |
| 3420 |
|
| 3421 |
// The parsed value should match the original |
| 3422 |
EXPECT_EQ(Bytes(orig->data, orig->length), |
| 3423 |
Bytes(parsed->data, parsed->length)); |
| 3424 |
} |
| 3425 |
|
| 3426 |
#endif // !WINDOWS || !SHARED_LIBRARY |