| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/secure_socket.h" | 5 #include "bin/secure_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 static const int kSSLFilterNativeFieldIndex = 0; | 39 static const int kSSLFilterNativeFieldIndex = 0; |
| 40 static const int kSecurityContextNativeFieldIndex = 0; | 40 static const int kSecurityContextNativeFieldIndex = 0; |
| 41 static const int kX509NativeFieldIndex = 0; | 41 static const int kX509NativeFieldIndex = 0; |
| 42 | 42 |
| 43 static const bool SSL_LOG_STATUS = false; | 43 static const bool SSL_LOG_STATUS = false; |
| 44 static const bool SSL_LOG_DATA = false; | 44 static const bool SSL_LOG_DATA = false; |
| 45 | 45 |
| 46 static const int SSL_ERROR_MESSAGE_BUFFER_SIZE = 1000; | 46 static const int SSL_ERROR_MESSAGE_BUFFER_SIZE = 1000; |
| 47 | 47 |
| 48 |
| 49 /* Get the error messages from BoringSSL, and put them in buffer as a |
| 50 * null-terminated string. */ |
| 51 static void FetchErrorString(char* buffer, int length) { |
| 52 buffer[0] = '\0'; |
| 53 int error = ERR_get_error(); |
| 54 while (error != 0) { |
| 55 int used = strlen(buffer); |
| 56 int free_length = length - used; |
| 57 if (free_length > 16) { |
| 58 // Enough room for error code at least. |
| 59 if (used > 0) { |
| 60 buffer[used] = '\n'; |
| 61 buffer[used + 1] = '\0'; |
| 62 used++; |
| 63 free_length--; |
| 64 } |
| 65 ERR_error_string_n(error, buffer + used, free_length); |
| 66 // ERR_error_string_n is guaranteed to leave a null-terminated string. |
| 67 } |
| 68 error = ERR_get_error(); |
| 69 } |
| 70 } |
| 71 |
| 72 |
| 48 /* Handle an error reported from the BoringSSL library. */ | 73 /* Handle an error reported from the BoringSSL library. */ |
| 49 static void ThrowIOException(int status, | 74 static void ThrowIOException(int status, |
| 50 const char* exception_type, | 75 const char* exception_type, |
| 51 const char* message, | 76 const char* message, |
| 52 bool free_message = false) { | 77 bool free_message = false) { |
| 53 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE]; | 78 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE]; |
| 54 error_string[0] = '\0'; | 79 FetchErrorString(error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE); |
| 55 int error = ERR_get_error(); | |
| 56 while (error != 0) { | |
| 57 int length = strlen(error_string); | |
| 58 int free_length = SSL_ERROR_MESSAGE_BUFFER_SIZE - length; | |
| 59 if (free_length > 16) { | |
| 60 // Enough room for error code at least. | |
| 61 if (length > 0) { | |
| 62 error_string[length] = '\n'; | |
| 63 error_string[length + 1] = '\0'; | |
| 64 length++; | |
| 65 free_length--; | |
| 66 } | |
| 67 ERR_error_string_n(error, error_string + length, free_length); | |
| 68 // ERR_error_string_n is guaranteed to leave a null-terminated string. | |
| 69 } | |
| 70 error = ERR_get_error(); | |
| 71 } | |
| 72 OSError os_error_struct(status, error_string, OSError::kBoringSSL); | 80 OSError os_error_struct(status, error_string, OSError::kBoringSSL); |
| 73 Dart_Handle os_error = DartUtils::NewDartOSError(&os_error_struct); | 81 Dart_Handle os_error = DartUtils::NewDartOSError(&os_error_struct); |
| 74 Dart_Handle exception = | 82 Dart_Handle exception = |
| 75 DartUtils::NewDartIOException(exception_type, message, os_error); | 83 DartUtils::NewDartIOException(exception_type, message, os_error); |
| 76 if (free_message) { | 84 if (free_message) { |
| 77 free(const_cast<char*>(message)); | 85 free(const_cast<char*>(message)); |
| 78 } | 86 } |
| 79 Dart_ThrowException(exception); | 87 Dart_ThrowException(exception); |
| 80 UNREACHABLE(); | 88 UNREACHABLE(); |
| 81 } | 89 } |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 | 603 |
| 596 if (filter->ProcessAllBuffers(starts, ends, in_handshake)) { | 604 if (filter->ProcessAllBuffers(starts, ends, in_handshake)) { |
| 597 CObjectArray* result = new CObjectArray( | 605 CObjectArray* result = new CObjectArray( |
| 598 CObject::NewArray(SSLFilter::kNumBuffers * 2)); | 606 CObject::NewArray(SSLFilter::kNumBuffers * 2)); |
| 599 for (int i = 0; i < SSLFilter::kNumBuffers; ++i) { | 607 for (int i = 0; i < SSLFilter::kNumBuffers; ++i) { |
| 600 result->SetAt(2 * i, new CObjectInt32(CObject::NewInt32(starts[i]))); | 608 result->SetAt(2 * i, new CObjectInt32(CObject::NewInt32(starts[i]))); |
| 601 result->SetAt(2 * i + 1, new CObjectInt32(CObject::NewInt32(ends[i]))); | 609 result->SetAt(2 * i + 1, new CObjectInt32(CObject::NewInt32(ends[i]))); |
| 602 } | 610 } |
| 603 return result; | 611 return result; |
| 604 } else { | 612 } else { |
| 605 // TODO(24185): Extract the BoringSSL OS error here and return it. | 613 int32_t error_code = static_cast<int32_t>(ERR_peek_error()); |
| 606 int error_code = 1; | 614 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE]; |
| 607 const char* error_message = "Obsolete PR Error message"; | 615 FetchErrorString(error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE); |
| 608 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); | 616 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); |
| 609 result->SetAt(0, new CObjectInt32(CObject::NewInt32(error_code))); | 617 result->SetAt(0, new CObjectInt32(CObject::NewInt32(error_code))); |
| 610 result->SetAt(1, new CObjectString(CObject::NewString(error_message))); | 618 result->SetAt(1, new CObjectString(CObject::NewString(error_string))); |
| 611 return result; | 619 return result; |
| 612 } | 620 } |
| 613 } | 621 } |
| 614 | 622 |
| 615 | 623 |
| 616 bool SSLFilter::ProcessAllBuffers(int starts[kNumBuffers], | 624 bool SSLFilter::ProcessAllBuffers(int starts[kNumBuffers], |
| 617 int ends[kNumBuffers], | 625 int ends[kNumBuffers], |
| 618 bool in_handshake) { | 626 bool in_handshake) { |
| 619 for (int i = 0; i < kNumBuffers; ++i) { | 627 for (int i = 0; i < kNumBuffers; ++i) { |
| 620 if (in_handshake && (i == kReadPlaintext || i == kWritePlaintext)) continue; | 628 if (in_handshake && (i == kReadPlaintext || i == kWritePlaintext)) continue; |
| (...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 } else { | 1130 } else { |
| 1123 if (SSL_LOG_DATA) Log::Print( | 1131 if (SSL_LOG_DATA) Log::Print( |
| 1124 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); | 1132 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); |
| 1125 } | 1133 } |
| 1126 } | 1134 } |
| 1127 return bytes_processed; | 1135 return bytes_processed; |
| 1128 } | 1136 } |
| 1129 | 1137 |
| 1130 } // namespace bin | 1138 } // namespace bin |
| 1131 } // namespace dart | 1139 } // namespace dart |
| OLD | NEW |