OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/test_tools/crypto_test_utils.h" | 5 #include "net/quic/test_tools/crypto_test_utils.h" |
6 | 6 |
7 #include <keyhi.h> | 7 #include <keyhi.h> |
8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
9 #include <sechash.h> | 9 #include <sechash.h> |
10 | 10 |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "crypto/ec_private_key.h" | 13 #include "crypto/ec_private_key.h" |
14 #include "net/quic/crypto/channel_id.h" | 14 #include "net/quic/crypto/channel_id.h" |
15 | 15 |
16 using base::StringPiece; | 16 using base::StringPiece; |
17 using std::string; | 17 using std::string; |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 namespace test { | 21 namespace test { |
22 | 22 |
23 // TODO(rtenneti): Implement NSS support ChannelIDSigner. Convert Sign() to be | 23 // TODO(rtenneti): Implement NSS support ChannelIDKey. Convert Sign() to be |
24 // asynchronous using completion callback. After porting TestChannelIDSigner, | 24 // asynchronous using completion callback. After porting TestChannelIDKey, |
25 // implement real ChannelIDSigner. | 25 // implement real ChannelIDKey. |
wtc
2014/05/29 21:59:14
Nit: This comment is difficult to understand. I su
| |
26 class TestChannelIDSigner : public ChannelIDSigner { | 26 class TestChannelIDKey : public ChannelIDKey { |
27 public: | 27 public: |
28 virtual ~TestChannelIDSigner() { | 28 explicit TestChannelIDKey(crypto::ECPrivateKey* ecdsa_keypair) |
29 STLDeleteValues(&hostname_to_key_); | 29 : ecdsa_keypair_(ecdsa_keypair) {} |
30 } | 30 virtual ~TestChannelIDKey() {} |
31 | 31 |
32 // ChannelIDSigner implementation. | 32 // ChannelIDKey implementation. |
33 | 33 |
34 virtual bool Sign(const string& hostname, | 34 virtual bool Sign(StringPiece signed_data, |
35 StringPiece signed_data, | |
36 string* out_key, | |
37 string* out_signature) OVERRIDE { | 35 string* out_signature) OVERRIDE { |
38 crypto::ECPrivateKey* ecdsa_keypair = HostnameToKey(hostname); | |
39 if (!ecdsa_keypair) { | |
40 return false; | |
41 } | |
42 | |
43 *out_key = SerializeKey(ecdsa_keypair->public_key()); | |
44 if (out_key->empty()) { | |
45 return false; | |
46 } | |
47 | |
48 unsigned char hash_buf[SHA256_LENGTH]; | 36 unsigned char hash_buf[SHA256_LENGTH]; |
49 SECItem hash_item = { siBuffer, hash_buf, sizeof(hash_buf) }; | 37 SECItem hash_item = { siBuffer, hash_buf, sizeof(hash_buf) }; |
50 | 38 |
51 HASHContext* sha256 = HASH_Create(HASH_AlgSHA256); | 39 HASHContext* sha256 = HASH_Create(HASH_AlgSHA256); |
52 if (!sha256) { | 40 if (!sha256) { |
53 return false; | 41 return false; |
54 } | 42 } |
55 HASH_Begin(sha256); | 43 HASH_Begin(sha256); |
56 HASH_Update(sha256, | 44 HASH_Update(sha256, |
57 reinterpret_cast<const unsigned char*>( | 45 reinterpret_cast<const unsigned char*>( |
(...skipping 12 matching lines...) Expand all Loading... | |
70 // The signature consists of a pair of 32-byte numbers. | 58 // The signature consists of a pair of 32-byte numbers. |
71 static const unsigned int kSignatureLength = 32 * 2; | 59 static const unsigned int kSignatureLength = 32 * 2; |
72 string signature; | 60 string signature; |
73 SECItem sig_item = { | 61 SECItem sig_item = { |
74 siBuffer, | 62 siBuffer, |
75 reinterpret_cast<unsigned char*>( | 63 reinterpret_cast<unsigned char*>( |
76 WriteInto(&signature, kSignatureLength + 1)), | 64 WriteInto(&signature, kSignatureLength + 1)), |
77 kSignatureLength | 65 kSignatureLength |
78 }; | 66 }; |
79 | 67 |
80 if (PK11_Sign(ecdsa_keypair->key(), &sig_item, &hash_item) != SECSuccess) { | 68 if (PK11_Sign(ecdsa_keypair_->key(), &sig_item, &hash_item) != SECSuccess) { |
81 return false; | 69 return false; |
82 } | 70 } |
83 *out_signature = signature; | 71 *out_signature = signature; |
84 return true; | 72 return true; |
85 } | 73 } |
86 | 74 |
87 virtual string GetKeyForHostname(const string& hostname) OVERRIDE { | 75 virtual string SerializeKey() OVERRIDE { |
88 crypto::ECPrivateKey* ecdsa_keypair = HostnameToKey(hostname); | 76 static const unsigned int kExpectedKeyLength = 65; |
89 if (!ecdsa_keypair) { | 77 |
78 // public_key->u.ec.publicValue is an ANSI X9.62 public key which, for | |
79 // a P-256 key, is 0x04 (meaning uncompressed) followed by the x and y field | |
80 // elements as 32-byte, big-endian numbers. | |
81 const SECKEYPublicKey* public_key = ecdsa_keypair_->public_key(); | |
wtc
2014/05/29 21:59:14
Nit: since the comment block refers to "public_key
| |
82 | |
83 const unsigned char* const data = public_key->u.ec.publicValue.data; | |
84 const unsigned int len = public_key->u.ec.publicValue.len; | |
85 if (len != kExpectedKeyLength || data[0] != 0x04) { | |
90 return ""; | 86 return ""; |
91 } | 87 } |
92 return SerializeKey(ecdsa_keypair->public_key()); | 88 |
89 string key(reinterpret_cast<const char*>(data + 1), kExpectedKeyLength - 1); | |
90 return key; | |
93 } | 91 } |
94 | 92 |
95 private: | 93 private: |
94 crypto::ECPrivateKey* ecdsa_keypair_; | |
95 }; | |
96 | |
97 class TestChannelIDSource : public ChannelIDSource { | |
98 public: | |
99 virtual ~TestChannelIDSource() { | |
100 STLDeleteValues(&hostname_to_key_); | |
101 } | |
102 | |
103 // ChannelIDSource implementation. | |
104 | |
105 virtual bool GetChannelIDKey( | |
106 const string& hostname, | |
107 scoped_ptr<ChannelIDKey>* channel_id_key) OVERRIDE { | |
108 channel_id_key->reset(new TestChannelIDKey(HostnameToKey(hostname))); | |
109 return true; | |
110 } | |
111 | |
112 private: | |
96 typedef std::map<string, crypto::ECPrivateKey*> HostnameToKeyMap; | 113 typedef std::map<string, crypto::ECPrivateKey*> HostnameToKeyMap; |
97 | 114 |
98 crypto::ECPrivateKey* HostnameToKey(const string& hostname) { | 115 crypto::ECPrivateKey* HostnameToKey(const string& hostname) { |
99 HostnameToKeyMap::const_iterator it = hostname_to_key_.find(hostname); | 116 HostnameToKeyMap::const_iterator it = hostname_to_key_.find(hostname); |
100 if (it != hostname_to_key_.end()) { | 117 if (it != hostname_to_key_.end()) { |
101 return it->second; | 118 return it->second; |
102 } | 119 } |
103 | 120 |
104 crypto::ECPrivateKey* keypair = crypto::ECPrivateKey::Create(); | 121 crypto::ECPrivateKey* keypair = crypto::ECPrivateKey::Create(); |
105 if (!keypair) { | 122 if (!keypair) { |
106 return NULL; | 123 return NULL; |
107 } | 124 } |
108 hostname_to_key_[hostname] = keypair; | 125 hostname_to_key_[hostname] = keypair; |
109 return keypair; | 126 return keypair; |
110 } | 127 } |
111 | 128 |
112 static string SerializeKey(const SECKEYPublicKey* public_key) { | |
113 // public_key->u.ec.publicValue is an ANSI X9.62 public key which, for | |
114 // a P-256 key, is 0x04 (meaning uncompressed) followed by the x and y field | |
115 // elements as 32-byte, big-endian numbers. | |
116 static const unsigned int kExpectedKeyLength = 65; | |
117 | |
118 const unsigned char* const data = public_key->u.ec.publicValue.data; | |
119 const unsigned int len = public_key->u.ec.publicValue.len; | |
120 if (len != kExpectedKeyLength || data[0] != 0x04) { | |
121 return ""; | |
122 } | |
123 | |
124 string key(reinterpret_cast<const char*>(data + 1), kExpectedKeyLength - 1); | |
125 return key; | |
126 } | |
127 | 129 |
128 HostnameToKeyMap hostname_to_key_; | 130 HostnameToKeyMap hostname_to_key_; |
129 }; | 131 }; |
130 | 132 |
131 // static | 133 // static |
132 ChannelIDSigner* CryptoTestUtils::ChannelIDSignerForTesting() { | 134 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() { |
133 return new TestChannelIDSigner(); | 135 return new TestChannelIDSource(); |
134 } | 136 } |
135 | 137 |
136 } // namespace test | 138 } // namespace test |
137 | 139 |
138 } // namespace net | 140 } // namespace net |
OLD | NEW |