OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, 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 BIN_TLS_SOCKET_H_ | |
6 #define BIN_TLS_SOCKET_H_ | |
7 | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <stdio.h> | |
11 #include <sys/types.h> | |
12 | |
13 #include <prinit.h> | |
14 #include <prerror.h> | |
15 #include <prnetdb.h> | |
16 | |
17 #include "bin/builtin.h" | |
18 #include "bin/dartutils.h" | |
19 #include "platform/globals.h" | |
20 #include "platform/thread.h" | |
21 | |
22 static void ThrowException(const char* message) { | |
23 Dart_Handle socket_io_exception = | |
24 DartUtils::NewDartSocketIOException(message, Dart_Null()); | |
25 Dart_ThrowException(socket_io_exception); | |
26 } | |
27 | |
28 | |
29 /* Handle an error reported from the NSS library. */ | |
30 static void ThrowPRException(const char* message) { | |
31 PRErrorCode error_code = PR_GetError(); | |
32 int error_length = PR_GetErrorTextLength(); | |
33 char* error_message = static_cast<char*>(malloc(error_length + 1)); | |
34 ASSERT(error_message != NULL); | |
35 int copied_length = PR_GetErrorText(error_message); | |
36 ASSERT(copied_length == error_length); | |
37 error_message[error_length] = '\0'; | |
38 OSError os_error_struct(error_code, error_message, OSError::kNSS); | |
39 Dart_Handle os_error = DartUtils::NewDartOSError(&os_error_struct); | |
40 Dart_Handle socket_io_exception = | |
41 DartUtils::NewDartSocketIOException(message, os_error); | |
42 free(error_message); | |
43 Dart_ThrowException(socket_io_exception); | |
44 } | |
45 | |
46 /* | |
47 * TlsFilter encapsulates the NSS SSL(TLS) code in a filter, that communicates | |
48 * with the containing _TlsFilterImpl Dart object through four shared | |
49 * ExternalByteArray buffers, for reading and writing plaintext, and | |
50 * reading and writing encrypted text. The filter handles handshaking | |
51 * and certificate verification. | |
52 */ | |
53 class TlsFilter { | |
54 public: | |
55 // These enums must agree with those in sdk/lib/io/tls_socket.dart. | |
56 enum BufferIndex { | |
57 kReadPlaintext, | |
58 kWritePlaintext, | |
59 kReadEncrypted, | |
60 kWriteEncrypted, | |
61 kNumBuffers | |
62 }; | |
63 | |
64 TlsFilter() | |
65 : string_start_(NULL), | |
66 string_length_(NULL), | |
67 handshake_complete_(NULL), | |
68 in_handshake_(false), | |
69 filter_(NULL) { } | |
70 | |
71 void Init(Dart_Handle dart_this); | |
72 void Connect(const char* host, | |
73 int port, | |
74 bool is_server, | |
75 const char* certificate_name); | |
76 void Destroy(); | |
77 void Handshake(); | |
78 void RegisterHandshakeCompleteCallback(Dart_Handle handshake_complete); | |
79 static void InitializeLibrary(const char* certificate_database, | |
80 const char* password); | |
81 | |
82 intptr_t ProcessBuffer(int bufferIndex); | |
83 | |
84 private: | |
85 static const int kMemioBufferSize = 20 * KB; | |
86 static bool library_initialized_; | |
87 static const char* password_; | |
88 static dart::Mutex mutex_; // To protect library initialization. | |
89 | |
90 uint8_t* buffers_[kNumBuffers]; | |
91 int64_t buffer_size_; | |
92 Dart_Handle string_start_; | |
93 Dart_Handle string_length_; | |
94 Dart_Handle dart_buffer_objects_[kNumBuffers]; | |
95 Dart_Handle handshake_complete_; | |
96 bool in_handshake_; | |
97 bool is_server_; | |
98 PRFileDesc* filter_; | |
99 | |
100 void InitializeBuffers(Dart_Handle dart_this); | |
101 void InitializePlatformData(); | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(TlsFilter); | |
104 }; | |
105 | |
106 #endif // BIN_TLS_SOCKET_H_ | |
OLD | NEW |