2 * Copyright (c) 2004-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/sysctl.h>
39 #include <readpassphrase.h>
47 #include <sys/param.h>
49 #include <sys/resource.h>
50 #include <opencrypto/cryptodev.h>
51 #include <geom/eli/g_eli.h>
52 #include <geom/eli/pkcs5v2.h>
54 #include "core/geom.h"
55 #include "misc/subr.h"
58 uint32_t lib_version = G_LIB_VERSION;
59 uint32_t version = G_ELI_VERSION;
61 #define GELI_BACKUP_DIR "/var/backups/"
62 #define GELI_ENC_ALGO "aes"
64 static void eli_main(struct gctl_req *req, unsigned flags);
65 static void eli_init(struct gctl_req *req);
66 static void eli_attach(struct gctl_req *req);
67 static void eli_configure(struct gctl_req *req);
68 static void eli_setkey(struct gctl_req *req);
69 static void eli_delkey(struct gctl_req *req);
70 static void eli_resume(struct gctl_req *req);
71 static void eli_kill(struct gctl_req *req);
72 static void eli_backup(struct gctl_req *req);
73 static void eli_restore(struct gctl_req *req);
74 static void eli_resize(struct gctl_req *req);
75 static void eli_clear(struct gctl_req *req);
76 static void eli_dump(struct gctl_req *req);
78 static int eli_backup_create(struct gctl_req *req, const char *prov,
84 * init [-bhPv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] prov
85 * label - alias for 'init'
86 * attach [-dprv] [-j passfile] [-k keyfile] prov
87 * detach [-fl] prov ...
88 * stop - alias for 'detach'
89 * onetime [-d] [-a aalgo] [-e ealgo] [-l keylen] prov
90 * configure [-bB] prov ...
91 * setkey [-pPv] [-n keyno] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov
92 * delkey [-afv] [-n keyno] prov
93 * suspend [-v] -a | prov ...
94 * resume [-pv] [-j passfile] [-k keyfile] prov
95 * kill [-av] [prov ...]
96 * backup [-v] prov file
97 * restore [-fv] file prov
98 * resize [-v] -s oldsize prov
102 struct g_command class_commands[] = {
103 { "init", G_FLAG_VERBOSE, eli_main,
105 { 'a', "aalgo", "", G_TYPE_STRING },
106 { 'b', "boot", NULL, G_TYPE_BOOL },
107 { 'B', "backupfile", "", G_TYPE_STRING },
108 { 'e', "ealgo", GELI_ENC_ALGO, G_TYPE_STRING },
109 { 'i', "iterations", "-1", G_TYPE_NUMBER },
110 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
111 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
112 { 'l', "keylen", "0", G_TYPE_NUMBER },
113 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
114 { 's', "sectorsize", "0", G_TYPE_NUMBER },
117 "[-bPv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] prov"
119 { "label", G_FLAG_VERBOSE, eli_main,
121 { 'a', "aalgo", "", G_TYPE_STRING },
122 { 'b', "boot", NULL, G_TYPE_BOOL },
123 { 'B', "backupfile", "", G_TYPE_STRING },
124 { 'e', "ealgo", GELI_ENC_ALGO, G_TYPE_STRING },
125 { 'i', "iterations", "-1", G_TYPE_NUMBER },
126 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
127 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
128 { 'l', "keylen", "0", G_TYPE_NUMBER },
129 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
130 { 's', "sectorsize", "0", G_TYPE_NUMBER },
133 "- an alias for 'init'"
135 { "attach", G_FLAG_VERBOSE | G_FLAG_LOADKLD, eli_main,
137 { 'd', "detach", NULL, G_TYPE_BOOL },
138 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
139 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
140 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
141 { 'r', "readonly", NULL, G_TYPE_BOOL },
144 "[-dprv] [-j passfile] [-k keyfile] prov"
148 { 'f', "force", NULL, G_TYPE_BOOL },
149 { 'l', "last", NULL, G_TYPE_BOOL },
156 { 'f', "force", NULL, G_TYPE_BOOL },
157 { 'l', "last", NULL, G_TYPE_BOOL },
160 "- an alias for 'detach'"
162 { "onetime", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
164 { 'a', "aalgo", "", G_TYPE_STRING },
165 { 'd', "detach", NULL, G_TYPE_BOOL },
166 { 'e', "ealgo", GELI_ENC_ALGO, G_TYPE_STRING },
167 { 'l', "keylen", "0", G_TYPE_NUMBER },
168 { 's', "sectorsize", "0", G_TYPE_NUMBER },
171 "[-d] [-a aalgo] [-e ealgo] [-l keylen] [-s sectorsize] prov"
173 { "configure", G_FLAG_VERBOSE, eli_main,
175 { 'b', "boot", NULL, G_TYPE_BOOL },
176 { 'B', "noboot", NULL, G_TYPE_BOOL },
181 { "setkey", G_FLAG_VERBOSE, eli_main,
183 { 'i', "iterations", "-1", G_TYPE_NUMBER },
184 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
185 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
186 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
187 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
188 { 'n', "keyno", "-1", G_TYPE_NUMBER },
189 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
190 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
193 "[-pPv] [-n keyno] [-i iterations] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov"
195 { "delkey", G_FLAG_VERBOSE, eli_main,
197 { 'a', "all", NULL, G_TYPE_BOOL },
198 { 'f', "force", NULL, G_TYPE_BOOL },
199 { 'n', "keyno", "-1", G_TYPE_NUMBER },
202 "[-afv] [-n keyno] prov"
204 { "suspend", G_FLAG_VERBOSE, NULL,
206 { 'a', "all", NULL, G_TYPE_BOOL },
211 { "resume", G_FLAG_VERBOSE, eli_main,
213 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
214 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
215 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
218 "[-pv] [-j passfile] [-k keyfile] prov"
220 { "kill", G_FLAG_VERBOSE, eli_main,
222 { 'a', "all", NULL, G_TYPE_BOOL },
227 { "backup", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
230 { "restore", G_FLAG_VERBOSE, eli_main,
232 { 'f', "force", NULL, G_TYPE_BOOL },
237 { "resize", G_FLAG_VERBOSE, eli_main,
239 { 's', "oldsize", NULL, G_TYPE_NUMBER },
242 "[-v] -s oldsize prov"
244 { "clear", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
247 { "dump", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
253 static int verbose = 0;
256 eli_protect(struct gctl_req *req)
260 /* Disable core dumps. */
263 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
264 gctl_error(req, "Cannot disable core dumps: %s.",
268 /* Disable swapping. */
269 if (mlockall(MCL_FUTURE) == -1) {
270 gctl_error(req, "Cannot lock memory: %s.", strerror(errno));
277 eli_main(struct gctl_req *req, unsigned int flags)
281 if (eli_protect(req) == -1)
284 if ((flags & G_FLAG_VERBOSE) != 0)
287 name = gctl_get_ascii(req, "verb");
289 gctl_error(req, "No '%s' argument.", "verb");
292 if (strcmp(name, "init") == 0 || strcmp(name, "label") == 0)
294 else if (strcmp(name, "attach") == 0)
296 else if (strcmp(name, "configure") == 0)
298 else if (strcmp(name, "setkey") == 0)
300 else if (strcmp(name, "delkey") == 0)
302 else if (strcmp(name, "resume") == 0)
304 else if (strcmp(name, "kill") == 0)
306 else if (strcmp(name, "backup") == 0)
308 else if (strcmp(name, "restore") == 0)
310 else if (strcmp(name, "resize") == 0)
312 else if (strcmp(name, "dump") == 0)
314 else if (strcmp(name, "clear") == 0)
317 gctl_error(req, "Unknown command: %s.", name);
321 arc4rand(unsigned char *buf, size_t size)
327 buf4 = (uint32_t *)buf;
330 for (i = 0; i < size4; i++)
331 buf4[i] = arc4random();
332 for (i *= 4; i < size; i++)
333 buf[i] = arc4random() % 0xff;
337 eli_is_attached(const char *prov)
339 char name[MAXPATHLEN];
343 * Not the best way to do it, but the easiest.
344 * We try to open provider and check if it is a GEOM provider
345 * by asking about its sectorsize.
347 snprintf(name, sizeof(name), "%s%s", prov, G_ELI_SUFFIX);
348 secsize = g_get_sectorsize(name);
355 eli_genkey_files(struct gctl_req *req, bool new, const char *type,
356 struct hmac_ctx *ctxp, char *passbuf, size_t passbufsize)
358 char *p, buf[MAXPHYS], argname[16];
363 assert((strcmp(type, "keyfile") == 0 && ctxp != NULL &&
364 passbuf == NULL && passbufsize == 0) ||
365 (strcmp(type, "passfile") == 0 && ctxp == NULL &&
366 passbuf != NULL && passbufsize > 0));
367 assert(strcmp(type, "keyfile") == 0 || passbuf[0] == '\0');
370 snprintf(argname, sizeof(argname), "%s%s%d",
371 new ? "new" : "", type, i);
373 /* No more {key,pass}files? */
374 if (!gctl_has_param(req, argname))
377 file = gctl_get_ascii(req, "%s", argname);
378 assert(file != NULL);
380 if (strcmp(file, "-") == 0)
383 fd = open(file, O_RDONLY);
385 gctl_error(req, "Cannot open %s %s: %s.",
386 type, file, strerror(errno));
390 if (strcmp(type, "keyfile") == 0) {
391 while ((done = read(fd, buf, sizeof(buf))) > 0)
392 g_eli_crypto_hmac_update(ctxp, buf, done);
393 } else /* if (strcmp(type, "passfile") == 0) */ {
394 while ((done = read(fd, buf, sizeof(buf) - 1)) > 0) {
396 p = strchr(buf, '\n');
401 if (strlcat(passbuf, buf, passbufsize) >=
404 "Passphrase in %s too long.", file);
405 bzero(buf, sizeof(buf));
413 if (strcmp(file, "-") != 0)
415 bzero(buf, sizeof(buf));
417 gctl_error(req, "Cannot read %s %s: %s.",
418 type, file, strerror(error));
426 eli_genkey_passphrase_prompt(struct gctl_req *req, bool new, char *passbuf,
433 new ? "Enter new passphrase:" : "Enter passphrase:",
434 passbuf, passbufsize, RPP_ECHO_OFF | RPP_REQUIRE_TTY);
436 bzero(passbuf, passbufsize);
437 gctl_error(req, "Cannot read passphrase: %s.",
445 p = readpassphrase("Reenter new passphrase: ",
446 tmpbuf, sizeof(tmpbuf),
447 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
449 bzero(passbuf, passbufsize);
451 "Cannot read passphrase: %s.",
456 if (strcmp(passbuf, tmpbuf) != 0) {
457 bzero(passbuf, passbufsize);
458 fprintf(stderr, "They didn't match.\n");
461 bzero(tmpbuf, sizeof(tmpbuf));
469 eli_genkey_passphrase(struct gctl_req *req, struct g_eli_metadata *md, bool new,
470 struct hmac_ctx *ctxp)
472 char passbuf[MAXPHYS];
477 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
479 if (gctl_has_param(req, new ? "newpassfile0" : "passfile0")) {
481 "Options -%c and -%c are mutually exclusive.",
482 new ? 'J' : 'j', new ? 'P' : 'p');
488 if (!new && md->md_iterations == -1) {
489 gctl_error(req, "Missing -p flag.");
493 nfiles = eli_genkey_files(req, new, "passfile", NULL, passbuf,
497 else if (nfiles == 0) {
498 if (eli_genkey_passphrase_prompt(req, new, passbuf,
499 sizeof(passbuf)) == -1) {
504 * Field md_iterations equal to -1 means "choose some sane
507 if (md->md_iterations == -1) {
510 printf("Calculating number of iterations...\n");
511 md->md_iterations = pkcs5v2_calculate(2000000);
512 assert(md->md_iterations > 0);
514 printf("Done, using %d iterations.\n",
519 * If md_iterations is equal to 0, user doesn't want PKCS#5v2.
521 if (md->md_iterations == 0) {
522 g_eli_crypto_hmac_update(ctxp, md->md_salt,
523 sizeof(md->md_salt));
524 g_eli_crypto_hmac_update(ctxp, passbuf, strlen(passbuf));
525 } else /* if (md->md_iterations > 0) */ {
526 unsigned char dkey[G_ELI_USERKEYLEN];
528 pkcs5v2_genkey(dkey, sizeof(dkey), md->md_salt,
529 sizeof(md->md_salt), passbuf, md->md_iterations);
530 g_eli_crypto_hmac_update(ctxp, dkey, sizeof(dkey));
531 bzero(dkey, sizeof(dkey));
533 bzero(passbuf, sizeof(passbuf));
538 static unsigned char *
539 eli_genkey(struct gctl_req *req, struct g_eli_metadata *md, unsigned char *key,
547 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
549 g_eli_crypto_hmac_init(&ctx, NULL, 0);
551 nfiles = eli_genkey_files(req, new, "keyfile", &ctx, NULL, 0);
554 else if (nfiles == 0 && nopassphrase) {
555 gctl_error(req, "No key components given.");
559 if (eli_genkey_passphrase(req, md, new, &ctx) == -1)
562 g_eli_crypto_hmac_final(&ctx, key, 0);
568 eli_metadata_read(struct gctl_req *req, const char *prov,
569 struct g_eli_metadata *md)
571 unsigned char sector[sizeof(struct g_eli_metadata)];
574 if (g_get_sectorsize(prov) == 0) {
577 /* This is a file probably. */
578 fd = open(prov, O_RDONLY);
580 gctl_error(req, "Cannot open %s: %s.", prov,
584 if (read(fd, sector, sizeof(sector)) != sizeof(sector)) {
585 gctl_error(req, "Cannot read metadata from %s: %s.",
586 prov, strerror(errno));
592 /* This is a GEOM provider. */
593 error = g_metadata_read(prov, sector, sizeof(sector),
596 gctl_error(req, "Cannot read metadata from %s: %s.",
597 prov, strerror(error));
601 if (eli_metadata_decode(sector, md) != 0) {
602 gctl_error(req, "MD5 hash mismatch for %s.", prov);
609 eli_metadata_store(struct gctl_req *req, const char *prov,
610 struct g_eli_metadata *md)
612 unsigned char sector[sizeof(struct g_eli_metadata)];
615 eli_metadata_encode(md, sector);
616 if (g_get_sectorsize(prov) == 0) {
619 /* This is a file probably. */
620 fd = open(prov, O_WRONLY | O_TRUNC);
622 gctl_error(req, "Cannot open %s: %s.", prov,
624 bzero(sector, sizeof(sector));
627 if (write(fd, sector, sizeof(sector)) != sizeof(sector)) {
628 gctl_error(req, "Cannot write metadata to %s: %s.",
629 prov, strerror(errno));
630 bzero(sector, sizeof(sector));
636 /* This is a GEOM provider. */
637 error = g_metadata_store(prov, sector, sizeof(sector));
639 gctl_error(req, "Cannot write metadata to %s: %s.",
640 prov, strerror(errno));
641 bzero(sector, sizeof(sector));
645 bzero(sector, sizeof(sector));
650 eli_init(struct gctl_req *req)
652 struct g_eli_metadata md;
653 unsigned char sector[sizeof(struct g_eli_metadata)];
654 unsigned char key[G_ELI_USERKEYLEN];
655 char backfile[MAXPATHLEN];
656 const char *str, *prov;
662 nargs = gctl_get_int(req, "nargs");
664 gctl_error(req, "Invalid number of arguments.");
667 prov = gctl_get_ascii(req, "arg0");
668 mediasize = g_get_mediasize(prov);
669 secsize = g_get_sectorsize(prov);
670 if (mediasize == 0 || secsize == 0) {
671 gctl_error(req, "Cannot get informations about %s: %s.", prov,
676 bzero(&md, sizeof(md));
677 strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic));
678 md.md_version = G_ELI_VERSION;
680 if (gctl_get_int(req, "boot"))
681 md.md_flags |= G_ELI_FLAG_BOOT;
682 md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1;
683 str = gctl_get_ascii(req, "aalgo");
685 md.md_aalgo = g_eli_str2aalgo(str);
686 if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN &&
687 md.md_aalgo <= CRYPTO_ALGORITHM_MAX) {
688 md.md_flags |= G_ELI_FLAG_AUTH;
691 * For backward compatibility, check if the -a option
692 * was used to provide encryption algorithm.
694 md.md_ealgo = g_eli_str2ealgo(str);
695 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
696 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
698 "Invalid authentication algorithm.");
701 fprintf(stderr, "warning: The -e option, not "
702 "the -a option is now used to specify "
703 "encryption algorithm to use.\n");
707 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
708 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
709 str = gctl_get_ascii(req, "ealgo");
710 md.md_ealgo = g_eli_str2ealgo(str);
711 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
712 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
713 gctl_error(req, "Invalid encryption algorithm.");
717 val = gctl_get_intmax(req, "keylen");
719 md.md_keylen = g_eli_keylen(md.md_ealgo, md.md_keylen);
720 if (md.md_keylen == 0) {
721 gctl_error(req, "Invalid key length.");
724 md.md_provsize = mediasize;
726 val = gctl_get_intmax(req, "iterations");
731 * Don't allow to set iterations when there will be no
734 nonewpassphrase = gctl_get_int(req, "nonewpassphrase");
735 if (nonewpassphrase) {
737 "Options -i and -P are mutually exclusive.");
741 md.md_iterations = val;
743 val = gctl_get_intmax(req, "sectorsize");
745 md.md_sectorsize = secsize;
747 if (val < 0 || (val % secsize) != 0) {
748 gctl_error(req, "Invalid sector size.");
751 if (val > sysconf(_SC_PAGE_SIZE)) {
753 "warning: Using sectorsize bigger than the page size!\n");
755 md.md_sectorsize = val;
759 arc4rand(md.md_salt, sizeof(md.md_salt));
760 arc4rand(md.md_mkeys, sizeof(md.md_mkeys));
762 /* Generate user key. */
763 if (eli_genkey(req, &md, key, true) == NULL) {
764 bzero(key, sizeof(key));
765 bzero(&md, sizeof(md));
769 /* Encrypt the first and the only Master Key. */
770 error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen, md.md_mkeys);
771 bzero(key, sizeof(key));
773 bzero(&md, sizeof(md));
774 gctl_error(req, "Cannot encrypt Master Key: %s.",
779 eli_metadata_encode(&md, sector);
780 bzero(&md, sizeof(md));
781 error = g_metadata_store(prov, sector, sizeof(sector));
782 bzero(sector, sizeof(sector));
784 gctl_error(req, "Cannot store metadata on %s: %s.", prov,
789 printf("Metadata value stored on %s.\n", prov);
790 /* Backup metadata to a file. */
791 str = gctl_get_ascii(req, "backupfile");
792 if (str[0] != '\0') {
793 /* Backupfile given be the user, just copy it. */
794 strlcpy(backfile, str, sizeof(backfile));
796 /* Generate file name automatically. */
797 const char *p = prov;
800 if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
801 p += sizeof(_PATH_DEV) - 1;
802 snprintf(backfile, sizeof(backfile), "%s%s.eli",
804 /* Replace all / with _. */
805 for (i = strlen(GELI_BACKUP_DIR); backfile[i] != '\0'; i++) {
806 if (backfile[i] == '/')
810 if (strcmp(backfile, "none") != 0 &&
811 eli_backup_create(req, prov, backfile) == 0) {
812 printf("\nMetadata backup can be found in %s and\n", backfile);
813 printf("can be restored with the following command:\n");
814 printf("\n\t# geli restore %s %s\n\n", backfile, prov);
819 eli_attach(struct gctl_req *req)
821 struct g_eli_metadata md;
822 unsigned char key[G_ELI_USERKEYLEN];
827 nargs = gctl_get_int(req, "nargs");
829 gctl_error(req, "Invalid number of arguments.");
832 prov = gctl_get_ascii(req, "arg0");
834 if (eli_metadata_read(req, prov, &md) == -1)
837 mediasize = g_get_mediasize(prov);
838 if (md.md_provsize != (uint64_t)mediasize) {
839 gctl_error(req, "Provider size mismatch.");
843 if (eli_genkey(req, &md, key, false) == NULL) {
844 bzero(key, sizeof(key));
848 gctl_ro_param(req, "key", sizeof(key), key);
849 if (gctl_issue(req) == NULL) {
851 printf("Attached to %s.\n", prov);
853 bzero(key, sizeof(key));
857 eli_configure_detached(struct gctl_req *req, const char *prov, bool boot)
859 struct g_eli_metadata md;
861 if (eli_metadata_read(req, prov, &md) == -1)
864 if (boot && (md.md_flags & G_ELI_FLAG_BOOT)) {
866 printf("BOOT flag already configured for %s.\n", prov);
867 } else if (!boot && !(md.md_flags & G_ELI_FLAG_BOOT)) {
869 printf("BOOT flag not configured for %s.\n", prov);
872 md.md_flags |= G_ELI_FLAG_BOOT;
874 md.md_flags &= ~G_ELI_FLAG_BOOT;
875 eli_metadata_store(req, prov, &md);
877 bzero(&md, sizeof(md));
881 eli_configure(struct gctl_req *req)
887 nargs = gctl_get_int(req, "nargs");
889 gctl_error(req, "Too few arguments.");
893 boot = gctl_get_int(req, "boot");
894 noboot = gctl_get_int(req, "noboot");
896 if (boot && noboot) {
897 gctl_error(req, "Options -b and -B are mutually exclusive.");
900 if (!boot && !noboot) {
901 gctl_error(req, "No option given.");
905 /* First attached providers. */
908 for (i = 0; i < nargs; i++) {
909 prov = gctl_get_ascii(req, "arg%d", i);
910 if (!eli_is_attached(prov))
911 eli_configure_detached(req, prov, boot);
916 eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md)
918 unsigned char key[G_ELI_USERKEYLEN];
919 intmax_t val, old = 0;
922 val = gctl_get_intmax(req, "iterations");
923 /* Check if iterations number should be changed. */
925 md->md_iterations = val;
927 old = md->md_iterations;
929 /* Generate key for Master Key encryption. */
930 if (eli_genkey(req, md, key, true) == NULL) {
931 bzero(key, sizeof(key));
935 * If number of iterations has changed, but wasn't given as a
936 * command-line argument, update the request.
938 if (val == -1 && md->md_iterations != old) {
939 error = gctl_change_param(req, "iterations", sizeof(intmax_t),
944 gctl_ro_param(req, "key", sizeof(key), key);
946 bzero(key, sizeof(key));
950 eli_setkey_detached(struct gctl_req *req, const char *prov,
951 struct g_eli_metadata *md)
953 unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
954 unsigned char *mkeydst;
959 if (md->md_keys == 0) {
960 gctl_error(req, "No valid keys on %s.", prov);
964 /* Generate key for Master Key decryption. */
965 if (eli_genkey(req, md, key, false) == NULL) {
966 bzero(key, sizeof(key));
970 /* Decrypt Master Key. */
971 error = g_eli_mkey_decrypt(md, key, mkey, &nkey);
972 bzero(key, sizeof(key));
974 bzero(md, sizeof(*md));
976 gctl_error(req, "Wrong key for %s.", prov);
977 else /* if (error > 0) */ {
978 gctl_error(req, "Cannot decrypt Master Key: %s.",
984 printf("Decrypted Master Key %u.\n", nkey);
986 val = gctl_get_intmax(req, "keyno");
991 ; /* Use the key number which was found during decryption. */
993 if (nkey >= G_ELI_MAXMKEYS) {
994 gctl_error(req, "Invalid '%s' argument.", "keyno");
998 val = gctl_get_intmax(req, "iterations");
999 /* Check if iterations number should and can be changed. */
1001 if (bitcount32(md->md_keys) != 1) {
1002 gctl_error(req, "To be able to use '-i' option, only "
1003 "one key can be defined.");
1006 if (md->md_keys != (1 << nkey)) {
1007 gctl_error(req, "Only already defined key can be "
1008 "changed when '-i' option is used.");
1011 md->md_iterations = val;
1014 mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN;
1015 md->md_keys |= (1 << nkey);
1017 bcopy(mkey, mkeydst, sizeof(mkey));
1018 bzero(mkey, sizeof(mkey));
1020 /* Generate key for Master Key encryption. */
1021 if (eli_genkey(req, md, key, true) == NULL) {
1022 bzero(key, sizeof(key));
1023 bzero(md, sizeof(*md));
1027 /* Encrypt the Master-Key with the new key. */
1028 error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst);
1029 bzero(key, sizeof(key));
1031 bzero(md, sizeof(*md));
1032 gctl_error(req, "Cannot encrypt Master Key: %s.",
1037 /* Store metadata with fresh key. */
1038 eli_metadata_store(req, prov, md);
1039 bzero(md, sizeof(*md));
1043 eli_setkey(struct gctl_req *req)
1045 struct g_eli_metadata md;
1049 nargs = gctl_get_int(req, "nargs");
1051 gctl_error(req, "Invalid number of arguments.");
1054 prov = gctl_get_ascii(req, "arg0");
1056 if (eli_metadata_read(req, prov, &md) == -1)
1059 if (eli_is_attached(prov))
1060 eli_setkey_attached(req, &md);
1062 eli_setkey_detached(req, prov, &md);
1064 if (req->error == NULL || req->error[0] == '\0') {
1065 printf("Note, that the master key encrypted with old keys "
1066 "and/or passphrase may still exists in a metadata backup "
1072 eli_delkey_attached(struct gctl_req *req, const char *prov __unused)
1079 eli_delkey_detached(struct gctl_req *req, const char *prov)
1081 struct g_eli_metadata md;
1082 unsigned char *mkeydst;
1087 if (eli_metadata_read(req, prov, &md) == -1)
1090 all = gctl_get_int(req, "all");
1092 arc4rand(md.md_mkeys, sizeof(md.md_mkeys));
1094 force = gctl_get_int(req, "force");
1095 val = gctl_get_intmax(req, "keyno");
1097 gctl_error(req, "Key number has to be specified.");
1101 if (nkey >= G_ELI_MAXMKEYS) {
1102 gctl_error(req, "Invalid '%s' argument.", "keyno");
1105 if (!(md.md_keys & (1 << nkey)) && !force) {
1106 gctl_error(req, "Master Key %u is not set.", nkey);
1109 md.md_keys &= ~(1 << nkey);
1110 if (md.md_keys == 0 && !force) {
1111 gctl_error(req, "This is the last Master Key. Use '-f' "
1112 "option if you really want to remove it.");
1115 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN;
1116 arc4rand(mkeydst, G_ELI_MKEYLEN);
1119 eli_metadata_store(req, prov, &md);
1120 bzero(&md, sizeof(md));
1124 eli_delkey(struct gctl_req *req)
1129 nargs = gctl_get_int(req, "nargs");
1131 gctl_error(req, "Invalid number of arguments.");
1134 prov = gctl_get_ascii(req, "arg0");
1136 if (eli_is_attached(prov))
1137 eli_delkey_attached(req, prov);
1139 eli_delkey_detached(req, prov);
1143 eli_resume(struct gctl_req *req)
1145 struct g_eli_metadata md;
1146 unsigned char key[G_ELI_USERKEYLEN];
1151 nargs = gctl_get_int(req, "nargs");
1153 gctl_error(req, "Invalid number of arguments.");
1156 prov = gctl_get_ascii(req, "arg0");
1158 if (eli_metadata_read(req, prov, &md) == -1)
1161 mediasize = g_get_mediasize(prov);
1162 if (md.md_provsize != (uint64_t)mediasize) {
1163 gctl_error(req, "Provider size mismatch.");
1167 if (eli_genkey(req, &md, key, false) == NULL) {
1168 bzero(key, sizeof(key));
1172 gctl_ro_param(req, "key", sizeof(key), key);
1173 if (gctl_issue(req) == NULL) {
1175 printf("Resumed %s.\n", prov);
1177 bzero(key, sizeof(key));
1181 eli_trash_metadata(struct gctl_req *req, const char *prov, int fd, off_t offset)
1183 unsigned int overwrites;
1184 unsigned char *sector;
1188 size = sizeof(overwrites);
1189 if (sysctlbyname("kern.geom.eli.overwrites", &overwrites, &size,
1190 NULL, 0) == -1 || overwrites == 0) {
1191 overwrites = G_ELI_OVERWRITES;
1194 size = g_sectorsize(fd);
1196 gctl_error(req, "Cannot obtain provider sector size %s: %s.",
1197 prov, strerror(errno));
1200 sector = malloc(size);
1201 if (sector == NULL) {
1202 gctl_error(req, "Cannot allocate %zd bytes of memory.", size);
1208 arc4rand(sector, size);
1209 if (pwrite(fd, sector, size, offset) != size) {
1214 } while (--overwrites > 0);
1216 gctl_error(req, "Cannot trash metadata on provider %s: %s.",
1217 prov, strerror(error));
1224 eli_kill_detached(struct gctl_req *req, const char *prov)
1230 * NOTE: Maybe we should verify if this is geli provider first,
1231 * but 'kill' command is quite critical so better don't waste
1235 error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md),
1238 gctl_error(req, "Cannot read metadata from %s: %s.", prov,
1244 fd = g_open(prov, 1);
1246 gctl_error(req, "Cannot open provider %s: %s.", prov,
1250 offset = g_mediasize(fd) - g_sectorsize(fd);
1253 "Cannot obtain media size or sector size for provider %s: %s.",
1254 prov, strerror(errno));
1258 (void)eli_trash_metadata(req, prov, fd, offset);
1263 eli_kill(struct gctl_req *req)
1268 nargs = gctl_get_int(req, "nargs");
1269 all = gctl_get_int(req, "all");
1270 if (!all && nargs == 0) {
1271 gctl_error(req, "Too few arguments.");
1275 * How '-a' option combine with a list of providers:
1276 * Delete Master Keys from all attached providers:
1278 * Delete Master Keys from all attached providers and from
1279 * detached da0 and da1:
1280 * geli kill -a da0 da1
1281 * Delete Master Keys from (attached or detached) da0 and da1:
1285 /* First detached providers. */
1286 for (i = 0; i < nargs; i++) {
1287 prov = gctl_get_ascii(req, "arg%d", i);
1288 if (!eli_is_attached(prov))
1289 eli_kill_detached(req, prov);
1291 /* Now attached providers. */
1296 eli_backup_create(struct gctl_req *req, const char *prov, const char *file)
1298 struct g_eli_metadata md;
1299 unsigned char *sector;
1302 int filefd, provfd, ret;
1305 provfd = filefd = -1;
1309 provfd = g_open(prov, 0);
1311 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno));
1314 filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1316 gctl_error(req, "Cannot open %s: %s.", file, strerror(errno));
1320 mediasize = g_mediasize(provfd);
1321 secsize = g_sectorsize(provfd);
1322 if (mediasize == -1 || secsize == -1) {
1323 gctl_error(req, "Cannot get informations about %s: %s.", prov,
1328 sector = malloc(secsize);
1329 if (sector == NULL) {
1330 gctl_error(req, "Cannot allocate memory.");
1334 /* Read metadata from the provider. */
1335 if (pread(provfd, sector, secsize, mediasize - secsize) != secsize) {
1336 gctl_error(req, "Cannot read metadata: %s.", strerror(errno));
1339 /* Check if this is geli provider. */
1340 if (eli_metadata_decode(sector, &md) != 0) {
1341 gctl_error(req, "MD5 hash mismatch: not a geli provider?");
1344 /* Write metadata to the destination file. */
1345 if (write(filefd, sector, secsize) != secsize) {
1346 gctl_error(req, "Cannot write to %s: %s.", file,
1350 (void)fsync(filefd);
1355 (void)g_close(provfd);
1357 (void)close(filefd);
1358 if (sector != NULL) {
1359 bzero(sector, secsize);
1366 eli_backup(struct gctl_req *req)
1368 const char *file, *prov;
1371 nargs = gctl_get_int(req, "nargs");
1373 gctl_error(req, "Invalid number of arguments.");
1376 prov = gctl_get_ascii(req, "arg0");
1377 file = gctl_get_ascii(req, "arg1");
1379 eli_backup_create(req, prov, file);
1383 eli_restore(struct gctl_req *req)
1385 struct g_eli_metadata md;
1386 const char *file, *prov;
1387 unsigned char *sector;
1390 int nargs, filefd, provfd;
1392 nargs = gctl_get_int(req, "nargs");
1394 gctl_error(req, "Invalid number of arguments.");
1397 file = gctl_get_ascii(req, "arg0");
1398 prov = gctl_get_ascii(req, "arg1");
1400 provfd = filefd = -1;
1404 filefd = open(file, O_RDONLY);
1406 gctl_error(req, "Cannot open %s: %s.", file, strerror(errno));
1409 provfd = g_open(prov, 1);
1411 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno));
1415 mediasize = g_mediasize(provfd);
1416 secsize = g_sectorsize(provfd);
1417 if (mediasize == -1 || secsize == -1) {
1418 gctl_error(req, "Cannot get informations about %s: %s.", prov,
1423 sector = malloc(secsize);
1424 if (sector == NULL) {
1425 gctl_error(req, "Cannot allocate memory.");
1429 /* Read metadata from the backup file. */
1430 if (read(filefd, sector, secsize) != secsize) {
1431 gctl_error(req, "Cannot read from %s: %s.", file,
1435 /* Check if this file contains geli metadata. */
1436 if (eli_metadata_decode(sector, &md) != 0) {
1437 gctl_error(req, "MD5 hash mismatch: not a geli backup file?");
1440 /* Check if the provider size has changed since we did the backup. */
1441 if (md.md_provsize != (uint64_t)mediasize) {
1442 if (gctl_get_int(req, "force")) {
1443 md.md_provsize = mediasize;
1444 eli_metadata_encode(&md, sector);
1446 gctl_error(req, "Provider size mismatch: "
1447 "wrong backup file?");
1451 /* Write metadata from the provider. */
1452 if (pwrite(provfd, sector, secsize, mediasize - secsize) != secsize) {
1453 gctl_error(req, "Cannot write metadata: %s.", strerror(errno));
1456 (void)g_flush(provfd);
1459 (void)g_close(provfd);
1461 (void)close(filefd);
1462 if (sector != NULL) {
1463 bzero(sector, secsize);
1469 eli_resize(struct gctl_req *req)
1471 struct g_eli_metadata md;
1473 unsigned char *sector;
1475 off_t mediasize, oldsize;
1478 nargs = gctl_get_int(req, "nargs");
1480 gctl_error(req, "Invalid number of arguments.");
1483 prov = gctl_get_ascii(req, "arg0");
1489 provfd = g_open(prov, 1);
1491 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno));
1495 mediasize = g_mediasize(provfd);
1496 secsize = g_sectorsize(provfd);
1497 if (mediasize == -1 || secsize == -1) {
1498 gctl_error(req, "Cannot get information about %s: %s.", prov,
1503 sector = malloc(secsize);
1504 if (sector == NULL) {
1505 gctl_error(req, "Cannot allocate memory.");
1509 oldsize = gctl_get_intmax(req, "oldsize");
1510 if (oldsize < 0 || oldsize > mediasize) {
1511 gctl_error(req, "Invalid oldsize: Out of range.");
1514 if (oldsize == mediasize) {
1515 gctl_error(req, "Size hasn't changed.");
1519 /* Read metadata from the 'oldsize' offset. */
1520 if (pread(provfd, sector, secsize, oldsize - secsize) != secsize) {
1521 gctl_error(req, "Cannot read old metadata: %s.",
1526 /* Check if this sector contains geli metadata. */
1527 if (eli_metadata_decode(sector, &md) != 0) {
1528 gctl_error(req, "MD5 hash mismatch: no metadata for oldsize.");
1533 * If the old metadata doesn't have a correct provider size, refuse
1536 if (md.md_provsize != (uint64_t)oldsize) {
1537 gctl_error(req, "Provider size mismatch at oldsize.");
1542 * Update the old metadata with the current provider size and write
1543 * it back to the correct place on the provider.
1545 md.md_provsize = mediasize;
1546 eli_metadata_encode(&md, sector);
1547 if (pwrite(provfd, sector, secsize, mediasize - secsize) != secsize) {
1548 gctl_error(req, "Cannot write metadata: %s.", strerror(errno));
1551 (void)g_flush(provfd);
1553 /* Now trash the old metadata. */
1554 if (eli_trash_metadata(req, prov, provfd, oldsize - secsize) == -1)
1558 (void)g_close(provfd);
1559 if (sector != NULL) {
1560 bzero(sector, secsize);
1566 eli_clear(struct gctl_req *req)
1569 int error, i, nargs;
1571 nargs = gctl_get_int(req, "nargs");
1573 gctl_error(req, "Too few arguments.");
1577 for (i = 0; i < nargs; i++) {
1578 name = gctl_get_ascii(req, "arg%d", i);
1579 error = g_metadata_clear(name, G_ELI_MAGIC);
1581 fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
1582 name, strerror(error));
1583 gctl_error(req, "Not fully done.");
1587 printf("Metadata cleared on %s.\n", name);
1592 eli_dump(struct gctl_req *req)
1594 struct g_eli_metadata md, tmpmd;
1596 int error, i, nargs;
1598 nargs = gctl_get_int(req, "nargs");
1600 gctl_error(req, "Too few arguments.");
1604 for (i = 0; i < nargs; i++) {
1605 name = gctl_get_ascii(req, "arg%d", i);
1606 error = g_metadata_read(name, (unsigned char *)&tmpmd,
1607 sizeof(tmpmd), G_ELI_MAGIC);
1609 fprintf(stderr, "Cannot read metadata from %s: %s.\n",
1610 name, strerror(error));
1611 gctl_error(req, "Not fully done.");
1614 if (eli_metadata_decode((unsigned char *)&tmpmd, &md) != 0) {
1615 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
1617 gctl_error(req, "Not fully done.");
1620 printf("Metadata on %s:\n", name);
1621 eli_metadata_dump(&md);