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 explicit_bzero(key, sizeof(key)); 892 if (error != 0) { 893 gctl_error(r, "Cannot encrypt Master Key: %s.", 894 strerror(error)); 895 goto out; 896 } 897 898 /* 899 * Convert metadata to on-disk format and then immediately erase 900 * sensitive data from the metadata struct. 901 */ 902 eli_metadata_encode(&md, sector); 903 explicit_bzero(&md.md_provsize, sizeof(md.md_provsize)); 904 explicit_bzero(&md.md_sectorsize, sizeof(md.md_sectorsize)); 905 explicit_bzero(&md.md_salt, sizeof(md.md_salt)); 906 explicit_bzero(&md.md_mkeys, sizeof(md.md_mkeys)); 907 908 /* 909 * Store metadata to disk and then immediately erase sensitive 910 * data from memory. 911 */ 912 error = g_metadata_store(prov, sector, sizeof(sector)); 913 explicit_bzero(sector, sizeof(sector)); 914 if (error != 0) { 915 gctl_error(r, "Cannot store metadata on %s: %s.", prov, 916 strerror(error)); 917 goto out; 918 } 919 if (verbose) 920 printf("Metadata value stored on %s.\n", prov); 921 922 /* Backup metadata to a file. */ 923 const char *p = prov; 924 unsigned int j; 925 926 /* 927 * Check if provider string includes the devfs mountpoint 928 * (typically /dev/). 929 */ 930 if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) { 931 /* Skip forward to the device filename only. */ 932 p += sizeof(_PATH_DEV) - 1; 933 } 934 935 str = gctl_get_ascii(r, "backupfile"); 936 if (str[0] != '\0') { 937 /* Backupfile given by the user, just copy it. */ 938 strlcpy(backfile, str, sizeof(backfile)); 939 940 /* Make the backup filename unique if multiple providers 941 * initialized in one command. */ 942 if (nargs > 1) { 943 /* 944 * Replace first occurrence of "PROV" with 945 * provider name. 946 */ 947 str = strnstr(backfile, "PROV", 948 sizeof(backfile)); 949 if (str != NULL) { 950 char suffix[MAXPATHLEN]; 951 j = str - backfile; 952 strlcpy(suffix, &backfile[j+4], 953 sizeof(suffix)); 954 backfile[j] = '\0'; 955 strlcat(backfile, p, sizeof(backfile)); 956 strlcat(backfile, suffix, 957 sizeof(backfile)); 958 } else { 959 /* 960 * "PROV" not found in backfile, append 961 * provider name. 962 */ 963 strlcat(backfile, "-", 964 sizeof(backfile)); 965 strlcat(backfile, p, sizeof(backfile)); 966 } 967 } 968 } else { 969 /* Generate filename automatically. */ 970 snprintf(backfile, sizeof(backfile), "%s%s.eli", 971 GELI_BACKUP_DIR, p); 972 /* Replace all / with _. */ 973 for (j = strlen(GELI_BACKUP_DIR); backfile[j] != '\0'; 974 j++) { 975 if (backfile[j] == '/') 976 backfile[j] = '_'; 977 } 978 } 979 if (strcmp(backfile, "none") != 0 && 980 eli_backup_create(r, prov, backfile) == 0) { 981 printf("\nMetadata backup for provider %s can be found " 982 "in %s\n", prov, backfile); 983 printf("and can be restored with the following " 984 "command:\n"); 985 printf("\n\t# geli restore %s %s\n\n", backfile, prov); 986 } 987 988 out: 989 /* 990 * Print error for this request, and set parent request error 991 * message. 992 */ 993 if (r->error != NULL && r->error[0] != '\0') { 994 warnx("%s", r->error); 995 gctl_error(req, "There was an error with at least one " 996 "provider."); 997 } 998 999 gctl_free(r); 1000 1001 /* 1002 * Erase sensitive data from memory, and ensure subsequent 1003 * providers are initialized with unique metadata. 1004 */ 1005 explicit_bzero(key, sizeof(key)); 1006 explicit_bzero(&md, sizeof(md)); 1007 } 1008 1009 /* Clear the cached metadata, including keys. */ 1010 explicit_bzero(&md, sizeof(md)); 1011 } 1012 1013 static void 1014 eli_attach(struct gctl_req *req) 1015 { 1016 struct g_eli_metadata md; 1017 struct gctl_req *r; 1018 const char *prov; 1019 off_t mediasize; 1020 int i, nargs, nparams, param; 1021 const int one = 1; 1022 1023 nargs = gctl_get_int(req, "nargs"); 1024 if (nargs <= 0) { 1025 gctl_error(req, "Too few arguments."); 1026 return; 1027 } 1028 1029 unsigned char key[G_ELI_USERKEYLEN]; 1030 1031 /* 1032 * Determine number of parameters in the parent geom request before the 1033 * nargs parameter and list of providers. 1034 */ 1035 nparams = req->narg - nargs - 1; 1036 1037 /* Create new child request for each provider and issue to kernel */ 1038 for (i = 0; i < nargs; i++) { 1039 r = gctl_get_handle(); 1040 1041 /* Copy each parameter from the parent request to the child */ 1042 for (param = 0; param < nparams; param++) { 1043 gctl_ro_param(r, req->arg[param].name, 1044 req->arg[param].len, req->arg[param].value); 1045 } 1046 1047 /* Add a single provider to the parameter list of the child */ 1048 gctl_ro_param(r, "nargs", sizeof(one), &one); 1049 prov = gctl_get_ascii(req, "arg%d", i); 1050 gctl_ro_param(r, "arg0", -1, prov); 1051 1052 if (eli_metadata_read(r, prov, &md) == -1) { 1053 /* 1054 * Error reading metadata - details added to geom 1055 * request by eli_metadata_read(). 1056 */ 1057 goto out; 1058 } 1059 1060 mediasize = g_get_mediasize(prov); 1061 if (md.md_provsize != (uint64_t)mediasize) { 1062 gctl_error(r, "Provider size mismatch."); 1063 goto out; 1064 } 1065 1066 if (eli_genkey(r, &md, key, false) == NULL) { 1067 /* 1068 * Error generating key - details added to geom request 1069 * by eli_genkey(). 1070 */ 1071 goto out; 1072 } 1073 1074 gctl_ro_param(r, "key", sizeof(key), key); 1075 1076 if (gctl_issue(r) == NULL) { 1077 if (verbose) 1078 printf("Attached to %s.\n", prov); 1079 } 1080 1081 out: 1082 /* 1083 * Print error for this request, and set parent request error 1084 * message. 1085 */ 1086 if (r->error != NULL && r->error[0] != '\0') { 1087 warnx("%s", r->error); 1088 gctl_error(req, "There was an error with at least one " 1089 "provider."); 1090 } 1091 1092 gctl_free(r); 1093 1094 /* Clear sensitive data from memory. */ 1095 explicit_bzero(key, sizeof(key)); 1096 } 1097 1098 /* Clear sensitive data from memory. */ 1099 explicit_bzero(cached_passphrase, sizeof(cached_passphrase)); 1100 } 1101 1102 static void 1103 eli_configure_detached(struct gctl_req *req, const char *prov, int boot, 1104 int geliboot, int displaypass, int trim) 1105 { 1106 struct g_eli_metadata md; 1107 bool changed = 0; 1108 1109 if (eli_metadata_read(req, prov, &md) == -1) 1110 return; 1111 1112 if (boot == 1 && (md.md_flags & G_ELI_FLAG_BOOT)) { 1113 if (verbose) 1114 printf("BOOT flag already configured for %s.\n", prov); 1115 } else if (boot == 0 && !(md.md_flags & G_ELI_FLAG_BOOT)) { 1116 if (verbose) 1117 printf("BOOT flag not configured for %s.\n", prov); 1118 } else if (boot >= 0) { 1119 if (boot) 1120 md.md_flags |= G_ELI_FLAG_BOOT; 1121 else 1122 md.md_flags &= ~G_ELI_FLAG_BOOT; 1123 changed = 1; 1124 } 1125 1126 if (geliboot == 1 && (md.md_flags & G_ELI_FLAG_GELIBOOT)) { 1127 if (verbose) 1128 printf("GELIBOOT flag already configured for %s.\n", prov); 1129 } else if (geliboot == 0 && !(md.md_flags & G_ELI_FLAG_GELIBOOT)) { 1130 if (verbose) 1131 printf("GELIBOOT flag not configured for %s.\n", prov); 1132 } else if (geliboot >= 0) { 1133 if (geliboot) 1134 md.md_flags |= G_ELI_FLAG_GELIBOOT; 1135 else 1136 md.md_flags &= ~G_ELI_FLAG_GELIBOOT; 1137 changed = 1; 1138 } 1139 1140 if (displaypass == 1 && (md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) { 1141 if (verbose) 1142 printf("GELIDISPLAYPASS flag already configured for %s.\n", prov); 1143 } else if (displaypass == 0 && 1144 !(md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) { 1145 if (verbose) 1146 printf("GELIDISPLAYPASS flag not configured for %s.\n", prov); 1147 } else if (displaypass >= 0) { 1148 if (displaypass) 1149 md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS; 1150 else 1151 md.md_flags &= ~G_ELI_FLAG_GELIDISPLAYPASS; 1152 changed = 1; 1153 } 1154 1155 if (trim == 0 && (md.md_flags & G_ELI_FLAG_NODELETE)) { 1156 if (verbose) 1157 printf("TRIM disable flag already configured for %s.\n", prov); 1158 } else if (trim == 1 && !(md.md_flags & G_ELI_FLAG_NODELETE)) { 1159 if (verbose) 1160 printf("TRIM disable flag not configured for %s.\n", prov); 1161 } else if (trim >= 0) { 1162 if (trim) 1163 md.md_flags &= ~G_ELI_FLAG_NODELETE; 1164 else 1165 md.md_flags |= G_ELI_FLAG_NODELETE; 1166 changed = 1; 1167 } 1168 1169 if (changed) 1170 eli_metadata_store(req, prov, &md); 1171 explicit_bzero(&md, sizeof(md)); 1172 } 1173 1174 static void 1175 eli_configure(struct gctl_req *req) 1176 { 1177 const char *prov; 1178 bool boot, noboot, geliboot, nogeliboot, displaypass, nodisplaypass; 1179 bool trim, notrim; 1180 int doboot, dogeliboot, dodisplaypass, dotrim; 1181 int i, nargs; 1182 1183 nargs = gctl_get_int(req, "nargs"); 1184 if (nargs == 0) { 1185 gctl_error(req, "Too few arguments."); 1186 return; 1187 } 1188 1189 boot = gctl_get_int(req, "boot"); 1190 noboot = gctl_get_int(req, "noboot"); 1191 geliboot = gctl_get_int(req, "geliboot"); 1192 nogeliboot = gctl_get_int(req, "nogeliboot"); 1193 displaypass = gctl_get_int(req, "displaypass"); 1194 nodisplaypass = gctl_get_int(req, "nodisplaypass"); 1195 trim = gctl_get_int(req, "trim"); 1196 notrim = gctl_get_int(req, "notrim"); 1197 1198 doboot = -1; 1199 if (boot && noboot) { 1200 gctl_error(req, "Options -b and -B are mutually exclusive."); 1201 return; 1202 } 1203 if (boot) 1204 doboot = 1; 1205 else if (noboot) 1206 doboot = 0; 1207 1208 dogeliboot = -1; 1209 if (geliboot && nogeliboot) { 1210 gctl_error(req, "Options -g and -G are mutually exclusive."); 1211 return; 1212 } 1213 if (geliboot) 1214 dogeliboot = 1; 1215 else if (nogeliboot) 1216 dogeliboot = 0; 1217 1218 dodisplaypass = -1; 1219 if (displaypass && nodisplaypass) { 1220 gctl_error(req, "Options -d and -D are mutually exclusive."); 1221 return; 1222 } 1223 if (displaypass) 1224 dodisplaypass = 1; 1225 else if (nodisplaypass) 1226 dodisplaypass = 0; 1227 1228 dotrim = -1; 1229 if (trim && notrim) { 1230 gctl_error(req, "Options -t and -T are mutually exclusive."); 1231 return; 1232 } 1233 if (trim) 1234 dotrim = 1; 1235 else if (notrim) 1236 dotrim = 0; 1237 1238 if (doboot == -1 && dogeliboot == -1 && dodisplaypass == -1 && 1239 dotrim == -1) { 1240 gctl_error(req, "No option given."); 1241 return; 1242 } 1243 1244 /* First attached providers. */ 1245 gctl_issue(req); 1246 /* Now the rest. */ 1247 for (i = 0; i < nargs; i++) { 1248 prov = gctl_get_ascii(req, "arg%d", i); 1249 if (!eli_is_attached(prov)) { 1250 eli_configure_detached(req, prov, doboot, dogeliboot, 1251 dodisplaypass, dotrim); 1252 } 1253 } 1254 } 1255 1256 static void 1257 eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md) 1258 { 1259 unsigned char key[G_ELI_USERKEYLEN]; 1260 intmax_t val, old = 0; 1261 int error; 1262 1263 val = gctl_get_intmax(req, "iterations"); 1264 /* Check if iterations number should be changed. */ 1265 if (val != -1) 1266 md->md_iterations = val; 1267 else 1268 old = md->md_iterations; 1269 1270 /* Generate key for Master Key encryption. */ 1271 if (eli_genkey(req, md, key, true) == NULL) { 1272 explicit_bzero(key, sizeof(key)); 1273 return; 1274 } 1275 /* 1276 * If number of iterations has changed, but wasn't given as a 1277 * command-line argument, update the request. 1278 */ 1279 if (val == -1 && md->md_iterations != old) { 1280 error = gctl_change_param(req, "iterations", sizeof(intmax_t), 1281 &md->md_iterations); 1282 assert(error == 0); 1283 } 1284 1285 gctl_ro_param(req, "key", sizeof(key), key); 1286 gctl_issue(req); 1287 explicit_bzero(key, sizeof(key)); 1288 } 1289 1290 static void 1291 eli_setkey_detached(struct gctl_req *req, const char *prov, 1292 struct g_eli_metadata *md) 1293 { 1294 unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 1295 unsigned char *mkeydst; 1296 unsigned int nkey; 1297 intmax_t val; 1298 int error; 1299 1300 if (md->md_keys == 0) { 1301 gctl_error(req, "No valid keys on %s.", prov); 1302 return; 1303 } 1304 1305 /* Generate key for Master Key decryption. */ 1306 if (eli_genkey(req, md, key, false) == NULL) { 1307 explicit_bzero(key, sizeof(key)); 1308 return; 1309 } 1310 1311 /* Decrypt Master Key. */ 1312 error = g_eli_mkey_decrypt_any(md, key, mkey, &nkey); 1313 explicit_bzero(key, sizeof(key)); 1314 if (error != 0) { 1315 explicit_bzero(md, sizeof(*md)); 1316 if (error == -1) 1317 gctl_error(req, "Wrong key for %s.", prov); 1318 else /* if (error > 0) */ { 1319 gctl_error(req, "Cannot decrypt Master Key: %s.", 1320 strerror(error)); 1321 } 1322 return; 1323 } 1324 if (verbose) 1325 printf("Decrypted Master Key %u.\n", nkey); 1326 1327 val = gctl_get_intmax(req, "keyno"); 1328 if (val != -1) 1329 nkey = val; 1330 #if 0 1331 else 1332 ; /* Use the key number which was found during decryption. */ 1333 #endif 1334 if (nkey >= G_ELI_MAXMKEYS) { 1335 gctl_error(req, "Invalid '%s' argument.", "keyno"); 1336 return; 1337 } 1338 1339 val = gctl_get_intmax(req, "iterations"); 1340 /* Check if iterations number should and can be changed. */ 1341 if (val != -1 && md->md_iterations == -1) { 1342 md->md_iterations = val; 1343 } else if (val != -1 && val != md->md_iterations) { 1344 if (bitcount32(md->md_keys) != 1) { 1345 gctl_error(req, "To be able to use '-i' option, only " 1346 "one key can be defined."); 1347 return; 1348 } 1349 if (md->md_keys != (1 << nkey)) { 1350 gctl_error(req, "Only already defined key can be " 1351 "changed when '-i' option is used."); 1352 return; 1353 } 1354 md->md_iterations = val; 1355 } 1356 1357 mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN; 1358 md->md_keys |= (1 << nkey); 1359 1360 bcopy(mkey, mkeydst, sizeof(mkey)); 1361 explicit_bzero(mkey, sizeof(mkey)); 1362 1363 /* Generate key for Master Key encryption. */ 1364 if (eli_genkey(req, md, key, true) == NULL) { 1365 explicit_bzero(key, sizeof(key)); 1366 explicit_bzero(md, sizeof(*md)); 1367 return; 1368 } 1369 1370 /* Encrypt the Master-Key with the new key. */ 1371 error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst); 1372 explicit_bzero(key, sizeof(key)); 1373 if (error != 0) { 1374 explicit_bzero(md, sizeof(*md)); 1375 gctl_error(req, "Cannot encrypt Master Key: %s.", 1376 strerror(error)); 1377 return; 1378 } 1379 1380 /* Store metadata with fresh key. */ 1381 eli_metadata_store(req, prov, md); 1382 explicit_bzero(md, sizeof(*md)); 1383 } 1384 1385 static void 1386 eli_setkey(struct gctl_req *req) 1387 { 1388 struct g_eli_metadata md; 1389 const char *prov; 1390 int nargs; 1391 1392 nargs = gctl_get_int(req, "nargs"); 1393 if (nargs != 1) { 1394 gctl_error(req, "Invalid number of arguments."); 1395 return; 1396 } 1397 prov = gctl_get_ascii(req, "arg0"); 1398 1399 if (eli_metadata_read(req, prov, &md) == -1) 1400 return; 1401 1402 if (eli_is_attached(prov)) 1403 eli_setkey_attached(req, &md); 1404 else 1405 eli_setkey_detached(req, prov, &md); 1406 1407 if (req->error == NULL || req->error[0] == '\0') { 1408 printf("Note, that the master key encrypted with old keys " 1409 "and/or passphrase may still exists in a metadata backup " 1410 "file.\n"); 1411 } 1412 } 1413 1414 static void 1415 eli_delkey_attached(struct gctl_req *req, const char *prov __unused) 1416 { 1417 1418 gctl_issue(req); 1419 } 1420 1421 static void 1422 eli_delkey_detached(struct gctl_req *req, const char *prov) 1423 { 1424 struct g_eli_metadata md; 1425 unsigned char *mkeydst; 1426 unsigned int nkey; 1427 intmax_t val; 1428 bool all, force; 1429 1430 if (eli_metadata_read(req, prov, &md) == -1) 1431 return; 1432 1433 all = gctl_get_int(req, "all"); 1434 if (all) 1435 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys)); 1436 else { 1437 force = gctl_get_int(req, "force"); 1438 val = gctl_get_intmax(req, "keyno"); 1439 if (val == -1) { 1440 gctl_error(req, "Key number has to be specified."); 1441 return; 1442 } 1443 nkey = val; 1444 if (nkey >= G_ELI_MAXMKEYS) { 1445 gctl_error(req, "Invalid '%s' argument.", "keyno"); 1446 return; 1447 } 1448 if (!(md.md_keys & (1 << nkey)) && !force) { 1449 gctl_error(req, "Master Key %u is not set.", nkey); 1450 return; 1451 } 1452 md.md_keys &= ~(1 << nkey); 1453 if (md.md_keys == 0 && !force) { 1454 gctl_error(req, "This is the last Master Key. Use '-f' " 1455 "option if you really want to remove it."); 1456 return; 1457 } 1458 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN; 1459 arc4random_buf(mkeydst, G_ELI_MKEYLEN); 1460 } 1461 1462 eli_metadata_store(req, prov, &md); 1463 explicit_bzero(&md, sizeof(md)); 1464 } 1465 1466 static void 1467 eli_delkey(struct gctl_req *req) 1468 { 1469 const char *prov; 1470 int nargs; 1471 1472 nargs = gctl_get_int(req, "nargs"); 1473 if (nargs != 1) { 1474 gctl_error(req, "Invalid number of arguments."); 1475 return; 1476 } 1477 prov = gctl_get_ascii(req, "arg0"); 1478 1479 if (eli_is_attached(prov)) 1480 eli_delkey_attached(req, prov); 1481 else 1482 eli_delkey_detached(req, prov); 1483 } 1484 1485 static void 1486 eli_resume(struct gctl_req *req) 1487 { 1488 struct g_eli_metadata md; 1489 unsigned char key[G_ELI_USERKEYLEN]; 1490 const char *prov; 1491 off_t mediasize; 1492 int nargs; 1493 1494 nargs = gctl_get_int(req, "nargs"); 1495 if (nargs != 1) { 1496 gctl_error(req, "Invalid number of arguments."); 1497 return; 1498 } 1499 prov = gctl_get_ascii(req, "arg0"); 1500 1501 if (eli_metadata_read(req, prov, &md) == -1) 1502 return; 1503 1504 mediasize = g_get_mediasize(prov); 1505 if (md.md_provsize != (uint64_t)mediasize) { 1506 gctl_error(req, "Provider size mismatch."); 1507 return; 1508 } 1509 1510 if (eli_genkey(req, &md, key, false) == NULL) { 1511 explicit_bzero(key, sizeof(key)); 1512 return; 1513 } 1514 1515 gctl_ro_param(req, "key", sizeof(key), key); 1516 if (gctl_issue(req) == NULL) { 1517 if (verbose) 1518 printf("Resumed %s.\n", prov); 1519 } 1520 explicit_bzero(key, sizeof(key)); 1521 } 1522 1523 static int 1524 eli_trash_metadata(struct gctl_req *req, const char *prov, int fd, off_t offset) 1525 { 1526 unsigned int overwrites; 1527 unsigned char *sector; 1528 ssize_t size; 1529 int error; 1530 1531 size = sizeof(overwrites); 1532 if (sysctlbyname("kern.geom.eli.overwrites", &overwrites, &size, 1533 NULL, 0) == -1 || overwrites == 0) { 1534 overwrites = G_ELI_OVERWRITES; 1535 } 1536 1537 size = g_sectorsize(fd); 1538 if (size <= 0) { 1539 gctl_error(req, "Cannot obtain provider sector size %s: %s.", 1540 prov, strerror(errno)); 1541 return (-1); 1542 } 1543 sector = malloc(size); 1544 if (sector == NULL) { 1545 gctl_error(req, "Cannot allocate %zd bytes of memory.", size); 1546 return (-1); 1547 } 1548 1549 error = 0; 1550 do { 1551 arc4random_buf(sector, size); 1552 if (pwrite(fd, sector, size, offset) != size) { 1553 if (error == 0) 1554 error = errno; 1555 } 1556 (void)g_flush(fd); 1557 } while (--overwrites > 0); 1558 free(sector); 1559 if (error != 0) { 1560 gctl_error(req, "Cannot trash metadata on provider %s: %s.", 1561 prov, strerror(error)); 1562 return (-1); 1563 } 1564 return (0); 1565 } 1566 1567 static void 1568 eli_kill_detached(struct gctl_req *req, const char *prov) 1569 { 1570 off_t offset; 1571 int fd; 1572 1573 /* 1574 * NOTE: Maybe we should verify if this is geli provider first, 1575 * but 'kill' command is quite critical so better don't waste 1576 * the time. 1577 */ 1578 #if 0 1579 error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md), 1580 G_ELI_MAGIC); 1581 if (error != 0) { 1582 gctl_error(req, "Cannot read metadata from %s: %s.", prov, 1583 strerror(error)); 1584 return; 1585 } 1586 #endif 1587 1588 fd = g_open(prov, 1); 1589 if (fd == -1) { 1590 gctl_error(req, "Cannot open provider %s: %s.", prov, 1591 strerror(errno)); 1592 return; 1593 } 1594 offset = g_mediasize(fd) - g_sectorsize(fd); 1595 if (offset <= 0) { 1596 gctl_error(req, 1597 "Cannot obtain media size or sector size for provider %s: %s.", 1598 prov, strerror(errno)); 1599 (void)g_close(fd); 1600 return; 1601 } 1602 (void)eli_trash_metadata(req, prov, fd, offset); 1603 (void)g_close(fd); 1604 } 1605 1606 static void 1607 eli_kill(struct gctl_req *req) 1608 { 1609 const char *prov; 1610 int i, nargs, all; 1611 1612 nargs = gctl_get_int(req, "nargs"); 1613 all = gctl_get_int(req, "all"); 1614 if (!all && nargs == 0) { 1615 gctl_error(req, "Too few arguments."); 1616 return; 1617 } 1618 /* 1619 * How '-a' option combine with a list of providers: 1620 * Delete Master Keys from all attached providers: 1621 * geli kill -a 1622 * Delete Master Keys from all attached providers and from 1623 * detached da0 and da1: 1624 * geli kill -a da0 da1 1625 * Delete Master Keys from (attached or detached) da0 and da1: 1626 * geli kill da0 da1 1627 */ 1628 1629 /* First detached providers. */ 1630 for (i = 0; i < nargs; i++) { 1631 prov = gctl_get_ascii(req, "arg%d", i); 1632 if (!eli_is_attached(prov)) 1633 eli_kill_detached(req, prov); 1634 } 1635 /* Now attached providers. */ 1636 gctl_issue(req); 1637 } 1638 1639 static int 1640 eli_backup_create(struct gctl_req *req, const char *prov, const char *file) 1641 { 1642 unsigned char *sector; 1643 ssize_t secsize; 1644 int error, filefd, ret; 1645 1646 ret = -1; 1647 filefd = -1; 1648 sector = NULL; 1649 secsize = 0; 1650 1651 secsize = g_get_sectorsize(prov); 1652 if (secsize == 0) { 1653 gctl_error(req, "Cannot get informations about %s: %s.", prov, 1654 strerror(errno)); 1655 goto out; 1656 } 1657 sector = malloc(secsize); 1658 if (sector == NULL) { 1659 gctl_error(req, "Cannot allocate memory."); 1660 goto out; 1661 } 1662 /* Read metadata from the provider. */ 1663 error = g_metadata_read(prov, sector, secsize, G_ELI_MAGIC); 1664 if (error != 0) { 1665 gctl_error(req, "Unable to read metadata from %s: %s.", prov, 1666 strerror(error)); 1667 goto out; 1668 } 1669 1670 filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600); 1671 if (filefd == -1) { 1672 gctl_error(req, "Unable to open %s: %s.", file, 1673 strerror(errno)); 1674 goto out; 1675 } 1676 /* Write metadata to the destination file. */ 1677 if (write(filefd, sector, secsize) != secsize) { 1678 gctl_error(req, "Unable to write to %s: %s.", file, 1679 strerror(errno)); 1680 (void)close(filefd); 1681 (void)unlink(file); 1682 goto out; 1683 } 1684 (void)fsync(filefd); 1685 (void)close(filefd); 1686 /* Success. */ 1687 ret = 0; 1688 out: 1689 if (sector != NULL) { 1690 explicit_bzero(sector, secsize); 1691 free(sector); 1692 } 1693 return (ret); 1694 } 1695 1696 static void 1697 eli_backup(struct gctl_req *req) 1698 { 1699 const char *file, *prov; 1700 int nargs; 1701 1702 nargs = gctl_get_int(req, "nargs"); 1703 if (nargs != 2) { 1704 gctl_error(req, "Invalid number of arguments."); 1705 return; 1706 } 1707 prov = gctl_get_ascii(req, "arg0"); 1708 file = gctl_get_ascii(req, "arg1"); 1709 1710 eli_backup_create(req, prov, file); 1711 } 1712 1713 static void 1714 eli_restore(struct gctl_req *req) 1715 { 1716 struct g_eli_metadata md; 1717 const char *file, *prov; 1718 off_t mediasize; 1719 int nargs; 1720 1721 nargs = gctl_get_int(req, "nargs"); 1722 if (nargs != 2) { 1723 gctl_error(req, "Invalid number of arguments."); 1724 return; 1725 } 1726 file = gctl_get_ascii(req, "arg0"); 1727 prov = gctl_get_ascii(req, "arg1"); 1728 1729 /* Read metadata from the backup file. */ 1730 if (eli_metadata_read(req, file, &md) == -1) 1731 return; 1732 /* Obtain provider's mediasize. */ 1733 mediasize = g_get_mediasize(prov); 1734 if (mediasize == 0) { 1735 gctl_error(req, "Cannot get informations about %s: %s.", prov, 1736 strerror(errno)); 1737 return; 1738 } 1739 /* Check if the provider size has changed since we did the backup. */ 1740 if (md.md_provsize != (uint64_t)mediasize) { 1741 if (gctl_get_int(req, "force")) { 1742 md.md_provsize = mediasize; 1743 } else { 1744 gctl_error(req, "Provider size mismatch: " 1745 "wrong backup file?"); 1746 return; 1747 } 1748 } 1749 /* Write metadata to the provider. */ 1750 (void)eli_metadata_store(req, prov, &md); 1751 } 1752 1753 static void 1754 eli_resize(struct gctl_req *req) 1755 { 1756 struct g_eli_metadata md; 1757 const char *prov; 1758 unsigned char *sector; 1759 ssize_t secsize; 1760 off_t mediasize, oldsize; 1761 int error, nargs, provfd; 1762 1763 nargs = gctl_get_int(req, "nargs"); 1764 if (nargs != 1) { 1765 gctl_error(req, "Invalid number of arguments."); 1766 return; 1767 } 1768 prov = gctl_get_ascii(req, "arg0"); 1769 1770 provfd = -1; 1771 sector = NULL; 1772 secsize = 0; 1773 1774 provfd = g_open(prov, 1); 1775 if (provfd == -1) { 1776 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno)); 1777 goto out; 1778 } 1779 1780 mediasize = g_mediasize(provfd); 1781 secsize = g_sectorsize(provfd); 1782 if (mediasize == -1 || secsize == -1) { 1783 gctl_error(req, "Cannot get information about %s: %s.", prov, 1784 strerror(errno)); 1785 goto out; 1786 } 1787 1788 sector = malloc(secsize); 1789 if (sector == NULL) { 1790 gctl_error(req, "Cannot allocate memory."); 1791 goto out; 1792 } 1793 1794 oldsize = gctl_get_intmax(req, "oldsize"); 1795 if (oldsize < 0 || oldsize > mediasize) { 1796 gctl_error(req, "Invalid oldsize: Out of range."); 1797 goto out; 1798 } 1799 if (oldsize == mediasize) { 1800 gctl_error(req, "Size hasn't changed."); 1801 goto out; 1802 } 1803 1804 /* Read metadata from the 'oldsize' offset. */ 1805 if (pread(provfd, sector, secsize, oldsize - secsize) != secsize) { 1806 gctl_error(req, "Cannot read old metadata: %s.", 1807 strerror(errno)); 1808 goto out; 1809 } 1810 1811 /* Check if this sector contains geli metadata. */ 1812 error = eli_metadata_decode(sector, &md); 1813 switch (error) { 1814 case 0: 1815 break; 1816 case EOPNOTSUPP: 1817 gctl_error(req, 1818 "Provider's %s metadata version %u is too new.\n" 1819 "geli: The highest supported version is %u.", 1820 prov, (unsigned int)md.md_version, G_ELI_VERSION); 1821 goto out; 1822 case EINVAL: 1823 gctl_error(req, "Inconsistent provider's %s metadata.", prov); 1824 goto out; 1825 default: 1826 gctl_error(req, 1827 "Unexpected error while decoding provider's %s metadata: %s.", 1828 prov, strerror(error)); 1829 goto out; 1830 } 1831 1832 /* 1833 * If the old metadata doesn't have a correct provider size, refuse 1834 * to resize. 1835 */ 1836 if (md.md_provsize != (uint64_t)oldsize) { 1837 gctl_error(req, "Provider size mismatch at oldsize."); 1838 goto out; 1839 } 1840 1841 /* 1842 * Update the old metadata with the current provider size and write 1843 * it back to the correct place on the provider. 1844 */ 1845 md.md_provsize = mediasize; 1846 /* Write metadata to the provider. */ 1847 (void)eli_metadata_store(req, prov, &md); 1848 /* Now trash the old metadata. */ 1849 (void)eli_trash_metadata(req, prov, provfd, oldsize - secsize); 1850 out: 1851 if (provfd != -1) 1852 (void)g_close(provfd); 1853 if (sector != NULL) { 1854 explicit_bzero(sector, secsize); 1855 free(sector); 1856 } 1857 } 1858 1859 static void 1860 eli_version(struct gctl_req *req) 1861 { 1862 struct g_eli_metadata md; 1863 const char *name; 1864 unsigned int version; 1865 int error, i, nargs; 1866 1867 nargs = gctl_get_int(req, "nargs"); 1868 1869 if (nargs == 0) { 1870 unsigned int kernver; 1871 ssize_t size; 1872 1873 size = sizeof(kernver); 1874 if (sysctlbyname("kern.geom.eli.version", &kernver, &size, 1875 NULL, 0) == -1) { 1876 warn("Unable to obtain GELI kernel version"); 1877 } else { 1878 printf("kernel: %u\n", kernver); 1879 } 1880 printf("userland: %u\n", G_ELI_VERSION); 1881 return; 1882 } 1883 1884 for (i = 0; i < nargs; i++) { 1885 name = gctl_get_ascii(req, "arg%d", i); 1886 error = g_metadata_read(name, (unsigned char *)&md, 1887 sizeof(md), G_ELI_MAGIC); 1888 if (error != 0) { 1889 warn("%s: Unable to read metadata: %s.", name, 1890 strerror(error)); 1891 gctl_error(req, "Not fully done."); 1892 continue; 1893 } 1894 version = le32dec(&md.md_version); 1895 printf("%s: %u\n", name, version); 1896 } 1897 } 1898 1899 static void 1900 eli_clear(struct gctl_req *req) 1901 { 1902 const char *name; 1903 int error, i, nargs; 1904 1905 nargs = gctl_get_int(req, "nargs"); 1906 if (nargs < 1) { 1907 gctl_error(req, "Too few arguments."); 1908 return; 1909 } 1910 1911 for (i = 0; i < nargs; i++) { 1912 name = gctl_get_ascii(req, "arg%d", i); 1913 error = g_metadata_clear(name, G_ELI_MAGIC); 1914 if (error != 0) { 1915 fprintf(stderr, "Cannot clear metadata on %s: %s.\n", 1916 name, strerror(error)); 1917 gctl_error(req, "Not fully done."); 1918 continue; 1919 } 1920 if (verbose) 1921 printf("Metadata cleared on %s.\n", name); 1922 } 1923 } 1924 1925 static void 1926 eli_dump(struct gctl_req *req) 1927 { 1928 struct g_eli_metadata md; 1929 const char *name; 1930 int i, nargs; 1931 1932 nargs = gctl_get_int(req, "nargs"); 1933 if (nargs < 1) { 1934 gctl_error(req, "Too few arguments."); 1935 return; 1936 } 1937 1938 for (i = 0; i < nargs; i++) { 1939 name = gctl_get_ascii(req, "arg%d", i); 1940 if (eli_metadata_read(NULL, name, &md) == -1) { 1941 gctl_error(req, "Not fully done."); 1942 continue; 1943 } 1944 printf("Metadata on %s:\n", name); 1945 eli_metadata_dump(&md); 1946 printf("\n"); 1947 } 1948 } 1949