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 memio_(NULL) { } |
| 70 |
| 71 void Init(Dart_Handle dart_this); |
| 72 void Connect(const char* host, int port); |
| 73 void Destroy(); |
| 74 void DestroyPlatformIndependent(); |
| 75 void Handshake(); |
| 76 void RegisterHandshakeCompleteCallback(Dart_Handle handshake_complete); |
| 77 static void InitializeLibrary(const char* pkcert_directory); |
| 78 |
| 79 intptr_t ProcessBuffer(int bufferIndex); |
| 80 |
| 81 private: |
| 82 static const int kMemioBufferSize = 20 * KB; |
| 83 static bool library_initialized_; |
| 84 static dart::Mutex mutex_; // To protect library initialization. |
| 85 |
| 86 uint8_t* buffers_[kNumBuffers]; |
| 87 int64_t buffer_size_; |
| 88 Dart_Handle string_start_; |
| 89 Dart_Handle string_length_; |
| 90 Dart_Handle dart_buffer_objects_[kNumBuffers]; |
| 91 Dart_Handle handshake_complete_; |
| 92 bool in_handshake_; |
| 93 PRFileDesc* memio_; |
| 94 |
| 95 void InitializeBuffers(Dart_Handle dart_this); |
| 96 void InitializePlatformData(); |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(TlsFilter); |
| 99 }; |
| 100 |
| 101 #endif // BIN_TLS_SOCKET_H_ |
OLD | NEW |