Chromium Code Reviews| Index: runtime/bin/tls_socket.cc |
| diff --git a/runtime/bin/tls_socket.cc b/runtime/bin/tls_socket.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c28250f5610acf790783a757da559fe9e5fb95c |
| --- /dev/null |
| +++ b/runtime/bin/tls_socket.cc |
| @@ -0,0 +1,397 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +#include "bin/tls_socket.h" |
| + |
| +#include <errno.h> |
| +#include <fcntl.h> |
| +#include <sys/stat.h> |
| +#include <stdio.h> |
| +#include <string.h> |
| + |
| +#include <prinit.h> |
| +#include <prerror.h> |
| +#include <prnetdb.h> |
| +#include <nss.h> |
| +#include <ssl.h> |
| + |
| +#include "bin/builtin.h" |
| +#include "bin/dartutils.h" |
| +#include "bin/thread.h" |
| +#include "bin/utils.h" |
| +#include "bin/net/nss_memio.h" |
| +#include "platform/utils.h" |
| + |
| +#include "include/dart_api.h" |
| + |
| +bool TlsFilter::library_initialized_ = false; |
| +static const int kTlsFilterNativeFieldIndex = 0; |
| + |
| +static TlsFilter* GetTlsFilter(Dart_NativeArguments args) { |
| + TlsFilter* filter; |
| + |
| + Dart_Handle dart_this = HandleError(Dart_GetNativeArgument(args, 0)); |
| + ASSERT(Dart_IsInstance(dart_this)); |
| + HandleError(Dart_GetNativeInstanceField(dart_this, |
| + kTlsFilterNativeFieldIndex, |
| + reinterpret_cast<intptr_t*>(&filter))); |
| + return filter; |
| +} |
| + |
| + |
| +static void SetTlsFilter(Dart_NativeArguments args, TlsFilter* filter) { |
| + Dart_Handle dart_this = HandleError(Dart_GetNativeArgument(args, 0)); |
| + ASSERT(Dart_IsInstance(dart_this)); |
| + HandleError(Dart_SetNativeInstanceField(dart_this, |
| + kTlsFilterNativeFieldIndex, |
| + reinterpret_cast<intptr_t>(filter))); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_Init)(Dart_NativeArguments args) { |
|
Søren Gjesse
2012/11/01 11:49:11
Maybe assert that filter is not already set.
Bill Hesse
2012/11/11 22:34:34
This is not easy. Filter is set by putting it in
|
| + Dart_EnterScope(); |
| + TlsFilter* filter = new TlsFilter; |
| + Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); |
| + if (Dart_IsError(dart_this)) { |
| + delete filter; |
| + Dart_PropagateError(dart_this); |
| + } |
| + SetTlsFilter(args, filter); |
| + filter->Init(dart_this); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_Connect)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + GetTlsFilter(args)->Connect(); |
| + Dart_SetReturnValue(args, Dart_Null()); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_Destroy)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + TlsFilter* filter = GetTlsFilter(args); |
| + SetTlsFilter(args, NULL); |
| + filter->Destroy(); |
| + delete filter; |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_RegisterHandshakeCallbacks)( |
| + Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + Dart_Handle handshake_start = HandleError(Dart_GetNativeArgument(args, 1)); |
| + Dart_Handle handshake_finish = HandleError(Dart_GetNativeArgument(args, 2)); |
| + if (!Dart_IsClosure(handshake_start) || |
| + !Dart_IsClosure(handshake_finish)) { |
| + Dart_ThrowException(DartUtils::NewDartArgumentError( |
| + "Illegal argument to RegisterHandshakeCallbacks")); |
| + } |
| + GetTlsFilter(args)->RegisterHandshakeCallbacks(handshake_start, |
| + handshake_finish); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_ProcessBuffer)(Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + Dart_Handle buffer_id_object = HandleError(Dart_GetNativeArgument(args, 1)); |
| + int64_t buffer_id = DartUtils::GetIntegerValue(buffer_id_object); |
| + if (buffer_id < 0 || buffer_id >= TlsFilter::kNumBuffers) { |
| + Dart_ThrowException(DartUtils::NewDartArgumentError( |
| + "Illegal argument to ProcessBuffer")); |
| + } |
| + |
| + intptr_t bytes_read = |
| + GetTlsFilter(args)->ProcessBuffer(static_cast<int>(buffer_id)); |
| + Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
| + Dart_ExitScope(); |
| +} |
| + |
| + |
| +void FUNCTION_NAME(TlsSocket_SetCertificateDatabase) |
| + (Dart_NativeArguments args) { |
| + Dart_EnterScope(); |
| + Dart_Handle dart_pkcert_dir = HandleError(Dart_GetNativeArgument(args, 0)); |
| + // Check that the type is string, and get the UTF-8 C string value from it. |
| + if (Dart_IsString(dart_pkcert_dir)) { |
| + const char* pkcert_dir = NULL; |
| + HandleError(Dart_StringToCString(dart_pkcert_dir, &pkcert_dir)); |
| + TlsFilter::InitializeLibrary(pkcert_dir); |
| + } else { |
| + Dart_ThrowException(DartUtils::NewDartArgumentError( |
| + "Non-String argument to SetCertificateDatabase")); |
| + } |
| + Dart_ExitScope(); |
| +} |
| + |
| +void TlsFilter::Init(Dart_Handle dart_this) { |
| + stringStart_ = HandleError( |
| + Dart_NewPersistentHandle(Dart_NewString("start"))); |
| + stringLength_ = HandleError( |
| + Dart_NewPersistentHandle(Dart_NewString("length"))); |
| + |
| + InitializeBuffers(dart_this); |
| + InitializePlatformData(); |
| +} |
| + |
| + |
| +void TlsFilter::InitializeBuffers(Dart_Handle dart_this) { |
| + // Create TlsFilter buffers as ExternalUint8Array objects. |
| + Dart_Handle dart_buffers_object = HandleError( |
| + Dart_GetField(dart_this, Dart_NewString("buffers"))); |
| + Dart_Handle dart_buffer_object = HandleError( |
| + Dart_ListGetAt(dart_buffers_object, kReadPlaintext)); |
| + Dart_Handle tls_external_buffer_class = HandleError( |
| + Dart_InstanceGetClass(dart_buffer_object)); |
| + Dart_Handle dart_buffer_size = HandleError( |
| + Dart_GetField(tls_external_buffer_class, Dart_NewString("kSize"))); |
| + buffer_size_ = DartUtils::GetIntegerValue(dart_buffer_size); |
| + if (buffer_size_ <= 0 || buffer_size_ > 1024 * 1024) { |
| + Dart_ThrowException( |
| + Dart_NewString("Invalid buffer size in _TlsExternalBuffer")); |
| + } |
| + |
| + for (int i = 0; i < kNumBuffers; ++i) { |
| + dart_buffer_objects_[i] = HandleError( |
| + Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i))); |
| + buffers_[i] = new uint8_t[buffer_size_]; |
| + Dart_Handle data = HandleError( |
| + Dart_NewExternalByteArray(buffers_[i], |
| + buffer_size_, NULL, NULL)); |
| + HandleError( |
| + Dart_SetField(dart_buffer_objects_[i], Dart_NewString("data"), data)); |
| + } |
| +} |
| + |
| + |
| +void TlsFilter::RegisterHandshakeCallbacks(Dart_Handle start, |
| + Dart_Handle finish) { |
| + handshake_start_ = HandleError(Dart_NewPersistentHandle(start)); |
| + handshake_finish_ = HandleError(Dart_NewPersistentHandle(finish)); |
| +} |
| + |
| + |
| +void TlsFilter::DestroyPlatformIndependent() { |
| + for (int i = 0; i < kNumBuffers; ++i) { |
| + Dart_DeletePersistentHandle(dart_buffer_objects_[i]); |
| + } |
| + Dart_DeletePersistentHandle(stringStart_); |
| + Dart_DeletePersistentHandle(stringLength_); |
| +} |
| + |
| + |
| +class TlsFilterPlatformData { |
| + public: |
| + PRFileDesc* memio; |
| +}; |
| + |
| + |
| +TlsFilter::TlsFilter() : in_handshake_(false) { } |
| + |
| + |
| +void TlsFilter::InitializeLibrary(const char* pkcert_database) { |
| + TlsLog("Entering InitializeLibrary"); |
| + LockInitMutex(); |
| + if (!library_initialized_) { |
| + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| + // TODO: Allow read-write opening of pkcert_database using |
| + // NSS_InitReadWrite, controlled by an optional argument. |
| + SECStatus status = |
| + NSS_Init(pkcert_database); |
| + if (status != SECSuccess) { |
| + ReportError("Unsuccessful NSS_Init call.\n", PR_GetError()); |
| + } |
| + |
| + status = NSS_SetDomesticPolicy(); |
| + if (status != SECSuccess) { |
| + ReportError("Unsuccessful NSS_SetDomesticPolicy call.\n", PR_GetError()); |
| + } |
| + } else { |
| + ReportError("Called TlsFilter::InitializeLibrary more than once", 0); |
| + } |
| + UnlockInitMutex(); |
| + TlsLog("Exiting InitializeLibrary"); |
| +} |
| + |
| + |
| +void TlsFilter::InitializePlatformData() { |
| + data_ = new TlsFilterPlatformData; |
| + data_->memio = memio_CreateIOLayer(30000); |
| +} |
| + |
| + |
| +char host_entry_buffer[PR_NETDB_BUF_SIZE]; |
| +PRHostEnt host_entry; |
| +PRNetAddr host_address; |
| +PRFileDesc* my_socket; |
| + |
| +bool SECURITY = true; |
| +SECStatus status; |
| + |
| +void TlsFilter::Connect() { |
| + TlsLog("Entering InitSocket"); |
| + if (!in_handshake_) { |
| + my_socket = data_->memio; |
| + |
| + my_socket = SSL_ImportFD(NULL, my_socket); |
| + if (my_socket == NULL) { |
| + ReportError("Unsuccessful SSL_ImportFD call", 0); |
| + } |
| + |
| + if (SSL_SetURL(my_socket, "www.google.dk") == -1) { |
| + ReportError("Unsuccessful SetURL call", 0); |
| + } |
| + |
| + status = SSL_ResetHandshake(my_socket, PR_FALSE); |
| + if (status != SECSuccess) { |
| + ReportError("Unsuccessful SSL_ResetHandshake call", PR_GetError()); |
| + } |
| + |
| + // SetPeerAddress |
| + PRNetAddr host_address; |
| + char host_entry_buffer[PR_NETDB_BUF_SIZE]; |
| + PRHostEnt host_entry; |
| + PRStatus rv = PR_GetHostByName("www.google.dk", host_entry_buffer, |
| + PR_NETDB_BUF_SIZE, &host_entry); |
| + if (rv != PR_SUCCESS) { |
| + ReportError("Unsuccessful PR_GetHostByName call", PR_GetError()); |
| + } |
| + |
| + int index = PR_EnumerateHostEnt(0, &host_entry, 443, &host_address); |
| + if (index == -1 || index == 0) { |
| + ReportError("Unsuccessful PR_EnumerateHostEnt call", 0); |
| + } |
| + if (host_address.inet.family == PR_AF_INET) { |
|
Søren Gjesse
2012/11/01 11:49:11
Debug code.
|
| + TlsLog("Got IP V4 address"); |
| + } |
| + |
| + memio_SetPeerName(my_socket, &host_address); |
| + |
| + data_->memio = my_socket; |
| + } |
|
Søren Gjesse
2012/11/01 11:49:11
Indentation.
|
| + |
| + // Handshake |
| + status = SSL_ForceHandshake(my_socket); |
| + if (status == SECSuccess) { |
| + TlsLog("Successful SSL_ForceHandshake call"); |
|
Søren Gjesse
2012/11/01 11:49:11
Please remove logging.
Bill Hesse
2012/11/11 22:34:34
Removed - it is in a separate private patch, becau
|
| + if (in_handshake_) { |
| + TlsLog("Exiting handshake state"); |
| + HandleError(Dart_InvokeClosure(handshake_finish_, 0, NULL)); |
| + in_handshake_ = false; |
| + } |
| + } else { |
| + TlsLogInt("Unsuccessful SSL_ForceHandshake call", PR_GetError()); |
| + if (!in_handshake_) { |
| + TlsLog("Entering handshake state"); |
| + // int writable = ProcessBuffer(kWriteEncrypted); |
|
Søren Gjesse
2012/11/01 11:49:11
Code in comments.
|
| + // TlsLog("ProcessBuffer(kWriteEncrypted) returns %d\n", writable); |
| + HandleError(Dart_InvokeClosure(handshake_start_, 0, NULL)); |
| + in_handshake_ = true; |
| + } |
| + } |
| +} |
| + |
| + |
| +void TlsFilter::Destroy() { |
| + DestroyPlatformIndependent(); |
| + // TODO(whesse): Destroy OpenSSL objects here. |
| +} |
| + |
| +intptr_t TlsFilter::ProcessBuffer(int buffer_index) { |
| + TlsLogInt("Entering processBuffer with index", buffer_index); |
| + Dart_Handle buffer_object = dart_buffer_objects_[buffer_index]; |
| + Dart_Handle start_object = HandleError( |
| + Dart_GetField(buffer_object, stringStart_)); |
| + Dart_Handle length_object = HandleError( |
| + Dart_GetField(buffer_object, stringLength_)); |
| + int64_t unsafe_start = DartUtils::GetIntegerValue(start_object); |
|
Søren Gjesse
2012/11/01 11:49:11
Maybe change the type to intptr_t here and do the
Bill Hesse
2012/11/11 22:34:34
I prefer doing the range check on unsafe_length be
|
| + int64_t unsafe_length = DartUtils::GetIntegerValue(length_object); |
| + if (unsafe_start < 0 || unsafe_start >= buffer_size_ || |
| + unsafe_length < 0 || unsafe_length > buffer_size_) { |
| + Dart_ThrowException(DartUtils::NewDartArgumentError( |
| + "Illegal .start or .length on a _TlsExternalBuffer")); |
| + } |
| + intptr_t start = static_cast<intptr_t>(unsafe_start); |
| + intptr_t length = static_cast<intptr_t>(unsafe_length); |
| + |
| + int bytes_sent = 0; |
|
Søren Gjesse
2012/11/01 11:49:11
Maybe rename bytes_sent to bytes_processed, as thi
Bill Hesse
2012/11/11 22:34:34
Done.
|
| + switch (buffer_index) { |
| + case kReadPlaintext: { |
| + int bytes_free = buffer_size_ - start - length; |
| + bytes_sent = PR_Read(data_->memio, |
| + buffers_[buffer_index] + start + length, |
| + bytes_free); |
| + TlsLogInt("plaintext read Dart buffer bytes free", bytes_free); |
| + TlsLogInt("plaintext bytes read from filter", bytes_sent); |
| + if (bytes_sent < 0) bytes_sent = 0; |
| + break; |
| + } |
| + |
| + case kWriteEncrypted: { |
| + const uint8_t* buf1; |
| + const uint8_t* buf2; |
| + unsigned int len1; |
| + unsigned int len2; |
| + int bytes_free = buffer_size_ - start - length; |
| + memio_Private* secret = memio_GetSecret(data_->memio); |
| + memio_GetWriteParams(secret, &buf1, &len1, &buf2, &len2); |
| + int bytes_to_send = |
| + dart::Utils::Minimum(len1, static_cast<unsigned>(bytes_free)); |
| + if (bytes_to_send > 0) { |
| + memmove(buffers_[buffer_index] + start + length, buf1, bytes_to_send); |
| + bytes_sent = bytes_to_send; |
| + } |
| + bytes_to_send = dart::Utils::Minimum(len2, |
| + static_cast<unsigned>(bytes_free - bytes_sent)); |
| + if (bytes_to_send > 0) { |
| + memmove(buffers_[buffer_index] + start + length + bytes_sent, buf2, |
| + bytes_to_send); |
| + bytes_sent += bytes_to_send; |
| + } |
| + TlsLogInt("encrypted write Dart buffer bytes free", bytes_free); |
| + TlsLogInt("encrypted bytes written from filter", bytes_sent); |
| + if (bytes_sent > 0) { |
| + memio_PutWriteResult(secret, bytes_sent); |
| + } |
| + break; |
| + } |
| + |
| + case kReadEncrypted: { |
| + if (length > 0) { |
| + bytes_sent = length; |
| + // NEW |
|
Søren Gjesse
2012/11/01 11:49:11
Strange comment.
Bill Hesse
2012/11/11 22:34:34
Done.
|
| + memio_Private* secret = memio_GetSecret(data_->memio); |
| + uint8_t* memio_buf; |
| + int free_bytes = memio_GetReadParams(secret, &memio_buf); |
| + if (free_bytes < bytes_sent) bytes_sent = free_bytes; |
| + TlsLogInt("encrypted read Dart buffer bytes available", length); |
| + TlsLogInt("encrypted bytes read to filter", bytes_sent); |
| + memmove(memio_buf, |
| + buffers_[buffer_index] + start, |
| + bytes_sent); |
| + memio_PutReadResult(secret, bytes_sent); |
| + } |
| + break; |
| + } |
| + |
| + case kWritePlaintext: { |
| + if (length > 0) { |
| + bytes_sent = PR_Write(data_->memio, |
| + buffers_[buffer_index] + start, |
| + length); |
| + } |
| + TlsLogInt("plaintext write Dart buffer bytes available", length); |
| + TlsLogInt("plaintext bytes written to filter", bytes_sent); |
| + |
| + if (bytes_sent < 0) bytes_sent = 0; |
|
Søren Gjesse
2012/11/01 11:49:11
Negative bytes_sent here is not an error?
Bill Hesse
2012/11/11 22:34:34
We should only normally get a blocking error due t
|
| + break; |
| + } |
| + } |
| + TlsLog("Exiting processBuffer"); |
| + return bytes_sent; |
| +} |