Skip to content

Commit

Permalink
Bubble up encryption key exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
avazirna committed Jan 23, 2024
1 parent a2928bd commit bd512d6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
18 changes: 10 additions & 8 deletions src/main/java/org/commcare/core/encryption/CryptUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.commcare.core.encryption;

import org.commcare.util.EncryptionKeyHelper;
import org.javarosa.core.io.StreamsUtil;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -141,21 +142,22 @@ public static SecretKey generateSymmetricKey(byte[] prngSeed) {
}

// Generate random Secret key with a default key lenght of 256 bits
public static SecretKey generateRandomSecretKey() {
public static SecretKey generateRandomSecretKey()
throws EncryptionKeyHelper.EncryptionKeyException {
final int AES_DEFAULT_KEY_LENGTH = 256;
return generateRandomSecretKey(AES_DEFAULT_KEY_LENGTH);
}

public static SecretKey generateRandomSecretKey(int keylength) {
public static SecretKey generateRandomSecretKey(int keylength)
throws EncryptionKeyHelper.EncryptionKeyException {
KeyGenerator generator;
try {
generator = KeyGenerator.getInstance("AES");
generator.init(keylength, new SecureRandom());
return generator.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new EncryptionKeyHelper.EncryptionKeyException("Error encountered while generating random Key Pair", e);
}
return null;
}

public static Cipher getPrivateKeyCipher(byte[] privateKey)
Expand Down Expand Up @@ -186,15 +188,15 @@ private static Cipher getAesKeyCipher(byte[] aesKey, int mode)
}

// For RSA
public static KeyPair generateRandomKeyPair(int keyLength) {
public static KeyPair generateRandomKeyPair(int keyLength)
throws EncryptionKeyHelper.EncryptionKeyException {
KeyPairGenerator generator;
try {
generator = KeyPairGenerator.getInstance("RSA");
generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(keyLength, new SecureRandom());
return generator.genKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new EncryptionKeyHelper.EncryptionKeyException("Error encountered while generating random Key Pair", e);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.commcare.core.interfaces;

import org.commcare.util.EncryptionKeyHelper;

import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -36,4 +38,9 @@ public interface HttpResponseProcessor {
* A issue occurred while processing the http request or response
*/
void handleIOException(IOException exception);

/**
* Encryption key error occurred while processing the http response
*/
void handleEncryptionKeyException(EncryptionKeyHelper.EncryptionKeyException mException);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.commcare.core.interfaces;

import org.commcare.util.EncryptionKeyHelper;

import java.io.IOException;
import java.io.InputStream;

public interface ResponseStreamAccessor {
InputStream getResponseStream() throws IOException;
InputStream getResponseStream() throws IOException, EncryptionKeyHelper.EncryptionKeyException;
}
10 changes: 8 additions & 2 deletions src/main/java/org/commcare/core/network/ModernHttpRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.commcare.core.interfaces.ResponseStreamAccessor;
import org.commcare.core.network.bitcache.BitCache;
import org.commcare.core.network.bitcache.BitCacheFactory;
import org.commcare.util.EncryptionKeyHelper;
import org.commcare.util.NetworkStatus;
import org.javarosa.core.io.StreamsUtil;

Expand Down Expand Up @@ -152,6 +153,9 @@ public static void processResponse(HttpResponseProcessor responseProcessor,
} catch (IOException e) {
responseProcessor.handleIOException(e);
return;
} catch (EncryptionKeyHelper.EncryptionKeyException e) {
responseProcessor.handleEncryptionKeyException(e);
return;
}
responseProcessor.processSuccess(responseCode, responseStream);
} finally {
Expand All @@ -172,7 +176,8 @@ public static void processResponse(HttpResponseProcessor responseProcessor,
* @throws IOException if an io error happens while reading or writing to cache
*/

public InputStream getResponseStream(Response<ResponseBody> response) throws IOException {
public InputStream getResponseStream(Response<ResponseBody> response)
throws IOException, EncryptionKeyHelper.EncryptionKeyException {
InputStream inputStream = response.body().byteStream();
BitCache cache = BitCacheFactory.getCache(cacheDirSetup, getContentLength(response));
cache.initializeCache();
Expand All @@ -187,7 +192,8 @@ public InputStream getResponseStream(Response<ResponseBody> response) throws IOE
* @throws IOException if an io error happens while reading or writing to cache
*/
@Override
public InputStream getResponseStream() throws IOException {
public InputStream getResponseStream()
throws IOException, EncryptionKeyHelper.EncryptionKeyException {
return getResponseStream(response);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.commcare.core.network.bitcache;

import org.commcare.util.EncryptionKeyHelper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -8,7 +10,7 @@
* @author ctsims
*/
public interface BitCache {
void initializeCache() throws IOException;
void initializeCache() throws IOException, EncryptionKeyHelper.EncryptionKeyException;

OutputStream getCacheStream() throws IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.commcare.core.network.bitcache;

import org.commcare.core.encryption.CryptUtil;
import org.commcare.util.EncryptionKeyHelper;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -33,7 +34,7 @@ protected FileBitCache(BitCacheFactory.CacheDirSetup cacheDirSetup) {
}

@Override
public void initializeCache() throws IOException {
public void initializeCache() throws IOException, EncryptionKeyHelper.EncryptionKeyException {
File cacheLocation = cacheDirSetup.getCacheDir();

//generate temp file
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/commcare/util/LogTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,8 @@ public class LogTypes {
*/
public static final String TYPE_FCM = "fcm";

/**
* Errors while generating/handling encryption keys
*/
public static final String TYPE_ERROR_ENCRYPTION_KEY = "encryption-key";
}

0 comments on commit bd512d6

Please sign in to comment.