1 | # Forge
|
2 |
|
3 | [![npm package](https://nodei.co/npm/node-forge.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/node-forge/)
|
4 |
|
5 | [![Build status](https://img.shields.io/travis/digitalbazaar/forge.svg?branch=master)](https://travis-ci.org/digitalbazaar/forge)
|
6 |
|
7 | A native implementation of [TLS][] (and various other cryptographic tools) in
|
8 | [JavaScript][].
|
9 |
|
10 | Introduction
|
11 | ------------
|
12 |
|
13 | The Forge software is a fully native implementation of the [TLS][] protocol
|
14 | in JavaScript, a set of cryptography utilities, and a set of tools for
|
15 | developing Web Apps that utilize many network resources.
|
16 |
|
17 | Performance
|
18 | ------------
|
19 |
|
20 | Forge is fast. Benchmarks against other popular JavaScript cryptography
|
21 | libraries can be found here:
|
22 |
|
23 | * http://dominictarr.github.io/crypto-bench/
|
24 | * http://cryptojs.altervista.org/test/simulate-threading-speed_test.html
|
25 |
|
26 | Documentation
|
27 | -------------
|
28 |
|
29 | * [Introduction](#introduction)
|
30 | * [Performance](#performance)
|
31 | * [Installation](#installation)
|
32 | * [Testing](#testing)
|
33 | * [Contributing](#contributing)
|
34 |
|
35 | ### API
|
36 |
|
37 | * [Options](#options)
|
38 |
|
39 | ### Transports
|
40 |
|
41 | * [TLS](#tls)
|
42 | * [HTTP](#http)
|
43 | * [SSH](#ssh)
|
44 | * [XHR](#xhr)
|
45 | * [Sockets](#socket)
|
46 |
|
47 | ### Ciphers
|
48 |
|
49 | * [CIPHER](#cipher)
|
50 | * [AES](#aes)
|
51 | * [DES](#des)
|
52 | * [RC2](#rc2)
|
53 |
|
54 | ### PKI
|
55 |
|
56 | * [ED25519](#ed25519)
|
57 | * [RSA](#rsa)
|
58 | * [RSA-KEM](#rsakem)
|
59 | * [X.509](#x509)
|
60 | * [PKCS#5](#pkcs5)
|
61 | * [PKCS#7](#pkcs7)
|
62 | * [PKCS#8](#pkcs8)
|
63 | * [PKCS#10](#pkcs10)
|
64 | * [PKCS#12](#pkcs12)
|
65 | * [ASN.1](#asn)
|
66 |
|
67 | ### Message Digests
|
68 |
|
69 | * [SHA1](#sha1)
|
70 | * [SHA256](#sha256)
|
71 | * [SHA384](#sha384)
|
72 | * [SHA512](#sha512)
|
73 | * [MD5](#md5)
|
74 | * [HMAC](#hmac)
|
75 |
|
76 | ### Utilities
|
77 |
|
78 | * [Prime](#prime)
|
79 | * [PRNG](#prng)
|
80 | * [Tasks](#task)
|
81 | * [Utilities](#util)
|
82 | * [Logging](#log)
|
83 | * [Debugging](#debug)
|
84 | * [Flash Networking Support](#flash)
|
85 |
|
86 | ### Other
|
87 |
|
88 | * [Security Considerations](#security-considerations)
|
89 | * [Library Background](#library-background)
|
90 | * [Contact](#contact)
|
91 | * [Donations](#donations)
|
92 |
|
93 | ---------------------------------------
|
94 |
|
95 | Installation
|
96 | ------------
|
97 |
|
98 | **Note**: Please see the [Security Considerations](#security-considerations)
|
99 | section before using packaging systems and pre-built files.
|
100 |
|
101 | Forge uses a [CommonJS][] module structure with a build process for browser
|
102 | bundles. The older [0.6.x][] branch with standalone files is available but will
|
103 | not be regularly updated.
|
104 |
|
105 | ### Node.js
|
106 |
|
107 | If you want to use forge with [Node.js][], it is available through `npm`:
|
108 |
|
109 | https://npmjs.org/package/node-forge
|
110 |
|
111 | Installation:
|
112 |
|
113 | npm install node-forge
|
114 |
|
115 | You can then use forge as a regular module:
|
116 |
|
117 | ```js
|
118 | var forge = require('node-forge');
|
119 | ```
|
120 |
|
121 | The npm package includes pre-built `forge.min.js`, `forge.all.min.js`, and
|
122 | `prime.worker.min.js` using the [UMD][] format.
|
123 |
|
124 | ### Bundle / Bower
|
125 |
|
126 | Each release is published in a separate repository as pre-built and minimized
|
127 | basic forge bundles using the [UMD][] format.
|
128 |
|
129 | https://github.com/digitalbazaar/forge-dist
|
130 |
|
131 | This bundle can be used in many environments. In particular it can be installed
|
132 | with [Bower][]:
|
133 |
|
134 | bower install forge
|
135 |
|
136 | ### jsDelivr CDN
|
137 |
|
138 | To use it via [jsDelivr](https://www.jsdelivr.com/package/npm/node-forge) include this in your html:
|
139 |
|
140 | ```html
|
141 | <script src="https://cdn.jsdelivr.net/npm/node-forge@0.7.0/dist/forge.min.js"></script>
|
142 | ```
|
143 |
|
144 | ### unpkg CDN
|
145 |
|
146 | To use it via [unpkg](https://unpkg.com/#/) include this in your html:
|
147 |
|
148 | ```html
|
149 | <script src="https://unpkg.com/node-forge@0.7.0/dist/forge.min.js"></script>
|
150 | ```
|
151 |
|
152 | ### Development Requirements
|
153 |
|
154 | The core JavaScript has the following requirements to build and test:
|
155 |
|
156 | * Building a browser bundle:
|
157 | * Node.js
|
158 | * npm
|
159 | * Testing
|
160 | * Node.js
|
161 | * npm
|
162 | * Chrome, Firefox, Safari (optional)
|
163 |
|
164 | Some special networking features can optionally use a Flash component. See the
|
165 | [Flash README](./flash/README.md) for details.
|
166 |
|
167 | ### Building for a web browser
|
168 |
|
169 | To create single file bundles for use with browsers run the following:
|
170 |
|
171 | npm install
|
172 | npm run build
|
173 |
|
174 | This will create single non-minimized and minimized files that can be
|
175 | included in the browser:
|
176 |
|
177 | dist/forge.js
|
178 | dist/forge.min.js
|
179 |
|
180 | A bundle that adds some utilities and networking support is also available:
|
181 |
|
182 | dist/forge.all.js
|
183 | dist/forge.all.min.js
|
184 |
|
185 | Include the file via:
|
186 |
|
187 | ```html
|
188 | <script src="YOUR_SCRIPT_PATH/forge.js"></script>
|
189 | ```
|
190 | or
|
191 | ```html
|
192 | <script src="YOUR_SCRIPT_PATH/forge.min.js"></script>
|
193 | ```
|
194 |
|
195 | The above bundles will synchronously create a global 'forge' object.
|
196 |
|
197 | **Note**: These bundles will not include any WebWorker scripts (eg:
|
198 | `dist/prime.worker.js`), so these will need to be accessible from the browser
|
199 | if any WebWorkers are used.
|
200 |
|
201 | ### Building a custom browser bundle
|
202 |
|
203 | The build process uses [webpack][] and the [config](./webpack.config.js) file
|
204 | can be modified to generate a file or files that only contain the parts of
|
205 | forge you need.
|
206 |
|
207 | [Browserify][] override support is also present in `package.json`.
|
208 |
|
209 | Testing
|
210 | -------
|
211 |
|
212 | ### Prepare to run tests
|
213 |
|
214 | npm install
|
215 |
|
216 | ### Running automated tests with Node.js
|
217 |
|
218 | Forge natively runs in a [Node.js][] environment:
|
219 |
|
220 | npm test
|
221 |
|
222 | ### Running automated tests with Headless Chrome
|
223 |
|
224 | Automated testing is done via [Karma][]. By default it will run the tests with
|
225 | Headless Chrome.
|
226 |
|
227 | npm run test-karma
|
228 |
|
229 | Is 'mocha' reporter output too verbose? Other reporters are available. Try
|
230 | 'dots', 'progress', or 'tap'.
|
231 |
|
232 | npm run test-karma -- --reporters progress
|
233 |
|
234 | By default [webpack][] is used. [Browserify][] can also be used.
|
235 |
|
236 | BUNDLER=browserify npm run test-karma
|
237 |
|
238 | ### Running automated tests with one or more browsers
|
239 |
|
240 | You can also specify one or more browsers to use.
|
241 |
|
242 | npm run test-karma -- --browsers Chrome,Firefox,Safari,ChromeHeadless
|
243 |
|
244 | The reporter option and `BUNDLER` environment variable can also be used.
|
245 |
|
246 | ### Running manual tests in a browser
|
247 |
|
248 | Testing in a browser uses [webpack][] to combine forge and all tests and then
|
249 | loading the result in a browser. A simple web server is provided that will
|
250 | output the HTTP or HTTPS URLs to load. It also will start a simple Flash Policy
|
251 | Server. Unit tests and older legacy tests are provided. Custom ports can be
|
252 | used by running `node tests/server.js` manually.
|
253 |
|
254 | To run the unit tests in a browser a special forge build is required:
|
255 |
|
256 | npm run test-build
|
257 |
|
258 | To run legacy browser based tests the main forge build is required:
|
259 |
|
260 | npm run build
|
261 |
|
262 | The tests are run with a custom server that prints out the URLs to use:
|
263 |
|
264 | npm run test-server
|
265 |
|
266 | ### Running other tests
|
267 |
|
268 | There are some other random tests and benchmarks available in the tests
|
269 | directory.
|
270 |
|
271 | ### Coverage testing
|
272 |
|
273 | To perform coverage testing of the unit tests, run the following. The results
|
274 | will be put in the `coverage/` directory. Note that coverage testing can slow
|
275 | down some tests considerably.
|
276 |
|
277 | npm install
|
278 | npm run coverage
|
279 |
|
280 | Contributing
|
281 | ------------
|
282 |
|
283 | Any contributions (eg: PRs) that are accepted will be brought under the same
|
284 | license used by the rest of the Forge project. This license allows Forge to
|
285 | be used under the terms of either the BSD License or the GNU General Public
|
286 | License (GPL) Version 2.
|
287 |
|
288 | See: [LICENSE](https://github.com/digitalbazaar/forge/blob/cbebca3780658703d925b61b2caffb1d263a6c1d/LICENSE)
|
289 |
|
290 | If a contribution contains 3rd party source code with its own license, it
|
291 | may retain it, so long as that license is compatible with the Forge license.
|
292 |
|
293 | API
|
294 | ---
|
295 |
|
296 | <a name="options" />
|
297 |
|
298 | ### Options
|
299 |
|
300 | If at any time you wish to disable the use of native code, where available,
|
301 | for particular forge features like its secure random number generator, you
|
302 | may set the ```forge.options.usePureJavaScript``` flag to ```true```. It is
|
303 | not recommended that you set this flag as native code is typically more
|
304 | performant and may have stronger security properties. It may be useful to
|
305 | set this flag to test certain features that you plan to run in environments
|
306 | that are different from your testing environment.
|
307 |
|
308 | To disable native code when including forge in the browser:
|
309 |
|
310 | ```js
|
311 | // run this *after* including the forge script
|
312 | forge.options.usePureJavaScript = true;
|
313 | ```
|
314 |
|
315 | To disable native code when using Node.js:
|
316 |
|
317 | ```js
|
318 | var forge = require('node-forge');
|
319 | forge.options.usePureJavaScript = true;
|
320 | ```
|
321 |
|
322 | Transports
|
323 | ----------
|
324 |
|
325 | <a name="tls" />
|
326 |
|
327 | ### TLS
|
328 |
|
329 | Provides a native javascript client and server-side [TLS][] implementation.
|
330 |
|
331 | __Examples__
|
332 |
|
333 | ```js
|
334 | // create TLS client
|
335 | var client = forge.tls.createConnection({
|
336 | server: false,
|
337 | caStore: /* Array of PEM-formatted certs or a CA store object */,
|
338 | sessionCache: {},
|
339 | // supported cipher suites in order of preference
|
340 | cipherSuites: [
|
341 | forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,
|
342 | forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],
|
343 | virtualHost: 'example.com',
|
344 | verify: function(connection, verified, depth, certs) {
|
345 | if(depth === 0) {
|
346 | var cn = certs[0].subject.getField('CN').value;
|
347 | if(cn !== 'example.com') {
|
348 | verified = {
|
349 | alert: forge.tls.Alert.Description.bad_certificate,
|
350 | message: 'Certificate common name does not match hostname.'
|
351 | };
|
352 | }
|
353 | }
|
354 | return verified;
|
355 | },
|
356 | connected: function(connection) {
|
357 | console.log('connected');
|
358 | // send message to server
|
359 | connection.prepare(forge.util.encodeUtf8('Hi server!'));
|
360 | /* NOTE: experimental, start heartbeat retransmission timer
|
361 | myHeartbeatTimer = setInterval(function() {
|
362 | connection.prepareHeartbeatRequest(forge.util.createBuffer('1234'));
|
363 | }, 5*60*1000);*/
|
364 | },
|
365 | /* provide a client-side cert if you want
|
366 | getCertificate: function(connection, hint) {
|
367 | return myClientCertificate;
|
368 | },
|
369 | /* the private key for the client-side cert if provided */
|
370 | getPrivateKey: function(connection, cert) {
|
371 | return myClientPrivateKey;
|
372 | },
|
373 | tlsDataReady: function(connection) {
|
374 | // TLS data (encrypted) is ready to be sent to the server
|
375 | sendToServerSomehow(connection.tlsData.getBytes());
|
376 | // if you were communicating with the server below, you'd do:
|
377 | // server.process(connection.tlsData.getBytes());
|
378 | },
|
379 | dataReady: function(connection) {
|
380 | // clear data from the server is ready
|
381 | console.log('the server sent: ' +
|
382 | forge.util.decodeUtf8(connection.data.getBytes()));
|
383 | // close connection
|
384 | connection.close();
|
385 | },
|
386 | /* NOTE: experimental
|
387 | heartbeatReceived: function(connection, payload) {
|
388 | // restart retransmission timer, look at payload
|
389 | clearInterval(myHeartbeatTimer);
|
390 | myHeartbeatTimer = setInterval(function() {
|
391 | connection.prepareHeartbeatRequest(forge.util.createBuffer('1234'));
|
392 | }, 5*60*1000);
|
393 | payload.getBytes();
|
394 | },*/
|
395 | closed: function(connection) {
|
396 | console.log('disconnected');
|
397 | },
|
398 | error: function(connection, error) {
|
399 | console.log('uh oh', error);
|
400 | }
|
401 | });
|
402 |
|
403 | // start the handshake process
|
404 | client.handshake();
|
405 |
|
406 | // when encrypted TLS data is received from the server, process it
|
407 | client.process(encryptedBytesFromServer);
|
408 |
|
409 | // create TLS server
|
410 | var server = forge.tls.createConnection({
|
411 | server: true,
|
412 | caStore: /* Array of PEM-formatted certs or a CA store object */,
|
413 | sessionCache: {},
|
414 | // supported cipher suites in order of preference
|
415 | cipherSuites: [
|
416 | forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,
|
417 | forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],
|
418 | // require a client-side certificate if you want
|
419 | verifyClient: true,
|
420 | verify: function(connection, verified, depth, certs) {
|
421 | if(depth === 0) {
|
422 | var cn = certs[0].subject.getField('CN').value;
|
423 | if(cn !== 'the-client') {
|
424 | verified = {
|
425 | alert: forge.tls.Alert.Description.bad_certificate,
|
426 | message: 'Certificate common name does not match expected client.'
|
427 | };
|
428 | }
|
429 | }
|
430 | return verified;
|
431 | },
|
432 | connected: function(connection) {
|
433 | console.log('connected');
|
434 | // send message to client
|
435 | connection.prepare(forge.util.encodeUtf8('Hi client!'));
|
436 | /* NOTE: experimental, start heartbeat retransmission timer
|
437 | myHeartbeatTimer = setInterval(function() {
|
438 | connection.prepareHeartbeatRequest(forge.util.createBuffer('1234'));
|
439 | }, 5*60*1000);*/
|
440 | },
|
441 | getCertificate: function(connection, hint) {
|
442 | return myServerCertificate;
|
443 | },
|
444 | getPrivateKey: function(connection, cert) {
|
445 | return myServerPrivateKey;
|
446 | },
|
447 | tlsDataReady: function(connection) {
|
448 | // TLS data (encrypted) is ready to be sent to the client
|
449 | sendToClientSomehow(connection.tlsData.getBytes());
|
450 | // if you were communicating with the client above you'd do:
|
451 | // client.process(connection.tlsData.getBytes());
|
452 | },
|
453 | dataReady: function(connection) {
|
454 | // clear data from the client is ready
|
455 | console.log('the client sent: ' +
|
456 | forge.util.decodeUtf8(connection.data.getBytes()));
|
457 | // close connection
|
458 | connection.close();
|
459 | },
|
460 | /* NOTE: experimental
|
461 | heartbeatReceived: function(connection, payload) {
|
462 | // restart retransmission timer, look at payload
|
463 | clearInterval(myHeartbeatTimer);
|
464 | myHeartbeatTimer = setInterval(function() {
|
465 | connection.prepareHeartbeatRequest(forge.util.createBuffer('1234'));
|
466 | }, 5*60*1000);
|
467 | payload.getBytes();
|
468 | },*/
|
469 | closed: function(connection) {
|
470 | console.log('disconnected');
|
471 | },
|
472 | error: function(connection, error) {
|
473 | console.log('uh oh', error);
|
474 | }
|
475 | });
|
476 |
|
477 | // when encrypted TLS data is received from the client, process it
|
478 | server.process(encryptedBytesFromClient);
|
479 | ```
|
480 |
|
481 | Connect to a TLS server using node's net.Socket:
|
482 |
|
483 | ```js
|
484 | var socket = new net.Socket();
|
485 |
|
486 | var client = forge.tls.createConnection({
|
487 | server: false,
|
488 | verify: function(connection, verified, depth, certs) {
|
489 | // skip verification for testing
|
490 | console.log('[tls] server certificate verified');
|
491 | return true;
|
492 | },
|
493 | connected: function(connection) {
|
494 | console.log('[tls] connected');
|
495 | // prepare some data to send (note that the string is interpreted as
|
496 | // 'binary' encoded, which works for HTTP which only uses ASCII, use
|
497 | // forge.util.encodeUtf8(str) otherwise
|
498 | client.prepare('GET / HTTP/1.0\r\n\r\n');
|
499 | },
|
500 | tlsDataReady: function(connection) {
|
501 | // encrypted data is ready to be sent to the server
|
502 | var data = connection.tlsData.getBytes();
|
503 | socket.write(data, 'binary'); // encoding should be 'binary'
|
504 | },
|
505 | dataReady: function(connection) {
|
506 | // clear data from the server is ready
|
507 | var data = connection.data.getBytes();
|
508 | console.log('[tls] data received from the server: ' + data);
|
509 | },
|
510 | closed: function() {
|
511 | console.log('[tls] disconnected');
|
512 | },
|
513 | error: function(connection, error) {
|
514 | console.log('[tls] error', error);
|
515 | }
|
516 | });
|
517 |
|
518 | socket.on('connect', function() {
|
519 | console.log('[socket] connected');
|
520 | client.handshake();
|
521 | });
|
522 | socket.on('data', function(data) {
|
523 | client.process(data.toString('binary')); // encoding should be 'binary'
|
524 | });
|
525 | socket.on('end', function() {
|
526 | console.log('[socket] disconnected');
|
527 | });
|
528 |
|
529 | // connect to google.com
|
530 | socket.connect(443, 'google.com');
|
531 |
|
532 | // or connect to gmail's imap server (but don't send the HTTP header above)
|
533 | //socket.connect(993, 'imap.gmail.com');
|
534 | ```
|
535 |
|
536 | <a name="http" />
|
537 |
|
538 | ### HTTP
|
539 |
|
540 | Provides a native [JavaScript][] mini-implementation of an http client that
|
541 | uses pooled sockets.
|
542 |
|
543 | __Examples__
|
544 |
|
545 | ```js
|
546 | // create an HTTP GET request
|
547 | var request = forge.http.createRequest({method: 'GET', path: url.path});
|
548 |
|
549 | // send the request somewhere
|
550 | sendSomehow(request.toString());
|
551 |
|
552 | // receive response
|
553 | var buffer = forge.util.createBuffer();
|
554 | var response = forge.http.createResponse();
|
555 | var someAsyncDataHandler = function(bytes) {
|
556 | if(!response.bodyReceived) {
|
557 | buffer.putBytes(bytes);
|
558 | if(!response.headerReceived) {
|
559 | if(response.readHeader(buffer)) {
|
560 | console.log('HTTP response header: ' + response.toString());
|
561 | }
|
562 | }
|
563 | if(response.headerReceived && !response.bodyReceived) {
|
564 | if(response.readBody(buffer)) {
|
565 | console.log('HTTP response body: ' + response.body);
|
566 | }
|
567 | }
|
568 | }
|
569 | };
|
570 | ```
|
571 |
|
572 | <a name="ssh" />
|
573 |
|
574 | ### SSH
|
575 |
|
576 | Provides some SSH utility functions.
|
577 |
|
578 | __Examples__
|
579 |
|
580 | ```js
|
581 | // encodes (and optionally encrypts) a private RSA key as a Putty PPK file
|
582 | forge.ssh.privateKeyToPutty(privateKey, passphrase, comment);
|
583 |
|
584 | // encodes a public RSA key as an OpenSSH file
|
585 | forge.ssh.publicKeyToOpenSSH(key, comment);
|
586 |
|
587 | // encodes a private RSA key as an OpenSSH file
|
588 | forge.ssh.privateKeyToOpenSSH(privateKey, passphrase);
|
589 |
|
590 | // gets the SSH public key fingerprint in a byte buffer
|
591 | forge.ssh.getPublicKeyFingerprint(key);
|
592 |
|
593 | // gets a hex-encoded, colon-delimited SSH public key fingerprint
|
594 | forge.ssh.getPublicKeyFingerprint(key, {encoding: 'hex', delimiter: ':'});
|
595 | ```
|
596 |
|
597 | <a name="xhr" />
|
598 |
|
599 | ### XHR
|
600 |
|
601 | Provides an XmlHttpRequest implementation using forge.http as a backend.
|
602 |
|
603 | __Examples__
|
604 |
|
605 | ```js
|
606 | // TODO
|
607 | ```
|
608 |
|
609 | <a name="socket" />
|
610 |
|
611 | ### Sockets
|
612 |
|
613 | Provides an interface to create and use raw sockets provided via Flash.
|
614 |
|
615 | __Examples__
|
616 |
|
617 | ```js
|
618 | // TODO
|
619 | ```
|
620 |
|
621 | Ciphers
|
622 | -------
|
623 |
|
624 | <a name="cipher" />
|
625 |
|
626 | ### CIPHER
|
627 |
|
628 | Provides a basic API for block encryption and decryption. There is built-in
|
629 | support for the ciphers: [AES][], [3DES][], and [DES][], and for the modes
|
630 | of operation: [ECB][], [CBC][], [CFB][], [OFB][], [CTR][], and [GCM][].
|
631 |
|
632 | These algorithms are currently supported:
|
633 |
|
634 | * AES-ECB
|
635 | * AES-CBC
|
636 | * AES-CFB
|
637 | * AES-OFB
|
638 | * AES-CTR
|
639 | * AES-GCM
|
640 | * 3DES-ECB
|
641 | * 3DES-CBC
|
642 | * DES-ECB
|
643 | * DES-CBC
|
644 |
|
645 | When using an [AES][] algorithm, the key size will determine whether
|
646 | AES-128, AES-192, or AES-256 is used (all are supported). When a [DES][]
|
647 | algorithm is used, the key size will determine whether [3DES][] or regular
|
648 | [DES][] is used. Use a [3DES][] algorithm to enforce Triple-DES.
|
649 |
|
650 | __Examples__
|
651 |
|
652 | ```js
|
653 | // generate a random key and IV
|
654 | // Note: a key size of 16 bytes will use AES-128, 24 => AES-192, 32 => AES-256
|
655 | var key = forge.random.getBytesSync(16);
|
656 | var iv = forge.random.getBytesSync(16);
|
657 |
|
658 | /* alternatively, generate a password-based 16-byte key
|
659 | var salt = forge.random.getBytesSync(128);
|
660 | var key = forge.pkcs5.pbkdf2('password', salt, numIterations, 16);
|
661 | */
|
662 |
|
663 | // encrypt some bytes using CBC mode
|
664 | // (other modes include: ECB, CFB, OFB, CTR, and GCM)
|
665 | // Note: CBC and ECB modes use PKCS#7 padding as default
|
666 | var cipher = forge.cipher.createCipher('AES-CBC', key);
|
667 | cipher.start({iv: iv});
|
668 | cipher.update(forge.util.createBuffer(someBytes));
|
669 | cipher.finish();
|
670 | var encrypted = cipher.output;
|
671 | // outputs encrypted hex
|
672 | console.log(encrypted.toHex());
|
673 |
|
674 | // decrypt some bytes using CBC mode
|
675 | // (other modes include: CFB, OFB, CTR, and GCM)
|
676 | var decipher = forge.cipher.createDecipher('AES-CBC', key);
|
677 | decipher.start({iv: iv});
|
678 | decipher.update(encrypted);
|
679 | var result = decipher.finish(); // check 'result' for true/false
|
680 | // outputs decrypted hex
|
681 | console.log(decipher.output.toHex());
|
682 |
|
683 | // decrypt bytes using CBC mode and streaming
|
684 | // Performance can suffer for large multi-MB inputs due to buffer
|
685 | // manipulations. Stream processing in chunks can offer significant
|
686 | // improvement. CPU intensive update() calls could also be performed with
|
687 | // setImmediate/setTimeout to avoid blocking the main browser UI thread (not
|
688 | // shown here). Optimal block size depends on the JavaScript VM and other
|
689 | // factors. Encryption can use a simple technique for increased performance.
|
690 | var encryptedBytes = encrypted.bytes();
|
691 | var decipher = forge.cipher.createDecipher('AES-CBC', key);
|
692 | decipher.start({iv: iv});
|
693 | var length = encryptedBytes.length;
|
694 | var chunkSize = 1024 * 64;
|
695 | var index = 0;
|
696 | var decrypted = '';
|
697 | do {
|
698 | decrypted += decipher.output.getBytes();
|
699 | var buf = forge.util.createBuffer(encryptedBytes.substr(index, chunkSize));
|
700 | decipher.update(buf);
|
701 | index += chunkSize;
|
702 | } while(index < length);
|
703 | var result = decipher.finish();
|
704 | assert(result);
|
705 | decrypted += decipher.output.getBytes();
|
706 | console.log(forge.util.bytesToHex(decrypted));
|
707 |
|
708 | // encrypt some bytes using GCM mode
|
709 | var cipher = forge.cipher.createCipher('AES-GCM', key);
|
710 | cipher.start({
|
711 | iv: iv, // should be a 12-byte binary-encoded string or byte buffer
|
712 | additionalData: 'binary-encoded string', // optional
|
713 | tagLength: 128 // optional, defaults to 128 bits
|
714 | });
|
715 | cipher.update(forge.util.createBuffer(someBytes));
|
716 | cipher.finish();
|
717 | var encrypted = cipher.output;
|
718 | var tag = cipher.mode.tag;
|
719 | // outputs encrypted hex
|
720 | console.log(encrypted.toHex());
|
721 | // outputs authentication tag
|
722 | console.log(tag.toHex());
|
723 |
|
724 | // decrypt some bytes using GCM mode
|
725 | var decipher = forge.cipher.createDecipher('AES-GCM', key);
|
726 | decipher.start({
|
727 | iv: iv,
|
728 | additionalData: 'binary-encoded string', // optional
|
729 | tagLength: 128, // optional, defaults to 128 bits
|
730 | tag: tag // authentication tag from encryption
|
731 | });
|
732 | decipher.update(encrypted);
|
733 | var pass = decipher.finish();
|
734 | // pass is false if there was a failure (eg: authentication tag didn't match)
|
735 | if(pass) {
|
736 | // outputs decrypted hex
|
737 | console.log(decipher.output.toHex());
|
738 | }
|
739 | ```
|
740 |
|
741 | Using forge in Node.js to match openssl's "enc" command line tool (**Note**: OpenSSL "enc" uses a non-standard file format with a custom key derivation function and a fixed iteration count of 1, which some consider less secure than alternatives such as [OpenPGP](https://tools.ietf.org/html/rfc4880)/[GnuPG](https://www.gnupg.org/)):
|
742 |
|
743 | ```js
|
744 | var forge = require('node-forge');
|
745 | var fs = require('fs');
|
746 |
|
747 | // openssl enc -des3 -in input.txt -out input.enc
|
748 | function encrypt(password) {
|
749 | var input = fs.readFileSync('input.txt', {encoding: 'binary'});
|
750 |
|
751 | // 3DES key and IV sizes
|
752 | var keySize = 24;
|
753 | var ivSize = 8;
|
754 |
|
755 | // get derived bytes
|
756 | // Notes:
|
757 | // 1. If using an alternative hash (eg: "-md sha1") pass
|
758 | // "forge.md.sha1.create()" as the final parameter.
|
759 | // 2. If using "-nosalt", set salt to null.
|
760 | var salt = forge.random.getBytesSync(8);
|
761 | // var md = forge.md.sha1.create(); // "-md sha1"
|
762 | var derivedBytes = forge.pbe.opensslDeriveBytes(
|
763 | password, salt, keySize + ivSize/*, md*/);
|
764 | var buffer = forge.util.createBuffer(derivedBytes);
|
765 | var key = buffer.getBytes(keySize);
|
766 | var iv = buffer.getBytes(ivSize);
|
767 |
|
768 | var cipher = forge.cipher.createCipher('3DES-CBC', key);
|
769 | cipher.start({iv: iv});
|
770 | cipher.update(forge.util.createBuffer(input, 'binary'));
|
771 | cipher.finish();
|
772 |
|
773 | var output = forge.util.createBuffer();
|
774 |
|
775 | // if using a salt, prepend this to the output:
|
776 | if(salt !== null) {
|
777 | output.putBytes('Salted__'); // (add to match openssl tool output)
|
778 | output.putBytes(salt);
|
779 | }
|
780 | output.putBuffer(cipher.output);
|
781 |
|
782 | fs.writeFileSync('input.enc', output.getBytes(), {encoding: 'binary'});
|
783 | }
|
784 |
|
785 | // openssl enc -d -des3 -in input.enc -out input.dec.txt
|
786 | function decrypt(password) {
|
787 | var input = fs.readFileSync('input.enc', {encoding: 'binary'});
|
788 |
|
789 | // parse salt from input
|
790 | input = forge.util.createBuffer(input, 'binary');
|
791 | // skip "Salted__" (if known to be present)
|
792 | input.getBytes('Salted__'.length);
|
793 | // read 8-byte salt
|
794 | var salt = input.getBytes(8);
|
795 |
|
796 | // Note: if using "-nosalt", skip above parsing and use
|
797 | // var salt = null;
|
798 |
|
799 | // 3DES key and IV sizes
|
800 | var keySize = 24;
|
801 | var ivSize = 8;
|
802 |
|
803 | var derivedBytes = forge.pbe.opensslDeriveBytes(
|
804 | password, salt, keySize + ivSize);
|
805 | var buffer = forge.util.createBuffer(derivedBytes);
|
806 | var key = buffer.getBytes(keySize);
|
807 | var iv = buffer.getBytes(ivSize);
|
808 |
|
809 | var decipher = forge.cipher.createDecipher('3DES-CBC', key);
|
810 | decipher.start({iv: iv});
|
811 | decipher.update(input);
|
812 | var result = decipher.finish(); // check 'result' for true/false
|
813 |
|
814 | fs.writeFileSync(
|
815 | 'input.dec.txt', decipher.output.getBytes(), {encoding: 'binary'});
|
816 | }
|
817 | ```
|
818 |
|
819 | <a name="aes" />
|
820 |
|
821 | ### AES
|
822 |
|
823 | Provides [AES][] encryption and decryption in [CBC][], [CFB][], [OFB][],
|
824 | [CTR][], and [GCM][] modes. See [CIPHER](#cipher) for examples.
|
825 |
|
826 | <a name="des" />
|
827 |
|
828 | ### DES
|
829 |
|
830 | Provides [3DES][] and [DES][] encryption and decryption in [ECB][] and
|
831 | [CBC][] modes. See [CIPHER](#cipher) for examples.
|
832 |
|
833 | <a name="rc2" />
|
834 |
|
835 | ### RC2
|
836 |
|
837 | __Examples__
|
838 |
|
839 | ```js
|
840 | // generate a random key and IV
|
841 | var key = forge.random.getBytesSync(16);
|
842 | var iv = forge.random.getBytesSync(8);
|
843 |
|
844 | // encrypt some bytes
|
845 | var cipher = forge.rc2.createEncryptionCipher(key);
|
846 | cipher.start(iv);
|
847 | cipher.update(forge.util.createBuffer(someBytes));
|
848 | cipher.finish();
|
849 | var encrypted = cipher.output;
|
850 | // outputs encrypted hex
|
851 | console.log(encrypted.toHex());
|
852 |
|
853 | // decrypt some bytes
|
854 | var cipher = forge.rc2.createDecryptionCipher(key);
|
855 | cipher.start(iv);
|
856 | cipher.update(encrypted);
|
857 | cipher.finish();
|
858 | // outputs decrypted hex
|
859 | console.log(cipher.output.toHex());
|
860 | ```
|
861 |
|
862 | PKI
|
863 | ---
|
864 |
|
865 | Provides [X.509][] certificate support, ED25519 key generation and
|
866 | signing/verifying, and RSA public and private key encoding, decoding,
|
867 | encryption/decryption, and signing/verifying.
|
868 |
|
869 | <a name="ed25519" />
|
870 |
|
871 | ### ED25519
|
872 |
|
873 | Special thanks to [TweetNaCl.js][] for providing the bulk of the implementation.
|
874 |
|
875 | __Examples__
|
876 |
|
877 | ```js
|
878 | var ed25519 = forge.pki.ed25519;
|
879 |
|
880 | // generate a random ED25519 keypair
|
881 | var keypair = ed25519.generateKeyPair();
|
882 | // `keypair.publicKey` is a node.js Buffer or Uint8Array
|
883 | // `keypair.privateKey` is a node.js Buffer or Uint8Array
|
884 |
|
885 | // generate a random ED25519 keypair based on a random 32-byte seed
|
886 | var seed = forge.random.getBytesSync(32);
|
887 | var keypair = ed25519.generateKeyPair({seed: seed});
|
888 |
|
889 | // generate a random ED25519 keypair based on a "password" 32-byte seed
|
890 | var password = 'Mai9ohgh6ahxee0jutheew0pungoozil';
|
891 | var seed = new forge.util.ByteBuffer(password, 'utf8');
|
892 | var keypair = ed25519.generateKeyPair({seed: seed});
|
893 |
|
894 | // sign a UTF-8 message
|
895 | var signature = ED25519.sign({
|
896 | message: 'test',
|
897 | // also accepts `binary` if you want to pass a binary string
|
898 | encoding: 'utf8',
|
899 | // node.js Buffer, Uint8Array, forge ByteBuffer, binary string
|
900 | privateKey: privateKey
|
901 | });
|
902 | // `signature` is a node.js Buffer or Uint8Array
|
903 |
|
904 | // sign a message passed as a buffer
|
905 | var signature = ED25519.sign({
|
906 | // also accepts a forge ByteBuffer or Uint8Array
|
907 | message: Buffer.from('test', 'utf8'),
|
908 | privateKey: privateKey
|
909 | });
|
910 |
|
911 | // sign a message digest (shorter "message" == better performance)
|
912 | var md = forge.md.sha256.create();
|
913 | md.update('test', 'utf8');
|
914 | var signature = ED25519.sign({
|
915 | md: md,
|
916 | privateKey: privateKey
|
917 | });
|
918 |
|
919 | // verify a signature on a UTF-8 message
|
920 | var verified = ED25519.verify({
|
921 | message: 'test',
|
922 | encoding: 'utf8',
|
923 | // node.js Buffer, Uint8Array, forge ByteBuffer, or binary string
|
924 | signature: signature,
|
925 | // node.js Buffer, Uint8Array, forge ByteBuffer, or binary string
|
926 | publicKey: publicKey
|
927 | });
|
928 | // `verified` is true/false
|
929 |
|
930 | // sign a message passed as a buffer
|
931 | var verified = ED25519.verify({
|
932 | // also accepts a forge ByteBuffer or Uint8Array
|
933 | message: Buffer.from('test', 'utf8'),
|
934 | // node.js Buffer, Uint8Array, forge ByteBuffer, or binary string
|
935 | signature: signature,
|
936 | // node.js Buffer, Uint8Array, forge ByteBuffer, or binary string
|
937 | publicKey: publicKey
|
938 | });
|
939 |
|
940 | // verify a signature on a message digest
|
941 | var md = forge.md.sha256.create();
|
942 | md.update('test', 'utf8');
|
943 | var verified = ED25519.verify({
|
944 | md: md,
|
945 | // node.js Buffer, Uint8Array, forge ByteBuffer, or binary string
|
946 | signature: signature,
|
947 | // node.js Buffer, Uint8Array, forge ByteBuffer, or binary string
|
948 | publicKey: publicKey
|
949 | });
|
950 | ```
|
951 |
|
952 | <a name="rsa" />
|
953 |
|
954 | ### RSA
|
955 |
|
956 | __Examples__
|
957 |
|
958 | ```js
|
959 | var rsa = forge.pki.rsa;
|
960 |
|
961 | // generate an RSA key pair synchronously
|
962 | // *NOT RECOMMENDED*: Can be significantly slower than async and may block
|
963 | // JavaScript execution. Will use native Node.js 10.12.0+ API if possible.
|
964 | var keypair = rsa.generateKeyPair({bits: 2048, e: 0x10001});
|
965 |
|
966 | // generate an RSA key pair asynchronously (uses web workers if available)
|
967 | // use workers: -1 to run a fast core estimator to optimize # of workers
|
968 | // *RECOMMENDED*: Can be significantly faster than sync. Will use native
|
969 | // Node.js 10.12.0+ or WebCrypto API if possible.
|
970 | rsa.generateKeyPair({bits: 2048, workers: 2}, function(err, keypair) {
|
971 | // keypair.privateKey, keypair.publicKey
|
972 | });
|
973 |
|
974 | // generate an RSA key pair in steps that attempt to run for a specified period
|
975 | // of time on the main JS thread
|
976 | var state = rsa.createKeyPairGenerationState(2048, 0x10001);
|
977 | var step = function() {
|
978 | // run for 100 ms
|
979 | if(!rsa.stepKeyPairGenerationState(state, 100)) {
|
980 | setTimeout(step, 1);
|
981 | }
|
982 | else {
|
983 | // done, turn off progress indicator, use state.keys
|
984 | }
|
985 | };
|
986 | // turn on progress indicator, schedule generation to run
|
987 | setTimeout(step);
|
988 |
|
989 | // sign data with a private key and output DigestInfo DER-encoded bytes
|
990 | // (defaults to RSASSA PKCS#1 v1.5)
|
991 | var md = forge.md.sha1.create();
|
992 | md.update('sign this', 'utf8');
|
993 | var signature = privateKey.sign(md);
|
994 |
|
995 | // verify data with a public key
|
996 | // (defaults to RSASSA PKCS#1 v1.5)
|
997 | var verified = publicKey.verify(md.digest().bytes(), signature);
|
998 |
|
999 | // sign data using RSASSA-PSS where PSS uses a SHA-1 hash, a SHA-1 based
|
1000 | // masking function MGF1, and a 20 byte salt
|
1001 | var md = forge.md.sha1.create();
|
1002 | md.update('sign this', 'utf8');
|
1003 | var pss = forge.pss.create({
|
1004 | md: forge.md.sha1.create(),
|
1005 | mgf: forge.mgf.mgf1.create(forge.md.sha1.create()),
|
1006 | saltLength: 20
|
1007 | // optionally pass 'prng' with a custom PRNG implementation
|
1008 | // optionalls pass 'salt' with a forge.util.ByteBuffer w/custom salt
|
1009 | });
|
1010 | var signature = privateKey.sign(md, pss);
|
1011 |
|
1012 | // verify RSASSA-PSS signature
|
1013 | var pss = forge.pss.create({
|
1014 | md: forge.md.sha1.create(),
|
1015 | mgf: forge.mgf.mgf1.create(forge.md.sha1.create()),
|
1016 | saltLength: 20
|
1017 | // optionally pass 'prng' with a custom PRNG implementation
|
1018 | });
|
1019 | var md = forge.md.sha1.create();
|
1020 | md.update('sign this', 'utf8');
|
1021 | publicKey.verify(md.digest().getBytes(), signature, pss);
|
1022 |
|
1023 | // encrypt data with a public key (defaults to RSAES PKCS#1 v1.5)
|
1024 | var encrypted = publicKey.encrypt(bytes);
|
1025 |
|
1026 | // decrypt data with a private key (defaults to RSAES PKCS#1 v1.5)
|
1027 | var decrypted = privateKey.decrypt(encrypted);
|
1028 |
|
1029 | // encrypt data with a public key using RSAES PKCS#1 v1.5
|
1030 | var encrypted = publicKey.encrypt(bytes, 'RSAES-PKCS1-V1_5');
|
1031 |
|
1032 | // decrypt data with a private key using RSAES PKCS#1 v1.5
|
1033 | var decrypted = privateKey.decrypt(encrypted, 'RSAES-PKCS1-V1_5');
|
1034 |
|
1035 | // encrypt data with a public key using RSAES-OAEP
|
1036 | var encrypted = publicKey.encrypt(bytes, 'RSA-OAEP');
|
1037 |
|
1038 | // decrypt data with a private key using RSAES-OAEP
|
1039 | var decrypted = privateKey.decrypt(encrypted, 'RSA-OAEP');
|
1040 |
|
1041 | // encrypt data with a public key using RSAES-OAEP/SHA-256
|
1042 | var encrypted = publicKey.encrypt(bytes, 'RSA-OAEP', {
|
1043 | md: forge.md.sha256.create()
|
1044 | });
|
1045 |
|
1046 | // decrypt data with a private key using RSAES-OAEP/SHA-256
|
1047 | var decrypted = privateKey.decrypt(encrypted, 'RSA-OAEP', {
|
1048 | md: forge.md.sha256.create()
|
1049 | });
|
1050 |
|
1051 | // encrypt data with a public key using RSAES-OAEP/SHA-256/MGF1-SHA-1
|
1052 | // compatible with Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding
|
1053 | var encrypted = publicKey.encrypt(bytes, 'RSA-OAEP', {
|
1054 | md: forge.md.sha256.create(),
|
1055 | mgf1: {
|
1056 | md: forge.md.sha1.create()
|
1057 | }
|
1058 | });
|
1059 |
|
1060 | // decrypt data with a private key using RSAES-OAEP/SHA-256/MGF1-SHA-1
|
1061 | // compatible with Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding
|
1062 | var decrypted = privateKey.decrypt(encrypted, 'RSA-OAEP', {
|
1063 | md: forge.md.sha256.create(),
|
1064 | mgf1: {
|
1065 | md: forge.md.sha1.create()
|
1066 | }
|
1067 | });
|
1068 |
|
1069 | ```
|
1070 |
|
1071 | <a name="rsakem" />
|
1072 |
|
1073 | ### RSA-KEM
|
1074 |
|
1075 | __Examples__
|
1076 |
|
1077 | ```js
|
1078 | // generate an RSA key pair asynchronously (uses web workers if available)
|
1079 | // use workers: -1 to run a fast core estimator to optimize # of workers
|
1080 | forge.rsa.generateKeyPair({bits: 2048, workers: -1}, function(err, keypair) {
|
1081 | // keypair.privateKey, keypair.publicKey
|
1082 | });
|
1083 |
|
1084 | // generate and encapsulate a 16-byte secret key
|
1085 | var kdf1 = new forge.kem.kdf1(forge.md.sha1.create());
|
1086 | var kem = forge.kem.rsa.create(kdf1);
|
1087 | var result = kem.encrypt(keypair.publicKey, 16);
|
1088 | // result has 'encapsulation' and 'key'
|
1089 |
|
1090 | // encrypt some bytes
|
1091 | var iv = forge.random.getBytesSync(12);
|
1092 | var someBytes = 'hello world!';
|
1093 | var cipher = forge.cipher.createCipher('AES-GCM', result.key);
|
1094 | cipher.start({iv: iv});
|
1095 | cipher.update(forge.util.createBuffer(someBytes));
|
1096 | cipher.finish();
|
1097 | var encrypted = cipher.output.getBytes();
|
1098 | var tag = cipher.mode.tag.getBytes();
|
1099 |
|
1100 | // send 'encrypted', 'iv', 'tag', and result.encapsulation to recipient
|
1101 |
|
1102 | // decrypt encapsulated 16-byte secret key
|
1103 | var kdf1 = new forge.kem.kdf1(forge.md.sha1.create());
|
1104 | var kem = forge.kem.rsa.create(kdf1);
|
1105 | var key = kem.decrypt(keypair.privateKey, result.encapsulation, 16);
|
1106 |
|
1107 | // decrypt some bytes
|
1108 | var decipher = forge.cipher.createDecipher('AES-GCM', key);
|
1109 | decipher.start({iv: iv, tag: tag});
|
1110 | decipher.update(forge.util.createBuffer(encrypted));
|
1111 | var pass = decipher.finish();
|
1112 | // pass is false if there was a failure (eg: authentication tag didn't match)
|
1113 | if(pass) {
|
1114 | // outputs 'hello world!'
|
1115 | console.log(decipher.output.getBytes());
|
1116 | }
|
1117 |
|
1118 | ```
|
1119 |
|
1120 | <a name="x509" />
|
1121 |
|
1122 | ### X.509
|
1123 |
|
1124 | __Examples__
|
1125 |
|
1126 | ```js
|
1127 | var pki = forge.pki;
|
1128 |
|
1129 | // convert a PEM-formatted public key to a Forge public key
|
1130 | var publicKey = pki.publicKeyFromPem(pem);
|
1131 |
|
1132 | // convert a Forge public key to PEM-format
|
1133 | var pem = pki.publicKeyToPem(publicKey);
|
1134 |
|
1135 | // convert an ASN.1 SubjectPublicKeyInfo to a Forge public key
|
1136 | var publicKey = pki.publicKeyFromAsn1(subjectPublicKeyInfo);
|
1137 |
|
1138 | // convert a Forge public key to an ASN.1 SubjectPublicKeyInfo
|
1139 | var subjectPublicKeyInfo = pki.publicKeyToAsn1(publicKey);
|
1140 |
|
1141 | // gets a SHA-1 RSAPublicKey fingerprint a byte buffer
|
1142 | pki.getPublicKeyFingerprint(key);
|
1143 |
|
1144 | // gets a SHA-1 SubjectPublicKeyInfo fingerprint a byte buffer
|
1145 | pki.getPublicKeyFingerprint(key, {type: 'SubjectPublicKeyInfo'});
|
1146 |
|
1147 | // gets a hex-encoded, colon-delimited SHA-1 RSAPublicKey public key fingerprint
|
1148 | pki.getPublicKeyFingerprint(key, {encoding: 'hex', delimiter: ':'});
|
1149 |
|
1150 | // gets a hex-encoded, colon-delimited SHA-1 SubjectPublicKeyInfo public key fingerprint
|
1151 | pki.getPublicKeyFingerprint(key, {
|
1152 | type: 'SubjectPublicKeyInfo',
|
1153 | encoding: 'hex',
|
1154 | delimiter: ':'
|
1155 | });
|
1156 |
|
1157 | // gets a hex-encoded, colon-delimited MD5 RSAPublicKey public key fingerprint
|
1158 | pki.getPublicKeyFingerprint(key, {
|
1159 | md: forge.md.md5.create(),
|
1160 | encoding: 'hex',
|
1161 | delimiter: ':'
|
1162 | });
|
1163 |
|
1164 | // creates a CA store
|
1165 | var caStore = pki.createCaStore([/* PEM-encoded cert */, ...]);
|
1166 |
|
1167 | // add a certificate to the CA store
|
1168 | caStore.addCertificate(certObjectOrPemString);
|
1169 |
|
1170 | // gets the issuer (its certificate) for the given certificate
|
1171 | var issuerCert = caStore.getIssuer(subjectCert);
|
1172 |
|
1173 | // verifies a certificate chain against a CA store
|
1174 | pki.verifyCertificateChain(caStore, chain, customVerifyCallback);
|
1175 |
|
1176 | // signs a certificate using the given private key
|
1177 | cert.sign(privateKey);
|
1178 |
|
1179 | // signs a certificate using SHA-256 instead of SHA-1
|
1180 | cert.sign(privateKey, forge.md.sha256.create());
|
1181 |
|
1182 | // verifies an issued certificate using the certificates public key
|
1183 | var verified = issuer.verify(issued);
|
1184 |
|
1185 | // generate a keypair and create an X.509v3 certificate
|
1186 | var keys = pki.rsa.generateKeyPair(2048);
|
1187 | var cert = pki.createCertificate();
|
1188 | cert.publicKey = keys.publicKey;
|
1189 | // alternatively set public key from a csr
|
1190 | //cert.publicKey = csr.publicKey;
|
1191 | // NOTE: serialNumber is the hex encoded value of an ASN.1 INTEGER.
|
1192 | // Conforming CAs should ensure serialNumber is:
|
1193 | // - no more than 20 octets
|
1194 | // - non-negative (prefix a '00' if your value starts with a '1' bit)
|
1195 | cert.serialNumber = '01';
|
1196 | cert.validity.notBefore = new Date();
|
1197 | cert.validity.notAfter = new Date();
|
1198 | cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
|
1199 | var attrs = [{
|
1200 | name: 'commonName',
|
1201 | value: 'example.org'
|
1202 | }, {
|
1203 | name: 'countryName',
|
1204 | value: 'US'
|
1205 | }, {
|
1206 | shortName: 'ST',
|
1207 | value: 'Virginia'
|
1208 | }, {
|
1209 | name: 'localityName',
|
1210 | value: 'Blacksburg'
|
1211 | }, {
|
1212 | name: 'organizationName',
|
1213 | value: 'Test'
|
1214 | }, {
|
1215 | shortName: 'OU',
|
1216 | value: 'Test'
|
1217 | }];
|
1218 | cert.setSubject(attrs);
|
1219 | // alternatively set subject from a csr
|
1220 | //cert.setSubject(csr.subject.attributes);
|
1221 | cert.setIssuer(attrs);
|
1222 | cert.setExtensions([{
|
1223 | name: 'basicConstraints',
|
1224 | cA: true
|
1225 | }, {
|
1226 | name: 'keyUsage',
|
1227 | keyCertSign: true,
|
1228 | digitalSignature: true,
|
1229 | nonRepudiation: true,
|
1230 | keyEncipherment: true,
|
1231 | dataEncipherment: true
|
1232 | }, {
|
1233 | name: 'extKeyUsage',
|
1234 | serverAuth: true,
|
1235 | clientAuth: true,
|
1236 | codeSigning: true,
|
1237 | emailProtection: true,
|
1238 | timeStamping: true
|
1239 | }, {
|
1240 | name: 'nsCertType',
|
1241 | client: true,
|
1242 | server: true,
|
1243 | email: true,
|
1244 | objsign: true,
|
1245 | sslCA: true,
|
1246 | emailCA: true,
|
1247 | objCA: true
|
1248 | }, {
|
1249 | name: 'subjectAltName',
|
1250 | altNames: [{
|
1251 | type: 6, // URI
|
1252 | value: 'http://example.org/webid#me'
|
1253 | }, {
|
1254 | type: 7, // IP
|
1255 | ip: '127.0.0.1'
|
1256 | }]
|
1257 | }, {
|
1258 | name: 'subjectKeyIdentifier'
|
1259 | }]);
|
1260 | /* alternatively set extensions from a csr
|
1261 | var extensions = csr.getAttribute({name: 'extensionRequest'}).extensions;
|
1262 | // optionally add more extensions
|
1263 | extensions.push.apply(extensions, [{
|
1264 | name: 'basicConstraints',
|
1265 | cA: true
|
1266 | }, {
|
1267 | name: 'keyUsage',
|
1268 | keyCertSign: true,
|
1269 | digitalSignature: true,
|
1270 | nonRepudiation: true,
|
1271 | keyEncipherment: true,
|
1272 | dataEncipherment: true
|
1273 | }]);
|
1274 | cert.setExtensions(extensions);
|
1275 | */
|
1276 | // self-sign certificate
|
1277 | cert.sign(keys.privateKey);
|
1278 |
|
1279 | // convert a Forge certificate to PEM
|
1280 | var pem = pki.certificateToPem(cert);
|
1281 |
|
1282 | // convert a Forge certificate from PEM
|
1283 | var cert = pki.certificateFromPem(pem);
|
1284 |
|
1285 | // convert an ASN.1 X.509x3 object to a Forge certificate
|
1286 | var cert = pki.certificateFromAsn1(obj);
|
1287 |
|
1288 | // convert a Forge certificate to an ASN.1 X.509v3 object
|
1289 | var asn1Cert = pki.certificateToAsn1(cert);
|
1290 | ```
|
1291 |
|
1292 | <a name="pkcs5" />
|
1293 |
|
1294 | ### PKCS#5
|
1295 |
|
1296 | Provides the password-based key-derivation function from [PKCS#5][].
|
1297 |
|
1298 | __Examples__
|
1299 |
|
1300 | ```js
|
1301 | // generate a password-based 16-byte key
|
1302 | // note an optional message digest can be passed as the final parameter
|
1303 | var salt = forge.random.getBytesSync(128);
|
1304 | var derivedKey = forge.pkcs5.pbkdf2('password', salt, numIterations, 16);
|
1305 |
|
1306 | // generate key asynchronously
|
1307 | // note an optional message digest can be passed before the callback
|
1308 | forge.pkcs5.pbkdf2('password', salt, numIterations, 16, function(err, derivedKey) {
|
1309 | // do something w/derivedKey
|
1310 | });
|
1311 | ```
|
1312 |
|
1313 | <a name="pkcs7" />
|
1314 |
|
1315 | ### PKCS#7
|
1316 |
|
1317 | Provides cryptographically protected messages from [PKCS#7][].
|
1318 |
|
1319 | __Examples__
|
1320 |
|
1321 | ```js
|
1322 | // convert a message from PEM
|
1323 | var p7 = forge.pkcs7.messageFromPem(pem);
|
1324 | // look at p7.recipients
|
1325 |
|
1326 | // find a recipient by the issuer of a certificate
|
1327 | var recipient = p7.findRecipient(cert);
|
1328 |
|
1329 | // decrypt
|
1330 | p7.decrypt(p7.recipients[0], privateKey);
|
1331 |
|
1332 | // create a p7 enveloped message
|
1333 | var p7 = forge.pkcs7.createEnvelopedData();
|
1334 |
|
1335 | // add a recipient
|
1336 | var cert = forge.pki.certificateFromPem(certPem);
|
1337 | p7.addRecipient(cert);
|
1338 |
|
1339 | // set content
|
1340 | p7.content = forge.util.createBuffer('Hello');
|
1341 |
|
1342 | // encrypt
|
1343 | p7.encrypt();
|
1344 |
|
1345 | // convert message to PEM
|
1346 | var pem = forge.pkcs7.messageToPem(p7);
|
1347 |
|
1348 | // create a degenerate PKCS#7 certificate container
|
1349 | // (CRLs not currently supported, only certificates)
|
1350 | var p7 = forge.pkcs7.createSignedData();
|
1351 | p7.addCertificate(certOrCertPem1);
|
1352 | p7.addCertificate(certOrCertPem2);
|
1353 | var pem = forge.pkcs7.messageToPem(p7);
|
1354 |
|
1355 | // create PKCS#7 signed data with authenticatedAttributes
|
1356 | // attributes include: PKCS#9 content-type, message-digest, and signing-time
|
1357 | var p7 = forge.pkcs7.createSignedData();
|
1358 | p7.content = forge.util.createBuffer('Some content to be signed.', 'utf8');
|
1359 | p7.addCertificate(certOrCertPem);
|
1360 | p7.addSigner({
|
1361 | key: privateKeyAssociatedWithCert,
|
1362 | certificate: certOrCertPem,
|
1363 | digestAlgorithm: forge.pki.oids.sha256,
|
1364 | authenticatedAttributes: [{
|
1365 | type: forge.pki.oids.contentType,
|
1366 | value: forge.pki.oids.data
|
1367 | }, {
|
1368 | type: forge.pki.oids.messageDigest
|
1369 | // value will be auto-populated at signing time
|
1370 | }, {
|
1371 | type: forge.pki.oids.signingTime,
|
1372 | // value can also be auto-populated at signing time
|
1373 | value: new Date()
|
1374 | }]
|
1375 | });
|
1376 | p7.sign();
|
1377 | var pem = forge.pkcs7.messageToPem(p7);
|
1378 |
|
1379 | // PKCS#7 Sign in detached mode.
|
1380 | // Includes the signature and certificate without the signed data.
|
1381 | p7.sign({detached: true});
|
1382 |
|
1383 | ```
|
1384 |
|
1385 | <a name="pkcs8" />
|
1386 |
|
1387 | ### PKCS#8
|
1388 |
|
1389 | __Examples__
|
1390 |
|
1391 | ```js
|
1392 | var pki = forge.pki;
|
1393 |
|
1394 | // convert a PEM-formatted private key to a Forge private key
|
1395 | var privateKey = pki.privateKeyFromPem(pem);
|
1396 |
|
1397 | // convert a Forge private key to PEM-format
|
1398 | var pem = pki.privateKeyToPem(privateKey);
|
1399 |
|
1400 | // convert an ASN.1 PrivateKeyInfo or RSAPrivateKey to a Forge private key
|
1401 | var privateKey = pki.privateKeyFromAsn1(rsaPrivateKey);
|
1402 |
|
1403 | // convert a Forge private key to an ASN.1 RSAPrivateKey
|
1404 | var rsaPrivateKey = pki.privateKeyToAsn1(privateKey);
|
1405 |
|
1406 | // wrap an RSAPrivateKey ASN.1 object in a PKCS#8 ASN.1 PrivateKeyInfo
|
1407 | var privateKeyInfo = pki.wrapRsaPrivateKey(rsaPrivateKey);
|
1408 |
|
1409 | // convert a PKCS#8 ASN.1 PrivateKeyInfo to PEM
|
1410 | var pem = pki.privateKeyInfoToPem(privateKeyInfo);
|
1411 |
|
1412 | // encrypts a PrivateKeyInfo and outputs an EncryptedPrivateKeyInfo
|
1413 | var encryptedPrivateKeyInfo = pki.encryptPrivateKeyInfo(
|
1414 | privateKeyInfo, 'password', {
|
1415 | algorithm: 'aes256', // 'aes128', 'aes192', 'aes256', '3des'
|
1416 | });
|
1417 |
|
1418 | // decrypts an ASN.1 EncryptedPrivateKeyInfo
|
1419 | var privateKeyInfo = pki.decryptPrivateKeyInfo(
|
1420 | encryptedPrivateKeyInfo, 'password');
|
1421 |
|
1422 | // converts an EncryptedPrivateKeyInfo to PEM
|
1423 | var pem = pki.encryptedPrivateKeyToPem(encryptedPrivateKeyInfo);
|
1424 |
|
1425 | // converts a PEM-encoded EncryptedPrivateKeyInfo to ASN.1 format
|
1426 | var encryptedPrivateKeyInfo = pki.encryptedPrivateKeyFromPem(pem);
|
1427 |
|
1428 | // wraps and encrypts a Forge private key and outputs it in PEM format
|
1429 | var pem = pki.encryptRsaPrivateKey(privateKey, 'password');
|
1430 |
|
1431 | // encrypts a Forge private key and outputs it in PEM format using OpenSSL's
|
1432 | // proprietary legacy format + encapsulated PEM headers (DEK-Info)
|
1433 | var pem = pki.encryptRsaPrivateKey(privateKey, 'password', {legacy: true});
|
1434 |
|
1435 | // decrypts a PEM-formatted, encrypted private key
|
1436 | var privateKey = pki.decryptRsaPrivateKey(pem, 'password');
|
1437 |
|
1438 | // sets an RSA public key from a private key
|
1439 | var publicKey = pki.setRsaPublicKey(privateKey.n, privateKey.e);
|
1440 | ```
|
1441 |
|
1442 | <a name="pkcs10" />
|
1443 |
|
1444 | ### PKCS#10
|
1445 |
|
1446 | Provides certification requests or certificate signing requests (CSR) from
|
1447 | [PKCS#10][].
|
1448 |
|
1449 | __Examples__
|
1450 |
|
1451 | ```js
|
1452 | // generate a key pair
|
1453 | var keys = forge.pki.rsa.generateKeyPair(1024);
|
1454 |
|
1455 | // create a certification request (CSR)
|
1456 | var csr = forge.pki.createCertificationRequest();
|
1457 | csr.publicKey = keys.publicKey;
|
1458 | csr.setSubject([{
|
1459 | name: 'commonName',
|
1460 | value: 'example.org'
|
1461 | }, {
|
1462 | name: 'countryName',
|
1463 | value: 'US'
|
1464 | }, {
|
1465 | shortName: 'ST',
|
1466 | value: 'Virginia'
|
1467 | }, {
|
1468 | name: 'localityName',
|
1469 | value: 'Blacksburg'
|
1470 | }, {
|
1471 | name: 'organizationName',
|
1472 | value: 'Test'
|
1473 | }, {
|
1474 | shortName: 'OU',
|
1475 | value: 'Test'
|
1476 | }]);
|
1477 | // set (optional) attributes
|
1478 | csr.setAttributes([{
|
1479 | name: 'challengePassword',
|
1480 | value: 'password'
|
1481 | }, {
|
1482 | name: 'unstructuredName',
|
1483 | value: 'My Company, Inc.'
|
1484 | }, {
|
1485 | name: 'extensionRequest',
|
1486 | extensions: [{
|
1487 | name: 'subjectAltName',
|
1488 | altNames: [{
|
1489 | // 2 is DNS type
|
1490 | type: 2,
|
1491 | value: 'test.domain.com'
|
1492 | }, {
|
1493 | type: 2,
|
1494 | value: 'other.domain.com',
|
1495 | }, {
|
1496 | type: 2,
|
1497 | value: 'www.domain.net'
|
1498 | }]
|
1499 | }]
|
1500 | }]);
|
1501 |
|
1502 | // sign certification request
|
1503 | csr.sign(keys.privateKey);
|
1504 |
|
1505 | // verify certification request
|
1506 | var verified = csr.verify();
|
1507 |
|
1508 | // convert certification request to PEM-format
|
1509 | var pem = forge.pki.certificationRequestToPem(csr);
|
1510 |
|
1511 | // convert a Forge certification request from PEM-format
|
1512 | var csr = forge.pki.certificationRequestFromPem(pem);
|
1513 |
|
1514 | // get an attribute
|
1515 | csr.getAttribute({name: 'challengePassword'});
|
1516 |
|
1517 | // get extensions array
|
1518 | csr.getAttribute({name: 'extensionRequest'}).extensions;
|
1519 |
|
1520 | ```
|
1521 |
|
1522 | <a name="pkcs12" />
|
1523 |
|
1524 | ### PKCS#12
|
1525 |
|
1526 | Provides the cryptographic archive file format from [PKCS#12][].
|
1527 |
|
1528 | **Note for Chrome/Firefox/iOS/similar users**: If you have trouble importing
|
1529 | a PKCS#12 container, try using the TripleDES algorithm. It can be passed
|
1530 | to `forge.pkcs12.toPkcs12Asn1` using the `{algorithm: '3des'}` option.
|
1531 |
|
1532 | __Examples__
|
1533 |
|
1534 | ```js
|
1535 | // decode p12 from base64
|
1536 | var p12Der = forge.util.decode64(p12b64);
|
1537 | // get p12 as ASN.1 object
|
1538 | var p12Asn1 = forge.asn1.fromDer(p12Der);
|
1539 | // decrypt p12 using the password 'password'
|
1540 | var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, 'password');
|
1541 | // decrypt p12 using non-strict parsing mode (resolves some ASN.1 parse errors)
|
1542 | var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, 'password');
|
1543 | // decrypt p12 using literally no password (eg: Mac OS X/apple push)
|
1544 | var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1);
|
1545 | // decrypt p12 using an "empty" password (eg: OpenSSL with no password input)
|
1546 | var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, '');
|
1547 | // p12.safeContents is an array of safe contents, each of
|
1548 | // which contains an array of safeBags
|
1549 |
|
1550 | // get bags by friendlyName
|
1551 | var bags = p12.getBags({friendlyName: 'test'});
|
1552 | // bags are key'd by attribute type (here "friendlyName")
|
1553 | // and the key values are an array of matching objects
|
1554 | var cert = bags.friendlyName[0];
|
1555 |
|
1556 | // get bags by localKeyId
|
1557 | var bags = p12.getBags({localKeyId: buffer});
|
1558 | // bags are key'd by attribute type (here "localKeyId")
|
1559 | // and the key values are an array of matching objects
|
1560 | var cert = bags.localKeyId[0];
|
1561 |
|
1562 | // get bags by localKeyId (input in hex)
|
1563 | var bags = p12.getBags({localKeyIdHex: '7b59377ff142d0be4565e9ac3d396c01401cd879'});
|
1564 | // bags are key'd by attribute type (here "localKeyId", *not* "localKeyIdHex")
|
1565 | // and the key values are an array of matching objects
|
1566 | var cert = bags.localKeyId[0];
|
1567 |
|
1568 | // get bags by type
|
1569 | var bags = p12.getBags({bagType: forge.pki.oids.certBag});
|
1570 | // bags are key'd by bagType and each bagType key's value
|
1571 | // is an array of matches (in this case, certificate objects)
|
1572 | var cert = bags[forge.pki.oids.certBag][0];
|
1573 |
|
1574 | // get bags by friendlyName and filter on bag type
|
1575 | var bags = p12.getBags({
|
1576 | friendlyName: 'test',
|
1577 | bagType: forge.pki.oids.certBag
|
1578 | });
|
1579 |
|
1580 | // get key bags
|
1581 | var bags = p12.getBags({bagType: forge.pki.oids.keyBag});
|
1582 | // get key
|
1583 | var bag = bags[forge.pki.oids.keyBag][0];
|
1584 | var key = bag.key;
|
1585 | // if the key is in a format unrecognized by forge then
|
1586 | // bag.key will be `null`, use bag.asn1 to get the ASN.1
|
1587 | // representation of the key
|
1588 | if(bag.key === null) {
|
1589 | var keyAsn1 = bag.asn1;
|
1590 | // can now convert back to DER/PEM/etc for export
|
1591 | }
|
1592 |
|
1593 | // generate a p12 using AES (default)
|
1594 | var p12Asn1 = forge.pkcs12.toPkcs12Asn1(
|
1595 | privateKey, certificateChain, 'password');
|
1596 |
|
1597 | // generate a p12 that can be imported by Chrome/Firefox/iOS
|
1598 | // (requires the use of Triple DES instead of AES)
|
1599 | var p12Asn1 = forge.pkcs12.toPkcs12Asn1(
|
1600 | privateKey, certificateChain, 'password',
|
1601 | {algorithm: '3des'});
|
1602 |
|
1603 | // base64-encode p12
|
1604 | var p12Der = forge.asn1.toDer(p12Asn1).getBytes();
|
1605 | var p12b64 = forge.util.encode64(p12Der);
|
1606 |
|
1607 | // create download link for p12
|
1608 | var a = document.createElement('a');
|
1609 | a.download = 'example.p12';
|
1610 | a.setAttribute('href', 'data:application/x-pkcs12;base64,' + p12b64);
|
1611 | a.appendChild(document.createTextNode('Download'));
|
1612 | ```
|
1613 |
|
1614 | <a name="asn" />
|
1615 |
|
1616 | ### ASN.1
|
1617 |
|
1618 | Provides [ASN.1][] DER encoding and decoding.
|
1619 |
|
1620 | __Examples__
|
1621 |
|
1622 | ```js
|
1623 | var asn1 = forge.asn1;
|
1624 |
|
1625 | // create a SubjectPublicKeyInfo
|
1626 | var subjectPublicKeyInfo =
|
1627 | asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
1628 | // AlgorithmIdentifier
|
1629 | asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
1630 | // algorithm
|
1631 | asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
|
1632 | asn1.oidToDer(pki.oids['rsaEncryption']).getBytes()),
|
1633 | // parameters (null)
|
1634 | asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
|
1635 | ]),
|
1636 | // subjectPublicKey
|
1637 | asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false, [
|
1638 | // RSAPublicKey
|
1639 | asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
1640 | // modulus (n)
|
1641 | asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
|
1642 | _bnToBytes(key.n)),
|
1643 | // publicExponent (e)
|
1644 | asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
|
1645 | _bnToBytes(key.e))
|
1646 | ])
|
1647 | ])
|
1648 | ]);
|
1649 |
|
1650 | // serialize an ASN.1 object to DER format
|
1651 | var derBuffer = asn1.toDer(subjectPublicKeyInfo);
|
1652 |
|
1653 | // deserialize to an ASN.1 object from a byte buffer filled with DER data
|
1654 | var object = asn1.fromDer(derBuffer);
|
1655 |
|
1656 | // convert an OID dot-separated string to a byte buffer
|
1657 | var derOidBuffer = asn1.oidToDer('1.2.840.113549.1.1.5');
|
1658 |
|
1659 | // convert a byte buffer with a DER-encoded OID to a dot-separated string
|
1660 | console.log(asn1.derToOid(derOidBuffer));
|
1661 | // output: 1.2.840.113549.1.1.5
|
1662 |
|
1663 | // validates that an ASN.1 object matches a particular ASN.1 structure and
|
1664 | // captures data of interest from that structure for easy access
|
1665 | var publicKeyValidator = {
|
1666 | name: 'SubjectPublicKeyInfo',
|
1667 | tagClass: asn1.Class.UNIVERSAL,
|
1668 | type: asn1.Type.SEQUENCE,
|
1669 | constructed: true,
|
1670 | captureAsn1: 'subjectPublicKeyInfo',
|
1671 | value: [{
|
1672 | name: 'SubjectPublicKeyInfo.AlgorithmIdentifier',
|
1673 | tagClass: asn1.Class.UNIVERSAL,
|
1674 | type: asn1.Type.SEQUENCE,
|
1675 | constructed: true,
|
1676 | value: [{
|
1677 | name: 'AlgorithmIdentifier.algorithm',
|
1678 | tagClass: asn1.Class.UNIVERSAL,
|
1679 | type: asn1.Type.OID,
|
1680 | constructed: false,
|
1681 | capture: 'publicKeyOid'
|
1682 | }]
|
1683 | }, {
|
1684 | // subjectPublicKey
|
1685 | name: 'SubjectPublicKeyInfo.subjectPublicKey',
|
1686 | tagClass: asn1.Class.UNIVERSAL,
|
1687 | type: asn1.Type.BITSTRING,
|
1688 | constructed: false,
|
1689 | value: [{
|
1690 | // RSAPublicKey
|
1691 | name: 'SubjectPublicKeyInfo.subjectPublicKey.RSAPublicKey',
|
1692 | tagClass: asn1.Class.UNIVERSAL,
|
1693 | type: asn1.Type.SEQUENCE,
|
1694 | constructed: true,
|
1695 | optional: true,
|
1696 | captureAsn1: 'rsaPublicKey'
|
1697 | }]
|
1698 | }]
|
1699 | };
|
1700 |
|
1701 | var capture = {};
|
1702 | var errors = [];
|
1703 | if(!asn1.validate(
|
1704 | publicKeyValidator, subjectPublicKeyInfo, validator, capture, errors)) {
|
1705 | throw 'ASN.1 object is not a SubjectPublicKeyInfo.';
|
1706 | }
|
1707 | // capture.subjectPublicKeyInfo contains the full ASN.1 object
|
1708 | // capture.rsaPublicKey contains the full ASN.1 object for the RSA public key
|
1709 | // capture.publicKeyOid only contains the value for the OID
|
1710 | var oid = asn1.derToOid(capture.publicKeyOid);
|
1711 | if(oid !== pki.oids['rsaEncryption']) {
|
1712 | throw 'Unsupported OID.';
|
1713 | }
|
1714 |
|
1715 | // pretty print an ASN.1 object to a string for debugging purposes
|
1716 | asn1.prettyPrint(object);
|
1717 | ```
|
1718 |
|
1719 | Message Digests
|
1720 | ----------------
|
1721 |
|
1722 | <a name="sha1" />
|
1723 |
|
1724 | ### SHA1
|
1725 |
|
1726 | Provides [SHA-1][] message digests.
|
1727 |
|
1728 | __Examples__
|
1729 |
|
1730 | ```js
|
1731 | var md = forge.md.sha1.create();
|
1732 | md.update('The quick brown fox jumps over the lazy dog');
|
1733 | console.log(md.digest().toHex());
|
1734 | // output: 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
|
1735 | ```
|
1736 |
|
1737 | <a name="sha256" />
|
1738 |
|
1739 | ### SHA256
|
1740 |
|
1741 | Provides [SHA-256][] message digests.
|
1742 |
|
1743 | __Examples__
|
1744 |
|
1745 | ```js
|
1746 | var md = forge.md.sha256.create();
|
1747 | md.update('The quick brown fox jumps over the lazy dog');
|
1748 | console.log(md.digest().toHex());
|
1749 | // output: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
|
1750 | ```
|
1751 |
|
1752 | <a name="sha384" />
|
1753 |
|
1754 | ### SHA384
|
1755 |
|
1756 | Provides [SHA-384][] message digests.
|
1757 |
|
1758 | __Examples__
|
1759 |
|
1760 | ```js
|
1761 | var md = forge.md.sha384.create();
|
1762 | md.update('The quick brown fox jumps over the lazy dog');
|
1763 | console.log(md.digest().toHex());
|
1764 | // output: ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1
|
1765 | ```
|
1766 |
|
1767 | <a name="sha512" />
|
1768 |
|
1769 | ### SHA512
|
1770 |
|
1771 | Provides [SHA-512][] message digests.
|
1772 |
|
1773 | __Examples__
|
1774 |
|
1775 | ```js
|
1776 | // SHA-512
|
1777 | var md = forge.md.sha512.create();
|
1778 | md.update('The quick brown fox jumps over the lazy dog');
|
1779 | console.log(md.digest().toHex());
|
1780 | // output: 07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6
|
1781 |
|
1782 | // SHA-512/224
|
1783 | var md = forge.md.sha512.sha224.create();
|
1784 | md.update('The quick brown fox jumps over the lazy dog');
|
1785 | console.log(md.digest().toHex());
|
1786 | // output: 944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37
|
1787 |
|
1788 | // SHA-512/256
|
1789 | var md = forge.md.sha512.sha256.create();
|
1790 | md.update('The quick brown fox jumps over the lazy dog');
|
1791 | console.log(md.digest().toHex());
|
1792 | // output: dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d
|
1793 | ```
|
1794 |
|
1795 | <a name="md5" />
|
1796 |
|
1797 | ### MD5
|
1798 |
|
1799 | Provides [MD5][] message digests.
|
1800 |
|
1801 | __Examples__
|
1802 |
|
1803 | ```js
|
1804 | var md = forge.md.md5.create();
|
1805 | md.update('The quick brown fox jumps over the lazy dog');
|
1806 | console.log(md.digest().toHex());
|
1807 | // output: 9e107d9d372bb6826bd81d3542a419d6
|
1808 | ```
|
1809 |
|
1810 | <a name="hmac" />
|
1811 |
|
1812 | ### HMAC
|
1813 |
|
1814 | Provides [HMAC][] w/any supported message digest algorithm.
|
1815 |
|
1816 | __Examples__
|
1817 |
|
1818 | ```js
|
1819 | var hmac = forge.hmac.create();
|
1820 | hmac.start('sha1', 'Jefe');
|
1821 | hmac.update('what do ya want for nothing?');
|
1822 | console.log(hmac.digest().toHex());
|
1823 | // output: effcdf6ae5eb2fa2d27416d5f184df9c259a7c79
|
1824 | ```
|
1825 |
|
1826 | Utilities
|
1827 | ---------
|
1828 |
|
1829 | <a name="prime" />
|
1830 |
|
1831 | ### Prime
|
1832 |
|
1833 | Provides an API for generating large, random, probable primes.
|
1834 |
|
1835 | __Examples__
|
1836 |
|
1837 | ```js
|
1838 | // generate a random prime on the main JS thread
|
1839 | var bits = 1024;
|
1840 | forge.prime.generateProbablePrime(bits, function(err, num) {
|
1841 | console.log('random prime', num.toString(16));
|
1842 | });
|
1843 |
|
1844 | // generate a random prime using Web Workers (if available, otherwise
|
1845 | // falls back to the main thread)
|
1846 | var bits = 1024;
|
1847 | var options = {
|
1848 | algorithm: {
|
1849 | name: 'PRIMEINC',
|
1850 | workers: -1 // auto-optimize # of workers
|
1851 | }
|
1852 | };
|
1853 | forge.prime.generateProbablePrime(bits, options, function(err, num) {
|
1854 | console.log('random prime', num.toString(16));
|
1855 | });
|
1856 | ```
|
1857 |
|
1858 | <a name="prng" />
|
1859 |
|
1860 | ### PRNG
|
1861 |
|
1862 | Provides a [Fortuna][]-based cryptographically-secure pseudo-random number
|
1863 | generator, to be used with a cryptographic function backend, e.g. [AES][]. An
|
1864 | implementation using [AES][] as a backend is provided. An API for collecting
|
1865 | entropy is given, though if window.crypto.getRandomValues is available, it will
|
1866 | be used automatically.
|
1867 |
|
1868 | __Examples__
|
1869 |
|
1870 | ```js
|
1871 | // get some random bytes synchronously
|
1872 | var bytes = forge.random.getBytesSync(32);
|
1873 | console.log(forge.util.bytesToHex(bytes));
|
1874 |
|
1875 | // get some random bytes asynchronously
|
1876 | forge.random.getBytes(32, function(err, bytes) {
|
1877 | console.log(forge.util.bytesToHex(bytes));
|
1878 | });
|
1879 |
|
1880 | // collect some entropy if you'd like
|
1881 | forge.random.collect(someRandomBytes);
|
1882 | jQuery().mousemove(function(e) {
|
1883 | forge.random.collectInt(e.clientX, 16);
|
1884 | forge.random.collectInt(e.clientY, 16);
|
1885 | });
|
1886 |
|
1887 | // specify a seed file for use with the synchronous API if you'd like
|
1888 | forge.random.seedFileSync = function(needed) {
|
1889 | // get 'needed' number of random bytes from somewhere
|
1890 | return fetchedRandomBytes;
|
1891 | };
|
1892 |
|
1893 | // specify a seed file for use with the asynchronous API if you'd like
|
1894 | forge.random.seedFile = function(needed, callback) {
|
1895 | // get the 'needed' number of random bytes from somewhere
|
1896 | callback(null, fetchedRandomBytes);
|
1897 | });
|
1898 |
|
1899 | // register the main thread to send entropy or a Web Worker to receive
|
1900 | // entropy on demand from the main thread
|
1901 | forge.random.registerWorker(self);
|
1902 |
|
1903 | // generate a new instance of a PRNG with no collected entropy
|
1904 | var myPrng = forge.random.createInstance();
|
1905 | ```
|
1906 |
|
1907 | <a name="task" />
|
1908 |
|
1909 | ### Tasks
|
1910 |
|
1911 | Provides queuing and synchronizing tasks in a web application.
|
1912 |
|
1913 | __Examples__
|
1914 |
|
1915 | ```js
|
1916 | // TODO
|
1917 | ```
|
1918 |
|
1919 | <a name="util" />
|
1920 |
|
1921 | ### Utilities
|
1922 |
|
1923 | Provides utility functions, including byte buffer support, base64,
|
1924 | bytes to/from hex, zlib inflate/deflate, etc.
|
1925 |
|
1926 | __Examples__
|
1927 |
|
1928 | ```js
|
1929 | // encode/decode base64
|
1930 | var encoded = forge.util.encode64(str);
|
1931 | var str = forge.util.decode64(encoded);
|
1932 |
|
1933 | // encode/decode UTF-8
|
1934 | var encoded = forge.util.encodeUtf8(str);
|
1935 | var str = forge.util.decodeUtf8(encoded);
|
1936 |
|
1937 | // bytes to/from hex
|
1938 | var bytes = forge.util.hexToBytes(hex);
|
1939 | var hex = forge.util.bytesToHex(bytes);
|
1940 |
|
1941 | // create an empty byte buffer
|
1942 | var buffer = forge.util.createBuffer();
|
1943 | // create a byte buffer from raw binary bytes
|
1944 | var buffer = forge.util.createBuffer(input, 'raw');
|
1945 | // create a byte buffer from utf8 bytes
|
1946 | var buffer = forge.util.createBuffer(input, 'utf8');
|
1947 |
|
1948 | // get the length of the buffer in bytes
|
1949 | buffer.length();
|
1950 | // put bytes into the buffer
|
1951 | buffer.putBytes(bytes);
|
1952 | // put a 32-bit integer into the buffer
|
1953 | buffer.putInt32(10);
|
1954 | // buffer to hex
|
1955 | buffer.toHex();
|
1956 | // get a copy of the bytes in the buffer
|
1957 | bytes.bytes(/* count */);
|
1958 | // empty this buffer and get its contents
|
1959 | bytes.getBytes(/* count */);
|
1960 |
|
1961 | // convert a forge buffer into a Node.js Buffer
|
1962 | // make sure you specify the encoding as 'binary'
|
1963 | var forgeBuffer = forge.util.createBuffer();
|
1964 | var nodeBuffer = Buffer.from(forgeBuffer.getBytes(), 'binary');
|
1965 |
|
1966 | // convert a Node.js Buffer into a forge buffer
|
1967 | // make sure you specify the encoding as 'binary'
|
1968 | var nodeBuffer = Buffer.from('CAFE', 'hex');
|
1969 | var forgeBuffer = forge.util.createBuffer(nodeBuffer.toString('binary'));
|
1970 |
|
1971 | // parse a URL
|
1972 | var parsed = forge.util.parseUrl('http://example.com/foo?bar=baz');
|
1973 | // parsed.scheme, parsed.host, parsed.port, parsed.path, parsed.fullHost
|
1974 | ```
|
1975 |
|
1976 | <a name="log" />
|
1977 |
|
1978 | ### Logging
|
1979 |
|
1980 | Provides logging to a javascript console using various categories and
|
1981 | levels of verbosity.
|
1982 |
|
1983 | __Examples__
|
1984 |
|
1985 | ```js
|
1986 | // TODO
|
1987 | ```
|
1988 |
|
1989 | <a name="debug" />
|
1990 |
|
1991 | ### Debugging
|
1992 |
|
1993 | Provides storage of debugging information normally inaccessible in
|
1994 | closures for viewing/investigation.
|
1995 |
|
1996 | __Examples__
|
1997 |
|
1998 | ```js
|
1999 | // TODO
|
2000 | ```
|
2001 |
|
2002 | <a name="flash" />
|
2003 |
|
2004 | ### Flash Networking Support
|
2005 |
|
2006 | The [flash README](./flash/README.md) provides details on rebuilding the
|
2007 | optional Flash component used for networking. It also provides details on
|
2008 | Policy Server support.
|
2009 |
|
2010 | Security Considerations
|
2011 | -----------------------
|
2012 |
|
2013 | When using this code please keep the following in mind:
|
2014 |
|
2015 | - Cryptography is hard. Please review and test this code before depending on it
|
2016 | for critical functionality.
|
2017 | - The nature of JavaScript is that execution of this code depends on trusting a
|
2018 | very large set of JavaScript tools and systems. Consider runtime variations,
|
2019 | runtime characteristics, runtime optimization, code optimization, code
|
2020 | minimization, code obfuscation, bundling tools, possible bugs, the Forge code
|
2021 | itself, and so on.
|
2022 | - If using pre-built bundles from [Bower][] or similar be aware someone else
|
2023 | ran the tools to create those files.
|
2024 | - Use a secure transport channel such as [TLS][] to load scripts and consider
|
2025 | using additional security mechanisms such as [Subresource Integrity][] script
|
2026 | attributes.
|
2027 | - Use "native" functionality where possible. This can be critical when dealing
|
2028 | with performance and random number generation. Note that the JavaScript
|
2029 | random number algorithms should perform well if given suitable entropy.
|
2030 | - Understand possible attacks against cryptographic systems. For instance side
|
2031 | channel and timing attacks may be possible due to the difficulty in
|
2032 | implementing constant time algorithms in pure JavaScript.
|
2033 | - Certain features in this library are less susceptible to attacks depending on
|
2034 | usage. This primarily includes features that deal with data format
|
2035 | manipulation or those that are not involved in communication.
|
2036 |
|
2037 | Library Background
|
2038 | ------------------
|
2039 |
|
2040 | * https://digitalbazaar.com/2010/07/20/javascript-tls-1/
|
2041 | * https://digitalbazaar.com/2010/07/20/javascript-tls-2/
|
2042 |
|
2043 | Contact
|
2044 | -------
|
2045 |
|
2046 | * Code: https://github.com/digitalbazaar/forge
|
2047 | * Bugs: https://github.com/digitalbazaar/forge/issues
|
2048 | * Email: support@digitalbazaar.com
|
2049 | * IRC: [#forgejs][] on [freenode][]
|
2050 |
|
2051 | Donations
|
2052 | ---------
|
2053 |
|
2054 | Financial support is welcome and helps contribute to futher development:
|
2055 |
|
2056 | * For [PayPal][] please send to paypal@digitalbazaar.com.
|
2057 | * Something else? Please contact support@digitalbazaar.com.
|
2058 |
|
2059 | [#forgejs]: https://webchat.freenode.net/?channels=#forgejs
|
2060 | [0.6.x]: https://github.com/digitalbazaar/forge/tree/0.6.x
|
2061 | [3DES]: https://en.wikipedia.org/wiki/Triple_DES
|
2062 | [AES]: https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
|
2063 | [ASN.1]: https://en.wikipedia.org/wiki/ASN.1
|
2064 | [Bower]: https://bower.io/
|
2065 | [Browserify]: http://browserify.org/
|
2066 | [CBC]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
|
2067 | [CFB]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
|
2068 | [CTR]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
|
2069 | [CommonJS]: https://en.wikipedia.org/wiki/CommonJS
|
2070 | [DES]: https://en.wikipedia.org/wiki/Data_Encryption_Standard
|
2071 | [ECB]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
|
2072 | [Fortuna]: https://en.wikipedia.org/wiki/Fortuna_(PRNG)
|
2073 | [GCM]: https://en.wikipedia.org/wiki/GCM_mode
|
2074 | [HMAC]: https://en.wikipedia.org/wiki/HMAC
|
2075 | [JavaScript]: https://en.wikipedia.org/wiki/JavaScript
|
2076 | [Karma]: https://karma-runner.github.io/
|
2077 | [MD5]: https://en.wikipedia.org/wiki/MD5
|
2078 | [Node.js]: https://nodejs.org/
|
2079 | [OFB]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
|
2080 | [PKCS#10]: https://en.wikipedia.org/wiki/Certificate_signing_request
|
2081 | [PKCS#12]: https://en.wikipedia.org/wiki/PKCS_%E2%99%AF12
|
2082 | [PKCS#5]: https://en.wikipedia.org/wiki/PKCS
|
2083 | [PKCS#7]: https://en.wikipedia.org/wiki/Cryptographic_Message_Syntax
|
2084 | [PayPal]: https://www.paypal.com/
|
2085 | [RC2]: https://en.wikipedia.org/wiki/RC2
|
2086 | [SHA-1]: https://en.wikipedia.org/wiki/SHA-1
|
2087 | [SHA-256]: https://en.wikipedia.org/wiki/SHA-256
|
2088 | [SHA-384]: https://en.wikipedia.org/wiki/SHA-384
|
2089 | [SHA-512]: https://en.wikipedia.org/wiki/SHA-512
|
2090 | [Subresource Integrity]: https://www.w3.org/TR/SRI/
|
2091 | [TLS]: https://en.wikipedia.org/wiki/Transport_Layer_Security
|
2092 | [UMD]: https://github.com/umdjs/umd
|
2093 | [X.509]: https://en.wikipedia.org/wiki/X.509
|
2094 | [freenode]: https://freenode.net/
|
2095 | [unpkg]: https://unpkg.com/
|
2096 | [webpack]: https://webpack.github.io/
|
2097 | [TweetNaCl.js]: https://github.com/dchest/tweetnacl-js
|