Home | History | Annotate | Download | only in socket
      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/socket/ssl_client_socket.h"
      6 
      7 #include "base/strings/string_util.h"
      8 
      9 namespace net {
     10 
     11 SSLClientSocket::SSLClientSocket()
     12     : was_npn_negotiated_(false),
     13       was_spdy_negotiated_(false),
     14       protocol_negotiated_(kProtoUnknown),
     15       channel_id_sent_(false) {
     16 }
     17 
     18 // static
     19 NextProto SSLClientSocket::NextProtoFromString(
     20     const std::string& proto_string) {
     21   if (proto_string == "http1.1" || proto_string == "http/1.1") {
     22     return kProtoHTTP11;
     23   } else if (proto_string == "spdy/1") {
     24     return kProtoSPDY1;
     25   } else if (proto_string == "spdy/2") {
     26     return kProtoSPDY2;
     27   } else if (proto_string == "spdy/3") {
     28     return kProtoSPDY3;
     29   } else if (proto_string == "spdy/3.1") {
     30     return kProtoSPDY31;
     31   } else if (proto_string == "spdy/4a2") {
     32     return kProtoSPDY4a2;
     33   } else if (proto_string == "HTTP-draft-04/2.0") {
     34     return kProtoHTTP2Draft04;
     35   } else if (proto_string == "quic/1+spdy/3") {
     36     return kProtoQUIC1SPDY3;
     37   } else {
     38     return kProtoUnknown;
     39   }
     40 }
     41 
     42 // static
     43 const char* SSLClientSocket::NextProtoToString(NextProto next_proto) {
     44   switch (next_proto) {
     45     case kProtoHTTP11:
     46       return "http/1.1";
     47     case kProtoSPDY1:
     48       return "spdy/1";
     49     case kProtoSPDY2:
     50       return "spdy/2";
     51     case kProtoSPDY3:
     52       return "spdy/3";
     53     case kProtoSPDY31:
     54       return "spdy/3.1";
     55     case kProtoSPDY4a2:
     56       return "spdy/4a2";
     57     case kProtoHTTP2Draft04:
     58       return "HTTP-draft-04/2.0";
     59     case kProtoQUIC1SPDY3:
     60       return "quic/1+spdy/3";
     61     case kProtoSPDY21:
     62     case kProtoUnknown:
     63       break;
     64   }
     65   return "unknown";
     66 }
     67 
     68 // static
     69 const char* SSLClientSocket::NextProtoStatusToString(
     70     const SSLClientSocket::NextProtoStatus status) {
     71   switch (status) {
     72     case kNextProtoUnsupported:
     73       return "unsupported";
     74     case kNextProtoNegotiated:
     75       return "negotiated";
     76     case kNextProtoNoOverlap:
     77       return "no-overlap";
     78   }
     79   return NULL;
     80 }
     81 
     82 // static
     83 std::string SSLClientSocket::ServerProtosToString(
     84     const std::string& server_protos) {
     85   const char* protos = server_protos.c_str();
     86   size_t protos_len = server_protos.length();
     87   std::vector<std::string> server_protos_with_commas;
     88   for (size_t i = 0; i < protos_len; ) {
     89     const size_t len = protos[i];
     90     std::string proto_str(&protos[i + 1], len);
     91     server_protos_with_commas.push_back(proto_str);
     92     i += len + 1;
     93   }
     94   return JoinString(server_protos_with_commas, ',');
     95 }
     96 
     97 bool SSLClientSocket::WasNpnNegotiated() const {
     98   return was_npn_negotiated_;
     99 }
    100 
    101 NextProto SSLClientSocket::GetNegotiatedProtocol() const {
    102   return protocol_negotiated_;
    103 }
    104 
    105 bool SSLClientSocket::IgnoreCertError(int error, int load_flags) {
    106   if (error == OK || load_flags & LOAD_IGNORE_ALL_CERT_ERRORS)
    107     return true;
    108 
    109   if (error == ERR_CERT_COMMON_NAME_INVALID &&
    110       (load_flags & LOAD_IGNORE_CERT_COMMON_NAME_INVALID))
    111     return true;
    112 
    113   if (error == ERR_CERT_DATE_INVALID &&
    114       (load_flags & LOAD_IGNORE_CERT_DATE_INVALID))
    115     return true;
    116 
    117   if (error == ERR_CERT_AUTHORITY_INVALID &&
    118       (load_flags & LOAD_IGNORE_CERT_AUTHORITY_INVALID))
    119     return true;
    120 
    121   return false;
    122 }
    123 
    124 bool SSLClientSocket::set_was_npn_negotiated(bool negotiated) {
    125   return was_npn_negotiated_ = negotiated;
    126 }
    127 
    128 bool SSLClientSocket::was_spdy_negotiated() const {
    129   return was_spdy_negotiated_;
    130 }
    131 
    132 bool SSLClientSocket::set_was_spdy_negotiated(bool negotiated) {
    133   return was_spdy_negotiated_ = negotiated;
    134 }
    135 
    136 void SSLClientSocket::set_protocol_negotiated(NextProto protocol_negotiated) {
    137   protocol_negotiated_ = protocol_negotiated;
    138 }
    139 
    140 bool SSLClientSocket::WasChannelIDSent() const {
    141   return channel_id_sent_;
    142 }
    143 
    144 void SSLClientSocket::set_channel_id_sent(bool channel_id_sent) {
    145   channel_id_sent_ = channel_id_sent;
    146 }
    147 
    148 }  // namespace net
    149