• 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         if (password == null)
33         {
34             password = new char[0];
35         }
36 
37         this.password = new char[password.length];
38         this.useWrongZeroLengthConversion = useWrongZeroLengthConversion;
39 
40         System.arraycopy(password, 0, this.password, 0, password.length);
41     }
42 
43     /**
44      * Return a reference to the char[] array holding the password.
45      *
46      * @return a reference to the password array.
47      */
getPassword()48     public char[] getPassword()
49     {
50         return password;
51     }
52 
53     /**
54      * Return the password based key derivation function this key is for,
55      *
56      * @return the string "PKCS12"
57      */
getAlgorithm()58     public String getAlgorithm()
59     {
60         return "PKCS12";
61     }
62 
63     /**
64      * Return the format encoding.
65      *
66      * @return the string "PKCS12", representing the char[] to byte[] conversion.
67      */
getFormat()68     public String getFormat()
69     {
70         return "PKCS12";
71     }
72 
73     /**
74      * Return the password converted to bytes.
75      *
76      * @return the password converted to a byte array.
77      */
getEncoded()78     public byte[] getEncoded()
79     {
80         if (useWrongZeroLengthConversion && password.length == 0)
81         {
82             return new byte[2];
83         }
84 
85         return PBEParametersGenerator.PKCS12PasswordToBytes(password);
86     }
87 }
88