1 /* DSAParameters.java --- DSA Parameters Implementation
2 Copyright (C) 1999,2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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, or (at your option)
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.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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
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. */
39 package gnu
.java
.security
.provider
;
41 import java
.io
.ByteArrayInputStream
;
42 import java
.io
.ByteArrayOutputStream
;
43 import java
.io
.IOException
;
45 import java
.math
.BigInteger
;
47 import java
.security
.AlgorithmParametersSpi
;
48 import java
.security
.InvalidAlgorithmParameterException
;
49 import java
.security
.spec
.AlgorithmParameterSpec
;
50 import java
.security
.spec
.DSAParameterSpec
;
51 import java
.security
.spec
.InvalidParameterSpecException
;
53 import java
.util
.ArrayList
;
54 import java
.util
.List
;
55 import java
.util
.Random
;
57 import gnu
.java
.io
.ASN1ParsingException
;
58 import gnu
.java
.security
.der
.DER
;
59 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
;
64 import gnu
.java
.security
.util
.Prime
;
67 ASN.1 Encoding for DSA from rfc2459
69 id-dsa ID ::= { iso(1) member-body(2) us(840) x9-57(10040)
72 Dss-Parms ::= SEQUENCE {
78 public class DSAParameters
extends AlgorithmParametersSpi
80 private BigInteger q
; // the small prime
81 private BigInteger p
; // the big prime
85 public void engineInit(AlgorithmParameterSpec paramSpec
)
86 throws InvalidParameterSpecException
88 if( paramSpec
instanceof DSAParameterSpec
) {
89 DSAParameterSpec dsaParamSpec
= (DSAParameterSpec
)paramSpec
;
90 p
= dsaParamSpec
.getP();
91 q
= dsaParamSpec
.getQ();
92 g
= dsaParamSpec
.getG();
95 throw new InvalidParameterSpecException("Only accepts DSAParameterSpec");
98 public void engineInit(byte[] params
)
101 DERReader in
= new DERReader(params
);
102 DERValue val
= in
.read();
103 if (val
.getValue() != DER
.CONSTRUCTED_VALUE
)
104 throw new ASN1ParsingException("badly formed parameters");
107 p
= (BigInteger
) in
.read().getValue();
108 q
= (BigInteger
) in
.read().getValue();
109 g
= (BigInteger
) in
.read().getValue();
113 throw new ASN1ParsingException("badly formed parameters");
117 public void engineInit(byte[] params
, String format
)
120 if( !format
.equals("ASN.1") )
121 throw new IOException("Invalid Format: Only accepts ASN.1");
122 engineInit( params
);
125 public AlgorithmParameterSpec
engineGetParameterSpec(Class paramSpec
)
126 throws InvalidParameterSpecException
128 if( paramSpec
.isAssignableFrom(DSAParameterSpec
.class) )
129 return new DSAParameterSpec(p
, q
, g
);
131 throw new InvalidParameterSpecException("Only accepts DSAParameterSpec");
134 public byte[] engineGetEncoded()
137 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
138 ArrayList seq
= new ArrayList(3);
139 seq
.add(new DERValue(DER
.INTEGER
, p
));
140 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();
147 public byte[] engineGetEncoded(String format
)
150 if( !format
.equals("ASN.1") )
151 throw new IOException("Invalid Format: Only accepts ASN.1");
152 return engineGetEncoded();
155 public String
engineToString()
157 return ("q: " + q
+ " p: " + p
+ " g: " + g
);