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

Side by Side Diff: runtime/bin/secure_socket_scope_utils.h

Issue 2903743002: Porting SecureSocket to use BoringSSL on OSX (Closed)
Patch Set: General cleanup Created 3 years, 6 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
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
zra 2017/05/26 18:11:13 Maybe just secure_socket_utils.h and put some of t
bkonyi 2017/05/26 23:35:31 Done.
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_SCOPE_UTILS_H_
6 #define RUNTIME_BIN_SECURE_SOCKET_SCOPE_UTILS_H_
7
8 #if !defined(RUNTIME_BIN_SECURE_SOCKET_H_)
9 #error Do not include secure_socket_scope_utils.h directly. Use secure_socket.h.
10 #endif
11
12 #include <openssl/bio.h>
13 #include <openssl/err.h>
14 #include <openssl/pkcs12.h>
15 #include <openssl/ssl.h>
16 #include <openssl/x509.h>
17
18
19 namespace dart {
20 namespace bin {
21
22 // Where the argument to the constructor is the handle for an object
23 // implementing List<int>, this class creates a scope in which a memory-backed
24 // BIO is allocated. Leaving the scope cleans up the BIO and the buffer that
25 // was used to create it.
26 //
27 // Do not make Dart_ API calls while in a ScopedMemBIO.
28 // Do not call Dart_PropagateError while in a ScopedMemBIO.
29 class ScopedMemBIO {
30 public:
31 explicit ScopedMemBIO(Dart_Handle object) {
32 if (!Dart_IsTypedData(object) && !Dart_IsList(object)) {
33 Dart_ThrowException(
34 DartUtils::NewDartArgumentError("Argument is not a List<int>"));
35 }
36
37 uint8_t* bytes = NULL;
38 intptr_t bytes_len = 0;
39 bool is_typed_data = false;
40 if (Dart_IsTypedData(object)) {
41 is_typed_data = true;
42 Dart_TypedData_Type typ;
43 ThrowIfError(Dart_TypedDataAcquireData(
44 object, &typ, reinterpret_cast<void**>(&bytes), &bytes_len));
45 } else {
46 ASSERT(Dart_IsList(object));
47 ThrowIfError(Dart_ListLength(object, &bytes_len));
48 bytes = Dart_ScopeAllocate(bytes_len);
49 ASSERT(bytes != NULL);
50 ThrowIfError(Dart_ListGetAsBytes(object, 0, bytes, bytes_len));
51 }
52
53 object_ = object;
54 bytes_ = bytes;
55 bytes_len_ = bytes_len;
56 bio_ = BIO_new_mem_buf(bytes, bytes_len);
57 ASSERT(bio_ != NULL);
58 is_typed_data_ = is_typed_data;
59 }
60
61 ~ScopedMemBIO() {
62 ASSERT(bio_ != NULL);
63 if (is_typed_data_) {
64 BIO_free(bio_);
65 ThrowIfError(Dart_TypedDataReleaseData(object_));
66 } else {
67 BIO_free(bio_);
68 }
69 }
70
71 BIO* bio() {
72 ASSERT(bio_ != NULL);
73 return bio_;
74 }
75
76 private:
77 Dart_Handle object_;
78 uint8_t* bytes_;
79 intptr_t bytes_len_;
80 BIO* bio_;
81 bool is_typed_data_;
82
83 DISALLOW_ALLOCATION();
84 DISALLOW_COPY_AND_ASSIGN(ScopedMemBIO);
85 };
86
87 template <typename T, void (*free_func)(T*)>
88 class ScopedSSLType {
89 public:
90 explicit ScopedSSLType(T* obj) : obj_(obj) {}
91
92 ~ScopedSSLType() {
93 if (obj_ != NULL) {
94 free_func(obj_);
95 }
96 }
97
98 T* get() { return obj_; }
99 const T* get() const { return obj_; }
100
101 T* release() {
102 T* result = obj_;
103 obj_ = NULL;
104 return result;
105 }
106
107 private:
108 T* obj_;
109
110 DISALLOW_ALLOCATION();
111 DISALLOW_COPY_AND_ASSIGN(ScopedSSLType);
112 };
113
114 template <typename T, typename E, void (*func)(E*)>
115 class ScopedSSLStackType {
116 public:
117 explicit ScopedSSLStackType(T* obj) : obj_(obj) {}
118
119 ~ScopedSSLStackType() {
120 if (obj_ != NULL) {
121 sk_pop_free(reinterpret_cast<_STACK*>(obj_),
122 reinterpret_cast<void (*)(void*)>(func));
123 }
124 }
125
126 T* get() { return obj_; }
127 const T* get() const { return obj_; }
128
129 T* release() {
130 T* result = obj_;
131 obj_ = NULL;
132 return result;
133 }
134
135 private:
136 T* obj_;
137
138 DISALLOW_ALLOCATION();
139 DISALLOW_COPY_AND_ASSIGN(ScopedSSLStackType);
140 };
141
142
143 typedef ScopedSSLType<PKCS12, PKCS12_free> ScopedPKCS12;
144 typedef ScopedSSLType<X509, X509_free> ScopedX509;
145 typedef ScopedSSLStackType<STACK_OF(X509), X509, X509_free> ScopedX509Stack;
146
147 } // namespace bin
148 } // namespace dart
149
150 #endif // RUNTIME_BIN_SECURE_SOCKET_SCOPE_UTILS_H_
OLDNEW
« runtime/bin/secure_socket_macos.cc ('K') | « runtime/bin/secure_socket_macos.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698