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

File AbstractBcSignerFactory.java

 

Coverage histogram

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

Code metrics

6
11
5
1
101
50
8
0.73
2.2
5
1.6

Classes

Class Line # Actions
AbstractBcSignerFactory 35 11 0% 8 6
0.7272727572.7%
 

Contributing tests

This file is covered by 48 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 org.bouncycastle.asn1.x509.AlgorithmIdentifier;
23    import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters;
24    import org.xwiki.crypto.params.cipher.CipherParameters;
25    import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricCipherParameters;
26    import org.xwiki.crypto.signer.Signer;
27    import org.xwiki.crypto.signer.internal.BcSigner;
28   
29    /**
30    * Abstract base class for signer factory of Bouncy Castle based signer.
31    *
32    * @version $Id: a183fef68fbad72243f44ea1a3b06c49701ff23b $
33    * @since 5.4RC1
34    */
 
35    public abstract class AbstractBcSignerFactory extends AbstractSignerFactory implements BcSignerFactory
36    {
37    /**
38    * @return a new native bouncy castle instance of a signer.
39    * @param parameters cipher parameters.
40    */
41    protected abstract org.bouncycastle.crypto.Signer getSignerInstance(AsymmetricCipherParameters parameters);
42   
43    /**
44    * @return a new native bouncy castle instance of a signer.
45    * @param parameters cipher parameters.
46    */
47    protected abstract AlgorithmIdentifier getSignerAlgorithmIdentifier(AsymmetricCipherParameters parameters);
48   
 
49  171 toggle protected AsymmetricCipherParameters getCipherParameters(CipherParameters parameters)
50    {
51  171 if (!(parameters instanceof AsymmetricCipherParameters)) {
52  0 throw new IllegalArgumentException("Unexpected parameters received for signer: "
53    + parameters.getClass().getName());
54    }
55   
56  171 return (AsymmetricCipherParameters) parameters;
57    }
58   
 
59  118 toggle @Override
60    public Signer getInstance(boolean forSigning, CipherParameters parameters)
61    {
62  118 return new BcSigner(getSignerInstance((AsymmetricCipherParameters) parameters), forSigning,
63    getBcCipherParameter(getCipherParameters(parameters)),
64    getSignerAlgorithmName(), getSignerAlgorithmIdentifier((AsymmetricCipherParameters) parameters));
65    }
66   
 
67  5 toggle @Override
68    public Signer getInstance(boolean forSigning, CipherParameters parameters, byte[] encoded)
69    {
70  5 return getInstance(forSigning, parameters, AlgorithmIdentifier.getInstance(encoded));
71    }
72   
 
73  53 toggle @Override
74    public Signer getInstance(boolean forSigning, CipherParameters parameters, AlgorithmIdentifier algId)
75    {
76  53 if (!algId.getAlgorithm().equals(
77    getSignerAlgorithmIdentifier(getCipherParameters(parameters)).getAlgorithm())) {
78  0 throw new IllegalArgumentException("Incompatible algorithm for this signer: "
79    + algId.getAlgorithm().getId());
80    }
81   
82  53 return getInstance(forSigning, parameters);
83    }
84   
85    /**
86    * Convert cipher parameters to Bouncy Castle equivalent.
87    *
88    * @param parameters some asymmetric cipher parameters.
89    * @return equivalent bouncy castle parameters.
90    */
 
91  90 toggle protected org.bouncycastle.crypto.CipherParameters getBcCipherParameter(AsymmetricCipherParameters parameters)
92    {
93  90 if (parameters instanceof BcAsymmetricKeyParameters) {
94  90 return ((BcAsymmetricKeyParameters) parameters).getParameters();
95    }
96   
97    // TODO: convert parameters to compatible ones
98  0 throw new UnsupportedOperationException("Cipher parameters are incompatible with this signer: "
99    + parameters.getClass().getName());
100    }
101    }