1/* $Id: openssl-compat.h,v 1.24 2013/02/12 00:00:40 djm Exp $ */ 2 3/* 4 * Copyright (c) 2005 Darren Tucker <dtucker@zip.com.au> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include "includes.h" 20#ifdef __APPLE_CRYPTO__ 21#include "ossl-crypto.h" 22#include "ossl-evp.h" 23#include "ossl-rsa.h" 24#include "ossl-dsa.h" 25#else 26#include <openssl/opensslv.h> 27#include <openssl/evp.h> 28#include <openssl/rsa.h> 29#include <openssl/dsa.h> 30#endif 31 32/* Only in 0.9.8 */ 33#ifndef OPENSSL_DSA_MAX_MODULUS_BITS 34# define OPENSSL_DSA_MAX_MODULUS_BITS 10000 35#endif 36#ifndef OPENSSL_RSA_MAX_MODULUS_BITS 37# define OPENSSL_RSA_MAX_MODULUS_BITS 16384 38#endif 39 40/* OPENSSL_free() is Free() in versions before OpenSSL 0.9.6 */ 41#if !defined(OPENSSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x0090600f) 42# define OPENSSL_free(x) Free(x) 43#endif 44 45#if OPENSSL_VERSION_NUMBER < 0x00906000L 46# define SSH_OLD_EVP 47# define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data) 48#endif 49 50#if OPENSSL_VERSION_NUMBER < 0x10000001L 51# define LIBCRYPTO_EVP_INL_TYPE unsigned int 52#else 53# define LIBCRYPTO_EVP_INL_TYPE size_t 54#endif 55 56#if (OPENSSL_VERSION_NUMBER < 0x00907000L) || defined(OPENSSL_LOBOTOMISED_AES) 57# define USE_BUILTIN_RIJNDAEL 58#endif 59 60#ifdef USE_BUILTIN_RIJNDAEL 61# include "rijndael.h" 62# define AES_KEY rijndael_ctx 63# define AES_BLOCK_SIZE 16 64# define AES_encrypt(a, b, c) rijndael_encrypt(c, a, b) 65# define AES_set_encrypt_key(a, b, c) rijndael_set_key(c, (char *)a, b, 1) 66# define EVP_aes_128_cbc evp_rijndael 67# define EVP_aes_192_cbc evp_rijndael 68# define EVP_aes_256_cbc evp_rijndael 69const EVP_CIPHER *evp_rijndael(void); 70void ssh_rijndael_iv(EVP_CIPHER_CTX *, int, u_char *, u_int); 71#endif 72 73#ifndef OPENSSL_HAVE_EVPCTR 74#define EVP_aes_128_ctr evp_aes_128_ctr 75#define EVP_aes_192_ctr evp_aes_128_ctr 76#define EVP_aes_256_ctr evp_aes_128_ctr 77const EVP_CIPHER *evp_aes_128_ctr(void); 78void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, size_t); 79#endif 80 81/* Avoid some #ifdef. Code that uses these is unreachable without GCM */ 82#if !defined(OPENSSL_HAVE_EVPGCM) && !defined(EVP_CTRL_GCM_SET_IV_FIXED) 83# define EVP_CTRL_GCM_SET_IV_FIXED -1 84# define EVP_CTRL_GCM_IV_GEN -1 85# define EVP_CTRL_GCM_SET_TAG -1 86# define EVP_CTRL_GCM_GET_TAG -1 87#endif 88 89/* Replace missing EVP_CIPHER_CTX_ctrl() with something that returns failure */ 90#ifndef HAVE_EVP_CIPHER_CTX_CTRL 91# ifdef OPENSSL_HAVE_EVPGCM 92# error AES-GCM enabled without EVP_CIPHER_CTX_ctrl /* shouldn't happen */ 93# else 94# define EVP_CIPHER_CTX_ctrl(a,b,c,d) (0) 95# endif 96#endif 97 98#if OPENSSL_VERSION_NUMBER < 0x00907000L 99#define EVP_X_STATE(evp) &(evp).c 100#define EVP_X_STATE_LEN(evp) sizeof((evp).c) 101#else 102#define EVP_X_STATE(evp) (evp).cipher_data 103#define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size 104#endif 105 106/* OpenSSL 0.9.8e returns cipher key len not context key len */ 107#if (OPENSSL_VERSION_NUMBER == 0x0090805fL) 108# define EVP_CIPHER_CTX_key_length(c) ((c)->key_len) 109#endif 110 111#ifndef HAVE_RSA_GET_DEFAULT_METHOD 112RSA_METHOD *RSA_get_default_method(void); 113#endif 114 115/* 116 * We overload some of the OpenSSL crypto functions with ssh_* equivalents 117 * which cater for older and/or less featureful OpenSSL version. 118 * 119 * In order for the compat library to call the real functions, it must 120 * define SSH_DONT_OVERLOAD_OPENSSL_FUNCS before including this file and 121 * implement the ssh_* equivalents. 122 */ 123#ifndef SSH_DONT_OVERLOAD_OPENSSL_FUNCS 124 125# ifdef SSH_OLD_EVP 126# ifdef EVP_Cipher 127# undef EVP_Cipher 128# endif 129# define EVP_CipherInit(a,b,c,d,e) ssh_EVP_CipherInit((a),(b),(c),(d),(e)) 130# define EVP_Cipher(a,b,c,d) ssh_EVP_Cipher((a),(b),(c),(d)) 131# define EVP_CIPHER_CTX_cleanup(a) ssh_EVP_CIPHER_CTX_cleanup((a)) 132# endif /* SSH_OLD_EVP */ 133 134# ifdef OPENSSL_EVP_DIGESTUPDATE_VOID 135# define EVP_DigestUpdate(a,b,c) ssh_EVP_DigestUpdate((a),(b),(c)) 136# endif 137 138# ifdef USE_OPENSSL_ENGINE 139# ifdef OpenSSL_add_all_algorithms 140# undef OpenSSL_add_all_algorithms 141# endif 142# define OpenSSL_add_all_algorithms() ssh_OpenSSL_add_all_algorithms() 143# endif 144 145# ifndef HAVE_BN_IS_PRIME_EX 146int BN_is_prime_ex(const BIGNUM *, int, BN_CTX *, void *); 147# endif 148 149# ifndef HAVE_DSA_GENERATE_PARAMETERS_EX 150int DSA_generate_parameters_ex(DSA *, int, const unsigned char *, int, int *, 151 unsigned long *, void *); 152# endif 153 154# ifndef HAVE_RSA_GENERATE_KEY_EX 155int RSA_generate_key_ex(RSA *, int, BIGNUM *, void *); 156# endif 157 158int ssh_EVP_CipherInit(EVP_CIPHER_CTX *, const EVP_CIPHER *, unsigned char *, 159 unsigned char *, int); 160int ssh_EVP_Cipher(EVP_CIPHER_CTX *, char *, char *, int); 161int ssh_EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *); 162void ssh_OpenSSL_add_all_algorithms(void); 163 164# ifndef HAVE_HMAC_CTX_INIT 165# define HMAC_CTX_init(a) 166# endif 167 168#endif /* SSH_DONT_OVERLOAD_OPENSSL_FUNCS */ 169 170