Source for gnu.javax.net.ssl.provider.SessionImpl

   1: /* SessionImpl.java --
   2:    Copyright (C) 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.net.ssl.provider;
  40: 
  41: import gnu.javax.crypto.key.GnuPBEKey;
  42: import gnu.javax.net.ssl.Session;
  43: import java.io.IOException;
  44: import java.io.Serializable;
  45: 
  46: import java.security.InvalidKeyException;
  47: import java.security.NoSuchAlgorithmException;
  48: import java.security.SecureRandom;
  49: import javax.crypto.Cipher;
  50: import javax.crypto.IllegalBlockSizeException;
  51: import javax.crypto.NoSuchPaddingException;
  52: import javax.crypto.SealedObject;
  53: import javax.net.ssl.SSLException;
  54: 
  55: public class SessionImpl extends Session
  56: {
  57:   static final long serialVersionUID = 8932976607588442485L;
  58:   CipherSuite suite;
  59:   ProtocolVersion version;
  60:   byte[] privateDataSalt;
  61:   SealedObject sealedPrivateData;
  62:   MaxFragmentLength maxLength;
  63: 
  64:   transient PrivateData privateData;
  65: 
  66:   public SessionImpl()
  67:   {
  68:     super();
  69:     privateData = new PrivateData();
  70:   }
  71: 
  72:   SecureRandom random ()
  73:   {
  74:     return random;
  75:   }
  76: 
  77:   public String getProtocol()
  78:   {
  79:     return version.toString();
  80:   }
  81: 
  82:   public void prepare(char[] passwd) throws SSLException
  83:   {
  84:     try
  85:       {
  86:         privateDataSalt = new byte[32];
  87:         random.nextBytes(privateDataSalt);
  88:         GnuPBEKey key = new GnuPBEKey(passwd, privateDataSalt, 1000);
  89:         Cipher cipher = Cipher.getInstance("PBEWithHMacSHA256AndAES/OFB/PKCS7Padding");
  90:         cipher.init(Cipher.ENCRYPT_MODE, key);
  91:         sealedPrivateData = new SealedObject(privateData, cipher);
  92:       }
  93:     catch (IllegalBlockSizeException ibse)
  94:       {
  95:         throw new SSLException(ibse);
  96:       }
  97:     catch (InvalidKeyException ike)
  98:       {
  99:         throw new SSLException(ike);
 100:       }
 101:     catch (IOException ioe)
 102:       {
 103:         throw new SSLException(ioe);
 104:       }
 105:     catch (NoSuchAlgorithmException nsae)
 106:       {
 107:         throw new SSLException(nsae);
 108:       }
 109:     catch (NoSuchPaddingException nspe)
 110:       {
 111:         throw new SSLException(nspe);
 112:       }
 113:   }
 114: 
 115:   public void repair(char[] passwd) throws SSLException
 116:   {
 117:     try
 118:       {
 119:         GnuPBEKey key = new GnuPBEKey(passwd, privateDataSalt, 1000);
 120:         privateData = (PrivateData) sealedPrivateData.getObject(key);
 121:       }
 122:     catch (ClassNotFoundException cnfe)
 123:       {
 124:         throw new SSLException(cnfe);
 125:       }
 126:     catch (InvalidKeyException ike)
 127:       {
 128:         throw new SSLException(ike);
 129:       }
 130:     catch (IOException ioe)
 131:       {
 132:         throw new SSLException(ioe);
 133:       }
 134:     catch (NoSuchAlgorithmException nsae)
 135:       {
 136:         throw new SSLException(nsae);
 137:       }
 138:   }
 139: 
 140:   public SealedObject privateData() throws SSLException
 141:   {
 142:     if (privateData == null)
 143:       throw new SSLException("this session has not been prepared");
 144:     return sealedPrivateData;
 145:   }
 146: 
 147:   public void setPrivateData(SealedObject so) throws SSLException
 148:   {
 149:     this.sealedPrivateData = so;
 150:   }
 151: 
 152:   void setApplicationBufferSize(int size)
 153:   {
 154:     applicationBufferSize = size;
 155:   }
 156: 
 157:   void setRandom(SecureRandom random)
 158:   {
 159:     this.random = random;
 160:   }
 161: 
 162:   void setTruncatedMac(boolean truncatedMac)
 163:   {
 164:     this.truncatedMac = truncatedMac;
 165:   }
 166: 
 167:   void setId(Session.ID id)
 168:   {
 169:     this.sessionId = id;
 170:   }
 171: 
 172:   void setLocalCertificates(java.security.cert.Certificate[] chain)
 173:   {
 174:     this.localCerts = chain;
 175:   }
 176: 
 177:   void setPeerCertificates(java.security.cert.Certificate[] chain)
 178:   {
 179:     this.peerCerts = chain;
 180:   }
 181: 
 182:   void setPeerVerified(boolean peerVerified)
 183:   {
 184:     this.peerVerified = peerVerified;
 185:   }
 186: 
 187:   static class PrivateData implements Serializable
 188:   {
 189:     static final long serialVersionUID = -8040597659545984581L;
 190:     byte[] masterSecret;
 191:   }
 192: }