You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1232 lines
35 KiB

  1. From a294365f8524e6cc3bb82bdcb459e95d65226fce Mon Sep 17 00:00:00 2001
  2. From: Damien Miller <djm@mindrot.org>
  3. Date: Thu, 13 Sep 2018 12:13:50 +1000
  4. Subject: [PATCH 2/5] adapt -portable to OpenSSL 1.1x API
  5. Polyfill missing API with replacement functions extracted from LibreSSL
  6. ---
  7. auth-pam.c | 4 +
  8. cipher.c | 38 ---
  9. configure.ac | 112 +++++-
  10. dh.c | 2 +
  11. kexdh.c | 2 +
  12. kexdhc.c | 2 +
  13. kexdhs.c | 2 +
  14. kexgex.c | 2 +
  15. kexgexc.c | 2 +
  16. kexgexs.c | 2 +
  17. monitor.c | 4 +-
  18. openbsd-compat/Makefile.in | 1 +
  19. openbsd-compat/libressl-api-compat.c | 636 +++++++++++++++++++++++++++++++++++
  20. openbsd-compat/openssl-compat.h | 136 ++++++++
  21. ssh-dss.c | 2 +
  22. ssh-ecdsa.c | 2 +
  23. ssh-pkcs11-client.c | 2 +
  24. ssh-pkcs11.c | 1 +
  25. ssh-rsa.c | 2 +
  26. sshkey.c | 3 +-
  27. 20 files changed, 916 insertions(+), 41 deletions(-)
  28. create mode 100644 openbsd-compat/libressl-api-compat.c
  29. diff --git a/auth-pam.c b/auth-pam.c
  30. index 8c013836..1dec53e9 100644
  31. --- a/auth-pam.c
  32. +++ b/auth-pam.c
  33. @@ -128,6 +128,10 @@ extern u_int utmp_len;
  34. typedef pthread_t sp_pthread_t;
  35. #else
  36. typedef pid_t sp_pthread_t;
  37. +#define pthread_exit fake_pthread_exit
  38. +#define pthread_create fake_pthread_create
  39. +#define pthread_cancel fake_pthread_cancel
  40. +#define pthread_join fake_pthread_join
  41. #endif
  42. struct pam_ctxt {
  43. diff --git a/cipher.c b/cipher.c
  44. index df43826e..12c59888 100644
  45. --- a/cipher.c
  46. +++ b/cipher.c
  47. @@ -525,41 +525,3 @@ cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len)
  48. #endif
  49. return 0;
  50. }
  51. -
  52. -#ifdef WITH_OPENSSL
  53. -#define EVP_X_STATE(evp) (evp)->cipher_data
  54. -#define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
  55. -#endif
  56. -
  57. -int
  58. -cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
  59. -{
  60. -#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
  61. - const struct sshcipher *c = cc->cipher;
  62. - int plen = 0;
  63. -
  64. - if (c->evptype == EVP_rc4) {
  65. - plen = EVP_X_STATE_LEN(cc->evp);
  66. - if (dat == NULL)
  67. - return (plen);
  68. - memcpy(dat, EVP_X_STATE(cc->evp), plen);
  69. - }
  70. - return (plen);
  71. -#else
  72. - return 0;
  73. -#endif
  74. -}
  75. -
  76. -void
  77. -cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
  78. -{
  79. -#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
  80. - const struct sshcipher *c = cc->cipher;
  81. - int plen;
  82. -
  83. - if (c->evptype == EVP_rc4) {
  84. - plen = EVP_X_STATE_LEN(cc->evp);
  85. - memcpy(EVP_X_STATE(cc->evp), dat, plen);
  86. - }
  87. -#endif
  88. -}
  89. diff --git a/configure.ac b/configure.ac
  90. index 83e53075..c0e120fe 100644
  91. --- a/configure.ac
  92. +++ b/configure.ac
  93. @@ -2602,9 +2602,10 @@ if test "x$openssl" = "xyes" ; then
  94. AC_MSG_ERROR([OpenSSL >= 1.0.1 required (have "$ssl_library_ver")])
  95. ;;
  96. 100*) ;; # 1.0.x
  97. + 101*) ;; # 1.1.x
  98. 200*) ;; # LibreSSL
  99. *)
  100. - AC_MSG_ERROR([OpenSSL >= 1.1.0 is not yet supported (have "$ssl_library_ver")])
  101. + AC_MSG_ERROR([OpenSSL > 1.1.x is not yet supported (have "$ssl_library_ver")])
  102. ;;
  103. esac
  104. AC_MSG_RESULT([$ssl_library_ver])
  105. @@ -2777,6 +2778,115 @@ if test "x$openssl" = "xyes" ; then
  106. [AC_DEFINE([HAVE_EVP_CIPHER_CTX_CTRL], [1],
  107. [Define if libcrypto has EVP_CIPHER_CTX_ctrl])])
  108. + # LibreSSL/OpenSSL 1.1x API
  109. + AC_SEARCH_LIBS([DH_get0_key], [crypto],
  110. + [AC_DEFINE([HAVE_DH_GET0_KEY], [1],
  111. + [Define if libcrypto has DH_get0_key])])
  112. + AC_SEARCH_LIBS([DH_get0_pqg], [crypto],
  113. + [AC_DEFINE([HAVE_DH_GET0_PQG], [1],
  114. + [Define if libcrypto has DH_get0_pqg])])
  115. + AC_SEARCH_LIBS([DH_set0_key], [crypto],
  116. + [AC_DEFINE([HAVE_DH_SET0_KEY], [1],
  117. + [Define if libcrypto has DH_set0_key])])
  118. + AC_SEARCH_LIBS([DH_set_length], [crypto],
  119. + [AC_DEFINE([HAVE_DH_SET_LENGTH], [1],
  120. + [Define if libcrypto has DH_set_length])])
  121. + AC_SEARCH_LIBS([DH_set0_pqg], [crypto],
  122. + [AC_DEFINE([HAVE_DH_SET0_PQG], [1],
  123. + [Define if libcrypto has DH_set0_pqg])])
  124. +
  125. + AC_SEARCH_LIBS([DSA_get0_key], [crypto],
  126. + [AC_DEFINE([HAVE_DSA_GET0_KEY], [1],
  127. + [Define if libcrypto has DSA_get0_key])])
  128. + AC_SEARCH_LIBS([DSA_get0_pqg], [crypto],
  129. + [AC_DEFINE([HAVE_DSA_GET0_PQG], [1],
  130. + [Define if libcrypto has DSA_get0_pqg])])
  131. + AC_SEARCH_LIBS([DSA_set0_key], [crypto],
  132. + [AC_DEFINE([HAVE_DSA_SET0_KEY], [1],
  133. + [Define if libcrypto has DSA_set0_key])])
  134. + AC_SEARCH_LIBS([DSA_set0_pqg], [crypto],
  135. + [AC_DEFINE([HAVE_DSA_SET0_PQG], [1],
  136. + [Define if libcrypto has DSA_set0_pqg])])
  137. +
  138. + AC_SEARCH_LIBS([DSA_SIG_get0], [crypto],
  139. + [AC_DEFINE([HAVE_DSA_SIG_GET0], [1],
  140. + [Define if libcrypto has DSA_SIG_get0])])
  141. + AC_SEARCH_LIBS([DSA_SIG_set0], [crypto],
  142. + [AC_DEFINE([HAVE_DSA_SIG_SET0], [1],
  143. + [Define if libcrypto has DSA_SIG_set0])])
  144. +
  145. + AC_SEARCH_LIBS([ECDSA_SIG_get0], [crypto],
  146. + [AC_DEFINE([HAVE_ECDSA_SIG_GET0], [1],
  147. + [Define if libcrypto has ECDSA_SIG_get0])])
  148. + AC_SEARCH_LIBS([ECDSA_SIG_set0], [crypto],
  149. + [AC_DEFINE([HAVE_ECDSA_SIG_SET0], [1],
  150. + [Define if libcrypto has ECDSA_SIG_set0])])
  151. +
  152. + AC_SEARCH_LIBS([EVP_CIPHER_CTX_iv], [crypto],
  153. + [AC_DEFINE([HAVE_EVP_CIPHER_CTX_IV], [1],
  154. + [Define if libcrypto has EVP_CIPHER_CTX_iv])])
  155. + AC_SEARCH_LIBS([EVP_CIPHER_CTX_iv_noconst], [crypto],
  156. + [AC_DEFINE([HAVE_EVP_CIPHER_CTX_IV_NOCONST], [1],
  157. + [Define if libcrypto has EVP_CIPHER_CTX_iv_noconst])])
  158. + AC_SEARCH_LIBS([EVP_CIPHER_CTX_get_iv], [crypto],
  159. + [AC_DEFINE([HAVE_EVP_CIPHER_CTX_GET_IV], [1],
  160. + [Define if libcrypto has EVP_CIPHER_CTX_get_iv])])
  161. + AC_SEARCH_LIBS([EVP_CIPHER_CTX_set_iv], [crypto],
  162. + [AC_DEFINE([HAVE_EVP_CIPHER_CTX_GET_IV], [1],
  163. + [Define if libcrypto has EVP_CIPHER_CTX_set_iv])])
  164. +
  165. + AC_SEARCH_LIBS([RSA_get0_crt_params], [crypto],
  166. + [AC_DEFINE([HAVE_RSA_GET0_CRT_PARAMS], [1],
  167. + [Define if libcrypto has RSA_get0_crt_params])])
  168. + AC_SEARCH_LIBS([RSA_get0_factors], [crypto],
  169. + [AC_DEFINE([HAVE_RSA_GET0_FACTORS], [1],
  170. + [Define if libcrypto has RSA_get0_factors])])
  171. + AC_SEARCH_LIBS([RSA_get0_key], [crypto],
  172. + [AC_DEFINE([HAVE_RSA_GET0_KEY], [1],
  173. + [Define if libcrypto has RSA_get0_key])])
  174. + AC_SEARCH_LIBS([RSA_set0_crt_params], [crypto],
  175. + [AC_DEFINE([HAVE_RSA_SET0_CRT_PARAMS], [1],
  176. + [Define if libcrypto has RSA_get0_srt_params])])
  177. + AC_SEARCH_LIBS([RSA_set0_factors], [crypto],
  178. + [AC_DEFINE([HAVE_RSA_SET0_FACTORS], [1],
  179. + [Define if libcrypto has RSA_set0_factors])])
  180. + AC_SEARCH_LIBS([RSA_set0_key], [crypto],
  181. + [AC_DEFINE([HAVE_RSA_SET0_KEY], [1],
  182. + [Define if libcrypto has RSA_set0_key])])
  183. +
  184. + AC_SEARCH_LIBS([RSA_meth_free], [crypto],
  185. + [AC_DEFINE([HAVE_RSA_METH_FREE], [1],
  186. + [Define if libcrypto has RSA_meth_free])])
  187. + AC_SEARCH_LIBS([RSA_meth_dup], [crypto],
  188. + [AC_DEFINE([HAVE_RSA_METH_DUP], [1],
  189. + [Define if libcrypto has RSA_meth_dup])])
  190. + AC_SEARCH_LIBS([RSA_meth_set1_name], [crypto],
  191. + [AC_DEFINE([HAVE_RSA_METH_SET1_NAME], [1],
  192. + [Define if libcrypto has RSA_meth_set1_name])])
  193. + AC_SEARCH_LIBS([RSA_meth_get_finish], [crypto],
  194. + [AC_DEFINE([HAVE_RSA_METH_GET_FINISH], [1],
  195. + [Define if libcrypto has RSA_meth_get_finish])])
  196. + AC_SEARCH_LIBS([RSA_meth_set_priv_enc], [crypto],
  197. + [AC_DEFINE([HAVE_RSA_METH_SET_PRIV_ENC], [1],
  198. + [Define if libcrypto has RSA_meth_set_priv_enc])])
  199. + AC_SEARCH_LIBS([RSA_meth_set_priv_dec], [crypto],
  200. + [AC_DEFINE([HAVE_RSA_METH_SET_PRIV_DEC], [1],
  201. + [Define if libcrypto has RSA_meth_set_priv_dec])])
  202. + AC_SEARCH_LIBS([RSA_meth_set_finish], [crypto],
  203. + [AC_DEFINE([HAVE_RSA_METH_SET_FINISH], [1],
  204. + [Define if libcrypto has RSA_meth_set_finish])])
  205. +
  206. + AC_SEARCH_LIBS([EVP_PKEY_get0_RSA], [crypto],
  207. + [AC_DEFINE([HAVE_EVP_PKEY_GET0_RSA], [1],
  208. + [Define if libcrypto has EVP_PKEY_get0_RSA])])
  209. +
  210. + AC_SEARCH_LIBS([EVP_MD_CTX_new], [crypto],
  211. + [AC_DEFINE([HAVE_EVP_MD_CTX_NEW], [1],
  212. + [Define if libcrypto has EVP_MD_CTX_new])])
  213. + AC_SEARCH_LIBS([EVP_MD_CTX_free], [crypto],
  214. + [AC_DEFINE([HAVE_EVP_MD_CTX_FREE], [1],
  215. + [Define if libcrypto has EVP_MD_CTX_free])])
  216. +
  217. AC_MSG_CHECKING([if EVP_DigestUpdate returns an int])
  218. AC_LINK_IFELSE(
  219. [AC_LANG_PROGRAM([[
  220. diff --git a/dh.c b/dh.c
  221. index d0d4527b..f3ed3882 100644
  222. --- a/dh.c
  223. +++ b/dh.c
  224. @@ -43,6 +43,8 @@
  225. #include "misc.h"
  226. #include "ssherr.h"
  227. +#include "openbsd-compat/openssl-compat.h"
  228. +
  229. static int
  230. parse_prime(int linenum, char *line, struct dhgroup *dhg)
  231. {
  232. diff --git a/kexdh.c b/kexdh.c
  233. index 0bf0dc13..e6925b18 100644
  234. --- a/kexdh.c
  235. +++ b/kexdh.c
  236. @@ -33,6 +33,8 @@
  237. #include <openssl/evp.h>
  238. +#include "openbsd-compat/openssl-compat.h"
  239. +
  240. #include "ssh2.h"
  241. #include "sshkey.h"
  242. #include "cipher.h"
  243. diff --git a/kexdhc.c b/kexdhc.c
  244. index a8b74247..8b56377a 100644
  245. --- a/kexdhc.c
  246. +++ b/kexdhc.c
  247. @@ -36,6 +36,8 @@
  248. #include <string.h>
  249. #include <signal.h>
  250. +#include "openbsd-compat/openssl-compat.h"
  251. +
  252. #include "sshkey.h"
  253. #include "cipher.h"
  254. #include "digest.h"
  255. diff --git a/kexdhs.c b/kexdhs.c
  256. index 8367c6c3..337aab5b 100644
  257. --- a/kexdhs.c
  258. +++ b/kexdhs.c
  259. @@ -35,6 +35,8 @@
  260. #include <openssl/dh.h>
  261. +#include "openbsd-compat/openssl-compat.h"
  262. +
  263. #include "sshkey.h"
  264. #include "cipher.h"
  265. #include "digest.h"
  266. diff --git a/kexgex.c b/kexgex.c
  267. index 8b0d8333..3ca4bd37 100644
  268. --- a/kexgex.c
  269. +++ b/kexgex.c
  270. @@ -33,6 +33,8 @@
  271. #include <openssl/evp.h>
  272. #include <signal.h>
  273. +#include "openbsd-compat/openssl-compat.h"
  274. +
  275. #include "sshkey.h"
  276. #include "cipher.h"
  277. #include "kex.h"
  278. diff --git a/kexgexc.c b/kexgexc.c
  279. index 955bc837..0d07f73c 100644
  280. --- a/kexgexc.c
  281. +++ b/kexgexc.c
  282. @@ -37,6 +37,8 @@
  283. #include <string.h>
  284. #include <signal.h>
  285. +#include "openbsd-compat/openssl-compat.h"
  286. +
  287. #include "sshkey.h"
  288. #include "cipher.h"
  289. #include "digest.h"
  290. diff --git a/kexgexs.c b/kexgexs.c
  291. index 2a4aa7e8..ce934f88 100644
  292. --- a/kexgexs.c
  293. +++ b/kexgexs.c
  294. @@ -36,6 +36,8 @@
  295. #include <openssl/dh.h>
  296. +#include "openbsd-compat/openssl-compat.h"
  297. +
  298. #include "sshkey.h"
  299. #include "cipher.h"
  300. #include "digest.h"
  301. diff --git a/monitor.c b/monitor.c
  302. index b30813b4..531b2993 100644
  303. --- a/monitor.c
  304. +++ b/monitor.c
  305. @@ -29,7 +29,6 @@
  306. #include <sys/types.h>
  307. #include <sys/socket.h>
  308. -#include "openbsd-compat/sys-tree.h"
  309. #include <sys/wait.h>
  310. #include <errno.h>
  311. @@ -60,7 +59,10 @@
  312. #include <openssl/dh.h>
  313. #endif
  314. +#include "openbsd-compat/sys-tree.h"
  315. #include "openbsd-compat/sys-queue.h"
  316. +#include "openbsd-compat/openssl-compat.h"
  317. +
  318. #include "atomicio.h"
  319. #include "xmalloc.h"
  320. #include "ssh.h"
  321. diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
  322. index 2fd9b952..c1e14cbd 100644
  323. --- a/openbsd-compat/Makefile.in
  324. +++ b/openbsd-compat/Makefile.in
  325. @@ -85,6 +85,7 @@ COMPAT= arc4random.o \
  326. getrrsetbyname-ldns.o \
  327. kludge-fd_set.o \
  328. openssl-compat.o \
  329. + libressl-api-compat.o \
  330. xcrypt.o
  331. PORTS= port-aix.o \
  332. diff --git a/openbsd-compat/libressl-api-compat.c b/openbsd-compat/libressl-api-compat.c
  333. new file mode 100644
  334. index 00000000..de3e64a6
  335. --- /dev/null
  336. +++ b/openbsd-compat/libressl-api-compat.c
  337. @@ -0,0 +1,636 @@
  338. +/* $OpenBSD: dsa_lib.c,v 1.29 2018/04/14 07:09:21 tb Exp $ */
  339. +/* $OpenBSD: rsa_lib.c,v 1.37 2018/04/14 07:09:21 tb Exp $ */
  340. +/* $OpenBSD: evp_lib.c,v 1.17 2018/09/12 06:35:38 djm Exp $ */
  341. +/* $OpenBSD: dh_lib.c,v 1.32 2018/05/02 15:48:38 tb Exp $ */
  342. +/* $OpenBSD: p_lib.c,v 1.24 2018/05/30 15:40:50 tb Exp $ */
  343. +/* $OpenBSD: digest.c,v 1.30 2018/04/14 07:09:21 tb Exp $ */
  344. +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  345. + * All rights reserved.
  346. + *
  347. + * This package is an SSL implementation written
  348. + * by Eric Young (eay@cryptsoft.com).
  349. + * The implementation was written so as to conform with Netscapes SSL.
  350. + *
  351. + * This library is free for commercial and non-commercial use as long as
  352. + * the following conditions are aheared to. The following conditions
  353. + * apply to all code found in this distribution, be it the RC4, RSA,
  354. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  355. + * included with this distribution is covered by the same copyright terms
  356. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  357. + *
  358. + * Copyright remains Eric Young's, and as such any Copyright notices in
  359. + * the code are not to be removed.
  360. + * If this package is used in a product, Eric Young should be given attribution
  361. + * as the author of the parts of the library used.
  362. + * This can be in the form of a textual message at program startup or
  363. + * in documentation (online or textual) provided with the package.
  364. + *
  365. + * Redistribution and use in source and binary forms, with or without
  366. + * modification, are permitted provided that the following conditions
  367. + * are met:
  368. + * 1. Redistributions of source code must retain the copyright
  369. + * notice, this list of conditions and the following disclaimer.
  370. + * 2. Redistributions in binary form must reproduce the above copyright
  371. + * notice, this list of conditions and the following disclaimer in the
  372. + * documentation and/or other materials provided with the distribution.
  373. + * 3. All advertising materials mentioning features or use of this software
  374. + * must display the following acknowledgement:
  375. + * "This product includes cryptographic software written by
  376. + * Eric Young (eay@cryptsoft.com)"
  377. + * The word 'cryptographic' can be left out if the rouines from the library
  378. + * being used are not cryptographic related :-).
  379. + * 4. If you include any Windows specific code (or a derivative thereof) from
  380. + * the apps directory (application code) you must include an acknowledgement:
  381. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  382. + *
  383. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  384. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  385. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  386. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  387. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  388. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  389. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  390. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  391. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  392. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  393. + * SUCH DAMAGE.
  394. + *
  395. + * The licence and distribution terms for any publically available version or
  396. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  397. + * copied and put under another distribution licence
  398. + * [including the GNU Public Licence.]
  399. + */
  400. +
  401. +/* $OpenBSD: dsa_asn1.c,v 1.22 2018/06/14 17:03:19 jsing Exp $ */
  402. +/* $OpenBSD: ecs_asn1.c,v 1.9 2018/03/17 15:24:44 tb Exp $ */
  403. +/* $OpenBSD: digest.c,v 1.30 2018/04/14 07:09:21 tb Exp $ */
  404. +/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  405. + * project 2000.
  406. + */
  407. +/* ====================================================================
  408. + * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
  409. + *
  410. + * Redistribution and use in source and binary forms, with or without
  411. + * modification, are permitted provided that the following conditions
  412. + * are met:
  413. + *
  414. + * 1. Redistributions of source code must retain the above copyright
  415. + * notice, this list of conditions and the following disclaimer.
  416. + *
  417. + * 2. Redistributions in binary form must reproduce the above copyright
  418. + * notice, this list of conditions and the following disclaimer in
  419. + * the documentation and/or other materials provided with the
  420. + * distribution.
  421. + *
  422. + * 3. All advertising materials mentioning features or use of this
  423. + * software must display the following acknowledgment:
  424. + * "This product includes software developed by the OpenSSL Project
  425. + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  426. + *
  427. + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  428. + * endorse or promote products derived from this software without
  429. + * prior written permission. For written permission, please contact
  430. + * licensing@OpenSSL.org.
  431. + *
  432. + * 5. Products derived from this software may not be called "OpenSSL"
  433. + * nor may "OpenSSL" appear in their names without prior written
  434. + * permission of the OpenSSL Project.
  435. + *
  436. + * 6. Redistributions of any form whatsoever must retain the following
  437. + * acknowledgment:
  438. + * "This product includes software developed by the OpenSSL Project
  439. + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  440. + *
  441. + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  442. + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  443. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  444. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  445. + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  446. + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  447. + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  448. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  449. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  450. + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  451. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  452. + * OF THE POSSIBILITY OF SUCH DAMAGE.
  453. + * ====================================================================
  454. + *
  455. + * This product includes cryptographic software written by Eric Young
  456. + * (eay@cryptsoft.com). This product includes software written by Tim
  457. + * Hudson (tjh@cryptsoft.com).
  458. + *
  459. + */
  460. +
  461. +/* $OpenBSD: rsa_meth.c,v 1.2 2018/09/12 06:35:38 djm Exp $ */
  462. +/*
  463. + * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
  464. + *
  465. + * Permission to use, copy, modify, and distribute this software for any
  466. + * purpose with or without fee is hereby granted, provided that the above
  467. + * copyright notice and this permission notice appear in all copies.
  468. + *
  469. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  470. + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  471. + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  472. + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  473. + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  474. + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  475. + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  476. + */
  477. +
  478. +#include "includes.h"
  479. +
  480. +#ifdef WITH_OPENSSL
  481. +
  482. +#include <sys/types.h>
  483. +
  484. +#include <stdlib.h>
  485. +#include <string.h>
  486. +
  487. +#include <openssl/err.h>
  488. +#include <openssl/bn.h>
  489. +#include <openssl/dsa.h>
  490. +#include <openssl/rsa.h>
  491. +#include <openssl/evp.h>
  492. +#include <openssl/ecdsa.h>
  493. +#include <openssl/dh.h>
  494. +
  495. +#ifndef HAVE_DSA_GET0_PQG
  496. +void
  497. +DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  498. +{
  499. + if (p != NULL)
  500. + *p = d->p;
  501. + if (q != NULL)
  502. + *q = d->q;
  503. + if (g != NULL)
  504. + *g = d->g;
  505. +}
  506. +#endif /* HAVE_DSA_GET0_PQG */
  507. +
  508. +#ifndef HAVE_DSA_SET0_PQG
  509. +int
  510. +DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  511. +{
  512. + if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) ||
  513. + (d->g == NULL && g == NULL))
  514. + return 0;
  515. +
  516. + if (p != NULL) {
  517. + BN_free(d->p);
  518. + d->p = p;
  519. + }
  520. + if (q != NULL) {
  521. + BN_free(d->q);
  522. + d->q = q;
  523. + }
  524. + if (g != NULL) {
  525. + BN_free(d->g);
  526. + d->g = g;
  527. + }
  528. +
  529. + return 1;
  530. +}
  531. +#endif /* HAVE_DSA_SET0_PQG */
  532. +
  533. +#ifndef HAVE_DSA_GET0_KEY
  534. +void
  535. +DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
  536. +{
  537. + if (pub_key != NULL)
  538. + *pub_key = d->pub_key;
  539. + if (priv_key != NULL)
  540. + *priv_key = d->priv_key;
  541. +}
  542. +#endif /* HAVE_DSA_GET0_KEY */
  543. +
  544. +#ifndef HAVE_DSA_SET0_KEY
  545. +int
  546. +DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
  547. +{
  548. + if (d->pub_key == NULL && pub_key == NULL)
  549. + return 0;
  550. +
  551. + if (pub_key != NULL) {
  552. + BN_free(d->pub_key);
  553. + d->pub_key = pub_key;
  554. + }
  555. + if (priv_key != NULL) {
  556. + BN_free(d->priv_key);
  557. + d->priv_key = priv_key;
  558. + }
  559. +
  560. + return 1;
  561. +}
  562. +#endif /* HAVE_DSA_SET0_KEY */
  563. +
  564. +#ifndef HAVE_RSA_GET0_KEY
  565. +void
  566. +RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  567. +{
  568. + if (n != NULL)
  569. + *n = r->n;
  570. + if (e != NULL)
  571. + *e = r->e;
  572. + if (d != NULL)
  573. + *d = r->d;
  574. +}
  575. +#endif /* HAVE_RSA_GET0_KEY */
  576. +
  577. +#ifndef HAVE_RSA_SET0_KEY
  578. +int
  579. +RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  580. +{
  581. + if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
  582. + return 0;
  583. +
  584. + if (n != NULL) {
  585. + BN_free(r->n);
  586. + r->n = n;
  587. + }
  588. + if (e != NULL) {
  589. + BN_free(r->e);
  590. + r->e = e;
  591. + }
  592. + if (d != NULL) {
  593. + BN_free(r->d);
  594. + r->d = d;
  595. + }
  596. +
  597. + return 1;
  598. +}
  599. +#endif /* HAVE_RSA_SET0_KEY */
  600. +
  601. +#ifndef HAVE_RSA_GET0_CRT_PARAMS
  602. +void
  603. +RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
  604. + const BIGNUM **iqmp)
  605. +{
  606. + if (dmp1 != NULL)
  607. + *dmp1 = r->dmp1;
  608. + if (dmq1 != NULL)
  609. + *dmq1 = r->dmq1;
  610. + if (iqmp != NULL)
  611. + *iqmp = r->iqmp;
  612. +}
  613. +#endif /* HAVE_RSA_GET0_CRT_PARAMS */
  614. +
  615. +#ifndef HAVE_RSA_SET0_CRT_PARAMS
  616. +int
  617. +RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  618. +{
  619. + if ((r->dmp1 == NULL && dmp1 == NULL) ||
  620. + (r->dmq1 == NULL && dmq1 == NULL) ||
  621. + (r->iqmp == NULL && iqmp == NULL))
  622. + return 0;
  623. +
  624. + if (dmp1 != NULL) {
  625. + BN_free(r->dmp1);
  626. + r->dmp1 = dmp1;
  627. + }
  628. + if (dmq1 != NULL) {
  629. + BN_free(r->dmq1);
  630. + r->dmq1 = dmq1;
  631. + }
  632. + if (iqmp != NULL) {
  633. + BN_free(r->iqmp);
  634. + r->iqmp = iqmp;
  635. + }
  636. +
  637. + return 1;
  638. +}
  639. +#endif /* HAVE_RSA_SET0_CRT_PARAMS */
  640. +
  641. +#ifndef HAVE_RSA_GET0_FACTORS
  642. +void
  643. +RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  644. +{
  645. + if (p != NULL)
  646. + *p = r->p;
  647. + if (q != NULL)
  648. + *q = r->q;
  649. +}
  650. +#endif /* HAVE_RSA_GET0_FACTORS */
  651. +
  652. +#ifndef HAVE_RSA_SET0_FACTORS
  653. +int
  654. +RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  655. +{
  656. + if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
  657. + return 0;
  658. +
  659. + if (p != NULL) {
  660. + BN_free(r->p);
  661. + r->p = p;
  662. + }
  663. + if (q != NULL) {
  664. + BN_free(r->q);
  665. + r->q = q;
  666. + }
  667. +
  668. + return 1;
  669. +}
  670. +#endif /* HAVE_RSA_SET0_FACTORS */
  671. +
  672. +#ifndef HAVE_EVP_CIPHER_CTX_GET_IV
  673. +int
  674. +EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, unsigned char *iv, size_t len)
  675. +{
  676. + if (ctx == NULL)
  677. + return 0;
  678. + if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
  679. + return 0;
  680. + if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
  681. + return 0;
  682. + if (len > EVP_MAX_IV_LENGTH)
  683. + return 0; /* sanity check; shouldn't happen */
  684. + /*
  685. + * Skip the memcpy entirely when the requested IV length is zero,
  686. + * since the iv pointer may be NULL or invalid.
  687. + */
  688. + if (len != 0) {
  689. + if (iv == NULL)
  690. + return 0;
  691. +# ifdef HAVE_EVP_CIPHER_CTX_IV
  692. + memcpy(iv, EVP_CIPHER_CTX_iv(ctx), len);
  693. +# else
  694. + memcpy(iv, ctx->iv, len);
  695. +# endif /* HAVE_EVP_CIPHER_CTX_IV */
  696. + }
  697. + return 1;
  698. +}
  699. +#endif /* HAVE_EVP_CIPHER_CTX_GET_IV */
  700. +
  701. +#ifndef HAVE_EVP_CIPHER_CTX_SET_IV
  702. +int
  703. +EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, const unsigned char *iv, size_t len)
  704. +{
  705. + if (ctx == NULL)
  706. + return 0;
  707. + if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
  708. + return 0;
  709. + if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
  710. + return 0;
  711. + if (len > EVP_MAX_IV_LENGTH)
  712. + return 0; /* sanity check; shouldn't happen */
  713. + /*
  714. + * Skip the memcpy entirely when the requested IV length is zero,
  715. + * since the iv pointer may be NULL or invalid.
  716. + */
  717. + if (len != 0) {
  718. + if (iv == NULL)
  719. + return 0;
  720. +# ifdef HAVE_EVP_CIPHER_CTX_IV_NOCONST
  721. + memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, len);
  722. +# else
  723. + memcpy(ctx->iv, iv, len);
  724. +# endif /* HAVE_EVP_CIPHER_CTX_IV_NOCONST */
  725. + }
  726. + return 1;
  727. +}
  728. +#endif /* HAVE_EVP_CIPHER_CTX_SET_IV */
  729. +
  730. +#ifndef HAVE_DSA_SIG_GET0
  731. +void
  732. +DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  733. +{
  734. + if (pr != NULL)
  735. + *pr = sig->r;
  736. + if (ps != NULL)
  737. + *ps = sig->s;
  738. +}
  739. +#endif /* HAVE_DSA_SIG_GET0 */
  740. +
  741. +#ifndef HAVE_DSA_SIG_SET0
  742. +int
  743. +DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  744. +{
  745. + if (r == NULL || s == NULL)
  746. + return 0;
  747. +
  748. + BN_clear_free(sig->r);
  749. + sig->r = r;
  750. + BN_clear_free(sig->s);
  751. + sig->s = s;
  752. +
  753. + return 1;
  754. +}
  755. +#endif /* HAVE_DSA_SIG_SET0 */
  756. +
  757. +#ifndef HAVE_ECDSA_SIG_GET0
  758. +void
  759. +ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  760. +{
  761. + if (pr != NULL)
  762. + *pr = sig->r;
  763. + if (ps != NULL)
  764. + *ps = sig->s;
  765. +}
  766. +#endif /* HAVE_ECDSA_SIG_GET0 */
  767. +
  768. +#ifndef HAVE_ECDSA_SIG_SET0
  769. +int
  770. +ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  771. +{
  772. + if (r == NULL || s == NULL)
  773. + return 0;
  774. +
  775. + BN_clear_free(sig->r);
  776. + BN_clear_free(sig->s);
  777. + sig->r = r;
  778. + sig->s = s;
  779. + return 1;
  780. +}
  781. +#endif /* HAVE_ECDSA_SIG_SET0 */
  782. +
  783. +#ifndef HAVE_DH_GET0_PQG
  784. +void
  785. +DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  786. +{
  787. + if (p != NULL)
  788. + *p = dh->p;
  789. + if (q != NULL)
  790. + *q = dh->q;
  791. + if (g != NULL)
  792. + *g = dh->g;
  793. +}
  794. +#endif /* HAVE_DH_GET0_PQG */
  795. +
  796. +#ifndef HAVE_DH_SET0_PQG
  797. +int
  798. +DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  799. +{
  800. + if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
  801. + return 0;
  802. +
  803. + if (p != NULL) {
  804. + BN_free(dh->p);
  805. + dh->p = p;
  806. + }
  807. + if (q != NULL) {
  808. + BN_free(dh->q);
  809. + dh->q = q;
  810. + }
  811. + if (g != NULL) {
  812. + BN_free(dh->g);
  813. + dh->g = g;
  814. + }
  815. +
  816. + return 1;
  817. +}
  818. +#endif /* HAVE_DH_SET0_PQG */
  819. +
  820. +#ifndef HAVE_DH_GET0_KEY
  821. +void
  822. +DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  823. +{
  824. + if (pub_key != NULL)
  825. + *pub_key = dh->pub_key;
  826. + if (priv_key != NULL)
  827. + *priv_key = dh->priv_key;
  828. +}
  829. +#endif /* HAVE_DH_GET0_KEY */
  830. +
  831. +#ifndef HAVE_DH_SET0_KEY
  832. +int
  833. +DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
  834. +{
  835. + if (pub_key != NULL) {
  836. + BN_free(dh->pub_key);
  837. + dh->pub_key = pub_key;
  838. + }
  839. + if (priv_key != NULL) {
  840. + BN_free(dh->priv_key);
  841. + dh->priv_key = priv_key;
  842. + }
  843. +
  844. + return 1;
  845. +}
  846. +#endif /* HAVE_DH_SET0_KEY */
  847. +
  848. +#ifndef HAVE_DH_SET_LENGTH
  849. +int
  850. +DH_set_length(DH *dh, long length)
  851. +{
  852. + if (length < 0 || length > INT_MAX)
  853. + return 0;
  854. +
  855. + dh->length = length;
  856. + return 1;
  857. +}
  858. +#endif /* HAVE_DH_SET_LENGTH */
  859. +
  860. +#ifndef HAVE_RSA_METH_FREE
  861. +void
  862. +RSA_meth_free(RSA_METHOD *meth)
  863. +{
  864. + if (meth != NULL) {
  865. + free((char *)meth->name);
  866. + free(meth);
  867. + }
  868. +}
  869. +#endif /* HAVE_RSA_METH_FREE */
  870. +
  871. +#ifndef HAVE_RSA_METH_DUP
  872. +RSA_METHOD *
  873. +RSA_meth_dup(const RSA_METHOD *meth)
  874. +{
  875. + RSA_METHOD *copy;
  876. +
  877. + if ((copy = calloc(1, sizeof(*copy))) == NULL)
  878. + return NULL;
  879. + memcpy(copy, meth, sizeof(*copy));
  880. + if ((copy->name = strdup(meth->name)) == NULL) {
  881. + free(copy);
  882. + return NULL;
  883. + }
  884. +
  885. + return copy;
  886. +}
  887. +#endif /* HAVE_RSA_METH_DUP */
  888. +
  889. +#ifndef HAVE_RSA_METH_SET1_NAME
  890. +int
  891. +RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
  892. +{
  893. + char *copy;
  894. +
  895. + if ((copy = strdup(name)) == NULL)
  896. + return 0;
  897. + free((char *)meth->name);
  898. + meth->name = copy;
  899. + return 1;
  900. +}
  901. +#endif /* HAVE_RSA_METH_SET1_NAME */
  902. +
  903. +#ifndef HAVE_RSA_METH_GET_FINISH
  904. +int
  905. +(*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa)
  906. +{
  907. + return meth->finish;
  908. +}
  909. +#endif /* HAVE_RSA_METH_GET_FINISH */
  910. +
  911. +#ifndef HAVE_RSA_METH_SET_PRIV_ENC
  912. +int
  913. +RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
  914. + const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
  915. +{
  916. + meth->rsa_priv_enc = priv_enc;
  917. + return 1;
  918. +}
  919. +#endif /* HAVE_RSA_METH_SET_PRIV_ENC */
  920. +
  921. +#ifndef HAVE_RSA_METH_SET_PRIV_DEC
  922. +int
  923. +RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
  924. + const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
  925. +{
  926. + meth->rsa_priv_dec = priv_dec;
  927. + return 1;
  928. +}
  929. +#endif /* HAVE_RSA_METH_SET_PRIV_DEC */
  930. +
  931. +#ifndef HAVE_RSA_METH_SET_FINISH
  932. +int
  933. +RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa))
  934. +{
  935. + meth->finish = finish;
  936. + return 1;
  937. +}
  938. +#endif /* HAVE_RSA_METH_SET_FINISH */
  939. +
  940. +#ifndef HAVE_EVP_PKEY_GET0_RSA
  941. +RSA *
  942. +EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  943. +{
  944. + if (pkey->type != EVP_PKEY_RSA) {
  945. + /* EVPerror(EVP_R_EXPECTING_AN_RSA_KEY); */
  946. + return NULL;
  947. + }
  948. + return pkey->pkey.rsa;
  949. +}
  950. +#endif /* HAVE_EVP_PKEY_GET0_RSA */
  951. +
  952. +#ifndef HAVE_EVP_MD_CTX_NEW
  953. +EVP_MD_CTX *
  954. +EVP_MD_CTX_new(void)
  955. +{
  956. + return calloc(1, sizeof(EVP_MD_CTX));
  957. +}
  958. +#endif /* HAVE_EVP_MD_CTX_NEW */
  959. +
  960. +#ifndef HAVE_EVP_MD_CTX_FREE
  961. +void
  962. +EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  963. +{
  964. + if (ctx == NULL)
  965. + return;
  966. +
  967. + EVP_MD_CTX_cleanup(ctx);
  968. +
  969. + free(ctx);
  970. +}
  971. +#endif /* HAVE_EVP_MD_CTX_FREE */
  972. +
  973. +#endif /* WITH_OPENSSL */
  974. diff --git a/openbsd-compat/openssl-compat.h b/openbsd-compat/openssl-compat.h
  975. index 2ae42bac..9e0264c0 100644
  976. --- a/openbsd-compat/openssl-compat.h
  977. +++ b/openbsd-compat/openssl-compat.h
  978. @@ -24,6 +24,8 @@
  979. #include <openssl/evp.h>
  980. #include <openssl/rsa.h>
  981. #include <openssl/dsa.h>
  982. +#include <openssl/ecdsa.h>
  983. +#include <openssl/dh.h>
  984. int ssh_compatible_openssl(long, long);
  985. @@ -96,5 +98,139 @@ void ssh_OpenSSL_add_all_algorithms(void);
  986. #endif /* SSH_DONT_OVERLOAD_OPENSSL_FUNCS */
  987. +/* LibreSSL/OpenSSL 1.1x API compat */
  988. +#ifndef HAVE_DSA_GET0_PQG
  989. +void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q,
  990. + const BIGNUM **g);
  991. +#endif /* HAVE_DSA_GET0_PQG */
  992. +
  993. +#ifndef HAVE_DSA_SET0_PQG
  994. +int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  995. +#endif /* HAVE_DSA_SET0_PQG */
  996. +
  997. +#ifndef HAVE_DSA_GET0_KEY
  998. +void DSA_get0_key(const DSA *d, const BIGNUM **pub_key,
  999. + const BIGNUM **priv_key);
  1000. +#endif /* HAVE_DSA_GET0_KEY */
  1001. +
  1002. +#ifndef HAVE_DSA_SET0_KEY
  1003. +int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key);
  1004. +#endif /* HAVE_DSA_SET0_KEY */
  1005. +
  1006. +#ifndef HAVE_EVP_CIPHER_CTX_GET_IV
  1007. +int EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx,
  1008. + unsigned char *iv, size_t len);
  1009. +#endif /* HAVE_EVP_CIPHER_CTX_GET_IV */
  1010. +
  1011. +#ifndef HAVE_EVP_CIPHER_CTX_SET_IV
  1012. +int EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx,
  1013. + const unsigned char *iv, size_t len);
  1014. +#endif /* HAVE_EVP_CIPHER_CTX_SET_IV */
  1015. +
  1016. +#ifndef HAVE_RSA_GET0_KEY
  1017. +void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e,
  1018. + const BIGNUM **d);
  1019. +#endif /* HAVE_RSA_GET0_KEY */
  1020. +
  1021. +#ifndef HAVE_RSA_SET0_KEY
  1022. +int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
  1023. +#endif /* HAVE_RSA_SET0_KEY */
  1024. +
  1025. +#ifndef HAVE_RSA_GET0_CRT_PARAMS
  1026. +void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
  1027. + const BIGNUM **iqmp);
  1028. +#endif /* HAVE_RSA_GET0_CRT_PARAMS */
  1029. +
  1030. +#ifndef HAVE_RSA_SET0_CRT_PARAMS
  1031. +int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
  1032. +#endif /* HAVE_RSA_SET0_CRT_PARAMS */
  1033. +
  1034. +#ifndef HAVE_RSA_GET0_FACTORS
  1035. +void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
  1036. +#endif /* HAVE_RSA_GET0_FACTORS */
  1037. +
  1038. +#ifndef HAVE_RSA_SET0_FACTORS
  1039. +int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
  1040. +#endif /* HAVE_RSA_SET0_FACTORS */
  1041. +
  1042. +#ifndef DSA_SIG_GET0
  1043. +void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
  1044. +#endif /* DSA_SIG_GET0 */
  1045. +
  1046. +#ifndef DSA_SIG_SET0
  1047. +int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  1048. +#endif /* DSA_SIG_SET0 */
  1049. +
  1050. +#ifndef HAVE_ECDSA_SIG_GET0
  1051. +void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
  1052. +#endif /* HAVE_ECDSA_SIG_GET0 */
  1053. +
  1054. +#ifndef HAVE_ECDSA_SIG_SET0
  1055. +int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  1056. +#endif /* HAVE_ECDSA_SIG_SET0 */
  1057. +
  1058. +#ifndef HAVE_DH_GET0_PQG
  1059. +void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q,
  1060. + const BIGNUM **g);
  1061. +#endif /* HAVE_DH_GET0_PQG */
  1062. +
  1063. +#ifndef HAVE_DH_SET0_PQG
  1064. +int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  1065. +#endif /* HAVE_DH_SET0_PQG */
  1066. +
  1067. +#ifndef HAVE_DH_GET0_KEY
  1068. +void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
  1069. +#endif /* HAVE_DH_GET0_KEY */
  1070. +
  1071. +#ifndef HAVE_DH_SET0_KEY
  1072. +int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
  1073. +#endif /* HAVE_DH_SET0_KEY */
  1074. +
  1075. +#ifndef HAVE_DH_SET_LENGTH
  1076. +int DH_set_length(DH *dh, long length);
  1077. +#endif /* HAVE_DH_SET_LENGTH */
  1078. +
  1079. +#ifndef HAVE_RSA_METH_FREE
  1080. +void RSA_meth_free(RSA_METHOD *meth);
  1081. +#endif /* HAVE_RSA_METH_FREE */
  1082. +
  1083. +#ifndef HAVE_RSA_METH_DUP
  1084. +RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth);
  1085. +#endif /* HAVE_RSA_METH_DUP */
  1086. +
  1087. +#ifndef HAVE_RSA_METH_SET1_NAME
  1088. +int RSA_meth_set1_name(RSA_METHOD *meth, const char *name);
  1089. +#endif /* HAVE_RSA_METH_SET1_NAME */
  1090. +
  1091. +#ifndef HAVE_RSA_METH_GET_FINISH
  1092. +int (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa);
  1093. +#endif /* HAVE_RSA_METH_GET_FINISH */
  1094. +
  1095. +#ifndef HAVE_RSA_METH_SET_PRIV_ENC
  1096. +int RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
  1097. + const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
  1098. +#endif /* HAVE_RSA_METH_SET_PRIV_ENC */
  1099. +
  1100. +#ifndef HAVE_RSA_METH_SET_PRIV_DEC
  1101. +int RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
  1102. + const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
  1103. +#endif /* HAVE_RSA_METH_SET_PRIV_DEC */
  1104. +
  1105. +#ifndef HAVE_RSA_METH_SET_FINISH
  1106. +int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa));
  1107. +#endif /* HAVE_RSA_METH_SET_FINISH */
  1108. +
  1109. +#ifndef HAVE_EVP_PKEY_GET0_RSA
  1110. +RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
  1111. +#endif /* HAVE_EVP_PKEY_GET0_RSA */
  1112. +
  1113. +#ifndef HAVE_EVP_MD_CTX_new
  1114. +EVP_MD_CTX *EVP_MD_CTX_new(void);
  1115. +#endif /* HAVE_EVP_MD_CTX_new */
  1116. +
  1117. +#ifndef HAVE_EVP_MD_CTX_free
  1118. +void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
  1119. +#endif /* HAVE_EVP_MD_CTX_free */
  1120. +
  1121. #endif /* WITH_OPENSSL */
  1122. #endif /* _OPENSSL_COMPAT_H */
  1123. diff --git a/ssh-dss.c b/ssh-dss.c
  1124. index 631b1571..a23c383d 100644
  1125. --- a/ssh-dss.c
  1126. +++ b/ssh-dss.c
  1127. @@ -43,6 +43,8 @@
  1128. #define SSHKEY_INTERNAL
  1129. #include "sshkey.h"
  1130. +#include "openbsd-compat/openssl-compat.h"
  1131. +
  1132. #define INTBLOB_LEN 20
  1133. #define SIGBLOB_LEN (2*INTBLOB_LEN)
  1134. diff --git a/ssh-ecdsa.c b/ssh-ecdsa.c
  1135. index 9e92af04..2f553175 100644
  1136. --- a/ssh-ecdsa.c
  1137. +++ b/ssh-ecdsa.c
  1138. @@ -43,6 +43,8 @@
  1139. #define SSHKEY_INTERNAL
  1140. #include "sshkey.h"
  1141. +#include "openbsd-compat/openssl-compat.h"
  1142. +
  1143. /* ARGSUSED */
  1144. int
  1145. ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
  1146. diff --git a/ssh-pkcs11-client.c b/ssh-pkcs11-client.c
  1147. index bcc18c6b..d1241ce6 100644
  1148. --- a/ssh-pkcs11-client.c
  1149. +++ b/ssh-pkcs11-client.c
  1150. @@ -32,6 +32,8 @@
  1151. #include <openssl/rsa.h>
  1152. +#include "openbsd-compat/openssl-compat.h"
  1153. +
  1154. #include "pathnames.h"
  1155. #include "xmalloc.h"
  1156. #include "sshbuf.h"
  1157. diff --git a/ssh-pkcs11.c b/ssh-pkcs11.c
  1158. index c35f9415..775de964 100644
  1159. --- a/ssh-pkcs11.c
  1160. +++ b/ssh-pkcs11.c
  1161. @@ -30,6 +30,7 @@
  1162. #include <dlfcn.h>
  1163. #include "openbsd-compat/sys-queue.h"
  1164. +#include "openbsd-compat/openssl-compat.h"
  1165. #include <openssl/x509.h>
  1166. diff --git a/ssh-rsa.c b/ssh-rsa.c
  1167. index 2788f334..9b14f9a9 100644
  1168. --- a/ssh-rsa.c
  1169. +++ b/ssh-rsa.c
  1170. @@ -35,6 +35,8 @@
  1171. #include "digest.h"
  1172. #include "log.h"
  1173. +#include "openbsd-compat/openssl-compat.h"
  1174. +
  1175. static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
  1176. static const char *
  1177. diff --git a/sshkey.c b/sshkey.c
  1178. index a5e6e60e..18b253d9 100644
  1179. --- a/sshkey.c
  1180. +++ b/sshkey.c
  1181. @@ -60,6 +60,8 @@
  1182. #include "xmss_fast.h"
  1183. +#include "openbsd-compat/openssl-compat.h"
  1184. +
  1185. /* openssh private key file format */
  1186. #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
  1187. #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n"
  1188. @@ -1727,7 +1729,6 @@ int
  1189. sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
  1190. {
  1191. struct sshkey *n = NULL;
  1192. - int ret = SSH_ERR_INTERNAL_ERROR;
  1193. int r = SSH_ERR_INTERNAL_ERROR;
  1194. #ifdef WITH_OPENSSL
  1195. const BIGNUM *rsa_n, *rsa_e;
  1196. --
  1197. 2.16.4