2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
21 * Provides a SSL BIO implementation wrapping a Thrift transport.
23 * This way, SSL I/O can be relayed over Thrift transport without introducing
24 * an additional layer of buffering, especially for the non-blocking
27 * For the Thrift transport incarnations of the SSL entities, "tt" is used as
30 module thrift.internal.ssl_bio;
32 import core.stdc.config;
33 import core.stdc.string : strlen;
34 import core.memory : GC;
35 import deimos.openssl.bio;
36 import deimos.openssl.err;
38 import thrift.internal.ssl;
39 import thrift.transport.base;
42 * Creates an SSL BIO object wrapping the given transport.
44 * Exceptions thrown by the transport are pushed onto the OpenSSL error stack,
45 * using the location/reason values from thrift.internal.ssl.ERR_*_D_EXCEPTION.
47 * The transport is assumed to be ready for reading and writing when the BIO
48 * functions are called, it is not opened by the implementation.
51 * transport = The transport to wrap.
52 * closeTransport = Whether the close the transport when the SSL BIO is
55 BIO* createTTransportBIO(TTransport transport, bool closeTransport) {
56 auto result = BIO_new(cast(BIO_METHOD*)&ttBioMethod);
57 if (!result) return null;
59 GC.addRoot(cast(void*)transport);
60 BIO_set_fd(result, closeTransport, cast(c_long)cast(void*)transport);
66 // Helper to get the Thrift transport assigned with the given BIO.
67 TTransport trans(BIO* b) nothrow {
68 auto result = cast(TTransport)b.ptr;
73 void setError(Exception e) nothrow {
74 ERR_put_error(ERR_LIB_D_EXCEPTION, ERR_F_D_EXCEPTION, ERR_R_D_EXCEPTION,
75 ERR_FILE_D_EXCEPTION, ERR_LINE_D_EXCEPTION);
76 try { GC.addRoot(cast(void*)e); } catch (Throwable) {}
77 ERR_set_error_data(cast(char*)e, ERR_FLAGS_D_EXCEPTION);
80 extern(C) int ttWrite(BIO* b, const(char)* data, int length) nothrow {
82 if (!data || length <= 0) return 0;
84 trans(b).write((cast(ubyte*)data)[0 .. length]);
86 } catch (Exception e) {
92 extern(C) int ttRead(BIO* b, char* data, int length) nothrow {
94 if (!data || length <= 0) return 0;
96 return cast(int)trans(b).read((cast(ubyte*)data)[0 .. length]);
97 } catch (Exception e) {
103 extern(C) int ttPuts(BIO* b, const(char)* str) nothrow {
104 return ttWrite(b, str, cast(int)strlen(str));
107 extern(C) c_long ttCtrl(BIO* b, int cmd, c_long num, void* ptr) nothrow {
112 // Note that close flag and "fd" are actually reversed here because we
113 // need 64 bit width for the pointer – should probably drop BIO_set_fd
116 b.ptr = cast(void*)num;
117 b.shutdown = cast(int)ptr;
121 if (!b.init_) return -1;
122 *(cast(void**)ptr) = b.ptr;
123 return cast(c_long)b.ptr;
124 case BIO_CTRL_GET_CLOSE:
126 case BIO_CTRL_SET_CLOSE:
127 b.shutdown = cast(int)num;
133 } catch (Exception e) {
138 // Seems like we have nothing to do on duplication, but couldn't find
139 // any documentation if this actually ever happens during normal SSL
147 extern(C) int ttCreate(BIO* b) nothrow {
150 b.num = 0; // User-defined number field, unused here.
156 extern(C) int ttDestroy(BIO* b) nothrow {
164 GC.removeRoot(cast(void*)trans(b));
166 } catch (Exception e) {
178 immutable BIO_METHOD ttBioMethod = {
179 BIO_TYPE_SOURCE_SINK,
188 null // callback_ctrl