Skip to content

Commit 159341b

Browse files
andrii0lomakintglman
authored andcommitted
Usage of Bouncy Caster 18+ dependency.
1 parent ead23ca commit 159341b

File tree

2 files changed

+48
-100
lines changed

2 files changed

+48
-100
lines changed

server/pom.xml

+9-3
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,25 @@
6262
<artifactId>jaxb-runtime</artifactId>
6363
<version>3.0.2</version>
6464
</dependency>
65-
6665
<dependency>
66+
<groupId>org.bouncycastle</groupId>
67+
<artifactId>bcpkix-jdk18on</artifactId>
68+
<version>1.77</version>
69+
</dependency>
70+
71+
<dependency>
6772
<groupId>org.apache.httpcomponents</groupId>
6873
<artifactId>httpclient</artifactId>
6974
<version>4.5.14</version>
7075
<scope>test</scope>
71-
</dependency>
72-
<dependency>
76+
</dependency>
77+
<dependency>
7378
<groupId>org.apache.httpcomponents</groupId>
7479
<artifactId>fluent-hc</artifactId>
7580
<version>4.5.14</version>
7681
<scope>test</scope>
7782
</dependency>
83+
7884
</dependencies>
7985

8086
<profiles>

server/src/main/java/com/orientechnologies/orient/server/security/OSelfSignedCertificate.java

+39-97
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22

33
import java.io.IOException;
44
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;
614
import java.security.cert.CertificateException;
715
import java.security.cert.X509Certificate;
816
import java.util.Calendar;
917
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;
1124

1225
/**
1326
* @author Matteo Bollo (matteo.bollo-at-sap.com)
@@ -178,138 +191,67 @@ public void composeSelfSignedCertificate() {
178191
this.certificate =
179192
generateSelfSignedCertificate(
180193
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);
187196
}
188197
}
189198

190199
public static X509Certificate generateSelfSignedCertificate(
191200
KeyPair keypair, int validity, String ownerFDN, BigInteger certSN)
192201
throws CertificateException, IOException, NoSuchAlgorithmException {
193202

194-
X509CertImpl cert;
195-
196-
// Build the X.509 certificate content:
197-
X509CertInfo info = new X509CertInfo();
198203
X500Name owner;
199204
owner = new X500Name(ownerFDN);
200205

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-
230206
Date from, to;
231207
Calendar c = Calendar.getInstance();
232208
c.add(Calendar.DAY_OF_YEAR, 0);
233209
from = c.getTime();
234210
c.add(Calendar.DAY_OF_YEAR, validity);
235211
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()));
240212

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()));
247221

248222
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);
259231
}
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;
282232
}
283233

284234
public X509Certificate getCertificate() throws CertificateException {
285235

286236
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.");
291240
}
292241
return this.certificate;
293242
}
294243

295-
public static boolean checkCertificate(X509Certificate cert, PublicKey publicKey, Date date)
244+
public static void checkCertificate(X509Certificate cert, PublicKey publicKey, Date date)
296245
throws NoSuchProviderException, CertificateException, NoSuchAlgorithmException,
297246
InvalidKeyException, SignatureException {
298247
cert.checkValidity(date);
299248
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()));
307249
}
308250

309-
public boolean checkThisCertificate()
251+
public void checkThisCertificate()
310252
throws NoSuchAlgorithmException, CertificateException, NoSuchProviderException,
311253
InvalidKeyException, SignatureException {
312-
return checkCertificate(
254+
checkCertificate(
313255
this.certificate, this.keyPair.getPublic(), new Date(System.currentTimeMillis()));
314256
}
315257

0 commit comments

Comments
 (0)