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
8
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
9
* Copyright (C) 2010 Fox Crypto B.V. <openvpn@fox-it.com>
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.
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.
20
* You should have received a copy of the GNU General Public License
21
* along with this program (see the file COPYING included with this
22
* distribution); if not, write to the Free Software Foundation, Inc.,
23
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
* @file Data Channel Cryptography OpenSSL-specific backend interface
32
#elif defined(_MSC_VER)
33
#include "config-msvc.h"
38
#if defined(ENABLE_CRYPTO) && defined(ENABLE_CRYPTO_OPENSSL)
43
#include "crypto_backend.h"
44
#include <openssl/objects.h>
45
#include <openssl/evp.h>
46
#include <openssl/des.h>
49
* Check for key size creepage.
52
#if MAX_CIPHER_KEY_LENGTH < EVP_MAX_KEY_LENGTH
53
#warning Some OpenSSL EVP ciphers now support key lengths greater than MAX_CIPHER_KEY_LENGTH -- consider increasing MAX_CIPHER_KEY_LENGTH
56
#if MAX_HMAC_KEY_LENGTH < EVP_MAX_MD_SIZE
57
#warning Some OpenSSL HMAC message digests now support key lengths greater than MAX_HMAC_KEY_LENGTH -- consider increasing MAX_HMAC_KEY_LENGTH
62
* Workarounds for incompatibilites between OpenSSL libraries.
63
* Right now we accept OpenSSL libraries from 0.9.5 to 0.9.7.
67
#if SSLEAY_VERSION_NUMBER < 0x00907000L
69
/* Workaround: EVP_CIPHER_mode is defined wrong in OpenSSL 0.9.6 but is fixed in 0.9.7 */
70
#undef EVP_CIPHER_mode
71
#define EVP_CIPHER_mode(e) (((e)->flags) & EVP_CIPH_MODE)
73
#define DES_cblock des_cblock
74
#define DES_is_weak_key des_is_weak_key
75
#define DES_check_key_parity des_check_key_parity
76
#define DES_set_odd_parity des_set_odd_parity
78
#define HMAC_CTX_init(ctx) CLEAR (*ctx)
79
#define HMAC_Init_ex(ctx,sec,len,md,impl) HMAC_Init(ctx, sec, len, md)
80
#define HMAC_CTX_cleanup(ctx) HMAC_cleanup(ctx)
81
#define EVP_MD_CTX_cleanup(md) CLEAR (*md)
83
#define INFO_CALLBACK_SSL_CONST
88
EVP_CipherInit_ov (EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, uint8_t *key, uint8_t *iv, int enc)
90
return EVP_CipherInit (ctx, type, key, iv, enc);
94
EVP_CipherUpdate_ov (EVP_CIPHER_CTX *ctx, uint8_t *out, int *outl, uint8_t *in, int inl)
96
return EVP_CipherUpdate (ctx, out, outl, in, inl);
100
cipher_ok (const char* name)
105
#ifndef EVP_CIPHER_name
106
#define EVP_CIPHER_name(e) OBJ_nid2sn(EVP_CIPHER_nid(e))
110
#define EVP_MD_name(e) OBJ_nid2sn(EVP_MD_type(e))
113
#if HAVE_OPENSSL_ENGINE
114
#include <openssl/engine.h>
116
static bool engine_initialized = false; /* GLOBAL */
118
static ENGINE *engine_persist = NULL; /* GLOBAL */
120
/* Try to load an engine in a shareable library */
122
try_load_engine (const char *engine)
124
ENGINE *e = ENGINE_by_id ("dynamic");
127
if (!ENGINE_ctrl_cmd_string (e, "SO_PATH", engine, 0)
128
|| !ENGINE_ctrl_cmd_string (e, "LOAD", NULL, 0))
138
setup_engine (const char *engine)
142
ENGINE_load_builtin_engines ();
146
if (strcmp (engine, "auto") == 0)
148
msg (M_INFO, "Initializing OpenSSL auto engine support");
149
ENGINE_register_all_complete ();
152
if ((e = ENGINE_by_id (engine)) == NULL
153
&& (e = try_load_engine (engine)) == NULL)
155
msg (M_FATAL, "OpenSSL error: cannot load engine '%s'", engine);
158
if (!ENGINE_set_default (e, ENGINE_METHOD_ALL))
160
msg (M_FATAL, "OpenSSL error: ENGINE_set_default failed on engine '%s'",
164
msg (M_INFO, "Initializing OpenSSL support for engine '%s'",
170
#endif /* HAVE_OPENSSL_ENGINE */
173
crypto_init_lib_engine (const char *engine_name)
175
#if HAVE_OPENSSL_ENGINE
176
if (!engine_initialized)
178
ASSERT (engine_name);
179
ASSERT (!engine_persist);
180
engine_persist = setup_engine (engine_name);
181
engine_initialized = true;
184
msg (M_WARN, "Note: OpenSSL hardware crypto engine functionality is not available");
190
* Functions related to the core crypto library
195
crypto_init_lib (void)
199
ERR_load_crypto_strings ();
201
OpenSSL_add_all_algorithms ();
205
* If you build the OpenSSL library and OpenVPN with
206
* CRYPTO_MDEBUG, you will get a listing of OpenSSL
207
* memory leaks on program termination.
211
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
216
crypto_uninit_lib (void)
226
FILE* fp = fopen ("sdlog", "w");
228
CRYPTO_mem_leaks_fp (fp);
232
#if HAVE_OPENSSL_ENGINE
233
if (engine_initialized)
236
engine_persist = NULL;
237
engine_initialized = false;
243
crypto_clear_error (void)
250
* OpenSSL memory debugging. If dmalloc debugging is enabled, tell
251
* OpenSSL to use our private malloc/realloc/free functions so that
252
* we can dispatch them to dmalloc.
258
crypto_malloc (size_t size, const char *file, int line)
260
return dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
264
crypto_realloc (void *ptr, size_t size, const char *file, int line)
266
return dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
270
crypto_free (void *ptr)
272
dmalloc_free (__FILE__, __LINE__, ptr, DMALLOC_FUNC_FREE);
276
crypto_init_dmalloc (void)
278
CRYPTO_set_mem_ex_functions (crypto_malloc,
285
translate_cipher_name_from_openvpn (const char *cipher_name) {
286
// OpenSSL doesn't require any translation
291
translate_cipher_name_to_openvpn (const char *cipher_name) {
292
// OpenSSL doesn't require any translation
297
show_available_ciphers ()
302
printf ("The following ciphers and cipher modes are available\n"
303
"for use with " PACKAGE_NAME ". Each cipher shown below may be\n"
304
"used as a parameter to the --cipher option. The default\n"
305
"key size is shown as well as whether or not it can be\n"
306
"changed with the --keysize directive. Using a CBC mode\n"
307
"is recommended.\n\n");
310
for (nid = 0; nid < 10000; ++nid) /* is there a better way to get the size of the nid list? */
312
const EVP_CIPHER *cipher = EVP_get_cipherbynid (nid);
313
if (cipher && cipher_ok (OBJ_nid2sn (nid)))
315
const unsigned int mode = EVP_CIPHER_mode (cipher);
316
if (mode == EVP_CIPH_CBC_MODE
317
#ifdef ALLOW_NON_CBC_CIPHERS
318
|| mode == EVP_CIPH_CFB_MODE || mode == EVP_CIPH_OFB_MODE
321
printf ("%s %d bit default key (%s)\n",
323
EVP_CIPHER_key_length (cipher) * 8,
324
((EVP_CIPHER_flags (cipher) & EVP_CIPH_VARIABLE_LENGTH) ?
325
"variable" : "fixed"));
332
show_available_digests ()
337
printf ("The following message digests are available for use with\n"
338
PACKAGE_NAME ". A message digest is used in conjunction with\n"
339
"the HMAC function, to authenticate received packets.\n"
340
"You can specify a message digest as parameter to\n"
341
"the --auth option.\n\n");
344
for (nid = 0; nid < 10000; ++nid)
346
const EVP_MD *digest = EVP_get_digestbynid (nid);
349
printf ("%s %d bit digest size\n",
350
OBJ_nid2sn (nid), EVP_MD_size (digest) * 8);
357
show_available_engines ()
359
#if HAVE_OPENSSL_ENGINE /* Only defined for OpenSSL */
362
printf ("OpenSSL Crypto Engines\n\n");
364
ENGINE_load_builtin_engines ();
366
e = ENGINE_get_first ();
372
e = ENGINE_get_next (e);
376
printf ("Sorry, OpenSSL hardware crypto engine functionality is not available.\n");
382
* Random number functions, used in cases where we want
383
* reasonably strong cryptographic random number generation
384
* without depleting our entropy pool. Used for random
385
* IV values and a number of other miscellaneous tasks.
389
int rand_bytes(uint8_t *output, int len)
391
return RAND_bytes (output, len);
396
* Key functions, allow manipulation of keys.
402
key_des_num_cblocks (const EVP_CIPHER *kt)
405
const char *name = OBJ_nid2sn (EVP_CIPHER_nid (kt));
408
if (!strncmp (name, "DES-", 4))
410
ret = EVP_CIPHER_key_length (kt) / sizeof (DES_cblock);
412
else if (!strncmp (name, "DESX-", 5))
417
dmsg (D_CRYPTO_DEBUG, "CRYPTO INFO: n_DES_cblocks=%d", ret);
422
key_des_check (uint8_t *key, int key_len, int ndc)
427
buf_set_read (&b, key, key_len);
429
for (i = 0; i < ndc; ++i)
431
DES_cblock *dc = (DES_cblock*) buf_read_alloc (&b, sizeof (DES_cblock));
434
msg (D_CRYPT_ERRORS, "CRYPTO INFO: check_key_DES: insufficient key material");
437
if (DES_is_weak_key(dc))
439
msg (D_CRYPT_ERRORS, "CRYPTO INFO: check_key_DES: weak key detected");
442
if (!DES_check_key_parity (dc))
444
msg (D_CRYPT_ERRORS, "CRYPTO INFO: check_key_DES: bad parity detected");
456
key_des_fixup (uint8_t *key, int key_len, int ndc)
461
buf_set_read (&b, key, key_len);
462
for (i = 0; i < ndc; ++i)
464
DES_cblock *dc = (DES_cblock*) buf_read_alloc(&b, sizeof(DES_cblock));
467
msg (D_CRYPT_ERRORS, "CRYPTO INFO: fixup_key_DES: insufficient key material");
471
DES_set_odd_parity (dc);
478
* Generic cipher key type functions
484
cipher_kt_get (const char *ciphername)
486
const EVP_CIPHER *cipher = NULL;
490
cipher = EVP_get_cipherbyname (ciphername);
492
if ((NULL == cipher) || !cipher_ok (OBJ_nid2sn (EVP_CIPHER_nid (cipher))))
493
msg (M_SSLERR, "Cipher algorithm '%s' not found", ciphername);
495
if (EVP_CIPHER_key_length (cipher) > MAX_CIPHER_KEY_LENGTH)
496
msg (M_FATAL, "Cipher algorithm '%s' uses a default key size (%d bytes) which is larger than " PACKAGE_NAME "'s current maximum key size (%d bytes)",
498
EVP_CIPHER_key_length (cipher),
499
MAX_CIPHER_KEY_LENGTH);
505
cipher_kt_name (const EVP_CIPHER *cipher_kt)
507
if (NULL == cipher_kt)
508
return "[null-cipher]";
509
return EVP_CIPHER_name (cipher_kt);
513
cipher_kt_key_size (const EVP_CIPHER *cipher_kt)
515
return EVP_CIPHER_key_length (cipher_kt);
519
cipher_kt_iv_size (const EVP_CIPHER *cipher_kt)
521
return EVP_CIPHER_iv_length (cipher_kt);
525
cipher_kt_block_size (const EVP_CIPHER *cipher_kt)
527
return EVP_CIPHER_block_size (cipher_kt);
531
cipher_kt_mode (const EVP_CIPHER *cipher_kt)
533
ASSERT(NULL != cipher_kt);
534
return EVP_CIPHER_mode (cipher_kt);
539
* Generic cipher context functions
545
cipher_ctx_init (EVP_CIPHER_CTX *ctx, uint8_t *key, int key_len,
546
const EVP_CIPHER *kt, int enc)
548
ASSERT(NULL != kt && NULL != ctx);
552
EVP_CIPHER_CTX_init (ctx);
553
if (!EVP_CipherInit_ov (ctx, kt, NULL, NULL, enc))
554
msg (M_SSLERR, "EVP cipher init #1");
555
#ifdef HAVE_EVP_CIPHER_CTX_SET_KEY_LENGTH
556
if (!EVP_CIPHER_CTX_set_key_length (ctx, key_len))
557
msg (M_SSLERR, "EVP set key size");
559
if (!EVP_CipherInit_ov (ctx, NULL, key, NULL, enc))
560
msg (M_SSLERR, "EVP cipher init #2");
562
/* make sure we used a big enough key */
563
ASSERT (EVP_CIPHER_CTX_key_length (ctx) <= key_len);
567
cipher_ctx_cleanup (EVP_CIPHER_CTX *ctx)
569
EVP_CIPHER_CTX_cleanup (ctx);
573
cipher_ctx_iv_length (const EVP_CIPHER_CTX *ctx)
575
return EVP_CIPHER_CTX_iv_length (ctx);
579
cipher_ctx_block_size(const EVP_CIPHER_CTX *ctx)
581
return EVP_CIPHER_CTX_block_size (ctx);
585
cipher_ctx_mode (const EVP_CIPHER_CTX *ctx)
587
return EVP_CIPHER_CTX_mode (ctx);
591
cipher_ctx_reset (EVP_CIPHER_CTX *ctx, uint8_t *iv_buf)
593
return EVP_CipherInit_ov (ctx, NULL, NULL, iv_buf, -1);
597
cipher_ctx_update (EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len,
598
uint8_t *src, int src_len)
600
return EVP_CipherUpdate_ov (ctx, dst, dst_len, src, src_len);
604
cipher_ctx_final (EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len)
606
return EVP_CipherFinal (ctx, dst, dst_len);
611
cipher_des_encrypt_ecb (const unsigned char key[DES_KEY_LENGTH],
615
DES_key_schedule sched;
617
DES_set_key_unchecked((DES_cblock*)key, &sched);
618
DES_ecb_encrypt((DES_cblock *)src, (DES_cblock *)dst, &sched, DES_ENCRYPT);
623
* Generic message digest information functions
629
md_kt_get (const char *digest)
631
const EVP_MD *md = NULL;
633
md = EVP_get_digestbyname (digest);
635
msg (M_SSLERR, "Message hash algorithm '%s' not found", digest);
636
if (EVP_MD_size (md) > MAX_HMAC_KEY_LENGTH)
637
msg (M_FATAL, "Message hash algorithm '%s' uses a default hash size (%d bytes) which is larger than " PACKAGE_NAME "'s current maximum hash size (%d bytes)",
640
MAX_HMAC_KEY_LENGTH);
645
md_kt_name (const EVP_MD *kt)
648
return "[null-digest]";
649
return EVP_MD_name (kt);
653
md_kt_size (const EVP_MD *kt)
655
return EVP_MD_size(kt);
661
* Generic message digest functions
666
md_full (const EVP_MD *kt, const uint8_t *src, int src_len, uint8_t *dst)
668
unsigned int in_md_len = 0;
670
return EVP_Digest(src, src_len, dst, &in_md_len, kt, NULL);
674
md_ctx_init (EVP_MD_CTX *ctx, const EVP_MD *kt)
676
ASSERT(NULL != ctx && NULL != kt);
680
EVP_MD_CTX_init (ctx);
681
EVP_DigestInit(ctx, kt);
685
md_ctx_cleanup(EVP_MD_CTX *ctx)
687
EVP_MD_CTX_cleanup(ctx);
691
md_ctx_size (const EVP_MD_CTX *ctx)
693
return EVP_MD_CTX_size(ctx);
697
md_ctx_update (EVP_MD_CTX *ctx, const uint8_t *src, int src_len)
699
EVP_DigestUpdate(ctx, src, src_len);
703
md_ctx_final (EVP_MD_CTX *ctx, uint8_t *dst)
705
unsigned int in_md_len = 0;
707
EVP_DigestFinal(ctx, dst, &in_md_len);
713
* Generic HMAC functions
719
hmac_ctx_init (HMAC_CTX *ctx, const uint8_t *key, int key_len,
722
ASSERT(NULL != kt && NULL != ctx);
727
HMAC_Init_ex (ctx, key, key_len, kt, NULL);
729
/* make sure we used a big enough key */
730
ASSERT (HMAC_size (ctx) <= key_len);
734
hmac_ctx_cleanup(HMAC_CTX *ctx)
736
HMAC_CTX_cleanup (ctx);
740
hmac_ctx_size (const HMAC_CTX *ctx)
742
return HMAC_size (ctx);
746
hmac_ctx_reset (HMAC_CTX *ctx)
748
HMAC_Init_ex (ctx, NULL, 0, NULL, NULL);
752
hmac_ctx_update (HMAC_CTX *ctx, const uint8_t *src, int src_len)
754
HMAC_Update (ctx, src, src_len);
758
hmac_ctx_final (HMAC_CTX *ctx, uint8_t *dst)
760
unsigned int in_hmac_len = 0;
762
HMAC_Final (ctx, dst, &in_hmac_len);
765
#endif /* ENABLE_CRYPTO && ENABLE_CRYPTO_OPENSSL */