Chromium Code Reviews| Index: runtime/bin/tls_socket_linux.cc |
| diff --git a/runtime/bin/tls_socket_linux.cc b/runtime/bin/tls_socket_linux.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cd54d1a22c6adbd4b0e1d2a769f3a27e7ff15d07 |
| --- /dev/null |
| +++ b/runtime/bin/tls_socket_linux.cc |
| @@ -0,0 +1,144 @@ |
| +// 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 <unistd.h> |
| +#include <libgen.h> |
| +#include <stdio.h> |
| + |
| +#include <openssl/ssl.h> |
| +#include <openssl/bio.h> |
| +#include <openssl/err.h> |
| + |
| +#include "bin/builtin.h" |
| + |
| +static intptr_t Int64ToIntptr(int64_t x) { |
| + if (x < kIntptrMin || x > kIntptrMax) { |
| + Dart_ThrowException(DartUtils::NewDartIllegalArgumentError( |
| + "Out-of-range cast from int64_t to intptr_t")); |
| + } |
| + return static_cast<intptr_t>(x); |
| +} |
| + |
|
Søren Gjesse
2012/07/31 09:07:15
Two empty lines between class declarations and bet
Bill Hesse
2012/08/08 17:00:05
Done.
|
| +class TlsFilterPlatformData { |
| + public: |
| + SSL* ssl; |
| + SSL_CTX* ssl_ctx; |
| + BIO* encrypted; |
| + BIO* plaintext; |
| + BIO* internal; |
| +}; |
| + |
| +TlsFilter::TlsFilter() { |
| + printf("Entering TlsFilter constructor\n"); |
|
Mads Ager (google)
2012/07/31 10:16:01
Remove all debug printing.
Bill Hesse
2012/08/08 17:00:05
Done.
|
| + LockInitMutex(); |
| + if (!library_initialized_) { |
| + InitializeLibrary(); |
| + library_initialized_ = true; |
| + } |
| + UnlockInitMutex(); |
| + in_handshake = false; |
| + printf("Exiting TlsFilter constructor\n"); |
| +} |
| + |
| +void TlsFilter::InitializeLibrary() { |
| + printf("Entering TlsFilter::InitializeLibrary\n"); |
| + SSL_library_init(); |
| + SSL_load_error_strings(); |
| + OpenSSL_add_all_algorithms(); |
| + printf("Exiting TlsFilter::InitializeLibrary\n"); |
| +} |
| + |
| +void TlsFilter::InitializePlatformData() { |
| + data = new TlsFilterPlatformData; |
| + data->ssl_ctx = SSL_CTX_new(SSLv23_client_method()); |
| + BIO_new_bio_pair(&(data->internal), 0, &(data->encrypted), 0); |
| + data->plaintext = BIO_new_ssl(data->ssl_ctx, 1); |
| + // data->plaintext = BIO_new(BIO_f_null()); |
| + // data->plaintext = BIO_new(BIO_f_buffer()); |
| + data->plaintext = BIO_push(data->plaintext, data->internal); |
| +} |
| + |
| +void TlsFilter::Connect() { |
| + printf("Entering Connect\n"); |
| + // BIO_do_handshake(data->plaintext); |
| + int result = BIO_do_handshake(data->plaintext); |
| + printf("BIO_do_handshake result: %d\n", result); |
| + if (result == 1) { |
| + if (in_handshake) { |
| + HandleError(Dart_InvokeClosure(handshake_finish, 0, NULL)); |
| + in_handshake = false; |
| + } |
| + } else if (BIO_should_retry(data->plaintext)) { |
| + if (!in_handshake) { |
| + HandleError(Dart_InvokeClosure(handshake_start, 0, NULL)); |
| + in_handshake = true; |
| + } |
| + } |
| + printf("Exiting Connect\n"); |
| +} |
| + |
| +void TlsFilter::Destroy() { |
| + printf("Entering Destroy\n"); |
| + printf("Exiting Destroy\n"); |
| +} |
| + |
| +intptr_t TlsFilter::ProcessBuffer(int buffer_index) { |
| + printf(" Entering %s\n", bufferNames[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)); |
| + intptr_t start = Int64ToIntptr(DartUtils::GetIntegerValue(start_object)); |
| + intptr_t length = Int64ToIntptr(DartUtils::GetIntegerValue(length_object)); |
| + if (start < 0 || start >= buffer_size || |
| + length < 0 || length > buffer_size) { |
| + Dart_ThrowException(DartUtils::NewDartIllegalArgumentError( |
| + "Illegal .start or .length on a _TlsExternalBuffer")); |
| + } |
| + |
| + BIO* bio_stream; |
| + if (buffer_index == kReadEncrypted || buffer_index == kWriteEncrypted) { |
| + bio_stream = data->encrypted; |
| + } else { |
| + bio_stream = data->plaintext; |
| + } |
| + |
| + intptr_t bytes_processed = 0; |
| + int bio_bytes = 0; |
| + switch (buffer_index) { |
| + case kReadPlaintext: |
| + case kWriteEncrypted: { |
| + // Write from the BIO to the buffer. |
| + intptr_t bytes_free = buffer_size - start - length; |
| + if (bytes_free > 0) { |
| + bio_bytes = BIO_read(bio_stream, |
|
Søren Gjesse
2012/07/31 09:07:15
Indentation of arguments.
|
| + buffers[buffer_index] + start + length, |
| + bytes_free); |
| + } |
| + break; |
| + } |
| + case kReadEncrypted: |
| + case kWritePlaintext: |
| + if (length > 0) { |
| + // Call BIO_write. |
| + bio_bytes = BIO_write(bio_stream, |
|
Søren Gjesse
2012/07/31 09:07:15
Ditto.
|
| + buffers[buffer_index] + start, |
| + length); |
| + BIO_flush(bio_stream); |
| + } |
| + break; |
| + } |
| + if (bio_bytes > 0) { |
| + bytes_processed = bio_bytes; |
| + } |
| + printf(" %d bytes processed to/from openssl.\n", bio_bytes); |
| + printf(" Exiting %s\n", bufferNames[buffer_index]); |
| + return bytes_processed; |
| +} |