Merge RP1A.191031.003

Change-Id: Idcf6c5ccd8dbfea46b82f0ae834d7d996dd74028
diff --git a/FsCrypt.cpp b/FsCrypt.cpp
index a84b497..0d4f02e 100644
--- a/FsCrypt.cpp
+++ b/FsCrypt.cpp
@@ -69,18 +69,10 @@
 using android::vold::Keymaster;
 using android::hardware::keymaster::V4_0::KeyFormat;
 using android::vold::writeStringToFile;
+using namespace android::fscrypt;
 
 namespace {
 
-struct PolicyKeyRef {
-    std::string contents_mode;
-    std::string filenames_mode;
-    int policy_version;
-    std::string key_raw_ref;
-
-    PolicyKeyRef() : policy_version(0) {}
-};
-
 const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
 const std::string device_key_path = device_key_dir + "/key";
 const std::string device_key_temp = device_key_dir + "/temp";
@@ -208,46 +200,35 @@
 }
 
 // Retrieve the options to use for encryption policies on the /data filesystem.
-static void get_data_file_encryption_options(PolicyKeyRef* key_ref) {
+static void get_data_file_encryption_options(EncryptionOptions* options) {
     auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
     if (entry == nullptr) {
         return;
     }
-    key_ref->contents_mode = entry->file_contents_mode;
-    key_ref->filenames_mode = entry->file_names_mode;
-    key_ref->policy_version = entry->file_policy_version;
+    if (!ParseOptions(entry->encryption_options, options)) {
+        LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
+                   << entry->encryption_options;
+        return;
+    }
 }
 
 // Retrieve the version to use for encryption policies on the /data filesystem.
 static int get_data_file_policy_version(void) {
-    auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
-    if (entry == nullptr) {
-        return 0;
-    }
-    return entry->file_policy_version;
+    EncryptionOptions options;
+    get_data_file_encryption_options(&options);
+    return options.version;
 }
 
 // Retrieve the options to use for encryption policies on adoptable storage.
-static bool get_volume_file_encryption_options(PolicyKeyRef* key_ref) {
-    key_ref->contents_mode =
-            android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
-    key_ref->filenames_mode =
+static bool get_volume_file_encryption_options(EncryptionOptions* options, int flags) {
+    auto contents_mode =
+            android::base::GetProperty("ro.crypto.volume.contents_mode",
+                                       is_ice_supported_external(flags) ? "ice" : "aes-256-xts");
+    auto filenames_mode =
             android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
-    key_ref->policy_version = 1;
-
-    std::string raw_flags = android::base::GetProperty("ro.crypto.volume.flags", "");
-    auto flags = android::base::Split(raw_flags, "+");
-    for (const auto& flag : flags) {
-        if (flag == "v1") {
-            key_ref->policy_version = 1;
-        } else if (flag == "v2") {
-            key_ref->policy_version = 2;
-        } else {
-            LOG(ERROR) << "Unknown flag in ro.crypto.volume.flags: " << flag;
-            return false;
-        }
-    }
-    return true;
+    return ParseOptions(android::base::GetProperty("ro.crypto.volume.options",
+                                                   contents_mode + ":" + filenames_mode + ":v1"),
+                        options);
 }
 
 // Install a key for use by encrypted files on the /data filesystem.
@@ -401,12 +382,6 @@
     return true;
 }
 
-static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
-    return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
-                                 key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
-                                 key_ref.filenames_mode.c_str(), key_ref.policy_version) == 0;
-}
-
 static bool is_numeric(const char* name) {
     for (const char* p = name; *p != '\0'; p++) {
         if (!isdigit(*p)) return false;
@@ -468,23 +443,25 @@
         return true;
     }
 
-    PolicyKeyRef device_ref;
-    get_data_file_encryption_options(&device_ref);
+    EncryptionPolicy device_policy;
+    get_data_file_encryption_options(&device_policy.options);
     wrapped_key_supported = is_wrapped_key_supported();
 
     if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
-                                              device_key_temp, "", device_ref.policy_version,
-                                              &device_ref.key_raw_ref, wrapped_key_supported))
+                                              device_key_temp, "", device_policy.options.version,
+                                              &device_policy.key_raw_ref, wrapped_key_supported))
         return false;
 
-    std::string options_string =
-            StringPrintf("%s:%s:v%d", device_ref.contents_mode.c_str(),
-                         device_ref.filenames_mode.c_str(), device_ref.policy_version);
+    std::string options_string;
+    if (!OptionsToString(device_policy.options, &options_string)) {
+        LOG(ERROR) << "Unable to serialize options";
+        return false;
+    }
     std::string options_filename = std::string("/data") + fscrypt_key_mode;
     if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
 
     std::string ref_filename = std::string("/data") + fscrypt_key_ref;
-    if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
+    if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
     LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
 
     KeyBuffer per_boot_key;
@@ -676,7 +653,7 @@
 }
 
 static bool read_or_create_volkey(const std::string& misc_path, const std::string& volume_uuid,
-                                  PolicyKeyRef* key_ref, int flags) {
+                                  EncryptionPolicy* policy, int flags) {
     auto secdiscardable_path = volume_secdiscardable_path(volume_uuid);
     std::string secdiscardable_hash;
     bool wrapped_key_supported = false;
@@ -698,25 +675,12 @@
     }
     android::vold::KeyAuthentication auth("", secdiscardable_hash);
 
-    if (!get_volume_file_encryption_options(key_ref)) return false;
+    if (!get_volume_file_encryption_options(&policy->options, flags)) return false;
     wrapped_key_supported = is_wrapped_key_supported_external(flags);
 
-    if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
-                                              volume_uuid, key_ref->policy_version,
-                                              &key_ref->key_raw_ref, wrapped_key_supported))
-        return false;
-
-    if (is_ice_supported_external(flags)) {
-        key_ref->contents_mode =
-             android::base::GetProperty("ro.crypto.volume.contents_mode", "ice");
-    } else {
-        key_ref->contents_mode =
-             android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
-    }
-
-    key_ref->filenames_mode =
-        android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
-    return true;
+    return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
+                                                volume_uuid, policy->options.version,
+                                                &policy->key_raw_ref, wrapped_key_supported);
 }
 
 static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
@@ -915,17 +879,18 @@
         if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
 
         if (fscrypt_is_native()) {
-            PolicyKeyRef de_ref;
+            EncryptionPolicy de_policy;
             if (volume_uuid.empty()) {
-                if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
-                get_data_file_encryption_options(&de_ref);
-                if (!ensure_policy(de_ref, system_de_path)) return false;
-                if (!ensure_policy(de_ref, misc_de_path)) return false;
-                if (!ensure_policy(de_ref, vendor_de_path)) return false;
+                if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_policy.key_raw_ref))
+                    return false;
+                get_data_file_encryption_options(&de_policy.options);
+                if (!EnsurePolicy(de_policy, system_de_path)) return false;
+                if (!EnsurePolicy(de_policy, misc_de_path)) return false;
+                if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
             } else {
-                if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_ref, flags)) return false;
+                if (!read_or_create_volkey(misc_de_path, volume_uuid, &de_policy, flags)) return false;
             }
-            if (!ensure_policy(de_ref, user_de_path)) return false;
+            if (!EnsurePolicy(de_policy, user_de_path)) return false;
         }
     }
 
@@ -946,19 +911,20 @@
         if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
 
         if (fscrypt_is_native()) {
-            PolicyKeyRef ce_ref;
+            EncryptionPolicy ce_policy;
             if (volume_uuid.empty()) {
-                if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
-                get_data_file_encryption_options(&ce_ref);
-                if (!ensure_policy(ce_ref, system_ce_path)) return false;
-                if (!ensure_policy(ce_ref, misc_ce_path)) return false;
-                if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
+                if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_policy.key_raw_ref))
+                    return false;
+                get_data_file_encryption_options(&ce_policy.options);
+                if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
+                if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
+                if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
 
             } else {
-                if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_ref, flags)) return false;
+                if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy, flags)) return false;
             }
-            if (!ensure_policy(ce_ref, media_ce_path)) return false;
-            if (!ensure_policy(ce_ref, user_ce_path)) return false;
+            if (!EnsurePolicy(ce_policy, media_ce_path)) return false;
+            if (!EnsurePolicy(ce_policy, user_ce_path)) return false;
         }
 
         if (volume_uuid.empty()) {