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