Skip to content

Commit

Permalink
Merge pull request #99 from nchandran10/main
Browse files Browse the repository at this point in the history
EncryptionUtils - added support for passing key and certificate as bytes array
  • Loading branch information
karen-avetisyan-mc authored Sep 11, 2024
2 parents 50bfc06 + 3d5080b commit bb115d9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.mastercard.developer</groupId>
<artifactId>client-encryption</artifactId>
<version>1.8.2</version>
<version>1.8.3</version>
<packaging>jar</packaging>
<description>Library for Mastercard API compliant payload encryption/decryption</description>
<url>https://github.com/Mastercard/client-encryption-java</url>
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/com/mastercard/developer/utils/EncryptionUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.mastercard.developer.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -32,17 +32,32 @@ private EncryptionUtils() {
/**
* Populate a X509 encryption certificate object with the certificate data at the given file path.
*/
public static Certificate loadEncryptionCertificate(String certificatePath) throws CertificateException, FileNotFoundException {
public static Certificate loadEncryptionCertificate(String certificatePath) throws CertificateException, IOException {
return loadEncryptionCertificate(Files.newInputStream(Paths.get(certificatePath)));
}

/**
* Populate a X509 encryption certificate object with the certificate data at the given certificate data in bytes.
*/
public static Certificate loadEncryptionCertificate(InputStream certificateStream) throws CertificateException {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
return factory.generateCertificate(new FileInputStream(certificatePath));
return factory.generateCertificate(certificateStream);
}

/**
* Load a RSA decryption key from a file (PEM or DER).
*/
public static PrivateKey loadDecryptionKey(String keyFilePath) throws GeneralSecurityException, IOException {
byte[] keyDataBytes = Files.readAllBytes(Paths.get(keyFilePath));
String keyDataString = new String(keyDataBytes, StandardCharsets.UTF_8);
InputStream keyStream = new ByteArrayInputStream(Files.readAllBytes(Paths.get(keyFilePath)));
return loadDecryptionKey(keyStream);
}

/**
* Load a RSA decryption key from key data in bytes.
*/
public static PrivateKey loadDecryptionKey(InputStream keyDataStream) throws GeneralSecurityException, IOException {
byte[] keyBytes = keyDataStream.readAllBytes();
String keyDataString = new String(keyBytes, StandardCharsets.UTF_8);

if (keyDataString.contains(PKCS_1_PEM_HEADER)) {
// OpenSSL / PKCS#1 Base64 PEM encoded file
Expand All @@ -63,17 +78,17 @@ public static PrivateKey loadDecryptionKey(String keyFilePath) throws GeneralSec
}

// We assume it's a PKCS#8 DER encoded binary file
return readPkcs8PrivateKey(Files.readAllBytes(Paths.get(keyFilePath)));
return readPkcs8PrivateKey(keyBytes);
}

/**
* Load a RSA decryption key out of a PKCS#12 container.
*/
public static PrivateKey loadDecryptionKey(String pkcs12KeyFilePath,
String decryptionKeyAlias,
String decryptionKeyPassword) throws GeneralSecurityException, IOException {
String decryptionKeyAlias,
String decryptionKeyPassword) throws GeneralSecurityException, IOException {
KeyStore pkcs12KeyStore = KeyStore.getInstance("PKCS12");
pkcs12KeyStore.load(new FileInputStream(pkcs12KeyFilePath), decryptionKeyPassword.toCharArray());
pkcs12KeyStore.load(Files.newInputStream(Paths.get(pkcs12KeyFilePath)), decryptionKeyPassword.toCharArray());
return (PrivateKey) pkcs12KeyStore.getKey(decryptionKeyAlias, decryptionKeyPassword.toCharArray());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.rules.ExpectedException;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.spec.InvalidKeySpecException;
Expand Down Expand Up @@ -68,6 +71,26 @@ void testLoadDecryptionKey_ShouldSupportPkcs8Der(String keyLocation, String expe
assertArrayEquals(base64Decode(expectedEncoding), privateKey.getEncoded());
}

@ParameterizedTest
@CsvSource({
"pkcs8/test_key_pkcs8-2048.der, MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD0ynqAQWn0T7/VJLletTJgoxsTt5TR3IkJ+Yk/Pxg6Q5hXuiGrBdC+OVo/9hrNnptuZh9rZYKto6lbSjYFiKMeBDvPZrYDPzusp0C0KllIoVbzYiOezD76XHsQAEje0UXbzZlXstPXef2bi2HkqV26ST167L5O4moK8+7jHMT80T6XgsUyvyt8PjsQ9CSu6fnD9NfCSYmt2cb16OXcEtA7To2zoGznXqB6JhntFjG0jxee7RkLR+moOqMI9kFM5GSIV4uhwQ9FtOCjUf7TFAU12wwfX/QXUEj6G93GVtzf6QdkVkWh4EyRHeMLyMNc5c0Iw1ZvXdOKfoeo9F47QpbzAgMBAAECggEAK3dMmzuCSdxjTsCPnc6E3H35z914Mm97ceb6RN26OpZIFcO6OLj2oOBkMxlLFxnDta2yhIpo0tZNuyUJRKBHfov35tLxHNB8kyK7rYIbincDjoHtm0PfJuuG+odiaRY11lrCkLzzOr6xlo4AWu7r8qkQnqQtAqrXc4xu7artG4rfMIunGnjjWQGzovtey1JgZctO97MU4Wvw18vgYBI6JM4eHJkZxgEhVQblBTKZs4OfiWk6MRHchgvqnWugwl213FgCzwy9cnyxTP13i9QKaFzL29TYmmN6bRWBH95z41M8IAa0CGahrSJjudZCFwsFh413YWv/pdqdkKHg1sqseQKBgQD641RYQkMn4G9vOiwB/is5M0OAhhUdWH1QtB8vvhY5ISTjFMqgIIVQvGmqDDk8QqFMOfFFqLtnArGn8HrKmBXMpRigS4ae/QgHEz34/RFjNDQ9zxIf/yoCRH5PmnPPU6x8j3bj/vJMRQA6/yngoca+9qvi3R32AtC5DUELnwyzNwKBgQD5x1iEV+albyCNNyLoT/f6LSH1NVcO+0IOvIaAVMtfy+hEEXz7izv3/AgcogVZzRARSK0qsQ+4WQN6Q2WG5cQYSyB92PR+Vgw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ouGpnavWSHMQ6HIfPtgEg0BhSqOOgUBVR+wdntq4zux1AFnHVXBLZdE+CWCmyj3ASFMTPvkLssfj/ae7UEhUB24TZxz3nAo8RR7Gmz8TUsCgYEAimnEVK8K+kg6nObI+D2yeO3ivHe/DpjcAjqCUXxCWjV4mmMcxaaUChOo4Dsr0vMvvNpsVUc/eqO2J9j1sVXbHL5iFI9Q2/Pect5Oh6svbpTAejIUzrrup7wC3GGEp5zsbP/KBf7KSjKSDRGAB+ey8oKbvInbbTymAsql/6iswiECgYEAuukzFZFe5banMpHaglKvwoSXT8hpv2Ci4samox6C/C+zGpsyx4i2+RMcwHy26kn9drRSxOrM7OeojvA40g8EPO06kAZIAeaDdfhZaIJdd44N32p9VcCTGZxYE/jI9+Dwk83tERtlTWxkUWgpAA+YNIO0BnCxR1+I7uTBfvBjvzcCgYBDrjptyo8peY0Jzaijg7hWBwLZp/M27GhR8uAV6FVESP+1uG06k3g3CECxCE3Pi6HVSaW6UpNMZnrtVaKQCJDyKnkdIExFVP8DhkJSHmid1TXJXEfpDT57JD4UX6NOCcB0ynSyYvDvJ6bodx6SSyB03CEMqJ8VMjXeYpZSHyAF7A=="
})
void testLoadDecryptionKey_ShouldSupportPkcs8DerBytes(String keyLocation, String expectedEncoding) throws Exception {

// GIVEN
InputStream keyStream = Files.newInputStream(Paths.get(String.format("./src/test/resources/keys/%s", keyLocation)));

// WHEN
PrivateKey privateKey = EncryptionUtils.loadDecryptionKey(keyStream);

// THEN
assertNotNull(privateKey);
assertEquals("RSA", privateKey.getAlgorithm());
assertArrayEquals(base64Decode(expectedEncoding), privateKey.getEncoded());
}

@ParameterizedTest
@CsvSource({
"test_key_pkcs1-512.pem",
Expand All @@ -87,6 +110,25 @@ void testLoadDecryptionKey_ShouldSupportPkcs1Base64Pem_512bits(String fileName)
assertEquals("RSA", privateKey.getAlgorithm());
}

@ParameterizedTest
@CsvSource({
"test_key_pkcs1-512.pem",
"test_key_pkcs1-1024.pem",
"test_key_pkcs1-4096.pem"
})
void testLoadDecryptionKey_ShouldSupportPkcs1Base64PemBytes_512bits(String fileName) throws Exception {

// GIVEN
InputStream keyStream = Files.newInputStream(Paths.get(String.format("./src/test/resources/keys/pkcs1/%s", fileName)));

// WHEN
PrivateKey privateKey = EncryptionUtils.loadDecryptionKey(keyStream);

// THEN
assertNotNull(privateKey);
assertEquals("RSA", privateKey.getAlgorithm());
}

@Test
public void testLoadDecryptionKey_ShouldSupportPkcs12() throws Exception {

Expand Down

0 comments on commit bb115d9

Please sign in to comment.