openpgp-notes/book/source/09-verification.md
2023-11-24 16:44:41 +01:00

173 lines
12 KiB
Markdown

<!--
SPDX-FileCopyrightText: 2023 The "Notes on OpenPGP" project
SPDX-License-Identifier: CC-BY-SA-4.0
-->
(verification_chapter)=
# Verification
Signature verification in the OpenPGP protocol is a complex process.
There are lots of different factors that can influence the validity of a signature, most importantly its expiration date.
A signature can be valid at one point in time and invalid merely a second later.
Signatures can be invalid due to the absence or presence of other signatures (e.g. revocations).
Some signatures can be verified standalone, while others require the verification of a chain-like structure of other signatures, mostly on the issuers certificate.
## When are signatures valid?
As a necessary condition, a valid signature must be cryptographically correct, meaning the signature, as well as the signed information must be intact.
However, there is a difference between signature *correctness* and *validity*.
A signature might be correct, but still disqualify as a valid signature.
Put mathematically, the set of valid signatures is a subset of the set of correct signatures.
The validity of a correct signature is additionally constrained by a number of conditions:
* well-formedness
Signatures need to be well-formed, meaning they must contain required signature subpackets in the proper subpacket area and must not contain unknown critical subpackets or unknown critical notations.
Note: This also means, that a signature might be considered valid by one implementation and be rejected by another.
Some implementations further apply a policy when verifying signatures, putting constraints on used hash- and key algorithms and key strengths.
* temporal validity
Most signatures have a limited validity period, constrained by the signature creation- and expiration time.
* qualification
Furthermore, some signatures need to be *qualified* by other valid signatures in order to be considered valid.
This is especially the case with signatures created by dedicated signing subkeys, where, in addition to the signature itself, the subkeys binding signature(s) must be verified.
* revocation
Lastly, signatures can be invalidated by revocations.
### Temporal validity
A signature is valid only for a constrained period of time.
A lower constraint for the validity period is the creation time of the signature, meaning a signature only becomes valid after its creation timestamp.
An upper constraint might be the signatures expiration time.
A special case are hard revocation signatures, where the lower constraint is dropped, so hard revocations are valid since the dawn of time.
When checking a signature for validity, a reference time is defined.
For an email that might be the signature creation time itself, or the reception date.
For the signature to qualify as valid, it needs to be effective, in other words, the reference time must fall into the period from signature creation to signature expiration.
The same reference time must be used when verifying additional qualifying signatures.
### Self-qualifying and non-self-qualifying signatures
Some signatures can be verified on their own, while others require the verification of additional signatures on the issuer certificate. We will call the former category *self-qualifying* signatures.
Typically, self-qualifying signatures are self-signatures, meaning signatures issued by an OpenPGP key over its own components.
Examples for self-qualifying signatures are direct-key self-signatures (0x1F), User ID self-certifications (0x10-0x13), key-revocation self-signatures (0x20), certification revocation self-signatures (0x30) or signatures used to bind or revoke subkeys (0x18, 0x19, 0x28).
Examples for signatures which are not self-qualifying are data signatures (0x00, 0x01) and signatures issued over third-party certificates, such as third-party direct-key signatures (0x1F) or key-revocations (0x20), third-party certification or revocation signatures (0x10-0x13, 0x30).
### Signature qualification
To verify non-self-qualifying signatures, it is not sufficient to only look at the signature itself.
The reason is, that the issuer (sub-) key needs to be qualified to create such a signature (e.g. because a special key-flag is required).
This qualification typically comes via another self-signature on the key itself.
Instead, a chain of valid signatures from the signature itself to the primary key of the issuer certificate needs to be established.
For example, a data signature over an email body may be issued by a subkey only if that subkey is validly bound to the users certificate via a subkey binding signature, and that binding signature needs to contain a key flags subpacket marking the subkey as **S**igning capable.
Similarly, certification signatures over third-party certificates require the issuer key to carry a self-signature qualifying it to **C**ertify other keys.
Self-qualifying signatures have no such limitations.
For example, a certificate consisting only of a primary key and a single key-revocation self-signature contains everything needed to verify the revocation, as key-revocation self-signatures are self-qualifying.
This construct is referred to as a [revocation certificate](https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-12.html#name-openpgp-v6-revocation-certi).
On the other hand, in order to verify a data signature over a text document, an implementation would need to verify not only the data signature itself, but also the binding signature (and back-signature) of the signing subkey, which qualify the signing subkey.
```{include} mermaid/09-sigtree.md
```
### Attribute shadowing
When determining preferences of a key, different signatures can be inspected.
For example, when using a signing subkey to generate a data signature, the implementation might want to check for hash algorithm preferences on the subkey binding signature.
At the same time, the specification states, that signature subpackets on the direct-key signature of the OpenPGP keys primary key apply to the whole key (therefore also to the signing subkey).
In this case, the implementation uses the preferences from the subkey binding signature, but if no such subpacket is found on the latest binding signature, it falls back to the preferences of the direct-key signature.
This is called attribute shadowing, since direct-key signature subpackets apply to all subkeys, but are shadowed by binding signature subpackets.
```{figure} drawio/attribute-shadowing.png
Attributes from the primary key's Direct-Key signature apply to the whole certificate, but can be shadowed by binding signatures.
```
Note: Attribute shadowing should only be used for algorithm preferences, since there are subpacket types where shadowing makes no sense (e.g. key expiration time subpackets).
### Signature shadowing
When inspecting signatures on a component of an OpenPGP certificate, only the newest, effective signature for each function is considered.
In other words; If there are three binding signatures `A, B, C` for a subkey, where `A` was created at `t0`, `B` at `t1` and `C` at `t3` with `t0 < t1 < t2 < t3`, at `t2` an implementation only needs to consider `B`, as `C` is not yet effective.
`A` is therefore shadowed.
```{figure} drawio/cert-validity-subkey.png
An example for how certificate validity can change with time.
```
Note: Signature shadowing is not to be mistaken with attribute shadowing.
Attribute- and signature shadowing also combine, so it is not always obvious, what properties a key has at any given time.
```{figure} drawio/dk-attributes-and-shadowing.png
Signatures shadow another, based on reference time.
```
### Revocations
A signature might be *disqualified* by the presence of a revocation signature.
Revocations can be limited in scope, e.g. a subkey-revocation signature only revokes a single subkey.
Moreover, revocations can also be constrained to a certain validity period by including a soft revocation reason and expiration time in the revocation signature.
TODO: Give guidance, which revocations need to be considered for different types of signatures
## Which signatures take precedence?
An OpenPGP certificate or component can have multiple signatures with conflicting information attached to it.
When verifying a non-self-qualifying signature, an implementation needs to identify self-qualifying signatures on the certificate to qualify that signature.
There might be more than one candidate for such a signature.
For example, there might be multiple subkey binding signatures for the same subkey.
In general, for each category of signatures, only that with the latest signature creation time is considered and takes precendence.
Alternatively, there might be competing qualifying signatures of different types, e.g. a direct-key signature and a self-certification signature on a primary User ID.
In this case, depending on how a key is "addressed", different attributes from both candidates "shadow" another.
```
TODO: Replace hash algorithm preferences with AEAD preferences for a more realistic example.
```
For example, the latest direct-key signature could list "SHA512, SHA384" as hash algorithm preferences, while the latest self-certification of User ID "Bob" could list "SHA256" only.
For yet another User ID "Bobby", the self-signature could list no hash algorithm preferences at all.
If the user wants to compose a signed message using the associated OpenPGP key, they need to figure out, which preferences to use.
The specification recommends, that implementations decide which signature takes precendence by the way the certificate is "addressed".
```{figure} drawio/narrow-interpretation.png
Preferrences are sourced from different component signatures, depending on how the key is addressed.
```
If the user wants to write an email as "Bob", it should consider the signature on "Bob", so SHA256 should be used as hash algorithm.
If instead the user wants to write as "Bobby", the impementation should inspect the self-certification on "Bobby" instead.
However, since this signature does not carry any hash algorithm preferences subpacket, the implementation must fall back to the direct-key signature instead.
The same is true, if the certificate is used without any User ID as sender.
But it gets more complicated still.
Algorithm preferences can also "live" on subkey binding signatures, so if the certificate has a dedicated signing subkey, there is yet another signature which could take precendence.
Preferences from the subkey binding signature take precendence over the direct-key signature, but not over self-certifications on the User ID.
TODO: Have a table that lists which signatures take precendence in which cases.
There can be more than one signature on a component. For example, there could be 3 direct-key signatures, e.g. because the user extended the lifespan of their key 2 times already.
In general, for each component, only the newest self-signature is "in effect", and older signatures are "shadowed".
For each certificate, there is at most one "active" direct-key signature, for each User ID at most one active self-certification and for each subkey exactly one subkey binding.
TODO: Direct-Key Signaures can be revoked, canceling them, meaning an older one might get active?
## Complexity of the packet format
Unfortunately, the OpenPGP packet format allows for quite a lot of flexibility when composing certificates.
User ID packets for example, are not fixed with regards to their position, which means that an attacker (or canonicalizer) can change the order in which User IDs appear in the certificates packet sequence.
As a concrete example, consider a certificate with multiple User IDs, all marked as primary. Or equaly, a certificate with multiple User IDs of which none is marked as primary.
Clients might apply different heuristics to figure out, which User ID actually qualifies as the primary User ID here.
You might wonder, which signature on the primary key takes precendence in case of multiple signature candidates with conflicting signature subpackets.