1 /*
2  *  Copyright 2016 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/p2p/quic/quictransport.h"
12 
13 #include "webrtc/base/checks.h"
14 #include "webrtc/p2p/base/p2ptransportchannel.h"
15 
16 namespace cricket {
17 
QuicTransport(const std::string & name,PortAllocator * allocator,const rtc::scoped_refptr<rtc::RTCCertificate> & certificate)18 QuicTransport::QuicTransport(
19     const std::string& name,
20     PortAllocator* allocator,
21     const rtc::scoped_refptr<rtc::RTCCertificate>& certificate)
22     : Transport(name, allocator), local_certificate_(certificate) {}
23 
~QuicTransport()24 QuicTransport::~QuicTransport() {
25   DestroyAllChannels();
26 }
27 
SetLocalCertificate(const rtc::scoped_refptr<rtc::RTCCertificate> & certificate)28 void QuicTransport::SetLocalCertificate(
29     const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
30   local_certificate_ = certificate;
31 }
GetLocalCertificate(rtc::scoped_refptr<rtc::RTCCertificate> * certificate)32 bool QuicTransport::GetLocalCertificate(
33     rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
34   if (!local_certificate_) {
35     return false;
36   }
37   *certificate = local_certificate_;
38   return true;
39 }
40 
ApplyLocalTransportDescription(TransportChannelImpl * channel,std::string * error_desc)41 bool QuicTransport::ApplyLocalTransportDescription(
42     TransportChannelImpl* channel,
43     std::string* error_desc) {
44   rtc::SSLFingerprint* local_fp =
45       local_description()->identity_fingerprint.get();
46   if (!VerifyCertificateFingerprint(local_certificate_.get(), local_fp,
47                                     error_desc)) {
48     return false;
49   }
50   if (!channel->SetLocalCertificate(local_certificate_)) {
51     return BadTransportDescription("Failed to set local identity.", error_desc);
52   }
53   return Transport::ApplyLocalTransportDescription(channel, error_desc);
54 }
55 
NegotiateTransportDescription(ContentAction action,std::string * error_desc)56 bool QuicTransport::NegotiateTransportDescription(ContentAction action,
57                                                   std::string* error_desc) {
58   if (!local_description() || !remote_description()) {
59     const std::string msg =
60         "Local and Remote description must be set before "
61         "transport descriptions are negotiated";
62     return BadTransportDescription(msg, error_desc);
63   }
64   rtc::SSLFingerprint* local_fp =
65       local_description()->identity_fingerprint.get();
66   rtc::SSLFingerprint* remote_fp =
67       remote_description()->identity_fingerprint.get();
68   if (!local_fp || !remote_fp) {
69     return BadTransportDescription("Fingerprints must be supplied for QUIC.",
70                                    error_desc);
71   }
72   remote_fingerprint_.reset(new rtc::SSLFingerprint(*remote_fp));
73   if (!NegotiateRole(action, &local_role_, error_desc)) {
74     return false;
75   }
76   // Now run the negotiation for the Transport class.
77   return Transport::NegotiateTransportDescription(action, error_desc);
78 }
79 
CreateTransportChannel(int component)80 QuicTransportChannel* QuicTransport::CreateTransportChannel(int component) {
81   P2PTransportChannel* ice_channel =
82       new P2PTransportChannel(name(), component, port_allocator());
83   return new QuicTransportChannel(ice_channel);
84 }
85 
DestroyTransportChannel(TransportChannelImpl * channel)86 void QuicTransport::DestroyTransportChannel(TransportChannelImpl* channel) {
87   delete channel;
88 }
89 
GetSslRole(rtc::SSLRole * ssl_role) const90 bool QuicTransport::GetSslRole(rtc::SSLRole* ssl_role) const {
91   RTC_DCHECK(ssl_role != NULL);
92   *ssl_role = local_role_;
93   return true;
94 }
95 
ApplyNegotiatedTransportDescription(TransportChannelImpl * channel,std::string * error_desc)96 bool QuicTransport::ApplyNegotiatedTransportDescription(
97     TransportChannelImpl* channel,
98     std::string* error_desc) {
99   // Set ssl role and remote fingerprint. These are required for QUIC setup.
100   if (!channel->SetSslRole(local_role_)) {
101     return BadTransportDescription("Failed to set ssl role for the channel.",
102                                    error_desc);
103   }
104   // Apply remote fingerprint.
105   if (!channel->SetRemoteFingerprint(
106           remote_fingerprint_->algorithm,
107           reinterpret_cast<const uint8_t*>(remote_fingerprint_->digest.data()),
108           remote_fingerprint_->digest.size())) {
109     return BadTransportDescription("Failed to apply remote fingerprint.",
110                                    error_desc);
111   }
112   return Transport::ApplyNegotiatedTransportDescription(channel, error_desc);
113 }
114 
115 }  // namespace cricket
116