Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
secure_socket_utils.cc
Go to the documentation of this file.
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
8
9#include <openssl/err.h>
10#include <openssl/ssl.h>
11
12#include "platform/globals.h"
13
14#include "bin/file.h"
17#include "platform/syslog.h"
18
19namespace dart {
20namespace bin {
21
22// Get the error messages from BoringSSL, and put them in buffer as a
23// null-terminated string.
24// This function extracts all the error messages into a string and returns
25// the first error code so that this error can be passed in as the OSError
26// error code to the IOException.
28 TextBuffer* text_buffer) {
29 const char* sep = File::PathSeparator();
30 uint32_t errCode = 0;
31 while (true) {
32 const char* path = nullptr;
33 int line = -1;
34 uint32_t error = ERR_get_error_line(&path, &line);
35 if (error == 0) {
36 break;
37 }
38 if (errCode == 0) {
39 errCode = error;
40 }
41 text_buffer->Printf("\n\t%s", ERR_reason_error_string(error));
42 if ((ssl != nullptr) && (ERR_GET_LIB(error) == ERR_LIB_SSL) &&
43 (ERR_GET_REASON(error) == SSL_R_CERTIFICATE_VERIFY_FAILED)) {
44 intptr_t result = SSL_get_verify_result(ssl);
45 text_buffer->Printf(": %s", X509_verify_cert_error_string(result));
46 }
47 if ((path != nullptr) && (line >= 0)) {
48 const char* file = strrchr(path, sep[0]);
49 path = file != nullptr ? file + 1 : path;
50 text_buffer->Printf("(%s:%d)", path, line);
51 }
52 }
53 return errCode;
54}
55
56// Handle an error reported from the BoringSSL library.
58 const char* exception_type,
59 const char* message,
60 const SSL* ssl) {
61 Dart_Handle exception;
62 {
64 uint32_t errCode = SecureSocketUtils::FetchErrorString(ssl, &error_string);
65 if (status == 0) {
66 status = errCode;
67 }
68 OSError os_error_struct(status, error_string.buffer(), OSError::kBoringSSL);
69 Dart_Handle os_error = DartUtils::NewDartOSError(&os_error_struct);
70 exception =
71 DartUtils::NewDartIOException(exception_type, message, os_error);
72 ASSERT(!Dart_IsError(exception));
73 }
74 Dart_ThrowException(exception);
76}
77
79 const char* type,
80 const char* message,
81 const SSL* ssl) {
82 // TODO(24183): Take appropriate action on failed calls,
83 // throw exception that includes all messages from the error stack.
84 if (status == 1) {
85 return;
86 }
87 if (SSL_LOG_STATUS) {
88 int error = ERR_get_error();
89 Syslog::PrintErr("Failed: %s status: %d ", message, status);
90 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE];
91 ERR_error_string_n(error, error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE);
92 Syslog::PrintErr("%s\n", error_string);
93 }
95}
96
98 const char* type,
99 const char* message) {
101}
102
104 ASN1_TIME* not_before = X509_get_notBefore(root_cert);
105 ASN1_TIME* not_after = X509_get_notAfter(root_cert);
106 int days_since_valid = 0;
107 int secs_since_valid = 0;
108 int days_before_invalid = 0;
109 int secs_before_invalid = 0;
110 // nullptr indicates current date/time
111 ASN1_TIME_diff(&days_since_valid, &secs_since_valid, not_before,
112 /*to=*/nullptr);
113 ASN1_TIME_diff(&days_before_invalid, &secs_before_invalid,
114 /*from=*/nullptr, not_after);
115 return days_since_valid >= 0 && secs_since_valid >= 0 &&
116 days_before_invalid >= 0 && secs_before_invalid >= 0;
117}
118
119} // namespace bin
120} // namespace dart
121
122#endif // !defined(DART_IO_SECURE_SOCKET_DISABLED)
#define UNREACHABLE()
Definition assert.h:248
intptr_t Printf(const char *format,...) PRINTF_ATTRIBUTE(2
char * buffer() const
Definition text_buffer.h:35
static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static Dart_Handle NewDartOSError()
Definition dartutils.cc:706
static Dart_Handle NewDartIOException(const char *exception_name, const char *message, Dart_Handle os_error)
Definition dartutils.cc:762
static const char * PathSeparator()
static constexpr int SSL_ERROR_MESSAGE_BUFFER_SIZE
static bool IsCurrentTimeInsideCertValidDateRange(X509 *root_cert)
static void ThrowIOException(int status, const char *exception_type, const char *message, const SSL *ssl)
static uint32_t FetchErrorString(const SSL *ssl, TextBuffer *text_buffer)
static void CheckStatusSSL(int status, const char *type, const char *message, const SSL *ssl)
static void CheckStatus(int status, const char *type, const char *message)
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
#define ASSERT(E)
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
Win32Message message
const bool SSL_LOG_STATUS
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception)