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 #ifdef LIBRESSL_VERSION_NUMBER
66 # if LIBRESSL_VERSION_NUMBER < 0x3010000fL
67 #  define HAVE_BROKEN_CHACHA20
68 # endif
69 #endif
70 
71 #ifndef OPENSSL_HAVE_EVPCTR
72 # define EVP_aes_128_ctr evp_aes_128_ctr
73 # define EVP_aes_192_ctr evp_aes_128_ctr
74 # define EVP_aes_256_ctr evp_aes_128_ctr
75 const EVP_CIPHER *evp_aes_128_ctr(void);
76 void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, size_t);
77 #endif
78 
79 /* Avoid some #ifdef. Code that uses these is unreachable without GCM */
80 #if !defined(OPENSSL_HAVE_EVPGCM) && !defined(EVP_CTRL_GCM_SET_IV_FIXED)
81 # define EVP_CTRL_GCM_SET_IV_FIXED -1
82 # define EVP_CTRL_GCM_IV_GEN -1
83 # define EVP_CTRL_GCM_SET_TAG -1
84 # define EVP_CTRL_GCM_GET_TAG -1
85 #endif
86 
87 /* Replace missing EVP_CIPHER_CTX_ctrl() with something that returns failure */
88 #ifndef HAVE_EVP_CIPHER_CTX_CTRL
89 # ifdef OPENSSL_HAVE_EVPGCM
90 #  error AES-GCM enabled without EVP_CIPHER_CTX_ctrl /* shouldn't happen */
91 # else
92 # define EVP_CIPHER_CTX_ctrl(a,b,c,d) (0)
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 # ifdef HAVE_EVP_CIPHER_CTX_GET_UPDATED_IV
117 #  define EVP_CIPHER_CTX_get_iv EVP_CIPHER_CTX_get_updated_iv
118 # else /* HAVE_EVP_CIPHER_CTX_GET_UPDATED_IV */
119 int EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx,
120     unsigned char *iv, size_t len);
121 # endif /* HAVE_EVP_CIPHER_CTX_GET_UPDATED_IV */
122 #endif /* HAVE_EVP_CIPHER_CTX_GET_IV */
123 
124 #ifndef HAVE_EVP_CIPHER_CTX_SET_IV
125 int EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx,
126     const unsigned char *iv, size_t len);
127 #endif /* HAVE_EVP_CIPHER_CTX_SET_IV */
128 
129 #ifndef HAVE_RSA_GET0_KEY
130 void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e,
131     const BIGNUM **d);
132 #endif /* HAVE_RSA_GET0_KEY */
133 
134 #ifndef HAVE_RSA_SET0_KEY
135 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
136 #endif /* HAVE_RSA_SET0_KEY */
137 
138 #ifndef HAVE_RSA_GET0_CRT_PARAMS
139 void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
140     const BIGNUM **iqmp);
141 #endif /* HAVE_RSA_GET0_CRT_PARAMS */
142 
143 #ifndef HAVE_RSA_SET0_CRT_PARAMS
144 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
145 #endif /* HAVE_RSA_SET0_CRT_PARAMS */
146 
147 #ifndef HAVE_RSA_GET0_FACTORS
148 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
149 #endif /* HAVE_RSA_GET0_FACTORS */
150 
151 #ifndef HAVE_RSA_SET0_FACTORS
152 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
153 #endif /* HAVE_RSA_SET0_FACTORS */
154 
155 #ifndef DSA_SIG_GET0
156 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
157 #endif /* DSA_SIG_GET0 */
158 
159 #ifndef DSA_SIG_SET0
160 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
161 #endif /* DSA_SIG_SET0 */
162 
163 #ifdef OPENSSL_HAS_ECC
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 #endif /* OPENSSL_HAS_ECC */
172 
173 #ifndef HAVE_DH_GET0_PQG
174 void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q,
175     const BIGNUM **g);
176 #endif /* HAVE_DH_GET0_PQG */
177 
178 #ifndef HAVE_DH_SET0_PQG
179 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
180 #endif /* HAVE_DH_SET0_PQG */
181 
182 #ifndef HAVE_DH_GET0_KEY
183 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
184 #endif /* HAVE_DH_GET0_KEY */
185 
186 #ifndef HAVE_DH_SET0_KEY
187 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
188 #endif /* HAVE_DH_SET0_KEY */
189 
190 #ifndef HAVE_DH_SET_LENGTH
191 int DH_set_length(DH *dh, long length);
192 #endif /* HAVE_DH_SET_LENGTH */
193 
194 #ifndef HAVE_RSA_METH_FREE
195 void RSA_meth_free(RSA_METHOD *meth);
196 #endif /* HAVE_RSA_METH_FREE */
197 
198 #ifndef HAVE_RSA_METH_DUP
199 RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth);
200 #endif /* HAVE_RSA_METH_DUP */
201 
202 #ifndef HAVE_RSA_METH_SET1_NAME
203 int RSA_meth_set1_name(RSA_METHOD *meth, const char *name);
204 #endif /* HAVE_RSA_METH_SET1_NAME */
205 
206 #ifndef HAVE_RSA_METH_GET_FINISH
207 int (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa);
208 #endif /* HAVE_RSA_METH_GET_FINISH */
209 
210 #ifndef HAVE_RSA_METH_SET_PRIV_ENC
211 int RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
212     const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
213 #endif /* HAVE_RSA_METH_SET_PRIV_ENC */
214 
215 #ifndef HAVE_RSA_METH_SET_PRIV_DEC
216 int RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
217     const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
218 #endif /* HAVE_RSA_METH_SET_PRIV_DEC */
219 
220 #ifndef HAVE_RSA_METH_SET_FINISH
221 int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa));
222 #endif /* HAVE_RSA_METH_SET_FINISH */
223 
224 #ifndef HAVE_EVP_PKEY_GET0_RSA
225 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
226 #endif /* HAVE_EVP_PKEY_GET0_RSA */
227 
228 #ifndef HAVE_EVP_MD_CTX_new
229 EVP_MD_CTX *EVP_MD_CTX_new(void);
230 #endif /* HAVE_EVP_MD_CTX_new */
231 
232 #ifndef HAVE_EVP_MD_CTX_free
233 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
234 #endif /* HAVE_EVP_MD_CTX_free */
235 
236 #endif /* WITH_OPENSSL */
237 #endif /* _OPENSSL_COMPAT_H */
238