diff options
Diffstat (limited to 'src/mint/key_io.c')
-rw-r--r-- | src/mint/key_io.c | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/src/mint/key_io.c b/src/mint/key_io.c new file mode 100644 index 000000000..f401a1268 --- /dev/null +++ b/src/mint/key_io.c | |||
@@ -0,0 +1,241 @@ | |||
1 | /* | ||
2 | This file is part of TALER | ||
3 | (C) 2014 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | TALER is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | TALER is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file key_io.c | ||
19 | * @brief I/O operations for the Mint's private keys | ||
20 | * @author Florian Dold | ||
21 | * @author Benedikt Mueller | ||
22 | * @author Sree Harsha Totakura | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "mint.h" | ||
27 | #include "key_io.h" | ||
28 | |||
29 | |||
30 | struct SignkeysIterateContext | ||
31 | { | ||
32 | TALER_MINT_SignkeyIterator it; | ||
33 | void *it_cls; | ||
34 | }; | ||
35 | |||
36 | |||
37 | struct DenomkeysIterateContext | ||
38 | { | ||
39 | const char *alias; | ||
40 | TALER_MINT_DenomkeyIterator it; | ||
41 | void *it_cls; | ||
42 | }; | ||
43 | |||
44 | |||
45 | static int | ||
46 | signkeys_iterate_dir_iter (void *cls, | ||
47 | const char *filename) | ||
48 | { | ||
49 | |||
50 | struct SignkeysIterateContext *skc = cls; | ||
51 | ssize_t nread; | ||
52 | struct TALER_MINT_SignKeyIssuePriv issue; | ||
53 | |||
54 | nread = GNUNET_DISK_fn_read (filename, | ||
55 | &issue, | ||
56 | sizeof (struct TALER_MINT_SignKeyIssuePriv)); | ||
57 | if (nread != sizeof (struct TALER_MINT_SignKeyIssuePriv)) | ||
58 | { | ||
59 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid signkey file: '%s'\n", filename); | ||
60 | return GNUNET_OK; | ||
61 | } | ||
62 | return skc->it (skc->it_cls, &issue); | ||
63 | } | ||
64 | |||
65 | |||
66 | int | ||
67 | TALER_MINT_signkeys_iterate (const char *mint_base_dir, | ||
68 | TALER_MINT_SignkeyIterator it, void *cls) | ||
69 | { | ||
70 | char *signkey_dir; | ||
71 | size_t len; | ||
72 | struct SignkeysIterateContext skc; | ||
73 | |||
74 | len = GNUNET_asprintf (&signkey_dir, ("%s" DIR_SEPARATOR_STR DIR_SIGNKEYS), mint_base_dir); | ||
75 | GNUNET_assert (len > 0); | ||
76 | |||
77 | skc.it = it; | ||
78 | skc.it_cls = cls; | ||
79 | |||
80 | return GNUNET_DISK_directory_scan (signkey_dir, &signkeys_iterate_dir_iter, &skc); | ||
81 | } | ||
82 | |||
83 | |||
84 | /** | ||
85 | * Import a denomination key from the given file | ||
86 | * | ||
87 | * @param filename the file to import the key from | ||
88 | * @param dki pointer to return the imported denomination key | ||
89 | * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure | ||
90 | */ | ||
91 | int | ||
92 | TALER_MINT_read_denom_key (const char *filename, | ||
93 | struct TALER_MINT_DenomKeyIssuePriv *dki) | ||
94 | { | ||
95 | uint64_t size; | ||
96 | size_t offset; | ||
97 | void *data; | ||
98 | struct GNUNET_CRYPTO_rsa_PrivateKey *priv; | ||
99 | int ret; | ||
100 | |||
101 | ret = GNUNET_SYSERR; | ||
102 | data = NULL; | ||
103 | offset = sizeof (struct TALER_MINT_DenomKeyIssuePriv) | ||
104 | - offsetof (struct TALER_MINT_DenomKeyIssuePriv, issue.signature); | ||
105 | if (GNUNET_OK != GNUNET_DISK_file_size (filename, | ||
106 | &size, | ||
107 | GNUNET_YES, | ||
108 | GNUNET_YES)) | ||
109 | goto cleanup; | ||
110 | if (size <= offset) | ||
111 | { | ||
112 | GNUNET_break (0); | ||
113 | goto cleanup; | ||
114 | } | ||
115 | data = GNUNET_malloc (size); | ||
116 | if (size != GNUNET_DISK_fn_read (filename, | ||
117 | data, | ||
118 | size)) | ||
119 | goto cleanup; | ||
120 | if (NULL == (priv = GNUNET_CRYPTO_rsa_private_key_decode (data + offset, | ||
121 | size - offset))) | ||
122 | goto cleanup; | ||
123 | dki->denom_priv = priv; | ||
124 | memcpy (&dki->issue.signature, data, offset); | ||
125 | ret = GNUNET_OK; | ||
126 | |||
127 | cleanup: | ||
128 | GNUNET_free_non_null (data); | ||
129 | return ret; | ||
130 | } | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Exports a denomination key to the given file | ||
135 | * | ||
136 | * @param filename the file where to write the denomination key | ||
137 | * @param dki the denomination key | ||
138 | * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure. | ||
139 | */ | ||
140 | int | ||
141 | TALER_MINT_write_denom_key (const char *filename, | ||
142 | const struct TALER_MINT_DenomKeyIssuePriv *dki) | ||
143 | { | ||
144 | char *priv_enc; | ||
145 | size_t priv_enc_size; | ||
146 | struct GNUNET_DISK_FileHandle *fh; | ||
147 | ssize_t wrote; | ||
148 | size_t wsize; | ||
149 | int ret; | ||
150 | |||
151 | fh = NULL; | ||
152 | priv_enc_size = GNUNET_CRYPTO_rsa_private_key_encode (dki->denom_priv, | ||
153 | &priv_enc); | ||
154 | ret = GNUNET_SYSERR; | ||
155 | if (NULL == (fh = GNUNET_DISK_file_open | ||
156 | (filename, | ||
157 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE, | ||
158 | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))) | ||
159 | goto cleanup; | ||
160 | wsize = sizeof (struct TALER_MINT_DenomKeyIssuePriv) | ||
161 | - offsetof (struct TALER_MINT_DenomKeyIssuePriv, issue.signature); | ||
162 | if (GNUNET_SYSERR == (wrote = GNUNET_DISK_file_write (fh, | ||
163 | &dki->issue.signature, | ||
164 | wsize))) | ||
165 | goto cleanup; | ||
166 | if (wrote != wsize) | ||
167 | goto cleanup; | ||
168 | if (GNUNET_SYSERR == (wrote = GNUNET_DISK_file_write (fh, | ||
169 | priv_enc, | ||
170 | priv_enc_size))) | ||
171 | goto cleanup; | ||
172 | if (wrote != priv_enc_size) | ||
173 | goto cleanup; | ||
174 | ret = GNUNET_OK; | ||
175 | cleanup: | ||
176 | GNUNET_free_non_null (priv_enc); | ||
177 | if (NULL != fh) | ||
178 | (void) GNUNET_DISK_file_close (fh); | ||
179 | return ret; | ||
180 | } | ||
181 | |||
182 | |||
183 | static int | ||
184 | denomkeys_iterate_keydir_iter (void *cls, | ||
185 | const char *filename) | ||
186 | { | ||
187 | |||
188 | struct DenomkeysIterateContext *dic = cls; | ||
189 | struct TALER_MINT_DenomKeyIssuePriv issue; | ||
190 | |||
191 | if (GNUNET_OK != TALER_MINT_read_denom_key (filename, &issue)) | ||
192 | { | ||
193 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
194 | "Invalid denomkey file: '%s'\n", | ||
195 | filename); | ||
196 | return GNUNET_OK; | ||
197 | } | ||
198 | return dic->it (dic->it_cls, dic->alias, &issue); | ||
199 | } | ||
200 | |||
201 | |||
202 | static int | ||
203 | denomkeys_iterate_topdir_iter (void *cls, | ||
204 | const char *filename) | ||
205 | { | ||
206 | struct DenomkeysIterateContext *dic = cls; | ||
207 | |||
208 | dic->alias = GNUNET_STRINGS_get_short_name (filename); | ||
209 | |||
210 | // FIXME: differentiate between error case and normal iteration abortion | ||
211 | if (0 > GNUNET_DISK_directory_scan (filename, &denomkeys_iterate_keydir_iter, dic)) | ||
212 | return GNUNET_SYSERR; | ||
213 | return GNUNET_OK; | ||
214 | } | ||
215 | |||
216 | |||
217 | int | ||
218 | TALER_MINT_denomkeys_iterate (const char *mint_base_dir, | ||
219 | TALER_MINT_DenomkeyIterator it, void *cls) | ||
220 | { | ||
221 | char *dir; | ||
222 | size_t len; | ||
223 | struct DenomkeysIterateContext dic; | ||
224 | |||
225 | len = GNUNET_asprintf (&dir, | ||
226 | "%s" DIR_SEPARATOR_STR DIR_DENOMKEYS, | ||
227 | mint_base_dir); | ||
228 | GNUNET_assert (len > 0); | ||
229 | |||
230 | dic.it = it; | ||
231 | dic.it_cls = cls; | ||
232 | |||
233 | // scan over alias dirs | ||
234 | return GNUNET_DISK_directory_scan (dir, | ||
235 | &denomkeys_iterate_topdir_iter, | ||
236 | &dic); | ||
237 | } | ||
238 | |||
239 | |||
240 | |||
241 | /* end of mint_common.c */ | ||