| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import java.io.UnsupportedEncodingException;
|
| 4 | import java.security.GeneralSecurityException;
|
| 5 | import java.security.InvalidKeyException;
|
| 6 | import java.security.Key;
|
| 7 | import java.security.KeyFactory;
|
| 8 | import java.security.KeyPair;
|
| 9 | import java.security.KeyPairGenerator;
|
| 10 | import java.security.MessageDigest;
|
| 11 | import java.security.NoSuchAlgorithmException;
|
| 12 | import java.security.PrivateKey;
|
| 13 | import java.security.PublicKey;
|
| 14 | import java.security.spec.AlgorithmParameterSpec;
|
| 15 | import java.security.spec.EncodedKeySpec;
|
| 16 | import java.security.spec.InvalidKeySpecException;
|
| 17 | import java.security.spec.X509EncodedKeySpec;
|
| 18 | import javax.crypto.BadPaddingException;
|
| 19 | import javax.crypto.Cipher;
|
| 20 | import javax.crypto.IllegalBlockSizeException;
|
| 21 | import javax.crypto.KeyGenerator;
|
| 22 | import javax.crypto.NoSuchPaddingException;
|
| 23 | import javax.crypto.SecretKey;
|
| 24 | import javax.crypto.spec.IvParameterSpec;
|
| 25 | import javax.crypto.spec.SecretKeySpec;
|
| 26 | import org.apache.logging.log4j.LogManager;
|
| 27 | import org.apache.logging.log4j.Logger;
|
| 28 |
|
| 29 | public class CryptManager
|
| 30 | {
|
| 31 | private static final Logger LOGGER = LogManager.getLogger();
|
| 32 |
|
| 33 | /**
|
| 34 | * Generate a new shared secret AES key from a secure random source
|
| 35 | */
|
| 36 | public static SecretKey createNewSharedKey()
|
| 37 | {
|
| 38 | try
|
| 39 | {
|
| 40 | KeyGenerator keygenerator = KeyGenerator.getInstance("AES");
|
| 41 | keygenerator.init(128);
|
| 42 | return keygenerator.generateKey();
|
| 43 | }
|
| 44 | catch (NoSuchAlgorithmException nosuchalgorithmexception)
|
| 45 | {
|
| 46 | throw new Error(nosuchalgorithmexception);
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | /**
|
| 51 | * Generates RSA KeyPair
|
| 52 | */
|
| 53 | public static KeyPair generateKeyPair()
|
| 54 | {
|
| 55 | try
|
| 56 | {
|
| 57 | KeyPairGenerator keypairgenerator = KeyPairGenerator.getInstance("RSA");
|
| 58 | keypairgenerator.initialize(1024);
|
| 59 | return keypairgenerator.generateKeyPair();
|
| 60 | }
|
| 61 | catch (NoSuchAlgorithmException nosuchalgorithmexception)
|
| 62 | {
|
| 63 | nosuchalgorithmexception.printStackTrace();
|
| 64 | LOGGER.error("Key pair generation failed!");
|
| 65 | return null;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | /**
|
| 70 | * Compute a serverId hash for use by sendSessionRequest()
|
| 71 | */
|
| 72 | public static byte[] getServerIdHash(String serverId, PublicKey publicKey, SecretKey secretKey)
|
| 73 | {
|
| 74 | try
|
| 75 | {
|
| 76 | return digestOperation("SHA-1", new byte[][] {serverId.getBytes("ISO_8859_1"), secretKey.getEncoded(), publicKey.getEncoded()});
|
| 77 | }
|
| 78 | catch (UnsupportedEncodingException unsupportedencodingexception)
|
| 79 | {
|
| 80 | unsupportedencodingexception.printStackTrace();
|
| 81 | return null;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | /**
|
| 86 | * Compute a message digest on arbitrary byte[] data
|
| 87 | */
|
| 88 | private static byte[] digestOperation(String algorithm, byte[]... data)
|
| 89 | {
|
| 90 | try
|
| 91 | {
|
| 92 | MessageDigest messagedigest = MessageDigest.getInstance(algorithm);
|
| 93 |
|
| 94 | for (byte[] abyte : data)
|
| 95 | {
|
| 96 | messagedigest.update(abyte);
|
| 97 | }
|
| 98 |
|
| 99 | return messagedigest.digest();
|
| 100 | }
|
| 101 | catch (NoSuchAlgorithmException nosuchalgorithmexception)
|
| 102 | {
|
| 103 | nosuchalgorithmexception.printStackTrace();
|
| 104 | return null;
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | /**
|
| 109 | * Create a new PublicKey from encoded X.509 data
|
| 110 | */
|
| 111 | public static PublicKey decodePublicKey(byte[] encodedKey)
|
| 112 | {
|
| 113 | try
|
| 114 | {
|
| 115 | EncodedKeySpec encodedkeyspec = new X509EncodedKeySpec(encodedKey);
|
| 116 | KeyFactory keyfactory = KeyFactory.getInstance("RSA");
|
| 117 | return keyfactory.generatePublic(encodedkeyspec);
|
| 118 | }
|
| 119 | catch (NoSuchAlgorithmException var3)
|
| 120 | {
|
| 121 | ;
|
| 122 | }
|
| 123 | catch (InvalidKeySpecException var4)
|
| 124 | {
|
| 125 | ;
|
| 126 | }
|
| 127 |
|
| 128 | LOGGER.error("Public key reconstitute failed!");
|
| 129 | return null;
|
| 130 | }
|
| 131 |
|
| 132 | /**
|
| 133 | * Decrypt shared secret AES key using RSA private key
|
| 134 | */
|
| 135 | public static SecretKey decryptSharedKey(PrivateKey key, byte[] secretKeyEncrypted)
|
| 136 | {
|
| 137 | return new SecretKeySpec(decryptData(key, secretKeyEncrypted), "AES");
|
| 138 | }
|
| 139 |
|
| 140 | /**
|
| 141 | * Encrypt byte[] data with RSA public key
|
| 142 | */
|
| 143 | public static byte[] encryptData(Key key, byte[] data)
|
| 144 | {
|
| 145 | return cipherOperation(1, key, data);
|
| 146 | }
|
| 147 |
|
| 148 | /**
|
| 149 | * Decrypt byte[] data with RSA private key
|
| 150 | */
|
| 151 | public static byte[] decryptData(Key key, byte[] data)
|
| 152 | {
|
| 153 | return cipherOperation(2, key, data);
|
| 154 | }
|
| 155 |
|
| 156 | /**
|
| 157 | * Encrypt or decrypt byte[] data using the specified key
|
| 158 | */
|
| 159 | private static byte[] cipherOperation(int opMode, Key key, byte[] data)
|
| 160 | {
|
| 161 | try
|
| 162 | {
|
| 163 | return createTheCipherInstance(opMode, key.getAlgorithm(), key).doFinal(data);
|
| 164 | }
|
| 165 | catch (IllegalBlockSizeException illegalblocksizeexception)
|
| 166 | {
|
| 167 | illegalblocksizeexception.printStackTrace();
|
| 168 | }
|
| 169 | catch (BadPaddingException badpaddingexception)
|
| 170 | {
|
| 171 | badpaddingexception.printStackTrace();
|
| 172 | }
|
| 173 |
|
| 174 | LOGGER.error("Cipher data failed!");
|
| 175 | return null;
|
| 176 | }
|
| 177 |
|
| 178 | /**
|
| 179 | * Creates the Cipher Instance.
|
| 180 | */
|
| 181 | private static Cipher createTheCipherInstance(int opMode, String transformation, Key key)
|
| 182 | {
|
| 183 | try
|
| 184 | {
|
| 185 | Cipher cipher = Cipher.getInstance(transformation);
|
| 186 | cipher.init(opMode, key);
|
| 187 | return cipher;
|
| 188 | }
|
| 189 | catch (InvalidKeyException invalidkeyexception)
|
| 190 | {
|
| 191 | invalidkeyexception.printStackTrace();
|
| 192 | }
|
| 193 | catch (NoSuchAlgorithmException nosuchalgorithmexception)
|
| 194 | {
|
| 195 | nosuchalgorithmexception.printStackTrace();
|
| 196 | }
|
| 197 | catch (NoSuchPaddingException nosuchpaddingexception)
|
| 198 | {
|
| 199 | nosuchpaddingexception.printStackTrace();
|
| 200 | }
|
| 201 |
|
| 202 | LOGGER.error("Cipher creation failed!");
|
| 203 | return null;
|
| 204 | }
|
| 205 |
|
| 206 | /**
|
| 207 | * Creates an Cipher instance using the AES/CFB8/NoPadding algorithm. Used for protocol encryption.
|
| 208 | */
|
| 209 | public static Cipher createNetCipherInstance(int opMode, Key key)
|
| 210 | {
|
| 211 | try
|
| 212 | {
|
| 213 | Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
|
| 214 | cipher.init(opMode, (Key)key, (AlgorithmParameterSpec)(new IvParameterSpec(key.getEncoded())));
|
| 215 | return cipher;
|
| 216 | }
|
| 217 | catch (GeneralSecurityException generalsecurityexception)
|
| 218 | {
|
| 219 | throw new RuntimeException(generalsecurityexception);
|
| 220 | }
|
| 221 | }
|
| 222 | }
|
| 223 |
|