Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: runtime/bin/tls_socket_linux.cc

Issue 10704159: Start adding TLS (SSL) sockets to dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Stub out win32 and macos platforms. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
21 class TlsFilterPlatformData {
22 public:
23 SSL* ssl;
24 SSL_CTX* ssl_ctx;
25 BIO* encrypted;
26 BIO* plaintext;
27 BIO* internal;
28 };
29
30
31 TlsFilter::TlsFilter() : in_handshake_(false) {
32 LockInitMutex();
33 if (!library_initialized_) {
34 InitializeLibrary();
35 library_initialized_ = true;
36 }
37 UnlockInitMutex();
38 }
39
40
41 void TlsFilter::InitializeLibrary() {
42 SSL_library_init();
43 SSL_load_error_strings();
44 OpenSSL_add_all_algorithms();
45 }
46
47
48 void TlsFilter::InitializePlatformData() {
49 data_ = new TlsFilterPlatformData;
50 data_->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
51 BIO_new_bio_pair(&(data_->internal), 0, &(data_->encrypted), 0);
52 data_->plaintext = BIO_new_ssl(data_->ssl_ctx, 1);
53 data_->plaintext = BIO_push(data_->plaintext, data_->internal);
54 }
55
56
57 void TlsFilter::Connect() {
58 int result = BIO_do_handshake(data_->plaintext);
59 if (result == 1) {
60 if (in_handshake_) {
61 HandleError(Dart_InvokeClosure(handshake_finish_, 0, NULL));
62 in_handshake_ = false;
63 }
64 } else if (BIO_should_retry(data_->plaintext)) {
65 if (!in_handshake_) {
66 HandleError(Dart_InvokeClosure(handshake_start_, 0, NULL));
67 in_handshake_ = true;
68 }
69 }
70 }
71
72
73 void TlsFilter::Destroy() {
74 // TODO(whesse): Destroy OpenSSL objects here.
75 }
76
77
78 intptr_t TlsFilter::ProcessBuffer(int buffer_index) {
Søren Gjesse 2012/08/09 10:22:52 Wondering whether it would be easier to pass start
Bill Hesse 2012/08/13 11:20:03 I thought it was better to get them this way. It
79 Dart_Handle buffer_object = dart_buffer_objects_[buffer_index];
80 Dart_Handle start_object = HandleError(
81 Dart_GetField(buffer_object, stringStart_));
82 Dart_Handle length_object = HandleError(
83 Dart_GetField(buffer_object, stringLength_));
84 int64_t unsafe_start = DartUtils::GetIntegerValue(start_object);
85 int64_t unsafe_length = DartUtils::GetIntegerValue(length_object);
86 if (unsafe_start < 0 || unsafe_start >= buffer_size_ ||
87 unsafe_length < 0 || unsafe_length > buffer_size_) {
88 Dart_ThrowException(DartUtils::NewDartIllegalArgumentException(
89 "Illegal .start or .length on a _TlsExternalBuffer"));
90 }
91 intptr_t start = static_cast<intptr_t>(unsafe_start);
92 intptr_t length = static_cast<intptr_t>(unsafe_length);
93
94 BIO* bio_stream;
95 if (buffer_index == kReadEncrypted || buffer_index == kWriteEncrypted) {
96 bio_stream = data_->encrypted;
97 } else {
98 bio_stream = data_->plaintext;
99 }
100
101 intptr_t bytes_processed = 0;
102 int bio_bytes = 0;
103 switch (buffer_index) {
104 case kReadPlaintext:
105 case kWriteEncrypted: {
106 // Write from the BIO to the buffer.
107 intptr_t bytes_free = buffer_size_ - start - length;
108 if (bytes_free > 0) {
109 bio_bytes = BIO_read(bio_stream,
110 buffers_[buffer_index] + start + length,
111 bytes_free);
112 }
113 break;
114 }
115 case kReadEncrypted:
116 case kWritePlaintext:
117 if (length > 0) {
118 // Call BIO_write.
119 bio_bytes = BIO_write(bio_stream,
120 buffers_[buffer_index] + start,
121 length);
122 BIO_flush(bio_stream);
123 }
124 break;
125 }
126 if (bio_bytes > 0) {
127 bytes_processed = bio_bytes;
128 }
129 return bytes_processed;
130 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698