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

Side by Side Diff: runtime/bin/secure_socket_macos.cc

Issue 1842703003: Fixes handling of short reads/writes for Mac SSL (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments Created 4 years, 8 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
« no previous file with comments | « no previous file | sdk/lib/io/secure_socket.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 #if !defined(DART_IO_SECURE_SOCKET_DISABLED) 5 #if !defined(DART_IO_SECURE_SOCKET_DISABLED)
6 6
7 #include "platform/globals.h" 7 #include "platform/globals.h"
8 #if defined(TARGET_OS_MACOS) 8 #if defined(TARGET_OS_MACOS)
9 9
10 #include "bin/secure_socket.h" 10 #include "bin/secure_socket.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 // https://webkit.googlesource.com/WebKit/+/master/Source/WebKit2/Shared/cf/Argu mentCodersCF.cpp 49 // https://webkit.googlesource.com/WebKit/+/master/Source/WebKit2/Shared/cf/Argu mentCodersCF.cpp
50 extern "C" { 50 extern "C" {
51 SecIdentityRef SecIdentityCreate(CFAllocatorRef allocator, 51 SecIdentityRef SecIdentityCreate(CFAllocatorRef allocator,
52 SecCertificateRef certificate, 52 SecCertificateRef certificate,
53 SecKeyRef private_key); 53 SecKeyRef private_key);
54 } 54 }
55 55
56 namespace dart { 56 namespace dart {
57 namespace bin { 57 namespace bin {
58 58
59 static const int kSSLFilterNativeFieldIndex = 0;
60 static const int kSecurityContextNativeFieldIndex = 0;
61 static const int kX509NativeFieldIndex = 0;
62
63 static const bool SSL_LOG_STATUS = false;
64 static const bool SSL_LOG_DATA = false;
65 static const bool SSL_LOG_CERTS = false;
66 static const int SSL_ERROR_MESSAGE_BUFFER_SIZE = 1000;
67 static const intptr_t PEM_BUFSIZE = 1024;
68
59 // SSLCertContext wraps the certificates needed for a SecureTransport 69 // SSLCertContext wraps the certificates needed for a SecureTransport
60 // connection. Fields are protected by the mutex_ field, and may only be set 70 // connection. Fields are protected by the mutex_ field, and may only be set
61 // once. This is to allow access by both the Dart thread and the IOService 71 // once. This is to allow access by both the Dart thread and the IOService
62 // thread. Setters return false if the field was already set. 72 // thread. Setters return false if the field was already set.
63 class SSLCertContext { 73 class SSLCertContext {
64 public: 74 public:
65 SSLCertContext() : 75 SSLCertContext() :
66 mutex_(new Mutex()), 76 mutex_(new Mutex()),
67 private_key_(NULL), 77 private_key_(NULL),
68 keychain_(NULL), 78 keychain_(NULL),
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 // CFArrays of SecCertificateRef. 186 // CFArrays of SecCertificateRef.
177 CFArrayRef cert_chain_; 187 CFArrayRef cert_chain_;
178 CFArrayRef trusted_certs_; 188 CFArrayRef trusted_certs_;
179 CFArrayRef cert_authorities_; 189 CFArrayRef cert_authorities_;
180 190
181 bool trust_builtin_; 191 bool trust_builtin_;
182 192
183 DISALLOW_COPY_AND_ASSIGN(SSLCertContext); 193 DISALLOW_COPY_AND_ASSIGN(SSLCertContext);
184 }; 194 };
185 195
186 static const int kSSLFilterNativeFieldIndex = 0;
187 static const int kSecurityContextNativeFieldIndex = 0;
188 static const int kX509NativeFieldIndex = 0;
189
190 static const bool SSL_LOG_STATUS = false;
191 static const bool SSL_LOG_DATA = false;
192 static const bool SSL_LOG_CERTS = false;
193 static const int SSL_ERROR_MESSAGE_BUFFER_SIZE = 1000;
194 static const intptr_t PEM_BUFSIZE = 1024;
195 196
196 static char* CFStringRefToCString(CFStringRef cfstring) { 197 static char* CFStringRefToCString(CFStringRef cfstring) {
197 CFIndex len = CFStringGetLength(cfstring); 198 CFIndex len = CFStringGetLength(cfstring);
198 CFIndex max_len = 199 CFIndex max_len =
199 CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1; 200 CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8) + 1;
200 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len)); 201 char* result = reinterpret_cast<char*>(Dart_ScopeAllocate(max_len));
201 ASSERT(result != NULL); 202 ASSERT(result != NULL);
202 bool success = 203 bool success =
203 CFStringGetCString(cfstring, result, max_len, kCFStringEncodingUTF8); 204 CFStringGetCString(cfstring, result, max_len, kCFStringEncodingUTF8);
204 return success ? result : NULL; 205 return success ? result : NULL;
(...skipping 1630 matching lines...) Expand 10 before | Expand all | Expand 10 after
1835 Dart_DeletePersistentHandle(handshake_complete_); 1836 Dart_DeletePersistentHandle(handshake_complete_);
1836 handshake_complete_ = NULL; 1837 handshake_complete_ = NULL;
1837 } 1838 }
1838 if (bad_certificate_callback_ != NULL) { 1839 if (bad_certificate_callback_ != NULL) {
1839 Dart_DeletePersistentHandle(bad_certificate_callback_); 1840 Dart_DeletePersistentHandle(bad_certificate_callback_);
1840 bad_certificate_callback_ = NULL; 1841 bad_certificate_callback_ = NULL;
1841 } 1842 }
1842 } 1843 }
1843 1844
1844 1845
1845 static intptr_t AvailableToRead(intptr_t start, intptr_t end, intptr_t size) {
1846 intptr_t data_available = 0;
1847 if (end < start) {
1848 // Data may be split into two segments. In this case,
1849 // the first is [start, size).
1850 intptr_t buffer_end = (start == 0) ? size - 1 : size;
1851 intptr_t available = buffer_end - start;
1852 start += available;
1853 data_available += available;
1854 ASSERT(start <= size);
1855 if (start == size) {
1856 start = 0;
1857 }
1858 }
1859 if (start < end) {
1860 intptr_t available = end - start;
1861 start += available;
1862 data_available += available;
1863 ASSERT(start <= end);
1864 }
1865 return data_available;
1866 }
1867
1868
1869 OSStatus SSLFilter::SSLReadCallback(SSLConnectionRef connection, 1846 OSStatus SSLFilter::SSLReadCallback(SSLConnectionRef connection,
1870 void* data, size_t* data_requested) { 1847 void* data, size_t* data_requested) {
1871 // Copy at most `data_requested` bytes from `buffers_[kReadEncrypted]` into 1848 // Copy at most `data_requested` bytes from `buffers_[kReadEncrypted]` into
1872 // `data` 1849 // `data`
1873 ASSERT(connection != NULL); 1850 ASSERT(connection != NULL);
1874 ASSERT(data != NULL); 1851 ASSERT(data != NULL);
1875 ASSERT(data_requested != NULL); 1852 ASSERT(data_requested != NULL);
1876 1853
1877 SSLFilter* filter = 1854 SSLFilter* filter =
1878 const_cast<SSLFilter*>(reinterpret_cast<const SSLFilter*>(connection)); 1855 const_cast<SSLFilter*>(reinterpret_cast<const SSLFilter*>(connection));
1879 uint8_t* datap = reinterpret_cast<uint8_t*>(data); 1856 uint8_t* datap = reinterpret_cast<uint8_t*>(data);
1880 uint8_t* buffer = filter->buffers_[kReadEncrypted]; 1857 uint8_t* buffer = filter->buffers_[kReadEncrypted];
1881 intptr_t start = filter->GetBufferStart(kReadEncrypted); 1858 intptr_t start = filter->GetBufferStart(kReadEncrypted);
1882 intptr_t end = filter->GetBufferEnd(kReadEncrypted); 1859 intptr_t end = filter->GetBufferEnd(kReadEncrypted);
1883 intptr_t size = filter->encrypted_buffer_size_; 1860 intptr_t size = filter->encrypted_buffer_size_;
1884 intptr_t requested = static_cast<intptr_t>(*data_requested); 1861 intptr_t requested = static_cast<intptr_t>(*data_requested);
1885 intptr_t data_read = 0; 1862 intptr_t data_read = 0;
1886 1863
1887 if (AvailableToRead(start, end, size) < requested) {
1888 *data_requested = 0;
1889 return errSSLWouldBlock;
1890 }
1891
1892 if (end < start) { 1864 if (end < start) {
1893 // Data may be split into two segments. In this case, 1865 // Data may be split into two segments. In this case,
1894 // the first is [start, size). 1866 // the first is [start, size).
1895 intptr_t buffer_end = (start == 0) ? size - 1 : size; 1867 intptr_t buffer_end = (start == 0) ? size - 1 : size;
1896 intptr_t available = buffer_end - start; 1868 intptr_t available = buffer_end - start;
1897 intptr_t bytes = requested < available ? requested : available; 1869 intptr_t bytes = requested < available ? requested : available;
1898 memmove(datap, &buffer[start], bytes); 1870 memmove(datap, &buffer[start], bytes);
1899 start += bytes; 1871 start += bytes;
1900 datap += bytes; 1872 datap += bytes;
1901 data_read += bytes; 1873 data_read += bytes;
(...skipping 13 matching lines...) Expand all
1915 requested -= bytes; 1887 requested -= bytes;
1916 ASSERT(start <= end); 1888 ASSERT(start <= end);
1917 } 1889 }
1918 1890
1919 if (SSL_LOG_DATA) { 1891 if (SSL_LOG_DATA) {
1920 Log::Print("SSLReadCallback: requested: %ld, read %ld bytes\n", 1892 Log::Print("SSLReadCallback: requested: %ld, read %ld bytes\n",
1921 *data_requested, data_read); 1893 *data_requested, data_read);
1922 } 1894 }
1923 1895
1924 filter->SetBufferStart(kReadEncrypted, start); 1896 filter->SetBufferStart(kReadEncrypted, start);
1897 bool short_read = data_read < static_cast<intptr_t>(*data_requested);
1925 *data_requested = data_read; 1898 *data_requested = data_read;
1926 return noErr; 1899 return short_read ? errSSLWouldBlock : noErr;
1927 } 1900 }
1928 1901
1929 1902
1930 // Read decrypted data from the filter to the circular buffer. 1903 // Read decrypted data from the filter to the circular buffer.
1931 OSStatus SSLFilter::ProcessReadPlaintextBuffer(intptr_t start, 1904 OSStatus SSLFilter::ProcessReadPlaintextBuffer(intptr_t start,
1932 intptr_t end, 1905 intptr_t end,
1933 intptr_t* bytes_processed) { 1906 intptr_t* bytes_processed) {
1934 ASSERT(bytes_processed != NULL); 1907 ASSERT(bytes_processed != NULL);
1935 intptr_t length = end - start; 1908 intptr_t length = end - start;
1936 OSStatus status = noErr; 1909 OSStatus status = noErr;
1937 size_t bytes = 0; 1910 size_t bytes = 0;
1938 if (length > 0) { 1911 if (length > 0) {
1939 status = SSLRead( 1912 status = SSLRead(
1940 ssl_context_, 1913 ssl_context_,
1941 reinterpret_cast<void*>((buffers_[kReadPlaintext] + start)), 1914 reinterpret_cast<void*>((buffers_[kReadPlaintext] + start)),
1942 length, 1915 length,
1943 &bytes); 1916 &bytes);
1917 if (SSL_LOG_STATUS) {
1918 Log::Print("SSLRead: status = %ld\n", static_cast<intptr_t>(status));
1919 }
1944 if ((status != noErr) && (status != errSSLWouldBlock)) { 1920 if ((status != noErr) && (status != errSSLWouldBlock)) {
1945 *bytes_processed = 0; 1921 *bytes_processed = 0;
1946 return status; 1922 return status;
1947 } 1923 }
1948 } 1924 }
1949 if (SSL_LOG_DATA) { 1925 if (SSL_LOG_DATA) {
1950 Log::Print("ProcessReadPlaintextBuffer: requested: %ld, read %ld bytes\n", 1926 Log::Print("ProcessReadPlaintextBuffer: requested: %ld, read %ld bytes\n",
1951 length, bytes); 1927 length, bytes);
1952 } 1928 }
1953 *bytes_processed = static_cast<intptr_t>(bytes); 1929 *bytes_processed = static_cast<intptr_t>(bytes);
1954 return status; 1930 return status;
1955 } 1931 }
1956 1932
1957 1933
1958 intptr_t SpaceToWrite(intptr_t start, intptr_t end, intptr_t size) {
1959 intptr_t writable_space = 0;
1960
1961 // is full, neither if statement is executed and nothing happens.
1962 if (start <= end) {
1963 // If the free space may be split into two segments,
1964 // then the first is [end, size), unless start == 0.
1965 // Then, since the last free byte is at position start - 2,
1966 // the interval is [end, size - 1).
1967 intptr_t buffer_end = (start == 0) ? size - 1 : size;
1968 intptr_t available = buffer_end - end;
1969 end += available;
1970 writable_space += available;
1971 ASSERT(end <= size);
1972 if (end == size) {
1973 end = 0;
1974 }
1975 }
1976 if (start > end + 1) {
1977 intptr_t available = (start - 1) - end;
1978 end += available;
1979 writable_space += available;
1980 ASSERT(end < start);
1981 }
1982
1983 return writable_space;
1984 }
1985
1986
1987 OSStatus SSLFilter::SSLWriteCallback(SSLConnectionRef connection, 1934 OSStatus SSLFilter::SSLWriteCallback(SSLConnectionRef connection,
1988 const void* data, size_t* data_provided) { 1935 const void* data, size_t* data_provided) {
1989 // Copy at most `data_provided` bytes from data into 1936 // Copy at most `data_provided` bytes from data into
1990 // `buffers_[kWriteEncrypted]`. 1937 // `buffers_[kWriteEncrypted]`.
1991 ASSERT(connection != NULL); 1938 ASSERT(connection != NULL);
1992 ASSERT(data != NULL); 1939 ASSERT(data != NULL);
1993 ASSERT(data_provided != NULL); 1940 ASSERT(data_provided != NULL);
1994 1941
1995 SSLFilter* filter = 1942 SSLFilter* filter =
1996 const_cast<SSLFilter*>(reinterpret_cast<const SSLFilter*>(connection)); 1943 const_cast<SSLFilter*>(reinterpret_cast<const SSLFilter*>(connection));
1997 const uint8_t* datap = reinterpret_cast<const uint8_t*>(data); 1944 const uint8_t* datap = reinterpret_cast<const uint8_t*>(data);
1998 uint8_t* buffer = filter->buffers_[kWriteEncrypted]; 1945 uint8_t* buffer = filter->buffers_[kWriteEncrypted];
1999 intptr_t start = filter->GetBufferStart(kWriteEncrypted); 1946 intptr_t start = filter->GetBufferStart(kWriteEncrypted);
2000 intptr_t end = filter->GetBufferEnd(kWriteEncrypted); 1947 intptr_t end = filter->GetBufferEnd(kWriteEncrypted);
2001 intptr_t size = filter->encrypted_buffer_size_; 1948 intptr_t size = filter->encrypted_buffer_size_;
2002 intptr_t provided = static_cast<intptr_t>(*data_provided); 1949 intptr_t provided = static_cast<intptr_t>(*data_provided);
2003 intptr_t data_written = 0; 1950 intptr_t data_written = 0;
2004 1951
2005 if (SpaceToWrite(start, end, size) < provided) {
2006 *data_provided = 0;
2007 return errSSLWouldBlock;
2008 }
2009
2010 // is full, neither if statement is executed and nothing happens. 1952 // is full, neither if statement is executed and nothing happens.
2011 if (start <= end) { 1953 if (start <= end) {
2012 // If the free space may be split into two segments, 1954 // If the free space may be split into two segments,
2013 // then the first is [end, size), unless start == 0. 1955 // then the first is [end, size), unless start == 0.
2014 // Then, since the last free byte is at position start - 2, 1956 // Then, since the last free byte is at position start - 2,
2015 // the interval is [end, size - 1). 1957 // the interval is [end, size - 1).
2016 intptr_t buffer_end = (start == 0) ? size - 1 : size; 1958 intptr_t buffer_end = (start == 0) ? size - 1 : size;
2017 intptr_t available = buffer_end - end; 1959 intptr_t available = buffer_end - end;
2018 intptr_t bytes = provided < available ? provided : available; 1960 intptr_t bytes = provided < available ? provided : available;
2019 memmove(&buffer[end], datap, bytes); 1961 memmove(&buffer[end], datap, bytes);
(...skipping 17 matching lines...) Expand all
2037 ASSERT(end < start); 1979 ASSERT(end < start);
2038 } 1980 }
2039 1981
2040 if (SSL_LOG_DATA) { 1982 if (SSL_LOG_DATA) {
2041 Log::Print("SSLWriteCallback: provided: %ld, written %ld bytes\n", 1983 Log::Print("SSLWriteCallback: provided: %ld, written %ld bytes\n",
2042 *data_provided, data_written); 1984 *data_provided, data_written);
2043 } 1985 }
2044 1986
2045 filter->SetBufferEnd(kWriteEncrypted, end); 1987 filter->SetBufferEnd(kWriteEncrypted, end);
2046 *data_provided = data_written; 1988 *data_provided = data_written;
2047 return noErr; 1989 return (data_written == 0) ? errSSLWouldBlock : noErr;
2048 } 1990 }
2049 1991
2050 1992
2051 OSStatus SSLFilter::ProcessWritePlaintextBuffer(intptr_t start, 1993 OSStatus SSLFilter::ProcessWritePlaintextBuffer(intptr_t start,
2052 intptr_t end, 1994 intptr_t end,
2053 intptr_t* bytes_processed) { 1995 intptr_t* bytes_processed) {
2054 ASSERT(bytes_processed != NULL); 1996 ASSERT(bytes_processed != NULL);
2055 intptr_t length = end - start; 1997 intptr_t length = end - start;
2056 OSStatus status = noErr; 1998 OSStatus status = noErr;
2057 size_t bytes = 0; 1999 size_t bytes = 0;
2058 if (length > 0) { 2000 if (length > 0) {
2059 status = SSLWrite( 2001 status = SSLWrite(
2060 ssl_context_, 2002 ssl_context_,
2061 reinterpret_cast<void*>(buffers_[kWritePlaintext] + start), 2003 reinterpret_cast<void*>(buffers_[kWritePlaintext] + start),
2062 length, 2004 length,
2063 &bytes); 2005 &bytes);
2006 if (SSL_LOG_STATUS) {
2007 Log::Print("SSLWrite: status = %ld\n", static_cast<intptr_t>(status));
2008 }
2064 if ((status != noErr) && (status != errSSLWouldBlock)) { 2009 if ((status != noErr) && (status != errSSLWouldBlock)) {
2065 *bytes_processed = 0; 2010 *bytes_processed = 0;
2066 return status; 2011 return status;
2067 } 2012 }
2068 } 2013 }
2014 if (SSL_LOG_DATA) {
2015 Log::Print("ProcessWritePlaintextBuffer: requested: %ld, written: %ld\n",
2016 length, bytes);
2017 }
2069 *bytes_processed = static_cast<intptr_t>(bytes); 2018 *bytes_processed = static_cast<intptr_t>(bytes);
2070 return status; 2019 return status;
2071 } 2020 }
2072 2021
2073 } // namespace bin 2022 } // namespace bin
2074 } // namespace dart 2023 } // namespace dart
2075 2024
2076 #endif // defined(TARGET_OS_MACOS) 2025 #endif // defined(TARGET_OS_MACOS)
2077 2026
2078 #endif // !defined(DART_IO_SECURE_SOCKET_DISABLED) 2027 #endif // !defined(DART_IO_SECURE_SOCKET_DISABLED)
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/io/secure_socket.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698