Skip to content

Commit

Permalink
Merge pull request #66 from Geod24/openssl-3
Browse files Browse the repository at this point in the history
Move the version detection script from `vibe-d:tls` to `deimos/openssl`
  • Loading branch information
CyberShadow authored May 21, 2022
2 parents a033f18 + 74c1adb commit ce9fd3f
Show file tree
Hide file tree
Showing 82 changed files with 312 additions and 89 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated version file
/source/deimos/openssl/version_.d

# DUB artifacts
/.dub/
/openssl
/*-test-library
31 changes: 0 additions & 31 deletions README

This file was deleted.

36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
OpenSSL D interface
-------------------

From the OpenSSL website:

> The OpenSSL Project is a collaborative effort to develop a robust,
> commercial-grade, full-featured, and Open Source toolkit
> implementing the Secure Sockets Layer (SSL v2/v3) and Transport
> Layer Security (TLS v1) protocols as well as a full-strength general
> purpose cryptography library. The project is managed by a worldwide
> community of volunteers that use the Internet to communicate, plan,
> and develop the OpenSSL toolkit and its related documentation.
This repository contains D bindings for OpenSSL.

Status: Varies, depending on targeted OpenSSL version.

The OpenSSL headers are huge (>35k LOC) and make quite liberal use of the C
preprocessor, and thus a fully automatic translation is as desirable as
it is infeasible. This repository contains the result of a semi-automatic
approach, and while all header files have been ported (and successfully
compile), some preprocessor artifacts still need to be ported (currently
commented out and tagged with a `FIXME` note).

The latest version of this package aims to provide compatibility with
[current versions of OpenSSL](https://www.openssl.org/news/changelog.html);
to facilitate this, a build script will detect the OpenSSL version on the
host system and configure the bindings appropriately. This will be done
automatically when using these bindings with Dub.

### License

The OpenSSL toolkit is under a dual license, i.e. both the conditions
of the OpenSSL License and the original SSLeay license apply to the toolkit.
See the OpenSSL distribution for details. These interface files are a derived
work and do not impose any additional restrictions.
19 changes: 14 additions & 5 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@ description "Deimos bindings for the OpenSSL cryptographic library"
homepage "http://www.openssl.org/"
license "OpenSSL or SSLeay"
libs "ssl" "crypto" platform="posix"
importPaths "."

configuration "library" {
configuration "library-autodetect" {
targetType "sourceLibrary"
excludedSourceFiles "deimos/openssl/applink.d"
excludedSourceFiles "source/deimos/openssl/applink.d"
preGenerateCommands `${DUB} scripts/generate_version.d` platform="posix"
versions `DeimosOpenSSLAutoDetect`
}

configuration "library-manual-version" {
targetType "sourceLibrary"
excludedSourceFiles "source/deimos/openssl/applink.d"
}

// Includes a module to replace `applink.c` as described in:
// https://www.openssl.org/docs/manmaster/man3/OPENSSL_Applink.html
configuration "library-applink" {
targetType "sourceLibrary"
}

configuration "unittest" {
targetType "executable"
dflags "-main"
sourcePaths "deimos/openssl"
excludedSourceFiles "deimos/openssl/applink.d"
excludedSourceFiles "source/deimos/openssl/applink.d"
preGenerateCommands `${DUB} scripts/generate_version.d` platform="posix"
versions `DeimosOpenSSLAutoDetect`
}
112 changes: 112 additions & 0 deletions scripts/generate_version.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/+ dub.sdl:
name "script"
+/

/**
* This program will attempt to detect which version of openssl is installed
*
* End-users might have different versions of OpenSSL installed.
* The version might ever differ among members of a development team.
*
* This script attempts to first calls `pkg-config` to find out the version,
* then reverts to calling the `openssl` binary if `pkg-config` didn't work.
*
* It is called directly as a `preGenerateCommand` (see dub.sdl).
* To use it with another build system, pass the directory in which to write
* the `version_.d` file as first and only argument. The directory
* must exist, this script will not create it.
*/
module generate_version;

import std.algorithm;
import std.conv;
import std.file;
import std.functional;
import std.path;
import std.process;
import std.range;
import std.stdio;
import std.string;
import std.uni;

// file full path is: $SOME_PATH/openssl/scripts/generate_version.d
// We want: $SOME_PATH/openssl/deimos/openssl/
immutable TARGET_DIR_PATH = __FILE_FULL_PATH__
.dirName.dirName.buildPath("source", "deimos", "openssl");

void main(string[] args)
{
string target;

if (args.length == 2)
{
assert(args[1].isDir(),
"OpenSSL version detection: Argument '" ~ args[1] ~ "' is not a directory");
target = args[1].buildPath("version_.d");
}
else
{
assert(args.length == 1,
"OpenSSL version detection expects only one argument, " ~
"a directory path where to write `version_.d`");
target = TARGET_DIR_PATH.buildPath("version_.d");
}

string opensslVersion;
try
{
const res = execute(["pkg-config", "openssl", "--modversion"]);
if (res.status == 0)
opensslVersion = res.output.strip();
}
catch (Exception e) {}

if (!opensslVersion.length) try
{
const res = execute(["openssl", "version"]).output;
if (res.canFind("OpenSSL "))
{
opensslVersion = res.splitter(" ").dropOne.front.filter!(not!(std.uni.isAlpha)).text;
}
else if (res.canFind("LibreSSL "))
{
writeln("\tWarning: Your default openssl binary points to LibreSSL, which is not supported.");
version (OSX)
{
writeln("\tOn Mac OSX, this is the default behavior.");
writeln("\tIf you installed openssl via a package manager, you need to tell DUB how to find it.");
writeln("\tAssuming brew, run [brew link openssl] and follow the instructions for pkg-config.\n");
}
}
}
catch (Exception e) {}

if (!opensslVersion.length)
{
writeln("\tWarning: Could not find OpenSSL version via pkg-config nor by calling the openssl binary.");
writeln("\tAssuming version 1.1.0.");
writeln("\tYou might need to export PKG_CONFIG_PATH or install the openssl package if you have a library-only package.");
opensslVersion = "1.1.0h";
}
auto data = format(q{/**
* Provide the version of the libssl being linked to at compile time
*
* This module was auto-generated by deimos/openssl's script/generate_version.d
* Manual edit might get overwritten by later build.
*
* This module should not be directly dependend upon.
* Instead, use `deimos.openssl.opensslv`, which handles explicit overrides
* provides a uniform interface, and a few utilities.
*/
module deimos.openssl.version_;

/// Ditto
package enum OpenSSLTextVersion = "%s";
}, opensslVersion);

// Only write the file iff it has changed or didn't exist before.
// This way timestamp-based build system will not rebuild,
// and changes on the installed OpenSSL will be correctly detected.
if (!target.exists || target.readText.strip != data.strip)
data.toFile(target);
}
File renamed without changes.
File renamed without changes.
86 changes: 48 additions & 38 deletions deimos/openssl/applink.d → source/deimos/openssl/applink.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
/**
* Glue between OpenSSL BIO and Win32 compiler run-time
*
* Duplicate the content of the `applink.c` source file
* to avoid linking it in user code without adding a dependency
* to a C build system/compiler.
*
* See_Also: https://www.openssl.org/docs/manmaster/man3/OPENSSL_Applink.html
*/
module deimos.openssl.applink;

import core.stdc.stdio;
import std.stdio : _fileno, _setmode, _O_BINARY;
import core.sys.posix.fcntl;
Expand Down Expand Up @@ -35,53 +45,53 @@ enum _O_TEXT = 0x4000;

extern(C)
{
void *app_stdin()
{
return cast(void*)stdin;
void *app_stdin()
{
return cast(void*)stdin;
}
void *app_stdout()
{
return cast(void*)stdout;

void *app_stdout()
{
return cast(void*)stdout;
}
void *app_stderr()
{
return cast(void*)stderr;

void *app_stderr()
{
return cast(void*)stderr;
}
int app_feof(FILE *fp)
{
return feof(fp);

int app_feof(FILE *fp)
{
return feof(fp);
}
int app_ferror(FILE *fp)
{
return ferror(fp);

int app_ferror(FILE *fp)
{
return ferror(fp);
}

void app_clearerr(FILE *fp)
{
clearerr(fp);
{
clearerr(fp);
}
int app_fileno(FILE *fp)
{
return _fileno(fp);

int app_fileno(FILE *fp)
{
return _fileno(fp);
}

int app_fsetmod(FILE *fp, char mod)
{
return _setmode (_fileno(fp),mod=='b'?_O_BINARY:_O_TEXT);
{
return _setmode (_fileno(fp),mod=='b'?_O_BINARY:_O_TEXT);
}

__gshared bool once = true;
__gshared void*[APPLINK_MAX+1] OPENSSL_ApplinkTable = cast(void*)APPLINK_MAX;

export void** OPENSSL_Applink()
{
{
if (once)
{
{
OPENSSL_ApplinkTable[APPLINK_STDIN] = &app_stdin;
OPENSSL_ApplinkTable[APPLINK_STDOUT] = &app_stdout;
OPENSSL_ApplinkTable[APPLINK_STDERR] = &app_stderr;
Expand All @@ -92,24 +102,24 @@ extern(C)
OPENSSL_ApplinkTable[APPLINK_FSETMOD] = &app_fsetmod;
OPENSSL_ApplinkTable[APPLINK_FEOF] = &app_feof;
OPENSSL_ApplinkTable[APPLINK_FCLOSE] = &fclose;

OPENSSL_ApplinkTable[APPLINK_FOPEN] = &fopen;
OPENSSL_ApplinkTable[APPLINK_FSEEK] = &fseek;
OPENSSL_ApplinkTable[APPLINK_FTELL] = &ftell;
OPENSSL_ApplinkTable[APPLINK_FFLUSH] = &fflush;
OPENSSL_ApplinkTable[APPLINK_FERROR] = &app_ferror;
OPENSSL_ApplinkTable[APPLINK_CLEARERR] = &app_clearerr;
OPENSSL_ApplinkTable[APPLINK_FILENO] = &app_fileno;

OPENSSL_ApplinkTable[APPLINK_OPEN] = &fopen;
OPENSSL_ApplinkTable[APPLINK_READ] = &fread;
OPENSSL_ApplinkTable[APPLINK_WRITE] = &fwrite;
OPENSSL_ApplinkTable[APPLINK_LSEEK] = &fseek;
OPENSSL_ApplinkTable[APPLINK_CLOSE] = &fclose;

once = false;
}

return OPENSSL_ApplinkTable.ptr;
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ce9fd3f

Please sign in to comment.