|
| 1 | +import json |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from subprocess import run |
| 7 | +from urllib.request import urlopen, Request |
| 8 | +from zipfile import ZipFile |
| 9 | + |
| 10 | +try: |
| 11 | + TOOLS = Path(os.environ["SIGN_TOOLS"]) |
| 12 | + if not TOOLS.is_dir(): |
| 13 | + raise KeyError |
| 14 | +except KeyError: |
| 15 | + TOOLS = Path("_sign_tools").absolute() |
| 16 | + |
| 17 | +def download_tool(url, name): |
| 18 | + dest = TOOLS / name |
| 19 | + dest.mkdir(parents=True, exist_ok=True) |
| 20 | + req = Request(url) |
| 21 | + print("Downloading from", req.full_url) |
| 22 | + with urlopen(req) as r: |
| 23 | + with open(dest / "_package.zip", "wb") as f: |
| 24 | + while b := r.read(1024 * 1024): |
| 25 | + f.write(b) |
| 26 | + with ZipFile(dest / "_package.zip", "r") as zf: |
| 27 | + for f in zf.namelist(): |
| 28 | + if not f.replace("\\", "/").startswith("bin/"): |
| 29 | + continue |
| 30 | + if not (dest / f).relative_to(dest): |
| 31 | + print("Attempted to extract outside target directory") |
| 32 | + sys.exit(1) |
| 33 | + (dest / f).parent.mkdir(parents=True, exist_ok=True) |
| 34 | + with open(dest / f, "wb") as f2: |
| 35 | + f2.write(zf.read(f)) |
| 36 | + |
| 37 | +def find_tool(pattern, url): |
| 38 | + tools = list(TOOLS.glob(pattern)) |
| 39 | + if tools: |
| 40 | + return tools[-1] |
| 41 | + if url: |
| 42 | + download_tool(url, pattern.replace("/", "\\").partition("\\")[0]) |
| 43 | + tools = list(TOOLS.glob(pattern)) |
| 44 | + if tools: |
| 45 | + return tools[-1] |
| 46 | + print("Failed to install tool for", pattern.replace("/", "\\").rpartition("\\")[-1]) |
| 47 | + sys.exit(1) |
| 48 | + |
| 49 | +SIGNTOOL = find_tool( |
| 50 | + "sign/bin/*/x64/signtool.exe", |
| 51 | + "https://www.nuget.org/api/v2/package/Microsoft.Windows.SDK.BuildTools/10.0.28000.1721", |
| 52 | +) |
| 53 | +MAKECAT = find_tool( |
| 54 | + "sign/bin/*/x64/makecat.exe", |
| 55 | + None, |
| 56 | +) |
| 57 | +DLIB = find_tool( |
| 58 | + "dlib/bin/x64/Azure.CodeSigning.Dlib.dll", |
| 59 | + "https://www.nuget.org/api/v2/package/Microsoft.ArtifactSigning.Client/1.0.128", |
| 60 | +) |
| 61 | + |
| 62 | + |
| 63 | +print("signtool:", SIGNTOOL) |
| 64 | +print("makecat:", MAKECAT) |
| 65 | +print("dlib:", DLIB) |
| 66 | + |
| 67 | +AAS_DATA = { |
| 68 | + "Endpoint": os.environ["TRUSTED_SIGNING_URI"], |
| 69 | + "CodeSigningAccountName": os.environ["TRUSTED_SIGNING_ACCOUNT"], |
| 70 | + "CertificateProfileName": os.environ["TRUSTED_SIGNING_CERTIFICATE_NAME"], |
| 71 | + "ExcludeCredentials": [ |
| 72 | + "ManagedIdentityCredential", |
| 73 | + "WorkloadIdentityCredential", |
| 74 | + "SharedTokenCacheCredential", |
| 75 | + "VisualStudioCredential", |
| 76 | + "VisualStudioCodeCredential", |
| 77 | + "AzureCliCredential", |
| 78 | + "AzurePowerShellCredential", |
| 79 | + "AzureDeveloperCliCredential", |
| 80 | + "InteractiveBrowserCredential" |
| 81 | + ] |
| 82 | +} |
| 83 | + |
| 84 | +with open(TOOLS / "metadata.json", "w", encoding="utf-8") as f: |
| 85 | + json.dump(AAS_DATA, f, indent=2) |
| 86 | + |
| 87 | +CAT = Path.cwd() / (Path(sys.argv[1]).stem + ".cat") |
| 88 | + |
| 89 | +with open(TOOLS / "files.cdf", "w", encoding="ansi") as f: |
| 90 | + print("[CatalogHeader]", file=f) |
| 91 | + print("Name=", CAT.name, sep="", file=f) |
| 92 | + print("ResultDir=", CAT.parent, sep="", file=f) |
| 93 | + print("PublicVersion=0x00000001", file=f) |
| 94 | + print("CatalogVersion=2", file=f) |
| 95 | + print("HashAlgorithms=SHA256", file=f) |
| 96 | + print("EncodingType=", file=f) |
| 97 | + print(file=f) |
| 98 | + print("[CatalogFiles]", file=f) |
| 99 | + for a in map(Path, sys.argv[1:]): |
| 100 | + print("<HASH>", a.name, "=", a.absolute(), sep="", file=f) |
| 101 | + |
| 102 | +if CAT.is_file(): |
| 103 | + CAT.unlink() |
| 104 | + |
| 105 | +args = [MAKECAT, "-v", TOOLS / "files.cdf"] |
| 106 | +print("##[command]", end="") |
| 107 | +print(*args) |
| 108 | +run(args) |
| 109 | + |
| 110 | +if not CAT.is_file(): |
| 111 | + print("Failed to create catalog.") |
| 112 | + sys.exit(2) |
| 113 | + |
| 114 | +args = [ |
| 115 | + SIGNTOOL, "sign", |
| 116 | + "/v", |
| 117 | + "/fd", "sha256", |
| 118 | + "/tr", "http://timestamp.acs.microsoft.com", |
| 119 | + "/td", "SHA256", |
| 120 | + "/dlib", DLIB, |
| 121 | + "/dmdf", TOOLS / "metadata.json", |
| 122 | + CAT |
| 123 | +] |
| 124 | + |
| 125 | +print("##[command]", end="") |
| 126 | +print(*args) |
| 127 | +run(args) |
0 commit comments