2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2010-05-04 22:49:00 +02:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
try {
|
|
|
|
var binding = process.binding('crypto');
|
|
|
|
var SecureContext = binding.SecureContext;
|
2010-05-04 00:37:49 +02:00
|
|
|
var Hmac = binding.Hmac;
|
2010-04-12 22:25:16 +02:00
|
|
|
var Hash = binding.Hash;
|
2010-05-04 00:37:49 +02:00
|
|
|
var Cipher = binding.Cipher;
|
|
|
|
var Decipher = binding.Decipher;
|
2010-04-12 22:25:16 +02:00
|
|
|
var Sign = binding.Sign;
|
|
|
|
var Verify = binding.Verify;
|
2011-01-19 02:00:38 +01:00
|
|
|
var DiffieHellman = binding.DiffieHellman;
|
2012-01-22 19:24:37 +01:00
|
|
|
var DiffieHellmanGroup = binding.DiffieHellmanGroup;
|
2011-08-11 11:40:55 +02:00
|
|
|
var PBKDF2 = binding.PBKDF2;
|
2011-09-22 20:33:58 +02:00
|
|
|
var randomBytes = binding.randomBytes;
|
|
|
|
var pseudoRandomBytes = binding.pseudoRandomBytes;
|
2012-10-13 01:26:14 +02:00
|
|
|
var getCiphers = binding.getCiphers;
|
2010-04-12 22:25:16 +02:00
|
|
|
var crypto = true;
|
|
|
|
} catch (e) {
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
var crypto = false;
|
|
|
|
}
|
2010-05-04 22:49:00 +02:00
|
|
|
|
|
|
|
|
2011-05-20 19:27:39 +02:00
|
|
|
function Credentials(secureProtocol, flags, context) {
|
2010-12-01 00:59:59 +01:00
|
|
|
if (!(this instanceof Credentials)) {
|
2011-01-21 22:12:35 +01:00
|
|
|
return new Credentials(secureProtocol);
|
2010-12-01 00:59:59 +01:00
|
|
|
}
|
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
if (!crypto) {
|
2010-12-01 00:59:59 +01:00
|
|
|
throw new Error('node.js not compiled with openssl crypto support.');
|
2010-04-12 22:25:16 +02:00
|
|
|
}
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2011-05-19 21:42:13 +02:00
|
|
|
if (context) {
|
|
|
|
this.context = context;
|
2010-11-29 08:20:59 +01:00
|
|
|
} else {
|
2011-05-19 21:42:13 +02:00
|
|
|
this.context = new SecureContext();
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2011-05-19 21:42:13 +02:00
|
|
|
if (secureProtocol) {
|
|
|
|
this.context.init(secureProtocol);
|
|
|
|
} else {
|
|
|
|
this.context.init();
|
|
|
|
}
|
|
|
|
}
|
2011-04-02 06:53:07 +02:00
|
|
|
|
2011-05-20 19:27:39 +02:00
|
|
|
if (flags) this.context.setOptions(flags);
|
2010-04-12 22:25:16 +02:00
|
|
|
}
|
|
|
|
|
2010-12-01 00:59:59 +01:00
|
|
|
exports.Credentials = Credentials;
|
|
|
|
|
|
|
|
|
2011-05-19 21:42:13 +02:00
|
|
|
exports.createCredentials = function(options, context) {
|
2010-12-01 00:59:59 +01:00
|
|
|
if (!options) options = {};
|
2011-05-20 19:27:39 +02:00
|
|
|
|
|
|
|
var c = new Credentials(options.secureProtocol,
|
|
|
|
options.secureOptions,
|
|
|
|
context);
|
2011-05-19 21:42:13 +02:00
|
|
|
|
|
|
|
if (context) return c;
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2011-10-26 19:34:56 +02:00
|
|
|
if (options.key) {
|
|
|
|
if (options.passphrase) {
|
|
|
|
c.context.setKey(options.key, options.passphrase);
|
|
|
|
} else {
|
|
|
|
c.context.setKey(options.key);
|
|
|
|
}
|
|
|
|
}
|
2010-12-01 00:59:59 +01:00
|
|
|
|
|
|
|
if (options.cert) c.context.setCert(options.cert);
|
|
|
|
|
2011-04-05 05:27:20 +02:00
|
|
|
if (options.ciphers) c.context.setCiphers(options.ciphers);
|
|
|
|
|
2010-12-01 00:59:59 +01:00
|
|
|
if (options.ca) {
|
2010-12-01 20:23:25 +01:00
|
|
|
if (Array.isArray(options.ca)) {
|
2010-12-01 00:59:59 +01:00
|
|
|
for (var i = 0, len = options.ca.length; i < len; i++) {
|
|
|
|
c.context.addCACert(options.ca[i]);
|
|
|
|
}
|
2010-04-12 22:25:16 +02:00
|
|
|
} else {
|
2010-12-01 00:59:59 +01:00
|
|
|
c.context.addCACert(options.ca);
|
2010-04-12 22:25:16 +02:00
|
|
|
}
|
2010-05-04 22:49:00 +02:00
|
|
|
} else {
|
2010-12-01 01:13:05 +01:00
|
|
|
c.context.addRootCerts();
|
2010-04-12 22:25:16 +02:00
|
|
|
}
|
2010-12-01 00:59:59 +01:00
|
|
|
|
2010-11-23 19:00:42 +01:00
|
|
|
if (options.crl) {
|
|
|
|
if (Array.isArray(options.crl)) {
|
2011-10-05 00:08:18 +02:00
|
|
|
for (var i = 0, len = options.crl.length; i < len; i++) {
|
2010-11-23 19:00:42 +01:00
|
|
|
c.context.addCRL(options.crl[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.context.addCRL(options.crl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-21 06:19:22 +02:00
|
|
|
if (options.sessionIdContext) {
|
|
|
|
c.context.setSessionIdContext(options.sessionIdContext);
|
|
|
|
}
|
|
|
|
|
2012-05-13 21:38:23 +02:00
|
|
|
if (options.pfx) {
|
|
|
|
if (options.passphrase) {
|
|
|
|
c.context.loadPKCS12(options.pfx, options.passphrase);
|
|
|
|
} else {
|
|
|
|
c.context.loadPKCS12(options.pfx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
return c;
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-04-12 22:25:16 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
exports.Hash = Hash;
|
2010-12-02 02:29:11 +01:00
|
|
|
exports.createHash = function(hash) {
|
2010-06-14 22:10:23 +02:00
|
|
|
return new Hash(hash);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-04-12 22:25:16 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-05-04 00:37:49 +02:00
|
|
|
exports.Hmac = Hmac;
|
2010-12-02 02:29:11 +01:00
|
|
|
exports.createHmac = function(hmac, key) {
|
2010-05-04 00:37:49 +02:00
|
|
|
return (new Hmac).init(hmac, key);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-05-04 00:37:49 +02:00
|
|
|
exports.Cipher = Cipher;
|
2011-07-24 08:56:23 +02:00
|
|
|
exports.createCipher = function(cipher, password) {
|
|
|
|
return (new Cipher).init(cipher, password);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
exports.createCipheriv = function(cipher, key, iv) {
|
2010-05-04 00:37:49 +02:00
|
|
|
return (new Cipher).initiv(cipher, key, iv);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-05-04 00:37:49 +02:00
|
|
|
exports.Decipher = Decipher;
|
2011-07-24 08:56:23 +02:00
|
|
|
exports.createDecipher = function(cipher, password) {
|
|
|
|
return (new Decipher).init(cipher, password);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
exports.createDecipheriv = function(cipher, key, iv) {
|
2010-05-04 00:37:49 +02:00
|
|
|
return (new Decipher).initiv(cipher, key, iv);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
exports.Sign = Sign;
|
2010-12-02 02:29:11 +01:00
|
|
|
exports.createSign = function(algorithm) {
|
2010-04-12 22:25:16 +02:00
|
|
|
return (new Sign).init(algorithm);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-04-12 22:25:16 +02:00
|
|
|
|
|
|
|
exports.Verify = Verify;
|
2010-12-02 02:29:11 +01:00
|
|
|
exports.createVerify = function(algorithm) {
|
2010-04-12 22:25:16 +02:00
|
|
|
return (new Verify).init(algorithm);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2011-01-19 02:00:38 +01:00
|
|
|
|
|
|
|
exports.DiffieHellman = DiffieHellman;
|
|
|
|
exports.createDiffieHellman = function(size_or_key, enc) {
|
|
|
|
if (!size_or_key) {
|
|
|
|
return new DiffieHellman();
|
|
|
|
} else if (!enc) {
|
|
|
|
return new DiffieHellman(size_or_key);
|
|
|
|
} else {
|
|
|
|
return new DiffieHellman(size_or_key, enc);
|
|
|
|
}
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
};
|
2012-01-22 19:24:37 +01:00
|
|
|
exports.getDiffieHellman = function(group_name) {
|
|
|
|
return new DiffieHellmanGroup(group_name);
|
|
|
|
};
|
2011-08-11 11:40:55 +02:00
|
|
|
|
|
|
|
exports.pbkdf2 = PBKDF2;
|
2011-09-22 20:33:58 +02:00
|
|
|
|
|
|
|
exports.randomBytes = randomBytes;
|
|
|
|
exports.pseudoRandomBytes = pseudoRandomBytes;
|
|
|
|
|
|
|
|
exports.rng = randomBytes;
|
|
|
|
exports.prng = pseudoRandomBytes;
|
2012-10-13 01:26:14 +02:00
|
|
|
|
|
|
|
exports.getCiphers = getCiphers;
|