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

File BcStoreX509CertificateProvider.java

 

Coverage histogram

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

Code metrics

4
16
6
1
123
74
10
0.62
2.67
6
1.67

Classes

Class Line # Actions
BcStoreX509CertificateProvider 52 16 0% 10 4
0.8461538684.6%
 

Contributing tests

This file is covered by 12 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.pkix.internal;
22   
23    import java.math.BigInteger;
24    import java.util.ArrayList;
25    import java.util.Collection;
26   
27    import javax.inject.Inject;
28    import javax.inject.Named;
29   
30    import org.bouncycastle.cert.AttributeCertificateHolder;
31    import org.bouncycastle.cert.X509CertificateHolder;
32    import org.bouncycastle.cms.SignerId;
33    import org.bouncycastle.util.Selector;
34    import org.bouncycastle.util.Store;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.component.annotation.InstantiationStrategy;
37    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
38    import org.xwiki.crypto.pkix.CertificateFactory;
39    import org.xwiki.crypto.pkix.CertificateProvider;
40    import org.xwiki.crypto.pkix.params.CertifiedPublicKey;
41    import org.xwiki.crypto.pkix.params.PrincipalIndentifier;
42   
43    /**
44    * Adapter of a Bouncy Castle {@link Store} to a {@link CertificateProvider}.
45    *
46    * @version $Id: b0a8bd822ae721ea4ccc35a2e3e2cef25affe795 $
47    * @since 6.0M1
48    */
49    @Component
50    @Named("BCStoreX509")
51    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
52    public class BcStoreX509CertificateProvider implements CertificateProvider
53    {
54    @Inject
55    @Named("X509")
56    private CertificateFactory factory;
57   
58    private Store store;
59   
60    /**
61    * Set the store this adapter will delegate to. If no store is set, the adapter does not return any certificates.
62    *
63    * @param store the store to wrap.
64    */
 
65  19 toggle public void setStore(Store store)
66    {
67  19 this.store = store;
68    }
69   
70    /**
71    * Get the first certificate matching the provided selector.
72    *
73    * @param selector the selector.
74    * @return a certificate holder.
75    */
 
76  49 toggle public X509CertificateHolder getCertificate(Selector selector)
77    {
78  49 try {
79  49 return (X509CertificateHolder) this.store.getMatches(selector).iterator().next();
80    } catch (Throwable t) {
81  19 return null;
82    }
83    }
84   
 
85  35 toggle @Override
86    public CertifiedPublicKey getCertificate(byte[] keyIdentifier)
87    {
88  35 return BcUtils.convertCertificate(this.factory, getCertificate(new SignerId(keyIdentifier)));
89    }
90   
 
91  11 toggle @Override
92    public CertifiedPublicKey getCertificate(PrincipalIndentifier issuer, BigInteger serial)
93    {
94  11 return BcUtils.convertCertificate(this.factory,
95    getCertificate(new SignerId(BcUtils.getX500Name(issuer), serial)));
96    }
97   
 
98  0 toggle @Override
99    public CertifiedPublicKey getCertificate(PrincipalIndentifier issuer, BigInteger serial, byte[] keyIdentifier)
100    {
101  0 return BcUtils.convertCertificate(this.factory,
102    getCertificate(new SignerId(BcUtils.getX500Name(issuer), serial, keyIdentifier)));
103    }
104   
 
105  3 toggle @Override
106    public Collection<CertifiedPublicKey> getCertificate(PrincipalIndentifier subject)
107    {
108  3 AttributeCertificateHolder selector = new AttributeCertificateHolder(BcUtils.getX500Name(subject));
109   
110  3 try {
111  3 Collection<?> matches = this.store.getMatches(selector);
112  2 Collection<CertifiedPublicKey> result = new ArrayList<CertifiedPublicKey>(matches.size());
113  2 for (Object holder : matches) {
114  2 if (holder instanceof X509CertificateHolder) {
115  2 result.add(BcUtils.convertCertificate(this.factory, (X509CertificateHolder) holder));
116    }
117    }
118  2 return (!result.isEmpty()) ? result : null;
119    } catch (Throwable t) {
120  1 return null;
121    }
122    }
123    }