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.io.IOException; |
23 |
|
import java.security.PrivateKey; |
24 |
|
import java.security.PublicKey; |
25 |
|
|
26 |
|
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; |
27 |
|
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; |
28 |
|
import org.bouncycastle.crypto.util.PrivateKeyFactory; |
29 |
|
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory; |
30 |
|
import org.bouncycastle.crypto.util.PublicKeyFactory; |
31 |
|
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter; |
32 |
|
import org.xwiki.crypto.AsymmetricKeyFactory; |
33 |
|
import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters; |
34 |
|
import org.xwiki.crypto.internal.asymmetric.BcPrivateKeyParameters; |
35 |
|
import org.xwiki.crypto.internal.asymmetric.BcPublicKeyParameters; |
36 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters; |
37 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 69.7% |
Uncovered Elements: 20 (66) |
Complexity: 20 |
Complexity Density: 0.48 |
|
45 |
|
public abstract class AbstractBcKeyFactory implements AsymmetricKeyFactory, AsymmetricKeyInfoConverter |
46 |
|
{ |
47 |
|
private static final String PRIVATE = "private"; |
48 |
|
|
49 |
|
private static final String PUBLIC = "public"; |
50 |
|
|
51 |
|
private static final String CLASS_ERROR = "Expected a %s %s key, but key class is %s."; |
52 |
|
|
53 |
|
private static final String ALGORITHM_ERROR = "Expected a %s %s key, but key algorithm is %s."; |
54 |
|
|
55 |
|
|
56 |
|
@return |
57 |
|
|
58 |
|
protected abstract AsymmetricKeyInfoConverter getKeyInfoConverter(); |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@param |
64 |
|
@return |
65 |
|
|
66 |
|
protected abstract String checkKeyType(BcAsymmetricKeyParameters key); |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
72 |
10 |
@Override... |
73 |
|
public PublicKeyParameters fromX509(byte[] encoded) throws IOException |
74 |
|
{ |
75 |
10 |
BcPublicKeyParameters key = new BcPublicKeyParameters(PublicKeyFactory.createKey(encoded)); |
76 |
10 |
String keyType = checkKeyType(key); |
77 |
10 |
if (keyType != null) { |
78 |
0 |
throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PUBLIC, |
79 |
|
key.getParameters().getClass().getName())); |
80 |
|
} |
81 |
10 |
return key; |
82 |
|
} |
83 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
84 |
26 |
@Override... |
85 |
|
public PrivateKeyParameters fromPKCS8(byte[] encoded) throws IOException |
86 |
|
{ |
87 |
26 |
BcPrivateKeyParameters key = new BcPrivateKeyParameters(PrivateKeyFactory.createKey(encoded)); |
88 |
26 |
String keyType = checkKeyType(key); |
89 |
26 |
if (keyType != null) { |
90 |
0 |
throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PRIVATE, |
91 |
|
key.getParameters().getClass().getName())); |
92 |
|
} |
93 |
26 |
return key; |
94 |
|
} |
95 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
96 |
2 |
@Override... |
97 |
|
public PublicKeyParameters fromKey(PublicKey publicKey) |
98 |
|
{ |
99 |
2 |
try { |
100 |
2 |
BcPublicKeyParameters key = new BcPublicKeyParameters(PublicKeyFactory.createKey(publicKey.getEncoded())); |
101 |
2 |
String keyType = checkKeyType(key); |
102 |
2 |
if (keyType != null) { |
103 |
0 |
throw new IllegalArgumentException(String.format(ALGORITHM_ERROR, keyType, PUBLIC, |
104 |
|
publicKey.getAlgorithm())); |
105 |
|
} |
106 |
2 |
return key; |
107 |
|
} catch (IOException e) { |
108 |
0 |
throw new IllegalArgumentException("Invalid public key: " + publicKey.getClass().getName()); |
109 |
|
} |
110 |
|
} |
111 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
112 |
2 |
@Override... |
113 |
|
public PrivateKeyParameters fromKey(PrivateKey privateKey) |
114 |
|
{ |
115 |
2 |
try { |
116 |
2 |
BcPrivateKeyParameters key = |
117 |
|
new BcPrivateKeyParameters(PrivateKeyFactory.createKey(privateKey.getEncoded())); |
118 |
2 |
String keyType = checkKeyType(key); |
119 |
2 |
if (keyType != null) { |
120 |
0 |
throw new IllegalArgumentException(String.format(ALGORITHM_ERROR, keyType, PRIVATE, |
121 |
|
privateKey.getAlgorithm())); |
122 |
|
} |
123 |
2 |
return key; |
124 |
|
} catch (IOException e) { |
125 |
0 |
throw new IllegalArgumentException("Invalid private key: " + privateKey.getClass().getName()); |
126 |
|
} |
127 |
|
} |
128 |
|
|
|
|
| 58.3% |
Uncovered Elements: 5 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
129 |
2 |
@Override... |
130 |
|
public PublicKey toKey(PublicKeyParameters key) |
131 |
|
{ |
132 |
2 |
try { |
133 |
|
|
134 |
2 |
if (key instanceof BcAsymmetricKeyParameters) { |
135 |
2 |
String keyType = checkKeyType((BcAsymmetricKeyParameters) key); |
136 |
2 |
if (keyType != null) { |
137 |
0 |
throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PUBLIC, |
138 |
|
key.getClass().getName())); |
139 |
|
} |
140 |
|
|
141 |
2 |
return generatePublic(((BcPublicKeyParameters) key).getSubjectPublicKeyInfo()); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
0 |
return generatePublic(SubjectPublicKeyInfo.getInstance(key.getEncoded())); |
146 |
|
} catch (IOException e) { |
147 |
0 |
throw new IllegalArgumentException("Invalid public key parameters: " + key.getClass().getName()); |
148 |
|
} |
149 |
|
} |
150 |
|
|
|
|
| 58.3% |
Uncovered Elements: 5 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
151 |
2 |
@Override... |
152 |
|
public PrivateKey toKey(PrivateKeyParameters key) |
153 |
|
{ |
154 |
2 |
try { |
155 |
|
|
156 |
2 |
if (key instanceof BcAsymmetricKeyParameters) { |
157 |
2 |
String keyType = checkKeyType((BcAsymmetricKeyParameters) key); |
158 |
2 |
if (keyType != null) { |
159 |
0 |
throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PRIVATE, |
160 |
|
key.getClass().getName())); |
161 |
|
} |
162 |
|
|
163 |
2 |
return generatePrivate(PrivateKeyInfoFactory.createPrivateKeyInfo( |
164 |
|
((BcAsymmetricKeyParameters) key).getParameters())); |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
0 |
return generatePrivate(PrivateKeyInfo.getInstance(key.getEncoded())); |
169 |
|
} catch (IOException e) { |
170 |
0 |
throw new IllegalArgumentException("Invalid private key parameters: " + key.getClass().getName()); |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
178 |
2 |
@Override... |
179 |
|
public PrivateKey generatePrivate(PrivateKeyInfo privateKeyInfo) throws IOException |
180 |
|
{ |
181 |
2 |
return getKeyInfoConverter().generatePrivate(privateKeyInfo); |
182 |
|
} |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
184 |
2 |
@Override... |
185 |
|
public PublicKey generatePublic(SubjectPublicKeyInfo publicKeyInfo) throws IOException |
186 |
|
{ |
187 |
2 |
return getKeyInfoConverter().generatePublic(publicKeyInfo); |
188 |
|
} |
189 |
|
} |