Merge branch 'v0.4'
[platform/upstream/nodejs.git] / lib / crypto.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22
23 try {
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;
33   var crypto = true;
34 } catch (e) {
35
36   var crypto = false;
37 }
38
39
40 function Credentials(secureProtocol, flags, context) {
41   if (!(this instanceof Credentials)) {
42     return new Credentials(secureProtocol);
43   }
44
45   if (!crypto) {
46     throw new Error('node.js not compiled with openssl crypto support.');
47   }
48
49   if (context) {
50     this.context = context;
51   } else {
52     this.context = new SecureContext();
53
54     if (secureProtocol) {
55       this.context.init(secureProtocol);
56     } else {
57       this.context.init();
58     }
59   }
60
61   if (flags) this.context.setOptions(flags);
62 }
63
64 exports.Credentials = Credentials;
65
66
67 exports.createCredentials = function(options, context) {
68   if (!options) options = {};
69
70   var c = new Credentials(options.secureProtocol,
71                           options.secureOptions,
72                           context);
73
74   if (context) return c;
75
76   if (options.key) c.context.setKey(options.key);
77
78   if (options.cert) c.context.setCert(options.cert);
79
80   if (options.ciphers) c.context.setCiphers(options.ciphers);
81
82   if (options.ca) {
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]);
86       }
87     } else {
88       c.context.addCACert(options.ca);
89     }
90   } else {
91     c.context.addRootCerts();
92   }
93
94   if (options.crl) {
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]);
98       }
99     } else {
100       c.context.addCRL(options.crl);
101     }
102   }
103
104   return c;
105 };
106
107
108 exports.Hash = Hash;
109 exports.createHash = function(hash) {
110   return new Hash(hash);
111 };
112
113
114 exports.Hmac = Hmac;
115 exports.createHmac = function(hmac, key) {
116   return (new Hmac).init(hmac, key);
117 };
118
119
120 exports.Cipher = Cipher;
121 exports.createCipher = function(cipher, password) {
122   return (new Cipher).init(cipher, password);
123 };
124
125
126 exports.createCipheriv = function(cipher, key, iv) {
127   return (new Cipher).initiv(cipher, key, iv);
128 };
129
130
131 exports.Decipher = Decipher;
132 exports.createDecipher = function(cipher, password) {
133   return (new Decipher).init(cipher, password);
134 };
135
136
137 exports.createDecipheriv = function(cipher, key, iv) {
138   return (new Decipher).initiv(cipher, key, iv);
139 };
140
141
142 exports.Sign = Sign;
143 exports.createSign = function(algorithm) {
144   return (new Sign).init(algorithm);
145 };
146
147 exports.Verify = Verify;
148 exports.createVerify = function(algorithm) {
149   return (new Verify).init(algorithm);
150 };
151
152 exports.DiffieHellman = DiffieHellman;
153 exports.createDiffieHellman = function(size_or_key, enc) {
154   if (!size_or_key) {
155     return new DiffieHellman();
156   } else if (!enc) {
157     return new DiffieHellman(size_or_key);
158   } else {
159     return new DiffieHellman(size_or_key, enc);
160   }
161
162 }