249 Commits

Author SHA1 Message Date
larabr
727c7cad37 read[Private]Key: support parsing key blocks (return first parsable key)
Previously, `readKey` and `readPrivateKey` would throw when given a block
of keys as input.
With this change, the first parsable key is returned by both functions:
the behaviour is equivalent to calling `readKeys` (resp. `readPrivateKeys`)
and taking the first array entry.
2024-05-16 14:07:39 +02:00
larabr
f3f1ab931b Tests: update SEIPD version check to no longer depend on config.aeadProtect
The logic was updated in github.com/openpgpjs/openpgpjs/pull/1678 .
The tests worked anyway thanks to the config option matching the (monkey patched)
keys' feature flags, which are the deciding factor for whether to use AEAD.
2024-05-02 21:45:58 +02:00
larabr
6ebd179ed5 Fix encrypting to a key with no declared features 2024-03-22 17:12:45 +01:00
larabr
3320eaccb2
Relax constraints for UserID email address validity (#1641)
New checks align with the HTML5 W3C spec and should be more lax than the
existing ones (meaning, addresses which passed validation before should
continue to be valid).
Addresses such as `@localhost` are now allowed too, since presence of "." is no
longer enforced.

These checks should not be considered exhaustive: library users are encouraged
to implement separate checks for email validity if needed.

Co-authored-by: Daniel Huigens <d.huigens@protonmail.com>
2024-02-02 15:04:34 +01:00
larabr
22c2682574
Ensure primary key meets strength and algo requirements when encrypting/verifying/signing using subkeys (#1719)
Breaking change: the requirements of `config.minRSABits`, `rejectPublicKeyAlgorithms` and `rejectCurves`
are now applied to the primary key, aside from the selected subkey.

The motivation is that the subkeys are certified by the primary key, but if the latter is
weak, arbitrary subkeys could potentially be added.

Note that the change does not affect decryption, to allow decrypting older messages.
2024-02-02 14:50:32 +01:00
larabr
f64dc3f35f
enums.curve: rename NIST curves (p256,p384,p521`) and clean up unused internal values (#1721)
This is a breaking change, as NIST curves identifiers and values in
`enums.curves` have been renamed:
- the identifiers `enums.curve.p256`, `.p384`, `.p521` are now marked as
`@deprecated`
- the new identifiers are, respectively: `enums.curve.nistP256`, `.nistP384`,
`.nistP521`.
- the corresponding values have been changed from `'p256'`,`'p384'`,`'p521'` to
`'nistP256'`, `'nistP384'`, `'nistP521'`.

Affected high-level API functions:
- in `generateKey`, the `options.curve` argument will expect the updated string
values
- `Key.getAlgorithmInfo()` will return the updated `curve` values
2024-02-02 12:54:24 +01:00
larabr
99899d1d5c
Drop support for native Node Readable stream: require passing Node Web Streams (#1716)
Breaking change: all functions taking streams as inputs will now require passing Web Streams in Node.js . If given a native `stream.Readable` input, they will throw. The browser build is unaffected by this change.

Utils to convert from and to Web Streams in Node are available from v17,
see https://nodejs.org/api/stream.html#streamreadabletowebstreamreadable-options .
Previously, we automatically converted between Node native streams and custom, Web-like Readable streams.
This led to occasional issues.
2024-01-26 17:52:29 +01:00
larabr
01df8ca889 Rename values of enums.curve.{curve, ed}25519Legacy from '{curve. ed}25519' to '{curve. ed}25519Legacy'
To reflect the crypto-refresh naming, after the standardisation of the new EdDSA
key types.
2023-10-25 12:53:14 +02:00
larabr
7c2248151d Default to generating new curve25519 format for v6 keys
As per the spec, v6 keys must not use the legacy curve25519 format.
The new format is not used by default with v4 keys as it's not compatible with OpenPGP.js older than v5.10.0 .
However, v6 keys already break compatibility, so if the user requests them via config flag, we can safely use the new curve format as well.
2023-10-25 12:53:14 +02:00
larabr
24c644207d Support generating Curve448 and Curve25519 keys (new format)
Neither type is set as default for now, since they are not widely supported.
2023-10-25 12:53:14 +02:00
larabr
d291ce6d0f Update Curve448 tests using inputs from gopenpgp 2023-10-25 12:53:13 +02:00
larabr
56cd448a32 crypto-refresh: add support for X448 2023-10-25 12:53:13 +02:00
larabr
1ebf7034f5 crypto-refresh: add support for Ed448 2023-10-25 12:53:13 +02:00
larabr
105b3cdde4 Disregard config.aeadProtect when encrypting to public keys (#1678)
Determine whether AEAD should be used for encryption solely based the encryption key preferences.
Previously, the config flag was also used to control the behaviour, since AEAD messages were not standardised nor widely supported.

To generate keys that declare AEAD in their preferences, use `generateKey` with `config.aeadProtect = true`.
2023-10-25 12:53:13 +02:00
Ryan
53e1ec023f Add SHA-3 signature support (#1680)
To support parsing, signing and verifying SHA3 signatures over messages and
keys.
2023-10-25 12:53:12 +02:00
larabr
8fe04c99c6 Remove unused enums.symmetric.plaintext
This special cipher value can be relevant for unencrypted private keys:
https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-10.html#section-12.2.1 .
However, it is no longer used internally, and on the contrary it could cause
confusion on SKESK decryption, where "random" cipher algos are returned in case
of wrong password.

This change also fixes a flaky test on password-based decryption, caused by the
PKESK v6 changes which add support for `null` cipher algos. The code did not
distinguish between a `null` and a `0` (plaintext) algo identifier, and would
break when the latter was returned on SKESK decryption.
2023-10-25 12:53:12 +02:00
larabr
1423bdd564 Add PKESK.fromObject 2023-10-25 12:53:12 +02:00
larabr
33af3debc4 Throw intelligible error on GCM authentication failure, fix/refactor test for modification detection on decryption
Also, address race condition in error handling as part of AEAD message decryption,
which would cause non-uniform errors during testing.
2023-10-25 12:53:12 +02:00
Daniel Huigens
7e382e6e43 Add support for PKESK v6
Also, set version in PKESK constructor to null,
requiring to explicitly set all fields.

Co-authored-by: Lukas Burkhalter <lukas.burkhalter@proton.ch>
2023-10-25 12:53:12 +02:00
Daniel Huigens
f77ed0c0ed Look up preferred ciphersuite in one go
Instead of calling getPreferredAlgo('symmetric') and
getPreferredAlgo('aead'), we define and call getPreferredCipherSuite()
to determine the preferred symmetric and AEAD algorithm.

Additionally, we remove isAEADSupported(), instead we return
aeadAlgorithm: undefined from getPreferredCipherSuite() if AEAD is not
supported (CFB is used instead).

And finally, we define getPreferredCompressionAlgo() to replace
getPreferredAlgo('compression').
2023-10-25 12:53:12 +02:00
Daniel Huigens
9d85938ed7 Implement SEIPD v2 2023-10-25 12:53:12 +02:00
Daniel Huigens
939622e827 Remove armor checksum check
The crypto refresh says that we MUST NOT reject messages where the
CRC24 checksum is incorrect. So, we remove the check for it.

Also, remove the checksumRequired config.
2023-10-25 12:53:12 +02:00
Daniel Huigens
8816bd7541 Replace config.v5Keys with config.v6Keys flag
Also, don't generate v5 keys flag, which has been removed from the draft specification.
2023-10-25 12:53:11 +02:00
larabr
b3ef95e60e Tests: update sinon 2023-10-25 12:53:11 +02:00
larabr
1aefed9602 Fix streaming tests for browser, drop NodeReadableStream tests in Node.js
Unclear why the Node tests fails, but we're planning to drop support
2023-10-25 12:53:10 +02:00
larabr
ae4ed1fbf3 Tests: explicitly share openpgp instance used in tests
Also, init config before any code is run in tests
2023-10-25 12:53:10 +02:00
larabr
d49d92e5cb Update to Mocha v10 in tests, declare lib as module and add exports to package.json
Mocha v10 requires the lib to be esm compliant.
ESM mandates the use of file extensions in imports, so to minimize the
changes (for now), we rely on the flag `experimental-specifier-resolution=node`
and on `ts-node` (needed only for Node 20).

Breaking changes:
downstream bundlers might be affected by the package.json changes depending on
how they load the library.
NB: legacy package.json entrypoints are still available.
2023-10-25 12:53:10 +02:00
larabr
ebf22f2ee7 crypto-refresh: add support for Argon2 S2K (#1597)
In terms of API, this feature is backwards compatible, no breaking changes.
However, since a Wasm module is loaded for the Argon2 computation, browser apps
might need to make changes to their CSP policy in order to use the feature.

Newly introduced config fields:
- `config.s2kType` (defaulting to `enums.s2k.iterated`): s2k to use on
password-based encryption as well as private key encryption;
- `config.s2kArgon2Params` (defaulting to "uniformly safe settings" from Argon
RFC): parameters to use on encryption when `config.s2kType` is set to
`enums.s2k.argon2`;
2023-10-25 12:53:10 +02:00
Daniel Huigens
95c73738fa Update ESLint globals syntax 2023-10-25 12:53:10 +02:00
Daniel Huigens
de5549ff69 Remove embedded Web Streams ponyfill
Require the application to load a polyfill instead.
2023-10-25 12:53:10 +02:00
larabr
ee4ad89451 Enforce AES with PKESK v3 using x25519 (new format)
Fail on PKESK parsing as well as session key generation and encryption
2023-07-25 10:17:17 +02:00
larabr
1c07d268b8 crypto-refresh: add support for new X25519 key and PKESK format
As specified in openpgp-crypto-refresh-09.

Instead of encoding the symmetric key algorithm in the PKESK ciphertext (requiring padding),
the symmetric key algorithm is left unencrypted.

Co-authored-by: Lukas Burkhalter <lukas.burkhalter@proton.ch>
2023-07-25 10:17:09 +02:00
larabr
3f44082457 crypto-refresh: add support for new Ed25519 key and signature format
This addition is backwards compatible. We offer no way to generate v4 keys in the new format.
2023-07-13 11:58:40 +02:00
larabr
d72cece54a
Support parsing encrypted key with unknown s2k types or cipher algos (#1658)
Such keys are still capable of encryption and signature verification.
This change is relevant for forward compatibility of v4 keys encrypted using e.g. argon2.
2023-07-10 15:23:47 +02:00
larabr
94868e606a ESLint: drop unnecessary rules
Most rules are derived from the `airbnb` template.
Some "bad" rule exceptions remain, but they require too many changes to fix, so
we leave it to a future refactoring.
2023-02-21 18:27:59 +01:00
larabr
705f238e1e Update ESlint 2023-02-21 18:27:56 +01:00
larabr
126ab53840
Remove internal, unused RandomBuffer (#1593)
The changes do not affect the public API:
`RandomBuffer` was used internally for secure randomness generation before
`crypto.getRandomValues` was made available to WebWorkers, requiring
generating randomness in the main thread.
As a result of the change, the internal `getRandomBytes()` and some functions
that use it are no longer async.
2023-02-09 23:11:53 +01:00
larabr
e862d5f20b
Throw in encryptSessionKey if no keys or passwords are provided (#1547)
Previously, the operation would return an empty message.
2022-07-27 17:47:47 +02:00
larabr
6c32b62ef3
Throw on decryption of messages that don't contain any encrypted data packet (#1529)
Calling `openpgp.decrypt` with a message that contains encrypted session keys
followed by a non-encrypted packet (e.g. Literal or Compressed Data packet)
used to succeed, even if a wrong passphrase/key was provided.
With this change, the operation will always fail, and the user is warned that
the data was not encrypted.

NB: a message that did not contain any encrypted session key packet would fail
to decrypt even prior to this change.
2022-06-07 14:29:31 +02:00
larabr
cb8901c16d
Fix loading browser built in JSDom environment (#1518) 2022-05-12 17:29:52 +02:00
larabr
4713282bb1
Throw on empty passphrase in encryptKey and SecretKeyPacket.encrypt (#1508)
Breaking change: `openpgp.encryptKey` now throws if an empty string is given as
passphrase. The operation used to succeed, but the resulting key was left in an
inconsistent state, and e.g. serialization would not be possible.

Non-breaking changes:
- `options.passphrase` in `generateKey` and `reformatKey` now defaults to
`undefined` instead of empty string. Passing an empty string does not throw for
now, but this might change in the future to align with `encryptKey`'s
behaviour.
- In TS, add `GenerateKeyOptions` as alias of `KeyOptions`, to clarify its
scope.
2022-03-22 15:11:51 +01:00
larabr
22a68878e4
Add support for constant-time decryption of PKCS#1 v1.5-encoded session keys (#1445)
Implement optional constant-time decryption flow to hinder Bleichenbacher-like
attacks against RSA- and ElGamal public-key encrypted session keys.

Changes:
- Add `config.constantTimePKCS1Decryption` to enable the constant-time
processing (defaults to `false`). The constant-time option is off by default
since it has measurable performance impact on message decryption, and it is
only helpful in specific application scenarios (more info below).
- Add `config.constantTimePKCS1DecryptionSupportedSymmetricAlgorithms`
(defaults to the AES algorithms). The set of supported ciphers is restricted by
default since the number of algorithms negatively affects performance.

Bleichenbacher-like attacks are of concern for applications where both of the
following conditions are met:
1. new/incoming messages are automatically decrypted (without user
interaction);
2. an attacker can determine how long it takes to decrypt each message (e.g.
due to decryption errors being logged remotely).
2022-01-19 19:05:43 +01:00
larabr
6da1c53de7
Replace strings with integer algorithm identifiers in packet classes (#1410)
In several packet classes, we used to store string identifiers for public-key,
aead, cipher or hash algorithms. To make the code consistent and to avoid
having to convert to/from string values, we now always store integer values
instead, e.g. `enums.symmetric.aes128` is used instead of `'aes128'`.

This is not expected to be a breaking change for most library users. Note that
the type of `Key.getAlgorithmInfo()` and of the session key objects returned
and accepted by top-level functions remain unchanged.

Affected classes (type changes for some properties and method's arguments):
- `PublicKeyPacket`, `PublicSubkeyPacket`, `SecretKeyPacket`,
`SecretSubkeyPacket`
- `SymEncryptedIntegrityProtectedDataPacket`, `AEADEncryptedDataPacket`,
`SymmetricallyEncryptedDataPacket`
- `LiteralDataPacket`, `CompressedDataPacket`
- `PublicKeyEncryptedSessionKey`, `SymEncryptedSessionKeyPacket`
- `SignaturePacket`

Other potentially breaking changes:
- Removed property `AEADEncryptedDataPacket.aeadAlgo`, since it was redudant
given `.aeadAlgorithm`.
- Renamed `AEADEncryptedDataPacket.cipherAlgo` -> `.cipherAlgorithm`
2021-11-22 11:51:27 +01:00
larabr
cfeef41d61
Do not include checksum values in error messages when armor integrity check fails (#1428) 2021-10-21 16:14:46 +02:00
larabr
3fa778abe2
Add config.rejectCurves and prevent generating keys using blacklisted algorithms (#1395)
Breaking changes:
- throw error on key generation if the requested public key algorithm is
included in `config.rejectPublicKeyAlgorithms`;
- add `config.rejectCurves` to blacklist a set of ECC curves, to prevent keys
using those curves from being generated, or being used to
encrypt/decrypt/sign/verify messages.
By default, `config.rejectCurves` includes the brainpool curves
(`brainpoolP256r1`, `brainpoolP384r1`, `brainpoolP512r1`) and the Bitcoin curve
(`secp256k1`). This is because it's unclear whether these curves will be
standardised[1], and we prefer to blacklist them already, rather than introduce
a breaking change after release.

[1] https://gitlab.com/openpgp-wg/rfc4880bis/-/merge_requests/47#note_634199141
2021-08-19 17:58:16 +02:00
larabr
b604458ba8
Rename format: 'armor' option to format: 'armored' in top-level functions (#1377)
The `format` option in `openpgp.generateKey, reformatKey, revokeKey, encrypt,
sign, encryptSessionKey` now expects the value `'armored'` instead of `'armor'`
to output armored data. The other format options (i.e. `'binary'` and
`'object'`) remain unchanged.
2021-07-19 19:08:49 +02:00
larabr
ce70484738
Replace armor option with format in openpgp.encrypt, sign and encryptSessionKey (#1354)
Breaking changes:
- a new `format` option has been added to `openpgp.encrypt`, `sign` and
`encryptSessionKey` to select the format of the output message. `format`
replaces the existing `armor` option, and accepts three values:
   * if `format: 'armor'` (default), an armored signed/encrypted message is
returned (same as `armor: true`).
   * if `format: 'binary'`,  a binary signed/encrypted message is returned (same
as `armor: false`).
   * if `format: 'object'`, a Message or Signature object is returned (this was
not supported before).
This change is to uniform the output format selection across all top-level
functions (following up to #1345).

- All top-level functions now throw if unrecognised options are passed, to make
library users aware that those options are not being applied.
2021-07-19 18:12:42 +02:00
larabr
3886358592
Remove valid and error from the verification result of openpgp.verify and decrypt (#1348)
This change is to make the code more consistent between the streaming and
non-streaming cases.
The validity of a signature (or the corresponding verification error) can be
determined through the existing `verified` property.
2021-07-09 17:44:34 +02:00
larabr
ab22fe86da
Lint: enforce single quotes and do not error on class methods without this (#1341) 2021-06-24 22:58:15 +02:00
larabr
d238a023c1
Support using Key.isPrivate() for type inference, remove Key.isPublic() (#1347)
API changes:
- `Key.isPublic()` has been removed, since it was redundant and it would
introduce TypeScript issues. Call `!Key.isPrivate()` instead.

TypeScript changes:
- the `openpgp.readKey(s)` functions are now declared as returning a `Key`
instead of a `PublicKey`. This is just a readability improvement to make it
clearer that the result could also be a `PrivateKey`.
- All `Key` methods that return a key object now have the narrowest possible
return type.
- The `Key.isPrivate()` method can now be used for type inference, allowing the
compiler to distinguish between `PrivateKey` and `PublicKey`. 

Calling `key.isPrivate()` is the recommended way of distinguishing between a
`PrivateKey` and `PublicKey` at runtime, over using `key instanceof ...`, since
the latter depends on the specifics of the `Key` class hierarchy.
2021-06-24 19:53:10 +02:00