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