Merge "Use a keystore_device for fallback to softkeymaster"
diff --git a/keystore-engine/Android.mk b/keystore-engine/Android.mk
index b33a347..e409952 100644
--- a/keystore-engine/Android.mk
+++ b/keystore-engine/Android.mk
@@ -15,16 +15,18 @@
 LOCAL_PATH := $(call my-dir)
 
 include $(CLEAR_VARS)
-include $(TOP)/external/openssl/flavor.mk
 
+
+ifneq (,$(wildcard $(TOP)/external/boringssl/flavor.mk))
+	include $(TOP)/external/boringssl/flavor.mk
+else
+	include $(TOP)/external/openssl/flavor.mk
+endif
 ifeq ($(OPENSSL_FLAVOR),BoringSSL)
   LOCAL_MODULE := libkeystore-engine
 
   LOCAL_SRC_FILES := \
 	android_engine.cpp
-
-  LOCAL_C_INCLUDES += \
-	external/openssl/src/include
 else
   LOCAL_MODULE := libkeystore
 
diff --git a/keystore-engine/keyhandle.cpp b/keystore-engine/keyhandle.cpp
index 1799735..aeba896 100644
--- a/keystore-engine/keyhandle.cpp
+++ b/keystore-engine/keyhandle.cpp
@@ -25,6 +25,8 @@
 
 #include <openssl/engine.h>
 
+#include <string.h>
+
 /**
  * Makes sure the ex_data for the keyhandle is initially set to NULL.
  */
diff --git a/keystore/Android.mk b/keystore/Android.mk
index 9dca502..42d05f7 100644
--- a/keystore/Android.mk
+++ b/keystore/Android.mk
@@ -22,7 +22,6 @@
 endif
 LOCAL_CFLAGS := -Wall -Wextra -Werror
 LOCAL_SRC_FILES := keystore.cpp keyblob_utils.cpp
-LOCAL_C_INCLUDES := external/openssl/include
 LOCAL_SHARED_LIBRARIES := \
 	libbinder \
 	libcutils \
@@ -44,7 +43,6 @@
 endif
 LOCAL_CFLAGS := -Wall -Wextra -Werror
 LOCAL_SRC_FILES := keystore_cli.cpp
-LOCAL_C_INCLUDES := external/openssl/include
 LOCAL_SHARED_LIBRARIES := libcutils libcrypto libkeystore_binder libutils liblog libbinder
 LOCAL_MODULE := keystore_cli
 LOCAL_MODULE_TAGS := debug
diff --git a/keystore/IKeystoreService.cpp b/keystore/IKeystoreService.cpp
index 40fbe0e..53788bd 100644
--- a/keystore/IKeystoreService.cpp
+++ b/keystore/IKeystoreService.cpp
@@ -295,6 +295,7 @@
         data.writeInt32(keyType);
         data.writeInt32(keySize);
         data.writeInt32(flags);
+        data.writeInt32(1);
         data.writeInt32(args->size());
         for (Vector<sp<KeystoreArg> >::iterator it = args->begin(); it != args->end(); ++it) {
             sp<KeystoreArg> item = *it;
@@ -640,7 +641,7 @@
     }
 };
 
-IMPLEMENT_META_INTERFACE(KeystoreService, "android.security.keystore");
+IMPLEMENT_META_INTERFACE(KeystoreService, "android.security.IKeystoreService");
 
 // ----------------------------------------------------------------------
 
@@ -768,15 +769,19 @@
             int32_t keySize = data.readInt32();
             int32_t flags = data.readInt32();
             Vector<sp<KeystoreArg> > args;
-            ssize_t numArgs = data.readInt32();
-            if (numArgs > 0) {
-                for (size_t i = 0; i < (size_t) numArgs; i++) {
-                    ssize_t inSize = data.readInt32();
-                    if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
-                        sp<KeystoreArg> arg = new KeystoreArg(data.readInplace(inSize), inSize);
-                        args.push_back(arg);
-                    } else {
-                        args.push_back(NULL);
+            int32_t argsPresent = data.readInt32();
+            if (argsPresent == 1) {
+                ssize_t numArgs = data.readInt32();
+                if (numArgs > 0) {
+                    for (size_t i = 0; i < (size_t) numArgs; i++) {
+                        ssize_t inSize = data.readInt32();
+                        if (inSize >= 0 && (size_t) inSize <= data.dataAvail()) {
+                            sp<KeystoreArg> arg = new KeystoreArg(data.readInplace(inSize),
+                                                                  inSize);
+                            args.push_back(arg);
+                        } else {
+                            args.push_back(NULL);
+                        }
                     }
                 }
             }
diff --git a/keystore/keyblob_utils.cpp b/keystore/keyblob_utils.cpp
index b208073..9970e8c 100644
--- a/keystore/keyblob_utils.cpp
+++ b/keystore/keyblob_utils.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 
diff --git a/keystore/keystore.cpp b/keystore/keystore.cpp
index d2875e5..eb1b836 100644
--- a/keystore/keystore.cpp
+++ b/keystore/keystore.cpp
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
+#include <strings.h>
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
diff --git a/softkeymaster/Android.mk b/softkeymaster/Android.mk
index 0c733aa..2a0a83f 100644
--- a/softkeymaster/Android.mk
+++ b/softkeymaster/Android.mk
@@ -21,9 +21,7 @@
 LOCAL_MODULE := keystore.default
 LOCAL_MODULE_RELATIVE_PATH := hw
 LOCAL_SRC_FILES := module.cpp
-LOCAL_C_INCLUDES := \
-	system/security/keystore \
-	external/openssl/include
+LOCAL_C_INCLUDES := system/security/keystore
 LOCAL_CFLAGS = -fvisibility=hidden -Wall -Werror
 LOCAL_SHARED_LIBRARIES := libcrypto liblog libkeystore_binder libsoftkeymaster
 LOCAL_MODULE_TAGS := optional
@@ -36,9 +34,7 @@
 endif
 LOCAL_MODULE := libsoftkeymaster
 LOCAL_SRC_FILES := keymaster_openssl.cpp
-LOCAL_C_INCLUDES := \
-	system/security/keystore \
-	external/openssl/include
+LOCAL_C_INCLUDES := system/security/keystore
 LOCAL_CFLAGS = -fvisibility=hidden -Wall -Werror
 LOCAL_SHARED_LIBRARIES := libcrypto liblog libkeystore_binder
 LOCAL_MODULE_TAGS := optional
diff --git a/softkeymaster/include/keymaster/softkeymaster.h b/softkeymaster/include/keymaster/softkeymaster.h
index 17d1389..e4b173c 100644
--- a/softkeymaster/include/keymaster/softkeymaster.h
+++ b/softkeymaster/include/keymaster/softkeymaster.h
@@ -40,5 +40,6 @@
 
 int openssl_open(const hw_module_t* module, const char* name, hw_device_t** device);
 
-struct keystore_module softkeymaster_module;
+extern struct keystore_module softkeymaster_module;
+
 #endif  // SOFTKEYMASTER_INCLUDE_KEYMASTER_SOFTKEYMASTER_H_