summaryrefslogtreecommitdiff
path: root/libsylph/masterpassword.c
diff options
context:
space:
mode:
Diffstat (limited to 'libsylph/masterpassword.c')
-rw-r--r--libsylph/masterpassword.c159
1 files changed, 115 insertions, 44 deletions
diff --git a/libsylph/masterpassword.c b/libsylph/masterpassword.c
index ee443aa..4cef644 100644
--- a/libsylph/masterpassword.c
+++ b/libsylph/masterpassword.c
@@ -21,6 +21,8 @@
21#include "config.h" 21#include "config.h"
22#endif 22#endif
23 23
24#include <glib/gi18n.h>
25
24#include "prefs_common.h" 26#include "prefs_common.h"
25#include "ssl.h" 27#include "ssl.h"
26#include "utils.h" 28#include "utils.h"
@@ -29,72 +31,141 @@
29gchar *master_password; 31gchar *master_password;
30 32
31void set_master_password(const char *password) { 33void set_master_password(const char *password) {
32 master_password = password; 34 master_password = password;
33} 35}
34 36
35gchar *get_master_password(void) { 37gchar *get_master_password(void) {
36 return master_password; 38 return master_password;
37} 39}
38 40
41void unload_master_password(void) {
42
39#if USE_SSL 43#if USE_SSL
44 OPENSSL_cleanse(master_password, strlen(master_password));
45#endif
46 g_free(master_password);
47 master_password = NULL;
40 48
41gint set_master_password_interactively(guint max_attempts) { 49}
50
51gboolean master_password_active(void) {
52
53#if USE_SSL
54 return ((master_password != NULL) &&
55 prefs_common.use_master_password);
56#else
57 return FALSE;
58#endif
59
60}
42 61
43 if (master_password != NULL) 62gchar *decrypt_with_master_password(gchar *str) {
44 return 0; /* master_password already set */
45 63
46 master_password = input_set_new_password(max_attempts); 64#if USE_SSL
65 gchar *new_str;
47 66
48 if (master_password == NULL) 67 if ((!str) || (!master_password_active()))
49 return 1; 68 return str; /* do nothing */
50 69
51 if (generate_password_hash( 70 if (decrypt_data(&new_str,
52 &prefs_common.master_password_hash, 71 str,
53 master_password, 72 master_password,
54 NULL) != RC_OK) { 73 strlen(str)) != RC_OK) {
55 /* should not really happen unless buggy code / library */ 74 OPENSSL_cleanse(new_str, strlen(new_str));
56 g_free(prefs_common.master_password_hash); 75 g_free(new_str);
57 prefs_common.master_password_hash = NULL; 76 return str;
58 debug_print(_("Could not generate master password hash")); 77 }
59 return 1;
60 }
61 78
62 prefs_common_write_config(); 79 return new_str;
63 return 0; 80#else
81 return str; /* do nothing */
82#endif
64 83
65} 84}
66 85
67gint check_master_password_interactively(guint max_attempts) { 86gchar *encrypt_with_master_password(gchar *str) {
87
88#if USE_SSL
89 gchar *new_str;
90 gint length_encrypted;
68 91
69 guint cnt; 92 if ((!str) || (!master_password_active()))
93 return str; /* do nothing */
70 94
71 if (max_attempts < 1) 95 if (encrypt_data(&new_str,
72 return 1; 96 &length_encrypted,
97 str,
98 master_password,
99 strlen(str),
100 32, /* TODO: to be set in prefs_common */
101 TRUE) != RC_OK) {
102 OPENSSL_cleanse(new_str, strlen(new_str));
103 g_free(new_str);
104 return str;
105 }
73 106
74 if (prefs_common.master_password_hash != NULL) { 107 return new_str;
75 return 1; 108#else
76 } 109 return str; /* do nothing */
110#endif
111
112}
113
114#if USE_SSL
115gint set_master_password_interactively(guint max_attempts) {
77 116
78 if (master_password != NULL) { 117 if (master_password == NULL)
79 /* password already cached */ 118 master_password = input_set_new_password(max_attempts);
80 return check_password(master_password,
81 prefs_common.master_password_hash);
82 }
83 119
84 for (cnt = 0; cnt < max_attempts; ++cnt) { 120 if (master_password == NULL)
85 master_password = input_query_master_password(); 121 return 1;
86 if (check_password(master_password,
87 prefs_common.master_password_hash) == RC_OK) {
88 return RC_OK; /* match */
89 }
90 debug_print(_("Wrong master password entered (%d)\n"), cnt);
91 /* TODO: clear before free? */
92 g_free(master_password);
93 master_password = NULL;
94 }
95 122
96 return 1; /* no match */ 123 if (generate_password_hash(
124 &prefs_common.master_password_hash,
125 master_password,
126 NULL) != RC_OK) {
127 /* should not really happen unless buggy code / library */
128 g_free(prefs_common.master_password_hash);
129 prefs_common.master_password_hash = NULL;
130 debug_print(_("Could not generate master password hash"));
131 return 1;
132 }
133
134 prefs_common_write_config();
135 return 0;
97 136
98} 137}
99 138
139gint check_master_password_interactively(guint max_attempts) {
140
141 guint cnt;
142
143 if (max_attempts < 1)
144 return 1;
145
146 if (prefs_common.master_password_hash != NULL) {
147 return 1;
148 }
149
150 if (master_password != NULL) {
151 /* password already cached */
152 return check_password(master_password,
153 prefs_common.master_password_hash);
154 }
155
156 for (cnt = 0; cnt < max_attempts; ++cnt) {
157 master_password = input_query_master_password();
158 if (check_password(master_password,
159 prefs_common.master_password_hash) == RC_OK) {
160 return RC_OK; /* match */
161 }
162 debug_print(_("Wrong master password entered (%d)\n"), cnt);
163 OPENSSL_cleanse(master_password, strlen(master_password));
164 g_free(master_password);
165 master_password = NULL;
166 }
167
168 return 1; /* no match */
169
170}
100#endif /* USE_SSL */ 171#endif /* USE_SSL */