1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.password.internal.pbe.factory; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
|
24 |
|
import org.bouncycastle.asn1.ASN1Encodable; |
25 |
|
import org.bouncycastle.asn1.ASN1OctetString; |
26 |
|
import org.bouncycastle.asn1.pkcs.EncryptionScheme; |
27 |
|
import org.bouncycastle.asn1.pkcs.KeyDerivationFunc; |
28 |
|
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
29 |
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
30 |
|
import org.xwiki.crypto.params.cipher.symmetric.KeyParameter; |
31 |
|
import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters; |
32 |
|
import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters; |
33 |
|
import org.xwiki.crypto.password.KeyDerivationFunction; |
34 |
|
import org.xwiki.crypto.password.KeyDerivationFunctionFactory; |
35 |
|
import org.xwiki.crypto.password.PasswordBasedCipher; |
36 |
|
import org.xwiki.crypto.password.internal.kdf.PBES2Parameters; |
37 |
|
import org.xwiki.crypto.password.internal.kdf.factory.AbstractBcKDFFactory; |
38 |
|
import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 69.4% |
Uncovered Elements: 15 (49) |
Complexity: 17 |
Complexity Density: 0.57 |
|
46 |
|
public abstract class AbstractBcPBES2CipherFactory extends AbstractBcPBCipherFactory |
47 |
|
{ |
48 |
|
private static final RuntimeException UNSUPPORTED = |
49 |
|
new UnsupportedOperationException("Sorry, no concrete implementation to create an instance."); |
50 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
51 |
25 |
private KeyDerivationFunctionFactory safeGetKDFFactory()... |
52 |
|
{ |
53 |
25 |
try { |
54 |
25 |
return getKDFFactory(); |
55 |
|
} catch (UnsupportedOperationException e) { |
56 |
0 |
throw UNSUPPORTED; |
57 |
|
} |
58 |
|
} |
59 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
60 |
13 |
@Override... |
61 |
|
public PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, ASN1Encodable parameters) |
62 |
|
{ |
63 |
13 |
AlgorithmIdentifier alg = AlgorithmIdentifier.getInstance(parameters); |
64 |
|
|
65 |
13 |
if (!alg.getAlgorithm().equals(PKCSObjectIdentifiers.id_PBES2)) { |
66 |
0 |
throw new IllegalArgumentException("Illegal algorithm identifier for PBES2: " + alg.getAlgorithm().getId()); |
67 |
|
} |
68 |
|
|
69 |
13 |
PBES2Parameters params = PBES2Parameters.getInstance(alg.getParameters()); |
70 |
13 |
return getInstance(forEncryption, password, params.getKeyDerivationFunc(), params.getEncryptionScheme()); |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
73 |
12 |
@Override... |
74 |
|
public PasswordBasedCipher getInstance(boolean forEncryption, SymmetricCipherParameters password, |
75 |
|
KeyDerivationFunctionParameters parameters) |
76 |
|
{ |
77 |
12 |
KeyDerivationFunction kdf = safeGetKDFFactory().getInstance(parameters); |
78 |
|
|
79 |
12 |
if (kdf.getKeySize() < 0 || !isSupportedKeySize(kdf.getKeySize())) { |
80 |
5 |
kdf.overrideKeySize(getKeySize()); |
81 |
|
} |
82 |
|
|
83 |
12 |
return getInstance(forEncryption, password, kdf); |
84 |
|
} |
85 |
|
|
|
|
| 45.5% |
Uncovered Elements: 6 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
86 |
12 |
@Override... |
87 |
|
public PasswordBasedCipher getInstance(boolean forEncryption, SymmetricCipherParameters password, |
88 |
|
KeyDerivationFunction kdf) |
89 |
|
{ |
90 |
12 |
SymmetricCipherParameters params; |
91 |
|
|
92 |
12 |
if (password instanceof KeyWithIVParameters) { |
93 |
12 |
params = new KeyWithIVParameters(kdf.derive(((KeyWithIVParameters) password).getKey()), |
94 |
|
((KeyWithIVParameters) password).getIV()); |
95 |
0 |
} else if (password instanceof KeyParameter) { |
96 |
0 |
params = kdf.derive(((KeyParameter) password).getKey(), getIVSize()); |
97 |
|
} else { |
98 |
0 |
throw new IllegalArgumentException("Invalid cipher parameters for this password based cipher: " |
99 |
|
+ password.getClass().getName()); |
100 |
|
} |
101 |
|
|
102 |
12 |
return getPasswordBasedCipher(forEncryption, kdf, params); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
@param |
107 |
|
@param |
108 |
|
@param |
109 |
|
@param |
110 |
|
@return |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
112 |
5 |
protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams,... |
113 |
|
EncryptionScheme scheme) |
114 |
|
{ |
115 |
5 |
KeyDerivationFunction kdf = getKeyDerivationFunction(kdfParams); |
116 |
|
|
117 |
|
|
118 |
5 |
if (kdf.getKeySize() < 0 || !isSupportedKeySize(kdf.getKeySize())) { |
119 |
3 |
kdf.overrideKeySize(getKeySize()); |
120 |
|
} |
121 |
|
|
122 |
5 |
return getPasswordBasedCipher(forEncryption, kdf, new KeyWithIVParameters(kdf.derive(password).getKey(), |
123 |
|
((ASN1OctetString) scheme.getParameters()).getOctets())); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
@param |
130 |
|
@param |
131 |
|
@param |
132 |
|
@return |
133 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
134 |
0 |
protected PasswordBasedCipher getPasswordBasedCipher(boolean forEncryption, final KeyDerivationFunction kdf,... |
135 |
|
SymmetricCipherParameters params) |
136 |
|
{ |
137 |
0 |
throw UNSUPPORTED; |
138 |
|
} |
139 |
|
|
|
|
| 50% |
Uncovered Elements: 4 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
140 |
13 |
protected KeyDerivationFunction getKeyDerivationFunction(KeyDerivationFunc func)... |
141 |
|
{ |
142 |
13 |
KeyDerivationFunctionFactory kdfFactory = safeGetKDFFactory(); |
143 |
|
|
144 |
|
|
145 |
13 |
if (kdfFactory instanceof AbstractBcKDFFactory) { |
146 |
13 |
return ((AbstractBcKDFFactory) kdfFactory).getInstance(func); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
0 |
try { |
151 |
0 |
return kdfFactory.getInstance(func.toASN1Primitive().getEncoded()); |
152 |
|
} catch (IOException e) { |
153 |
|
|
154 |
0 |
throw new RuntimeException("Unexpected exception during parameter encoding"); |
155 |
|
} |
156 |
|
} |
157 |
|
} |