Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #305 from lrasmus/master
Browse files Browse the repository at this point in the history
Closes #304 - support HTTPS on non-standard port
  • Loading branch information
denzilferreira authored Jan 11, 2023
2 parents 1077308 + 0570ae1 commit 84660be
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions aware-core/src/main/java/com/aware/utils/SSLManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static void handleUrl(Context context, String url, boolean block) {
if (protocol.equalsIgnoreCase("http")) return; //no need to do anything for http server

String hostname = study_uri.getHost();
int port = study_uri.getPort();
if (study_uri.getQuery() != null) {
// If it is in URL parameters, always unconditionally handle it
String crt = study_uri.getQueryParameter("crt");
Expand All @@ -66,7 +67,7 @@ public static void handleUrl(Context context, String url, boolean block) {
if (Aware.DEBUG)
Log.d(Aware.TAG, "Certificates: Downloading crt if not present: " + hostname);
if (!hasCertificate(context, hostname)) {
downloadCertificate(context, protocol, hostname, true);
downloadCertificate(context, protocol, hostname, port, true);
} else {
if (Aware.DEBUG)
Log.d(Aware.TAG, "Certificates: Already present and key_management=once: " + hostname);
Expand All @@ -75,7 +76,7 @@ public static void handleUrl(Context context, String url, boolean block) {
try {
if (!hasCertificate(context, hostname)) {
if (Aware.DEBUG) Log.d(Aware.TAG, "Certificates: Downloading for the first time SSL certificate: " + protocol+"://"+hostname);
downloadCertificate(context, protocol, hostname, true);
downloadCertificate(context, protocol, hostname, port, true);
} else {
//Cached certificate information
InputStream localCertificate = getCertificate(context, hostname);
Expand All @@ -96,6 +97,7 @@ public static class CheckCertificates extends AsyncTask<X509Certificate, Void, V
private String url;
private String protocol;
private String hostname;
private int port;
private Context context;

CheckCertificates(Context context, String URL) {
Expand All @@ -104,6 +106,7 @@ public static class CheckCertificates extends AsyncTask<X509Certificate, Void, V

Uri study_uri = Uri.parse(url);
this.hostname = study_uri.getHost();
this.port = study_uri.getPort();

this.protocol = "http";
try {
Expand All @@ -116,9 +119,9 @@ public static class CheckCertificates extends AsyncTask<X509Certificate, Void, V
@Override
protected Void doInBackground(X509Certificate... x509Certificate) {
try {
X509Certificate remote_certificate = retrieveRemoteCertificate(new URL(protocol+"://"+hostname));
X509Certificate remote_certificate = retrieveRemoteCertificate(new URL(protocol, hostname, port, ""));
if (!x509Certificate[0].equals(remote_certificate)) { //local certificate is expired or different, download new certificate
downloadCertificate(context, protocol, hostname, true);
downloadCertificate(context, protocol, hostname, port, true);
//this will force download of SSL certificate from the server. Checked every 15 minutes until successful update to up-to-date certificate.
}
} catch (MalformedURLException e) {
Expand Down Expand Up @@ -204,7 +207,7 @@ public boolean verify(String hostname, SSLSession session) {
* @param hostname Hostname to download.
* @param block If true, block until certificate retrieved, otherwise do not.
*/
private static void downloadCertificate(Context context, String protocol, String hostname, boolean block) {
private static void downloadCertificate(Context context, String protocol, String hostname, int port, boolean block) {
File root_folder;
if (context.getApplicationContext().getResources().getBoolean(R.bool.internalstorage)) {
root_folder = new File(context.getFilesDir(), "/credentials/" + hostname);
Expand All @@ -216,7 +219,7 @@ private static void downloadCertificate(Context context, String protocol, String
root_folder.mkdirs();

try {
X509Certificate certificate = retrieveRemoteCertificate(new URL(protocol+"://"+hostname));
X509Certificate certificate = retrieveRemoteCertificate(new URL(protocol, hostname, port, ""));
byte[] certificate_data = certificate.getEncoded();
FileOutputStream outputStream = new FileOutputStream(new File(root_folder.toString() + "/server.crt"));
outputStream.write(certificate_data);
Expand Down

0 comments on commit 84660be

Please sign in to comment.