| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.primitives.Longs;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import it.unimi.dsi.fastutil.bytes.ByteArrays;
|
| 7 | import java.nio.charset.StandardCharsets;
|
| 8 | import java.security.Key;
|
| 9 | import java.security.KeyFactory;
|
| 10 | import java.security.KeyPair;
|
| 11 | import java.security.KeyPairGenerator;
|
| 12 | import java.security.MessageDigest;
|
| 13 | import java.security.PrivateKey;
|
| 14 | import java.security.PublicKey;
|
| 15 | import java.security.SecureRandom;
|
| 16 | import java.security.spec.EncodedKeySpec;
|
| 17 | import java.security.spec.PKCS8EncodedKeySpec;
|
| 18 | import java.security.spec.X509EncodedKeySpec;
|
| 19 | import java.util.Base64;
|
| 20 | import java.util.Base64.Encoder;
|
| 21 | import javax.crypto.Cipher;
|
| 22 | import javax.crypto.KeyGenerator;
|
| 23 | import javax.crypto.SecretKey;
|
| 24 | import javax.crypto.spec.IvParameterSpec;
|
| 25 | import javax.crypto.spec.SecretKeySpec;
|
| 26 | import net.minecraft.network.FriendlyByteBuf;
|
| 27 |
|
| 28 | public class Crypt {
|
| 29 | private static final String SYMMETRIC_ALGORITHM = "AES";
|
| 30 | private static final int SYMMETRIC_BITS = 128;
|
| 31 | private static final String ASYMMETRIC_ALGORITHM = "RSA";
|
| 32 | private static final int ASYMMETRIC_BITS = 1024;
|
| 33 | private static final String BYTE_ENCODING = "ISO_8859_1";
|
| 34 | private static final String HASH_ALGORITHM = "SHA-1";
|
| 35 | public static final String SIGNING_ALGORITHM = "SHA256withRSA";
|
| 36 | public static final int SIGNATURE_BYTES = 256;
|
| 37 | private static final String PEM_RSA_PRIVATE_KEY_HEADER = "-----BEGIN RSA PRIVATE KEY-----";
|
| 38 | private static final String PEM_RSA_PRIVATE_KEY_FOOTER = "-----END RSA PRIVATE KEY-----";
|
| 39 | public static final String RSA_PUBLIC_KEY_HEADER = "-----BEGIN RSA PUBLIC KEY-----";
|
| 40 | private static final String RSA_PUBLIC_KEY_FOOTER = "-----END RSA PUBLIC KEY-----";
|
| 41 | public static final String MIME_LINE_SEPARATOR = "\n";
|
| 42 | public static final Encoder MIME_ENCODER = Base64.getMimeEncoder(76, "\n".getBytes(StandardCharsets.UTF_8));
|
| 43 | public static final Codec<PublicKey> PUBLIC_KEY_CODEC = Codec.STRING.comapFlatMap(rsaString -> {
|
| 44 | try {
|
| 45 | return DataResult.success(stringToRsaPublicKey(rsaString));
|
| 46 | } catch (CryptException e) {
|
| 47 | return DataResult.error(e::getMessage);
|
| 48 | }
|
| 49 | }, Crypt::rsaPublicKeyToString);
|
| 50 | public static final Codec<PrivateKey> PRIVATE_KEY_CODEC = Codec.STRING.comapFlatMap(rsaString -> {
|
| 51 | try {
|
| 52 | return DataResult.success(stringToPemRsaPrivateKey(rsaString));
|
| 53 | } catch (CryptException e) {
|
| 54 | return DataResult.error(e::getMessage);
|
| 55 | }
|
| 56 | }, Crypt::pemRsaPrivateKeyToString);
|
| 57 |
|
| 58 | public static SecretKey generateSecretKey() throws CryptException {
|
| 59 | try {
|
| 60 | KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
|
| 61 | keyGenerator.init(128);
|
| 62 | return keyGenerator.generateKey();
|
| 63 | } catch (Exception e) {
|
| 64 | throw new CryptException(e);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | public static KeyPair generateKeyPair() throws CryptException {
|
| 69 | try {
|
| 70 | KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
| 71 | generator.initialize(1024);
|
| 72 | return generator.generateKeyPair();
|
| 73 | } catch (Exception e) {
|
| 74 | throw new CryptException(e);
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | public static byte[] digestData(final String serverId, final PublicKey publicKey, final SecretKey sharedKey) throws CryptException {
|
| 79 | try {
|
| 80 | return digestData(serverId.getBytes("ISO_8859_1"), sharedKey.getEncoded(), publicKey.getEncoded());
|
| 81 | } catch (Exception e) {
|
| 82 | throw new CryptException(e);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | private static byte[] digestData(final byte[]... inputs) throws Exception {
|
| 87 | MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
|
| 88 |
|
| 89 | for (byte[] input : inputs) {
|
| 90 | messageDigest.update(input);
|
| 91 | }
|
| 92 |
|
| 93 | return messageDigest.digest();
|
| 94 | }
|
| 95 |
|
| 96 | private static <T extends Key> T rsaStringToKey(String input, final String header, final String footer, final Crypt.ByteArrayToKeyFunction<T> byteArrayToKey) throws CryptException {
|
| 97 | int begin = input.indexOf(header);
|
| 98 | if (begin != -1) {
|
| 99 | begin += header.length();
|
| 100 | int end = input.indexOf(footer, begin);
|
| 101 | input = input.substring(begin, end + 1);
|
| 102 | }
|
| 103 |
|
| 104 | try {
|
| 105 | return byteArrayToKey.apply(Base64.getMimeDecoder().decode(input));
|
| 106 | } catch (IllegalArgumentException e) {
|
| 107 | throw new CryptException(e);
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | public static PrivateKey stringToPemRsaPrivateKey(final String rsaString) throws CryptException {
|
| 112 | return rsaStringToKey(rsaString, "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----", Crypt::byteToPrivateKey);
|
| 113 | }
|
| 114 |
|
| 115 | public static PublicKey stringToRsaPublicKey(final String rsaString) throws CryptException {
|
| 116 | return rsaStringToKey(rsaString, "-----BEGIN RSA PUBLIC KEY-----", "-----END RSA PUBLIC KEY-----", Crypt::byteToPublicKey);
|
| 117 | }
|
| 118 |
|
| 119 | public static String rsaPublicKeyToString(final PublicKey publicKey) {
|
| 120 | if (!"RSA".equals(publicKey.getAlgorithm())) {
|
| 121 | throw new IllegalArgumentException("Public key must be RSA");
|
| 122 | } else {
|
| 123 | return "-----BEGIN RSA PUBLIC KEY-----\n" + MIME_ENCODER.encodeToString(publicKey.getEncoded()) + "\n-----END RSA PUBLIC KEY-----\n";
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | public static String pemRsaPrivateKeyToString(final PrivateKey privateKey) {
|
| 128 | if (!"RSA".equals(privateKey.getAlgorithm())) {
|
| 129 | throw new IllegalArgumentException("Private key must be RSA");
|
| 130 | } else {
|
| 131 | return "-----BEGIN RSA PRIVATE KEY-----\n" + MIME_ENCODER.encodeToString(privateKey.getEncoded()) + "\n-----END RSA PRIVATE KEY-----\n";
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | private static PrivateKey byteToPrivateKey(final byte[] keyData) throws CryptException {
|
| 136 | try {
|
| 137 | EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyData);
|
| 138 | KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
| 139 | return keyFactory.generatePrivate(keySpec);
|
| 140 | } catch (Exception e) {
|
| 141 | throw new CryptException(e);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | public static PublicKey byteToPublicKey(final byte[] keyData) throws CryptException {
|
| 146 | try {
|
| 147 | EncodedKeySpec keySpec = new X509EncodedKeySpec(keyData);
|
| 148 | KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
| 149 | return keyFactory.generatePublic(keySpec);
|
| 150 | } catch (Exception e) {
|
| 151 | throw new CryptException(e);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | public static SecretKey decryptByteToSecretKey(final PrivateKey privateKey, final byte[] keyData) throws CryptException {
|
| 156 | byte[] key = decryptUsingKey(privateKey, keyData);
|
| 157 |
|
| 158 | try {
|
| 159 | return new SecretKeySpec(key, "AES");
|
| 160 | } catch (Exception e) {
|
| 161 | throw new CryptException(e);
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | public static byte[] encryptUsingKey(final Key key, final byte[] input) throws CryptException {
|
| 166 | return cipherData(1, key, input);
|
| 167 | }
|
| 168 |
|
| 169 | public static byte[] decryptUsingKey(final Key key, final byte[] input) throws CryptException {
|
| 170 | return cipherData(2, key, input);
|
| 171 | }
|
| 172 |
|
| 173 | private static byte[] cipherData(final int cipherOpMode, final Key key, final byte[] input) throws CryptException {
|
| 174 | try {
|
| 175 | return setupCipher(cipherOpMode, key.getAlgorithm(), key).doFinal(input);
|
| 176 | } catch (Exception e) {
|
| 177 | throw new CryptException(e);
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | private static Cipher setupCipher(final int cipherOpMode, final String algorithm, final Key key) throws Exception {
|
| 182 | Cipher cipher = Cipher.getInstance(algorithm);
|
| 183 | cipher.init(cipherOpMode, key);
|
| 184 | return cipher;
|
| 185 | }
|
| 186 |
|
| 187 | public static Cipher getCipher(final int opMode, final Key key) throws CryptException {
|
| 188 | try {
|
| 189 | Cipher cip = Cipher.getInstance("AES/CFB8/NoPadding");
|
| 190 | cip.init(opMode, key, new IvParameterSpec(key.getEncoded()));
|
| 191 | return cip;
|
| 192 | } catch (Exception e) {
|
| 193 | throw new CryptException(e);
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | private interface ByteArrayToKeyFunction<T extends Key> {
|
| 198 | T apply(final byte[] input) throws CryptException;
|
| 199 | }
|
| 200 |
|
| 201 | public record SaltSignaturePair(long salt, byte[] signature) {
|
| 202 | public static final Crypt.SaltSignaturePair EMPTY = new Crypt.SaltSignaturePair(0L, ByteArrays.EMPTY_ARRAY);
|
| 203 |
|
| 204 | public SaltSignaturePair(final FriendlyByteBuf input) {
|
| 205 | this(input.readLong(), input.readByteArray());
|
| 206 | }
|
| 207 |
|
| 208 | public boolean isValid() {
|
| 209 | return this.signature.length > 0;
|
| 210 | }
|
| 211 |
|
| 212 | public static void write(final FriendlyByteBuf output, final Crypt.SaltSignaturePair saltSignaturePair) {
|
| 213 | output.writeLong(saltSignaturePair.salt);
|
| 214 | output.writeByteArray(saltSignaturePair.signature);
|
| 215 | }
|
| 216 |
|
| 217 | public byte[] saltAsBytes() {
|
| 218 | return Longs.toByteArray(this.salt);
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | public static class SaltSupplier {
|
| 223 | private static final SecureRandom secureRandom = new SecureRandom();
|
| 224 |
|
| 225 | public static long getLong() {
|
| 226 | return secureRandom.nextLong();
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 |
|