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

   1: /* EncryptedPreMasterSecret.java -- RSA encrypted secret.
   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.classpath.debug.Component;
  42: import gnu.classpath.debug.SystemLogger;
  43: 
  44: import java.io.PrintWriter;
  45: import java.io.StringWriter;
  46: 
  47: import java.nio.ByteBuffer;
  48: 
  49: /**
  50:  * The client's RSA-encrypted pre-master secret.
  51:  *
  52:  * <pre>
  53: struct {
  54:   public-key-encrypted PreMasterSecret pre_master_secret;
  55: } EncryptedPreMasterSecret;</pre>
  56:  */
  57: public final class EncryptedPreMasterSecret extends ExchangeKeys implements Builder
  58: {
  59:   private final ProtocolVersion version;
  60: 
  61:   public EncryptedPreMasterSecret(ByteBuffer buffer, ProtocolVersion version)
  62:   {
  63:     super(buffer);
  64:     version.getClass();
  65:     this.version = version;
  66:   }
  67:   
  68:   public EncryptedPreMasterSecret(byte[] encryptedSecret, ProtocolVersion version)
  69:   {
  70:     this(ByteBuffer.allocate(version == ProtocolVersion.SSL_3
  71:                              ? encryptedSecret.length
  72:                              : encryptedSecret.length + 2), version);
  73:     ByteBuffer b = buffer.duplicate();
  74:     if (version != ProtocolVersion.SSL_3)
  75:       b.putShort((short) encryptedSecret.length);
  76:     b.put(encryptedSecret);
  77:   }
  78:   
  79:   public ByteBuffer buffer()
  80:   {
  81:     return (ByteBuffer) buffer.duplicate().rewind();
  82:   }
  83: 
  84:   public byte[] encryptedSecret()
  85:   {
  86:     byte[] secret;
  87:     if (version == ProtocolVersion.SSL_3)
  88:       {
  89:         buffer.position (0);
  90:         secret = new byte[buffer.limit ()];
  91:         buffer.get(secret);
  92:       }
  93:     else
  94:       {
  95:         int len = buffer.getShort(0) & 0xFFFF;
  96:         secret = new byte[len];
  97:         buffer.position(2);
  98:         buffer.get(secret);
  99:       }
 100:     return secret;
 101:   }
 102: 
 103:   public void setEncryptedSecret(final byte[] secret, final int offset, final int length)
 104:   {
 105:     if (version == ProtocolVersion.SSL_3)
 106:       {
 107:         buffer.position(0);
 108:         buffer.put(secret, offset, length);
 109:         buffer.rewind();
 110:       }
 111:     else
 112:       {
 113:         buffer.putShort(0, (short) length);
 114:         buffer.position(2);
 115:         buffer.put(secret, offset, length);
 116:         buffer.rewind();
 117:       }
 118:   }
 119: 
 120:   public int length ()
 121:   {
 122:     if (version == ProtocolVersion.SSL_3)
 123:       {
 124:         return buffer.capacity();
 125:       }
 126:     else
 127:       {
 128:         return (buffer.getShort(0) & 0xFFFF) + 2;
 129:       }
 130:   }
 131: 
 132:   public String toString ()
 133:   {
 134:     return toString (null);
 135:   }
 136: 
 137:   public String toString (final String prefix)
 138:   {
 139:     StringWriter str = new StringWriter();
 140:     PrintWriter out = new PrintWriter(str);
 141:     if (prefix != null) out.print(prefix);
 142:     out.println("struct {");
 143:     if (prefix != null) out.print(prefix);
 144:     out.println("  pre_master_secret = ");
 145:     out.print(Util.hexDump(encryptedSecret(), prefix != null ? prefix + "    "
 146:                                                              : "    "));
 147:     if (prefix != null) out.print(prefix);
 148:     out.print("} EncryptedPreMasterSecret;");
 149:     return str.toString();
 150:   }