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 < 0x10100000L
37 # define OpenSSL_version_num	SSLeay
38 # define OpenSSL_version	SSLeay_version
39 # define OPENSSL_VERSION	SSLEAY_VERSION
40 #endif
41 
42 #if OPENSSL_VERSION_NUMBER < 0x10000001L
43 # define LIBCRYPTO_EVP_INL_TYPE unsigned int
44 #else
45 # define LIBCRYPTO_EVP_INL_TYPE size_t
46 #endif
47 
48 #ifndef OPENSSL_RSA_MAX_MODULUS_BITS
49 # define OPENSSL_RSA_MAX_MODULUS_BITS	16384
50 #endif
51 #ifndef OPENSSL_DSA_MAX_MODULUS_BITS
52 # define OPENSSL_DSA_MAX_MODULUS_BITS	10000
53 #endif
54 
55 #ifndef OPENSSL_HAVE_EVPCTR
56 # define EVP_aes_128_ctr evp_aes_128_ctr
57 # define EVP_aes_192_ctr evp_aes_128_ctr
58 # define EVP_aes_256_ctr evp_aes_128_ctr
59 const EVP_CIPHER *evp_aes_128_ctr(void);
60 void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, size_t);
61 #endif
62 
63 /* Avoid some #ifdef. Code that uses these is unreachable without GCM */
64 #if !defined(OPENSSL_HAVE_EVPGCM) && !defined(EVP_CTRL_GCM_SET_IV_FIXED)
65 # define EVP_CTRL_GCM_SET_IV_FIXED -1
66 # define EVP_CTRL_GCM_IV_GEN -1
67 # define EVP_CTRL_GCM_SET_TAG -1
68 # define EVP_CTRL_GCM_GET_TAG -1
69 #endif
70 
71 /* Replace missing EVP_CIPHER_CTX_ctrl() with something that returns failure */
72 #ifndef HAVE_EVP_CIPHER_CTX_CTRL
73 # ifdef OPENSSL_HAVE_EVPGCM
74 #  error AES-GCM enabled without EVP_CIPHER_CTX_ctrl /* shouldn't happen */
75 # else
76 # define EVP_CIPHER_CTX_ctrl(a,b,c,d) (0)
77 # endif
78 #endif
79 
80 #if defined(HAVE_EVP_RIPEMD160)
81 # if defined(OPENSSL_NO_RIPEMD) || defined(OPENSSL_NO_RMD160)
82 #  undef HAVE_EVP_RIPEMD160
83 # endif
84 #endif
85 
86 /*
87  * We overload some of the OpenSSL crypto functions with ssh_* equivalents
88  * to automatically handle OpenSSL engine initialisation.
89  *
90  * In order for the compat library to call the real functions, it must
91  * define SSH_DONT_OVERLOAD_OPENSSL_FUNCS before including this file and
92  * implement the ssh_* equivalents.
93  */
94 #ifndef SSH_DONT_OVERLOAD_OPENSSL_FUNCS
95 
96 # ifdef USE_OPENSSL_ENGINE
97 #  ifdef OpenSSL_add_all_algorithms
98 #   undef OpenSSL_add_all_algorithms
99 #  endif
100 #  define OpenSSL_add_all_algorithms()  ssh_OpenSSL_add_all_algorithms()
101 # endif
102 
103 void ssh_OpenSSL_add_all_algorithms(void);
104 
105 #endif	/* SSH_DONT_OVERLOAD_OPENSSL_FUNCS */
106 
107 /* LibreSSL/OpenSSL 1.1x API compat */
108 #ifndef HAVE_DSA_GET0_PQG
109 void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q,
110     const BIGNUM **g);
111 #endif /* HAVE_DSA_GET0_PQG */
112 
113 #ifndef HAVE_DSA_SET0_PQG
114 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g);
115 #endif /* HAVE_DSA_SET0_PQG */
116 
117 #ifndef HAVE_DSA_GET0_KEY
118 void DSA_get0_key(const DSA *d, const BIGNUM **pub_key,
119     const BIGNUM **priv_key);
120 #endif /* HAVE_DSA_GET0_KEY */
121 
122 #ifndef HAVE_DSA_SET0_KEY
123 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key);
124 #endif /* HAVE_DSA_SET0_KEY */
125 
126 #ifndef HAVE_EVP_CIPHER_CTX_GET_IV
127 int EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx,
128     unsigned char *iv, size_t len);
129 #endif /* HAVE_EVP_CIPHER_CTX_GET_IV */
130 
131 #ifndef HAVE_EVP_CIPHER_CTX_SET_IV
132 int EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx,
133     const unsigned char *iv, size_t len);
134 #endif /* HAVE_EVP_CIPHER_CTX_SET_IV */
135 
136 #ifndef HAVE_RSA_GET0_KEY
137 void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e,
138     const BIGNUM **d);
139 #endif /* HAVE_RSA_GET0_KEY */
140 
141 #ifndef HAVE_RSA_SET0_KEY
142 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
143 #endif /* HAVE_RSA_SET0_KEY */
144 
145 #ifndef HAVE_RSA_GET0_CRT_PARAMS
146 void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
147     const BIGNUM **iqmp);
148 #endif /* HAVE_RSA_GET0_CRT_PARAMS */
149 
150 #ifndef HAVE_RSA_SET0_CRT_PARAMS
151 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
152 #endif /* HAVE_RSA_SET0_CRT_PARAMS */
153 
154 #ifndef HAVE_RSA_GET0_FACTORS
155 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
156 #endif /* HAVE_RSA_GET0_FACTORS */
157 
158 #ifndef HAVE_RSA_SET0_FACTORS
159 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
160 #endif /* HAVE_RSA_SET0_FACTORS */
161 
162 #ifndef DSA_SIG_GET0
163 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
164 #endif /* DSA_SIG_GET0 */
165 
166 #ifndef DSA_SIG_SET0
167 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
168 #endif /* DSA_SIG_SET0 */
169 
170 #ifndef HAVE_ECDSA_SIG_GET0
171 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
172 #endif /* HAVE_ECDSA_SIG_GET0 */
173 
174 #ifndef HAVE_ECDSA_SIG_SET0
175 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
176 #endif /* HAVE_ECDSA_SIG_SET0 */
177 
178 #ifndef HAVE_DH_GET0_PQG
179 void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q,
180     const BIGNUM **g);
181 #endif /* HAVE_DH_GET0_PQG */
182 
183 #ifndef HAVE_DH_SET0_PQG
184 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
185 #endif /* HAVE_DH_SET0_PQG */
186 
187 #ifndef HAVE_DH_GET0_KEY
188 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
189 #endif /* HAVE_DH_GET0_KEY */
190 
191 #ifndef HAVE_DH_SET0_KEY
192 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
193 #endif /* HAVE_DH_SET0_KEY */
194 
195 #ifndef HAVE_DH_SET_LENGTH
196 int DH_set_length(DH *dh, long length);
197 #endif /* HAVE_DH_SET_LENGTH */
198 
199 #ifndef HAVE_RSA_METH_FREE
200 void RSA_meth_free(RSA_METHOD *meth);
201 #endif /* HAVE_RSA_METH_FREE */
202 
203 #ifndef HAVE_RSA_METH_DUP
204 RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth);
205 #endif /* HAVE_RSA_METH_DUP */
206 
207 #ifndef HAVE_RSA_METH_SET1_NAME
208 int RSA_meth_set1_name(RSA_METHOD *meth, const char *name);
209 #endif /* HAVE_RSA_METH_SET1_NAME */
210 
211 #ifndef HAVE_RSA_METH_GET_FINISH
212 int (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa);
213 #endif /* HAVE_RSA_METH_GET_FINISH */
214 
215 #ifndef HAVE_RSA_METH_SET_PRIV_ENC
216 int RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
217     const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
218 #endif /* HAVE_RSA_METH_SET_PRIV_ENC */
219 
220 #ifndef HAVE_RSA_METH_SET_PRIV_DEC
221 int RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
222     const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
223 #endif /* HAVE_RSA_METH_SET_PRIV_DEC */
224 
225 #ifndef HAVE_RSA_METH_SET_FINISH
226 int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa));
227 #endif /* HAVE_RSA_METH_SET_FINISH */
228 
229 #ifndef HAVE_EVP_PKEY_GET0_RSA
230 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
231 #endif /* HAVE_EVP_PKEY_GET0_RSA */
232 
233 #ifndef HAVE_EVP_MD_CTX_new
234 EVP_MD_CTX *EVP_MD_CTX_new(void);
235 #endif /* HAVE_EVP_MD_CTX_new */
236 
237 #ifndef HAVE_EVP_MD_CTX_free
238 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
239 #endif /* HAVE_EVP_MD_CTX_free */
240 
241 #endif /* WITH_OPENSSL */
242 #endif /* _OPENSSL_COMPAT_H */
243