1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.macro; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.security.GeneralSecurityException; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
|
27 |
|
import org.xwiki.component.manager.ComponentLookupException; |
28 |
|
import org.xwiki.component.manager.ComponentManager; |
29 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
30 |
|
import org.xwiki.crypto.pkix.CertificateProvider; |
31 |
|
import org.xwiki.crypto.signer.param.CMSSignedDataGeneratorParameters; |
32 |
|
import org.xwiki.crypto.signer.param.CMSSignedDataVerified; |
33 |
|
import org.xwiki.crypto.store.SignatureStore; |
34 |
|
import org.xwiki.crypto.store.SignatureStoreException; |
35 |
|
import org.xwiki.model.reference.BlockReferenceResolver; |
36 |
|
import org.xwiki.model.reference.EntityReference; |
37 |
|
import org.xwiki.rendering.block.Block; |
38 |
|
import org.xwiki.rendering.macro.descriptor.ContentDescriptor; |
39 |
|
import org.xwiki.rendering.signature.BlockSignatureGenerator; |
40 |
|
import org.xwiki.rendering.signature.BlockSignatureVerifier; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@link |
45 |
|
|
46 |
|
@param |
47 |
|
@version |
48 |
|
@since |
49 |
|
|
|
|
| 66.1% |
Uncovered Elements: 21 (62) |
Complexity: 27 |
Complexity Density: 0.69 |
|
50 |
|
public abstract class AbstractSignableMacro<P> extends AbstractMacro<P> implements SignableMacro |
51 |
|
{ |
52 |
|
private static final String HINT = "macro"; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Inject |
58 |
|
private ComponentManager componentManager; |
59 |
|
|
60 |
|
|
61 |
|
private SignatureStore signatureStore; |
62 |
|
private BlockSignatureGenerator signer; |
63 |
|
private BlockSignatureVerifier verifier; |
64 |
|
private BlockReferenceResolver<Block> blockResolver; |
65 |
|
|
66 |
|
|
67 |
|
@link |
68 |
|
|
69 |
|
@param |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
71 |
2 |
public AbstractSignableMacro(String name)... |
72 |
|
{ |
73 |
2 |
super(name); |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
@link |
78 |
|
|
79 |
|
@param |
80 |
|
@param |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
0 |
public AbstractSignableMacro(String name, String description)... |
83 |
|
{ |
84 |
0 |
super(name, description); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
@link |
89 |
|
|
90 |
|
@param |
91 |
|
@param |
92 |
|
@param@link |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
0 |
public AbstractSignableMacro(String name, String description,... |
95 |
|
ContentDescriptor contentDescriptor) |
96 |
|
{ |
97 |
0 |
super(name, description, contentDescriptor); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
@link |
102 |
|
|
103 |
|
@param |
104 |
|
@param |
105 |
|
@param |
106 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
107 |
0 |
public AbstractSignableMacro(String name, String description, Class<?> parametersBeanClass)... |
108 |
|
{ |
109 |
0 |
super(name, description, parametersBeanClass); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
@link |
114 |
|
|
115 |
|
@param |
116 |
|
@param |
117 |
|
@param@link |
118 |
|
@param |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
93 |
public AbstractSignableMacro(String name, String description,... |
121 |
|
ContentDescriptor contentDescriptor, Class<?> parametersBeanClass) |
122 |
|
{ |
123 |
93 |
super(name, description, contentDescriptor, parametersBeanClass); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
@return |
128 |
|
@since |
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
8636 |
protected ComponentManager getComponentManager()... |
131 |
|
{ |
132 |
8636 |
return this.componentManager; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
@return |
137 |
|
@throws |
138 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
139 |
2 |
private SignatureStore getSignatureStore() throws ComponentLookupException... |
140 |
|
{ |
141 |
2 |
if (signatureStore == null) { |
142 |
2 |
signatureStore = getComponentManager().getInstance(SignatureStore.class); |
143 |
|
} |
144 |
2 |
return signatureStore; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
@return |
149 |
|
@throws |
150 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
151 |
1 |
private BlockSignatureGenerator getSigner() throws ComponentLookupException... |
152 |
|
{ |
153 |
1 |
if (signer == null) { |
154 |
1 |
signer = getComponentManager().getInstance(BlockSignatureGenerator.class, HINT); |
155 |
|
} |
156 |
1 |
return signer; |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
@return |
161 |
|
@throws |
162 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
163 |
1 |
private BlockSignatureVerifier getVerifier() throws ComponentLookupException... |
164 |
|
{ |
165 |
1 |
if (verifier == null) { |
166 |
1 |
verifier = getComponentManager().getInstance(BlockSignatureVerifier.class, HINT); |
167 |
|
} |
168 |
1 |
return verifier; |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
@return |
173 |
|
@throws |
174 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
175 |
2 |
private BlockReferenceResolver<Block> getBlockResolver() throws ComponentLookupException... |
176 |
|
{ |
177 |
2 |
if (blockResolver == null) { |
178 |
2 |
blockResolver = getComponentManager().getInstance( |
179 |
|
new DefaultParameterizedType(null, BlockReferenceResolver.class, Block.class), |
180 |
|
"currentsignedmacro"); |
181 |
|
} |
182 |
2 |
return blockResolver; |
183 |
|
} |
184 |
|
|
|
|
| 42.9% |
Uncovered Elements: 4 (7) |
Complexity: 5 |
Complexity Density: 0.71 |
|
185 |
1 |
@Override... |
186 |
|
public void sign(Block block, CMSSignedDataGeneratorParameters parameters) throws MacroSignatureException |
187 |
|
{ |
188 |
1 |
EntityReference blockRef = getBlockReference(block); |
189 |
|
|
190 |
1 |
try { |
191 |
1 |
getSignatureStore().store(blockRef, getSigner().generate(block, parameters)); |
192 |
|
} catch (SignatureStoreException e) { |
193 |
0 |
throw new MacroSignatureException( |
194 |
|
String.format("Unable to store the signature of macro block [%s].", blockRef), e); |
195 |
|
} catch (GeneralSecurityException e) { |
196 |
0 |
throw new MacroSignatureException( |
197 |
|
String.format("Unable to compute signature of macro block [%s].", blockRef), e); |
198 |
|
} catch (IOException e) { |
199 |
0 |
throw new MacroSignatureException( |
200 |
|
String.format("Unable to encode signature of macro block [%s].", blockRef), e); |
201 |
|
} catch (ComponentLookupException e) { |
202 |
0 |
throw new MacroSignatureException( |
203 |
|
String.format("Missing components to sign macro block [%s].", blockRef), e); |
204 |
|
} |
205 |
|
} |
206 |
|
|
|
|
| 42.9% |
Uncovered Elements: 4 (7) |
Complexity: 5 |
Complexity Density: 0.71 |
|
207 |
1 |
@Override... |
208 |
|
public CMSSignedDataVerified verify(Block block, CertificateProvider certificateProvider) |
209 |
|
throws MacroSignatureException |
210 |
|
{ |
211 |
1 |
EntityReference blockRef = getBlockReference(block); |
212 |
|
|
213 |
1 |
try { |
214 |
1 |
return getVerifier().verify(getSignatureStore().retrieve(blockRef), block, certificateProvider); |
215 |
|
} catch (SignatureStoreException e) { |
216 |
0 |
throw new MacroSignatureException( |
217 |
|
String.format("Unable to retrieve the signature of macro block [%s].", blockRef), e); |
218 |
|
} catch (GeneralSecurityException e) { |
219 |
0 |
throw new MacroSignatureException( |
220 |
|
String.format("Unable to verify signature of macro block [%s].", blockRef), e); |
221 |
|
} catch (IOException e) { |
222 |
0 |
throw new MacroSignatureException( |
223 |
|
String.format("Unable to decode signature of macro block [%s].", blockRef), e); |
224 |
|
} catch (ComponentLookupException e) { |
225 |
0 |
throw new MacroSignatureException( |
226 |
|
String.format("Missing components to verify macro block [%s].", blockRef), e); |
227 |
|
} |
228 |
|
} |
229 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
230 |
2 |
private EntityReference getBlockReference(Block block) throws MacroSignatureException... |
231 |
|
{ |
232 |
2 |
EntityReference blockRef; |
233 |
|
|
234 |
2 |
try { |
235 |
2 |
blockRef = getBlockResolver().resolve(block); |
236 |
|
} catch (ComponentLookupException e) { |
237 |
0 |
throw new MacroSignatureException("Missing component to resolve macro block reference.", e); |
238 |
|
} |
239 |
|
|
240 |
2 |
if (blockRef == null) { |
241 |
0 |
throw new MacroSignatureException("Unable to determine the block reference of the macro block."); |
242 |
|
} |
243 |
|
|
244 |
2 |
return blockRef; |
245 |
|
} |
246 |
|
} |