libressl-api-compat.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /* $OpenBSD: dsa_lib.c,v 1.29 2018/04/14 07:09:21 tb Exp $ */
  2. /* $OpenBSD: rsa_lib.c,v 1.37 2018/04/14 07:09:21 tb Exp $ */
  3. /* $OpenBSD: evp_lib.c,v 1.17 2018/09/12 06:35:38 djm Exp $ */
  4. /* $OpenBSD: dh_lib.c,v 1.32 2018/05/02 15:48:38 tb Exp $ */
  5. /* $OpenBSD: p_lib.c,v 1.24 2018/05/30 15:40:50 tb Exp $ */
  6. /* $OpenBSD: digest.c,v 1.30 2018/04/14 07:09:21 tb Exp $ */
  7. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  8. * All rights reserved.
  9. *
  10. * This package is an SSL implementation written
  11. * by Eric Young (eay@cryptsoft.com).
  12. * The implementation was written so as to conform with Netscapes SSL.
  13. *
  14. * This library is free for commercial and non-commercial use as long as
  15. * the following conditions are aheared to. The following conditions
  16. * apply to all code found in this distribution, be it the RC4, RSA,
  17. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  18. * included with this distribution is covered by the same copyright terms
  19. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  20. *
  21. * Copyright remains Eric Young's, and as such any Copyright notices in
  22. * the code are not to be removed.
  23. * If this package is used in a product, Eric Young should be given attribution
  24. * as the author of the parts of the library used.
  25. * This can be in the form of a textual message at program startup or
  26. * in documentation (online or textual) provided with the package.
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. * 1. Redistributions of source code must retain the copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. * 3. All advertising materials mentioning features or use of this software
  37. * must display the following acknowledgement:
  38. * "This product includes cryptographic software written by
  39. * Eric Young (eay@cryptsoft.com)"
  40. * The word 'cryptographic' can be left out if the rouines from the library
  41. * being used are not cryptographic related :-).
  42. * 4. If you include any Windows specific code (or a derivative thereof) from
  43. * the apps directory (application code) you must include an acknowledgement:
  44. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  45. *
  46. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  47. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  49. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  50. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  52. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  54. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  56. * SUCH DAMAGE.
  57. *
  58. * The licence and distribution terms for any publically available version or
  59. * derivative of this code cannot be changed. i.e. this code cannot simply be
  60. * copied and put under another distribution licence
  61. * [including the GNU Public Licence.]
  62. */
  63. /* $OpenBSD: dsa_asn1.c,v 1.22 2018/06/14 17:03:19 jsing Exp $ */
  64. /* $OpenBSD: ecs_asn1.c,v 1.9 2018/03/17 15:24:44 tb Exp $ */
  65. /* $OpenBSD: digest.c,v 1.30 2018/04/14 07:09:21 tb Exp $ */
  66. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  67. * project 2000.
  68. */
  69. /* ====================================================================
  70. * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
  71. *
  72. * Redistribution and use in source and binary forms, with or without
  73. * modification, are permitted provided that the following conditions
  74. * are met:
  75. *
  76. * 1. Redistributions of source code must retain the above copyright
  77. * notice, this list of conditions and the following disclaimer.
  78. *
  79. * 2. Redistributions in binary form must reproduce the above copyright
  80. * notice, this list of conditions and the following disclaimer in
  81. * the documentation and/or other materials provided with the
  82. * distribution.
  83. *
  84. * 3. All advertising materials mentioning features or use of this
  85. * software must display the following acknowledgment:
  86. * "This product includes software developed by the OpenSSL Project
  87. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  88. *
  89. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  90. * endorse or promote products derived from this software without
  91. * prior written permission. For written permission, please contact
  92. * licensing@OpenSSL.org.
  93. *
  94. * 5. Products derived from this software may not be called "OpenSSL"
  95. * nor may "OpenSSL" appear in their names without prior written
  96. * permission of the OpenSSL Project.
  97. *
  98. * 6. Redistributions of any form whatsoever must retain the following
  99. * acknowledgment:
  100. * "This product includes software developed by the OpenSSL Project
  101. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  102. *
  103. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  104. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  105. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  106. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  107. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  108. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  109. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  110. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  111. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  112. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  114. * OF THE POSSIBILITY OF SUCH DAMAGE.
  115. * ====================================================================
  116. *
  117. * This product includes cryptographic software written by Eric Young
  118. * (eay@cryptsoft.com). This product includes software written by Tim
  119. * Hudson (tjh@cryptsoft.com).
  120. *
  121. */
  122. /* $OpenBSD: rsa_meth.c,v 1.2 2018/09/12 06:35:38 djm Exp $ */
  123. /*
  124. * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
  125. *
  126. * Permission to use, copy, modify, and distribute this software for any
  127. * purpose with or without fee is hereby granted, provided that the above
  128. * copyright notice and this permission notice appear in all copies.
  129. *
  130. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  131. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  132. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  133. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  134. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  135. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  136. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  137. */
  138. #include "includes.h"
  139. #ifdef WITH_OPENSSL
  140. #include <sys/types.h>
  141. #include <stdlib.h>
  142. #include <string.h>
  143. #include <openssl/err.h>
  144. #include <openssl/bn.h>
  145. #include <openssl/dsa.h>
  146. #include <openssl/rsa.h>
  147. #include <openssl/evp.h>
  148. #ifdef OPENSSL_HAS_ECC
  149. #include <openssl/ecdsa.h>
  150. #endif
  151. #include <openssl/dh.h>
  152. #ifndef HAVE_DSA_GET0_PQG
  153. void
  154. DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  155. {
  156. if (p != NULL)
  157. *p = d->p;
  158. if (q != NULL)
  159. *q = d->q;
  160. if (g != NULL)
  161. *g = d->g;
  162. }
  163. #endif /* HAVE_DSA_GET0_PQG */
  164. #ifndef HAVE_DSA_SET0_PQG
  165. int
  166. DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  167. {
  168. if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) ||
  169. (d->g == NULL && g == NULL))
  170. return 0;
  171. if (p != NULL) {
  172. BN_free(d->p);
  173. d->p = p;
  174. }
  175. if (q != NULL) {
  176. BN_free(d->q);
  177. d->q = q;
  178. }
  179. if (g != NULL) {
  180. BN_free(d->g);
  181. d->g = g;
  182. }
  183. return 1;
  184. }
  185. #endif /* HAVE_DSA_SET0_PQG */
  186. #ifndef HAVE_DSA_GET0_KEY
  187. void
  188. DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
  189. {
  190. if (pub_key != NULL)
  191. *pub_key = d->pub_key;
  192. if (priv_key != NULL)
  193. *priv_key = d->priv_key;
  194. }
  195. #endif /* HAVE_DSA_GET0_KEY */
  196. #ifndef HAVE_DSA_SET0_KEY
  197. int
  198. DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
  199. {
  200. if (d->pub_key == NULL && pub_key == NULL)
  201. return 0;
  202. if (pub_key != NULL) {
  203. BN_free(d->pub_key);
  204. d->pub_key = pub_key;
  205. }
  206. if (priv_key != NULL) {
  207. BN_free(d->priv_key);
  208. d->priv_key = priv_key;
  209. }
  210. return 1;
  211. }
  212. #endif /* HAVE_DSA_SET0_KEY */
  213. #ifndef HAVE_RSA_GET0_KEY
  214. void
  215. RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  216. {
  217. if (n != NULL)
  218. *n = r->n;
  219. if (e != NULL)
  220. *e = r->e;
  221. if (d != NULL)
  222. *d = r->d;
  223. }
  224. #endif /* HAVE_RSA_GET0_KEY */
  225. #ifndef HAVE_RSA_SET0_KEY
  226. int
  227. RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  228. {
  229. if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
  230. return 0;
  231. if (n != NULL) {
  232. BN_free(r->n);
  233. r->n = n;
  234. }
  235. if (e != NULL) {
  236. BN_free(r->e);
  237. r->e = e;
  238. }
  239. if (d != NULL) {
  240. BN_free(r->d);
  241. r->d = d;
  242. }
  243. return 1;
  244. }
  245. #endif /* HAVE_RSA_SET0_KEY */
  246. #ifndef HAVE_RSA_GET0_CRT_PARAMS
  247. void
  248. RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
  249. const BIGNUM **iqmp)
  250. {
  251. if (dmp1 != NULL)
  252. *dmp1 = r->dmp1;
  253. if (dmq1 != NULL)
  254. *dmq1 = r->dmq1;
  255. if (iqmp != NULL)
  256. *iqmp = r->iqmp;
  257. }
  258. #endif /* HAVE_RSA_GET0_CRT_PARAMS */
  259. #ifndef HAVE_RSA_SET0_CRT_PARAMS
  260. int
  261. RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  262. {
  263. if ((r->dmp1 == NULL && dmp1 == NULL) ||
  264. (r->dmq1 == NULL && dmq1 == NULL) ||
  265. (r->iqmp == NULL && iqmp == NULL))
  266. return 0;
  267. if (dmp1 != NULL) {
  268. BN_free(r->dmp1);
  269. r->dmp1 = dmp1;
  270. }
  271. if (dmq1 != NULL) {
  272. BN_free(r->dmq1);
  273. r->dmq1 = dmq1;
  274. }
  275. if (iqmp != NULL) {
  276. BN_free(r->iqmp);
  277. r->iqmp = iqmp;
  278. }
  279. return 1;
  280. }
  281. #endif /* HAVE_RSA_SET0_CRT_PARAMS */
  282. #ifndef HAVE_RSA_GET0_FACTORS
  283. void
  284. RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  285. {
  286. if (p != NULL)
  287. *p = r->p;
  288. if (q != NULL)
  289. *q = r->q;
  290. }
  291. #endif /* HAVE_RSA_GET0_FACTORS */
  292. #ifndef HAVE_RSA_SET0_FACTORS
  293. int
  294. RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  295. {
  296. if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
  297. return 0;
  298. if (p != NULL) {
  299. BN_free(r->p);
  300. r->p = p;
  301. }
  302. if (q != NULL) {
  303. BN_free(r->q);
  304. r->q = q;
  305. }
  306. return 1;
  307. }
  308. #endif /* HAVE_RSA_SET0_FACTORS */
  309. #ifndef HAVE_EVP_CIPHER_CTX_GET_IV
  310. int
  311. EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, unsigned char *iv, size_t len)
  312. {
  313. if (ctx == NULL)
  314. return 0;
  315. if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
  316. return 0;
  317. if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
  318. return 0;
  319. if (len > EVP_MAX_IV_LENGTH)
  320. return 0; /* sanity check; shouldn't happen */
  321. /*
  322. * Skip the memcpy entirely when the requested IV length is zero,
  323. * since the iv pointer may be NULL or invalid.
  324. */
  325. if (len != 0) {
  326. if (iv == NULL)
  327. return 0;
  328. # ifdef HAVE_EVP_CIPHER_CTX_IV
  329. memcpy(iv, EVP_CIPHER_CTX_iv(ctx), len);
  330. # else
  331. memcpy(iv, ctx->iv, len);
  332. # endif /* HAVE_EVP_CIPHER_CTX_IV */
  333. }
  334. return 1;
  335. }
  336. #endif /* HAVE_EVP_CIPHER_CTX_GET_IV */
  337. #ifndef HAVE_EVP_CIPHER_CTX_SET_IV
  338. int
  339. EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, const unsigned char *iv, size_t len)
  340. {
  341. if (ctx == NULL)
  342. return 0;
  343. if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
  344. return 0;
  345. if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
  346. return 0;
  347. if (len > EVP_MAX_IV_LENGTH)
  348. return 0; /* sanity check; shouldn't happen */
  349. /*
  350. * Skip the memcpy entirely when the requested IV length is zero,
  351. * since the iv pointer may be NULL or invalid.
  352. */
  353. if (len != 0) {
  354. if (iv == NULL)
  355. return 0;
  356. # ifdef HAVE_EVP_CIPHER_CTX_IV_NOCONST
  357. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, len);
  358. # else
  359. memcpy(ctx->iv, iv, len);
  360. # endif /* HAVE_EVP_CIPHER_CTX_IV_NOCONST */
  361. }
  362. return 1;
  363. }
  364. #endif /* HAVE_EVP_CIPHER_CTX_SET_IV */
  365. #ifndef HAVE_DSA_SIG_GET0
  366. void
  367. DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  368. {
  369. if (pr != NULL)
  370. *pr = sig->r;
  371. if (ps != NULL)
  372. *ps = sig->s;
  373. }
  374. #endif /* HAVE_DSA_SIG_GET0 */
  375. #ifndef HAVE_DSA_SIG_SET0
  376. int
  377. DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  378. {
  379. if (r == NULL || s == NULL)
  380. return 0;
  381. BN_clear_free(sig->r);
  382. sig->r = r;
  383. BN_clear_free(sig->s);
  384. sig->s = s;
  385. return 1;
  386. }
  387. #endif /* HAVE_DSA_SIG_SET0 */
  388. #ifdef OPENSSL_HAS_ECC
  389. #ifndef HAVE_ECDSA_SIG_GET0
  390. void
  391. ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  392. {
  393. if (pr != NULL)
  394. *pr = sig->r;
  395. if (ps != NULL)
  396. *ps = sig->s;
  397. }
  398. #endif /* HAVE_ECDSA_SIG_GET0 */
  399. #ifndef HAVE_ECDSA_SIG_SET0
  400. int
  401. ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  402. {
  403. if (r == NULL || s == NULL)
  404. return 0;
  405. BN_clear_free(sig->r);
  406. BN_clear_free(sig->s);
  407. sig->r = r;
  408. sig->s = s;
  409. return 1;
  410. }
  411. #endif /* HAVE_ECDSA_SIG_SET0 */
  412. #endif /* OPENSSL_HAS_ECC */
  413. #ifndef HAVE_DH_GET0_PQG
  414. void
  415. DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  416. {
  417. if (p != NULL)
  418. *p = dh->p;
  419. if (q != NULL)
  420. *q = dh->q;
  421. if (g != NULL)
  422. *g = dh->g;
  423. }
  424. #endif /* HAVE_DH_GET0_PQG */
  425. #ifndef HAVE_DH_SET0_PQG
  426. int
  427. DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  428. {
  429. if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
  430. return 0;
  431. if (p != NULL) {
  432. BN_free(dh->p);
  433. dh->p = p;
  434. }
  435. if (q != NULL) {
  436. BN_free(dh->q);
  437. dh->q = q;
  438. }
  439. if (g != NULL) {
  440. BN_free(dh->g);
  441. dh->g = g;
  442. }
  443. return 1;
  444. }
  445. #endif /* HAVE_DH_SET0_PQG */
  446. #ifndef HAVE_DH_GET0_KEY
  447. void
  448. DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  449. {
  450. if (pub_key != NULL)
  451. *pub_key = dh->pub_key;
  452. if (priv_key != NULL)
  453. *priv_key = dh->priv_key;
  454. }
  455. #endif /* HAVE_DH_GET0_KEY */
  456. #ifndef HAVE_DH_SET0_KEY
  457. int
  458. DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
  459. {
  460. if (pub_key != NULL) {
  461. BN_free(dh->pub_key);
  462. dh->pub_key = pub_key;
  463. }
  464. if (priv_key != NULL) {
  465. BN_free(dh->priv_key);
  466. dh->priv_key = priv_key;
  467. }
  468. return 1;
  469. }
  470. #endif /* HAVE_DH_SET0_KEY */
  471. #ifndef HAVE_DH_SET_LENGTH
  472. int
  473. DH_set_length(DH *dh, long length)
  474. {
  475. if (length < 0 || length > INT_MAX)
  476. return 0;
  477. dh->length = length;
  478. return 1;
  479. }
  480. #endif /* HAVE_DH_SET_LENGTH */
  481. #ifndef HAVE_RSA_METH_FREE
  482. void
  483. RSA_meth_free(RSA_METHOD *meth)
  484. {
  485. if (meth != NULL) {
  486. free((char *)meth->name);
  487. free(meth);
  488. }
  489. }
  490. #endif /* HAVE_RSA_METH_FREE */
  491. #ifndef HAVE_RSA_METH_DUP
  492. RSA_METHOD *
  493. RSA_meth_dup(const RSA_METHOD *meth)
  494. {
  495. RSA_METHOD *copy;
  496. if ((copy = calloc(1, sizeof(*copy))) == NULL)
  497. return NULL;
  498. memcpy(copy, meth, sizeof(*copy));
  499. if ((copy->name = strdup(meth->name)) == NULL) {
  500. free(copy);
  501. return NULL;
  502. }
  503. return copy;
  504. }
  505. #endif /* HAVE_RSA_METH_DUP */
  506. #ifndef HAVE_RSA_METH_SET1_NAME
  507. int
  508. RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
  509. {
  510. char *copy;
  511. if ((copy = strdup(name)) == NULL)
  512. return 0;
  513. free((char *)meth->name);
  514. meth->name = copy;
  515. return 1;
  516. }
  517. #endif /* HAVE_RSA_METH_SET1_NAME */
  518. #ifndef HAVE_RSA_METH_GET_FINISH
  519. int
  520. (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa)
  521. {
  522. return meth->finish;
  523. }
  524. #endif /* HAVE_RSA_METH_GET_FINISH */
  525. #ifndef HAVE_RSA_METH_SET_PRIV_ENC
  526. int
  527. RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
  528. const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
  529. {
  530. meth->rsa_priv_enc = priv_enc;
  531. return 1;
  532. }
  533. #endif /* HAVE_RSA_METH_SET_PRIV_ENC */
  534. #ifndef HAVE_RSA_METH_SET_PRIV_DEC
  535. int
  536. RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
  537. const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
  538. {
  539. meth->rsa_priv_dec = priv_dec;
  540. return 1;
  541. }
  542. #endif /* HAVE_RSA_METH_SET_PRIV_DEC */
  543. #ifndef HAVE_RSA_METH_SET_FINISH
  544. int
  545. RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa))
  546. {
  547. meth->finish = finish;
  548. return 1;
  549. }
  550. #endif /* HAVE_RSA_METH_SET_FINISH */
  551. #ifndef HAVE_EVP_PKEY_GET0_RSA
  552. RSA *
  553. EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  554. {
  555. if (pkey->type != EVP_PKEY_RSA) {
  556. /* EVPerror(EVP_R_EXPECTING_AN_RSA_KEY); */
  557. return NULL;
  558. }
  559. return pkey->pkey.rsa;
  560. }
  561. #endif /* HAVE_EVP_PKEY_GET0_RSA */
  562. #ifndef HAVE_EVP_MD_CTX_NEW
  563. EVP_MD_CTX *
  564. EVP_MD_CTX_new(void)
  565. {
  566. return calloc(1, sizeof(EVP_MD_CTX));
  567. }
  568. #endif /* HAVE_EVP_MD_CTX_NEW */
  569. #ifndef HAVE_EVP_MD_CTX_FREE
  570. void
  571. EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  572. {
  573. if (ctx == NULL)
  574. return;
  575. EVP_MD_CTX_cleanup(ctx);
  576. free(ctx);
  577. }
  578. #endif /* HAVE_EVP_MD_CTX_FREE */
  579. #endif /* WITH_OPENSSL */