1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.signer.internal.factory

File BcRsaSsaPssSignerFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
17
3
1
106
69
9
0.53
5.67
3
3

Classes

Class Line # Actions
BcRsaSsaPssSignerFactory 49 17 0% 9 6
0.812581.2%
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Factory for RSASSA-PSS signature processing.
43    *
44    * @version $Id: 40431c16ca0af96fca11c8be6cb0846c3ea9c0bc $
45    * @since 5.4RC1
46    */
47    @Component(hints = { "RSASSA-PSS", "1.2.840.113549.1.1.10" })
48    @Singleton
 
49    public class BcRsaSsaPssSignerFactory extends AbstractBcPssSignerFactory
50    {
51    private static final String PSS_PARAMS_ERROR = "PSS signer parameters are invalid: ";
52   
 
53  28 toggle @Override
54    protected AsymmetricBlockCipher getCipherEngine()
55    {
56  28 return new RSABlindedEngine();
57    }
58   
 
59  6 toggle @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   
 
87  28 toggle @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    }