1 |
/* GnuDSAPrivateKey.java --- Gnu DSA Private Key |
/* GnuDSAPrivateKey.java --- Gnu DSA Private Key |
2 |
Copyright (C) 1999 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.DER; |
43 |
|
import gnu.java.security.der.DERValue; |
44 |
|
import gnu.java.security.der.DERWriter; |
45 |
|
|
46 |
|
import java.io.ByteArrayOutputStream; |
47 |
|
import java.io.IOException; |
48 |
|
|
49 |
import java.math.BigInteger; |
import java.math.BigInteger; |
50 |
|
|
51 |
import java.security.interfaces.DSAPrivateKey; |
import java.security.interfaces.DSAPrivateKey; |
52 |
import java.security.interfaces.DSAParams; |
import java.security.interfaces.DSAParams; |
53 |
import java.security.spec.DSAParameterSpec; |
import java.security.spec.DSAParameterSpec; |
54 |
|
|
55 |
|
import java.util.ArrayList; |
56 |
|
|
57 |
public class GnuDSAPrivateKey implements DSAPrivateKey |
public class GnuDSAPrivateKey implements DSAPrivateKey |
58 |
{ |
{ |
59 |
|
private byte[] encodedKey; |
60 |
BigInteger x; |
BigInteger x; |
61 |
BigInteger p; |
BigInteger p; |
62 |
BigInteger q; |
BigInteger q; |
63 |
BigInteger g; |
BigInteger g; |
64 |
|
|
65 |
public GnuDSAPrivateKey(BigInteger x, BigInteger p, BigInteger q, BigInteger g ) |
public GnuDSAPrivateKey(BigInteger x, BigInteger p, BigInteger q, BigInteger g ) |
66 |
{ |
{ |
67 |
this.x = x; |
this.x = x; |
68 |
this.p = p; |
this.p = p; |
77 |
|
|
78 |
public String getFormat() |
public String getFormat() |
79 |
{ |
{ |
80 |
return null; |
return "PKCS#8"; |
81 |
} |
} |
82 |
|
|
83 |
|
/** |
84 |
|
* Encodes this key as a <code>PrivateKeyInfo</code>, as described in |
85 |
|
* PKCS #8. The ASN.1 specification for this structure is: |
86 |
|
* |
87 |
|
* <blockquote><pre> |
88 |
|
* PrivateKeyInfo ::= SEQUENCE { |
89 |
|
* version Version, |
90 |
|
* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, |
91 |
|
* privateKey PrivateKey, |
92 |
|
* attributes [0] IMPLICIT Attributes OPTIONAL } |
93 |
|
* |
94 |
|
* Version ::= INTEGER |
95 |
|
* |
96 |
|
* PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier |
97 |
|
* |
98 |
|
* PrivateKey ::= OCTET STRING |
99 |
|
* |
100 |
|
* Attributes ::= SET OF Attribute |
101 |
|
* </pre></blockquote> |
102 |
|
* |
103 |
|
* <p>DSA private keys (in Classpath at least) have no attributes. |
104 |
|
*/ |
105 |
public byte[] getEncoded() |
public byte[] getEncoded() |
106 |
{ |
{ |
107 |
return null; |
if (encodedKey != null) |
108 |
|
return (byte[]) encodedKey.clone(); |
109 |
|
try |
110 |
|
{ |
111 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
112 |
|
ArrayList pki = new ArrayList(3); |
113 |
|
pki.add(new DERValue(DER.INTEGER, BigInteger.ZERO)); |
114 |
|
ArrayList algId = new ArrayList(2); |
115 |
|
algId.add(new DERValue(DER.OBJECT_IDENTIFIER, |
116 |
|
new OID("1.2.840.10040.4.1"))); |
117 |
|
ArrayList algParams = new ArrayList(3); |
118 |
|
algParams.add(new DERValue(DER.INTEGER, p)); |
119 |
|
algParams.add(new DERValue(DER.INTEGER, q)); |
120 |
|
algParams.add(new DERValue(DER.INTEGER, g)); |
121 |
|
algId.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, algParams)); |
122 |
|
pki.add(new DERValue(DER.OCTET_STRING, x.toByteArray())); |
123 |
|
DERWriter.write(out, new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, pki)); |
124 |
|
return (byte[]) (encodedKey = out.toByteArray()).clone(); |
125 |
|
} |
126 |
|
catch (IOException ioe) |
127 |
|
{ |
128 |
|
return null; |
129 |
|
} |
130 |
} |
} |
131 |
|
|
132 |
public DSAParams getParams() |
public DSAParams getParams() |
141 |
|
|
142 |
public String toString() |
public String toString() |
143 |
{ |
{ |
144 |
return "GnuDSAPrivateKey: x=" + x.toString(16) + " p=" + p.toString(16) |
return "GnuDSAPrivateKey: x=" |
145 |
+ " q=" + q.toString(16) + " g=" + g.toString(16); |
+ (x != null ? x.toString(16) : "null") + " p=" |
146 |
|
+ (p != null ? p.toString(16) : "null") + " q=" |
147 |
|
+ (q != null ? q.toString(16) : "null") + " g=" |
148 |
|
+ (g != null ? g.toString(16) : "null"); |
149 |
} |
} |
150 |
} |
} |