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 #include "bin/tls_socket.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> |
| 10 #include <stdio.h> |
| 11 #include <string.h> |
| 12 |
| 13 #include <nss.h> |
| 14 #include <prerror.h> |
| 15 #include <prinit.h> |
| 16 #include <prnetdb.h> |
| 17 #include <ssl.h> |
| 18 |
| 19 #include "bin/builtin.h" |
| 20 #include "bin/dartutils.h" |
| 21 #include "bin/net/nss_memio.h" |
| 22 #include "bin/thread.h" |
| 23 #include "bin/utils.h" |
| 24 #include "platform/utils.h" |
| 25 |
| 26 #include "include/dart_api.h" |
| 27 |
| 28 bool TlsFilter::library_initialized_ = false; |
| 29 static const int kTlsFilterNativeFieldIndex = 0; |
| 30 |
| 31 static TlsFilter* GetTlsFilter(Dart_NativeArguments args) { |
| 32 TlsFilter* filter; |
| 33 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
| 34 ASSERT(Dart_IsInstance(dart_this)); |
| 35 ThrowIfError(Dart_GetNativeInstanceField( |
| 36 dart_this, |
| 37 kTlsFilterNativeFieldIndex, |
| 38 reinterpret_cast<intptr_t*>(&filter))); |
| 39 return filter; |
| 40 } |
| 41 |
| 42 |
| 43 static void SetTlsFilter(Dart_NativeArguments args, TlsFilter* filter) { |
| 44 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
| 45 ASSERT(Dart_IsInstance(dart_this)); |
| 46 ThrowIfError(Dart_SetNativeInstanceField( |
| 47 dart_this, |
| 48 kTlsFilterNativeFieldIndex, |
| 49 reinterpret_cast<intptr_t>(filter))); |
| 50 } |
| 51 |
| 52 |
| 53 void FUNCTION_NAME(TlsSocket_Init)(Dart_NativeArguments args) { |
| 54 Dart_EnterScope(); |
| 55 TlsFilter* filter = new TlsFilter; |
| 56 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); |
| 57 if (Dart_IsError(dart_this)) { |
| 58 delete filter; |
| 59 Dart_PropagateError(dart_this); |
| 60 } |
| 61 SetTlsFilter(args, filter); |
| 62 filter->Init(dart_this); |
| 63 Dart_ExitScope(); |
| 64 } |
| 65 |
| 66 |
| 67 void FUNCTION_NAME(TlsSocket_Connect)(Dart_NativeArguments args) { |
| 68 Dart_EnterScope(); |
| 69 Dart_Handle host_name = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 70 Dart_Handle port_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); |
| 71 |
| 72 const char* host_name_string = NULL; |
| 73 // TODO(whesse): Is truncating a Dart string containing \0 what we want? |
| 74 ThrowIfError(Dart_StringToCString(host_name, &host_name_string)); |
| 75 |
| 76 int64_t port; |
| 77 if (!DartUtils::GetInt64Value(port_object, &port) || |
| 78 port < 0 || port > 65535) { |
| 79 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 80 "Illegal port parameter in TlsSocket")); |
| 81 } |
| 82 |
| 83 GetTlsFilter(args)->Connect(host_name_string, static_cast<int>(port)); |
| 84 Dart_ExitScope(); |
| 85 } |
| 86 |
| 87 |
| 88 void FUNCTION_NAME(TlsSocket_Destroy)(Dart_NativeArguments args) { |
| 89 Dart_EnterScope(); |
| 90 TlsFilter* filter = GetTlsFilter(args); |
| 91 SetTlsFilter(args, NULL); |
| 92 filter->Destroy(); |
| 93 delete filter; |
| 94 Dart_ExitScope(); |
| 95 } |
| 96 |
| 97 |
| 98 void FUNCTION_NAME(TlsSocket_Handshake)(Dart_NativeArguments args) { |
| 99 Dart_EnterScope(); |
| 100 GetTlsFilter(args)->Handshake(); |
| 101 Dart_ExitScope(); |
| 102 } |
| 103 |
| 104 |
| 105 void FUNCTION_NAME(TlsSocket_RegisterHandshakeCallbacks)( |
| 106 Dart_NativeArguments args) { |
| 107 Dart_EnterScope(); |
| 108 Dart_Handle handshake_start = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 109 Dart_Handle handshake_finish = ThrowIfError(Dart_GetNativeArgument(args, 2)); |
| 110 if (!Dart_IsClosure(handshake_start) || |
| 111 !Dart_IsClosure(handshake_finish)) { |
| 112 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 113 "Illegal argument to RegisterHandshakeCallbacks")); |
| 114 } |
| 115 GetTlsFilter(args)->RegisterHandshakeCallbacks(handshake_start, |
| 116 handshake_finish); |
| 117 Dart_ExitScope(); |
| 118 } |
| 119 |
| 120 |
| 121 void FUNCTION_NAME(TlsSocket_ProcessBuffer)(Dart_NativeArguments args) { |
| 122 Dart_EnterScope(); |
| 123 Dart_Handle buffer_id_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 124 int64_t buffer_id = DartUtils::GetIntegerValue(buffer_id_object); |
| 125 if (buffer_id < 0 || buffer_id >= TlsFilter::kNumBuffers) { |
| 126 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 127 "Illegal argument to ProcessBuffer")); |
| 128 } |
| 129 |
| 130 intptr_t bytes_read = |
| 131 GetTlsFilter(args)->ProcessBuffer(static_cast<int>(buffer_id)); |
| 132 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
| 133 Dart_ExitScope(); |
| 134 } |
| 135 |
| 136 |
| 137 void FUNCTION_NAME(TlsSocket_SetCertificateDatabase) |
| 138 (Dart_NativeArguments args) { |
| 139 Dart_EnterScope(); |
| 140 Dart_Handle dart_pkcert_dir = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
| 141 // Check that the type is string, and get the UTF-8 C string value from it. |
| 142 if (Dart_IsString(dart_pkcert_dir)) { |
| 143 const char* pkcert_dir = NULL; |
| 144 ThrowIfError(Dart_StringToCString(dart_pkcert_dir, &pkcert_dir)); |
| 145 TlsFilter::InitializeLibrary(pkcert_dir); |
| 146 } else { |
| 147 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 148 "Non-String argument to SetCertificateDatabase")); |
| 149 } |
| 150 Dart_ExitScope(); |
| 151 } |
| 152 |
| 153 |
| 154 void TlsFilter::Init(Dart_Handle dart_this) { |
| 155 stringStart_ = ThrowIfError( |
| 156 Dart_NewPersistentHandle(DartUtils::NewString("start"))); |
| 157 stringLength_ = ThrowIfError( |
| 158 Dart_NewPersistentHandle(DartUtils::NewString("length"))); |
| 159 |
| 160 InitializeBuffers(dart_this); |
| 161 memio_ = memio_CreateIOLayer(kMemioBufferSize); |
| 162 } |
| 163 |
| 164 |
| 165 void TlsFilter::InitializeBuffers(Dart_Handle dart_this) { |
| 166 // Create TlsFilter buffers as ExternalUint8Array objects. |
| 167 Dart_Handle dart_buffers_object = ThrowIfError( |
| 168 Dart_GetField(dart_this, DartUtils::NewString("buffers"))); |
| 169 Dart_Handle dart_buffer_object = |
| 170 Dart_ListGetAt(dart_buffers_object, kReadPlaintext); |
| 171 Dart_Handle tls_external_buffer_class = |
| 172 Dart_InstanceGetClass(dart_buffer_object); |
| 173 Dart_Handle dart_buffer_size = ThrowIfError( |
| 174 Dart_GetField(tls_external_buffer_class, DartUtils::NewString("SIZE"))); |
| 175 buffer_size_ = DartUtils::GetIntegerValue(dart_buffer_size); |
| 176 if (buffer_size_ <= 0 || buffer_size_ > 1024 * 1024) { |
| 177 Dart_ThrowException( |
| 178 DartUtils::NewString("Invalid buffer size in _TlsExternalBuffer")); |
| 179 } |
| 180 |
| 181 Dart_Handle data_identifier = DartUtils::NewString("data"); |
| 182 for (int i = 0; i < kNumBuffers; ++i) { |
| 183 dart_buffer_objects_[i] = ThrowIfError( |
| 184 Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i))); |
| 185 buffers_[i] = new uint8_t[buffer_size_]; |
| 186 Dart_Handle data = ThrowIfError( |
| 187 Dart_NewExternalByteArray(buffers_[i], buffer_size_, NULL, NULL)); |
| 188 ThrowIfError(Dart_SetField(dart_buffer_objects_[i], |
| 189 data_identifier, |
| 190 data)); |
| 191 } |
| 192 } |
| 193 |
| 194 |
| 195 void TlsFilter::RegisterHandshakeCallbacks(Dart_Handle start, |
| 196 Dart_Handle finish) { |
| 197 ASSERT(NULL == handshake_start_); |
| 198 ASSERT(NULL == handshake_finish_); |
| 199 handshake_start_ = ThrowIfError(Dart_NewPersistentHandle(start)); |
| 200 handshake_finish_ = ThrowIfError(Dart_NewPersistentHandle(finish)); |
| 201 } |
| 202 |
| 203 |
| 204 void TlsFilter::InitializeLibrary(const char* pkcert_database) { |
| 205 LockInitMutex(); |
| 206 if (!library_initialized_) { |
| 207 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 208 SECStatus status = NSS_Init(pkcert_database); |
| 209 if (status != SECSuccess) { |
| 210 ReportPRError("Unsuccessful NSS_Init call."); |
| 211 } |
| 212 |
| 213 status = NSS_SetDomesticPolicy(); |
| 214 if (status != SECSuccess) { |
| 215 ReportPRError("Unsuccessful NSS_SetDomesticPolicy call."); |
| 216 } |
| 217 } else { |
| 218 ReportError("Called TlsFilter::InitializeLibrary more than once"); |
| 219 } |
| 220 UnlockInitMutex(); |
| 221 } |
| 222 |
| 223 |
| 224 void TlsFilter::Connect(const char* host, int port) { |
| 225 if (in_handshake_) { |
| 226 ReportError("Connect called while already in handshake state."); |
| 227 } |
| 228 PRFileDesc* my_socket = memio_; |
| 229 |
| 230 my_socket = SSL_ImportFD(NULL, my_socket); |
| 231 if (my_socket == NULL) { |
| 232 ReportPRError("Unsuccessful SSL_ImportFD call"); |
| 233 } |
| 234 |
| 235 if (SSL_SetURL(my_socket, host) == -1) { |
| 236 ReportPRError("Unsuccessful SetURL call"); |
| 237 } |
| 238 |
| 239 SECStatus status = SSL_ResetHandshake(my_socket, PR_FALSE); |
| 240 if (status != SECSuccess) { |
| 241 ReportPRError("Unsuccessful SSL_ResetHandshake call"); |
| 242 } |
| 243 |
| 244 // SetPeerAddress |
| 245 PRNetAddr host_address; |
| 246 char host_entry_buffer[PR_NETDB_BUF_SIZE]; |
| 247 PRHostEnt host_entry; |
| 248 PRStatus rv = PR_GetHostByName(host, host_entry_buffer, |
| 249 PR_NETDB_BUF_SIZE, &host_entry); |
| 250 if (rv != PR_SUCCESS) { |
| 251 ReportPRError("Unsuccessful PR_GetHostByName call"); |
| 252 } |
| 253 |
| 254 int index = PR_EnumerateHostEnt(0, &host_entry, port, &host_address); |
| 255 if (index == -1 || index == 0) { |
| 256 ReportPRError("Unsuccessful PR_EnumerateHostEnt call"); |
| 257 } |
| 258 |
| 259 memio_SetPeerName(my_socket, &host_address); |
| 260 memio_ = my_socket; |
| 261 } |
| 262 |
| 263 |
| 264 void TlsFilter::Handshake() { |
| 265 SECStatus status = SSL_ForceHandshake(memio_); |
| 266 if (status == SECSuccess) { |
| 267 if (in_handshake_) { |
| 268 ThrowIfError(Dart_InvokeClosure(handshake_finish_, 0, NULL)); |
| 269 in_handshake_ = false; |
| 270 } |
| 271 } else { |
| 272 PRErrorCode error = PR_GetError(); |
| 273 if (error == PR_WOULD_BLOCK_ERROR) { |
| 274 if (!in_handshake_) { |
| 275 ThrowIfError(Dart_InvokeClosure(handshake_start_, 0, NULL)); |
| 276 in_handshake_ = true; |
| 277 } |
| 278 } else { |
| 279 ReportPRError("Unexpected handshake error"); |
| 280 } |
| 281 } |
| 282 } |
| 283 |
| 284 |
| 285 void TlsFilter::Destroy() { |
| 286 for (int i = 0; i < kNumBuffers; ++i) { |
| 287 Dart_DeletePersistentHandle(dart_buffer_objects_[i]); |
| 288 } |
| 289 Dart_DeletePersistentHandle(stringStart_); |
| 290 Dart_DeletePersistentHandle(stringLength_); |
| 291 Dart_DeletePersistentHandle(handshake_start_); |
| 292 Dart_DeletePersistentHandle(handshake_finish_); |
| 293 // TODO(whesse): Free NSS objects here. |
| 294 } |
| 295 |
| 296 |
| 297 intptr_t TlsFilter::ProcessBuffer(int buffer_index) { |
| 298 Dart_Handle buffer_object = dart_buffer_objects_[buffer_index]; |
| 299 Dart_Handle start_object = ThrowIfError( |
| 300 Dart_GetField(buffer_object, stringStart_)); |
| 301 Dart_Handle length_object = ThrowIfError( |
| 302 Dart_GetField(buffer_object, stringLength_)); |
| 303 int64_t unsafe_start = DartUtils::GetIntegerValue(start_object); |
| 304 int64_t unsafe_length = DartUtils::GetIntegerValue(length_object); |
| 305 ASSERT(unsafe_start >= 0); |
| 306 ASSERT(unsafe_start < buffer_size_); |
| 307 ASSERT(unsafe_length >= 0); |
| 308 ASSERT(unsafe_length <= buffer_size_); |
| 309 intptr_t start = static_cast<intptr_t>(unsafe_start); |
| 310 intptr_t length = static_cast<intptr_t>(unsafe_length); |
| 311 |
| 312 int bytes_processed = 0; |
| 313 switch (buffer_index) { |
| 314 case kReadPlaintext: { |
| 315 int bytes_free = buffer_size_ - start - length; |
| 316 bytes_processed = PR_Read(memio_, |
| 317 buffers_[buffer_index] + start + length, |
| 318 bytes_free); |
| 319 if (bytes_processed < 0) { |
| 320 ASSERT(bytes_processed == -1); |
| 321 // TODO(whesse): Handle unexpected errors here. |
| 322 PRErrorCode pr_error = PR_GetError(); |
| 323 if (PR_WOULD_BLOCK_ERROR != pr_error) { |
| 324 ReportPRError("Error reading plaintext from TlsFilter"); |
| 325 } |
| 326 bytes_processed = 0; |
| 327 } |
| 328 break; |
| 329 } |
| 330 |
| 331 case kWriteEncrypted: { |
| 332 const uint8_t* buf1; |
| 333 const uint8_t* buf2; |
| 334 unsigned int len1; |
| 335 unsigned int len2; |
| 336 int bytes_free = buffer_size_ - start - length; |
| 337 memio_Private* secret = memio_GetSecret(memio_); |
| 338 memio_GetWriteParams(secret, &buf1, &len1, &buf2, &len2); |
| 339 int bytes_to_send = |
| 340 dart::Utils::Minimum(len1, static_cast<unsigned>(bytes_free)); |
| 341 if (bytes_to_send > 0) { |
| 342 memmove(buffers_[buffer_index] + start + length, buf1, bytes_to_send); |
| 343 bytes_processed = bytes_to_send; |
| 344 } |
| 345 bytes_to_send = dart::Utils::Minimum(len2, |
| 346 static_cast<unsigned>(bytes_free - bytes_processed)); |
| 347 if (bytes_to_send > 0) { |
| 348 memmove(buffers_[buffer_index] + start + length + bytes_processed, buf2, |
| 349 bytes_to_send); |
| 350 bytes_processed += bytes_to_send; |
| 351 } |
| 352 if (bytes_processed > 0) { |
| 353 memio_PutWriteResult(secret, bytes_processed); |
| 354 } |
| 355 break; |
| 356 } |
| 357 |
| 358 case kReadEncrypted: { |
| 359 if (length > 0) { |
| 360 bytes_processed = length; |
| 361 memio_Private* secret = memio_GetSecret(memio_); |
| 362 uint8_t* memio_buf; |
| 363 int free_bytes = memio_GetReadParams(secret, &memio_buf); |
| 364 if (free_bytes < bytes_processed) bytes_processed = free_bytes; |
| 365 memmove(memio_buf, |
| 366 buffers_[buffer_index] + start, |
| 367 bytes_processed); |
| 368 memio_PutReadResult(secret, bytes_processed); |
| 369 } |
| 370 break; |
| 371 } |
| 372 |
| 373 case kWritePlaintext: { |
| 374 if (length > 0) { |
| 375 bytes_processed = PR_Write(memio_, |
| 376 buffers_[buffer_index] + start, |
| 377 length); |
| 378 } |
| 379 |
| 380 if (bytes_processed < 0) { |
| 381 ASSERT(bytes_processed == -1); |
| 382 // TODO(whesse): Handle unexpected errors here. |
| 383 PRErrorCode pr_error = PR_GetError(); |
| 384 if (PR_WOULD_BLOCK_ERROR != pr_error) { |
| 385 ReportPRError("Error reading plaintext from TlsFilter"); |
| 386 } |
| 387 bytes_processed = 0; |
| 388 } |
| 389 break; |
| 390 } |
| 391 } |
| 392 return bytes_processed; |
| 393 } |
OLD | NEW |