This source file includes following definitions.
- GenerateNonce
- IsValidSNI
- NormalizeHostname
- DeriveKeys
#include "net/quic/crypto/crypto_utils.h"
#include "crypto/hkdf.h"
#include "net/base/net_util.h"
#include "net/quic/crypto/crypto_handshake.h"
#include "net/quic/crypto/crypto_protocol.h"
#include "net/quic/crypto/quic_decrypter.h"
#include "net/quic/crypto/quic_encrypter.h"
#include "net/quic/crypto/quic_random.h"
#include "net/quic/quic_time.h"
#include "url/url_canon.h"
using base::StringPiece;
using std::string;
namespace net {
void CryptoUtils::GenerateNonce(QuicWallTime now,
QuicRandom* random_generator,
StringPiece orbit,
string* nonce) {
nonce->reserve(kNonceSize);
nonce->resize(kNonceSize);
uint32 gmt_unix_time = now.ToUNIXSeconds();
(*nonce)[0] = static_cast<char>(gmt_unix_time >> 24);
(*nonce)[1] = static_cast<char>(gmt_unix_time >> 16);
(*nonce)[2] = static_cast<char>(gmt_unix_time >> 8);
(*nonce)[3] = static_cast<char>(gmt_unix_time);
size_t bytes_written = sizeof(gmt_unix_time);
if (orbit.size() == 8) {
memcpy(&(*nonce)[bytes_written], orbit.data(), orbit.size());
bytes_written += orbit.size();
}
random_generator->RandBytes(&(*nonce)[bytes_written],
kNonceSize - bytes_written);
}
bool CryptoUtils::IsValidSNI(StringPiece sni) {
url_canon::CanonHostInfo host_info;
string canonicalized_host(CanonicalizeHost(sni.as_string(), &host_info));
return !host_info.IsIPAddress() &&
IsCanonicalizedHostCompliant(canonicalized_host, std::string()) &&
sni.find_last_of('.') != string::npos;
}
string CryptoUtils::NormalizeHostname(const char* hostname) {
url_canon::CanonHostInfo host_info;
string host(CanonicalizeHost(hostname, &host_info));
size_t host_end = host.length();
while (host_end != 0 && host[host_end - 1] == '.') {
host_end--;
}
if (host_end != host.length()) {
host.erase(host_end, host.length() - host_end);
}
return host;
}
bool CryptoUtils::DeriveKeys(StringPiece premaster_secret,
QuicTag aead,
StringPiece client_nonce,
StringPiece server_nonce,
const string& hkdf_input,
Perspective perspective,
CrypterPair* out) {
out->encrypter.reset(QuicEncrypter::Create(aead));
out->decrypter.reset(QuicDecrypter::Create(aead));
size_t key_bytes = out->encrypter->GetKeySize();
size_t nonce_prefix_bytes = out->encrypter->GetNoncePrefixSize();
StringPiece nonce = client_nonce;
string nonce_storage;
if (!server_nonce.empty()) {
nonce_storage = client_nonce.as_string() + server_nonce.as_string();
nonce = nonce_storage;
}
crypto::HKDF hkdf(premaster_secret, nonce, hkdf_input, key_bytes,
nonce_prefix_bytes);
if (perspective == SERVER) {
if (!out->encrypter->SetKey(hkdf.server_write_key()) ||
!out->encrypter->SetNoncePrefix(hkdf.server_write_iv()) ||
!out->decrypter->SetKey(hkdf.client_write_key()) ||
!out->decrypter->SetNoncePrefix(hkdf.client_write_iv())) {
return false;
}
} else {
if (!out->encrypter->SetKey(hkdf.client_write_key()) ||
!out->encrypter->SetNoncePrefix(hkdf.client_write_iv()) ||
!out->decrypter->SetKey(hkdf.server_write_key()) ||
!out->decrypter->SetNoncePrefix(hkdf.server_write_iv())) {
return false;
}
}
return true;
}
}