1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.cipher.internal.asymmetric.factory; |
21 |
|
|
22 |
|
import org.junit.Before; |
23 |
|
import org.junit.Rule; |
24 |
|
import org.junit.Test; |
25 |
|
import org.junit.rules.ExpectedException; |
26 |
|
import org.xwiki.crypto.AsymmetricKeyFactory; |
27 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
28 |
|
import org.xwiki.crypto.cipher.Cipher; |
29 |
|
import org.xwiki.crypto.cipher.CipherFactory; |
30 |
|
import org.xwiki.crypto.internal.DefaultSecureRandomProvider; |
31 |
|
import org.xwiki.crypto.internal.asymmetric.keyfactory.BcRSAKeyFactory; |
32 |
|
import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder; |
33 |
|
import org.xwiki.crypto.internal.encoder.HexBinaryStringEncoder; |
34 |
|
import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricCipherParameters; |
35 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters; |
36 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters; |
37 |
|
import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters; |
38 |
|
import org.xwiki.test.annotation.ComponentList; |
39 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
40 |
|
|
41 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
42 |
|
import static org.junit.Assert.assertThat; |
43 |
|
|
44 |
|
@ComponentList({Base64BinaryStringEncoder.class, HexBinaryStringEncoder.class, |
45 |
|
BcRSAKeyFactory.class, DefaultSecureRandomProvider.class}) |
|
|
| 82.6% |
Uncovered Elements: 8 (46) |
Complexity: 6 |
Complexity Density: 0.15 |
|
46 |
|
public class BcRsaOaepCipherFactoryTest |
47 |
|
{ |
48 |
|
private static final String PRIVATE_KEY = |
49 |
|
|
50 |
|
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDCmjim/3likJ4" |
51 |
|
+ "VF564UyygqPjIX/z090AImLl0fDLUkIyCVTSd18wJ3axr1qjLtSgNPWet0puSxO" |
52 |
|
+ "FH0AzFKRCJOjUkQRU8iAkz64MLAf9xrx4nBECciqeB941s01kLtG8C/UqC3O9Sw" |
53 |
|
+ "HSdhtUpUU8V/91SiD09yNJsnODi3WqM3oLg1QYzKhoaD2mVo2xJLQ/QXqr2XIc5" |
54 |
|
+ "i2Mlpfq6S5JNbFD/I+UFhBUlBNuDOEV7ttIt2eFMEUsfkCestGo0YoQYOpTLPcP" |
55 |
|
+ "GRS7MnSY1CLWGUYqaMSnes0nS8ke2PPD4Q0suAZz4msnhNufanscstM8tcNtsZF" |
56 |
|
+ "6hj0JvbZok89szAgMBAAECggEBAKWJ1SlR5ysORDtDBXRc5HiiZEbnSGIFtYXaj" |
57 |
|
+ "N/nCsJBWBVCb+jZeirmU9bEGoB20OQ6WOjHYCnAqraQ51wMK5HgXvZBGtSMD/AH" |
58 |
|
+ "pkiF4YsOYULlXiUL2aQ4NijdvEC1sz1Cw9CAKmElb83UtZ1ZGkJnjhi35giZvU5" |
59 |
|
+ "BQRgbK5k57DFY66yv9VDg8tuD/enI9sRsCUZfCImuShGv4nLqhPMPg+1UxDPGet" |
60 |
|
+ "Vs8uEaJQ017E14wLKLA0DlED13icelU1A7ufkEdeBSv/yZ7ENjervzPwa9nITK/" |
61 |
|
+ "19uzqaHOcYZxmDQn6UHTnaLpIEaUvpp/pbed5S97ETSsqUBC8fqEUECgYEA/Sba" |
62 |
|
+ "o6efydhlXDHbXtyvaJWao19sbI9OfxGC6dR2fZiBx8Do9kVDDbMtb1PYEfLhYbi" |
63 |
|
+ "urmKGbUtcLSFgxNbZifUmG54M92nBsnsetMCqvMVNzYl2Je83V+NrIs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nrdKbksThR9AMxS" |
81 |
|
+ "kQiTo1JEEVPIgJM+uDCwH/ca8eJwRAnIqngfeNbNNZC7RvAv1KgtzvUsB0nYbVK" |
82 |
|
+ "VFPFf/dUog9PcjSbJzg4t1qjN6C4NUGMyoaGg9plaNsSS0P0F6q9lyHOYtjJaX6" |
83 |
|
+ "ukuSTWxQ/yPlBYQVJQTbgzhFe7bSLdnhTBFLH5AnrLRqNGKEGDqUyz3DxkUuzJ0" |
84 |
|
+ "mNQi1hlGKmjEp3rNJ0vJHtjzw+ENLLgGc+JrJ4Tbn2p7HLLTPLXDbbGReoY9Cb2" |
85 |
|
+ "2aJPPbMwIDAQAB"; |
86 |
|
|
87 |
|
private static final String INPUT = "4e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e"; |
88 |
|
|
89 |
|
@Rule |
90 |
|
public final MockitoComponentMockingRule<CipherFactory> mocker = |
91 |
|
new MockitoComponentMockingRule<CipherFactory>(BcRsaOaepCipherFactory.class); |
92 |
|
|
93 |
|
private CipherFactory factory; |
94 |
|
|
95 |
|
private static PrivateKeyParameters privateKey; |
96 |
|
private static PublicKeyParameters publicKey; |
97 |
|
private static byte[] input; |
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
99 |
4 |
@Before... |
100 |
|
public void configure() throws Exception |
101 |
|
{ |
102 |
4 |
factory = mocker.getComponentUnderTest(); |
103 |
|
|
104 |
|
|
105 |
4 |
if (privateKey == null) { |
106 |
1 |
BinaryStringEncoder base64Encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64"); |
107 |
1 |
BinaryStringEncoder hexEencoder = mocker.getInstance(BinaryStringEncoder.class, "Base64"); |
108 |
1 |
AsymmetricKeyFactory keyfactory = mocker.getInstance(AsymmetricKeyFactory.class, "RSA"); |
109 |
|
|
110 |
1 |
privateKey = keyfactory.fromPKCS8(base64Encoder.decode(PRIVATE_KEY)); |
111 |
1 |
publicKey = keyfactory.fromX509(base64Encoder.decode(PUBLIC_KEY)); |
112 |
1 |
input = hexEencoder.decode(INPUT); |
113 |
|
} |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
116 |
1 |
@Test... |
117 |
|
public void testRSAEncryptionDecryptionOneShot() throws Exception |
118 |
|
{ |
119 |
1 |
Cipher cipher = factory.getInstance(true, publicKey); |
120 |
1 |
byte[] encrypted = cipher.doFinal(input); |
121 |
1 |
cipher = factory.getInstance(false, privateKey); |
122 |
1 |
assertThat(cipher.doFinal(encrypted), equalTo(input)); |
123 |
|
|
124 |
1 |
cipher = factory.getInstance(true, privateKey); |
125 |
1 |
encrypted = cipher.doFinal(input); |
126 |
1 |
cipher = factory.getInstance(false, publicKey); |
127 |
1 |
assertThat(cipher.doFinal(encrypted), equalTo(input)); |
128 |
|
} |
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
130 |
1 |
@Test... |
131 |
|
public void testRSAEncryptionDecryptionProgressive() throws Exception |
132 |
|
{ |
133 |
1 |
Cipher cipher = factory.getInstance(true, publicKey); |
134 |
1 |
cipher.update(input, 0, 17); |
135 |
1 |
cipher.update(input, 17, 1); |
136 |
1 |
cipher.update(input, 18, input.length - 18); |
137 |
1 |
byte[] encrypted = cipher.doFinal(); |
138 |
1 |
cipher = factory.getInstance(false, privateKey); |
139 |
1 |
cipher.update(encrypted, 0, 65); |
140 |
1 |
cipher.update(encrypted, 65, 1); |
141 |
1 |
cipher.update(encrypted, 66, encrypted.length - 66); |
142 |
1 |
assertThat(cipher.doFinal(), equalTo(input)); |
143 |
|
|
144 |
1 |
cipher = factory.getInstance(true, privateKey); |
145 |
1 |
cipher.update(input, 0, 15); |
146 |
1 |
cipher.update(input, 15, 1); |
147 |
1 |
encrypted = cipher.doFinal(input, 16, input.length - 16); |
148 |
1 |
cipher = factory.getInstance(false, publicKey); |
149 |
1 |
cipher.update(encrypted); |
150 |
1 |
assertThat(cipher.doFinal(), equalTo(input)); |
151 |
|
} |
152 |
|
|
153 |
|
@Rule public ExpectedException thrown = ExpectedException.none(); |
154 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
155 |
|
class WrongParameters implements AsymmetricCipherParameters |
156 |
|
{ } |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
3FAIL
|
|
158 |
0 |
@Test... |
159 |
|
public void testCipherWithWrongParameters() throws Exception |
160 |
|
{ |
161 |
0 |
thrown.expect(UnsupportedOperationException.class); |
162 |
0 |
thrown.expectMessage("Cipher parameters are incompatible with this cipher: " + WrongParameters.class.getName()); |
163 |
0 |
factory.getInstance(true, new WrongParameters()); |
164 |
|
} |
165 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
166 |
|
class SymmetricParameters implements SymmetricCipherParameters |
167 |
|
{ } |
168 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
3FAIL
|
|
169 |
0 |
@Test... |
170 |
|
public void testCipherWithAsymmetricParameters() throws Exception |
171 |
|
{ |
172 |
0 |
thrown.expect(IllegalArgumentException.class); |
173 |
0 |
thrown.expectMessage("Unexpected parameters received for a asymmetric cipher: " |
174 |
|
+ SymmetricParameters.class.getName()); |
175 |
0 |
factory.getInstance(true, new SymmetricParameters()); |
176 |
|
} |
177 |
|
} |