Debian Codegraph @

openpgp/errors/errors.go

raw
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package errors contains common error types for the OpenPGP packages.
6 package errors // import "github.com/ProtonMail/go-crypto/openpgp/errors"
7
8 import (
9 "fmt"
10 "strconv"
11 )
12
13 var (
14 // ErrDecryptSessionKeyParsing is a generic error message for parsing errors in decrypted data
15 // to reduce the risk of oracle attacks.
16 ErrDecryptSessionKeyParsing = DecryptWithSessionKeyError("parsing error")
17 // ErrAEADTagVerification is returned if one of the tag verifications in SEIPDv2 fails
18 ErrAEADTagVerification error = DecryptWithSessionKeyError("AEAD tag verification failed")
19 // ErrMDCHashMismatch
20 ErrMDCHashMismatch error = SignatureError("MDC hash mismatch")
21 // ErrMDCMissing
22 ErrMDCMissing error = SignatureError("MDC packet not found")
23 )
24
25 // A StructuralError is returned when OpenPGP data is found to be syntactically
26 // invalid.
27 type StructuralError string
28
29 func (s StructuralError) Error() string {
30 return "openpgp: invalid data: " + string(s)
31 }
32
33 // A DecryptWithSessionKeyError is returned when a failure occurs when reading from symmetrically decrypted data or
34 // an authentication tag verification fails.
35 // Such an error indicates that the supplied session key is likely wrong or the data got corrupted.
36 type DecryptWithSessionKeyError string
37
38 func (s DecryptWithSessionKeyError) Error() string {
39 return "openpgp: decryption with session key failed: " + string(s)
40 }
41
42 // HandleSensitiveParsingError handles parsing errors when reading data from potentially decrypted data.
43 // The function makes parsing errors generic to reduce the risk of oracle attacks in SEIPDv1.
44 func HandleSensitiveParsingError(err error, decrypted bool) error {
45 if !decrypted {
46 // Data was not encrypted so we return the inner error.
47 return err
48 }
49 // The data is read from a stream that decrypts using a session key;
50 // therefore, we need to handle parsing errors appropriately.
51 // This is essential to mitigate the risk of oracle attacks.
52 if decError, ok := err.(*DecryptWithSessionKeyError); ok {
53 return decError
54 }
55 if decError, ok := err.(DecryptWithSessionKeyError); ok {
56 return decError
57 }
58 return ErrDecryptSessionKeyParsing
59 }
60
61 // UnsupportedError indicates that, although the OpenPGP data is valid, it
62 // makes use of currently unimplemented features.
63 type UnsupportedError string
64
65 func (s UnsupportedError) Error() string {
66 return "openpgp: unsupported feature: " + string(s)
67 }
68
69 // InvalidArgumentError indicates that the caller is in error and passed an
70 // incorrect value.
71 type InvalidArgumentError string
72
73 func (i InvalidArgumentError) Error() string {
74 return "openpgp: invalid argument: " + string(i)
75 }
76
77 // SignatureError indicates that a syntactically valid signature failed to
78 // validate.
79 type SignatureError string
80
81 func (b SignatureError) Error() string {
82 return "openpgp: invalid signature: " + string(b)
83 }
84
85 type signatureExpiredError int
86
87 func (se signatureExpiredError) Error() string {
88 return "openpgp: signature expired"
89 }
90
91 var ErrSignatureExpired error = signatureExpiredError(0)
92
93 type keyExpiredError int
94
95 func (ke keyExpiredError) Error() string {
96 return "openpgp: key expired"
97 }
98
99 var ErrSignatureOlderThanKey error = signatureOlderThanKeyError(0)
100
101 type signatureOlderThanKeyError int
102
103 func (ske signatureOlderThanKeyError) Error() string {
104 return "openpgp: signature is older than the key"
105 }
106
107 var ErrKeyExpired error = keyExpiredError(0)
108
109 type keyIncorrectError int
110
111 func (ki keyIncorrectError) Error() string {
112 return "openpgp: incorrect key"
113 }
114
115 var ErrKeyIncorrect error = keyIncorrectError(0)
116
117 // KeyInvalidError indicates that the public key parameters are invalid
118 // as they do not match the private ones
119 type KeyInvalidError string
120
121 func (e KeyInvalidError) Error() string {
122 return "openpgp: invalid key: " + string(e)
123 }
124
125 type unknownIssuerError int
126
127 func (unknownIssuerError) Error() string {
128 return "openpgp: signature made by unknown entity"
129 }
130
131 var ErrUnknownIssuer error = unknownIssuerError(0)
132
133 type keyRevokedError int
134
135 func (keyRevokedError) Error() string {
136 return "openpgp: signature made by revoked key"
137 }
138
139 var ErrKeyRevoked error = keyRevokedError(0)
140
141 type WeakAlgorithmError string
142
143 func (e WeakAlgorithmError) Error() string {
144 return "openpgp: weak algorithms are rejected: " + string(e)
145 }
146
147 type UnknownPacketTypeError uint8
148
149 func (upte UnknownPacketTypeError) Error() string {
150 return "openpgp: unknown packet type: " + strconv.Itoa(int(upte))
151 }
152
153 type CriticalUnknownPacketTypeError uint8
154
155 func (upte CriticalUnknownPacketTypeError) Error() string {
156 return "openpgp: unknown critical packet type: " + strconv.Itoa(int(upte))
157 }
158
159 // AEADError indicates that there is a problem when initializing or using a
160 // AEAD instance, configuration struct, nonces or index values.
161 type AEADError string
162
163 func (ae AEADError) Error() string {
164 return "openpgp: aead error: " + string(ae)
165 }
166
167 // ErrDummyPrivateKey results when operations are attempted on a private key
168 // that is just a dummy key. See
169 // https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob;f=doc/DETAILS;h=fe55ae16ab4e26d8356dc574c9e8bc935e71aef1;hb=23191d7851eae2217ecdac6484349849a24fd94a#l1109
170 type ErrDummyPrivateKey string
171
172 func (dke ErrDummyPrivateKey) Error() string {
173 return "openpgp: s2k GNU dummy key: " + string(dke)
174 }
175
176 // ErrMalformedMessage results when the packet sequence is incorrect
177 type ErrMalformedMessage string
178
179 func (dke ErrMalformedMessage) Error() string {
180 return "openpgp: malformed message " + string(dke)
181 }
182
183 type messageTooLargeError int
184
185 func (e messageTooLargeError) Error() string {
186 return "openpgp: decompressed message size exceeds provided limit"
187 }
188
189 // ErrMessageTooLarge is returned if the read data from
190 // a compressed packet exceeds the provided limit.
191 var ErrMessageTooLarge error = messageTooLargeError(0)
192
193 // ErrEncryptionKeySelection is returned if encryption key selection fails (v2 API).
194 type ErrEncryptionKeySelection struct {
195 PrimaryKeyId string
196 PrimaryKeyErr error
197 EncSelectionKeyId *string
198 EncSelectionErr error
199 }
200
201 func (eks ErrEncryptionKeySelection) Error() string {
202 prefix := fmt.Sprintf("openpgp: key selection for primary key %s:", eks.PrimaryKeyId)
203 if eks.PrimaryKeyErr != nil {
204 return fmt.Sprintf("%s invalid primary key: %s", prefix, eks.PrimaryKeyErr)
205 }
206 if eks.EncSelectionKeyId != nil {
207 return fmt.Sprintf("%s invalid encryption key %s: %s", prefix, *eks.EncSelectionKeyId, eks.EncSelectionErr)
208 }
209 return fmt.Sprintf("%s no encryption key: %s", prefix, eks.EncSelectionErr)
210 }