Skip to content

Commit 4412fe0

Browse files
committed
crypto: runtime-deprecate DEP0203 and DEP0204
1 parent dbc7405 commit 4412fe0

File tree

5 files changed

+109
-10
lines changed

5 files changed

+109
-10
lines changed

doc/api/crypto.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2255,12 +2255,20 @@ be listed in the `transferList` argument.
22552255

22562256
<!-- YAML
22572257
added: v15.0.0
2258+
changes:
2259+
- version: REPLACEME
2260+
pr-url: https://github.com/nodejs/node/pull/62453
2261+
description: Passing a non-extractable CryptoKey as `key` is deprecated.
22582262
-->
22592263

22602264
* `key` {CryptoKey}
22612265
* Returns: {KeyObject}
22622266

2263-
Example: Converting a `CryptoKey` instance to a `KeyObject`:
2267+
Returns the underlying {KeyObject} of a {CryptoKey}. The returned {KeyObject}
2268+
does not retain any of the restrictions imposed by the Web Crypto API on the
2269+
original {CryptoKey}, such as the allowed key usages, the algorithm or hash
2270+
algorithm bindings, and the extractability flag. In particular, the underlying
2271+
key material of the returned {KeyObject} can always be exported.
22642272

22652273
```mjs
22662274
const { KeyObject } = await import('node:crypto');
@@ -3522,6 +3530,9 @@ operations. The specific constants currently defined are described in
35223530
<!-- YAML
35233531
added: v0.1.94
35243532
changes:
3533+
- version: REPLACEME
3534+
pr-url: https://github.com/nodejs/node/pull/62453
3535+
description: Passing a CryptoKey as `key` is deprecated.
35253536
- version:
35263537
- v17.9.0
35273538
- v16.17.0
@@ -3596,6 +3607,9 @@ given IV will be.
35963607
<!-- YAML
35973608
added: v0.1.94
35983609
changes:
3610+
- version: REPLACEME
3611+
pr-url: https://github.com/nodejs/node/pull/62453
3612+
description: Passing a CryptoKey as `key` is deprecated.
35993613
- version:
36003614
- v17.9.0
36013615
- v16.17.0
@@ -3819,6 +3833,9 @@ input.on('readable', () => {
38193833
<!-- YAML
38203834
added: v0.1.94
38213835
changes:
3836+
- version: REPLACEME
3837+
pr-url: https://github.com/nodejs/node/pull/62453
3838+
description: Passing a CryptoKey as `key` is deprecated.
38223839
- version: v15.0.0
38233840
pr-url: https://github.com/nodejs/node/pull/35093
38243841
description: The key can also be an ArrayBuffer or CryptoKey. The
@@ -3909,6 +3926,9 @@ input.on('readable', () => {
39093926
<!-- YAML
39103927
added: v11.6.0
39113928
changes:
3929+
- version: REPLACEME
3930+
pr-url: https://github.com/nodejs/node/pull/62453
3931+
description: Passing a CryptoKey as `key` is deprecated.
39123932
- version: REPLACEME
39133933
pr-url: https://github.com/nodejs/node/pull/62240
39143934
description: Added support for `'raw-private'` and `'raw-seed'`
@@ -3957,6 +3977,9 @@ of the passphrase is limited to 1024 bytes.
39573977
<!-- YAML
39583978
added: v11.6.0
39593979
changes:
3980+
- version: REPLACEME
3981+
pr-url: https://github.com/nodejs/node/pull/62453
3982+
description: Passing a CryptoKey as `key` is deprecated.
39603983
- version: REPLACEME
39613984
pr-url: https://github.com/nodejs/node/pull/62240
39623985
description: Added support for `'raw-public'` format.

doc/api/deprecations.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4485,12 +4485,15 @@ const server = http2.createSecureServer({
44854485
44864486
<!-- YAML
44874487
changes:
4488+
- version: REPLACEME
4489+
pr-url: https://github.com/nodejs/node/pull/62453
4490+
description: Runtime deprecation.
44884491
- version: REPLACEME
44894492
pr-url: https://github.com/nodejs/node/pull/62321
44904493
description: Documentation-only deprecation.
44914494
-->
44924495
4493-
Type: Documentation-only
4496+
Type: Runtime
44944497
44954498
Passing a [`CryptoKey`][] to `node:crypto` functions is deprecated and
44964499
will throw an error in a future version. This includes
@@ -4507,12 +4510,15 @@ will throw an error in a future version. This includes
45074510
45084511
<!-- YAML
45094512
changes:
4513+
- version: REPLACEME
4514+
pr-url: https://github.com/nodejs/node/pull/62453
4515+
description: Runtime deprecation.
45104516
- version: REPLACEME
45114517
pr-url: https://github.com/nodejs/node/pull/62321
45124518
description: Documentation-only deprecation.
45134519
-->
45144520
4515-
Type: Documentation-only
4521+
Type: Runtime
45164522
45174523
Passing a non-extractable [`CryptoKey`][] to [`KeyObject.from()`][] is
45184524
deprecated and will throw an error in a future version.

lib/internal/crypto/keys.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const {
7474

7575
const {
7676
customInspectSymbol: kInspect,
77+
getDeprecationWarningEmitter,
7778
kEnumerableProperty,
7879
lazyDOMException,
7980
} = require('internal/util');
@@ -89,6 +90,18 @@ const kKeyUsages = Symbol('kKeyUsages');
8990
const kCachedAlgorithm = Symbol('kCachedAlgorithm');
9091
const kCachedKeyUsages = Symbol('kCachedKeyUsages');
9192

93+
const emitDEP0203 = getDeprecationWarningEmitter(
94+
'DEP0203',
95+
'Passing a CryptoKey to node:crypto functions is deprecated.',
96+
);
97+
98+
const maybeEmitDEP0204 = getDeprecationWarningEmitter(
99+
'DEP0204',
100+
'Passing a non-extractable CryptoKey to KeyObject.from() is deprecated.',
101+
undefined,
102+
false,
103+
(key) => !key[kExtractable],
104+
);
92105

93106
// Key input contexts.
94107
const kConsumePublic = 0;
@@ -140,6 +153,7 @@ const {
140153
static from(key) {
141154
if (!isCryptoKey(key))
142155
throw new ERR_INVALID_ARG_TYPE('key', 'CryptoKey', key);
156+
maybeEmitDEP0204(key);
143157
return key[kKeyObject];
144158
}
145159

@@ -796,21 +810,28 @@ function prepareAsymmetricKey(key, ctx) {
796810
if (isKeyObject(key)) {
797811
// Best case: A key object, as simple as that.
798812
return { data: getKeyObjectHandle(key, ctx) };
799-
} else if (isCryptoKey(key)) {
813+
}
814+
if (isCryptoKey(key)) {
815+
emitDEP0203();
800816
return { data: getKeyObjectHandle(key[kKeyObject], ctx) };
801-
} else if (isStringOrBuffer(key)) {
817+
}
818+
if (isStringOrBuffer(key)) {
802819
// Expect PEM by default, mostly for backward compatibility.
803820
return { format: kKeyFormatPEM, data: getArrayBufferOrView(key, 'key') };
804-
} else if (typeof key === 'object') {
821+
}
822+
if (typeof key === 'object') {
805823
const { key: data, encoding, format } = key;
806824

807825
// The 'key' property can be a KeyObject as well to allow specifying
808826
// additional options such as padding along with the key.
809-
if (isKeyObject(data))
827+
if (isKeyObject(data)) {
810828
return { data: getKeyObjectHandle(data, ctx) };
811-
else if (isCryptoKey(data))
829+
}
830+
if (isCryptoKey(data)) {
831+
emitDEP0203();
812832
return { data: getKeyObjectHandle(data[kKeyObject], ctx) };
813-
else if (format === 'jwk') {
833+
}
834+
if (format === 'jwk') {
814835
validateObject(data, 'key.key');
815836
return { data: getKeyObjectHandleFromJwk(data, ctx), format: 'jwk' };
816837
} else if (format === 'raw-public' || format === 'raw-private' ||
@@ -836,6 +857,7 @@ function prepareAsymmetricKey(key, ctx) {
836857
...parseKeyEncoding(key, undefined, isPublic),
837858
};
838859
}
860+
839861
throw new ERR_INVALID_ARG_TYPE(
840862
'key',
841863
getKeyTypes(ctx !== kCreatePrivate),
@@ -856,7 +878,9 @@ function prepareSecretKey(key, encoding, bufferOnly = false) {
856878
if (key.type !== 'secret')
857879
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, 'secret');
858880
return key[kHandle];
859-
} else if (isCryptoKey(key)) {
881+
}
882+
if (isCryptoKey(key)) {
883+
emitDEP0203();
860884
if (key[kKeyType] !== 'secret')
861885
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key[kKeyType], 'secret');
862886
return key[kKeyObject][kHandle];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const crypto = require('crypto');
8+
9+
common.expectWarning({
10+
DeprecationWarning: {
11+
DEP0203: 'Passing a CryptoKey to node:crypto functions is deprecated.',
12+
},
13+
});
14+
15+
(async () => {
16+
const key = await globalThis.crypto.subtle.generateKey(
17+
{ name: 'AES-CBC', length: 128 },
18+
true,
19+
['encrypt'],
20+
);
21+
22+
crypto.createCipheriv('aes-128-cbc', key, Buffer.alloc(16));
23+
})().then(common.mustCall());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const { KeyObject } = require('crypto');
8+
9+
common.expectWarning({
10+
DeprecationWarning: {
11+
DEP0204: 'Passing a non-extractable CryptoKey to KeyObject.from() is deprecated.',
12+
},
13+
});
14+
15+
(async () => {
16+
const key = await globalThis.crypto.subtle.generateKey(
17+
{ name: 'AES-CBC', length: 128 },
18+
false, // non-extractable
19+
['encrypt'],
20+
);
21+
22+
KeyObject.from(key);
23+
})().then(common.mustCall());

0 commit comments

Comments
 (0)