001: /*
002: * @(#)DSAParameters.java 1.19 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.AlgorithmParametersSpi;
034: import java.security.spec.AlgorithmParameterSpec;
035: import java.security.spec.DSAParameterSpec;
036: import java.security.spec.InvalidParameterSpecException;
037:
038: import sun.security.util.Debug;
039: import sun.security.util.DerValue;
040: import sun.security.util.DerOutputStream;
041:
042: /**
043: * This class implements the parameter set used by the
044: * Digital Signature Algorithm as specified in the FIPS 186
045: * standard.
046: *
047: * @author Jan Luehe
048: *
049: * @version 1.13, 02/02/00
050: *
051: * @since JDK1.2
052: */
053:
054: public class DSAParameters extends AlgorithmParametersSpi {
055:
056: // the prime (p)
057: protected BigInteger p;
058:
059: // the sub-prime (q)
060: protected BigInteger q;
061:
062: // the base (g)
063: protected BigInteger g;
064:
065: protected void engineInit(AlgorithmParameterSpec paramSpec)
066: throws InvalidParameterSpecException {
067: if (!(paramSpec instanceof DSAParameterSpec)) {
068: throw new InvalidParameterSpecException(
069: "Inappropriate parameter specification");
070: }
071: this .p = ((DSAParameterSpec) paramSpec).getP();
072: this .q = ((DSAParameterSpec) paramSpec).getQ();
073: this .g = ((DSAParameterSpec) paramSpec).getG();
074: }
075:
076: protected void engineInit(byte[] params) throws IOException {
077: DerValue encodedParams = new DerValue(params);
078:
079: if (encodedParams.tag != DerValue.tag_Sequence) {
080: throw new IOException("DSA params parsing error");
081: }
082:
083: encodedParams.data.reset();
084:
085: this .p = encodedParams.data.getBigInteger();
086: this .q = encodedParams.data.getBigInteger();
087: this .g = encodedParams.data.getBigInteger();
088:
089: if (encodedParams.data.available() != 0) {
090: throw new IOException("encoded params have "
091: + encodedParams.data.available() + " extra bytes");
092: }
093: }
094:
095: protected void engineInit(byte[] params, String decodingMethod)
096: throws IOException {
097: engineInit(params);
098: }
099:
100: protected AlgorithmParameterSpec engineGetParameterSpec(
101: Class paramSpec) throws InvalidParameterSpecException {
102: try {
103: Class dsaParamSpec = Class
104: .forName("java.security.spec.DSAParameterSpec");
105: if (dsaParamSpec.isAssignableFrom(paramSpec)) {
106: return new DSAParameterSpec(this .p, this .q, this .g);
107: } else {
108: throw new InvalidParameterSpecException(
109: "Inappropriate parameter Specification");
110: }
111: } catch (ClassNotFoundException e) {
112: throw new InvalidParameterSpecException(
113: "Unsupported parameter specification: "
114: + e.getMessage());
115: }
116: }
117:
118: protected byte[] engineGetEncoded() throws IOException {
119: DerOutputStream out = new DerOutputStream();
120: DerOutputStream bytes = new DerOutputStream();
121:
122: bytes.putInteger(p);
123: bytes.putInteger(q);
124: bytes.putInteger(g);
125: out.write(DerValue.tag_Sequence, bytes);
126: return out.toByteArray();
127: }
128:
129: protected byte[] engineGetEncoded(String encodingMethod)
130: throws IOException {
131: return engineGetEncoded();
132: }
133:
134: /*
135: * Returns a formatted string describing the parameters.
136: */
137: protected String engineToString() {
138: return "\n\tp: " + Debug.toHexString(p) + "\n\tq: "
139: + Debug.toHexString(q) + "\n\tg: "
140: + Debug.toHexString(g) + "\n";
141: }
142: }
|