1 #define TAG "ext4_utils"
3 #include "ext4_crypt.h"
10 #include <sys/mount.h>
12 #include <cutils/klog.h>
13 #include <cutils/properties.h>
15 #include "unencrypted_properties.h"
18 std::map<std::string, std::string> s_password_store;
21 bool e4crypt_non_default_key(const char* dir)
23 int type = e4crypt_get_password_type(dir);
25 // ext4enc:TODO Use consts, not 1 here
26 return type != -1 && type != 1;
29 int e4crypt_get_password_type(const char* path)
31 UnencryptedProperties props(path);
32 if (props.Get<std::string>(properties::key).empty()) {
33 KLOG_INFO(TAG, "No master key, so not ext4enc\n");
37 return props.Get<int>(properties::type, 1);
40 int e4crypt_change_password(const char* path, int crypt_type,
43 // ext4enc:TODO Encrypt master key with password securely. Store hash of
44 // master key for validation
45 UnencryptedProperties props(path);
46 if ( props.Set(properties::password, password)
47 && props.Set(properties::type, crypt_type))
52 int e4crypt_crypto_complete(const char* path)
54 KLOG_INFO(TAG, "ext4 crypto complete called on %s\n", path);
55 if (UnencryptedProperties(path).Get<std::string>(properties::key).empty()) {
56 KLOG_INFO(TAG, "No master key, so not ext4enc\n");
63 int e4crypt_check_passwd(const char* path, const char* password)
65 UnencryptedProperties props(path);
66 if (props.Get<std::string>(properties::key).empty()) {
67 KLOG_INFO(TAG, "No master key, so not ext4enc\n");
71 auto actual_password = props.Get<std::string>(properties::password);
73 if (actual_password == password) {
74 s_password_store[path] = password;
81 int e4crypt_restart(const char* path)
85 KLOG_INFO(TAG, "ext4 restart called on %s\n", path);
86 property_set("vold.decrypt", "trigger_reset_main");
87 KLOG_INFO(TAG, "Just asked init to shut down class main\n");
90 std::string tmp_path = std::string() + path + "/tmp_mnt";
92 // ext4enc:TODO add retry logic
93 rc = umount(tmp_path.c_str());
95 KLOG_ERROR(TAG, "umount %s failed with rc %d, msg %s\n",
96 tmp_path.c_str(), rc, strerror(errno));
100 // ext4enc:TODO add retry logic
103 KLOG_ERROR(TAG, "umount %s failed with rc %d, msg %s\n",
104 path, rc, strerror(errno));
111 const char* e4crypt_get_password(const char* path)
113 // ext4enc:TODO scrub password after timeout
114 auto i = s_password_store.find(path);
115 if (i == s_password_store.end()) {
118 return i->second.c_str();