henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | #ifndef WEBRTC_BASE_OPENSSLADAPTER_H__ |
| 12 | #define WEBRTC_BASE_OPENSSLADAPTER_H__ |
| 13 | |
| 14 | #include <string> |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 15 | #include "webrtc/base/messagehandler.h" |
| 16 | #include "webrtc/base/messagequeue.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | #include "webrtc/base/ssladapter.h" |
| 18 | |
| 19 | typedef struct ssl_st SSL; |
| 20 | typedef struct ssl_ctx_st SSL_CTX; |
| 21 | typedef struct x509_store_ctx_st X509_STORE_CTX; |
| 22 | |
| 23 | namespace rtc { |
| 24 | |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 27 | class OpenSSLAdapter : public SSLAdapter, public MessageHandler { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 28 | public: |
| 29 | static bool InitializeSSL(VerificationCallback callback); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | |
| 31 | OpenSSLAdapter(AsyncSocket* socket); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 32 | ~OpenSSLAdapter() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 34 | void SetMode(SSLMode mode) override; |
| 35 | int StartSSL(const char* hostname, bool restartable) override; |
| 36 | int Send(const void* pv, size_t cb) override; |
| 37 | int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override; |
| 38 | int Recv(void* pv, size_t cb) override; |
| 39 | int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) override; |
| 40 | int Close() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | |
| 42 | // Note that the socket returns ST_CONNECTING while SSL is being negotiated. |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 43 | ConnState GetState() const override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | |
| 45 | protected: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 46 | void OnConnectEvent(AsyncSocket* socket) override; |
| 47 | void OnReadEvent(AsyncSocket* socket) override; |
| 48 | void OnWriteEvent(AsyncSocket* socket) override; |
| 49 | void OnCloseEvent(AsyncSocket* socket, int err) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | enum SSLState { |
| 53 | SSL_NONE, SSL_WAIT, SSL_CONNECTING, SSL_CONNECTED, SSL_ERROR |
| 54 | }; |
| 55 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 56 | enum { MSG_TIMEOUT }; |
| 57 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | int BeginSSL(); |
| 59 | int ContinueSSL(); |
| 60 | void Error(const char* context, int err, bool signal = true); |
| 61 | void Cleanup(); |
| 62 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 63 | void OnMessage(Message* msg) override; |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 64 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | static bool VerifyServerName(SSL* ssl, const char* host, |
| 66 | bool ignore_bad_cert); |
| 67 | bool SSLPostConnectionCheck(SSL* ssl, const char* host); |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 68 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | static void SSLInfoCallback(const SSL* s, int where, int ret); |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 70 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | static int SSLVerifyCallback(int ok, X509_STORE_CTX* store); |
| 72 | static VerificationCallback custom_verify_callback_; |
| 73 | friend class OpenSSLStreamAdapter; // for custom_verify_callback_; |
| 74 | |
| 75 | static bool ConfigureTrustedRootCertificates(SSL_CTX* ctx); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 76 | SSL_CTX* SetupSSLContext(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | |
| 78 | SSLState state_; |
| 79 | bool ssl_read_needs_write_; |
| 80 | bool ssl_write_needs_read_; |
| 81 | // If true, socket will retain SSL configuration after Close. |
| 82 | bool restartable_; |
| 83 | |
| 84 | SSL* ssl_; |
| 85 | SSL_CTX* ssl_ctx_; |
| 86 | std::string ssl_host_name_; |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 87 | // Do DTLS or not |
| 88 | SSLMode ssl_mode_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | |
| 90 | bool custom_verification_succeeded_; |
| 91 | }; |
| 92 | |
| 93 | ///////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | } // namespace rtc |
| 96 | |
| 97 | #endif // WEBRTC_BASE_OPENSSLADAPTER_H__ |