14c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrompackage org.bouncycastle.jcajce.provider.asymmetric.dsa;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.InvalidKeyException;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.PrivateKey;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.PublicKey;
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.interfaces.DSAPrivateKey;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.interfaces.DSAPublicKey;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
94c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1ObjectIdentifier;
104c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
114c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.AsymmetricKeyParameter;
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.DSAParameters;
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.DSAPublicKeyParameters;
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * utility class for converting jce/jca DSA objects
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * objects into their org.bouncycastle.crypto counterparts.
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DSAUtil
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
234c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public static final ASN1ObjectIdentifier[] dsaOids =
244c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
254c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        X9ObjectIdentifiers.id_dsa,
264c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        OIWObjectIdentifiers.dsaWithSHA1
274c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    };
284c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
294c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public static boolean isDsaOid(
304c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1ObjectIdentifier algOid)
314c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
324c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        for (int i = 0; i != dsaOids.length; i++)
334c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        {
344c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            if (algOid.equals(dsaOids[i]))
354c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            {
364c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom                return true;
374c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            }
384c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        }
394c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
404c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return false;
414c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    }
424c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    static public AsymmetricKeyParameter generatePublicKeyParameter(
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        PublicKey    key)
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws InvalidKeyException
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (key instanceof DSAPublicKey)
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            DSAPublicKey    k = (DSAPublicKey)key;
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new DSAPublicKeyParameters(k.getY(),
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                new DSAParameters(k.getParams().getP(), k.getParams().getQ(), k.getParams().getG()));
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new InvalidKeyException("can't identify DSA public key: " + key.getClass().getName());
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    static public AsymmetricKeyParameter generatePrivateKeyParameter(
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        PrivateKey    key)
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws InvalidKeyException
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (key instanceof DSAPrivateKey)
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            DSAPrivateKey    k = (DSAPrivateKey)key;
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new DSAPrivateKeyParameters(k.getX(),
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                new DSAParameters(k.getParams().getP(), k.getParams().getQ(), k.getParams().getG()));
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new InvalidKeyException("can't identify DSA private key.");
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
73