1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.xwiki.crypto.signer.internal.cms; |
22 |
|
|
23 |
|
import java.math.BigInteger; |
24 |
|
import java.security.GeneralSecurityException; |
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.Collection; |
27 |
|
|
28 |
|
import org.bouncycastle.asn1.x500.X500Name; |
29 |
|
import org.bouncycastle.cert.X509CertificateHolder; |
30 |
|
import org.bouncycastle.cms.SignerId; |
31 |
|
import org.bouncycastle.cms.SignerInformation; |
32 |
|
import org.bouncycastle.util.CollectionStore; |
33 |
|
import org.bouncycastle.util.Store; |
34 |
|
import org.xwiki.component.manager.ComponentLookupException; |
35 |
|
import org.xwiki.component.manager.ComponentManager; |
36 |
|
import org.xwiki.crypto.pkix.CertificateFactory; |
37 |
|
import org.xwiki.crypto.pkix.CertificateProvider; |
38 |
|
import org.xwiki.crypto.pkix.ChainingCertificateProvider; |
39 |
|
import org.xwiki.crypto.pkix.internal.BcStoreX509CertificateProvider; |
40 |
|
import org.xwiki.crypto.pkix.internal.BcUtils; |
41 |
|
import org.xwiki.crypto.pkix.params.CertifiedPublicKey; |
42 |
|
import org.xwiki.crypto.pkix.params.PrincipalIndentifier; |
43 |
|
import org.xwiki.crypto.pkix.params.x509certificate.DistinguishedName; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
|
|
| 75.9% |
Uncovered Elements: 13 (54) |
Complexity: 16 |
Complexity Density: 0.48 |
|
51 |
|
public final class BcStoreUtils |
52 |
|
{ |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
53 |
0 |
private BcStoreUtils()... |
54 |
|
{ |
55 |
|
|
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@param |
62 |
|
@param |
63 |
|
@param |
64 |
|
@return |
65 |
|
@throws |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
67 |
9 |
public static CertificateProvider getCertificateProvider(ComponentManager manager, Store store,... |
68 |
|
CertificateProvider certificateProvider) throws GeneralSecurityException |
69 |
|
{ |
70 |
9 |
CertificateProvider provider = newCertificateProvider(manager, store); |
71 |
|
|
72 |
9 |
if (certificateProvider == null) { |
73 |
3 |
return provider; |
74 |
|
} |
75 |
|
|
76 |
6 |
return new ChainingCertificateProvider(provider, certificateProvider); |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
@param |
84 |
|
@param |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
86 |
9 |
public static void addCertificatesToVerifiedData(Store store, BcCMSSignedDataVerified verifiedData,... |
87 |
|
CertificateFactory certFactory) |
88 |
|
{ |
89 |
9 |
for (X509CertificateHolder cert : getCertificates(store)) { |
90 |
10 |
verifiedData.addCertificate(BcUtils.convertCertificate(certFactory, cert)); |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
@param |
99 |
|
@return |
100 |
|
@throws |
101 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
102 |
6 |
public static CertificateProvider getCertificateProvider(ComponentManager manager,... |
103 |
|
Collection<CertifiedPublicKey> certificates) throws GeneralSecurityException |
104 |
|
{ |
105 |
6 |
if (certificates == null || certificates.isEmpty()) { |
106 |
0 |
return null; |
107 |
|
} |
108 |
|
|
109 |
6 |
Collection<X509CertificateHolder> certs = new ArrayList<X509CertificateHolder>(certificates.size()); |
110 |
|
|
111 |
6 |
for (CertifiedPublicKey cert : certificates) { |
112 |
17 |
certs.add(BcUtils.getX509CertificateHolder(cert)); |
113 |
|
} |
114 |
|
|
115 |
6 |
return newCertificateProvider(manager, new CollectionStore(certs)); |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
@param |
122 |
|
@param |
123 |
|
@return |
124 |
|
@throws |
125 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
126 |
15 |
private static CertificateProvider newCertificateProvider(ComponentManager manager, Store store)... |
127 |
|
throws GeneralSecurityException |
128 |
|
{ |
129 |
15 |
try { |
130 |
15 |
CertificateProvider provider = manager.getInstance(CertificateProvider.class, "BCStoreX509"); |
131 |
15 |
((BcStoreX509CertificateProvider) provider).setStore(store); |
132 |
|
|
133 |
15 |
return provider; |
134 |
|
} catch (ComponentLookupException e) { |
135 |
0 |
throw new GeneralSecurityException("Unable to initialize the certificates store", e); |
136 |
|
} |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
9 |
@SuppressWarnings("unchecked")... |
140 |
|
private static Collection<X509CertificateHolder> getCertificates(Store store) |
141 |
|
{ |
142 |
9 |
return store.getMatches(null); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
@param |
149 |
|
@param |
150 |
|
@param |
151 |
|
@return |
152 |
|
|
|
|
| 64% |
Uncovered Elements: 9 (25) |
Complexity: 6 |
Complexity Density: 0.4 |
|
153 |
9 |
public static CertifiedPublicKey getCertificate(CertificateProvider provider, SignerInformation signer,... |
154 |
|
CertificateFactory factory) |
155 |
|
{ |
156 |
9 |
SignerId id = signer.getSID(); |
157 |
|
|
158 |
9 |
if (provider instanceof BcStoreX509CertificateProvider) { |
159 |
3 |
X509CertificateHolder cert = ((BcStoreX509CertificateProvider) provider).getCertificate(id); |
160 |
3 |
return (cert != null) ? BcUtils.convertCertificate(factory, cert) : null; |
161 |
|
} |
162 |
|
|
163 |
6 |
X500Name bcIssuer = id.getIssuer(); |
164 |
6 |
BigInteger serial = id.getSerialNumber(); |
165 |
6 |
byte[] keyId = id.getSubjectKeyIdentifier(); |
166 |
|
|
167 |
6 |
if (bcIssuer != null) { |
168 |
6 |
PrincipalIndentifier issuer = new DistinguishedName(bcIssuer); |
169 |
6 |
if (keyId != null) { |
170 |
0 |
return provider.getCertificate(issuer, serial, keyId); |
171 |
|
} |
172 |
6 |
return provider.getCertificate(issuer, serial); |
173 |
|
} |
174 |
|
|
175 |
0 |
if (keyId != null) { |
176 |
0 |
return provider.getCertificate(keyId); |
177 |
|
} |
178 |
|
|
179 |
0 |
return null; |
180 |
|
} |
181 |
|
} |