Skip to content

Commit bf84c26

Browse files
Merge pull request #1203 from browserstack/fix/SDK-8000-emudhra-key-block
[SDK-8000] Reject TLS chains carrying the compromised eMudhra intermediate keys
2 parents a4eaeb6 + 96b3a51 commit bf84c26

3 files changed

Lines changed: 221 additions & 0 deletions

File tree

‎bin/helpers/distrustedCaCheck.js‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict';
2+
3+
/*
4+
* The private keys of 8 eMudhra intermediate CAs are recoverable, so anyone can issue certificates
5+
* under them. Browsers and OS trust stores block them, but Node's bundled roots still accept their
6+
* chains. Reject any TLS chain that carries one of those keys (SPKI SHA-256, which catches both the
7+
* emSign-issued and the Sectigo cross-signed copies) or one of their names.
8+
* Source: https://emudhra.we-have-a-problem.com/impacted-intermediates.json
9+
*/
10+
11+
const tls = require('tls');
12+
const crypto = require('crypto');
13+
14+
const STATE_KEY = Symbol.for('browserstack.distrustedCaCheck');
15+
16+
const DISTRUSTED_SPKI_SHA256 = [
17+
'EBED6475C2C2D80FCB199F3FE8DD6E6B3CE60B77EB15D4E42C3DEC0C6F5B5105', // EM DV TLS CA - G3B
18+
'43BFFA19D6EF651451F6841E019931E96F6F2EAEFF28AB4B6F1CE4576DE2717B', // EM OV TLS CA - G3B
19+
'E0254C7FCDC265E2F0F8A428DABB75AB4AE039B4872D62F5EE57F0877D5A8A41', // EM EV TLS CA - G3B
20+
'49DD353DC2B919A3770645117D0E60C16115E7D1BC259EEFE734B00EA434FE5F', // EM DV TLS CA - G3C
21+
'63ECEFEE431D49523BC1136C4E72D0206A477086CAF606CD5CF025686C109CF8', // EM OV TLS CA - G3C
22+
'5C0660A2B164910D8FF589C18E14552E9E0EC237DCEFE2CB736D41A4811F81A0', // EM EV TLS CA - G3C
23+
'EC4FFA1AD0AC88081314B7AAFFEF6A91F97CD57291BB5BA686C5415F5EB4F6B6', // EM OV CS CA - G3B
24+
'92C063D67E8F14DB71D288E11266FFDAF69CBF115F7BAB14F3BC2F68D25E0E30' // EM OV CS CA - G3C
25+
];
26+
27+
// Node links issuerCertificate by name, so a decoy certificate sent ahead of the real intermediate can divert
28+
// the walk away from the distrusted key. The 8 intermediates are pathlen:0, so the leaf's issuer is always
29+
// one of their names; matching names closes that gap.
30+
const DISTRUSTED_NAMES = [
31+
'EM DV TLS CA - G3B|eMudhra BV|NL',
32+
'EM OV TLS CA - G3B|eMudhra BV|NL',
33+
'EM EV TLS CA - G3B|eMudhra BV|NL',
34+
'EM DV TLS CA - G3C|eMudhra Inc|US',
35+
'EM OV TLS CA - G3C|eMudhra Inc|US',
36+
'EM EV TLS CA - G3C|eMudhra Inc|US',
37+
'EM OV CS CA - G3B|eMudhra BV|NL',
38+
'EM OV CS CA - G3C|eMudhra Inc|US'
39+
].map((name) => name.toLowerCase());
40+
41+
// OpenSSL chains names case-insensitively with whitespace collapsed, so compare them the same way.
42+
function normalize(value) {
43+
return String((Array.isArray(value) ? value.join(',') : value) || '').trim().replace(/\s+/g, ' ').toLowerCase();
44+
}
45+
46+
function nameKey(name) {
47+
return name ? `${normalize(name.CN)}|${normalize(name.O)}|${normalize(name.C)}` : '';
48+
}
49+
50+
// The peer certificate's `pubkey` is the raw point for EC keys, not the SPKI, so re-export the SPKI from
51+
// the certificate. createPublicKey rather than X509Certificate, which needs Node 15.6.
52+
function spkiSha256(rawCert) {
53+
const pem = `-----BEGIN CERTIFICATE-----\n${rawCert.toString('base64').match(/.{1,64}/g).join('\n')}\n-----END CERTIFICATE-----\n`;
54+
const spki = crypto.createPublicKey(pem).export({type: 'spki', format: 'der'});
55+
56+
return crypto.createHash('sha256').update(spki).digest('hex').toUpperCase();
57+
}
58+
59+
// Returns the name of the distrusted intermediate found in the chain, or null.
60+
function findDistrustedIntermediate(cert, state) {
61+
for (let current = cert, previous; current && current !== previous; previous = current, current = current.issuerCertificate) {
62+
if (state.distrustedNames.has(nameKey(current.subject))) {
63+
return current.subject.CN;
64+
}
65+
if (state.distrustedNames.has(nameKey(current.issuer))) {
66+
return current.issuer.CN;
67+
}
68+
let hash;
69+
try {
70+
hash = current.raw && spkiSha256(current.raw);
71+
} catch (err) {
72+
// A key createPublicKey cannot parse is not one of the distrusted EC keys; keep checking its issuers.
73+
continue;
74+
}
75+
if (hash && state.distrusted.has(hash)) {
76+
return (current.subject && current.subject.CN) || 'unknown';
77+
}
78+
}
79+
80+
return null;
81+
}
82+
83+
// Every copy of this module in the process shares one state and one wrapper. The state lives on the tls module,
84+
// not on global: Jest gives each test file its own global but shares core modules across them.
85+
function installDistrustedCaCheck() {
86+
if (typeof crypto.createPublicKey !== 'function') {
87+
return false;
88+
}
89+
const existing = tls[STATE_KEY];
90+
if (existing) {
91+
DISTRUSTED_SPKI_SHA256.forEach((hash) => existing.distrusted.add(hash));
92+
DISTRUSTED_NAMES.forEach((name) => existing.distrustedNames.add(name));
93+
94+
return false;
95+
}
96+
const state = {distrusted: new Set(DISTRUSTED_SPKI_SHA256), distrustedNames: new Set(DISTRUSTED_NAMES)};
97+
tls[STATE_KEY] = state;
98+
99+
const originalCheckServerIdentity = tls.checkServerIdentity;
100+
tls.checkServerIdentity = function checkServerIdentity(hostname, cert) {
101+
const identityError = originalCheckServerIdentity(hostname, cert);
102+
if (identityError) {
103+
return identityError;
104+
}
105+
const distrustedName = findDistrustedIntermediate(cert, state);
106+
if (!distrustedName) {
107+
return undefined;
108+
}
109+
const error = new Error(`Certificate chain for ${hostname} contains the distrusted eMudhra intermediate "${distrustedName}"`);
110+
error.code = 'ERR_TLS_DISTRUSTED_INTERMEDIATE';
111+
error.host = hostname;
112+
113+
return error;
114+
};
115+
116+
return true;
117+
}
118+
119+
module.exports = {installDistrustedCaCheck};

‎bin/helpers/utils.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
"use strict";
2+
// Installed here rather than in runner.js: the CLI and the reporter/plugins that run inside the customer's Cypress all load this file.
3+
try {
4+
require('./distrustedCaCheck').installDistrustedCaCheck();
5+
} catch (err) {
6+
require('./logger').winstonLogger.debug(`Distrusted CA check failed to install: ${err && err.message}`);
7+
}
8+
29
const os = require("os");
310
const path = require("path");
411
const fs = require("fs");
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
const chai = require("chai"),
3+
expect = chai.expect,
4+
tls = require('tls');
5+
6+
const { installDistrustedCaCheck } = require("../../../../bin/helpers/distrustedCaCheck");
7+
8+
// EM DV TLS CA - G3B as issued by emSign Root TLS CA - G3, from https://emudhra.we-have-a-problem.com/impacted-intermediates.json
9+
const DISTRUSTED_INTERMEDIATE_PEM = `-----BEGIN CERTIFICATE-----
10+
MIIDaDCCAu6gAwIBAgIQAKSu5njyM2K0wORgt3LyFDAKBggqhkjOPQQDAzBWMQsw
11+
CQYDVQQGEwJJTjElMCMGA1UECgwcZU11ZGhyYSBUZWNobm9sb2dpZXMgTGltaXRl
12+
ZDEgMB4GA1UEAwwXZW1TaWduIFJvb3QgVExTIENBIC0gRzMwHhcNMjUwNDMwMTU1
13+
NDA4WhcNNDAwNDI4MTgzMDAwWjA/MQswCQYDVQQGEwJOTDETMBEGA1UECgwKZU11
14+
ZGhyYSBCVjEbMBkGA1UEAwwSRU0gRFYgVExTIENBIC0gRzNCMHYwEAYHKoZIzj0C
15+
AQYFK4EEACIDYgAE557ZwEqXc8RzXlERHahawr1eZKs3S4DhLLwgOOweTw47gS/J
16+
hZ4eZ6+wTi+LyX90hbplqpEe6KTkW2LVzAPVa+Z4s8OMtNyTfKJGDiOEU8iSk10p
17+
1E+R4VYGtjzO2rRoo4IBljCCAZIwHwYDVR0jBBgwFoAU4TqrIXzx401UrtRyt/2L
18+
WUIgkFQwHQYDVR0OBBYEFOIeL+ccGTkOVUtiwSAkVzUkiiUUMB0GA1UdJQQWMBQG
19+
CCsGAQUFBwMBBggrBgEFBQcDAjBdBgNVHSAEVjBUMDkGCysGAQQBg44hAQABMCow
20+
KAYIKwYBBQUHAgEWHGh0dHA6Ly9yZXBvc2l0b3J5LmVtc2lnbi5jb20wDQYLKwYB
21+
BAGDjiEBAmQwCAYGZ4EMAQIBMHUGCCsGAQUFBwEBBGkwZzAkBggrBgEFBQcwAYYY
22+
aHR0cDovL29jc3AtYi5lbXNpZ24uY29tMD8GCCsGAQUFBzAChjNodHRwOi8vcmVw
23+
b3NpdG9yeS5lbXNpZ24uY29tL2NlcnRzL0VNRFZUTFNDQUczQi5wN2MwNwYDVR0f
24+
BDAwLjAsoCqgKIYmaHR0cDovL2NybC5lbXNpZ24uY29tLz9yb290dGxzY2FnMy5j
25+
cmwwDgYDVR0PAQH/BAQDAgEGMBIGA1UdEwEB/wQIMAYBAf8CAQAwCgYIKoZIzj0E
26+
AwMDaAAwZQIwLmsGXi6oFMJ2klkcEMXMug6gvxQWZWkewiDWVzQyl2L6z6DuQfPO
27+
Ibh1grvdULDCAjEAxZvLlxUavG69J6wnSjC8XBbqNIQsJ6CE2QdNOe84Ptxb6VG8
28+
gt2TlfJbHii+KNvK
29+
-----END CERTIFICATE-----`;
30+
31+
const NOT_A_CERTIFICATE = Buffer.from('not a certificate');
32+
33+
function peerChain(intermediate, leafIssuer = intermediate.subject) {
34+
const root = { subject: { CN: 'Test Root', O: 'Test', C: 'US' }, issuer: { CN: 'Test Root', O: 'Test', C: 'US' }, raw: NOT_A_CERTIFICATE };
35+
root.issuerCertificate = root;
36+
intermediate.issuer = root.subject;
37+
intermediate.issuerCertificate = root;
38+
return {
39+
subject: { CN: 'example.com' },
40+
subjectaltname: 'DNS:example.com',
41+
issuer: leafIssuer,
42+
raw: NOT_A_CERTIFICATE,
43+
issuerCertificate: intermediate,
44+
};
45+
}
46+
47+
describe("distrustedCaCheck", () => {
48+
before(() => {
49+
installDistrustedCaCheck();
50+
});
51+
52+
it("refuses a chain carrying a distrusted intermediate's name", () => {
53+
const cert = peerChain({ subject: { CN: 'EM DV TLS CA - G3B', O: 'eMudhra BV', C: 'NL' }, raw: NOT_A_CERTIFICATE });
54+
55+
const error = tls.checkServerIdentity('example.com', cert);
56+
57+
expect(error).to.be.an('error');
58+
expect(error.code).to.equal('ERR_TLS_DISTRUSTED_INTERMEDIATE');
59+
});
60+
61+
it("compares names case-insensitively with whitespace collapsed", () => {
62+
const cert = peerChain(
63+
{ subject: { CN: 'Test Intermediate', O: 'Test', C: 'US' }, raw: NOT_A_CERTIFICATE },
64+
{ CN: ' em dv tls ca - g3b', O: 'EMUDHRA BV', C: 'nl' }
65+
);
66+
67+
expect(tls.checkServerIdentity('example.com', cert).code).to.equal('ERR_TLS_DISTRUSTED_INTERMEDIATE');
68+
});
69+
70+
it("refuses a chain carrying a distrusted key under another name", () => {
71+
const raw = Buffer.from(DISTRUSTED_INTERMEDIATE_PEM.replace(/-----[^-]+-----|\s/g, ''), 'base64');
72+
const cert = peerChain({ subject: { CN: 'Test Intermediate', O: 'Test', C: 'US' }, raw });
73+
74+
expect(tls.checkServerIdentity('example.com', cert).code).to.equal('ERR_TLS_DISTRUSTED_INTERMEDIATE');
75+
});
76+
77+
it("allows a chain without a distrusted intermediate", () => {
78+
const cert = peerChain({ subject: { CN: 'Test Intermediate', O: 'Test', C: 'US' }, raw: NOT_A_CERTIFICATE });
79+
80+
expect(tls.checkServerIdentity('example.com', cert)).to.be.undefined;
81+
});
82+
83+
it("returns the hostname error before checking the chain", () => {
84+
const cert = peerChain({ subject: { CN: 'EM DV TLS CA - G3B', O: 'eMudhra BV', C: 'NL' }, raw: NOT_A_CERTIFICATE });
85+
86+
expect(tls.checkServerIdentity('other.com', cert).code).to.equal('ERR_TLS_CERT_ALTNAME_INVALID');
87+
});
88+
89+
it("does not wrap tls.checkServerIdentity a second time", () => {
90+
const wrapped = tls.checkServerIdentity;
91+
92+
expect(installDistrustedCaCheck()).to.equal(false);
93+
expect(tls.checkServerIdentity).to.equal(wrapped);
94+
});
95+
});

0 commit comments

Comments
 (0)