diff --git a/doc/api/crypto.md b/doc/api/crypto.md index c10313b7241..f3c75ff7edf 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -5418,13 +5418,14 @@ const extensions = [ ASCII strings without NUL and are not otherwise validated. * Subject Key Identifier: `type: 'subjectKeyIdentifier'` and `value: 'auto'` or `value` {ArrayBuffer|TypedArray|Buffer|DataView}. A binary value contains the - key identifier bytes, not DER. `'auto'` uses [RFC 7093][] method 1: the - leftmost 160 bits of the SHA-256 digest of the `subjectPublicKey` BIT STRING - contents. This typed form is always noncritical. + nonempty key identifier bytes, not DER. `'auto'` uses [RFC 7093][] method 1: + the leftmost 160 bits of the SHA-256 digest of the `subjectPublicKey` BIT + STRING contents. This typed form is always noncritical. * Authority Key Identifier: `type: 'authorityKeyIdentifier'` and `value: 'auto'` or `value` - {ArrayBuffer|TypedArray|Buffer|DataView}. A binary value contains the key - identifier bytes and populates only the extension's `keyIdentifier` field. + {ArrayBuffer|TypedArray|Buffer|DataView}. A binary value contains the + nonempty key identifier bytes and populates only the extension's + `keyIdentifier` field. When `options.issuer` is supplied, `'auto'` asks the configured crypto backend to decode and copy its Subject Key Identifier. If the extension is absent, the identifier is instead derived from the issuer public key using diff --git a/lib/internal/crypto/x509_generation.js b/lib/internal/crypto/x509_generation.js index 3399fb8bf21..c57958e59e1 100644 --- a/lib/internal/crypto/x509_generation.js +++ b/lib/internal/crypto/x509_generation.js @@ -434,12 +434,18 @@ function normalizeExtensions(extensions, arrayPath = 'options.extensions') { case 'subjectKeyIdentifier': case 'authorityKeyIdentifier': { const value = getRequired(extension, 'value', `${path}.value`); + const identifier = + value === 'auto' ? undefined : snapshotBinary(value, `${path}.value`); + if (identifier?.length === 0) { + throw new ERR_INVALID_ARG_VALUE( + `${path}.value`, value, 'must not be empty'); + } normalized = [ type === 'subjectKeyIdentifier' ? kExtensionSubjectKeyIdentifier : kExtensionAuthorityKeyIdentifier, false, - value === 'auto' ? undefined : snapshotBinary(value, `${path}.value`), + identifier, ]; break; } diff --git a/src/crypto/crypto_x509_gen.cc b/src/crypto/crypto_x509_gen.cc index ca13382819e..3f2345aae74 100644 --- a/src/crypto/crypto_x509_gen.cc +++ b/src/crypto/crypto_x509_gen.cc @@ -440,11 +440,11 @@ bool ReadKeyIdentifierSpec(Environment* env, if (!IsAnyBufferSource(identifier_value)) return false; ArrayBufferOrViewContents contents(identifier_value); - if (!contents.CheckSizeInt32()) return false; + if (!contents.CheckSizeInt32() || contents.size() == 0) return false; identifier->reset(ASN1_OCTET_STRING_new()); return *identifier && ASN1_OCTET_STRING_set(identifier->get(), - contents.size() == 0 ? nullptr : contents.data(), + contents.data(), static_cast(contents.size())) == 1; } diff --git a/test/parallel/test-crypto-x509-generate.js b/test/parallel/test-crypto-x509-generate.js index 15218776b3c..9aa8c9afaf9 100644 --- a/test/parallel/test-crypto-x509-generate.js +++ b/test/parallel/test-crypto-x509-generate.js @@ -618,6 +618,12 @@ const invalidOptions = [ ['ERR_INVALID_ARG_VALUE', baseOptions(leafKeys, { subject: [{ name: 'CN', value: 'example.com\0.example' }], })], + ['ERR_INVALID_ARG_VALUE', baseOptions(leafKeys, { + extensions: [{ type: 'subjectKeyIdentifier', value: Buffer.alloc(0) }], + })], + ['ERR_INVALID_ARG_VALUE', baseOptions(leafKeys, { + extensions: [{ type: 'authorityKeyIdentifier', value: Buffer.alloc(0) }], + })], ['ERR_INVALID_ARG_VALUE', baseOptions(leafKeys, { subject: [ { name: 'CN', value: 'example.com' },