4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (C) 2015, Trustees of Indiana University
25 * Copyright (c) 2016, Intel Corporation.
27 * Author: Jeremy Filizetti <jfilizet@iu.edu>
40 #include <sys/types.h>
43 #include <lnet/nidstr.h>
44 #include <lustre/lustre_idl.h>
53 /* One week default expiration */
54 #define SK_DEFAULT_EXPIRE 604800
55 #define SK_DEFAULT_SK_KEYLEN 256
56 #define SK_DEFAULT_PRIME_BITS 2048
57 #define SK_DEFAULT_NODEMAP "default"
59 /* Names match up with openssl enc and dgst commands */
60 char *sk_crypt2name[] = {
61 [SK_CRYPT_EMPTY] = "NONE",
62 [SK_CRYPT_AES256_CTR] = "AES-256-CTR",
65 char *sk_hmac2name[] = {
66 [SK_HMAC_EMPTY] = "NONE",
67 [SK_HMAC_SHA256] = "SHA256",
68 [SK_HMAC_SHA512] = "SHA512",
71 static int sk_name2crypt(char *name)
75 for (i = 0; i < SK_CRYPT_MAX; i++) {
76 if (strcasecmp(name, sk_crypt2name[i]) == 0)
80 return SK_CRYPT_INVALID;
83 static int sk_name2hmac(char *name)
87 for (i = 0; i < SK_HMAC_MAX; i++) {
88 if (strcasecmp(name, sk_hmac2name[i]) == 0)
92 return SK_HMAC_INVALID;
95 static void usage(FILE *fp, char *program)
99 fprintf(fp, "Usage %s [OPTIONS] {-l|-m|-r|-w} <keyfile>\n", program);
100 fprintf(fp, "-l|--load <keyfile> Load key from file into user's "
101 "session keyring\n");
102 fprintf(fp, "-m|--modify <keyfile> Modify keyfile's attributes\n");
103 fprintf(fp, "-r|--read <keyfile> Show keyfile's attributes\n");
104 fprintf(fp, "-w|--write <keyfile> Generate keyfile\n\n");
105 fprintf(fp, "Modify/Write Options:\n");
106 fprintf(fp, "-c|--crypt <num> Cipher for encryption "
107 "(Default: AES Counter mode)\n");
108 for (i = 1; i < SK_CRYPT_MAX; i++)
109 fprintf(fp, " %s\n", sk_crypt2name[i]);
111 fprintf(fp, "-i|--hmac <num> Hash algorithm for integrity "
112 "(Default: SHA256)\n");
113 for (i = 1; i < SK_HMAC_MAX; i++)
114 fprintf(fp, " %s\n", sk_hmac2name[i]);
116 fprintf(fp, "-e|--expire <num> Seconds before contexts from "
117 "key expire (Default: %d seconds (%.3g days))\n",
118 SK_DEFAULT_EXPIRE, (double)SK_DEFAULT_EXPIRE / 3600 / 24);
119 fprintf(fp, "-f|--fsname <name> File system name for key\n");
120 fprintf(fp, "-g|--mgsnids <nids> Comma seperated list of MGS "
121 "NIDs. Only required when mgssec is used (Default: \"\")\n");
122 fprintf(fp, "-n|--nodemap <name> Nodemap name for key "
123 "(Default: \"%s\")\n", SK_DEFAULT_NODEMAP);
124 fprintf(fp, "-p|--prime-bits <len> Prime length (p) for DHKE in "
125 "bits (Default: %d)\n", SK_DEFAULT_PRIME_BITS);
126 fprintf(fp, "-t|--type <type> Key type (mgs, server, "
128 fprintf(fp, "-k|--key-bits <len> Shared key length in bits "
129 "(Default: %d)\n", SK_DEFAULT_SK_KEYLEN);
130 fprintf(fp, "-d|--data <file> Key random data source "
131 "(Default: /dev/random)\n\n");
132 fprintf(fp, "Other Options:\n");
133 fprintf(fp, "-v|--verbose Increase verbosity for errors\n");
137 static ssize_t get_key_data(char *src, void *buffer, size_t bits)
144 /* convert bits to minimum number of bytes */
145 remain = (bits + 7) / 8;
147 printf("Reading random data for shared key from '%s'\n", src);
148 fd = open(src, O_RDONLY);
150 fprintf(stderr, "error: opening '%s': %s\n", src,
156 rc = read(fd, ptr, remain);
160 fprintf(stderr, "error: reading from '%s': %s\n", src,
165 } else if (rc == 0) {
167 "error: key source too short for %zd-bit key\n",
182 static int write_config_file(char *output_file,
183 struct sk_keyfile_config *config, bool overwrite)
187 int flags = O_WRONLY | O_CREAT;
192 sk_config_cpu_to_disk(config);
194 fd = open(output_file, flags, 0400);
196 fprintf(stderr, "error: opening '%s': %s\n", output_file,
201 rc = write(fd, config, sizeof(*config));
203 fprintf(stderr, "error: writing to '%s': %s\n", output_file,
206 } else if (rc != sizeof(*config)) {
207 fprintf(stderr, "error: short write to '%s'\n", output_file);
218 static int print_config(char *filename)
220 struct sk_keyfile_config *config;
223 config = sk_read_file(filename);
227 if (sk_validate_config(config)) {
228 fprintf(stderr, "error: key configuration failed validation\n");
233 printf("Version: %u\n", config->skc_version);
235 if (config->skc_type & SK_TYPE_MGS)
237 if (config->skc_type & SK_TYPE_SERVER)
239 if (config->skc_type & SK_TYPE_CLIENT)
242 printf("HMAC alg: %s\n", sk_hmac2name[config->skc_hmac_alg]);
243 printf("Crypto alg: %s\n", sk_crypt2name[config->skc_crypt_alg]);
244 printf("Ctx Expiration: %u seconds\n", config->skc_expire);
245 printf("Shared keylen: %u bits\n", config->skc_shared_keylen);
246 printf("Prime length: %u bits\n", config->skc_prime_bits);
247 printf("File system: %s\n", config->skc_fsname);
248 printf("MGS NIDs: ");
249 for (i = 0; i < MAX_MGSNIDS; i++) {
250 if (config->skc_mgsnids[i] == LNET_NID_ANY)
252 printf(" %s", libcfs_nid2str(config->skc_mgsnids[i]));
255 printf("Nodemap name: %s\n", config->skc_nodemap);
256 printf("Shared key:\n");
257 print_hex(0, config->skc_shared_key, config->skc_shared_keylen / 8);
259 /* Don't print empty keys */
260 for (i = 0; i < SK_MAX_P_BYTES; i++)
261 if (config->skc_p[i] != 0)
264 if (i != SK_MAX_P_BYTES) {
265 printf("Prime (p):\n");
266 print_hex(0, config->skc_p, config->skc_prime_bits / 8);
273 static int parse_mgsnids(char *mgsnids, struct sk_keyfile_config *config)
282 /* replace all old values */
283 for (i = 0; i < MAX_MGSNIDS; i++)
284 config->skc_mgsnids[i] = LNET_NID_ANY;
287 end = mgsnids + strlen(mgsnids);
289 while (ptr < end && i < MAX_MGSNIDS) {
290 sep = strstr(ptr, ",");
294 nid = libcfs_str2nid(ptr);
295 if (nid == LNET_NID_ANY) {
296 fprintf(stderr, "error: invalid MGS NID: %s\n", ptr);
301 config->skc_mgsnids[i++] = nid;
302 ptr += strlen(ptr) + 1;
305 if (i == MAX_MGSNIDS) {
306 fprintf(stderr, "error: more than %u MGS NIDs provided\n", i);
313 int main(int argc, char **argv)
315 struct sk_keyfile_config *config;
316 char *datafile = NULL;
321 char *mgsnids = NULL;
322 char *nodemap = NULL;
326 int crypt = SK_CRYPT_EMPTY;
327 int hmac = SK_HMAC_EMPTY;
329 int shared_keylen = -1;
334 enum sk_key_type type = SK_TYPE_INVALID;
335 bool generate_prime = false;
338 static struct option long_opts[] = {
339 { .name = "crypt", .has_arg = required_argument, .val = 'c'},
340 { .name = "data", .has_arg = required_argument, .val = 'd'},
341 { .name = "expire", .has_arg = required_argument, .val = 'e'},
342 { .name = "fsname", .has_arg = required_argument, .val = 'f'},
343 { .name = "mgsnids", .has_arg = required_argument, .val = 'g'},
344 { .name = "help", .has_arg = no_argument, .val = 'h'},
345 { .name = "hmac", .has_arg = required_argument, .val = 'i'},
346 { .name = "integrity", .has_arg = required_argument, .val = 'i'},
347 { .name = "key-bits", .has_arg = required_argument, .val = 'k'},
348 { .name = "shared", .has_arg = required_argument, .val = 'k'},
349 { .name = "load", .has_arg = required_argument, .val = 'l'},
350 { .name = "modify", .has_arg = required_argument, .val = 'm'},
351 { .name = "nodemap", .has_arg = required_argument, .val = 'n'},
352 { .name = "prime-bits", .has_arg = required_argument, .val = 'p'},
353 { .name = "read", .has_arg = required_argument, .val = 'r'},
354 { .name = "type", .has_arg = required_argument, .val = 't'},
355 { .name = "verbose", .has_arg = no_argument, .val = 'v'},
356 { .name = "write", .has_arg = required_argument, .val = 'w'},
359 while ((opt = getopt_long(argc, argv,
360 "c:d:e:f:g:hi:l:m:n:p:r:s:k:t:w:v", long_opts,
364 crypt = sk_name2crypt(optarg);
370 expire = atoi(optarg);
372 fprintf(stderr, "warning: using a %us key "
373 "expiration may cause issues during "
374 "key renegotiation\n", expire);
378 if (strlen(fsname) > MTI_NAME_MAXLEN) {
380 "error: file system name longer than "
381 "%u characters\n", MTI_NAME_MAXLEN);
389 usage(stdout, argv[0]);
392 hmac = sk_name2hmac(optarg);
395 shared_keylen = atoi(optarg);
405 if (strlen(nodemap) > LUSTRE_NODEMAP_NAME_LENGTH) {
407 "error: nodemap name longer than "
409 LUSTRE_NODEMAP_NAME_LENGTH);
414 prime_bits = atoi(optarg);
415 if (prime_bits <= 0) {
417 "error: invalid prime length: '%s'\n",
426 tmp2 = strdup(optarg);
429 "error: failed to allocate type\n");
432 tmp = strsep(&tmp2, ",");
433 while (tmp != NULL) {
434 if (strcasecmp(tmp, "server") == 0) {
435 type |= SK_TYPE_SERVER;
436 } else if (strcasecmp(tmp, "mgs") == 0) {
438 } else if (strcasecmp(tmp, "client") == 0) {
439 type |= SK_TYPE_CLIENT;
442 "error: invalid type '%s', "
443 "must be mgs, server, or client"
447 tmp = strsep(&tmp2, ",");
458 fprintf(stderr, "error: unknown option: '%c'\n", opt);
464 if (optind != argc) {
466 "error: extraneous arguments provided, check usage\n");
470 if (!input && !output && !load && !modify) {
471 usage(stderr, argv[0]);
475 /* init gss logger for foreground (no syslog) which prints to stderr */
476 initerr(NULL, verbose, 1);
479 return print_config(input);
482 if (sk_load_keyfile(load))
487 if (crypt == SK_CRYPT_INVALID) {
488 fprintf(stderr, "error: invalid crypt algorithm specified\n");
491 if (hmac == SK_HMAC_INVALID) {
492 fprintf(stderr, "error: invalid HMAC algorithm specified\n");
497 config = sk_read_file(modify);
501 if (type != SK_TYPE_INVALID) {
502 /* generate key when adding client type */
503 if (!(config->skc_type & SK_TYPE_CLIENT) &&
504 type & SK_TYPE_CLIENT)
505 generate_prime = true;
506 else if (!(type & SK_TYPE_CLIENT))
507 memset(config->skc_p, 0, SK_MAX_P_BYTES);
509 config->skc_type = type;
511 if (prime_bits != -1) {
512 memset(config->skc_p, 0, SK_MAX_P_BYTES);
513 if (config->skc_prime_bits != prime_bits &&
514 config->skc_type & SK_TYPE_CLIENT)
515 generate_prime = true;
518 /* write mode for a new key */
519 if (!fsname && !mgsnids) {
521 "error: missing --fsname or --mgsnids\n");
525 config = calloc(1, sizeof(*config));
529 /* Set the defaults for new key */
530 config->skc_version = SK_CONF_VERSION;
531 config->skc_expire = SK_DEFAULT_EXPIRE;
532 config->skc_shared_keylen = SK_DEFAULT_SK_KEYLEN;
533 config->skc_prime_bits = SK_DEFAULT_PRIME_BITS;
534 config->skc_crypt_alg = SK_CRYPT_AES256_CTR;
535 config->skc_hmac_alg = SK_HMAC_SHA256;
536 for (i = 0; i < MAX_MGSNIDS; i++)
537 config->skc_mgsnids[i] = LNET_NID_ANY;
539 if (type == SK_TYPE_INVALID) {
540 fprintf(stderr, "error: no type specified for key\n");
543 config->skc_type = type;
544 generate_prime = type & SK_TYPE_CLIENT;
546 strncpy(config->skc_nodemap, SK_DEFAULT_NODEMAP,
547 strlen(SK_DEFAULT_NODEMAP));
550 datafile = "/dev/random";
553 if (crypt != SK_CRYPT_EMPTY)
554 config->skc_crypt_alg = crypt;
555 if (hmac != SK_HMAC_EMPTY)
556 config->skc_hmac_alg = hmac;
558 config->skc_expire = expire;
559 if (shared_keylen != -1)
560 config->skc_shared_keylen = shared_keylen;
561 if (prime_bits != -1)
562 config->skc_prime_bits = prime_bits;
564 strncpy(config->skc_fsname, fsname, strlen(fsname));
566 strncpy(config->skc_nodemap, nodemap, strlen(nodemap));
567 if (mgsnids && parse_mgsnids(mgsnids, config))
569 if (sk_validate_config(config)) {
570 fprintf(stderr, "error: key configuration failed validation\n");
574 if (datafile && get_key_data(datafile, config->skc_shared_key,
575 config->skc_shared_keylen)) {
576 fprintf(stderr, "error: failure getting key data from '%s'\n",
581 if (generate_prime) {
582 printf("Generating DH parameters, this can take a while...\n");
583 dh = DH_generate_parameters(config->skc_prime_bits,
584 SK_GENERATOR, NULL, NULL);
585 if (BN_num_bytes(dh->p) > SK_MAX_P_BYTES) {
586 fprintf(stderr, "error: cannot generate DH parameters: "
587 "requested length %d exceeds maximum %d\n",
588 config->skc_prime_bits, SK_MAX_P_BYTES * 8);
591 if (BN_bn2bin(dh->p, config->skc_p) != BN_num_bytes(dh->p)) {
593 "error: convert BIGNUM p to binary failed\n");
598 if (write_config_file(modify ?: output, config, modify))