gpg-email-helper/gpg-email-helper.py
2024-02-28 14:13:22 -06:00

112 lines
4.3 KiB
Python

import gpg
import hkp4py
import os
import sys
XDG_DATA_HOME = os.getenv('XDG_DATA_HOME')
HOME = os.path.expanduser('~')
if XDG_DATA_HOME == None:
XDG_DATA_HOME = f'{HOME}/.local/share'
filename = sys.argv[-1]
email = ''.join(open(filename, 'rt').readlines())
if 'gpg-email-helper' not in os.listdir(f'{XDG_DATA_HOME}'):
os.mkdir(f'{XDG_DATA_HOME}/gpg-email-helper')
with open(f'{XDG_DATA_HOME}/gpg-email-helper/log', 'a') as log:
log.write(f'\n\nRunning on {filename}\n')
if (
email.rfind('-----BEGIN PGP PUBLIC KEY BLOCK-----') != -1
and email.rfind('-----END PGP PUBLIC KEY BLOCK-----') != -1
):
public_key = email[
email.rfind('-----BEGIN PGP PUBLIC KEY BLOCK-----') : email.rfind(
'-----END PGP PUBLIC KEY BLOCK-----'
)
+ 35
]
# quoted-printable attachments have the equals sign escaped as =3D apparently
public_key = public_key.replace('=3D', '=')
log.write(
f'Attempted to import key from email: {gpg.Context(armor=True).key_import(public_key.encode())}\n'
)
elif (
email.rfind('-----BEGIN PGP SIGNATURE-----') != -1
and email.rfind('-----END PGP SIGNATURE-----') != -1
):
signature = email[
email.rfind('-----BEGIN PGP SIGNATURE-----') : email.rfind(
'-----END PGP SIGNATURE-----'
)
+ 27
]
signature.replace('=3D', '=')
try:
# Needed to skip geting the key if it already exists.
gpg.Context().verify(signature.encode())
except gpg.errors.BadSignatures as e:
if 'No public key' in str(e):
fingerprint = str(e)[: str(e).find(':')]
log.write(
f'{fingerprint} not found, gonna try to find it on a keyserver...\n'
)
keyservers = [
'keyserver.ubuntu.com',
'pgpkeys.eu',
'pgp.cyberbits.eu',
'pgp.flexyz.com',
'sks.pgpkeys.eu',
'fi.pgpkeys.eu',
'keys.nicemail.eu',
'pubkeys.intevation.de',
'pgp.surf.nl',
'sks.pod03.togsvcs.com',
'sks.ewr1.newconews.org',
'sks.pod02.fleetstreetops.com',
'sks.pod01.fleetstreetops.com',
'pgp.net.nz',
'zuul.rediris.es',
'pgp.gnd.pw',
'openpgp.circl.lu',
'keywin.trifence.ch',
'data.pgp.gnd.pw',
'sks.infcs.de',
'keyserver.escomposlinux.org',
'keyserver.cert.or.id',
'keyserver1.computer42.org',
'keyserver2.computer42.org',
'keyserver.spline.inf.fu-berlin.de',
'key-server.org',
'pgp.id',
'sks.ygrek.org',
# 'pgp.mit.edu' # still in operation but very unreliable
]
for server in keyservers:
log.write(f'{server}: ')
try:
pubkey = hkp4py.KeyServer(
# This isn't perfect, it's a loose search
# for example searching for 7C79FCBB8372E5DE5B17E09A90D4B9641E092971 on keyserver.ubuntu.com returns the wrong key. But it works 99% of the time.
f'hkp://{server}'
).search(f'0x{fingerprint}')
if pubkey != None:
import_result = gpg.Context().key_import(
pubkey[0].key.encode()
)
log.write(
f'Attempted to import from {server}: {import_result}\n'
)
if type(import_result) == gpg.results.ImportResult:
break
else:
log.write('None\n')
except Exception as e:
log.write(f'[ERROR] {e}\n\n')
except Exception as e:
log.write(f'[ERROR] {e}\n\n')