Skip to content

Commit 3fcaa7a

Browse files
committed
Add autogenerated releases for OpenSSL, Crypto++ and Botan
1 parent 40f5279 commit 3fcaa7a

6 files changed

+388
-63
lines changed

fetchReleases.py

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
5+
import json
6+
import jinja2
7+
import re
8+
import requests
9+
10+
import subprocess as sp
11+
12+
from base64 import b32encode, b32decode, b64encode, b16decode
13+
from bs4 import BeautifulSoup
14+
15+
env = jinja2.Environment()
16+
17+
all_versions_template = env.from_string("""{
18+
buildECTesterStandalone
19+
}:
20+
{ {% for version in pkg_versions %}
21+
{{ version }} {% endfor %}
22+
}""")
23+
24+
def get_source_hash(url, unpack=False):
25+
digest_type = "sha256"
26+
27+
cmd = ["nix-prefetch-url"]
28+
if unpack:
29+
cmd.append("--unpack")
30+
cmd.extend(["--type", digest_type, url])
31+
32+
digest_nixbase32 = sp.check_output(cmd, stderr=sp.DEVNULL).strip()
33+
digest_sri = sp.check_output(["nix", "hash", "to-sri", "--type", digest_type, digest_nixbase32.decode()], stderr=sp.DEVNULL).strip().decode()
34+
return digest_sri
35+
36+
def fetch_botan():
37+
# NOTE: this way omits the older releases at https://botan.randombit.net/releases/old
38+
release_list = "https://botan.randombit.net/releases/"
39+
download_url = "https://botan.randombit.net/releases/{version}"
40+
resp = requests.get(release_list)
41+
soup = BeautifulSoup(resp.content, 'html.parser')
42+
43+
single_version_template = env.from_string("""{{ flat_version }} = buildECTesterStandalone {
44+
{{ pkg }} = { version="{{ version }}"; source_extension="{{ ext }}"; hash="{{ digest }}"; };
45+
};""")
46+
47+
renders = []
48+
for link in soup.find_all("a"):
49+
if link.text.startswith("Botan") and not link.text.endswith('.asc'):
50+
download_link = download_url.format(version=link['href'])
51+
52+
match = re.match(r"Botan-(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.(?P<ext>.*)", link.text)
53+
version = f"{match['major']}.{match['minor']}.{match['patch']}"
54+
ext = f"{match['ext']}"
55+
56+
digest = get_source_hash(download_link)
57+
# NOTE: use underscore to separate the versions?
58+
flat_version = f"v{match['major']}{match['minor']}{match['patch']}"
59+
60+
rendered = single_version_template.render(pkg="botan", digest=digest, ext=ext, flat_version=flat_version, version=version).strip()
61+
renders.append(rendered)
62+
63+
all_versions = all_versions_template.render(pkg_versions=renders).strip()
64+
with open("./nix/botan_pkg_versions.nix", "w") as handle:
65+
handle.write(all_versions)
66+
67+
def fetch_cryptopp():
68+
owner = "weidai11"
69+
repo = "cryptopp"
70+
release_url = f"https://api.github.com/repos/{owner}/{repo}/releases"
71+
resp = requests.get(release_url)
72+
73+
single_version_template = env.from_string("""{{ flat_version }} = buildECTesterStandalone {
74+
{{ pkg }} = { version="{{ version }}"; hash="{{ digest }}"; };
75+
};""")
76+
renders = []
77+
for release in resp.json():
78+
if not release['draft'] and not release['prerelease']:
79+
_, *version_values = release['tag_name'].split('_')
80+
underscored_version = '_'.join(version_values)
81+
flat_version = "v" + "".join(version_values)
82+
download_url = f"https://github.com/{owner}/{repo}/archive/{release['tag_name']}.tar.gz"
83+
digest = get_source_hash(download_url, unpack=True)
84+
85+
86+
rendered = single_version_template.render(pkg="cryptopp", digest=digest, flat_version=flat_version, version=underscored_version).strip()
87+
renders.append(rendered)
88+
89+
all_versions = all_versions_template.render(pkg_versions=renders).strip()
90+
with open("./nix/cryptopp_pkg_versions.nix", "w") as handle:
91+
handle.write(all_versions)
92+
93+
def fetch_openssl():
94+
pkg = "openssl"
95+
owner = "openssl"
96+
repo = "openssl"
97+
release_url = f"https://api.github.com/repos/{owner}/{repo}/releases"
98+
resp = requests.get(release_url)
99+
100+
single_version_template = env.from_string("""{{ flat_version }} = buildECTesterStandalone {
101+
{{ pkg }} = { version="{{ version }}"; hash="{{ digest }}"; };
102+
};""")
103+
renders = []
104+
for release in resp.json():
105+
if not release['draft'] and not release['prerelease']:
106+
try:
107+
_, dotted_version = release['tag_name'].split('-')
108+
except ValueError:
109+
continue
110+
flat_version = "v" + "".join(dotted_version.split('.'))
111+
download_url = f"https://github.com/{owner}/{repo}/archive/{release['tag_name']}.tar.gz"
112+
digest = get_source_hash(download_url)
113+
114+
115+
rendered = single_version_template.render(pkg=pkg, digest=digest, flat_version=flat_version, version=dotted_version).strip()
116+
renders.append(rendered)
117+
118+
all_versions = all_versions_template.render(pkg_versions=renders).strip()
119+
with open(f"./nix/{pkg}_pkg_versions.nix", "w") as handle:
120+
handle.write(all_versions)
121+
122+
123+
124+
125+
def main():
126+
parser = argparse.ArgumentParser()
127+
parser.add_argument("lib")
128+
args = parser.parse_args()
129+
130+
match args.lib:
131+
case "botan":
132+
fetch_botan()
133+
case "cryptopp":
134+
fetch_cryptopp()
135+
case "openssl":
136+
fetch_openssl()
137+
138+
139+
if __name__ == '__main__':
140+
main()

flake.nix

+39-26
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,25 @@
4545
'';
4646
});
4747
# FIXME: `nix develeop` now has different version than `nix run`
48-
openssl = { version ? "", hash ? "" }: (pkgs.openssl.override { static = true; }).overrideAttrs (final: prev: rec {
48+
opensslBuilder = { version ? null, hash ? null }: (pkgs.openssl.override { static = true; }).overrideAttrs (final: prev: rec {
4949
pname = "openssl";
50-
src = if version != "" then pkgs.fetchurl {
50+
src = if version != null then pkgs.fetchurl {
5151
url = "https://www.openssl.org/source/openssl-${version}.tar.gz";
5252
hash = hash;
5353
} else prev.src;
5454
# FIXME Removing patches might cause unwanted things; this should be version based!
5555
patches = [];
5656
});
57+
botan2Builder = { version, source_extension, hash }: pkgs.botan2.overrideAttrs (final: prev: {
58+
src = if ( version == null ) then prev.src else
59+
pkgs.fetchurl {
60+
urls = [
61+
"http://botan.randombit.net/releases/Botan-${version}.${source_extension}"
62+
];
63+
inherit hash;
64+
};
65+
});
66+
5767
libgcrypt = pkgs.libgcrypt.overrideAttrs (final: prev: {
5868
configureFlags = ( prev.configureFlags or [] ) ++ [ "--enable-static" ];
5969
});
@@ -96,7 +106,15 @@
96106
nettle = pkgs.nettle.overrideAttrs (final: prev: {
97107
configureFlags = ( prev.configureFlags or [] ) ++ [ "--enable-static" ];
98108
});
99-
cryptopp = pkgs.cryptopp.override { enableStatic = true; };
109+
cryptoppBuilder = { version, hash }: (pkgs.cryptopp.override { enableStatic = true; }).overrideAttrs (final: prev: {
110+
src = if version == null then prev.src else
111+
pkgs.fetchFromGitHub {
112+
owner = "weidai11";
113+
repo = "cryptopp";
114+
rev = "CRYPTOPP_${version}";
115+
inherit hash;
116+
};
117+
});
100118
libressl = (pkgs.libressl.override { buildShared = false; } ).overrideAttrs (_old: rec {
101119
patches = [
102120
(pkgs.fetchpatch {
@@ -135,10 +153,10 @@
135153

136154
# Shims and libs
137155
# Current list of targets: tomcrypt botan cryptopp openssl boringssl gcrypt mbedtls ippcp nettle libressl
138-
tomcryptShim = import ./nix/tomcryptshim.nix { inherit pkgs libtomcrypt libtommath; };
139-
botanShim = import ./nix/botanshim.nix { inherit pkgs; };
140-
cryptoppShim = import ./nix/cryptoppshim.nix { inherit pkgs cryptopp; };
141-
opensslShimBuilder = { version, hash }: import ./nix/opensslshim.nix { inherit pkgs; openssl = (openssl { version = version; hash = hash;}); };
156+
tomcryptShim = pkgs.callPackage ./nix/tomcryptshim.nix { inherit pkgs libtomcrypt libtommath; };
157+
botanShimBuilder = { version, source_extension, hash }: pkgs.callPackage ./nix/botanshim.nix { botan2 = botan2Builder { inherit version source_extension hash; }; };
158+
cryptoppShimBuilder = { version, hash}: pkgs.callPackage ./nix/cryptoppshim.nix { cryptopp = cryptoppBuilder { inherit version hash; };};
159+
opensslShimBuilder = { version, hash }: import ./nix/opensslshim.nix { inherit pkgs; openssl = (opensslBuilder { version = version; hash = hash;}); };
142160
boringsslShim = import ./nix/boringsslshim.nix { inherit pkgs; boringssl = boringssl; };
143161
gcryptShim = import ./nix/gcryptshim.nix { inherit pkgs libgcrypt libgpg-error; };
144162
mbedtlsShim = import ./nix/mbedtlsshim.nix { pkgs = pkgs; };
@@ -148,9 +166,15 @@
148166

149167
commonLibs = import ./nix/commonlibs.nix { pkgs = pkgs; };
150168

151-
buildECTesterStandalone = { opensslVersion, opensslHash }: (
169+
buildECTesterStandalone = {
170+
openssl ? { version = null; hash = null; },
171+
botan ? { version = null; source_extension = null; hash = null; },
172+
cryptopp ? { version = null; hash = null; },
173+
}: (
152174
let
153-
opensslShim = (opensslShimBuilder { version = opensslVersion; hash = opensslHash; });
175+
opensslShim = (opensslShimBuilder { inherit (openssl) version hash; });
176+
botanShim = botanShimBuilder { inherit (botan) version source_extension hash; };
177+
cryptoppShim = cryptoppShimBuilder { inherit (cryptopp) version hash; };
154178
in
155179
with pkgs;
156180
gradle2nix.builders.${system}.buildGradlePackage rec {
@@ -201,20 +225,10 @@
201225
in
202226
{
203227
packages = rec {
204-
default = openssl_331;
205-
openssl_331 = buildECTesterStandalone {
206-
opensslVersion="3.3.1"; opensslHash="sha256-d3zVlihMiDN1oqehG/XSeG/FQTJV76sgxQ1v/m0CC34=";
207-
};
208-
openssl_322 = buildECTesterStandalone {
209-
opensslVersion="3.2.2"; opensslHash="sha256-GXFJwY2enyksQ/BACsq6EuX1LKz+BQ89GZJ36nOOwuc=";
210-
};
211-
openssl_316 = buildECTesterStandalone {
212-
opensslVersion="3.1.6"; opensslHash="sha256-XSvkA2tHjvPLCoVMqbNTByw6DibYpW+PCrn7btMtONc=";
213-
};
214-
openssl_3014 = buildECTesterStandalone {
215-
opensslVersion="3.0.14"; opensslHash="sha256-7soDXU3U6E/CWEbZUtpil0hK+gZQpvhMaC453zpBI8o=";
216-
};
217-
# openssl_111w = buildECTesterStandalone "1.1.1w" "sha256-zzCYlQy02FOtlcCEHx+cbT3BAtzPys1SHZOSUgi3asg=";
228+
default = openssl.v331;
229+
openssl = pkgs.callPackage ./nix/openssl_pkg_versions.nix { inherit buildECTesterStandalone; };
230+
botan = pkgs.callPackage ./nix/botan_pkg_versions.nix { inherit buildECTesterStandalone; };
231+
cryptopp = pkgs.callPackage ./nix/cryptopp_pkg_versions.nix { inherit buildECTesterStandalone; };
218232
};
219233
devShells.default = with pkgs; mkShell rec {
220234
nativeBuildInputs = [
@@ -235,7 +249,7 @@
235249
global-platform-pro
236250
gradle
237251
# libraries to test
238-
(openssl {})
252+
(opensslBuilder {})
239253
libressl
240254
# glibc
241255
boringssl
@@ -280,8 +294,7 @@
280294
libtomcrypt
281295
botan2
282296
cryptopp
283-
# (openssl {})
284-
(openssl {})
297+
(opensslBuilder {})
285298
boringssl
286299
libgcrypt
287300
libgpg-error

0 commit comments

Comments
 (0)