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; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.security.GeneralSecurityException; |
24 |
|
import java.security.SecureRandom; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Provider; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo; |
31 |
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.manager.ComponentLookupException; |
34 |
|
import org.xwiki.component.manager.ComponentManager; |
35 |
|
import org.xwiki.crypto.AsymmetricKeyFactory; |
36 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters; |
37 |
|
import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters; |
38 |
|
import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters; |
39 |
|
import org.xwiki.crypto.password.KeyDerivationFunction; |
40 |
|
import org.xwiki.crypto.password.PasswordBasedCipher; |
41 |
|
import org.xwiki.crypto.password.PasswordBasedCipherFactory; |
42 |
|
import org.xwiki.crypto.password.PrivateKeyPasswordBasedEncryptor; |
43 |
|
import org.xwiki.crypto.password.internal.pbe.AbstractBcPBCipher; |
44 |
|
import org.xwiki.crypto.password.internal.pbe.factory.AbstractBcPBCipherFactory; |
45 |
|
import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters; |
46 |
|
import org.xwiki.crypto.password.params.PBKDF2Parameters; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Singleton |
|
|
| 69.8% |
Uncovered Elements: 13 (43) |
Complexity: 14 |
Complexity Density: 0.5 |
|
56 |
|
public class DefaultPrivateKeyPasswordBasedEncryptor implements PrivateKeyPasswordBasedEncryptor |
57 |
|
{ |
58 |
|
@Inject |
59 |
|
private ComponentManager manager; |
60 |
|
|
61 |
|
@Inject |
62 |
|
private AsymmetricKeyFactory keyFactory; |
63 |
|
|
64 |
|
@Inject |
65 |
|
private Provider<SecureRandom> randomProvider; |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
67 |
7 |
@Override... |
68 |
|
public PrivateKeyParameters decrypt(byte[] password, byte[] encoded) |
69 |
|
throws GeneralSecurityException, IOException |
70 |
|
{ |
71 |
7 |
EncryptedPrivateKeyInfo encKeyInfo = EncryptedPrivateKeyInfo.getInstance(encoded); |
72 |
7 |
return decrypt(password, encKeyInfo.getEncryptionAlgorithm(), encKeyInfo.getEncryptedData()); |
73 |
|
} |
74 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
0 |
@Override... |
76 |
|
public PrivateKeyParameters decrypt(byte[] password, javax.crypto.EncryptedPrivateKeyInfo privateKeyInfo) |
77 |
|
throws GeneralSecurityException, IOException |
78 |
|
{ |
79 |
0 |
return decrypt(password, privateKeyInfo.getEncoded()); |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
7 |
private PrivateKeyParameters decrypt(byte[] password, AlgorithmIdentifier algId, byte[] encoded)... |
83 |
|
throws GeneralSecurityException, IOException |
84 |
|
{ |
85 |
7 |
return this.keyFactory.fromPKCS8(getPBECipher(password, algId).doFinal(encoded)); |
86 |
|
} |
87 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
88 |
7 |
private PasswordBasedCipher getPBECipher(byte[] password, AlgorithmIdentifier algId) throws IOException... |
89 |
|
{ |
90 |
7 |
PasswordBasedCipherFactory factory = getPBEFactory(algId.getAlgorithm().getId()); |
91 |
|
|
92 |
|
|
93 |
7 |
if (factory instanceof AbstractBcPBCipherFactory) { |
94 |
7 |
return ((AbstractBcPBCipherFactory) factory).getInstance(false, password, algId); |
95 |
|
} |
96 |
|
|
97 |
0 |
return factory.getInstance(false, password, algId.getEncoded()); |
98 |
|
} |
99 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
100 |
14 |
private PasswordBasedCipherFactory getPBEFactory(String hint)... |
101 |
|
{ |
102 |
14 |
try { |
103 |
14 |
return this.manager.getInstance(PasswordBasedCipherFactory.class, hint); |
104 |
|
} catch (ComponentLookupException e) { |
105 |
0 |
throw new UnsupportedOperationException("Password based cipher factory not found: " + hint, e); |
106 |
|
} |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
109 |
5 |
@Override... |
110 |
|
public byte[] encrypt(String algHint, SymmetricCipherParameters password, |
111 |
|
KeyDerivationFunctionParameters kdfParameters, PrivateKeyParameters privateKey) |
112 |
|
throws GeneralSecurityException, IOException |
113 |
|
{ |
114 |
5 |
PasswordBasedCipher cipher = getPBEFactory(algHint).getInstance(true, password, kdfParameters); |
115 |
5 |
return encrypt(cipher, privateKey); |
116 |
|
} |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
118 |
0 |
@Override... |
119 |
|
public byte[] encrypt(String algHint, SymmetricCipherParameters password, KeyDerivationFunction function, |
120 |
|
PrivateKeyParameters privateKey) throws GeneralSecurityException, IOException |
121 |
|
{ |
122 |
0 |
PasswordBasedCipher cipher = getPBEFactory(algHint).getInstance(true, password, function); |
123 |
0 |
return encrypt(cipher, privateKey); |
124 |
|
} |
125 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
126 |
0 |
@Override... |
127 |
|
public byte[] encrypt(String algHint, byte[] password, byte[] encoded, PrivateKeyParameters privateKey) |
128 |
|
throws GeneralSecurityException, IOException |
129 |
|
{ |
130 |
0 |
PasswordBasedCipher cipher = getPBEFactory(algHint).getInstance(true, password, encoded); |
131 |
0 |
return encrypt(cipher, privateKey); |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
134 |
1 |
@Override... |
135 |
|
public byte[] encrypt(String algHint, byte[] password, KeyDerivationFunctionParameters kdfParameters, |
136 |
|
PrivateKeyParameters privateKey) throws GeneralSecurityException, IOException |
137 |
|
{ |
138 |
1 |
PasswordBasedCipherFactory factory = getPBEFactory(algHint); |
139 |
1 |
PasswordBasedCipher cipher = factory.getInstance(true, |
140 |
|
new KeyWithIVParameters(password, factory.getIVSize(), this.randomProvider.get()), |
141 |
|
kdfParameters); |
142 |
1 |
return encrypt(cipher, privateKey); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
145 |
1 |
@Override... |
146 |
|
public byte[] encrypt(byte[] password, PrivateKeyParameters privateKey) throws GeneralSecurityException, IOException |
147 |
|
{ |
148 |
1 |
PasswordBasedCipherFactory factory = getPBEFactory("PBES2-AES-CBC-Pad"); |
149 |
1 |
PasswordBasedCipher cipher = factory.getInstance(true, |
150 |
|
new KeyWithIVParameters(password, factory.getIVSize(), this.randomProvider.get()), |
151 |
|
new PBKDF2Parameters(this.randomProvider.get())); |
152 |
1 |
return encrypt(cipher, privateKey); |
153 |
|
} |
154 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
155 |
7 |
@Override... |
156 |
|
public byte[] encrypt(PasswordBasedCipher cipher, PrivateKeyParameters privateKey) |
157 |
|
throws IOException, GeneralSecurityException |
158 |
|
{ |
159 |
7 |
AlgorithmIdentifier algId; |
160 |
|
|
161 |
|
|
162 |
7 |
if (cipher instanceof AbstractBcPBCipher) { |
163 |
7 |
algId = ((AbstractBcPBCipher) cipher).getPBEParameters(); |
164 |
|
} else { |
165 |
0 |
algId = AlgorithmIdentifier.getInstance(cipher.getEncoded()); |
166 |
|
} |
167 |
|
|
168 |
7 |
return new EncryptedPrivateKeyInfo(algId, cipher.doFinal(privateKey.getEncoded())).getEncoded(); |
169 |
|
} |
170 |
|
} |