Skip to content

Instantly share code, notes, and snippets.

@waveto
Created February 23, 2010 22:31
Show Gist options
  • Save waveto/312809 to your computer and use it in GitHub Desktop.
Save waveto/312809 to your computer and use it in GitHub Desktop.
node.js TLS connection to smtp.gmail.com
tcp = require("tcp");
sys = require("sys");
// Google smtp server
var client = tcp.createConnection(25, 'smtp.gmail.com');
// Thawte Premium Server CA
var caPem = '-----BEGIN CERTIFICATE-----\n'
+ 'MIIDJzCCApCgAwIBAgIBATANBgkqhkiG9w0BAQQFADCBzjELMAkGA1UEBhMCWkEx\n'
+ 'FTATBgNVBAgTDFdlc3Rlcm4gQ2FwZTESMBAGA1UEBxMJQ2FwZSBUb3duMR0wGwYD\n'
+ 'VQQKExRUaGF3dGUgQ29uc3VsdGluZyBjYzEoMCYGA1UECxMfQ2VydGlmaWNhdGlv\n'
+ 'biBTZXJ2aWNlcyBEaXZpc2lvbjEhMB8GA1UEAxMYVGhhd3RlIFByZW1pdW0gU2Vy\n'
+ 'dmVyIENBMSgwJgYJKoZIhvcNAQkBFhlwcmVtaXVtLXNlcnZlckB0aGF3dGUuY29t\n'
+ 'MB4XDTk2MDgwMTAwMDAwMFoXDTIwMTIzMTIzNTk1OVowgc4xCzAJBgNVBAYTAlpB\n'
+ 'MRUwEwYDVQQIEwxXZXN0ZXJuIENhcGUxEjAQBgNVBAcTCUNhcGUgVG93bjEdMBsG\n'
+ 'A1UEChMUVGhhd3RlIENvbnN1bHRpbmcgY2MxKDAmBgNVBAsTH0NlcnRpZmljYXRp\n'
+ 'b24gU2VydmljZXMgRGl2aXNpb24xITAfBgNVBAMTGFRoYXd0ZSBQcmVtaXVtIFNl\n'
+ 'cnZlciBDQTEoMCYGCSqGSIb3DQEJARYZcHJlbWl1bS1zZXJ2ZXJAdGhhd3RlLmNv\n'
+ 'bTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0jY2aovXwlue2oFBYo847kkE\n'
+ 'VdbQ7xwblRZH7xhINTpS9CtqBo87L+pW46+GjZ4X9560ZXUCTe/LCaIhUdib0GfQ\n'
+ 'ug2SBhRz1JPLlyoAnFxODLz6FVL88kRu2hFKbgifLy3j+ao6hnO2RlNYyIkFvYMR\n'
+ 'uHM/qgeN9EJN50CdHDcCAwEAAaMTMBEwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG\n'
+ '9w0BAQQFAAOBgQAmSCwWwlj66BZ0DKqqX1Q/8tfJeGBeXm43YyJ3Nn6yF8Q0ufUI\n'
+ 'hfzJATj/Tb7yFkJD57taRvvBxhEf8UqwKEbJw8RCfbz6q1lu1bdRiBHjpIUZa4JM\n'
+ 'pAwSremkrj/xw0llmozFyD4lt5SZu5IycQfwhl7tUCemDaYj+bvLpgcUQg==\n'
+ '-----END CERTIFICATE-----\n';
client.setEncoding("utf8");
client.addListener("connect", function () {
client.write("EHLO smtp.gmail.com\n");
});
// Simple SMTP TLS handshake, noddy parsing
client.addListener("data", function (data) {
sys.puts(data);
if (data.indexOf('250-STARTTLS')>0) {
client.write("STARTTLS\n");
}
if (data.indexOf('Ready to start TLS')>=0) {
// Make the connection secure at this point
client.setSecure("X509_PEM", caPem);
}
if (data.indexOf('250-AUTH LOGIN PLAIN')>=0) {
// Get the details of the server's certificate
sys.puts(client.getPeerCertificate("DNSstring"));
sys.puts("Verified : "+client.verifyPeer());
// do stuff
client.close();
}
});
client.addListener("close", function () {
sys.puts("closed");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment