1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.store.wiki.internal; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Named; |
24 |
|
import javax.inject.Provider; |
25 |
|
|
26 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
27 |
|
import org.xwiki.crypto.pkix.CertificateFactory; |
28 |
|
import org.xwiki.crypto.pkix.params.CertifiedPublicKey; |
29 |
|
import org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey; |
30 |
|
import org.xwiki.crypto.store.CertificateStoreException; |
31 |
|
import org.xwiki.crypto.store.StoreReference; |
32 |
|
import org.xwiki.crypto.store.WikiStoreReference; |
33 |
|
import org.xwiki.crypto.store.wiki.internal.query.CertificateObjectReference; |
34 |
|
import org.xwiki.crypto.store.wiki.internal.query.X509CertificateReferenceIssuerAndSerialQuery; |
35 |
|
import org.xwiki.crypto.store.wiki.internal.query.X509CertificateReferenceKeyIdentifierQuery; |
36 |
|
import org.xwiki.model.EntityType; |
37 |
|
import org.xwiki.model.reference.DocumentReference; |
38 |
|
import org.xwiki.model.reference.EntityReference; |
39 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
40 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
41 |
|
import org.xwiki.query.QueryManager; |
42 |
|
|
43 |
|
import com.xpn.xwiki.XWikiContext; |
44 |
|
import com.xpn.xwiki.XWikiException; |
45 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
46 |
|
import com.xpn.xwiki.objects.BaseObject; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
|
|
| 93.8% |
Uncovered Elements: 5 (80) |
Complexity: 22 |
Complexity Density: 0.43 |
|
54 |
|
public abstract class AbstractX509WikiStore |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@Inject |
60 |
|
private Provider<XWikiContext> contextProvider; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@Inject |
66 |
|
@Named("current") |
67 |
|
private EntityReferenceResolver<EntityReference> referenceResolver; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
@Named("current") |
74 |
|
private EntityReferenceResolver<String> stringReferenceResolver; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
@Inject |
80 |
|
@Named("local") |
81 |
|
private EntityReferenceSerializer<String> entityReferenceSerializer; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@Inject |
87 |
|
@Named("Base64") |
88 |
|
private BinaryStringEncoder base64; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@Inject |
94 |
|
@Named("X509") |
95 |
|
private CertificateFactory certificateFactory; |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
@Inject |
101 |
|
private QueryManager queryManager; |
102 |
|
|
103 |
|
|
104 |
|
@return |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
22 |
protected XWikiContext getXWikiContext()... |
107 |
|
{ |
108 |
22 |
return this.contextProvider.get(); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
@return |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
20 |
protected BinaryStringEncoder getEncoder()... |
115 |
|
{ |
116 |
20 |
return this.base64; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
@return |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
10 |
protected CertificateFactory getCertificateFactory()... |
123 |
|
{ |
124 |
10 |
return this.certificateFactory; |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
@return |
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
8 |
protected QueryManager getQueryManager()... |
131 |
|
{ |
132 |
8 |
return this.queryManager; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
@return |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
138 |
8 |
protected EntityReferenceSerializer<String> getSerializer()... |
139 |
|
{ |
140 |
8 |
return this.entityReferenceSerializer; |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
@param |
148 |
|
@param |
149 |
|
@return |
150 |
|
@throws |
151 |
|
|
|
|
| 88.9% |
Uncovered Elements: 3 (27) |
Complexity: 5 |
Complexity Density: 0.24 |
|
152 |
14 |
protected XWikiDocument storeCertificate(StoreReference store, CertifiedPublicKey certificate,... |
153 |
|
XWikiContext context) throws CertificateStoreException |
154 |
|
{ |
155 |
14 |
if (!(certificate instanceof X509CertifiedPublicKey)) { |
156 |
0 |
throw new IllegalArgumentException("Certificate should be X509 certificates."); |
157 |
|
} |
158 |
|
|
159 |
14 |
X509CertifiedPublicKey publicKey = (X509CertifiedPublicKey) certificate; |
160 |
|
|
161 |
14 |
try { |
162 |
14 |
CertificateObjectReference certRef = findCertificate(store, publicKey); |
163 |
|
|
164 |
14 |
XWikiDocument document; |
165 |
14 |
BaseObject obj; |
166 |
|
|
167 |
14 |
if (certRef != null) { |
168 |
7 |
document = getDocument(store, certRef, context); |
169 |
7 |
obj = document.getXObject(X509CertificateWikiStore.CERTIFICATECLASS, certRef.getObjectNumber()); |
170 |
|
} else { |
171 |
7 |
document = context.getWiki().getDocument(getDocumentReference(store, publicKey), context); |
172 |
7 |
obj = document.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, context); |
173 |
|
|
174 |
7 |
byte[] keyId = publicKey.getSubjectKeyIdentifier(); |
175 |
7 |
if (keyId != null) { |
176 |
5 |
obj.setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, this.base64.encode(keyId)); |
177 |
|
} |
178 |
7 |
obj.setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, |
179 |
|
publicKey.getIssuer().getName()); |
180 |
7 |
obj.setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, |
181 |
|
publicKey.getSerialNumber().toString()); |
182 |
7 |
obj.setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, |
183 |
|
publicKey.getSubject().getName()); |
184 |
|
} |
185 |
|
|
186 |
14 |
obj.setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, |
187 |
|
this.base64.encode(certificate.getEncoded(), 64)); |
188 |
|
|
189 |
14 |
return document; |
190 |
|
} catch (Exception e) { |
191 |
0 |
throw new CertificateStoreException("Error while preparing certificate for store [" + store + "]", e); |
192 |
|
} |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
@param |
199 |
|
@param |
200 |
|
@return |
201 |
|
@throws |
202 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
203 |
18 |
protected CertificateObjectReference findCertificate(StoreReference store, X509CertifiedPublicKey publicKey)... |
204 |
|
throws CertificateStoreException |
205 |
|
{ |
206 |
18 |
byte[] keyId = publicKey.getSubjectKeyIdentifier(); |
207 |
18 |
CertificateObjectReference certRef; |
208 |
18 |
if (keyId != null) { |
209 |
14 |
certRef = |
210 |
|
new X509CertificateReferenceKeyIdentifierQuery(resolveStore(store), this.base64, this.queryManager, |
211 |
|
this.entityReferenceSerializer) |
212 |
|
.getReference(keyId); |
213 |
|
} else { |
214 |
4 |
certRef = |
215 |
|
new X509CertificateReferenceIssuerAndSerialQuery(resolveStore(store), this.base64, this.queryManager, |
216 |
|
this.entityReferenceSerializer) |
217 |
|
.getReference(publicKey.getIssuer(), publicKey.getSerialNumber()); |
218 |
|
} |
219 |
|
|
220 |
18 |
return certRef; |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
@param |
227 |
|
@param |
228 |
|
@param |
229 |
|
@return |
230 |
|
@throws |
231 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
232 |
10 |
protected XWikiDocument getDocument(StoreReference store, CertificateObjectReference certRef, XWikiContext context)... |
233 |
|
throws XWikiException |
234 |
|
{ |
235 |
10 |
XWikiDocument document; |
236 |
10 |
document = context.getWiki().getDocument( |
237 |
|
new DocumentReference(this.stringReferenceResolver.resolve(certRef.getDocumentName(), |
238 |
|
EntityType.DOCUMENT, store)), |
239 |
|
context); |
240 |
10 |
return document; |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
@param |
247 |
|
@return |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
249 |
8 |
protected DocumentReference getDocumentReference(StoreReference store)... |
250 |
|
{ |
251 |
8 |
return new DocumentReference(this.referenceResolver.resolve(getStoreReference(store), EntityType.DOCUMENT)); |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
@param |
258 |
|
@param |
259 |
|
@return |
260 |
|
@throws |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
262 |
7 |
protected DocumentReference getDocumentReference(StoreReference store, X509CertifiedPublicKey publicKey)... |
263 |
|
throws Exception |
264 |
|
{ |
265 |
7 |
EntityReference reference = getStoreReference(store); |
266 |
|
|
267 |
7 |
if (reference.getType() == EntityType.DOCUMENT) { |
268 |
4 |
return getDocumentReference(store); |
269 |
|
} |
270 |
3 |
return new DocumentReference(this.referenceResolver.resolve( |
271 |
|
new EntityReference(getCertIdentifier(publicKey), EntityType.DOCUMENT), EntityType.DOCUMENT, reference)); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
@param |
280 |
|
@return |
281 |
|
@throws |
282 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
283 |
3 |
private String getCertIdentifier(X509CertifiedPublicKey publicKey) throws Exception... |
284 |
|
{ |
285 |
3 |
byte[] keyId = publicKey.getSubjectKeyIdentifier(); |
286 |
3 |
if (keyId != null) { |
287 |
2 |
return this.base64.encode(keyId); |
288 |
|
} |
289 |
1 |
return publicKey.getSerialNumber().toString() + ", " + publicKey.getIssuer().getName(); |
290 |
|
} |
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
@param |
296 |
|
@return |
297 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
298 |
26 |
protected EntityReference resolveStore(StoreReference store)... |
299 |
|
{ |
300 |
26 |
EntityReference reference = getStoreReference(store); |
301 |
|
|
302 |
26 |
if (reference.getType() == EntityType.DOCUMENT) { |
303 |
12 |
return this.referenceResolver.resolve(reference, EntityType.DOCUMENT); |
304 |
|
} |
305 |
|
|
306 |
14 |
return this.referenceResolver.resolve(reference, EntityType.SPACE); |
307 |
|
} |
308 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
309 |
41 |
private EntityReference getStoreReference(StoreReference store)... |
310 |
|
{ |
311 |
41 |
if (store instanceof WikiStoreReference) { |
312 |
41 |
return ((WikiStoreReference) store).getReference(); |
313 |
|
} |
314 |
0 |
throw new IllegalArgumentException("Unsupported store reference [" + store.getClass().getName() |
315 |
|
+ "] for this implementation."); |
316 |
|
} |
317 |
|
} |