1package org.bouncycastle.jcajce;
2
3import java.io.IOException;
4import java.security.AlgorithmParameters;
5
6import org.bouncycastle.asn1.ASN1Encodable;
7import org.bouncycastle.asn1.ASN1Primitive;
8
9public class JcaJceUtils
10{
11    private JcaJceUtils()
12    {
13
14    }
15
16    /**
17     * Extract an ASN.1 encodable from an AlgorithmParameters object.
18     *
19     * @param params the object to get the encoding used to create the return value.
20     * @return an ASN.1 object representing the primitives making up the params parameter.
21     * @throws IOException if an encoding cannot be extracted.
22     */
23    public static ASN1Encodable extractParameters(AlgorithmParameters params)
24        throws IOException
25    {
26        // we try ASN.1 explicitly first just in case and then role back to the default.
27        ASN1Encodable asn1Params;
28        try
29        {
30            asn1Params = ASN1Primitive.fromByteArray(params.getEncoded("ASN.1"));
31        }
32        catch (Exception ex)
33        {
34            asn1Params = ASN1Primitive.fromByteArray(params.getEncoded());
35        }
36
37        return asn1Params;
38    }
39
40    public static void loadParameters(AlgorithmParameters params, ASN1Encodable sParams)
41        throws IOException
42    {
43        // we try ASN.1 explicitly first just in case and then role back to the default.
44        try
45        {
46            params.init(sParams.toASN1Primitive().getEncoded(), "ASN.1");
47        }
48        catch (Exception ex)
49        {
50            params.init(sParams.toASN1Primitive().getEncoded());
51        }
52    }
53}
54