aboutsummaryrefslogtreecommitdiff
path: root/b4/kr.py
blob: 9f642bb20009fc4328add7df158698a35b86f896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2020-2021 by the Linux Foundation
#
__author__ = 'Konstantin Ryabitsev <konstantin@linuxfoundation.org>'

import os
import sys
import pathlib
import re

import b4
import b4.mbox


logger = b4.logger


def main(cmdargs):
    msgid, msgs = b4.mbox.get_msgs(cmdargs)
    if cmdargs.showkeys:
        logger.info('---')
        try:
            import patatt
        except ModuleNotFoundError:
            logger.info('--show-keys requires the patatt library')
            sys.exit(1)

        keydata = set()
        for msg in msgs:
            xdk = msg.get('x-developer-key')
            xds = msg.get('x-developer-signature')
            if not xdk or not xds:
                continue
            # grab the selector they used
            kdata = b4.LoreMessage.get_parts_from_header(xdk)
            sdata = b4.LoreMessage.get_parts_from_header(xds)
            algo = kdata.get('a')
            identity = kdata.get('i')
            selector = sdata.get('s', 'default')
            if algo == 'openpgp':
                keyinfo = kdata.get('fpr')
            elif algo == 'ed25519':
                keyinfo = kdata.get('pk')
            else:
                logger.debug('Unknown key type: %s', algo)
                continue
            keydata.add((identity, algo, selector, keyinfo))

        if not keydata:
            logger.info('No keys found in the thread.')
            sys.exit(0)
        krpath = os.path.join(b4.get_data_dir(), 'keyring')
        pgp = False
        ecc = False
        for identity, algo, selector, keyinfo in keydata:
            keypath = patatt.make_pkey_path(algo, identity, selector)
            fullpath = os.path.join(krpath, keypath)
            if os.path.exists(fullpath):
                status = 'known'
            else:
                status = 'unknown'
                if algo == 'openpgp':
                    try:
                        uids = b4.get_gpg_uids(keyinfo)
                        if len(uids):
                            status = 'in default keyring'
                    except KeyError:
                        pass
            pathlib.Path(os.path.dirname(fullpath)).mkdir(parents=True, exist_ok=True)

            logger.info('%s: (%s)', identity, status)
            logger.info('    keytype: %s', algo)
            if algo == 'openpgp':
                pgp = True
                logger.info('      keyid: %s', keyinfo[-16:])
                logger.info('        fpr: %s', ':'.join(re.findall(r'.{4}', keyinfo)))
            else:
                ecc = True
                logger.info('     pubkey: %s', keyinfo)
            logger.info('     krpath: %s', keypath)
            logger.info('   fullpath: %s', fullpath)
        logger.info('---')
        if pgp:
            logger.info('For openpgp keys:')
            logger.info('    gpg --recv-key [keyid]')
            logger.info('    gpg -a --export [keyid] > [fullpath]')
        if ecc:
            logger.info('For ed25519 keys:')
            logger.info('    echo [pubkey] > [fullpath]')

        sys.exit(0)