Flutter Engine
The Flutter Engine
secure_socket_filter.h
Go to the documentation of this file.
1// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_BIN_SECURE_SOCKET_FILTER_H_
6#define RUNTIME_BIN_SECURE_SOCKET_FILTER_H_
7
8#include <openssl/bio.h>
9#include <openssl/ssl.h>
10#include <openssl/x509.h>
11
12#include <memory>
13
14#include "bin/builtin.h"
17#include "platform/utils.h"
18
19namespace dart {
20namespace bin {
21
22/* These are defined in root_certificates.cc. */
23extern const unsigned char* root_certificates_pem;
24extern unsigned int root_certificates_pem_length;
25
27 public:
28 X509TrustState(const X509* x509, bool is_trusted)
29 : x509_(x509), is_trusted_(is_trusted) {}
30
31 const X509* x509() const { return x509_; }
32 bool is_trusted() const { return is_trusted_; }
33
34 private:
35 const X509* x509_;
36 bool is_trusted_;
37
38 DISALLOW_COPY_AND_ASSIGN(X509TrustState);
39};
40
41class SSLFilter : public ReferenceCounted<SSLFilter> {
42 public:
43 static void Init();
44 static void Cleanup();
45
46 // These enums must agree with those in sdk/lib/io/secure_socket.dart.
54 };
55
56 static const intptr_t kApproximateSize;
57 static constexpr int kSSLFilterNativeFieldIndex = 0;
58
60 : callback_error(nullptr),
61 ssl_(nullptr),
62 socket_side_(nullptr),
63 string_start_(nullptr),
64 string_length_(nullptr),
65 handshake_complete_(nullptr),
66 bad_certificate_callback_(nullptr),
67 in_handshake_(false),
68 hostname_(nullptr) {}
69
70 ~SSLFilter();
71
72 char* hostname() const { return hostname_; }
73 bool is_server() const { return is_server_; }
74 bool is_client() const { return !is_server_; }
75
76 Dart_Handle Init(Dart_Handle dart_this);
77 void Connect(const char* hostname,
78 SSLCertContext* context,
79 bool is_server,
80 bool request_client_certificate,
81 bool require_client_certificate,
82 Dart_Handle protocols_handle);
83 void Destroy();
84 void FreeResources();
88 void RegisterHandshakeCompleteCallback(Dart_Handle handshake_complete);
91 Dart_Port key_log_port() { return key_log_port_; }
93 return Dart_HandleFromPersistent(bad_certificate_callback_);
94 }
95 int ProcessReadPlaintextBuffer(int start, int end);
96 int ProcessWritePlaintextBuffer(int start, int end);
97 int ProcessReadEncryptedBuffer(int start, int end);
98 int ProcessWriteEncryptedBuffer(int start, int end);
99 bool ProcessAllBuffers(int starts[kNumBuffers],
100 int ends[kNumBuffers],
101 bool in_handshake);
103 static void InitializeLibrary();
105
106 static CObject* ProcessFilterRequest(const CObjectArray& request);
107
108 // The index of the external data field in _ssl that points to the SSLFilter.
110 // The index of the external data field in _ssl that points to the
111 // SSLCertContext.
113
115 return certificate_trust_state_.get();
116 }
117 Dart_Port reply_port() const { return reply_port_; }
119 return trust_evaluate_reply_port_;
120 }
121
122 private:
123 static const intptr_t kInternalBIOSize;
124 static bool library_initialized_;
125 static Mutex* mutex_; // To protect library initialization.
126
127 SSL* ssl_;
128 BIO* socket_side_;
129 // Currently only one(root) certificate is evaluated via
130 // TrustEvaluate mechanism.
131 std::unique_ptr<X509TrustState> certificate_trust_state_;
132
133 uint8_t* buffers_[kNumBuffers];
134 int buffer_size_;
135 int encrypted_buffer_size_;
136 Dart_PersistentHandle string_start_;
137 Dart_PersistentHandle string_length_;
138 Dart_PersistentHandle dart_buffer_objects_[kNumBuffers];
139 Dart_PersistentHandle handshake_complete_;
140 Dart_PersistentHandle bad_certificate_callback_;
141 bool in_handshake_;
142 bool is_server_;
143 char* hostname_;
144
145 Dart_Port reply_port_ = ILLEGAL_PORT;
146 Dart_Port trust_evaluate_reply_port_ = ILLEGAL_PORT;
147 Dart_Port key_log_port_ = ILLEGAL_PORT;
148
149 static bool IsBufferEncrypted(int i) {
150 return static_cast<BufferIndex>(i) >= kFirstEncrypted;
151 }
152 Dart_Handle InitializeBuffers(Dart_Handle dart_this);
153 void InitializePlatformData();
154
155 DISALLOW_COPY_AND_ASSIGN(SSLFilter);
156};
157
158} // namespace bin
159} // namespace dart
160
161#endif // RUNTIME_BIN_SECURE_SOCKET_FILTER_H_
void Connect(const char *hostname, SSLCertContext *context, bool is_server, bool request_client_certificate, bool require_client_certificate, Dart_Handle protocols_handle)
int ProcessReadEncryptedBuffer(int start, int end)
void RegisterHandshakeCompleteCallback(Dart_Handle handshake_complete)
void RegisterKeyLogPort(Dart_Port key_log_port)
int ProcessWritePlaintextBuffer(int start, int end)
void MarkAsTrusted(Dart_NativeArguments args)
void RegisterBadCertificateCallback(Dart_Handle callback)
void GetSelectedProtocol(Dart_NativeArguments args)
Dart_Handle bad_certificate_callback()
bool ProcessAllBuffers(int starts[kNumBuffers], int ends[kNumBuffers], bool in_handshake)
static CObject * ProcessFilterRequest(const CObjectArray &request)
int Handshake(Dart_Port reply_port)
static const intptr_t kApproximateSize
Dart_Port trust_evaluate_reply_port() const
static constexpr int kSSLFilterNativeFieldIndex
int ProcessReadPlaintextBuffer(int start, int end)
int ProcessWriteEncryptedBuffer(int start, int end)
Dart_Port reply_port() const
const X509TrustState * certificate_trust_state()
X509TrustState(const X509 *x509, bool is_trusted)
#define ILLEGAL_PORT
Definition: dart_api.h:1535
int64_t Dart_Port
Definition: dart_api.h:1525
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
Dart_Handle Dart_PersistentHandle
Definition: dart_api.h:259
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
const unsigned char * root_certificates_pem
unsigned int root_certificates_pem_length
Definition: dart_vm.cc:33
DART_EXPORT Dart_Handle Dart_HandleFromPersistent(Dart_PersistentHandle object)