1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * All rights reserved.
7 *
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * XXX: Future stuff
35 *
36 * Replace the template file options (-i & -f) with command-line variables
37 * "-v property=foo"
38 *
39 * Introduce -e, extra entropy source (XOR with /dev/random)
40 *
41 * Introduce -E, alternate entropy source (instead of /dev/random)
42 *
43 * Introduce -i take IV from keyboard or
44 *
45 * Introduce -I take IV from file/cmd
46 *
47 * Introduce -m/-M store encrypted+encoded masterkey in file
48 *
49 * Introduce -k/-K get pass-phrase part from file/cmd
50 *
51 * Introduce -d add more dest-devices to worklist.
52 *
53 * Add key-option: selfdestruct bit.
54 *
55 * New/changed verbs:
56 * "onetime" attach with onetime nonstored locksector
57 * "key"/"unkey" to blast memory copy of key without orphaning
58 * "nuke" blow away everything attached, crash/halt/power-off if possible.
59 * "blast" destroy all copies of the masterkey
60 * "destroy" destroy one copy of the masterkey
61 * "backup"/"restore" of masterkey sectors.
62 *
63 * Make all verbs work on both attached/detached devices.
64 *
65 */
66
67 #include <sys/types.h>
68 #include <sys/queue.h>
69 #include <sys/mutex.h>
70 #include <md5.h>
71 #include <readpassphrase.h>
72 #include <string.h>
73 #include <stdint.h>
74 #include <unistd.h>
75 #include <fcntl.h>
76 #include <paths.h>
77 #include <strings.h>
78 #include <stdlib.h>
79 #include <err.h>
80 #include <stdio.h>
81 #include <libutil.h>
82 #include <libgeom.h>
83 #include <sys/errno.h>
84 #include <sys/disk.h>
85 #include <sys/stat.h>
86 #include <crypto/rijndael/rijndael-api-fst.h>
87 #include <crypto/sha2/sha512.h>
88 #include <sys/param.h>
89 #include <sys/linker.h>
90
91 #define GBDEMOD "geom_bde"
92 #define KASSERT(foo, bar) do { if(!(foo)) { warn bar ; exit (1); } } while (0)
93
94 #include <geom/geom.h>
95 #include <geom/bde/g_bde.h>
96
97 extern const char template[];
98
99
100 #if 0
101 static void
102 g_hexdump(void *ptr, int length)
103 {
104 int i, j, k;
105 unsigned char *cp;
106
107 cp = ptr;
108 for (i = 0; i < length; i+= 16) {
109 printf("%04x ", i);
110 for (j = 0; j < 16; j++) {
111 k = i + j;
112 if (k < length)
113 printf(" %02x", cp[k]);
114 else
115 printf(" ");
116 }
117 printf(" |");
118 for (j = 0; j < 16; j++) {
119 k = i + j;
120 if (k >= length)
121 printf(" ");
122 else if (cp[k] >= ' ' && cp[k] <= '~')
123 printf("%c", cp[k]);
124 else
125 printf(".");
126 }
127 printf("|\n");
128 }
129 }
130 #endif
131
132 static void __dead2
usage(void)133 usage(void)
134 {
135
136 (void)fprintf(stderr,
137 "usage: gbde attach destination [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
138 " gbde detach destination\n"
139 " gbde init destination [-i] [-f filename] [-K new-keyfile]\n"
140 " [-L new-lockfile] [-P new-pass-phrase]\n"
141 " gbde setkey destination [-n key]\n"
142 " [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
143 " [-K new-keyfile] [-L new-lockfile] [-P new-pass-phrase]\n"
144 " gbde nuke destination [-n key]\n"
145 " [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
146 " gbde destroy destination [-k keyfile] [-l lockfile] [-p pass-phrase]\n");
147 exit(1);
148 }
149
150 void *
g_read_data(struct g_consumer * cp,off_t offset,off_t length,int * error)151 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
152 {
153 void *p;
154 int fd, i;
155 off_t o2;
156
157 p = malloc(length);
158 if (p == NULL)
159 err(1, "malloc");
160 fd = *(int *)cp;
161 o2 = lseek(fd, offset, SEEK_SET);
162 if (o2 != offset)
163 err(1, "lseek");
164 i = read(fd, p, length);
165 if (i != length)
166 err(1, "read");
167 if (error != NULL)
168 error = 0;
169 return (p);
170 }
171
172 static void
random_bits(void * p,u_int len)173 random_bits(void *p, u_int len)
174 {
175 arc4random_buf(p, len);
176 }
177
178 /* XXX: not nice */
179 static u_char sha2[SHA512_DIGEST_LENGTH];
180
181 static void
reset_passphrase(struct g_bde_softc * sc)182 reset_passphrase(struct g_bde_softc *sc)
183 {
184
185 memcpy(sc->sha2, sha2, SHA512_DIGEST_LENGTH);
186 }
187
188 static void
setup_passphrase(struct g_bde_softc * sc,int sure,const char * input,const char * keyfile)189 setup_passphrase(struct g_bde_softc *sc, int sure, const char *input,
190 const char *keyfile)
191 {
192 char buf1[BUFSIZ + SHA512_DIGEST_LENGTH];
193 char buf2[BUFSIZ + SHA512_DIGEST_LENGTH];
194 char *p;
195 int kfd, klen, bpos = 0;
196
197 if (keyfile != NULL) {
198 /* Read up to BUFSIZ bytes from keyfile */
199 kfd = open(keyfile, O_RDONLY, 0);
200 if (kfd < 0)
201 err(1, "%s", keyfile);
202 klen = read(kfd, buf1, BUFSIZ);
203 if (klen == -1)
204 err(1, "%s", keyfile);
205 close(kfd);
206
207 /* Prepend the passphrase with the hash of the key read */
208 g_bde_hash_pass(sc, buf1, klen);
209 memcpy(buf1, sc->sha2, SHA512_DIGEST_LENGTH);
210 memcpy(buf2, sc->sha2, SHA512_DIGEST_LENGTH);
211 bpos = SHA512_DIGEST_LENGTH;
212 }
213
214 if (input != NULL) {
215 if (strlen(input) >= BUFSIZ)
216 errx(1, "Passphrase too long");
217 strcpy(buf1 + bpos, input);
218
219 g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
220 memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
221 return;
222 }
223 for (;;) {
224 p = readpassphrase(
225 sure ? "Enter new passphrase:" : "Enter passphrase: ",
226 buf1 + bpos, sizeof buf1 - bpos,
227 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
228 if (p == NULL)
229 err(1, "readpassphrase");
230
231 if (sure) {
232 p = readpassphrase("Reenter new passphrase: ",
233 buf2 + bpos, sizeof buf2 - bpos,
234 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
235 if (p == NULL)
236 err(1, "readpassphrase");
237
238 if (strcmp(buf1 + bpos, buf2 + bpos)) {
239 printf("They didn't match.\n");
240 continue;
241 }
242 }
243 if (strlen(buf1 + bpos) < 3) {
244 printf("Too short passphrase.\n");
245 continue;
246 }
247 break;
248 }
249 g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
250 memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
251 }
252
253 static void
encrypt_sector(void * d,int len,int klen,void * key)254 encrypt_sector(void *d, int len, int klen, void *key)
255 {
256 keyInstance ki;
257 cipherInstance ci;
258 int error;
259
260 error = rijndael_cipherInit(&ci, MODE_CBC, NULL);
261 if (error <= 0)
262 errx(1, "rijndael_cipherInit=%d", error);
263 error = rijndael_makeKey(&ki, DIR_ENCRYPT, klen, key);
264 if (error <= 0)
265 errx(1, "rijndael_makeKeY=%d", error);
266 error = rijndael_blockEncrypt(&ci, &ki, d, len * 8, d);
267 if (error <= 0)
268 errx(1, "rijndael_blockEncrypt=%d", error);
269 }
270
271 static void
cmd_attach(const struct g_bde_softc * sc,const char * dest,const char * lfile)272 cmd_attach(const struct g_bde_softc *sc, const char *dest, const char *lfile)
273 {
274 int ffd;
275 u_char buf[16];
276 struct gctl_req *r;
277 const char *errstr;
278
279 r = gctl_get_handle();
280 gctl_ro_param(r, "verb", -1, "create geom");
281 gctl_ro_param(r, "class", -1, "BDE");
282 gctl_ro_param(r, "provider", -1, dest);
283 gctl_ro_param(r, "pass", SHA512_DIGEST_LENGTH, sc->sha2);
284 if (lfile != NULL) {
285 ffd = open(lfile, O_RDONLY, 0);
286 if (ffd < 0)
287 err(1, "%s", lfile);
288 read(ffd, buf, 16);
289 gctl_ro_param(r, "key", 16, buf);
290 close(ffd);
291 }
292 errstr = gctl_issue(r);
293 if (errstr != NULL)
294 errx(1, "Attach to %s failed: %s", dest, errstr);
295
296 exit (0);
297 }
298
299 static void
cmd_detach(const char * dest)300 cmd_detach(const char *dest)
301 {
302 struct gctl_req *r;
303 const char *errstr;
304 char buf[BUFSIZ];
305
306 r = gctl_get_handle();
307 gctl_ro_param(r, "verb", -1, "destroy geom");
308 gctl_ro_param(r, "class", -1, "BDE");
309 sprintf(buf, "%s.bde", dest);
310 gctl_ro_param(r, "geom", -1, buf);
311 /* gctl_dump(r, stdout); */
312 errstr = gctl_issue(r);
313 if (errstr != NULL)
314 errx(1, "Detach of %s failed: %s", dest, errstr);
315 exit (0);
316 }
317
318 static void
cmd_open(struct g_bde_softc * sc,int dfd,const char * l_opt,u_int * nkey)319 cmd_open(struct g_bde_softc *sc, int dfd , const char *l_opt, u_int *nkey)
320 {
321 int error;
322 int ffd;
323 u_char keyloc[16];
324 u_int sectorsize;
325 off_t mediasize;
326 struct stat st;
327
328 error = ioctl(dfd, DIOCGSECTORSIZE, §orsize);
329 if (error)
330 sectorsize = 512;
331 error = ioctl(dfd, DIOCGMEDIASIZE, &mediasize);
332 if (error) {
333 error = fstat(dfd, &st);
334 if (error == 0 && S_ISREG(st.st_mode))
335 mediasize = st.st_size;
336 else
337 error = ENOENT;
338 }
339 if (error)
340 mediasize = (off_t)-1;
341 if (l_opt != NULL) {
342 ffd = open(l_opt, O_RDONLY, 0);
343 if (ffd < 0)
344 err(1, "%s", l_opt);
345 read(ffd, keyloc, sizeof keyloc);
346 close(ffd);
347 } else {
348 memset(keyloc, 0, sizeof keyloc);
349 }
350
351 error = g_bde_decrypt_lock(sc, sc->sha2, keyloc, mediasize,
352 sectorsize, nkey);
353 if (error == ENOENT)
354 errx(1, "Lock was destroyed.");
355 if (error == ESRCH)
356 errx(1, "Lock was nuked.");
357 if (error == ENOTDIR)
358 errx(1, "Lock not found");
359 if (error != 0)
360 errx(1, "Error %d decrypting lock", error);
361 if (nkey)
362 printf("Opened with key %u\n", 1 + *nkey);
363 return;
364 }
365
366 static void
cmd_nuke(struct g_bde_key * gl,int dfd,int key)367 cmd_nuke(struct g_bde_key *gl, int dfd , int key)
368 {
369 int i;
370 u_char *sbuf;
371 off_t offset, offset2;
372
373 sbuf = malloc(gl->sectorsize);
374 memset(sbuf, 0, gl->sectorsize);
375 offset = (gl->lsector[key] & ~(gl->sectorsize - 1));
376 offset2 = lseek(dfd, offset, SEEK_SET);
377 if (offset2 != offset)
378 err(1, "lseek");
379 i = write(dfd, sbuf, gl->sectorsize);
380 free(sbuf);
381 if (i != (int)gl->sectorsize)
382 err(1, "write");
383 printf("Nuked key %d\n", 1 + key);
384 }
385
386 static void
cmd_write(struct g_bde_key * gl,struct g_bde_softc * sc,int dfd,int key,const char * l_opt)387 cmd_write(struct g_bde_key *gl, struct g_bde_softc *sc, int dfd , int key, const char *l_opt)
388 {
389 int i, ffd;
390 uint64_t off[2];
391 u_char keyloc[16];
392 u_char *sbuf, *q;
393 off_t offset, offset2;
394
395 sbuf = malloc(gl->sectorsize);
396 /*
397 * Find the byte-offset in the lock sector where we will put the lock
398 * data structure. We can put it any random place as long as the
399 * structure fits.
400 */
401 for(;;) {
402 random_bits(off, sizeof off);
403 off[0] &= (gl->sectorsize - 1);
404 if (off[0] + G_BDE_LOCKSIZE > gl->sectorsize)
405 continue;
406 break;
407 }
408
409 /* Add the sector offset in bytes */
410 off[0] += (gl->lsector[key] & ~(gl->sectorsize - 1));
411 gl->lsector[key] = off[0];
412
413 i = g_bde_keyloc_encrypt(sc->sha2, off[0], off[1], keyloc);
414 if (i)
415 errx(1, "g_bde_keyloc_encrypt()");
416 if (l_opt != NULL) {
417 ffd = open(l_opt, O_WRONLY | O_CREAT | O_TRUNC, 0600);
418 if (ffd < 0)
419 err(1, "%s", l_opt);
420 write(ffd, keyloc, sizeof keyloc);
421 close(ffd);
422 } else if (gl->flags & GBDE_F_SECT0) {
423 offset2 = lseek(dfd, 0, SEEK_SET);
424 if (offset2 != 0)
425 err(1, "lseek");
426 i = read(dfd, sbuf, gl->sectorsize);
427 if (i != (int)gl->sectorsize)
428 err(1, "read");
429 memcpy(sbuf + key * 16, keyloc, sizeof keyloc);
430 offset2 = lseek(dfd, 0, SEEK_SET);
431 if (offset2 != 0)
432 err(1, "lseek");
433 i = write(dfd, sbuf, gl->sectorsize);
434 if (i != (int)gl->sectorsize)
435 err(1, "write");
436 } else {
437 errx(1, "No -L option and no space in sector 0 for lockfile");
438 }
439
440 /* Allocate a sectorbuffer and fill it with random junk */
441 if (sbuf == NULL)
442 err(1, "malloc");
443 random_bits(sbuf, gl->sectorsize);
444
445 /* Fill random bits in the spare field */
446 random_bits(gl->spare, sizeof(gl->spare));
447
448 /* Encode the structure where we want it */
449 q = sbuf + (off[0] % gl->sectorsize);
450 i = g_bde_encode_lock(sc->sha2, gl, q);
451 if (i < 0)
452 errx(1, "programming error encoding lock");
453
454 encrypt_sector(q, G_BDE_LOCKSIZE, 256, sc->sha2 + 16);
455 offset = gl->lsector[key] & ~(gl->sectorsize - 1);
456 offset2 = lseek(dfd, offset, SEEK_SET);
457 if (offset2 != offset)
458 err(1, "lseek");
459 i = write(dfd, sbuf, gl->sectorsize);
460 if (i != (int)gl->sectorsize)
461 err(1, "write");
462 free(sbuf);
463 #if 0
464 printf("Wrote key %d at %jd\n", key, (intmax_t)offset);
465 printf("s0 = %jd\n", (intmax_t)gl->sector0);
466 printf("sN = %jd\n", (intmax_t)gl->sectorN);
467 printf("l[0] = %jd\n", (intmax_t)gl->lsector[0]);
468 printf("l[1] = %jd\n", (intmax_t)gl->lsector[1]);
469 printf("l[2] = %jd\n", (intmax_t)gl->lsector[2]);
470 printf("l[3] = %jd\n", (intmax_t)gl->lsector[3]);
471 printf("k = %jd\n", (intmax_t)gl->keyoffset);
472 printf("ss = %jd\n", (intmax_t)gl->sectorsize);
473 #endif
474 }
475
476 static void
cmd_destroy(struct g_bde_key * gl,int nkey)477 cmd_destroy(struct g_bde_key *gl, int nkey)
478 {
479 int i;
480
481 bzero(&gl->sector0, sizeof gl->sector0);
482 bzero(&gl->sectorN, sizeof gl->sectorN);
483 bzero(&gl->keyoffset, sizeof gl->keyoffset);
484 gl->flags &= GBDE_F_SECT0;
485 bzero(gl->mkey, sizeof gl->mkey);
486 for (i = 0; i < G_BDE_MAXKEYS; i++)
487 if (i != nkey)
488 gl->lsector[i] = ~0;
489 }
490
491 static int
sorthelp(const void * a,const void * b)492 sorthelp(const void *a, const void *b)
493 {
494 const uint64_t *oa, *ob;
495
496 oa = a;
497 ob = b;
498 if (*oa > *ob)
499 return 1;
500 if (*oa < *ob)
501 return -1;
502 return 0;
503 }
504
505 static void
cmd_init(struct g_bde_key * gl,int dfd,const char * f_opt,int i_opt,const char * l_opt)506 cmd_init(struct g_bde_key *gl, int dfd, const char *f_opt, int i_opt, const char *l_opt)
507 {
508 int i;
509 u_char *buf;
510 unsigned sector_size;
511 uint64_t first_sector;
512 uint64_t last_sector;
513 uint64_t total_sectors;
514 off_t off, off2;
515 unsigned nkeys;
516 const char *p;
517 char *q, cbuf[BUFSIZ];
518 unsigned u, u2;
519 uint64_t o;
520 properties params;
521
522 bzero(gl, sizeof *gl);
523 if (f_opt != NULL) {
524 i = open(f_opt, O_RDONLY);
525 if (i < 0)
526 err(1, "%s", f_opt);
527 params = properties_read(i);
528 close (i);
529 } else if (i_opt) {
530 /* XXX: Polish */
531 asprintf(&q, "%stemp.XXXXXXXXXX", _PATH_TMP);
532 if (q == NULL)
533 err(1, "asprintf");
534 i = mkstemp(q);
535 if (i < 0)
536 err(1, "%s", q);
537 write(i, template, strlen(template));
538 close (i);
539 p = getenv("EDITOR");
540 if (p == NULL)
541 p = "vi";
542 if (snprintf(cbuf, sizeof(cbuf), "%s %s\n", p, q) >=
543 (ssize_t)sizeof(cbuf)) {
544 unlink(q);
545 errx(1, "EDITOR is too long");
546 }
547 system(cbuf);
548 i = open(q, O_RDONLY);
549 if (i < 0)
550 err(1, "%s", f_opt);
551 params = properties_read(i);
552 close (i);
553 unlink(q);
554 free(q);
555 } else {
556 /* XXX: Hack */
557 i = open(_PATH_DEVNULL, O_RDONLY);
558 if (i < 0)
559 err(1, "%s", _PATH_DEVNULL);
560 params = properties_read(i);
561 close (i);
562 }
563
564 /* <sector_size> */
565 p = property_find(params, "sector_size");
566 i = ioctl(dfd, DIOCGSECTORSIZE, &u);
567 if (p != NULL) {
568 sector_size = strtoul(p, &q, 0);
569 if (!*p || *q)
570 errx(1, "sector_size not a proper number");
571 } else if (i == 0) {
572 sector_size = u;
573 } else {
574 errx(1, "Missing sector_size property");
575 }
576 if (sector_size & (sector_size - 1))
577 errx(1, "sector_size not a power of 2");
578 if (sector_size < 512)
579 errx(1, "sector_size is smaller than 512");
580 buf = malloc(sector_size);
581 if (buf == NULL)
582 err(1, "Failed to malloc sector buffer");
583 gl->sectorsize = sector_size;
584
585 i = ioctl(dfd, DIOCGMEDIASIZE, &off);
586 if (i == 0) {
587 first_sector = 0;
588 total_sectors = off / sector_size;
589 last_sector = total_sectors - 1;
590 } else {
591 first_sector = 0;
592 last_sector = 0;
593 total_sectors = 0;
594 }
595
596 /* <first_sector> */
597 p = property_find(params, "first_sector");
598 if (p != NULL) {
599 first_sector = strtoul(p, &q, 0);
600 if (!*p || *q)
601 errx(1, "first_sector not a proper number");
602 }
603
604 /* <last_sector> */
605 p = property_find(params, "last_sector");
606 if (p != NULL) {
607 last_sector = strtoul(p, &q, 0);
608 if (!*p || *q)
609 errx(1, "last_sector not a proper number");
610 if (last_sector <= first_sector)
611 errx(1, "last_sector not larger than first_sector");
612 total_sectors = last_sector + 1;
613 }
614
615 /* <total_sectors> */
616 p = property_find(params, "total_sectors");
617 if (p != NULL) {
618 total_sectors = strtoul(p, &q, 0);
619 if (!*p || *q)
620 errx(1, "total_sectors not a proper number");
621 if (last_sector == 0)
622 last_sector = first_sector + total_sectors - 1;
623 }
624
625 if (l_opt == NULL && first_sector != 0)
626 errx(1, "No -L new-lockfile argument and first_sector != 0");
627 else if (l_opt == NULL) {
628 first_sector++;
629 total_sectors--;
630 gl->flags |= GBDE_F_SECT0;
631 }
632 gl->sector0 = first_sector * gl->sectorsize;
633
634 if (total_sectors != (last_sector - first_sector) + 1)
635 errx(1, "total_sectors disagree with first_sector and last_sector");
636 if (total_sectors == 0)
637 errx(1, "missing last_sector or total_sectors");
638
639 gl->sectorN = (last_sector + 1) * gl->sectorsize;
640
641 /* Find a random keyoffset */
642 random_bits(&o, sizeof o);
643 o %= (gl->sectorN - gl->sector0);
644 o &= ~(gl->sectorsize - 1);
645 gl->keyoffset = o;
646
647 /* <number_of_keys> */
648 p = property_find(params, "number_of_keys");
649 if (p != NULL) {
650 nkeys = strtoul(p, &q, 0);
651 if (!*p || *q)
652 errx(1, "number_of_keys not a proper number");
653 if (nkeys < 1 || nkeys > G_BDE_MAXKEYS)
654 errx(1, "number_of_keys out of range");
655 } else {
656 nkeys = 4;
657 }
658 for (u = 0; u < nkeys; u++) {
659 for(;;) {
660 do {
661 random_bits(&o, sizeof o);
662 o %= gl->sectorN;
663 o &= ~(gl->sectorsize - 1);
664 } while(o < gl->sector0);
665 for (u2 = 0; u2 < u; u2++)
666 if (o == gl->lsector[u2])
667 break;
668 if (u2 < u)
669 continue;
670 break;
671 }
672 gl->lsector[u] = o;
673 }
674 for (; u < G_BDE_MAXKEYS; u++) {
675 do
676 random_bits(&o, sizeof o);
677 while (o < gl->sectorN);
678 gl->lsector[u] = o;
679 }
680 qsort(gl->lsector, G_BDE_MAXKEYS, sizeof gl->lsector[0], sorthelp);
681
682 /* Flush sector zero if we use it for lockfile data */
683 if (gl->flags & GBDE_F_SECT0) {
684 off2 = lseek(dfd, 0, SEEK_SET);
685 if (off2 != 0)
686 err(1, "lseek(2) to sector 0");
687 random_bits(buf, sector_size);
688 i = write(dfd, buf, sector_size);
689 if (i != (int)sector_size)
690 err(1, "write sector 0");
691 }
692
693 /* <random_flush> */
694 p = property_find(params, "random_flush");
695 if (p != NULL) {
696 off = first_sector * sector_size;
697 off2 = lseek(dfd, off, SEEK_SET);
698 if (off2 != off)
699 err(1, "lseek(2) to first_sector");
700 off2 = last_sector * sector_size;
701 while (off <= off2) {
702 random_bits(buf, sector_size);
703 i = write(dfd, buf, sector_size);
704 if (i != (int)sector_size)
705 err(1, "write to $device_name");
706 off += sector_size;
707 }
708 }
709
710 random_bits(gl->mkey, sizeof gl->mkey);
711 random_bits(gl->salt, sizeof gl->salt);
712
713 return;
714 }
715
716 static enum action {
717 ACT_HUH,
718 ACT_ATTACH, ACT_DETACH,
719 ACT_INIT, ACT_SETKEY, ACT_DESTROY, ACT_NUKE
720 } action;
721
722 int
main(int argc,char ** argv)723 main(int argc, char **argv)
724 {
725 const char *opts;
726 const char *k_opt, *K_opt;
727 const char *l_opt, *L_opt;
728 const char *p_opt, *P_opt;
729 const char *f_opt;
730 char *dest;
731 int i_opt, n_opt, ch, dfd, doopen;
732 u_int nkey;
733 int i;
734 char *q, buf[BUFSIZ];
735 struct g_bde_key *gl;
736 struct g_bde_softc sc;
737
738 fprintf(stderr, "GBDE disk-encryption is deprecated,\n");
739 fprintf(stderr, "and will be removed in FreeBSD 15.0\n");
740 fprintf(stderr, "(continuing in 5 seconds)\n");
741 sleep(5);
742
743 if (argc < 3)
744 usage();
745
746 if (modfind("g_bde") < 0) {
747 /* need to load the gbde module */
748 if (kldload(GBDEMOD) < 0 || modfind("g_bde") < 0)
749 err(1, GBDEMOD ": Kernel module not available");
750 }
751 doopen = 0;
752 if (!strcmp(argv[1], "attach")) {
753 action = ACT_ATTACH;
754 opts = "k:l:p:";
755 } else if (!strcmp(argv[1], "detach")) {
756 action = ACT_DETACH;
757 opts = "";
758 } else if (!strcmp(argv[1], "init")) {
759 action = ACT_INIT;
760 doopen = 1;
761 opts = "f:iK:L:P:";
762 } else if (!strcmp(argv[1], "setkey")) {
763 action = ACT_SETKEY;
764 doopen = 1;
765 opts = "k:K:l:L:n:p:P:";
766 } else if (!strcmp(argv[1], "destroy")) {
767 action = ACT_DESTROY;
768 doopen = 1;
769 opts = "k:l:p:";
770 } else if (!strcmp(argv[1], "nuke")) {
771 action = ACT_NUKE;
772 doopen = 1;
773 opts = "k:l:n:p:";
774 } else {
775 usage();
776 }
777 argc--;
778 argv++;
779
780 dest = strdup(argv[1]);
781 argc--;
782 argv++;
783
784 p_opt = NULL;
785 P_opt = NULL;
786 k_opt = NULL;
787 K_opt = NULL;
788 l_opt = NULL;
789 L_opt = NULL;
790 f_opt = NULL;
791 n_opt = 0;
792 i_opt = 0;
793
794 while((ch = getopt(argc, argv, opts)) != -1)
795 switch (ch) {
796 case 'f':
797 f_opt = optarg;
798 break;
799 case 'i':
800 i_opt = !i_opt;
801 break;
802 case 'k':
803 k_opt = optarg;
804 break;
805 case 'K':
806 K_opt = optarg;
807 break;
808 case 'l':
809 l_opt = optarg;
810 break;
811 case 'L':
812 L_opt = optarg;
813 break;
814 case 'n':
815 n_opt = strtoul(optarg, &q, 0);
816 if (!*optarg || *q)
817 errx(1, "-n argument not numeric");
818 if (n_opt < -1 || n_opt > G_BDE_MAXKEYS)
819 errx(1, "-n argument out of range");
820 break;
821 case 'p':
822 p_opt = optarg;
823 break;
824 case 'P':
825 P_opt = optarg;
826 break;
827 default:
828 usage();
829 }
830
831 if (doopen) {
832 dfd = open(dest, O_RDWR);
833 if (dfd < 0 && dest[0] != '/') {
834 if (snprintf(buf, sizeof(buf), "%s%s",
835 _PATH_DEV, dest) >= (ssize_t)sizeof(buf))
836 errno = ENAMETOOLONG;
837 else
838 dfd = open(buf, O_RDWR);
839 }
840 if (dfd < 0)
841 err(1, "%s", dest);
842 } else {
843 if (!memcmp(dest, _PATH_DEV, strlen(_PATH_DEV)))
844 strcpy(dest, dest + strlen(_PATH_DEV));
845 }
846
847 memset(&sc, 0, sizeof sc);
848 sc.consumer = (void *)&dfd;
849 gl = &sc.key;
850 switch(action) {
851 case ACT_ATTACH:
852 setup_passphrase(&sc, 0, p_opt, k_opt);
853 cmd_attach(&sc, dest, l_opt);
854 break;
855 case ACT_DETACH:
856 cmd_detach(dest);
857 break;
858 case ACT_INIT:
859 cmd_init(gl, dfd, f_opt, i_opt, L_opt);
860 setup_passphrase(&sc, 1, P_opt, K_opt);
861 cmd_write(gl, &sc, dfd, 0, L_opt);
862 break;
863 case ACT_SETKEY:
864 setup_passphrase(&sc, 0, p_opt, k_opt);
865 cmd_open(&sc, dfd, l_opt, &nkey);
866 if (n_opt == 0)
867 n_opt = nkey + 1;
868 setup_passphrase(&sc, 1, P_opt, K_opt);
869 cmd_write(gl, &sc, dfd, n_opt - 1, L_opt);
870 break;
871 case ACT_DESTROY:
872 setup_passphrase(&sc, 0, p_opt, k_opt);
873 cmd_open(&sc, dfd, l_opt, &nkey);
874 cmd_destroy(gl, nkey);
875 reset_passphrase(&sc);
876 cmd_write(gl, &sc, dfd, nkey, l_opt);
877 break;
878 case ACT_NUKE:
879 setup_passphrase(&sc, 0, p_opt, k_opt);
880 cmd_open(&sc, dfd, l_opt, &nkey);
881 if (n_opt == 0)
882 n_opt = nkey + 1;
883 if (n_opt == -1) {
884 for(i = 0; i < G_BDE_MAXKEYS; i++)
885 cmd_nuke(gl, dfd, i);
886 } else {
887 cmd_nuke(gl, dfd, n_opt - 1);
888 }
889 break;
890 default:
891 errx(1, "internal error");
892 }
893
894 return(0);
895 }
896