1 |
/* GnuDSAPublicKey.java --- Gnu DSA Public Key |
/* GnuDSAPublicKey.java --- Gnu DSA Public Key |
2 |
Copyright (C) 1999,2003 Free Software Foundation, Inc. |
Copyright (C) 1999,2003,2004 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
7 |
it under the terms of the GNU General Public License as published by |
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) |
the Free Software Foundation; either version 2, or (at your option) |
9 |
any later version. |
any later version. |
10 |
|
|
11 |
GNU Classpath is distributed in the hope that it will be useful, but |
GNU Classpath is distributed in the hope that it will be useful, but |
12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
38 |
|
|
39 |
package gnu.java.security.provider; |
package gnu.java.security.provider; |
40 |
|
|
41 |
|
import gnu.java.security.OID; |
42 |
|
import gnu.java.security.der.BitString; |
43 |
|
import gnu.java.security.der.DER; |
44 |
|
import gnu.java.security.der.DERValue; |
45 |
|
import gnu.java.security.der.DERWriter; |
46 |
|
|
47 |
|
import java.io.ByteArrayOutputStream; |
48 |
|
import java.io.IOException; |
49 |
|
|
50 |
import java.math.BigInteger; |
import java.math.BigInteger; |
51 |
|
|
52 |
import java.security.interfaces.DSAPublicKey; |
import java.security.interfaces.DSAPublicKey; |
53 |
import java.security.interfaces.DSAParams; |
import java.security.interfaces.DSAParams; |
54 |
import java.security.spec.DSAParameterSpec; |
import java.security.spec.DSAParameterSpec; |
55 |
|
|
56 |
|
import java.util.ArrayList; |
57 |
|
|
58 |
public class GnuDSAPublicKey implements DSAPublicKey |
public class GnuDSAPublicKey implements DSAPublicKey |
59 |
{ |
{ |
60 |
|
private byte[] encodedKey; |
61 |
BigInteger y; |
BigInteger y; |
62 |
BigInteger p; |
BigInteger p; |
63 |
BigInteger q; |
BigInteger q; |
64 |
BigInteger g; |
BigInteger g; |
65 |
|
|
66 |
public GnuDSAPublicKey(BigInteger y, BigInteger p, BigInteger q, BigInteger g ) |
public GnuDSAPublicKey(BigInteger y, BigInteger p, BigInteger q, BigInteger g ) |
67 |
{ |
{ |
68 |
this.y = y; |
this.y = y; |
69 |
this.p = p; |
this.p = p; |
78 |
|
|
79 |
public String getFormat() |
public String getFormat() |
80 |
{ |
{ |
81 |
return null; |
return "X.509"; |
82 |
} |
} |
83 |
|
|
84 |
|
/** |
85 |
|
* The encoded form of DSA public keys is: |
86 |
|
* |
87 |
|
* <blockquote><pre> |
88 |
|
* SubjectPublicKeyInfo ::= SEQUENCE { |
89 |
|
* algorithm AlgorithmIdentifier, |
90 |
|
* subjectPublicKey BIT STRING } |
91 |
|
* </pre></blockquote> |
92 |
|
*/ |
93 |
public byte[] getEncoded() |
public byte[] getEncoded() |
94 |
{ |
{ |
95 |
return null; |
if (encodedKey != null) |
96 |
|
return (byte[]) encodedKey.clone(); |
97 |
|
try |
98 |
|
{ |
99 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
100 |
|
ArrayList spki = new ArrayList(2); |
101 |
|
ArrayList alg = new ArrayList(2); |
102 |
|
alg.add(new DERValue(DER.OBJECT_IDENTIFIER, |
103 |
|
new OID("1.2.840.113549.1.1.1"))); |
104 |
|
ArrayList params = new ArrayList(3); |
105 |
|
params.add(new DERValue(DER.INTEGER, p)); |
106 |
|
params.add(new DERValue(DER.INTEGER, q)); |
107 |
|
params.add(new DERValue(DER.INTEGER, g)); |
108 |
|
alg.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, params)); |
109 |
|
spki.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, alg)); |
110 |
|
spki.add(new DERValue(DER.BIT_STRING, new BitString(y.toByteArray()))); |
111 |
|
DERWriter.write(out, new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, spki)); |
112 |
|
return (byte[]) (encodedKey = out.toByteArray()).clone(); |
113 |
|
} |
114 |
|
catch (IOException ioe) |
115 |
|
{ |
116 |
|
return null; |
117 |
|
} |
118 |
} |
} |
119 |
|
|
120 |
public DSAParams getParams() |
public DSAParams getParams() |
121 |
{ |
{ |
122 |
|
if (p == null || q == null || g == null) |
123 |
|
return null; |
124 |
return (DSAParams)(new DSAParameterSpec(p,q,g)); |
return (DSAParams)(new DSAParameterSpec(p,q,g)); |
125 |
} |
} |
126 |
|
|
131 |
|
|
132 |
public String toString() |
public String toString() |
133 |
{ |
{ |
134 |
return "GnuDSAPublicKey: y=" + y.toString(16) + " p=" + p.toString(16) |
return |
135 |
+ " q=" + q.toString(16) + " g=" + g.toString(16); |
"GnuDSAPublicKey: y=" + (y != null ? y.toString(16) : "(null)") + |
136 |
|
" p=" + (p != null ? p.toString(16) : "(null)") + |
137 |
|
" q=" + (q != null ? q.toString(16) : "(null)") + |
138 |
|
" g=" + (g != null ? g.toString(16) : "(null)"); |
139 |
} |
} |
140 |
} |
} |