| 1 |
// Copyright 2012 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 packet |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto" |
| 9 |
"crypto/rand" |
| 10 |
"io" |
| 11 |
"math/big" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/ProtonMail/go-crypto/openpgp/s2k" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
defaultRejectPublicKeyAlgorithms = map[PublicKeyAlgorithm]bool{ |
| 19 |
PubKeyAlgoElGamal: true, |
| 20 |
PubKeyAlgoDSA: true, |
| 21 |
} |
| 22 |
defaultRejectHashAlgorithms = map[crypto.Hash]bool{ |
| 23 |
crypto.MD5: true, |
| 24 |
crypto.RIPEMD160: true, |
| 25 |
} |
| 26 |
defaultRejectMessageHashAlgorithms = map[crypto.Hash]bool{ |
| 27 |
crypto.SHA1: true, |
| 28 |
crypto.MD5: true, |
| 29 |
crypto.RIPEMD160: true, |
| 30 |
} |
| 31 |
defaultRejectCurves = map[Curve]bool{ |
| 32 |
CurveSecP256k1: true, |
| 33 |
} |
| 34 |
) |
| 35 |
|
| 36 |
// A global feature flag to indicate v5 support. |
| 37 |
// Can be set via a build tag, e.g.: `go build -tags v5 ./...` |
| 38 |
// If the build tag is missing config_v5.go will set it to true. |
| 39 |
// |
| 40 |
// Disables parsing of v5 keys and v5 signatures. |
| 41 |
// These are non-standard entities, which in the crypto-refresh have been superseded |
| 42 |
// by v6 keys, v6 signatures and SEIPDv2 encrypted data, respectively. |
| 43 |
var V5Disabled = false |
| 44 |
|
| 45 |
// Config collects a number of parameters along with sensible defaults. |
| 46 |
// A nil *Config is valid and results in all default values. |
| 47 |
type Config struct { |
| 48 |
// Rand provides the source of entropy. |
| 49 |
// If nil, the crypto/rand Reader is used. |
| 50 |
// Since Go 1.26, standard library calls (e.g., key generation) ignore Rand |
| 51 |
// unless GODEBUG=cryptocustomrand=1 is set. |
| 52 |
Rand io.Reader |
| 53 |
// DefaultHash is the default hash function to be used. |
| 54 |
// If zero, SHA-256 is used. |
| 55 |
DefaultHash crypto.Hash |
| 56 |
// DefaultCipher is the cipher to be used. |
| 57 |
// If zero, AES-128 is used. |
| 58 |
DefaultCipher CipherFunction |
| 59 |
// Time returns the current time as the number of seconds since the |
| 60 |
// epoch. If Time is nil, time.Now is used. |
| 61 |
Time func() time.Time |
| 62 |
// DefaultCompressionAlgo is the compression algorithm to be |
| 63 |
// applied to the plaintext before encryption. If zero, no |
| 64 |
// compression is done. |
| 65 |
DefaultCompressionAlgo CompressionAlgo |
| 66 |
// CompressionConfig configures the compression settings. |
| 67 |
CompressionConfig *CompressionConfig |
| 68 |
// S2K (String to Key) config, used for key derivation in the context of secret key encryption |
| 69 |
// and password-encrypted data. |
| 70 |
// If nil, the default configuration is used |
| 71 |
S2KConfig *s2k.Config |
| 72 |
// Iteration count for Iterated S2K (String to Key). |
| 73 |
// Only used if sk2.Mode is nil. |
| 74 |
// This value is duplicated here from s2k.Config for backwards compatibility. |
| 75 |
// It determines the strength of the passphrase stretching when |
| 76 |
// the said passphrase is hashed to produce a key. S2KCount |
| 77 |
// should be between 65536 and 65011712, inclusive. If Config |
| 78 |
// is nil or S2KCount is 0, the value 16777216 used. Not all |
| 79 |
// values in the above range can be represented. S2KCount will |
| 80 |
// be rounded up to the next representable value if it cannot |
| 81 |
// be encoded exactly. When set, it is strongly encrouraged to |
| 82 |
// use a value that is at least 65536. See RFC 4880 Section |
| 83 |
// 3.7.1.3. |
| 84 |
// |
| 85 |
// Deprecated: SK2Count should be configured in S2KConfig instead. |
| 86 |
S2KCount int |
| 87 |
// RSABits is the number of bits in new RSA keys made with NewEntity. |
| 88 |
// If zero, then 2048 bit keys are created. |
| 89 |
RSABits int |
| 90 |
// The public key algorithm to use - will always create a signing primary |
| 91 |
// key and encryption subkey. |
| 92 |
Algorithm PublicKeyAlgorithm |
| 93 |
// Some known primes that are optionally prepopulated by the caller |
| 94 |
RSAPrimes []*big.Int |
| 95 |
// Curve configures the desired packet.Curve if the Algorithm is PubKeyAlgoECDSA, |
| 96 |
// PubKeyAlgoEdDSA, or PubKeyAlgoECDH. If empty Curve25519 is used. |
| 97 |
Curve Curve |
| 98 |
// AEADConfig configures the use of the new AEAD Encrypted Data Packet, |
| 99 |
// defined in the draft of the next version of the OpenPGP specification. |
| 100 |
// If a non-nil AEADConfig is passed, usage of this packet is enabled. By |
| 101 |
// default, it is disabled. See the documentation of AEADConfig for more |
| 102 |
// configuration options related to AEAD. |
| 103 |
// **Note: using this option may break compatibility with other OpenPGP |
| 104 |
// implementations, as well as future versions of this library.** |
| 105 |
AEADConfig *AEADConfig |
| 106 |
// V6Keys configures version 6 key generation. If false, this package still |
| 107 |
// supports version 6 keys, but produces version 4 keys. |
| 108 |
V6Keys bool |
| 109 |
// Minimum RSA key size allowed for key generation and message signing, verification and encryption. |
| 110 |
MinRSABits uint16 |
| 111 |
// Reject insecure algorithms, only works with v2 api |
| 112 |
RejectPublicKeyAlgorithms map[PublicKeyAlgorithm]bool |
| 113 |
RejectHashAlgorithms map[crypto.Hash]bool |
| 114 |
RejectMessageHashAlgorithms map[crypto.Hash]bool |
| 115 |
RejectCurves map[Curve]bool |
| 116 |
// "The validity period of the key. This is the number of seconds after |
| 117 |
// the key creation time that the key expires. If this is not present |
| 118 |
// or has a value of zero, the key never expires. This is found only on |
| 119 |
// a self-signature."" |
| 120 |
// https://tools.ietf.org/html/rfc4880#section-5.2.3.6 |
| 121 |
KeyLifetimeSecs uint32 |
| 122 |
// "The validity period of the signature. This is the number of seconds |
| 123 |
// after the signature creation time that the signature expires. If |
| 124 |
// this is not present or has a value of zero, it never expires." |
| 125 |
// https://tools.ietf.org/html/rfc4880#section-5.2.3.10 |
| 126 |
SigLifetimeSecs uint32 |
| 127 |
// SigningKeyId is used to specify the signing key to use (by Key ID). |
| 128 |
// By default, the signing key is selected automatically, preferring |
| 129 |
// signing subkeys if available. |
| 130 |
SigningKeyId uint64 |
| 131 |
// SigningIdentity is used to specify a user ID (packet Signer's User ID, type 28) |
| 132 |
// when producing a generic certification signature onto an existing user ID. |
| 133 |
// The identity must be present in the signer Entity. |
| 134 |
SigningIdentity string |
| 135 |
// InsecureAllowUnauthenticatedMessages controls, whether it is tolerated to read |
| 136 |
// encrypted messages without Modification Detection Code (MDC). |
| 137 |
// MDC is mandated by the IETF OpenPGP Crypto Refresh draft and has long been implemented |
| 138 |
// in most OpenPGP implementations. Messages without MDC are considered unnecessarily |
| 139 |
// insecure and should be prevented whenever possible. |
| 140 |
// In case one needs to deal with messages from very old OpenPGP implementations, there |
| 141 |
// might be no other way than to tolerate the missing MDC. Setting this flag, allows this |
| 142 |
// mode of operation. It should be considered a measure of last resort. |
| 143 |
InsecureAllowUnauthenticatedMessages bool |
| 144 |
// InsecureAllowDecryptionWithSigningKeys allows decryption with keys marked as signing keys in the v2 API. |
| 145 |
// This setting is potentially insecure, but it is needed as some libraries |
| 146 |
// ignored key flags when selecting a key for encryption. |
| 147 |
// Not relevant for the v1 API, as all keys were allowed in decryption. |
| 148 |
InsecureAllowDecryptionWithSigningKeys bool |
| 149 |
// KnownNotations is a map of Notation Data names to bools, which controls |
| 150 |
// the notation names that are allowed to be present in critical Notation Data |
| 151 |
// signature subpackets. |
| 152 |
KnownNotations map[string]bool |
| 153 |
// SignatureNotations is a list of Notations to be added to any signatures. |
| 154 |
SignatureNotations []*Notation |
| 155 |
// CheckIntendedRecipients controls, whether the OpenPGP Intended Recipient Fingerprint feature |
| 156 |
// should be enabled for encryption and decryption. |
| 157 |
// (See https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-12.html#name-intended-recipient-fingerpr). |
| 158 |
// When the flag is set, encryption produces Intended Recipient Fingerprint signature sub-packets and decryption |
| 159 |
// checks whether the key it was encrypted to is one of the included fingerprints in the signature. |
| 160 |
// If the flag is disabled, no Intended Recipient Fingerprint sub-packets are created or checked. |
| 161 |
// The default behavior, when the config or flag is nil, is to enable the feature. |
| 162 |
CheckIntendedRecipients *bool |
| 163 |
// CacheSessionKey controls if decryption should return the session key used for decryption. |
| 164 |
// If the flag is set, the session key is cached in the message details struct. |
| 165 |
CacheSessionKey bool |
| 166 |
// CheckPacketSequence is a flag that controls if the pgp message reader should strictly check |
| 167 |
// that the packet sequence conforms with the grammar mandated by rfc4880. |
| 168 |
// The default behavior, when the config or flag is nil, is to check the packet sequence. |
| 169 |
CheckPacketSequence *bool |
| 170 |
// NonDeterministicSignaturesViaNotation is a flag to enable randomization of signatures. |
| 171 |
// If true, a salt notation is used to randomize signatures generated by v4 and v5 keys |
| 172 |
// (v6 signatures are always non-deterministic, by design). |
| 173 |
// This protects EdDSA signatures from potentially leaking the secret key in case of faults (i.e. bitflips) which, in principle, could occur |
| 174 |
// during the signing computation. It is added to signatures of any algo for simplicity, and as it may also serve as protection in case of |
| 175 |
// weaknesses in the hash algo, potentially hindering e.g. some chosen-prefix attacks. |
| 176 |
// The default behavior, when the config or flag is nil, is to enable the feature. |
| 177 |
NonDeterministicSignaturesViaNotation *bool |
| 178 |
|
| 179 |
// InsecureAllowAllKeyFlagsWhenMissing determines how a key without valid key flags is handled. |
| 180 |
// When set to true, a key without flags is treated as if all flags are enabled. |
| 181 |
// This behavior is consistent with GPG. |
| 182 |
InsecureAllowAllKeyFlagsWhenMissing bool |
| 183 |
// InsecureGenerateNonCriticalKeyFlags causes the "Key Flags" signature subpacket |
| 184 |
// to be non-critical in newly generated signatures. |
| 185 |
// This may be needed for keys to be accepted by older clients who do not recognize |
| 186 |
// the subpacket. |
| 187 |
// For example, rpm 4.14.3-150400.59.3.1 in OpenSUSE Leap 15.4 does not recognize it. |
| 188 |
InsecureGenerateNonCriticalKeyFlags bool |
| 189 |
// InsecureGenerateNonCriticalSignatureCreationTime causes the "Signature Creation Time" signature subpacket |
| 190 |
// to be non-critical in newly generated signatures. |
| 191 |
// This may be needed for keys to be accepted by older clients who do not recognize |
| 192 |
// the subpacket. |
| 193 |
// For example, yum 3.4.3-168 in CentOS 7 and yum 3.4.3-158 in Amazon Linux 2 do not recognize it. |
| 194 |
InsecureGenerateNonCriticalSignatureCreationTime bool |
| 195 |
|
| 196 |
// MaxDecompressedMessageSize specifies the maximum number of bytes that can be |
| 197 |
// read from a compressed packet. This serves as an upper limit to prevent |
| 198 |
// excessively large decompressed messages. |
| 199 |
MaxDecompressedMessageSize *int64 |
| 200 |
} |
| 201 |
|
| 202 |
func (c *Config) Random() io.Reader { |
| 203 |
if c == nil || c.Rand == nil { |
| 204 |
return rand.Reader |
| 205 |
} |
| 206 |
return c.Rand |
| 207 |
} |
| 208 |
|
| 209 |
func (c *Config) Hash() crypto.Hash { |
| 210 |
if c == nil || uint(c.DefaultHash) == 0 { |
| 211 |
return crypto.SHA256 |
| 212 |
} |
| 213 |
return c.DefaultHash |
| 214 |
} |
| 215 |
|
| 216 |
func (c *Config) Cipher() CipherFunction { |
| 217 |
if c == nil || uint8(c.DefaultCipher) == 0 { |
| 218 |
return CipherAES128 |
| 219 |
} |
| 220 |
return c.DefaultCipher |
| 221 |
} |
| 222 |
|
| 223 |
func (c *Config) Now() time.Time { |
| 224 |
if c == nil || c.Time == nil { |
| 225 |
return time.Now().Truncate(time.Second) |
| 226 |
} |
| 227 |
return c.Time().Truncate(time.Second) |
| 228 |
} |
| 229 |
|
| 230 |
// KeyLifetime returns the validity period of the key. |
| 231 |
func (c *Config) KeyLifetime() uint32 { |
| 232 |
if c == nil { |
| 233 |
return 0 |
| 234 |
} |
| 235 |
return c.KeyLifetimeSecs |
| 236 |
} |
| 237 |
|
| 238 |
// SigLifetime returns the validity period of the signature. |
| 239 |
func (c *Config) SigLifetime() uint32 { |
| 240 |
if c == nil { |
| 241 |
return 0 |
| 242 |
} |
| 243 |
return c.SigLifetimeSecs |
| 244 |
} |
| 245 |
|
| 246 |
func (c *Config) Compression() CompressionAlgo { |
| 247 |
if c == nil { |
| 248 |
return CompressionNone |
| 249 |
} |
| 250 |
return c.DefaultCompressionAlgo |
| 251 |
} |
| 252 |
|
| 253 |
func (c *Config) RSAModulusBits() int { |
| 254 |
if c == nil || c.RSABits == 0 { |
| 255 |
return 2048 |
| 256 |
} |
| 257 |
return c.RSABits |
| 258 |
} |
| 259 |
|
| 260 |
func (c *Config) PublicKeyAlgorithm() PublicKeyAlgorithm { |
| 261 |
if c == nil || c.Algorithm == 0 { |
| 262 |
return PubKeyAlgoRSA |
| 263 |
} |
| 264 |
return c.Algorithm |
| 265 |
} |
| 266 |
|
| 267 |
func (c *Config) CurveName() Curve { |
| 268 |
if c == nil || c.Curve == "" { |
| 269 |
return Curve25519 |
| 270 |
} |
| 271 |
return c.Curve |
| 272 |
} |
| 273 |
|
| 274 |
// Deprecated: The hash iterations should now be queried via the S2K() method. |
| 275 |
func (c *Config) PasswordHashIterations() int { |
| 276 |
if c == nil || c.S2KCount == 0 { |
| 277 |
return 0 |
| 278 |
} |
| 279 |
return c.S2KCount |
| 280 |
} |
| 281 |
|
| 282 |
func (c *Config) S2K() *s2k.Config { |
| 283 |
if c == nil { |
| 284 |
return nil |
| 285 |
} |
| 286 |
// for backwards compatibility |
| 287 |
if c.S2KCount > 0 && c.S2KConfig == nil { |
| 288 |
return &s2k.Config{ |
| 289 |
S2KCount: c.S2KCount, |
| 290 |
} |
| 291 |
} |
| 292 |
return c.S2KConfig |
| 293 |
} |
| 294 |
|
| 295 |
func (c *Config) AEAD() *AEADConfig { |
| 296 |
if c == nil { |
| 297 |
return nil |
| 298 |
} |
| 299 |
return c.AEADConfig |
| 300 |
} |
| 301 |
|
| 302 |
func (c *Config) SigningKey() uint64 { |
| 303 |
if c == nil { |
| 304 |
return 0 |
| 305 |
} |
| 306 |
return c.SigningKeyId |
| 307 |
} |
| 308 |
|
| 309 |
func (c *Config) SigningUserId() string { |
| 310 |
if c == nil { |
| 311 |
return "" |
| 312 |
} |
| 313 |
return c.SigningIdentity |
| 314 |
} |
| 315 |
|
| 316 |
func (c *Config) AllowUnauthenticatedMessages() bool { |
| 317 |
if c == nil { |
| 318 |
return false |
| 319 |
} |
| 320 |
return c.InsecureAllowUnauthenticatedMessages |
| 321 |
} |
| 322 |
|
| 323 |
func (c *Config) AllowDecryptionWithSigningKeys() bool { |
| 324 |
if c == nil { |
| 325 |
return false |
| 326 |
} |
| 327 |
return c.InsecureAllowDecryptionWithSigningKeys |
| 328 |
} |
| 329 |
|
| 330 |
func (c *Config) KnownNotation(notationName string) bool { |
| 331 |
if c == nil { |
| 332 |
return false |
| 333 |
} |
| 334 |
return c.KnownNotations[notationName] |
| 335 |
} |
| 336 |
|
| 337 |
func (c *Config) Notations() []*Notation { |
| 338 |
if c == nil { |
| 339 |
return nil |
| 340 |
} |
| 341 |
return c.SignatureNotations |
| 342 |
} |
| 343 |
|
| 344 |
func (c *Config) V6() bool { |
| 345 |
if c == nil { |
| 346 |
return false |
| 347 |
} |
| 348 |
return c.V6Keys |
| 349 |
} |
| 350 |
|
| 351 |
func (c *Config) IntendedRecipients() bool { |
| 352 |
if c == nil || c.CheckIntendedRecipients == nil { |
| 353 |
return true |
| 354 |
} |
| 355 |
return *c.CheckIntendedRecipients |
| 356 |
} |
| 357 |
|
| 358 |
func (c *Config) RetrieveSessionKey() bool { |
| 359 |
if c == nil { |
| 360 |
return false |
| 361 |
} |
| 362 |
return c.CacheSessionKey |
| 363 |
} |
| 364 |
|
| 365 |
func (c *Config) MinimumRSABits() uint16 { |
| 366 |
if c == nil || c.MinRSABits == 0 { |
| 367 |
return 2047 |
| 368 |
} |
| 369 |
return c.MinRSABits |
| 370 |
} |
| 371 |
|
| 372 |
func (c *Config) RejectPublicKeyAlgorithm(alg PublicKeyAlgorithm) bool { |
| 373 |
var rejectedAlgorithms map[PublicKeyAlgorithm]bool |
| 374 |
if c == nil || c.RejectPublicKeyAlgorithms == nil { |
| 375 |
// Default |
| 376 |
rejectedAlgorithms = defaultRejectPublicKeyAlgorithms |
| 377 |
} else { |
| 378 |
rejectedAlgorithms = c.RejectPublicKeyAlgorithms |
| 379 |
} |
| 380 |
return rejectedAlgorithms[alg] |
| 381 |
} |
| 382 |
|
| 383 |
func (c *Config) RejectHashAlgorithm(hash crypto.Hash) bool { |
| 384 |
var rejectedAlgorithms map[crypto.Hash]bool |
| 385 |
if c == nil || c.RejectHashAlgorithms == nil { |
| 386 |
// Default |
| 387 |
rejectedAlgorithms = defaultRejectHashAlgorithms |
| 388 |
} else { |
| 389 |
rejectedAlgorithms = c.RejectHashAlgorithms |
| 390 |
} |
| 391 |
return rejectedAlgorithms[hash] |
| 392 |
} |
| 393 |
|
| 394 |
func (c *Config) RejectMessageHashAlgorithm(hash crypto.Hash) bool { |
| 395 |
var rejectedAlgorithms map[crypto.Hash]bool |
| 396 |
if c == nil || c.RejectMessageHashAlgorithms == nil { |
| 397 |
// Default |
| 398 |
rejectedAlgorithms = defaultRejectMessageHashAlgorithms |
| 399 |
} else { |
| 400 |
rejectedAlgorithms = c.RejectMessageHashAlgorithms |
| 401 |
} |
| 402 |
return rejectedAlgorithms[hash] |
| 403 |
} |
| 404 |
|
| 405 |
func (c *Config) RejectCurve(curve Curve) bool { |
| 406 |
var rejectedCurve map[Curve]bool |
| 407 |
if c == nil || c.RejectCurves == nil { |
| 408 |
// Default |
| 409 |
rejectedCurve = defaultRejectCurves |
| 410 |
} else { |
| 411 |
rejectedCurve = c.RejectCurves |
| 412 |
} |
| 413 |
return rejectedCurve[curve] |
| 414 |
} |
| 415 |
|
| 416 |
func (c *Config) StrictPacketSequence() bool { |
| 417 |
if c == nil || c.CheckPacketSequence == nil { |
| 418 |
return true |
| 419 |
} |
| 420 |
return *c.CheckPacketSequence |
| 421 |
} |
| 422 |
|
| 423 |
func (c *Config) RandomizeSignaturesViaNotation() bool { |
| 424 |
if c == nil || c.NonDeterministicSignaturesViaNotation == nil { |
| 425 |
return true |
| 426 |
} |
| 427 |
return *c.NonDeterministicSignaturesViaNotation |
| 428 |
} |
| 429 |
|
| 430 |
func (c *Config) AllowAllKeyFlagsWhenMissing() bool { |
| 431 |
if c == nil { |
| 432 |
return false |
| 433 |
} |
| 434 |
return c.InsecureAllowAllKeyFlagsWhenMissing |
| 435 |
} |
| 436 |
|
| 437 |
func (c *Config) GenerateNonCriticalKeyFlags() bool { |
| 438 |
if c == nil { |
| 439 |
return false |
| 440 |
} |
| 441 |
return c.InsecureGenerateNonCriticalKeyFlags |
| 442 |
} |
| 443 |
|
| 444 |
func (c *Config) GenerateNonCriticalSignatureCreationTime() bool { |
| 445 |
if c == nil { |
| 446 |
return false |
| 447 |
} |
| 448 |
return c.InsecureGenerateNonCriticalSignatureCreationTime |
| 449 |
} |
| 450 |
|
| 451 |
func (c *Config) DecompressedMessageSizeLimit() *int64 { |
| 452 |
if c == nil { |
| 453 |
return nil |
| 454 |
} |
| 455 |
return c.MaxDecompressedMessageSize |
| 456 |
} |
| 457 |
|
| 458 |
// BoolPointer is a helper function to set a boolean pointer in the Config. |
| 459 |
// e.g., config.CheckPacketSequence = BoolPointer(true) |
| 460 |
func BoolPointer(value bool) *bool { |
| 461 |
return &value |
| 462 |
} |