1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
24 var binding = process.binding('crypto');
25 var SecureContext = binding.SecureContext;
26 var Hmac = binding.Hmac;
27 var Hash = binding.Hash;
28 var Cipher = binding.Cipher;
29 var Decipher = binding.Decipher;
30 var Sign = binding.Sign;
31 var Verify = binding.Verify;
32 var DiffieHellman = binding.DiffieHellman;
40 function Credentials(secureProtocol, flags, context) {
41 if (!(this instanceof Credentials)) {
42 return new Credentials(secureProtocol);
46 throw new Error('node.js not compiled with openssl crypto support.');
50 this.context = context;
52 this.context = new SecureContext();
55 this.context.init(secureProtocol);
61 if (flags) this.context.setOptions(flags);
64 exports.Credentials = Credentials;
67 exports.createCredentials = function(options, context) {
68 if (!options) options = {};
70 var c = new Credentials(options.secureProtocol,
71 options.secureOptions,
74 if (context) return c;
76 if (options.key) c.context.setKey(options.key);
78 if (options.cert) c.context.setCert(options.cert);
80 if (options.ciphers) c.context.setCiphers(options.ciphers);
83 if (Array.isArray(options.ca)) {
84 for (var i = 0, len = options.ca.length; i < len; i++) {
85 c.context.addCACert(options.ca[i]);
88 c.context.addCACert(options.ca);
91 c.context.addRootCerts();
95 if (Array.isArray(options.crl)) {
96 for(var i = 0, len = options.crl.length; i < len; i++) {
97 c.context.addCRL(options.crl[i]);
100 c.context.addCRL(options.crl);
109 exports.createHash = function(hash) {
110 return new Hash(hash);
115 exports.createHmac = function(hmac, key) {
116 return (new Hmac).init(hmac, key);
120 exports.Cipher = Cipher;
121 exports.createCipher = function(cipher, password) {
122 return (new Cipher).init(cipher, password);
126 exports.createCipheriv = function(cipher, key, iv) {
127 return (new Cipher).initiv(cipher, key, iv);
131 exports.Decipher = Decipher;
132 exports.createDecipher = function(cipher, password) {
133 return (new Decipher).init(cipher, password);
137 exports.createDecipheriv = function(cipher, key, iv) {
138 return (new Decipher).initiv(cipher, key, iv);
143 exports.createSign = function(algorithm) {
144 return (new Sign).init(algorithm);
147 exports.Verify = Verify;
148 exports.createVerify = function(algorithm) {
149 return (new Verify).init(algorithm);
152 exports.DiffieHellman = DiffieHellman;
153 exports.createDiffieHellman = function(size_or_key, enc) {
155 return new DiffieHellman();
157 return new DiffieHellman(size_or_key);
159 return new DiffieHellman(size_or_key, enc);