OpenVPN
crypto_epoch.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2024 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2024 Arne Schwabe <arne@rfc2549.org>
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#include <inttypes.h>
32#include <string.h>
33#include <stdlib.h>
34
35#include "crypto_backend.h"
36#include "packet_id.h"
37#include "crypto.h"
38#include "crypto_epoch.h"
39#include "buffer.h"
40#include "integer.h"
41
42void
43ovpn_hkdf_expand(const uint8_t *secret,
44 const uint8_t *info, int info_len,
45 uint8_t *out, int out_len)
46{
47 hmac_ctx_t *hmac_ctx = hmac_ctx_new();
48 hmac_ctx_init(hmac_ctx, secret, "SHA256");
49
50 const int digest_size = SHA256_DIGEST_LENGTH;
51
52 /* T(0) = empty string */
53 uint8_t t_prev[SHA256_DIGEST_LENGTH];
54 int t_prev_len = 0;
55
56 for (uint8_t block = 1; (block - 1) * digest_size < out_len; block++)
57 {
58 hmac_ctx_reset(hmac_ctx);
59
60 /* calculate T(block) */
61 hmac_ctx_update(hmac_ctx, t_prev, t_prev_len);
62 hmac_ctx_update(hmac_ctx, info, info_len);
63 hmac_ctx_update(hmac_ctx, &block, 1);
64 hmac_ctx_final(hmac_ctx, t_prev);
65 t_prev_len = digest_size;
66
67 /* Copy a full hmac output or remaining bytes */
68 int out_offset = (block - 1) * digest_size;
69 int copylen = min_int(digest_size, out_len - out_offset);
70
71 memcpy(out + out_offset, t_prev, copylen);
72 }
73 hmac_ctx_cleanup(hmac_ctx);
74 hmac_ctx_free(hmac_ctx);
75}
76
77bool
78ovpn_expand_label(const uint8_t *secret, size_t secret_len,
79 const uint8_t *label, size_t label_len,
80 const uint8_t *context, size_t context_len,
81 uint8_t *out, uint16_t out_len)
82{
83 if (secret_len != 32 || label_len > 250 || context_len > 255
84 || label_len < 1)
85 {
86 /* Our current implementation is not a general purpose one
87 * and assumes that the secret size matches the size of the
88 * hash (SHA256) key. Also label length and context length
89 * need need to be in range */
90 return false;
91 }
92
93 struct gc_arena gc = gc_new();
94 /* 2 byte for the outlen encoded as uint16, 5 bytes for "ovpn ",
95 * 1 byte for context len byte and 1 byte for label len byte */
96 const uint8_t *label_prefix = (const uint8_t *) ("ovpn ");
97 int prefix_len = 5;
98
99 int hkdf_label_len = 2 + prefix_len + 1 + label_len + 1 + context_len;
101
103 buf_write_u8(&hkdf_label, prefix_len + label_len);
104 buf_write(&hkdf_label, label_prefix, prefix_len);
106
108 if (context_len > 0)
109 {
111 }
112
114
116 buf_len(&hkdf_label), out, out_len);
117
118 gc_free(&gc);
119 return true;
120}
121
127static void
129{
130 struct epoch_key new_epoch_key = { 0 };
131 new_epoch_key.epoch = epoch_key->epoch + 1;
132 const uint8_t epoch_update_label[] = "datakey upd";
133 /* length of the array without extra \0 byte from the string */
134 const size_t epoch_update_label_len = sizeof(epoch_update_label) - 1;
135
136 /* E_N+1 = OVPN-Expand-Label(E_N, "datakey upd", "", 32) */
138 epoch_update_label, epoch_update_label_len,
139 NULL, 0,
140 new_epoch_key.epoch_key, sizeof(new_epoch_key.epoch_key));
141 *epoch_key = new_epoch_key;
142}
143
144void
146 const struct epoch_key *epoch_key,
147 const struct key_type *kt)
148{
149 key->hmac_size = cipher_kt_iv_size(kt->cipher);
150 key->cipher_size = cipher_kt_key_size(kt->cipher);
151
152 /* Generate data key from epoch key:
153 * K_i = OVPN-Expand-Label(E_i, "data_key", "", key_size)
154 * implicit_iv = OVPN-Expand-Label(E_i, "data_iv", "", implicit_iv_len)
155 */
156
157 const uint8_t epoch_data_key_label[] = "data_key";
158 /* length of the array without extra \0 byte from the string */
159 const size_t epoch_data_key_label_len = sizeof(epoch_data_key_label) - 1;
160
162 epoch_data_key_label, epoch_data_key_label_len,
163 NULL, 0,
164 (uint8_t *)(&key->cipher), key->cipher_size);
165
166 const uint8_t epoch_data_iv_label[] = "data_iv";
167 /* length of the array without extra \0 byte from the string */
168 const size_t epoch_data_iv_label_len = sizeof(epoch_data_iv_label) - 1;
169
171 epoch_data_iv_label, epoch_data_iv_label_len,
172 NULL, 0,
173 (uint8_t *)(&key->hmac), key->hmac_size);
174 key->epoch = epoch_key->epoch;
175}
176
177static void
179{
180 /* Ensure that we are NEVER regenerating the same key that has already
181 * been generated. Since we also reset the packet ID counter this would be
182 * catastrophic as we would do IV reuse which breaks ciphers like AES-GCM */
184 char name[32] = { 0 };
185 snprintf(name, sizeof(name), "Epoch Data key %" PRIu16, co->epoch_key_send.epoch);
186
187 struct key_parameters send_key = { 0 };
188
190
192 &co->epoch_key_type, name);
194 CLEAR(send_key);
195}
196
197
198static void
200{
201 struct key_parameters recv_key = { 0 };
203
204 char name[32];
205
206 snprintf(name, sizeof(name), "Epoch Data key %" PRIu16, co->epoch_key_recv.epoch);
207
208 init_key_bi_ctx_recv(ctx, &recv_key, &co->epoch_key_type, name);
209 CLEAR(recv_key);
210}
211
212void
214{
215 /* We want the future receive keys to start just after the epoch of
216 * the currently used decryption key. */
218 uint16_t current_decrypt_epoch = co->key_ctx_bi.decrypt.epoch;
219
220 /* Either we have not generated any future keys yet (first initialisation)
221 * or the last index is the same as our current epoch key
222 * (last generated receive epoch key should match the epoch key) */
223 struct key_ctx *highest_future_key = &co->epoch_data_keys_future[co->epoch_data_keys_future_count - 1];
224
226 || highest_future_key->epoch == co->epoch_key_recv.epoch);
227
228 /* free the keys that are not used anymore */
229 for (uint16_t i = 0; i < co->epoch_data_keys_future_count; i++)
230 {
231 /* Keys in future keys are always epoch > 1 if initialised */
232 if (co->epoch_data_keys_future[i].epoch > 0
233 && co->epoch_data_keys_future[i].epoch < current_decrypt_epoch)
234 {
235 /* Key is old, free it */
237 }
238 }
239
240 /* Calculate the number of keys that need to be generated,
241 * if no keys have been generated assume only the first key is defined */
242 uint16_t current_highest_key = highest_future_key->epoch ? highest_future_key->epoch : 1;
243 uint16_t desired_highest_key = current_decrypt_epoch + co->epoch_data_keys_future_count;
244 uint16_t num_keys_generate = desired_highest_key - current_highest_key;
245
246
247 /* Move the old keys out of the way so the order of keys stays strictly
248 * monotonic and consecutive. */
249 /* first check that the destination we are going to overwrite is freed */
250 for (uint16_t i = 0; i < num_keys_generate; i++)
251 {
253 }
254 memmove(co->epoch_data_keys_future,
255 co->epoch_data_keys_future + num_keys_generate,
256 (co->epoch_data_keys_future_count - num_keys_generate) * sizeof(struct key_ctx));
257
258 /* Clear and regenerate the array elements at the end */
259 for (uint16_t i = co->epoch_data_keys_future_count - num_keys_generate; i < co->epoch_data_keys_future_count; i++)
260 {
263
265 }
266
267 /* Assert that all keys are initialised */
268 for (uint16_t i = 0; i < co->epoch_data_keys_future_count; i++)
269 {
271 }
272}
273
274void
282
283void
285 uint16_t new_epoch)
286{
287 /* Find the key of the new epoch in future keys */
288 uint16_t fki;
289 for (fki = 0; fki < co->epoch_data_keys_future_count; fki++)
290 {
291 if (co->epoch_data_keys_future[fki].epoch == new_epoch)
292 {
293 break;
294 }
295 }
296 /* we should only ever be called when we successfully decrypted/authenticated
297 * a packet from a peer, ie the epoch recv key *MUST* be in that
298 * array */
299 ASSERT(fki < co->epoch_data_keys_future_count);
300 ASSERT(co->epoch_data_keys_future[fki].epoch == new_epoch);
301
302 struct key_ctx *new_ctx = &co->epoch_data_keys_future[fki];
303
304 /* Check if the new recv key epoch is higher than the send key epoch. If
305 * yes we will replace the send key as well */
306 if (co->key_ctx_bi.encrypt.epoch < new_epoch)
307 {
309
310 /* Update the epoch_key for send to match the current key being used.
311 * This is a bit of extra work but since we are a maximum of 16
312 * keys behind, a maximum 16 HMAC invocations are a small price to
313 * pay for not keeping all the old epoch keys around in future_keys
314 * array */
315 while (co->epoch_key_send.epoch < new_epoch)
316 {
318 }
320 }
321
322 /* Replace receive key */
326
327 co->key_ctx_bi.decrypt = *new_ctx;
328
329 /* Zero the old location instead to of free_key_ctx since we moved the keys
330 * and do not want to free the pointers in the old place */
331 memset(new_ctx, 0, sizeof(struct key_ctx));
332
333 /* Generate new future keys */
335}
336
337void
339{
340 for (uint16_t i = 0; i < co->epoch_data_keys_future_count; i++)
341 {
343 }
344
345 free(co->epoch_data_keys_future);
350}
351
352void
354 const struct epoch_key *e1_send,
355 const struct epoch_key *e1_recv,
356 uint16_t future_key_count)
357{
358 ASSERT(e1_send->epoch == 1 && e1_recv->epoch == 1);
359 co->epoch_key_recv = *e1_recv;
360 co->epoch_key_send = *e1_send;
361
364
367 co->key_ctx_bi.initialized = true;
368
369 co->epoch_data_keys_future_count = future_key_count;
372}
373
374struct key_ctx *
376{
377 /* Current decrypt key is the most likely one */
378 if (opt->key_ctx_bi.decrypt.epoch == epoch)
379 {
380 return &opt->key_ctx_bi.decrypt;
381 }
384 {
386 }
387 else if (epoch > opt->key_ctx_bi.decrypt.epoch
389 {
390 /* Key in the range of future keys */
391 int index = epoch - (opt->key_ctx_bi.decrypt.epoch + 1);
392
393 /* If we have reached the edge of the valid keys we do not return
394 * the key anymore since regenerating the new keys would move us
395 * over the window of valid keys and would need all kind of
396 * special casing, so we stop returning the key in this case */
397 if (epoch > (UINT16_MAX - opt->epoch_data_keys_future_count - 1))
398 {
399 return NULL;
400 }
401 else
402 {
403 return &opt->epoch_data_keys_future[index];
404 }
405 }
406 else
407 {
408 return NULL;
409 }
410}
411
412
413void
415{
416 if (opt->epoch_key_send.epoch == UINT16_MAX)
417 {
418 /* limit of epoch keys reached, cannot move to a newer key anymore */
419 return;
420 }
421 if (opt->aead_usage_limit)
422 {
424 opt->packet_id.send.id))
425 {
426 /* Send key limit reached */
428 }
429 /* draft 8 of the aead usage limit still had but draft 9 complete
430 * dropped this statement:
431 *
432 * In particular, in two-party communication, one participant cannot
433 * regard apparent overuse of a key by other participants as
434 * being in error, when it could be that the other participant has
435 * better information about bounds.
436 *
437 * OpenVPN 2.x implements a compromise of not regarding this as an
438 * error but still accepting packets of the usage limit but tries to
439 * push the peer to a new epoch key by increasing our own key epoch
440 *
441 * Also try to push the sender to use a new key if we are the
442 * decryption fail warn limit.
443 * */
444 else if (opt->key_ctx_bi.encrypt.epoch == opt->key_ctx_bi.decrypt.epoch
446 &opt->key_ctx_bi.decrypt,
447 opt->packet_id.rec.id)
449 ))
450 {
451 /* Receive key limit reached. Increase our own send key to signal
452 * that we want to use a new epoch. Peer should then also move its
453 * key but is not required to do this */
455 }
456 }
457
459 {
461 }
462
463}
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:88
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Definition buffer.h:698
static uint8_t * buf_bptr(const struct buffer *buf)
Definition buffer.h:240
#define ALLOC_ARRAY_CLEAR(dptr, type, n)
Definition buffer.h:1076
static int buf_len(const struct buffer *buf)
Definition buffer.h:253
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition buffer.h:668
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Definition buffer.h:692
static void gc_free(struct gc_arena *a)
Definition buffer.h:1033
static struct gc_arena gc_new(void)
Definition buffer.h:1025
void init_key_bi_ctx_send(struct key_ctx *ctx, const struct key_parameters *key_params, const struct key_type *kt, const char *name)
Definition crypto.c:1063
uint64_t cipher_get_aead_limits(const char *ciphername)
Check if the cipher is an AEAD cipher and needs to be limited to a certain number of number of blocks...
Definition crypto.c:353
void free_key_ctx(struct key_ctx *ctx)
Definition crypto.c:1106
void init_key_bi_ctx_recv(struct key_ctx *ctx, const struct key_parameters *key_params, const struct key_type *kt, const char *name)
Definition crypto.c:1075
Data Channel Cryptography Module.
static bool cipher_decrypt_verify_fail_warn(const struct key_ctx *ctx)
Check if the number of failed decryption is approaching the limit and we should try to move to a new ...
Definition crypto.h:736
static bool aead_usage_limit_reached(const uint64_t limit, const struct key_ctx *key_ctx, int64_t higest_pid)
Checks if the usage limit for an AEAD cipher is reached.
Definition crypto.h:765
Data Channel Cryptography SSL library-specific backend interface.
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
hmac_ctx_t * hmac_ctx_new(void)
void hmac_ctx_reset(hmac_ctx_t *ctx)
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
void hmac_ctx_free(hmac_ctx_t *ctx)
int cipher_kt_iv_size(const char *ciphername)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
void hmac_ctx_cleanup(hmac_ctx_t *ctx)
bool ovpn_expand_label(const uint8_t *secret, size_t secret_len, const uint8_t *label, size_t label_len, const uint8_t *context, size_t context_len, uint8_t *out, uint16_t out_len)
Variant of the RFC 8446 TLS 1.3 HKDF-Expand-Label function with the following differences/restriction...
void epoch_generate_future_receive_keys(struct crypto_options *co)
Generates and fills the epoch_data_keys_future with next valid future keys in crypto_options using th...
void epoch_replace_update_recv_key(struct crypto_options *co, uint16_t new_epoch)
This is called when the peer uses a new send key that is not the default key.
static void epoch_key_iterate(struct epoch_key *epoch_key)
Iterates the epoch key to make it E_n+1, ie increase the epoch by one and derive the new key material...
static void epoch_init_send_key_ctx(struct crypto_options *co)
void free_epoch_key_ctx(struct crypto_options *co)
Frees the extra data structures used by epoch keys in crypto_options.
void epoch_check_send_iterate(struct crypto_options *opt)
Checks if we need to iterate the send epoch key.
static void epoch_init_recv_key(struct key_ctx *ctx, struct crypto_options *co)
struct key_ctx * epoch_lookup_decrypt_key(struct crypto_options *opt, uint16_t epoch)
Using an epoch, this function will try to retrieve a decryption key context that matches that epoch f...
void epoch_init_key_ctx(struct crypto_options *co, const struct key_type *key_type, const struct epoch_key *e1_send, const struct epoch_key *e1_recv, uint16_t future_key_count)
Initialises data channel keys and internal structures for epoch data keys using the provided E0 epoch...
void epoch_data_key_derive(struct key_parameters *key, const struct epoch_key *epoch_key, const struct key_type *kt)
Generate a data channel key pair from the epoch key.
void ovpn_hkdf_expand(const uint8_t *secret, const uint8_t *info, int info_len, uint8_t *out, int out_len)
Implementation of the RFC5869 HKDF-Expand function with the following restrictions.
void epoch_iterate_send_key(struct crypto_options *co)
Updates the send key and send_epoch_key in cryptio_options->key_ctx_bi to use the next epoch.
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
#define SHA256_DIGEST_LENGTH
static int min_int(int x, int y)
Definition integer.h:102
#define CLEAR(x)
Definition basic.h:33
#define ASSERT(x)
Definition error.h:195
void packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src)
Move the packet id recv structure from src to dest.
Definition packet_id.c:109
static void reset_packet_id_send(struct packet_id_send *p)
Reset the current send packet id to its initial state.
Definition packet_id.h:307
#define PACKET_ID_EPOCH_MAX
Definition packet_id.h:48
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
Contains all state information for one tunnel.
Definition openvpn.h:474
Security parameter state for processing data channel packets.
Definition crypto.h:292
struct epoch_key epoch_key_send
last epoch_key used for generation of the current send data keys.
Definition crypto.h:301
struct key_type epoch_key_type
the key_type that is used to generate the epoch keys
Definition crypto.h:307
struct key_ctx epoch_retiring_data_receive_key
The old key before the sender switched to a new epoch data key.
Definition crypto.h:327
uint64_t aead_usage_limit
The limit for AEAD cipher, this is the sum of packets + blocks that are allowed to be used.
Definition crypto.h:312
struct key_ctx * epoch_data_keys_future
Keeps the future epoch data keys for decryption.
Definition crypto.h:321
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
Definition crypto.h:293
uint16_t epoch_data_keys_future_count
number of keys stored in epoch_data_keys_future
Definition crypto.h:324
struct packet_id_rec epoch_retiring_key_pid_recv
Definition crypto.h:328
struct epoch_key epoch_key_recv
epoch_key used for the highest receive epoch keys
Definition crypto.h:304
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
Definition crypto.h:330
uint8_t epoch_key[SHA256_DIGEST_LENGTH]
Definition crypto.h:192
uint16_t epoch
Definition crypto.h:193
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition crypto.h:279
bool initialized
Definition crypto.h:284
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
Definition crypto.h:282
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
Definition crypto.h:280
Container for one set of cipher and/or HMAC contexts.
Definition crypto.h:201
uint16_t epoch
OpenVPN data channel epoch, this variable holds the epoch number this key belongs to.
Definition crypto.h:227
internal structure similar to struct key that holds key information but is not represented on wire an...
Definition crypto.h:162
const char * cipher
const name of the cipher
Definition crypto.h:142
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
uint8_t cipher[MAX_CIPHER_KEY_LENGTH]
Key material for cipher operations.
Definition crypto.h:153
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
Definition crypto.h:155
uint64_t id
Definition packet_id.h:118
struct seq_list * seq_list
Definition packet_id.h:123
uint64_t id
Definition packet_id.h:154
struct packet_id_send send
Definition packet_id.h:201
struct packet_id_rec rec
Definition packet_id.h:202
struct gc_arena gc
Definition test_ssl.c:155