forked from My-Name-Is-Jeff/SkyblockClient-Updater
-
Notifications
You must be signed in to change notification settings - Fork 6
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
6 changed files
with
105 additions
and
140 deletions.
There are no files selected for viewing
85 changes: 0 additions & 85 deletions
85
src/main/java/mynameisjeff/skyblockclientupdater/utils/ssl/FixSSL.java
This file was deleted.
Oops, something went wrong.
52 changes: 0 additions & 52 deletions
52
src/main/java/mynameisjeff/skyblockclientupdater/utils/ssl/LambdaExceptionUtils.java
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
src/main/java/mynameisjeff/skyblockclientupdater/utils/ssl/SSLStore.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,89 @@ | ||
package mynameisjeff.skyblockclientupdater.utils.ssl; | ||
|
||
import javax.net.ssl.KeyManager; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.TrustManagerFactory; | ||
import java.io.BufferedInputStream; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.KeyStore; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateFactory; | ||
|
||
/** | ||
* SSLStore attempts to work around the limitations of legacy Minecraft versions in their | ||
* collection of certificates. Since many legacy versions including 1.8 use legacy versions | ||
* of Java, these distributions tend to have outdated or missing CA certificates. | ||
* | ||
* This class loads and injects a new CA Root certificate into the normal Java KeyStore | ||
* without having to restart the Java runtime. This allows us to load the certificate | ||
* from Stage 0 (Wrapper), before any HTTP requests are made. We use this ability to | ||
* take the context that SSLStore creates and use it for all future HTTP requests | ||
* during the lifetime of the Java runtime (including for all other stages and mods). | ||
* | ||
* @author pauliesnug | ||
* @see javax.net.ssl.KeyManager | ||
* @see javax.net.ssl.SSLContext | ||
*/ | ||
public class SSLStore { | ||
private final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); | ||
private final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
|
||
public SSLStore() throws Exception { | ||
Path keyStorePath = Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts"); | ||
this.keyStore.load(Files.newInputStream(keyStorePath), (char[])null); | ||
} | ||
|
||
/** | ||
* Loads the specified SSL certificate. | ||
* | ||
* @param sslFile A .der filename | ||
* @throws Exception Uses Exception to cover the SSL loading and generation | ||
*/ | ||
public SSLStore load(String sslFile) throws Exception { | ||
InputStream certificateResource = SSLStore.class.getResourceAsStream(sslFile); | ||
Throwable sslThrowable = null; | ||
|
||
// Try to gen and load the certificate | ||
try { | ||
InputStream certStream = new BufferedInputStream(certificateResource); | ||
Certificate generatedCertificate = this.certificateFactory.generateCertificate(certStream); | ||
|
||
this.keyStore.setCertificateEntry(sslFile, generatedCertificate); | ||
} catch (Throwable sslException) { | ||
sslThrowable = sslException; | ||
throw sslException; | ||
} finally { | ||
if (certificateResource != null) { | ||
try { | ||
certificateResource.close(); | ||
} catch (Throwable closeException) { | ||
sslThrowable.addSuppressed(closeException); | ||
} | ||
} else { | ||
certificateResource.close(); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Generates and returns the SSLContext after the new cert has been added with SSLStore.load(). | ||
* | ||
* @return The SSLContext generated after init. | ||
* @throws Exception Uses Exception to cover the TMF init and SSLContext init. | ||
*/ | ||
public SSLContext finish() throws Exception { | ||
// Initialize TrustManagerFactory with the new KeyStore once the new cert has been added | ||
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
trustManagerFactory.init(this.keyStore); | ||
|
||
// Return the SSLContext after init. | ||
SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
sslContext.init((KeyManager[])null, trustManagerFactory.getTrustManagers(), null); | ||
|
||
return sslContext; | ||
} | ||
} |
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
Binary file not shown.
Binary file not shown.