blob: 271daf5fe8d9c10f5cf91ac45f31d8dd39cd003f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.provider;
27
28import java.util.*;
29import java.io.*;
30import java.math.BigInteger;
31import java.security.AlgorithmParametersSpi;
32import java.security.spec.AlgorithmParameterSpec;
33import java.security.spec.DSAParameterSpec;
34import java.security.spec.InvalidParameterSpecException;
35
36import sun.security.util.Debug;
37import sun.security.util.DerValue;
38import sun.security.util.DerOutputStream;
39
40/**
41 * This class implements the parameter set used by the
42 * Digital Signature Algorithm as specified in the FIPS 186
43 * standard.
44 *
45 * @author Jan Luehe
46 *
47 *
48 * @since 1.2
49 */
50
51public class DSAParameters extends AlgorithmParametersSpi {
52
53 // the prime (p)
54 protected BigInteger p;
55
56 // the sub-prime (q)
57 protected BigInteger q;
58
59 // the base (g)
60 protected BigInteger g;
61
62 protected void engineInit(AlgorithmParameterSpec paramSpec)
63 throws InvalidParameterSpecException {
64 if (!(paramSpec instanceof DSAParameterSpec)) {
65 throw new InvalidParameterSpecException
66 ("Inappropriate parameter specification");
67 }
68 this.p = ((DSAParameterSpec)paramSpec).getP();
69 this.q = ((DSAParameterSpec)paramSpec).getQ();
70 this.g = ((DSAParameterSpec)paramSpec).getG();
71 }
72
73 protected void engineInit(byte[] params) throws IOException {
74 DerValue encodedParams = new DerValue(params);
75
76 if (encodedParams.tag != DerValue.tag_Sequence) {
77 throw new IOException("DSA params parsing error");
78 }
79
80 encodedParams.data.reset();
81
82 this.p = encodedParams.data.getBigInteger();
83 this.q = encodedParams.data.getBigInteger();
84 this.g = encodedParams.data.getBigInteger();
85
86 if (encodedParams.data.available() != 0) {
87 throw new IOException("encoded params have " +
88 encodedParams.data.available() +
89 " extra bytes");
90 }
91 }
92
93 protected void engineInit(byte[] params, String decodingMethod)
94 throws IOException {
95 engineInit(params);
96 }
97
98 protected <T extends AlgorithmParameterSpec>
99 T engineGetParameterSpec(Class<T> paramSpec)
100 throws InvalidParameterSpecException
101 {
102 try {
103 Class<?> dsaParamSpec = Class.forName
104 ("java.security.spec.DSAParameterSpec");
105 if (dsaParamSpec.isAssignableFrom(paramSpec)) {
106 return (T)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: " + e.getMessage());
114 }
115 }
116
117 protected byte[] engineGetEncoded() throws IOException {
118 DerOutputStream out = new DerOutputStream();
119 DerOutputStream bytes = new DerOutputStream();
120
121 bytes.putInteger(p);
122 bytes.putInteger(q);
123 bytes.putInteger(g);
124 out.write(DerValue.tag_Sequence, bytes);
125 return out.toByteArray();
126 }
127
128 protected byte[] engineGetEncoded(String encodingMethod)
129 throws IOException {
130 return engineGetEncoded();
131 }
132
133 /*
134 * Returns a formatted string describing the parameters.
135 */
136 protected String engineToString() {
137 return "\n\tp: " + Debug.toHexString(p)
138 + "\n\tq: " + Debug.toHexString(q)
139 + "\n\tg: " + Debug.toHexString(g)
140 + "\n";
141 }
142}