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