OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/quic/crypto/crypto_utils.h" |
| 6 |
| 7 #include "base/string_piece.h" |
| 8 #include "net/quic/crypto/crypto_protocol.h" |
| 9 #include "net/quic/crypto/quic_random.h" |
| 10 #include "net/quic/quic_clock.h" |
| 11 |
| 12 using base::StringPiece; |
| 13 using std::string; |
| 14 |
| 15 namespace net { |
| 16 |
| 17 void CryptoUtils::GenerateNonce(const QuicClock* clock, |
| 18 QuicRandom* random_generator, |
| 19 string* nonce) { |
| 20 // a 4-byte timestamp + 28 random bytes. |
| 21 nonce->reserve(kNonceSize); |
| 22 nonce->resize(kNonceSize); |
| 23 QuicTime::Delta now = clock->NowAsDeltaSinceUnixEpoch(); |
| 24 uint32 gmt_unix_time = now.ToSeconds(); |
| 25 const size_t time_size = sizeof(gmt_unix_time); |
| 26 memcpy(&(*nonce)[0], &gmt_unix_time, time_size); |
| 27 random_generator->RandBytes(&(*nonce)[time_size], kNonceSize - time_size); |
| 28 } |
| 29 |
| 30 void CryptoUtils::FillClientHelloMessage(const QuicClientCryptoConfig& config, |
| 31 const string& nonce, |
| 32 CryptoHandshakeMessage* message) { |
| 33 message->tag = kCHLO; |
| 34 |
| 35 StringPiece value; |
| 36 |
| 37 // Version. |
| 38 value.set(&config.version, sizeof(config.version)); |
| 39 message->tag_value_map[kVERS] = value.as_string(); |
| 40 |
| 41 // Key exchange methods. |
| 42 value.set(&config.key_exchange[0], |
| 43 config.key_exchange.size() * sizeof(config.key_exchange[0])); |
| 44 message->tag_value_map[kKEXS] = value.as_string(); |
| 45 |
| 46 // Authenticated encryption algorithms. |
| 47 value.set(&config.aead[0], config.aead.size() * sizeof(config.aead[0])); |
| 48 message->tag_value_map[kAEAD] = value.as_string(); |
| 49 |
| 50 // Congestion control feedback types. |
| 51 value.set(&config.congestion_control[0], |
| 52 config.congestion_control.size() * |
| 53 sizeof(config.congestion_control[0])); |
| 54 message->tag_value_map[kCGST] = value.as_string(); |
| 55 |
| 56 // Idle connection state lifetime. |
| 57 uint32 idle_connection_state_lifetime_secs = |
| 58 config.idle_connection_state_lifetime.ToSeconds(); |
| 59 value.set(&idle_connection_state_lifetime_secs, |
| 60 sizeof(idle_connection_state_lifetime_secs)); |
| 61 message->tag_value_map[kICSL] = value.as_string(); |
| 62 |
| 63 // Keepalive timeout. |
| 64 uint32 keepalive_timeout_secs = config.keepalive_timeout.ToSeconds(); |
| 65 value.set(&keepalive_timeout_secs, sizeof(keepalive_timeout_secs)); |
| 66 message->tag_value_map[kKATO] = value.as_string(); |
| 67 |
| 68 // Connection nonce. |
| 69 message->tag_value_map[kNONC] = nonce; |
| 70 |
| 71 // Server name indication. |
| 72 // TODO(wtc): if server_hostname_ is a DNS name, store it in |
| 73 // message->tag_value_map[kSNI]. |
| 74 } |
| 75 |
| 76 } // namespace net |
OLD | NEW |