| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_P2P_BASE_DTLSTRANSPORT_H_ | 11 #ifndef WEBRTC_P2P_BASE_DTLSTRANSPORT_H_ |
| 12 #define WEBRTC_P2P_BASE_DTLSTRANSPORT_H_ | 12 #define WEBRTC_P2P_BASE_DTLSTRANSPORT_H_ |
| 13 | 13 |
| 14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/p2p/base/dtlstransportchannel.h" | 15 #include "webrtc/p2p/base/dtlstransportchannel.h" |
| 16 #include "webrtc/p2p/base/transport.h" | 16 #include "webrtc/p2p/base/transport.h" |
| 17 | 17 |
| 18 namespace rtc { | 18 namespace rtc { |
| 19 class SSLIdentity; | 19 class SSLIdentity; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace cricket { | 22 namespace cricket { |
| 23 | 23 |
| 24 class PortAllocator; | 24 class PortAllocator; |
| 25 | 25 |
| 26 // Base should be a descendant of cricket::Transport | 26 // Base should be a descendant of cricket::Transport and have a constructor |
| 27 // TODO(hbos): Add appropriate DCHECK thread checks to all methods. | 27 // that takes a transport name and PortAllocator. |
| 28 // |
| 29 // Everything in this class should be called on the worker thread. |
| 28 template<class Base> | 30 template<class Base> |
| 29 class DtlsTransport : public Base { | 31 class DtlsTransport : public Base { |
| 30 public: | 32 public: |
| 31 DtlsTransport(rtc::Thread* signaling_thread, | 33 DtlsTransport(const std::string& name, |
| 32 rtc::Thread* worker_thread, | |
| 33 const std::string& content_name, | |
| 34 PortAllocator* allocator, | 34 PortAllocator* allocator, |
| 35 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) | 35 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) |
| 36 : Base(signaling_thread, worker_thread, content_name, allocator), | 36 : Base(name, allocator), |
| 37 certificate_(certificate), | 37 certificate_(certificate), |
| 38 secure_role_(rtc::SSL_CLIENT), | 38 secure_role_(rtc::SSL_CLIENT), |
| 39 ssl_max_version_(rtc::SSL_PROTOCOL_DTLS_10) { | 39 ssl_max_version_(rtc::SSL_PROTOCOL_DTLS_10) {} |
| 40 } | |
| 41 | 40 |
| 42 ~DtlsTransport() { | 41 ~DtlsTransport() { |
| 43 Base::DestroyAllChannels(); | 42 Base::DestroyAllChannels(); |
| 44 } | 43 } |
| 45 void SetCertificate_w( | 44 |
| 45 void SetLocalCertificate( |
| 46 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { | 46 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
| 47 DCHECK(Base::worker_thread()->IsCurrent()); | |
| 48 certificate_ = certificate; | 47 certificate_ = certificate; |
| 49 } | 48 } |
| 50 bool GetCertificate_w( | 49 bool GetLocalCertificate( |
| 51 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override { | 50 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override { |
| 52 DCHECK(Base::worker_thread()->IsCurrent()); | |
| 53 if (!certificate_) | 51 if (!certificate_) |
| 54 return false; | 52 return false; |
| 55 | 53 |
| 56 *certificate = certificate_; | 54 *certificate = certificate_; |
| 57 return true; | 55 return true; |
| 58 } | 56 } |
| 59 | 57 |
| 60 bool SetSslMaxProtocolVersion_w(rtc::SSLProtocolVersion version) override { | 58 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) override { |
| 61 DCHECK(Base::worker_thread()->IsCurrent()); | |
| 62 ssl_max_version_ = version; | 59 ssl_max_version_ = version; |
| 63 return true; | 60 return true; |
| 64 } | 61 } |
| 65 | 62 |
| 66 bool ApplyLocalTransportDescription_w(TransportChannelImpl* channel, | 63 bool ApplyLocalTransportDescription(TransportChannelImpl* channel, |
| 67 std::string* error_desc) override { | 64 std::string* error_desc) override { |
| 68 DCHECK(Base::worker_thread()->IsCurrent()); | |
| 69 rtc::SSLFingerprint* local_fp = | 65 rtc::SSLFingerprint* local_fp = |
| 70 Base::local_description()->identity_fingerprint.get(); | 66 Base::local_description()->identity_fingerprint.get(); |
| 71 | 67 |
| 72 if (local_fp) { | 68 if (local_fp) { |
| 73 // Sanity check local fingerprint. | 69 // Sanity check local fingerprint. |
| 74 if (certificate_) { | 70 if (certificate_) { |
| 75 rtc::scoped_ptr<rtc::SSLFingerprint> local_fp_tmp( | 71 rtc::scoped_ptr<rtc::SSLFingerprint> local_fp_tmp( |
| 76 rtc::SSLFingerprint::Create(local_fp->algorithm, | 72 rtc::SSLFingerprint::Create(local_fp->algorithm, |
| 77 certificate_->identity())); | 73 certificate_->identity())); |
| 78 ASSERT(local_fp_tmp.get() != NULL); | 74 ASSERT(local_fp_tmp.get() != NULL); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 91 } else { | 87 } else { |
| 92 certificate_ = nullptr; | 88 certificate_ = nullptr; |
| 93 } | 89 } |
| 94 | 90 |
| 95 if (!channel->SetLocalCertificate(certificate_)) { | 91 if (!channel->SetLocalCertificate(certificate_)) { |
| 96 return BadTransportDescription("Failed to set local identity.", | 92 return BadTransportDescription("Failed to set local identity.", |
| 97 error_desc); | 93 error_desc); |
| 98 } | 94 } |
| 99 | 95 |
| 100 // Apply the description in the base class. | 96 // Apply the description in the base class. |
| 101 return Base::ApplyLocalTransportDescription_w(channel, error_desc); | 97 return Base::ApplyLocalTransportDescription(channel, error_desc); |
| 102 } | 98 } |
| 103 | 99 |
| 104 bool NegotiateTransportDescription_w(ContentAction local_role, | 100 bool NegotiateTransportDescription(ContentAction local_role, |
| 105 std::string* error_desc) override { | 101 std::string* error_desc) override { |
| 106 DCHECK(Base::worker_thread()->IsCurrent()); | |
| 107 if (!Base::local_description() || !Base::remote_description()) { | 102 if (!Base::local_description() || !Base::remote_description()) { |
| 108 const std::string msg = "Local and Remote description must be set before " | 103 const std::string msg = "Local and Remote description must be set before " |
| 109 "transport descriptions are negotiated"; | 104 "transport descriptions are negotiated"; |
| 110 return BadTransportDescription(msg, error_desc); | 105 return BadTransportDescription(msg, error_desc); |
| 111 } | 106 } |
| 112 | 107 |
| 113 rtc::SSLFingerprint* local_fp = | 108 rtc::SSLFingerprint* local_fp = |
| 114 Base::local_description()->identity_fingerprint.get(); | 109 Base::local_description()->identity_fingerprint.get(); |
| 115 rtc::SSLFingerprint* remote_fp = | 110 rtc::SSLFingerprint* remote_fp = |
| 116 Base::remote_description()->identity_fingerprint.get(); | 111 Base::remote_description()->identity_fingerprint.get(); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 return BadTransportDescription( | 188 return BadTransportDescription( |
| 194 "Local fingerprint supplied when caller didn't offer DTLS.", | 189 "Local fingerprint supplied when caller didn't offer DTLS.", |
| 195 error_desc); | 190 error_desc); |
| 196 } else { | 191 } else { |
| 197 // We are not doing DTLS | 192 // We are not doing DTLS |
| 198 remote_fingerprint_.reset(new rtc::SSLFingerprint( | 193 remote_fingerprint_.reset(new rtc::SSLFingerprint( |
| 199 "", NULL, 0)); | 194 "", NULL, 0)); |
| 200 } | 195 } |
| 201 | 196 |
| 202 // Now run the negotiation for the base class. | 197 // Now run the negotiation for the base class. |
| 203 return Base::NegotiateTransportDescription_w(local_role, error_desc); | 198 return Base::NegotiateTransportDescription(local_role, error_desc); |
| 204 } | 199 } |
| 205 | 200 |
| 206 DtlsTransportChannelWrapper* CreateTransportChannel(int component) override { | 201 DtlsTransportChannelWrapper* CreateTransportChannel(int component) override { |
| 207 DtlsTransportChannelWrapper* channel = new DtlsTransportChannelWrapper( | 202 DtlsTransportChannelWrapper* channel = new DtlsTransportChannelWrapper( |
| 208 this, Base::CreateTransportChannel(component)); | 203 this, Base::CreateTransportChannel(component)); |
| 209 channel->SetSslMaxProtocolVersion(ssl_max_version_); | 204 channel->SetSslMaxProtocolVersion(ssl_max_version_); |
| 210 return channel; | 205 return channel; |
| 211 } | 206 } |
| 212 | 207 |
| 213 void DestroyTransportChannel(TransportChannelImpl* channel) override { | 208 void DestroyTransportChannel(TransportChannelImpl* channel) override { |
| 214 // Kind of ugly, but this lets us do the exact inverse of the create. | 209 // Kind of ugly, but this lets us do the exact inverse of the create. |
| 215 DtlsTransportChannelWrapper* dtls_channel = | 210 DtlsTransportChannelWrapper* dtls_channel = |
| 216 static_cast<DtlsTransportChannelWrapper*>(channel); | 211 static_cast<DtlsTransportChannelWrapper*>(channel); |
| 217 TransportChannelImpl* base_channel = dtls_channel->channel(); | 212 TransportChannelImpl* base_channel = dtls_channel->channel(); |
| 218 delete dtls_channel; | 213 delete dtls_channel; |
| 219 Base::DestroyTransportChannel(base_channel); | 214 Base::DestroyTransportChannel(base_channel); |
| 220 } | 215 } |
| 221 | 216 |
| 222 bool GetSslRole_w(rtc::SSLRole* ssl_role) const override { | 217 bool GetSslRole(rtc::SSLRole* ssl_role) const override { |
| 223 DCHECK(Base::worker_thread()->IsCurrent()); | |
| 224 ASSERT(ssl_role != NULL); | 218 ASSERT(ssl_role != NULL); |
| 225 *ssl_role = secure_role_; | 219 *ssl_role = secure_role_; |
| 226 return true; | 220 return true; |
| 227 } | 221 } |
| 228 | 222 |
| 229 private: | 223 private: |
| 230 bool ApplyNegotiatedTransportDescription_w( | 224 bool ApplyNegotiatedTransportDescription(TransportChannelImpl* channel, |
| 231 TransportChannelImpl* channel, | 225 std::string* error_desc) override { |
| 232 std::string* error_desc) override { | |
| 233 DCHECK(Base::worker_thread()->IsCurrent()); | |
| 234 // Set ssl role. Role must be set before fingerprint is applied, which | 226 // Set ssl role. Role must be set before fingerprint is applied, which |
| 235 // initiates DTLS setup. | 227 // initiates DTLS setup. |
| 236 if (!channel->SetSslRole(secure_role_)) { | 228 if (!channel->SetSslRole(secure_role_)) { |
| 237 return BadTransportDescription("Failed to set ssl role for the channel.", | 229 return BadTransportDescription("Failed to set ssl role for the channel.", |
| 238 error_desc); | 230 error_desc); |
| 239 } | 231 } |
| 240 // Apply remote fingerprint. | 232 // Apply remote fingerprint. |
| 241 if (!channel->SetRemoteFingerprint( | 233 if (!channel->SetRemoteFingerprint( |
| 242 remote_fingerprint_->algorithm, | 234 remote_fingerprint_->algorithm, |
| 243 reinterpret_cast<const uint8*>(remote_fingerprint_->digest.data()), | 235 reinterpret_cast<const uint8*>(remote_fingerprint_->digest.data()), |
| 244 remote_fingerprint_->digest.size())) { | 236 remote_fingerprint_->digest.size())) { |
| 245 return BadTransportDescription("Failed to apply remote fingerprint.", | 237 return BadTransportDescription("Failed to apply remote fingerprint.", |
| 246 error_desc); | 238 error_desc); |
| 247 } | 239 } |
| 248 return Base::ApplyNegotiatedTransportDescription_w(channel, error_desc); | 240 return Base::ApplyNegotiatedTransportDescription(channel, error_desc); |
| 249 } | 241 } |
| 250 | 242 |
| 251 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 243 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
| 252 rtc::SSLRole secure_role_; | 244 rtc::SSLRole secure_role_; |
| 253 rtc::SSLProtocolVersion ssl_max_version_; | 245 rtc::SSLProtocolVersion ssl_max_version_; |
| 254 rtc::scoped_ptr<rtc::SSLFingerprint> remote_fingerprint_; | 246 rtc::scoped_ptr<rtc::SSLFingerprint> remote_fingerprint_; |
| 255 }; | 247 }; |
| 256 | 248 |
| 257 } // namespace cricket | 249 } // namespace cricket |
| 258 | 250 |
| 259 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORT_H_ | 251 #endif // WEBRTC_P2P_BASE_DTLSTRANSPORT_H_ |
| OLD | NEW |