AlgorithmParametersSpi.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.jcajce.provider.asymmetric.dsa;
2
3import java.io.IOException;
4import java.security.spec.AlgorithmParameterSpec;
5import java.security.spec.DSAParameterSpec;
6import java.security.spec.InvalidParameterSpecException;
7
8import org.bouncycastle.asn1.ASN1Encoding;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.ASN1Sequence;
11import org.bouncycastle.asn1.x509.DSAParameter;
12
13public class AlgorithmParametersSpi
14    extends java.security.AlgorithmParametersSpi
15{
16    DSAParameterSpec currentSpec;
17
18    protected boolean isASN1FormatString(String format)
19    {
20        return format == null || format.equals("ASN.1");
21    }
22
23    protected AlgorithmParameterSpec engineGetParameterSpec(
24        Class paramSpec)
25        throws InvalidParameterSpecException
26    {
27        if (paramSpec == null)
28        {
29            throw new NullPointerException("argument to getParameterSpec must not be null");
30        }
31
32        return localEngineGetParameterSpec(paramSpec);
33    }
34
35    /**
36     * Return the X.509 ASN.1 structure DSAParameter.
37     * <p/>
38     * <pre>
39     *  DSAParameter ::= SEQUENCE {
40     *                   prime INTEGER, -- p
41     *                   subprime INTEGER, -- q
42     *                   base INTEGER, -- g}
43     * </pre>
44     */
45    protected byte[] engineGetEncoded()
46    {
47        DSAParameter dsaP = new DSAParameter(currentSpec.getP(), currentSpec.getQ(), currentSpec.getG());
48
49        try
50        {
51            return dsaP.getEncoded(ASN1Encoding.DER);
52        }
53        catch (IOException e)
54        {
55            throw new RuntimeException("Error encoding DSAParameters");
56        }
57    }
58
59    protected byte[] engineGetEncoded(
60        String format)
61    {
62        if (isASN1FormatString(format))
63        {
64            return engineGetEncoded();
65        }
66
67        return null;
68    }
69
70    protected AlgorithmParameterSpec localEngineGetParameterSpec(
71        Class paramSpec)
72        throws InvalidParameterSpecException
73    {
74        if (paramSpec == DSAParameterSpec.class)
75        {
76            return currentSpec;
77        }
78
79        throw new InvalidParameterSpecException("unknown parameter spec passed to DSA parameters object.");
80    }
81
82    protected void engineInit(
83        AlgorithmParameterSpec paramSpec)
84        throws InvalidParameterSpecException
85    {
86        if (!(paramSpec instanceof DSAParameterSpec))
87        {
88            throw new InvalidParameterSpecException("DSAParameterSpec required to initialise a DSA algorithm parameters object");
89        }
90
91        this.currentSpec = (DSAParameterSpec)paramSpec;
92    }
93
94    protected void engineInit(
95        byte[] params)
96        throws IOException
97    {
98        try
99        {
100            DSAParameter dsaP = new DSAParameter((ASN1Sequence)ASN1Primitive.fromByteArray(params));
101
102            currentSpec = new DSAParameterSpec(dsaP.getP(), dsaP.getQ(), dsaP.getG());
103        }
104        catch (ClassCastException e)
105        {
106            throw new IOException("Not a valid DSA Parameter encoding.");
107        }
108        catch (ArrayIndexOutOfBoundsException e)
109        {
110            throw new IOException("Not a valid DSA Parameter encoding.");
111        }
112    }
113
114    protected void engineInit(
115        byte[] params,
116        String format)
117        throws IOException
118    {
119        if (isASN1FormatString(format) || format.equalsIgnoreCase("X.509"))
120        {
121            engineInit(params);
122        }
123        else
124        {
125            throw new IOException("Unknown parameter format " + format);
126        }
127    }
128
129    protected String engineToString()
130    {
131        return "DSA Parameters";
132    }
133}
134