Launcher/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/KeyAgreementManager.java

67 lines
3.2 KiB
Java
Raw Normal View History

2021-04-07 10:00:30 +03:00
package pro.gravit.launchserver.manangers;
2021-05-10 10:34:27 +03:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-04-07 10:00:30 +03:00
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.SecurityHelper;
import java.io.IOException;
import java.nio.file.Path;
2021-05-25 12:17:29 +03:00
import java.security.KeyPair;
import java.security.SecureRandom;
2021-04-07 10:00:30 +03:00
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
public class KeyAgreementManager {
public final ECPublicKey ecdsaPublicKey;
public final ECPrivateKey ecdsaPrivateKey;
public final RSAPublicKey rsaPublicKey;
public final RSAPrivateKey rsaPrivateKey;
2021-05-10 10:34:27 +03:00
private transient final Logger logger = LogManager.getLogger();
2021-04-07 10:00:30 +03:00
public KeyAgreementManager(ECPublicKey ecdsaPublicKey, ECPrivateKey ecdsaPrivateKey, RSAPublicKey rsaPublicKey, RSAPrivateKey rsaPrivateKey) {
this.ecdsaPublicKey = ecdsaPublicKey;
this.ecdsaPrivateKey = ecdsaPrivateKey;
this.rsaPublicKey = rsaPublicKey;
this.rsaPrivateKey = rsaPrivateKey;
}
public KeyAgreementManager(Path keyDirectory) throws IOException, InvalidKeySpecException {
Path ecdsaPublicKeyPath = keyDirectory.resolve("ecdsa_id.pub"), ecdsaPrivateKeyPath = keyDirectory.resolve("ecdsa_id");
if (IOHelper.isFile(ecdsaPublicKeyPath) && IOHelper.isFile(ecdsaPrivateKeyPath)) {
2021-05-10 10:34:27 +03:00
logger.info("Reading ECDSA keypair");
2021-04-07 10:00:30 +03:00
ecdsaPublicKey = SecurityHelper.toPublicECDSAKey(IOHelper.read(ecdsaPublicKeyPath));
ecdsaPrivateKey = SecurityHelper.toPrivateECDSAKey(IOHelper.read(ecdsaPrivateKeyPath));
} else {
2021-05-10 10:34:27 +03:00
logger.info("Generating ECDSA keypair");
2021-04-07 10:00:30 +03:00
KeyPair pair = SecurityHelper.genECDSAKeyPair(new SecureRandom());
ecdsaPublicKey = (ECPublicKey) pair.getPublic();
ecdsaPrivateKey = (ECPrivateKey) pair.getPrivate();
// Write key pair list
2021-05-10 10:34:27 +03:00
logger.info("Writing ECDSA keypair list");
2021-04-07 10:00:30 +03:00
IOHelper.write(ecdsaPublicKeyPath, ecdsaPublicKey.getEncoded());
IOHelper.write(ecdsaPrivateKeyPath, ecdsaPrivateKey.getEncoded());
}
Path rsaPublicKeyPath = keyDirectory.resolve("rsa_id.pub"), rsaPrivateKeyPath = keyDirectory.resolve("rsa_id");
if (IOHelper.isFile(rsaPublicKeyPath) && IOHelper.isFile(rsaPrivateKeyPath)) {
2021-05-10 10:34:27 +03:00
logger.info("Reading RSA keypair");
2021-04-07 10:00:30 +03:00
rsaPublicKey = SecurityHelper.toPublicRSAKey(IOHelper.read(rsaPublicKeyPath));
rsaPrivateKey = SecurityHelper.toPrivateRSAKey(IOHelper.read(rsaPrivateKeyPath));
} else {
2021-05-10 10:34:27 +03:00
logger.info("Generating RSA keypair");
2021-04-07 10:00:30 +03:00
KeyPair pair = SecurityHelper.genRSAKeyPair(new SecureRandom());
rsaPublicKey = (RSAPublicKey) pair.getPublic();
rsaPrivateKey = (RSAPrivateKey) pair.getPrivate();
// Write key pair list
2021-05-10 10:34:27 +03:00
logger.info("Writing RSA keypair list");
2021-04-07 10:00:30 +03:00
IOHelper.write(rsaPublicKeyPath, rsaPublicKey.getEncoded());
IOHelper.write(rsaPrivateKeyPath, rsaPrivateKey.getEncoded());
}
}
}