Home | History | Annotate | Download | only in symmetric
      1 package org.bouncycastle.jcajce.provider.symmetric;
      2 
      3 import java.io.IOException;
      4 import java.security.spec.AlgorithmParameterSpec;
      5 import java.security.spec.InvalidParameterSpecException;
      6 
      7 import javax.crypto.spec.PBEParameterSpec;
      8 
      9 import org.bouncycastle.asn1.ASN1Encoding;
     10 import org.bouncycastle.asn1.ASN1Primitive;
     11 import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
     12 import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
     13 import org.bouncycastle.jcajce.provider.symmetric.util.BaseAlgorithmParameters;
     14 import org.bouncycastle.jcajce.provider.util.AlgorithmProvider;
     15 
     16 public 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