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