[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / quic / crypto / crypto_utils.h
blob725a863826136986a28d2fd1c87f2fbb1607e202
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 // Some helpers for quic crypto
7 #ifndef NET_QUIC_CRYPTO_CRYPTO_UTILS_H_
8 #define NET_QUIC_CRYPTO_CRYPTO_UTILS_H_
10 #include <string>
12 #include "base/strings/string_piece.h"
13 #include "net/base/net_export.h"
14 #include "net/quic/crypto/crypto_handshake.h"
15 #include "net/quic/crypto/crypto_protocol.h"
16 #include "net/quic/quic_protocol.h"
17 #include "net/quic/quic_time.h"
19 namespace net {
21 class QuicTime;
22 class QuicRandom;
23 struct QuicCryptoNegotiatedParameters;
25 class NET_EXPORT_PRIVATE CryptoUtils {
26 public:
27 // Generates the connection nonce. The nonce is formed as:
28 // <4 bytes> current time
29 // <8 bytes> |orbit| (or random if |orbit| is empty)
30 // <20 bytes> random
31 static void GenerateNonce(QuicWallTime now,
32 QuicRandom* random_generator,
33 base::StringPiece orbit,
34 std::string* nonce);
36 // Returns true if the sni is valid, false otherwise.
37 // (1) disallow IP addresses;
38 // (2) check that the hostname contains valid characters only; and
39 // (3) contains at least one dot.
40 static bool IsValidSNI(base::StringPiece sni);
42 // Convert hostname to lowercase and remove the trailing '.'.
43 // Returns |hostname|. NormalizeHostname() doesn't support IP address
44 // literals. IsValidSNI() should be called before calling NormalizeHostname().
45 static std::string NormalizeHostname(const char* hostname);
47 // DeriveKeys populates |crypters->encrypter|, |crypters->decrypter|, and
48 // |subkey_secret| (optional -- may be null) given the contents of
49 // |premaster_secret|, |client_nonce|, |server_nonce| and |hkdf_input|. |aead|
50 // determines which cipher will be used. |perspective| controls whether the
51 // server's keys are assigned to |encrypter| or |decrypter|. |server_nonce| is
52 // optional and, if non-empty, is mixed into the key derivation.
53 // |subkey_secret| will have the same length as |premaster_secret|.
54 static bool DeriveKeys(base::StringPiece premaster_secret,
55 QuicTag aead,
56 base::StringPiece client_nonce,
57 base::StringPiece server_nonce,
58 const std::string& hkdf_input,
59 Perspective perspective,
60 CrypterPair* crypters,
61 std::string* subkey_secret);
63 // Performs key extraction to derive a new secret of |result_len| bytes
64 // dependent on |subkey_secret|, |label|, and |context|. Returns false if the
65 // parameters are invalid (e.g. |label| contains null bytes); returns true on
66 // success.
67 static bool ExportKeyingMaterial(base::StringPiece subkey_secret,
68 base::StringPiece label,
69 base::StringPiece context,
70 size_t result_len,
71 std::string* result);
73 private:
74 DISALLOW_COPY_AND_ASSIGN(CryptoUtils);
77 } // namespace net
79 #endif // NET_QUIC_CRYPTO_CRYPTO_UTILS_H_