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.factory; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
|
24 |
|
import org.bouncycastle.crypto.AsymmetricBlockCipher; |
25 |
|
import org.bouncycastle.crypto.Digest; |
26 |
|
import org.bouncycastle.crypto.digests.SHA1Digest; |
27 |
|
import org.bouncycastle.crypto.signers.PSSSigner; |
28 |
|
import org.xwiki.component.manager.ComponentLookupException; |
29 |
|
import org.xwiki.component.manager.ComponentManager; |
30 |
|
import org.xwiki.crypto.DigestFactory; |
31 |
|
import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters; |
32 |
|
import org.xwiki.crypto.internal.digest.factory.AbstractBcDigestFactory; |
33 |
|
import org.xwiki.crypto.internal.digest.factory.BcDigestFactory; |
34 |
|
import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricCipherParameters; |
35 |
|
import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricKeyParameters; |
36 |
|
import org.xwiki.crypto.signer.params.PssParameters; |
37 |
|
import org.xwiki.crypto.signer.params.PssSignerParameters; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 78.9% |
Uncovered Elements: 8 (38) |
Complexity: 12 |
Complexity Density: 0.57 |
|
45 |
|
public abstract class AbstractBcPssSignerFactory extends AbstractBcSignerFactory |
46 |
|
{ |
47 |
|
private static final String PSS_PARAMS_ERROR = "PSS signer parameters are invalid: "; |
48 |
|
|
49 |
|
@Inject |
50 |
|
private ComponentManager manager; |
51 |
|
|
52 |
|
protected abstract AsymmetricBlockCipher getCipherEngine(); |
53 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
54 |
28 |
@Override... |
55 |
|
protected org.bouncycastle.crypto.Signer getSignerInstance(AsymmetricCipherParameters parameters) |
56 |
|
{ |
57 |
28 |
if (parameters instanceof AsymmetricKeyParameters) { |
58 |
12 |
return new PSSSigner(getCipherEngine(), new SHA1Digest(), 20); |
59 |
16 |
} else if (parameters instanceof PssSignerParameters) { |
60 |
16 |
PssParameters pssParams = ((PssSignerParameters) parameters).getPssParameters(); |
61 |
16 |
Digest digest = getDigestFactory(pssParams.getHashAlgorithm()).getDigestInstance(); |
62 |
|
|
63 |
16 |
return new PSSSigner(getCipherEngine(), digest, |
64 |
|
getDigestFactory(pssParams.getMaskGenAlgorithm()).getDigestInstance(), |
65 |
16 |
pssParams.getSaltLength() >= 0 ? pssParams.getSaltLength() : digest.getDigestSize(), |
66 |
|
pssParams.getTrailerByte()); |
67 |
|
} |
68 |
|
|
69 |
0 |
throw new UnsupportedOperationException(PSS_PARAMS_ERROR + parameters.getClass().getName()); |
70 |
|
} |
71 |
|
|
|
|
| 78.6% |
Uncovered Elements: 3 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
72 |
28 |
@Override... |
73 |
|
protected org.bouncycastle.crypto.CipherParameters getBcCipherParameter(AsymmetricCipherParameters parameters) |
74 |
|
{ |
75 |
28 |
AsymmetricKeyParameters keyParams = null; |
76 |
|
|
77 |
28 |
if (parameters instanceof AsymmetricKeyParameters) { |
78 |
12 |
keyParams = (AsymmetricKeyParameters) parameters; |
79 |
16 |
} else if (parameters instanceof PssSignerParameters) { |
80 |
16 |
keyParams = ((PssSignerParameters) parameters).getKeyParameters(); |
81 |
|
} |
82 |
|
|
83 |
28 |
if (keyParams != null && keyParams instanceof BcAsymmetricKeyParameters) { |
84 |
28 |
return ((BcAsymmetricKeyParameters) keyParams).getParameters(); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
0 |
throw new UnsupportedOperationException("Cipher parameters are incompatible with this signer: " |
89 |
|
+ parameters.getClass().getName()); |
90 |
|
} |
91 |
|
|
|
|
| 62.5% |
Uncovered Elements: 3 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
92 |
64 |
protected BcDigestFactory getDigestFactory(String hint)... |
93 |
|
{ |
94 |
64 |
try { |
95 |
64 |
DigestFactory factory = this.manager.getInstance(DigestFactory.class, hint); |
96 |
|
|
97 |
64 |
if (!(factory instanceof BcDigestFactory)) { |
98 |
0 |
throw new IllegalArgumentException( |
99 |
|
"Requested digest algorithm is not implemented by a factory compatible with this factory." |
100 |
|
+ " Factory found: " + factory.getClass().getName()); |
101 |
|
} |
102 |
|
|
103 |
64 |
return (AbstractBcDigestFactory) factory; |
104 |
|
} catch (ComponentLookupException e) { |
105 |
0 |
throw new UnsupportedOperationException("Digest algorithm not found: " + hint, e); |
106 |
|
} |
107 |
|
} |
108 |
|
} |