Skip to content

Commit

Permalink
RSAPadding: hacky attempt to fix
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduytran0 committed Jul 27, 2020
1 parent 39c3281 commit 120a3d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
36 changes: 23 additions & 13 deletions app/src/main/java/net/kdt/pojavlaunch/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -983,10 +983,8 @@ private void runCraft() throws Throwable
net.minecraft.launchwrapper.Launch.main(launchArgs);
} else {
LoggerJava.OnStringPrintListener printLog = new LoggerJava.OnStringPrintListener(){

@Override
public void onCharPrint(char c)
{
public void onCharPrint(char c) {
appendToLog(Character.toString(c));
}
};
Expand Down Expand Up @@ -1020,19 +1018,25 @@ public void fixRSAPadding() throws Exception {
// welcome to the territory of YOLO; I'll be your tour guide for today.

try {
List<Provider.Service> rsaList, rsaPkcs1List;
if (android.os.Build.VERSION.SDK_INT > 23) { // Nougat
// GetInstance.getServices("KeyPairGenerator", algorithm);
// Since Android 7, it use sun.security.jca.GetInstance

Class.forName("sun.security.jca.GetInstance")
.getDeclaredMethod("getServices", String.class, String.class)
.invoke(null, new Object[]{"Cipher", "RSA"});
/*
* Since Android 7, it use OpenJDK sun.security.jca.GetInstance
* But it's not part of Android SDK.
*/
rsaList = getCipherServices("RSA");
rsaPkcs1List = getCipherServices("RSA/ECB/PKCS1PADDING");
} else {
ArrayList<Provider.Service> rsaList = Services.getServices("Cipher.RSA");
ArrayList<Provider.Service> rsaPkcs1List = Services.getServices("Cipher.RSA/ECB/PKCS1PADDING");
rsaList.clear();
rsaList.addAll(rsaPkcs1List);
rsaList = Services.getServices("Cipher.RSA");
rsaPkcs1List = Services.getServices("Cipher.RSA/ECB/PKCS1PADDING");
}

/*
* Not .clear() directly since the entry removal is protected,
* so some reflections to reset it
*/
Modifiable.resetServiceList(rsaList);
rsaList.addAll(rsaPkcs1List);
} catch (Throwable th) {
th.printStackTrace();

Expand Down Expand Up @@ -1072,6 +1076,12 @@ public void run() {
*/
}

private List<Provider.Service> getCipherServices(String algorithm) throws InvocationTargetException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, ClassNotFoundException {
return (List<Provider.Service>) Class.forName("sun.security.jca.GetInstance")
.getDeclaredMethod("getServices", String.class, String.class)
.invoke(null, new Object[]{"Cipher", algorithm});
}

private void debug_printMethodInfo(PrintStream stream, Method[] methods) {
StringBuilder methodInfo = new StringBuilder();
for (Method method : methods) {
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/net/kdt/pojavlaunch/Modifiable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;
import java.lang.reflect.*;
import java.security.*;

// This class simply get the modifiable original Collection from unmodifable one.
public class Modifiable
Expand Down Expand Up @@ -29,7 +30,27 @@ public static <E> Collection<E> modifyCollection(Collection<? extends E> collect
return (Collection<E>) collection;
}

public static <E> List<E> modifyList(List<? extends E> list) {
return (List<E>) modifyCollection(list);
}

public static <E> Set<E> modifySet(Set<? extends E> set) {
return (Set<E>) modifyCollection(set);
}

// Get modifiable list from ServiceList
public static void resetServiceList(List<Provider.Service> list) throws Throwable {
Class<?> listClass = Class.forName("sun.security.jca.ProviderList$ServiceList");
Field servicesField = listClass.getDeclaredField("services");
Field firstServiceField = listClass.getDeclaredField("firstService");

servicesField.setAccessible(true);
firstServiceField.setAccessible(true);

List<Provider.Service> services = (List<Provider.Service>) servicesField.get(list);
firstServiceField.set(list, null);
if (services == null) {
System.err.println("ServiceList is null, how to erase?");
} else services.clear();
}
}

0 comments on commit 120a3d3

Please sign in to comment.