1 |
/* DSAParameters.java --- DSA Parameters Implementation |
/* DSAParameters.java --- DSA Parameters Implementation |
2 |
Copyright (C) 1999 Free Software Foundation, Inc. |
Copyright (C) 1999,2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
38 |
|
|
39 |
package gnu.java.security.provider; |
package gnu.java.security.provider; |
40 |
|
|
41 |
|
import java.io.ByteArrayInputStream; |
42 |
|
import java.io.ByteArrayOutputStream; |
43 |
import java.io.IOException; |
import java.io.IOException; |
44 |
|
|
45 |
import java.math.BigInteger; |
import java.math.BigInteger; |
46 |
|
|
47 |
import java.security.AlgorithmParametersSpi; |
import java.security.AlgorithmParametersSpi; |
48 |
import java.security.InvalidAlgorithmParameterException; |
import java.security.InvalidAlgorithmParameterException; |
49 |
import java.security.spec.AlgorithmParameterSpec; |
import java.security.spec.AlgorithmParameterSpec; |
50 |
import java.security.spec.DSAParameterSpec; |
import java.security.spec.DSAParameterSpec; |
51 |
import java.security.spec.InvalidParameterSpecException; |
import java.security.spec.InvalidParameterSpecException; |
52 |
|
|
53 |
|
import java.util.ArrayList; |
54 |
|
import java.util.List; |
55 |
import java.util.Random; |
import java.util.Random; |
56 |
|
|
57 |
|
import gnu.java.io.ASN1ParsingException; |
58 |
|
import gnu.java.security.der.DER; |
59 |
import gnu.java.security.der.DEREncodingException; |
import gnu.java.security.der.DEREncodingException; |
60 |
|
import gnu.java.security.der.DERReader; |
61 |
|
import gnu.java.security.der.DERValue; |
62 |
|
import gnu.java.security.der.DERWriter; |
63 |
|
|
64 |
import gnu.java.security.util.Prime; |
import gnu.java.security.util.Prime; |
65 |
|
|
89 |
DSAParameterSpec dsaParamSpec = (DSAParameterSpec)paramSpec; |
DSAParameterSpec dsaParamSpec = (DSAParameterSpec)paramSpec; |
90 |
p = dsaParamSpec.getP(); |
p = dsaParamSpec.getP(); |
91 |
q = dsaParamSpec.getQ(); |
q = dsaParamSpec.getQ(); |
92 |
q = dsaParamSpec.getG(); |
g = dsaParamSpec.getG(); |
93 |
} |
} |
94 |
else |
else |
95 |
throw new InvalidParameterSpecException("Only accepts DSAParameterSpec"); |
throw new InvalidParameterSpecException("Only accepts DSAParameterSpec"); |
98 |
public void engineInit(byte[] params) |
public void engineInit(byte[] params) |
99 |
throws IOException |
throws IOException |
100 |
{ |
{ |
101 |
DERReader reader = new DERReader( params ); |
DERReader in = new DERReader(params); |
102 |
try { |
DERValue val = in.read(); |
103 |
|
if (val.getValue() != DER.CONSTRUCTED_VALUE) |
104 |
p = reader.getBigInteger(); |
throw new ASN1ParsingException("badly formed parameters"); |
105 |
q = reader.getBigInteger(); |
try |
106 |
g = reader.getBigInteger(); |
{ |
107 |
|
p = (BigInteger) in.read().getValue(); |
108 |
} catch ( DEREncodingException DERee) { |
q = (BigInteger) in.read().getValue(); |
109 |
throw new IOException("Invalid Format: Only accepts ASN.1"); |
g = (BigInteger) in.read().getValue(); |
110 |
} |
} |
111 |
|
catch (Exception x) |
112 |
|
{ |
113 |
|
throw new ASN1ParsingException("badly formed parameters"); |
114 |
|
} |
115 |
} |
} |
116 |
|
|
117 |
public void engineInit(byte[] params, String format) |
public void engineInit(byte[] params, String format) |
134 |
public byte[] engineGetEncoded() |
public byte[] engineGetEncoded() |
135 |
throws IOException |
throws IOException |
136 |
{ |
{ |
137 |
DERWriter writer = new DERWriter(); |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
138 |
return writer.joinarrays( writer.writeBigInteger(p), |
ArrayList seq = new ArrayList(3); |
139 |
writer.writeBigInteger(q), |
seq.add(new DERValue(DER.INTEGER, p)); |
140 |
writer.writeBigInteger(g) ); |
seq.add(new DERValue(DER.INTEGER, q)); |
141 |
|
seq.add(new DERValue(DER.INTEGER, g)); |
142 |
|
DERWriter.write(bout, new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, seq)); |
143 |
|
return bout.toByteArray(); |
144 |
} |
} |
145 |
|
|
146 |
|
|
155 |
public String engineToString() |
public String engineToString() |
156 |
{ |
{ |
157 |
String lineSeparator = System.getProperty("line.seperator"); |
String lineSeparator = System.getProperty("line.seperator"); |
158 |
return ("q: " + q + lineSeparator + "p: " + p + lineSeparator + "g:" + g); |
return ("q: " + q + " p: " + p + " g: " + g); |
159 |
} |
} |
160 |
|
|
161 |
} |
} |