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