Source for gnu.javax.crypto.key.BaseKeyAgreementParty

   1: /* BaseKeyAgreementParty.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.crypto.key;
  40: 
  41: import gnu.java.security.prng.IRandom;
  42: import gnu.java.security.prng.LimitReachedException;
  43: import gnu.java.security.util.PRNG;
  44: 
  45: import java.math.BigInteger;
  46: import java.security.SecureRandom;
  47: import java.util.Map;
  48: 
  49: /**
  50:  * A base abstract class to facilitate implementations of concrete key agreement
  51:  * protocol handlers.
  52:  */
  53: public abstract class BaseKeyAgreementParty
  54:     implements IKeyAgreementParty
  55: {
  56:   protected static final BigInteger TWO = BigInteger.valueOf(2L);
  57:   /** The canonical name of the protocol. */
  58:   protected String name;
  59:   /** Whether the instance is initialised or not. */
  60:   protected boolean initialised = false;
  61:   /** The current step index of the protocol exchange. */
  62:   protected int step = -1;
  63:   /** Whether the exchange has concluded or not. */
  64:   protected boolean complete = false;
  65:   /** The optional {@link SecureRandom} instance to use. */
  66:   protected SecureRandom rnd = null;
  67:   /** The optional {@link IRandom} instance to use. */
  68:   protected IRandom irnd = null;
  69:   /** Our default source of randomness. */
  70:   private PRNG prng = null;
  71: 
  72:   protected BaseKeyAgreementParty(String name)
  73:   {
  74:     super();
  75: 
  76:     this.name = name;
  77:   }
  78: 
  79:   public String name()
  80:   {
  81:     return name;
  82:   }
  83: 
  84:   public void init(Map attributes) throws KeyAgreementException
  85:   {
  86:     if (initialised)
  87:       throw new IllegalStateException("already initialised");
  88:     this.engineInit(attributes);
  89:     initialised = true;
  90:     this.step = -1;
  91:     this.complete = false;
  92:   }
  93: 
  94:   public OutgoingMessage processMessage(IncomingMessage in)
  95:       throws KeyAgreementException
  96:   {
  97:     if (! initialised)
  98:       throw new IllegalStateException("not initialised");
  99:     if (complete)
 100:       throw new IllegalStateException("exchange has already concluded");
 101:     step++;
 102:     return this.engineProcessMessage(in);
 103:   }
 104: 
 105:   public boolean isComplete()
 106:   {
 107:     return complete;
 108:   }
 109: 
 110:   public byte[] getSharedSecret() throws KeyAgreementException
 111:   {
 112:     if (! initialised)
 113:       throw new KeyAgreementException("not yet initialised");
 114:     if (! isComplete())
 115:       throw new KeyAgreementException("not yet computed");
 116:     return engineSharedSecret();
 117:   }
 118: 
 119:   public void reset()
 120:   {
 121:     if (initialised)
 122:       {
 123:         this.engineReset();
 124:         initialised = false;
 125:       }
 126:   }
 127: 
 128:   protected abstract void engineInit(Map attributes)
 129:       throws KeyAgreementException;
 130: 
 131:   protected abstract OutgoingMessage engineProcessMessage(IncomingMessage in)
 132:       throws KeyAgreementException;
 133: 
 134:   protected abstract byte[] engineSharedSecret() throws KeyAgreementException;
 135: 
 136:   protected abstract void engineReset();
 137: 
 138:   /**
 139:    * Fills the designated byte array with random data.
 140:    *
 141:    * @param buffer the byte array to fill with random data.
 142:    */
 143:   protected void nextRandomBytes(byte[] buffer)
 144:   {
 145:     if (rnd != null)
 146:       rnd.nextBytes(buffer);
 147:     else if (irnd != null)
 148:       try
 149:         {
 150:           irnd.nextBytes(buffer, 0, buffer.length);
 151:         }
 152:       catch (LimitReachedException lre)
 153:         {
 154:           irnd = null;
 155:           getDefaultPRNG().nextBytes(buffer);
 156:         }
 157:     else
 158:       getDefaultPRNG().nextBytes(buffer);
 159:   }
 160: 
 161:   private PRNG getDefaultPRNG()
 162:   {
 163:     if (prng == null)
 164:       prng = PRNG.getInstance();
 165: 
 166:     return prng;
 167:   }
 168: }