OpenVPN
crypto_mbedtls_legacy.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) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2026 Sentyron B.V. <openvpn@sentyron.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include "syshead.h"
35
36#if defined(ENABLE_CRYPTO_MBEDTLS)
37#include <mbedtls/version.h>
38
39#if MBEDTLS_VERSION_NUMBER < 0x04000000
40
41#include "errlevel.h"
42#include "basic.h"
43#include "buffer.h"
44#include "crypto.h"
45#include "integer.h"
46#include "crypto_backend.h"
47#include "otime.h"
48#include "misc.h"
49
50#include <mbedtls/base64.h>
51#include <mbedtls/des.h>
52#include <mbedtls/error.h>
53#include <mbedtls/md5.h>
54#include <mbedtls/cipher.h>
55#include <mbedtls/pem.h>
56
57#include <mbedtls/entropy.h>
58#include <mbedtls/ssl.h>
59
60
61/*
62 *
63 * Hardware engine support. Allows loading/unloading of engines.
64 *
65 */
66
67void
68crypto_init_lib_engine(const char *engine_name)
69{
70 msg(M_WARN, "Note: mbed TLS hardware crypto engine functionality is not "
71 "available");
72}
73
75crypto_load_provider(const char *provider)
76{
77 if (provider)
78 {
79 msg(M_WARN, "Note: mbed TLS provider functionality is not available");
80 }
81 return NULL;
82}
83
84void
85crypto_unload_provider(const char *provname, provider_t *provider)
86{
87}
88
89/*
90 *
91 * Functions related to the core crypto library
92 *
93 */
94
95void
97{
98}
99
100void
102{
103}
104
105void
107{
108}
109
110bool
111mbed_log_err(unsigned int flags, int errval, const char *prefix)
112{
113 if (0 != errval)
114 {
115 char errstr[256];
116 mbedtls_strerror(errval, errstr, sizeof(errstr));
117
118 if (NULL == prefix)
119 {
120 prefix = "mbed TLS error";
121 }
122 msg(flags, "%s: %s", prefix, errstr);
123 }
124
125 return 0 == errval;
126}
127
128bool
129mbed_log_func_line(unsigned int flags, int errval, const char *func, int line)
130{
131 char prefix[256];
132
133 if (!checked_snprintf(prefix, sizeof(prefix), "%s:%d", func, line))
134 {
135 return mbed_log_err(flags, errval, func);
136 }
137
138 return mbed_log_err(flags, errval, prefix);
139}
140
141
142#ifdef DMALLOC
143void
144crypto_init_dmalloc(void)
145{
146 msg(M_ERR, "Error: dmalloc support is not available for mbed TLS.");
147}
148#endif /* DMALLOC */
149
151 { "BF-CBC", "BLOWFISH-CBC" },
152 { "BF-CFB", "BLOWFISH-CFB64" },
153 { "CAMELLIA-128-CFB", "CAMELLIA-128-CFB128" },
154 { "CAMELLIA-192-CFB", "CAMELLIA-192-CFB128" },
155 { "CAMELLIA-256-CFB", "CAMELLIA-256-CFB128" }
156};
159
160void
162{
163 const int *ciphers = mbedtls_cipher_list();
164
165#ifndef ENABLE_SMALL
166 printf("The following ciphers and cipher modes are available for use\n"
167 "with " PACKAGE_NAME ". Each cipher shown below may be used as a\n"
168 "parameter to the --data-ciphers (or --cipher) option. Using a\n"
169 "GCM or CBC mode is recommended. In static key mode only CBC\n"
170 "mode is allowed.\n\n");
171#endif
172
173 while (*ciphers != 0)
174 {
175 const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type(*ciphers);
176 const char *name = mbedtls_cipher_info_get_name(info);
177 if (info && name && !cipher_kt_insecure(name)
178 && (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(name)))
179 {
180 print_cipher(name);
181 }
182 ciphers++;
183 }
184
185 printf("\nThe following ciphers have a block size of less than 128 bits, \n"
186 "and are therefore deprecated. Do not use unless you have to.\n\n");
187 ciphers = mbedtls_cipher_list();
188 while (*ciphers != 0)
189 {
190 const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type(*ciphers);
191 const char *name = mbedtls_cipher_info_get_name(info);
192 if (info && name && cipher_kt_insecure(name)
193 && (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(name)))
194 {
195 print_cipher(name);
196 }
197 ciphers++;
198 }
199 printf("\n");
200}
201
202void
204{
205 const int *digests = mbedtls_md_list();
206
207#ifndef ENABLE_SMALL
208 printf("The following message digests are available for use with\n" PACKAGE_NAME
209 ". A message digest is used in conjunction with\n"
210 "the HMAC function, to authenticate received packets.\n"
211 "You can specify a message digest as parameter to\n"
212 "the --auth option.\n\n");
213#endif
214
215 while (*digests != 0)
216 {
217 const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*digests);
218
219 if (info)
220 {
221 printf("%s %d bit default key\n", mbedtls_md_get_name(info),
222 mbedtls_md_get_size(info) * 8);
223 }
224 digests++;
225 }
226 printf("\n");
227}
228
229void
231{
232 printf("Sorry, mbed TLS hardware crypto engine functionality is not "
233 "available\n");
234}
235
236#if defined(__GNUC__) || defined(__clang__)
237#pragma GCC diagnostic push
238#pragma GCC diagnostic ignored "-Wconversion"
239#endif
240
241bool
242crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src,
243 struct gc_arena *gc)
244{
245 /* 1000 chars is the PEM line length limit (+1 for tailing NUL) */
246 char header[1000 + 1] = { 0 };
247 char footer[1000 + 1] = { 0 };
248
249 if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name))
250 {
251 return false;
252 }
253 if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----\n", name))
254 {
255 return false;
256 }
257
258 size_t out_len = 0;
259 if (MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL
260 != mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src), NULL, 0, &out_len))
261 {
262 return false;
263 }
264
265 /* We set the size buf to out_len-1 to NOT include the 0 byte that
266 * mbedtls_pem_write_buffer in its length calculation */
267 *dst = alloc_buf_gc(out_len, gc);
268 if (!mbed_ok(mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src), BPTR(dst),
269 BCAP(dst), &out_len))
270 || !buf_inc_len(dst, out_len - 1))
271 {
272 CLEAR(*dst);
273 return false;
274 }
275
276 return true;
277}
278
279bool
280crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
281{
282 /* 1000 chars is the PEM line length limit (+1 for tailing NUL) */
283 char header[1000 + 1] = { 0 };
284 char footer[1000 + 1] = { 0 };
285
286 if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----", name))
287 {
288 return false;
289 }
290 if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----", name))
291 {
292 return false;
293 }
294
295 /* mbed TLS requires the src to be null-terminated */
296 /* allocate a new buffer to avoid modifying the src buffer */
297 struct gc_arena gc = gc_new();
298 struct buffer input = alloc_buf_gc(BLEN(src) + 1, &gc);
299 buf_copy(&input, src);
301
302 size_t use_len = 0;
303 mbedtls_pem_context ctx = { 0 };
304 bool ret =
306 size_t buf_size = 0;
307 const unsigned char *buf = mbedtls_pem_get_buffer(&ctx, &buf_size);
308 if (ret && !buf_write(dst, buf, buf_size))
309 {
310 ret = false;
311 msg(M_WARN, "PEM decode error: destination buffer too small");
312 }
313
314 mbedtls_pem_free(&ctx);
315 gc_free(&gc);
316 return ret;
317}
318
319/*
320 *
321 * Random number functions, used in cases where we want
322 * reasonably strong cryptographic random number generation
323 * without depleting our entropy pool. Used for random
324 * IV values and a number of other miscellaneous tasks.
325 *
326 */
327
328/*
329 * Initialise the given ctr_drbg context, using a personalisation string and an
330 * entropy gathering function.
331 */
333rand_ctx_get(void)
334{
335 static mbedtls_entropy_context ec = { 0 };
336 static mbedtls_ctr_drbg_context cd_ctx = { 0 };
337 static bool rand_initialised = false;
338
339 if (!rand_initialised)
340 {
341 struct gc_arena gc = gc_new();
342 struct buffer pers_string = alloc_buf_gc(100, &gc);
343
344 /*
345 * Personalisation string, should be as unique as possible (see NIST
346 * 800-90 section 8.7.1). We have very little information at this stage.
347 * Include Program Name, memory address of the context and PID.
348 */
349 buf_printf(&pers_string, "OpenVPN %0u %p %s", platform_getpid(), &cd_ctx,
350 time_string(0, 0, 0, &gc));
351
352 /* Initialise mbed TLS RNG, and built-in entropy sources */
354
357 BLEN(&pers_string))))
358 {
359 msg(M_FATAL, "Failed to initialize random generator");
360 }
361
362 gc_free(&gc);
363 rand_initialised = true;
364 }
365
366 return &cd_ctx;
367}
368
369int
371{
373
374 while (len > 0)
375 {
378 {
379 return 0;
380 }
381
382 output += blen;
383 len -= blen;
384 }
385
386 return 1;
387}
388
389/*
390 *
391 * Generic cipher key type functions
392 *
393 */
394static const mbedtls_cipher_info_t *
395cipher_get(const char *ciphername)
396{
397 ASSERT(ciphername);
398
399 const mbedtls_cipher_info_t *cipher = NULL;
400
401 ciphername = translate_cipher_name_from_openvpn(ciphername);
402 cipher = mbedtls_cipher_info_from_string(ciphername);
403 return cipher;
404}
405
406bool
407cipher_valid_reason(const char *ciphername, const char **reason)
408{
409 ASSERT(reason);
410
411 const mbedtls_cipher_info_t *cipher = cipher_get(ciphername);
412
413 if (NULL == cipher)
414 {
415 msg(D_LOW, "Cipher algorithm '%s' not found", ciphername);
416 *reason = "disabled because unknown";
417 return false;
418 }
419
420 const size_t key_bytelen = mbedtls_cipher_info_get_key_bitlen(cipher) / 8;
422 {
423 msg(D_LOW,
424 "Cipher algorithm '%s' uses a default key size (%zu bytes) "
425 "which is larger than " PACKAGE_NAME "'s current maximum key size "
426 "(%d bytes)",
428 *reason = "disabled due to key size too large";
429 return false;
430 }
431
432 *reason = NULL;
433 return true;
434}
435
436const char *
437cipher_kt_name(const char *ciphername)
438{
439 const mbedtls_cipher_info_t *cipher_kt = cipher_get(ciphername);
440 if (NULL == cipher_kt)
441 {
442 return "[null-cipher]";
443 }
444
446}
447
448unsigned int
449cipher_kt_key_size(const char *ciphername)
450{
451 const mbedtls_cipher_info_t *cipher_kt = cipher_get(ciphername);
452
453 if (NULL == cipher_kt)
454 {
455 return 0;
456 }
457
459}
460
461unsigned int
462cipher_kt_iv_size(const char *ciphername)
463{
464 const mbedtls_cipher_info_t *cipher_kt = cipher_get(ciphername);
465
466 if (NULL == cipher_kt)
467 {
468 return 0;
469 }
471}
472
473unsigned int
474cipher_kt_block_size(const char *ciphername)
475{
476 const mbedtls_cipher_info_t *cipher_kt = cipher_get(ciphername);
477 if (NULL == cipher_kt)
478 {
479 return 0;
480 }
482}
483
484unsigned int
485cipher_kt_tag_size(const char *ciphername)
486{
487 if (cipher_kt_mode_aead(ciphername))
488 {
490 }
491 return 0;
492}
493
494bool
495cipher_kt_insecure(const char *ciphername)
496{
497 const mbedtls_cipher_info_t *cipher_kt = cipher_get(ciphername);
498 if (!cipher_kt)
499 {
500 return true;
501 }
502
503 return !(cipher_kt_block_size(ciphername) >= 128 / 8
504#ifdef MBEDTLS_CHACHAPOLY_C
506#endif
507 );
508}
509
512{
515}
516
517bool
518cipher_kt_mode_cbc(const char *ciphername)
519{
520 const mbedtls_cipher_info_t *cipher = cipher_get(ciphername);
521 return cipher && cipher_kt_mode(cipher) == OPENVPN_MODE_CBC;
522}
523
524bool
525cipher_kt_mode_ofb_cfb(const char *ciphername)
526{
527 const mbedtls_cipher_info_t *cipher = cipher_get(ciphername);
528 return cipher
529 && (cipher_kt_mode(cipher) == OPENVPN_MODE_OFB
530 || cipher_kt_mode(cipher) == OPENVPN_MODE_CFB);
531}
532
533bool
534cipher_kt_mode_aead(const char *ciphername)
535{
536 const mbedtls_cipher_info_t *cipher = cipher_get(ciphername);
537 return cipher
538 && (cipher_kt_mode(cipher) == OPENVPN_MODE_GCM
539#ifdef MBEDTLS_CHACHAPOLY_C
541#endif
542 );
543}
544
545
546/*
547 *
548 * Generic cipher context functions
549 *
550 */
551
553cipher_ctx_new(void)
554{
557 return ctx;
558}
559
560void
562{
564 free(ctx);
565}
566
567void
568cipher_ctx_init(mbedtls_cipher_context_t *ctx, const uint8_t *key, const char *ciphername,
570{
571 ASSERT(NULL != ciphername && NULL != ctx);
572 CLEAR(*ctx);
573
574 const mbedtls_cipher_info_t *kt = cipher_get(ciphername);
575 ASSERT(kt);
577
578 if (!mbed_ok(mbedtls_cipher_setup(ctx, kt)))
579 {
580 msg(M_FATAL, "mbed TLS cipher context init #1");
581 }
582
584 {
585 msg(M_FATAL, "mbed TLS cipher set key");
586 }
587
589 {
591 {
592 msg(M_FATAL, "mbed TLS cipher set padding mode");
593 }
594 }
595
596 /* make sure we used a big enough key */
598}
599
600unsigned int
602{
603 return mbedtls_cipher_get_iv_size(ctx);
604}
605
606int
608{
609 if (tag_len < 0)
610 {
611 return 0;
612 }
613
614 if (!mbed_ok(mbedtls_cipher_write_tag(ctx, (unsigned char *)tag, tag_len)))
615 {
616 return 0;
617 }
618
619 return 1;
620}
621
622unsigned int
624{
626}
627
628int
630{
631 ASSERT(NULL != ctx);
632
634}
635
636bool
638{
639 return ctx && cipher_ctx_mode(ctx) == OPENVPN_MODE_CBC;
640}
641
642
643bool
645{
646 return ctx
649}
650
651bool
653{
654 return ctx
656#ifdef MBEDTLS_CHACHAPOLY_C
658#endif
659 );
660}
661
662int
664{
665 if (!mbed_ok(mbedtls_cipher_reset(ctx)))
666 {
667 return 0;
668 }
669
671 {
672 return 0;
673 }
674
675 return 1;
676}
677
678int
680{
681 if (src_len < 0)
682 {
683 return 0;
684 }
685
687 {
688 return 0;
689 }
690
691 return 1;
692}
693
694int
696 int src_len)
697{
698 size_t s_dst_len = *dst_len;
699
700 if (!mbed_ok(mbedtls_cipher_update(ctx, src, (size_t)src_len, dst, &s_dst_len)))
701 {
702 return 0;
703 }
704
706
707 return 1;
708}
709
710int
712{
713 size_t s_dst_len = *dst_len;
714
716 {
717 return 0;
718 }
719
721
722 return 1;
723}
724
725int
727 size_t tag_len)
728{
729 size_t olen = 0;
730
732 {
733 return 0;
734 }
735
736 if (tag_len > SIZE_MAX)
737 {
738 return 0;
739 }
740
742 {
743 msg(D_CRYPT_ERRORS, "%s: cipher_ctx_final() failed", __func__);
744 return 0;
745 }
746
747 if (olen > INT_MAX)
748 {
749 return 0;
750 }
751 *dst_len = olen;
752
753 if (!mbed_ok(mbedtls_cipher_check_tag(ctx, (const unsigned char *)tag, tag_len)))
754 {
755 return 0;
756 }
757
758 return 1;
759}
760
761#if defined(__GNUC__) || defined(__clang__)
762#pragma GCC diagnostic pop
763#endif
764
765/*
766 *
767 * Generic message digest information functions
768 *
769 */
770
771
772static const mbedtls_md_info_t *
773md_get(const char *digest)
774{
775 const mbedtls_md_info_t *md = NULL;
776 ASSERT(digest);
777
779 if (!md)
780 {
781 msg(M_FATAL, "Message hash algorithm '%s' not found", digest);
782 }
784 {
785 msg(M_FATAL,
786 "Message hash algorithm '%s' uses a default hash size (%d bytes) which is larger than " PACKAGE_NAME
787 "'s current maximum hash size (%d bytes)",
789 }
790 return md;
791}
792
793bool
794md_valid(const char *digest)
795{
797 return md != NULL;
798}
799
800const char *
801md_kt_name(const char *mdname)
802{
803 if (!strcmp("none", mdname))
804 {
805 return "[null-digest]";
806 }
807 const mbedtls_md_info_t *kt = md_get(mdname);
808 return mbedtls_md_get_name(kt);
809}
810
811unsigned char
812md_kt_size(const char *mdname)
813{
814 if (!strcmp("none", mdname))
815 {
816 return 0;
817 }
818 const mbedtls_md_info_t *kt = md_get(mdname);
819 return mbedtls_md_get_size(kt);
820}
821
822/*
823 *
824 * Generic message digest functions
825 *
826 */
827
828bool
829md_full(const char *mdname, const uint8_t *src, size_t src_len, uint8_t *dst)
830{
831 const mbedtls_md_info_t *kt = md_get(mdname);
832 return 0 == mbedtls_md(kt, src, src_len, dst);
833}
834
836md_ctx_new(void)
837{
840 return ctx;
841}
842
843void
845{
846 free(ctx);
847}
848
849void
850md_ctx_init(mbedtls_md_context_t *ctx, const char *mdname)
851{
852 const mbedtls_md_info_t *kt = md_get(mdname);
853 ASSERT(NULL != ctx && NULL != kt);
854
855 mbedtls_md_init(ctx);
856 ASSERT(0 == mbedtls_md_setup(ctx, kt, 0));
857 ASSERT(0 == mbedtls_md_starts(ctx));
858}
859
860void
862{
863 mbedtls_md_free(ctx);
864}
865
866int
868{
869 if (NULL == ctx)
870 {
871 return 0;
872 }
874}
875
876void
878{
879 ASSERT(0 == mbedtls_md_update(ctx, src, src_len));
880}
881
882void
884{
885 ASSERT(0 == mbedtls_md_finish(ctx, dst));
886 mbedtls_md_free(ctx);
887}
888
889
890/*
891 *
892 * Generic HMAC functions
893 *
894 */
895
896
897/*
898 * TODO: re-enable dmsg for crypto debug
899 */
900
902hmac_ctx_new(void)
903{
906 return ctx;
907}
908
909void
911{
912 free(ctx);
913}
914
915void
916hmac_ctx_init(mbedtls_md_context_t *ctx, const uint8_t *key, const char *mdname)
917{
918 const mbedtls_md_info_t *kt = md_get(mdname);
919 ASSERT(NULL != kt && NULL != ctx);
920
921 mbedtls_md_init(ctx);
923 ASSERT(0 == mbedtls_md_setup(ctx, kt, 1));
925
926 /* make sure we used a big enough key */
928}
929
930void
932{
933 mbedtls_md_free(ctx);
934}
935
936int
938{
939 if (NULL == ctx)
940 {
941 return 0;
942 }
944}
945
946void
948{
949 ASSERT(0 == mbedtls_md_hmac_reset(ctx));
950}
951
952void
954{
956}
957
958void
960{
962}
963
964int
965memcmp_constant_time(const void *a, const void *b, size_t size)
966{
967 /* mbed TLS has a no const time memcmp function that it exposes
968 * via its APIs like OpenSSL does with CRYPTO_memcmp
969 * Adapt the function that mbedtls itself uses in
970 * mbedtls_safer_memcmp as it considers that to be safe */
971 volatile const unsigned char *A = (volatile const unsigned char *)a;
972 volatile const unsigned char *B = (volatile const unsigned char *)b;
973 volatile unsigned char diff = 0;
974
975 for (size_t i = 0; i < size; i++)
976 {
977 unsigned char x = A[i], y = B[i];
978 diff |= x ^ y;
979 }
980
981 return diff;
982}
983
984#if defined(__GNUC__) || defined(__clang__)
985#pragma GCC diagnostic push
986#pragma GCC diagnostic ignored "-Wconversion"
987#endif
988
989/*
990 * Generate the hash required by for the \c tls1_PRF function.
991 *
992 * @param md_kt Message digest to use
993 * @param sec Secret to base the hash on
994 * @param sec_len Length of the secret
995 * @param seed Seed to hash
996 * @param seed_len Length of the seed
997 * @param out Output buffer
998 * @param olen Length of the output buffer
999 */
1000static void
1001tls1_P_hash(const mbedtls_md_info_t *md_kt, const uint8_t *sec, size_t sec_len, const uint8_t *seed,
1002 size_t seed_len, uint8_t *out, size_t olen)
1003{
1004 struct gc_arena gc = gc_new();
1005 uint8_t A1[MAX_HMAC_KEY_LENGTH];
1006
1007#ifdef ENABLE_DEBUG
1008 /* used by the D_SHOW_KEY_SOURCE, guarded with ENABLE_DEBUG to avoid unused
1009 * variables warnings if compiled with --enable-small */
1010 const size_t olen_orig = olen;
1011 const uint8_t *out_orig = out;
1012#endif
1013
1014 hmac_ctx_t *ctx = hmac_ctx_new();
1015 hmac_ctx_t *ctx_tmp = hmac_ctx_new();
1016
1017 dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash sec: %s", format_hex(sec, sec_len, 0, &gc));
1018 dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash seed: %s", format_hex(seed, seed_len, 0, &gc));
1019
1020 unsigned int chunk = mbedtls_md_get_size(md_kt);
1021 unsigned int A1_len = mbedtls_md_get_size(md_kt);
1022
1023 /* This is the only place where we init an HMAC with a key that is not
1024 * equal to its size, therefore we init the hmac ctx manually here */
1025 mbedtls_md_init(ctx);
1026 ASSERT(0 == mbedtls_md_setup(ctx, md_kt, 1));
1027 ASSERT(0 == mbedtls_md_hmac_starts(ctx, sec, sec_len));
1028
1029 mbedtls_md_init(ctx_tmp);
1030 ASSERT(0 == mbedtls_md_setup(ctx_tmp, md_kt, 1));
1031 ASSERT(0 == mbedtls_md_hmac_starts(ctx_tmp, sec, sec_len));
1032
1033 hmac_ctx_update(ctx, seed, seed_len);
1034 hmac_ctx_final(ctx, A1);
1035
1036 for (;;)
1037 {
1038 hmac_ctx_reset(ctx);
1039 hmac_ctx_reset(ctx_tmp);
1040 hmac_ctx_update(ctx, A1, A1_len);
1041 hmac_ctx_update(ctx_tmp, A1, A1_len);
1042 hmac_ctx_update(ctx, seed, seed_len);
1043
1044 if (olen > chunk)
1045 {
1046 hmac_ctx_final(ctx, out);
1047 out += chunk;
1048 olen -= chunk;
1049 hmac_ctx_final(ctx_tmp, A1); /* calc the next A1 value */
1050 }
1051 else /* last one */
1052 {
1053 hmac_ctx_final(ctx, A1);
1054 memcpy(out, A1, olen);
1055 break;
1056 }
1057 }
1058 hmac_ctx_cleanup(ctx);
1059 hmac_ctx_free(ctx);
1060 hmac_ctx_cleanup(ctx_tmp);
1061 hmac_ctx_free(ctx_tmp);
1062 secure_memzero(A1, sizeof(A1));
1063
1064 dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash out: %s", format_hex(out_orig, olen_orig, 0, &gc));
1065 gc_free(&gc);
1066}
1067
1068/*
1069 * Use the TLS PRF function for generating data channel keys.
1070 * This code is based on the OpenSSL library.
1071 *
1072 * TLS generates keys as such:
1073 *
1074 * master_secret[48] = PRF(pre_master_secret[48], "master secret",
1075 * ClientHello.random[32] + ServerHello.random[32])
1076 *
1077 * key_block[] = PRF(SecurityParameters.master_secret[48],
1078 * "key expansion",
1079 * SecurityParameters.server_random[32] +
1080 * SecurityParameters.client_random[32]);
1081 *
1082 * Notes:
1083 *
1084 * (1) key_block contains a full set of 4 keys.
1085 * (2) The pre-master secret is generated by the client.
1086 */
1087bool
1088ssl_tls1_PRF(const uint8_t *label, size_t label_len, const uint8_t *sec, size_t slen, uint8_t *out1,
1089 size_t olen)
1090{
1091 struct gc_arena gc = gc_new();
1092 const md_kt_t *md5 = md_get("MD5");
1093 const md_kt_t *sha1 = md_get("SHA1");
1094
1095 uint8_t *out2 = (uint8_t *)gc_malloc(olen, false, &gc);
1096
1097 size_t len = slen / 2;
1098 const uint8_t *S1 = sec;
1099 const uint8_t *S2 = &(sec[len]);
1100 len += (slen & 1); /* add for odd, make longer */
1101
1102 tls1_P_hash(md5, S1, len, label, label_len, out1, olen);
1103 tls1_P_hash(sha1, S2, len, label, label_len, out2, olen);
1104
1105 for (size_t i = 0; i < olen; i++)
1106 {
1107 out1[i] ^= out2[i];
1108 }
1109
1110 secure_memzero(out2, olen);
1111
1112 dmsg(D_SHOW_KEY_SOURCE, "tls1_PRF out[%zu]: %s", olen, format_hex(out1, olen, 0, &gc));
1113
1114 gc_free(&gc);
1115 return true;
1116}
1117
1118#if defined(__GNUC__) || defined(__clang__)
1119#pragma GCC diagnostic pop
1120#endif
1121
1122#endif /* MBEDTLS_VERSION_NUMBER < 0x040000 */
1123#endif /* ENABLE_CRYPTO_MBEDTLS */
bool buf_printf(struct buffer *buf, const char *format,...)
Definition buffer.c:241
void buf_null_terminate(struct buffer *buf)
Definition buffer.c:537
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition buffer.c:341
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
bool checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
Definition buffer.c:1143
#define ALLOC_OBJ(dptr, type)
Definition buffer.h:1083
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition buffer.h:705
#define BPTR(buf)
Definition buffer.h:123
static bool buf_inc_len(struct buffer *buf, int inc)
Definition buffer.h:589
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:415
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Definition buffer.h:661
#define BLEN(buf)
Definition buffer.h:126
static char * format_hex(const uint8_t *data, size_t size, size_t maxoutput, struct gc_arena *gc)
Definition buffer.h:504
#define BCAP(buf)
Definition buffer.h:130
static void gc_free(struct gc_arena *a)
Definition buffer.h:1049
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition buffer.h:1088
static struct gc_arena gc_new(void)
Definition buffer.h:1041
const char * translate_cipher_name_from_openvpn(const char *cipher_name)
Translate an OpenVPN cipher name to a crypto library cipher name.
Definition crypto.c:1797
const char * translate_cipher_name_to_openvpn(const char *cipher_name)
Translate a crypto library cipher name to an OpenVPN cipher name.
Definition crypto.c:1810
void print_cipher(const char *ciphername)
Print a cipher list entry.
Definition crypto.c:1748
Data Channel Cryptography Module.
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
Data Channel Cryptography SSL library-specific backend interface.
int cipher_ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len)
Updates the given cipher context, encrypting data in the source buffer, and placing any complete bloc...
bool ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len, uint8_t *output, size_t output_len)
Calculates the TLS 1.0-1.1 PRF function.
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)
bool cipher_kt_mode_cbc(const char *ciphername)
Check if the supplied cipher is a supported CBC mode cipher.
void show_available_engines(void)
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
md_ctx_t * md_ctx_new(void)
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
void md_ctx_update(md_ctx_t *ctx, const uint8_t *src, size_t src_len)
unsigned 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.
unsigned int cipher_ctx_iv_length(const cipher_ctx_t *ctx)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
void crypto_unload_provider(const char *provname, provider_t *provider)
Unloads the given (OpenSSL) provider.
void crypto_uninit_lib(void)
bool cipher_kt_mode_aead(const char *ciphername)
Check if the supplied cipher is a supported AEAD mode cipher.
int md_ctx_size(const md_ctx_t *ctx)
void show_available_ciphers(void)
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported CBC mode cipher.
void crypto_init_lib(void)
cipher_ctx_t * cipher_ctx_new(void)
Generic cipher functions.
bool cipher_kt_mode_ofb_cfb(const char *ciphername)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
const char * md_kt_name(const char *mdname)
Retrieve a string describing the digest digest (e.g.
int hmac_ctx_size(hmac_ctx_t *ctx)
bool cipher_kt_insecure(const char *ciphername)
Returns true if we consider this cipher to be insecure.
#define MAX_CIPHER_KEY_LENGTH
void crypto_clear_error(void)
bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
Decode a PEM buffer to binary data.
provider_t * crypto_load_provider(const char *provider)
Load the given (OpenSSL) providers.
void cipher_ctx_free(cipher_ctx_t *ctx)
Cleanup and free a cipher context.
int cipher_ctx_mode(const cipher_ctx_t *ctx)
Returns the mode that the cipher runs in.
bool cipher_ctx_mode_ofb_cfb(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported AEAD mode cipher.
void hmac_ctx_free(hmac_ctx_t *ctx)
unsigned int cipher_kt_tag_size(const char *ciphername)
Returns the MAC tag size of the cipher, in bytes.
int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len)
Updates the given cipher context, providing additional data (AD) for authenticated encryption with ad...
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
const size_t cipher_name_translation_table_count
const char * cipher_kt_name(const char *ciphername)
Retrieve a normalised string describing the cipher (e.g.
#define MAX_HMAC_KEY_LENGTH
unsigned int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
#define OPENVPN_AEAD_TAG_LENGTH
void cipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphername, crypto_operation_t enc)
Initialise a cipher context, based on the given key and key type.
int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len)
Gets the computed message authenticated code (MAC) tag for this cipher.
void crypto_init_lib_engine(const char *engine_name)
void hmac_ctx_cleanup(hmac_ctx_t *ctx)
int cipher_ctx_reset(cipher_ctx_t *ctx, const uint8_t *iv_buf)
Resets the given cipher context, setting the IV to the specified value.
const cipher_name_pair cipher_name_translation_table[]
Cipher name translation table.
bool md_full(const char *mdname, const uint8_t *src, size_t src_len, uint8_t *dst)
Calculates the message digest for the given buffer.
void md_ctx_cleanup(md_ctx_t *ctx)
unsigned int cipher_ctx_block_size(const cipher_ctx_t *ctx)
Returns the block size of the cipher, in bytes.
int cipher_ctx_final(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len)
Pads the final cipher block using PKCS padding, and output to the destination buffer.
unsigned int cipher_kt_block_size(const char *ciphername)
Returns the block size of the cipher, in bytes.
void md_ctx_final(md_ctx_t *ctx, uint8_t *dst)
unsigned char md_kt_size(const char *mdname)
Returns the size of the message digest, in bytes.
void show_available_digests(void)
bool cipher_valid_reason(const char *ciphername, const char **reason)
Returns if the cipher is valid, based on the given cipher name and provides a reason if invalid.
int cipher_ctx_final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag, size_t tag_len)
Like cipher_ctx_final, but check the computed authentication tag against the supplied (expected) tag.
void md_ctx_init(md_ctx_t *ctx, const char *mdname)
Initialises the given message digest context.
void md_ctx_free(md_ctx_t *ctx)
bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src, struct gc_arena *gc)
Encode binary data as PEM.
#define OPENVPN_MODE_OFB
Cipher is in OFB mode.
#define mbed_ok(errval)
Check errval and log on error.
int crypto_operation_t
bool mbed_log_err(unsigned int flags, int errval, const char *prefix)
Log the supplied mbed TLS error, prefixed by supplied prefix.
#define OPENVPN_MODE_CFB
Cipher is in CFB mode.
#define OPENVPN_MODE_CBC
Cipher is in CBC mode.
void provider_t
#define OPENVPN_MODE_GCM
Cipher is in GCM mode.
bool mbed_log_func_line(unsigned int flags, int errval, const char *func, int line)
Log the supplied mbed TLS error, prefixed by function name and line number.
mbedtls_ctr_drbg_context * rand_ctx_get(void)
Returns a singleton instance of the mbed TLS random number generator.
mbedtls_md_info_t md_kt_t
Generic message digest key type context.
int cipher_kt_mode(const EVP_CIPHER *cipher_kt)
static evp_cipher_type * cipher_get(const char *ciphername)
static evp_md_type * md_get(const char *digest)
#define D_CRYPT_ERRORS
Definition errlevel.h:57
#define D_SHOW_KEY_SOURCE
Definition errlevel.h:121
#define D_LOW
Definition errlevel.h:96
static int min_int(int x, int y)
Definition integer.h:105
#define CLEAR(x)
Definition basic.h:32
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:172
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
const char * time_string(time_t t, tv_usec_t usec, bool show_usec, struct gc_arena *gc)
Definition otime.c:104
unsigned int platform_getpid(void)
Definition platform.c:333
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
Struct used in cipher name translation table.
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
struct gc_arena gc
Definition test_ssl.c:131