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 + diff --git a/.gitignore b/.gitignore index 6143e53..a4beaae 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,17 @@ # 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 + +# Bazel output files +bazel-* 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 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..aee5b85 --- /dev/null +++ b/keytransparency/src/main/java/com/google/keytransparency/KTClient.java @@ -0,0 +1,77 @@ +package com.google.keytransparency; + +import android.content.Context; +import android.util.Log; +import android.widget.TextView; + +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 static KTClient client; + + // TODO remove me + TextView tv; + public void setTextViewForLogs(TextView tv) { + this.tv = tv; + } + + + 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 static KTClient getClient(int timeoutInMs){ + if (client == null) { + client = new KTClient(timeoutInMs); + } + return client; + } + + public void addKtServer(String ktUrl, boolean insecureTLS, byte[] ktTlsCertPem, byte[] domainInfoHash) throws KeyTransparencyException { + try { + GobindClient.bAddKtServer(ktUrl, insecureTLS, ktTlsCertPem, domainInfoHash); + } catch (Exception e) { + throw new KeyTransparencyException(e); + } + } + + 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 + 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; + } + } + +} 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 +