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 <stdio.h> | |
| 11 #include <string.h> | |
| 12 | |
| 13 #include <prinit.h> | |
| 14 #include <prerror.h> | |
| 15 #include <prnetdb.h> | |
| 16 #include <nss.h> | |
|
Mads Ager (google)
2012/11/12 11:39:08
Can we list this in alphabetic order or are there
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 17 #include <ssl.h> | |
| 18 | |
| 19 #include "bin/builtin.h" | |
| 20 #include "bin/dartutils.h" | |
| 21 #include "bin/thread.h" | |
| 22 #include "bin/utils.h" | |
| 23 #include "bin/net/nss_memio.h" | |
|
Mads Ager (google)
2012/11/12 11:39:08
Ditto?
Bill Hesse
2012/11/13 20:11:08
Done.
Is it allowable to put dart_api separately,
| |
| 24 #include "platform/utils.h" | |
| 25 | |
| 26 #include "include/dart_api.h" | |
| 27 | |
| 28 bool TlsFilter::library_initialized_ = false; | |
| 29 static const int kTlsFilterNativeFieldIndex = 0; | |
| 30 | |
| 31 static TlsFilter* GetTlsFilter(Dart_NativeArguments args) { | |
| 32 TlsFilter* filter; | |
| 33 | |
|
Mads Ager (google)
2012/11/12 11:39:08
I would delete this empty line.
| |
| 34 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | |
| 35 ASSERT(Dart_IsInstance(dart_this)); | |
| 36 ThrowIfError(Dart_GetNativeInstanceField(dart_this, | |
|
Mads Ager (google)
2012/11/12 11:39:08
Please move the first argument to the next line as
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 37 kTlsFilterNativeFieldIndex, | |
| 38 reinterpret_cast<intptr_t*>(&filter))); | |
| 39 return filter; | |
| 40 } | |
| 41 | |
| 42 | |
| 43 static void SetTlsFilter(Dart_NativeArguments args, TlsFilter* filter) { | |
| 44 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | |
| 45 ASSERT(Dart_IsInstance(dart_this)); | |
| 46 ThrowIfError(Dart_SetNativeInstanceField(dart_this, | |
|
Mads Ager (google)
2012/11/12 11:39:08
Ditto
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 47 kTlsFilterNativeFieldIndex, | |
| 48 reinterpret_cast<intptr_t>(filter))); | |
| 49 } | |
| 50 | |
| 51 | |
| 52 void FUNCTION_NAME(TlsSocket_Init)(Dart_NativeArguments args) { | |
| 53 Dart_EnterScope(); | |
| 54 TlsFilter* filter = new TlsFilter; | |
| 55 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); | |
| 56 if (Dart_IsError(dart_this)) { | |
| 57 delete filter; | |
| 58 Dart_PropagateError(dart_this); | |
| 59 } | |
| 60 SetTlsFilter(args, filter); | |
| 61 filter->Init(dart_this); | |
| 62 Dart_ExitScope(); | |
| 63 } | |
| 64 | |
| 65 | |
| 66 void FUNCTION_NAME(TlsSocket_Connect)(Dart_NativeArguments args) { | |
| 67 Dart_EnterScope(); | |
| 68 Dart_Handle host_name = ThrowIfError(Dart_GetNativeArgument(args, 1)); | |
| 69 | |
| 70 const char* host_name_string = NULL; | |
| 71 // TODO(whesse): Is truncating a Dart string containing \0 what we want? | |
| 72 ThrowIfError(Dart_StringToCString(host_name, &host_name_string)); | |
| 73 | |
| 74 GetTlsFilter(args)->Connect(host_name_string); | |
| 75 Dart_ExitScope(); | |
| 76 } | |
| 77 | |
| 78 | |
| 79 void FUNCTION_NAME(TlsSocket_Destroy)(Dart_NativeArguments args) { | |
| 80 Dart_EnterScope(); | |
| 81 TlsFilter* filter = GetTlsFilter(args); | |
| 82 SetTlsFilter(args, NULL); | |
| 83 filter->Destroy(); | |
| 84 delete filter; | |
| 85 Dart_ExitScope(); | |
| 86 } | |
| 87 | |
| 88 | |
| 89 void FUNCTION_NAME(TlsSocket_Handshake)(Dart_NativeArguments args) { | |
| 90 Dart_EnterScope(); | |
| 91 GetTlsFilter(args)->Handshake(); | |
| 92 Dart_ExitScope(); | |
| 93 } | |
| 94 | |
| 95 | |
| 96 void FUNCTION_NAME(TlsSocket_RegisterHandshakeCallbacks)( | |
| 97 Dart_NativeArguments args) { | |
| 98 Dart_EnterScope(); | |
| 99 Dart_Handle handshake_start = ThrowIfError(Dart_GetNativeArgument(args, 1)); | |
| 100 Dart_Handle handshake_finish = ThrowIfError(Dart_GetNativeArgument(args, 2)); | |
| 101 if (!Dart_IsClosure(handshake_start) || | |
| 102 !Dart_IsClosure(handshake_finish)) { | |
| 103 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 104 "Illegal argument to RegisterHandshakeCallbacks")); | |
| 105 } | |
| 106 GetTlsFilter(args)->RegisterHandshakeCallbacks(handshake_start, | |
| 107 handshake_finish); | |
| 108 Dart_ExitScope(); | |
| 109 } | |
| 110 | |
| 111 | |
| 112 void FUNCTION_NAME(TlsSocket_ProcessBuffer)(Dart_NativeArguments args) { | |
| 113 Dart_EnterScope(); | |
| 114 Dart_Handle buffer_id_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); | |
| 115 int64_t buffer_id = DartUtils::GetIntegerValue(buffer_id_object); | |
| 116 if (buffer_id < 0 || buffer_id >= TlsFilter::kNumBuffers) { | |
| 117 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 118 "Illegal argument to ProcessBuffer")); | |
| 119 } | |
| 120 | |
| 121 intptr_t bytes_read = | |
| 122 GetTlsFilter(args)->ProcessBuffer(static_cast<int>(buffer_id)); | |
| 123 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | |
| 124 Dart_ExitScope(); | |
| 125 } | |
| 126 | |
| 127 | |
| 128 void FUNCTION_NAME(TlsSocket_SetCertificateDatabase) | |
| 129 (Dart_NativeArguments args) { | |
| 130 Dart_EnterScope(); | |
| 131 Dart_Handle dart_pkcert_dir = ThrowIfError(Dart_GetNativeArgument(args, 0)); | |
| 132 // Check that the type is string, and get the UTF-8 C string value from it. | |
| 133 if (Dart_IsString(dart_pkcert_dir)) { | |
| 134 const char* pkcert_dir = NULL; | |
| 135 ThrowIfError(Dart_StringToCString(dart_pkcert_dir, &pkcert_dir)); | |
| 136 TlsFilter::InitializeLibrary(pkcert_dir); | |
| 137 } else { | |
| 138 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 139 "Non-String argument to SetCertificateDatabase")); | |
| 140 } | |
| 141 Dart_ExitScope(); | |
| 142 } | |
| 143 | |
| 144 | |
| 145 void TlsFilter::Init(Dart_Handle dart_this) { | |
| 146 stringStart_ = ThrowIfError( | |
| 147 Dart_NewPersistentHandle(DartUtils::NewString("start"))); | |
| 148 stringLength_ = ThrowIfError( | |
| 149 Dart_NewPersistentHandle(DartUtils::NewString("length"))); | |
| 150 | |
| 151 InitializeBuffers(dart_this); | |
| 152 memio_ = memio_CreateIOLayer(kMemioBufferSize); | |
| 153 } | |
| 154 | |
| 155 | |
| 156 void TlsFilter::InitializeBuffers(Dart_Handle dart_this) { | |
| 157 // Create TlsFilter buffers as ExternalUint8Array objects. | |
| 158 Dart_Handle dart_buffers_object = ThrowIfError( | |
| 159 Dart_GetField(dart_this, DartUtils::NewString("buffers"))); | |
| 160 Dart_Handle dart_buffer_object = ThrowIfError( | |
| 161 Dart_ListGetAt(dart_buffers_object, kReadPlaintext)); | |
| 162 Dart_Handle tls_external_buffer_class = ThrowIfError( | |
|
Søren Gjesse
2012/11/12 12:04:57
Wouldn't it be simpler to just look up the class "
Bill Hesse
2012/11/13 20:11:08
I beleive we need a library for that, and so the n
| |
| 163 Dart_InstanceGetClass(dart_buffer_object)); | |
| 164 Dart_Handle dart_buffer_size = ThrowIfError( | |
| 165 Dart_GetField(tls_external_buffer_class, DartUtils::NewString("kSize"))); | |
| 166 buffer_size_ = DartUtils::GetIntegerValue(dart_buffer_size); | |
| 167 if (buffer_size_ <= 0 || buffer_size_ > 1024 * 1024) { | |
| 168 Dart_ThrowException( | |
| 169 DartUtils::NewString("Invalid buffer size in _TlsExternalBuffer")); | |
| 170 } | |
| 171 | |
| 172 for (int i = 0; i < kNumBuffers; ++i) { | |
| 173 dart_buffer_objects_[i] = ThrowIfError( | |
| 174 Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i))); | |
| 175 buffers_[i] = new uint8_t[buffer_size_]; | |
| 176 Dart_Handle data = ThrowIfError( | |
| 177 Dart_NewExternalByteArray(buffers_[i], | |
| 178 buffer_size_, NULL, NULL)); | |
|
Mads Ager (google)
2012/11/12 11:39:08
Might fit on one line? If not, either move more to
| |
| 179 ThrowIfError(Dart_SetField(dart_buffer_objects_[i], | |
| 180 DartUtils::NewString("data"), | |
|
Søren Gjesse
2012/11/12 12:04:57
Move DartUtils::NewString("data") out out the loop
| |
| 181 data)); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 | |
| 186 void TlsFilter::RegisterHandshakeCallbacks(Dart_Handle start, | |
| 187 Dart_Handle finish) { | |
| 188 handshake_start_ = ThrowIfError(Dart_NewPersistentHandle(start)); | |
|
Mads Ager (google)
2012/11/12 11:39:08
Where are these persistent handles destroyed? Is t
Bill Hesse
2012/11/13 20:11:08
This is only called once, in the constructor of th
| |
| 189 handshake_finish_ = ThrowIfError(Dart_NewPersistentHandle(finish)); | |
| 190 } | |
| 191 | |
| 192 | |
| 193 void TlsFilter::InitializeLibrary(const char* pkcert_database) { | |
| 194 LockInitMutex(); | |
| 195 if (!library_initialized_) { | |
| 196 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); | |
| 197 // TODO: Allow read-write opening of pkcert_database using | |
|
Mads Ager (google)
2012/11/12 11:39:08
Let's remove this TODO for now? We are not current
Søren Gjesse
2012/11/12 12:04:57
Please add whesse to TODO.
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 198 // NSS_InitReadWrite, controlled by an optional argument. | |
| 199 SECStatus status = | |
|
Søren Gjesse
2012/11/12 12:04:57
Fits on one line.
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 200 NSS_Init(pkcert_database); | |
|
Mads Ager (google)
2012/11/12 11:39:08
Fits on one line.
| |
| 201 if (status != SECSuccess) { | |
| 202 ReportError("Unsuccessful NSS_Init call.\n", PR_GetError()); | |
|
Mads Ager (google)
2012/11/12 11:39:08
Remove the \n from the strings here and let Report
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 203 } | |
| 204 | |
| 205 status = NSS_SetDomesticPolicy(); | |
| 206 if (status != SECSuccess) { | |
| 207 ReportError("Unsuccessful NSS_SetDomesticPolicy call.\n", PR_GetError()); | |
| 208 } | |
| 209 } else { | |
| 210 ReportError("Called TlsFilter::InitializeLibrary more than once", 0); | |
|
Mads Ager (google)
2012/11/12 11:39:08
Indentation is off.
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 211 } | |
| 212 UnlockInitMutex(); | |
| 213 } | |
| 214 | |
| 215 | |
| 216 void TlsFilter::Connect(const char* host) { | |
| 217 if (in_handshake_) { | |
| 218 ReportError("Connect called while already in handshake state.", 0); | |
| 219 } | |
| 220 PRFileDesc* my_socket = memio_; | |
| 221 | |
| 222 my_socket = SSL_ImportFD(NULL, my_socket); | |
| 223 if (my_socket == NULL) { | |
| 224 ReportError("Unsuccessful SSL_ImportFD call", 0); | |
| 225 } | |
| 226 | |
| 227 if (SSL_SetURL(my_socket, host) == -1) { | |
| 228 ReportError("Unsuccessful SetURL call", 0); | |
| 229 } | |
| 230 | |
| 231 SECStatus status = SSL_ResetHandshake(my_socket, PR_FALSE); | |
| 232 if (status != SECSuccess) { | |
| 233 ReportError("Unsuccessful SSL_ResetHandshake call", PR_GetError()); | |
| 234 } | |
| 235 | |
| 236 // SetPeerAddress | |
| 237 PRNetAddr host_address; | |
| 238 char host_entry_buffer[PR_NETDB_BUF_SIZE]; | |
| 239 PRHostEnt host_entry; | |
| 240 PRStatus rv = PR_GetHostByName(host, host_entry_buffer, | |
| 241 PR_NETDB_BUF_SIZE, &host_entry); | |
| 242 if (rv != PR_SUCCESS) { | |
| 243 ReportError("Unsuccessful PR_GetHostByName call", PR_GetError()); | |
| 244 } | |
| 245 | |
| 246 int index = PR_EnumerateHostEnt(0, &host_entry, 443, &host_address); | |
|
Søren Gjesse
2012/11/12 12:04:57
Should 443 be hardcoded here? How about TLS on oth
| |
| 247 if (index == -1 || index == 0) { | |
| 248 ReportError("Unsuccessful PR_EnumerateHostEnt call", 0); | |
| 249 } | |
| 250 | |
| 251 memio_SetPeerName(my_socket, &host_address); | |
| 252 memio_ = my_socket; | |
| 253 } | |
| 254 | |
| 255 | |
| 256 void TlsFilter::Handshake() { | |
|
Mads Ager (google)
2012/11/12 11:39:08
This looks a little convoluted to me.
Don't we al
Mads Ager (google)
2012/11/14 10:18:54
How about this comment, Bill? Can this be simplifi
Bill Hesse
2012/11/14 13:33:30
Done.
| |
| 257 SECStatus status = SSL_ForceHandshake(memio_); | |
| 258 if (status == SECSuccess) { | |
| 259 if (in_handshake_) { | |
| 260 ThrowIfError(Dart_InvokeClosure(handshake_finish_, 0, NULL)); | |
| 261 in_handshake_ = false; | |
| 262 } | |
| 263 } else { | |
| 264 PRErrorCode error = PR_GetError(); | |
| 265 if (error == PR_WOULD_BLOCK_ERROR) { | |
| 266 if (!in_handshake_) { | |
| 267 ThrowIfError(Dart_InvokeClosure(handshake_start_, 0, NULL)); | |
| 268 in_handshake_ = true; | |
| 269 } | |
| 270 } else { | |
| 271 ReportError("Unexpected handshake error", error); | |
| 272 } | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 | |
| 277 void TlsFilter::Destroy() { | |
| 278 for (int i = 0; i < kNumBuffers; ++i) { | |
| 279 Dart_DeletePersistentHandle(dart_buffer_objects_[i]); | |
| 280 } | |
| 281 Dart_DeletePersistentHandle(stringStart_); | |
| 282 Dart_DeletePersistentHandle(stringLength_); | |
| 283 // TODO(whesse): Destroy OpenSSL objects here. | |
|
Mads Ager (google)
2012/11/12 11:39:08
OpenSSL?!?
I made this comment in my last round o
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 284 } | |
| 285 | |
| 286 | |
| 287 intptr_t TlsFilter::ProcessBuffer(int buffer_index) { | |
|
Søren Gjesse
2012/11/12 12:04:57
Maybe add a short comment on how the four buffers
Bill Hesse
2012/11/13 20:11:08
Because it is easiest to get an int return value b
| |
| 288 Dart_Handle buffer_object = dart_buffer_objects_[buffer_index]; | |
| 289 Dart_Handle start_object = ThrowIfError( | |
| 290 Dart_GetField(buffer_object, stringStart_)); | |
| 291 Dart_Handle length_object = ThrowIfError( | |
| 292 Dart_GetField(buffer_object, stringLength_)); | |
| 293 int64_t unsafe_start = DartUtils::GetIntegerValue(start_object); | |
| 294 int64_t unsafe_length = DartUtils::GetIntegerValue(length_object); | |
| 295 if (unsafe_start < 0 || unsafe_start >= buffer_size_ || | |
|
Mads Ager (google)
2012/11/12 11:39:08
What happens here if we get a bigint? Can that hap
Bill Hesse
2012/11/13 20:11:08
Yes, this is a "can't happen" situation. We contr
| |
| 296 unsafe_length < 0 || unsafe_length > buffer_size_) { | |
| 297 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 298 "Illegal .start or .length on a _TlsExternalBuffer")); | |
| 299 } | |
| 300 intptr_t start = static_cast<intptr_t>(unsafe_start); | |
| 301 intptr_t length = static_cast<intptr_t>(unsafe_length); | |
| 302 | |
| 303 int bytes_processed = 0; | |
| 304 switch (buffer_index) { | |
| 305 case kReadPlaintext: { | |
| 306 int bytes_free = buffer_size_ - start - length; | |
| 307 bytes_processed = PR_Read(memio_, | |
|
Søren Gjesse
2012/11/12 12:04:57
Indentation.
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 308 buffers_[buffer_index] + start + length, | |
|
Mads Ager (google)
2012/11/12 11:39:08
Indentation is off.
| |
| 309 bytes_free); | |
| 310 if (bytes_processed < 0) { | |
| 311 ASSERT(bytes_processed == -1); | |
| 312 // TODO(whesse): Handle unexpected errors here. | |
| 313 PRErrorCode pr_error = PR_GetError(); | |
| 314 ASSERT(PR_WOULD_BLOCK_ERROR == pr_error); | |
| 315 bytes_processed = 0; | |
| 316 } | |
| 317 break; | |
| 318 } | |
| 319 | |
| 320 case kWriteEncrypted: { | |
| 321 const uint8_t* buf1; | |
| 322 const uint8_t* buf2; | |
| 323 unsigned int len1; | |
| 324 unsigned int len2; | |
| 325 int bytes_free = buffer_size_ - start - length; | |
| 326 memio_Private* secret = memio_GetSecret(memio_); | |
| 327 memio_GetWriteParams(secret, &buf1, &len1, &buf2, &len2); | |
| 328 int bytes_to_send = | |
| 329 dart::Utils::Minimum(len1, static_cast<unsigned>(bytes_free)); | |
| 330 if (bytes_to_send > 0) { | |
| 331 memmove(buffers_[buffer_index] + start + length, buf1, bytes_to_send); | |
| 332 bytes_processed = bytes_to_send; | |
| 333 } | |
| 334 bytes_to_send = dart::Utils::Minimum(len2, | |
| 335 static_cast<unsigned>(bytes_free - bytes_processed)); | |
| 336 if (bytes_to_send > 0) { | |
| 337 memmove(buffers_[buffer_index] + start + length + bytes_processed, buf2, | |
| 338 bytes_to_send); | |
| 339 bytes_processed += bytes_to_send; | |
| 340 } | |
| 341 if (bytes_processed > 0) { | |
| 342 memio_PutWriteResult(secret, bytes_processed); | |
| 343 } | |
| 344 break; | |
| 345 } | |
| 346 | |
| 347 case kReadEncrypted: { | |
| 348 if (length > 0) { | |
| 349 bytes_processed = length; | |
| 350 memio_Private* secret = memio_GetSecret(memio_); | |
| 351 uint8_t* memio_buf; | |
| 352 int free_bytes = memio_GetReadParams(secret, &memio_buf); | |
| 353 if (free_bytes < bytes_processed) bytes_processed = free_bytes; | |
| 354 memmove(memio_buf, | |
| 355 buffers_[buffer_index] + start, | |
| 356 bytes_processed); | |
| 357 memio_PutReadResult(secret, bytes_processed); | |
| 358 } | |
| 359 break; | |
| 360 } | |
| 361 | |
| 362 case kWritePlaintext: { | |
| 363 if (length > 0) { | |
| 364 bytes_processed = PR_Write(memio_, | |
|
Søren Gjesse
2012/11/12 12:04:57
Indentation.
Bill Hesse
2012/11/13 20:11:08
Done.
| |
| 365 buffers_[buffer_index] + start, | |
| 366 length); | |
| 367 } | |
| 368 | |
| 369 if (bytes_processed < 0) { | |
| 370 ASSERT(bytes_processed == -1); | |
| 371 // TODO(whesse): Handle unexpected errors here. | |
| 372 PRErrorCode pr_error = PR_GetError(); | |
| 373 ASSERT(PR_WOULD_BLOCK_ERROR == pr_error); | |
| 374 bytes_processed = 0; | |
| 375 } | |
| 376 break; | |
| 377 } | |
| 378 } | |
| 379 return bytes_processed; | |
| 380 } | |
| OLD | NEW |