From 934432b338d0610008398d27754f648fb32e75e4 Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Tue, 19 Mar 2024 15:19:25 +0530 Subject: [PATCH] Fix build failures --- .../stdlib/crypto/nativeimpl/Decode.java | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) 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 96093da4..106cffe0 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 @@ -322,36 +322,56 @@ private static Object getPublicKey(BMap trustStoreRecord, BStr } public static Object decodeRsaPublicKeyFromCertFile(BString certFilePath) { - X509Certificate certificate = (X509Certificate) getCertificateFromFile(certFilePath); - return buildRsaPublicKeyRecord(certificate); + File certFile = new File(certFilePath.getValue()); + try (FileInputStream fileInputStream = new FileInputStream(certFile)) { + CertificateFactory certificateFactory = CertificateFactory.getInstance(Constants.CERTIFICATE_TYPE_X509); + X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(fileInputStream); + return buildRsaPublicKeyRecord(certificate); + } catch (FileNotFoundException e) { + return CryptoUtils.createError("Certificate file not found at: " + certFile.getAbsolutePath()); + } catch (CertificateException | IOException e) { + return CryptoUtils.createError("Unable to do public key operations: " + e.getMessage()); + } } public static Object decodeEcPublicKeyFromCertFile(BString certFilePath) { - X509Certificate certificate = (X509Certificate) getCertificateFromFile(certFilePath); - return buildEcPublicKeyRecord(certificate); + File certFile = new File(certFilePath.getValue()); + try (FileInputStream fileInputStream = new FileInputStream(certFile)) { + CertificateFactory certificateFactory = CertificateFactory.getInstance(Constants.CERTIFICATE_TYPE_X509); + X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(fileInputStream); + return buildEcPublicKeyRecord(certificate); + } catch (FileNotFoundException e) { + return CryptoUtils.createError("Certificate file not found at: " + certFile.getAbsolutePath()); + } catch (CertificateException | IOException e) { + return CryptoUtils.createError("Unable to do public key operations: " + e.getMessage()); + } } public static Object decodeDilithium3PublicKeyFromCertFile(BString certFilePath) { if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { Security.addProvider(new BouncyCastleProvider()); } - X509Certificate certificate = (X509Certificate) getCertificateFromFile(certFilePath); - return buildDilithium3PublicKeyRecord(certificate); + File certFile = new File(certFilePath.getValue()); + try (FileInputStream fileInputStream = new FileInputStream(certFile)) { + CertificateFactory certificateFactory = CertificateFactory.getInstance(Constants.CERTIFICATE_TYPE_X509); + X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(fileInputStream); + return buildDilithium3PublicKeyRecord(certificate); + } catch (FileNotFoundException e) { + return CryptoUtils.createError("Certificate file not found at: " + certFile.getAbsolutePath()); + } catch (CertificateException | IOException e) { + return CryptoUtils.createError("Unable to do public key operations: " + e.getMessage()); + } } public static Object decodeKyber768PublicKeyFromCertFile(BString certFilePath) { if (Security.getProvider(BouncyCastlePQCProvider.PROVIDER_NAME) == null) { Security.addProvider(new BouncyCastlePQCProvider()); } - X509Certificate certificate = (X509Certificate) getCertificateFromFile(certFilePath); - return buildKyber768PublicKeyRecord(certificate); - } - - private static Object getCertificateFromFile(BString certFilePath) { File certFile = new File(certFilePath.getValue()); try (FileInputStream fileInputStream = new FileInputStream(certFile)) { CertificateFactory certificateFactory = CertificateFactory.getInstance(Constants.CERTIFICATE_TYPE_X509); - return certificateFactory.generateCertificate(fileInputStream); + X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(fileInputStream); + return buildKyber768PublicKeyRecord(certificate); } catch (FileNotFoundException e) { return CryptoUtils.createError("Certificate file not found at: " + certFile.getAbsolutePath()); } catch (CertificateException | IOException e) {