@@ -551,10 +551,12 @@ protected void initKeyPair() throws Exception {
551551
552552 protected void createNewKey (String message ) {
553553 try {
554+ // cache the previous group key before overwriting it, so that messages still in flight (encrypted with
555+ // the old key) can be decrypted after the rotation
556+ cacheGroupKey (secret_key , sym_version );
554557 this .secret_key =createSecretKey ();
555558 initSymCiphers (sym_algorithm , secret_key );
556559 log .debug ("%s: created new group key (version: %s) %s" , local_addr , Util .byteArrayToHexString (sym_version ), message );
557- cacheGroupKey (sym_version );
558560 }
559561 catch (Exception ex ) {
560562 log .error ("%s: failed creating group key and initializing ciphers: %s" , local_addr , ex );
@@ -570,17 +572,19 @@ protected synchronized void installSharedGroupKey(Address sender, SecretKey key,
570572 }
571573 log .debug ("%s: installing group key received from %s (version: %s)" ,
572574 local_addr , sender != null ? sender : "key exchange protocol" , Util .byteArrayToHexString (version ));
575+ // cache the previous group key before overwriting it, so that messages still in flight (encrypted with
576+ // the old key) can be decrypted after the key change
577+ cacheGroupKey (secret_key , sym_version );
573578 secret_key =key ;
574579 initSymCiphers (sym_algorithm , key );
575580 sym_version =version ;
576- cacheGroupKey (version );
577581 }
578582
579- /** Cache the current shared key to decrypt messages encrypted with the old shared group key */
580- protected void cacheGroupKey ( byte [] version ) throws Exception {
581- // put the previous key into the map
582- if (secret_key != null )
583- key_map .putIfAbsent (new AsciiString (version ), secret_key );
583+ /** Caches a group key ( key, version) so that messages encrypted with a previous group key can still be decrypted
584+ * after a key rotation */
585+ protected void cacheGroupKey ( Key key , byte [] version ) {
586+ if (key != null && version != null )
587+ key_map .putIfAbsent (new AsciiString (version ), key );
584588 }
585589
586590 /** Encrypts the current secret key with the requester's public key (the requester will decrypt it with its private key) */
0 commit comments