From e52882da4ca99bd9c4f63890224327632f8bda42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 7 Dec 2021 00:21:28 +0000 Subject: [PATCH] crypto,tls: implement safe x509 GeneralName format This change introduces JSON-compatible escaping rules for strings that include X.509 GeneralName components (see RFC 5280). This non-standard format avoids ambiguities and prevents injection attacks that could previously lead to X.509 certificates being accepted even though they were not valid for the target hostname. These changes affect the format of subject alternative names and the format of authority information access. The checkServerIdentity function has been modified to safely handle the new format, eliminating the possibility of injecting subject alternative names into the verification logic. Because each subject alternative name is only encoded as a JSON string literal if necessary for security purposes, this change will only be visible in rare cases. This addresses CVE-2021-44532. Co-authored-by: Akshay K CVE-ID: CVE-2021-44532 Backport-PR-URL: https://github.com/nodejs-private/node-private/pull/304 PR-URL: https://github.com/nodejs-private/node-private/pull/300 Reviewed-By: Michael Dawson Reviewed-By: Rich Trott --- doc/api/crypto.md | 39 +- doc/api/errors.md | 9 + lib/_tls_common.js | 9 + lib/internal/errors.js | 2 + lib/tls.js | 48 +- src/crypto/crypto_common.cc | 319 ++++++++++- src/crypto/crypto_common.h | 25 +- src/crypto/crypto_x509.cc | 4 +- test/fixtures/keys/Makefile | 14 + .../incorrect_san_correct_subject-cert.pem | 11 + .../incorrect_san_correct_subject-key.pem | 5 + test/fixtures/x509-escaping/.gitignore | 2 + test/fixtures/x509-escaping/alt-0-cert.pem | 29 + test/fixtures/x509-escaping/alt-1-cert.pem | 28 + test/fixtures/x509-escaping/alt-10-cert.pem | 28 + test/fixtures/x509-escaping/alt-11-cert.pem | 28 + test/fixtures/x509-escaping/alt-12-cert.pem | 28 + test/fixtures/x509-escaping/alt-13-cert.pem | 28 + test/fixtures/x509-escaping/alt-14-cert.pem | 29 + test/fixtures/x509-escaping/alt-15-cert.pem | 29 + test/fixtures/x509-escaping/alt-16-cert.pem | 29 + test/fixtures/x509-escaping/alt-17-cert.pem | 29 + test/fixtures/x509-escaping/alt-18-cert.pem | 29 + test/fixtures/x509-escaping/alt-19-cert.pem | 29 + test/fixtures/x509-escaping/alt-2-cert.pem | 28 + test/fixtures/x509-escaping/alt-20-cert.pem | 29 + test/fixtures/x509-escaping/alt-21-cert.pem | 29 + test/fixtures/x509-escaping/alt-22-cert.pem | 28 + test/fixtures/x509-escaping/alt-23-cert.pem | 28 + test/fixtures/x509-escaping/alt-24-cert.pem | 28 + test/fixtures/x509-escaping/alt-25-cert.pem | 29 + test/fixtures/x509-escaping/alt-26-cert.pem | 29 + test/fixtures/x509-escaping/alt-27-cert.pem | 28 + test/fixtures/x509-escaping/alt-28-cert.pem | 28 + test/fixtures/x509-escaping/alt-29-cert.pem | 28 + test/fixtures/x509-escaping/alt-3-cert.pem | 28 + test/fixtures/x509-escaping/alt-30-cert.pem | 28 + test/fixtures/x509-escaping/alt-4-cert.pem | 28 + test/fixtures/x509-escaping/alt-5-cert.pem | 29 + test/fixtures/x509-escaping/alt-6-cert.pem | 28 + test/fixtures/x509-escaping/alt-7-cert.pem | 28 + test/fixtures/x509-escaping/alt-8-cert.pem | 28 + test/fixtures/x509-escaping/alt-9-cert.pem | 28 + test/fixtures/x509-escaping/create-certs.js | 502 ++++++++++++++++++ .../x509-escaping/google/intermediate.pem | 11 + test/fixtures/x509-escaping/google/key.pem | 5 + test/fixtures/x509-escaping/google/leaf0.pem | 10 + test/fixtures/x509-escaping/google/leaf1.pem | 10 + test/fixtures/x509-escaping/google/leaf2.pem | 10 + test/fixtures/x509-escaping/google/leaf3.pem | 10 + test/fixtures/x509-escaping/google/leaf4.pem | 10 + test/fixtures/x509-escaping/google/root.pem | 9 + test/fixtures/x509-escaping/info-0-cert.pem | 30 ++ test/fixtures/x509-escaping/info-1-cert.pem | 31 ++ test/fixtures/x509-escaping/info-2-cert.pem | 29 + test/fixtures/x509-escaping/info-3-cert.pem | 30 ++ test/fixtures/x509-escaping/info-4-cert.pem | 29 + test/fixtures/x509-escaping/package.json | 12 + test/fixtures/x509-escaping/server-key.pem | 52 ++ test/parallel/test-tls-0-dns-altname.js | 2 +- test/parallel/test-x509-escaping.js | 338 ++++++++++++ 61 files changed, 2455 insertions(+), 42 deletions(-) create mode 100644 test/fixtures/keys/incorrect_san_correct_subject-cert.pem create mode 100644 test/fixtures/keys/incorrect_san_correct_subject-key.pem create mode 100644 test/fixtures/x509-escaping/.gitignore create mode 100644 test/fixtures/x509-escaping/alt-0-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-1-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-10-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-11-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-12-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-13-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-14-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-15-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-16-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-17-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-18-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-19-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-2-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-20-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-21-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-22-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-23-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-24-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-25-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-26-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-27-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-28-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-29-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-3-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-30-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-4-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-5-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-6-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-7-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-8-cert.pem create mode 100644 test/fixtures/x509-escaping/alt-9-cert.pem create mode 100644 test/fixtures/x509-escaping/create-certs.js create mode 100644 test/fixtures/x509-escaping/google/intermediate.pem create mode 100644 test/fixtures/x509-escaping/google/key.pem create mode 100644 test/fixtures/x509-escaping/google/leaf0.pem create mode 100644 test/fixtures/x509-escaping/google/leaf1.pem create mode 100644 test/fixtures/x509-escaping/google/leaf2.pem create mode 100644 test/fixtures/x509-escaping/google/leaf3.pem create mode 100644 test/fixtures/x509-escaping/google/leaf4.pem create mode 100644 test/fixtures/x509-escaping/google/root.pem create mode 100644 test/fixtures/x509-escaping/info-0-cert.pem create mode 100644 test/fixtures/x509-escaping/info-1-cert.pem create mode 100644 test/fixtures/x509-escaping/info-2-cert.pem create mode 100644 test/fixtures/x509-escaping/info-3-cert.pem create mode 100644 test/fixtures/x509-escaping/info-4-cert.pem create mode 100644 test/fixtures/x509-escaping/package.json create mode 100644 test/fixtures/x509-escaping/server-key.pem create mode 100644 test/parallel/test-x509-escaping.js diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 669eb4373d65d2..b4a48988966273 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2565,11 +2565,27 @@ The SHA-256 fingerprint of this certificate. * Type: {string} -The information access content of this certificate. +A textual representation of the certificate's authority information access +extension. + +This is a line feed separated list of access descriptions. Each line begins with +the access method and the kind of the access location, followed by a colon and +the value associated with the access location. + +After the prefix denoting the access method and the kind of the access location, +the remainder of each line might be enclosed in quotes to indicate that the +value is a JSON string literal. For backward compatibility, Node.js only uses +JSON string literals within this property when necessary to avoid ambiguity. +Third-party code should be prepared to handle both possible entry formats. ### `x509.issuer` @@ -2646,12 +2662,32 @@ The complete subject of this certificate. * Type: {string} The subject alternative name specified for this certificate. +This is a comma-separated list of subject alternative names. Each entry begins +with a string identifying the kind of the subject alternative name followed by +a colon and the value associated with the entry. + +Earlier versions of Node.js incorrectly assumed that it is safe to split this +property at the two-character sequence `', '` (see [CVE-2021-44532][]). However, +both malicious and legitimate certificates can contain subject alternative names +that include this sequence when represented as a string. + +After the prefix denoting the type of the entry, the remainder of each entry +might be enclosed in quotes to indicate that the value is a JSON string literal. +For backward compatibility, Node.js only uses JSON string literals within this +property when necessary to avoid ambiguity. Third-party code should be prepared +to handle both possible entry formats. + ### `x509.toJSON()`