diff --git a/ballerina/tests/sign_verify_test.bal b/ballerina/tests/sign_verify_test.bal index deea7d86..2f899580 100644 --- a/ballerina/tests/sign_verify_test.bal +++ b/ballerina/tests/sign_verify_test.bal @@ -243,3 +243,59 @@ isolated function testVerifySha384withEcdsa() returns Error? { byte[] sha384withEcdsaSignature = check signSha384withEcdsa(payload, privateKey); test:assertTrue(check verifySha384withEcdsaSignature(payload, sha384withEcdsaSignature, publicKey)); } + +@test:Config {} +isolated function testDecodeRsaPrivateKeyError() returns Error? { + KeyStore keyStore = { + path: EC_KEYSTORE_PATH, + password: "ballerina" + }; + PrivateKey|Error privateKey = decodeRsaPrivateKeyFromKeyStore(keyStore, "ec-keypair", "ballerina"); + if privateKey is Error { + test:assertEquals(privateKey.message(), "Not a valid RSA key."); + } else { + test:assertFail("Expected error not found."); + } +} + +@test:Config {} +isolated function testDecodeEcPrivateKeyError() returns Error? { + KeyStore keyStore = { + path: KEYSTORE_PATH, + password: "ballerina" + }; + PrivateKey|Error privateKey = decodeEcPrivateKeyFromKeyStore(keyStore, "ballerina", "ballerina"); + if privateKey is Error { + test:assertEquals(privateKey.message(), "Not a valid EC key."); + } else { + test:assertFail("Expected error not found."); + } +} + +@test:Config {} +isolated function testDecodeEcPublicKeyError() returns Error? { + KeyStore keyStore = { + path: KEYSTORE_PATH, + password: "ballerina" + }; + PublicKey|Error publicKey = decodeEcPublicKeyFromTrustStore(keyStore, "ballerina"); + if publicKey is Error { + test:assertEquals(publicKey.message(), "Not a valid EC public key."); + } else { + test:assertFail("Expected error not found"); + } +} + +@test:Config {} +isolated function testDecodeRsaPublicKeyError() returns Error? { + KeyStore keyStore = { + path: EC_KEYSTORE_PATH, + password: "ballerina" + }; + PublicKey|Error publicKey = decodeRsaPublicKeyFromTrustStore(keyStore, "ec-keypair"); + if publicKey is Error { + test:assertEquals(publicKey.message(), "Not a valid RSA public key."); + } else { + test:assertFail("Expected error not found"); + } +} diff --git a/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java b/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java index b662bd25..fe5fef28 100644 --- a/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java +++ b/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java @@ -247,7 +247,7 @@ private static Object buildRsaPublicKeyRecord(Certificate certificate) { if (publicKey.getAlgorithm().equals(Constants.RSA_ALGORITHM)) { return getPublicKeyRecord(certificate, certificateBMap, publicKey); } - return CryptoUtils.createError("Not a valid RSA key."); + return CryptoUtils.createError("Not a valid RSA public key."); } private static Object buildEcPublicKeyRecord(Certificate certificate) { @@ -256,7 +256,7 @@ private static Object buildEcPublicKeyRecord(Certificate certificate) { if (publicKey.getAlgorithm().equals(Constants.EC_ALGORITHM)) { return getPublicKeyRecord(certificate, certificateBMap, publicKey); } - return CryptoUtils.createError("Not a valid EC key."); + return CryptoUtils.createError("Not a valid EC public key."); } private static Object getPublicKeyRecord(Certificate certificate, BMap certificateBMap,