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.Singleton; |
23 |
|
|
24 |
|
import org.bouncycastle.asn1.ASN1Encodable; |
25 |
|
import org.bouncycastle.asn1.ASN1Integer; |
26 |
|
import org.bouncycastle.asn1.DERNull; |
27 |
|
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
28 |
|
import org.bouncycastle.asn1.pkcs.RSASSAPSSparams; |
29 |
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
30 |
|
import org.bouncycastle.crypto.AsymmetricBlockCipher; |
31 |
|
import org.bouncycastle.crypto.engines.RSABlindedEngine; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.crypto.internal.digest.factory.BcDigestFactory; |
34 |
|
import org.xwiki.crypto.params.cipher.CipherParameters; |
35 |
|
import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricCipherParameters; |
36 |
|
import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricKeyParameters; |
37 |
|
import org.xwiki.crypto.signer.Signer; |
38 |
|
import org.xwiki.crypto.signer.params.PssParameters; |
39 |
|
import org.xwiki.crypto.signer.params.PssSignerParameters; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component(hints = { "RSASSA-PSS", "1.2.840.113549.1.1.10" }) |
48 |
|
@Singleton |
|
|
| 81.2% |
Uncovered Elements: 6 (32) |
Complexity: 9 |
Complexity Density: 0.53 |
|
49 |
|
public class BcRsaSsaPssSignerFactory extends AbstractBcPssSignerFactory |
50 |
|
{ |
51 |
|
private static final String PSS_PARAMS_ERROR = "PSS signer parameters are invalid: "; |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
28 |
@Override... |
54 |
|
protected AsymmetricBlockCipher getCipherEngine() |
55 |
|
{ |
56 |
28 |
return new RSABlindedEngine(); |
57 |
|
} |
58 |
|
|
|
|
| 73.3% |
Uncovered Elements: 4 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
59 |
6 |
@Override... |
60 |
|
public Signer getInstance(boolean forSigning, CipherParameters parameters, AlgorithmIdentifier algId) |
61 |
|
{ |
62 |
6 |
if (!algId.getAlgorithm().equals(PKCSObjectIdentifiers.id_RSASSA_PSS)) { |
63 |
0 |
throw new IllegalArgumentException("Incompatible algorithm for this signer: " |
64 |
|
+ algId.getAlgorithm().getId()); |
65 |
|
} |
66 |
|
|
67 |
6 |
ASN1Encodable algParams = algId.getParameters(); |
68 |
|
|
69 |
6 |
if (DERNull.INSTANCE.equals(algParams)) { |
70 |
2 |
return getInstance(forSigning, parameters); |
71 |
|
} else { |
72 |
4 |
RSASSAPSSparams pssParams = RSASSAPSSparams.getInstance(algId.getParameters()); |
73 |
|
|
74 |
4 |
if (parameters instanceof AsymmetricKeyParameters) { |
75 |
4 |
return getInstance(forSigning, new PssSignerParameters((AsymmetricKeyParameters) parameters, |
76 |
|
pssParams.getHashAlgorithm().getAlgorithm().getId(), |
77 |
|
AlgorithmIdentifier |
78 |
|
.getInstance(pssParams.getMaskGenAlgorithm().getParameters()).getAlgorithm().getId(), |
79 |
|
pssParams.getSaltLength().intValue(), |
80 |
|
pssParams.getTrailerField().intValue())); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
0 |
throw new UnsupportedOperationException(PSS_PARAMS_ERROR + parameters.getClass().getName()); |
85 |
|
} |
86 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
87 |
28 |
@Override... |
88 |
|
protected AlgorithmIdentifier getSignerAlgorithmIdentifier(AsymmetricCipherParameters parameters) |
89 |
|
{ |
90 |
28 |
if (parameters instanceof AsymmetricKeyParameters) { |
91 |
12 |
return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_RSASSA_PSS, DERNull.INSTANCE); |
92 |
16 |
} else if (parameters instanceof PssSignerParameters) { |
93 |
16 |
PssParameters pssParams = ((PssSignerParameters) parameters).getPssParameters(); |
94 |
16 |
BcDigestFactory factory = getDigestFactory(pssParams.getHashAlgorithm()); |
95 |
|
|
96 |
16 |
return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_RSASSA_PSS, new RSASSAPSSparams( |
97 |
|
factory.getAlgorithmIdentifier(), |
98 |
|
new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, |
99 |
|
getDigestFactory(pssParams.getMaskGenAlgorithm()).getAlgorithmIdentifier()), |
100 |
16 |
new ASN1Integer(pssParams.getSaltLength() >= 0 ? pssParams.getSaltLength() : factory.getDigestSize()), |
101 |
|
new ASN1Integer(pssParams.getTrailerField()))); |
102 |
|
} |
103 |
|
|
104 |
0 |
throw new UnsupportedOperationException(PSS_PARAMS_ERROR + parameters.getClass().getName()); |
105 |
|
} |
106 |
|
} |