| 1 |
package v2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 8 |
"github.com/ProtonMail/go-crypto/openpgp/packet" |
| 9 |
) |
| 10 |
|
| 11 |
// Subkey is an additional public key in an Entity. Subkeys can be used for |
| 12 |
// encryption. |
| 13 |
type Subkey struct { |
| 14 |
Primary *Entity |
| 15 |
PublicKey *packet.PublicKey |
| 16 |
PrivateKey *packet.PrivateKey |
| 17 |
Bindings []*packet.VerifiableSignature |
| 18 |
Revocations []*packet.VerifiableSignature |
| 19 |
} |
| 20 |
|
| 21 |
func readSubkey(primary *Entity, packets *packet.Reader, pub *packet.PublicKey, priv *packet.PrivateKey) error { |
| 22 |
subKey := Subkey{ |
| 23 |
PublicKey: pub, |
| 24 |
PrivateKey: priv, |
| 25 |
Primary: primary, |
| 26 |
} |
| 27 |
|
| 28 |
for { |
| 29 |
p, err := packets.Next() |
| 30 |
if err == io.EOF { |
| 31 |
break |
| 32 |
} else if err != nil { |
| 33 |
return errors.StructuralError("subkey signature invalid: " + err.Error()) |
| 34 |
} |
| 35 |
|
| 36 |
sig, ok := p.(*packet.Signature) |
| 37 |
if !ok { |
| 38 |
packets.Unread(p) |
| 39 |
break |
| 40 |
} |
| 41 |
|
| 42 |
if sig.SigType != packet.SigTypeSubkeyBinding && sig.SigType != packet.SigTypeSubkeyRevocation { |
| 43 |
// Ignore signatures with wrong type |
| 44 |
continue |
| 45 |
} |
| 46 |
switch sig.SigType { |
| 47 |
case packet.SigTypeSubkeyRevocation: |
| 48 |
subKey.Revocations = append(subKey.Revocations, packet.NewVerifiableSig(sig)) |
| 49 |
case packet.SigTypeSubkeyBinding: |
| 50 |
subKey.Bindings = append(subKey.Bindings, packet.NewVerifiableSig(sig)) |
| 51 |
} |
| 52 |
} |
| 53 |
primary.Subkeys = append(primary.Subkeys, subKey) |
| 54 |
return nil |
| 55 |
} |
| 56 |
|
| 57 |
// Serialize serializes the subkey and writes it into writer. |
| 58 |
// The includeSecrets flag controls if the secrets should be included in the encoding or not. |
| 59 |
func (s *Subkey) Serialize(w io.Writer, includeSecrets bool) error { |
| 60 |
if includeSecrets { |
| 61 |
if err := s.PrivateKey.Serialize(w); err != nil { |
| 62 |
return err |
| 63 |
} |
| 64 |
} else { |
| 65 |
if err := s.PublicKey.Serialize(w); err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
} |
| 69 |
for _, revocation := range s.Revocations { |
| 70 |
if err := revocation.Packet.Serialize(w); err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
} |
| 74 |
for _, bindingSig := range s.Bindings { |
| 75 |
if err := bindingSig.Packet.Serialize(w); err != nil { |
| 76 |
return err |
| 77 |
} |
| 78 |
} |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
// ReSign resigns the latest valid subkey binding signature with the given config. |
| 83 |
func (s *Subkey) ReSign(config *packet.Config) error { |
| 84 |
selectedSig, err := s.LatestValidBindingSignature(time.Time{}, config) |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
err = selectedSig.SignKey(s.PublicKey, s.Primary.PrivateKey, config) |
| 89 |
if err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
if selectedSig.EmbeddedSignature != nil { |
| 93 |
err = selectedSig.EmbeddedSignature.CrossSignKey(s.PublicKey, s.Primary.PrimaryKey, |
| 94 |
s.PrivateKey, config) |
| 95 |
if err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
} |
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
// Verify checks if the subkey is valid by checking: |
| 103 |
// - that the key is not revoked |
| 104 |
// - that there is valid non-expired binding self-signature |
| 105 |
// - that the subkey is not expired |
| 106 |
// If date is zero (i.e., date.IsZero() == true) the time checks are not performed. |
| 107 |
func (s *Subkey) Verify(date time.Time, config *packet.Config) (selfSig *packet.Signature, err error) { |
| 108 |
selfSig, err = s.LatestValidBindingSignature(date, config) |
| 109 |
if err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
if s.Revoked(selfSig, date) { |
| 113 |
return nil, errors.ErrKeyRevoked |
| 114 |
} |
| 115 |
if !date.IsZero() && s.Expired(selfSig, date) { |
| 116 |
return nil, errors.ErrKeyExpired |
| 117 |
} |
| 118 |
return |
| 119 |
} |
| 120 |
|
| 121 |
// Expired checks if given the selected self-signature if the subkey is expired. |
| 122 |
func (s *Subkey) Expired(selectedSig *packet.Signature, date time.Time) bool { |
| 123 |
return s.PublicKey.KeyExpired(selectedSig, date) || selectedSig.SigExpired(date) |
| 124 |
} |
| 125 |
|
| 126 |
// Revoked returns whether the subkey has been revoked by a self-signature. |
| 127 |
// Note that third-party revocation signatures are not supported. |
| 128 |
func (s *Subkey) Revoked(selfCertification *packet.Signature, date time.Time) bool { |
| 129 |
// Verify revocations first |
| 130 |
for _, revocation := range s.Revocations { |
| 131 |
if revocation.Valid == nil { |
| 132 |
err := s.Primary.PrimaryKey.VerifySubkeyRevocationSignature(revocation.Packet, s.PublicKey) |
| 133 |
valid := err == nil |
| 134 |
revocation.Valid = &valid |
| 135 |
} |
| 136 |
if *revocation.Valid && |
| 137 |
(revocation.Packet.RevocationReason == nil || |
| 138 |
*revocation.Packet.RevocationReason == packet.Unknown || |
| 139 |
*revocation.Packet.RevocationReason == packet.NoReason || |
| 140 |
*revocation.Packet.RevocationReason == packet.KeyCompromised) { |
| 141 |
// If the key is compromised, the key is considered revoked even before the revocation date. |
| 142 |
return true |
| 143 |
} |
| 144 |
if *revocation.Valid && (date.IsZero() || |
| 145 |
!revocation.Packet.SigExpired(date) && |
| 146 |
(selfCertification == nil || |
| 147 |
selfCertification.CreationTime.Unix() <= revocation.Packet.CreationTime.Unix())) { |
| 148 |
return true |
| 149 |
} |
| 150 |
} |
| 151 |
return false |
| 152 |
} |
| 153 |
|
| 154 |
// Revoke generates a subkey revocation signature (packet.SigTypeSubkeyRevocation) for |
| 155 |
// a subkey with the specified reason code and text (RFC4880 section-5.2.3.23). |
| 156 |
// If config is nil, sensible defaults will be used. |
| 157 |
func (s *Subkey) Revoke(reason packet.ReasonForRevocation, reasonText string, config *packet.Config) error { |
| 158 |
// Check that the subkey is valid (not considering expiration) |
| 159 |
if _, err := s.Verify(time.Time{}, config); err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
|
| 163 |
revSig := createSignaturePacket(s.Primary.PrimaryKey, packet.SigTypeSubkeyRevocation, config) |
| 164 |
revSig.RevocationReason = &reason |
| 165 |
revSig.RevocationReasonText = reasonText |
| 166 |
|
| 167 |
if err := revSig.RevokeSubkey(s.PublicKey, s.Primary.PrivateKey, config); err != nil { |
| 168 |
return err |
| 169 |
} |
| 170 |
sig := packet.NewVerifiableSig(revSig) |
| 171 |
valid := true |
| 172 |
sig.Valid = &valid |
| 173 |
s.Revocations = append(s.Revocations, sig) |
| 174 |
return nil |
| 175 |
} |
| 176 |
|
| 177 |
// LatestValidBindingSignature returns the latest valid self-signature of this subkey |
| 178 |
// that is not newer than the provided date. |
| 179 |
// Does not consider signatures/embedded signatures that are expired. |
| 180 |
// If date is zero (i.e., date.IsZero() == true) the expiration checks are not performed. |
| 181 |
// Returns a StructuralError if no valid self-signature is found. |
| 182 |
func (s *Subkey) LatestValidBindingSignature(date time.Time, config *packet.Config) (selectedSig *packet.Signature, err error) { |
| 183 |
for sigIdx := len(s.Bindings) - 1; sigIdx >= 0; sigIdx-- { |
| 184 |
sig := s.Bindings[sigIdx] |
| 185 |
if (date.IsZero() || date.Unix() >= sig.Packet.CreationTime.Unix()) && |
| 186 |
(selectedSig == nil || selectedSig.CreationTime.Unix() < sig.Packet.CreationTime.Unix()) { |
| 187 |
if sig.Valid == nil { |
| 188 |
err := s.Primary.PrimaryKey.VerifyKeySignature(s.PublicKey, sig.Packet) |
| 189 |
if err == nil { |
| 190 |
err = checkSignatureDetails(sig.Packet, date, config) |
| 191 |
} |
| 192 |
valid := err == nil |
| 193 |
sig.Valid = &valid |
| 194 |
} |
| 195 |
mainSigExpired := !date.IsZero() && |
| 196 |
sig.Packet.SigExpired(date) |
| 197 |
embeddedSigExpired := !date.IsZero() && |
| 198 |
sig.Packet.FlagSign && |
| 199 |
sig.Packet.EmbeddedSignature != nil && |
| 200 |
sig.Packet.EmbeddedSignature.SigExpired(date) |
| 201 |
if *sig.Valid && !mainSigExpired && !embeddedSigExpired { |
| 202 |
selectedSig = sig.Packet |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
if selectedSig == nil { |
| 207 |
return nil, errors.StructuralError("no valid binding signature found for subkey") |
| 208 |
} |
| 209 |
return selectedSig, nil |
| 210 |
} |
| 211 |
|
| 212 |
// IsPQ returns true if the algorithm is Post-Quantum safe |
| 213 |
func (s *Subkey) IsPQ() bool { |
| 214 |
switch s.PublicKey.PubKeyAlgo { |
| 215 |
case packet.PubKeyAlgoMlkem768X25519, packet.PubKeyAlgoMlkem1024X448, |
| 216 |
packet.PubKeyAlgoMldsa65Ed25519, packet.PubKeyAlgoMldsa87Ed448, |
| 217 |
packet.PubKeyAlgoSlhdsaShake128s, packet.PubKeyAlgoSlhdsaShake128f, packet.PubKeyAlgoSlhdsaShake256s: |
| 218 |
return true |
| 219 |
default: |
| 220 |
return false |
| 221 |
} |
| 222 |
|
| 223 |
} |