From 6fab6157e2ce56e9693cb16a8a99f8fbe87f9199 Mon Sep 17 00:00:00 2001 From: AMarcedone Date: Mon, 14 Aug 2017 18:29:20 -0700 Subject: [PATCH 1/6] Add amarcedone to CONTRIBUTORS file --- CONTRIBUTORS | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 4f093a2..409f59a 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -9,3 +9,4 @@ # Names should be added to this file as: # Name +Antonio Marcedone From c5d093d9d2599de7da755fcee680a7639b8668bb Mon Sep 17 00:00:00 2001 From: AMarcedone Date: Thu, 17 Aug 2017 14:35:32 -0700 Subject: [PATCH 2/6] Add default .bazelrc config file --- .bazelrc | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .bazelrc diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000..5c002c0 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,5 @@ +# Optional: GOPATH and ANDROID_HOME need to be passed to the bazel build command. +# To avoid typing them in the command line, please edit and uncomment the following lines. +# build --action_env=ANDROID_HOME=/Your/Android/Home/Path # example:/Users/YourUserName/Library/Android/sdk +# build --action_env=GOPATH=/Your/Go/Path/ # example: /Users/YourUserName/go + From d11625d1dfb3286878e3b6a9f1bb291763946cfc Mon Sep 17 00:00:00 2001 From: AMarcedone Date: Thu, 17 Aug 2017 14:38:19 -0700 Subject: [PATCH 3/6] Add initial KT library and bazel BUILD file --- WORKSPACE | 6 ++ keytransparency/BUILD | 28 +++++++ keytransparency/src/main/AndroidManifest.xml | 12 +++ .../com/google/keytransparency/KTClient.java | 84 +++++++++++++++++++ .../KeyTransparencyException.java | 10 +++ .../src/main/res/values/strings.xml | 3 + 6 files changed, 143 insertions(+) create mode 100644 WORKSPACE create mode 100644 keytransparency/BUILD create mode 100644 keytransparency/src/main/AndroidManifest.xml create mode 100644 keytransparency/src/main/java/com/google/keytransparency/KTClient.java create mode 100644 keytransparency/src/main/java/com/google/keytransparency/KeyTransparencyException.java create mode 100644 keytransparency/src/main/res/values/strings.xml diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000..5803f09 --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,6 @@ +android_sdk_repository( + name = "androidsdk", + path = "/Users/antonio.marcedone/Library/Android/sdk", + api_level = 25, + build_tools_version = "25.0.0" +) diff --git a/keytransparency/BUILD b/keytransparency/BUILD new file mode 100644 index 0000000..f79234b --- /dev/null +++ b/keytransparency/BUILD @@ -0,0 +1,28 @@ +android_library( + name = "keytransparency", + visibility = ["//visibility:public"], + srcs = ["src/main/java/com/google/keytransparency/KeyTransparencyException.java", + "src/main/java/com/google/keytransparency/KTClient.java"], + custom_package = "com.google.keytransparency", + manifest = "src/main/AndroidManifest.xml", + resource_files = glob(["src/main/res/**"]), + deps = [":gobind_keytransparency_android"], +) + +aar_import( + name = "gobind_keytransparency_android", + aar = "keytransparency_gobind.aar", + ) + +genrule( + name = "gobind_keytransparency_android_gen_aar", + srcs = [ + # "//some:files", # a filegroup with multiple files in it ==> $(locations) + # "//other:gen", # a genrule with a single output ==> $(location) + ], + outs = ["keytransparency_gobind.aar"], + tags=["local"], + cmd = "gomobile bind -target android -o $@ -javapkg com.google.keytransparency.gobind github.com/google/keytransparency/core/client/gobindClient", # github.com/google/keytransparency/core/client/kt github.com/google/keytransparency/core/crypto/commitments", +) + + diff --git a/keytransparency/src/main/AndroidManifest.xml b/keytransparency/src/main/AndroidManifest.xml new file mode 100644 index 0000000..43c17b5 --- /dev/null +++ b/keytransparency/src/main/AndroidManifest.xml @@ -0,0 +1,12 @@ + + + + + + + + + diff --git a/keytransparency/src/main/java/com/google/keytransparency/KTClient.java b/keytransparency/src/main/java/com/google/keytransparency/KTClient.java new file mode 100644 index 0000000..138ed1b --- /dev/null +++ b/keytransparency/src/main/java/com/google/keytransparency/KTClient.java @@ -0,0 +1,84 @@ +package com.google.keytransparency; + +import android.content.Context; +import android.util.Log; +import android.widget.TextView; + +import com.google.keytransparency.gobind.gobindClient.BClientParams; +import com.google.keytransparency.gobind.gobindClient.GobindClient; +import com.google.keytransparency.gobind.gobindClient.BWriter; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + + +public class KTClient { + private static final String TAG_LOGS_FROM_GOBIND = "KTGo:"; + + private final BClientParams clientParams; + + // TODO remove me + TextView tv; + public void setTextViewForLogs(TextView tv) { + this.tv = tv; + } + + + public KTClient(String ktUrl, long mapID, byte[] ktServerTLSCert, byte[] vrfPubKey, byte[] ktSmrPubKey, byte[] trLogKey) { + clientParams = new BClientParams(ktUrl, mapID, ktServerTLSCert, vrfPubKey, ktSmrPubKey, trLogKey); + GobindClient.bSetCustomLogger(new WriterForGoLogs()); + } + + public KTClient(String ktUrl, long mapID, Context context, int ktServerTLSCertResID, int vrfPubKeyResID, int ktSmrPubKeyResID, int trLogKeyResID) throws IOException { + this(ktUrl, mapID, resourceToByteArray(context, ktServerTLSCertResID), + resourceToByteArray(context, vrfPubKeyResID), + resourceToByteArray(context, ktSmrPubKeyResID), + resourceToByteArray(context, trLogKeyResID)); + } + + public byte[] getEntry(String userName, String appName, int timeoutInMilliseconds) throws KeyTransparencyException { + try { + // TODO(amarcedone): do we want to store the latest smr so that it can be used for consistency of new requests? + return GobindClient.bGetEntry(timeoutInMilliseconds, clientParams, userName, appName); + } catch (Exception e) { + throw new KeyTransparencyException(e); + } + } + +// byte[] verifyGetEntryResponse(){ +// return null; +// } + + private class WriterForGoLogs implements BWriter { + @Override + public long write(byte[] bytes) throws Exception { + // TODO(amarcedone): confirm utf-8 is the correct encoding here, as well as loglevel (i). + Log.i(TAG_LOGS_FROM_GOBIND, new String(bytes, "UTF-8")); + + // TODO REMOVE ME. + if(tv != null){ + tv.append(TAG_LOGS_FROM_GOBIND + new String(bytes, "UTF-8")); + } + + return bytes.length; + } + } + + private static byte[] resourceToByteArray(Context context, int resourceId) throws IOException { + InputStream is = context.getResources().openRawResource(resourceId); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + + int nRead; + // TODO(amarcedone): Buffer size was chosen arbitrarily. Figure out what is appropriate. + byte[] data = new byte[16384]; + + while ((nRead = is.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + + buffer.flush(); + return buffer.toByteArray(); + } + +} diff --git a/keytransparency/src/main/java/com/google/keytransparency/KeyTransparencyException.java b/keytransparency/src/main/java/com/google/keytransparency/KeyTransparencyException.java new file mode 100644 index 0000000..c20d730 --- /dev/null +++ b/keytransparency/src/main/java/com/google/keytransparency/KeyTransparencyException.java @@ -0,0 +1,10 @@ +package com.google.keytransparency; + +/** + * A KeyTransparencyException is thrown whenever the go native code throws an Exception. + */ +public class KeyTransparencyException extends Throwable { + public KeyTransparencyException(Exception e) { + super(e); + } +} diff --git a/keytransparency/src/main/res/values/strings.xml b/keytransparency/src/main/res/values/strings.xml new file mode 100644 index 0000000..d306963 --- /dev/null +++ b/keytransparency/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + keytransparency + From e7aa66be91718383546059dfc91ccf4c700b3f77 Mon Sep 17 00:00:00 2001 From: AMarcedone Date: Thu, 17 Aug 2017 14:40:04 -0700 Subject: [PATCH 4/6] Add AndroidStudio files to gitignore --- .gitignore | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.gitignore b/.gitignore index 6143e53..70483ee 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,14 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +# AndroidStudio files +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild From 06e4c8ff9b38ddd10e64e1fbe4c2efdfc7d92cc2 Mon Sep 17 00:00:00 2001 From: AMarcedone Date: Thu, 17 Aug 2017 15:46:38 -0700 Subject: [PATCH 5/6] Add gradle output files to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 70483ee..a4beaae 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ hs_err_pid* /build /captures .externalNativeBuild + +# Bazel output files +bazel-* From 696fa07cbdb9939137f9e77b622a03a3ed0f9252 Mon Sep 17 00:00:00 2001 From: AMarcedone Date: Tue, 22 Aug 2017 15:10:29 -0700 Subject: [PATCH 6/6] Updated client --- .../com/google/keytransparency/KTClient.java | 57 ++++++++----------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/keytransparency/src/main/java/com/google/keytransparency/KTClient.java b/keytransparency/src/main/java/com/google/keytransparency/KTClient.java index 138ed1b..aee5b85 100644 --- a/keytransparency/src/main/java/com/google/keytransparency/KTClient.java +++ b/keytransparency/src/main/java/com/google/keytransparency/KTClient.java @@ -4,7 +4,6 @@ import android.util.Log; import android.widget.TextView; -import com.google.keytransparency.gobind.gobindClient.BClientParams; import com.google.keytransparency.gobind.gobindClient.GobindClient; import com.google.keytransparency.gobind.gobindClient.BWriter; @@ -16,7 +15,7 @@ public class KTClient { private static final String TAG_LOGS_FROM_GOBIND = "KTGo:"; - private final BClientParams clientParams; + private static KTClient client; // TODO remove me TextView tv; @@ -25,30 +24,40 @@ public void setTextViewForLogs(TextView tv) { } - public KTClient(String ktUrl, long mapID, byte[] ktServerTLSCert, byte[] vrfPubKey, byte[] ktSmrPubKey, byte[] trLogKey) { - clientParams = new BClientParams(ktUrl, mapID, ktServerTLSCert, vrfPubKey, ktSmrPubKey, trLogKey); - GobindClient.bSetCustomLogger(new WriterForGoLogs()); + private KTClient(int timeoutInMs){ + try { + GobindClient.bInit(timeoutInMs); + GobindClient.bSetCustomLogger(new WriterForGoLogs()); + }catch (Exception e) { + // This should never happen actually. We are enforcing init can be called only once, + // and so far the only error comes from calling init twice. + throw new RuntimeException(e); + } } - public KTClient(String ktUrl, long mapID, Context context, int ktServerTLSCertResID, int vrfPubKeyResID, int ktSmrPubKeyResID, int trLogKeyResID) throws IOException { - this(ktUrl, mapID, resourceToByteArray(context, ktServerTLSCertResID), - resourceToByteArray(context, vrfPubKeyResID), - resourceToByteArray(context, ktSmrPubKeyResID), - resourceToByteArray(context, trLogKeyResID)); + public static KTClient getClient(int timeoutInMs){ + if (client == null) { + client = new KTClient(timeoutInMs); + } + return client; } - public byte[] getEntry(String userName, String appName, int timeoutInMilliseconds) throws KeyTransparencyException { + public void addKtServer(String ktUrl, boolean insecureTLS, byte[] ktTlsCertPem, byte[] domainInfoHash) throws KeyTransparencyException { try { - // TODO(amarcedone): do we want to store the latest smr so that it can be used for consistency of new requests? - return GobindClient.bGetEntry(timeoutInMilliseconds, clientParams, userName, appName); + GobindClient.bAddKtServer(ktUrl, insecureTLS, ktTlsCertPem, domainInfoHash); } catch (Exception e) { throw new KeyTransparencyException(e); } } -// byte[] verifyGetEntryResponse(){ -// return null; -// } + public byte[] getEntry(String ktUrl, String userName, String appName) throws KeyTransparencyException { + try { + // TODO(amarcedone): do we want to store the latest smr so that it can be used for consistency of new requests? + return GobindClient.bGetEntry(ktUrl, userName, appName); + } catch (Exception e) { + throw new KeyTransparencyException(e); + } + } private class WriterForGoLogs implements BWriter { @Override @@ -65,20 +74,4 @@ public long write(byte[] bytes) throws Exception { } } - private static byte[] resourceToByteArray(Context context, int resourceId) throws IOException { - InputStream is = context.getResources().openRawResource(resourceId); - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - - int nRead; - // TODO(amarcedone): Buffer size was chosen arbitrarily. Figure out what is appropriate. - byte[] data = new byte[16384]; - - while ((nRead = is.read(data, 0, data.length)) != -1) { - buffer.write(data, 0, nRead); - } - - buffer.flush(); - return buffer.toByteArray(); - } - }