GtkTextAreaPeer.java (minimumSize): Removed unused variables hScrollbarHeight and...
[gcc.git] / libjava / gnu / java / security / provider / DSAParameters.java
1 /* DSAParameters.java --- DSA Parameters Implementation
2 Copyright (C) 1999,2003 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02111-1307 USA.
20
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
24 combination.
25
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. */
37
38
39 package gnu.java.security.provider;
40
41 import java.io.ByteArrayInputStream;
42 import java.io.ByteArrayOutputStream;
43 import java.io.IOException;
44
45 import java.math.BigInteger;
46
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;
52
53 import java.util.ArrayList;
54 import java.util.List;
55 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;
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;
65
66 /*
67 ASN.1 Encoding for DSA from rfc2459
68
69 id-dsa ID ::= { iso(1) member-body(2) us(840) x9-57(10040)
70 x9cm(4) 1 }
71
72 Dss-Parms ::= SEQUENCE {
73 p INTEGER,
74 q INTEGER,
75 g INTEGER }
76
77 */
78 public class DSAParameters extends AlgorithmParametersSpi
79 {
80 private BigInteger q; // the small prime
81 private BigInteger p; // the big prime
82 private BigInteger g;
83
84
85 public void engineInit(AlgorithmParameterSpec paramSpec)
86 throws InvalidParameterSpecException
87 {
88 if( paramSpec instanceof DSAParameterSpec ) {
89 DSAParameterSpec dsaParamSpec = (DSAParameterSpec)paramSpec;
90 p = dsaParamSpec.getP();
91 q = dsaParamSpec.getQ();
92 g = dsaParamSpec.getG();
93 }
94 else
95 throw new InvalidParameterSpecException("Only accepts DSAParameterSpec");
96 }
97
98 public void engineInit(byte[] params)
99 throws IOException
100 {
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");
105 try
106 {
107 p = (BigInteger) in.read().getValue();
108 q = (BigInteger) in.read().getValue();
109 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)
118 throws IOException
119 {
120 if( !format.equals("ASN.1") )
121 throw new IOException("Invalid Format: Only accepts ASN.1");
122 engineInit( params );
123 }
124
125 public AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
126 throws InvalidParameterSpecException
127 {
128 if( paramSpec.isAssignableFrom(DSAParameterSpec.class) )
129 return new DSAParameterSpec(p, q, g);
130 else
131 throw new InvalidParameterSpecException("Only accepts DSAParameterSpec");
132 }
133
134 public byte[] engineGetEncoded()
135 throws IOException
136 {
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();
144 }
145
146
147 public byte[] engineGetEncoded(String format)
148 throws IOException
149 {
150 if( !format.equals("ASN.1") )
151 throw new IOException("Invalid Format: Only accepts ASN.1");
152 return engineGetEncoded();
153 }
154
155 public String engineToString()
156 {
157 return ("q: " + q + " p: " + p + " g: " + g);
158 }
159
160 }