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

   1: /* ServerKeyExchange.java -- SSL ServerKeyExchange message.
   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 java.io.PrintWriter;
  42: import java.io.StringWriter;
  43: 
  44: import java.nio.ByteBuffer;
  45: import java.nio.ByteOrder;
  46: 
  47: /**
  48:  * The server key exchange message.
  49:  *
  50:  * <pre>
  51: struct
  52: {
  53:   select (KeyExchangeAlgorithm)
  54:   {
  55:     case diffie_hellman:
  56:       ServerDHParams params;
  57:       Signature signed_params;
  58:     case rsa:
  59:       ServerRSAParams params;
  60:       Signature signed_params;
  61:     case srp:
  62:       ServerSRPParams params;
  63:       Signature signed_params;
  64:   };
  65: } ServerKeyExchange;
  66: </pre>
  67:  */
  68: public class ServerKeyExchange implements Handshake.Body
  69: {
  70: 
  71:   protected ByteBuffer buffer;
  72:   protected final CipherSuite suite;
  73: 
  74:   public ServerKeyExchange(final ByteBuffer buffer, final CipherSuite suite)
  75:   {
  76:     suite.getClass();
  77:     this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
  78:     this.suite = suite;
  79:   }
  80: 
  81:   public int length ()
  82:   {
  83:     if (suite.keyExchangeAlgorithm ().equals (KeyExchangeAlgorithm.NONE))
  84:       return 0;
  85:     int len = 0;
  86:     ServerKeyExchangeParams params = params();
  87:     Signature sig = signature();
  88:     if (params != null)
  89:       len += params.length();
  90:     if (sig != null)
  91:       len += sig.length();
  92:     return len;
  93:   }
  94: 
  95:   /**
  96:    * Returns the server's key exchange parameters. The value returned will
  97:    * depend on the key exchange algorithm this object was created with.
  98:    *
  99:    * @return The server's key exchange parameters.
 100:    */
 101:   public ServerKeyExchangeParams params ()
 102:   {
 103:     KeyExchangeAlgorithm kex = suite.keyExchangeAlgorithm ();
 104:     if (kex == KeyExchangeAlgorithm.RSA)
 105:       return new ServerRSAParams(buffer.duplicate ());
 106:     else if (kex == KeyExchangeAlgorithm.DHE_DSS
 107:              || kex == KeyExchangeAlgorithm.DHE_RSA
 108:              || kex == KeyExchangeAlgorithm.DH_anon)
 109:       return new ServerDHParams(buffer.duplicate());
 110: //     else if (kex.equals (KeyExchangeAlgorithm.SRP))
 111: //       return new ServerSRPParams (buffer.duplicate ());
 112:     else if (kex == KeyExchangeAlgorithm.NONE)
 113:       return null;
 114:     else if (kex == KeyExchangeAlgorithm.DHE_PSK)
 115:       return new ServerDHE_PSKParameters(buffer.duplicate());
 116:     else if (kex == KeyExchangeAlgorithm.PSK)
 117:       return new ServerPSKParameters(buffer.duplicate());
 118:     else if (kex == KeyExchangeAlgorithm.RSA_PSK)
 119:       return new ServerPSKParameters(buffer.duplicate());
 120:     throw new IllegalArgumentException ("unsupported key exchange: " + kex);
 121:   }
 122: 
 123:   /**
 124:    * Returns the digital signature made over the key exchange parameters.
 125:    *
 126:    * @return The signature.
 127:    */
 128:   public Signature signature ()
 129:   {
 130:     KeyExchangeAlgorithm kex = suite.keyExchangeAlgorithm();
 131:     if (kex == KeyExchangeAlgorithm.NONE
 132:         || kex == KeyExchangeAlgorithm.DH_anon
 133:         || kex == KeyExchangeAlgorithm.DHE_PSK
 134:         || kex == KeyExchangeAlgorithm.PSK
 135:         || kex == KeyExchangeAlgorithm.RSA_PSK)
 136:       return null;
 137:     ServerKeyExchangeParams params = params();
 138:     ByteBuffer sigbuf = ((ByteBuffer) buffer.position(params.length ())).slice ();
 139:     return new Signature (sigbuf, suite.signatureAlgorithm ());
 140:   }
 141: 
 142:   public String toString()
 143:   {
 144:     return toString (null);
 145:   }
 146: 
 147:   public String toString (final String prefix)
 148:   {
 149:     StringWriter str = new StringWriter();
 150:     PrintWriter out = new PrintWriter(str);
 151:     if (prefix != null) out.print (prefix);
 152:     out.println("struct {");
 153:     if (prefix != null) out.print (prefix);
 154:     out.print ("  algorithm: ");
 155:     out.print (suite.keyExchangeAlgorithm ());
 156:     out.println (";");
 157:     if (!suite.keyExchangeAlgorithm ().equals (KeyExchangeAlgorithm.NONE))
 158:       {
 159:         if (prefix != null) out.print (prefix);
 160:         out.println ("  parameters:");
 161:         out.println (params ().toString (prefix != null ? prefix+"  " : "  "));
 162:       }
 163:     if (!suite.signatureAlgorithm ().equals (SignatureAlgorithm.ANONYMOUS))
 164:       {
 165:         if (prefix != null) out.print (prefix);
 166:         out.println ("  signature:");
 167:         out.println (signature ().toString (prefix != null ? prefix+"  " : "  "));
 168:       }
 169:     if (prefix != null) out.print (prefix);
 170:     out.print ("} ServerKeyExchange;");
 171:     return str.toString();
 172:   }
 173: }