Skip to content

Commit 9f2c526

Browse files
mhdawsonkumarak
authored andcommitted
src: add cve reverts and associated tests
Co-authored-by: Akshay K <iit.akshay@gmail.com> Backport-PR-URL: nodejs-private/node-private#305 PR-URL: nodejs-private/node-private#300 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 83d8f88 commit 9f2c526

File tree

6 files changed

+556
-3
lines changed

6 files changed

+556
-3
lines changed

lib/tls.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ const { isArrayBufferView } = require('internal/util/types');
5050

5151
const net = require('net');
5252
const { getOptionValue } = require('internal/options');
53+
const url = require('url');
5354
const { getRootCertificates, getSSLCiphers } = internalBinding('crypto');
5455
const { Buffer } = require('buffer');
5556
const EventEmitter = require('events');
57+
const { URL } = require('internal/url');
5658
const DuplexPair = require('internal/streams/duplexpair');
5759
const { canonicalizeIP } = internalBinding('cares_wrap');
5860
const _tls_common = require('_tls_common');
@@ -261,10 +263,12 @@ function splitEscapedAltNames(altNames) {
261263
return result;
262264
}
263265

266+
let urlWarningEmitted = false;
264267
exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
265268
const subject = cert.subject;
266269
const altNames = cert.subjectaltname;
267270
const dnsNames = [];
271+
const uriNames = [];
268272
const ips = [];
269273

270274
hostname = '' + hostname;
@@ -276,6 +280,22 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
276280
for (const name of splitAltNames) {
277281
if (name.startsWith('DNS:')) {
278282
dnsNames.push(name.slice(4));
283+
} else if (process.REVERT_CVE_2021_44531 && name.startsWith('URI:')) {
284+
let uri;
285+
try {
286+
uri = new URL(name.slice(4));
287+
} catch {
288+
uri = url.parse(name.slice(4));
289+
if (!urlWarningEmitted && !process.noDeprecation) {
290+
urlWarningEmitted = true;
291+
process.emitWarning(
292+
`The URI ${name.slice(4)} found in cert.subjectaltname ` +
293+
'is not a valid URI, and is supported in the tls module ' +
294+
'solely for compatibility.',
295+
'DeprecationWarning', 'DEP0109');
296+
}
297+
}
298+
uriNames.push(uri.hostname); // TODO(bnoordhuis) Also use scheme.
279299
} else if (name.startsWith('IP Address:')) {
280300
ips.push(canonicalizeIP(name.slice(11)));
281301
}
@@ -285,19 +305,25 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
285305
let valid = false;
286306
let reason = 'Unknown reason';
287307

308+
const hasAltNames =
309+
dnsNames.length > 0 || ips.length > 0 || uriNames.length > 0;
310+
288311
hostname = unfqdn(hostname); // Remove trailing dot for error messages.
289312

290313
if (net.isIP(hostname)) {
291314
valid = ips.includes(canonicalizeIP(hostname));
292315
if (!valid)
293316
reason = `IP: ${hostname} is not in the cert's list: ${ips.join(', ')}`;
294317
// TODO(bnoordhuis) Also check URI SANs that are IP addresses.
295-
} else if (dnsNames.length > 0 || (subject && subject.CN)) {
318+
} else if ((process.REVERT_CVE_2021_44531 && (hasAltNames || subject)) ||
319+
(dnsNames.length > 0 || (subject && subject.CN))) {
296320
const hostParts = splitHost(hostname);
297321
const wildcard = (pattern) => check(hostParts, pattern, true);
298322

299-
if (dnsNames.length > 0) {
300-
valid = dnsNames.some(wildcard);
323+
if ((process.REVERT_CVE_2021_44531 && hasAltNames) ||
324+
(dnsNames.length > 0)) {
325+
const noWildcard = (pattern) => check(hostParts, pattern, false);
326+
valid = dnsNames.some(wildcard) || uriNames.some(noWildcard);
301327
if (!valid)
302328
reason =
303329
`Host: ${hostname}. is not in the cert's altnames: ${altNames}`;

src/node_crypto_common.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "node_crypto_common.h"
77
#include "node.h"
88
#include "node_internals.h"
9+
#include "node_revert.h"
910
#include "node_url.h"
1011
#include "string_bytes.h"
1112
#include "v8.h"
@@ -482,6 +483,44 @@ void AddFingerprintDigest(
482483
}
483484
}
484485

486+
// deprecated, only used for security revert
487+
bool SafeX509ExtPrint(const BIOPointer& out, X509_EXTENSION* ext) {
488+
const X509V3_EXT_METHOD* method = X509V3_EXT_get(ext);
489+
490+
if (method != X509V3_EXT_get_nid(NID_subject_alt_name))
491+
return false;
492+
493+
GENERAL_NAMES* names = static_cast<GENERAL_NAMES*>(X509V3_EXT_d2i(ext));
494+
if (names == nullptr)
495+
return false;
496+
497+
for (int i = 0; i < sk_GENERAL_NAME_num(names); i++) {
498+
GENERAL_NAME* gen = sk_GENERAL_NAME_value(names, i);
499+
500+
if (i != 0)
501+
BIO_write(out.get(), ", ", 2);
502+
503+
if (gen->type == GEN_DNS) {
504+
ASN1_IA5STRING* name = gen->d.dNSName;
505+
506+
BIO_write(out.get(), "DNS:", 4);
507+
BIO_write(out.get(), name->data, name->length);
508+
} else {
509+
STACK_OF(CONF_VALUE)* nval = i2v_GENERAL_NAME(
510+
const_cast<X509V3_EXT_METHOD*>(method), gen, nullptr);
511+
if (nval == nullptr) {
512+
sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
513+
return false;
514+
}
515+
X509V3_EXT_val_prn(out.get(), nval, 0, 0);
516+
sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
517+
}
518+
}
519+
sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
520+
521+
return true;
522+
}
523+
485524
static inline bool IsSafeAltName(const char* name, size_t length, bool utf8) {
486525
for (size_t i = 0; i < length; i++) {
487526
char c = name[i];
@@ -706,6 +745,10 @@ bool SafeX509SubjectAltNamePrint(const BIOPointer& out, X509_EXTENSION* ext) {
706745
const X509V3_EXT_METHOD* method = X509V3_EXT_get(ext);
707746
CHECK(method == X509V3_EXT_get_nid(NID_subject_alt_name));
708747

748+
if (IsReverted(SECURITY_REVERT_CVE_2021_44532)) {
749+
return SafeX509ExtPrint(out, ext);
750+
}
751+
709752
GENERAL_NAMES* names = static_cast<GENERAL_NAMES*>(X509V3_EXT_d2i(ext));
710753
if (names == nullptr)
711754
return false;
@@ -731,6 +774,10 @@ bool SafeX509InfoAccessPrint(const BIOPointer& out, X509_EXTENSION* ext) {
731774
const X509V3_EXT_METHOD* method = X509V3_EXT_get(ext);
732775
CHECK(method == X509V3_EXT_get_nid(NID_info_access));
733776

777+
if (IsReverted(SECURITY_REVERT_CVE_2021_44532)) {
778+
return (X509V3_EXT_print(out.get(), ext, 0, 0) == 1);
779+
}
780+
734781
AUTHORITY_INFO_ACCESS* descs =
735782
static_cast<AUTHORITY_INFO_ACCESS*>(X509V3_EXT_d2i(ext));
736783
if (descs == nullptr)

src/node_revert.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
namespace node {
1717

1818
#define SECURITY_REVERSIONS(XX) \
19+
XX(CVE_2021_44531, "CVE-2021-44531", "Cert Verif Bypass via URI SAN") \
20+
XX(CVE_2021_44532, "CVE-2021-44532", "Cert Verif Bypass via Str Inject") \
1921
// XX(CVE_2016_PEND, "CVE-2016-PEND", "Vulnerability Title")
2022

2123
enum reversion {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Flags: --security-revert=CVE-2021-44531
2+
'use strict';
3+
const common = require('../common');
4+
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
const assert = require('assert');
9+
const util = require('util');
10+
11+
const tls = require('tls');
12+
13+
common.expectWarning('DeprecationWarning', [
14+
['The URI http://[a.b.a.com]/ found in cert.subjectaltname ' +
15+
'is not a valid URI, and is supported in the tls module ' +
16+
'solely for compatibility.',
17+
'DEP0109'],
18+
]);
19+
20+
const tests = [
21+
// Likewise for "URI:" Subject Alternative Names.
22+
// See also https://github.com/nodejs/node/issues/8108.
23+
{
24+
host: '8.8.8.8',
25+
cert: { subject: { CN: '8.8.8.8' }, subjectaltname: 'URI:http://8.8.8.8/' },
26+
error: 'IP: 8.8.8.8 is not in the cert\'s list: '
27+
},
28+
// Empty Subject w/URI name
29+
{
30+
host: 'a.b.a.com', cert: {
31+
subjectaltname: 'URI:http://a.b.a.com/',
32+
}
33+
},
34+
// URI names
35+
{
36+
host: 'a.b.a.com', cert: {
37+
subjectaltname: 'URI:http://a.b.a.com/',
38+
subject: {}
39+
}
40+
},
41+
{
42+
host: 'a.b.a.com', cert: {
43+
subjectaltname: 'URI:http://*.b.a.com/',
44+
subject: {}
45+
},
46+
error: 'Host: a.b.a.com. is not in the cert\'s altnames: ' +
47+
'URI:http://*.b.a.com/'
48+
},
49+
// Invalid URI
50+
{
51+
host: 'a.b.a.com', cert: {
52+
subjectaltname: 'URI:http://[a.b.a.com]/',
53+
subject: {}
54+
}
55+
},
56+
];
57+
58+
tests.forEach(function(test, i) {
59+
const err = tls.checkServerIdentity(test.host, test.cert);
60+
assert.strictEqual(err && err.reason,
61+
test.error,
62+
`Test# ${i} failed: ${util.inspect(test)} \n` +
63+
`${test.error} != ${(err && err.reason)}`);
64+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
// Flags: --security-revert=CVE-2021-44532
22+
'use strict';
23+
const common = require('../common');
24+
if (!common.hasCrypto)
25+
common.skip('missing crypto');
26+
27+
// Check getPeerCertificate can properly handle '\0' for fix CVE-2009-2408.
28+
29+
const assert = require('assert');
30+
const tls = require('tls');
31+
const fixtures = require('../common/fixtures');
32+
33+
const server = tls.createServer({
34+
key: fixtures.readSync(['0-dns', '0-dns-key.pem']),
35+
cert: fixtures.readSync(['0-dns', '0-dns-cert.pem'])
36+
}, common.mustCall((c) => {
37+
c.once('data', common.mustCall(() => {
38+
c.destroy();
39+
server.close();
40+
}));
41+
})).listen(0, common.mustCall(() => {
42+
const c = tls.connect(server.address().port, {
43+
rejectUnauthorized: false
44+
}, common.mustCall(() => {
45+
const cert = c.getPeerCertificate();
46+
assert.strictEqual(cert.subjectaltname,
47+
'DNS:good.example.org\0.evil.example.com, ' +
48+
'DNS:just-another.example.com, ' +
49+
'IP Address:8.8.8.8, ' +
50+
'IP Address:8.8.4.4, ' +
51+
'DNS:last.example.com');
52+
c.write('ok');
53+
}));
54+
}));

0 commit comments

Comments
 (0)