Skip to content

Commit 79197af

Browse files
committed
tools: add gpg verification using current_ver.dep
1 parent a95ea15 commit 79197af

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

configure.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,12 +1771,6 @@ def icu_download(path):
17711771
attemptdownload = nodedownload.candownload(auto_downloads, "icu")
17721772
for icu in icus:
17731773
url = icu['url']
1774-
(expectHash, hashAlgo, allAlgos) = nodedownload.findHash(icu)
1775-
if not expectHash:
1776-
error(f'''Could not find a hash to verify ICU download.
1777-
{depFile} may be incorrect.
1778-
For the entry {url},
1779-
Expected one of these keys: {' '.join(allAlgos)}''')
17801774
local = url.split('/')[-1]
17811775
targetfile = Path(options.download_path, local)
17821776
if not targetfile.is_file():
@@ -1785,14 +1779,28 @@ def icu_download(path):
17851779
else:
17861780
print(f'Re-using existing {targetfile}')
17871781
if targetfile.is_file():
1788-
print(f'Checking file integrity with {hashAlgo}:\r')
1789-
gotHash = nodedownload.checkHash(targetfile, hashAlgo)
1790-
print(f'{hashAlgo}: {gotHash} {targetfile}')
1791-
if expectHash == gotHash:
1792-
return targetfile
1793-
1794-
warn(f'Expected: {expectHash} *MISMATCH*')
1795-
warn(f'\n ** Corrupted ZIP? Delete {targetfile} to retry download.\n')
1782+
if "gpg" in icu:
1783+
key_url = icu['gpg']["key"]
1784+
sig_url = icu['gpg']["asc"]
1785+
try:
1786+
nodedownload.checkGPG(targetfile, key_url, sig_url)
1787+
return targetfile
1788+
except Exception as e:
1789+
warn(e)
1790+
else:
1791+
(expectHash, hashAlgo, allAlgos) = nodedownload.findHash(icu)
1792+
if not expectHash:
1793+
error(f'''Could not find a hash to verify ICU download.
1794+
{depFile} may be incorrect.
1795+
For the entry {url},
1796+
Expected one of these keys: {' '.join(allAlgos)}''')
1797+
print(f'Checking file integrity with {hashAlgo}:\r')
1798+
gotHash = nodedownload.checkHash(targetfile, hashAlgo)
1799+
print(f'{hashAlgo}: {gotHash} {targetfile}')
1800+
if expectHash == gotHash:
1801+
return targetfile
1802+
warn(f'Expected: {expectHash} *MISMATCH*')
1803+
warn(f'\n ** Corrupted ZIP? Delete {targetfile} to retry download.\n')
17961804
return None
17971805
icu_config = {
17981806
'variables': {}

tools/configure.d/nodedownload.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import zipfile
88
import tarfile
99
import contextlib
10+
import subprocess
11+
import tempfile
1012
try:
11-
from urllib.request import FancyURLopener, URLopener
13+
from urllib.request import FancyURLopener, URLopener, urlopen
1214
except ImportError:
13-
from urllib import FancyURLopener, URLopener
15+
from urllib import FancyURLopener, URLopener, urlopen
16+
import os
1417

1518
def formatSize(amt):
1619
"""Format a size as a string in MB"""
@@ -68,6 +71,40 @@ def checkHash(targetfile, hashAlgo):
6871
chunk = f.read(1024)
6972
return digest.hexdigest()
7073

74+
def checkGPG(targetfile, key_url, sig_url):
75+
key_data = download_file(key_url)
76+
sig_data = download_file(sig_url)
77+
with tempfile.NamedTemporaryFile(delete=False) as f:
78+
f.write(key_data)
79+
key_file = f.name
80+
81+
with tempfile.NamedTemporaryFile(delete=False) as f:
82+
f.write(sig_data)
83+
sig_file = f.name
84+
try:
85+
cmd = ["gpg", "--import", key_file]
86+
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
87+
except subprocess.CalledProcessError as e:
88+
os.remove(key_file)
89+
os.remove(sig_file)
90+
raise Exception("Failed to import key. Check key url. \n%s" % e.stderr.decode("utf-8"))
91+
92+
try:
93+
cmd = ["gpg", "--verify", sig_file, targetfile]
94+
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
95+
print("Signature is valid")
96+
os.remove(key_file)
97+
os.remove(sig_file)
98+
return True
99+
except subprocess.CalledProcessError as e:
100+
os.remove(key_file)
101+
os.remove(sig_file)
102+
raise Exception("Failed to verify signature. Check target file is valid. \n%s" % e.stderr.decode("utf-8"))
103+
104+
def download_file(url):
105+
with urlopen(url) as response:
106+
return response.read()
107+
71108
def unpack(packedfile, parent_path):
72109
"""Unpacks packedfile into parent_path. Assumes .zip. Returns parent_path"""
73110
if zipfile.is_zipfile(packedfile):

tools/dep_updaters/update-icu.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ if [ -n "$CHECKSUM" ]; then
5757
echo "Skipped because checksums do not match."
5858
exit 0
5959
fi
60+
perl -i -pe "s|\"(md5\|gpg)\": .*|\"md5\": \"$CHECKSUM\"|" "$TOOLS_DIR/icu/current_ver.dep"
6061
else
6162
echo "Checksum not found"
6263
echo "check with gpg"
@@ -67,10 +68,11 @@ else
6768
if gpg --verify signature.asc data.tgz; then
6869
echo "Signature verified"
6970
rm data.tgz signature.asc KEYS
71+
perl -i -pe "s|\"(gpg\|md5)\": .*|\"gpg\": { \"key\": \"$KEY_URL\", \"asc\": \"$NEW_VERSION_TGZ_ASC_URL\" }|" "$TOOLS_DIR/icu/current_ver.dep"
7072
else
7173
echo "Skipped because signature verification failed."
7274
rm data.tgz signature.asc KEYS
73-
exit 1
75+
exit 0
7476
fi
7577
fi
7678

@@ -82,7 +84,6 @@ rm -rf "$DEPS_DIR/icu"
8284

8385
perl -i -pe "s|\"url\": .*|\"url\": \"$NEW_VERSION_TGZ_URL\",|" "$TOOLS_DIR/icu/current_ver.dep"
8486

85-
perl -i -pe "s|\"md5\": .*|\"md5\": \"$CHECKSUM\"|" "$TOOLS_DIR/icu/current_ver.dep"
8687

8788
rm -rf out "$DEPS_DIR/icu" "$DEPS_DIR/icu4c*"
8889

0 commit comments

Comments
 (0)