Source for gnu.java.security.key.dss.DSSKey

   1: /* DSSKey.java -- 
   2:    Copyright 2001, 2002, 2003, 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.java.security.key.dss;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.action.GetPropertyAction;
  43: import gnu.java.security.util.FormatUtil;
  44: 
  45: import java.math.BigInteger;
  46: import java.security.AccessController;
  47: import java.security.Key;
  48: import java.security.interfaces.DSAKey;
  49: import java.security.interfaces.DSAParams;
  50: import java.security.spec.DSAParameterSpec;
  51: 
  52: /**
  53:  * A base asbtract class for both public and private DSS (Digital Signature
  54:  * Standard) keys. It encapsulates the three DSS numbers: <code>p</code>,
  55:  * <code>q</code> and <code>g</code>.
  56:  * <p>
  57:  * According to the JDK, cryptographic <i>Keys</i> all have a <i>format</i>.
  58:  * The format used in this implementation is called <i>Raw</i>, and basically
  59:  * consists of the raw byte sequences of algorithm parameters. The exact order
  60:  * of the byte sequences and the implementation details are given in each of the
  61:  * relevant <code>getEncoded()</code> methods of each of the private and
  62:  * public keys.
  63:  * <p>
  64:  * <b>IMPORTANT</b>: Under certain circumstances (e.g. in an X.509 certificate
  65:  * with inherited AlgorithmIdentifier's parameters of a SubjectPublicKeyInfo
  66:  * element) these three MPIs may be <code>null</code>.
  67:  * 
  68:  * @see DSSPrivateKey#getEncoded
  69:  * @see DSSPublicKey#getEncoded
  70:  */
  71: public abstract class DSSKey
  72:     implements Key, DSAKey
  73: {
  74:   /**
  75:    * A prime modulus, where
  76:    * <code>2<sup>L-1</sup> &lt; p &lt; 2<sup>L</sup></code> for
  77:    * <code>512 &lt;= L &lt;= 1024</code> and <code>L</code> a multiple of
  78:    * <code>64</code>.
  79:    */
  80:   protected final BigInteger p;
  81: 
  82:   /**
  83:    * A prime divisor of <code>p - 1</code>, where
  84:    * <code>2<sup>159</sup> &lt; q
  85:    * &lt; 2<sup>160</sup></code>.
  86:    */
  87:   protected final BigInteger q;
  88: 
  89:   /**
  90:    * <code>g = h<sup>(p-1)</sup>/q mod p</code>, where <code>h</code> is
  91:    * any integer with <code>1 &lt; h &lt; p - 1</code> such that <code>h<sup>
  92:    * (p-1)</sup>/q mod p > 1</code> (<code>g</code>
  93:    * has order <code>q mod p
  94:    * </code>).
  95:    */
  96:   protected final BigInteger g;
  97: 
  98:   /**
  99:    * Identifier of the default encoding format to use when externalizing the key
 100:    * material.
 101:    */
 102:   protected final int defaultFormat;
 103: 
 104:   /** String representation of this key. Cached for speed. */
 105:   private transient String str;
 106: 
 107:   /**
 108:    * Trivial protected constructor.
 109:    * 
 110:    * @param defaultFormat the identifier of the encoding format to use by
 111:    *          default when externalizing the key.
 112:    * @param p the DSS parameter <code>p</code>.
 113:    * @param q the DSS parameter <code>q</code>.
 114:    * @param g the DSS parameter <code>g</code>.
 115:    */
 116:   protected DSSKey(int defaultFormat, BigInteger p, BigInteger q, BigInteger g)
 117:   {
 118:     super();
 119: 
 120:     this.defaultFormat = defaultFormat <= 0 ? Registry.RAW_ENCODING_ID
 121:                                             : defaultFormat;
 122:     this.p = p;
 123:     this.q = q;
 124:     this.g = g;
 125:   }
 126: 
 127:   public DSAParams getParams()
 128:   {
 129:     return new DSAParameterSpec(p, q, g);
 130:   }
 131: 
 132:   public String getAlgorithm()
 133:   {
 134:     return Registry.DSS_KPG;
 135:   }
 136: 
 137:   /** @deprecated see getEncoded(int). */
 138:   public byte[] getEncoded()
 139:   {
 140:     return getEncoded(defaultFormat);
 141:   }
 142: 
 143:   public String getFormat()
 144:   {
 145:     return FormatUtil.getEncodingShortName(defaultFormat);
 146:   }
 147: 
 148:   /**
 149:    * Returns <code>true</code> if the designated object is an instance of
 150:    * {@link DSAKey} and has the same DSS (Digital Signature Standard) parameter
 151:    * values as this one.
 152:    * <p>
 153:    * Always returns <code>false</code> if the MPIs of this key are
 154:    * <i>inherited</i>. This may be the case when the key is re-constructed from
 155:    * an X.509 certificate with absent or NULL AlgorithmIdentifier's parameters
 156:    * field.
 157:    * 
 158:    * @param obj the other non-null DSS key to compare to.
 159:    * @return <code>true</code> if the designated object is of the same type
 160:    *         and value as this one.
 161:    */
 162:   public boolean equals(Object obj)
 163:   {
 164:     if (hasInheritedParameters())
 165:       return false;
 166: 
 167:     if (obj == null)
 168:       return false;
 169: 
 170:     if (! (obj instanceof DSAKey))
 171:       return false;
 172: 
 173:     DSAKey that = (DSAKey) obj;
 174:     return p.equals(that.getParams().getP())
 175:            && q.equals(that.getParams().getQ())
 176:            && g.equals(that.getParams().getG());
 177:   }
 178: 
 179:   public String toString()
 180:   {
 181:     if (str == null)
 182:       {
 183:         String ls = (String) AccessController.doPrivileged(new GetPropertyAction("line.separator"));
 184:         StringBuilder sb = new StringBuilder(ls)
 185:             .append("defaultFormat=").append(defaultFormat).append(",")
 186:             .append(ls);
 187:         if (hasInheritedParameters())
 188:           sb.append("p=inherited,").append(ls)
 189:               .append("q=inherited,").append(ls)
 190:               .append("g=inherited");
 191:         else
 192:           sb.append("p=0x").append(p.toString(16)).append(",").append(ls)
 193:               .append("q=0x").append(q.toString(16)).append(",").append(ls)
 194:               .append("g=0x").append(g.toString(16));
 195:         str = sb.toString();
 196:       }
 197:     return str;
 198:   }
 199: 
 200:   public abstract byte[] getEncoded(int format);
 201: 
 202:   /**
 203:    * @return <code>true</code> if <code>p</code>, <code>q</code> and
 204:    *         <code>g</code> are all <code>null</code>. Returns
 205:    *         <code>false</code> otherwise.
 206:    */
 207:   public boolean hasInheritedParameters()
 208:   {
 209:     return p == null && q == null && g == null;
 210:   }
 211: }