1{ stdenv, fetchurl, buildPackages, perl, coreutils
2, withCryptodev ? false, cryptodev
3, enableSSL2 ? false
4, static ? false
5}:
6
7with stdenv.lib;
8
9let
10 common = { version, sha256, patches ? [], withDocs ? false, extraMeta ? {} }:
11 stdenv.mkDerivation rec {
12 pname = "openssl";
13 inherit version;
14
15 src = fetchurl {
16 url = "https://www.openssl.org/source/${pname}-${version}.tar.gz";
17 inherit sha256;
18 };
19
20 inherit patches;
21
22 postPatch = ''
23 patchShebangs Configure
24 '' + optionalString (versionOlder version "1.1.0") ''
25 patchShebangs test/*
26 for a in test/t* ; do
27 substituteInPlace "$a" \
28 --replace /bin/rm rm
29 done
30 '' + optionalString (versionAtLeast version "1.1.1") ''
31 substituteInPlace config --replace '/usr/bin/env' '${coreutils}/bin/env'
32 '' + optionalString (versionAtLeast version "1.1.0" && stdenv.hostPlatform.isMusl) ''
33 substituteInPlace crypto/async/arch/async_posix.h \
34 --replace '!defined(__ANDROID__) && !defined(__OpenBSD__)' \
35 '!defined(__ANDROID__) && !defined(__OpenBSD__) && 0'
36 '';
37
38 outputs = [ "bin" "dev" "out" "man" ] ++ optional withDocs "doc";
39 setOutputFlags = false;
40 separateDebugInfo = !(stdenv.hostPlatform.useLLVM or false) && stdenv.cc.isGNU;
41
42 nativeBuildInputs = [ perl ];
43 buildInputs = stdenv.lib.optional withCryptodev cryptodev;
44
45 # TODO(@Ericson2314): Improve with mass rebuild
46 configurePlatforms = [];
47 configureScript = {
48 armv6l-linux = "./Configure linux-armv4 -march=armv6";
49 armv7l-linux = "./Configure linux-armv4 -march=armv7-a";
50 x86_64-darwin = "./Configure darwin64-x86_64-cc";
51 x86_64-linux = "./Configure linux-x86_64";
52 x86_64-solaris = "./Configure solaris64-x86_64-gcc";
53 }.${stdenv.hostPlatform.system} or (
54 if stdenv.hostPlatform == stdenv.buildPlatform
55 then "./config"
56 else if stdenv.hostPlatform.isMinGW
57 then "./Configure mingw${optionalString
58 (stdenv.hostPlatform.parsed.cpu.bits != 32)
59 (toString stdenv.hostPlatform.parsed.cpu.bits)}"
60 else if stdenv.hostPlatform.isLinux
61 then (if stdenv.hostPlatform.isx86_64
62 then "./Configure linux-x86_64"
63 else "./Configure linux-generic${toString stdenv.hostPlatform.parsed.cpu.bits}")
64 else if stdenv.hostPlatform.isiOS
65 then "./Configure ios${toString stdenv.hostPlatform.parsed.cpu.bits}-cross"
66 else
67 throw "Not sure what configuration to use for ${stdenv.hostPlatform.config}"
68 );
69
70 configureFlags = [
71 "shared" # "shared" builds both shared and static libraries
72 "--libdir=lib"
73 "--openssldir=etc/ssl"
74 ] ++ stdenv.lib.optionals withCryptodev [
75 "-DHAVE_CRYPTODEV"
76 "-DUSE_CRYPTODEV_DIGESTS"
77 ] ++ stdenv.lib.optional enableSSL2 "enable-ssl2"
78 ++ stdenv.lib.optional (versionAtLeast version "1.1.0" && stdenv.hostPlatform.isAarch64) "no-afalgeng"
79 # OpenSSL needs a specific `no-shared` configure flag.
80 # See https://wiki.openssl.org/index.php/Compilation_and_Installation#Configure_Options
81 # for a comprehensive list of configuration options.
82 ++ stdenv.lib.optional (versionAtLeast version "1.1.0" && static) "no-shared";
83
84 makeFlags = [
85 "MANDIR=$(man)/share/man"
86 # This avoids conflicts between man pages of openssl subcommands (for
87 # example 'ts' and 'err') man pages and their equivalent top-level
88 # command in other packages (respectively man-pages and moreutils).
89 # This is done in ubuntu and archlinux, and possiibly many other distros.
90 "MANSUFFIX=ssl"
91 ];
92
93 enableParallelBuilding = true;
94
95 postInstall =
96 stdenv.lib.optionalString (!static) ''
97 # If we're building dynamic libraries, then don't install static
98 # libraries.
99 if [ -n "$(echo $out/lib/*.so $out/lib/*.dylib $out/lib/*.dll)" ]; then
100 rm "$out/lib/"*.a
101 fi
102
103 '' +
104 ''
105 mkdir -p $bin
106 '' + stdenv.lib.optionalString (!stdenv.hostPlatform.isWindows)
107 ''
108 substituteInPlace $out/bin/c_rehash --replace ${buildPackages.perl} ${perl}
109 '' +
110 ''
111 mv $out/bin $bin/
112
113 mkdir $dev
114 mv $out/include $dev/
115
116 # remove dependency on Perl at runtime
117 rm -r $out/etc/ssl/misc
118
119 rmdir $out/etc/ssl/{certs,private}
120 '';
121
122 postFixup = stdenv.lib.optionalString (!stdenv.hostPlatform.isWindows) ''
123 # Check to make sure the main output doesn't depend on perl
124 if grep -r '${buildPackages.perl}' $out; then
125 echo "Found an erroneous dependency on perl ^^^" >&2
126 exit 1
127 fi
128 '';
129
130 meta = with stdenv.lib; {
131 homepage = https://www.openssl.org/;
132 description = "A cryptographic library that implements the SSL and TLS protocols";
133 license = licenses.openssl;
134 platforms = platforms.all;
135 maintainers = [ maintainers.peti ];
136 } // extraMeta;
137 };
138
139in {
140
141 openssl_1_0_2 = common {
142 version = "1.0.2u";
143 sha256 = "ecd0c6ffb493dd06707d38b14bb4d8c2288bb7033735606569d8f90f89669d16";
144 patches = [
145 ./1.0.2/nix-ssl-cert-file.patch
146
147 (if stdenv.hostPlatform.isDarwin
148 then ./1.0.2/use-etc-ssl-certs-darwin.patch
149 else ./1.0.2/use-etc-ssl-certs.patch)
150 ];
151 extraMeta.knownVulnerabilities = [ "Support for OpenSSL 1.0.2 ended with 2019." ];
152 };
153
154 openssl_1_1 = common {
155 version = "1.1.1f";
156 sha256 = "186c6bfe6ecfba7a5b48c47f8a1673d0f3b0e5ba2e25602dd23b629975da3f35";
157 patches = [
158 ./1.1/nix-ssl-cert-file.patch
159
160 (if stdenv.hostPlatform.isDarwin
161 then ./1.1/use-etc-ssl-certs-darwin.patch
162 else ./1.1/use-etc-ssl-certs.patch)
163 ];
164 withDocs = true;
165 };
166
167}