Debian Codegraph @

openpgp/v2/user.go

raw
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 // Identity represents an identity claimed by an Entity and zero or more
12 // assertions by other entities about that claim.
13 type Identity struct {
14 Primary *Entity
15 Name string // by convention, has the form "Full Name (comment) <[email protected]>"
16 UserId *packet.UserId
17 SelfCertifications []*packet.VerifiableSignature
18 OtherCertifications []*packet.VerifiableSignature
19 Revocations []*packet.VerifiableSignature
20 }
21
22 func readUser(e *Entity, packets *packet.Reader, pkt *packet.UserId) error {
23 identity := Identity{
24 Primary: e,
25 Name: pkt.Id,
26 UserId: pkt,
27 }
28 for {
29 p, err := packets.NextWithUnsupported()
30 if err == io.EOF {
31 break
32 } else if err != nil {
33 return err
34 }
35
36 unsupportedPacket, unsupported := p.(*packet.UnsupportedPacket)
37 sigCandidate := p
38 if unsupported {
39 sigCandidate = unsupportedPacket.IncompletePacket
40 }
41 sig, ok := sigCandidate.(*packet.Signature)
42 if !ok {
43 // sigCandidate is a not a signature packet, reset and stop.
44 packets.Unread(p)
45 break
46 } else if unsupported {
47 // sigCandidate is a signature packet but unsupported.
48 continue
49 }
50
51 if sig.SigType != packet.SigTypeGenericCert &&
52 sig.SigType != packet.SigTypePersonaCert &&
53 sig.SigType != packet.SigTypeCasualCert &&
54 sig.SigType != packet.SigTypePositiveCert &&
55 sig.SigType != packet.SigTypeCertificationRevocation {
56 // Ignore signatures with wrong type
57 continue
58 }
59
60 if sig.CheckKeyIdOrFingerprint(e.PrimaryKey) {
61 if sig.SigType == packet.SigTypeCertificationRevocation {
62 identity.Revocations = append(identity.Revocations, packet.NewVerifiableSig(sig))
63 } else {
64 identity.SelfCertifications = append(identity.SelfCertifications, packet.NewVerifiableSig(sig))
65 }
66 e.Identities[pkt.Id] = &identity
67 } else {
68 identity.OtherCertifications = append(identity.OtherCertifications, packet.NewVerifiableSig(sig))
69 }
70 }
71 return nil
72 }
73
74 // Serialize serializes the user id to the writer.
75 func (i *Identity) Serialize(w io.Writer) error {
76 if err := i.UserId.Serialize(w); err != nil {
77 return err
78 }
79 for _, sig := range i.Revocations {
80 if err := sig.Packet.Serialize(w); err != nil {
81 return err
82 }
83 }
84 for _, sig := range i.SelfCertifications {
85 if err := sig.Packet.Serialize(w); err != nil {
86 return err
87 }
88 }
89 for _, sig := range i.OtherCertifications {
90 if err := sig.Packet.Serialize(w); err != nil {
91 return err
92 }
93 }
94 return nil
95 }
96
97 // Verify checks if the user-id is valid by checking:
98 // - that a valid self-certification exists and is not expired
99 // - that user-id has not been revoked at the given point in time
100 // If date is zero (i.e., date.IsZero() == true) the time checks are not performed.
101 func (i *Identity) Verify(date time.Time, config *packet.Config) (selfSignature *packet.Signature, err error) {
102 if selfSignature, err = i.LatestValidSelfCertification(date, config); err != nil {
103 return
104 }
105 if i.Revoked(selfSignature, date, config) {
106 return nil, errors.StructuralError("user-id is revoked")
107 }
108 return
109 }
110
111 // Revoked returns whether the identity has been revoked by a self-signature.
112 // Note that third-party revocation signatures are not supported.
113 func (i *Identity) Revoked(selfCertification *packet.Signature, date time.Time, config *packet.Config) bool {
114 // Verify revocations first
115 for _, revocation := range i.Revocations {
116 if selfCertification == nil || // if there is not selfCertification verify revocation
117 selfCertification.IssuerKeyId == nil ||
118 revocation.Packet.IssuerKeyId == nil ||
119 (*selfCertification.IssuerKeyId == *revocation.Packet.IssuerKeyId) { // check matching key id
120 if revocation.Valid == nil {
121 // Verify revocation signature (not verified yet).
122 err := i.Primary.PrimaryKey.VerifyUserIdSignature(i.Name, i.Primary.PrimaryKey, revocation.Packet)
123 if err == nil {
124 err = checkSignatureDetails(revocation.Packet, date, config)
125 }
126 valid := err == nil
127 revocation.Valid = &valid
128 }
129
130 if *revocation.Valid &&
131 (date.IsZero() || // Check revocation not expired
132 !revocation.Packet.SigExpired(date)) &&
133 (selfCertification == nil || // Check that revocation is not older than the selfCertification
134 selfCertification.CreationTime.Unix() <= revocation.Packet.CreationTime.Unix()) {
135 return true
136 }
137 }
138 }
139 return false
140 }
141
142 // ReSign resigns the latest valid self-certification with the given config.
143 func (i *Identity) ReSign(config *packet.Config) error {
144 selectedSig, err := i.LatestValidSelfCertification(config.Now(), config)
145 if err != nil {
146 return err
147 }
148 if err = selectedSig.SignUserId(
149 i.UserId.Id,
150 i.Primary.PrimaryKey,
151 i.Primary.PrivateKey,
152 config,
153 ); err != nil {
154 return err
155 }
156 return nil
157 }
158
159 // SignIdentity adds a signature to e, from signer, attesting that identity is
160 // associated with e. The provided identity must already be an element of
161 // e.Identities and the private key of signer must have been decrypted if
162 // necessary.
163 // If config is nil, sensible defaults will be used.
164 func (ident *Identity) SignIdentity(signer *Entity, config *packet.Config) error {
165 certificationKey, ok := signer.CertificationKey(config.Now(), config)
166 if !ok {
167 return errors.InvalidArgumentError("no valid certification key found")
168 }
169
170 if certificationKey.PrivateKey.Encrypted {
171 return errors.InvalidArgumentError("signing Entity's private key must be decrypted")
172 }
173
174 if !ok {
175 return errors.InvalidArgumentError("given identity string not found in Entity")
176 }
177
178 sig := createSignaturePacket(certificationKey.PublicKey, packet.SigTypeGenericCert, config)
179
180 signingUserID := config.SigningUserId()
181 if signingUserID != "" {
182 if _, ok := signer.Identities[signingUserID]; !ok {
183 return errors.InvalidArgumentError("signer identity string not found in signer Entity")
184 }
185 sig.SignerUserId = &signingUserID
186 }
187
188 if err := sig.SignUserId(ident.Name, ident.Primary.PrimaryKey, certificationKey.PrivateKey, config); err != nil {
189 return err
190 }
191 ident.OtherCertifications = append(ident.OtherCertifications, packet.NewVerifiableSig(sig))
192 return nil
193 }
194
195 // LatestValidSelfCertification returns the latest valid self-signature of this user-id
196 // that is not newer than the provided date.
197 // Does not consider signatures that are expired.
198 // If date is zero (i.e., date.IsZero() == true) the expiration checks are not performed.
199 // Returns a StructuralError if no valid self-certification is found.
200 func (i *Identity) LatestValidSelfCertification(date time.Time, config *packet.Config) (selectedSig *packet.Signature, err error) {
201 for sigIdx := len(i.SelfCertifications) - 1; sigIdx >= 0; sigIdx-- {
202 sig := i.SelfCertifications[sigIdx]
203 if (date.IsZero() || date.Unix() >= sig.Packet.CreationTime.Unix()) && // SelfCertification must be older than date
204 (selectedSig == nil || selectedSig.CreationTime.Unix() < sig.Packet.CreationTime.Unix()) { // Newer ones are preferred
205 if sig.Valid == nil {
206 // Verify revocation signature (not verified yet).
207 err = i.Primary.PrimaryKey.VerifyUserIdSignature(i.Name, i.Primary.PrimaryKey, sig.Packet)
208 if err == nil {
209 err = checkSignatureDetails(sig.Packet, date, config)
210 }
211 valid := err == nil
212 sig.Valid = &valid
213 }
214 if *sig.Valid && (date.IsZero() || !sig.Packet.SigExpired(date)) {
215 selectedSig = sig.Packet
216 }
217 }
218 }
219 if selectedSig == nil {
220 return nil, errors.StructuralError("no valid certification signature found for identity")
221 }
222 return selectedSig, nil
223 }