CVS update: JGroups/src/org/jgroups/protocols ENCRYPT.java

"Bela Ban" <[email protected]>
Newsgroups gmane.comp.java.javagroups.cvs
Message-ID <[email protected]>
  User: belaban 
  Date: 10/08/17 08:28:46

  Modified:    src/org/jgroups/protocols Tag: Branch_JGroups_2_6
                        ENCRYPT.java
  Log:
  Fixed concurrent access to Cipher (https://jira.jboss.org/browse/JGRP-1228)
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.38.4.2  +24 -10    JGroups/src/org/jgroups/protocols/ENCRYPT.java
  
  Index: ENCRYPT.java
  ===================================================================
  RCS file: /cvsroot/javagroups/JGroups/src/org/jgroups/protocols/ENCRYPT.java,v
  retrieving revision 1.38.4.1
  retrieving revision 1.38.4.2
  diff -u -r1.38.4.1 -r1.38.4.2
  --- ENCRYPT.java	12 Mar 2008 07:28:27 -0000	1.38.4.1
  +++ ENCRYPT.java	17 Aug 2010 08:28:46 -0000	1.38.4.2
  @@ -1,4 +1,4 @@
  -// $Id: ENCRYPT.java,v 1.38.4.1 2008/03/12 07:28:27 belaban Exp $
  +// $Id: ENCRYPT.java,v 1.38.4.2 2010/08/17 08:28:46 belaban Exp $
   
   package org.jgroups.protocols;
   
  @@ -24,6 +24,8 @@
   import java.util.concurrent.BlockingQueue;
   import java.util.concurrent.LinkedBlockingQueue;
   import java.util.concurrent.TimeUnit;
  +import java.util.concurrent.locks.Lock;
  +import java.util.concurrent.locks.ReentrantLock;
   
   
   /**
  @@ -151,7 +153,7 @@
       // queue to hold upcoming messages while key negotiation is happening
       private BlockingQueue<Event> upMessageQueue = new LinkedBlockingQueue<Event>();
   
  -//	 queue to hold downcoming messages while key negotiation is happening
  +    //	 queue to hold downcoming messages while key negotiation is happening
       private BlockingQueue<Event> downMessageQueue = new LinkedBlockingQueue<Event>();
       // decrypting cypher for secret key requests
       private Cipher asymCipher;
  @@ -159,6 +161,9 @@
       /** determines whether to encrypt the entire message, or just the buffer */
       private boolean encrypt_entire_message=false;
   
  +    /** To prevent concurrent access to the decrypting cypher */
  +    protected final Lock decrypt_lock=new ReentrantLock();
  +
   
       public ENCRYPT()
       {
  @@ -764,7 +769,7 @@
           //we do not synchronize here as we only have one up thread so we should never get an issue
           //synchronized(upLock){
           Event tmp =null;
  -        while ((tmp = (Event)upMessageQueue.poll(0L, TimeUnit.MILLISECONDS)) != null){
  +        while ((tmp =upMessageQueue.poll(0L, TimeUnit.MILLISECONDS)) != null){
               Message msg = decryptMessage(symDecodingCipher, ((Message)tmp.getArg()).copy());
   
               if (msg != null){
  @@ -824,7 +829,7 @@
           EncryptHeader hdr = (EncryptHeader)msg.getHeader(EncryptHeader.KEY);
           if (!hdr.getVersion().equals(getSymVersion())){
               log.warn("attempting to use stored cipher as message does not uses current encryption version ");
  -            cipher = (Cipher)keyMap.get(hdr.getVersion());
  +            cipher =keyMap.get(hdr.getVersion());
               if (cipher == null) {
                   log.warn("Unable to find a matching cipher in previous key map");
                   return null;
  @@ -843,13 +848,22 @@
       }
   
   
  -    private static Message _decrypt(Cipher cipher, Message msg, boolean decrypt_entire_msg) throws Exception {
  +    private Message _decrypt(final Cipher cipher, Message msg, boolean decrypt_entire_msg) throws Exception {
  +        byte[] decrypted_msg;
  +
  +        decrypt_lock.lock();
  +        try {
  +            decrypted_msg=cipher.doFinal(msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  +        }
  +        finally {
  +            decrypt_lock.unlock();
  +        }
  +
           if(!decrypt_entire_msg) {
  -            msg.setBuffer(cipher.doFinal(msg.getRawBuffer(), msg.getOffset(), msg.getLength()));
  +            msg.setBuffer(decrypted_msg);
               return msg;
           }
   
  -        byte[] decrypted_msg=cipher.doFinal(msg.getRawBuffer(), msg.getOffset(), msg.getLength());
           Message ret=(Message)Util.streamableFromByteBuffer(Message.class, decrypted_msg);
           if(ret.getDest() == null)
               ret.setDest(msg.getDest());
  @@ -1040,7 +1054,7 @@
           //	we do not synchronize here as we only have one down thread so we should never get an issue
           //  first lets replay any oustanding events
           Event tmp =null;
  -        while((tmp = (Event)downMessageQueue.poll(0L, TimeUnit.MILLISECONDS) )!= null){
  +        while((tmp =downMessageQueue.poll(0L, TimeUnit.MILLISECONDS))!= null){
               sendDown(tmp);
           }
       }
  @@ -1093,13 +1107,13 @@
        * @return
        * @throws Exception
        */
  -    private static byte[] encryptMessage(Cipher cipher, byte[] plain, int offset, int length) throws Exception
  +    private synchronized byte[] encryptMessage(Cipher cipher, byte[] plain, int offset, int length) throws Exception
       {
           return cipher.doFinal(plain, offset, length);
       }
   
   
  -    private SecretKeySpec decodeKey(byte[] encodedKey) throws Exception
  +    private synchronized SecretKeySpec decodeKey(byte[] encodedKey) throws Exception
       {
           // try and decode secrey key sent from keyserver
           byte[] keyBytes = asymCipher.doFinal(encodedKey);
  
  
  

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.