1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.xwiki.crypto.store.filesystem.internal; |
22 |
|
|
23 |
|
import java.io.BufferedReader; |
24 |
|
import java.io.BufferedWriter; |
25 |
|
import java.io.File; |
26 |
|
import java.io.IOException; |
27 |
|
import java.security.GeneralSecurityException; |
28 |
|
|
29 |
|
import javax.inject.Inject; |
30 |
|
import javax.inject.Named; |
31 |
|
|
32 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
33 |
|
import org.xwiki.crypto.pkix.CertificateFactory; |
34 |
|
import org.xwiki.crypto.pkix.params.CertifiedPublicKey; |
35 |
|
import org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey; |
36 |
|
import org.xwiki.crypto.store.FileStoreReference; |
37 |
|
import org.xwiki.crypto.store.StoreReference; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 83.1% |
Uncovered Elements: 11 (65) |
Complexity: 19 |
Complexity Density: 0.5 |
|
45 |
|
public abstract class AbstractX509FileSystemStore |
46 |
|
{ |
47 |
|
protected static final String CERTIFICATE = "CERTIFICATE"; |
48 |
|
|
49 |
|
protected static final String DASHES = "-----"; |
50 |
|
|
51 |
|
protected static final String PEM_BEGIN = DASHES + "BEGIN "; |
52 |
|
|
53 |
|
protected static final String PEM_END = DASHES + "END "; |
54 |
|
|
55 |
|
protected static final String KEY_FILE_EXTENSION = ".key"; |
56 |
|
|
57 |
|
protected static final String CERTIFICATE_FILE_EXTENSION = ".cert"; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
@Named("Base64") |
64 |
|
private BinaryStringEncoder base64; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@Inject |
70 |
|
@Named("Hex") |
71 |
|
private BinaryStringEncoder hex; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@Inject |
77 |
|
@Named("X509") |
78 |
|
private CertificateFactory certificateFactory; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@param |
84 |
|
@param |
85 |
|
@param |
86 |
|
@throws |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
88 |
8 |
protected void store(BufferedWriter out, String type, byte[] data) throws IOException... |
89 |
|
{ |
90 |
8 |
write(out, type, data); |
91 |
8 |
out.close(); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
@param |
99 |
|
@param |
100 |
|
@throws |
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
102 |
10 |
protected void write(BufferedWriter out, String type, byte[] data) throws IOException... |
103 |
|
{ |
104 |
10 |
writeHeader(out, type); |
105 |
10 |
out.write(this.base64.encode(data, 64)); |
106 |
10 |
out.newLine(); |
107 |
10 |
writeFooter(out, type); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
@param |
115 |
|
@throws |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
117 |
10 |
private static void writeHeader(BufferedWriter out, String type) throws IOException... |
118 |
|
{ |
119 |
10 |
out.write(PEM_BEGIN + type + DASHES); |
120 |
10 |
out.newLine(); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@param |
127 |
|
@param |
128 |
|
@throws |
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
130 |
10 |
private static void writeFooter(BufferedWriter out, String type) throws IOException... |
131 |
|
{ |
132 |
10 |
out.write(PEM_END + type + DASHES); |
133 |
10 |
out.newLine(); |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@return |
141 |
|
@throws |
142 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
143 |
13 |
protected File getStoreFile(StoreReference store)... |
144 |
|
{ |
145 |
13 |
if (store instanceof FileStoreReference) { |
146 |
13 |
return ((FileStoreReference) store).getFile(); |
147 |
|
} |
148 |
0 |
throw new IllegalArgumentException(String.format("Unsupported store reference [%s] for this implementation.", |
149 |
|
store.getClass().getName())); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
@param |
156 |
|
@return |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
13 |
protected boolean isMulti(StoreReference store)... |
159 |
|
{ |
160 |
13 |
return !(store instanceof FileStoreReference) || ((FileStoreReference) store).isMulti(); |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
@param |
167 |
|
@return |
168 |
|
@throws |
169 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
170 |
9 |
protected X509CertifiedPublicKey getPublicKey(CertifiedPublicKey publicKey)... |
171 |
|
{ |
172 |
9 |
if (publicKey instanceof X509CertifiedPublicKey) { |
173 |
9 |
return (X509CertifiedPublicKey) publicKey; |
174 |
|
} |
175 |
|
|
176 |
0 |
throw new IllegalArgumentException(String.format("Unsupported certificate [%s], expecting X509 certificates.", |
177 |
|
publicKey.getClass().getName())); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
@param |
186 |
|
@return |
187 |
|
@throws |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
189 |
7 |
protected String getCertIdentifier(X509CertifiedPublicKey publicKey) throws IOException... |
190 |
|
{ |
191 |
7 |
byte[] keyId = publicKey.getSubjectKeyIdentifier(); |
192 |
7 |
if (keyId != null) { |
193 |
6 |
return this.hex.encode(keyId); |
194 |
|
} |
195 |
1 |
return publicKey.getSerialNumber().toString() + ", " + publicKey.getIssuer().getName(); |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
@param |
202 |
|
@param |
203 |
|
@return |
204 |
|
@throws |
205 |
|
@throws |
206 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
207 |
13 |
protected Object readObject(BufferedReader in, byte[] password) throws IOException, GeneralSecurityException... |
208 |
|
{ |
209 |
13 |
String line; |
210 |
13 |
Object obj = null; |
211 |
|
|
212 |
? |
while ((line = in.readLine()) != null) { |
213 |
9 |
obj = processObject(in, line, password); |
214 |
9 |
if (obj != null) { |
215 |
9 |
break; |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
13 |
return obj; |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
@param |
226 |
|
@param |
227 |
|
@param |
228 |
|
@return |
229 |
|
@throws |
230 |
|
@throws |
231 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
232 |
3 |
protected Object processObject(BufferedReader in, String line, byte[] password)... |
233 |
|
throws IOException, GeneralSecurityException |
234 |
|
{ |
235 |
3 |
if (line.contains(PEM_BEGIN + CERTIFICATE + DASHES)) { |
236 |
3 |
return this.certificateFactory.decode(readBytes(in, PEM_END + CERTIFICATE + DASHES)); |
237 |
|
} |
238 |
0 |
return null; |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
@param |
245 |
|
@param |
246 |
|
@return |
247 |
|
@throws |
248 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
249 |
9 |
protected byte[] readBytes(BufferedReader in, String endMarker) throws IOException... |
250 |
|
{ |
251 |
9 |
String line; |
252 |
9 |
StringBuilder buf = new StringBuilder(); |
253 |
|
|
254 |
? |
while ((line = in.readLine()) != null) { |
255 |
18 |
if (line.contains(endMarker)) { |
256 |
9 |
break; |
257 |
|
} |
258 |
9 |
buf.append(line.trim()); |
259 |
|
} |
260 |
|
|
261 |
9 |
return this.base64.decode(buf.toString()); |
262 |
|
} |
263 |
|
} |