Skip to content
Permalink
Browse files
crypto: remove arbitrary UTF16 restriction
Since 71f633a, this is no longer necessary.

Refs: #22622
Fixes: #29793

PR-URL: #29795
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
  • Loading branch information
addaleax authored and BridgeAR committed Oct 9, 2019
1 parent 87fb1c2 commit ab4c53e0ef6704b6c526bb8ee0cdc9e5aa896c92
Showing with 22 additions and 31 deletions.
  1. +12 −8 doc/api/errors.md
  2. +4 −7 lib/internal/crypto/hash.js
  3. +0 −2 lib/internal/errors.js
  4. +0 −1 src/node_crypto.cc
  5. +3 −7 test/parallel/test-crypto-hash.js
  6. +3 −6 test/parallel/test-crypto-hmac.js
@@ -763,14 +763,6 @@ to enable or disable FIPS mode in the `crypto` module.
An attempt was made to enable or disable FIPS mode, but FIPS mode was not
available.

<a id="ERR_CRYPTO_HASH_DIGEST_NO_UTF16"></a>
### ERR_CRYPTO_HASH_DIGEST_NO_UTF16

The UTF-16 encoding was used with [`hash.digest()`][]. While the
`hash.digest()` method does allow an `encoding` argument to be passed in,
causing the method to return a string rather than a `Buffer`, the UTF-16
encoding (e.g. `ucs` or `utf16le`) is not supported.

<a id="ERR_CRYPTO_HASH_FINALIZED"></a>
### ERR_CRYPTO_HASH_FINALIZED

@@ -2070,6 +2062,18 @@ removed: v11.12.0
There was an attempt to use a `MessagePort` instance in a closed
state, usually after `.close()` has been called.

<a id="ERR_CRYPTO_HASH_DIGEST_NO_UTF16"></a>
### ERR_CRYPTO_HASH_DIGEST_NO_UTF16
<!-- YAML
added: v9.0.0
removed: REPLACEME
-->

The UTF-16 encoding was used with [`hash.digest()`][]. While the
`hash.digest()` method does allow an `encoding` argument to be passed in,
causing the method to return a string rather than a `Buffer`, the UTF-16
encoding (e.g. `ucs` or `utf16le`) is not supported.

<a id="ERR_HTTP2_FRAME_ERROR"></a>
### ERR_HTTP2_FRAME_ERROR
<!-- YAML
@@ -20,13 +20,14 @@ const {
const { Buffer } = require('buffer');

const {
ERR_CRYPTO_HASH_DIGEST_NO_UTF16,
ERR_CRYPTO_HASH_FINALIZED,
ERR_CRYPTO_HASH_UPDATE_FAILED,
ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes;
const { validateString, validateUint32 } = require('internal/validators');
const { normalizeEncoding } = require('internal/util');
const {
validateString,
validateUint32
} = require('internal/validators');
const { isArrayBufferView } = require('internal/util/types');
const LazyTransform = require('internal/streams/lazy_transform');
const kState = Symbol('kState');
@@ -85,8 +86,6 @@ Hash.prototype.digest = function digest(outputEncoding) {
if (state[kFinalized])
throw new ERR_CRYPTO_HASH_FINALIZED();
outputEncoding = outputEncoding || getDefaultEncoding();
if (normalizeEncoding(outputEncoding) === 'utf16le')
throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16();

// Explicit conversion for backward compatibility.
const ret = this[kHandle].digest(`${outputEncoding}`);
@@ -116,8 +115,6 @@ Hmac.prototype.update = Hash.prototype.update;
Hmac.prototype.digest = function digest(outputEncoding) {
const state = this[kState];
outputEncoding = outputEncoding || getDefaultEncoding();
if (normalizeEncoding(outputEncoding) === 'utf16le')
throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16();

if (state[kFinalized]) {
const buf = Buffer.from('');
@@ -744,8 +744,6 @@ E('ERR_CRYPTO_FIPS_FORCED',
'Cannot set FIPS mode, it was forced with --force-fips at startup.', Error);
E('ERR_CRYPTO_FIPS_UNAVAILABLE', 'Cannot set FIPS mode in a non-FIPS build.',
Error);
E('ERR_CRYPTO_HASH_DIGEST_NO_UTF16', 'hash.digest() does not support UTF-16',
Error);
E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called', Error);
E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed', Error);
E('ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS', 'The selected key encoding %s %s.',
@@ -4676,7 +4676,6 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
if (args.Length() >= 1) {
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
}
CHECK_NE(encoding, UCS2); // Digest does not support UTF-16

unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len = 0;
@@ -161,13 +161,9 @@ common.expectsError(
type: Error
});

common.expectsError(
() => crypto.createHash('sha256').digest('ucs2'),
{
code: 'ERR_CRYPTO_HASH_DIGEST_NO_UTF16',
type: Error
}
);
assert.strictEqual(
crypto.createHash('sha256').update('test').digest('ucs2'),
crypto.createHash('sha256').update('test').digest().toString('ucs2'));

common.expectsError(
() => crypto.createHash(),
@@ -408,12 +408,9 @@ const rfc2202_sha1 = [
for (const { key, data, hmac } of rfc2202_sha1)
testHmac('sha1', key, data, hmac);

common.expectsError(
() => crypto.createHmac('sha256', 'w00t').digest('ucs2'),
{
code: 'ERR_CRYPTO_HASH_DIGEST_NO_UTF16',
type: Error
});
assert.strictEqual(
crypto.createHmac('sha256', 'w00t').digest('ucs2'),
crypto.createHmac('sha256', 'w00t').digest().toString('ucs2'));

// Check initialized -> uninitialized state transition after calling digest().
{

0 comments on commit ab4c53e

Please sign in to comment.