Skip to content

Commit

Permalink
Merge pull request #216 from cconlon/androidLint
Browse files Browse the repository at this point in the history
JNI/JSSE: adjust for methods not available in Android API 24
  • Loading branch information
JacobBarthelmeh authored Aug 7, 2024
2 parents 4e70b68 + 1906b93 commit 473f587
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 11 deletions.
55 changes: 55 additions & 0 deletions src/java/com/wolfssl/WolfSSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

package com.wolfssl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

/**
* Base class which wraps the native WolfSSL embedded SSL library.
* This class contains library init and cleanup methods, general callback
Expand Down Expand Up @@ -707,6 +712,56 @@ public static void loadLibraryAbsolute(String libPath)
System.load(libPath);
}

/* ----------------- generic static helper functions ---------------- */

/**
* Read a File into byte array.
*
* This method can't use the java.nio package since we have users
* on Android API 24 which does not support java.nio.
*
* @param file File to read into byte array
*
* @return byte array representing input File, or null if file is null
*/
protected static byte[] fileToBytes(File file)
throws FileNotFoundException, IOException {

int bytesRead = 0;
long fileLen = 0;
byte[] fileBytes = null;
FileInputStream fis = null;

if (file == null) {
return null;
}

fileLen = file.length();
if (fileLen == 0) {
return new byte[0];
}

try {
fis = new FileInputStream(file);
if (fis != null) {
fileBytes = new byte[(int)fileLen];

bytesRead = fis.read(fileBytes);

if (bytesRead != fileLen) {
throw new IOException("Unable to read entire file: " +
file.getAbsolutePath());
}
}
} finally {
if (fis != null) {
fis.close();
}
}

return fileBytes;
}

/* --------------- native feature detection functions --------------- */

/**
Expand Down
20 changes: 16 additions & 4 deletions src/java/com/wolfssl/WolfSSLCertRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.security.PublicKey;
import java.security.PrivateKey;
Expand Down Expand Up @@ -243,6 +242,7 @@ public void setPublicKey(String filePath, int keyType, int format)

int ret = 0;
File keyFile = null;
byte[] fileBytes = null;

confirmObjectIsActive();

Expand All @@ -256,7 +256,13 @@ public void setPublicKey(String filePath, int keyType, int format)
filePath);
}

setPublicKey(Files.readAllBytes(keyFile.toPath()), keyType, format);
fileBytes = WolfSSL.fileToBytes(keyFile);
if (fileBytes == null) {
throw new WolfSSLException("Failed to read bytes from file: " +
filePath);
}

setPublicKey(fileBytes, keyType, format);
}

/**
Expand Down Expand Up @@ -506,6 +512,7 @@ public void signRequest(String filePath, int keyType, int format,

int ret = 0;
File keyFile = null;
byte[] fileBytes = null;

confirmObjectIsActive();

Expand All @@ -519,8 +526,13 @@ public void signRequest(String filePath, int keyType, int format,
filePath);
}

signRequest(Files.readAllBytes(keyFile.toPath()), keyType,
format, digestAlg);
fileBytes = WolfSSL.fileToBytes(keyFile);
if (fileBytes == null) {
throw new WolfSSLException("Failed to read bytes from file: " +
filePath);
}

signRequest(fileBytes, keyType, format, digestAlg);
}

/**
Expand Down
19 changes: 16 additions & 3 deletions src/java/com/wolfssl/WolfSSLCertificate.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ public void setPublicKey(String filePath, int keyType, int format)

int ret = 0;
File keyFile = null;
byte[] fileBytes = null;

confirmObjectIsActive();

Expand All @@ -509,7 +510,13 @@ public void setPublicKey(String filePath, int keyType, int format)
filePath);
}

setPublicKey(Files.readAllBytes(keyFile.toPath()), keyType, format);
fileBytes = WolfSSL.fileToBytes(keyFile);
if (fileBytes == null) {
throw new WolfSSLException("Failed to read bytes from file: " +
filePath);
}

setPublicKey(fileBytes, keyType, format);
}

/**
Expand Down Expand Up @@ -882,6 +889,7 @@ public void signCert(String filePath, int keyType, int format,

int ret = 0;
File keyFile = null;
byte[] fileBytes = null;

confirmObjectIsActive();

Expand All @@ -895,8 +903,13 @@ public void signCert(String filePath, int keyType, int format,
filePath);
}

signCert(Files.readAllBytes(keyFile.toPath()), keyType, format,
digestAlg);
fileBytes = WolfSSL.fileToBytes(keyFile);
if (fileBytes == null) {
throw new WolfSSLException("Failed to read bytes from file: " +
filePath);
}

signCert(fileBytes, keyType, format, digestAlg);
}

/**
Expand Down
23 changes: 19 additions & 4 deletions src/java/com/wolfssl/provider/jsse/adapter/WolfSSLJDK8Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.net.ssl.SSLParameters;
Expand Down Expand Up @@ -162,10 +163,24 @@ protected static void getApplicationProtocols(final SSLParameters in,
"WolfSSLJDK8Helper.getApplicationProtocols() cannot be null");
}

String[] appProtos = in.getApplicationProtocols();
if (appProtos != null) {
/* call WolfSSLParameters.setApplicationProtocols() */
out.setApplicationProtocols(appProtos);
try {
/* Android API < 29 does not support SSLParameters
* getApplicationProtocols(). Use reflection here to conditionally
* call it if available */
Method meth = SSLParameters.class.getMethod(
"getApplicationProtocols");
if (meth == null) {
return;
}
String[] appProtos = (String[])meth.invoke(in);
if (appProtos != null) {
/* call WolfSSLParameters.setApplicationProtocols() */
out.setApplicationProtocols(appProtos);
}
} catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
/* getApplicationProtocols() not available, just return */
return;
}
}

Expand Down

0 comments on commit 473f587

Please sign in to comment.