Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

tls: better error messages for certificate validation errors #7460

Closed
bnoordhuis opened this issue Apr 12, 2014 · 2 comments
Closed

tls: better error messages for certificate validation errors #7460

bnoordhuis opened this issue Apr 12, 2014 · 2 comments

Comments

@bnoordhuis
Copy link
Member

Example:

var tls = require('tls');
var util = require('util');

var cacert =
  '-----BEGIN CERTIFICATE-----\n' +
  'MIIB5TCCAY8CAkFVMA0GCSqGSIb3DQEBBQUAMH0xCzAJBgNVBAYTAlVTMQswCQYD\n' +
  'VQQIDAJDQTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNjbzEZMBcGA1UECgwQU3Ryb25n\n' +
  'TG9vcCwgSW5jLjESMBAGA1UECwwJU3Ryb25nT3BzMRowGAYDVQQDDBFjYS5zdHJv\n' +
  'bmdsb29wLmNvbTAeFw05OTAzMzEyMjAwMDBaFw0wMTEyMjQyMjAwMDBaMH0xCzAJ\n' +
  'BgNVBAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNjbzEZ\n' +
  'MBcGA1UECgwQU3Ryb25nTG9vcCwgSW5jLjESMBAGA1UECwwJU3Ryb25nT3BzMRow\n' +
  'GAYDVQQDDBFjYS5zdHJvbmdsb29wLmNvbTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgC\n' +
  'QQC7dz/dN2Qms8xBbNny5KTHoOtbl+y7vNAl2YxKpLKRFvHH95uk3jlxp0xjLypr\n' +
  'CD9Dqs95UW6fcuPXaIEuKgAvAgMBAAEwDQYJKoZIhvcNAQEFBQADQQASgzqiepEQ\n' +
  'h/TGAIDbps9iBwAZE9vuGQwbJLPt1eqgmwqpcF0m/vV4STDiBuMBMLXTV4J+zv3B\n' +
  'QEQrSwnx/Y9f\n' +
  '-----END CERTIFICATE-----\n';

var cert =
  '-----BEGIN CERTIFICATE-----\n' +
  'MIIBnDCCAUYCAmQTMA0GCSqGSIb3DQEBBQUAMH0xCzAJBgNVB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sZjIqAqXNoIzpSQIgS35nfACVBZ5Infz98QKfjj2/ni2TW/jJ\n' +
  'kkmZ3rU5GKs=\n' +
  '-----END PRIVATE KEY-----\n';

var ca = [cert, cacert];
var ciphers = 'NULL-MD5';
var options = { ciphers: ciphers, ca: ca, cert: cert, key: key };

tls.createServer(options, function(conn) {
  conn.end('go away');
  this.close();
}).listen(function() {
  var conn = tls.connect({
    host: this.address().address,
    port: this.address().port,
    ciphers: ciphers,
  });
  conn.on('error', function(err) {
    console.error(util.inspect(err, true, 1));
  });
});

In this example, the server certificate is valid but the CA certificate has expired. When you run it, it prints:

{ [Error: CERT_HAS_EXPIRED]
  [stack]: [Getter/Setter],
  [arguments]: undefined,
  [type]: undefined,
  [message]: 'CERT_HAS_EXPIRED' }

That's not very helpful because when you inspect the return value of conn.getPeerCertificate(), the expiry date is August 15, 2026.

Suggestions for improvement:

  • Add a property that tells what certificate has expired.
  • Add the 'Not Before' and 'Not After' fields as properties (for this particular error.)

Apropos getPeerCertificate(), it's kind of odd that it calls the fields valid_from and valid_to instead of e.g. notBefore and notAfter.

@bnoordhuis
Copy link
Member Author

For some reason, GH swallows half the error message...

@indutny
Copy link
Member

indutny commented Apr 14, 2014

I'm too lame to implement it, but I'll accept PR doing it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants