2021-10-31 11:53:20 -06:00
|
|
|
from collections import defaultdict
|
|
|
|
from functools import wraps
|
|
|
|
from pathlib import Path
|
2021-11-07 15:35:23 -06:00
|
|
|
from random import choice
|
|
|
|
from random import randint
|
2021-10-31 11:53:20 -06:00
|
|
|
from shutil import copytree
|
2021-11-07 15:35:23 -06:00
|
|
|
from string import ascii_letters
|
|
|
|
from string import digits
|
|
|
|
from string import hexdigits
|
|
|
|
from string import punctuation
|
2021-11-04 12:26:11 -06:00
|
|
|
from subprocess import PIPE
|
|
|
|
from subprocess import Popen
|
|
|
|
from tempfile import NamedTemporaryFile
|
2021-10-31 11:53:20 -06:00
|
|
|
from tempfile import TemporaryDirectory
|
2021-11-01 11:37:00 -06:00
|
|
|
from typing import Any
|
|
|
|
from typing import Callable
|
2021-10-31 11:53:20 -06:00
|
|
|
from typing import Dict
|
2021-11-01 11:37:00 -06:00
|
|
|
from typing import Generator
|
2021-10-31 11:53:20 -06:00
|
|
|
from typing import List
|
2021-11-01 11:37:00 -06:00
|
|
|
from typing import Optional
|
2021-10-31 11:53:20 -06:00
|
|
|
from typing import Set
|
|
|
|
|
|
|
|
from pytest import fixture
|
|
|
|
|
|
|
|
from libkeyringctl.keyring import convert_certificate
|
2021-11-04 12:26:11 -06:00
|
|
|
from libkeyringctl.keyring import export
|
2021-11-07 14:52:33 -06:00
|
|
|
from libkeyringctl.keyring import get_fingerprints_from_keyring_files
|
2021-10-31 11:53:20 -06:00
|
|
|
from libkeyringctl.sequoia import certify
|
|
|
|
from libkeyringctl.sequoia import key_extract_certificate
|
|
|
|
from libkeyringctl.sequoia import key_generate
|
2021-11-03 13:38:29 -06:00
|
|
|
from libkeyringctl.sequoia import keyring_merge
|
|
|
|
from libkeyringctl.sequoia import packet_join
|
2021-10-31 11:53:20 -06:00
|
|
|
from libkeyringctl.types import Fingerprint
|
|
|
|
from libkeyringctl.types import Uid
|
|
|
|
from libkeyringctl.types import Username
|
|
|
|
from libkeyringctl.util import cwd
|
2021-11-25 16:31:36 -06:00
|
|
|
from libkeyringctl.util import simplify_uid
|
2021-11-04 12:26:11 -06:00
|
|
|
from libkeyringctl.util import system
|
2021-10-31 11:53:20 -06:00
|
|
|
|
|
|
|
test_keys: Dict[Username, List[Path]] = defaultdict(list)
|
2021-11-03 13:38:29 -06:00
|
|
|
test_key_revocation: Dict[Username, List[Path]] = defaultdict(list)
|
2021-10-31 11:53:20 -06:00
|
|
|
test_certificates: Dict[Username, List[Path]] = defaultdict(list)
|
2021-11-04 12:26:11 -06:00
|
|
|
test_certificate_uids: Dict[Username, List[List[Uid]]] = defaultdict(list)
|
2021-10-31 11:53:20 -06:00
|
|
|
test_keyring_certificates: Dict[Username, List[Path]] = defaultdict(list)
|
|
|
|
test_main_fingerprints: Set[Fingerprint] = set()
|
2021-11-04 12:26:11 -06:00
|
|
|
test_all_fingerprints: Set[Fingerprint] = set()
|
2021-10-31 11:53:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
@fixture(autouse=True)
|
2021-11-01 11:37:00 -06:00
|
|
|
def reset_storage() -> None:
|
2021-10-31 11:53:20 -06:00
|
|
|
test_keys.clear()
|
2021-11-03 13:38:29 -06:00
|
|
|
test_key_revocation.clear()
|
2021-10-31 11:53:20 -06:00
|
|
|
test_certificates.clear()
|
2021-11-04 12:26:11 -06:00
|
|
|
test_certificate_uids.clear()
|
2021-10-31 11:53:20 -06:00
|
|
|
test_keyring_certificates.clear()
|
|
|
|
test_main_fingerprints.clear()
|
2021-11-04 12:26:11 -06:00
|
|
|
test_all_fingerprints.clear()
|
2021-10-31 11:53:20 -06:00
|
|
|
|
|
|
|
|
2021-11-01 11:37:00 -06:00
|
|
|
def create_certificate(
|
|
|
|
username: Username,
|
|
|
|
uids: List[Uid],
|
|
|
|
keyring_type: str = "packager",
|
|
|
|
func: Optional[Callable[..., Any]] = None,
|
|
|
|
) -> Callable[..., Any]:
|
|
|
|
def decorator(decorated_func: Callable[..., None]) -> Callable[..., Any]:
|
2021-10-31 11:53:20 -06:00
|
|
|
@wraps(decorated_func)
|
2021-11-01 11:37:00 -06:00
|
|
|
def wrapper(working_dir: Path, *args: Any, **kwargs: Any) -> None:
|
2021-11-04 12:26:11 -06:00
|
|
|
key_directory = working_dir / "secret" / f"{username}"
|
2021-10-31 11:53:20 -06:00
|
|
|
key_directory.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
key_file: Path = key_directory / f"{username}.asc"
|
|
|
|
key_generate(uids=uids, outfile=key_file)
|
|
|
|
test_keys[username].append(key_file)
|
|
|
|
|
2021-11-04 12:26:11 -06:00
|
|
|
certificate_directory = working_dir / "certificate" / f"{username}"
|
2021-10-31 11:53:20 -06:00
|
|
|
certificate_directory.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
keyring_root: Path = working_dir / "keyring"
|
|
|
|
keyring_root.mkdir(parents=True, exist_ok=True)
|
|
|
|
certificate_file: Path = certificate_directory / f"{username}.asc"
|
|
|
|
|
|
|
|
key_extract_certificate(key=key_file, output=certificate_file)
|
|
|
|
test_certificates[username].append(certificate_file)
|
2021-11-04 12:26:11 -06:00
|
|
|
test_certificate_uids[username].append(uids)
|
2021-10-31 11:53:20 -06:00
|
|
|
|
2021-11-03 13:38:29 -06:00
|
|
|
key_revocation_packet = key_file.parent / f"{key_file.name}.rev"
|
|
|
|
key_revocation_joined = key_file.parent / f"{key_file.name}.joined.rev"
|
|
|
|
key_revocation_cert = key_file.parent / f"{key_file.name}.cert.rev"
|
|
|
|
packet_join(packets=[certificate_file, key_revocation_packet], output=key_revocation_joined)
|
|
|
|
keyring_merge(certificates=[key_revocation_joined], output=key_revocation_cert)
|
|
|
|
test_key_revocation[username].append(key_revocation_cert)
|
|
|
|
|
2021-10-31 11:53:20 -06:00
|
|
|
target_dir = keyring_root / keyring_type
|
|
|
|
|
2021-11-07 14:52:33 -06:00
|
|
|
for fingerprint in get_fingerprints_from_keyring_files(
|
|
|
|
working_dir=working_dir, source=[certificate_file]
|
|
|
|
).keys():
|
|
|
|
test_all_fingerprints.add(fingerprint)
|
|
|
|
|
2021-10-31 11:53:20 -06:00
|
|
|
decomposed_path: Path = convert_certificate(
|
|
|
|
working_dir=working_dir,
|
|
|
|
certificate=certificate_file,
|
|
|
|
keyring_dir=keyring_root / keyring_type,
|
2021-11-07 14:52:33 -06:00
|
|
|
fingerprint_filter=test_all_fingerprints,
|
2021-10-31 11:53:20 -06:00
|
|
|
)
|
|
|
|
user_dir = decomposed_path.parent
|
|
|
|
(target_dir / user_dir.name).mkdir(parents=True, exist_ok=True)
|
|
|
|
copytree(src=user_dir, dst=(target_dir / user_dir.name), dirs_exist_ok=True)
|
|
|
|
test_keyring_certificates[username].append(target_dir / user_dir.name / decomposed_path.name)
|
|
|
|
|
2021-11-04 12:26:11 -06:00
|
|
|
certificate_fingerprint: Fingerprint = Fingerprint(decomposed_path.name)
|
2021-10-31 11:53:20 -06:00
|
|
|
if "main" == keyring_type:
|
2021-11-04 12:26:11 -06:00
|
|
|
test_main_fingerprints.add(certificate_fingerprint)
|
|
|
|
test_all_fingerprints.add(certificate_fingerprint)
|
2021-10-31 11:53:20 -06:00
|
|
|
|
|
|
|
decorated_func(working_dir=working_dir, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
if not func:
|
|
|
|
return decorator
|
|
|
|
return decorator(func)
|
|
|
|
|
|
|
|
|
2021-11-01 11:37:00 -06:00
|
|
|
def create_uid_certification(
|
|
|
|
issuer: Username, certified: Username, uid: Uid, func: Optional[Callable[[Any], None]] = None
|
|
|
|
) -> Callable[..., Any]:
|
|
|
|
def decorator(decorated_func: Callable[..., None]) -> Callable[..., Any]:
|
2021-10-31 11:53:20 -06:00
|
|
|
@wraps(decorated_func)
|
2021-11-01 11:37:00 -06:00
|
|
|
def wrapper(working_dir: Path, *args: Any, **kwargs: Any) -> None:
|
2021-10-31 11:53:20 -06:00
|
|
|
key: Path = test_keys[issuer][0]
|
|
|
|
certificate: Path = test_certificates[certified][0]
|
|
|
|
fingerprint: Fingerprint = Fingerprint(test_keyring_certificates[certified][0].name)
|
|
|
|
issuer_fingerprint: Fingerprint = Fingerprint(test_keyring_certificates[issuer][0].name)
|
2021-11-25 16:31:36 -06:00
|
|
|
simplified_uid = simplify_uid(Uid(uid))
|
2021-10-31 11:53:20 -06:00
|
|
|
|
|
|
|
output: Path = (
|
|
|
|
working_dir
|
|
|
|
/ "keyring"
|
|
|
|
/ "packager"
|
|
|
|
/ certified
|
|
|
|
/ fingerprint
|
|
|
|
/ "uid"
|
|
|
|
/ simplified_uid
|
|
|
|
/ "certification"
|
|
|
|
/ f"{issuer_fingerprint}.asc"
|
|
|
|
)
|
|
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
certify(key, certificate, uid, output)
|
|
|
|
|
|
|
|
decorated_func(working_dir=working_dir, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
if not func:
|
|
|
|
return decorator
|
|
|
|
return decorator(func)
|
|
|
|
|
|
|
|
|
2021-11-03 13:38:29 -06:00
|
|
|
def create_key_revocation(
|
|
|
|
username: Username,
|
|
|
|
keyring_type: str = "packager",
|
|
|
|
func: Optional[Callable[..., Any]] = None,
|
|
|
|
) -> Callable[..., Any]:
|
|
|
|
def decorator(decorated_func: Callable[..., None]) -> Callable[..., Any]:
|
|
|
|
@wraps(decorated_func)
|
|
|
|
def wrapper(working_dir: Path, *args: Any, **kwargs: Any) -> None:
|
|
|
|
|
|
|
|
revocation = test_key_revocation[username][0]
|
|
|
|
|
|
|
|
keyring_root: Path = working_dir / "keyring"
|
|
|
|
keyring_root.mkdir(parents=True, exist_ok=True)
|
|
|
|
target_dir = keyring_root / keyring_type
|
|
|
|
|
|
|
|
decomposed_path: Path = convert_certificate(
|
|
|
|
working_dir=working_dir,
|
|
|
|
certificate=revocation,
|
|
|
|
keyring_dir=keyring_root / keyring_type,
|
2021-11-07 14:52:33 -06:00
|
|
|
fingerprint_filter=test_all_fingerprints,
|
2021-11-03 13:38:29 -06:00
|
|
|
)
|
|
|
|
user_dir = decomposed_path.parent
|
|
|
|
(target_dir / user_dir.name).mkdir(parents=True, exist_ok=True)
|
|
|
|
copytree(src=user_dir, dst=(target_dir / user_dir.name), dirs_exist_ok=True)
|
|
|
|
|
|
|
|
decorated_func(working_dir=working_dir, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
if not func:
|
|
|
|
return decorator
|
|
|
|
return decorator(func)
|
|
|
|
|
|
|
|
|
2021-11-04 12:26:11 -06:00
|
|
|
def create_signature_revocation(
|
|
|
|
issuer: Username, certified: Username, uid: Uid, func: Optional[Callable[[Any], None]] = None
|
|
|
|
) -> Callable[..., Any]:
|
|
|
|
def decorator(decorated_func: Callable[..., None]) -> Callable[..., Any]:
|
|
|
|
@wraps(decorated_func)
|
|
|
|
def wrapper(working_dir: Path, *args: Any, **kwargs: Any) -> None:
|
|
|
|
|
|
|
|
issuer_key: Path = test_keys[issuer][0]
|
|
|
|
keyring_root: Path = working_dir / "keyring"
|
|
|
|
|
|
|
|
keyring_certificate: Path = test_keyring_certificates[certified][0]
|
|
|
|
certified_fingerprint = keyring_certificate.name
|
|
|
|
|
|
|
|
with NamedTemporaryFile(dir=str(working_dir), prefix=f"{certified}", suffix=".asc") as certificate:
|
|
|
|
certificate_path: Path = Path(certificate.name)
|
|
|
|
export(
|
|
|
|
working_dir=working_dir,
|
|
|
|
keyring_root=keyring_root,
|
|
|
|
sources=[keyring_certificate],
|
|
|
|
output=certificate_path,
|
|
|
|
)
|
|
|
|
|
|
|
|
with TemporaryDirectory(prefix="gnupg") as gnupg_home:
|
|
|
|
env = {"GNUPGHOME": gnupg_home}
|
|
|
|
|
|
|
|
print(
|
|
|
|
system(
|
|
|
|
[
|
|
|
|
"gpg",
|
|
|
|
"--no-auto-check-trustdb",
|
|
|
|
"--import",
|
|
|
|
f"{str(issuer_key)}",
|
|
|
|
f"{str(certificate_path)}",
|
|
|
|
],
|
|
|
|
env=env,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
uid_confirmations = ""
|
|
|
|
for cert_uid in test_certificate_uids[certified][0]:
|
|
|
|
if uid == cert_uid:
|
|
|
|
uid_confirmations += "y\n"
|
|
|
|
else:
|
|
|
|
uid_confirmations += "n\n"
|
|
|
|
|
|
|
|
commands = Popen(["echo", "-e", f"{uid_confirmations}y\n0\ny\n\ny\ny\nsave\n"], stdout=PIPE)
|
|
|
|
system(
|
|
|
|
[
|
|
|
|
"gpg",
|
|
|
|
"--no-auto-check-trustdb",
|
|
|
|
"--command-fd",
|
|
|
|
"0",
|
|
|
|
"--expert",
|
|
|
|
"--yes",
|
|
|
|
"--batch",
|
|
|
|
"--edit-key",
|
|
|
|
f"{certified_fingerprint}",
|
|
|
|
"revsig",
|
|
|
|
"save",
|
|
|
|
],
|
|
|
|
_stdin=commands.stdout,
|
|
|
|
env=env,
|
|
|
|
)
|
|
|
|
|
|
|
|
revoked_certificate = system(["gpg", "--armor", "--export", f"{certified_fingerprint}"], env=env)
|
|
|
|
certificate.truncate(0)
|
|
|
|
certificate.seek(0)
|
|
|
|
certificate.write(revoked_certificate.encode())
|
|
|
|
certificate.flush()
|
|
|
|
|
|
|
|
target_dir = keyring_root / "packager"
|
|
|
|
decomposed_path: Path = convert_certificate(
|
|
|
|
working_dir=working_dir,
|
|
|
|
certificate=certificate_path,
|
|
|
|
keyring_dir=target_dir,
|
2021-11-07 14:52:33 -06:00
|
|
|
fingerprint_filter=test_all_fingerprints,
|
2021-11-04 12:26:11 -06:00
|
|
|
)
|
|
|
|
user_dir = decomposed_path.parent
|
|
|
|
(target_dir / user_dir.name).mkdir(parents=True, exist_ok=True)
|
|
|
|
copytree(src=user_dir, dst=(target_dir / user_dir.name), dirs_exist_ok=True)
|
|
|
|
|
|
|
|
decorated_func(working_dir=working_dir, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
if not func:
|
|
|
|
return decorator
|
|
|
|
return decorator(func)
|
|
|
|
|
|
|
|
|
2021-10-31 11:53:20 -06:00
|
|
|
@fixture(scope="function")
|
2021-11-01 11:37:00 -06:00
|
|
|
def working_dir() -> Generator[Path, None, None]:
|
2021-10-31 11:53:20 -06:00
|
|
|
with TemporaryDirectory(prefix="arch-keyringctl-test-") as tempdir:
|
2021-11-01 11:37:00 -06:00
|
|
|
path: Path = Path(tempdir)
|
|
|
|
with cwd(path):
|
|
|
|
yield path
|
2021-10-31 11:53:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
@fixture(scope="function")
|
2021-11-01 11:37:00 -06:00
|
|
|
def keyring_dir(working_dir: Path) -> Generator[Path, None, None]:
|
2021-10-31 11:53:20 -06:00
|
|
|
yield working_dir / "keyring"
|
2021-11-07 15:35:23 -06:00
|
|
|
|
|
|
|
|
|
|
|
def random_string(length: int, chars: str) -> str:
|
|
|
|
return "".join(choice(chars) for x in range(length))
|
|
|
|
|
|
|
|
|
|
|
|
@fixture(scope="function", params=[16, 40], ids=["short_id", "long_id"])
|
|
|
|
def valid_fingerprint(request: Any) -> Generator[str, None, None]:
|
|
|
|
yield random_string(length=request.param, chars="ABCDEF" + digits)
|
|
|
|
|
|
|
|
|
|
|
|
@fixture(scope="function", params=[16, 40], ids=["short_id", "long_id"])
|
|
|
|
def valid_subkey_fingerprint(request: Any) -> Generator[str, None, None]:
|
|
|
|
yield random_string(length=request.param, chars="ABCDEF" + digits)
|
|
|
|
|
|
|
|
|
|
|
|
@fixture(
|
|
|
|
scope="function",
|
|
|
|
params=[
|
|
|
|
(
|
|
|
|
16,
|
|
|
|
ascii_letters + hexdigits + punctuation,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
40,
|
|
|
|
ascii_letters + hexdigits + punctuation,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
randint(0, 15),
|
|
|
|
"ABCDEF" + digits,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
randint(17, 39),
|
|
|
|
"ABCDEF" + digits,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
randint(41, 100),
|
|
|
|
"ABCDEF" + digits,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"short_id_wrong_chars",
|
|
|
|
"long_id_wrong_chars",
|
|
|
|
"right_chars_shorter_than_short_id",
|
|
|
|
"right_chars_shorter_than_long_id_longer_than_short_id",
|
|
|
|
"right_chars_longer_than_long_id",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def invalid_fingerprint(request: Any) -> Generator[str, None, None]:
|
|
|
|
yield random_string(length=request.param[0], chars=request.param[1])
|