1 /* 2 * Copyright (c) 2005 Darren Tucker <dtucker@zip.com.au> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef _OPENSSL_COMPAT_H 18 #define _OPENSSL_COMPAT_H 19 20 #include "includes.h" 21 #ifdef WITH_OPENSSL 22 23 #include <openssl/opensslv.h> 24 #include <openssl/evp.h> 25 #include <openssl/rsa.h> 26 #include <openssl/dsa.h> 27 #include <openssl/ecdsa.h> 28 #include <openssl/dh.h> 29 30 int ssh_compatible_openssl(long, long); 31 32 #if (OPENSSL_VERSION_NUMBER <= 0x0090805fL) 33 # error OpenSSL 0.9.8f or greater is required 34 #endif 35 36 #if OPENSSL_VERSION_NUMBER < 0x10000001L 37 # define LIBCRYPTO_EVP_INL_TYPE unsigned int 38 #else 39 # define LIBCRYPTO_EVP_INL_TYPE size_t 40 #endif 41 42 #ifndef OPENSSL_RSA_MAX_MODULUS_BITS 43 # define OPENSSL_RSA_MAX_MODULUS_BITS 16384 44 #endif 45 #ifndef OPENSSL_DSA_MAX_MODULUS_BITS 46 # define OPENSSL_DSA_MAX_MODULUS_BITS 10000 47 #endif 48 49 #ifndef OPENSSL_HAVE_EVPCTR 50 # define EVP_aes_128_ctr evp_aes_128_ctr 51 # define EVP_aes_192_ctr evp_aes_128_ctr 52 # define EVP_aes_256_ctr evp_aes_128_ctr 53 const EVP_CIPHER *evp_aes_128_ctr(void); 54 void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, size_t); 55 #endif 56 57 /* Avoid some #ifdef. Code that uses these is unreachable without GCM */ 58 #if !defined(OPENSSL_HAVE_EVPGCM) && !defined(EVP_CTRL_GCM_SET_IV_FIXED) 59 # define EVP_CTRL_GCM_SET_IV_FIXED -1 60 # define EVP_CTRL_GCM_IV_GEN -1 61 # define EVP_CTRL_GCM_SET_TAG -1 62 # define EVP_CTRL_GCM_GET_TAG -1 63 #endif 64 65 /* Replace missing EVP_CIPHER_CTX_ctrl() with something that returns failure */ 66 #ifndef HAVE_EVP_CIPHER_CTX_CTRL 67 # ifdef OPENSSL_HAVE_EVPGCM 68 # error AES-GCM enabled without EVP_CIPHER_CTX_ctrl /* shouldn't happen */ 69 # else 70 # define EVP_CIPHER_CTX_ctrl(a,b,c,d) (0) 71 # endif 72 #endif 73 74 #if defined(HAVE_EVP_RIPEMD160) 75 # if defined(OPENSSL_NO_RIPEMD) || defined(OPENSSL_NO_RMD160) 76 # undef HAVE_EVP_RIPEMD160 77 # endif 78 #endif 79 80 /* 81 * We overload some of the OpenSSL crypto functions with ssh_* equivalents 82 * to automatically handle OpenSSL engine initialisation. 83 * 84 * In order for the compat library to call the real functions, it must 85 * define SSH_DONT_OVERLOAD_OPENSSL_FUNCS before including this file and 86 * implement the ssh_* equivalents. 87 */ 88 #ifndef SSH_DONT_OVERLOAD_OPENSSL_FUNCS 89 90 # ifdef USE_OPENSSL_ENGINE 91 # ifdef OpenSSL_add_all_algorithms 92 # undef OpenSSL_add_all_algorithms 93 # endif 94 # define OpenSSL_add_all_algorithms() ssh_OpenSSL_add_all_algorithms() 95 # endif 96 97 void ssh_OpenSSL_add_all_algorithms(void); 98 99 #endif /* SSH_DONT_OVERLOAD_OPENSSL_FUNCS */ 100 101 /* LibreSSL/OpenSSL 1.1x API compat */ 102 #ifndef HAVE_DSA_GET0_PQG 103 void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, 104 const BIGNUM **g); 105 #endif /* HAVE_DSA_GET0_PQG */ 106 107 #ifndef HAVE_DSA_SET0_PQG 108 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g); 109 #endif /* HAVE_DSA_SET0_PQG */ 110 111 #ifndef HAVE_DSA_GET0_KEY 112 void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, 113 const BIGNUM **priv_key); 114 #endif /* HAVE_DSA_GET0_KEY */ 115 116 #ifndef HAVE_DSA_SET0_KEY 117 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key); 118 #endif /* HAVE_DSA_SET0_KEY */ 119 120 #ifndef HAVE_EVP_CIPHER_CTX_GET_IV 121 int EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, 122 unsigned char *iv, size_t len); 123 #endif /* HAVE_EVP_CIPHER_CTX_GET_IV */ 124 125 #ifndef HAVE_EVP_CIPHER_CTX_SET_IV 126 int EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, 127 const unsigned char *iv, size_t len); 128 #endif /* HAVE_EVP_CIPHER_CTX_SET_IV */ 129 130 #ifndef HAVE_RSA_GET0_KEY 131 void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, 132 const BIGNUM **d); 133 #endif /* HAVE_RSA_GET0_KEY */ 134 135 #ifndef HAVE_RSA_SET0_KEY 136 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d); 137 #endif /* HAVE_RSA_SET0_KEY */ 138 139 #ifndef HAVE_RSA_GET0_CRT_PARAMS 140 void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, 141 const BIGNUM **iqmp); 142 #endif /* HAVE_RSA_GET0_CRT_PARAMS */ 143 144 #ifndef HAVE_RSA_SET0_CRT_PARAMS 145 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp); 146 #endif /* HAVE_RSA_SET0_CRT_PARAMS */ 147 148 #ifndef HAVE_RSA_GET0_FACTORS 149 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q); 150 #endif /* HAVE_RSA_GET0_FACTORS */ 151 152 #ifndef HAVE_RSA_SET0_FACTORS 153 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q); 154 #endif /* HAVE_RSA_SET0_FACTORS */ 155 156 #ifndef DSA_SIG_GET0 157 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps); 158 #endif /* DSA_SIG_GET0 */ 159 160 #ifndef DSA_SIG_SET0 161 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s); 162 #endif /* DSA_SIG_SET0 */ 163 164 #ifndef HAVE_ECDSA_SIG_GET0 165 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps); 166 #endif /* HAVE_ECDSA_SIG_GET0 */ 167 168 #ifndef HAVE_ECDSA_SIG_SET0 169 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s); 170 #endif /* HAVE_ECDSA_SIG_SET0 */ 171 172 #ifndef HAVE_DH_GET0_PQG 173 void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, 174 const BIGNUM **g); 175 #endif /* HAVE_DH_GET0_PQG */ 176 177 #ifndef HAVE_DH_SET0_PQG 178 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g); 179 #endif /* HAVE_DH_SET0_PQG */ 180 181 #ifndef HAVE_DH_GET0_KEY 182 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key); 183 #endif /* HAVE_DH_GET0_KEY */ 184 185 #ifndef HAVE_DH_SET0_KEY 186 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key); 187 #endif /* HAVE_DH_SET0_KEY */ 188 189 #ifndef HAVE_DH_SET_LENGTH 190 int DH_set_length(DH *dh, long length); 191 #endif /* HAVE_DH_SET_LENGTH */ 192 193 #ifndef HAVE_RSA_METH_FREE 194 void RSA_meth_free(RSA_METHOD *meth); 195 #endif /* HAVE_RSA_METH_FREE */ 196 197 #ifndef HAVE_RSA_METH_DUP 198 RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth); 199 #endif /* HAVE_RSA_METH_DUP */ 200 201 #ifndef HAVE_RSA_METH_SET1_NAME 202 int RSA_meth_set1_name(RSA_METHOD *meth, const char *name); 203 #endif /* HAVE_RSA_METH_SET1_NAME */ 204 205 #ifndef HAVE_RSA_METH_GET_FINISH 206 int (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa); 207 #endif /* HAVE_RSA_METH_GET_FINISH */ 208 209 #ifndef HAVE_RSA_METH_SET_PRIV_ENC 210 int RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen, 211 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)); 212 #endif /* HAVE_RSA_METH_SET_PRIV_ENC */ 213 214 #ifndef HAVE_RSA_METH_SET_PRIV_DEC 215 int RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen, 216 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)); 217 #endif /* HAVE_RSA_METH_SET_PRIV_DEC */ 218 219 #ifndef HAVE_RSA_METH_SET_FINISH 220 int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa)); 221 #endif /* HAVE_RSA_METH_SET_FINISH */ 222 223 #ifndef HAVE_EVP_PKEY_GET0_RSA 224 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey); 225 #endif /* HAVE_EVP_PKEY_GET0_RSA */ 226 227 #ifndef HAVE_EVP_MD_CTX_new 228 EVP_MD_CTX *EVP_MD_CTX_new(void); 229 #endif /* HAVE_EVP_MD_CTX_new */ 230 231 #ifndef HAVE_EVP_MD_CTX_free 232 void EVP_MD_CTX_free(EVP_MD_CTX *ctx); 233 #endif /* HAVE_EVP_MD_CTX_free */ 234 235 #endif /* WITH_OPENSSL */ 236 #endif /* _OPENSSL_COMPAT_H */ 237