Frames | No Frames |
1: /* DSSPrivateKey.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.Configuration; 44: import gnu.java.security.Registry; 45: import gnu.java.security.action.GetPropertyAction; 46: import gnu.java.security.key.IKeyPairCodec; 47: 48: import java.math.BigInteger; 49: import java.security.AccessController; 50: import java.security.PrivateKey; 51: import java.security.interfaces.DSAPrivateKey; 52: 53: /** 54: * An object that embodies a DSS (Digital Signature Standard) private key. 55: * 56: * @see #getEncoded 57: */ 58: public class DSSPrivateKey 59: extends DSSKey 60: implements PrivateKey, DSAPrivateKey 61: { 62: /** 63: * A randomly or pseudorandomly generated integer with <code>0 < x < 64: * q</code>. 65: */ 66: private final BigInteger x; 67: 68: /** String representation of this key. Cached for speed. */ 69: private transient String str; 70: 71: /** 72: * Convenience constructor. Calls the constructor with 5 arguments passing 73: * {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred 74: * encoding format. 75: * 76: * @param p the public modulus. 77: * @param q the public prime divisor of <code>p-1</code>. 78: * @param g a generator of the unique cyclic group <code>Z<sup>*</sup> 79: * <sub>p</sub></code>. 80: * @param x the private key part. 81: */ 82: public DSSPrivateKey(BigInteger p, BigInteger q, BigInteger g, BigInteger x) 83: { 84: this(Registry.RAW_ENCODING_ID, p, q, g, x); 85: } 86: 87: /** 88: * Constructs a new instance of a <code>DSSPrivateKey</code> given the 89: * designated arguments. 90: * 91: * @param preferredFormat the indetifier of the preferred encoding format to 92: * use when externalizing this key. 93: * @param p the public modulus. 94: * @param q the public prime divisor of <code>p-1</code>. 95: * @param g a generator of the unique cyclic group <code>Z<sup>*</sup> 96: * <sub>p</sub></code>. 97: * @param x the private key part. 98: */ 99: public DSSPrivateKey(int preferredFormat, BigInteger p, BigInteger q, 100: BigInteger g, BigInteger x) 101: { 102: super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.PKCS8_ENCODING_ID 103: : preferredFormat, 104: p, q, g); 105: this.x = x; 106: } 107: 108: /** 109: * A class method that takes the output of the <code>encodePrivateKey()</code> 110: * method of a DSS keypair codec object (an instance implementing 111: * {@link gnu.java.security.key.IKeyPairCodec} for DSS keys, and re-constructs 112: * an instance of this object. 113: * 114: * @param k the contents of a previously encoded instance of this object. 115: * @exception ArrayIndexOutOfBoundsException if there is not enough bytes, in 116: * <code>k</code>, to represent a valid encoding of an 117: * instance of this object. 118: * @exception IllegalArgumentException if the byte sequence does not represent 119: * a valid encoding of an instance of this object. 120: */ 121: public static DSSPrivateKey valueOf(byte[] k) 122: { 123: // try RAW codec 124: if (k[0] == Registry.MAGIC_RAW_DSS_PRIVATE_KEY[0]) 125: try 126: { 127: return (DSSPrivateKey) new DSSKeyPairRawCodec().decodePrivateKey(k); 128: } 129: catch (IllegalArgumentException ignored) 130: { 131: } 132: // try PKCS#8 codec 133: return (DSSPrivateKey) new DSSKeyPairPKCS8Codec().decodePrivateKey(k); 134: } 135: 136: public BigInteger getX() 137: { 138: return x; 139: } 140: 141: /** 142: * Returns the encoded form of this private key according to the designated 143: * format. 144: * 145: * @param format the desired format identifier of the resulting encoding. 146: * @return the byte sequence encoding this key according to the designated 147: * format. 148: * @exception IllegalArgumentException if the format is not supported. 149: * @see DSSKeyPairRawCodec 150: */ 151: public byte[] getEncoded(int format) 152: { 153: byte[] result; 154: switch (format) 155: { 156: case IKeyPairCodec.RAW_FORMAT: 157: result = new DSSKeyPairRawCodec().encodePrivateKey(this); 158: break; 159: case IKeyPairCodec.PKCS8_FORMAT: 160: result = new DSSKeyPairPKCS8Codec().encodePrivateKey(this); 161: break; 162: default: 163: throw new IllegalArgumentException("Unsupported encoding format: " 164: + format); 165: } 166: return result; 167: } 168: 169: /** 170: * Returns <code>true</code> if the designated object is an instance of 171: * {@link DSAPrivateKey} and has the same DSS (Digital Signature Standard) 172: * parameter values as this one. 173: * 174: * @param obj the other non-null DSS key to compare to. 175: * @return <code>true</code> if the designated object is of the same type 176: * and value as this one. 177: */ 178: public boolean equals(Object obj) 179: { 180: if (obj == null) 181: return false; 182: 183: if (! (obj instanceof DSAPrivateKey)) 184: return false; 185: 186: DSAPrivateKey that = (DSAPrivateKey) obj; 187: return super.equals(that) && x.equals(that.getX()); 188: } 189: 190: public String toString() 191: { 192: if (str == null) 193: { 194: String ls = (String) AccessController.doPrivileged 195: (new GetPropertyAction("line.separator")); 196: str = new CPStringBuilder(this.getClass().getName()).append("(") 197: .append(super.toString()).append(",").append(ls) 198: .append("x=0x").append(Configuration.DEBUG ? x.toString(16) 199: : "**...*").append(ls) 200: .append(")") 201: .toString(); 202: } 203: return str; 204: } 205: }