• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.bouncycastle.jcajce;
2 
3 import org.bouncycastle.crypto.PBEParametersGenerator;
4 
5 /**
6  * A password based key for use with PKCS#12.
7  */
8 public class PKCS12Key
9     implements PBKDFKey
10 {
11     private final char[] password;
12     private final boolean useWrongZeroLengthConversion;
13     /**
14      * Basic constructor for a password based key - secret key generation parameters will be passed separately..
15      *
16      * @param password password to use.
17      */
PKCS12Key(char[] password)18     public PKCS12Key(char[] password)
19     {
20         this(password, false);
21     }
22 
23     /**
24      * Unfortunately there seems to be some confusion about how to handle zero length
25      * passwords.
26      *
27      * @param password password to use.
28      * @param useWrongZeroLengthConversion use the incorrect encoding approach (add pad bytes)
29      */
PKCS12Key(char[] password, boolean useWrongZeroLengthConversion)30     public PKCS12Key(char[] password, boolean useWrongZeroLengthConversion)
31     {
32         this.password = new char[password.length];
33         this.useWrongZeroLengthConversion = useWrongZeroLengthConversion;
34 
35         System.arraycopy(password, 0, this.password, 0, password.length);
36     }
37 
38     /**
39      * Return a reference to the char[] array holding the password.
40      *
41      * @return a reference to the password array.
42      */
getPassword()43     public char[] getPassword()
44     {
45         return password;
46     }
47 
48     /**
49      * Return the password based key derivation function this key is for,
50      *
51      * @return the string "PKCS12"
52      */
getAlgorithm()53     public String getAlgorithm()
54     {
55         return "PKCS12";
56     }
57 
58     /**
59      * Return the format encoding.
60      *
61      * @return the string "PKCS12", representing the char[] to byte[] conversion.
62      */
getFormat()63     public String getFormat()
64     {
65         return "PKCS12";
66     }
67 
68     /**
69      * Return the password converted to bytes.
70      *
71      * @return the password converted to a byte array.
72      */
getEncoded()73     public byte[] getEncoded()
74     {
75         if (useWrongZeroLengthConversion && password.length == 0)
76         {
77             return new byte[2];
78         }
79 
80         return PBEParametersGenerator.PKCS12PasswordToBytes(password);
81     }
82 }
83