summaryrefslogtreecommitdiff
path: root/bcprov/src/main/java/org/bouncycastle/jcajce/PKCS12Key.java
blob: 856b3e5ba726bf3a032ce68dbcbd496399ec82bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.bouncycastle.jcajce;

import org.bouncycastle.crypto.PBEParametersGenerator;

/**
 * A password based key for use with PKCS#12.
 */
public class PKCS12Key
    implements PBKDFKey
{
    private final char[] password;
    private final boolean useWrongZeroLengthConversion;
    /**
     * Basic constructor for a password based key - secret key generation parameters will be passed separately..
     *
     * @param password password to use.
     */
    public PKCS12Key(char[] password)
    {
        this(password, false);
    }

    /**
     * Unfortunately there seems to be some confusion about how to handle zero length
     * passwords.
     *
     * @param password password to use.
     * @param useWrongZeroLengthConversion use the incorrect encoding approach (add pad bytes)
     */
    public PKCS12Key(char[] password, boolean useWrongZeroLengthConversion)
    {
        if (password == null)
        {
            password = new char[0];
        }

        this.password = new char[password.length];
        this.useWrongZeroLengthConversion = useWrongZeroLengthConversion;

        System.arraycopy(password, 0, this.password, 0, password.length);
    }

    /**
     * Return a reference to the char[] array holding the password.
     *
     * @return a reference to the password array.
     */
    public char[] getPassword()
    {
        return password;
    }

    /**
     * Return the password based key derivation function this key is for,
     *
     * @return the string "PKCS12"
     */
    public String getAlgorithm()
    {
        return "PKCS12";
    }

    /**
     * Return the format encoding.
     *
     * @return the string "PKCS12", representing the char[] to byte[] conversion.
     */
    public String getFormat()
    {
        return "PKCS12";
    }

    /**
     * Return the password converted to bytes.
     *
     * @return the password converted to a byte array.
     */
    public byte[] getEncoded()
    {
        if (useWrongZeroLengthConversion && password.length == 0)
        {
            return new byte[2];
        }

        return PBEParametersGenerator.PKCS12PasswordToBytes(password);
    }
}