Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #ifndef RUNTIME_BIN_SECURE_SOCKET_UTILS_H_ | |
| 6 #define RUNTIME_BIN_SECURE_SOCKET_UTILS_H_ | |
| 7 | |
| 8 #include <openssl/bio.h> | |
| 9 #include <openssl/err.h> | |
| 10 #include <openssl/pkcs12.h> | |
| 11 #include <openssl/ssl.h> | |
| 12 #include <openssl/x509.h> | |
| 13 | |
| 14 #include "platform/globals.h" | |
| 15 | |
| 16 #include "bin/dartutils.h" | |
| 17 #include "platform/text_buffer.h" | |
| 18 | |
| 19 namespace dart { | |
| 20 namespace bin { | |
| 21 | |
| 22 class SecureSocketUtils : public AllStatic { | |
| 23 public: | |
| 24 static const int SSL_ERROR_MESSAGE_BUFFER_SIZE = 1000; | |
| 25 | |
| 26 static void ThrowIOException(int status, | |
| 27 const char* exception_type, | |
| 28 const char* message, | |
| 29 const SSL* ssl); | |
| 30 | |
| 31 static void CheckStatusSSL(int status, | |
| 32 const char* type, | |
| 33 const char* message, | |
| 34 const SSL* ssl); | |
| 35 | |
| 36 static void CheckStatus(int status, const char* type, const char* message); | |
| 37 | |
| 38 static bool NoPEMStartLine() { | |
| 39 uint32_t last_error = ERR_peek_last_error(); | |
| 40 return (ERR_GET_LIB(last_error) == ERR_LIB_PEM) && | |
| 41 (ERR_GET_REASON(last_error) == PEM_R_NO_START_LINE); | |
| 42 } | |
| 43 | |
| 44 static void FetchErrorString(const SSL* ssl, TextBuffer* text_buffer); | |
| 45 | |
| 46 private: | |
| 47 DISALLOW_ALLOCATION(); | |
| 48 DISALLOW_IMPLICIT_CONSTRUCTORS(SecureSocketUtils); | |
| 49 }; | |
| 50 | |
| 51 | |
| 52 class X509Helper : public AllStatic { | |
|
zra
2017/06/02 22:56:24
This is only used in security_context.cc. Maybe mo
bkonyi
2017/06/05 20:25:51
Done.
| |
| 53 public: | |
| 54 static Dart_Handle GetSubject(Dart_NativeArguments args); | |
| 55 static Dart_Handle GetIssuer(Dart_NativeArguments args); | |
| 56 static Dart_Handle GetStartValidity(Dart_NativeArguments args); | |
| 57 static Dart_Handle GetEndValidity(Dart_NativeArguments args); | |
| 58 | |
| 59 private: | |
| 60 DISALLOW_ALLOCATION(); | |
| 61 DISALLOW_IMPLICIT_CONSTRUCTORS(X509Helper); | |
| 62 }; | |
| 63 | |
| 64 | |
| 65 // Where the argument to the constructor is the handle for an object | |
| 66 // implementing List<int>, this class creates a scope in which a memory-backed | |
| 67 // BIO is allocated. Leaving the scope cleans up the BIO and the buffer that | |
| 68 // was used to create it. | |
| 69 // | |
| 70 // Do not make Dart_ API calls while in a ScopedMemBIO. | |
| 71 // Do not call Dart_PropagateError while in a ScopedMemBIO. | |
| 72 class ScopedMemBIO { | |
| 73 public: | |
| 74 explicit ScopedMemBIO(Dart_Handle object) { | |
| 75 if (!Dart_IsTypedData(object) && !Dart_IsList(object)) { | |
| 76 Dart_ThrowException( | |
| 77 DartUtils::NewDartArgumentError("Argument is not a List<int>")); | |
| 78 } | |
| 79 | |
| 80 uint8_t* bytes = NULL; | |
| 81 intptr_t bytes_len = 0; | |
| 82 bool is_typed_data = false; | |
| 83 if (Dart_IsTypedData(object)) { | |
| 84 is_typed_data = true; | |
| 85 Dart_TypedData_Type typ; | |
| 86 ThrowIfError(Dart_TypedDataAcquireData( | |
| 87 object, &typ, reinterpret_cast<void**>(&bytes), &bytes_len)); | |
| 88 } else { | |
| 89 ASSERT(Dart_IsList(object)); | |
| 90 ThrowIfError(Dart_ListLength(object, &bytes_len)); | |
| 91 bytes = Dart_ScopeAllocate(bytes_len); | |
| 92 ASSERT(bytes != NULL); | |
| 93 ThrowIfError(Dart_ListGetAsBytes(object, 0, bytes, bytes_len)); | |
| 94 } | |
| 95 | |
| 96 object_ = object; | |
| 97 bytes_ = bytes; | |
| 98 bytes_len_ = bytes_len; | |
| 99 bio_ = BIO_new_mem_buf(bytes, bytes_len); | |
| 100 ASSERT(bio_ != NULL); | |
| 101 is_typed_data_ = is_typed_data; | |
| 102 } | |
| 103 | |
| 104 ~ScopedMemBIO() { | |
| 105 ASSERT(bio_ != NULL); | |
| 106 if (is_typed_data_) { | |
| 107 BIO_free(bio_); | |
| 108 ThrowIfError(Dart_TypedDataReleaseData(object_)); | |
| 109 } else { | |
| 110 BIO_free(bio_); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 BIO* bio() { | |
| 115 ASSERT(bio_ != NULL); | |
| 116 return bio_; | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 Dart_Handle object_; | |
| 121 uint8_t* bytes_; | |
| 122 intptr_t bytes_len_; | |
| 123 BIO* bio_; | |
| 124 bool is_typed_data_; | |
| 125 | |
| 126 DISALLOW_ALLOCATION(); | |
| 127 DISALLOW_COPY_AND_ASSIGN(ScopedMemBIO); | |
| 128 }; | |
| 129 | |
| 130 template <typename T, void (*free_func)(T*)> | |
| 131 class ScopedSSLType { | |
| 132 public: | |
| 133 explicit ScopedSSLType(T* obj) : obj_(obj) {} | |
| 134 | |
| 135 ~ScopedSSLType() { | |
| 136 if (obj_ != NULL) { | |
| 137 free_func(obj_); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 T* get() { return obj_; } | |
| 142 const T* get() const { return obj_; } | |
| 143 | |
| 144 T* release() { | |
| 145 T* result = obj_; | |
| 146 obj_ = NULL; | |
| 147 return result; | |
| 148 } | |
| 149 | |
| 150 private: | |
| 151 T* obj_; | |
| 152 | |
| 153 DISALLOW_ALLOCATION(); | |
| 154 DISALLOW_COPY_AND_ASSIGN(ScopedSSLType); | |
| 155 }; | |
| 156 | |
| 157 template <typename T, typename E, void (*func)(E*)> | |
| 158 class ScopedSSLStackType { | |
| 159 public: | |
| 160 explicit ScopedSSLStackType(T* obj) : obj_(obj) {} | |
| 161 | |
| 162 ~ScopedSSLStackType() { | |
| 163 if (obj_ != NULL) { | |
| 164 sk_pop_free(reinterpret_cast<_STACK*>(obj_), | |
| 165 reinterpret_cast<void (*)(void*)>(func)); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 T* get() { return obj_; } | |
| 170 const T* get() const { return obj_; } | |
| 171 | |
| 172 T* release() { | |
| 173 T* result = obj_; | |
| 174 obj_ = NULL; | |
| 175 return result; | |
| 176 } | |
| 177 | |
| 178 private: | |
| 179 T* obj_; | |
| 180 | |
| 181 DISALLOW_ALLOCATION(); | |
| 182 DISALLOW_COPY_AND_ASSIGN(ScopedSSLStackType); | |
| 183 }; | |
| 184 | |
| 185 | |
| 186 typedef ScopedSSLType<PKCS12, PKCS12_free> ScopedPKCS12; | |
| 187 typedef ScopedSSLType<X509, X509_free> ScopedX509; | |
| 188 typedef ScopedSSLStackType<STACK_OF(X509), X509, X509_free> ScopedX509Stack; | |
| 189 | |
| 190 } // namespace bin | |
| 191 } // namespace dart | |
| 192 | |
| 193 #endif // RUNTIME_BIN_SECURE_SOCKET_UTILS_H_ | |
| OLD | NEW |