1b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// found in the LICENSE file.
4b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
5b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/quic/crypto/common_cert_set.h"
6b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
7b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
8b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
9b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)using base::StringPiece;
10b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
11b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)namespace net {
12b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)namespace test {
13b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)static const unsigned char kGIACertificate[] = {
15b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x30, 0x82, 0x02, 0xb0, 0x30, 0x82, 0x02, 0x19, 0xa0, 0x03, 0x02, 0x01,
16b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x02, 0x02, 0x03, 0x0b, 0x67, 0x71, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
17b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x4e, 0x31,
18b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
19b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x07, 0x45,
20b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x71, 0x75, 0x69, 0x66, 0x61, 0x78, 0x31, 0x2d, 0x30, 0x2b, 0x06, 0x03,
21b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x55, 0x04, 0x0b, 0x13, 0x24, 0x45, 0x71, 0x75, 0x69, 0x66, 0x61, 0x78,
22b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x20, 0x43, 0x65, 0x72, 0x74,
23b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x20, 0x41, 0x75, 0x74, 0x68,
24b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x6f, 0x72, 0x69, 0x74, 0x79, 0x30, 0x1e, 0x17, 0x0d, 0x30, 0x39, 0x30,
25b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x36, 0x30, 0x38, 0x32, 0x30, 0x34, 0x33, 0x32, 0x37, 0x5a, 0x17, 0x0d,
26b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x31, 0x33, 0x30, 0x36, 0x30, 0x37, 0x31, 0x39, 0x34, 0x33, 0x32, 0x37,
27b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x5a, 0x30, 0x46, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06,
28b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
29b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x0a, 0x13, 0x0a, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x20, 0x49, 0x6e,
30b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x63, 0x31, 0x22, 0x30, 0x20, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x19,
31b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72,
32b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x6e, 0x65, 0x74, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74,
33b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x79, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
34b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30,
35b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xc9, 0xed, 0xb7, 0xa4, 0x8b, 0x9c,
36b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x57, 0xe7, 0x84, 0x3e, 0x40, 0x7d, 0x84, 0xf4, 0x8f, 0xd1, 0x71, 0x63,
37b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x53, 0x99, 0xe7, 0x79, 0x74, 0x14, 0xaf, 0x44, 0x99, 0x33, 0x20, 0x92,
38b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x8d, 0x7b, 0xe5, 0x28, 0x0c, 0xba, 0xad, 0x6c, 0x49, 0x7e, 0x83, 0x5f,
39b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x34, 0x59, 0x4e, 0x0a, 0x7a, 0x30, 0xcd, 0xd0, 0xd7, 0xc4, 0x57, 0x45,
40b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xed, 0xd5, 0xaa, 0xd6, 0x73, 0x26, 0xce, 0xad, 0x32, 0x13, 0xb8, 0xd7,
41b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x0f, 0x1d, 0x3b, 0xdf, 0xdd, 0xdc, 0x08, 0x36, 0xa8, 0x6f, 0x51, 0x44,
42b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x9b, 0xca, 0xd6, 0x20, 0x52, 0x73, 0xb7, 0x26, 0x87, 0x35, 0x6a, 0xdb,
43b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xa9, 0xe5, 0xd4, 0x59, 0xa5, 0x2b, 0xfc, 0x67, 0x19, 0x39, 0xfa, 0x93,
44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x18, 0x18, 0x6c, 0xde, 0xdd, 0x25, 0x8a, 0x0e, 0x33, 0x14, 0x47, 0xc2,
45b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xef, 0x01, 0x50, 0x79, 0xe4, 0xfd, 0x69, 0xd1, 0xa7, 0xc0, 0xac, 0xe2,
46b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x57, 0x6f, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x81, 0xa3, 0x30, 0x81,
47b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xa0, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04,
48b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x04, 0x03, 0x02, 0x01, 0x06, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
49b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x04, 0x16, 0x04, 0x14, 0xbf, 0xc0, 0x30, 0xeb, 0xf5, 0x43, 0x11, 0x3e,
50b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x67, 0xba, 0x9e, 0x91, 0xfb, 0xfc, 0x6a, 0xda, 0xe3, 0x6b, 0x12, 0x24,
51b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
52b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x14, 0x48, 0xe6, 0x68, 0xf9, 0x2b, 0xd2, 0xb2, 0x95, 0xd7, 0x47, 0xd8,
53b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x23, 0x20, 0x10, 0x4f, 0x33, 0x98, 0x90, 0x9f, 0xd4, 0x30, 0x12, 0x06,
54b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01,
55b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x3a, 0x06, 0x03, 0x55, 0x1d, 0x1f,
56b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x04, 0x33, 0x30, 0x31, 0x30, 0x2f, 0xa0, 0x2d, 0xa0, 0x2b, 0x86, 0x29,
57b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x63, 0x72, 0x6c, 0x2e, 0x67,
58b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x65, 0x6f, 0x74, 0x72, 0x75, 0x73, 0x74, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
59b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x63, 0x72, 0x6c, 0x73, 0x2f, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x63,
60b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x61, 0x2e, 0x63, 0x72, 0x6c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
61b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00,
62b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xb8, 0x8a, 0x23, 0xc6, 0x48, 0x96, 0xb1, 0x11, 0x7c, 0x60, 0x77, 0x5e,
63b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x05, 0x9a, 0xab, 0xa1, 0xc6, 0xfa, 0x82, 0x1c, 0x18, 0x07, 0xc4, 0xeb,
64b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x81, 0xb0, 0xa8, 0x66, 0xeb, 0x49, 0xa8, 0xe9, 0x0c, 0xd3, 0x29, 0xad,
65b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xf5, 0xef, 0x24, 0x4c, 0xfd, 0xe4, 0x4b, 0xca, 0x7f, 0x5e, 0x63, 0xab,
66b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x99, 0x27, 0xcb, 0x9f, 0x36, 0x21, 0x2c, 0xb9, 0x10, 0x60, 0x67, 0xcd,
67b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xd2, 0xb4, 0xf0, 0xf0, 0xab, 0x71, 0xe5, 0x8b, 0x5a, 0x89, 0x27, 0x11,
68b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x84, 0xaa, 0x8e, 0xbf, 0x99, 0xf0, 0x9d, 0x09, 0x21, 0x0a, 0x52, 0x19,
69b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x9a, 0x5a, 0x09, 0xd2, 0x90, 0xb7, 0xfa, 0x0c, 0xf8, 0x7e, 0x78, 0xa2,
70b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0xb0, 0x85, 0xaf, 0x5c, 0x4c, 0x99, 0xd9, 0x5c, 0x55, 0x29, 0xf9, 0xa5,
71b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x51, 0x42, 0x2e, 0x3a, 0xcb, 0x38, 0x8c, 0x78, 0x3b, 0xcb, 0xf8, 0xfb,
72b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  0x95, 0x87, 0xbc, 0xbc, 0x90, 0xf9, 0x50, 0x32,
73b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)};
74b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
75b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)TEST(CommonCertSets, FindGIA) {
76b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  StringPiece gia(reinterpret_cast<const char*>(kGIACertificate),
77b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                  sizeof(kGIACertificate));
78b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const CommonCertSets* sets(CommonCertSets::GetInstanceQUIC());
80b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
81558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  const uint64 in_hash = GG_UINT64_C(0xc9fef74053f99f39);
82b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  uint64 hash;
83b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  uint32 index;
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(sets->MatchCert(
85b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      gia,
86b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      StringPiece(reinterpret_cast<const char*>(&in_hash), sizeof(in_hash)),
87b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      &hash, &index));
88b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_EQ(in_hash, hash);
89b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StringPiece gia_copy = sets->GetCert(hash, index);
91b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_FALSE(gia_copy.empty());
92b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  ASSERT_EQ(gia.size(), gia_copy.size());
9368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(0, memcmp(gia.data(), gia_copy.data(), gia.size()));
94b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
95b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
96b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)TEST(CommonCertSets, NonMatch) {
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const CommonCertSets* sets(CommonCertSets::GetInstanceQUIC());
98b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  StringPiece not_a_cert("hello");
99558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  const uint64 in_hash = GG_UINT64_C(0xc9fef74053f99f39);
100b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  uint64 hash;
101b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  uint32 index;
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(sets->MatchCert(
103b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      not_a_cert,
104b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      StringPiece(reinterpret_cast<const char*>(&in_hash), sizeof(in_hash)),
105b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      &hash, &index));
106b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
107b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
108b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}  // namespace test
109b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}  // namespace net
110