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

File DefaultBcContentVerifierProviderBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

2
25
14
1
162
117
18
0.72
1.79
14
1.29

Classes

Class Line # Actions
DefaultBcContentVerifierProviderBuilder 52 25 0% 18 25
0.390243939%
 

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;
22   
23    import java.io.IOException;
24    import java.io.OutputStream;
25    import java.security.GeneralSecurityException;
26   
27    import javax.inject.Inject;
28    import javax.inject.Singleton;
29   
30    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
31    import org.bouncycastle.cert.X509CertificateHolder;
32    import org.bouncycastle.operator.ContentVerifier;
33    import org.bouncycastle.operator.ContentVerifierProvider;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.manager.ComponentLookupException;
36    import org.xwiki.component.manager.ComponentManager;
37    import org.xwiki.crypto.params.cipher.CipherParameters;
38    import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters;
39    import org.xwiki.crypto.pkix.internal.BcUtils;
40    import org.xwiki.crypto.pkix.params.CertifiedPublicKey;
41    import org.xwiki.crypto.signer.SignerFactory;
42    import org.xwiki.crypto.signer.internal.factory.BcSignerFactory;
43   
44    /**
45    * Bridge {@link ContentVerifierProvider} with XWiki components.
46    *
47    * @version $Id: 2f74f416a6a5b70f165e1914f9b36cfd73020d2f $
48    * @since 6.0M1
49    */
50    @Component
51    @Singleton
 
52    public class DefaultBcContentVerifierProviderBuilder implements BcContentVerifierProviderBuilder
53    {
54    @Inject
55    private ComponentManager manager;
56   
 
57  9 toggle @Override
58    public ContentVerifierProvider build(final CertifiedPublicKey certificate)
59    {
60  9 return new ContentVerifierProvider()
61    {
 
62  9 toggle @Override
63    public boolean hasAssociatedCertificate()
64    {
65  9 return true;
66    }
67   
 
68  9 toggle @Override
69    public X509CertificateHolder getAssociatedCertificate()
70    {
71  9 return BcUtils.getX509CertificateHolder(certificate);
72    }
73   
 
74  9 toggle @Override
75    public ContentVerifier get(AlgorithmIdentifier algorithm)
76    {
77  9 return getInstance(certificate.getPublicKeyParameters(), algorithm);
78    }
79    };
80    }
81   
 
82  0 toggle @Override
83    public ContentVerifierProvider build(final PublicKeyParameters publicKey)
84    {
85  0 return new ContentVerifierProvider()
86    {
 
87  0 toggle @Override
88    public boolean hasAssociatedCertificate()
89    {
90  0 return false;
91    }
92   
 
93  0 toggle @Override
94    public X509CertificateHolder getAssociatedCertificate()
95    {
96  0 return null;
97    }
98   
 
99  0 toggle @Override
100    public ContentVerifier get(AlgorithmIdentifier algorithm)
101    {
102  0 return getInstance(publicKey, algorithm);
103    }
104    };
105    }
106   
 
107  9 toggle private ContentVerifier getInstance(CipherParameters parameters, final AlgorithmIdentifier algId)
108    {
109  9 SignerFactory factory = getFactory(algId.getAlgorithm().getId());
110   
111  9 if (factory instanceof BcSignerFactory) {
112  9 return (ContentVerifier) ((BcSignerFactory) factory).getInstance(false, parameters, algId);
113    }
114   
115  0 final org.xwiki.crypto.signer.Signer signer;
116  0 try {
117  0 signer = factory.getInstance(false, parameters, algId.getEncoded());
118    } catch (IOException e) {
119    // Unlikely
120  0 throw new IllegalArgumentException("Unable to encode algorithm identifier.");
121    }
122   
123  0 return new ContentVerifier()
124    {
 
125  0 toggle @Override
126    public AlgorithmIdentifier getAlgorithmIdentifier()
127    {
128  0 return algId;
129    }
130   
 
131  0 toggle @Override
132    public OutputStream getOutputStream()
133    {
134  0 return signer.getOutputStream();
135    }
136   
 
137  0 toggle @Override
138    public boolean verify(byte[] bytes)
139    {
140  0 return DefaultBcContentVerifierProviderBuilder.verify(signer, bytes);
141    }
142    };
143    }
144   
 
145  0 toggle private static boolean verify(org.xwiki.crypto.signer.Signer signer, byte[] bytes)
146    {
147  0 try {
148  0 return signer.verify(bytes);
149    } catch (GeneralSecurityException e) {
150  0 return false;
151    }
152    }
153   
 
154  9 toggle protected SignerFactory getFactory(String hint)
155    {
156  9 try {
157  9 return this.manager.getInstance(SignerFactory.class, hint);
158    } catch (ComponentLookupException e) {
159  0 throw new UnsupportedOperationException("Signing algorithm not found.", e);
160    }
161    }
162    }