Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright (C) 2018 Free Software Foundation, Inc.  | 
3  |  |  *  | 
4  |  |  * Author: Ander Juaristi  | 
5  |  |  *  | 
6  |  |  * This file is part of GnuTLS.  | 
7  |  |  *  | 
8  |  |  * The GnuTLS is free software; you can redistribute it and/or  | 
9  |  |  * modify it under the terms of the GNU Lesser General Public License  | 
10  |  |  * as published by the Free Software Foundation; either version 2.1 of  | 
11  |  |  * the License, or (at your option) any later version.  | 
12  |  |  *  | 
13  |  |  * This library is distributed in the hope that it will be useful, but  | 
14  |  |  * WITHOUT ANY WARRANTY; without even the implied warranty of  | 
15  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
16  |  |  * Lesser General Public License for more details.  | 
17  |  |  *  | 
18  |  |  * You should have received a copy of the GNU Lesser General Public License  | 
19  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>  | 
20  |  |  *  | 
21  |  |  */  | 
22  |  | #include "gnutls_int.h"  | 
23  |  | #include "stek.h"  | 
24  |  |  | 
25  | 0  | #define NAME_POS (0)  | 
26  | 0  | #define KEY_POS (TICKET_KEY_NAME_SIZE)  | 
27  | 0  | #define MAC_SECRET_POS (TICKET_KEY_NAME_SIZE + TICKET_CIPHER_KEY_SIZE)  | 
28  |  |  | 
29  |  | static int totp_sha3(gnutls_session_t session, uint64_t t,  | 
30  |  |          const gnutls_datum_t *secret,  | 
31  |  |          uint8_t out[TICKET_MASTER_KEY_SIZE])  | 
32  | 0  | { | 
33  | 0  |   int retval;  | 
34  | 0  |   uint8_t t_be[8];  | 
35  | 0  |   digest_hd_st hd;  | 
36  |  |   /*  | 
37  |  |    * We choose SHA3-512 because it outputs 64 bytes,  | 
38  |  |    * just the same length as the ticket key.  | 
39  |  |    */  | 
40  | 0  |   const gnutls_digest_algorithm_t algo = GNUTLS_DIG_SHA3_512;  | 
41  |  | #if TICKET_MASTER_KEY_SIZE != 64  | 
42  |  | #error "TICKET_MASTER_KEY_SIZE must be 64 bytes"  | 
43  |  | #endif  | 
44  |  | 
  | 
45  | 0  |   if (unlikely(secret == NULL))  | 
46  | 0  |     return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
47  |  |  | 
48  | 0  |   if ((retval = _gnutls_hash_init(&hd, hash_to_entry(algo))) < 0)  | 
49  | 0  |     return gnutls_assert_val(retval);  | 
50  |  |  | 
51  | 0  |   _gnutls_write_uint64(t, t_be);  | 
52  |  | 
  | 
53  | 0  |   if ((retval = _gnutls_hash(&hd, t_be, sizeof(t_be))) < 0)  | 
54  | 0  |     return gnutls_assert_val(retval);  | 
55  | 0  |   if ((retval = _gnutls_hash(&hd, secret->data, secret->size)) < 0)  | 
56  | 0  |     return gnutls_assert_val(retval);  | 
57  |  |  | 
58  | 0  |   _gnutls_hash_deinit(&hd, out);  | 
59  | 0  |   return GNUTLS_E_SUCCESS;  | 
60  | 0  | }  | 
61  |  |  | 
62  |  | static uint64_t T(gnutls_session_t session, time_t t)  | 
63  | 0  | { | 
64  | 0  |   uint64_t numeral = t;  | 
65  | 0  |   unsigned int x =  | 
66  | 0  |     session->internals.expire_time * STEK_ROTATION_PERIOD_PRODUCT;  | 
67  |  | 
  | 
68  | 0  |   if (numeral <= 0)  | 
69  | 0  |     return 0;  | 
70  |  |  | 
71  | 0  |   return (numeral / x);  | 
72  | 0  | }  | 
73  |  |  | 
74  |  | static int64_t totp_next(gnutls_session_t session)  | 
75  | 0  | { | 
76  | 0  |   time_t t;  | 
77  | 0  |   uint64_t result;  | 
78  |  | 
  | 
79  | 0  |   t = gnutls_time(NULL);  | 
80  | 0  |   if (unlikely(t == (time_t)-1))  | 
81  | 0  |     return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
82  |  |  | 
83  | 0  |   result = T(session, t);  | 
84  | 0  |   if (result == 0)  | 
85  | 0  |     return 0;  | 
86  |  |  | 
87  | 0  |   if (result == session->key.totp.last_result)  | 
88  | 0  |     return 0;  | 
89  |  |  | 
90  | 0  |   return result;  | 
91  | 0  | }  | 
92  |  |  | 
93  |  | static int64_t totp_previous(gnutls_session_t session)  | 
94  | 0  | { | 
95  | 0  |   uint64_t result;  | 
96  |  | 
  | 
97  | 0  |   if (session->key.totp.last_result == 0)  | 
98  | 0  |     return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
99  | 0  |   if (!session->key.totp.was_rotated)  | 
100  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
101  |  |  | 
102  | 0  |   result = session->key.totp.last_result - 1;  | 
103  | 0  |   if (result == 0)  | 
104  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
105  |  |  | 
106  | 0  |   return result;  | 
107  | 0  | }  | 
108  |  |  | 
109  |  | static void call_rotation_callback(gnutls_session_t session,  | 
110  |  |            uint8_t key[TICKET_MASTER_KEY_SIZE],  | 
111  |  |            uint64_t t)  | 
112  | 0  | { | 
113  | 0  |   gnutls_datum_t prev_key, new_key;  | 
114  |  | 
  | 
115  | 0  |   if (session->key.totp.cb) { | 
116  | 0  |     new_key.data = key;  | 
117  | 0  |     new_key.size = TICKET_MASTER_KEY_SIZE;  | 
118  | 0  |     prev_key.data = session->key.session_ticket_key;  | 
119  | 0  |     prev_key.size = TICKET_MASTER_KEY_SIZE;  | 
120  |  | 
  | 
121  | 0  |     session->key.totp.cb(&prev_key, &new_key, t);  | 
122  | 0  |   }  | 
123  | 0  | }  | 
124  |  |  | 
125  |  | static int rotate(gnutls_session_t session)  | 
126  | 0  | { | 
127  | 0  |   int64_t t;  | 
128  | 0  |   gnutls_datum_t secret;  | 
129  | 0  |   uint8_t key[TICKET_MASTER_KEY_SIZE];  | 
130  |  |  | 
131  |  |   /* Do we need to calculate new totp? */  | 
132  | 0  |   t = totp_next(session);  | 
133  | 0  |   if (t > 0) { | 
134  | 0  |     secret.data = session->key.initial_stek;  | 
135  | 0  |     secret.size = TICKET_MASTER_KEY_SIZE;  | 
136  |  |  | 
137  |  |     /* Generate next key */  | 
138  | 0  |     if (totp_sha3(session, t, &secret, key) < 0) { | 
139  | 0  |       gnutls_assert();  | 
140  | 0  |       return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
141  | 0  |     }  | 
142  |  |  | 
143  |  |     /* Replace old key with new one, and call callback if provided */  | 
144  | 0  |     call_rotation_callback(session, key, t);  | 
145  | 0  |     session->key.totp.last_result = t;  | 
146  | 0  |     _gnutls_memory_mark_defined(session->key.session_ticket_key,  | 
147  | 0  |               sizeof(key));  | 
148  | 0  |     memcpy(session->key.session_ticket_key, key, sizeof(key));  | 
149  |  | 
  | 
150  | 0  |     session->key.totp.was_rotated = 1;  | 
151  | 0  |   } else if (t < 0) { | 
152  | 0  |     return gnutls_assert_val(t);  | 
153  | 0  |   }  | 
154  |  |  | 
155  | 0  |   return GNUTLS_E_SUCCESS;  | 
156  | 0  | }  | 
157  |  |  | 
158  |  | static int rotate_back_and_peek(gnutls_session_t session,  | 
159  |  |         uint8_t key[TICKET_MASTER_KEY_SIZE])  | 
160  | 0  | { | 
161  | 0  |   int64_t t;  | 
162  | 0  |   gnutls_datum_t secret;  | 
163  |  |  | 
164  |  |   /* Get the previous TOTP */  | 
165  | 0  |   t = totp_previous(session);  | 
166  | 0  |   if (t < 0)  | 
167  | 0  |     return gnutls_assert_val(t);  | 
168  |  |  | 
169  | 0  |   secret.data = session->key.initial_stek;  | 
170  | 0  |   secret.size = TICKET_MASTER_KEY_SIZE;  | 
171  |  | 
  | 
172  | 0  |   if (totp_sha3(session, t, &secret, key) < 0)  | 
173  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
174  |  |  | 
175  | 0  |   return 0;  | 
176  | 0  | }  | 
177  |  |  | 
178  |  | /*  | 
179  |  |  * _gnutls_get_session_ticket_encryption_key:  | 
180  |  |  * @key_name: an empty datum that will receive the key name part of the STEK  | 
181  |  |  * @mac_key: an empty datum that will receive the MAC key part of the STEK  | 
182  |  |  * @enc_key: an empty datum that will receive the encryption key part of the STEK  | 
183  |  |  *  | 
184  |  |  * Get the currently active session ticket encryption key (STEK).  | 
185  |  |  *  | 
186  |  |  * The STEK is a 64-byte blob which is further divided in three parts,  | 
187  |  |  * and this function requires the caller to supply three separate datums for each one.  | 
188  |  |  * Though the caller might omit one or more of those if not interested in that part of the STEK.  | 
189  |  |  *  | 
190  |  |  * These are the three parts the STEK is divided in:  | 
191  |  |  *  | 
192  |  |  *  - Key name: 16 bytes  | 
193  |  |  *  - Encryption key: 32 bytes  | 
194  |  |  *  - MAC key: 16 bytes  | 
195  |  |  *  | 
196  |  |  * This function will transparently rotate the key, if the time has come for that,  | 
197  |  |  * before returning it to the caller.  | 
198  |  |  */  | 
199  |  | int _gnutls_get_session_ticket_encryption_key(gnutls_session_t session,  | 
200  |  |                 gnutls_datum_t *key_name,  | 
201  |  |                 gnutls_datum_t *mac_key,  | 
202  |  |                 gnutls_datum_t *enc_key)  | 
203  | 0  | { | 
204  | 0  |   int retval;  | 
205  |  | 
  | 
206  | 0  |   if (unlikely(session == NULL)) { | 
207  | 0  |     gnutls_assert();  | 
208  | 0  |     return GNUTLS_E_INTERNAL_ERROR;  | 
209  | 0  |   }  | 
210  |  |  | 
211  | 0  |   if (!session->key.stek_initialized) { | 
212  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
213  | 0  |   }  | 
214  |  |  | 
215  | 0  |   if ((retval = rotate(session)) < 0)  | 
216  | 0  |     return gnutls_assert_val(retval);  | 
217  |  |  | 
218  |  |   /* Copy key parts to user-supplied datums (if provided) */  | 
219  | 0  |   if (key_name) { | 
220  | 0  |     key_name->data = &session->key.session_ticket_key[NAME_POS];  | 
221  | 0  |     key_name->size = TICKET_KEY_NAME_SIZE;  | 
222  | 0  |   }  | 
223  | 0  |   if (mac_key) { | 
224  | 0  |     mac_key->data =  | 
225  | 0  |       &session->key.session_ticket_key[MAC_SECRET_POS];  | 
226  | 0  |     mac_key->size = TICKET_MAC_SECRET_SIZE;  | 
227  | 0  |   }  | 
228  | 0  |   if (enc_key) { | 
229  | 0  |     enc_key->data = &session->key.session_ticket_key[KEY_POS];  | 
230  | 0  |     enc_key->size = TICKET_CIPHER_KEY_SIZE;  | 
231  | 0  |   }  | 
232  |  | 
  | 
233  | 0  |   return retval;  | 
234  | 0  | }  | 
235  |  |  | 
236  |  | /*  | 
237  |  |  * _gnutls_get_session_ticket_decryption_key:  | 
238  |  |  * @ticket_data: the bytes of a session ticket that must be decrypted  | 
239  |  |  * @key_name: an empty datum that will receive the key name part of the STEK  | 
240  |  |  * @mac_key: an empty datum that will receive the MAC key part of the STEK  | 
241  |  |  * @enc_key: an empty datum that will receive the encryption key part of the STEK  | 
242  |  |  *  | 
243  |  |  * Get the key (STEK) the given session ticket was encrypted with.  | 
244  |  |  *  | 
245  |  |  * As with its encryption counterpart (%_gnutls_get_session_ticket_encryption_key),  | 
246  |  |  * this function will also transparently rotate  | 
247  |  |  * the currently active STEK if time has come for that, and it also requires the different  | 
248  |  |  * parts of the STEK to be obtained in different datums.  | 
249  |  |  *  | 
250  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, or a negative error code, such as  | 
251  |  |  * %GNUTLS_E_REQUSTED_DATA_NOT_AVAILABLE if no key could be found for the supplied ticket.  | 
252  |  |  */  | 
253  |  | int _gnutls_get_session_ticket_decryption_key(gnutls_session_t session,  | 
254  |  |                 const gnutls_datum_t *ticket_data,  | 
255  |  |                 gnutls_datum_t *key_name,  | 
256  |  |                 gnutls_datum_t *mac_key,  | 
257  |  |                 gnutls_datum_t *enc_key)  | 
258  | 0  | { | 
259  | 0  |   int retval;  | 
260  | 0  |   uint8_t *key_data;  | 
261  |  | 
  | 
262  | 0  |   if (unlikely(session == NULL || ticket_data == NULL ||  | 
263  | 0  |          ticket_data->data == NULL))  | 
264  | 0  |     return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
265  |  |  | 
266  | 0  |   if (ticket_data->size < TICKET_KEY_NAME_SIZE)  | 
267  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
268  |  |  | 
269  | 0  |   if (!session->key.stek_initialized) { | 
270  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
271  | 0  |   }  | 
272  |  |  | 
273  | 0  |   if ((retval = rotate(session)) < 0)  | 
274  | 0  |     return gnutls_assert_val(retval);  | 
275  |  |  | 
276  |  |   /*  | 
277  |  |    * Is current key valid?  | 
278  |  |    * We compare the first 16 bytes --> The key_name field.  | 
279  |  |    */  | 
280  | 0  |   if (memcmp(ticket_data->data,  | 
281  | 0  |        &session->key.session_ticket_key[NAME_POS],  | 
282  | 0  |        TICKET_KEY_NAME_SIZE) == 0) { | 
283  | 0  |     key_data = session->key.session_ticket_key;  | 
284  | 0  |     goto key_found;  | 
285  | 0  |   }  | 
286  |  |  | 
287  |  |   /*  | 
288  |  |    * Current key is not valid.  | 
289  |  |    * Compute previous key and see if that matches.  | 
290  |  |    */  | 
291  | 0  |   _gnutls_memory_mark_defined(session->key.previous_ticket_key,  | 
292  | 0  |             TICKET_MASTER_KEY_SIZE);  | 
293  | 0  |   if ((retval = rotate_back_and_peek(  | 
294  | 0  |          session, session->key.previous_ticket_key)) < 0) { | 
295  | 0  |     _gnutls_memory_mark_undefined(session->key.previous_ticket_key,  | 
296  | 0  |                 TICKET_MASTER_KEY_SIZE);  | 
297  | 0  |     return gnutls_assert_val(retval);  | 
298  | 0  |   }  | 
299  |  |  | 
300  | 0  |   if (memcmp(ticket_data->data,  | 
301  | 0  |        &session->key.previous_ticket_key[NAME_POS],  | 
302  | 0  |        TICKET_KEY_NAME_SIZE) == 0) { | 
303  | 0  |     key_data = session->key.previous_ticket_key;  | 
304  | 0  |     goto key_found;  | 
305  | 0  |   }  | 
306  |  |  | 
307  | 0  |   return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
308  |  |  | 
309  | 0  | key_found:  | 
310  | 0  |   if (key_name) { | 
311  | 0  |     key_name->data = &key_data[NAME_POS];  | 
312  | 0  |     key_name->size = TICKET_KEY_NAME_SIZE;  | 
313  | 0  |   }  | 
314  | 0  |   if (mac_key) { | 
315  | 0  |     mac_key->data = &key_data[MAC_SECRET_POS];  | 
316  | 0  |     mac_key->size = TICKET_MAC_SECRET_SIZE;  | 
317  | 0  |   }  | 
318  | 0  |   if (enc_key) { | 
319  | 0  |     enc_key->data = &key_data[KEY_POS];  | 
320  | 0  |     enc_key->size = TICKET_CIPHER_KEY_SIZE;  | 
321  | 0  |   }  | 
322  |  | 
  | 
323  | 0  |   return GNUTLS_E_SUCCESS;  | 
324  | 0  | }  | 
325  |  |  | 
326  |  | /*  | 
327  |  |  * _gnutls_initialize_session_ticket_key_rotation:  | 
328  |  |  * @key: Initial session ticket key  | 
329  |  |  *  | 
330  |  |  * Initialize the session ticket key rotation.  | 
331  |  |  *  | 
332  |  |  * This function will not enable session ticket keys on the server side. That is done  | 
333  |  |  * with the gnutls_session_ticket_enable_server() function. This function just initializes  | 
334  |  |  * the internal state to support periodical rotation of the session ticket encryption key.  | 
335  |  |  *  | 
336  |  |  * Returns: %GNUTLS_E_SUCCESS (0) on success, or %GNUTLS_E_INVALID_REQUEST on error.  | 
337  |  |  */  | 
338  |  | int _gnutls_initialize_session_ticket_key_rotation(gnutls_session_t session,  | 
339  |  |                const gnutls_datum_t *key)  | 
340  | 0  | { | 
341  | 0  |   if (unlikely(session == NULL || key == NULL))  | 
342  | 0  |     return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
343  |  |  | 
344  | 0  |   if (unlikely(session->key.totp.last_result != 0))  | 
345  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
346  |  |  | 
347  | 0  |   _gnutls_memory_mark_defined(session->key.initial_stek,  | 
348  | 0  |             TICKET_MASTER_KEY_SIZE);  | 
349  | 0  |   memcpy(session->key.initial_stek, key->data, key->size);  | 
350  |  | 
  | 
351  | 0  |   session->key.stek_initialized = true;  | 
352  | 0  |   session->key.totp.was_rotated = 0;  | 
353  | 0  |   return 0;  | 
354  | 0  | }  | 
355  |  |  | 
356  |  | /*  | 
357  |  |  * _gnutls_set_session_ticket_key_rotation_callback:  | 
358  |  |  * @cb: the callback function  | 
359  |  |  *  | 
360  |  |  * Set a callback function that will be invoked every time the session ticket key  | 
361  |  |  * is rotated.  | 
362  |  |  *  | 
363  |  |  * The function will take as arguments the previous key, the new key and the time  | 
364  |  |  * step value that caused the key to rotate.  | 
365  |  |  *  | 
366  |  |  */  | 
367  |  | void _gnutls_set_session_ticket_key_rotation_callback(  | 
368  |  |   gnutls_session_t session, gnutls_stek_rotation_callback_t cb)  | 
369  | 0  | { | 
370  | 0  |   if (session)  | 
371  | 0  |     session->key.totp.cb = cb;  | 
372  | 0  | }  |