~ubuntu-branches/debian/sid/cryptsetup/sid

« back to all changes in this revision

Viewing changes to debian/patches/03_create_fix_keyfile.patch

  • Committer: Package Import Robot
  • Author(s): Jonas Meurer
  • Date: 2012-02-05 03:17:59 UTC
  • mfrom: (0.2.9)
  • Revision ID: package-import@ubuntu.com-20120205031759-kua3bplwunj0q2zl
Tags: 2:1.4.1-1
* new upstream release (1.4.0 + 1.4.1) (closes: #647851)
  - fixes typo in german translation. (closes: #645528)
  - remove patches, all incorporated upstream.
  - soname bump, rename library package to libcryptsetup4
* check for busybox in initramfs cryptroot hook, and install the sed binary
  in case it's either not installed or not activated. (closes: #591853)
* add checks for 'type $KEYSCRIPT' to initscripts cryptdisks.functions, and
  to cryptroot initramfs script/hook. this adds support for keyscripts inside
  $PATH. thanks to Ian Jackson for the suggestion. (closes: #597583)
* use argument '--sysinit' for vgchange in cryptroot initramfs script. Thanks
  to Christoph Anton Mitterer for the suggestion.
* add option for discard/trim features to crypttab and initramfs scripts.
  Thanks to intrigeri and Peter Colberg for patches. (closes: #648868)
* print $target on error in initramfs hook. Thanks to Daniel Hahler for the
  bugreport. (closes: #648192)
* add a warning about using decrypt_derived keyscript for devices with
  persistent data. Thanks to Arno Wagner for pointing this out.
* remove quotes from resume device candidates at get_resume_devs() in
  initramfs hook script. Thanks to Johannes Rohr. (closes: #634017)
* support custom $TABFILE, thanks to Douglas Huff. (closes: #638317)
* fix get_lvm_deps() in initramfs cryptroot hook to add all physical volumes
  of lvm volume group that contains the rootfs logical volume, even if the
  rootfs is lv is not spread over all physical volumes. Thanks to Christian
  Pernegger for bugreport and patch. (closes: #634109)
* debian/initramfs/cryptroot-script: Move check for maximum number of tries
  behind the while loop, to make the warning appear in case that maximum
  number of tries is reached. Thanks to Chistian Lamparter for bugreport and
  patch. (closes: #646083)
* incorporate changes to package descriptions and debconf templates that
  suggested by debian-l10n-english people. Special thanks go to Justin B Rye.
* acknowledge NMU, thanks a lot to Christian Perrier for his great work on
  the i18n front. (closes: #633105, #641719, #641839, #641947, #642470,
  #640056, #642540, #643633, #643962, #644853)
* add and update debconf translations:
  - italian, thanks to Milo Casagrande, Francesca Ciceri. (closes: #656933)
  - german, thanks to Erik Pfannenstein. (closes: #642147)
  - spanish, thanks to Camaleón. (closes: #658360)
  - russian, thanks to Yuri Kuzlov (closes: #654676)
* set architecture to linux-any, depends on linux kernel anyway. Thanks to
  Christoph Egger. (closes: #638257)
* small updates to the copyright file.
* add targets build-indep and build-arch to debian/rules, thanks to lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Description: Fix keyfile=- processing in create command. (regression in 1.3.0)
2
 
Author: Milan Broz <milan.broz@redhat.com>
3
 
Last-Update: 2011-05-17
4
 
 
5
 
--- a/src/cryptsetup.c
6
 
+++ b/src/cryptsetup.c
7
 
@@ -263,8 +263,9 @@
8
 
                goto out;
9
 
 
10
 
        if (opt_key_file)
11
 
+               /* With hashing, read the whole keyfile */
12
 
                r = crypt_activate_by_keyfile(cd, action_argv[0],
13
 
-                       CRYPT_ANY_SLOT, opt_key_file, key_size,
14
 
+                       CRYPT_ANY_SLOT, opt_key_file, params.hash ? 0 : key_size,
15
 
                        opt_readonly ?  CRYPT_ACTIVATE_READONLY : 0);
16
 
        else {
17
 
                r = crypt_get_key(_("Enter passphrase: "),
18
 
--- a/tests/password-hash-test
19
 
+++ b/tests/password-hash-test
20
 
@@ -34,10 +34,14 @@
21
 
                echo -e -n "$4" | $CRYPTSETUP create -c $MODE -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
22
 
                ret=$?
23
 
                ;;
24
 
-       stdin)
25
 
+       std-)
26
 
                echo -e -n "$4" | $CRYPTSETUP create -c $MODE -d "-" -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
27
 
                ret=$?
28
 
                ;;
29
 
+       stdin)
30
 
+               echo -e -n "$4" | $CRYPTSETUP create -c $MODE -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
31
 
+               ret=$?
32
 
+               ;;
33
 
        cat)
34
 
                cat $4 | $CRYPTSETUP create -c $MODE -h $1 -s $2 $LIMIT $DEV2 /dev/mapper/$DEV_NAME 2>/dev/null
35
 
                ret=$?
36
 
@@ -51,9 +55,12 @@
37
 
                ;;
38
 
        esac
39
 
 
40
 
-       if [ $ret -ne 0 ] ; then
41
 
-               echo " [n/a]"
42
 
-               return
43
 
+       # ignore these cases, not all libs/kernel supports it
44
 
+       if [ "$1" == "ripemd160" -o $2 -gt 256 ] ; then
45
 
+               if [ $ret -ne 0 ] ; then
46
 
+                       echo " [N/A] ($ret, SKIPPED)"
47
 
+                       return
48
 
+               fi
49
 
        fi
50
 
 
51
 
        VKEY=$(dmsetup table $DEV2 --showkeys 2>/dev/null | cut -d' '  -f 5)
52
 
@@ -84,12 +91,17 @@
53
 
 crypt_key sha256    256 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
54
 
 crypt_key sha256    128 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7
55
 
 
56
 
-crypt_key ripemd160   0 stdin "xxx"   aeb26d1f69eb6dddfb9381eed4d7299f091e99aa5d3ff06866d4ce9f620f7aca
57
 
-crypt_key ripemd160 256 stdin "xxx\n" 625ce2a8dbdf08f1de400dba7ab9fab246f2a55ad6136e6cafd6703732dab8cf
58
 
+crypt_key sha256   0 std- "xxx"    cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
59
 
+crypt_key sha256 256 std- "xxx\n"  042aea10a0f14f2d391373599be69d53a75dde9951fc3d3cd10b6100aa7a9f24
60
 
+crypt_key sha256 128 std- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" \
61
 
+                                  2a42b97084779dcedf2c66405c5d296c
62
 
+crypt_key sha256 256 stdin "xxx"   cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
63
 
+crypt_key sha256   0 stdin "xxx\n" cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf6860
64
 
 
65
 
 # with keyfile, hash is ignored
66
 
 crypt_key ripemd160 256 file /dev/zero 0000000000000000000000000000000000000000000000000000000000000000
67
 
 crypt_key sha256    256 file /dev/zero 0000000000000000000000000000000000000000000000000000000000000000
68
 
+crypt_key unknown*  256 file /dev/zero 0000000000000000000000000000000000000000000000000000000000000000
69
 
 
70
 
 # limiting key
71
 
 crypt_key sha256:20 256 pwd "xxx" cd2eb0837c9b4c962c22d2ff8b5441b7b4580588000000000000000000000000
72
 
--- a/tests/api-test.c
73
 
+++ b/tests/api-test.c
74
 
@@ -27,6 +27,7 @@
75
 
 #include <assert.h>
76
 
 #include <sys/stat.h>
77
 
 #include <sys/ioctl.h>
78
 
+#include <libdevmapper.h>
79
 
 
80
 
 #include "libcryptsetup.h"
81
 
 #include "utils_loop.h"
82
 
@@ -66,18 +67,59 @@
83
 
 static char *DEVICE_2 = NULL;
84
 
 
85
 
 // Helpers
86
 
-static int _prepare_keyfile(const char *name, const char *passphrase)
87
 
+
88
 
+// Get key from kernel dm mapping table using dm-ioctl
89
 
+static int _get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
90
 
 {
91
 
+       struct dm_task *dmt;
92
 
+       struct dm_info dmi;
93
 
+       uint64_t start, length;
94
 
+       char *target_type, *rcipher, *key, *params;
95
 
+       void *next = NULL;
96
 
+       int r = -EINVAL;
97
 
+
98
 
+       if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
99
 
+               goto out;
100
 
+       if (!dm_task_set_name(dmt, name))
101
 
+               goto out;
102
 
+       if (!dm_task_run(dmt))
103
 
+               goto out;
104
 
+       if (!dm_task_get_info(dmt, &dmi))
105
 
+               goto out;
106
 
+       if (!dmi.exists)
107
 
+               goto out;
108
 
+
109
 
+       next = dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
110
 
+       if (!target_type || strcmp(target_type, "crypt") != 0)
111
 
+               goto out;
112
 
+
113
 
+       rcipher = strsep(&params, " ");
114
 
+       key = strsep(&params, " ");
115
 
+
116
 
+       if (buffer_size <= strlen(key))
117
 
+               goto out;
118
 
+
119
 
+       strncpy(buffer, key, buffer_size);
120
 
+       r = 0;
121
 
+out:
122
 
+       if (dmt)
123
 
+               dm_task_destroy(dmt);
124
 
+
125
 
+       return r;
126
 
+}
127
 
+
128
 
+static int _prepare_keyfile(const char *name, const char *passphrase, int size)
129
 
+{
130
 
        int fd, r;
131
 
 
132
 
        fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR);
133
 
        if (fd != -1) {
134
 
-               r = write(fd, passphrase, strlen(passphrase));
135
 
+               r = write(fd, passphrase, size);
136
 
                close(fd);
137
 
        } else
138
 
                r = 0;
139
 
 
140
 
-       return r == strlen(passphrase) ? 0 : 1;
141
 
+       return r == size ? 0 : 1;
142
 
 }
143
 
 
144
 
 static void _remove_keyfiles(void)
145
 
@@ -299,7 +341,7 @@
146
 
                .icb = &cmd_icb,
147
 
        };
148
 
 
149
 
-       OK_(_prepare_keyfile(KEYFILE1, KEY1));
150
 
+       OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
151
 
        co.key_file = KEYFILE1;
152
 
 
153
 
        co.device = DEVICE_EMPTY;
154
 
@@ -364,7 +406,7 @@
155
 
                .icb = &cmd_icb,
156
 
        };
157
 
 
158
 
-       OK_(_prepare_keyfile(KEYFILE1, KEY1));
159
 
+       OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
160
 
 
161
 
        co.new_key_file = KEYFILE1;
162
 
        co.device = DEVICE_ERROR;
163
 
@@ -396,8 +438,8 @@
164
 
                .icb = &cmd_icb,
165
 
        };
166
 
 
167
 
-       OK_(_prepare_keyfile(KEYFILE1, KEY1));
168
 
-       OK_(_prepare_keyfile(KEYFILE2, KEY2));
169
 
+       OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
170
 
+       OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
171
 
 
172
 
        co.new_key_file = KEYFILE1;
173
 
        co.device = DEVICE_2;
174
 
@@ -486,7 +528,7 @@
175
 
 
176
 
        orig_size = _get_device_size(DEVICE_2);
177
 
 
178
 
-       OK_(_prepare_keyfile(KEYFILE2, KEY2));
179
 
+       OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
180
 
 
181
 
        co.key_file = KEYFILE2;
182
 
        co.size = 1000;
183
 
@@ -619,7 +661,7 @@
184
 
        OK_(crypt_deactivate(cd, CDEVICE_1));
185
 
 
186
 
        // now with keyfile
187
 
-       OK_(_prepare_keyfile(KEYFILE1, KEY1));
188
 
+       OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
189
 
        FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
190
 
        EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
191
 
        EQ_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
192
 
@@ -744,7 +786,7 @@
193
 
        OK_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)));
194
 
        FAIL_(crypt_resume_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEY1, strlen(KEY1)), "not suspended");
195
 
 
196
 
-       OK_(_prepare_keyfile(KEYFILE1, KEY1));
197
 
+       OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
198
 
        OK_(crypt_suspend(cd, CDEVICE_1));
199
 
        FAIL_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1 "blah", 0), "wrong keyfile");
200
 
        OK_(crypt_resume_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0));
201
 
@@ -789,8 +831,8 @@
202
 
        OK_(crypt_deactivate(cd, CDEVICE_2));
203
 
 
204
 
        EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
205
 
-       OK_(_prepare_keyfile(KEYFILE1, KEY1));
206
 
-       OK_(_prepare_keyfile(KEYFILE2, KEY2));
207
 
+       OK_(_prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
208
 
+       OK_(_prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
209
 
        EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
210
 
        FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
211
 
        EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
212
 
@@ -899,6 +941,110 @@
213
 
        crypt_free(cd);
214
 
 }
215
 
 
216
 
+static void HashDevicePlain(void)
217
 
+{
218
 
+       struct crypt_device *cd;
219
 
+       struct crypt_params_plain params = {
220
 
+               .hash = NULL,
221
 
+               .skip = 0,
222
 
+               .offset = 0,
223
 
+       };
224
 
+
225
 
+       size_t key_size;
226
 
+       char *mk_hex, *keystr, key[256];
227
 
+
228
 
+       OK_(crypt_init(&cd, DEVICE_1));
229
 
+       OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, &params));
230
 
+
231
 
+       // hash PLAIN, short key
232
 
+       OK_(_prepare_keyfile(KEYFILE1, "tooshort", 8));
233
 
+       FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 16, 0), "not enough data in keyfile");
234
 
+       _remove_keyfiles();
235
 
+
236
 
+       // hash PLAIN, exact key
237
 
+       //         0 1 2 3 4 5 6 7 8 9 a b c d e f
238
 
+       mk_hex = "caffeecaffeecaffeecaffeecaffee88";
239
 
+       key_size = 16;
240
 
+       crypt_decode_key(key, mk_hex, key_size);
241
 
+       OK_(_prepare_keyfile(KEYFILE1, key, key_size));
242
 
+       OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
243
 
+       OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
244
 
+       OK_(strcmp(key, mk_hex));
245
 
+       OK_(crypt_deactivate(cd, CDEVICE_1));
246
 
+
247
 
+       // Limit plain key
248
 
+       mk_hex = "caffeecaffeecaffeecaffeeca000000";
249
 
+       OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size - 3, 0));
250
 
+       OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
251
 
+       OK_(strcmp(key, mk_hex));
252
 
+       OK_(crypt_deactivate(cd, CDEVICE_1));
253
 
+
254
 
+       _remove_keyfiles();
255
 
+
256
 
+       // hash PLAIN, long key
257
 
+       //         0 1 2 3 4 5 6 7 8 9 a b c d e f
258
 
+       mk_hex = "caffeecaffeecaffeecaffeecaffee88babebabe";
259
 
+       key_size = 16;
260
 
+       crypt_decode_key(key, mk_hex, key_size);
261
 
+       OK_(_prepare_keyfile(KEYFILE1, key, strlen(mk_hex) / 2));
262
 
+       OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
263
 
+       OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
264
 
+       FAIL_(strcmp(key, mk_hex), "only key length used");
265
 
+       OK_(strncmp(key, mk_hex, key_size));
266
 
+       OK_(crypt_deactivate(cd, CDEVICE_1));
267
 
+
268
 
+       // Now without explicit limit
269
 
+       OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
270
 
+       OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
271
 
+       FAIL_(strcmp(key, mk_hex), "only key length used");
272
 
+       OK_(strncmp(key, mk_hex, key_size));
273
 
+       OK_(crypt_deactivate(cd, CDEVICE_1));
274
 
+
275
 
+       _remove_keyfiles();
276
 
+
277
 
+       // hash sha256
278
 
+       params.hash = "sha256";
279
 
+       OK_(crypt_format(cd, CRYPT_PLAIN, "aes", "cbc-essiv:sha256", NULL, NULL, 16, &params));
280
 
+
281
 
+       //         0 1 2 3 4 5 6 7 8 9 a b c d e f
282
 
+       mk_hex = "c62e4615bd39e222572f3a1bf7c2132e";
283
 
+       keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
284
 
+       key_size = strlen(keystr); // 32
285
 
+       OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
286
 
+       OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
287
 
+       OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
288
 
+       OK_(strcmp(key, mk_hex));
289
 
+       OK_(crypt_deactivate(cd, CDEVICE_1));
290
 
+
291
 
+       // Read full keyfile
292
 
+       OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
293
 
+       OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
294
 
+       OK_(strcmp(key, mk_hex));
295
 
+       OK_(crypt_deactivate(cd, CDEVICE_1));
296
 
+
297
 
+       _remove_keyfiles();
298
 
+
299
 
+       // Limit keyfile read
300
 
+       keystr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAAAAAAAA";
301
 
+       OK_(_prepare_keyfile(KEYFILE1, keystr, strlen(keystr)));
302
 
+       OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, key_size, 0));
303
 
+       OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
304
 
+       OK_(strcmp(key, mk_hex));
305
 
+       OK_(crypt_deactivate(cd, CDEVICE_1));
306
 
+
307
 
+       // Full keyfile
308
 
+       OK_(crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
309
 
+       OK_(_get_key_dm(CDEVICE_1, key, sizeof(key)));
310
 
+       OK_(strcmp(key, "0e49cb34a1dee1df33f6505e4de44a66"));
311
 
+       OK_(crypt_deactivate(cd, CDEVICE_1));
312
 
+
313
 
+       _remove_keyfiles();
314
 
+
315
 
+       // FIXME: add keyfile="-" tests somehow
316
 
+
317
 
+       crypt_free(cd);
318
 
+}
319
 
+
320
 
 // Check that gcrypt is properly initialised in format
321
 
 static void NonFIPSAlg(void)
322
 
 {
323
 
@@ -962,6 +1108,7 @@
324
 
        RUN_(DeviceResizeGame, "regular crypto, resize calls");
325
 
 
326
 
        RUN_(AddDevicePlain, "plain device API creation exercise");
327
 
+       RUN_(HashDevicePlain, "plain device API hash test");
328
 
        RUN_(AddDeviceLuks, "Format and use LUKS device");
329
 
        RUN_(UseLuksDevice, "Use pre-formated LUKS device");
330
 
        RUN_(SuspendDevice, "Suspend/Resume test");
331
 
--- a/lib/setup.c
332
 
+++ b/lib/setup.c
333
 
@@ -2003,8 +2003,9 @@
334
 
                return -EINVAL;
335
 
 
336
 
        if (isPLAIN(cd->type)) {
337
 
-               r = key_from_file(cd, _("Enter passphrase: "), &passphrase_read,
338
 
-                                 &passphrase_size_read, keyfile, keyfile_size);
339
 
+               r = key_from_file(cd, _("Enter passphrase: "),
340
 
+                                 &passphrase_read, &passphrase_size_read,
341
 
+                                 keyfile, keyfile_size);
342
 
                if (r < 0)
343
 
                        goto out;
344
 
                r = create_device_helper(cd, name, cd->plain_hdr.hash,