|
| 1 | +package me.dustin.chatbot.helper; |
| 2 | + |
| 3 | +import com.google.common.primitives.Longs; |
| 4 | +import me.dustin.chatbot.network.key.KeyContainer; |
| 5 | +import me.dustin.chatbot.network.key.KeyPairResponse; |
| 6 | +import me.dustin.chatbot.network.key.PublicKeyContainer; |
| 7 | +import me.dustin.chatbot.network.key.SaltAndSig; |
| 8 | + |
| 9 | +import java.nio.ByteBuffer; |
| 10 | +import java.nio.ByteOrder; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.security.*; |
| 13 | +import java.security.spec.EncodedKeySpec; |
| 14 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 15 | +import java.time.Instant; |
| 16 | +import java.util.Base64; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +public class KeyHelper { |
| 22 | + |
| 23 | + public static KeyPairResponse getKeyPairResponse(String accessToken) { |
| 24 | + Map<String, String> headers = new HashMap<>(); |
| 25 | + headers.put("Content-Type", "application/json; charset=utf-8"); |
| 26 | + headers.put("Content-Length", "0"); |
| 27 | + headers.put("Authorization", "Bearer " + accessToken); |
| 28 | + GeneralHelper.HttpResponse httpResponse = GeneralHelper.httpRequest("https://api.minecraftservices.com/player/certificates", null, headers, "POST"); |
| 29 | + return GeneralHelper.gson.fromJson(httpResponse.data(), KeyPairResponse.class); |
| 30 | + } |
| 31 | + |
| 32 | + public static SaltAndSig sigForMessage(Instant instant, String string, PrivateKey privateKey, UUID uuid) { |
| 33 | + try { |
| 34 | + Signature signature = getSignature(privateKey); |
| 35 | + if (signature != null) { |
| 36 | + long l = new SecureRandom().nextLong(); |
| 37 | + updateSig(signature, l, uuid, instant, string); |
| 38 | + return new SaltAndSig(l, signature.sign()); |
| 39 | + } |
| 40 | + } catch (GeneralSecurityException var6) { |
| 41 | + System.out.println("Failed to sign chat message"); |
| 42 | + } |
| 43 | + |
| 44 | + return new SaltAndSig(0L, new byte[0]); |
| 45 | + } |
| 46 | + |
| 47 | + private static void updateSig(Signature signature, long l, UUID uUID, Instant instant, String string) throws SignatureException { |
| 48 | + signature.update(Longs.toByteArray(l)); |
| 49 | + signature.update(uuidToBytes(uUID.getMostSignificantBits(), uUID.getLeastSignificantBits())); |
| 50 | + signature.update(Longs.toByteArray(instant.getEpochSecond())); |
| 51 | + signature.update(string.getBytes(StandardCharsets.UTF_8)); |
| 52 | + } |
| 53 | + |
| 54 | + private static byte[] uuidToBytes(long l, long m) { |
| 55 | + ByteBuffer byteBuffer = ByteBuffer.allocate(16).order(ByteOrder.BIG_ENDIAN); |
| 56 | + byteBuffer.putLong(l).putLong(m); |
| 57 | + return byteBuffer.array(); |
| 58 | + } |
| 59 | + |
| 60 | + public static KeyContainer getKeyContainer(KeyPairResponse keyPairResponse) { |
| 61 | + return new KeyContainer(getPrivateKey(keyPairResponse.getPrivateKey()), new PublicKeyContainer(Instant.parse(keyPairResponse.getExpiresAt()), keyPairResponse.getPublicKey(), keyPairResponse.getPublicKeySignature()), Instant.parse(keyPairResponse.getRefreshedAfter())); |
| 62 | + } |
| 63 | + |
| 64 | + private static Signature getSignature(PrivateKey privateKey) throws GeneralSecurityException { |
| 65 | + if (privateKey == null) { |
| 66 | + return null; |
| 67 | + } else { |
| 68 | + Signature signature = Signature.getInstance("SHA1withRSA"); |
| 69 | + signature.initSign(privateKey); |
| 70 | + return signature; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static PrivateKey getPrivateKey(String s) { |
| 75 | + return decodePrivateKey(s); |
| 76 | + } |
| 77 | + |
| 78 | + private static PrivateKey createKey(byte[] bs) { |
| 79 | + try { |
| 80 | + EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(bs); |
| 81 | + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| 82 | + return keyFactory.generatePrivate(encodedKeySpec); |
| 83 | + } catch (Exception e) { |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + private static PrivateKey decodePrivateKey(String string) { |
| 90 | + int i = string.indexOf("-----BEGIN RSA PRIVATE KEY-----"); |
| 91 | + if (i != -1) { |
| 92 | + i += "-----BEGIN RSA PRIVATE KEY-----".length(); |
| 93 | + int j = string.indexOf("-----END RSA PRIVATE KEY-----", i); |
| 94 | + string = string.substring(i, j + 1); |
| 95 | + } |
| 96 | + |
| 97 | + return createKey(Base64.getMimeDecoder().decode(string)); |
| 98 | + } |
| 99 | + |
| 100 | + public static String decodePublicKey(String string) { |
| 101 | + int i = string.indexOf("-----BEGIN RSA PUBLIC KEY-----"); |
| 102 | + if (i != -1) { |
| 103 | + i += "-----BEGIN RSA PUBLIC KEY-----".length(); |
| 104 | + int j = string.indexOf("-----END RSA PUBLIC KEY-----", i); |
| 105 | + string = string.substring(i, j + 1); |
| 106 | + } |
| 107 | + |
| 108 | + return string; |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments