1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.pkix.internal; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.math.BigInteger; |
24 |
|
import java.security.GeneralSecurityException; |
25 |
|
import java.security.SecureRandom; |
26 |
|
import java.util.Calendar; |
27 |
|
|
28 |
|
import org.bouncycastle.asn1.x509.TBSCertificate; |
29 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters; |
30 |
|
import org.xwiki.crypto.pkix.CertificateGenerator; |
31 |
|
import org.xwiki.crypto.pkix.CertifyingSigner; |
32 |
|
import org.xwiki.crypto.pkix.params.CertificateParameters; |
33 |
|
import org.xwiki.crypto.pkix.params.CertifiedPublicKey; |
34 |
|
import org.xwiki.crypto.pkix.params.PrincipalIndentifier; |
35 |
|
import org.xwiki.crypto.pkix.params.x509certificate.X509CertificateGenerationParameters; |
36 |
|
import org.xwiki.crypto.pkix.params.x509certificate.X509CertificateParameters; |
37 |
|
import org.xwiki.crypto.signer.Signer; |
38 |
|
import org.xwiki.crypto.signer.SignerFactory; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 94.3% |
Uncovered Elements: 2 (35) |
Complexity: 7 |
Complexity Density: 0.27 |
|
46 |
|
public abstract class AbstractBcX509CertificateGenerator implements CertificateGenerator |
47 |
|
{ |
48 |
|
private final Signer signer; |
49 |
|
|
50 |
|
private final int validity; |
51 |
|
|
52 |
|
private final SignerFactory signerFactory; |
53 |
|
|
54 |
|
private final SecureRandom random; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@param |
60 |
|
|
61 |
|
@param |
62 |
|
@param |
63 |
|
@param |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
65 |
10 |
public AbstractBcX509CertificateGenerator(Signer signer, X509CertificateGenerationParameters parameters,... |
66 |
|
SignerFactory signerFactory, SecureRandom random) |
67 |
|
{ |
68 |
10 |
this.signer = signer; |
69 |
10 |
this.validity = parameters.getValidity(); |
70 |
10 |
this.signerFactory = signerFactory; |
71 |
10 |
this.random = random; |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
@return |
76 |
|
|
77 |
|
protected abstract BcX509TBSCertificateBuilder getTBSCertificateBuilder(); |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
@param |
84 |
|
@param |
85 |
|
@param |
86 |
|
@param |
87 |
|
@throws |
88 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
89 |
3 |
protected void extendsTBSCertificate(BcX509TBSCertificateBuilder builder, CertifiedPublicKey issuer,... |
90 |
|
PrincipalIndentifier subjectName, PublicKeyParameters subject, X509CertificateParameters parameters) |
91 |
|
throws IOException |
92 |
|
{ |
93 |
|
|
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@param |
100 |
|
@param |
101 |
|
@param |
102 |
|
@return |
103 |
|
@throws |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
105 |
10 |
public TBSCertificate buildTBSCertificate(PrincipalIndentifier subjectName,... |
106 |
|
PublicKeyParameters subject, X509CertificateParameters parameters) throws IOException |
107 |
|
{ |
108 |
10 |
PrincipalIndentifier issuerName; |
109 |
10 |
CertifiedPublicKey issuer = null; |
110 |
|
|
111 |
10 |
if (this.signer instanceof CertifyingSigner) { |
112 |
4 |
issuer = ((CertifyingSigner) this.signer).getCertifier(); |
113 |
4 |
issuerName = issuer.getSubject(); |
114 |
|
} else { |
115 |
6 |
issuerName = subjectName; |
116 |
|
} |
117 |
|
|
118 |
10 |
BcX509TBSCertificateBuilder builder = getTBSCertificateBuilder(); |
119 |
|
|
120 |
10 |
builder.setSerialNumber(new BigInteger(128, this.random)).setIssuer(issuerName); |
121 |
|
|
122 |
10 |
addValidityDates(builder); |
123 |
|
|
124 |
10 |
extendsTBSCertificate(builder, issuer, subjectName, subject, parameters); |
125 |
|
|
126 |
10 |
return builder.setSubject(subjectName).setSubjectPublicKeyInfo(subject).setSignature(this.signer).build(); |
127 |
|
} |
128 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
129 |
10 |
@Override... |
130 |
|
public CertifiedPublicKey generate(PrincipalIndentifier subjectName, PublicKeyParameters subject, |
131 |
|
CertificateParameters parameters) throws IOException, GeneralSecurityException |
132 |
|
{ |
133 |
10 |
if (!(parameters instanceof X509CertificateParameters)) { |
134 |
0 |
throw new IllegalArgumentException("Invalid parameters for X.509 certificate: " |
135 |
|
+ parameters.getClass().getName()); |
136 |
|
} |
137 |
|
|
138 |
10 |
TBSCertificate tbsCert = buildTBSCertificate(subjectName, subject, (X509CertificateParameters) parameters); |
139 |
|
|
140 |
10 |
return new BcX509CertifiedPublicKey( |
141 |
|
BcUtils.getX509CertificateHolder(tbsCert, BcUtils.updateDEREncodedObject(this.signer, tbsCert).generate()), |
142 |
|
this.signerFactory); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
145 |
10 |
private void addValidityDates(BcX509TBSCertificateBuilder builder)... |
146 |
|
{ |
147 |
10 |
Calendar cal = Calendar.getInstance(); |
148 |
10 |
cal.set(Calendar.HOUR, 0); |
149 |
10 |
cal.set(Calendar.MINUTE, 0); |
150 |
10 |
cal.set(Calendar.SECOND, 0); |
151 |
|
|
152 |
10 |
builder.setStartDate(cal.getTime()); |
153 |
|
|
154 |
10 |
cal.add(Calendar.DATE, this.validity); |
155 |
|
|
156 |
10 |
builder.setEndDate(cal.getTime()); |
157 |
|
} |
158 |
|
} |