Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 1 | #define TAG "ext4_utils" |
| 2 | |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 3 | #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 Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 11 | |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 12 | #include <cutils/klog.h> |
| 13 | #include <cutils/properties.h> |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 14 | |
| 15 | // ext4enc::TODO remove this duplicated const |
| 16 | static const std::string unencrypted_path = "/unencrypted"; |
| 17 | |
| 18 | static std::map<std::string, std::string> s_password_store; |
| 19 | |
| 20 | bool e4crypt_non_default_key(const char* dir) |
| 21 | { |
| 22 | int type = e4crypt_get_password_type(dir); |
| 23 | return type != -1 && type != 1; |
| 24 | } |
| 25 | |
| 26 | int e4crypt_get_password_type(const char* path) |
| 27 | { |
| 28 | auto full_path = std::string() + path + unencrypted_path; |
| 29 | if (!std::ifstream(full_path + "/key")) { |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 30 | KLOG_INFO(TAG, "No master key, so not ext4enc\n"); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 31 | return -1; |
| 32 | } |
| 33 | |
| 34 | std::ifstream type(full_path + "/type"); |
| 35 | if (!type) { |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 36 | KLOG_INFO(TAG, "No password type so default\n"); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 37 | return 1; // Default |
| 38 | } |
| 39 | |
| 40 | int value = 0; |
| 41 | type >> value; |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 42 | KLOG_INFO(TAG, "Password type is %d\n", value); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 43 | return value; |
| 44 | } |
| 45 | |
| 46 | int e4crypt_change_password(const char* path, int crypt_type, |
| 47 | const char* password) |
| 48 | { |
| 49 | // ext4enc:TODO Encrypt master key with password securely. Store hash of |
| 50 | // master key for validation |
| 51 | auto full_path = std::string() + path + unencrypted_path; |
| 52 | std::ofstream(full_path + "/password") << password; |
| 53 | std::ofstream(full_path + "/type") << crypt_type; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | int e4crypt_crypto_complete(const char* path) |
| 58 | { |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 59 | KLOG_INFO(TAG, "ext4 crypto complete called on %s\n", path); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 60 | |
| 61 | auto full_path = std::string() + path + unencrypted_path; |
| 62 | if (!std::ifstream(full_path + "/key")) { |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 63 | KLOG_INFO(TAG, "No master key, so not ext4enc\n"); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int e4crypt_check_passwd(const char* path, const char* password) |
| 71 | { |
| 72 | auto full_path = std::string() + path + unencrypted_path; |
| 73 | if (!std::ifstream(full_path + "/key")) { |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 74 | KLOG_INFO(TAG, "No master key, so not ext4enc\n"); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | std::string actual_password; |
| 79 | std::ifstream(full_path + "/password") >> actual_password; |
| 80 | |
| 81 | if (actual_password == password) { |
| 82 | s_password_store[path] = password; |
| 83 | return 0; |
| 84 | } else { |
| 85 | return -1; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int e4crypt_restart(const char* path) |
| 90 | { |
| 91 | int rc = 0; |
| 92 | |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 93 | KLOG_INFO(TAG, "ext4 restart called on %s\n", path); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 94 | property_set("vold.decrypt", "trigger_reset_main"); |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 95 | KLOG_INFO(TAG, "Just asked init to shut down class main\n"); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 96 | sleep(2); |
| 97 | |
| 98 | std::string tmp_path = std::string() + path + "/tmp_mnt"; |
| 99 | |
| 100 | // ext4enc:TODO add retry logic |
| 101 | rc = umount(tmp_path.c_str()); |
| 102 | if (rc) { |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 103 | KLOG_ERROR(TAG, "umount %s failed with rc %d, msg %s\n", |
| 104 | tmp_path.c_str(), rc, strerror(errno)); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 105 | return rc; |
| 106 | } |
| 107 | |
| 108 | // ext4enc:TODO add retry logic |
| 109 | rc = umount(path); |
| 110 | if (rc) { |
Elliott Hughes | ee29686 | 2015-03-28 10:39:46 -0700 | [diff] [blame^] | 111 | KLOG_ERROR(TAG, "umount %s failed with rc %d, msg %s\n", |
| 112 | path, rc, strerror(errno)); |
Paul Lawrence | 92da49d | 2015-02-25 15:11:13 -0800 | [diff] [blame] | 113 | return rc; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | const char* e4crypt_get_password(const char* path) |
| 120 | { |
| 121 | // ext4enc:TODO scrub password after timeout |
| 122 | auto i = s_password_store.find(path); |
| 123 | if (i == s_password_store.end()) { |
| 124 | return 0; |
| 125 | } else { |
| 126 | return i->second.c_str(); |
| 127 | } |
| 128 | } |