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