Merge pull request #4551 from alexzatsepin/add-self-signed-cert-support

[android] Added self-signed cert support
This commit is contained in:
Sergey Yershov 2016-10-25 18:51:22 +03:00 committed by GitHub
commit 79d61c0f34
4 changed files with 23 additions and 44 deletions

View file

@ -20,6 +20,7 @@ jclass g_myTrackerClazz;
jclass g_httpClientClazz;
jclass g_httpParamsClazz;
jclass g_socketWrapperClazz;
jclass g_utilsClazz;
extern "C"
{
@ -37,6 +38,7 @@ JNI_OnLoad(JavaVM * jvm, void *)
g_httpClientClazz = jni::GetGlobalClassRef(env, "com/mapswithme/util/HttpClient");
g_httpParamsClazz = jni::GetGlobalClassRef(env, "com/mapswithme/util/HttpClient$Params");
g_socketWrapperClazz = jni::GetGlobalClassRef(env, "com/mapswithme/maps/location/SocketWrapper");
g_utilsClazz = jni::GetGlobalClassRef(env, "com/mapswithme/util/Utils");
return JNI_VERSION_1_6;
}
@ -52,6 +54,7 @@ JNI_OnUnload(JavaVM *, void *)
env->DeleteGlobalRef(g_httpClientClazz);
env->DeleteGlobalRef(g_httpParamsClazz);
env->DeleteGlobalRef(g_socketWrapperClazz);
env->DeleteGlobalRef(g_utilsClazz);
}
} // extern "C"

View file

@ -15,6 +15,7 @@ extern jclass g_myTrackerClazz;
extern jclass g_httpClientClazz;
extern jclass g_httpParamsClazz;
extern jclass g_socketWrapperClazz;
extern jclass g_utilsClazz;
namespace jni
{

View file

@ -12,40 +12,12 @@
string Platform::UniqueClientId() const
{
string res;
if (!settings::Get("UniqueClientID", res))
{
JNIEnv * env = jni::GetEnv();
if (!env)
return string();
jclass uuidClass = env->FindClass("java/util/UUID");
ASSERT(uuidClass, ("Can't find java class java/util/UUID"));
jmethodID randomUUIDId = jni::GetStaticMethodID(env, uuidClass, "randomUUID", "()Ljava/util/UUID;");
jobject uuidInstance = env->CallStaticObjectMethod(uuidClass, randomUUIDId);
ASSERT(uuidInstance, ("UUID.randomUUID() returned NULL"));
jmethodID toStringId = env->GetMethodID(uuidClass, "toString", "()Ljava/lang/String;");
ASSERT(toStringId, ("Can't find java/util/UUID.toString() method"));
jstring uuidString = (jstring)env->CallObjectMethod(uuidInstance, toStringId);
ASSERT(uuidString, ("UUID.toString() returned NULL"));
char const * uuidUtf8 = env->GetStringUTFChars(uuidString, 0);
if (uuidUtf8 != 0)
{
res = uuidUtf8;
env->ReleaseStringUTFChars(uuidString, uuidUtf8);
}
res = HashUniqueID(res);
settings::Set("UniqueClientID", res);
}
return res;
JNIEnv * env = jni::GetEnv();
static jmethodID const getInstallationId = jni::GetStaticMethodID(env, g_utilsClazz, "getInstallationId",
"()Ljava/lang/String;");
static jstring const installationId = (jstring)env->CallStaticObjectMethod(g_utilsClazz, getInstallationId);
static string const result = jni::ToNativeString(env, installationId);
return result;
}
string Platform::GetMemoryInfo() const

View file

@ -1,12 +1,12 @@
package com.mapswithme.maps.location;
import android.net.SSLCertificateSocketFactory;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mapswithme.util.log.DebugLogger;
import com.mapswithme.util.log.Logger;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.io.InputStream;
@ -28,7 +28,7 @@ import java.net.SocketTimeoutException;
class SocketWrapper implements PlatformSocket
{
private final static Logger sLogger = new DebugLogger(SocketWrapper.class.getSimpleName());
private final static int DEFAULT_TIMEOUT = 30*1000;
private final static int DEFAULT_TIMEOUT = 30 * 1000;
@Nullable
private Socket mSocket;
@Nullable
@ -57,6 +57,7 @@ class SocketWrapper implements PlatformSocket
Socket socket = createSocket(host, port, true);
if (socket != null && socket.isConnected())
{
setReadSocketTimeout(socket, mTimeout);
mSocket = socket;
}
@ -73,13 +74,15 @@ class SocketWrapper implements PlatformSocket
{
if (ssl)
{
SocketFactory sf = SSLSocketFactory.getDefault();
//TODO: use a boolean flag about using the secure or insecure ssl socket (must be done before release!)
//https://jira.mail.ru/browse/MAPSME-2786
SSLSocketFactory sf = SSLCertificateSocketFactory.getInsecure(0, null);
try
{
return sf.createSocket(host, port);
} catch (IOException e)
{
sLogger.e("Failed to create the ssl socket, mHost", host, " mPort = ", port, e);
sLogger.e("Failed to create the ssl socket, mHost = ", host, " mPort = ", port, e);
}
} else
{
@ -194,7 +197,7 @@ class SocketWrapper implements PlatformSocket
sLogger.e(e, "Socked timeout has occurred after ", writingTime, " (ms) ");
} catch (IOException e)
{
sLogger.e(e, "Failed to write data from socket: ", this);
sLogger.e(e, "Failed to write data to socket: ", this);
}
return false;
@ -220,15 +223,15 @@ class SocketWrapper implements PlatformSocket
@Override
public void setTimeout(int millis)
{
if (mSocket == null)
throw new IllegalStateException("Socket must be initialized before setting the timeout");
mTimeout = millis;
sLogger.d("Set socket wrapper timeout = ", millis, " ms");
sLogger.d("Setting the socket wrapper timeout = ", millis, " ms");
}
private void setReadSocketTimeout(@NonNull Socket socket, int millis)
{
try
{
mSocket.setSoTimeout(millis);
socket.setSoTimeout(millis);
} catch (SocketException e)
{
sLogger.e("Failed to set system socket timeout: ", millis, "ms, ", this, e);