-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #675 from DwayneJengSage/dev4
Workaround for DIAN-749: Android inv-arc app has wrong public key, take 2
- Loading branch information
Showing
8 changed files
with
188 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/java/org/sagebionetworks/bridge/services/UploadArchiveServiceInvArcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.sagebionetworks.bridge.services; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.security.PrivateKey; | ||
import java.security.cert.CertificateEncodingException; | ||
import java.security.cert.X509Certificate; | ||
|
||
import com.google.common.cache.LoadingCache; | ||
import org.apache.commons.io.FileUtils; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import org.sagebionetworks.bridge.crypto.BcCmsEncryptor; | ||
import org.sagebionetworks.bridge.crypto.CmsEncryptor; | ||
import org.sagebionetworks.bridge.crypto.PemUtils; | ||
|
||
// Unit test for DIAN-749, Android inv-arc app had wrong public key for encryption. | ||
@SuppressWarnings("unchecked") | ||
public class UploadArchiveServiceInvArcTest { | ||
private static final String APP_ID_ARC = "arc"; | ||
private static final String APP_ID_INV_ARC = "inv-arc"; | ||
private static final byte[] PLAIN_TEXT_DATA = "This is my raw data".getBytes(); | ||
|
||
private static UploadArchiveService archiveService; | ||
|
||
@BeforeClass | ||
public static void before() throws Exception { | ||
// Load encryptors. rsacert.pem and rsaprivkey.pem represent the arc app. rsacert2.pem and rsaprivkey2.pem | ||
// represent the inv-arc app. | ||
CmsEncryptor arcEncryptor = loadEncryptor("/cms/rsacert.pem", "/cms/rsaprivkey.pem"); | ||
CmsEncryptor invArcEncryptor = loadEncryptor("/cms/rsacert2.pem", "/cms/rsaprivkey2.pem"); | ||
|
||
// Mock encryptor cache. | ||
LoadingCache<String, CmsEncryptor> mockEncryptorCache = mock(LoadingCache.class); | ||
when(mockEncryptorCache.get(APP_ID_ARC)).thenReturn(arcEncryptor); | ||
when(mockEncryptorCache.get(APP_ID_INV_ARC)).thenReturn(invArcEncryptor); | ||
|
||
// Create archive service. | ||
archiveService = new UploadArchiveService(); | ||
archiveService.setCmsEncryptorCache(mockEncryptorCache); | ||
archiveService.setMaxNumZipEntries(1000000); | ||
archiveService.setMaxZipEntrySize(1000000); | ||
} | ||
|
||
private static CmsEncryptor loadEncryptor(String publicKeyPath, String privateKeyPath) | ||
throws CertificateEncodingException, IOException { | ||
File certFile = new ClassPathResource(publicKeyPath).getFile(); | ||
byte[] certBytes = Files.readAllBytes(certFile.toPath()); | ||
X509Certificate cert = PemUtils.loadCertificateFromPem(new String(certBytes)); | ||
|
||
File privateKeyFile = new ClassPathResource(privateKeyPath).getFile(); | ||
byte[] privateKeyBytes = Files.readAllBytes(privateKeyFile.toPath()); | ||
PrivateKey privateKey = PemUtils.loadPrivateKeyFromPem(new String(privateKeyBytes)); | ||
|
||
return new BcCmsEncryptor(cert, privateKey); | ||
} | ||
|
||
@Test | ||
public void testInvArc() throws Exception { | ||
// Encrypt some data with the arc app and write it to a file. | ||
byte[] arcEncryptedData = archiveService.encrypt(APP_ID_ARC, PLAIN_TEXT_DATA); | ||
File arcEncryptedFile = File.createTempFile("arc", ".encrypted"); | ||
FileUtils.writeByteArrayToFile(arcEncryptedFile, arcEncryptedData); | ||
|
||
// Attempt to decrypt it with the inv-arc app. | ||
File arcDecryptedFile = File.createTempFile("arc", ".decrypted"); | ||
archiveService.decrypt(APP_ID_INV_ARC, arcEncryptedFile, arcDecryptedFile); | ||
byte[] result = FileUtils.readFileToByteArray(arcDecryptedFile); | ||
assertEquals(result, PLAIN_TEXT_DATA); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIExDCCA6ygAwIBAgIGC0ijqD17MA0GCSqGSIb3DQEBBQUAMIGeMQswCQYDVQQG | ||
EwJVUzELMAkGA1UECAwCV0ExEDAOBgNVBAcMB1NlYXR0bGUxGTAXBgNVBAoMEFNh | ||
Z2UgQmlvbmV0d29ya3MxDzANBgNVBAsMBkJyaWRnZTEkMCIGCSqGSIb3DQEJARYV | ||
YnJpZGdlSVRAc2FnZWJhc2Uub3JnMR4wHAYDVQQDDBVodHRwOi8vbG9jYWxob3N0 | ||
OjkwMDAwHhcNMjExMjE0MjIzMDE1WhcNNDkwNTAyMjIzMDE1WjCBnjELMAkGA1UE | ||
BhMCVVMxCzAJBgNVBAgMAldBMRAwDgYDVQQHDAdTZWF0dGxlMRkwFwYDVQQKDBBT | ||
YWdlIEJpb25ldHdvcmtzMQ8wDQYDVQQLDAZCcmlkZ2UxJDAi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MqQVwUcEddmAGKFIifVBgCzL/NvwAKgkrQOFj5R751lScE | ||
x9jir8xFUNAYw3Ax8NlEI6ay22u5wv89EoShU2EUbbW/CdpqMvLpo6j66D5fihl8 | ||
dKxv7JLTyYYMDDC7T9OU+1kF9fXKJYcrhciMTq5fHFnbiYFTtg/h9qJx35/27SZ6 | ||
cplcxo4YdWY3kSargw4L2Wvo6L5nAORN | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC2E7/43H5x2JBr | ||
nz9hw2aWMoJwxu2+8p1IpuwFHzIdwyhaAg0NAhKdbwdhEl8XIh73ualCpx03td4E | ||
ih/GOpgBM43xFezAoen/gS6pnvXpitbBkdJu45aUsYRVRJ/yNDRyjvLXdJMZggwD | ||
Du1nz5d/TOjwpp/3ERQpfzpTw+KzJiKA7ne/uIvKWmEbmvMs+Z1AS6NNeCIDEsF7 | ||
tSTxFLE6JO4lM0Z8+7cMW+63TJrXztdEjlgAWQ5FmQBMGLYkg5uu8rNOnQIg4yoQ | ||
91DzYdjdIvBNbleKvMyBvVi4EpJxbQCDpB5HEQjC8rOKd98LoShoiXl9tPEYWHsS | ||
gsswfrexAgMBAAECggEBAIyHVcWnuNf5hA3sjSjRfZ4zQcX1Y43bB1YJr2SMnUun | ||
Ur+VkakWjnOAPDvJyCa8qRYd7+uHu99BuSfby4ZdtvBGcClA+Mf8r/QKKo+0Jqyo | ||
AfTIrZf0hEYjdLWzD5gKfuhkOD3etaIcY1UA8m8LJCyWmbsTf6dbQSp+DfCU4aXO | ||
XTVi9ytPYrCPPI30wubDXIgUrfRkWVEpHQharJmR3P/4c2O2MapaXVz/64VQ0C4v | ||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