Chromium Code Reviews| 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 dart::Mutex TlsFilter::mutex_; // To protect library initialization. | |
| 30 static const int kTlsFilterNativeFieldIndex = 0; | |
| 31 | |
| 32 static TlsFilter* GetTlsFilter(Dart_NativeArguments args) { | |
| 33 TlsFilter* filter; | |
| 34 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | |
| 35 ASSERT(Dart_IsInstance(dart_this)); | |
| 36 ThrowIfError(Dart_GetNativeInstanceField( | |
| 37 dart_this, | |
| 38 kTlsFilterNativeFieldIndex, | |
| 39 reinterpret_cast<intptr_t*>(&filter))); | |
| 40 return filter; | |
| 41 } | |
| 42 | |
| 43 | |
| 44 static void SetTlsFilter(Dart_NativeArguments args, TlsFilter* filter) { | |
| 45 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | |
| 46 ASSERT(Dart_IsInstance(dart_this)); | |
| 47 ThrowIfError(Dart_SetNativeInstanceField( | |
| 48 dart_this, | |
| 49 kTlsFilterNativeFieldIndex, | |
| 50 reinterpret_cast<intptr_t>(filter))); | |
| 51 } | |
| 52 | |
| 53 | |
| 54 void FUNCTION_NAME(TlsSocket_Init)(Dart_NativeArguments args) { | |
| 55 Dart_EnterScope(); | |
| 56 TlsFilter* filter = new TlsFilter; | |
|
Søren Gjesse
2012/11/14 08:18:59
Move the creation of TlsFilter down to just before
Bill Hesse
2012/11/14 13:33:30
Done.
| |
| 57 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); | |
| 58 if (Dart_IsError(dart_this)) { | |
| 59 delete filter; | |
| 60 Dart_PropagateError(dart_this); | |
| 61 } | |
| 62 SetTlsFilter(args, filter); | |
| 63 filter->Init(dart_this); | |
| 64 Dart_ExitScope(); | |
| 65 } | |
| 66 | |
| 67 | |
| 68 void FUNCTION_NAME(TlsSocket_Connect)(Dart_NativeArguments args) { | |
| 69 Dart_EnterScope(); | |
| 70 Dart_Handle host_name = ThrowIfError(Dart_GetNativeArgument(args, 1)); | |
| 71 Dart_Handle port_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); | |
| 72 | |
| 73 const char* host_name_string = NULL; | |
| 74 // TODO(whesse): Is truncating a Dart string containing \0 what we want? | |
| 75 ThrowIfError(Dart_StringToCString(host_name, &host_name_string)); | |
| 76 | |
| 77 int64_t port; | |
| 78 if (!DartUtils::GetInt64Value(port_object, &port) || | |
| 79 port < 0 || port > 65535) { | |
| 80 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 81 "Illegal port parameter in TlsSocket")); | |
| 82 } | |
| 83 | |
| 84 GetTlsFilter(args)->Connect(host_name_string, static_cast<int>(port)); | |
| 85 Dart_ExitScope(); | |
| 86 } | |
| 87 | |
| 88 | |
| 89 void FUNCTION_NAME(TlsSocket_Destroy)(Dart_NativeArguments args) { | |
| 90 Dart_EnterScope(); | |
| 91 TlsFilter* filter = GetTlsFilter(args); | |
| 92 SetTlsFilter(args, NULL); | |
| 93 filter->Destroy(); | |
| 94 delete filter; | |
| 95 Dart_ExitScope(); | |
| 96 } | |
| 97 | |
| 98 | |
| 99 void FUNCTION_NAME(TlsSocket_Handshake)(Dart_NativeArguments args) { | |
| 100 Dart_EnterScope(); | |
| 101 GetTlsFilter(args)->Handshake(); | |
| 102 Dart_ExitScope(); | |
| 103 } | |
| 104 | |
| 105 | |
| 106 void FUNCTION_NAME(TlsSocket_RegisterHandshakeCallbacks)( | |
| 107 Dart_NativeArguments args) { | |
| 108 Dart_EnterScope(); | |
| 109 Dart_Handle handshake_start = ThrowIfError(Dart_GetNativeArgument(args, 1)); | |
| 110 Dart_Handle handshake_finish = ThrowIfError(Dart_GetNativeArgument(args, 2)); | |
| 111 if (!Dart_IsClosure(handshake_start) || | |
| 112 !Dart_IsClosure(handshake_finish)) { | |
| 113 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 114 "Illegal argument to RegisterHandshakeCallbacks")); | |
| 115 } | |
| 116 GetTlsFilter(args)->RegisterHandshakeCallbacks(handshake_start, | |
| 117 handshake_finish); | |
| 118 Dart_ExitScope(); | |
| 119 } | |
| 120 | |
| 121 | |
| 122 void FUNCTION_NAME(TlsSocket_ProcessBuffer)(Dart_NativeArguments args) { | |
| 123 Dart_EnterScope(); | |
| 124 Dart_Handle buffer_id_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); | |
| 125 int64_t buffer_id = DartUtils::GetIntegerValue(buffer_id_object); | |
| 126 if (buffer_id < 0 || buffer_id >= TlsFilter::kNumBuffers) { | |
| 127 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 128 "Illegal argument to ProcessBuffer")); | |
| 129 } | |
| 130 | |
| 131 intptr_t bytes_read = | |
| 132 GetTlsFilter(args)->ProcessBuffer(static_cast<int>(buffer_id)); | |
| 133 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | |
| 134 Dart_ExitScope(); | |
| 135 } | |
| 136 | |
| 137 | |
| 138 void FUNCTION_NAME(TlsSocket_SetCertificateDatabase) | |
| 139 (Dart_NativeArguments args) { | |
| 140 Dart_EnterScope(); | |
| 141 Dart_Handle dart_pkcert_dir = ThrowIfError(Dart_GetNativeArgument(args, 0)); | |
| 142 // Check that the type is string, and get the UTF-8 C string value from it. | |
| 143 if (Dart_IsString(dart_pkcert_dir)) { | |
| 144 const char* pkcert_dir = NULL; | |
| 145 ThrowIfError(Dart_StringToCString(dart_pkcert_dir, &pkcert_dir)); | |
| 146 TlsFilter::InitializeLibrary(pkcert_dir); | |
| 147 } else { | |
| 148 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 149 "Non-String argument to SetCertificateDatabase")); | |
| 150 } | |
| 151 Dart_ExitScope(); | |
| 152 } | |
| 153 | |
| 154 | |
| 155 void TlsFilter::Init(Dart_Handle dart_this) { | |
| 156 string_start_ = ThrowIfError( | |
| 157 Dart_NewPersistentHandle(DartUtils::NewString("start"))); | |
| 158 string_length_ = ThrowIfError( | |
| 159 Dart_NewPersistentHandle(DartUtils::NewString("length"))); | |
| 160 | |
| 161 InitializeBuffers(dart_this); | |
| 162 memio_ = memio_CreateIOLayer(kMemioBufferSize); | |
| 163 } | |
| 164 | |
| 165 | |
| 166 void TlsFilter::InitializeBuffers(Dart_Handle dart_this) { | |
| 167 // Create TlsFilter buffers as ExternalUint8Array objects. | |
| 168 Dart_Handle dart_buffers_object = ThrowIfError( | |
| 169 Dart_GetField(dart_this, DartUtils::NewString("buffers"))); | |
| 170 Dart_Handle dart_buffer_object = | |
| 171 Dart_ListGetAt(dart_buffers_object, kReadPlaintext); | |
| 172 Dart_Handle tls_external_buffer_class = | |
| 173 Dart_InstanceGetClass(dart_buffer_object); | |
| 174 Dart_Handle dart_buffer_size = ThrowIfError( | |
| 175 Dart_GetField(tls_external_buffer_class, DartUtils::NewString("SIZE"))); | |
| 176 buffer_size_ = DartUtils::GetIntegerValue(dart_buffer_size); | |
| 177 if (buffer_size_ <= 0 || buffer_size_ > 1024 * 1024) { | |
| 178 Dart_ThrowException( | |
| 179 DartUtils::NewString("Invalid buffer size in _TlsExternalBuffer")); | |
| 180 } | |
| 181 | |
| 182 Dart_Handle data_identifier = DartUtils::NewString("data"); | |
| 183 for (int i = 0; i < kNumBuffers; ++i) { | |
| 184 dart_buffer_objects_[i] = ThrowIfError( | |
| 185 Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i))); | |
| 186 buffers_[i] = new uint8_t[buffer_size_]; | |
| 187 Dart_Handle data = ThrowIfError( | |
| 188 Dart_NewExternalByteArray(buffers_[i], buffer_size_, NULL, NULL)); | |
|
Søren Gjesse
2012/11/14 08:18:59
You need to specify a peer pointer and callback he
Bill Hesse
2012/11/14 13:33:30
We are freeing the buffers manually in TlsFilter::
Søren Gjesse
2012/11/14 13:43:12
Even though we are freeing the persistent handles
Bill Hesse
2012/11/14 16:06:46
OK. Will change.
| |
| 189 ThrowIfError(Dart_SetField(dart_buffer_objects_[i], | |
| 190 data_identifier, | |
| 191 data)); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 | |
| 196 void TlsFilter::RegisterHandshakeCallbacks(Dart_Handle start, | |
| 197 Dart_Handle finish) { | |
| 198 ASSERT(NULL == handshake_start_); | |
| 199 ASSERT(NULL == handshake_finish_); | |
| 200 handshake_start_ = ThrowIfError(Dart_NewPersistentHandle(start)); | |
| 201 handshake_finish_ = ThrowIfError(Dart_NewPersistentHandle(finish)); | |
| 202 } | |
| 203 | |
| 204 | |
| 205 void TlsFilter::InitializeLibrary(const char* pkcert_database) { | |
| 206 MutexLocker locker(&mutex_); | |
| 207 if (!library_initialized_) { | |
| 208 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); | |
| 209 SECStatus status = NSS_Init(pkcert_database); | |
|
Søren Gjesse
2012/11/14 08:18:59
There might or might not be some UFT-8 issues here
Bill Hesse
2012/11/14 13:33:30
TODO added.
| |
| 210 if (status != SECSuccess) { | |
| 211 ReportPRError("Unsuccessful NSS_Init call."); | |
| 212 } | |
| 213 | |
| 214 status = NSS_SetDomesticPolicy(); | |
| 215 if (status != SECSuccess) { | |
| 216 ReportPRError("Unsuccessful NSS_SetDomesticPolicy call."); | |
| 217 } | |
| 218 } else { | |
| 219 ReportError("Called TlsFilter::InitializeLibrary more than once"); | |
| 220 } | |
| 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(string_start_); | |
| 290 Dart_DeletePersistentHandle(string_length_); | |
| 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, string_start_)); | |
| 301 Dart_Handle length_object = ThrowIfError( | |
| 302 Dart_GetField(buffer_object, string_length_)); | |
| 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); | |
|
Søren Gjesse
2012/11/14 08:18:59
Maybe add local variable
uint8_t* buffer = buffer
Bill Hesse
2012/11/14 13:33:30
Done.
| |
| 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); | |
|
Søren Gjesse
2012/11/14 08:18:59
As the buffers are circular shouldn't we try to fi
| |
| 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 |