001: /*
002: * @(#)DSAPrivateKey.java 1.58 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.provider;
029:
030: import java.util.*;
031: import java.io.*;
032: import java.math.BigInteger;
033: import java.security.InvalidKeyException;
034: import java.security.ProviderException;
035: import java.security.AlgorithmParameters;
036: import java.security.spec.DSAParameterSpec;
037: import java.security.spec.InvalidParameterSpecException;
038: import java.security.interfaces.DSAParams;
039:
040: import sun.security.x509.AlgIdDSA;
041: import sun.security.pkcs.PKCS8Key;
042: import sun.security.util.Debug;
043: import sun.security.util.DerValue;
044: import sun.security.util.DerInputStream;
045: import sun.security.util.DerOutputStream;
046:
047: /**
048: * A PKCS#8 private key for the Digital Signature Algorithm.
049: *
050: * @author Benjamin Renaud
051: *
052: * @version 1.52, 02/02/00
053: *
054: * @see DSAPublicKey
055: * @see AlgIdDSA
056: * @see DSA
057: */
058:
059: public final class DSAPrivateKey extends PKCS8Key implements
060: java.security.interfaces.DSAPrivateKey, Serializable {
061:
062: /** use serialVersionUID from JDK 1.1. for interoperability */
063: private static final long serialVersionUID = -3244453684193605938L;
064:
065: /* the private key */
066: private BigInteger x;
067:
068: /*
069: * Keep this constructor for backwards compatibility with JDK1.1.
070: */
071: public DSAPrivateKey() {
072: }
073:
074: /**
075: * Make a DSA private key out of a private key and three parameters.
076: */
077: public DSAPrivateKey(BigInteger x, BigInteger p, BigInteger q,
078: BigInteger g) throws InvalidKeyException {
079: this .x = x;
080: algid = new AlgIdDSA(p, q, g);
081:
082: try {
083: key = new DerValue(DerValue.tag_Integer, x.toByteArray())
084: .toByteArray();
085: encode();
086: } catch (IOException e) {
087: InvalidKeyException ike = new InvalidKeyException(
088: "could not DER encode x: " + e.getMessage());
089: ike.initCause(e);
090: throw ike;
091: }
092: }
093:
094: /**
095: * Make a DSA private key from its DER encoding (PKCS #8).
096: */
097: public DSAPrivateKey(byte[] encoded) throws InvalidKeyException {
098: clearOldKey();
099: decode(encoded);
100: }
101:
102: /**
103: * Returns the DSA parameters associated with this key, or null if the
104: * parameters could not be parsed.
105: */
106: public DSAParams getParams() {
107: try {
108: if (algid instanceof DSAParams) {
109: return (DSAParams) algid;
110: } else {
111: DSAParameterSpec paramSpec;
112: AlgorithmParameters algParams = algid.getParameters();
113: if (algParams == null) {
114: return null;
115: }
116: paramSpec = (DSAParameterSpec) algParams
117: .getParameterSpec(DSAParameterSpec.class);
118: return (DSAParams) paramSpec;
119: }
120: } catch (InvalidParameterSpecException e) {
121: return null;
122: }
123: }
124:
125: /**
126: * Get the raw private key, x, without the parameters.
127: *
128: * @see getParameters
129: */
130: public BigInteger getX() {
131: return x;
132: }
133:
134: private void clearOldKey() {
135: int i;
136: if (this .encodedKey != null) {
137: for (i = 0; i < this .encodedKey.length; i++) {
138: this .encodedKey[i] = (byte) 0x00;
139: }
140: }
141: if (this .key != null) {
142: for (i = 0; i < this .key.length; i++) {
143: this .key[i] = (byte) 0x00;
144: }
145: }
146: }
147:
148: public String toString() {
149: return "Sun DSA Private Key \nparameters:" + algid + "\nx: "
150: + Debug.toHexString(x) + "\n";
151: }
152:
153: protected void parseKeyBits() throws InvalidKeyException {
154: try {
155: DerInputStream in = new DerInputStream(key);
156: x = in.getBigInteger();
157: } catch (IOException e) {
158: InvalidKeyException ike = new InvalidKeyException(e
159: .getMessage());
160: ike.initCause(e);
161: throw ike;
162: }
163: }
164: }
|