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

File DefaultCMSSignedDataGenerator.java

 

Coverage histogram

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

Code metrics

8
24
3
1
126
85
10
0.42
8
3
3.33

Classes

Class Line # Actions
DefaultCMSSignedDataGenerator 58 24 0% 10 9
0.7428571674.3%
 

Contributing tests

This file is covered by 7 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   
21    package org.xwiki.crypto.signer.internal.cms;
22   
23    import java.io.IOException;
24    import java.security.GeneralSecurityException;
25    import java.util.ArrayList;
26    import java.util.Collection;
27    import java.util.List;
28   
29    import javax.inject.Inject;
30    import javax.inject.Singleton;
31   
32    import org.bouncycastle.cms.CMSException;
33    import org.bouncycastle.cms.CMSProcessableByteArray;
34    import org.bouncycastle.cms.CMSSignedDataGenerator;
35    import org.bouncycastle.cms.SignerInfoGeneratorBuilder;
36    import org.bouncycastle.cms.SignerInformation;
37    import org.bouncycastle.cms.SignerInformationStore;
38    import org.bouncycastle.operator.DigestCalculatorProvider;
39    import org.bouncycastle.operator.OperatorCreationException;
40    import org.xwiki.component.annotation.Component;
41    import org.xwiki.component.phase.Initializable;
42    import org.xwiki.component.phase.InitializationException;
43    import org.xwiki.crypto.DigestFactory;
44    import org.xwiki.crypto.pkix.CertifyingSigner;
45    import org.xwiki.crypto.pkix.internal.BcUtils;
46    import org.xwiki.crypto.pkix.params.CertifiedPublicKey;
47    import org.xwiki.crypto.signer.param.CMSSignedDataGeneratorParameters;
48    import org.xwiki.crypto.signer.param.CMSSignerInfo;
49   
50    /**
51    * Default implementation of {@link CMSSignedDataGenerator} based on Bouncy Castle.
52    *
53    * @version $Id: ff0e113ec2fe6847f6554bcbfc13e011b8df275b $
54    * @since 6.0M1
55    */
56    @Component
57    @Singleton
 
58    public class DefaultCMSSignedDataGenerator implements org.xwiki.crypto.signer.CMSSignedDataGenerator, Initializable
59    {
60    @Inject
61    private DigestFactory digestProvider;
62   
 
63  7 toggle @Override
64    public void initialize() throws InitializationException
65    {
66  7 if (!(this.digestProvider instanceof DigestCalculatorProvider)) {
67  0 throw new InitializationException("Incompatible DigestFactory for this signed data generator.");
68    }
69    }
70   
 
71  7 toggle @Override
72    public byte[] generate(byte[] data, CMSSignedDataGeneratorParameters parameters) throws GeneralSecurityException
73    {
74  7 return generate(data, parameters, false);
75    }
76   
 
77  9 toggle @Override
78    public byte[] generate(byte[] data, CMSSignedDataGeneratorParameters parameters, boolean embedData)
79    throws GeneralSecurityException
80    {
81  9 CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
82   
83  9 Collection<CMSSignerInfo> signersInfo = parameters.getSignatures();
84  9 if (!signersInfo.isEmpty()) {
85    // Add existing signatures
86  2 List<SignerInformation> signers = new ArrayList<SignerInformation>(parameters.getSignatures().size());
87  2 for (CMSSignerInfo signerInfo : parameters.getSignatures()) {
88  2 if (!(signerInfo instanceof BcCMSSignerInfo)) {
89  0 throw new GeneralSecurityException("Incompatible pre-calculated signature for this signed data "
90    + "generator");
91    }
92  2 signers.add(((BcCMSSignerInfo) signerInfo).getSignerInfo());
93    }
94  2 generator.addSigners(new SignerInformationStore(signers));
95    }
96   
97  9 try {
98    // Add new signers
99  9 Collection<CertifyingSigner> signers = parameters.getSigners();
100  9 for (CertifyingSigner signer : signers) {
101  7 if (signer.getAlgorithmIdentifier() == null) {
102  0 throw new GeneralSecurityException("Incompatible signer for this signed data generator for subject "
103    + signer.getCertifier().getSubject().getName());
104    }
105   
106  7 generator.addSignerInfoGenerator(
107    new SignerInfoGeneratorBuilder((DigestCalculatorProvider) this.digestProvider)
108    .build(signer, BcUtils.getX509CertificateHolder(signer.getCertifier()))
109    );
110    }
111   
112    // Add certificates
113  9 for (CertifiedPublicKey certifiedPublicKey : parameters.getCertificates()) {
114  10 generator.addCertificate(BcUtils.getX509CertificateHolder(certifiedPublicKey));
115    }
116   
117  9 return generator.generate(new CMSProcessableByteArray(data), embedData).getEncoded();
118    } catch (CMSException e) {
119  0 throw new GeneralSecurityException("Unable to generate CMS signature", e);
120    } catch (OperatorCreationException e) {
121  0 throw new GeneralSecurityException("Unable to prepare signers", e);
122    } catch (IOException e) {
123  0 throw new GeneralSecurityException("Unable to encode signed data", e);
124    }
125    }
126    }