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

File BcSigner.java

 

Coverage histogram

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

Code metrics

2
37
17
1
207
138
22
0.59
2.18
17
1.29

Classes

Class Line # Actions
BcSigner 45 37 0% 22 7
0.87587.5%
 

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;
21   
22    import java.io.FilterInputStream;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.OutputStream;
26    import java.security.GeneralSecurityException;
27    import java.security.SignatureException;
28   
29    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
30    import org.bouncycastle.crypto.CipherParameters;
31    import org.bouncycastle.crypto.CryptoException;
32    import org.bouncycastle.crypto.Signer;
33    import org.bouncycastle.crypto.io.SignerInputStream;
34    import org.bouncycastle.crypto.io.SignerOutputStream;
35    import org.bouncycastle.operator.ContentSigner;
36    import org.bouncycastle.operator.ContentVerifier;
37    import org.bouncycastle.operator.RuntimeOperatorException;
38   
39    /**
40    * Bouncy Castle based signer.
41    *
42    * @version $Id: 3604d470e8d9d6e831428b0871613791833e40f0 $
43    * @since 5.4RC1
44    */
 
45    public class BcSigner implements org.xwiki.crypto.signer.Signer, ContentSigner, ContentVerifier
46    {
47    /** The underlying signer. */
48    protected final Signer signer;
49   
50    /** The underlying signer. */
51    protected final String signerAlgorithm;
52   
53    /** True if the signer is initialized for signing. */
54    protected final boolean forSigning;
55   
56    /** Algorithm identifier of this signer ASN.1 encoded. */
57    protected AlgorithmIdentifier signerAlgorithmIdentifier;
58   
59    /**
60    * Generic Bouncy Castle based signer.
61    *
62    * @param signer the signer to encapsulate.
63    * @param forSigning true if the signer is setup for signing.
64    * @param parameters parameters to initialize the cipher.
65    * @param signerAlgorithm the name of the algorithm implemented by this signer.
66    * @param signerAlgId the algorithm identifier of the algorithm implemented by this signer.
67    */
 
68  118 toggle public BcSigner(Signer signer, boolean forSigning, CipherParameters parameters,
69    String signerAlgorithm, AlgorithmIdentifier signerAlgId)
70    {
71  118 this.signer = signer;
72  118 this.signerAlgorithm = signerAlgorithm;
73  118 this.forSigning = forSigning;
74  118 this.signerAlgorithmIdentifier = signerAlgId;
75  118 signer.init(forSigning, parameters);
76    }
77   
 
78  0 toggle @Override
79    public String getAlgorithmName()
80    {
81  0 return this.signerAlgorithm;
82    }
83   
 
84  31 toggle @Override
85    public AlgorithmIdentifier getAlgorithmIdentifier()
86    {
87  31 return this.signerAlgorithmIdentifier;
88    }
89   
 
90  10 toggle @Override
91    public boolean isForSigning()
92    {
93  10 return this.forSigning;
94    }
95   
 
96  1 toggle @Override
97    public FilterInputStream getInputStream(InputStream is)
98    {
99  1 this.signer.reset();
100  1 return new SignerInputStream(is, this.signer);
101    }
102   
 
103  65 toggle @Override
104    public OutputStream getOutputStream()
105    {
106  65 this.signer.reset();
107  65 return new SignerOutputStream(this.signer);
108    }
109   
 
110  2 toggle @Override
111    public void update(byte input)
112    {
113  2 this.signer.update(input);
114    }
115   
 
116  48 toggle @Override
117    public void update(byte[] input)
118    {
119  48 this.signer.update(input, 0, input.length);
120    }
121   
 
122  10 toggle @Override
123    public void update(byte[] input, int inputOffset, int inputLen)
124    {
125  10 this.signer.update(input, inputOffset, inputLen);
126    }
127   
 
128  44 toggle @Override
129    public byte[] generate() throws GeneralSecurityException
130    {
131  44 try {
132  44 return this.signer.generateSignature();
133    } catch (CryptoException e) {
134  0 throw new SignatureException(e);
135    }
136    }
137   
 
138  24 toggle @Override
139    public byte[] generate(byte[] input) throws GeneralSecurityException
140    {
141  24 update(input);
142  24 return generate();
143    }
144   
 
145  1 toggle @Override
146    public byte[] generate(byte[] input, int inputOffset, int inputLen) throws GeneralSecurityException
147    {
148  1 update(input, inputOffset, inputLen);
149  1 return generate();
150    }
151   
152    /**
153    * {@inheritDoc}
154    *
155    * @since 6.0M1
156    */
 
157  7 toggle @Override
158    public byte[] getSignature()
159    {
160  7 try
161    {
162  7 return generate();
163    } catch (GeneralSecurityException e)
164    {
165  0 throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
166    }
167    }
168   
 
169  74 toggle @Override
170    public boolean verify(byte[] signature)
171    {
172  74 return this.signer.verifySignature(signature);
173    }
174   
 
175  24 toggle @Override
176    public boolean verify(byte[] signature, byte[] input) throws GeneralSecurityException
177    {
178  24 update(input);
179  24 return verify(signature);
180    }
181   
 
182  1 toggle @Override
183    public boolean verify(byte[] signature, int signOffset, int signLen, byte[] input, int inputOffset, int inputLen)
184    throws GeneralSecurityException
185    {
186  1 update(input, inputOffset, inputLen);
187   
188  1 if (signOffset != 0 || signLen != signature.length) {
189  1 byte[] sign = new byte[signLen];
190  1 System.arraycopy(signature, signOffset, sign, 0, signLen);
191  1 return verify(sign);
192    }
193   
194  0 return verify(signature);
195    }
196   
 
197  60 toggle @Override
198    public byte[] getEncoded()
199    {
200  60 try {
201  60 return this.signerAlgorithmIdentifier.getEncoded();
202    } catch (IOException e) {
203    // Very unlikely to happen
204  0 return null;
205    }
206    }
207    }