1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.signer.internal.factory; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.io.OutputStream; |
26 |
|
|
27 |
|
import org.junit.Before; |
28 |
|
import org.junit.Rule; |
29 |
|
import org.junit.Test; |
30 |
|
import org.xwiki.crypto.internal.asymmetric.keyfactory.BcRSAKeyFactory; |
31 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA1DigestFactory; |
32 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA224DigestFactory; |
33 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA256DigestFactory; |
34 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA384DigestFactory; |
35 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA512DigestFactory; |
36 |
|
import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder; |
37 |
|
import org.xwiki.crypto.signer.Signer; |
38 |
|
import org.xwiki.crypto.signer.SignerFactory; |
39 |
|
import org.xwiki.crypto.signer.params.PssSignerParameters; |
40 |
|
import org.xwiki.test.annotation.ComponentList; |
41 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
42 |
|
|
43 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
44 |
|
import static org.junit.Assert.assertThat; |
45 |
|
import static org.junit.Assert.assertTrue; |
46 |
|
|
47 |
|
@ComponentList({Base64BinaryStringEncoder.class, BcRSAKeyFactory.class, BcSHA1DigestFactory.class, |
48 |
|
BcSHA224DigestFactory.class, BcSHA256DigestFactory.class, BcSHA384DigestFactory.class, |
49 |
|
BcSHA512DigestFactory.class}) |
|
|
| 97.3% |
Uncovered Elements: 2 (75) |
Complexity: 17 |
Complexity Density: 0.3 |
|
50 |
|
public class BcRsaSsaPssSignerFactoryTest extends AbstractRsaSignerFactoryTest |
51 |
|
{ |
52 |
|
@Rule |
53 |
|
public final MockitoComponentMockingRule<SignerFactory> mocker = |
54 |
|
new MockitoComponentMockingRule<SignerFactory>(BcRsaSsaPssSignerFactory.class); |
55 |
|
|
56 |
|
private SignerFactory factory; |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
58 |
13 |
@Before... |
59 |
|
public void configure() throws Exception |
60 |
|
{ |
61 |
13 |
factory = mocker.getComponentUnderTest(); |
62 |
13 |
setupTest(mocker); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
65 |
1 |
@Test... |
66 |
|
public void testDefaultSignatureVerification() throws Exception |
67 |
|
{ |
68 |
1 |
runTestSignatureVerification( |
69 |
|
factory.getInstance(true, privateKey), |
70 |
|
factory.getInstance(false, publicKey) |
71 |
|
); |
72 |
|
} |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
74 |
1 |
@Test... |
75 |
|
public void testSha224SignatureVerification() throws Exception |
76 |
|
{ |
77 |
1 |
runTestSignatureVerification( |
78 |
|
factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-224", -1)), |
79 |
|
factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-224", -1)) |
80 |
|
); |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
83 |
1 |
@Test... |
84 |
|
public void testSha256SignatureVerification() throws Exception |
85 |
|
{ |
86 |
1 |
runTestSignatureVerification( |
87 |
|
factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-256", -1)), |
88 |
|
factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-256", -1)) |
89 |
|
); |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
92 |
1 |
@Test... |
93 |
|
public void testSha384SignatureVerification() throws Exception |
94 |
|
{ |
95 |
1 |
runTestSignatureVerification( |
96 |
|
factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-384", -1)), |
97 |
|
factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-384", -1)) |
98 |
|
); |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
101 |
1 |
@Test... |
102 |
|
public void testSha512SignatureVerification() throws Exception |
103 |
|
{ |
104 |
1 |
runTestSignatureVerification( |
105 |
|
factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-512", -1)), |
106 |
|
factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-512", -1)) |
107 |
|
); |
108 |
|
} |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
110 |
1 |
@Test... |
111 |
|
public void testEncodedDefaultSignatureVerification() throws Exception |
112 |
|
{ |
113 |
1 |
Signer signer = factory.getInstance(true, privateKey); |
114 |
1 |
Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); |
115 |
|
|
116 |
1 |
runTestSignatureVerification(signer, verifier); |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
119 |
1 |
@Test... |
120 |
|
public void testEncodedSha224SignatureVerification() throws Exception |
121 |
|
{ |
122 |
1 |
Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-224", -1)); |
123 |
1 |
Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); |
124 |
|
|
125 |
1 |
runTestSignatureVerification(signer, verifier); |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
128 |
1 |
@Test... |
129 |
|
public void testEncodedSha256SignatureVerification() throws Exception |
130 |
|
{ |
131 |
1 |
Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-256", -1)); |
132 |
1 |
Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); |
133 |
|
|
134 |
1 |
runTestSignatureVerification(signer, verifier); |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
137 |
1 |
@Test... |
138 |
|
public void testEncodedSha384SignatureVerification() throws Exception |
139 |
|
{ |
140 |
1 |
Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-384", -1)); |
141 |
1 |
Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); |
142 |
|
|
143 |
1 |
runTestSignatureVerification(signer, verifier); |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void testEncodedSha512SignatureVerification() throws Exception |
148 |
|
{ |
149 |
1 |
Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-512", -1)); |
150 |
1 |
Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); |
151 |
|
|
152 |
1 |
runTestSignatureVerification(signer, verifier); |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
155 |
2 |
private void progressiveUpdateSignature(Signer signer, byte[] bytes, int blockSize) throws Exception... |
156 |
|
{ |
157 |
2 |
signer.update(bytes, 0, blockSize + 1); |
158 |
2 |
signer.update(bytes, blockSize + 1, blockSize - 1); |
159 |
2 |
signer.update(bytes, blockSize * 2, 1); |
160 |
2 |
signer.update(bytes[(blockSize * 2) + 1]); |
161 |
2 |
signer.update(bytes, ((blockSize * 2) + 2), bytes.length - ((blockSize * 2) + 2)); |
162 |
|
} |
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
164 |
1 |
@Test... |
165 |
|
public void testProgressiveSignatureVerification() throws Exception |
166 |
|
{ |
167 |
1 |
Signer signer = factory.getInstance(true, privateKey); |
168 |
1 |
progressiveUpdateSignature(signer, text, 17); |
169 |
|
|
170 |
1 |
byte[] signature = signer.generate(); |
171 |
|
|
172 |
1 |
Signer verifier = factory.getInstance(true, publicKey); |
173 |
1 |
progressiveUpdateSignature(verifier, text, 15); |
174 |
|
|
175 |
1 |
assertTrue(verifier.verify(signature)); |
176 |
|
} |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
178 |
1 |
@Test... |
179 |
|
public void testPartialBufferVerification() throws Exception |
180 |
|
{ |
181 |
1 |
Signer signer = factory.getInstance(true, privateKey); |
182 |
|
|
183 |
1 |
byte[] source = new byte[text.length * 2]; |
184 |
1 |
System.arraycopy(text, 0, source, text.length - 10, text.length); |
185 |
|
|
186 |
1 |
byte[] signature = signer.generate(source, text.length - 10, text.length); |
187 |
|
|
188 |
1 |
byte[] sign = new byte[signature.length * 2]; |
189 |
1 |
System.arraycopy(signature, 0, sign, signature.length - 5, signature.length); |
190 |
|
|
191 |
1 |
Signer verifier = factory.getInstance(true, publicKey); |
192 |
1 |
assertTrue( |
193 |
|
verifier.verify(sign, signature.length - 5, signature.length, source, text.length - 10, text.length)); |
194 |
|
} |
195 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
196 |
1 |
private int readAll(InputStream decis, byte[] out) throws IOException... |
197 |
|
{ |
198 |
1 |
int readLen = 0, len = 0; |
199 |
? |
while( (readLen = decis.read(out, len, Math.min(15, out.length - len))) > 0 ) { |
200 |
19 |
len += readLen; |
201 |
|
} |
202 |
1 |
decis.close(); |
203 |
1 |
return len; |
204 |
|
} |
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
206 |
1 |
@Test... |
207 |
|
public void testStreamSignatureVerification() throws Exception |
208 |
|
{ |
209 |
1 |
ByteArrayInputStream bais = new ByteArrayInputStream(text); |
210 |
|
|
211 |
1 |
Signer signer = factory.getInstance(true, privateKey); |
212 |
1 |
InputStream input = signer.getInputStream(bais); |
213 |
|
|
214 |
1 |
byte[] buf = new byte[text.length]; |
215 |
1 |
assertThat(readAll(input, buf), equalTo(text.length)); |
216 |
1 |
assertThat(buf, equalTo(text)); |
217 |
|
|
218 |
1 |
byte[] signature = signer.generate(); |
219 |
|
|
220 |
1 |
Signer verifier = factory.getInstance(false, publicKey); |
221 |
1 |
OutputStream output = verifier.getOutputStream(); |
222 |
|
|
223 |
1 |
output.write(text); |
224 |
1 |
assertTrue(verifier.verify(signature)); |
225 |
|
} |
226 |
|
} |