PBEPKCS12.java revision a198e1ecc615e26a167d0f2dca9fa7e5fc62de10
1package org.bouncycastle.jcajce.provider.symmetric;
2
3import java.io.IOException;
4import java.security.spec.AlgorithmParameterSpec;
5import java.security.spec.InvalidParameterSpecException;
6
7import javax.crypto.spec.PBEParameterSpec;
8
9import org.bouncycastle.asn1.ASN1Encoding;
10import org.bouncycastle.asn1.ASN1Primitive;
11import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
12import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
13import org.bouncycastle.jcajce.provider.symmetric.util.BaseAlgorithmParameters;
14import org.bouncycastle.jcajce.provider.util.AlgorithmProvider;
15
16public class PBEPKCS12
17{
18    private PBEPKCS12()
19    {
20
21    }
22
23    public static class AlgParams
24        extends BaseAlgorithmParameters
25    {
26        PKCS12PBEParams params;
27
28        protected byte[] engineGetEncoded()
29        {
30            try
31            {
32                return params.getEncoded(ASN1Encoding.DER);
33            }
34            catch (IOException e)
35            {
36                throw new RuntimeException("Oooops! " + e.toString());
37            }
38        }
39
40        protected byte[] engineGetEncoded(
41            String format)
42        {
43            if (this.isASN1FormatString(format))
44            {
45                return engineGetEncoded();
46            }
47
48            return null;
49        }
50
51        protected AlgorithmParameterSpec localEngineGetParameterSpec(
52            Class paramSpec)
53            throws InvalidParameterSpecException
54        {
55            if (paramSpec == PBEParameterSpec.class)
56            {
57                return new PBEParameterSpec(params.getIV(),
58                    params.getIterations().intValue());
59            }
60
61            throw new InvalidParameterSpecException("unknown parameter spec passed to PKCS12 PBE parameters object.");
62        }
63
64        protected void engineInit(
65            AlgorithmParameterSpec paramSpec)
66            throws InvalidParameterSpecException
67        {
68            if (!(paramSpec instanceof PBEParameterSpec))
69            {
70                throw new InvalidParameterSpecException("PBEParameterSpec required to initialise a PKCS12 PBE parameters algorithm parameters object");
71            }
72
73            PBEParameterSpec pbeSpec = (PBEParameterSpec)paramSpec;
74
75            this.params = new PKCS12PBEParams(pbeSpec.getSalt(),
76                pbeSpec.getIterationCount());
77        }
78
79        protected void engineInit(
80            byte[] params)
81            throws IOException
82        {
83            this.params = PKCS12PBEParams.getInstance(ASN1Primitive.fromByteArray(params));
84        }
85
86        protected void engineInit(
87            byte[] params,
88            String format)
89            throws IOException
90        {
91            if (this.isASN1FormatString(format))
92            {
93                engineInit(params);
94                return;
95            }
96
97            throw new IOException("Unknown parameters format in PKCS12 PBE parameters object");
98        }
99
100        protected String engineToString()
101        {
102            return "PKCS12 PBE Parameters";
103        }
104    }
105
106    public static class Mappings
107        extends AlgorithmProvider
108    {
109        private static final String PREFIX = PBEPKCS12.class.getName();
110
111        public Mappings()
112        {
113        }
114
115        public void configure(ConfigurableProvider provider)
116        {
117            provider.addAlgorithm("AlgorithmParameters.PKCS12PBE", PREFIX + "$AlgParams");
118        }
119    }
120}
121