ProtonMail / go-crypto

Fork of go/x/crypto, providing an up-to-date OpenPGP implementation
https://pkg.go.dev/github.com/ProtonMail/go-crypto
BSD 3-Clause "New" or "Revised" License
328 stars 99 forks source link

Version 2 upgrade of go-crypto #182

Closed lubux closed 7 months ago

lubux commented 1 year ago

This pull request includes the version 2 upgrade in a subfolder in openpgp and the v6 changes from #161. See here for the v2 changes history.

Version 2 Update

This document summarizes the major changes for reading and writing OpenPGP message introduced by ProtonMail/go-crypto/openpgp/v2 compared to ProtonMail/go-crypto/openpgp with the v6 update (#161.).

Import

The v2 API can be imported as:

openpgp ProtonMail/go-crypto/openpgp/v2

API breaking changes

openpgp.v2.Entity struct fields have changed:

openpgp.v2.Entity changed API methods:

openpgp.v2.Entity removed API methods:

openpgp.v2.Subkey struct fields have changed:

openpgp.v2.Subkey changed API methods:

openpgp.v2.Identity struct fields have changed:

openpgp.v2.Identity changed API methods:

openpgp.v2.Key struct fields have changed:

openpgp.v2.KeyRing interface has has changed:

openpgp.v2.FileHints struct field has changed:

openpgp.v2 API changes for reading messages:

openpgp.v2 API changes for writing messages:

Features added

Intended recipients

Version 2 of the ProtonMail/go-crypto library introduces a feature for including the recipients' key fingerprints in signatures during message encryption. When encrypting and signing a message, the intended recipients are automatically included in the signature, unless specifically hidden (i.e., hidden recipients). During the decryption process, if the signature contains intended recipients and the appropriate configuration flag is set, the library verifies whether the primary ID of the decryption key is present in the recipient list. This check can be disabled in the config when a hidden recipient decrypts the message.

Multi-signature support

In previous iterations of ProtonMail/go-crypto, only a single signature creation and verification were supported in a PGP message. However, in Version 2, the library introduces the ability to sign messages with multiple signatures using different keys, such as a v4 and a v6 key. The encryption and signing methods now accept multiple signing keys as arguments, with each key designated for a specific signature. When reading PGP messages with Version 2, the library maintains an internal state for each known signature and verifies all of them within the message. To facilitate this functionality, the message details struct includes a new field that stores the verification state for each signature. A message is considered valid if at least one of the signatures successfully validates without any errors. For callers, the process of checking for signature errors remains similar to previous versions. However, if the caller requires the verification state of all signatures, they can utilize the new field in the message details struct.

Rework how signatures in keys and signatures are verified

In previous iterations of ProtonMail/go-crypto, key verification occurred during import based on the current time, while signature verification did not involve further key checks. However, this approach had limitations, as invalid keys could have been valid at the time of signature creation and mistakenly considered invalid.

Version 2 changes how and when signatures are verified in keys (i.e., direct-signatures, self-signatures of userids, binding signatures in subkeys, revocations, etc). Unlike before, key signature verification no longer takes place during parsing. Instead, keys are now validated when they are utilized, following a similar approach to key handling in OpenPGP.js. Additionally, all signatures and expirations are validated to adhere to the key lifecycle outlined in the RFC. The validity of keys can now be checked at different points in time, leading to the following specific modifications:

Further, version 2 includes various small improvements to increase the robustness of the key parsing functions.

Weak algorithm rejection

Version 2 introduces the option to specify weak algorithms for signatures in the config. Signatures that use weak algorithms are considered invalid.

Optional packet sequence checker

Version 2 introduces a new feature that enables the validation of packet sequences in PGP messages. This functionality can be enabled in the config struct. In particular, it implements the pushdown automata (PDA) from PGPainless, developed by Paul Schaub. By leveraging this feature, users can ensure that the packet sequences in their PGP messages are valid and comply with the required structure. This addition further enhances the overall reliability and security of PGP message handling in Version 2.

Session key encryption and decryption

Version 2 allows advanced users to retrieve the session key while encrypting a message by setting the respective flag in the config. In decryption, a caller can provide a session key that should be used for decryption.

Unify write/read API

Version 2 improves the compatibility between different APIs to allow combinations. The DetachSign function requires the caller to provide a Reader for the message, while encrypt returns a WriteCloser to which the message is written to. The new version adds a function DetachSignWriter, which returns a WriteCloser similar to the encryption API. On the reading side, the verify detached signature API now relies on the same signature verification logic as the other read functions. Additionally, a new VerifyDetachedSignatureReader method similar to the ReadMessage API is introduced. It returns a message details struct that once read verifies the signature. Allows to chain different readers from the API, for example, to have a streaming API for encrypted detached signatures.

Params struct as a function argument in the write API

With the inclusion of new features, the write functions in go-crypto experienced significant growth in numbers. Each combination has its dedicated function. Version 2 introduces an EncryptWithParams/SignWithParams function that takes an EncryptParams/SignParams struct as an argument. The struct allows configuring the different features. This approach effectively reduces the number of API methods and simplifies the process of adding new features while maintaining compatibility with previous versions.

Others

lubux commented 9 months ago

@wussler thanks a lot for your time and effort in reviewing this slightly larger PR :sweat_smile: I addressed all of your suggestions and added the commit as a comment in each thread.

Just as a side note: The contents in the v2 folder are copies of the previous v1 root folder without the packages. So, the code is duplicated, and the new changes for v2 are integrated on top.

I will do another pass as well.