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

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: Add Android stubs for TlsSocket implementation. 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>
cshapiro 2012/08/14 21:17:25 This adds a build dependency which we never had be
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 DestroyPlatformIndependent();
75 // TODO(whesse): Destroy OpenSSL objects here.
76 }
77
78
79 intptr_t TlsFilter::ProcessBuffer(int buffer_index) {
80 Dart_Handle buffer_object = dart_buffer_objects_[buffer_index];
81 Dart_Handle start_object = HandleError(
82 Dart_GetField(buffer_object, stringStart_));
83 Dart_Handle length_object = HandleError(
84 Dart_GetField(buffer_object, stringLength_));
85 int64_t unsafe_start = DartUtils::GetIntegerValue(start_object);
86 int64_t unsafe_length = DartUtils::GetIntegerValue(length_object);
87 if (unsafe_start < 0 || unsafe_start >= buffer_size_ ||
88 unsafe_length < 0 || unsafe_length > buffer_size_) {
89 Dart_ThrowException(DartUtils::NewDartIllegalArgumentException(
90 "Illegal .start or .length on a _TlsExternalBuffer"));
91 }
92 intptr_t start = static_cast<intptr_t>(unsafe_start);
93 intptr_t length = static_cast<intptr_t>(unsafe_length);
94
95 BIO* bio_stream;
96 if (buffer_index == kReadEncrypted || buffer_index == kWriteEncrypted) {
97 bio_stream = data_->encrypted;
98 } else {
99 bio_stream = data_->plaintext;
100 }
101
102 intptr_t bytes_processed = 0;
103 int bio_bytes = 0;
104 switch (buffer_index) {
105 case kReadPlaintext:
106 case kWriteEncrypted: {
107 // Write from the BIO to the buffer.
108 intptr_t bytes_free = buffer_size_ - start - length;
109 if (bytes_free > 0) {
110 bio_bytes = BIO_read(bio_stream,
111 buffers_[buffer_index] + start + length,
112 bytes_free);
113 }
114 break;
115 }
116 case kReadEncrypted:
117 case kWritePlaintext:
118 if (length > 0) {
119 // Call BIO_write.
120 bio_bytes = BIO_write(bio_stream,
121 buffers_[buffer_index] + start,
122 length);
123 }
124 break;
125 }
cshapiro 2012/08/14 21:17:25 No default case? Is this even reachable? If not,
126 if (bio_bytes > 0) {
127 bytes_processed = bio_bytes;
cshapiro 2012/08/14 21:17:25 Is the indentation correct? The "b" in bytes_proce
128 }
129 return bytes_processed;
130 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698