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 javax.inject.Inject; |
23 |
|
import javax.inject.Singleton; |
24 |
|
|
25 |
|
import org.bouncycastle.asn1.ASN1Encodable; |
26 |
|
import org.bouncycastle.asn1.ASN1Sequence; |
27 |
|
import org.bouncycastle.asn1.pkcs.EncryptionScheme; |
28 |
|
import org.bouncycastle.asn1.pkcs.KeyDerivationFunc; |
29 |
|
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
30 |
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.component.manager.ComponentLookupException; |
33 |
|
import org.xwiki.component.manager.ComponentManager; |
34 |
|
import org.xwiki.crypto.cipher.CipherFactory; |
35 |
|
import org.xwiki.crypto.password.PasswordBasedCipher; |
36 |
|
import org.xwiki.crypto.password.PasswordBasedCipherFactory; |
37 |
|
import org.xwiki.crypto.password.internal.kdf.PBES2Parameters; |
38 |
|
import org.xwiki.crypto.password.internal.pbe.RC5CBCParameter; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
46 |
|
@Component(hints = { "PBES2", "1.2.840.113549.1.5.13" }) |
47 |
|
@Singleton |
|
|
| 59.5% |
Uncovered Elements: 17 (42) |
Complexity: 13 |
Complexity Density: 0.52 |
|
48 |
|
public class BcPBES2CipherFactory extends AbstractBcPBES2CipherFactory |
49 |
|
{ |
50 |
|
private static final RuntimeException UNSUPPORTED = |
51 |
|
new UnsupportedOperationException("Unexpected internal function call."); |
52 |
|
|
53 |
|
@Inject |
54 |
|
private ComponentManager manager; |
55 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
56 |
0 |
@Override... |
57 |
|
protected CipherFactory getCipherFactory() |
58 |
|
{ |
59 |
0 |
throw UNSUPPORTED; |
60 |
|
} |
61 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
62 |
6 |
@Override... |
63 |
|
public PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, byte[] encoded) |
64 |
|
{ |
65 |
6 |
ASN1Sequence seq = ASN1Sequence.getInstance(encoded); |
66 |
6 |
AlgorithmIdentifier alg = getPBES2AlgorithmIdentifier(seq); |
67 |
|
|
68 |
6 |
PBES2Parameters params = PBES2Parameters.getInstance(alg.getParameters()); |
69 |
6 |
PasswordBasedCipherFactory pbecf = getPBES2CipherFactory(params.getEncryptionScheme()); |
70 |
6 |
PasswordBasedCipher cipher = getBcPBES2PasswordBasedCipher(pbecf, forEncryption, password, seq); |
71 |
|
|
72 |
6 |
if (cipher != null) { |
73 |
6 |
return cipher; |
74 |
|
} |
75 |
0 |
return pbecf.getInstance(forEncryption, password, encoded); |
76 |
|
} |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
7 |
@Override... |
79 |
|
public PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, ASN1Encodable parameters) |
80 |
|
{ |
81 |
7 |
return getBcPBES2PasswordBasedCipher( |
82 |
|
getPBES2CipherFactory( |
83 |
|
PBES2Parameters.getInstance( |
84 |
|
getPBES2AlgorithmIdentifier(parameters).getParameters()).getEncryptionScheme()), |
85 |
|
forEncryption, password, parameters); |
86 |
|
} |
87 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
88 |
13 |
private AlgorithmIdentifier getPBES2AlgorithmIdentifier(ASN1Encodable parameters)... |
89 |
|
{ |
90 |
13 |
AlgorithmIdentifier alg = AlgorithmIdentifier.getInstance(parameters); |
91 |
|
|
92 |
13 |
if (!alg.getAlgorithm().equals(PKCSObjectIdentifiers.id_PBES2)) { |
93 |
0 |
throw new IllegalArgumentException("Illegal algorithm identifier for PBES2: " + alg.getAlgorithm().getId()); |
94 |
|
} |
95 |
13 |
return alg; |
96 |
|
} |
97 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
98 |
13 |
private PasswordBasedCipher getBcPBES2PasswordBasedCipher(PasswordBasedCipherFactory pbecf, boolean forEncryption,... |
99 |
|
byte[] password, ASN1Encodable parameters) |
100 |
|
{ |
101 |
13 |
if (pbecf instanceof AbstractBcPBES2CipherFactory) { |
102 |
13 |
return ((AbstractBcPBES2CipherFactory) pbecf).getInstance(forEncryption, password, parameters); |
103 |
|
} |
104 |
0 |
return null; |
105 |
|
} |
106 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
107 |
0 |
@Override... |
108 |
|
protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams, |
109 |
|
EncryptionScheme scheme) |
110 |
|
{ |
111 |
|
|
112 |
0 |
throw UNSUPPORTED; |
113 |
|
} |
114 |
|
|
|
|
| 36.4% |
Uncovered Elements: 7 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
115 |
13 |
private PasswordBasedCipherFactory getPBES2CipherFactory(EncryptionScheme scheme)... |
116 |
|
{ |
117 |
13 |
try { |
118 |
13 |
if (scheme.getAlgorithm().equals(PKCSObjectIdentifiers.encryptionAlgorithm.branch("9"))) { |
119 |
0 |
RC5CBCParameter rc5Param = RC5CBCParameter.getInstance(scheme.getParameters()); |
120 |
0 |
if (rc5Param.getBlockSizeInBits().intValue() > 64) { |
121 |
|
|
122 |
0 |
return this.manager.getInstance(PasswordBasedCipherFactory.class, "PBES2-RC5-64-CBC-Pad"); |
123 |
|
} |
124 |
|
} |
125 |
13 |
return this.manager.getInstance(PasswordBasedCipherFactory.class, scheme.getAlgorithm().getId()); |
126 |
|
} catch (ComponentLookupException e) { |
127 |
0 |
throw new UnsupportedOperationException("Password based cipher factory not found.", e); |
128 |
|
} |
129 |
|
} |
130 |
|
} |