Home | History | Annotate | Download | only in quic
      1 // Copyright (c) 2012 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/quic_utils.h"
      6 
      7 #include <ctype.h>
      8 
      9 #include <algorithm>
     10 
     11 #include "base/logging.h"
     12 #include "base/port.h"
     13 #include "base/strings/stringprintf.h"
     14 #include "base/strings/string_number_conversions.h"
     15 #include "net/spdy/write_blocked_list.h"
     16 
     17 using base::StringPiece;
     18 using std::string;
     19 
     20 namespace net {
     21 
     22 // static
     23 uint64 QuicUtils::FNV1a_64_Hash(const char* data, int len) {
     24   static const uint64 kOffset = GG_UINT64_C(14695981039346656037);
     25   static const uint64 kPrime = GG_UINT64_C(1099511628211);
     26 
     27   const uint8* octets = reinterpret_cast<const uint8*>(data);
     28 
     29   uint64 hash = kOffset;
     30 
     31   for (int i = 0; i < len; ++i) {
     32     hash = hash ^ octets[i];
     33     hash = hash * kPrime;
     34   }
     35 
     36   return hash;
     37 }
     38 
     39 // static
     40 uint128 QuicUtils::FNV1a_128_Hash(const char* data, int len) {
     41   // The following two constants are defined as part of the hash algorithm.
     42   // see http://www.isthe.com/chongo/tech/comp/fnv/
     43   // 309485009821345068724781371
     44   const uint128 kPrime(16777216, 315);
     45   // 144066263297769815596495629667062367629
     46   const uint128 kOffset(GG_UINT64_C(7809847782465536322),
     47                         GG_UINT64_C(7113472399480571277));
     48 
     49   const uint8* octets = reinterpret_cast<const uint8*>(data);
     50 
     51   uint128 hash = kOffset;
     52 
     53   for (int i = 0; i < len; ++i) {
     54     hash  = hash ^ uint128(0, octets[i]);
     55     hash = hash * kPrime;
     56   }
     57 
     58   return hash;
     59 }
     60 
     61 // static
     62 bool QuicUtils::FindMutualTag(const QuicTagVector& our_tags_vector,
     63                               const QuicTag* their_tags,
     64                               size_t num_their_tags,
     65                               Priority priority,
     66                               QuicTag* out_result,
     67                               size_t* out_index) {
     68   if (our_tags_vector.empty()) {
     69     return false;
     70   }
     71   const size_t num_our_tags = our_tags_vector.size();
     72   const QuicTag* our_tags = &our_tags_vector[0];
     73 
     74   size_t num_priority_tags, num_inferior_tags;
     75   const QuicTag* priority_tags;
     76   const QuicTag* inferior_tags;
     77   if (priority == LOCAL_PRIORITY) {
     78     num_priority_tags = num_our_tags;
     79     priority_tags = our_tags;
     80     num_inferior_tags = num_their_tags;
     81     inferior_tags = their_tags;
     82   } else {
     83     num_priority_tags = num_their_tags;
     84     priority_tags = their_tags;
     85     num_inferior_tags = num_our_tags;
     86     inferior_tags = our_tags;
     87   }
     88 
     89   for (size_t i = 0; i < num_priority_tags; i++) {
     90     for (size_t j = 0; j < num_inferior_tags; j++) {
     91       if (priority_tags[i] == inferior_tags[j]) {
     92         *out_result = priority_tags[i];
     93         if (out_index) {
     94           if (priority == LOCAL_PRIORITY) {
     95             *out_index = j;
     96           } else {
     97             *out_index = i;
     98           }
     99         }
    100         return true;
    101       }
    102     }
    103   }
    104 
    105   return false;
    106 }
    107 
    108 // static
    109 void QuicUtils::SerializeUint128(uint128 v, uint8* out) {
    110   const uint64 lo = Uint128Low64(v);
    111   const uint64 hi = Uint128High64(v);
    112   // This assumes that the system is little-endian.
    113   memcpy(out, &lo, sizeof(lo));
    114   memcpy(out + sizeof(lo), &hi, sizeof(hi));
    115 }
    116 
    117 // static
    118 void QuicUtils::SerializeUint128Short(uint128 v, uint8* out) {
    119   const uint64 lo = Uint128Low64(v);
    120   const uint64 hi = Uint128High64(v);
    121   // This assumes that the system is little-endian.
    122   memcpy(out, &lo, sizeof(lo));
    123   memcpy(out + sizeof(lo), &hi, sizeof(hi) / 2);
    124 }
    125 
    126 #define RETURN_STRING_LITERAL(x) \
    127 case x: \
    128 return #x;
    129 
    130 // static
    131 const char* QuicUtils::StreamErrorToString(QuicRstStreamErrorCode error) {
    132   switch (error) {
    133     RETURN_STRING_LITERAL(QUIC_STREAM_NO_ERROR);
    134     RETURN_STRING_LITERAL(QUIC_STREAM_CONNECTION_ERROR);
    135     RETURN_STRING_LITERAL(QUIC_ERROR_PROCESSING_STREAM);
    136     RETURN_STRING_LITERAL(QUIC_MULTIPLE_TERMINATION_OFFSETS);
    137     RETURN_STRING_LITERAL(QUIC_BAD_APPLICATION_PAYLOAD);
    138     RETURN_STRING_LITERAL(QUIC_STREAM_PEER_GOING_AWAY);
    139     RETURN_STRING_LITERAL(QUIC_STREAM_CANCELLED);
    140     RETURN_STRING_LITERAL(QUIC_STREAM_LAST_ERROR);
    141   }
    142   // Return a default value so that we return this when |error| doesn't match
    143   // any of the QuicRstStreamErrorCodes. This can happen when the RstStream
    144   // frame sent by the peer (attacker) has invalid error code.
    145   return "INVALID_RST_STREAM_ERROR_CODE";
    146 }
    147 
    148 // static
    149 const char* QuicUtils::ErrorToString(QuicErrorCode error) {
    150   switch (error) {
    151     RETURN_STRING_LITERAL(QUIC_NO_ERROR);
    152     RETURN_STRING_LITERAL(QUIC_INTERNAL_ERROR);
    153     RETURN_STRING_LITERAL(QUIC_STREAM_DATA_AFTER_TERMINATION);
    154     RETURN_STRING_LITERAL(QUIC_INVALID_PACKET_HEADER);
    155     RETURN_STRING_LITERAL(QUIC_INVALID_FRAME_DATA);
    156     RETURN_STRING_LITERAL(QUIC_MISSING_PAYLOAD);
    157     RETURN_STRING_LITERAL(QUIC_INVALID_FEC_DATA);
    158     RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_DATA);
    159     RETURN_STRING_LITERAL(QUIC_INVALID_RST_STREAM_DATA);
    160     RETURN_STRING_LITERAL(QUIC_INVALID_CONNECTION_CLOSE_DATA);
    161     RETURN_STRING_LITERAL(QUIC_INVALID_GOAWAY_DATA);
    162     RETURN_STRING_LITERAL(QUIC_INVALID_ACK_DATA);
    163     RETURN_STRING_LITERAL(QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
    164     RETURN_STRING_LITERAL(QUIC_INVALID_VERSION_NEGOTIATION_PACKET);
    165     RETURN_STRING_LITERAL(QUIC_INVALID_PUBLIC_RST_PACKET);
    166     RETURN_STRING_LITERAL(QUIC_DECRYPTION_FAILURE);
    167     RETURN_STRING_LITERAL(QUIC_ENCRYPTION_FAILURE);
    168     RETURN_STRING_LITERAL(QUIC_PACKET_TOO_LARGE);
    169     RETURN_STRING_LITERAL(QUIC_PACKET_FOR_NONEXISTENT_STREAM);
    170     RETURN_STRING_LITERAL(QUIC_PEER_GOING_AWAY);
    171     RETURN_STRING_LITERAL(QUIC_HANDSHAKE_FAILED);
    172     RETURN_STRING_LITERAL(QUIC_CRYPTO_TAGS_OUT_OF_ORDER);
    173     RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_ENTRIES);
    174     RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_REJECTS);
    175     RETURN_STRING_LITERAL(QUIC_CRYPTO_INVALID_VALUE_LENGTH)
    176     RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE);
    177     RETURN_STRING_LITERAL(QUIC_CRYPTO_INTERNAL_ERROR);
    178     RETURN_STRING_LITERAL(QUIC_CRYPTO_VERSION_NOT_SUPPORTED);
    179     RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT);
    180     RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE);
    181     RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER);
    182     RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND);
    183     RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP);
    184     RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND);
    185     RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID);
    186     RETURN_STRING_LITERAL(QUIC_INVALID_PRIORITY);
    187     RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS);
    188     RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET);
    189     RETURN_STRING_LITERAL(QUIC_INVALID_VERSION);
    190     RETURN_STRING_LITERAL(QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED);
    191     RETURN_STRING_LITERAL(QUIC_INVALID_HEADER_ID);
    192     RETURN_STRING_LITERAL(QUIC_INVALID_NEGOTIATED_VALUE);
    193     RETURN_STRING_LITERAL(QUIC_DECOMPRESSION_FAILURE);
    194     RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT);
    195     RETURN_STRING_LITERAL(QUIC_ERROR_MIGRATING_ADDRESS);
    196     RETURN_STRING_LITERAL(QUIC_PACKET_WRITE_ERROR);
    197     RETURN_STRING_LITERAL(QUIC_PACKET_READ_ERROR);
    198     RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_FRAME);
    199     RETURN_STRING_LITERAL(QUIC_PROOF_INVALID);
    200     RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG);
    201     RETURN_STRING_LITERAL(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT);
    202     RETURN_STRING_LITERAL(QUIC_CRYPTO_SERVER_CONFIG_EXPIRED);
    203     RETURN_STRING_LITERAL(QUIC_INVALID_CHANNEL_ID_SIGNATURE);
    204     RETURN_STRING_LITERAL(QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED);
    205     RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO);
    206     RETURN_STRING_LITERAL(QUIC_VERSION_NEGOTIATION_MISMATCH);
    207     RETURN_STRING_LITERAL(QUIC_LAST_ERROR);
    208     // Intentionally have no default case, so we'll break the build
    209     // if we add errors and don't put them here.
    210   }
    211   // Return a default value so that we return this when |error| doesn't match
    212   // any of the QuicErrorCodes. This can happen when the ConnectionClose
    213   // frame sent by the peer (attacker) has invalid error code.
    214   return "INVALID_ERROR_CODE";
    215 }
    216 
    217 // static
    218 const char* QuicUtils::EncryptionLevelToString(EncryptionLevel level) {
    219   switch (level) {
    220     RETURN_STRING_LITERAL(ENCRYPTION_NONE);
    221     RETURN_STRING_LITERAL(ENCRYPTION_INITIAL);
    222     RETURN_STRING_LITERAL(ENCRYPTION_FORWARD_SECURE);
    223     RETURN_STRING_LITERAL(NUM_ENCRYPTION_LEVELS);
    224   }
    225   return "INVALID_ENCRYPTION_LEVEL";
    226 }
    227 
    228 // static
    229 string QuicUtils::TagToString(QuicTag tag) {
    230   char chars[4];
    231   bool ascii = true;
    232   const QuicTag orig_tag = tag;
    233 
    234   for (size_t i = 0; i < sizeof(chars); i++) {
    235     chars[i] = tag;
    236     if ((chars[i] == 0 || chars[i] == '\xff') && i == 3) {
    237       chars[i] = ' ';
    238     }
    239     if (!isprint(static_cast<unsigned char>(chars[i]))) {
    240       ascii = false;
    241       break;
    242     }
    243     tag >>= 8;
    244   }
    245 
    246   if (ascii) {
    247     return string(chars, sizeof(chars));
    248   }
    249 
    250   return base::UintToString(orig_tag);
    251 }
    252 
    253 // static
    254 string QuicUtils::StringToHexASCIIDump(StringPiece in_buffer) {
    255   int offset = 0;
    256   const int kBytesPerLine = 16;   // Max bytes dumped per line
    257   const char* buf = in_buffer.data();
    258   int bytes_remaining = in_buffer.size();
    259   string s;   // our output
    260   const char* p = buf;
    261   while (bytes_remaining > 0) {
    262     const int line_bytes = std::min(bytes_remaining, kBytesPerLine);
    263     base::StringAppendF(&s, "0x%04x:  ", offset);  // Do the line header
    264     for (int i = 0; i < kBytesPerLine; ++i) {
    265       if (i < line_bytes) {
    266         base::StringAppendF(&s, "%02x", static_cast<unsigned char>(p[i]));
    267       } else {
    268         s += "  ";    // two-space filler instead of two-space hex digits
    269       }
    270       if (i % 2) s += ' ';
    271     }
    272     s += ' ';
    273     for (int i = 0; i < line_bytes; ++i) {  // Do the ASCII dump
    274       s+= (p[i] >  32 && p[i] < 127) ? p[i] : '.';
    275     }
    276 
    277     bytes_remaining -= line_bytes;
    278     offset += line_bytes;
    279     p += line_bytes;
    280     s += '\n';
    281   }
    282   return s;
    283 }
    284 
    285 // static
    286 QuicPriority QuicUtils::LowestPriority() {
    287   return static_cast<QuicPriority>(kLowestPriority);
    288 }
    289 
    290 // static
    291 QuicPriority QuicUtils::HighestPriority() {
    292   return static_cast<QuicPriority>(kHighestPriority);
    293 }
    294 
    295 }  // namespace net
    296