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/param.h>
32 #include <sys/sysctl.h>
33 #include <sys/resource.h>
34 #include <opencrypto/cryptodev.h>
42 #include <readpassphrase.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_version(struct gctl_req *req);
76 static void eli_clear(struct gctl_req *req);
77 static void eli_dump(struct gctl_req *req);
79 static int eli_backup_create(struct gctl_req *req, const char *prov,
85 * init [-bhPv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-V version] prov
86 * label - alias for 'init'
87 * attach [-dprv] [-j passfile] [-k keyfile] prov
88 * detach [-fl] prov ...
89 * stop - alias for 'detach'
90 * onetime [-d] [-a aalgo] [-e ealgo] [-l keylen] prov
91 * configure [-bB] prov ...
92 * setkey [-pPv] [-n keyno] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov
93 * delkey [-afv] [-n keyno] prov
94 * suspend [-v] -a | prov ...
95 * resume [-pv] [-j passfile] [-k keyfile] prov
96 * kill [-av] [prov ...]
97 * backup [-v] prov file
98 * restore [-fv] file prov
99 * resize [-v] -s oldsize prov
101 * clear [-v] prov ...
104 struct g_command class_commands[] = {
105 { "init", G_FLAG_VERBOSE, eli_main,
107 { 'a', "aalgo", "", G_TYPE_STRING },
108 { 'b', "boot", NULL, G_TYPE_BOOL },
109 { 'B', "backupfile", "", G_TYPE_STRING },
110 { 'e', "ealgo", "", G_TYPE_STRING },
111 { 'i', "iterations", "-1", G_TYPE_NUMBER },
112 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
113 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
114 { 'l', "keylen", "0", G_TYPE_NUMBER },
115 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
116 { 's', "sectorsize", "0", G_TYPE_NUMBER },
117 { 'V', "mdversion", "-1", G_TYPE_NUMBER },
120 "[-bPv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov"
122 { "label", G_FLAG_VERBOSE, eli_main,
124 { 'a', "aalgo", "", G_TYPE_STRING },
125 { 'b', "boot", NULL, G_TYPE_BOOL },
126 { 'B', "backupfile", "", G_TYPE_STRING },
127 { 'e', "ealgo", "", G_TYPE_STRING },
128 { 'i', "iterations", "-1", G_TYPE_NUMBER },
129 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
130 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
131 { 'l', "keylen", "0", G_TYPE_NUMBER },
132 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
133 { 's', "sectorsize", "0", G_TYPE_NUMBER },
134 { 'V', "mdversion", "-1", G_TYPE_NUMBER },
137 "- an alias for 'init'"
139 { "attach", G_FLAG_VERBOSE | G_FLAG_LOADKLD, eli_main,
141 { 'd', "detach", NULL, G_TYPE_BOOL },
142 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
143 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
144 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
145 { 'r', "readonly", NULL, G_TYPE_BOOL },
148 "[-dprv] [-j passfile] [-k keyfile] prov"
152 { 'f', "force", NULL, G_TYPE_BOOL },
153 { 'l', "last", NULL, G_TYPE_BOOL },
160 { 'f', "force", NULL, G_TYPE_BOOL },
161 { 'l', "last", NULL, G_TYPE_BOOL },
164 "- an alias for 'detach'"
166 { "onetime", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
168 { 'a', "aalgo", "", G_TYPE_STRING },
169 { 'd', "detach", NULL, G_TYPE_BOOL },
170 { 'e', "ealgo", GELI_ENC_ALGO, G_TYPE_STRING },
171 { 'l', "keylen", "0", G_TYPE_NUMBER },
172 { 's', "sectorsize", "0", G_TYPE_NUMBER },
175 "[-d] [-a aalgo] [-e ealgo] [-l keylen] [-s sectorsize] prov"
177 { "configure", G_FLAG_VERBOSE, eli_main,
179 { 'b', "boot", NULL, G_TYPE_BOOL },
180 { 'B', "noboot", NULL, G_TYPE_BOOL },
185 { "setkey", G_FLAG_VERBOSE, eli_main,
187 { 'i', "iterations", "-1", G_TYPE_NUMBER },
188 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
189 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
190 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
191 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
192 { 'n', "keyno", "-1", G_TYPE_NUMBER },
193 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
194 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
197 "[-pPv] [-n keyno] [-i iterations] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov"
199 { "delkey", G_FLAG_VERBOSE, eli_main,
201 { 'a', "all", NULL, G_TYPE_BOOL },
202 { 'f', "force", NULL, G_TYPE_BOOL },
203 { 'n', "keyno", "-1", G_TYPE_NUMBER },
206 "[-afv] [-n keyno] prov"
208 { "suspend", G_FLAG_VERBOSE, NULL,
210 { 'a', "all", NULL, G_TYPE_BOOL },
215 { "resume", G_FLAG_VERBOSE, eli_main,
217 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
218 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
219 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
222 "[-pv] [-j passfile] [-k keyfile] prov"
224 { "kill", G_FLAG_VERBOSE, eli_main,
226 { 'a', "all", NULL, G_TYPE_BOOL },
231 { "backup", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
234 { "restore", G_FLAG_VERBOSE, eli_main,
236 { 'f', "force", NULL, G_TYPE_BOOL },
241 { "resize", G_FLAG_VERBOSE, eli_main,
243 { 's', "oldsize", NULL, G_TYPE_NUMBER },
246 "[-v] -s oldsize prov"
248 { "version", G_FLAG_LOADKLD, eli_main, G_NULL_OPTS,
251 { "clear", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
254 { "dump", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
260 static int verbose = 0;
265 eli_protect(struct gctl_req *req)
269 /* Disable core dumps. */
272 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
273 gctl_error(req, "Cannot disable core dumps: %s.",
277 /* Disable swapping. */
278 if (mlockall(MCL_FUTURE) == -1) {
279 gctl_error(req, "Cannot lock memory: %s.", strerror(errno));
286 eli_main(struct gctl_req *req, unsigned int flags)
290 if (eli_protect(req) == -1)
293 if ((flags & G_FLAG_VERBOSE) != 0)
296 name = gctl_get_ascii(req, "verb");
298 gctl_error(req, "No '%s' argument.", "verb");
301 if (strcmp(name, "init") == 0 || strcmp(name, "label") == 0)
303 else if (strcmp(name, "attach") == 0)
305 else if (strcmp(name, "configure") == 0)
307 else if (strcmp(name, "setkey") == 0)
309 else if (strcmp(name, "delkey") == 0)
311 else if (strcmp(name, "resume") == 0)
313 else if (strcmp(name, "kill") == 0)
315 else if (strcmp(name, "backup") == 0)
317 else if (strcmp(name, "restore") == 0)
319 else if (strcmp(name, "resize") == 0)
321 else if (strcmp(name, "version") == 0)
323 else if (strcmp(name, "dump") == 0)
325 else if (strcmp(name, "clear") == 0)
328 gctl_error(req, "Unknown command: %s.", name);
332 eli_is_attached(const char *prov)
334 char name[MAXPATHLEN];
337 * Not the best way to do it, but the easiest.
338 * We try to open provider and check if it is a GEOM provider
339 * by asking about its sectorsize.
341 snprintf(name, sizeof(name), "%s%s", prov, G_ELI_SUFFIX);
342 return (g_get_sectorsize(name) > 0);
346 eli_genkey_files(struct gctl_req *req, bool new, const char *type,
347 struct hmac_ctx *ctxp, char *passbuf, size_t passbufsize)
349 char *p, buf[BUFSIZE], argname[16];
354 assert((strcmp(type, "keyfile") == 0 && ctxp != NULL &&
355 passbuf == NULL && passbufsize == 0) ||
356 (strcmp(type, "passfile") == 0 && ctxp == NULL &&
357 passbuf != NULL && passbufsize > 0));
358 assert(strcmp(type, "keyfile") == 0 || passbuf[0] == '\0');
361 snprintf(argname, sizeof(argname), "%s%s%d",
362 new ? "new" : "", type, i);
364 /* No more {key,pass}files? */
365 if (!gctl_has_param(req, argname))
368 file = gctl_get_ascii(req, "%s", argname);
369 assert(file != NULL);
371 if (strcmp(file, "-") == 0)
374 fd = open(file, O_RDONLY);
376 gctl_error(req, "Cannot open %s %s: %s.",
377 type, file, strerror(errno));
381 if (strcmp(type, "keyfile") == 0) {
382 while ((done = read(fd, buf, sizeof(buf))) > 0)
383 g_eli_crypto_hmac_update(ctxp, buf, done);
384 } else /* if (strcmp(type, "passfile") == 0) */ {
385 assert(strcmp(type, "passfile") == 0);
387 while ((done = read(fd, buf, sizeof(buf) - 1)) > 0) {
389 p = strchr(buf, '\n');
394 if (strlcat(passbuf, buf, passbufsize) >=
397 "Passphrase in %s too long.", file);
398 bzero(buf, sizeof(buf));
406 if (strcmp(file, "-") != 0)
408 bzero(buf, sizeof(buf));
410 gctl_error(req, "Cannot read %s %s: %s.",
411 type, file, strerror(error));
419 eli_genkey_passphrase_prompt(struct gctl_req *req, bool new, char *passbuf,
426 new ? "Enter new passphrase: " : "Enter passphrase: ",
427 passbuf, passbufsize, RPP_ECHO_OFF | RPP_REQUIRE_TTY);
429 bzero(passbuf, passbufsize);
430 gctl_error(req, "Cannot read passphrase: %s.",
436 char tmpbuf[BUFSIZE];
438 p = readpassphrase("Reenter new passphrase: ",
439 tmpbuf, sizeof(tmpbuf),
440 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
442 bzero(passbuf, passbufsize);
444 "Cannot read passphrase: %s.",
449 if (strcmp(passbuf, tmpbuf) != 0) {
450 bzero(passbuf, passbufsize);
451 fprintf(stderr, "They didn't match.\n");
454 bzero(tmpbuf, sizeof(tmpbuf));
462 eli_genkey_passphrase(struct gctl_req *req, struct g_eli_metadata *md, bool new,
463 struct hmac_ctx *ctxp)
465 char passbuf[BUFSIZE];
470 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
472 if (gctl_has_param(req, new ? "newpassfile0" : "passfile0")) {
474 "Options -%c and -%c are mutually exclusive.",
475 new ? 'J' : 'j', new ? 'P' : 'p');
481 if (!new && md->md_iterations == -1) {
482 gctl_error(req, "Missing -p flag.");
486 nfiles = eli_genkey_files(req, new, "passfile", NULL, passbuf,
490 else if (nfiles == 0) {
491 if (eli_genkey_passphrase_prompt(req, new, passbuf,
492 sizeof(passbuf)) == -1) {
497 * Field md_iterations equal to -1 means "choose some sane
500 if (md->md_iterations == -1) {
503 printf("Calculating number of iterations...\n");
504 md->md_iterations = pkcs5v2_calculate(2000000);
505 assert(md->md_iterations > 0);
507 printf("Done, using %d iterations.\n",
512 * If md_iterations is equal to 0, user doesn't want PKCS#5v2.
514 if (md->md_iterations == 0) {
515 g_eli_crypto_hmac_update(ctxp, md->md_salt,
516 sizeof(md->md_salt));
517 g_eli_crypto_hmac_update(ctxp, passbuf, strlen(passbuf));
518 } else /* if (md->md_iterations > 0) */ {
519 unsigned char dkey[G_ELI_USERKEYLEN];
521 pkcs5v2_genkey(dkey, sizeof(dkey), md->md_salt,
522 sizeof(md->md_salt), passbuf, md->md_iterations);
523 g_eli_crypto_hmac_update(ctxp, dkey, sizeof(dkey));
524 bzero(dkey, sizeof(dkey));
526 bzero(passbuf, sizeof(passbuf));
531 static unsigned char *
532 eli_genkey(struct gctl_req *req, struct g_eli_metadata *md, unsigned char *key,
540 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
542 g_eli_crypto_hmac_init(&ctx, NULL, 0);
544 nfiles = eli_genkey_files(req, new, "keyfile", &ctx, NULL, 0);
547 else if (nfiles == 0 && nopassphrase) {
548 gctl_error(req, "No key components given.");
552 if (eli_genkey_passphrase(req, md, new, &ctx) == -1)
555 g_eli_crypto_hmac_final(&ctx, key, 0);
561 eli_metadata_read(struct gctl_req *req, const char *prov,
562 struct g_eli_metadata *md)
564 unsigned char sector[sizeof(struct g_eli_metadata)];
567 if (g_get_sectorsize(prov) == 0) {
570 /* This is a file probably. */
571 fd = open(prov, O_RDONLY);
573 gctl_error(req, "Cannot open %s: %s.", prov,
577 if (read(fd, sector, sizeof(sector)) != sizeof(sector)) {
578 gctl_error(req, "Cannot read metadata from %s: %s.",
579 prov, strerror(errno));
585 /* This is a GEOM provider. */
586 error = g_metadata_read(prov, sector, sizeof(sector),
589 gctl_error(req, "Cannot read metadata from %s: %s.",
590 prov, strerror(error));
594 error = eli_metadata_decode(sector, md);
600 "Provider's %s metadata version %u is too new.\n"
601 "geli: The highest supported version is %u.",
602 prov, (unsigned int)md->md_version, G_ELI_VERSION);
605 gctl_error(req, "Inconsistent provider's %s metadata.", prov);
609 "Unexpected error while decoding provider's %s metadata: %s.",
610 prov, strerror(error));
617 eli_metadata_store(struct gctl_req *req, const char *prov,
618 struct g_eli_metadata *md)
620 unsigned char sector[sizeof(struct g_eli_metadata)];
623 eli_metadata_encode(md, sector);
624 if (g_get_sectorsize(prov) == 0) {
627 /* This is a file probably. */
628 fd = open(prov, O_WRONLY | O_TRUNC);
630 gctl_error(req, "Cannot open %s: %s.", prov,
632 bzero(sector, sizeof(sector));
635 if (write(fd, sector, sizeof(sector)) != sizeof(sector)) {
636 gctl_error(req, "Cannot write metadata to %s: %s.",
637 prov, strerror(errno));
638 bzero(sector, sizeof(sector));
644 /* This is a GEOM provider. */
645 error = g_metadata_store(prov, sector, sizeof(sector));
647 gctl_error(req, "Cannot write metadata to %s: %s.",
648 prov, strerror(errno));
649 bzero(sector, sizeof(sector));
653 bzero(sector, sizeof(sector));
658 eli_init(struct gctl_req *req)
660 struct g_eli_metadata md;
661 unsigned char sector[sizeof(struct g_eli_metadata)];
662 unsigned char key[G_ELI_USERKEYLEN];
663 char backfile[MAXPATHLEN];
664 const char *str, *prov;
665 unsigned int secsize, version;
670 nargs = gctl_get_int(req, "nargs");
672 gctl_error(req, "Invalid number of arguments.");
675 prov = gctl_get_ascii(req, "arg0");
676 mediasize = g_get_mediasize(prov);
677 secsize = g_get_sectorsize(prov);
678 if (mediasize == 0 || secsize == 0) {
679 gctl_error(req, "Cannot get informations about %s: %s.", prov,
684 bzero(&md, sizeof(md));
685 strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic));
686 val = gctl_get_intmax(req, "mdversion");
688 version = G_ELI_VERSION;
689 } else if (val < 0 || val > G_ELI_VERSION) {
691 "Invalid version specified should be between %u and %u.",
692 G_ELI_VERSION_00, G_ELI_VERSION);
697 md.md_version = version;
699 if (gctl_get_int(req, "boot"))
700 md.md_flags |= G_ELI_FLAG_BOOT;
701 md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1;
702 str = gctl_get_ascii(req, "aalgo");
704 if (version < G_ELI_VERSION_01) {
706 "Data authentication is supported starting from version %u.",
710 md.md_aalgo = g_eli_str2aalgo(str);
711 if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN &&
712 md.md_aalgo <= CRYPTO_ALGORITHM_MAX) {
713 md.md_flags |= G_ELI_FLAG_AUTH;
716 * For backward compatibility, check if the -a option
717 * was used to provide encryption algorithm.
719 md.md_ealgo = g_eli_str2ealgo(str);
720 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
721 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
723 "Invalid authentication algorithm.");
726 fprintf(stderr, "warning: The -e option, not "
727 "the -a option is now used to specify "
728 "encryption algorithm to use.\n");
732 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
733 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
734 str = gctl_get_ascii(req, "ealgo");
736 if (version < G_ELI_VERSION_05)
741 md.md_ealgo = g_eli_str2ealgo(str);
742 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
743 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
744 gctl_error(req, "Invalid encryption algorithm.");
747 if (md.md_ealgo == CRYPTO_CAMELLIA_CBC &&
748 version < G_ELI_VERSION_04) {
750 "Camellia-CBC algorithm is supported starting from version %u.",
754 if (md.md_ealgo == CRYPTO_AES_XTS &&
755 version < G_ELI_VERSION_05) {
757 "AES-XTS algorithm is supported starting from version %u.",
762 val = gctl_get_intmax(req, "keylen");
764 md.md_keylen = g_eli_keylen(md.md_ealgo, md.md_keylen);
765 if (md.md_keylen == 0) {
766 gctl_error(req, "Invalid key length.");
769 md.md_provsize = mediasize;
771 val = gctl_get_intmax(req, "iterations");
776 * Don't allow to set iterations when there will be no
779 nonewpassphrase = gctl_get_int(req, "nonewpassphrase");
780 if (nonewpassphrase) {
782 "Options -i and -P are mutually exclusive.");
786 md.md_iterations = val;
788 val = gctl_get_intmax(req, "sectorsize");
790 md.md_sectorsize = secsize;
792 if (val < 0 || (val % secsize) != 0) {
793 gctl_error(req, "Invalid sector size.");
796 if (val > sysconf(_SC_PAGE_SIZE)) {
798 "warning: Using sectorsize bigger than the page size!\n");
800 md.md_sectorsize = val;
804 arc4random_buf(md.md_salt, sizeof(md.md_salt));
805 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys));
807 /* Generate user key. */
808 if (eli_genkey(req, &md, key, true) == NULL) {
809 bzero(key, sizeof(key));
810 bzero(&md, sizeof(md));
814 /* Encrypt the first and the only Master Key. */
815 error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen, md.md_mkeys);
816 bzero(key, sizeof(key));
818 bzero(&md, sizeof(md));
819 gctl_error(req, "Cannot encrypt Master Key: %s.",
824 eli_metadata_encode(&md, sector);
825 bzero(&md, sizeof(md));
826 error = g_metadata_store(prov, sector, sizeof(sector));
827 bzero(sector, sizeof(sector));
829 gctl_error(req, "Cannot store metadata on %s: %s.", prov,
834 printf("Metadata value stored on %s.\n", prov);
835 /* Backup metadata to a file. */
836 str = gctl_get_ascii(req, "backupfile");
837 if (str[0] != '\0') {
838 /* Backupfile given be the user, just copy it. */
839 strlcpy(backfile, str, sizeof(backfile));
841 /* Generate file name automatically. */
842 const char *p = prov;
845 if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
846 p += sizeof(_PATH_DEV) - 1;
847 snprintf(backfile, sizeof(backfile), "%s%s.eli",
849 /* Replace all / with _. */
850 for (i = strlen(GELI_BACKUP_DIR); backfile[i] != '\0'; i++) {
851 if (backfile[i] == '/')
855 if (strcmp(backfile, "none") != 0 &&
856 eli_backup_create(req, prov, backfile) == 0) {
857 printf("\nMetadata backup can be found in %s and\n", backfile);
858 printf("can be restored with the following command:\n");
859 printf("\n\t# geli restore %s %s\n\n", backfile, prov);
864 eli_attach(struct gctl_req *req)
866 struct g_eli_metadata md;
867 unsigned char key[G_ELI_USERKEYLEN];
872 nargs = gctl_get_int(req, "nargs");
874 gctl_error(req, "Invalid number of arguments.");
877 prov = gctl_get_ascii(req, "arg0");
879 if (eli_metadata_read(req, prov, &md) == -1)
882 mediasize = g_get_mediasize(prov);
883 if (md.md_provsize != (uint64_t)mediasize) {
884 gctl_error(req, "Provider size mismatch.");
888 if (eli_genkey(req, &md, key, false) == NULL) {
889 bzero(key, sizeof(key));
893 gctl_ro_param(req, "key", sizeof(key), key);
894 if (gctl_issue(req) == NULL) {
896 printf("Attached to %s.\n", prov);
898 bzero(key, sizeof(key));
902 eli_configure_detached(struct gctl_req *req, const char *prov, bool boot)
904 struct g_eli_metadata md;
906 if (eli_metadata_read(req, prov, &md) == -1)
909 if (boot && (md.md_flags & G_ELI_FLAG_BOOT)) {
911 printf("BOOT flag already configured for %s.\n", prov);
912 } else if (!boot && !(md.md_flags & G_ELI_FLAG_BOOT)) {
914 printf("BOOT flag not configured for %s.\n", prov);
917 md.md_flags |= G_ELI_FLAG_BOOT;
919 md.md_flags &= ~G_ELI_FLAG_BOOT;
920 eli_metadata_store(req, prov, &md);
922 bzero(&md, sizeof(md));
926 eli_configure(struct gctl_req *req)
932 nargs = gctl_get_int(req, "nargs");
934 gctl_error(req, "Too few arguments.");
938 boot = gctl_get_int(req, "boot");
939 noboot = gctl_get_int(req, "noboot");
941 if (boot && noboot) {
942 gctl_error(req, "Options -b and -B are mutually exclusive.");
945 if (!boot && !noboot) {
946 gctl_error(req, "No option given.");
950 /* First attached providers. */
953 for (i = 0; i < nargs; i++) {
954 prov = gctl_get_ascii(req, "arg%d", i);
955 if (!eli_is_attached(prov))
956 eli_configure_detached(req, prov, boot);
961 eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md)
963 unsigned char key[G_ELI_USERKEYLEN];
964 intmax_t val, old = 0;
967 val = gctl_get_intmax(req, "iterations");
968 /* Check if iterations number should be changed. */
970 md->md_iterations = val;
972 old = md->md_iterations;
974 /* Generate key for Master Key encryption. */
975 if (eli_genkey(req, md, key, true) == NULL) {
976 bzero(key, sizeof(key));
980 * If number of iterations has changed, but wasn't given as a
981 * command-line argument, update the request.
983 if (val == -1 && md->md_iterations != old) {
984 error = gctl_change_param(req, "iterations", sizeof(intmax_t),
989 gctl_ro_param(req, "key", sizeof(key), key);
991 bzero(key, sizeof(key));
995 eli_setkey_detached(struct gctl_req *req, const char *prov,
996 struct g_eli_metadata *md)
998 unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
999 unsigned char *mkeydst;
1004 if (md->md_keys == 0) {
1005 gctl_error(req, "No valid keys on %s.", prov);
1009 /* Generate key for Master Key decryption. */
1010 if (eli_genkey(req, md, key, false) == NULL) {
1011 bzero(key, sizeof(key));
1015 /* Decrypt Master Key. */
1016 error = g_eli_mkey_decrypt(md, key, mkey, &nkey);
1017 bzero(key, sizeof(key));
1019 bzero(md, sizeof(*md));
1021 gctl_error(req, "Wrong key for %s.", prov);
1022 else /* if (error > 0) */ {
1023 gctl_error(req, "Cannot decrypt Master Key: %s.",
1029 printf("Decrypted Master Key %u.\n", nkey);
1031 val = gctl_get_intmax(req, "keyno");
1036 ; /* Use the key number which was found during decryption. */
1038 if (nkey >= G_ELI_MAXMKEYS) {
1039 gctl_error(req, "Invalid '%s' argument.", "keyno");
1043 val = gctl_get_intmax(req, "iterations");
1044 /* Check if iterations number should and can be changed. */
1046 if (bitcount32(md->md_keys) != 1) {
1047 gctl_error(req, "To be able to use '-i' option, only "
1048 "one key can be defined.");
1051 if (md->md_keys != (1 << nkey)) {
1052 gctl_error(req, "Only already defined key can be "
1053 "changed when '-i' option is used.");
1056 md->md_iterations = val;
1059 mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN;
1060 md->md_keys |= (1 << nkey);
1062 bcopy(mkey, mkeydst, sizeof(mkey));
1063 bzero(mkey, sizeof(mkey));
1065 /* Generate key for Master Key encryption. */
1066 if (eli_genkey(req, md, key, true) == NULL) {
1067 bzero(key, sizeof(key));
1068 bzero(md, sizeof(*md));
1072 /* Encrypt the Master-Key with the new key. */
1073 error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst);
1074 bzero(key, sizeof(key));
1076 bzero(md, sizeof(*md));
1077 gctl_error(req, "Cannot encrypt Master Key: %s.",
1082 /* Store metadata with fresh key. */
1083 eli_metadata_store(req, prov, md);
1084 bzero(md, sizeof(*md));
1088 eli_setkey(struct gctl_req *req)
1090 struct g_eli_metadata md;
1094 nargs = gctl_get_int(req, "nargs");
1096 gctl_error(req, "Invalid number of arguments.");
1099 prov = gctl_get_ascii(req, "arg0");
1101 if (eli_metadata_read(req, prov, &md) == -1)
1104 if (eli_is_attached(prov))
1105 eli_setkey_attached(req, &md);
1107 eli_setkey_detached(req, prov, &md);
1109 if (req->error == NULL || req->error[0] == '\0') {
1110 printf("Note, that the master key encrypted with old keys "
1111 "and/or passphrase may still exists in a metadata backup "
1117 eli_delkey_attached(struct gctl_req *req, const char *prov __unused)
1124 eli_delkey_detached(struct gctl_req *req, const char *prov)
1126 struct g_eli_metadata md;
1127 unsigned char *mkeydst;
1132 if (eli_metadata_read(req, prov, &md) == -1)
1135 all = gctl_get_int(req, "all");
1137 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys));
1139 force = gctl_get_int(req, "force");
1140 val = gctl_get_intmax(req, "keyno");
1142 gctl_error(req, "Key number has to be specified.");
1146 if (nkey >= G_ELI_MAXMKEYS) {
1147 gctl_error(req, "Invalid '%s' argument.", "keyno");
1150 if (!(md.md_keys & (1 << nkey)) && !force) {
1151 gctl_error(req, "Master Key %u is not set.", nkey);
1154 md.md_keys &= ~(1 << nkey);
1155 if (md.md_keys == 0 && !force) {
1156 gctl_error(req, "This is the last Master Key. Use '-f' "
1157 "option if you really want to remove it.");
1160 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN;
1161 arc4random_buf(mkeydst, G_ELI_MKEYLEN);
1164 eli_metadata_store(req, prov, &md);
1165 bzero(&md, sizeof(md));
1169 eli_delkey(struct gctl_req *req)
1174 nargs = gctl_get_int(req, "nargs");
1176 gctl_error(req, "Invalid number of arguments.");
1179 prov = gctl_get_ascii(req, "arg0");
1181 if (eli_is_attached(prov))
1182 eli_delkey_attached(req, prov);
1184 eli_delkey_detached(req, prov);
1188 eli_resume(struct gctl_req *req)
1190 struct g_eli_metadata md;
1191 unsigned char key[G_ELI_USERKEYLEN];
1196 nargs = gctl_get_int(req, "nargs");
1198 gctl_error(req, "Invalid number of arguments.");
1201 prov = gctl_get_ascii(req, "arg0");
1203 if (eli_metadata_read(req, prov, &md) == -1)
1206 mediasize = g_get_mediasize(prov);
1207 if (md.md_provsize != (uint64_t)mediasize) {
1208 gctl_error(req, "Provider size mismatch.");
1212 if (eli_genkey(req, &md, key, false) == NULL) {
1213 bzero(key, sizeof(key));
1217 gctl_ro_param(req, "key", sizeof(key), key);
1218 if (gctl_issue(req) == NULL) {
1220 printf("Resumed %s.\n", prov);
1222 bzero(key, sizeof(key));
1226 eli_trash_metadata(struct gctl_req *req, const char *prov, int fd, off_t offset)
1228 unsigned int overwrites;
1229 unsigned char *sector;
1233 size = sizeof(overwrites);
1234 if (sysctlbyname("kern.geom.eli.overwrites", &overwrites, &size,
1235 NULL, 0) == -1 || overwrites == 0) {
1236 overwrites = G_ELI_OVERWRITES;
1239 size = g_sectorsize(fd);
1241 gctl_error(req, "Cannot obtain provider sector size %s: %s.",
1242 prov, strerror(errno));
1245 sector = malloc(size);
1246 if (sector == NULL) {
1247 gctl_error(req, "Cannot allocate %zd bytes of memory.", size);
1253 arc4random_buf(sector, size);
1254 if (pwrite(fd, sector, size, offset) != size) {
1259 } while (--overwrites > 0);
1262 gctl_error(req, "Cannot trash metadata on provider %s: %s.",
1263 prov, strerror(error));
1270 eli_kill_detached(struct gctl_req *req, const char *prov)
1276 * NOTE: Maybe we should verify if this is geli provider first,
1277 * but 'kill' command is quite critical so better don't waste
1281 error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md),
1284 gctl_error(req, "Cannot read metadata from %s: %s.", prov,
1290 fd = g_open(prov, 1);
1292 gctl_error(req, "Cannot open provider %s: %s.", prov,
1296 offset = g_mediasize(fd) - g_sectorsize(fd);
1299 "Cannot obtain media size or sector size for provider %s: %s.",
1300 prov, strerror(errno));
1304 (void)eli_trash_metadata(req, prov, fd, offset);
1309 eli_kill(struct gctl_req *req)
1314 nargs = gctl_get_int(req, "nargs");
1315 all = gctl_get_int(req, "all");
1316 if (!all && nargs == 0) {
1317 gctl_error(req, "Too few arguments.");
1321 * How '-a' option combine with a list of providers:
1322 * Delete Master Keys from all attached providers:
1324 * Delete Master Keys from all attached providers and from
1325 * detached da0 and da1:
1326 * geli kill -a da0 da1
1327 * Delete Master Keys from (attached or detached) da0 and da1:
1331 /* First detached providers. */
1332 for (i = 0; i < nargs; i++) {
1333 prov = gctl_get_ascii(req, "arg%d", i);
1334 if (!eli_is_attached(prov))
1335 eli_kill_detached(req, prov);
1337 /* Now attached providers. */
1342 eli_backup_create(struct gctl_req *req, const char *prov, const char *file)
1344 unsigned char *sector;
1346 int error, filefd, ret;
1353 secsize = g_get_sectorsize(prov);
1355 gctl_error(req, "Cannot get informations about %s: %s.", prov,
1359 sector = malloc(secsize);
1360 if (sector == NULL) {
1361 gctl_error(req, "Cannot allocate memory.");
1364 /* Read metadata from the provider. */
1365 error = g_metadata_read(prov, sector, secsize, G_ELI_MAGIC);
1367 gctl_error(req, "Unable to read metadata from %s: %s.", prov,
1372 filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1374 gctl_error(req, "Unable to open %s: %s.", file,
1378 /* Write metadata to the destination file. */
1379 if (write(filefd, sector, secsize) != secsize) {
1380 gctl_error(req, "Unable to write to %s: %s.", file,
1382 (void)close(filefd);
1386 (void)fsync(filefd);
1387 (void)close(filefd);
1391 if (sector != NULL) {
1392 bzero(sector, secsize);
1399 eli_backup(struct gctl_req *req)
1401 const char *file, *prov;
1404 nargs = gctl_get_int(req, "nargs");
1406 gctl_error(req, "Invalid number of arguments.");
1409 prov = gctl_get_ascii(req, "arg0");
1410 file = gctl_get_ascii(req, "arg1");
1412 eli_backup_create(req, prov, file);
1416 eli_restore(struct gctl_req *req)
1418 struct g_eli_metadata md;
1419 const char *file, *prov;
1423 nargs = gctl_get_int(req, "nargs");
1425 gctl_error(req, "Invalid number of arguments.");
1428 file = gctl_get_ascii(req, "arg0");
1429 prov = gctl_get_ascii(req, "arg1");
1431 /* Read metadata from the backup file. */
1432 if (eli_metadata_read(req, file, &md) == -1)
1434 /* Obtain provider's mediasize. */
1435 mediasize = g_get_mediasize(prov);
1436 if (mediasize == 0) {
1437 gctl_error(req, "Cannot get informations about %s: %s.", prov,
1441 /* Check if the provider size has changed since we did the backup. */
1442 if (md.md_provsize != (uint64_t)mediasize) {
1443 if (gctl_get_int(req, "force")) {
1444 md.md_provsize = mediasize;
1446 gctl_error(req, "Provider size mismatch: "
1447 "wrong backup file?");
1451 /* Write metadata to the provider. */
1452 (void)eli_metadata_store(req, prov, &md);
1456 eli_resize(struct gctl_req *req)
1458 struct g_eli_metadata md;
1460 unsigned char *sector;
1462 off_t mediasize, oldsize;
1463 int error, nargs, provfd;
1465 nargs = gctl_get_int(req, "nargs");
1467 gctl_error(req, "Invalid number of arguments.");
1470 prov = gctl_get_ascii(req, "arg0");
1476 provfd = g_open(prov, 1);
1478 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno));
1482 mediasize = g_mediasize(provfd);
1483 secsize = g_sectorsize(provfd);
1484 if (mediasize == -1 || secsize == -1) {
1485 gctl_error(req, "Cannot get information about %s: %s.", prov,
1490 sector = malloc(secsize);
1491 if (sector == NULL) {
1492 gctl_error(req, "Cannot allocate memory.");
1496 oldsize = gctl_get_intmax(req, "oldsize");
1497 if (oldsize < 0 || oldsize > mediasize) {
1498 gctl_error(req, "Invalid oldsize: Out of range.");
1501 if (oldsize == mediasize) {
1502 gctl_error(req, "Size hasn't changed.");
1506 /* Read metadata from the 'oldsize' offset. */
1507 if (pread(provfd, sector, secsize, oldsize - secsize) != secsize) {
1508 gctl_error(req, "Cannot read old metadata: %s.",
1513 /* Check if this sector contains geli metadata. */
1514 error = eli_metadata_decode(sector, &md);
1520 "Provider's %s metadata version %u is too new.\n"
1521 "geli: The highest supported version is %u.",
1522 prov, (unsigned int)md.md_version, G_ELI_VERSION);
1525 gctl_error(req, "Inconsistent provider's %s metadata.", prov);
1529 "Unexpected error while decoding provider's %s metadata: %s.",
1530 prov, strerror(error));
1535 * If the old metadata doesn't have a correct provider size, refuse
1538 if (md.md_provsize != (uint64_t)oldsize) {
1539 gctl_error(req, "Provider size mismatch at oldsize.");
1544 * Update the old metadata with the current provider size and write
1545 * it back to the correct place on the provider.
1547 md.md_provsize = mediasize;
1548 /* Write metadata to the provider. */
1549 (void)eli_metadata_store(req, prov, &md);
1550 /* Now trash the old metadata. */
1551 (void)eli_trash_metadata(req, prov, provfd, oldsize - secsize);
1554 (void)g_close(provfd);
1555 if (sector != NULL) {
1556 bzero(sector, secsize);
1562 eli_version(struct gctl_req *req)
1564 struct g_eli_metadata md;
1566 unsigned int version;
1567 int error, i, nargs;
1569 nargs = gctl_get_int(req, "nargs");
1572 unsigned int kernver;
1575 size = sizeof(kernver);
1576 if (sysctlbyname("kern.geom.eli.version", &kernver, &size,
1578 warn("Unable to obtain GELI kernel version");
1580 printf("kernel: %u\n", kernver);
1582 printf("userland: %u\n", G_ELI_VERSION);
1586 for (i = 0; i < nargs; i++) {
1587 name = gctl_get_ascii(req, "arg%d", i);
1588 error = g_metadata_read(name, (unsigned char *)&md,
1589 sizeof(md), G_ELI_MAGIC);
1591 warn("%s: Unable to read metadata: %s.", name,
1593 gctl_error(req, "Not fully done.");
1596 version = le32dec(&md.md_version);
1597 printf("%s: %u\n", name, version);
1602 eli_clear(struct gctl_req *req)
1605 int error, i, nargs;
1607 nargs = gctl_get_int(req, "nargs");
1609 gctl_error(req, "Too few arguments.");
1613 for (i = 0; i < nargs; i++) {
1614 name = gctl_get_ascii(req, "arg%d", i);
1615 error = g_metadata_clear(name, G_ELI_MAGIC);
1617 fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
1618 name, strerror(error));
1619 gctl_error(req, "Not fully done.");
1623 printf("Metadata cleared on %s.\n", name);
1628 eli_dump(struct gctl_req *req)
1630 struct g_eli_metadata md;
1634 nargs = gctl_get_int(req, "nargs");
1636 gctl_error(req, "Too few arguments.");
1640 for (i = 0; i < nargs; i++) {
1641 name = gctl_get_ascii(req, "arg%d", i);
1642 if (eli_metadata_read(NULL, name, &md) == -1) {
1643 gctl_error(req, "Not fully done.");
1646 printf("Metadata on %s:\n", name);
1647 eli_metadata_dump(&md);