| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
"""Parse the output of `gpg --list-keys --with-colons`.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import dataclasses |
| 8 |
import typing |
| 9 |
|
| 10 |
import pyparsing as pyp |
| 11 |
|
| 12 |
from testsigs import defs |
| 13 |
|
| 14 |
|
| 15 |
if typing.TYPE_CHECKING: |
| 16 |
from typing import Final |
| 17 |
|
| 18 |
|
| 19 |
_p_eol = pyp.Char("\r\n").suppress() |
| 20 |
|
| 21 |
_p_ign = pyp.ZeroOrMore(pyp.CharsNotIn(":\n")).suppress() + pyp.Char(":").suppress() |
| 22 |
|
| 23 |
_p_ign_all = pyp.CharsNotIn("\n").suppress() |
| 24 |
|
| 25 |
_p_hex_string = pyp.Word("0123456789ABCDEF") |
| 26 |
|
| 27 |
_p_string = pyp.CharsNotIn(":\n") |
| 28 |
|
| 29 |
_p_ignored_line = pyp.Literal("tru:").suppress() + _p_ign_all + _p_eol |
| 30 |
|
| 31 |
_p_ignored_lines = pyp.ZeroOrMore(_p_ignored_line) |
| 32 |
|
| 33 |
_p_list_pub = ( |
| 34 |
pyp.Literal("pub:").suppress() + _p_ign + _p_ign + _p_ign + _p_hex_string + _p_ign_all + _p_eol |
| 35 |
) |
| 36 |
|
| 37 |
_p_list_fpr = pyp.Literal("fpr:").suppress() + _p_ign * 8 + _p_hex_string + _p_ign_all + _p_eol |
| 38 |
|
| 39 |
_p_list_uid = pyp.Literal("uid:").suppress() + _p_ign * 8 + _p_string + _p_ign_all + _p_eol |
| 40 |
|
| 41 |
_p_list_sub = ( |
| 42 |
pyp.Literal("sub:").suppress() |
| 43 |
+ _p_ign |
| 44 |
+ _p_ign |
| 45 |
+ _p_ign |
| 46 |
+ _p_hex_string |
| 47 |
+ _p_ign * 7 |
| 48 |
+ _p_string |
| 49 |
+ _p_ign_all |
| 50 |
+ _p_eol |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
@_p_list_sub.set_parse_action |
| 55 |
def _parse_list_sub(tokens: pyp.ParseResults) -> defs.Subkey: |
| 56 |
"""Parse a subkey definition.""" |
| 57 |
params: Final = tokens.as_list() |
| 58 |
match params: |
| 59 |
case [key_id, caps]: |
| 60 |
return defs.Subkey(key_id=key_id, fpr="", capabilities=set(caps)) |
| 61 |
|
| 62 |
case _: |
| 63 |
raise RuntimeError(repr(params)) |
| 64 |
|
| 65 |
|
| 66 |
_p_list_subkey = _p_ignored_lines + _p_list_sub + _p_ignored_lines + _p_list_fpr |
| 67 |
|
| 68 |
|
| 69 |
@_p_list_subkey.set_parse_action |
| 70 |
def _parse_list_subkey(tokens: pyp.ParseResults) -> defs.Subkey: |
| 71 |
"""Parse a subkey definition.""" |
| 72 |
params: Final = tokens.as_list() |
| 73 |
match params: |
| 74 |
case [sub_key_id, fpr]: |
| 75 |
return dataclasses.replace(sub_key_id, fpr=fpr) |
| 76 |
|
| 77 |
case _: |
| 78 |
raise RuntimeError(repr(params)) |
| 79 |
|
| 80 |
|
| 81 |
_p_list_pubkey = ( |
| 82 |
_p_ignored_lines |
| 83 |
+ _p_list_pub |
| 84 |
+ _p_ignored_lines |
| 85 |
+ _p_list_fpr |
| 86 |
+ _p_ignored_lines |
| 87 |
+ _p_list_uid |
| 88 |
+ pyp.OneOrMore(_p_list_subkey) |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
@_p_list_pubkey.set_parse_action |
| 93 |
def _parse_list_pubkey(tokens: pyp.ParseResults) -> defs.PublicKey: |
| 94 |
"""Parse a subkey definition.""" |
| 95 |
params: Final = tokens.as_list() |
| 96 |
match params: |
| 97 |
case [key_id, fpr, uid, *subkeys] if isinstance(key_id, str) and isinstance( |
| 98 |
fpr, |
| 99 |
str, |
| 100 |
) and isinstance(uid, str) and subkeys and all( |
| 101 |
isinstance(subkey, defs.Subkey) for subkey in subkeys |
| 102 |
): |
| 103 |
# We did just validate that one... |
| 104 |
return defs.PublicKey( |
| 105 |
key_id=key_id, |
| 106 |
fpr=fpr, |
| 107 |
uid=uid, |
| 108 |
subkeys=subkeys, # type: ignore[arg-type] |
| 109 |
) |
| 110 |
|
| 111 |
case _: |
| 112 |
raise RuntimeError(repr(params)) |
| 113 |
|
| 114 |
|
| 115 |
_p_list_keys = pyp.ZeroOrMore(_p_list_pubkey) + _p_ignored_lines |
| 116 |
|
| 117 |
_p_list_keys_complete = _p_list_keys.leave_whitespace().parse_with_tabs() |
| 118 |
|
| 119 |
|
| 120 |
def parse_list_keys(output: str) -> list[defs.PublicKey]: |
| 121 |
"""Parse the output of `gpg --list-keys`, extract the info we care about.""" |
| 122 |
res: Final = _p_list_keys_complete.parse_string(output, parse_all=True).as_list() |
| 123 |
if not all(isinstance(pkey, defs.PublicKey) for pkey in res): |
| 124 |
raise RuntimeError(repr(res)) |
| 125 |
return res |