2020var crypto = require ( 'crypto' ) ;
2121var base64 = require ( 'urlsafe-base64' ) ;
2222
23- var saved = {
24- keymap : { } ,
25- keylabels : { }
26- } ;
2723var AES_GCM = 'aes-128-gcm' ;
2824var PAD_SIZE = { 'aes128gcm' : 1 , 'aesgcm' : 2 , 'aesgcm128' : 1 } ;
2925var TAG_LENGTH = 16 ;
@@ -102,15 +98,6 @@ function lengthPrefix(buffer) {
10298
10399function extractDH ( header , mode ) {
104100 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- }
114101 var senderPubKey , receiverPubKey ;
115102 if ( mode === MODE_ENCRYPT ) {
116103 senderPubKey = key . getPublicKey ( ) ;
@@ -125,7 +112,7 @@ function extractDH(header, mode) {
125112 return {
126113 secret : key . computeSecret ( header . dh ) ,
127114 context : Buffer . concat ( [
128- Buffer . from ( header . keylabels [ header . keyid ] , 'ascii' ) ,
115+ Buffer . from ( header . keylabel , 'ascii' ) ,
129116 Buffer . from ( [ 0 ] ) ,
130117 lengthPrefix ( receiverPubKey ) , // user agent
131118 lengthPrefix ( senderPubKey ) // application server
@@ -248,12 +235,8 @@ function deriveKeyAndNonce(header, mode) {
248235/* Parse command-line arguments. */
249236function parseParams ( params ) {
250237 var header = { } ;
251- if ( params . version ) {
252- header . version = params . version ;
253- } else {
254- header . version = ( params . padSize === 1 ) ? 'aesgcm128' : 'aesgcm' ;
255- }
256238
239+ header . version = params . version || 'aes128gcm' ;
257240 header . rs = parseInt ( params . rs , 10 ) ;
258241 if ( isNaN ( header . rs ) ) {
259242 header . rs = 4096 ;
@@ -281,7 +264,7 @@ function parseParams(params) {
281264 header . keymap = params . keymap || saved . keymap ;
282265 }
283266 if ( header . version !== 'aes128gcm' ) {
284- header . keylabels = params . keylabels || saved . keylabels ;
267+ header . keylabel = params . keylabel || 'P-256' ;
285268 }
286269 if ( params . dh ) {
287270 header . dh = decode ( params . dh ) ;
@@ -362,28 +345,21 @@ function decryptRecord(key, counter, buffer, header, last) {
362345 return unpad ( data , last ) ;
363346}
364347
365- // TODO: this really should use the node streams stuff
366-
367348/**
368349 * Decrypt some bytes. This uses the parameters to determine the key and block
369350 * size, which are described in the draft. Binary values are base64url encoded.
370351 *
371352 * |params.version| contains the version of encoding to use: aes128gcm is the latest,
372353 * 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.
375355 *
376356 * If |params.key| is specified, that value is used as the key.
377357 *
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.
380361 *
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.
387363 */
388364function decrypt ( buffer , params ) {
389365 var header = parseParams ( params ) ;
@@ -470,21 +446,13 @@ function writeHeader(header) {
470446 *
471447 * |params.version| contains the version of encoding to use: aes128gcm is the latest,
472448 * 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.
475450 *
476451 * If |params.key| is specified, that value is used as the key.
477452 *
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- *
482453 * 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()|.
488456 */
489457function encrypt ( buffer , params ) {
490458 if ( ! Buffer . isBuffer ( buffer ) ) {
@@ -497,7 +465,7 @@ function encrypt(buffer, params) {
497465
498466 var result ;
499467 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 .
501469 if ( header . privateKey && ! header . keyid ) {
502470 header . keyid = header . privateKey . getPublicKey ( ) ;
503471 }
@@ -548,18 +516,7 @@ function encrypt(buffer, params) {
548516 return result ;
549517}
550518
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-
561519module . exports = {
562520 decrypt : decrypt ,
563- encrypt : encrypt ,
564- saveKey : saveKey
521+ encrypt : encrypt
565522} ;
0 commit comments