Add tests

This commit is contained in:
Oracle 2026-04-21 18:00:31 +02:00
parent 8acf584d28
commit 21b4169130
Signed by: Oracle
SSH key fingerprint: SHA256:x4/RtnjUyuHkdvmwNDsWSfcfF1V5PNr3OpriZqOvCX8
7 changed files with 879 additions and 18 deletions

View file

@ -12,10 +12,6 @@ import java.util.Set;
*/
public final class Constants {
private Constants() {
// Utility class prevents instantiation
}
// Protocol Constants
/**

View file

@ -16,7 +16,10 @@ import java.nio.file.attribute.PosixFilePermissions;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAKeyGenParameterSpec;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;
@ -238,22 +241,31 @@ public class SecureCompletionClient {
* @param password optional password for the encrypted private key
*/
public void loadKeys(String privateKeyPath, String publicPemKeyPath, String password) {
File keyFile = new File(privateKeyPath);
if (!keyFile.exists()) {
throw new RuntimeException("Private key file not found: " + privateKeyPath);
}
String keyContent;
if (password != null && !password.isEmpty()) {
String cipherText = getEncryptedPrivateKeyFromFile(privateKeyPath);
keyContent = getEncryptedPrivateKeyFromFile(privateKeyPath);
try {
cipherText = Pass2Key.decrypt("AES/GCM/NoPadding", cipherText, password);
keyContent = Pass2Key.decrypt("AES/GCM/NoPadding", keyContent, password);
} catch (NoSuchPaddingException | NoSuchAlgorithmException
| BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException |
InvalidKeyException e) {
System.out.println("Wrong password!");
return;
}
} else {
keyContent = getEncryptedPrivateKeyFromFile(privateKeyPath);
}
try {
this.privateKey = Pass2Key.convertStringToPrivateKey(cipherText);
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
this.privateKey = Pass2Key.convertStringToPrivateKey(keyContent);
} catch (Exception e) {
throw new RuntimeException("Failed to load private key: " + e.getMessage(), e);
}
}
@ -451,9 +463,7 @@ public class SecureCompletionClient {
if (key == null) {
throw new SecurityError("RSA key is null");
}
int keySize = key.getEncoded() != null ? key.getEncoded().length * 8 : 0;
System.out.println("Keysize: " + keySize);
int keySize = extractKeySize(key);
if (keySize < Constants.MIN_RSA_KEY_SIZE) {
throw new SecurityError(
@ -462,6 +472,27 @@ public class SecureCompletionClient {
}
}
private int extractKeySize(PrivateKey key) {
try {
java.security.KeyFactory kf = java.security.KeyFactory.getInstance("RSA");
java.security.spec.RSAPrivateCrtKeySpec crtSpec = kf.getKeySpec(key, java.security.spec.RSAPrivateCrtKeySpec.class);
return crtSpec.getModulus().bitLength();
} catch (Exception ignored) {
// Try RSAPrivateKeySpec
try {
java.security.KeyFactory kf = java.security.KeyFactory.getInstance("RSA");
java.security.spec.RSAPrivateKeySpec privSpec = kf.getKeySpec(key, java.security.spec.RSAPrivateKeySpec.class);
return privSpec.getModulus().bitLength();
} catch (Exception ignored2) {
// Fall back to encoded length
if (key.getEncoded() != null) {
return key.getEncoded().length * 8;
}
}
}
return 0;
}
// HTTP Status Exception Mapping
/**