1 | // Copyright (c) 2017, 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 | #if !defined(DART_IO_SECURE_SOCKET_DISABLED) |
6 | |
7 | #include "bin/secure_socket_utils.h" |
8 | |
9 | #include <openssl/err.h> |
10 | #include <openssl/ssl.h> |
11 | |
12 | #include "platform/globals.h" |
13 | |
14 | #include "bin/file.h" |
15 | #include "bin/secure_socket_filter.h" |
16 | #include "bin/security_context.h" |
17 | #include "platform/syslog.h" |
18 | |
19 | namespace dart { |
20 | namespace bin { |
21 | |
22 | // Get the error messages from BoringSSL, and put them in buffer as a |
23 | // null-terminated string. |
24 | void SecureSocketUtils::FetchErrorString(const SSL* ssl, |
25 | TextBuffer* text_buffer) { |
26 | const char* sep = File::PathSeparator(); |
27 | while (true) { |
28 | const char* path = NULL; |
29 | int line = -1; |
30 | uint32_t error = ERR_get_error_line(&path, &line); |
31 | if (error == 0) { |
32 | break; |
33 | } |
34 | text_buffer->Printf("\n\t%s" , ERR_reason_error_string(error)); |
35 | if ((ssl != NULL) && (ERR_GET_LIB(error) == ERR_LIB_SSL) && |
36 | (ERR_GET_REASON(error) == SSL_R_CERTIFICATE_VERIFY_FAILED)) { |
37 | intptr_t result = SSL_get_verify_result(ssl); |
38 | text_buffer->Printf(": %s" , X509_verify_cert_error_string(result)); |
39 | } |
40 | if ((path != NULL) && (line >= 0)) { |
41 | const char* file = strrchr(path, sep[0]); |
42 | path = file != nullptr ? file + 1 : path; |
43 | text_buffer->Printf("(%s:%d)" , path, line); |
44 | } |
45 | } |
46 | } |
47 | |
48 | // Handle an error reported from the BoringSSL library. |
49 | void SecureSocketUtils::ThrowIOException(int status, |
50 | const char* exception_type, |
51 | const char* message, |
52 | const SSL* ssl) { |
53 | Dart_Handle exception; |
54 | { |
55 | TextBuffer error_string(SSL_ERROR_MESSAGE_BUFFER_SIZE); |
56 | SecureSocketUtils::FetchErrorString(ssl, &error_string); |
57 | OSError os_error_struct(status, error_string.buffer(), OSError::kBoringSSL); |
58 | Dart_Handle os_error = DartUtils::NewDartOSError(&os_error_struct); |
59 | exception = |
60 | DartUtils::NewDartIOException(exception_type, message, os_error); |
61 | ASSERT(!Dart_IsError(exception)); |
62 | } |
63 | Dart_ThrowException(exception); |
64 | UNREACHABLE(); |
65 | } |
66 | |
67 | void SecureSocketUtils::CheckStatusSSL(int status, |
68 | const char* type, |
69 | const char* message, |
70 | const SSL* ssl) { |
71 | // TODO(24183): Take appropriate action on failed calls, |
72 | // throw exception that includes all messages from the error stack. |
73 | if (status == 1) { |
74 | return; |
75 | } |
76 | if (SSL_LOG_STATUS) { |
77 | int error = ERR_get_error(); |
78 | Syslog::PrintErr("Failed: %s status %d" , message, status); |
79 | char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE]; |
80 | ERR_error_string_n(error, error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE); |
81 | Syslog::PrintErr("ERROR: %d %s\n" , error, error_string); |
82 | } |
83 | SecureSocketUtils::ThrowIOException(status, type, message, ssl); |
84 | } |
85 | |
86 | void SecureSocketUtils::CheckStatus(int status, |
87 | const char* type, |
88 | const char* message) { |
89 | SecureSocketUtils::CheckStatusSSL(status, type, message, NULL); |
90 | } |
91 | |
92 | } // namespace bin |
93 | } // namespace dart |
94 | |
95 | #endif // !defined(DART_IO_SECURE_SOCKET_DISABLED) |
96 | |