blob: 554627f58fa7dfe66a10a2abf2256c874b05b4a0 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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.orga9b1ec02014-12-29 23:00:14 +000015#include "webrtc/base/messagehandler.h"
16#include "webrtc/base/messagequeue.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/ssladapter.h"
18
19typedef struct ssl_st SSL;
20typedef struct ssl_ctx_st SSL_CTX;
21typedef struct x509_store_ctx_st X509_STORE_CTX;
22
23namespace rtc {
24
25///////////////////////////////////////////////////////////////////////////////
26
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +000027class OpenSSLAdapter : public SSLAdapter, public MessageHandler {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028public:
29 static bool InitializeSSL(VerificationCallback callback);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010030 static bool InitializeSSLThread();
31 static bool CleanupSSL();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032
33 OpenSSLAdapter(AsyncSocket* socket);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000034 ~OpenSSLAdapter() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000036 void SetMode(SSLMode mode) override;
37 int StartSSL(const char* hostname, bool restartable) override;
38 int Send(const void* pv, size_t cb) override;
39 int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
Stefan Holmer9131efd2016-05-23 18:19:26 +020040 int Recv(void* pv, size_t cb, int64_t* timestamp) override;
41 int RecvFrom(void* pv,
42 size_t cb,
43 SocketAddress* paddr,
44 int64_t* timestamp) override;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000045 int Close() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
47 // Note that the socket returns ST_CONNECTING while SSL is being negotiated.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000048 ConnState GetState() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049
50protected:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000051 void OnConnectEvent(AsyncSocket* socket) override;
52 void OnReadEvent(AsyncSocket* socket) override;
53 void OnWriteEvent(AsyncSocket* socket) override;
54 void OnCloseEvent(AsyncSocket* socket, int err) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055
56private:
57 enum SSLState {
58 SSL_NONE, SSL_WAIT, SSL_CONNECTING, SSL_CONNECTED, SSL_ERROR
59 };
60
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +000061 enum { MSG_TIMEOUT };
62
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 int BeginSSL();
64 int ContinueSSL();
65 void Error(const char* context, int err, bool signal = true);
66 void Cleanup();
67
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000068 void OnMessage(Message* msg) override;
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +000069
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 static bool VerifyServerName(SSL* ssl, const char* host,
71 bool ignore_bad_cert);
72 bool SSLPostConnectionCheck(SSL* ssl, const char* host);
tfarinaa41ab932015-10-30 16:08:48 -070073#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 static void SSLInfoCallback(const SSL* s, int where, int ret);
tfarinaa41ab932015-10-30 16:08:48 -070075#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
77 static VerificationCallback custom_verify_callback_;
78 friend class OpenSSLStreamAdapter; // for custom_verify_callback_;
79
80 static bool ConfigureTrustedRootCertificates(SSL_CTX* ctx);
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +000081 SSL_CTX* SetupSSLContext();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082
83 SSLState state_;
84 bool ssl_read_needs_write_;
85 bool ssl_write_needs_read_;
86 // If true, socket will retain SSL configuration after Close.
87 bool restartable_;
88
89 SSL* ssl_;
90 SSL_CTX* ssl_ctx_;
91 std::string ssl_host_name_;
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +000092 // Do DTLS or not
93 SSLMode ssl_mode_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094
95 bool custom_verification_succeeded_;
96};
97
98/////////////////////////////////////////////////////////////////////////////
99
100} // namespace rtc
101
102#endif // WEBRTC_BASE_OPENSSLADAPTER_H__