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-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define ALLOW_NON_CBC_CIPHERS
32 * Does our OpenSSL library support crypto hardware acceleration?
34 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_LOAD_BUILTIN_ENGINES) && defined(HAVE_ENGINE_REGISTER_ALL_COMPLETE) && defined(HAVE_ENGINE_CLEANUP)
35 #define CRYPTO_ENGINE 1
37 #define CRYPTO_ENGINE 0
40 #include <openssl/objects.h>
41 #include <openssl/rand.h>
42 #include <openssl/evp.h>
43 #include <openssl/hmac.h>
44 #include <openssl/des.h>
45 #include <openssl/md5.h>
47 #include <openssl/md4.h>
49 #include <openssl/sha.h>
50 #include <openssl/err.h>
53 #include <openssl/engine.h>
56 #if SSLEAY_VERSION_NUMBER >= 0x00907000L
57 #include <openssl/des_old.h>
62 #include "packet_id.h"
66 * Workarounds for incompatibilites between OpenSSL libraries.
67 * Right now we accept OpenSSL libraries from 0.9.5 to 0.9.7.
70 #if SSLEAY_VERSION_NUMBER < 0x00907000L
72 /* Workaround: EVP_CIPHER_mode is defined wrong in OpenSSL 0.9.6 but is fixed in 0.9.7 */
73 #undef EVP_CIPHER_mode
74 #define EVP_CIPHER_mode(e) (((e)->flags) & EVP_CIPH_MODE)
76 #define DES_cblock des_cblock
77 #define DES_is_weak_key des_is_weak_key
78 #define DES_check_key_parity des_check_key_parity
79 #define DES_set_odd_parity des_set_odd_parity
81 #define HMAC_CTX_init(ctx) CLEAR (*ctx)
82 #define HMAC_Init_ex(ctx,sec,len,md,impl) HMAC_Init(ctx, sec, len, md)
83 #define HMAC_CTX_cleanup(ctx) HMAC_cleanup(ctx)
84 #define EVP_MD_CTX_cleanup(md) CLEAR (*md)
86 #define INFO_CALLBACK_SSL_CONST
90 #ifndef INFO_CALLBACK_SSL_CONST
91 #define INFO_CALLBACK_SSL_CONST const
94 #if SSLEAY_VERSION_NUMBER < 0x00906000
96 #undef EVP_CIPHER_mode
97 #define EVP_CIPHER_mode(x) 1
98 #define EVP_CIPHER_CTX_mode(x) 1
99 #define EVP_CIPHER_flags(x) 0
101 #define EVP_CIPH_CBC_MODE 1
102 #define EVP_CIPH_CFB_MODE 0
103 #define EVP_CIPH_OFB_MODE 0
104 #define EVP_CIPH_VARIABLE_LENGTH 0
106 #define OPENSSL_malloc(x) malloc(x)
107 #define OPENSSL_free(x) free(x)
110 EVP_CipherInit_ov (EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*type
, uint8_t *key
, uint8_t *iv
, int enc
)
112 EVP_CipherInit (ctx
, type
, key
, iv
, enc
);
117 EVP_CipherUpdate_ov (EVP_CIPHER_CTX
*ctx
, uint8_t *out
, int *outl
, uint8_t *in
, int inl
)
119 EVP_CipherUpdate (ctx
, out
, outl
, in
, inl
);
124 cipher_ok (const char* name
)
126 const int i
= strlen (name
) - 4;
128 return !strcmp (name
+ i
, "-CBC");
136 EVP_CipherInit_ov (EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*type
, uint8_t *key
, uint8_t *iv
, int enc
)
138 return EVP_CipherInit (ctx
, type
, key
, iv
, enc
);
142 EVP_CipherUpdate_ov (EVP_CIPHER_CTX
*ctx
, uint8_t *out
, int *outl
, uint8_t *in
, int inl
)
144 return EVP_CipherUpdate (ctx
, out
, outl
, in
, inl
);
148 cipher_ok (const char* name
)
155 #if SSLEAY_VERSION_NUMBER < 0x0090581f
156 #undef DES_check_key_parity
157 #define DES_check_key_parity(x) 1
160 #ifndef EVP_CIPHER_name
161 #define EVP_CIPHER_name(e) OBJ_nid2sn(EVP_CIPHER_nid(e))
165 #define EVP_MD_name(e) OBJ_nid2sn(EVP_MD_type(e))
169 * Max size in bytes of any cipher key that might conceivably be used.
171 * This value is checked at compile time in crypto.c to make sure
172 * it is always at least EVP_MAX_KEY_LENGTH.
174 * We define our own value, since this parameter
175 * is used to control the size of static key files.
176 * If the OpenSSL library increases EVP_MAX_KEY_LENGTH,
177 * we don't want our key files to be suddenly rendered
180 #define MAX_CIPHER_KEY_LENGTH 64
183 * Max size in bytes of any HMAC key that might conceivably be used.
185 * This value is checked at compile time in crypto.c to make sure
186 * it is always at least EVP_MAX_MD_SIZE. We define our own value
187 * for the same reason as above.
189 #define MAX_HMAC_KEY_LENGTH 64
192 * Defines a key type and key length for both cipher and HMAC.
196 uint8_t cipher_length
;
198 const EVP_CIPHER
*cipher
;
199 const EVP_MD
*digest
;
207 uint8_t cipher
[MAX_CIPHER_KEY_LENGTH
];
208 uint8_t hmac
[MAX_HMAC_KEY_LENGTH
];
211 #define KEY_DIRECTION_BIDIRECTIONAL 0 /* same keys for both directions */
212 #define KEY_DIRECTION_NORMAL 1 /* encrypt with keys[0], decrypt with keys[1] */
213 #define KEY_DIRECTION_INVERSE 2 /* encrypt with keys[1], decrypt with keys[0] */
216 * Dual random keys (for encrypt/decrypt)
225 * Used for controlling bidirectional keys
226 * vs. a separate key for each direction.
228 struct key_direction_state
236 * A key context for cipher and/or HMAC.
240 EVP_CIPHER_CTX
*cipher
;
245 * Cipher/HMAC key context for both sending and receiving
250 struct key_ctx encrypt
;
251 struct key_ctx decrypt
;
255 * Options for encrypt/decrypt.
257 struct crypto_options
259 struct key_ctx_bi
*key_ctx_bi
;
260 struct packet_id
*packet_id
;
261 struct packet_id_persist
*pid_persist
;
263 # define CO_PACKET_ID_LONG_FORM (1<<0)
264 # define CO_USE_IV (1<<1)
265 # define CO_IGNORE_PACKET_ID (1<<2)
266 # define CO_MUTE_REPLAY_WARNINGS (1<<3)
270 void init_key_type (struct key_type
*kt
, const char *ciphername
,
271 bool ciphername_defined
, const char *authname
,
272 bool authname_defined
, int keysize
,
273 bool cfb_ofb_allowed
, bool warn
);
275 #define RKF_MUST_SUCCEED (1<<0)
276 #define RKF_INLINE (1<<1)
277 void read_key_file (struct key2
*key2
, const char *file
, const unsigned int flags
);
279 int write_key_file (const int nkeys
, const char *filename
);
281 int read_passphrase_hash (const char *passphrase_file
,
282 const EVP_MD
*digest
,
286 void generate_key_random (struct key
*key
, const struct key_type
*kt
);
288 void check_replay_iv_consistency(const struct key_type
*kt
, bool packet_id
, bool use_iv
);
290 bool check_key (struct key
*key
, const struct key_type
*kt
);
292 void fixup_key (struct key
*key
, const struct key_type
*kt
);
294 bool write_key (const struct key
*key
, const struct key_type
*kt
,
297 int read_key (struct key
*key
, const struct key_type
*kt
, struct buffer
*buf
);
299 bool cfb_ofb_mode (const struct key_type
* kt
);
301 const char *kt_cipher_name (const struct key_type
*kt
);
302 const char *kt_digest_name (const struct key_type
*kt
);
303 int kt_key_size (const struct key_type
*kt
);
305 /* enc parameter in init_key_ctx */
309 void init_key_ctx (struct key_ctx
*ctx
, struct key
*key
,
310 const struct key_type
*kt
, int enc
,
313 void free_key_ctx (struct key_ctx
*ctx
);
314 void free_key_ctx_bi (struct key_ctx_bi
*ctx
);
316 void openvpn_encrypt (struct buffer
*buf
, struct buffer work
,
317 const struct crypto_options
*opt
,
318 const struct frame
* frame
);
320 bool openvpn_decrypt (struct buffer
*buf
, struct buffer work
,
321 const struct crypto_options
*opt
,
322 const struct frame
* frame
);
325 void crypto_adjust_frame_parameters(struct frame
*frame
,
326 const struct key_type
* kt
,
330 bool packet_id_long_form
);
332 #define NONCE_SECRET_LEN_MIN 16
333 #define NONCE_SECRET_LEN_MAX 64
334 void prng_init (const char *md_name
, const int nonce_secret_len_parm
);
335 void prng_bytes (uint8_t *output
, int len
);
338 void test_crypto (const struct crypto_options
*co
, struct frame
* f
);
340 const char *md5sum(uint8_t *buf
, int len
, int n_print_chars
, struct gc_arena
*gc
);
342 void show_available_ciphers (void);
344 void show_available_digests (void);
346 void show_available_engines (void);
348 void init_crypto_lib_engine (const char *engine_name
);
350 void init_crypto_lib (void);
352 void uninit_crypto_lib (void);
354 /* key direction functions */
356 void key_direction_state_init (struct key_direction_state
*kds
, int key_direction
);
358 void verify_fix_key2 (struct key2
*key2
, const struct key_type
*kt
, const char *shared_secret_file
);
360 void must_have_n_keys (const char *filename
, const char *option
, const struct key2
*key2
, int n
);
362 int ascii2keydirection (int msglevel
, const char *str
);
364 const char *keydirection2ascii (int kd
, bool remote
);
367 void key2_print (const struct key2
* k
,
368 const struct key_type
*kt
,
370 const char* prefix1
);
372 /* memory debugging */
373 void openssl_dmalloc_init (void);
377 #define GHK_INLINE (1<<0)
378 void get_tls_handshake_key (const struct key_type
*key_type
,
379 struct key_ctx_bi
*ctx
,
380 const char *passphrase_file
,
381 const int key_direction
,
382 const unsigned int flags
);
386 void init_ssl_lib (void);
387 void free_ssl_lib (void);
396 key_ctx_bi_defined(const struct key_ctx_bi
* key
)
398 return key
->encrypt
.cipher
|| key
->encrypt
.hmac
|| key
->decrypt
.cipher
|| key
->decrypt
.hmac
;
410 uint8_t digest
[MD5_DIGEST_LENGTH
];
413 void md5_state_init (struct md5_state
*s
);
414 void md5_state_update (struct md5_state
*s
, void *data
, size_t len
);
415 void md5_state_final (struct md5_state
*s
, struct md5_digest
*out
);
416 void md5_digest_clear (struct md5_digest
*digest
);
417 bool md5_digest_defined (const struct md5_digest
*digest
);
418 bool md5_digest_equal (const struct md5_digest
*d1
, const struct md5_digest
*d2
);
420 #endif /* USE_CRYPTO */
421 #endif /* CRYPTO_H */