Whamcloud - gitweb
LU-14929 gss: detect libkeyutils dependency
[fs/lustre-release.git] / lustre / utils / gss / sk_utils.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (C) 2015, Trustees of Indiana University
24  *
25  * Author: Jeremy Filizetti <jfilizet@iu.edu>
26  */
27
28 #ifndef SK_UTILS_H
29 #define SK_UTILS_H
30
31 #include <gssapi/gssapi.h>
32 #ifdef HAVE_LIBKEYUTILS
33 #include <keyutils.h>
34 #endif
35 #include <linux/lustre/lustre_idl.h>
36 #include <openssl/dh.h>
37 #include <openssl/evp.h>
38 #include <openssl/hmac.h>
39 #include <sys/types.h>
40
41 #include <libcfs/libcfs_crypto.h>
42 #include "lsupport.h"
43
44 #ifndef ARRAY_SIZE
45 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
46 #endif /* !ARRAY_SIZE */
47
48 /* LL_CRYPTO_MAX_NAME value must match value of
49  * CRYPTO_MAX_ALG_NAME in include/linux/crypto.h
50  */
51 #ifdef HAVE_CRYPTO_MAX_ALG_NAME_128
52 #define LL_CRYPTO_MAX_NAME 128
53 #else
54 #define LL_CRYPTO_MAX_NAME 64
55 #endif
56
57 #if OPENSSL_VERSION_NUMBER < 0x10100000L
58 static inline HMAC_CTX *HMAC_CTX_new(void)
59 {
60         HMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
61
62         if (ctx != NULL)
63                 HMAC_CTX_init(ctx);
64         return ctx;
65 }
66
67 static inline void HMAC_CTX_free(HMAC_CTX *ctx)
68 {
69         if (ctx != NULL) {
70                 HMAC_CTX_cleanup(ctx);
71                 OPENSSL_cleanse(ctx, sizeof(*ctx));
72                 OPENSSL_free(ctx);
73         }
74 }
75 static inline void DH_get0_pqg(const DH *dh,
76                                const BIGNUM **p, const BIGNUM **q,
77                                const BIGNUM **g)
78 {
79         if (p != NULL)
80                 *p = dh->p;
81         if (q != NULL)
82                 *q = dh->q;
83         if (g != NULL)
84                 *g = dh->g;
85 }
86
87 static inline int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
88 {
89         /* If the fields p and g in dh are NULL, the corresponding input
90          * parameters MUST be non-NULL.  q may remain NULL.
91          */
92         if ((dh->p == NULL && p == NULL)
93             || (dh->g == NULL && g == NULL))
94                 return 0;
95
96         if (p != NULL) {
97                 BN_free(dh->p);
98                 dh->p = p;
99         }
100         if (q != NULL) {
101                 BN_free(dh->q);
102                 dh->q = q;
103         }
104         if (g != NULL) {
105                 BN_free(dh->g);
106                 dh->g = g;
107         }
108
109         if (q != NULL)
110                 dh->length = BN_num_bits(q);
111
112         return 1;
113 }
114
115 static inline void DH_get0_key(const DH *dh, const BIGNUM **pub_key,
116                                const BIGNUM **priv_key)
117 {
118         if (pub_key != NULL)
119                 *pub_key = dh->pub_key;
120         if (priv_key != NULL)
121                 *priv_key = dh->priv_key;
122 }
123 #endif
124
125 /* Some limits and defaults */
126 #define SK_CONF_VERSION 1
127 #define SK_MSG_VERSION 1
128 #define SK_GENERATOR 2
129 #define SK_SESSION_MAX_KEYLEN_BYTES 1024
130 #define SK_MAX_KEYLEN_BYTES 128
131 #define SK_MAX_P_BYTES 2048
132 #define SK_NONCE_SIZE 4
133 #define MAX_MGSNIDS 16
134
135 enum sk_ctx_init_buffers {
136         /* Initiator netstring buffer ordering */
137         SK_INIT_VERSION = 0,
138         SK_INIT_RANDOM  = 1,
139         SK_INIT_P       = 2,
140         SK_INIT_PUB_KEY = 3,
141         SK_INIT_TARGET  = 4,
142         SK_INIT_NODEMAP = 5,
143         SK_INIT_FLAGS   = 6,
144         SK_INIT_HMAC    = 7,
145         SK_INIT_BUFFERS = 8,
146
147         /* Responder netstring buffer ordering */
148         SK_RESP_VERSION = 0,
149         SK_RESP_RANDOM  = 1,
150         SK_RESP_PUB_KEY = 2,
151         SK_RESP_HMAC    = 3,
152         SK_RESP_BUFFERS = 4,
153 };
154
155 /* String consisting of "lustre:fsname:nodemap_hash" */
156 #define SK_DESCRIPTION_SIZE (9 + MTI_NAME_MAXLEN + LUSTRE_NODEMAP_NAME_LENGTH)
157
158 enum sk_key_type {
159         SK_TYPE_INVALID = 0x0,
160         SK_TYPE_CLIENT  = 0x1,
161         SK_TYPE_SERVER  = 0x2,
162         SK_TYPE_MGS     = 0x4,
163 };
164
165 /* This is the packed structure format of key files that are distributed.
166  * The on disk format should be store in big-endian. */
167 struct sk_keyfile_config {
168         /* File format version */
169         uint32_t        skc_version;
170         /* HMAC algorithm used for message integrity */
171         uint16_t        skc_hmac_alg;
172         /* Crypt algorithm used for privacy mode */
173         uint16_t        skc_crypt_alg;
174         /* Number of seconds that a context is valid after it is created from
175          * this keyfile */
176         uint32_t        skc_expire;
177         /* Length of shared key in skc_shared_key */
178         uint32_t        skc_shared_keylen;
179         /* Length of the prime used in the DHKE */
180         uint32_t        skc_prime_bits;
181         /* Key type */
182         uint8_t         skc_type;
183         /* Array of MGS NIDs to load key's for.  This is for the client since
184          * the upcall only knows the target name which is MGC<IP>@<NET>
185          * Only needed when mounting with mgssec */
186         lnet_nid_t      skc_mgsnids[MAX_MGSNIDS];
187         /* File system name for this key.  It can be unused for MGS only keys */
188         char            skc_fsname[MTI_NAME_MAXLEN + 1];
189         /* Nodemap name for this key.  Used by the server side to verify the
190          * client is in the correct nodemap */
191         char            skc_nodemap[LUSTRE_NODEMAP_NAME_LENGTH + 1];
192         /* Shared key */
193         unsigned char   skc_shared_key[SK_MAX_KEYLEN_BYTES];
194         /* Prime (p) for DHKE */
195         unsigned char   skc_p[SK_MAX_P_BYTES];
196 } __attribute__((packed));
197
198 /* Format passed to the kernel from userspace */
199 struct sk_kernel_ctx {
200         uint32_t        skc_version;
201         char            skc_hmac_alg[LL_CRYPTO_MAX_NAME];
202         char            skc_crypt_alg[LL_CRYPTO_MAX_NAME];
203         uint32_t        skc_expire;
204         uint32_t        skc_host_random;
205         uint32_t        skc_peer_random;
206         gss_buffer_desc skc_hmac_key;
207         gss_buffer_desc skc_encrypt_key;
208         gss_buffer_desc skc_shared_key;
209         gss_buffer_desc skc_session_key;
210 };
211
212 /* Structure used in context initiation to hold all necessary data */
213 struct sk_cred {
214         uint32_t                 sc_flags;
215         gss_buffer_desc          sc_p;
216         gss_buffer_desc          sc_pub_key;
217         gss_buffer_desc          sc_tgt;
218         gss_buffer_desc          sc_nodemap_hash;
219         gss_buffer_desc          sc_hmac;
220         gss_buffer_desc          sc_dh_shared_key;
221         struct sk_kernel_ctx     sc_kctx;
222         DH                      *sc_params;
223 };
224
225 /* Names match up with openssl enc and dgst commands */
226 /* When adding new alg types, make sure first occurrence's name
227  * matches cht_name in hash_types array.
228  */
229 static const struct sk_crypt_type sk_crypt_algs[] = {
230         {
231                 .sct_name = "null",
232                 .sct_type = SK_CRYPT_EMPTY
233         },
234         {
235                 .sct_name = "NONE",
236                 .sct_type = SK_CRYPT_EMPTY
237         },
238         {
239                 .sct_name = "ctr(aes)",
240                 .sct_type = SK_CRYPT_AES256_CTR
241         },
242         {
243                 .sct_name = "AES-256-CTR",
244                 .sct_type = SK_CRYPT_AES256_CTR
245         }
246 };
247 static const struct sk_hmac_type sk_hmac_algs[] = {
248         {
249                 .sht_name = "null",
250                 .sht_type = SK_HMAC_EMPTY
251         },
252         {
253                 .sht_name = "NONE",
254                 .sht_type = SK_HMAC_EMPTY
255         },
256         {
257                 .sht_name = "sha256",
258                 .sht_type = SK_HMAC_SHA256
259         },
260         {
261                 .sht_name = "SHA256",
262                 .sht_type = SK_HMAC_SHA256
263         },
264         {
265                 .sht_name = "sha512",
266                 .sht_type = SK_HMAC_SHA512
267         },
268         {
269                 .sht_name = "SHA512",
270                 .sht_type = SK_HMAC_SHA512
271         }
272 };
273
274 static inline int sk_name2crypt(char *name)
275 {
276         int i;
277
278         for (i = 0; i < ARRAY_SIZE(sk_crypt_algs); i++) {
279                 if (strcasecmp(name, sk_crypt_algs[i].sct_name) == 0)
280                         return sk_crypt_algs[i].sct_type;
281         }
282
283         return SK_CRYPT_INVALID;
284 }
285
286 static inline int sk_name2hmac(char *name)
287 {
288         int i;
289
290         for (i = 0; i < ARRAY_SIZE(sk_hmac_algs); i++) {
291                 if (strcasecmp(name, sk_hmac_algs[i].sht_name) == 0)
292                         return sk_hmac_algs[i].sht_type;
293         }
294
295         return SK_HMAC_INVALID;
296 }
297
298 static inline const char *sk_crypt2name(enum sk_crypt_alg type)
299 {
300         int i;
301
302         for (i = 0; i < ARRAY_SIZE(sk_crypt_algs); i++) {
303                 if (type == sk_crypt_algs[i].sct_type)
304                         return sk_crypt_algs[i].sct_name;
305         }
306
307         return NULL;
308 }
309
310 static inline const char *sk_hmac2name(enum sk_hmac_alg type)
311 {
312         int i;
313
314         for (i = 0; i < ARRAY_SIZE(sk_hmac_algs); i++) {
315                 if (type == sk_hmac_algs[i].sht_type)
316                         return sk_hmac_algs[i].sht_name;
317         }
318
319         return NULL;
320 }
321
322 void sk_init_logging(char *program, int verbose, int fg);
323 struct sk_keyfile_config *sk_read_file(char *filename);
324 int sk_load_keyfile(char *path);
325 void sk_config_disk_to_cpu(struct sk_keyfile_config *config);
326 void sk_config_cpu_to_disk(struct sk_keyfile_config *config);
327 int sk_validate_config(const struct sk_keyfile_config *config);
328 uint32_t sk_verify_hash(const char *string, const EVP_MD *hash_alg,
329                         const gss_buffer_desc *current_hash);
330 struct sk_cred *sk_create_cred(const char *fsname, const char *cluster,
331                                const uint32_t flags);
332 int sk_speedtest_dh_valid(unsigned int usec_check_max);
333 uint32_t sk_gen_params(struct sk_cred *skc, int num_rounds);
334 int sk_sign_bufs(gss_buffer_desc *key, gss_buffer_desc *bufs, const int numbufs,
335                  const EVP_MD *hash_alg, gss_buffer_desc *hmac);
336 uint32_t sk_verify_hmac(struct sk_cred *skc, gss_buffer_desc *bufs,
337                         const int numbufs, const EVP_MD *hash_alg,
338                         gss_buffer_desc *hmac);
339 void sk_free_cred(struct sk_cred *skc);
340 int sk_session_kdf(struct sk_cred *skc, lnet_nid_t client_nid,
341                    gss_buffer_desc *client_token, gss_buffer_desc *server_token);
342 uint32_t sk_compute_dh_key(struct sk_cred *skc, const gss_buffer_desc *pub_key);
343 int sk_compute_keys(struct sk_cred *skc);
344 int sk_serialize_kctx(struct sk_cred *skc, gss_buffer_desc *ctx_token);
345 int sk_decode_netstring(gss_buffer_desc *bufs, int numbufs,
346                         gss_buffer_desc *ns);
347 int sk_encode_netstring(gss_buffer_desc *bufs, int numbufs,
348                         gss_buffer_desc *ns);
349
350 #endif /* SK_UTILS_H */