Merge pull request #216 from cconlon/androidLint

JNI/JSSE: adjust for methods not available in Android API 24
pull/218/head
JacobBarthelmeh 2024-08-07 11:45:05 -06:00 committed by GitHub
commit 473f587918
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 106 additions and 11 deletions

View File

@ -21,6 +21,11 @@
package com.wolfssl; 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. * Base class which wraps the native WolfSSL embedded SSL library.
* This class contains library init and cleanup methods, general callback * This class contains library init and cleanup methods, general callback
@ -707,6 +712,56 @@ public class WolfSSL {
System.load(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 --------------- */ /* --------------- native feature detection functions --------------- */
/** /**

View File

@ -22,7 +22,6 @@ package com.wolfssl;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.PublicKey; import java.security.PublicKey;
import java.security.PrivateKey; import java.security.PrivateKey;
@ -243,6 +242,7 @@ public class WolfSSLCertRequest {
int ret = 0; int ret = 0;
File keyFile = null; File keyFile = null;
byte[] fileBytes = null;
confirmObjectIsActive(); confirmObjectIsActive();
@ -256,7 +256,13 @@ public class WolfSSLCertRequest {
filePath); 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);
} }
/** /**
@ -506,6 +512,7 @@ public class WolfSSLCertRequest {
int ret = 0; int ret = 0;
File keyFile = null; File keyFile = null;
byte[] fileBytes = null;
confirmObjectIsActive(); confirmObjectIsActive();
@ -519,8 +526,13 @@ public class WolfSSLCertRequest {
filePath); filePath);
} }
signRequest(Files.readAllBytes(keyFile.toPath()), keyType, fileBytes = WolfSSL.fileToBytes(keyFile);
format, digestAlg); if (fileBytes == null) {
throw new WolfSSLException("Failed to read bytes from file: " +
filePath);
}
signRequest(fileBytes, keyType, format, digestAlg);
} }
/** /**

View File

@ -496,6 +496,7 @@ public class WolfSSLCertificate {
int ret = 0; int ret = 0;
File keyFile = null; File keyFile = null;
byte[] fileBytes = null;
confirmObjectIsActive(); confirmObjectIsActive();
@ -509,7 +510,13 @@ public class WolfSSLCertificate {
filePath); 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);
} }
/** /**
@ -882,6 +889,7 @@ public class WolfSSLCertificate {
int ret = 0; int ret = 0;
File keyFile = null; File keyFile = null;
byte[] fileBytes = null;
confirmObjectIsActive(); confirmObjectIsActive();
@ -895,8 +903,13 @@ public class WolfSSLCertificate {
filePath); filePath);
} }
signCert(Files.readAllBytes(keyFile.toPath()), keyType, format, fileBytes = WolfSSL.fileToBytes(keyFile);
digestAlg); if (fileBytes == null) {
throw new WolfSSLException("Failed to read bytes from file: " +
filePath);
}
signCert(fileBytes, keyType, format, digestAlg);
} }
/** /**

View File

@ -23,6 +23,7 @@ package com.wolfssl.provider.jsse;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLParameters;
@ -162,10 +163,24 @@ public class WolfSSLJDK8Helper
"WolfSSLJDK8Helper.getApplicationProtocols() cannot be null"); "WolfSSLJDK8Helper.getApplicationProtocols() cannot be null");
} }
String[] appProtos = in.getApplicationProtocols(); try {
if (appProtos != null) { /* Android API < 29 does not support SSLParameters
/* call WolfSSLParameters.setApplicationProtocols() */ * getApplicationProtocols(). Use reflection here to conditionally
out.setApplicationProtocols(appProtos); * 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;
} }
} }