1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/mman.h> 34 #include <sys/sysctl.h> 35 #include <sys/resource.h> 36 #include <opencrypto/cryptodev.h> 37 38 #include <assert.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <libgeom.h> 43 #include <paths.h> 44 #include <readpassphrase.h> 45 #include <stdbool.h> 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <strings.h> 51 #include <unistd.h> 52 53 #include <geom/eli/g_eli.h> 54 #include <geom/eli/pkcs5v2.h> 55 56 #include "core/geom.h" 57 #include "misc/subr.h" 58 59 60 uint32_t lib_version = G_LIB_VERSION; 61 uint32_t version = G_ELI_VERSION; 62 63 #define GELI_BACKUP_DIR "/var/backups/" 64 #define GELI_ENC_ALGO "aes" 65 #define BUFSIZE 1024 66 67 /* 68 * Passphrase cached when attaching multiple providers, in order to be more 69 * user-friendly if they are using the same passphrase. 70 */ 71 static char cached_passphrase[BUFSIZE] = ""; 72 73 static void eli_main(struct gctl_req *req, unsigned flags); 74 static void eli_init(struct gctl_req *req); 75 static void eli_attach(struct gctl_req *req); 76 static void eli_configure(struct gctl_req *req); 77 static void eli_setkey(struct gctl_req *req); 78 static void eli_delkey(struct gctl_req *req); 79 static void eli_resume(struct gctl_req *req); 80 static void eli_kill(struct gctl_req *req); 81 static void eli_backup(struct gctl_req *req); 82 static void eli_restore(struct gctl_req *req); 83 static void eli_resize(struct gctl_req *req); 84 static void eli_version(struct gctl_req *req); 85 static void eli_clear(struct gctl_req *req); 86 static void eli_dump(struct gctl_req *req); 87 88 static int eli_backup_create(struct gctl_req *req, const char *prov, 89 const char *file); 90 91 /* 92 * Available commands: 93 * 94 * init [-bdgPTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov ... 95 * label - alias for 'init' 96 * attach [-Cdprv] [-n keyno] [-j passfile] [-k keyfile] prov ... 97 * detach [-fl] prov ... 98 * stop - alias for 'detach' 99 * onetime [-d] [-a aalgo] [-e ealgo] [-l keylen] prov 100 * configure [-bBgGtT] prov ... 101 * setkey [-pPv] [-n keyno] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov 102 * delkey [-afv] [-n keyno] prov 103 * suspend [-v] -a | prov ... 104 * resume [-pv] [-j passfile] [-k keyfile] prov 105 * kill [-av] [prov ...] 106 * backup [-v] prov file 107 * restore [-fv] file prov 108 * resize [-v] -s oldsize prov 109 * version [prov ...] 110 * clear [-v] prov ... 111 * dump [-v] prov ... 112 */ 113 struct g_command class_commands[] = { 114 { "init", G_FLAG_VERBOSE, eli_main, 115 { 116 { 'a', "aalgo", "", G_TYPE_STRING }, 117 { 'b', "boot", NULL, G_TYPE_BOOL }, 118 { 'B', "backupfile", "", G_TYPE_STRING }, 119 { 'd', "displaypass", NULL, G_TYPE_BOOL }, 120 { 'e', "ealgo", "", G_TYPE_STRING }, 121 { 'g', "geliboot", NULL, G_TYPE_BOOL }, 122 { 'i', "iterations", "-1", G_TYPE_NUMBER }, 123 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 124 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 125 { 'l', "keylen", "0", G_TYPE_NUMBER }, 126 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 127 { 's', "sectorsize", "0", G_TYPE_NUMBER }, 128 { 'T', "notrim", NULL, G_TYPE_BOOL }, 129 { 'V', "mdversion", "-1", G_TYPE_NUMBER }, 130 G_OPT_SENTINEL 131 }, 132 "[-bdgPTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov ..." 133 }, 134 { "label", G_FLAG_VERBOSE, eli_main, 135 { 136 { 'a', "aalgo", "", G_TYPE_STRING }, 137 { 'b', "boot", NULL, G_TYPE_BOOL }, 138 { 'B', "backupfile", "", G_TYPE_STRING }, 139 { 'd', "displaypass", NULL, G_TYPE_BOOL }, 140 { 'e', "ealgo", "", G_TYPE_STRING }, 141 { 'g', "geliboot", NULL, G_TYPE_BOOL }, 142 { 'i', "iterations", "-1", G_TYPE_NUMBER }, 143 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 144 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 145 { 'l', "keylen", "0", G_TYPE_NUMBER }, 146 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 147 { 's', "sectorsize", "0", G_TYPE_NUMBER }, 148 { 'V', "mdversion", "-1", G_TYPE_NUMBER }, 149 G_OPT_SENTINEL 150 }, 151 "- an alias for 'init'" 152 }, 153 { "attach", G_FLAG_VERBOSE | G_FLAG_LOADKLD, eli_main, 154 { 155 { 'C', "dryrun", NULL, G_TYPE_BOOL }, 156 { 'd', "detach", NULL, G_TYPE_BOOL }, 157 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 158 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 159 { 'n', "keyno", "-1", G_TYPE_NUMBER }, 160 { 'p', "nopassphrase", NULL, G_TYPE_BOOL }, 161 { 'r', "readonly", NULL, G_TYPE_BOOL }, 162 G_OPT_SENTINEL 163 }, 164 "[-Cdprv] [-n keyno] [-j passfile] [-k keyfile] prov ..." 165 }, 166 { "detach", 0, NULL, 167 { 168 { 'f', "force", NULL, G_TYPE_BOOL }, 169 { 'l', "last", NULL, G_TYPE_BOOL }, 170 G_OPT_SENTINEL 171 }, 172 "[-fl] prov ..." 173 }, 174 { "stop", 0, NULL, 175 { 176 { 'f', "force", NULL, G_TYPE_BOOL }, 177 { 'l', "last", NULL, G_TYPE_BOOL }, 178 G_OPT_SENTINEL 179 }, 180 "- an alias for 'detach'" 181 }, 182 { "onetime", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, 183 { 184 { 'a', "aalgo", "", G_TYPE_STRING }, 185 { 'd', "detach", NULL, G_TYPE_BOOL }, 186 { 'e', "ealgo", GELI_ENC_ALGO, G_TYPE_STRING }, 187 { 'l', "keylen", "0", G_TYPE_NUMBER }, 188 { 's', "sectorsize", "0", G_TYPE_NUMBER }, 189 { 'T', "notrim", NULL, G_TYPE_BOOL }, 190 G_OPT_SENTINEL 191 }, 192 "[-dT] [-a aalgo] [-e ealgo] [-l keylen] [-s sectorsize] prov" 193 }, 194 { "configure", G_FLAG_VERBOSE, eli_main, 195 { 196 { 'b', "boot", NULL, G_TYPE_BOOL }, 197 { 'B', "noboot", NULL, G_TYPE_BOOL }, 198 { 'd', "displaypass", NULL, G_TYPE_BOOL }, 199 { 'D', "nodisplaypass", NULL, G_TYPE_BOOL }, 200 { 'g', "geliboot", NULL, G_TYPE_BOOL }, 201 { 'G', "nogeliboot", NULL, G_TYPE_BOOL }, 202 { 't', "trim", NULL, G_TYPE_BOOL }, 203 { 'T', "notrim", NULL, G_TYPE_BOOL }, 204 G_OPT_SENTINEL 205 }, 206 "[-bBdDgGtT] prov ..." 207 }, 208 { "setkey", G_FLAG_VERBOSE, eli_main, 209 { 210 { 'i', "iterations", "-1", G_TYPE_NUMBER }, 211 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 212 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 213 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 214 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 215 { 'n', "keyno", "-1", G_TYPE_NUMBER }, 216 { 'p', "nopassphrase", NULL, G_TYPE_BOOL }, 217 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 218 G_OPT_SENTINEL 219 }, 220 "[-pPv] [-n keyno] [-i iterations] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov" 221 }, 222 { "delkey", G_FLAG_VERBOSE, eli_main, 223 { 224 { 'a', "all", NULL, G_TYPE_BOOL }, 225 { 'f', "force", NULL, G_TYPE_BOOL }, 226 { 'n', "keyno", "-1", G_TYPE_NUMBER }, 227 G_OPT_SENTINEL 228 }, 229 "[-afv] [-n keyno] prov" 230 }, 231 { "suspend", G_FLAG_VERBOSE, NULL, 232 { 233 { 'a', "all", NULL, G_TYPE_BOOL }, 234 G_OPT_SENTINEL 235 }, 236 "[-v] -a | prov ..." 237 }, 238 { "resume", G_FLAG_VERBOSE, eli_main, 239 { 240 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 241 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 242 { 'p', "nopassphrase", NULL, G_TYPE_BOOL }, 243 G_OPT_SENTINEL 244 }, 245 "[-pv] [-j passfile] [-k keyfile] prov" 246 }, 247 { "kill", G_FLAG_VERBOSE, eli_main, 248 { 249 { 'a', "all", NULL, G_TYPE_BOOL }, 250 G_OPT_SENTINEL 251 }, 252 "[-av] [prov ...]" 253 }, 254 { "backup", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 255 "[-v] prov file" 256 }, 257 { "restore", G_FLAG_VERBOSE, eli_main, 258 { 259 { 'f', "force", NULL, G_TYPE_BOOL }, 260 G_OPT_SENTINEL 261 }, 262 "[-fv] file prov" 263 }, 264 { "resize", G_FLAG_VERBOSE, eli_main, 265 { 266 { 's', "oldsize", NULL, G_TYPE_NUMBER }, 267 G_OPT_SENTINEL 268 }, 269 "[-v] -s oldsize prov" 270 }, 271 { "version", G_FLAG_LOADKLD, eli_main, G_NULL_OPTS, 272 "[prov ...]" 273 }, 274 { "clear", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 275 "[-v] prov ..." 276 }, 277 { "dump", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 278 "[-v] prov ..." 279 }, 280 G_CMD_SENTINEL 281 }; 282 283 static int verbose = 0; 284 285 static int 286 eli_protect(struct gctl_req *req) 287 { 288 struct rlimit rl; 289 290 /* Disable core dumps. */ 291 rl.rlim_cur = 0; 292 rl.rlim_max = 0; 293 if (setrlimit(RLIMIT_CORE, &rl) == -1) { 294 gctl_error(req, "Cannot disable core dumps: %s.", 295 strerror(errno)); 296 return (-1); 297 } 298 /* Disable swapping. */ 299 if (mlockall(MCL_FUTURE) == -1) { 300 gctl_error(req, "Cannot lock memory: %s.", strerror(errno)); 301 return (-1); 302 } 303 return (0); 304 } 305 306 static void 307 eli_main(struct gctl_req *req, unsigned int flags) 308 { 309 const char *name; 310 311 if (eli_protect(req) == -1) 312 return; 313 314 if ((flags & G_FLAG_VERBOSE) != 0) 315 verbose = 1; 316 317 name = gctl_get_ascii(req, "verb"); 318 if (name == NULL) { 319 gctl_error(req, "No '%s' argument.", "verb"); 320 return; 321 } 322 if (strcmp(name, "init") == 0 || strcmp(name, "label") == 0) 323 eli_init(req); 324 else if (strcmp(name, "attach") == 0) 325 eli_attach(req); 326 else if (strcmp(name, "configure") == 0) 327 eli_configure(req); 328 else if (strcmp(name, "setkey") == 0) 329 eli_setkey(req); 330 else if (strcmp(name, "delkey") == 0) 331 eli_delkey(req); 332 else if (strcmp(name, "resume") == 0) 333 eli_resume(req); 334 else if (strcmp(name, "kill") == 0) 335 eli_kill(req); 336 else if (strcmp(name, "backup") == 0) 337 eli_backup(req); 338 else if (strcmp(name, "restore") == 0) 339 eli_restore(req); 340 else if (strcmp(name, "resize") == 0) 341 eli_resize(req); 342 else if (strcmp(name, "version") == 0) 343 eli_version(req); 344 else if (strcmp(name, "dump") == 0) 345 eli_dump(req); 346 else if (strcmp(name, "clear") == 0) 347 eli_clear(req); 348 else 349 gctl_error(req, "Unknown command: %s.", name); 350 } 351 352 static bool 353 eli_is_attached(const char *prov) 354 { 355 char name[MAXPATHLEN]; 356 357 /* 358 * Not the best way to do it, but the easiest. 359 * We try to open provider and check if it is a GEOM provider 360 * by asking about its sectorsize. 361 */ 362 snprintf(name, sizeof(name), "%s%s", prov, G_ELI_SUFFIX); 363 return (g_get_sectorsize(name) > 0); 364 } 365 366 static int 367 eli_genkey_files(struct gctl_req *req, bool new, const char *type, 368 struct hmac_ctx *ctxp, char *passbuf, size_t passbufsize) 369 { 370 char *p, buf[BUFSIZE], argname[16]; 371 const char *file; 372 int error, fd, i; 373 ssize_t done; 374 375 assert((strcmp(type, "keyfile") == 0 && ctxp != NULL && 376 passbuf == NULL && passbufsize == 0) || 377 (strcmp(type, "passfile") == 0 && ctxp == NULL && 378 passbuf != NULL && passbufsize > 0)); 379 assert(strcmp(type, "keyfile") == 0 || passbuf[0] == '\0'); 380 381 for (i = 0; ; i++) { 382 snprintf(argname, sizeof(argname), "%s%s%d", 383 new ? "new" : "", type, i); 384 385 /* No more {key,pass}files? */ 386 if (!gctl_has_param(req, argname)) 387 return (i); 388 389 file = gctl_get_ascii(req, "%s", argname); 390 assert(file != NULL); 391 392 if (strcmp(file, "-") == 0) 393 fd = STDIN_FILENO; 394 else { 395 fd = open(file, O_RDONLY); 396 if (fd == -1) { 397 gctl_error(req, "Cannot open %s %s: %s.", 398 type, file, strerror(errno)); 399 return (-1); 400 } 401 } 402 if (strcmp(type, "keyfile") == 0) { 403 while ((done = read(fd, buf, sizeof(buf))) > 0) 404 g_eli_crypto_hmac_update(ctxp, buf, done); 405 } else /* if (strcmp(type, "passfile") == 0) */ { 406 assert(strcmp(type, "passfile") == 0); 407 408 while ((done = read(fd, buf, sizeof(buf) - 1)) > 0) { 409 buf[done] = '\0'; 410 p = strchr(buf, '\n'); 411 if (p != NULL) { 412 *p = '\0'; 413 done = p - buf; 414 } 415 if (strlcat(passbuf, buf, passbufsize) >= 416 passbufsize) { 417 gctl_error(req, 418 "Passphrase in %s too long.", file); 419 explicit_bzero(buf, sizeof(buf)); 420 return (-1); 421 } 422 if (p != NULL) 423 break; 424 } 425 } 426 error = errno; 427 if (strcmp(file, "-") != 0) 428 close(fd); 429 explicit_bzero(buf, sizeof(buf)); 430 if (done == -1) { 431 gctl_error(req, "Cannot read %s %s: %s.", 432 type, file, strerror(error)); 433 return (-1); 434 } 435 } 436 /* NOTREACHED */ 437 } 438 439 static int 440 eli_genkey_passphrase_prompt(struct gctl_req *req, bool new, char *passbuf, 441 size_t passbufsize) 442 { 443 char *p; 444 445 for (;;) { 446 p = readpassphrase( 447 new ? "Enter new passphrase: " : "Enter passphrase: ", 448 passbuf, passbufsize, RPP_ECHO_OFF | RPP_REQUIRE_TTY); 449 if (p == NULL) { 450 explicit_bzero(passbuf, passbufsize); 451 gctl_error(req, "Cannot read passphrase: %s.", 452 strerror(errno)); 453 return (-1); 454 } 455 456 if (new) { 457 char tmpbuf[BUFSIZE]; 458 459 p = readpassphrase("Reenter new passphrase: ", 460 tmpbuf, sizeof(tmpbuf), 461 RPP_ECHO_OFF | RPP_REQUIRE_TTY); 462 if (p == NULL) { 463 explicit_bzero(passbuf, passbufsize); 464 gctl_error(req, 465 "Cannot read passphrase: %s.", 466 strerror(errno)); 467 return (-1); 468 } 469 470 if (strcmp(passbuf, tmpbuf) != 0) { 471 explicit_bzero(passbuf, passbufsize); 472 fprintf(stderr, "They didn't match.\n"); 473 continue; 474 } 475 explicit_bzero(tmpbuf, sizeof(tmpbuf)); 476 } 477 return (0); 478 } 479 /* NOTREACHED */ 480 } 481 482 static int 483 eli_genkey_passphrase(struct gctl_req *req, struct g_eli_metadata *md, bool new, 484 struct hmac_ctx *ctxp) 485 { 486 char passbuf[BUFSIZE]; 487 bool nopassphrase; 488 int nfiles; 489 490 /* 491 * Return error if the 'do not use passphrase' flag was given but a 492 * passfile was provided. 493 */ 494 nopassphrase = 495 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase"); 496 if (nopassphrase) { 497 if (gctl_has_param(req, new ? "newpassfile0" : "passfile0")) { 498 gctl_error(req, 499 "Options -%c and -%c are mutually exclusive.", 500 new ? 'J' : 'j', new ? 'P' : 'p'); 501 return (-1); 502 } 503 return (0); 504 } 505 506 /* 507 * Return error if using a provider which does not require a passphrase 508 * but the 'do not use passphrase' flag was not given. 509 */ 510 if (!new && md->md_iterations == -1) { 511 gctl_error(req, "Missing -p flag."); 512 return (-1); 513 } 514 passbuf[0] = '\0'; 515 516 /* Use cached passphrase if defined. */ 517 if (strlen(cached_passphrase) > 0) { 518 strlcpy(passbuf, cached_passphrase, sizeof(passbuf)); 519 } else { 520 nfiles = eli_genkey_files(req, new, "passfile", NULL, passbuf, 521 sizeof(passbuf)); 522 if (nfiles == -1) { 523 return (-1); 524 } else if (nfiles == 0) { 525 if (eli_genkey_passphrase_prompt(req, new, passbuf, 526 sizeof(passbuf)) == -1) { 527 return (-1); 528 } 529 } 530 /* Cache the passphrase for other providers. */ 531 strlcpy(cached_passphrase, passbuf, sizeof(cached_passphrase)); 532 } 533 /* 534 * Field md_iterations equal to -1 means "choose some sane 535 * value for me". 536 */ 537 if (md->md_iterations == -1) { 538 assert(new); 539 if (verbose) 540 printf("Calculating number of iterations...\n"); 541 md->md_iterations = pkcs5v2_calculate(2000000); 542 assert(md->md_iterations > 0); 543 if (verbose) { 544 printf("Done, using %d iterations.\n", 545 md->md_iterations); 546 } 547 } 548 /* 549 * If md_iterations is equal to 0, user doesn't want PKCS#5v2. 550 */ 551 if (md->md_iterations == 0) { 552 g_eli_crypto_hmac_update(ctxp, md->md_salt, 553 sizeof(md->md_salt)); 554 g_eli_crypto_hmac_update(ctxp, passbuf, strlen(passbuf)); 555 } else /* if (md->md_iterations > 0) */ { 556 unsigned char dkey[G_ELI_USERKEYLEN]; 557 558 pkcs5v2_genkey(dkey, sizeof(dkey), md->md_salt, 559 sizeof(md->md_salt), passbuf, md->md_iterations); 560 g_eli_crypto_hmac_update(ctxp, dkey, sizeof(dkey)); 561 explicit_bzero(dkey, sizeof(dkey)); 562 } 563 explicit_bzero(passbuf, sizeof(passbuf)); 564 565 return (0); 566 } 567 568 static unsigned char * 569 eli_genkey(struct gctl_req *req, struct g_eli_metadata *md, unsigned char *key, 570 bool new) 571 { 572 struct hmac_ctx ctx; 573 bool nopassphrase; 574 int nfiles; 575 576 nopassphrase = 577 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase"); 578 579 g_eli_crypto_hmac_init(&ctx, NULL, 0); 580 581 nfiles = eli_genkey_files(req, new, "keyfile", &ctx, NULL, 0); 582 if (nfiles == -1) 583 return (NULL); 584 else if (nfiles == 0 && nopassphrase) { 585 gctl_error(req, "No key components given."); 586 return (NULL); 587 } 588 589 if (eli_genkey_passphrase(req, md, new, &ctx) == -1) 590 return (NULL); 591 592 g_eli_crypto_hmac_final(&ctx, key, 0); 593 594 return (key); 595 } 596 597 static int 598 eli_metadata_read(struct gctl_req *req, const char *prov, 599 struct g_eli_metadata *md) 600 { 601 unsigned char sector[sizeof(struct g_eli_metadata)]; 602 int error; 603 604 if (g_get_sectorsize(prov) == 0) { 605 int fd; 606 607 /* This is a file probably. */ 608 fd = open(prov, O_RDONLY); 609 if (fd == -1) { 610 gctl_error(req, "Cannot open %s: %s.", prov, 611 strerror(errno)); 612 return (-1); 613 } 614 if (read(fd, sector, sizeof(sector)) != sizeof(sector)) { 615 gctl_error(req, "Cannot read metadata from %s: %s.", 616 prov, strerror(errno)); 617 close(fd); 618 return (-1); 619 } 620 close(fd); 621 } else { 622 /* This is a GEOM provider. */ 623 error = g_metadata_read(prov, sector, sizeof(sector), 624 G_ELI_MAGIC); 625 if (error != 0) { 626 gctl_error(req, "Cannot read metadata from %s: %s.", 627 prov, strerror(error)); 628 return (-1); 629 } 630 } 631 error = eli_metadata_decode(sector, md); 632 switch (error) { 633 case 0: 634 break; 635 case EOPNOTSUPP: 636 gctl_error(req, 637 "Provider's %s metadata version %u is too new.\n" 638 "geli: The highest supported version is %u.", 639 prov, (unsigned int)md->md_version, G_ELI_VERSION); 640 return (-1); 641 case EINVAL: 642 gctl_error(req, "Inconsistent provider's %s metadata.", prov); 643 return (-1); 644 default: 645 gctl_error(req, 646 "Unexpected error while decoding provider's %s metadata: %s.", 647 prov, strerror(error)); 648 return (-1); 649 } 650 return (0); 651 } 652 653 static int 654 eli_metadata_store(struct gctl_req *req, const char *prov, 655 struct g_eli_metadata *md) 656 { 657 unsigned char sector[sizeof(struct g_eli_metadata)]; 658 int error; 659 660 eli_metadata_encode(md, sector); 661 if (g_get_sectorsize(prov) == 0) { 662 int fd; 663 664 /* This is a file probably. */ 665 fd = open(prov, O_WRONLY | O_TRUNC); 666 if (fd == -1) { 667 gctl_error(req, "Cannot open %s: %s.", prov, 668 strerror(errno)); 669 explicit_bzero(sector, sizeof(sector)); 670 return (-1); 671 } 672 if (write(fd, sector, sizeof(sector)) != sizeof(sector)) { 673 gctl_error(req, "Cannot write metadata to %s: %s.", 674 prov, strerror(errno)); 675 explicit_bzero(sector, sizeof(sector)); 676 close(fd); 677 return (-1); 678 } 679 close(fd); 680 } else { 681 /* This is a GEOM provider. */ 682 error = g_metadata_store(prov, sector, sizeof(sector)); 683 if (error != 0) { 684 gctl_error(req, "Cannot write metadata to %s: %s.", 685 prov, strerror(errno)); 686 explicit_bzero(sector, sizeof(sector)); 687 return (-1); 688 } 689 } 690 explicit_bzero(sector, sizeof(sector)); 691 return (0); 692 } 693 694 static void 695 eli_init(struct gctl_req *req) 696 { 697 struct g_eli_metadata md; 698 struct gctl_req *r; 699 unsigned char sector[sizeof(struct g_eli_metadata)] __aligned(4); 700 unsigned char key[G_ELI_USERKEYLEN]; 701 char backfile[MAXPATHLEN]; 702 const char *str, *prov; 703 unsigned int secsize, version; 704 off_t mediasize; 705 intmax_t val; 706 int error, i, nargs, nparams, param; 707 const int one = 1; 708 709 nargs = gctl_get_int(req, "nargs"); 710 if (nargs <= 0) { 711 gctl_error(req, "Too few arguments."); 712 return; 713 } 714 715 /* Start generating metadata for provider(s) being initialized. */ 716 explicit_bzero(&md, sizeof(md)); 717 strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic)); 718 val = gctl_get_intmax(req, "mdversion"); 719 if (val == -1) { 720 version = G_ELI_VERSION; 721 } else if (val < 0 || val > G_ELI_VERSION) { 722 gctl_error(req, 723 "Invalid version specified should be between %u and %u.", 724 G_ELI_VERSION_00, G_ELI_VERSION); 725 return; 726 } else { 727 version = val; 728 } 729 md.md_version = version; 730 md.md_flags = 0; 731 if (gctl_get_int(req, "boot")) 732 md.md_flags |= G_ELI_FLAG_BOOT; 733 if (gctl_get_int(req, "geliboot")) 734 md.md_flags |= G_ELI_FLAG_GELIBOOT; 735 if (gctl_get_int(req, "displaypass")) 736 md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS; 737 if (gctl_get_int(req, "notrim")) 738 md.md_flags |= G_ELI_FLAG_NODELETE; 739 md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1; 740 str = gctl_get_ascii(req, "aalgo"); 741 if (*str != '\0') { 742 if (version < G_ELI_VERSION_01) { 743 gctl_error(req, 744 "Data authentication is supported starting from version %u.", 745 G_ELI_VERSION_01); 746 return; 747 } 748 md.md_aalgo = g_eli_str2aalgo(str); 749 if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN && 750 md.md_aalgo <= CRYPTO_ALGORITHM_MAX) { 751 md.md_flags |= G_ELI_FLAG_AUTH; 752 } else { 753 /* 754 * For backward compatibility, check if the -a option 755 * was used to provide encryption algorithm. 756 */ 757 md.md_ealgo = g_eli_str2ealgo(str); 758 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 759 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 760 gctl_error(req, 761 "Invalid authentication algorithm."); 762 return; 763 } else { 764 fprintf(stderr, "warning: The -e option, not " 765 "the -a option is now used to specify " 766 "encryption algorithm to use.\n"); 767 } 768 } 769 } 770 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 771 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 772 str = gctl_get_ascii(req, "ealgo"); 773 if (*str == '\0') { 774 if (version < G_ELI_VERSION_05) 775 str = "aes-cbc"; 776 else 777 str = GELI_ENC_ALGO; 778 } 779 md.md_ealgo = g_eli_str2ealgo(str); 780 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 781 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 782 gctl_error(req, "Invalid encryption algorithm."); 783 return; 784 } 785 if (md.md_ealgo == CRYPTO_CAMELLIA_CBC && 786 version < G_ELI_VERSION_04) { 787 gctl_error(req, 788 "Camellia-CBC algorithm is supported starting from version %u.", 789 G_ELI_VERSION_04); 790 return; 791 } 792 if (md.md_ealgo == CRYPTO_AES_XTS && 793 version < G_ELI_VERSION_05) { 794 gctl_error(req, 795 "AES-XTS algorithm is supported starting from version %u.", 796 G_ELI_VERSION_05); 797 return; 798 } 799 } 800 val = gctl_get_intmax(req, "keylen"); 801 md.md_keylen = val; 802 md.md_keylen = g_eli_keylen(md.md_ealgo, md.md_keylen); 803 if (md.md_keylen == 0) { 804 gctl_error(req, "Invalid key length."); 805 return; 806 } 807 808 val = gctl_get_intmax(req, "iterations"); 809 if (val != -1) { 810 int nonewpassphrase; 811 812 /* 813 * Don't allow to set iterations when there will be no 814 * passphrase. 815 */ 816 nonewpassphrase = gctl_get_int(req, "nonewpassphrase"); 817 if (nonewpassphrase) { 818 gctl_error(req, 819 "Options -i and -P are mutually exclusive."); 820 return; 821 } 822 } 823 md.md_iterations = val; 824 825 val = gctl_get_intmax(req, "sectorsize"); 826 if (val > sysconf(_SC_PAGE_SIZE)) { 827 fprintf(stderr, 828 "warning: Using sectorsize bigger than the page size!\n"); 829 } 830 831 md.md_keys = 0x01; 832 833 /* 834 * Determine number of parameters in the parent geom request before the 835 * nargs parameter and list of providers. 836 */ 837 nparams = req->narg - nargs - 1; 838 839 /* Create new child request for each provider and issue to kernel */ 840 for (i = 0; i < nargs; i++) { 841 r = gctl_get_handle(); 842 843 /* Copy each parameter from the parent request to the child */ 844 for (param = 0; param < nparams; param++) { 845 gctl_ro_param(r, req->arg[param].name, 846 req->arg[param].len, req->arg[param].value); 847 } 848 849 /* Add a single provider to the parameter list of the child */ 850 gctl_ro_param(r, "nargs", sizeof(one), &one); 851 prov = gctl_get_ascii(req, "arg%d", i); 852 gctl_ro_param(r, "arg0", -1, prov); 853 854 mediasize = g_get_mediasize(prov); 855 secsize = g_get_sectorsize(prov); 856 if (mediasize == 0 || secsize == 0) { 857 gctl_error(r, "Cannot get information about %s: %s.", 858 prov, strerror(errno)); 859 goto out; 860 } 861 862 md.md_provsize = mediasize; 863 864 val = gctl_get_intmax(r, "sectorsize"); 865 if (val == 0) { 866 md.md_sectorsize = secsize; 867 } else { 868 if (val < 0 || (val % secsize) != 0 || !powerof2(val)) { 869 gctl_error(r, "Invalid sector size."); 870 goto out; 871 } 872 md.md_sectorsize = val; 873 } 874 875 /* Use different salt and Master Key for each provider. */ 876 arc4random_buf(md.md_salt, sizeof(md.md_salt)); 877 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys)); 878 879 /* Generate user key. */ 880 if (eli_genkey(r, &md, key, true) == NULL) { 881 /* 882 * Error generating key - details added to geom request 883 * by eli_genkey(). 884 */ 885 goto out; 886 } 887 888 /* Encrypt the first and the only Master Key. */ 889 error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen, 890 md.md_mkeys); 891 if (error != 0) { 892 gctl_error(r, "Cannot encrypt Master Key: %s.", 893 strerror(error)); 894 goto out; 895 } 896 897 /* Convert metadata to on-disk format. */ 898 eli_metadata_encode(&md, sector); 899 900 /* Store metadata to disk. */ 901 error = g_metadata_store(prov, sector, sizeof(sector)); 902 if (error != 0) { 903 gctl_error(r, "Cannot store metadata on %s: %s.", prov, 904 strerror(error)); 905 goto out; 906 } 907 if (verbose) 908 printf("Metadata value stored on %s.\n", prov); 909 910 /* Backup metadata to a file. */ 911 const char *p = prov; 912 unsigned int j; 913 914 /* 915 * Check if provider string includes the devfs mountpoint 916 * (typically /dev/). 917 */ 918 if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) { 919 /* Skip forward to the device filename only. */ 920 p += sizeof(_PATH_DEV) - 1; 921 } 922 923 str = gctl_get_ascii(r, "backupfile"); 924 if (str[0] != '\0') { 925 /* Backupfile given by the user, just copy it. */ 926 strlcpy(backfile, str, sizeof(backfile)); 927 928 /* If multiple providers have been initialized in one 929 * command, and the backup filename has been specified 930 * as anything other than "none", make the backup 931 * filename unique for each provider. */ 932 if (nargs > 1 && strcmp(backfile, "none") != 0) { 933 /* 934 * Replace first occurrence of "PROV" with 935 * provider name. 936 */ 937 str = strnstr(backfile, "PROV", 938 sizeof(backfile)); 939 if (str != NULL) { 940 char suffix[MAXPATHLEN]; 941 j = str - backfile; 942 strlcpy(suffix, &backfile[j+4], 943 sizeof(suffix)); 944 backfile[j] = '\0'; 945 strlcat(backfile, p, sizeof(backfile)); 946 strlcat(backfile, suffix, 947 sizeof(backfile)); 948 } else { 949 /* 950 * "PROV" not found in backfile, append 951 * provider name. 952 */ 953 strlcat(backfile, "-", 954 sizeof(backfile)); 955 strlcat(backfile, p, sizeof(backfile)); 956 } 957 } 958 } else { 959 /* Generate filename automatically. */ 960 snprintf(backfile, sizeof(backfile), "%s%s.eli", 961 GELI_BACKUP_DIR, p); 962 /* Replace all / with _. */ 963 for (j = strlen(GELI_BACKUP_DIR); backfile[j] != '\0'; 964 j++) { 965 if (backfile[j] == '/') 966 backfile[j] = '_'; 967 } 968 } 969 if (strcmp(backfile, "none") != 0 && 970 eli_backup_create(r, prov, backfile) == 0) { 971 printf("\nMetadata backup for provider %s can be found " 972 "in %s\n", prov, backfile); 973 printf("and can be restored with the following " 974 "command:\n"); 975 printf("\n\t# geli restore %s %s\n\n", backfile, prov); 976 } 977 978 out: 979 /* 980 * Print error for this request, and set parent request error 981 * message. 982 */ 983 if (r->error != NULL && r->error[0] != '\0') { 984 warnx("%s", r->error); 985 gctl_error(req, "There was an error with at least one " 986 "provider."); 987 } 988 989 gctl_free(r); 990 991 /* 992 * Erase sensitive and provider specific data from memory. 993 */ 994 explicit_bzero(key, sizeof(key)); 995 explicit_bzero(sector, sizeof(sector)); 996 explicit_bzero(&md.md_provsize, sizeof(md.md_provsize)); 997 explicit_bzero(&md.md_sectorsize, sizeof(md.md_sectorsize)); 998 explicit_bzero(&md.md_salt, sizeof(md.md_salt)); 999 explicit_bzero(&md.md_mkeys, sizeof(md.md_mkeys)); 1000 } 1001 1002 /* Clear the cached metadata, including keys. */ 1003 explicit_bzero(&md, sizeof(md)); 1004 } 1005 1006 static void 1007 eli_attach(struct gctl_req *req) 1008 { 1009 struct g_eli_metadata md; 1010 struct gctl_req *r; 1011 const char *prov; 1012 off_t mediasize; 1013 int i, nargs, nparams, param; 1014 const int one = 1; 1015 1016 nargs = gctl_get_int(req, "nargs"); 1017 if (nargs <= 0) { 1018 gctl_error(req, "Too few arguments."); 1019 return; 1020 } 1021 1022 unsigned char key[G_ELI_USERKEYLEN]; 1023 1024 /* 1025 * Determine number of parameters in the parent geom request before the 1026 * nargs parameter and list of providers. 1027 */ 1028 nparams = req->narg - nargs - 1; 1029 1030 /* Create new child request for each provider and issue to kernel */ 1031 for (i = 0; i < nargs; i++) { 1032 r = gctl_get_handle(); 1033 1034 /* Copy each parameter from the parent request to the child */ 1035 for (param = 0; param < nparams; param++) { 1036 gctl_ro_param(r, req->arg[param].name, 1037 req->arg[param].len, req->arg[param].value); 1038 } 1039 1040 /* Add a single provider to the parameter list of the child */ 1041 gctl_ro_param(r, "nargs", sizeof(one), &one); 1042 prov = gctl_get_ascii(req, "arg%d", i); 1043 gctl_ro_param(r, "arg0", -1, prov); 1044 1045 if (eli_metadata_read(r, prov, &md) == -1) { 1046 /* 1047 * Error reading metadata - details added to geom 1048 * request by eli_metadata_read(). 1049 */ 1050 goto out; 1051 } 1052 1053 mediasize = g_get_mediasize(prov); 1054 if (md.md_provsize != (uint64_t)mediasize) { 1055 gctl_error(r, "Provider size mismatch."); 1056 goto out; 1057 } 1058 1059 if (eli_genkey(r, &md, key, false) == NULL) { 1060 /* 1061 * Error generating key - details added to geom request 1062 * by eli_genkey(). 1063 */ 1064 goto out; 1065 } 1066 1067 gctl_ro_param(r, "key", sizeof(key), key); 1068 1069 if (gctl_issue(r) == NULL) { 1070 if (verbose) 1071 printf("Attached to %s.\n", prov); 1072 } 1073 1074 out: 1075 /* 1076 * Print error for this request, and set parent request error 1077 * message. 1078 */ 1079 if (r->error != NULL && r->error[0] != '\0') { 1080 warnx("%s", r->error); 1081 gctl_error(req, "There was an error with at least one " 1082 "provider."); 1083 } 1084 1085 gctl_free(r); 1086 1087 /* Clear sensitive data from memory. */ 1088 explicit_bzero(key, sizeof(key)); 1089 } 1090 1091 /* Clear sensitive data from memory. */ 1092 explicit_bzero(cached_passphrase, sizeof(cached_passphrase)); 1093 } 1094 1095 static void 1096 eli_configure_detached(struct gctl_req *req, const char *prov, int boot, 1097 int geliboot, int displaypass, int trim) 1098 { 1099 struct g_eli_metadata md; 1100 bool changed = 0; 1101 1102 if (eli_metadata_read(req, prov, &md) == -1) 1103 return; 1104 1105 if (boot == 1 && (md.md_flags & G_ELI_FLAG_BOOT)) { 1106 if (verbose) 1107 printf("BOOT flag already configured for %s.\n", prov); 1108 } else if (boot == 0 && !(md.md_flags & G_ELI_FLAG_BOOT)) { 1109 if (verbose) 1110 printf("BOOT flag not configured for %s.\n", prov); 1111 } else if (boot >= 0) { 1112 if (boot) 1113 md.md_flags |= G_ELI_FLAG_BOOT; 1114 else 1115 md.md_flags &= ~G_ELI_FLAG_BOOT; 1116 changed = 1; 1117 } 1118 1119 if (geliboot == 1 && (md.md_flags & G_ELI_FLAG_GELIBOOT)) { 1120 if (verbose) 1121 printf("GELIBOOT flag already configured for %s.\n", prov); 1122 } else if (geliboot == 0 && !(md.md_flags & G_ELI_FLAG_GELIBOOT)) { 1123 if (verbose) 1124 printf("GELIBOOT flag not configured for %s.\n", prov); 1125 } else if (geliboot >= 0) { 1126 if (geliboot) 1127 md.md_flags |= G_ELI_FLAG_GELIBOOT; 1128 else 1129 md.md_flags &= ~G_ELI_FLAG_GELIBOOT; 1130 changed = 1; 1131 } 1132 1133 if (displaypass == 1 && (md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) { 1134 if (verbose) 1135 printf("GELIDISPLAYPASS flag already configured for %s.\n", prov); 1136 } else if (displaypass == 0 && 1137 !(md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) { 1138 if (verbose) 1139 printf("GELIDISPLAYPASS flag not configured for %s.\n", prov); 1140 } else if (displaypass >= 0) { 1141 if (displaypass) 1142 md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS; 1143 else 1144 md.md_flags &= ~G_ELI_FLAG_GELIDISPLAYPASS; 1145 changed = 1; 1146 } 1147 1148 if (trim == 0 && (md.md_flags & G_ELI_FLAG_NODELETE)) { 1149 if (verbose) 1150 printf("TRIM disable flag already configured for %s.\n", prov); 1151 } else if (trim == 1 && !(md.md_flags & G_ELI_FLAG_NODELETE)) { 1152 if (verbose) 1153 printf("TRIM disable flag not configured for %s.\n", prov); 1154 } else if (trim >= 0) { 1155 if (trim) 1156 md.md_flags &= ~G_ELI_FLAG_NODELETE; 1157 else 1158 md.md_flags |= G_ELI_FLAG_NODELETE; 1159 changed = 1; 1160 } 1161 1162 if (changed) 1163 eli_metadata_store(req, prov, &md); 1164 explicit_bzero(&md, sizeof(md)); 1165 } 1166 1167 static void 1168 eli_configure(struct gctl_req *req) 1169 { 1170 const char *prov; 1171 bool boot, noboot, geliboot, nogeliboot, displaypass, nodisplaypass; 1172 bool trim, notrim; 1173 int doboot, dogeliboot, dodisplaypass, dotrim; 1174 int i, nargs; 1175 1176 nargs = gctl_get_int(req, "nargs"); 1177 if (nargs == 0) { 1178 gctl_error(req, "Too few arguments."); 1179 return; 1180 } 1181 1182 boot = gctl_get_int(req, "boot"); 1183 noboot = gctl_get_int(req, "noboot"); 1184 geliboot = gctl_get_int(req, "geliboot"); 1185 nogeliboot = gctl_get_int(req, "nogeliboot"); 1186 displaypass = gctl_get_int(req, "displaypass"); 1187 nodisplaypass = gctl_get_int(req, "nodisplaypass"); 1188 trim = gctl_get_int(req, "trim"); 1189 notrim = gctl_get_int(req, "notrim"); 1190 1191 doboot = -1; 1192 if (boot && noboot) { 1193 gctl_error(req, "Options -b and -B are mutually exclusive."); 1194 return; 1195 } 1196 if (boot) 1197 doboot = 1; 1198 else if (noboot) 1199 doboot = 0; 1200 1201 dogeliboot = -1; 1202 if (geliboot && nogeliboot) { 1203 gctl_error(req, "Options -g and -G are mutually exclusive."); 1204 return; 1205 } 1206 if (geliboot) 1207 dogeliboot = 1; 1208 else if (nogeliboot) 1209 dogeliboot = 0; 1210 1211 dodisplaypass = -1; 1212 if (displaypass && nodisplaypass) { 1213 gctl_error(req, "Options -d and -D are mutually exclusive."); 1214 return; 1215 } 1216 if (displaypass) 1217 dodisplaypass = 1; 1218 else if (nodisplaypass) 1219 dodisplaypass = 0; 1220 1221 dotrim = -1; 1222 if (trim && notrim) { 1223 gctl_error(req, "Options -t and -T are mutually exclusive."); 1224 return; 1225 } 1226 if (trim) 1227 dotrim = 1; 1228 else if (notrim) 1229 dotrim = 0; 1230 1231 if (doboot == -1 && dogeliboot == -1 && dodisplaypass == -1 && 1232 dotrim == -1) { 1233 gctl_error(req, "No option given."); 1234 return; 1235 } 1236 1237 /* First attached providers. */ 1238 gctl_issue(req); 1239 /* Now the rest. */ 1240 for (i = 0; i < nargs; i++) { 1241 prov = gctl_get_ascii(req, "arg%d", i); 1242 if (!eli_is_attached(prov)) { 1243 eli_configure_detached(req, prov, doboot, dogeliboot, 1244 dodisplaypass, dotrim); 1245 } 1246 } 1247 } 1248 1249 static void 1250 eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md) 1251 { 1252 unsigned char key[G_ELI_USERKEYLEN]; 1253 intmax_t val, old = 0; 1254 int error; 1255 1256 val = gctl_get_intmax(req, "iterations"); 1257 /* Check if iterations number should be changed. */ 1258 if (val != -1) 1259 md->md_iterations = val; 1260 else 1261 old = md->md_iterations; 1262 1263 /* Generate key for Master Key encryption. */ 1264 if (eli_genkey(req, md, key, true) == NULL) { 1265 explicit_bzero(key, sizeof(key)); 1266 return; 1267 } 1268 /* 1269 * If number of iterations has changed, but wasn't given as a 1270 * command-line argument, update the request. 1271 */ 1272 if (val == -1 && md->md_iterations != old) { 1273 error = gctl_change_param(req, "iterations", sizeof(intmax_t), 1274 &md->md_iterations); 1275 assert(error == 0); 1276 } 1277 1278 gctl_ro_param(req, "key", sizeof(key), key); 1279 gctl_issue(req); 1280 explicit_bzero(key, sizeof(key)); 1281 } 1282 1283 static void 1284 eli_setkey_detached(struct gctl_req *req, const char *prov, 1285 struct g_eli_metadata *md) 1286 { 1287 unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 1288 unsigned char *mkeydst; 1289 unsigned int nkey; 1290 intmax_t val; 1291 int error; 1292 1293 if (md->md_keys == 0) { 1294 gctl_error(req, "No valid keys on %s.", prov); 1295 return; 1296 } 1297 1298 /* Generate key for Master Key decryption. */ 1299 if (eli_genkey(req, md, key, false) == NULL) { 1300 explicit_bzero(key, sizeof(key)); 1301 return; 1302 } 1303 1304 /* Decrypt Master Key. */ 1305 error = g_eli_mkey_decrypt_any(md, key, mkey, &nkey); 1306 explicit_bzero(key, sizeof(key)); 1307 if (error != 0) { 1308 explicit_bzero(md, sizeof(*md)); 1309 if (error == -1) 1310 gctl_error(req, "Wrong key for %s.", prov); 1311 else /* if (error > 0) */ { 1312 gctl_error(req, "Cannot decrypt Master Key: %s.", 1313 strerror(error)); 1314 } 1315 return; 1316 } 1317 if (verbose) 1318 printf("Decrypted Master Key %u.\n", nkey); 1319 1320 val = gctl_get_intmax(req, "keyno"); 1321 if (val != -1) 1322 nkey = val; 1323 #if 0 1324 else 1325 ; /* Use the key number which was found during decryption. */ 1326 #endif 1327 if (nkey >= G_ELI_MAXMKEYS) { 1328 gctl_error(req, "Invalid '%s' argument.", "keyno"); 1329 return; 1330 } 1331 1332 val = gctl_get_intmax(req, "iterations"); 1333 /* Check if iterations number should and can be changed. */ 1334 if (val != -1 && md->md_iterations == -1) { 1335 md->md_iterations = val; 1336 } else if (val != -1 && val != md->md_iterations) { 1337 if (bitcount32(md->md_keys) != 1) { 1338 gctl_error(req, "To be able to use '-i' option, only " 1339 "one key can be defined."); 1340 return; 1341 } 1342 if (md->md_keys != (1 << nkey)) { 1343 gctl_error(req, "Only already defined key can be " 1344 "changed when '-i' option is used."); 1345 return; 1346 } 1347 md->md_iterations = val; 1348 } 1349 1350 mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN; 1351 md->md_keys |= (1 << nkey); 1352 1353 bcopy(mkey, mkeydst, sizeof(mkey)); 1354 explicit_bzero(mkey, sizeof(mkey)); 1355 1356 /* Generate key for Master Key encryption. */ 1357 if (eli_genkey(req, md, key, true) == NULL) { 1358 explicit_bzero(key, sizeof(key)); 1359 explicit_bzero(md, sizeof(*md)); 1360 return; 1361 } 1362 1363 /* Encrypt the Master-Key with the new key. */ 1364 error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst); 1365 explicit_bzero(key, sizeof(key)); 1366 if (error != 0) { 1367 explicit_bzero(md, sizeof(*md)); 1368 gctl_error(req, "Cannot encrypt Master Key: %s.", 1369 strerror(error)); 1370 return; 1371 } 1372 1373 /* Store metadata with fresh key. */ 1374 eli_metadata_store(req, prov, md); 1375 explicit_bzero(md, sizeof(*md)); 1376 } 1377 1378 static void 1379 eli_setkey(struct gctl_req *req) 1380 { 1381 struct g_eli_metadata md; 1382 const char *prov; 1383 int nargs; 1384 1385 nargs = gctl_get_int(req, "nargs"); 1386 if (nargs != 1) { 1387 gctl_error(req, "Invalid number of arguments."); 1388 return; 1389 } 1390 prov = gctl_get_ascii(req, "arg0"); 1391 1392 if (eli_metadata_read(req, prov, &md) == -1) 1393 return; 1394 1395 if (eli_is_attached(prov)) 1396 eli_setkey_attached(req, &md); 1397 else 1398 eli_setkey_detached(req, prov, &md); 1399 1400 if (req->error == NULL || req->error[0] == '\0') { 1401 printf("Note, that the master key encrypted with old keys " 1402 "and/or passphrase may still exists in a metadata backup " 1403 "file.\n"); 1404 } 1405 } 1406 1407 static void 1408 eli_delkey_attached(struct gctl_req *req, const char *prov __unused) 1409 { 1410 1411 gctl_issue(req); 1412 } 1413 1414 static void 1415 eli_delkey_detached(struct gctl_req *req, const char *prov) 1416 { 1417 struct g_eli_metadata md; 1418 unsigned char *mkeydst; 1419 unsigned int nkey; 1420 intmax_t val; 1421 bool all, force; 1422 1423 if (eli_metadata_read(req, prov, &md) == -1) 1424 return; 1425 1426 all = gctl_get_int(req, "all"); 1427 if (all) 1428 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys)); 1429 else { 1430 force = gctl_get_int(req, "force"); 1431 val = gctl_get_intmax(req, "keyno"); 1432 if (val == -1) { 1433 gctl_error(req, "Key number has to be specified."); 1434 return; 1435 } 1436 nkey = val; 1437 if (nkey >= G_ELI_MAXMKEYS) { 1438 gctl_error(req, "Invalid '%s' argument.", "keyno"); 1439 return; 1440 } 1441 if (!(md.md_keys & (1 << nkey)) && !force) { 1442 gctl_error(req, "Master Key %u is not set.", nkey); 1443 return; 1444 } 1445 md.md_keys &= ~(1 << nkey); 1446 if (md.md_keys == 0 && !force) { 1447 gctl_error(req, "This is the last Master Key. Use '-f' " 1448 "option if you really want to remove it."); 1449 return; 1450 } 1451 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN; 1452 arc4random_buf(mkeydst, G_ELI_MKEYLEN); 1453 } 1454 1455 eli_metadata_store(req, prov, &md); 1456 explicit_bzero(&md, sizeof(md)); 1457 } 1458 1459 static void 1460 eli_delkey(struct gctl_req *req) 1461 { 1462 const char *prov; 1463 int nargs; 1464 1465 nargs = gctl_get_int(req, "nargs"); 1466 if (nargs != 1) { 1467 gctl_error(req, "Invalid number of arguments."); 1468 return; 1469 } 1470 prov = gctl_get_ascii(req, "arg0"); 1471 1472 if (eli_is_attached(prov)) 1473 eli_delkey_attached(req, prov); 1474 else 1475 eli_delkey_detached(req, prov); 1476 } 1477 1478 static void 1479 eli_resume(struct gctl_req *req) 1480 { 1481 struct g_eli_metadata md; 1482 unsigned char key[G_ELI_USERKEYLEN]; 1483 const char *prov; 1484 off_t mediasize; 1485 int nargs; 1486 1487 nargs = gctl_get_int(req, "nargs"); 1488 if (nargs != 1) { 1489 gctl_error(req, "Invalid number of arguments."); 1490 return; 1491 } 1492 prov = gctl_get_ascii(req, "arg0"); 1493 1494 if (eli_metadata_read(req, prov, &md) == -1) 1495 return; 1496 1497 mediasize = g_get_mediasize(prov); 1498 if (md.md_provsize != (uint64_t)mediasize) { 1499 gctl_error(req, "Provider size mismatch."); 1500 return; 1501 } 1502 1503 if (eli_genkey(req, &md, key, false) == NULL) { 1504 explicit_bzero(key, sizeof(key)); 1505 return; 1506 } 1507 1508 gctl_ro_param(req, "key", sizeof(key), key); 1509 if (gctl_issue(req) == NULL) { 1510 if (verbose) 1511 printf("Resumed %s.\n", prov); 1512 } 1513 explicit_bzero(key, sizeof(key)); 1514 } 1515 1516 static int 1517 eli_trash_metadata(struct gctl_req *req, const char *prov, int fd, off_t offset) 1518 { 1519 unsigned int overwrites; 1520 unsigned char *sector; 1521 ssize_t size; 1522 int error; 1523 1524 size = sizeof(overwrites); 1525 if (sysctlbyname("kern.geom.eli.overwrites", &overwrites, &size, 1526 NULL, 0) == -1 || overwrites == 0) { 1527 overwrites = G_ELI_OVERWRITES; 1528 } 1529 1530 size = g_sectorsize(fd); 1531 if (size <= 0) { 1532 gctl_error(req, "Cannot obtain provider sector size %s: %s.", 1533 prov, strerror(errno)); 1534 return (-1); 1535 } 1536 sector = malloc(size); 1537 if (sector == NULL) { 1538 gctl_error(req, "Cannot allocate %zd bytes of memory.", size); 1539 return (-1); 1540 } 1541 1542 error = 0; 1543 do { 1544 arc4random_buf(sector, size); 1545 if (pwrite(fd, sector, size, offset) != size) { 1546 if (error == 0) 1547 error = errno; 1548 } 1549 (void)g_flush(fd); 1550 } while (--overwrites > 0); 1551 free(sector); 1552 if (error != 0) { 1553 gctl_error(req, "Cannot trash metadata on provider %s: %s.", 1554 prov, strerror(error)); 1555 return (-1); 1556 } 1557 return (0); 1558 } 1559 1560 static void 1561 eli_kill_detached(struct gctl_req *req, const char *prov) 1562 { 1563 off_t offset; 1564 int fd; 1565 1566 /* 1567 * NOTE: Maybe we should verify if this is geli provider first, 1568 * but 'kill' command is quite critical so better don't waste 1569 * the time. 1570 */ 1571 #if 0 1572 error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md), 1573 G_ELI_MAGIC); 1574 if (error != 0) { 1575 gctl_error(req, "Cannot read metadata from %s: %s.", prov, 1576 strerror(error)); 1577 return; 1578 } 1579 #endif 1580 1581 fd = g_open(prov, 1); 1582 if (fd == -1) { 1583 gctl_error(req, "Cannot open provider %s: %s.", prov, 1584 strerror(errno)); 1585 return; 1586 } 1587 offset = g_mediasize(fd) - g_sectorsize(fd); 1588 if (offset <= 0) { 1589 gctl_error(req, 1590 "Cannot obtain media size or sector size for provider %s: %s.", 1591 prov, strerror(errno)); 1592 (void)g_close(fd); 1593 return; 1594 } 1595 (void)eli_trash_metadata(req, prov, fd, offset); 1596 (void)g_close(fd); 1597 } 1598 1599 static void 1600 eli_kill(struct gctl_req *req) 1601 { 1602 const char *prov; 1603 int i, nargs, all; 1604 1605 nargs = gctl_get_int(req, "nargs"); 1606 all = gctl_get_int(req, "all"); 1607 if (!all && nargs == 0) { 1608 gctl_error(req, "Too few arguments."); 1609 return; 1610 } 1611 /* 1612 * How '-a' option combine with a list of providers: 1613 * Delete Master Keys from all attached providers: 1614 * geli kill -a 1615 * Delete Master Keys from all attached providers and from 1616 * detached da0 and da1: 1617 * geli kill -a da0 da1 1618 * Delete Master Keys from (attached or detached) da0 and da1: 1619 * geli kill da0 da1 1620 */ 1621 1622 /* First detached providers. */ 1623 for (i = 0; i < nargs; i++) { 1624 prov = gctl_get_ascii(req, "arg%d", i); 1625 if (!eli_is_attached(prov)) 1626 eli_kill_detached(req, prov); 1627 } 1628 /* Now attached providers. */ 1629 gctl_issue(req); 1630 } 1631 1632 static int 1633 eli_backup_create(struct gctl_req *req, const char *prov, const char *file) 1634 { 1635 unsigned char *sector; 1636 ssize_t secsize; 1637 int error, filefd, ret; 1638 1639 ret = -1; 1640 filefd = -1; 1641 sector = NULL; 1642 secsize = 0; 1643 1644 secsize = g_get_sectorsize(prov); 1645 if (secsize == 0) { 1646 gctl_error(req, "Cannot get informations about %s: %s.", prov, 1647 strerror(errno)); 1648 goto out; 1649 } 1650 sector = malloc(secsize); 1651 if (sector == NULL) { 1652 gctl_error(req, "Cannot allocate memory."); 1653 goto out; 1654 } 1655 /* Read metadata from the provider. */ 1656 error = g_metadata_read(prov, sector, secsize, G_ELI_MAGIC); 1657 if (error != 0) { 1658 gctl_error(req, "Unable to read metadata from %s: %s.", prov, 1659 strerror(error)); 1660 goto out; 1661 } 1662 1663 filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600); 1664 if (filefd == -1) { 1665 gctl_error(req, "Unable to open %s: %s.", file, 1666 strerror(errno)); 1667 goto out; 1668 } 1669 /* Write metadata to the destination file. */ 1670 if (write(filefd, sector, secsize) != secsize) { 1671 gctl_error(req, "Unable to write to %s: %s.", file, 1672 strerror(errno)); 1673 (void)close(filefd); 1674 (void)unlink(file); 1675 goto out; 1676 } 1677 (void)fsync(filefd); 1678 (void)close(filefd); 1679 /* Success. */ 1680 ret = 0; 1681 out: 1682 if (sector != NULL) { 1683 explicit_bzero(sector, secsize); 1684 free(sector); 1685 } 1686 return (ret); 1687 } 1688 1689 static void 1690 eli_backup(struct gctl_req *req) 1691 { 1692 const char *file, *prov; 1693 int nargs; 1694 1695 nargs = gctl_get_int(req, "nargs"); 1696 if (nargs != 2) { 1697 gctl_error(req, "Invalid number of arguments."); 1698 return; 1699 } 1700 prov = gctl_get_ascii(req, "arg0"); 1701 file = gctl_get_ascii(req, "arg1"); 1702 1703 eli_backup_create(req, prov, file); 1704 } 1705 1706 static void 1707 eli_restore(struct gctl_req *req) 1708 { 1709 struct g_eli_metadata md; 1710 const char *file, *prov; 1711 off_t mediasize; 1712 int nargs; 1713 1714 nargs = gctl_get_int(req, "nargs"); 1715 if (nargs != 2) { 1716 gctl_error(req, "Invalid number of arguments."); 1717 return; 1718 } 1719 file = gctl_get_ascii(req, "arg0"); 1720 prov = gctl_get_ascii(req, "arg1"); 1721 1722 /* Read metadata from the backup file. */ 1723 if (eli_metadata_read(req, file, &md) == -1) 1724 return; 1725 /* Obtain provider's mediasize. */ 1726 mediasize = g_get_mediasize(prov); 1727 if (mediasize == 0) { 1728 gctl_error(req, "Cannot get informations about %s: %s.", prov, 1729 strerror(errno)); 1730 return; 1731 } 1732 /* Check if the provider size has changed since we did the backup. */ 1733 if (md.md_provsize != (uint64_t)mediasize) { 1734 if (gctl_get_int(req, "force")) { 1735 md.md_provsize = mediasize; 1736 } else { 1737 gctl_error(req, "Provider size mismatch: " 1738 "wrong backup file?"); 1739 return; 1740 } 1741 } 1742 /* Write metadata to the provider. */ 1743 (void)eli_metadata_store(req, prov, &md); 1744 } 1745 1746 static void 1747 eli_resize(struct gctl_req *req) 1748 { 1749 struct g_eli_metadata md; 1750 const char *prov; 1751 unsigned char *sector; 1752 ssize_t secsize; 1753 off_t mediasize, oldsize; 1754 int error, nargs, provfd; 1755 1756 nargs = gctl_get_int(req, "nargs"); 1757 if (nargs != 1) { 1758 gctl_error(req, "Invalid number of arguments."); 1759 return; 1760 } 1761 prov = gctl_get_ascii(req, "arg0"); 1762 1763 provfd = -1; 1764 sector = NULL; 1765 secsize = 0; 1766 1767 provfd = g_open(prov, 1); 1768 if (provfd == -1) { 1769 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno)); 1770 goto out; 1771 } 1772 1773 mediasize = g_mediasize(provfd); 1774 secsize = g_sectorsize(provfd); 1775 if (mediasize == -1 || secsize == -1) { 1776 gctl_error(req, "Cannot get information about %s: %s.", prov, 1777 strerror(errno)); 1778 goto out; 1779 } 1780 1781 sector = malloc(secsize); 1782 if (sector == NULL) { 1783 gctl_error(req, "Cannot allocate memory."); 1784 goto out; 1785 } 1786 1787 oldsize = gctl_get_intmax(req, "oldsize"); 1788 if (oldsize < 0 || oldsize > mediasize) { 1789 gctl_error(req, "Invalid oldsize: Out of range."); 1790 goto out; 1791 } 1792 if (oldsize == mediasize) { 1793 gctl_error(req, "Size hasn't changed."); 1794 goto out; 1795 } 1796 1797 /* Read metadata from the 'oldsize' offset. */ 1798 if (pread(provfd, sector, secsize, oldsize - secsize) != secsize) { 1799 gctl_error(req, "Cannot read old metadata: %s.", 1800 strerror(errno)); 1801 goto out; 1802 } 1803 1804 /* Check if this sector contains geli metadata. */ 1805 error = eli_metadata_decode(sector, &md); 1806 switch (error) { 1807 case 0: 1808 break; 1809 case EOPNOTSUPP: 1810 gctl_error(req, 1811 "Provider's %s metadata version %u is too new.\n" 1812 "geli: The highest supported version is %u.", 1813 prov, (unsigned int)md.md_version, G_ELI_VERSION); 1814 goto out; 1815 case EINVAL: 1816 gctl_error(req, "Inconsistent provider's %s metadata.", prov); 1817 goto out; 1818 default: 1819 gctl_error(req, 1820 "Unexpected error while decoding provider's %s metadata: %s.", 1821 prov, strerror(error)); 1822 goto out; 1823 } 1824 1825 /* 1826 * If the old metadata doesn't have a correct provider size, refuse 1827 * to resize. 1828 */ 1829 if (md.md_provsize != (uint64_t)oldsize) { 1830 gctl_error(req, "Provider size mismatch at oldsize."); 1831 goto out; 1832 } 1833 1834 /* 1835 * Update the old metadata with the current provider size and write 1836 * it back to the correct place on the provider. 1837 */ 1838 md.md_provsize = mediasize; 1839 /* Write metadata to the provider. */ 1840 (void)eli_metadata_store(req, prov, &md); 1841 /* Now trash the old metadata. */ 1842 (void)eli_trash_metadata(req, prov, provfd, oldsize - secsize); 1843 out: 1844 if (provfd != -1) 1845 (void)g_close(provfd); 1846 if (sector != NULL) { 1847 explicit_bzero(sector, secsize); 1848 free(sector); 1849 } 1850 } 1851 1852 static void 1853 eli_version(struct gctl_req *req) 1854 { 1855 struct g_eli_metadata md; 1856 const char *name; 1857 unsigned int version; 1858 int error, i, nargs; 1859 1860 nargs = gctl_get_int(req, "nargs"); 1861 1862 if (nargs == 0) { 1863 unsigned int kernver; 1864 ssize_t size; 1865 1866 size = sizeof(kernver); 1867 if (sysctlbyname("kern.geom.eli.version", &kernver, &size, 1868 NULL, 0) == -1) { 1869 warn("Unable to obtain GELI kernel version"); 1870 } else { 1871 printf("kernel: %u\n", kernver); 1872 } 1873 printf("userland: %u\n", G_ELI_VERSION); 1874 return; 1875 } 1876 1877 for (i = 0; i < nargs; i++) { 1878 name = gctl_get_ascii(req, "arg%d", i); 1879 error = g_metadata_read(name, (unsigned char *)&md, 1880 sizeof(md), G_ELI_MAGIC); 1881 if (error != 0) { 1882 warn("%s: Unable to read metadata: %s.", name, 1883 strerror(error)); 1884 gctl_error(req, "Not fully done."); 1885 continue; 1886 } 1887 version = le32dec(&md.md_version); 1888 printf("%s: %u\n", name, version); 1889 } 1890 } 1891 1892 static void 1893 eli_clear(struct gctl_req *req) 1894 { 1895 const char *name; 1896 int error, i, nargs; 1897 1898 nargs = gctl_get_int(req, "nargs"); 1899 if (nargs < 1) { 1900 gctl_error(req, "Too few arguments."); 1901 return; 1902 } 1903 1904 for (i = 0; i < nargs; i++) { 1905 name = gctl_get_ascii(req, "arg%d", i); 1906 error = g_metadata_clear(name, G_ELI_MAGIC); 1907 if (error != 0) { 1908 fprintf(stderr, "Cannot clear metadata on %s: %s.\n", 1909 name, strerror(error)); 1910 gctl_error(req, "Not fully done."); 1911 continue; 1912 } 1913 if (verbose) 1914 printf("Metadata cleared on %s.\n", name); 1915 } 1916 } 1917 1918 static void 1919 eli_dump(struct gctl_req *req) 1920 { 1921 struct g_eli_metadata md; 1922 const char *name; 1923 int i, nargs; 1924 1925 nargs = gctl_get_int(req, "nargs"); 1926 if (nargs < 1) { 1927 gctl_error(req, "Too few arguments."); 1928 return; 1929 } 1930 1931 for (i = 0; i < nargs; i++) { 1932 name = gctl_get_ascii(req, "arg%d", i); 1933 if (eli_metadata_read(NULL, name, &md) == -1) { 1934 gctl_error(req, "Not fully done."); 1935 continue; 1936 } 1937 printf("Metadata on %s:\n", name); 1938 eli_metadata_dump(&md); 1939 printf("\n"); 1940 } 1941 } 1942