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, 2017, Intel Corporation.
27 * Author: Jeremy Filizetti <jfilizet@iu.edu>
36 /* We need to use some deprecated APIs */
37 #define OPENSSL_SUPPRESS_DEPRECATED
38 #include <openssl/dh.h>
39 #include <openssl/engine.h>
40 #include <openssl/err.h>
41 #include <openssl/hmac.h>
42 #include <sys/types.h>
44 #include <libcfs/util/string.h>
48 #include "write_bytes.h"
50 #define SK_PBKDF2_ITERATIONS 10000
53 # include "lgss_utils.h"
55 # include "gss_util.h"
56 # include "gss_oids.h"
57 # include "err_util.h"
63 * \param[in] program Program name to output
64 * \param[in] verbose Verbose flag
65 * \param[in] fg Whether or not to run in foreground
68 void sk_init_logging(char *program, int verbose, int fg)
70 initerr(program, verbose, fg);
75 * Loads the key from \a filename and returns the struct sk_keyfile_config.
76 * It should be freed by the caller.
78 * \param[in] filename Disk or key payload data
80 * \return sk_keyfile_config sucess
81 * \return NULL failure
83 struct sk_keyfile_config *sk_read_file(char *filename)
85 struct sk_keyfile_config *config;
91 config = malloc(sizeof(*config));
93 printerr(0, "Failed to allocate memory for config\n");
97 /* allow standard input override */
98 if (strcmp(filename, "-") == 0)
101 fd = open(filename, O_RDONLY);
104 printerr(0, "Error opening key file '%s': %s\n", filename,
107 } else if (fd != STDIN_FILENO) {
111 if (rc == 0 && (st.st_mode & ~(S_IFREG | 0600)))
112 fprintf(stderr, "warning: "
113 "secret key '%s' has insecure file mode %#o\n",
114 filename, st.st_mode);
117 ptr = (char *)config;
118 remain = sizeof(*config);
120 rc = read(fd, ptr, remain);
124 printerr(0, "read() failed on %s: %s\n", filename,
127 } else if (rc == 0) {
128 printerr(0, "File %s does not have a complete key\n",
136 if (fd != STDIN_FILENO)
138 sk_config_disk_to_cpu(config);
149 * Checks if a key matching \a description is found in the keyring for
150 * logging purposes and then attempts to load \a payload of \a psize into a key
151 * with \a description.
153 * \param[in] payload Key payload
154 * \param[in] psize Payload size
155 * \param[in] description Description used for key in keyring
160 static key_serial_t sk_load_key(const struct sk_keyfile_config *skc,
161 const char *description)
163 struct sk_keyfile_config payload;
166 memcpy(&payload, skc, sizeof(*skc));
168 /* In the keyring use the disk layout so keyctl pipe can be used */
169 sk_config_cpu_to_disk(&payload);
171 /* Check to see if a key is already loaded matching description */
172 key = keyctl_search(KEY_SPEC_USER_KEYRING, "user", description, 0);
174 printerr(2, "Key %d found in session keyring, replacing\n",
177 key = add_key("user", description, &payload, sizeof(payload),
178 KEY_SPEC_USER_KEYRING);
180 key_perm_t perm = KEY_POS_ALL | KEY_USR_ALL |
181 KEY_GRP_ALL | KEY_OTH_ALL;
183 if (keyctl_setperm(key, perm) < 0)
184 printerr(2, "Failed to set perm 0x%x on key %d\n",
186 printerr(2, "Added key %d with description %s\n", key,
189 printerr(0, "Failed to add key with %s\n", description);
196 * Reads the key from \a path, verifies it and loads into the session keyring
197 * using a description determined by the the \a type. Existing keys with the
198 * same description are replaced.
200 * \param[in] path Path to key file
201 * \param[in] type Type of key to load which determines the description
206 int sk_load_keyfile(char *path)
208 struct sk_keyfile_config *config;
209 char description[SK_DESCRIPTION_SIZE + 1];
215 rc = stat(path, &buf);
217 printerr(0, "stat() failed for file %s: %s\n", path,
222 config = sk_read_file(path);
226 /* Similar to ssh, require adequate care of key files */
227 if (buf.st_mode & (S_IRGRP | S_IWGRP | S_IWOTH | S_IXOTH)) {
228 printerr(0, "Shared key files must be read/writeable only by "
233 if (sk_validate_config(config))
236 /* The server side can have multiple key files per file system so
237 * the nodemap name is appended to the key description to uniquely
239 if (config->skc_type & SK_TYPE_MGS) {
240 /* Any key can be an MGS key as long as we are told to use it */
241 rc = snprintf(description, SK_DESCRIPTION_SIZE, "lustre:MGS:%s",
242 config->skc_nodemap);
243 if (rc >= SK_DESCRIPTION_SIZE)
245 if (sk_load_key(config, description) == -1)
248 if (config->skc_type & SK_TYPE_SERVER) {
249 /* Server keys need to have the file system name in the key */
250 if (!config->skc_fsname) {
251 printerr(0, "Key configuration has no file system "
252 "attribute. Can't load as server type\n");
255 rc = snprintf(description, SK_DESCRIPTION_SIZE, "lustre:%s:%s",
256 config->skc_fsname, config->skc_nodemap);
257 if (rc >= SK_DESCRIPTION_SIZE)
259 if (sk_load_key(config, description) == -1)
262 if (config->skc_type & SK_TYPE_CLIENT) {
263 /* Load client file system key */
264 if (config->skc_fsname) {
265 rc = snprintf(description, SK_DESCRIPTION_SIZE,
266 "lustre:%s", config->skc_fsname);
267 if (rc >= SK_DESCRIPTION_SIZE)
269 if (sk_load_key(config, description) == -1)
273 /* Load client MGC keys */
274 for (i = 0; i < MAX_MGSNIDS; i++) {
275 if (config->skc_mgsnids[i] == LNET_NID_ANY)
277 rc = snprintf(description, SK_DESCRIPTION_SIZE,
279 libcfs_nid2str(config->skc_mgsnids[i]));
280 if (rc >= SK_DESCRIPTION_SIZE)
282 if (sk_load_key(config, description) == -1)
295 * Byte swaps config from cpu format to disk
297 * \param[in,out] config sk_keyfile_config to swap
299 void sk_config_cpu_to_disk(struct sk_keyfile_config *config)
306 config->skc_version = htobe32(config->skc_version);
307 config->skc_hmac_alg = htobe16(config->skc_hmac_alg);
308 config->skc_crypt_alg = htobe16(config->skc_crypt_alg);
309 config->skc_expire = htobe32(config->skc_expire);
310 config->skc_shared_keylen = htobe32(config->skc_shared_keylen);
311 config->skc_prime_bits = htobe32(config->skc_prime_bits);
313 for (i = 0; i < MAX_MGSNIDS; i++)
314 config->skc_mgsnids[i] = htobe64(config->skc_mgsnids[i]);
318 * Byte swaps config from disk format to cpu
320 * \param[in,out] config sk_keyfile_config to swap
322 void sk_config_disk_to_cpu(struct sk_keyfile_config *config)
329 config->skc_version = be32toh(config->skc_version);
330 config->skc_hmac_alg = be16toh(config->skc_hmac_alg);
331 config->skc_crypt_alg = be16toh(config->skc_crypt_alg);
332 config->skc_expire = be32toh(config->skc_expire);
333 config->skc_shared_keylen = be32toh(config->skc_shared_keylen);
334 config->skc_prime_bits = be32toh(config->skc_prime_bits);
336 for (i = 0; i < MAX_MGSNIDS; i++)
337 config->skc_mgsnids[i] = be64toh(config->skc_mgsnids[i]);
341 * Verifies the on key payload format is valid
343 * \param[in] config sk_keyfile_config
348 int sk_validate_config(const struct sk_keyfile_config *config)
353 printerr(0, "Null configuration passed\n");
357 if (config->skc_version != SK_CONF_VERSION) {
358 printerr(0, "Invalid version\n");
362 if (config->skc_hmac_alg == SK_HMAC_INVALID) {
363 printerr(0, "Invalid HMAC algorithm\n");
367 if (config->skc_crypt_alg == SK_CRYPT_INVALID) {
368 printerr(0, "Invalid crypt algorithm\n");
372 if (config->skc_expire < 60 || config->skc_expire > INT_MAX) {
373 /* Try to limit key expiration to some reasonable minimum and
374 * also prevent values over INT_MAX because there appears
375 * to be a type conversion issue */
376 printerr(0, "Invalid expiration time should be between %d "
377 "and %d\n", 60, INT_MAX);
380 if (config->skc_prime_bits % 8 != 0 ||
381 config->skc_prime_bits > SK_MAX_P_BYTES * 8) {
382 printerr(0, "Invalid session key length must be a multiple of 8"
383 " and less then %d bits\n",
387 if (config->skc_shared_keylen % 8 != 0 ||
388 config->skc_shared_keylen > SK_MAX_KEYLEN_BYTES * 8){
389 printerr(0, "Invalid shared key max length must be a multiple "
390 "of 8 and less then %d bits\n",
391 SK_MAX_KEYLEN_BYTES * 8);
395 /* Check for terminating nulls on strings */
396 for (i = 0; i < sizeof(config->skc_fsname) &&
397 config->skc_fsname[i] != '\0'; i++)
399 if (i == sizeof(config->skc_fsname)) {
400 printerr(0, "File system name not null terminated\n");
404 for (i = 0; i < sizeof(config->skc_nodemap) &&
405 config->skc_nodemap[i] != '\0'; i++)
407 if (i == sizeof(config->skc_nodemap)) {
408 printerr(0, "Nodemap name not null terminated\n");
412 if (config->skc_type == SK_TYPE_INVALID) {
413 printerr(0, "Invalid key type\n");
421 * Hashes \a string and places the hash in \a hash
424 * \param[in] string Null terminated string to hash
425 * \param[in] hash_alg OpenSSL EVP_MD to use for hash
426 * \param[in,out] hash gss_buffer_desc to hold the result
431 static int sk_hash_string(const char *string, const EVP_MD *hash_alg,
432 gss_buffer_desc *hash)
434 EVP_MD_CTX *ctx = EVP_MD_CTX_create();
435 size_t len = strlen(string);
436 unsigned int hashlen;
438 if (!hash->value || hash->length < EVP_MD_size(hash_alg))
440 if (!EVP_DigestInit_ex(ctx, hash_alg, NULL))
442 if (!EVP_DigestUpdate(ctx, string, len))
444 if (!EVP_DigestFinal_ex(ctx, hash->value, &hashlen))
447 EVP_MD_CTX_destroy(ctx);
448 hash->length = hashlen;
452 EVP_MD_CTX_destroy(ctx);
457 * Hashes \a string and verifies the resulting hash matches the value
460 * \param[in] string Null terminated string to hash
461 * \param[in] hash_alg OpenSSL EVP_MD to use for hash
462 * \param[in,out] current_hash gss_buffer_desc to compare to
464 * \return gss error failure
465 * \return GSS_S_COMPLETE success
467 uint32_t sk_verify_hash(const char *string, const EVP_MD *hash_alg,
468 const gss_buffer_desc *current_hash)
470 gss_buffer_desc hash;
471 unsigned char hashbuf[EVP_MAX_MD_SIZE];
473 hash.value = hashbuf;
474 hash.length = sizeof(hashbuf);
476 if (sk_hash_string(string, hash_alg, &hash))
477 return GSS_S_FAILURE;
478 if (current_hash->length != hash.length)
479 return GSS_S_DEFECTIVE_TOKEN;
480 if (memcmp(current_hash->value, hash.value, hash.length))
481 return GSS_S_BAD_SIG;
483 return GSS_S_COMPLETE;
486 static inline int sk_config_has_mgsnid(struct sk_keyfile_config *config,
492 nid = libcfs_str2nid(mgsnid);
493 if (nid == LNET_NID_ANY)
496 for (i = 0; i < MAX_MGSNIDS; i++)
497 if (config->skc_mgsnids[i] == nid)
503 * Create an sk_cred structure populated with initial configuration info and the
504 * key. \a tgt and \a nodemap are used in determining the expected key
505 * description so the key can be found by searching the keyring.
506 * This is done because there is no easy way to pass keys from the mount command
507 * all the way to the request_key call. In addition any keys can be dynamically
508 * added to the keyrings and still found. The keyring that needs to be used
509 * must be the session keyring.
511 * \param[in] tgt Target file system
512 * \param[in] nodemap Cluster name for the key. This correlates to
513 * the nodemap name and is used by the server side.
514 * For the client this will be NULL.
515 * \param[in] flags Flags for the credentials
517 * \return sk_cred Allocated struct sk_cred on success
518 * \return NULL failure
520 struct sk_cred *sk_create_cred(const char *tgt, const char *nodemap,
521 const uint32_t flags)
523 struct sk_keyfile_config *config;
524 struct sk_kernel_ctx *kctx;
525 struct sk_cred *skc = NULL;
526 char description[SK_DESCRIPTION_SIZE + 1];
527 char fsname[MTI_NAME_MAXLEN + 1];
528 const char *mgsnid = NULL;
535 printerr(2, "Creating credentials for target: %s with nodemap: %s\n",
538 memset(description, 0, sizeof(description));
539 memset(fsname, 0, sizeof(fsname));
541 /* extract the file system name from target */
542 ptr = index(tgt, '-');
546 /* This must be an MGC target */
547 if (strncmp(tgt, "MGC", 3) || len <= 3) {
548 printerr(0, "Invalid target name\n");
556 if (len > MTI_NAME_MAXLEN) {
557 printerr(0, "Invalid target name\n");
560 memcpy(fsname, tgt, len);
564 rc = snprintf(description, SK_DESCRIPTION_SIZE,
565 "lustre:MGS:%s", nodemap);
567 rc = snprintf(description, SK_DESCRIPTION_SIZE,
568 "lustre:%s:%s", fsname, nodemap);
570 rc = snprintf(description, SK_DESCRIPTION_SIZE, "lustre:%s",
574 if (rc >= SK_DESCRIPTION_SIZE) {
575 printerr(0, "Invalid key description\n");
579 /* It may be a good idea to move Lustre keys to the gss_keyring
580 * (lgssc) type so that they expire when Lustre modules are removed.
581 * Unfortunately it can't be done at mount time because the mount
582 * syscall could trigger the Lustre modules to load and until that
583 * point we don't have a lgssc key type.
585 * TODO: Query the community for a consensus here */
586 printerr(2, "Searching for key with description: %s\n", description);
587 sk_key = keyctl_search(KEY_SPEC_USER_KEYRING, "user",
590 printerr(1, "No key found for %s\n", description);
594 keylen = keyctl_read_alloc(sk_key, (void **)&config);
596 printerr(0, "keyctl_read() failed for key %ld: %s\n", sk_key,
599 } else if (keylen != sizeof(*config)) {
600 printerr(0, "Unexpected key size: %d returned for key %ld, "
601 "expected %zu bytes\n",
602 keylen, sk_key, sizeof(*config));
606 sk_config_disk_to_cpu(config);
608 if (sk_validate_config(config)) {
609 printerr(0, "Invalid key configuration for key: %ld\n", sk_key);
613 if (mgsnid && !sk_config_has_mgsnid(config, mgsnid)) {
614 printerr(0, "Target name does not match key's MGS NIDs\n");
618 if (!mgsnid && strcmp(fsname, config->skc_fsname)) {
619 printerr(0, "Target name does not match key's file system\n");
623 skc = malloc(sizeof(*skc));
625 printerr(0, "Failed to allocate memory for sk_cred\n");
629 /* this initializes all gss_buffer_desc to empty as well */
630 memset(skc, 0, sizeof(*skc));
632 skc->sc_flags = flags;
633 skc->sc_tgt.length = strlen(tgt) + 1;
634 skc->sc_tgt.value = malloc(skc->sc_tgt.length);
635 if (!skc->sc_tgt.value) {
636 printerr(0, "Failed to allocate memory for target\n");
639 memcpy(skc->sc_tgt.value, tgt, skc->sc_tgt.length);
641 skc->sc_nodemap_hash.length = EVP_MD_size(EVP_sha256());
642 skc->sc_nodemap_hash.value = malloc(skc->sc_nodemap_hash.length);
643 if (!skc->sc_nodemap_hash.value) {
644 printerr(0, "Failed to allocate memory for nodemap hash\n");
648 if (sk_hash_string(config->skc_nodemap, EVP_sha256(),
649 &skc->sc_nodemap_hash)) {
650 printerr(0, "Failed to generate hash for nodemap name\n");
654 kctx = &skc->sc_kctx;
655 kctx->skc_version = config->skc_version;
656 strcpy(kctx->skc_hmac_alg, sk_hmac2name(config->skc_hmac_alg));
657 strcpy(kctx->skc_crypt_alg, sk_crypt2name(config->skc_crypt_alg));
658 kctx->skc_expire = config->skc_expire;
660 /* key payload format is in bits, convert to bytes */
661 kctx->skc_shared_key.length = config->skc_shared_keylen / 8;
662 kctx->skc_shared_key.value = malloc(kctx->skc_shared_key.length);
663 if (!kctx->skc_shared_key.value) {
664 printerr(0, "Failed to allocate memory for shared key\n");
667 memcpy(kctx->skc_shared_key.value, config->skc_shared_key,
668 kctx->skc_shared_key.length);
670 skc->sc_p.length = config->skc_prime_bits / 8;
671 skc->sc_p.value = malloc(skc->sc_p.length);
672 if (!skc->sc_p.value) {
673 printerr(0, "Failed to allocate p\n");
676 memcpy(skc->sc_p.value, config->skc_p, skc->sc_p.length);
689 #define SK_GENERATOR 2
690 #define DH_NUMBER_ITERATIONS_FOR_PRIME 64
692 /* OpenSSL 1.1.1c increased the number of rounds used for Miller-Rabin testing
693 * of the prime provided as input parameter to DH_check(). This makes the check
694 * roughly x10 longer, and causes request timeouts when an SSK flavor is being
697 * Instead, use a dynamic number Miller-Rabin rounds based on the speed of the
698 * check on the current system, evaluated when the lsvcgssd daemon starts, but
699 * at least as many as OpenSSL 1.1.1b used for the same key size. If default
700 * DH_check() duration is OK, use it directly instead of limiting the rounds.
702 * If \a num_rounds == 0, we just call original DH_check() directly.
704 static bool sk_is_dh_valid(const DH *dh, int num_rounds)
713 if (num_rounds == 0) {
716 rc = DH_check(dh, &codes);
717 if (rc != 1 || codes) {
718 printerr(0, "DH_check(0) failed: codes=%#x: rc=%d\n",
725 DH_get0_pqg(dh, &p, NULL, &g);
727 if (!BN_is_word(g, SK_GENERATOR)) {
728 printerr(0, "%s: Diffie-Hellman generator is not %u\n",
729 program_invocation_short_name, SK_GENERATOR);
733 word = BN_mod_word(p, 24);
735 printerr(0, "%s: Diffie-Hellman prime modulo=%lu unsuitable\n",
736 program_invocation_short_name, word);
742 printerr(0, "%s: Diffie-Hellman error allocating context\n",
743 program_invocation_short_name);
747 r = BN_CTX_get(ctx); /* must be called before "ctx" used elsewhere */
749 rc = BN_is_prime_ex(p, num_rounds, ctx, NULL);
751 printerr(0, "%s: Diffie-Hellman 'p' not prime in %u rounds\n",
752 program_invocation_short_name, num_rounds);
756 if (!BN_rshift1(r, p)) {
757 printerr(0, "%s: error shifting BigNum 'r' by 'p'\n",
758 program_invocation_short_name);
761 rc = BN_is_prime_ex(r, num_rounds, ctx, NULL);
763 printerr(0, "%s: Diffie-Hellman 'r' not prime in %u rounds\n",
764 program_invocation_short_name, num_rounds);
777 #define VALUE_LENGTH 256
778 static unsigned char test_prime[VALUE_LENGTH] =
779 "\xf7\xfa\x49\xd8\xec\xb1\x3b\xff\x26\x10\x3f\xc5\x3a\xc5\xcc\x40"
780 "\x4f\xbf\x92\xe1\x8b\x83\xe7\xa2\xba\x0f\x51\x5a\x91\x48\xe0\xa3"
781 "\xf1\x4d\xbc\xbb\x8a\x28\x14\xac\x02\x23\x76\x42\x17\x4d\x3c\xdc"
782 "\x5e\x4f\x80\x1f\xd7\x54\x1c\x50\xac\x3b\x28\x68\x8d\x71\x41\x7f"
783 "\xa7\x1c\x2f\x22\xd3\xa8\x91\xb2\x64\xb6\x84\xa6\xcf\x06\x16\x91"
784 "\x2f\xb8\xb4\x42\x1d\x3a\x4e\x3a\x0c\x7f\x04\x69\x78\xb5\x8f\x92"
785 "\x07\x89\xac\x24\x06\x53\x2c\x23\xec\xaa\x5c\xb4\x7b\x49\xbc\xf4"
786 "\x90\x67\x71\x9c\x24\x2c\x1d\x8d\x76\xc8\x85\x4e\x19\xf1\xf9\x33"
787 "\x45\xbd\x9f\x7d\x0a\x08\x8c\x22\xcc\x35\xf3\x5b\xab\x3f\x24\x9d"
788 "\x61\x70\x86\xbb\xbe\xd8\xb0\xf8\x34\xfa\xeb\x5b\x8e\xf2\x62\x23"
789 "\xd1\xfb\xbb\xb8\x21\x71\x1e\x39\x39\x59\xe0\x82\x98\x41\x84\x40"
790 "\x1f\xd3\x9b\xa3\x73\xdb\xec\xe0\xc0\xde\x2d\x1c\xea\x43\x40\x93"
791 "\x98\x38\x03\x36\x1e\xe1\xe7\x39\x7b\x35\x92\x4a\x51\xa5\x91\x63"
792 "\xd5\x31\x98\x3d\x89\x27\x6f\xcc\x69\xff\xbe\x31\x13\xdc\x2f\x72"
793 "\x2d\xab\x6a\xb7\x13\xd3\x47\xda\xaa\xf3\x3c\xa0\xfd\xaa\x0f\x02"
794 "\x96\x81\x1a\x26\xe8\xf7\x25\x65\x33\x78\xd9\x6b\x6d\xb0\xd9\xfb";
797 * Measure time taken by prime testing routine for a 2048 bit long prime,
798 * depending on the number of check rounds.
800 * \param[in] usec_check_max max time allowed for DH_check completion
802 * \retval max number of rounds to keep prime testing under usec_check_max
803 * return 0 if we should use the default DH_check rounds
805 int sk_speedtest_dh_valid(unsigned int usec_check_max)
809 int num_rounds, prev_rounds = 0;
815 p = BN_bin2bn(test_prime, VALUE_LENGTH, NULL);
823 if (!BN_set_word(g, SK_GENERATOR))
826 /* "dh" takes over freeing of 'p' and 'g' if this succeeds */
827 if (!DH_set0_pqg(dh, p, NULL, g)) {
836 num_rounds <= DH_NUMBER_ITERATIONS_FOR_PRIME;
837 num_rounds += (num_rounds <= 4 ? 4 : 8)) {
838 unsigned int usec_this;
841 /* get max duration of 4 runs at current number of rounds */
843 for (j = 0; j < 4; j++) {
844 struct timeval now, prev;
845 unsigned int usec_curr;
847 gettimeofday(&prev, NULL);
848 if (!sk_is_dh_valid(dh, num_rounds)) {
849 /* if test_prime is found bad, use default */
853 gettimeofday(&now, NULL);
854 usec_curr = (now.tv_sec - prev.tv_sec) * 1000000 +
855 now.tv_usec - prev.tv_usec;
856 if (usec_curr > usec_this)
857 usec_this = usec_curr;
859 printerr(2, "%s: %d rounds: %d usec\n",
860 program_invocation_short_name, num_rounds, usec_this);
861 if (num_rounds == 0) {
862 if (usec_this <= usec_check_max)
863 /* using original check rounds as implemented in
864 * DH_check() took less time than the max allowed,
865 * so just use original DH_check()
868 } else if (usec_this >= usec_check_max) {
871 prev_rounds = num_rounds;
881 * Populates the DH parameters for the DHKE
883 * \param[in,out] skc Shared key credentials structure to
884 * populate with DH parameters
886 * \retval GSS_S_COMPLETE success
887 * \retval GSS_S_FAILURE failure
889 uint32_t sk_gen_params(struct sk_cred *skc, int num_rounds)
893 const BIGNUM *pub_key;
895 /* Random value used by both the request and response as part of the
896 * key binding material. This also should ensure we have unqiue
897 * tokens that are sent to the remote server which is important because
898 * the token is hashed for the sunrpc cache lookups and a failure there
899 * would cause connection attempts to fail indefinitely due to the large
900 * timeout value on the server side */
901 if (RAND_bytes((unsigned char *)&random, sizeof(random)) != 1) {
902 printerr(0, "Failed to get data for random parameter: %s\n",
903 ERR_error_string(ERR_get_error(), NULL));
904 return GSS_S_FAILURE;
907 /* The random value will always be used in byte range operations
908 * so we keep it as big endian from this point on */
909 skc->sc_kctx.skc_host_random = random;
911 /* Populate DH parameters */
912 skc->sc_params = DH_new();
913 if (!skc->sc_params) {
914 printerr(0, "Failed to allocate DH\n");
915 return GSS_S_FAILURE;
918 p = BN_bin2bn(skc->sc_p.value, skc->sc_p.length, NULL);
920 printerr(0, "Failed to convert binary to BIGNUM\n");
921 return GSS_S_FAILURE;
924 /* We use a static generator for shared key */
927 printerr(0, "Failed to allocate new BIGNUM\n");
928 return GSS_S_FAILURE;
930 if (BN_set_word(g, SK_GENERATOR) != 1) {
931 printerr(0, "Failed to set g value for DH params\n");
932 return GSS_S_FAILURE;
935 if (!DH_set0_pqg(skc->sc_params, p, NULL, g)) {
936 printerr(0, "Failed to set pqg\n");
937 return GSS_S_FAILURE;
940 /* Verify that we have a safe prime and valid generator */
941 if (!sk_is_dh_valid(skc->sc_params, num_rounds))
942 return GSS_S_FAILURE;
944 if (DH_generate_key(skc->sc_params) != 1) {
945 printerr(0, "Failed to generate public DH key: %s\n",
946 ERR_error_string(ERR_get_error(), NULL));
947 return GSS_S_FAILURE;
950 DH_get0_key(skc->sc_params, &pub_key, NULL);
951 skc->sc_pub_key.length = BN_num_bytes(pub_key);
952 skc->sc_pub_key.value = malloc(skc->sc_pub_key.length);
953 if (!skc->sc_pub_key.value) {
954 printerr(0, "Failed to allocate memory for public key\n");
955 return GSS_S_FAILURE;
958 BN_bn2bin(pub_key, skc->sc_pub_key.value);
960 return GSS_S_COMPLETE;
964 * Convert SK hash algorithm into openssl message digest
966 * \param[in,out] alg SK hash algorithm
970 static inline const EVP_MD *sk_hash_to_evp_md(enum cfs_crypto_hash_alg alg)
973 case CFS_HASH_ALG_SHA256:
975 case CFS_HASH_ALG_SHA512:
978 return EVP_md_null();
983 * Signs (via HMAC) the parameters used only in the key initialization protocol.
985 * \param[in] key Key to use for HMAC
986 * \param[in] bufs Array of gss_buffer_desc to generate
988 * \param[in] numbufs Number of buffers in array
989 * \param[in] hash_alg OpenSSL EVP_MD to use for hash
990 * \param[in,out] hmac HMAC of buffers is allocated and placed
991 * in this gss_buffer_desc. Caller must
997 int sk_sign_bufs(gss_buffer_desc *key, gss_buffer_desc *bufs, const int numbufs,
998 const EVP_MD *hash_alg, gss_buffer_desc *hmac)
1001 unsigned int hashlen = EVP_MD_size(hash_alg);
1005 if (hash_alg == EVP_md_null()) {
1006 printerr(0, "Invalid hash algorithm\n");
1010 hctx = HMAC_CTX_new();
1012 hmac->length = hashlen;
1013 hmac->value = malloc(hashlen);
1015 printerr(0, "Failed to allocate memory for HMAC\n");
1019 if (HMAC_Init_ex(hctx, key->value, key->length, hash_alg, NULL) != 1) {
1020 printerr(0, "Failed to init HMAC\n");
1024 for (i = 0; i < numbufs; i++) {
1025 if (HMAC_Update(hctx, bufs[i].value, bufs[i].length) != 1) {
1026 printerr(0, "Failed to update HMAC\n");
1031 /* The result gets populated in hmac */
1032 if (HMAC_Final(hctx, hmac->value, &hashlen) != 1) {
1033 printerr(0, "Failed to finalize HMAC\n");
1037 if (hmac->length != hashlen) {
1038 printerr(0, "HMAC size does not match expected\n");
1044 HMAC_CTX_free(hctx);
1049 * Generates an HMAC for gss_buffer_desc array in \a bufs of \a numbufs
1050 * and verifies against \a hmac.
1052 * \param[in] skc Shared key credentials
1053 * \param[in] bufs Array of gss_buffer_desc to generate HMAC for
1054 * \param[in] numbufs Number of buffers in array
1055 * \param[in] hash_alg OpenSSL EVP_MD to use for hash
1056 * \param[in] hmac HMAC to verify against
1058 * \retval GSS_S_COMPLETE success (match)
1059 * \retval gss error failure
1061 uint32_t sk_verify_hmac(struct sk_cred *skc, gss_buffer_desc *bufs,
1062 const int numbufs, const EVP_MD *hash_alg,
1063 gss_buffer_desc *hmac)
1065 gss_buffer_desc bufs_hmac;
1068 if (sk_sign_bufs(&skc->sc_kctx.skc_shared_key, bufs, numbufs, hash_alg,
1070 printerr(0, "Failed to sign buffers to verify HMAC\n");
1071 if (bufs_hmac.value)
1072 free(bufs_hmac.value);
1073 return GSS_S_FAILURE;
1076 if (hmac->length != bufs_hmac.length) {
1077 printerr(0, "Invalid HMAC size\n");
1078 free(bufs_hmac.value);
1079 return GSS_S_BAD_SIG;
1082 rc = memcmp(hmac->value, bufs_hmac.value, bufs_hmac.length);
1083 free(bufs_hmac.value);
1086 return GSS_S_BAD_SIG;
1088 return GSS_S_COMPLETE;
1092 * Cleanup an sk_cred freeing any resources
1094 * \param[in,out] skc Shared key credentials to free
1096 void sk_free_cred(struct sk_cred *skc)
1101 if (skc->sc_p.value)
1102 free(skc->sc_p.value);
1103 if (skc->sc_pub_key.value)
1104 free(skc->sc_pub_key.value);
1105 if (skc->sc_tgt.value)
1106 free(skc->sc_tgt.value);
1107 if (skc->sc_nodemap_hash.value)
1108 free(skc->sc_nodemap_hash.value);
1109 if (skc->sc_hmac.value)
1110 free(skc->sc_hmac.value);
1112 /* Overwrite keys and IV before freeing */
1113 if (skc->sc_dh_shared_key.value) {
1114 memset(skc->sc_dh_shared_key.value, 0,
1115 skc->sc_dh_shared_key.length);
1116 free(skc->sc_dh_shared_key.value);
1118 if (skc->sc_kctx.skc_hmac_key.value) {
1119 memset(skc->sc_kctx.skc_hmac_key.value, 0,
1120 skc->sc_kctx.skc_hmac_key.length);
1121 free(skc->sc_kctx.skc_hmac_key.value);
1123 if (skc->sc_kctx.skc_encrypt_key.value) {
1124 memset(skc->sc_kctx.skc_encrypt_key.value, 0,
1125 skc->sc_kctx.skc_encrypt_key.length);
1126 free(skc->sc_kctx.skc_encrypt_key.value);
1128 if (skc->sc_kctx.skc_shared_key.value) {
1129 memset(skc->sc_kctx.skc_shared_key.value, 0,
1130 skc->sc_kctx.skc_shared_key.length);
1131 free(skc->sc_kctx.skc_shared_key.value);
1133 if (skc->sc_kctx.skc_session_key.value) {
1134 memset(skc->sc_kctx.skc_session_key.value, 0,
1135 skc->sc_kctx.skc_session_key.length);
1136 free(skc->sc_kctx.skc_session_key.value);
1140 DH_free(skc->sc_params);
1146 /* This function handles key derivation using the hash algorithm specified in
1147 * \a hash_alg, buffers in \a key_binding_bufs, and original key in
1148 * \a origin_key to produce a \a derived_key. The first element of the
1149 * key_binding_bufs array is reserved for the counter used in the KDF. The
1150 * derived key in \a derived_key could differ in size from \a origin_key and
1151 * must be populated with the expected size and a valid buffer to hold the
1154 * If the derived key size is greater than the HMAC algorithm size it will be
1155 * a done using several iterations of a counter and the key binding bufs.
1157 * If the size is smaller it will take copy the first N bytes necessary to
1158 * fill the derived key. */
1159 int sk_kdf(gss_buffer_desc *derived_key , gss_buffer_desc *origin_key,
1160 gss_buffer_desc *key_binding_bufs, int numbufs,
1161 enum cfs_crypto_hash_alg hmac_alg)
1167 gss_buffer_desc tmp_hash;
1174 /* Use a counter as the first buffer followed by the key binding
1175 * buffers in the event we need more than one a single cycle to
1176 * produced a symmetric key large enough in size */
1177 key_binding_bufs[0].value = &counter;
1178 key_binding_bufs[0].length = sizeof(counter);
1180 remain = derived_key->length;
1181 keydata = derived_key->value;
1183 while (remain > 0) {
1184 counter = htobe32(i++);
1185 rc = sk_sign_bufs(origin_key, key_binding_bufs, numbufs,
1186 sk_hash_to_evp_md(hmac_alg), &tmp_hash);
1189 free(tmp_hash.value);
1193 if (cfs_crypto_hash_digestsize(hmac_alg) != tmp_hash.length) {
1194 free(tmp_hash.value);
1198 bytes = (remain < tmp_hash.length) ? remain : tmp_hash.length;
1199 memcpy(keydata, tmp_hash.value, bytes);
1200 free(tmp_hash.value);
1208 /* Populates the sk_cred's session_key using the a Key Derviation Function (KDF)
1209 * based on the recommendations in NIST Special Publication SP 800-56B Rev 1
1210 * (Sep 2014) Section 5.5.1
1212 * \param[in,out] skc Shared key credentials structure with
1214 * \return -1 failure
1217 int sk_session_kdf(struct sk_cred *skc, lnet_nid_t client_nid,
1218 gss_buffer_desc *client_token, gss_buffer_desc *server_token)
1220 struct sk_kernel_ctx *kctx = &skc->sc_kctx;
1221 gss_buffer_desc *session_key = &kctx->skc_session_key;
1222 gss_buffer_desc bufs[5];
1223 enum cfs_crypto_crypt_alg crypt_alg;
1226 crypt_alg = cfs_crypto_crypt_alg(kctx->skc_crypt_alg);
1227 session_key->length = cfs_crypto_crypt_keysize(crypt_alg);
1228 session_key->value = malloc(session_key->length);
1229 if (!session_key->value) {
1230 printerr(0, "Failed to allocate memory for session key\n");
1234 /* Key binding info ordering
1235 * 1. Reserved for counter
1239 * 4. Server's token */
1240 bufs[0].value = NULL;
1242 bufs[1] = skc->sc_dh_shared_key;
1243 bufs[2].value = &client_nid;
1244 bufs[2].length = sizeof(client_nid);
1245 bufs[3] = *client_token;
1246 bufs[4] = *server_token;
1248 return sk_kdf(&kctx->skc_session_key, &kctx->skc_shared_key, bufs,
1249 5, cfs_crypto_hash_alg(kctx->skc_hmac_alg));
1252 /* Uses the session key to create an HMAC key and encryption key. In
1253 * integrity mode the session key used to generate the HMAC key uses
1254 * session information which is available on the wire but by creating
1255 * a session based HMAC key we can prevent potential replay as both the
1256 * client and server have random numbers used as part of the key creation.
1258 * The keys used for integrity and privacy are formulated as below using
1259 * the session key that is the output of the key derivation function. The
1260 * HMAC algorithm is determined by the shared key algorithm selected in the
1264 * Session HMAC Key = PBKDF2("Integrity", KDF derived Session Key)
1267 * Session HMAC Key = PBKDF2("Integrity", KDF derived Session Key)
1268 * Session Encryption Key = PBKDF2("Encrypt", KDF derived Session Key)
1270 * \param[in,out] skc Shared key credentials structure with
1272 * \return -1 failure
1275 int sk_compute_keys(struct sk_cred *skc)
1277 struct sk_kernel_ctx *kctx = &skc->sc_kctx;
1278 gss_buffer_desc *session_key = &kctx->skc_session_key;
1279 gss_buffer_desc *hmac_key = &kctx->skc_hmac_key;
1280 gss_buffer_desc *encrypt_key = &kctx->skc_encrypt_key;
1281 enum cfs_crypto_hash_alg hmac_alg;
1282 enum cfs_crypto_crypt_alg crypt_alg;
1283 char *encrypt = "Encrypt";
1284 char *integrity = "Integrity";
1287 hmac_alg = cfs_crypto_hash_alg(kctx->skc_hmac_alg);
1288 hmac_key->length = cfs_crypto_hash_digestsize(hmac_alg);
1289 hmac_key->value = malloc(hmac_key->length);
1290 if (!hmac_key->value)
1293 rc = PKCS5_PBKDF2_HMAC(integrity, -1, session_key->value,
1294 session_key->length, SK_PBKDF2_ITERATIONS,
1295 sk_hash_to_evp_md(hmac_alg),
1296 hmac_key->length, hmac_key->value);
1300 /* Encryption key is only populated in privacy mode */
1301 if ((skc->sc_flags & LGSS_SVC_PRIV) == 0)
1304 crypt_alg = cfs_crypto_crypt_alg(kctx->skc_crypt_alg);
1305 encrypt_key->length = cfs_crypto_crypt_keysize(crypt_alg);
1306 encrypt_key->value = malloc(encrypt_key->length);
1307 if (!encrypt_key->value)
1310 rc = PKCS5_PBKDF2_HMAC(encrypt, -1, session_key->value,
1311 session_key->length, SK_PBKDF2_ITERATIONS,
1312 sk_hash_to_evp_md(hmac_alg),
1313 encrypt_key->length, encrypt_key->value);
1321 * Computes a session key based on the DH parameters from the host and its peer
1323 * \param[in,out] skc Shared key credentials structure with
1324 * the session key populated with the
1326 * \param[in] pub_key Public key returned from peer in
1328 * \return gss error failure
1329 * \return GSS_S_COMPLETE success
1331 uint32_t sk_compute_dh_key(struct sk_cred *skc, const gss_buffer_desc *pub_key)
1333 gss_buffer_desc *dh_shared = &skc->sc_dh_shared_key;
1334 BIGNUM *remote_pub_key;
1336 uint32_t rc = GSS_S_FAILURE;
1338 remote_pub_key = BN_bin2bn(pub_key->value, pub_key->length, NULL);
1339 if (!remote_pub_key) {
1340 printerr(0, "Failed to convert binary to BIGNUM\n");
1344 dh_shared->length = DH_size(skc->sc_params);
1345 dh_shared->value = malloc(dh_shared->length);
1346 if (!dh_shared->value) {
1347 printerr(0, "Failed to allocate memory for computed shared "
1352 /* This compute the shared key from the DHKE */
1353 status = DH_compute_key(dh_shared->value, remote_pub_key,
1356 printerr(0, "DH_compute_key() failed: %s\n",
1357 ERR_error_string(ERR_get_error(), NULL));
1359 } else if (status < dh_shared->length) {
1360 /* there is around 1 chance out of 256 that the returned
1361 * shared key is shorter than expected
1363 if (status >= dh_shared->length - 2) {
1364 int shift = dh_shared->length - status;
1365 /* if the key is short by only 1 or 2 bytes, just
1366 * prepend it with 0s
1368 memmove((void *)(dh_shared->value + shift),
1369 dh_shared->value, status);
1370 memset(dh_shared->value, 0, shift);
1372 /* if the key is really too short, return GSS_S_BAD_QOP
1373 * so that the caller can retry to generate
1375 printerr(0, "DH_compute_key() returned a short key of %d bytes, expected: %zu\n",
1376 status, dh_shared->length);
1382 rc = GSS_S_COMPLETE;
1385 BN_free(remote_pub_key);
1390 * Creates a serialized buffer for the kernel in the order of struct
1393 * \param[in,out] skc Shared key credentials structure
1394 * \param[in,out] ctx_token Serialized buffer for kernel.
1395 * Caller must free this buffer.
1398 * \return -1 failure
1400 int sk_serialize_kctx(struct sk_cred *skc, gss_buffer_desc *ctx_token)
1402 struct sk_kernel_ctx *kctx = &skc->sc_kctx;
1406 bufsize = sizeof(*kctx) + kctx->skc_hmac_key.length +
1407 kctx->skc_encrypt_key.length;
1409 ctx_token->value = malloc(bufsize);
1410 if (!ctx_token->value)
1412 ctx_token->length = bufsize;
1414 p = ctx_token->value;
1415 end = p + ctx_token->length;
1417 if (WRITE_BYTES(&p, end, kctx->skc_version))
1419 if (WRITE_BYTES(&p, end, kctx->skc_hmac_alg))
1421 if (WRITE_BYTES(&p, end, kctx->skc_crypt_alg))
1423 if (WRITE_BYTES(&p, end, kctx->skc_expire))
1425 if (WRITE_BYTES(&p, end, kctx->skc_host_random))
1427 if (WRITE_BYTES(&p, end, kctx->skc_peer_random))
1429 if (write_buffer(&p, end, &kctx->skc_hmac_key))
1431 if (write_buffer(&p, end, &kctx->skc_encrypt_key))
1434 printerr(2, "Serialized buffer of %zu bytes for kernel\n", bufsize);
1440 * Decodes a netstring \a ns into array of gss_buffer_descs at \a bufs
1441 * up to \a numbufs. Memory is allocated for each value and length
1442 * will be populated with the length
1444 * \param[in,out] bufs Array of gss_buffer_descs
1445 * \param[in,out] numbufs number of gss_buffer_desc in array
1446 * \param[in] ns netstring to decode
1448 * \return buffers populated success
1449 * \return -1 failure
1451 int sk_decode_netstring(gss_buffer_desc *bufs, int numbufs, gss_buffer_desc *ns)
1453 char *ptr = ns->value;
1454 size_t remain = ns->length;
1461 for (i = 0; i < numbufs; i++) {
1462 /* read the size of first buffer */
1463 rc = sscanf(ptr, "%9u", &size);
1466 digits = (size) ? ceil(log10(size + 1)) : 1;
1468 /* sep of current string */
1469 sep = size + digits + 2;
1471 /* check to make sure it's valid */
1472 if (remain < sep || ptr[digits] != ':' ||
1473 ptr[sep - 1] != ',')
1476 bufs[i].length = size;
1478 bufs[i].value = NULL;
1480 bufs[i].value = malloc(size);
1483 memcpy(bufs[i].value, &ptr[digits + 1], size);
1490 printerr(2, "Decoded netstring of %zu bytes\n", ns->length);
1496 free(bufs[i].value);
1503 * Creates a netstring in a gss_buffer_desc that consists of all
1504 * the gss_buffer_desc found in \a bufs. The netstring should be treated
1505 * as binary as it can contain null characters.
1507 * \param[in] bufs Array of gss_buffer_desc to use as input
1508 * \param[in] numbufs Number of buffers in array
1509 * \param[in,out] ns Destination gss_buffer_desc to hold
1512 * \return -1 failure
1515 int sk_encode_netstring(gss_buffer_desc *bufs, int numbufs,
1516 gss_buffer_desc *ns)
1523 /* size of string in decimal, string size, colon, and comma */
1524 for (i = 0; i < numbufs; i++) {
1526 if (bufs[i].length == 0)
1529 size += ceil(log10(bufs[i].length + 1)) +
1534 ns->value = malloc(ns->length);
1541 for (i = 0; i < numbufs; i++) {
1543 rc = scnprintf((char *) ptr, size, "%zu:", bufs[i].length);
1547 memcpy(ptr, bufs[i].value, bufs[i].length);
1548 ptr += bufs[i].length;
1553 size -= bufs[i].length + rc + 1;
1555 /* should not happen */
1560 printerr(2, "Encoded netstring of %zu bytes\n", ns->length);