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

File AbstractBcPssSignerFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

14
21
3
1
108
67
12
0.57
7
3
4

Classes

Class Line # Actions
AbstractBcPssSignerFactory 45 21 0% 12 8
0.789473778.9%
 

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.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    * Abtract base class for factories of PSS based signature processing.
41    *
42    * @version $Id: 04273490d61e7d2c09aea4e44b55dc57fad6eb19 $
43    * @since 5.4RC1
44    */
 
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   
 
54  28 toggle @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   
 
72  28 toggle @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    // TODO: convert parameters to compatible ones
88  0 throw new UnsupportedOperationException("Cipher parameters are incompatible with this signer: "
89    + parameters.getClass().getName());
90    }
91   
 
92  64 toggle 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    }