15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string_piece.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/base/net_export.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/quic_protocol.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace net {
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// An intermediate format of a handshake message that's convenient for a
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// CryptoFramer to serialize from or parse into.
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class NET_EXPORT_PRIVATE CryptoHandshakeMessage {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CryptoHandshakeMessage();
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CryptoHandshakeMessage(const CryptoHandshakeMessage& other);
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~CryptoHandshakeMessage();
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CryptoHandshakeMessage& operator=(const CryptoHandshakeMessage& other);
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Clears state.
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void Clear();
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // GetSerialized returns the serialized form of this message and caches the
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // result. Subsequently altering the message does not invalidate the cache.
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const QuicData& GetSerialized() const;
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // MarkDirty invalidates the cache created by |GetSerialized|.
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void MarkDirty();
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // SetValue sets an element with the given tag to the raw, memory contents of
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |v|.
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  template<class T> void SetValue(QuicTag tag, const T& v) {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    tag_value_map_[tag] =
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        std::string(reinterpret_cast<const char*>(&v), sizeof(v));
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // SetVector sets an element with the given tag to the raw contents of an
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // array of elements in |v|.
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  template<class T> void SetVector(QuicTag tag, const std::vector<T>& v) {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (v.empty()) {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      tag_value_map_[tag] = std::string();
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      tag_value_map_[tag] = std::string(reinterpret_cast<const char*>(&v[0]),
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                        v.size() * sizeof(T));
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the message tag.
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicTag tag() const { return tag_; }
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets the message tag.
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void set_tag(QuicTag tag) { tag_ = tag; }
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const QuicTagValueMap& tag_value_map() const { return tag_value_map_; }
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // SetTaglist sets an element with the given tag to contain a list of tags,
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // passed as varargs. The argument list must be terminated with a 0 element.
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetTaglist(QuicTag tag, ...);
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetStringPiece(QuicTag tag, base::StringPiece value);
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Erase removes a tag/value, if present, from the message.
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void Erase(QuicTag tag);
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // GetTaglist finds an element with the given tag containing zero or more
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // tags. If such a tag doesn't exist, it returns false. Otherwise it sets
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |out_tags| and |out_len| to point to the array of tags and returns true.
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The array points into the CryptoHandshakeMessage and is valid only for as
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // long as the CryptoHandshakeMessage exists and is not modified.
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicErrorCode GetTaglist(QuicTag tag, const QuicTag** out_tags,
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                           size_t* out_len) const;
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool GetStringPiece(QuicTag tag, base::StringPiece* out) const;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // GetNthValue24 interprets the value with the given tag to be a series of
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // 24-bit, length prefixed values and it returns the subvalue with the given
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // index.
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicErrorCode GetNthValue24(QuicTag tag,
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              unsigned index,
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              base::StringPiece* out) const;
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicErrorCode GetUint16(QuicTag tag, uint16* out) const;
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicErrorCode GetUint32(QuicTag tag, uint32* out) const;
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicErrorCode GetUint64(QuicTag tag, uint64* out) const;
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // size returns 4 (message tag) + 2 (uint16, number of entries) +
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // (4 (tag) + 4 (end offset))*tag_value_map_.size() + ∑ value sizes.
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t size() const;
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // set_minimum_size sets the minimum number of bytes that the message should
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // consume. The CryptoFramer will add a PAD tag as needed when serializing in
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // order to ensure this. Setting a value of 0 disables padding.
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Padding is useful in order to ensure that messages are a minimum size. A
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // QUIC server can require a minimum size in order to reduce the
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // amplification factor of any mirror DoS attack.
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void set_minimum_size(size_t min_bytes);
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t minimum_size() const;
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // DebugString returns a multi-line, string representation of the message
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // suitable for including in debug output.
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string DebugString() const;
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // GetPOD is a utility function for extracting a plain-old-data value. If
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |tag| exists in the message, and has a value of exactly |len| bytes then
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // it copies |len| bytes of data into |out|. Otherwise |len| bytes at |out|
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // are zeroed out.
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If used to copy integers then this assumes that the machine is
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // little-endian.
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicErrorCode GetPOD(QuicTag tag, void* out, size_t len) const;
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string DebugStringInternal(size_t indent) const;
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicTag tag_;
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicTagValueMap tag_value_map_;
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t minimum_size_;
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The serialized form of the handshake message. This member is constructed
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // lasily.
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  mutable scoped_ptr<QuicData> serialized_;
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace net
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_MESSAGE_H_
136