blob: bb573323d3e009f3e3e3c0203f743b6fa2a658f8 [file] [log] [blame]
Elliott Hughesee296862015-03-28 10:39:46 -07001#define TAG "ext4_utils"
2
Paul Lawrence92da49d2015-02-25 15:11:13 -08003#include "ext4_crypt.h"
4
5#include <string>
6#include <fstream>
7#include <map>
8
9#include <errno.h>
10#include <sys/mount.h>
Paul Lawrence92da49d2015-02-25 15:11:13 -080011
Elliott Hughesee296862015-03-28 10:39:46 -070012#include <cutils/klog.h>
13#include <cutils/properties.h>
Paul Lawrence92da49d2015-02-25 15:11:13 -080014
Paul Lawrence61980262015-03-16 15:35:55 -070015#include "unencrypted_properties.h"
Paul Lawrence92da49d2015-02-25 15:11:13 -080016
Paul Lawrence61980262015-03-16 15:35:55 -070017namespace {
18 std::map<std::string, std::string> s_password_store;
19}
Paul Lawrence92da49d2015-02-25 15:11:13 -080020
21bool e4crypt_non_default_key(const char* dir)
22{
23 int type = e4crypt_get_password_type(dir);
Paul Lawrence61980262015-03-16 15:35:55 -070024
25 // ext4enc:TODO Use consts, not 1 here
Paul Lawrence92da49d2015-02-25 15:11:13 -080026 return type != -1 && type != 1;
27}
28
29int e4crypt_get_password_type(const char* path)
30{
Paul Lawrence61980262015-03-16 15:35:55 -070031 UnencryptedProperties props(path);
32 if (props.Get<std::string>(properties::key).empty()) {
Elliott Hughesee296862015-03-28 10:39:46 -070033 KLOG_INFO(TAG, "No master key, so not ext4enc\n");
Paul Lawrence92da49d2015-02-25 15:11:13 -080034 return -1;
35 }
36
Paul Lawrence61980262015-03-16 15:35:55 -070037 return props.Get<int>(properties::type, 1);
Paul Lawrence92da49d2015-02-25 15:11:13 -080038}
39
40int e4crypt_change_password(const char* path, int crypt_type,
41 const char* password)
42{
43 // ext4enc:TODO Encrypt master key with password securely. Store hash of
44 // master key for validation
Paul Lawrence61980262015-03-16 15:35:55 -070045 UnencryptedProperties props(path);
46 if ( props.Set(properties::password, password)
47 && props.Set(properties::type, crypt_type))
48 return 0;
49 return -1;
Paul Lawrence92da49d2015-02-25 15:11:13 -080050}
51
Paul Lawrence61980262015-03-16 15:35:55 -070052int e4crypt_crypto_complete(const char* path)
Paul Lawrence92da49d2015-02-25 15:11:13 -080053{
Elliott Hughesee296862015-03-28 10:39:46 -070054 KLOG_INFO(TAG, "ext4 crypto complete called on %s\n", path);
Paul Lawrence61980262015-03-16 15:35:55 -070055 if (UnencryptedProperties(path).Get<std::string>(properties::key).empty()) {
Elliott Hughesee296862015-03-28 10:39:46 -070056 KLOG_INFO(TAG, "No master key, so not ext4enc\n");
Paul Lawrence92da49d2015-02-25 15:11:13 -080057 return -1;
58 }
59
60 return 0;
61}
62
63int e4crypt_check_passwd(const char* path, const char* password)
64{
Paul Lawrence61980262015-03-16 15:35:55 -070065 UnencryptedProperties props(path);
66 if (props.Get<std::string>(properties::key).empty()) {
Elliott Hughesee296862015-03-28 10:39:46 -070067 KLOG_INFO(TAG, "No master key, so not ext4enc\n");
Paul Lawrence92da49d2015-02-25 15:11:13 -080068 return -1;
69 }
70
Paul Lawrence61980262015-03-16 15:35:55 -070071 auto actual_password = props.Get<std::string>(properties::password);
Paul Lawrence92da49d2015-02-25 15:11:13 -080072
73 if (actual_password == password) {
74 s_password_store[path] = password;
75 return 0;
76 } else {
77 return -1;
78 }
79}
80
81int e4crypt_restart(const char* path)
82{
83 int rc = 0;
84
Elliott Hughesee296862015-03-28 10:39:46 -070085 KLOG_INFO(TAG, "ext4 restart called on %s\n", path);
Paul Lawrence92da49d2015-02-25 15:11:13 -080086 property_set("vold.decrypt", "trigger_reset_main");
Elliott Hughesee296862015-03-28 10:39:46 -070087 KLOG_INFO(TAG, "Just asked init to shut down class main\n");
Paul Lawrence92da49d2015-02-25 15:11:13 -080088 sleep(2);
89
90 std::string tmp_path = std::string() + path + "/tmp_mnt";
91
92 // ext4enc:TODO add retry logic
93 rc = umount(tmp_path.c_str());
94 if (rc) {
Elliott Hughesee296862015-03-28 10:39:46 -070095 KLOG_ERROR(TAG, "umount %s failed with rc %d, msg %s\n",
96 tmp_path.c_str(), rc, strerror(errno));
Paul Lawrence92da49d2015-02-25 15:11:13 -080097 return rc;
98 }
99
100 // ext4enc:TODO add retry logic
101 rc = umount(path);
102 if (rc) {
Elliott Hughesee296862015-03-28 10:39:46 -0700103 KLOG_ERROR(TAG, "umount %s failed with rc %d, msg %s\n",
104 path, rc, strerror(errno));
Paul Lawrence92da49d2015-02-25 15:11:13 -0800105 return rc;
106 }
107
108 return 0;
109}
110
111const char* e4crypt_get_password(const char* path)
112{
113 // ext4enc:TODO scrub password after timeout
114 auto i = s_password_store.find(path);
115 if (i == s_password_store.end()) {
116 return 0;
117 } else {
118 return i->second.c_str();
119 }
120}