|
| 1 | +# Copyright (c) 2024 E. Castedo Ellerman <[email protected]> |
| 2 | + |
| 3 | +# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU |
| 4 | +# General Public License as public by the Free Software Foundation; version 2.0 |
| 5 | +# or (at your option) any later version. You can redistribute it and/or |
| 6 | +# modify it under the terms of either of these two licenses. |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# |
| 14 | +# You should have received a copy of the licenses; if not, see |
| 15 | +# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License |
| 16 | +# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache |
| 17 | +# License, Version 2.0. |
| 18 | +# fmt: off |
| 19 | + |
| 20 | +import subprocess |
| 21 | +import tempfile |
| 22 | +from datetime import datetime, timezone |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +import dulwich.repo |
| 26 | + |
| 27 | +from ..objects import InvalidSignature, SignatureCriterion |
| 28 | + |
| 29 | +# See the following C git implementation code for more details: |
| 30 | +# https://archive.softwareheritage.org/swh:1:cnt:07335987a6b9ceaf6edc2da71c2e636b0513372f;origin=https://github.com/git/git;visit=swh:1:snp:e72051ba1b2437b7bf3ed0346d04b289f1393982;anchor=swh:1:rev:6a11438f43469f3815f2f0fc997bd45792ff04c0;path=/gpg-interface.c;lines=450 |
| 31 | + |
| 32 | +### WARNING! |
| 33 | +### verify_time might or might not be in UTC. |
| 34 | +### The following code might not be handling timezone correctly. |
| 35 | + |
| 36 | + |
| 37 | +class SshKeygenCheckCriterion(SignatureCriterion): |
| 38 | + """Checks signature using ssh-keygen -Y check-novalidate.""" |
| 39 | + |
| 40 | + def __init__(self, capture_output: bool = True): |
| 41 | + self.capture_output = capture_output |
| 42 | + |
| 43 | + def _ssh_keygen_check( |
| 44 | + self, subcmdline: list[str], crypto_msg: bytes, verify_time: int |
| 45 | + ) -> None: |
| 46 | + verify_dt = datetime.fromtimestamp(verify_time, tz=timezone.utc) |
| 47 | + cmdline = [ |
| 48 | + *subcmdline, |
| 49 | + "-n", "git", |
| 50 | + "-O", "verify-time=" + verify_dt.strftime("%Y%m%d%H%M%SZ"), |
| 51 | + ] |
| 52 | + result = subprocess.run( |
| 53 | + cmdline, input=crypto_msg, capture_output=self.capture_output |
| 54 | + ) |
| 55 | + if 0 != result.returncode: |
| 56 | + raise InvalidSignature |
| 57 | + |
| 58 | + def check(self, crypto_msg: bytes, signature: bytes, verify_time: int) -> None: |
| 59 | + with tempfile.NamedTemporaryFile() as sig_file: |
| 60 | + sig_file.write(signature) |
| 61 | + sig_file.flush() |
| 62 | + subcmdline = ["ssh-keygen", "-Y", "check-novalidate", "-s", sig_file.name] |
| 63 | + self._ssh_keygen_check(subcmdline, crypto_msg, verify_time) |
| 64 | + |
| 65 | + |
| 66 | +class SshKeygenVerifyCriterion(SshKeygenCheckCriterion): |
| 67 | + """Verifies signature using ssh-keygen -Y verify.""" |
| 68 | + |
| 69 | + def __init__(self, allowed_signers: Path, capture_output: bool = True): |
| 70 | + super().__init__(capture_output) |
| 71 | + self.allowed_signers = str(allowed_signers) |
| 72 | + |
| 73 | + def check(self, crypto_msg: bytes, signature: bytes, verify_time: int) -> None: |
| 74 | + with tempfile.NamedTemporaryFile() as sig_file: |
| 75 | + sig_file.write(signature) |
| 76 | + sig_file.flush() |
| 77 | + cmdline = [ |
| 78 | + "ssh-keygen", "-Y", "find-principals", |
| 79 | + "-s", sig_file.name, |
| 80 | + "-f", self.allowed_signers, |
| 81 | + ] |
| 82 | + result = subprocess.run(cmdline, capture_output=True) |
| 83 | + for principal in result.stdout.splitlines(): |
| 84 | + subcmdline = [ |
| 85 | + "ssh-keygen", "-Y", "verify", |
| 86 | + "-f", self.allowed_signers, |
| 87 | + "-I", str(principal), |
| 88 | + "-s", sig_file.name, |
| 89 | + ] |
| 90 | + self._ssh_keygen_check(subcmdline, crypto_msg, verify_time) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + import argparse |
| 95 | + |
| 96 | + parser = argparse.ArgumentParser() |
| 97 | + parser.add_argument("git_object", default="HEAD", nargs="?") |
| 98 | + parser.add_argument("--allow", type=Path, help="ssh-keygen allowed signers file") |
| 99 | + args = parser.parse_args() |
| 100 | + |
| 101 | + if args.allow is None: |
| 102 | + criterion = SshKeygenCheckCriterion(capture_output=False) |
| 103 | + else: |
| 104 | + criterion = SshKeygenVerifyCriterion(args.allow, capture_output=False) |
| 105 | + |
| 106 | + repo = dulwich.repo.Repo(".") |
| 107 | + commit = repo[args.git_object.encode()] |
| 108 | + print("commit", commit.id.decode()) |
| 109 | + try: |
| 110 | + commit.check_signature(criterion) |
| 111 | + # signature good |
| 112 | + except InvalidSignature: |
| 113 | + pass |
| 114 | + print("Author:", commit.author.decode()) |
| 115 | + print("\n ", commit.message.decode()) |
0 commit comments