hynek / pem

PEM file parsing in Python.
https://pem.readthedocs.io/
MIT License
159 stars 37 forks source link

Add support for OpenPGP - RFC 4880 #43

Closed adiroiban closed 1 year ago

adiroiban commented 4 years ago

I think that it would be nice if PEM could also support the armored OpenPGP files from https://tools.ietf.org/html/rfc4880

As a start without the support for headers.

Below are the message types for reference,

   BEGIN PGP MESSAGE
       Used for signed, encrypted, or compressed files.

   BEGIN PGP PUBLIC KEY BLOCK
       Used for armoring public keys.

   BEGIN PGP PRIVATE KEY BLOCK
       Used for armoring private keys.

   BEGIN PGP MESSAGE, PART X/Y
       Used for multi-part messages, where the armor is split amongst Y
       parts, and this is the Xth part out of Y.

   BEGIN PGP MESSAGE, PART X
       Used for multi-part messages, where this is the Xth part of an
       unspecified number of parts.  Requires the MESSAGE-ID Armor
       Header to be used.

   BEGIN PGP SIGNATURE
       Used for detached signatures, OpenPGP/MIME signatures, and
       cleartext signatures.  Note that PGP 2.x uses BEGIN PGP MESSAGE
       for detached signatures.

And this is how a PGP public key can look like

-----BEGIN PGP PUBLIC KEY BLOCK-----

mQGiBF6PHJoRBAD5netlVGrX5TKVQ9eoWKD67cwROI0LQHMJAAMxnrhcPogeNcIX
mo8WY+heFg8onJ5aV+bAhzpMRmC2DcVR3Yvoq64eAx8hSUI6eW5lsc+CseakV8Jc
BOJLI+4FVhJf1qgFmTHyBvKhL8IXy80E3bBApVcNTbCHCpBcsys/ga4stwCg3BUl
IFWMdNTkffrNlupUwfqmp8UD/j7ZkusPO0RjAO0gD3FBZMqeR1IfrYlsdW3oTglr
NdjNuPYe1mvT3EiBAoEEthaPE3XR3kY1HQBzoFyCK7DyBUI7AmVFMzybTxm89ufQ
rAr/kWSMY20fYjQZxX4705NvuLa3cGdKnmLP+cPmTHl5jV4+9d3NC58+38lpcfl7
UHk+A/wIvjiVzZgpRGF+BQccpo7JcCzJcXgxACx1lqRKNGDrSnJHUDWDXogy70xE
/kwUPuLTb78mAZsJPsB+d3aL1/SoKeTcveDc+EFfGPbrA+gzLhXj1GVmK4YbCcBz
TCTmVkm1OALfj0GpsP7e91wqLaAvf85sx097SDPkYanEbrE8jbQyRFNBIDEwMjQg
KFdpdGggRUxHLUUgc3Via2V5KSA8ZHNhLTEwMjRAY2hldmFoLmNvbT6IYQQTEQIA
IQUCXo8cmgIbIwYLCQgHAwIGFQgCCQoLAxYCAQIeAQIXgAAKCRC9K1lIgMBFqjh9
AJ9b/ezL1h0vv8WlkWA6wqnos+8tjACfWAGMDA0hXoEyL51G7dhLUyi2+5+5AQ0E
Xo8cmhAEAKRKf9/t3LPvCVrdOo3smQ8+QjB5GTSXFr+9CF7mbfPYp2vq6jcAlK5g
kOAXLdPvWE4DKgZBrK+kRJzs7CC+n5xo7i7nFH634lcsIIi56sYYgvz4NOahd10w
yNavtcIBBCi4qWv+L9UtDld1eXoZxFYyOGrQ6tOKZq0sV30/GZ+3AAMFA/9xxj4G
y72HAKlqP1Bv3n39c6cJZobbXjbG4YOFNfxjiAk9Y1kr857X1GeR4TV/mK4Zg0pE
4vLakIxoJfdYhYR54LdDeHeUC/UM6uiqkYhAsWNukjpQa6WxJT4+X5Vq5feYQRCV
2um4Aj9gEEHckw7RCAS0kvd9y+s5sgDpiVvmy4hJBBgRAgAJBQJejxyaAhsMAAoJ
EL0rWUiAwEWq6LUAoJXpzDlp/9bIQe1gMf39y5FCAZ74AJ4w+IZ6mMMBZKmkq/fD
4i34eph/pg==
=9ybR
-----END PGP PUBLIC KEY BLOCK-----
adiroiban commented 4 years ago

Let me know if you think that it make sense to have support for this RFC and I will implement it.

Thanks!

hynek commented 4 years ago

Sure why not! I guess we need another base class for messages?

adiroiban commented 4 years ago

Yes.... a base OpenPGPPEM or OpenPGPArmor would help to tell if something is from the OpenPGP realm.

Also, I would like to have something similar for SSH... like a base SSHPEMObject and have OpenSSH and SSH.COM and maybe PuTTY (well Putty has a different format) ... to make it easy to tell if some object is from the SSH world.

hynek commented 4 years ago

That too. But what I meant is that currently we afaik only handle private keys and certificates. Messages would be a third kind.

I guess we'll need mixin classes to signal something coming from SSH and OpenPGP, yeah. :|

adiroiban commented 4 years ago

Agree. We need another class for messages and another one for the signature.

Will create a PR for this one as soon as I am done with #46

hynek commented 1 year ago

Pretty sure you’re done with #46 🤓

adiroiban commented 1 year ago

Thanks for the followup

OK. so, as the initial scope, just public and private keys.

   BEGIN PGP PUBLIC KEY BLOCK
       Used for armoring public keys.

   BEGIN PGP PRIVATE KEY BLOCK
       Used for armoring private keys.

with

class OpenPGPPublicKey(PublicKey):
class OpenPGPPrivateKey(PrivateKey):

Would that work?

this is done base on the same approach as class SSHCOMPrivateKey(PrivateKey):

hynek commented 1 year ago

SGTM!

hynek commented 1 year ago

no rush, but do you think you'll get around to it in the next week-ish? I'm thinking to push out a release soon-ish, because I've realized that the API docs on RTD are missing the Twisted APIs.

adiroiban commented 1 year ago

Hi. Most probably I will not have time to work on this in the next months