| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
"""Run a stateless OpenPGP command-line tool.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import subprocess # noqa: S404 |
| 8 |
import typing |
| 9 |
|
| 10 |
from testsigs.parse import gnupg as p_gnupg |
| 11 |
|
| 12 |
from . import base |
| 13 |
|
| 14 |
|
| 15 |
if typing.TYPE_CHECKING: |
| 16 |
import pathlib |
| 17 |
from typing import Final |
| 18 |
|
| 19 |
from testsigs import defs |
| 20 |
|
| 21 |
|
| 22 |
def _gnupg_list_keys(cfg: defs.Config) -> list[defs.PublicKey]: |
| 23 |
"""Run `gpg --list-keys` and partially parse the output.""" |
| 24 |
list_output: Final = subprocess.check_output( # noqa: S603 |
| 25 |
[cfg.prog.gnupg, "--batch", "--with-colons", "--list-keys"], |
| 26 |
cwd=cfg.path.work, |
| 27 |
encoding="UTF-8", |
| 28 |
env=cfg.env, |
| 29 |
) |
| 30 |
print(list_output) # noqa: T201 |
| 31 |
return p_gnupg.parse_list_keys(list_output) |
| 32 |
|
| 33 |
|
| 34 |
class GnuPGCli(base.PGPCli): |
| 35 |
"""Invoke GnuPG as far as we can in batch mode.""" |
| 36 |
|
| 37 |
def generate_keys(self, uid: str) -> defs.KeyFiles: |
| 38 |
"""Generate an OpenPGP key to sign with.""" |
| 39 |
# GnuPG makes it... difficult to generate keys in an unattended manner. |
| 40 |
raise NotImplementedError |
| 41 |
|
| 42 |
def import_secret_key(self, keys: defs.KeyFiles, uid: str) -> defs.PublicKey: |
| 43 |
"""Import a secret key, return the parts of the public key we care about.""" |
| 44 |
no_keys: Final = _gnupg_list_keys(self.cfg) |
| 45 |
if no_keys: |
| 46 |
raise RuntimeError(repr((self.cfg.prog.gnupg, no_keys))) |
| 47 |
|
| 48 |
subprocess.check_call( # noqa: S603 |
| 49 |
[self.cfg.prog.gnupg, "--import", keys.secret_bin], |
| 50 |
cwd=self.cfg.path.work, |
| 51 |
env=self.cfg.env, |
| 52 |
) |
| 53 |
|
| 54 |
single_key: Final = _gnupg_list_keys(self.cfg) |
| 55 |
if len(single_key) != 1 or single_key[0].uid != uid: |
| 56 |
raise RuntimeError(repr((self.cfg.prog.gnupg, single_key))) |
| 57 |
return single_key[0] |
| 58 |
|
| 59 |
def sign_detached(self, _keys: defs.KeyFiles, datafile: pathlib.Path) -> pathlib.Path: |
| 60 |
"""Create an ASCII-armored detached signature, return the path to the new file.""" |
| 61 |
signature: Final = datafile.with_name(datafile.name + ".asc") |
| 62 |
subprocess.check_call( # noqa: S603 |
| 63 |
[self.cfg.prog.gnupg, "-ab", datafile.name], |
| 64 |
cwd=self.cfg.path.work, |
| 65 |
env=self.cfg.env, |
| 66 |
) |
| 67 |
if not signature.is_file(): |
| 68 |
raise RuntimeError(repr((datafile, signature))) |
| 69 |
return signature |
| 70 |
|
| 71 |
def verify_detached( |
| 72 |
self, |
| 73 |
_keys: defs.KeyFiles, |
| 74 |
_pub_key: defs.PublicKey, |
| 75 |
datafile: pathlib.Path, |
| 76 |
signature: pathlib.Path, |
| 77 |
) -> None: |
| 78 |
"""Verify an OpenPGP detached signature in a file.""" |
| 79 |
subprocess.check_call( # noqa: S603 |
| 80 |
[self.cfg.prog.gnupg, "--verify", signature, datafile], |
| 81 |
cwd=self.cfg.path.work, |
| 82 |
env=self.cfg.env, |
| 83 |
) |