Skip to content

Commit 74cb142

Browse files
authored
Merge pull request #1554 from dbussink/add-verify-identity
Add identify verification option for TLS
2 parents 595d900 + 83c6b8c commit 74cb142

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/connection.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,19 @@ class Connection extends EventEmitter {
342342
minVersion: this.config.ssl.minVersion
343343
});
344344
const rejectUnauthorized = this.config.ssl.rejectUnauthorized;
345+
const verifyIdentity = this.config.ssl.verifyIdentity;
346+
const host = this.config.host;
347+
345348
let secureEstablished = false;
346349
const secureSocket = new Tls.TLSSocket(this.stream, {
347350
rejectUnauthorized: rejectUnauthorized,
348351
requestCert: true,
349352
secureContext: secureContext,
350353
isServer: false
351354
});
355+
if (typeof host === 'string') {
356+
secureSocket.setServername(host);
357+
}
352358
// error handler for secure socket
353359
secureSocket.on('_tlsError', err => {
354360
if (secureEstablished) {
@@ -359,7 +365,15 @@ class Connection extends EventEmitter {
359365
});
360366
secureSocket.on('secure', () => {
361367
secureEstablished = true;
362-
onSecure(rejectUnauthorized ? secureSocket.ssl.verifyError() : null);
368+
let callbackValue = null;
369+
if (rejectUnauthorized) {
370+
callbackValue = secureSocket.ssl.verifyError()
371+
if (!callbackValue && typeof host === 'string' && verifyIdentity) {
372+
const cert = secureSocket.ssl.getPeerCertificate(true);
373+
callbackValue = Tls.checkServerIdentity(host, cert)
374+
}
375+
}
376+
onSecure(callbackValue);
363377
});
364378
secureSocket.on('data', data => {
365379
this.packetParser.execute(data);

typings/mysql/lib/Connection.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ declare namespace Connection {
227227
* Configure the minimum supported version of SSL, the default is TLSv1.2.
228228
*/
229229
minVersion?: string;
230+
231+
/**
232+
* You can verify the server name identity presented on the server certificate when connecting to a MySQL server.
233+
* You should enable this but it is disabled by default right now for backwards compatibility.
234+
*/
235+
verifyIdentity?: boolean;
230236
}
231237
}
232238

0 commit comments

Comments
 (0)