1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.internal.asymmetric.keyfactory; |
21 |
|
|
22 |
|
import java.math.BigInteger; |
23 |
|
import java.security.PrivateKey; |
24 |
|
import java.security.PublicKey; |
25 |
|
|
26 |
|
import org.bouncycastle.crypto.params.RSAKeyParameters; |
27 |
|
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters; |
28 |
|
import org.junit.Before; |
29 |
|
import org.junit.Rule; |
30 |
|
import org.junit.Test; |
31 |
|
import org.xwiki.crypto.AsymmetricKeyFactory; |
32 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
33 |
|
import org.xwiki.crypto.internal.DefaultSecureRandomProvider; |
34 |
|
import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters; |
35 |
|
import org.xwiki.crypto.internal.encoder.Base64BinaryStringEncoder; |
36 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters; |
37 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters; |
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.hamcrest.CoreMatchers.instanceOf; |
43 |
|
import static org.hamcrest.CoreMatchers.not; |
44 |
|
import static org.hamcrest.CoreMatchers.sameInstance; |
45 |
|
import static org.junit.Assert.assertThat; |
46 |
|
|
47 |
|
@ComponentList({Base64BinaryStringEncoder.class, DefaultSecureRandomProvider.class}) |
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 6 |
Complexity Density: 0.22 |
|
48 |
|
public class BcRSAKeyFactoryTest |
49 |
|
{ |
50 |
|
private static final String PRIVATE_KEY = |
51 |
|
|
52 |
|
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDCmjim/3likJ4" |
53 |
|
+ "VF564UyygqPjIX/z090AImLl0fDLUkIyCVTSd18wJ3axr1qjLtSgNPWet0puSxO" |
54 |
|
+ "FH0AzFKRCJOjUkQRU8iAkz64MLAf9xrx4nBECciqeB941s01kLtG8C/UqC3O9Sw" |
55 |
|
+ "HSdhtUpUU8V/91SiD09yNJsnODi3WqM3oLg1QYzKhoaD2mVo2xJLQ/QXqr2XIc5" |
56 |
|
+ "i2Mlpfq6S5JNbFD/I+UFhBUlBNuDOEV7ttIt2eFMEUsfkCestGo0YoQYOpTLPcP" |
57 |
|
+ "GRS7MnSY1CLWGUYqaMSnes0nS8ke2PPD4Q0suAZz4msnhNufanscstM8tcNtsZF" |
58 |
|
+ "6hj0JvbZok89szAgMBAAECggEBAKWJ1SlR5ysORDtDBXRc5HiiZEbnSGIFtYXaj" |
59 |
|
+ "N/nCsJBWBVCb+jZeirmU9bEGoB20OQ6WOjHYCnAqraQ51wMK5HgXvZBGtSMD/AH" |
60 |
|
+ "pkiF4YsOYULlXiUL2aQ4NijdvEC1sz1Cw9CAKmElb83UtZ1ZGkJnjhi35giZvU5" |
61 |
|
+ "BQRgbK5k57DFY66yv9VDg8tuD/enI9sRsCUZfCImuShGv4nLqhPMPg+1UxDPGet" |
62 |
|
+ "Vs8uEaJQ017E14wLKLA0DlED13icelU1A7ufkEdeBSv/yZ7ENjervzPwa9nITK/" |
63 |
|
+ "19uzqaHOcYZxmDQn6UHTnaLpIEaUvpp/pbed5S97ETSsqUBC8fqEUECgYEA/Sba" |
64 |
|
+ "o6efydhlXDHbXtyvaJWao19sbI9OfxGC6dR2fZiBx8Do9kVDDbMtb1PYEfLhYbi" |
65 |
|
+ "urmKGbUtcLSFgxNbZifUmG54M92nBsnsetMCqvMVNzYl2Je83V+NrIsLJjFIZ2C" |
66 |
|
+ "BvZa/FKOLDTwSe35fNqaS0ExdwcGNMIT//bDQCmyECgYEAxMq6rN+HpBRuhvvst" |
67 |
|
+ "V99zV+lI/1DzZuXExd+c3PSchiqkJrTLaQDvcaHQir9hK7RqF9vO7tvdluJjgX+" |
68 |
|
+ "f/CMPNQuC5k6vY/0fS4V2NQWtln9BBSzHtocTnZzFNq8tAZqyEhZUHIbkncroXv" |
69 |
|
+ "eUXqtlfOnKB2aYI/+3gPEMYJlH9MCgYA4exjA9r9B65QB0+Xb7mT8cpSD6uBoAD" |
70 |
|
+ "lFRITu4sZlE0exZ6sSdzWUsutqMUy+BHCguvHOWpEfhXbVYuMSR9VVYGrWMpc2B" |
71 |
|
+ "FSBG9MoBOyTHXpUZ10C7bJtW4IlyUvqkM7PV71C9MqKar2kvaUswdPTC7pZoBso" |
72 |
|
+ "GB9+M6crXxdNwQKBgDUVMlGbYi1CTaYfonQyM+8IE7WnhXiatZ+ywKtH3MZmHOw" |
73 |
|
+ "wtzIigdfZC3cvvX7i4S73vztvjdtxSaODvmiobEukOF9sj8m+YQa7Pa1lWFML5x" |
74 |
|
+ "IIu2BhGS2ZCeXgMvKkoH0x9tWaUhGqD5zZmtiDrPs75CUQBypw7SDaBzwLnld9A" |
75 |
|
+ "oGBAPgUh90PvUzbVVkzpVCPI82cmOIVMI1rDE6uCeNzIlN6Xu80RimCSaaDsESi" |
76 |
|
+ "tBtoVWLRWWmuCINyqr6e9AdyvbvT6mQCjbn9+y7t6ZAhLaya5ZMUVEBLyLLqMzr" |
77 |
|
+ "y oi/huj7m4nV4kPZz9LKxDRu3r6o0Pah+daDsTxEYObtsKa7e"; |
78 |
|
|
79 |
|
private static final String PUBLIC_KEY = |
80 |
|
|
81 |
|
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwpo4pv95YpCeFReeuFM" |
82 |
|
+ "soKj4yF/89PdACJi5dHwy1JCMglU0ndfMCd2sa9aoy7UoDT1nrdKbksThR9AMxS" |
83 |
|
+ "kQiTo1JEEVPIgJM+uDCwH/ca8eJwRAnIqngfeNbNNZC7RvAv1KgtzvUsB0nYbVK" |
84 |
|
+ "VFPFf/dUog9PcjSbJzg4t1qjN6C4NUGMyoaGg9plaNsSS0P0F6q9lyHOYtjJaX6" |
85 |
|
+ "ukuSTWxQ/yPlBYQVJQTbgzhFe7bSLdnhTBFLH5AnrLRqNGKEGDqUyz3DxkUuzJ0" |
86 |
|
+ "mNQi1hlGKmjEp3rNJ0vJHtjzw+ENLLgGc+JrJ4Tbn2p7HLLTPLXDbbGReoY9Cb2" |
87 |
|
+ "2aJPPbMwIDAQAB"; |
88 |
|
|
89 |
|
@Rule |
90 |
|
public final MockitoComponentMockingRule<AsymmetricKeyFactory> mocker = |
91 |
|
new MockitoComponentMockingRule<AsymmetricKeyFactory>(BcRSAKeyFactory.class); |
92 |
|
|
93 |
|
private AsymmetricKeyFactory factory; |
94 |
|
|
95 |
|
private static byte[] privateKey; |
96 |
|
private static byte[] publicKey; |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
98 |
4 |
@Before... |
99 |
|
public void configure() throws Exception |
100 |
|
{ |
101 |
4 |
factory = mocker.getComponentUnderTest(); |
102 |
|
|
103 |
|
|
104 |
4 |
if (privateKey == null) { |
105 |
1 |
BinaryStringEncoder base64encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64"); |
106 |
1 |
privateKey = base64encoder.decode(PRIVATE_KEY); |
107 |
1 |
publicKey = base64encoder.decode(PUBLIC_KEY); |
108 |
|
} |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
111 |
1 |
@Test... |
112 |
|
public void testPrivateKeyFromPKCS8() throws Exception |
113 |
|
{ |
114 |
1 |
PrivateKeyParameters key = factory.fromPKCS8(privateKey); |
115 |
|
|
116 |
1 |
assertThat(key, instanceOf(BcAsymmetricKeyParameters.class)); |
117 |
1 |
assertThat(((BcAsymmetricKeyParameters) key).getParameters(), instanceOf(RSAPrivateCrtKeyParameters.class)); |
118 |
1 |
assertThat(((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) key).getParameters()).getPublicExponent(), equalTo(BigInteger.valueOf(0x10001))); |
119 |
1 |
assertThat(((RSAPrivateCrtKeyParameters) ((BcAsymmetricKeyParameters) key).getParameters()).getModulus().bitLength(), equalTo(2048)); |
120 |
|
|
121 |
1 |
assertThat(key.getEncoded(), equalTo(privateKey)); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
124 |
1 |
@Test... |
125 |
|
public void testPublicKeyFromX509() throws Exception |
126 |
|
{ |
127 |
1 |
PublicKeyParameters key = factory.fromX509(publicKey); |
128 |
|
|
129 |
1 |
assertThat(key, instanceOf(BcAsymmetricKeyParameters.class)); |
130 |
1 |
assertThat(((BcAsymmetricKeyParameters) key).getParameters(), instanceOf(RSAKeyParameters.class)); |
131 |
1 |
assertThat(((RSAKeyParameters) ((BcAsymmetricKeyParameters) key).getParameters()).getExponent(), equalTo(BigInteger.valueOf(0x10001))); |
132 |
1 |
assertThat(((RSAKeyParameters) ((BcAsymmetricKeyParameters) key).getParameters()).getModulus().bitLength(), equalTo(2048)); |
133 |
1 |
assertThat(key.getEncoded(), equalTo(publicKey)); |
134 |
|
} |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
136 |
1 |
@Test... |
137 |
|
public void testPrivateKeyFromToKey() throws Exception |
138 |
|
{ |
139 |
1 |
PrivateKeyParameters key1 = factory.fromPKCS8(privateKey); |
140 |
1 |
PrivateKey pk = factory.toKey(key1); |
141 |
1 |
PrivateKeyParameters key2 = factory.fromKey(pk); |
142 |
|
|
143 |
1 |
assertThat(key1, not(sameInstance(key2))); |
144 |
1 |
assertThat(key1, equalTo(key2)); |
145 |
|
} |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
147 |
1 |
@Test... |
148 |
|
public void testPublicKeyFromToKey() throws Exception |
149 |
|
{ |
150 |
1 |
PublicKeyParameters key1 = factory.fromX509(publicKey); |
151 |
1 |
PublicKey pk = factory.toKey(key1); |
152 |
1 |
PublicKeyParameters key2 = factory.fromKey(pk); |
153 |
|
|
154 |
1 |
assertThat(key1, not(sameInstance(key2))); |
155 |
1 |
assertThat(key1, equalTo(key2)); |
156 |
|
} |
157 |
|
} |