20
20
var crypto = require ( 'crypto' ) ;
21
21
var base64 = require ( 'urlsafe-base64' ) ;
22
22
23
- var saved = {
24
- keymap : { } ,
25
- keylabels : { }
26
- } ;
27
23
var AES_GCM = 'aes-128-gcm' ;
28
24
var PAD_SIZE = { 'aes128gcm' : 1 , 'aesgcm' : 2 , 'aesgcm128' : 1 } ;
29
25
var TAG_LENGTH = 16 ;
@@ -102,15 +98,6 @@ function lengthPrefix(buffer) {
102
98
103
99
function extractDH ( header , mode ) {
104
100
var key = header . privateKey ;
105
- if ( ! key ) {
106
- if ( ! header . keymap || ! header . keyid || ! header . keymap [ header . keyid ] ) {
107
- throw new Error ( 'No known DH key for ' + header . keyid ) ;
108
- }
109
- key = header . keymap [ header . keyid ] ;
110
- }
111
- if ( ! header . keylabels [ header . keyid ] ) {
112
- throw new Error ( 'No known DH key label for ' + header . keyid ) ;
113
- }
114
101
var senderPubKey , receiverPubKey ;
115
102
if ( mode === MODE_ENCRYPT ) {
116
103
senderPubKey = key . getPublicKey ( ) ;
@@ -125,7 +112,7 @@ function extractDH(header, mode) {
125
112
return {
126
113
secret : key . computeSecret ( header . dh ) ,
127
114
context : Buffer . concat ( [
128
- Buffer . from ( header . keylabels [ header . keyid ] , 'ascii' ) ,
115
+ Buffer . from ( header . keylabel , 'ascii' ) ,
129
116
Buffer . from ( [ 0 ] ) ,
130
117
lengthPrefix ( receiverPubKey ) , // user agent
131
118
lengthPrefix ( senderPubKey ) // application server
@@ -248,12 +235,8 @@ function deriveKeyAndNonce(header, mode) {
248
235
/* Parse command-line arguments. */
249
236
function parseParams ( params ) {
250
237
var header = { } ;
251
- if ( params . version ) {
252
- header . version = params . version ;
253
- } else {
254
- header . version = ( params . padSize === 1 ) ? 'aesgcm128' : 'aesgcm' ;
255
- }
256
238
239
+ header . version = params . version || 'aes128gcm' ;
257
240
header . rs = parseInt ( params . rs , 10 ) ;
258
241
if ( isNaN ( header . rs ) ) {
259
242
header . rs = 4096 ;
@@ -281,7 +264,7 @@ function parseParams(params) {
281
264
header . keymap = params . keymap || saved . keymap ;
282
265
}
283
266
if ( header . version !== 'aes128gcm' ) {
284
- header . keylabels = params . keylabels || saved . keylabels ;
267
+ header . keylabel = params . keylabel || 'P-256' ;
285
268
}
286
269
if ( params . dh ) {
287
270
header . dh = decode ( params . dh ) ;
@@ -362,28 +345,21 @@ function decryptRecord(key, counter, buffer, header, last) {
362
345
return unpad ( data , last ) ;
363
346
}
364
347
365
- // TODO: this really should use the node streams stuff
366
-
367
348
/**
368
349
* Decrypt some bytes. This uses the parameters to determine the key and block
369
350
* size, which are described in the draft. Binary values are base64url encoded.
370
351
*
371
352
* |params.version| contains the version of encoding to use: aes128gcm is the latest,
372
353
* but aesgcm and aesgcm128 are also accepted (though the latter two might
373
- * disappear in a future release). If omitted, assume aesgcm, unless
374
- * |params.padSize| is set to 1, which means aesgcm128.
354
+ * disappear in a future release). If omitted, assume aes128gcm.
375
355
*
376
356
* If |params.key| is specified, that value is used as the key.
377
357
*
378
- * If |params.keyid| is specified without |params.dh|, the keyid value is used
379
- * to lookup the |params.keymap| for a buffer containing the key.
358
+ * If the version is aes128gcm, the keyid is extracted from the header and used
359
+ * as the ECDH public key of the sender. For version aesgcm and aesgcm128,
360
+ * |params.dh| needs to be provided with the public key of the sender.
380
361
*
381
- * For version aesgcm and aesgcm128, |params.dh| includes the public key of the sender. The ECDH key
382
- * pair used to decrypt is looked up using |params.keymap[params.keyid]|.
383
- *
384
- * Version aes128gcm is stricter. The |params.privateKey| includes the private
385
- * key of the receiver. The keyid is extracted from the header and used as the
386
- * ECDH public key of the sender.
362
+ * The |params.privateKey| includes the private key of the receiver.
387
363
*/
388
364
function decrypt ( buffer , params ) {
389
365
var header = parseParams ( params ) ;
@@ -470,21 +446,13 @@ function writeHeader(header) {
470
446
*
471
447
* |params.version| contains the version of encoding to use: aes128gcm is the latest,
472
448
* but aesgcm and aesgcm128 are also accepted (though the latter two might
473
- * disappear in a future release). If omitted, assume aesgcm, unless
474
- * |params.padSize| is set to 1, which means aesgcm128.
449
+ * disappear in a future release). If omitted, assume aes128gcm.
475
450
*
476
451
* If |params.key| is specified, that value is used as the key.
477
452
*
478
- * If |params.keyid| is specified without |params.dh|, the keyid value is used
479
- * to lookup the |params.keymap| for a buffer containing the key. This feature
480
- * is deprecated in favour of just including |params.key| or |params.privateKey|.
481
- *
482
453
* For Diffie-Hellman (WebPush), |params.dh| includes the public key of the
483
- * receiver. |params.privateKey| is used to establish a shared secret. For
484
- * versions aesgcm and aesgcm128, if a private key is not provided, the ECDH key
485
- * pair used to encrypt is looked up using |params.keymap[params.keyid]|, and
486
- * |params.keymap| defaults to the values saved with saveKey(). Key pairs can
487
- * be created using |crypto.createECDH()|.
454
+ * receiver. |params.privateKey| is used to establish a shared secret. Key
455
+ * pairs can be created using |crypto.createECDH()|.
488
456
*/
489
457
function encrypt ( buffer , params ) {
490
458
if ( ! Buffer . isBuffer ( buffer ) ) {
@@ -497,7 +465,7 @@ function encrypt(buffer, params) {
497
465
498
466
var result ;
499
467
if ( header . version === 'aes128gcm' ) {
500
- // Save the DH public key in the header.
468
+ // Save the DH public key in the header unless keyid is set .
501
469
if ( header . privateKey && ! header . keyid ) {
502
470
header . keyid = header . privateKey . getPublicKey ( ) ;
503
471
}
@@ -548,18 +516,7 @@ function encrypt(buffer, params) {
548
516
return result ;
549
517
}
550
518
551
- /**
552
- * Deprecated. Use the keymap and keylabels arguments to encrypt()/decrypt().
553
- */
554
- function saveKey ( id , key , dhLabel ) {
555
- saved . keymap [ id ] = key ;
556
- if ( dhLabel ) {
557
- saved . keylabels [ id ] = dhLabel ;
558
- }
559
- }
560
-
561
519
module . exports = {
562
520
decrypt : decrypt ,
563
- encrypt : encrypt ,
564
- saveKey : saveKey
521
+ encrypt : encrypt
565
522
} ;
0 commit comments