OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/crypto/crypto_utils.h" | 5 #include "net/quic/crypto/crypto_utils.h" |
6 | 6 |
7 #include "base/string_piece.h" | 7 #include "base/string_piece.h" |
8 #include "net/base/net_util.h" | |
9 #include "net/quic/crypto/crypto_protocol.h" | 8 #include "net/quic/crypto/crypto_protocol.h" |
10 #include "net/quic/crypto/quic_random.h" | 9 #include "net/quic/crypto/quic_random.h" |
11 #include "net/quic/quic_clock.h" | 10 #include "net/quic/quic_clock.h" |
12 | 11 |
13 using base::StringPiece; | 12 using base::StringPiece; |
14 using std::string; | 13 using std::string; |
15 | 14 |
16 namespace net { | 15 namespace net { |
17 | 16 |
| 17 // static |
| 18 bool CryptoUtils::FindMutualTag(const CryptoTagVector& preference, |
| 19 const CryptoTagVector& supported, |
| 20 CryptoTag* out_result) { |
| 21 for (CryptoTagVector::const_iterator i = preference.begin(); |
| 22 i != preference.end(); i++) { |
| 23 for (CryptoTagVector::const_iterator j = supported.begin(); |
| 24 j != supported.end(); j++) { |
| 25 if (*i == *j) { |
| 26 *out_result = *i; |
| 27 return true; |
| 28 } |
| 29 } |
| 30 } |
| 31 |
| 32 return false; |
| 33 } |
| 34 |
18 void CryptoUtils::GenerateNonce(const QuicClock* clock, | 35 void CryptoUtils::GenerateNonce(const QuicClock* clock, |
19 QuicRandom* random_generator, | 36 QuicRandom* random_generator, |
20 string* nonce) { | 37 string* nonce) { |
21 // a 4-byte timestamp + 28 random bytes. | 38 // a 4-byte timestamp + 28 random bytes. |
22 nonce->reserve(kNonceSize); | 39 nonce->reserve(kNonceSize); |
23 nonce->resize(kNonceSize); | 40 nonce->resize(kNonceSize); |
24 QuicTime::Delta now = clock->NowAsDeltaSinceUnixEpoch(); | 41 QuicTime::Delta now = clock->NowAsDeltaSinceUnixEpoch(); |
25 uint32 gmt_unix_time = now.ToSeconds(); | 42 uint32 gmt_unix_time = now.ToSeconds(); |
26 const size_t time_size = sizeof(gmt_unix_time); | 43 const size_t time_size = sizeof(gmt_unix_time); |
27 memcpy(&(*nonce)[0], &gmt_unix_time, time_size); | 44 memcpy(&(*nonce)[0], &gmt_unix_time, time_size); |
28 random_generator->RandBytes(&(*nonce)[time_size], kNonceSize - time_size); | 45 random_generator->RandBytes(&(*nonce)[time_size], kNonceSize - time_size); |
29 } | 46 } |
30 | 47 |
31 void CryptoUtils::FillClientHelloMessage( | |
32 const QuicCryptoConfig& client_config, | |
33 const string& nonce, | |
34 const string& server_hostname, | |
35 CryptoHandshakeMessage* message) { | |
36 message->tag = kCHLO; | |
37 | |
38 // Version. | |
39 message->tag_value_map[kVERS] = EncodeSingleValue(client_config.version); | |
40 | |
41 // Key exchange methods. | |
42 message->tag_value_map[kKEXS] = EncodeVectorValue(client_config.key_exchange); | |
43 | |
44 // Authenticated encryption algorithms. | |
45 message->tag_value_map[kAEAD] = EncodeVectorValue(client_config.aead); | |
46 | |
47 // Congestion control feedback types. | |
48 message->tag_value_map[kCGST] = | |
49 EncodeVectorValue(client_config.congestion_control); | |
50 | |
51 // Idle connection state lifetime. | |
52 uint32 idle_connection_state_lifetime_secs = | |
53 client_config.idle_connection_state_lifetime.ToSeconds(); | |
54 message->tag_value_map[kICSL] = | |
55 EncodeSingleValue(idle_connection_state_lifetime_secs); | |
56 | |
57 // Keepalive timeout. | |
58 uint32 keepalive_timeout_secs = client_config.keepalive_timeout.ToSeconds(); | |
59 message->tag_value_map[kKATO] = EncodeSingleValue(keepalive_timeout_secs); | |
60 | |
61 // Connection nonce. | |
62 message->tag_value_map[kNONC] = nonce; | |
63 | |
64 // Server name indication. | |
65 // If server_hostname is not an IP address literal, it is a DNS hostname. | |
66 IPAddressNumber ip_number; | |
67 if (!server_hostname.empty() && | |
68 !ParseIPLiteralToNumber(server_hostname, &ip_number)) { | |
69 message->tag_value_map[kSNI] = server_hostname; | |
70 } | |
71 } | |
72 | |
73 void CryptoUtils::FillServerHelloMessage( | |
74 const QuicCryptoNegotiatedParams& negotiated_params, | |
75 const string& nonce, | |
76 CryptoHandshakeMessage* message) { | |
77 message->tag = kSHLO; | |
78 | |
79 // Version. | |
80 message->tag_value_map[kVERS] = EncodeSingleValue(negotiated_params.version); | |
81 | |
82 // Key exchange method. | |
83 message->tag_value_map[kKEXS] = | |
84 EncodeSingleValue(negotiated_params.key_exchange); | |
85 | |
86 // Authenticated encryption algorithm. | |
87 message->tag_value_map[kAEAD] = EncodeSingleValue(negotiated_params.aead); | |
88 | |
89 // Congestion control feedback type. | |
90 message->tag_value_map[kCGST] = | |
91 EncodeSingleValue(negotiated_params.congestion_control); | |
92 | |
93 // Idle connection state lifetime. | |
94 uint32 idle_connection_state_lifetime_secs = | |
95 negotiated_params.idle_connection_state_lifetime.ToSeconds(); | |
96 message->tag_value_map[kICSL] = | |
97 EncodeSingleValue(idle_connection_state_lifetime_secs); | |
98 | |
99 // Keepalive timeout? | |
100 | |
101 // Connection nonce. | |
102 message->tag_value_map[kNONC] = nonce; | |
103 } | |
104 | |
105 } // namespace net | 48 } // namespace net |
OLD | NEW |