1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.signer.internal; |
21 |
|
|
22 |
|
import java.io.FilterInputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.io.OutputStream; |
26 |
|
import java.security.GeneralSecurityException; |
27 |
|
import java.security.SignatureException; |
28 |
|
|
29 |
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
30 |
|
import org.bouncycastle.crypto.CipherParameters; |
31 |
|
import org.bouncycastle.crypto.CryptoException; |
32 |
|
import org.bouncycastle.crypto.Signer; |
33 |
|
import org.bouncycastle.crypto.io.SignerInputStream; |
34 |
|
import org.bouncycastle.crypto.io.SignerOutputStream; |
35 |
|
import org.bouncycastle.operator.ContentSigner; |
36 |
|
import org.bouncycastle.operator.ContentVerifier; |
37 |
|
import org.bouncycastle.operator.RuntimeOperatorException; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 87.5% |
Uncovered Elements: 7 (56) |
Complexity: 22 |
Complexity Density: 0.59 |
|
45 |
|
public class BcSigner implements org.xwiki.crypto.signer.Signer, ContentSigner, ContentVerifier |
46 |
|
{ |
47 |
|
|
48 |
|
protected final Signer signer; |
49 |
|
|
50 |
|
|
51 |
|
protected final String signerAlgorithm; |
52 |
|
|
53 |
|
|
54 |
|
protected final boolean forSigning; |
55 |
|
|
56 |
|
|
57 |
|
protected AlgorithmIdentifier signerAlgorithmIdentifier; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
@param |
64 |
|
@param |
65 |
|
@param |
66 |
|
@param |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
68 |
118 |
public BcSigner(Signer signer, boolean forSigning, CipherParameters parameters,... |
69 |
|
String signerAlgorithm, AlgorithmIdentifier signerAlgId) |
70 |
|
{ |
71 |
118 |
this.signer = signer; |
72 |
118 |
this.signerAlgorithm = signerAlgorithm; |
73 |
118 |
this.forSigning = forSigning; |
74 |
118 |
this.signerAlgorithmIdentifier = signerAlgId; |
75 |
118 |
signer.init(forSigning, parameters); |
76 |
|
} |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
0 |
@Override... |
79 |
|
public String getAlgorithmName() |
80 |
|
{ |
81 |
0 |
return this.signerAlgorithm; |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
84 |
31 |
@Override... |
85 |
|
public AlgorithmIdentifier getAlgorithmIdentifier() |
86 |
|
{ |
87 |
31 |
return this.signerAlgorithmIdentifier; |
88 |
|
} |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
90 |
10 |
@Override... |
91 |
|
public boolean isForSigning() |
92 |
|
{ |
93 |
10 |
return this.forSigning; |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
96 |
1 |
@Override... |
97 |
|
public FilterInputStream getInputStream(InputStream is) |
98 |
|
{ |
99 |
1 |
this.signer.reset(); |
100 |
1 |
return new SignerInputStream(is, this.signer); |
101 |
|
} |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
103 |
65 |
@Override... |
104 |
|
public OutputStream getOutputStream() |
105 |
|
{ |
106 |
65 |
this.signer.reset(); |
107 |
65 |
return new SignerOutputStream(this.signer); |
108 |
|
} |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
110 |
2 |
@Override... |
111 |
|
public void update(byte input) |
112 |
|
{ |
113 |
2 |
this.signer.update(input); |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
116 |
48 |
@Override... |
117 |
|
public void update(byte[] input) |
118 |
|
{ |
119 |
48 |
this.signer.update(input, 0, input.length); |
120 |
|
} |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
10 |
@Override... |
123 |
|
public void update(byte[] input, int inputOffset, int inputLen) |
124 |
|
{ |
125 |
10 |
this.signer.update(input, inputOffset, inputLen); |
126 |
|
} |
127 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
128 |
44 |
@Override... |
129 |
|
public byte[] generate() throws GeneralSecurityException |
130 |
|
{ |
131 |
44 |
try { |
132 |
44 |
return this.signer.generateSignature(); |
133 |
|
} catch (CryptoException e) { |
134 |
0 |
throw new SignatureException(e); |
135 |
|
} |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
138 |
24 |
@Override... |
139 |
|
public byte[] generate(byte[] input) throws GeneralSecurityException |
140 |
|
{ |
141 |
24 |
update(input); |
142 |
24 |
return generate(); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
145 |
1 |
@Override... |
146 |
|
public byte[] generate(byte[] input, int inputOffset, int inputLen) throws GeneralSecurityException |
147 |
|
{ |
148 |
1 |
update(input, inputOffset, inputLen); |
149 |
1 |
return generate(); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
@inheritDoc |
154 |
|
|
155 |
|
@since |
156 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
157 |
7 |
@Override... |
158 |
|
public byte[] getSignature() |
159 |
|
{ |
160 |
7 |
try |
161 |
|
{ |
162 |
7 |
return generate(); |
163 |
|
} catch (GeneralSecurityException e) |
164 |
|
{ |
165 |
0 |
throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e); |
166 |
|
} |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
169 |
74 |
@Override... |
170 |
|
public boolean verify(byte[] signature) |
171 |
|
{ |
172 |
74 |
return this.signer.verifySignature(signature); |
173 |
|
} |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
175 |
24 |
@Override... |
176 |
|
public boolean verify(byte[] signature, byte[] input) throws GeneralSecurityException |
177 |
|
{ |
178 |
24 |
update(input); |
179 |
24 |
return verify(signature); |
180 |
|
} |
181 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
182 |
1 |
@Override... |
183 |
|
public boolean verify(byte[] signature, int signOffset, int signLen, byte[] input, int inputOffset, int inputLen) |
184 |
|
throws GeneralSecurityException |
185 |
|
{ |
186 |
1 |
update(input, inputOffset, inputLen); |
187 |
|
|
188 |
1 |
if (signOffset != 0 || signLen != signature.length) { |
189 |
1 |
byte[] sign = new byte[signLen]; |
190 |
1 |
System.arraycopy(signature, signOffset, sign, 0, signLen); |
191 |
1 |
return verify(sign); |
192 |
|
} |
193 |
|
|
194 |
0 |
return verify(signature); |
195 |
|
} |
196 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
197 |
60 |
@Override... |
198 |
|
public byte[] getEncoded() |
199 |
|
{ |
200 |
60 |
try { |
201 |
60 |
return this.signerAlgorithmIdentifier.getEncoded(); |
202 |
|
} catch (IOException e) { |
203 |
|
|
204 |
0 |
return null; |
205 |
|
} |
206 |
|
} |
207 |
|
} |