Skip to content

Commit

Permalink
Merge pull request #2190 from wilzbach/openssl-autodetect
Browse files Browse the repository at this point in the history
Auto-detect OpenSSL 1.1
merged-on-behalf-of: Sönke Ludwig <[email protected]>
  • Loading branch information
dlang-bot authored Feb 9, 2019
2 parents 5aa20a8 + c31bd06 commit ea28640
Show file tree
Hide file tree
Showing 24 changed files with 62 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ examples/uploader/uploader-example
examples/web/web-framework-example
examples/websocket/websocket-example
*.exe

# Ignore auto-generated OpenSSL version
tls/openssl_version.d
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ matrix:
include:
- d: dmd
env: DFLAGS="-cov -version=VibedSetCoverageMerge" PARTS=unittests,tests
exclude:
- d: ldc-1.2.0
env: VIBED_DRIVER=libasync PARTS=builds,unittests
allow_failures:
- d: dmd-beta
- d: ldc-1.3.0
env: VIBED_DRIVER=libasync PARTS=builds,unittests

#before_install:
# - pyenv global system 3.6
Expand Down
Binary file added lib/win-amd64/libcrypto-1_1-x64.dll
Binary file not shown.
Binary file added lib/win-amd64/libcrypto.lib
Binary file not shown.
Binary file removed lib/win-amd64/libeay32.dll
Binary file not shown.
Binary file removed lib/win-amd64/libeay32.lib
Binary file not shown.
Binary file added lib/win-amd64/libssl-1_1-x64.dll
Binary file not shown.
Binary file added lib/win-amd64/libssl.lib
Binary file not shown.
Binary file removed lib/win-amd64/ssleay32.dll
Binary file not shown.
Binary file removed lib/win-amd64/ssleay32.lib
Binary file not shown.
Binary file added lib/win-i386-mscoff/libcrypto.lib
Binary file not shown.
Binary file removed lib/win-i386-mscoff/libeay32.lib
Binary file not shown.
Binary file added lib/win-i386-mscoff/libssl.lib
Binary file not shown.
Binary file removed lib/win-i386-mscoff/ssleay32.lib
Binary file not shown.
Binary file removed lib/win-i386/eay.lib
Binary file not shown.
Binary file added lib/win-i386/libcrypto-1_1.dll
Binary file not shown.
Binary file added lib/win-i386/libcrypto.lib
Binary file not shown.
Binary file removed lib/win-i386/libeay32.dll
Binary file not shown.
Binary file added lib/win-i386/libssl-1_1.dll
Binary file not shown.
Binary file added lib/win-i386/libssl.lib
Binary file not shown.
Binary file removed lib/win-i386/ssl.lib
Binary file not shown.
Binary file removed lib/win-i386/ssleay32.dll
Binary file not shown.
36 changes: 31 additions & 5 deletions tls/dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,39 @@ targetType "library"
sourcePaths "."
importPaths "."

copyFiles "../lib/win-i386/libeay32.dll" "../lib/win-i386/ssleay32.dll" platform="windows-x86"
copyFiles "../lib/win-amd64/libeay32.dll" "../lib/win-amd64/ssleay32.dll" platform="windows-x86_64"
copyFiles "../lib/win-i386/libssl-1_1.dll" "../lib/win-i386/libcrypto-1_1.dll" platform="windows-x86"
copyFiles "../lib/win-amd64/libssl-1_1-x64.dll" "../lib/win-amd64/libcrypto-1_1-x64.dll" platform="windows-x86_64"

configuration "openssl-mscoff" {
platforms "windows-x86_mscoff" "windows-x86_64" "windows-x86-ldc"
dependency "openssl" version=">=1.0.0+1.0.0e"
sourceFiles "../lib/win-i386-mscoff/libeay32.lib" "../lib/win-i386-mscoff/ssleay32.lib" platform="windows-x86"
sourceFiles "../lib/win-amd64/libeay32.lib" "../lib/win-amd64/ssleay32.lib" platform="windows-x86_64"
sourceFiles "../lib/win-i386-mscoff/libssl.lib" "../lib/win-i386-mscoff/libcrypto.lib" platform="windows-x86"
sourceFiles "../lib/win-amd64/libssl.lib" "../lib/win-amd64/libcrypto.lib" platform="windows-x86_64"
}

configuration "openssl" {
sourceFiles "../lib/win-i386/eay.lib" "../lib/win-i386/ssl.lib" platform="windows-x86-dmd"
dependency "openssl" version="~>1.0"

// Windows
sourceFiles "../lib/win-i386/libssl.lib" "../lib/win-i386/libcrypto.lib" platform="windows-x86-dmd"

// Posix
sourceFiles "openssl_version.d" platform="posix"
preBuildCommands `rdmd --eval='
auto dir = environment.get("DUB_PACKAGE_DIR");
if (dir.buildPath("tls").exists) {
dir = dir.buildPath("tls");
}
auto opensslVersion = "0.0.0";
try {
const res = execute(["openssl", "version"]).output;
if (res.canFind("OpenSSL ")) {
opensslVersion = res.splitter(" ").dropOne.front.filter!(not!(std.uni.isAlpha)).text;
}
} catch (Exception e) { writeln("Warning: ", e); }
text("module openssl_version;\nenum OPENSSL_VERSION=\"", opensslVersion, "\";").
toFile(dir.buildPath("openssl_version.d"));
'` platform="posix"
}

configuration "openssl-1.1" {
Expand All @@ -30,6 +50,12 @@ configuration "openssl-1.1" {
versions "VibeUseOpenSSL11"
}

configuration "openssl-1.0" {
platforms "posix"
dependency "openssl" version="~>1.0"
versions "VibeUseOpenSSL10"
}

configuration "openssl-0.9" {
platforms "posix"
dependency "openssl" version="~>1.0"
Expand Down
37 changes: 28 additions & 9 deletions tls/vibe/stream/openssl.d
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,38 @@ import deimos.openssl.ssl;
import deimos.openssl.stack;
import deimos.openssl.x509v3;

// auto-detect OpenSSL 1.1.0
version (VibeUseOpenSSL11)
enum OPENSSL_VERSION = "1.1.0";
version (VibeUseOpenSSL10)
enum OPENSSL_VERSION = "1.0.0";
else version (VibeUseOldOpenSSL)
enum OPENSSL_VERSION = "0.9.0";
else version (Botan)
enum OPENSSL_VERSION = "0.0.0";
else
{
// Only use the openssl_version file if it has been generated
static if (__traits(compiles, {import openssl_version; }))
mixin("import openssl_version;");
else
// try 1.1.0 as softfallback if old other means failed
enum OPENSSL_VERSION = "1.1.0";
}

version (VibePragmaLib) {
pragma(lib, "ssl");
version (Windows) pragma(lib, "eay");
}

version (VibeUseOldOpenSSL) private enum haveECDH = false;
static if (OPENSSL_VERSION.startsWith("0.9")) private enum haveECDH = false;
else private enum haveECDH = OPENSSL_VERSION_NUMBER >= 0x10001000;
version(VibeForceALPN) enum alpn_forced = true;
else enum alpn_forced = false;
enum haveALPN = OPENSSL_VERSION_NUMBER >= 0x10200000 || alpn_forced;

// openssl/1.1.0 hack: provides a 1.0.x API in terms of the 1.1.x API
version (VibeUseOpenSSL11) {
static if (OPENSSL_VERSION.startsWith("1.1")) {
extern(C) const(SSL_METHOD)* TLS_client_method();
alias SSLv23_client_method = TLS_client_method;

Expand Down Expand Up @@ -79,11 +98,11 @@ version (VibeUseOpenSSL11) {
// #define SSL_get_ex_new_index(l, p, newf, dupf, freef) \
// CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, l, p, newf, dupf, freef)

extern(C) int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
extern(C) int CRYPTO_get_ex_new_index(int class_index, c_long argl, void *argp,
CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
CRYPTO_EX_free *free_func);

int SSL_get_ex_new_index(long argl, void *argp,
int SSL_get_ex_new_index(c_long argl, void *argp,
CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
CRYPTO_EX_free *free_func) {
// # define CRYPTO_EX_INDEX_SSL 0
Expand Down Expand Up @@ -126,7 +145,7 @@ version (VibeUseOpenSSL11) {

alias BIOMethWriteCallback = int function(BIO*, const(char)*, int);
alias BIOMethReadCallback = int function(BIO*, const(char)*, int);
alias BIOMethCtrlCallback = c_long function(BIO*, int, long, void*);
alias BIOMethCtrlCallback = c_long function(BIO*, int, c_long, void*);
alias BIOMethCreateCallback = int function(BIO*);
alias BIOMethDestroyCallback = int function(BIO*);

Expand Down Expand Up @@ -204,7 +223,7 @@ final class OpenSSLStream : TLSStream {
m_tls = null;
}

version (VibeUseOpenSSL11) {
static if (OPENSSL_VERSION.startsWith("1.1")) {
if (!s_bio_methods) initBioMethods();

m_bio = () @trusted { return BIO_new(s_bio_methods); } ();
Expand Down Expand Up @@ -577,7 +596,7 @@ final class OpenSSLContext : TLSContext {
const(SSL_METHOD)* method;
c_long veroptions = SSL_OP_NO_SSLv2;
c_long options = SSL_OP_NO_COMPRESSION;
version (VibeUseOpenSSL11) {}
static if (OPENSSL_VERSION.startsWith("1.1")) {}
else
options |= SSL_OP_SINGLE_DH_USE|SSL_OP_SINGLE_ECDH_USE;
int minver = TLS1_VERSION;
Expand Down Expand Up @@ -618,7 +637,7 @@ final class OpenSSLContext : TLSContext {
enforceSSL(0, "Failed to create SSL context");
assert(false);
}
version (VibeUseOpenSSL11) {
static if (OPENSSL_VERSION.startsWith("1.1")) {
() @trusted { return SSL_CTX_set_min_proto_version(m_ctx, minver); }()
.enforceSSL("Failed setting minimum protocol version");
auto retOptions = () @trusted { return SSL_CTX_set_options(m_ctx, options); }();
Expand Down Expand Up @@ -1343,7 +1362,7 @@ private void setSSLError(string msg, string submsg, int line = __LINE__, string
ERR_add_error_data(3, msg.toStringz, ": ".ptr, submsg.toStringz);
}

version (VibeUseOpenSSL11) {
static if (OPENSSL_VERSION.startsWith("1.1")) {
private BIO_METHOD* s_bio_methods;

private void initBioMethods()
Expand Down

0 comments on commit ea28640

Please sign in to comment.