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