forked from chhsiao90/nitmproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
247 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/com/github/chhsiao/nitm/nitmproxy/tls/CertUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.github.chhsiao.nitm.nitmproxy.tls; | ||
|
||
import org.bouncycastle.asn1.x500.X500Name; | ||
import org.bouncycastle.cert.X509CertificateHolder; | ||
import org.bouncycastle.cert.X509v3CertificateBuilder; | ||
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; | ||
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.bouncycastle.openssl.PEMKeyPair; | ||
import org.bouncycastle.openssl.PEMParser; | ||
import org.bouncycastle.openssl.PEMWriter; | ||
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; | ||
import org.bouncycastle.operator.ContentSigner; | ||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.math.BigInteger; | ||
import java.security.KeyPair; | ||
import java.security.KeyPairGenerator; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Provider; | ||
import java.security.SecureRandom; | ||
import java.security.cert.X509Certificate; | ||
import java.time.Instant; | ||
import java.time.Year; | ||
import java.time.ZoneId; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Date; | ||
|
||
public class CertUtil { | ||
private static final Provider PROVIDER = new BouncyCastleProvider(); | ||
|
||
public static Certificate newCert(String parentCertFile, String keyFile, String host) { | ||
try { | ||
Date before = Date.from(Instant.now()); | ||
Date after = Date.from(Year.now().plus(3, ChronoUnit.YEARS).atDay(1).atStartOfDay(ZoneId.systemDefault()).toInstant()); | ||
|
||
KeyPair keyPair = createKeyPair(); | ||
|
||
X509CertificateHolder parent = readPemFromFile(parentCertFile); | ||
PEMKeyPair parentPemKeyPair = readPemFromFile(keyFile); | ||
KeyPair parentKeyPair = new JcaPEMKeyConverter() | ||
.setProvider(PROVIDER) | ||
.getKeyPair(parentPemKeyPair); | ||
|
||
X509v3CertificateBuilder x509 = new JcaX509v3CertificateBuilder( | ||
parent.getSubject(), | ||
new BigInteger(64, new SecureRandom()), | ||
before, | ||
after, | ||
new X500Name("CN=" + host), | ||
keyPair.getPublic()); | ||
|
||
ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption") | ||
.build(parentKeyPair.getPrivate()); | ||
|
||
JcaX509CertificateConverter x509CertificateConverter = new JcaX509CertificateConverter() | ||
.setProvider(PROVIDER); | ||
|
||
return new Certificate( | ||
keyPair, | ||
x509CertificateConverter.getCertificate(x509.build(signer)), | ||
x509CertificateConverter.getCertificate(parent)); | ||
} catch (Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private static KeyPair createKeyPair() throws NoSuchAlgorithmException { | ||
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); | ||
keyGen.initialize(1024, new SecureRandom()); | ||
return keyGen.generateKeyPair(); | ||
} | ||
|
||
public static <T> T readPemFromFile(String pemFile) throws IOException { | ||
try (PEMParser pemParser = new PEMParser(new FileReader(pemFile))) { | ||
Object o = pemParser.readObject(); | ||
|
||
@SuppressWarnings("unchecked") | ||
T t = (T) o; | ||
return t; | ||
} | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
public static byte[] toPem(Object object) throws IOException { | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
try (PEMWriter writer = new PEMWriter(new OutputStreamWriter(outputStream))) { | ||
writer.writeObject(object); | ||
writer.flush(); | ||
return outputStream.toByteArray(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
public static byte[] toPem(Object... objects) throws IOException { | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
try (PEMWriter writer = new PEMWriter(new OutputStreamWriter(outputStream))) { | ||
for (Object object : objects) { | ||
writer.writeObject(object); | ||
} | ||
writer.flush(); | ||
return outputStream.toByteArray(); | ||
} | ||
} | ||
} |
Oops, something went wrong.