diff options
Diffstat (limited to 'libjava/gnu/java/security/provider/GnuDSAPrivateKey.java')
| -rw-r--r-- | libjava/gnu/java/security/provider/GnuDSAPrivateKey.java | 67 |
1 files changed, 63 insertions, 4 deletions
diff --git a/libjava/gnu/java/security/provider/GnuDSAPrivateKey.java b/libjava/gnu/java/security/provider/GnuDSAPrivateKey.java index 4eceb00..e82483c 100644 --- a/libjava/gnu/java/security/provider/GnuDSAPrivateKey.java +++ b/libjava/gnu/java/security/provider/GnuDSAPrivateKey.java @@ -1,5 +1,5 @@ /* GnuDSAPrivateKey.java --- Gnu DSA Private Key - Copyright (C) 1999 Free Software Foundation, Inc. + Copyright (C) 1999,2003,2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -38,13 +38,25 @@ exception statement from your version. */ package gnu.java.security.provider; +import gnu.java.security.OID; +import gnu.java.security.der.DER; +import gnu.java.security.der.DERValue; +import gnu.java.security.der.DERWriter; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + import java.math.BigInteger; + import java.security.interfaces.DSAPrivateKey; import java.security.interfaces.DSAParams; import java.security.spec.DSAParameterSpec; +import java.util.ArrayList; + public class GnuDSAPrivateKey implements DSAPrivateKey { + private byte[] encodedKey; BigInteger x; BigInteger p; BigInteger q; @@ -65,13 +77,57 @@ public class GnuDSAPrivateKey implements DSAPrivateKey public String getFormat() { - return null; + return "PKCS#8"; } + /** + * Encodes this key as a <code>PrivateKeyInfo</code>, as described in + * PKCS #8. The ASN.1 specification for this structure is: + * + * <blockquote><pre> + * PrivateKeyInfo ::= SEQUENCE { + * version Version, + * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, + * privateKey PrivateKey, + * attributes [0] IMPLICIT Attributes OPTIONAL } + * + * Version ::= INTEGER + * + * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier + * + * PrivateKey ::= OCTET STRING + * + * Attributes ::= SET OF Attribute + * </pre></blockquote> + * + * <p>DSA private keys (in Classpath at least) have no attributes. + */ public byte[] getEncoded() { + if (encodedKey != null) + return (byte[]) encodedKey.clone(); + try + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ArrayList pki = new ArrayList(3); + pki.add(new DERValue(DER.INTEGER, BigInteger.ZERO)); + ArrayList algId = new ArrayList(2); + algId.add(new DERValue(DER.OBJECT_IDENTIFIER, + new OID("1.2.840.10040.4.1"))); + ArrayList algParams = new ArrayList(3); + algParams.add(new DERValue(DER.INTEGER, p)); + algParams.add(new DERValue(DER.INTEGER, q)); + algParams.add(new DERValue(DER.INTEGER, g)); + algId.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, algParams)); + pki.add(new DERValue(DER.OCTET_STRING, x.toByteArray())); + DERWriter.write(out, new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, pki)); + return (byte[]) (encodedKey = out.toByteArray()).clone(); + } + catch (IOException ioe) + { return null; } + } public DSAParams getParams() { @@ -85,7 +141,10 @@ public class GnuDSAPrivateKey implements DSAPrivateKey public String toString() { - return "GnuDSAPrivateKey: x=" + x.toString(16) + " p=" + p.toString(16) - + " q=" + q.toString(16) + " g=" + g.toString(16); + return "GnuDSAPrivateKey: x=" + + (x != null ? x.toString(16) : "null") + " p=" + + (p != null ? p.toString(16) : "null") + " q=" + + (q != null ? q.toString(16) : "null") + " g=" + + (g != null ? g.toString(16) : "null"); } } |
