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

   1: /* ServerRSAParams.java -- The server's RSA 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: 
  46: /**
  47:  * The ServerRSAParams structure.
  48:  *
  49:  * <pre>
  50: struct
  51: {
  52:   opaque rsa_modulus&lt;1..2^16-1&gt;;
  53:   opaque rsa_exponent&lt;1..2^16-1&gt;;
  54: } ServerRSAParams;
  55: </pre>
  56:  */
  57: public class ServerRSAParams implements ServerKeyExchangeParams
  58: {
  59: 
  60:   private final ByteBuffer buffer;
  61: 
  62:   public ServerRSAParams (final ByteBuffer buffer)
  63:   {
  64:     this.buffer = buffer;
  65:   }
  66: 
  67:   public KeyExchangeAlgorithm algorithm ()
  68:   {
  69:     return KeyExchangeAlgorithm.RSA;
  70:   }
  71: 
  72:   public int length ()
  73:   {
  74:     int offset = buffer.getShort (0) & 0xFFFF;
  75:     return (buffer.getShort (offset + 2) & 0xFFFF) + offset + 4;
  76:   }
  77: 
  78:   /**
  79:    * Gets the modulus field.
  80:    *
  81:    * @return The modulus.
  82:    */
  83:   public BigInteger modulus ()
  84:   {
  85:     int len = buffer.getShort (0) & 0xFFFF;
  86:     byte[] buf = new byte[len];
  87:     buffer.position (2);
  88:     buffer.get (buf);
  89:     return new BigInteger (1, buf);
  90:   }
  91: 
  92:   /**
  93:    * Returns the exponent field.
  94:    *
  95:    * @return The exponent.
  96:    */
  97:   public BigInteger exponent ()
  98:   {
  99:     int off = (buffer.getShort (0) & 0xFFFF) + 2;
 100:     int len = buffer.getShort (off) & 0xFFFF;
 101:     byte[] buf = new byte[len];
 102:     buffer.position (off + 2);
 103:     buffer.get (buf);
 104:     return new BigInteger (1, buf);
 105:   }
 106: 
 107:   /**
 108:    * Sets the modulus.
 109:    *
 110:    * @param modulus The modulus.
 111:    * @throws java.nio.ReadOnlyBufferException If the underlying buffer
 112:    * is not writable.
 113:    */
 114:   public void setModulus (final BigInteger modulus)
 115:   {
 116:     byte[] buf = modulus.toByteArray ();
 117:     int length = (buf[0] == 0x00 ? buf.length - 1 : buf.length);
 118:     int offset = (buf[0] == 0x00 ? 1 : 0);
 119:     buffer.putShort (0, (short) length);
 120:     buffer.position (2);
 121:     buffer.put (buf, offset, length);
 122:   }
 123: 
 124:   /**
 125:    * Sets the exponent.
 126:    *
 127:    * @param exponent The exponent.
 128:    * @throws java.nio.ReadOnlyBufferException If the underlying buffer
 129:    * is not writeable.
 130:    */
 131:   public void setExponent (final BigInteger exponent)
 132:   {
 133:     byte[] buf = exponent.toByteArray ();
 134:     int length = (buf[0] == 0x00 ? buf.length -1 : buf.length);
 135:     int offset = (buf[0] == 0x00 ? 1 : 0);
 136:     int where = (buffer.getShort (0) & 0xFFFF) + 2;
 137:     buffer.putShort (where, (short) length);
 138:     buffer.position (where + 2);
 139:     buffer.put (buf, offset, length);
 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 ("  rsa_modulus:  ");
 155:     out.println (modulus ().toString (16));
 156:     if (prefix != null) out.print (prefix);
 157:     out.print ("  rsa_exponent: ");
 158:     out.println (exponent ());
 159:     if (prefix != null) out.print (prefix);
 160:     out.print ("} ServerRSAParams;");
 161:     return str.toString ();
 162:   }
 163: }