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 <unistd.h> | |
| 11 #include <libgen.h> | |
| 12 #include <stdio.h> | |
| 13 | |
| 14 #include <openssl/ssl.h> | |
| 15 #include <openssl/bio.h> | |
| 16 #include <openssl/err.h> | |
| 17 | |
| 18 #include "bin/builtin.h" | |
| 19 | |
| 20 static intptr_t Int64ToIntptr(int64_t x) { | |
| 21 if (x < kIntptrMin || x > kIntptrMax) { | |
| 22 Dart_ThrowException(DartUtils::NewDartIllegalArgumentError( | |
| 23 "Out-of-range cast from int64_t to intptr_t")); | |
| 24 } | |
| 25 return static_cast<intptr_t>(x); | |
| 26 } | |
| 27 | |
|
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.
| |
| 28 class TlsFilterPlatformData { | |
| 29 public: | |
| 30 SSL* ssl; | |
| 31 SSL_CTX* ssl_ctx; | |
| 32 BIO* encrypted; | |
| 33 BIO* plaintext; | |
| 34 BIO* internal; | |
| 35 }; | |
| 36 | |
| 37 TlsFilter::TlsFilter() { | |
| 38 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.
| |
| 39 LockInitMutex(); | |
| 40 if (!library_initialized_) { | |
| 41 InitializeLibrary(); | |
| 42 library_initialized_ = true; | |
| 43 } | |
| 44 UnlockInitMutex(); | |
| 45 in_handshake = false; | |
| 46 printf("Exiting TlsFilter constructor\n"); | |
| 47 } | |
| 48 | |
| 49 void TlsFilter::InitializeLibrary() { | |
| 50 printf("Entering TlsFilter::InitializeLibrary\n"); | |
| 51 SSL_library_init(); | |
| 52 SSL_load_error_strings(); | |
| 53 OpenSSL_add_all_algorithms(); | |
| 54 printf("Exiting TlsFilter::InitializeLibrary\n"); | |
| 55 } | |
| 56 | |
| 57 void TlsFilter::InitializePlatformData() { | |
| 58 data = new TlsFilterPlatformData; | |
| 59 data->ssl_ctx = SSL_CTX_new(SSLv23_client_method()); | |
| 60 BIO_new_bio_pair(&(data->internal), 0, &(data->encrypted), 0); | |
| 61 data->plaintext = BIO_new_ssl(data->ssl_ctx, 1); | |
| 62 // data->plaintext = BIO_new(BIO_f_null()); | |
| 63 // data->plaintext = BIO_new(BIO_f_buffer()); | |
| 64 data->plaintext = BIO_push(data->plaintext, data->internal); | |
| 65 } | |
| 66 | |
| 67 void TlsFilter::Connect() { | |
| 68 printf("Entering Connect\n"); | |
| 69 // BIO_do_handshake(data->plaintext); | |
| 70 int result = BIO_do_handshake(data->plaintext); | |
| 71 printf("BIO_do_handshake result: %d\n", result); | |
| 72 if (result == 1) { | |
| 73 if (in_handshake) { | |
| 74 HandleError(Dart_InvokeClosure(handshake_finish, 0, NULL)); | |
| 75 in_handshake = false; | |
| 76 } | |
| 77 } else if (BIO_should_retry(data->plaintext)) { | |
| 78 if (!in_handshake) { | |
| 79 HandleError(Dart_InvokeClosure(handshake_start, 0, NULL)); | |
| 80 in_handshake = true; | |
| 81 } | |
| 82 } | |
| 83 printf("Exiting Connect\n"); | |
| 84 } | |
| 85 | |
| 86 void TlsFilter::Destroy() { | |
| 87 printf("Entering Destroy\n"); | |
| 88 printf("Exiting Destroy\n"); | |
| 89 } | |
| 90 | |
| 91 intptr_t TlsFilter::ProcessBuffer(int buffer_index) { | |
| 92 printf(" Entering %s\n", bufferNames[buffer_index]); | |
| 93 Dart_Handle buffer_object = dart_buffer_objects[buffer_index]; | |
| 94 Dart_Handle start_object = HandleError( | |
| 95 Dart_GetField(buffer_object, stringStart)); | |
| 96 Dart_Handle length_object = HandleError( | |
| 97 Dart_GetField(buffer_object, stringLength)); | |
| 98 intptr_t start = Int64ToIntptr(DartUtils::GetIntegerValue(start_object)); | |
| 99 intptr_t length = Int64ToIntptr(DartUtils::GetIntegerValue(length_object)); | |
| 100 if (start < 0 || start >= buffer_size || | |
| 101 length < 0 || length > buffer_size) { | |
| 102 Dart_ThrowException(DartUtils::NewDartIllegalArgumentError( | |
| 103 "Illegal .start or .length on a _TlsExternalBuffer")); | |
| 104 } | |
| 105 | |
| 106 BIO* bio_stream; | |
| 107 if (buffer_index == kReadEncrypted || buffer_index == kWriteEncrypted) { | |
| 108 bio_stream = data->encrypted; | |
| 109 } else { | |
| 110 bio_stream = data->plaintext; | |
| 111 } | |
| 112 | |
| 113 intptr_t bytes_processed = 0; | |
| 114 int bio_bytes = 0; | |
| 115 switch (buffer_index) { | |
| 116 case kReadPlaintext: | |
| 117 case kWriteEncrypted: { | |
| 118 // Write from the BIO to the buffer. | |
| 119 intptr_t bytes_free = buffer_size - start - length; | |
| 120 if (bytes_free > 0) { | |
| 121 bio_bytes = BIO_read(bio_stream, | |
|
Søren Gjesse
2012/07/31 09:07:15
Indentation of arguments.
| |
| 122 buffers[buffer_index] + start + length, | |
| 123 bytes_free); | |
| 124 } | |
| 125 break; | |
| 126 } | |
| 127 case kReadEncrypted: | |
| 128 case kWritePlaintext: | |
| 129 if (length > 0) { | |
| 130 // Call BIO_write. | |
| 131 bio_bytes = BIO_write(bio_stream, | |
|
Søren Gjesse
2012/07/31 09:07:15
Ditto.
| |
| 132 buffers[buffer_index] + start, | |
| 133 length); | |
| 134 BIO_flush(bio_stream); | |
| 135 } | |
| 136 break; | |
| 137 } | |
| 138 if (bio_bytes > 0) { | |
| 139 bytes_processed = bio_bytes; | |
| 140 } | |
| 141 printf(" %d bytes processed to/from openssl.\n", bio_bytes); | |
| 142 printf(" Exiting %s\n", bufferNames[buffer_index]); | |
| 143 return bytes_processed; | |
| 144 } | |
| OLD | NEW |