|
| 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}; |
0 commit comments