Enable properties in ext4enc

Enables OwnerInfo and pattern suppression

Bug: 18151196

Change-Id: I46144e16cb00319deeb5492ab82c67f5dd43d6d3
diff --git a/Ext4Crypt.cpp b/Ext4Crypt.cpp
index 0c7b351..0f6af6b 100644
--- a/Ext4Crypt.cpp
+++ b/Ext4Crypt.cpp
@@ -46,14 +46,6 @@
         uint32_t size;
     };
 
-    // ext4enc:TODO Get from somewhere good
-    struct ext4_encryption_policy {
-        char version;
-        char contents_encryption_mode;
-        char filenames_encryption_mode;
-        char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
-    } __attribute__((__packed__));
-
     namespace tag {
         const char* magic = "magic";
         const char* major_version = "major_version";
@@ -440,3 +432,28 @@
     return GetPropsOrAltProps(path).GetChild(properties::key)
       .Get<int>(tag::crypt_type, CRYPT_TYPE_DEFAULT);
 }
+
+int e4crypt_get_field(const char* path, const char* fieldname,
+                      char* value, size_t len)
+{
+    auto v = GetPropsOrAltProps(path).GetChild(properties::props)
+      .Get<std::string>(fieldname);
+
+    if (v == "") {
+        return CRYPTO_GETFIELD_ERROR_NO_FIELD;
+    }
+
+    if (v.length() >= len) {
+        return CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
+    }
+
+    strlcpy(value, v.c_str(), len);
+    return 0;
+}
+
+int e4crypt_set_field(const char* path, const char* fieldname,
+                      const char* value)
+{
+    return GetPropsOrAltProps(path).GetChild(properties::props)
+        .Set(fieldname, std::string(value)) ? 0 : -1;
+}
diff --git a/Ext4Crypt.h b/Ext4Crypt.h
index 301639d..68e0fb2 100644
--- a/Ext4Crypt.h
+++ b/Ext4Crypt.h
@@ -1,3 +1,4 @@
+#include <stddef.h>
 #include <sys/cdefs.h>
 
 __BEGIN_DECLS
@@ -12,5 +13,9 @@
 int e4crypt_get_password_type(const char* path);
 const char* e4crypt_get_password(const char* path);
 int e4crypt_restart(const char* path);
+int e4crypt_get_field(const char* path, const char* fieldname,
+                      char* value, size_t len);
+int e4crypt_set_field(const char* path, const char* fieldname,
+                      const char* value);
 
 __END_DECLS
diff --git a/cryptfs.c b/cryptfs.c
index 7398995..13ccd6c 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -3578,6 +3578,10 @@
 /* Return the value of the specified field. */
 int cryptfs_getfield(const char *fieldname, char *value, int len)
 {
+    if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
+        return e4crypt_get_field(DATA_MNT_POINT, fieldname, value, len);
+    }
+
     char temp_value[PROPERTY_VALUE_MAX];
     /* CRYPTO_GETFIELD_OK is success,
      * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
@@ -3639,6 +3643,10 @@
 /* Set the value of the specified field. */
 int cryptfs_setfield(const char *fieldname, const char *value)
 {
+    if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
+        return e4crypt_set_field(DATA_MNT_POINT, fieldname, value);
+    }
+
     char encrypted_state[PROPERTY_VALUE_MAX];
     /* 0 is success, negative values are error */
     int rc = CRYPTO_SETFIELD_ERROR_OTHER;