|
2 | 2 |
|
3 | 3 | import java.io.IOException;
|
4 | 4 | import java.math.BigInteger;
|
5 |
| -import java.security.*; |
| 5 | +import java.security.InvalidKeyException; |
| 6 | +import java.security.KeyPair; |
| 7 | +import java.security.KeyPairGenerator; |
| 8 | +import java.security.NoSuchAlgorithmException; |
| 9 | +import java.security.NoSuchProviderException; |
| 10 | +import java.security.PrivateKey; |
| 11 | +import java.security.PublicKey; |
| 12 | +import java.security.SecureRandom; |
| 13 | +import java.security.SignatureException; |
6 | 14 | import java.security.cert.CertificateException;
|
7 | 15 | import java.security.cert.X509Certificate;
|
8 | 16 | import java.util.Calendar;
|
9 | 17 | import java.util.Date;
|
10 |
| -import sun.security.x509.*; |
| 18 | +import org.bouncycastle.asn1.x500.X500Name; |
| 19 | +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; |
| 20 | +import org.bouncycastle.cert.X509v3CertificateBuilder; |
| 21 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; |
| 22 | +import org.bouncycastle.operator.OperatorCreationException; |
| 23 | +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
11 | 24 |
|
12 | 25 | /**
|
13 | 26 | * @author Matteo Bollo (matteo.bollo-at-sap.com)
|
@@ -178,138 +191,67 @@ public void composeSelfSignedCertificate() {
|
178 | 191 | this.certificate =
|
179 | 192 | generateSelfSignedCertificate(
|
180 | 193 | this.keyPair, this.validity, this.ownerFDN, this.certificateSN);
|
181 |
| - } catch (CertificateException e) { |
182 |
| - e.printStackTrace(); |
183 |
| - } catch (IOException e) { |
184 |
| - e.printStackTrace(); |
185 |
| - } catch (NoSuchAlgorithmException e) { |
186 |
| - e.printStackTrace(); |
| 194 | + } catch (CertificateException | IOException | NoSuchAlgorithmException e) { |
| 195 | + throw new RuntimeException(e); |
187 | 196 | }
|
188 | 197 | }
|
189 | 198 |
|
190 | 199 | public static X509Certificate generateSelfSignedCertificate(
|
191 | 200 | KeyPair keypair, int validity, String ownerFDN, BigInteger certSN)
|
192 | 201 | throws CertificateException, IOException, NoSuchAlgorithmException {
|
193 | 202 |
|
194 |
| - X509CertImpl cert; |
195 |
| - |
196 |
| - // Build the X.509 certificate content: |
197 |
| - X509CertInfo info = new X509CertInfo(); |
198 | 203 | X500Name owner;
|
199 | 204 | owner = new X500Name(ownerFDN);
|
200 | 205 |
|
201 |
| - // set certificate VERSION |
202 |
| - try { |
203 |
| - info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3)); |
204 |
| - } catch (IOException e) { |
205 |
| - try { |
206 |
| - info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V2)); |
207 |
| - } catch (IOException ex) { |
208 |
| - info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V1)); |
209 |
| - } |
210 |
| - } |
211 |
| - |
212 |
| - // set certificate SERIAL NUMBER |
213 |
| - info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(certSN)); |
214 |
| - |
215 |
| - // set certificate SUBJECT i.e. the owner of the certificate. |
216 |
| - try { |
217 |
| - info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner)); |
218 |
| - } catch (CertificateException ignore) { |
219 |
| - info.set(X509CertInfo.SUBJECT, owner); |
220 |
| - } |
221 |
| - // set certificate ISSUER equal to SBUJECT as it is a self-signed certificate. |
222 |
| - try { |
223 |
| - info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner)); |
224 |
| - } catch (CertificateException ignore) { |
225 |
| - info.set(X509CertInfo.ISSUER, owner); |
226 |
| - } |
227 |
| - |
228 |
| - // set certificate VALIDITY from today to today+validity |
229 |
| - |
230 | 206 | Date from, to;
|
231 | 207 | Calendar c = Calendar.getInstance();
|
232 | 208 | c.add(Calendar.DAY_OF_YEAR, 0);
|
233 | 209 | from = c.getTime();
|
234 | 210 | c.add(Calendar.DAY_OF_YEAR, validity);
|
235 | 211 | to = c.getTime();
|
236 |
| - info.set(X509CertInfo.VALIDITY, new CertificateValidity(from, to)); |
237 |
| - |
238 |
| - // set certificate PUBLIC_KEY |
239 |
| - info.set(X509CertInfo.KEY, new CertificateX509Key(keypair.getPublic())); |
240 | 212 |
|
241 |
| - // set certificate Signature ALGORITHM = RSA |
242 |
| - info.set( |
243 |
| - X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(AlgorithmId.get("SHA256WithRSA"))); |
244 |
| - |
245 |
| - // Sign the cert to identify the algorithm that's used. |
246 |
| - cert = new X509CertImpl(info); |
| 213 | + var certBuilder = |
| 214 | + new X509v3CertificateBuilder( |
| 215 | + owner, |
| 216 | + certSN, |
| 217 | + from, |
| 218 | + to, |
| 219 | + owner, |
| 220 | + SubjectPublicKeyInfo.getInstance(keypair.getPublic().getEncoded())); |
247 | 221 |
|
248 | 222 | try {
|
249 |
| - cert.sign(keypair.getPrivate(), "SHA256withRSA"); |
250 |
| - // cert.sign(keyPair.getPrivate(),"SHA1withDSA"); |
251 |
| - } catch (NoSuchAlgorithmException e) { |
252 |
| - e.printStackTrace(); |
253 |
| - } catch (InvalidKeyException e) { |
254 |
| - e.printStackTrace(); |
255 |
| - } catch (NoSuchProviderException e) { |
256 |
| - e.printStackTrace(); |
257 |
| - } catch (SignatureException e) { |
258 |
| - e.printStackTrace(); |
| 223 | + var certHolder = |
| 224 | + certBuilder.build( |
| 225 | + new JcaContentSignerBuilder("SHA256WithRSA") |
| 226 | + .setProvider("BC") |
| 227 | + .build(keypair.getPrivate())); |
| 228 | + return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certHolder); |
| 229 | + } catch (OperatorCreationException e) { |
| 230 | + throw new RuntimeException(e); |
259 | 231 | }
|
260 |
| - |
261 |
| - // Update the algorithm and sign again. |
262 |
| - info.set( |
263 |
| - CertificateAlgorithmId.NAME + '.' + CertificateAlgorithmId.ALGORITHM, |
264 |
| - cert.get(X509CertImpl.SIG_ALG)); |
265 |
| - |
266 |
| - cert = new X509CertImpl(info); |
267 |
| - |
268 |
| - try { |
269 |
| - cert.sign(keypair.getPrivate(), "SHA256withRSA"); |
270 |
| - cert.verify(keypair.getPublic()); |
271 |
| - } catch (NoSuchAlgorithmException e) { |
272 |
| - e.printStackTrace(); |
273 |
| - } catch (InvalidKeyException e) { |
274 |
| - e.printStackTrace(); |
275 |
| - } catch (NoSuchProviderException e) { |
276 |
| - e.printStackTrace(); |
277 |
| - } catch (SignatureException e) { |
278 |
| - e.printStackTrace(); |
279 |
| - } |
280 |
| - |
281 |
| - return cert; |
282 | 232 | }
|
283 | 233 |
|
284 | 234 | public X509Certificate getCertificate() throws CertificateException {
|
285 | 235 |
|
286 | 236 | if (this.certificate == null) {
|
287 |
| - CertificateException cEx = |
288 |
| - new CertificateException( |
289 |
| - "The Self-Signed Certificate han not been genetated! You have to invoke the composeSelfSignedCertificate() before get it."); |
290 |
| - throw cEx; |
| 237 | + throw new CertificateException( |
| 238 | + "The Self-Signed Certificate han not been genetated! " |
| 239 | + + "You have to invoke the composeSelfSignedCertificate() before get it."); |
291 | 240 | }
|
292 | 241 | return this.certificate;
|
293 | 242 | }
|
294 | 243 |
|
295 |
| - public static boolean checkCertificate(X509Certificate cert, PublicKey publicKey, Date date) |
| 244 | + public static void checkCertificate(X509Certificate cert, PublicKey publicKey, Date date) |
296 | 245 | throws NoSuchProviderException, CertificateException, NoSuchAlgorithmException,
|
297 | 246 | InvalidKeyException, SignatureException {
|
298 | 247 | cert.checkValidity(date);
|
299 | 248 | cert.verify(publicKey);
|
300 |
| - return true; |
301 |
| - } |
302 |
| - |
303 |
| - public static boolean checkCertificate(X509Certificate cert, PublicKey publicKey) |
304 |
| - throws NoSuchProviderException, CertificateException, NoSuchAlgorithmException, |
305 |
| - InvalidKeyException, SignatureException { |
306 |
| - return checkCertificate(cert, publicKey, new Date(System.currentTimeMillis())); |
307 | 249 | }
|
308 | 250 |
|
309 |
| - public boolean checkThisCertificate() |
| 251 | + public void checkThisCertificate() |
310 | 252 | throws NoSuchAlgorithmException, CertificateException, NoSuchProviderException,
|
311 | 253 | InvalidKeyException, SignatureException {
|
312 |
| - return checkCertificate( |
| 254 | + checkCertificate( |
313 | 255 | this.certificate, this.keyPair.getPublic(), new Date(System.currentTimeMillis()));
|
314 | 256 | }
|
315 | 257 |
|
|
0 commit comments