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

   1: /* ServerDHParams.java -- The server's Diffie-Hellman parameters.
   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: import java.math.BigInteger;
  44: import java.nio.ByteBuffer;
  45: import java.nio.ByteOrder;
  46: 
  47: /**
  48:  * The server's Diffie-Hellman parameters message.
  49:  *
  50:  * <pre>
  51: struct
  52: {
  53:   opaque dh_p&lt;1..2^16-1&gt;;
  54:   opaque dh_g&lt;1..2^16-1&gt;;
  55:   opaque dh_Ys&lt;1..2^16-1&gt;;
  56: } ServerDHParams;
  57: </pre>
  58:  */
  59: public class ServerDHParams implements Builder, ServerKeyExchangeParams
  60: {
  61:   private final ByteBuffer buffer;
  62: 
  63:   public ServerDHParams (final ByteBuffer buffer)
  64:   {
  65:     this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
  66:   }
  67: 
  68:   public ServerDHParams (final BigInteger p, final BigInteger g,
  69:                          final BigInteger y)
  70:   {
  71:     byte[] p_bytes = p.toByteArray();
  72:     byte[] g_bytes = g.toByteArray();
  73:     byte[] y_bytes = y.toByteArray();
  74:     int len = p_bytes.length + g_bytes.length + y_bytes.length + 6;
  75: 
  76:     int p_off = 0;
  77:     if (p_bytes[0] == 0x00)
  78:       {
  79:         p_off = 1;
  80:         len--;
  81:       }
  82:     int g_off = 0;
  83:     if (g_bytes[0] == 0x00)
  84:       {
  85:         g_off = 1;
  86:         len--;
  87:       }
  88:     int y_off = 0;
  89:     if (y_bytes[0] == 0x00)
  90:       {
  91:         y_off = 1;
  92:         len--;
  93:       }
  94:     int p_len = p_bytes.length - p_off;
  95:     int g_len = g_bytes.length - g_off;
  96:     int y_len = y_bytes.length - y_off;
  97: 
  98:     buffer = ByteBuffer.allocate(len);
  99:     buffer.putShort((short) p_len);
 100:     buffer.put(p_bytes, p_off, p_len);
 101:     buffer.putShort((short) g_len);
 102:     buffer.put(g_bytes, g_off, g_len);
 103:     buffer.putShort((short) y_len);
 104:     buffer.put(y_bytes, y_off, y_len);
 105:   }
 106: 
 107:   @Deprecated public KeyExchangeAlgorithm algorithm ()
 108:   {
 109:     return null; // XXX can't support this.
 110:   }
 111: 
 112:   public int length ()
 113:   {
 114:     int offset1 = buffer.getShort (0) & 0xFFFF;
 115:     int offset2 = buffer.getShort (offset1 + 2) & 0xFFFF;
 116:     return ((buffer.getShort (offset1 + offset2 + 4) & 0xFFFF)
 117:             + offset1 + offset2 + 6);
 118:   }
 119: 
 120:   public ByteBuffer buffer()
 121:   {
 122:     return (ByteBuffer) buffer.duplicate().position(0).limit(length());
 123:   }
 124: 
 125:   /**
 126:    * Returns the server's prime modulus.
 127:    *
 128:    * @return p.
 129:    */
 130:   public BigInteger p ()
 131:   {
 132:     int len = buffer.getShort (0) & 0xFFFF;
 133:     byte[] buf = new byte[len];
 134:     buffer.position (2);
 135:     buffer.get (buf);
 136:     return new BigInteger (1, buf);
 137:   }
 138: 
 139:   /**
 140:    * Returns the server's generator value.
 141:    *
 142:    * @return g.
 143:    */
 144:   public BigInteger g ()
 145:   {
 146:     int off = (buffer.getShort (0) & 0xFFFF) + 2;
 147:     int len = buffer.getShort (off) & 0xFFFF;
 148:     byte[] buf = new byte[len];
 149:     buffer.position (off + 2);
 150:     buffer.get (buf);
 151:     return new BigInteger (1, buf);
 152:   }
 153: 
 154:   /**
 155:    * Returns the server's public value.
 156:    *
 157:    * @return Y.
 158:    */
 159:   public BigInteger y ()
 160:   {
 161:     int offset1 = (buffer.getShort (0) & 0xFFFF) + 2;
 162:     int offset2 = (buffer.getShort (offset1) & 0xFFFF) + offset1 + 2;
 163:     int len = buffer.getShort (offset2) & 0xFFFF;
 164:     byte[] buf = new byte[len];
 165:     buffer.position (offset2 + 2);
 166:     buffer.get (buf);
 167:     return new BigInteger (1, buf);
 168:   }
 169: 
 170:   /**
 171:    * Sets the server's prime modulus, p.
 172:    *
 173:    * @param p The p parameter.
 174:    * @throws java.nio.ReadOnlyBufferException If the underlying buffer
 175:    * is not writeable.
 176:    */
 177:   public void setP (final BigInteger p)
 178:   {
 179:     byte[] buf = p.toByteArray ();
 180:     int length = (buf[0] == 0x00 ? buf.length - 1 : buf.length);
 181:     int offset = (buf[0] == 0x00 ? 1 : 0);
 182:     buffer.putShort (0, (short) length);
 183:     buffer.position (2);
 184:     buffer.put (buf, offset, length);
 185:   }
 186: 
 187:   /**
 188:    * Sets the server's generator value, g.
 189:    *
 190:    * @param g The g parameter.
 191:    * @throws java.nio.ReadOnlyBufferException If the underlying buffer
 192:    * is not writeable.
 193:    */
 194:   public void setG (final BigInteger g)
 195:   {
 196:     byte[] buf = g.toByteArray ();
 197:     int length = (buf[0] == 0x00 ? buf.length -1 : buf.length);
 198:     int offset = (buf[0] == 0x00 ? 1 : 0);
 199:     int where = (buffer.getShort (0) & 0xFFFF) + 2;
 200:     buffer.putShort (where, (short) length);
 201:     buffer.position (where + 2);
 202:     buffer.put (buf, offset, length);
 203:   }
 204: 
 205:   /**
 206:    * Sets the server's public value, Y.
 207:    *
 208:    * @param y The Y parameter.
 209:    * @throws java.nio.ReadOnlyBufferException If the underlying buffer
 210:    * is not writeable.
 211:    */
 212:   public void setY (final BigInteger y)
 213:   {
 214:     int offset1 = (buffer.getShort (0) & 0xFFFF) + 2;
 215:     int offset2 = (buffer.getShort (offset1) & 0xFFFF) + offset1 + 2;
 216:     byte[] buf = y.toByteArray ();
 217:     int length = (buf[0] == 0x00 ? buf.length -1 : buf.length);
 218:     int offset = (buf[0] == 0x00 ? 1 : 0);
 219:     buffer.putShort (offset2, (short) length);
 220:     buffer.position (offset2 + 2);
 221:     buffer.put (buf, offset, length);
 222:   }
 223: 
 224:   public String toString ()
 225:   {
 226:     return toString (null);
 227:   }
 228: 
 229:   public String toString (final String prefix)
 230:   {
 231:     StringWriter str = new StringWriter ();
 232:     PrintWriter out = new PrintWriter (str);
 233:     if (prefix != null) out.print (prefix);
 234:     out.println ("struct {");
 235:     if (prefix != null) out.print (prefix);
 236:     out.print ("  dh_p:  ");
 237:     out.println (p ().toString (16));
 238:     if (prefix != null) out.print (prefix);
 239:     out.print ("  dh_g:  ");
 240:     out.println (g ().toString (16));
 241:     if (prefix != null) out.print (prefix);
 242:     out.print ("  dh_Ys: ");
 243:     out.println (y ().toString (16));
 244:     if (prefix != null) out.print (prefix);
 245:     out.print ("} ServerDHParams;");
 246:     return str.toString ();
 247:   }
 248: }