ssl_rsa.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "ssl_local.h"
  11. #include "internal/packet.h"
  12. #include <openssl/bio.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/x509v3.h>
  17. #include <openssl/pem.h>
  18. static int ssl_set_cert(CERT *c, X509 *x509);
  19. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
  20. #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
  21. | SSL_EXT_CLIENT_HELLO \
  22. | SSL_EXT_TLS1_2_SERVER_HELLO \
  23. | SSL_EXT_IGNORE_ON_RESUMPTION)
  24. int SSL_use_certificate(SSL *ssl, X509 *x)
  25. {
  26. int rv;
  27. if (x == NULL) {
  28. SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
  29. return 0;
  30. }
  31. if (!X509v3_cache_extensions(x, ssl->ctx->libctx, ssl->ctx->propq)) {
  32. SSLerr(0, ERR_LIB_X509);
  33. return 0;
  34. }
  35. rv = ssl_security_cert(ssl, NULL, x, 0, 1);
  36. if (rv != 1) {
  37. SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv);
  38. return 0;
  39. }
  40. return ssl_set_cert(ssl->cert, x);
  41. }
  42. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  43. {
  44. int j;
  45. BIO *in;
  46. int ret = 0;
  47. X509 *x = NULL;
  48. in = BIO_new(BIO_s_file());
  49. if (in == NULL) {
  50. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
  51. goto end;
  52. }
  53. if (BIO_read_filename(in, file) <= 0) {
  54. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
  55. goto end;
  56. }
  57. if (type == SSL_FILETYPE_ASN1) {
  58. j = ERR_R_ASN1_LIB;
  59. x = d2i_X509_bio(in, NULL);
  60. } else if (type == SSL_FILETYPE_PEM) {
  61. j = ERR_R_PEM_LIB;
  62. x = PEM_read_bio_X509(in, NULL, ssl->default_passwd_callback,
  63. ssl->default_passwd_callback_userdata);
  64. } else {
  65. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
  66. goto end;
  67. }
  68. if (x == NULL) {
  69. SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j);
  70. goto end;
  71. }
  72. ret = SSL_use_certificate(ssl, x);
  73. end:
  74. X509_free(x);
  75. BIO_free(in);
  76. return ret;
  77. }
  78. int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
  79. {
  80. X509 *x;
  81. int ret;
  82. x = d2i_X509(NULL, &d, (long)len);
  83. if (x == NULL) {
  84. SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
  85. return 0;
  86. }
  87. ret = SSL_use_certificate(ssl, x);
  88. X509_free(x);
  89. return ret;
  90. }
  91. #ifndef OPENSSL_NO_RSA
  92. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
  93. {
  94. EVP_PKEY *pkey;
  95. int ret;
  96. if (rsa == NULL) {
  97. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  98. return 0;
  99. }
  100. if ((pkey = EVP_PKEY_new()) == NULL) {
  101. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
  102. return 0;
  103. }
  104. RSA_up_ref(rsa);
  105. if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
  106. RSA_free(rsa);
  107. EVP_PKEY_free(pkey);
  108. return 0;
  109. }
  110. ret = ssl_set_pkey(ssl->cert, pkey);
  111. EVP_PKEY_free(pkey);
  112. return ret;
  113. }
  114. #endif
  115. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
  116. {
  117. size_t i;
  118. if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
  119. SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  120. return 0;
  121. }
  122. if (c->pkeys[i].x509 != NULL) {
  123. EVP_PKEY *pktmp;
  124. pktmp = X509_get0_pubkey(c->pkeys[i].x509);
  125. if (pktmp == NULL) {
  126. SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE);
  127. return 0;
  128. }
  129. /*
  130. * The return code from EVP_PKEY_copy_parameters is deliberately
  131. * ignored. Some EVP_PKEY types cannot do this.
  132. */
  133. EVP_PKEY_copy_parameters(pktmp, pkey);
  134. ERR_clear_error();
  135. #ifndef OPENSSL_NO_RSA
  136. /*
  137. * Don't check the public/private key, this is mostly for smart
  138. * cards.
  139. */
  140. if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA
  141. && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK) ;
  142. else
  143. #endif
  144. if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
  145. X509_free(c->pkeys[i].x509);
  146. c->pkeys[i].x509 = NULL;
  147. return 0;
  148. }
  149. }
  150. EVP_PKEY_free(c->pkeys[i].privatekey);
  151. EVP_PKEY_up_ref(pkey);
  152. c->pkeys[i].privatekey = pkey;
  153. c->key = &c->pkeys[i];
  154. return 1;
  155. }
  156. #ifndef OPENSSL_NO_RSA
  157. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
  158. {
  159. int j, ret = 0;
  160. BIO *in;
  161. RSA *rsa = NULL;
  162. in = BIO_new(BIO_s_file());
  163. if (in == NULL) {
  164. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
  165. goto end;
  166. }
  167. if (BIO_read_filename(in, file) <= 0) {
  168. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
  169. goto end;
  170. }
  171. if (type == SSL_FILETYPE_ASN1) {
  172. j = ERR_R_ASN1_LIB;
  173. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  174. } else if (type == SSL_FILETYPE_PEM) {
  175. j = ERR_R_PEM_LIB;
  176. rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
  177. ssl->default_passwd_callback,
  178. ssl->default_passwd_callback_userdata);
  179. } else {
  180. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  181. goto end;
  182. }
  183. if (rsa == NULL) {
  184. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j);
  185. goto end;
  186. }
  187. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  188. RSA_free(rsa);
  189. end:
  190. BIO_free(in);
  191. return ret;
  192. }
  193. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
  194. {
  195. int ret;
  196. const unsigned char *p;
  197. RSA *rsa;
  198. p = d;
  199. if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
  200. SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  201. return 0;
  202. }
  203. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  204. RSA_free(rsa);
  205. return ret;
  206. }
  207. #endif /* !OPENSSL_NO_RSA */
  208. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  209. {
  210. int ret;
  211. if (pkey == NULL) {
  212. SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  213. return 0;
  214. }
  215. ret = ssl_set_pkey(ssl->cert, pkey);
  216. return ret;
  217. }
  218. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
  219. {
  220. int j, ret = 0;
  221. BIO *in;
  222. EVP_PKEY *pkey = NULL;
  223. in = BIO_new(BIO_s_file());
  224. if (in == NULL) {
  225. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
  226. goto end;
  227. }
  228. if (BIO_read_filename(in, file) <= 0) {
  229. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
  230. goto end;
  231. }
  232. if (type == SSL_FILETYPE_PEM) {
  233. j = ERR_R_PEM_LIB;
  234. pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
  235. ssl->default_passwd_callback,
  236. ssl->default_passwd_callback_userdata,
  237. ssl->ctx->libctx,
  238. ssl->ctx->propq);
  239. } else if (type == SSL_FILETYPE_ASN1) {
  240. j = ERR_R_ASN1_LIB;
  241. pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
  242. ssl->ctx->propq);
  243. } else {
  244. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  245. goto end;
  246. }
  247. if (pkey == NULL) {
  248. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j);
  249. goto end;
  250. }
  251. ret = SSL_use_PrivateKey(ssl, pkey);
  252. EVP_PKEY_free(pkey);
  253. end:
  254. BIO_free(in);
  255. return ret;
  256. }
  257. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
  258. long len)
  259. {
  260. int ret;
  261. const unsigned char *p;
  262. EVP_PKEY *pkey;
  263. p = d;
  264. if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
  265. ssl->ctx->propq)) == NULL) {
  266. SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  267. return 0;
  268. }
  269. ret = SSL_use_PrivateKey(ssl, pkey);
  270. EVP_PKEY_free(pkey);
  271. return ret;
  272. }
  273. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  274. {
  275. int rv;
  276. if (x == NULL) {
  277. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER);
  278. return 0;
  279. }
  280. if (!X509v3_cache_extensions(x, ctx->libctx, ctx->propq)) {
  281. SSLerr(0, ERR_LIB_X509);
  282. return 0;
  283. }
  284. rv = ssl_security_cert(NULL, ctx, x, 0, 1);
  285. if (rv != 1) {
  286. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv);
  287. return 0;
  288. }
  289. return ssl_set_cert(ctx->cert, x);
  290. }
  291. static int ssl_set_cert(CERT *c, X509 *x)
  292. {
  293. EVP_PKEY *pkey;
  294. size_t i;
  295. pkey = X509_get0_pubkey(x);
  296. if (pkey == NULL) {
  297. SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB);
  298. return 0;
  299. }
  300. if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
  301. SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  302. return 0;
  303. }
  304. #ifndef OPENSSL_NO_EC
  305. if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
  306. SSLerr(SSL_F_SSL_SET_CERT, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  307. return 0;
  308. }
  309. #endif
  310. if (c->pkeys[i].privatekey != NULL) {
  311. /*
  312. * The return code from EVP_PKEY_copy_parameters is deliberately
  313. * ignored. Some EVP_PKEY types cannot do this.
  314. */
  315. EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
  316. ERR_clear_error();
  317. #ifndef OPENSSL_NO_RSA
  318. /*
  319. * Don't check the public/private key, this is mostly for smart
  320. * cards.
  321. */
  322. if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA
  323. && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) &
  324. RSA_METHOD_FLAG_NO_CHECK) ;
  325. else
  326. #endif /* OPENSSL_NO_RSA */
  327. if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
  328. /*
  329. * don't fail for a cert/key mismatch, just free current private
  330. * key (when switching to a different cert & key, first this
  331. * function should be used, then ssl_set_pkey
  332. */
  333. EVP_PKEY_free(c->pkeys[i].privatekey);
  334. c->pkeys[i].privatekey = NULL;
  335. /* clear error queue */
  336. ERR_clear_error();
  337. }
  338. }
  339. X509_free(c->pkeys[i].x509);
  340. X509_up_ref(x);
  341. c->pkeys[i].x509 = x;
  342. c->key = &(c->pkeys[i]);
  343. return 1;
  344. }
  345. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  346. {
  347. int j;
  348. BIO *in;
  349. int ret = 0;
  350. X509 *x = NULL;
  351. in = BIO_new(BIO_s_file());
  352. if (in == NULL) {
  353. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB);
  354. goto end;
  355. }
  356. if (BIO_read_filename(in, file) <= 0) {
  357. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
  358. goto end;
  359. }
  360. if (type == SSL_FILETYPE_ASN1) {
  361. j = ERR_R_ASN1_LIB;
  362. x = d2i_X509_bio(in, NULL);
  363. } else if (type == SSL_FILETYPE_PEM) {
  364. j = ERR_R_PEM_LIB;
  365. x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  366. ctx->default_passwd_callback_userdata);
  367. } else {
  368. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE);
  369. goto end;
  370. }
  371. if (x == NULL) {
  372. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j);
  373. goto end;
  374. }
  375. ret = SSL_CTX_use_certificate(ctx, x);
  376. end:
  377. X509_free(x);
  378. BIO_free(in);
  379. return ret;
  380. }
  381. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
  382. {
  383. X509 *x;
  384. int ret;
  385. x = d2i_X509(NULL, &d, (long)len);
  386. if (x == NULL) {
  387. SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB);
  388. return 0;
  389. }
  390. ret = SSL_CTX_use_certificate(ctx, x);
  391. X509_free(x);
  392. return ret;
  393. }
  394. #ifndef OPENSSL_NO_RSA
  395. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
  396. {
  397. int ret;
  398. EVP_PKEY *pkey;
  399. if (rsa == NULL) {
  400. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  401. return 0;
  402. }
  403. if ((pkey = EVP_PKEY_new()) == NULL) {
  404. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB);
  405. return 0;
  406. }
  407. RSA_up_ref(rsa);
  408. if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
  409. RSA_free(rsa);
  410. EVP_PKEY_free(pkey);
  411. return 0;
  412. }
  413. ret = ssl_set_pkey(ctx->cert, pkey);
  414. EVP_PKEY_free(pkey);
  415. return ret;
  416. }
  417. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  418. {
  419. int j, ret = 0;
  420. BIO *in;
  421. RSA *rsa = NULL;
  422. in = BIO_new(BIO_s_file());
  423. if (in == NULL) {
  424. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB);
  425. goto end;
  426. }
  427. if (BIO_read_filename(in, file) <= 0) {
  428. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB);
  429. goto end;
  430. }
  431. if (type == SSL_FILETYPE_ASN1) {
  432. j = ERR_R_ASN1_LIB;
  433. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  434. } else if (type == SSL_FILETYPE_PEM) {
  435. j = ERR_R_PEM_LIB;
  436. rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
  437. ctx->default_passwd_callback,
  438. ctx->default_passwd_callback_userdata);
  439. } else {
  440. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  441. goto end;
  442. }
  443. if (rsa == NULL) {
  444. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j);
  445. goto end;
  446. }
  447. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  448. RSA_free(rsa);
  449. end:
  450. BIO_free(in);
  451. return ret;
  452. }
  453. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
  454. long len)
  455. {
  456. int ret;
  457. const unsigned char *p;
  458. RSA *rsa;
  459. p = d;
  460. if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
  461. SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  462. return 0;
  463. }
  464. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  465. RSA_free(rsa);
  466. return ret;
  467. }
  468. #endif /* !OPENSSL_NO_RSA */
  469. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  470. {
  471. if (pkey == NULL) {
  472. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
  473. return 0;
  474. }
  475. return ssl_set_pkey(ctx->cert, pkey);
  476. }
  477. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  478. {
  479. int j, ret = 0;
  480. BIO *in;
  481. EVP_PKEY *pkey = NULL;
  482. in = BIO_new(BIO_s_file());
  483. if (in == NULL) {
  484. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB);
  485. goto end;
  486. }
  487. if (BIO_read_filename(in, file) <= 0) {
  488. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB);
  489. goto end;
  490. }
  491. if (type == SSL_FILETYPE_PEM) {
  492. j = ERR_R_PEM_LIB;
  493. pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
  494. ctx->default_passwd_callback,
  495. ctx->default_passwd_callback_userdata,
  496. ctx->libctx, ctx->propq);
  497. } else if (type == SSL_FILETYPE_ASN1) {
  498. j = ERR_R_ASN1_LIB;
  499. pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
  500. } else {
  501. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE);
  502. goto end;
  503. }
  504. if (pkey == NULL) {
  505. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j);
  506. goto end;
  507. }
  508. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  509. EVP_PKEY_free(pkey);
  510. end:
  511. BIO_free(in);
  512. return ret;
  513. }
  514. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
  515. const unsigned char *d, long len)
  516. {
  517. int ret;
  518. const unsigned char *p;
  519. EVP_PKEY *pkey;
  520. p = d;
  521. if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
  522. ctx->propq)) == NULL) {
  523. SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB);
  524. return 0;
  525. }
  526. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  527. EVP_PKEY_free(pkey);
  528. return ret;
  529. }
  530. /*
  531. * Read a file that contains our certificate in "PEM" format, possibly
  532. * followed by a sequence of CA certificates that should be sent to the peer
  533. * in the Certificate message.
  534. */
  535. static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
  536. {
  537. BIO *in;
  538. int ret = 0;
  539. X509 *x = NULL;
  540. pem_password_cb *passwd_callback;
  541. void *passwd_callback_userdata;
  542. ERR_clear_error(); /* clear error stack for
  543. * SSL_CTX_use_certificate() */
  544. if (ctx != NULL) {
  545. passwd_callback = ctx->default_passwd_callback;
  546. passwd_callback_userdata = ctx->default_passwd_callback_userdata;
  547. } else {
  548. passwd_callback = ssl->default_passwd_callback;
  549. passwd_callback_userdata = ssl->default_passwd_callback_userdata;
  550. }
  551. in = BIO_new(BIO_s_file());
  552. if (in == NULL) {
  553. SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB);
  554. goto end;
  555. }
  556. if (BIO_read_filename(in, file) <= 0) {
  557. SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB);
  558. goto end;
  559. }
  560. x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback,
  561. passwd_callback_userdata);
  562. if (x == NULL) {
  563. SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB);
  564. goto end;
  565. }
  566. if (ctx)
  567. ret = SSL_CTX_use_certificate(ctx, x);
  568. else
  569. ret = SSL_use_certificate(ssl, x);
  570. if (ERR_peek_error() != 0)
  571. ret = 0; /* Key/certificate mismatch doesn't imply
  572. * ret==0 ... */
  573. if (ret) {
  574. /*
  575. * If we could set up our certificate, now proceed to the CA
  576. * certificates.
  577. */
  578. X509 *ca;
  579. int r;
  580. unsigned long err;
  581. if (ctx)
  582. r = SSL_CTX_clear_chain_certs(ctx);
  583. else
  584. r = SSL_clear_chain_certs(ssl);
  585. if (r == 0) {
  586. ret = 0;
  587. goto end;
  588. }
  589. while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
  590. passwd_callback_userdata))
  591. != NULL) {
  592. if (ctx)
  593. r = SSL_CTX_add0_chain_cert(ctx, ca);
  594. else
  595. r = SSL_add0_chain_cert(ssl, ca);
  596. /*
  597. * Note that we must not free ca if it was successfully added to
  598. * the chain (while we must free the main certificate, since its
  599. * reference count is increased by SSL_CTX_use_certificate).
  600. */
  601. if (!r) {
  602. X509_free(ca);
  603. ret = 0;
  604. goto end;
  605. }
  606. }
  607. /* When the while loop ends, it's usually just EOF. */
  608. err = ERR_peek_last_error();
  609. if (ERR_GET_LIB(err) == ERR_LIB_PEM
  610. && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  611. ERR_clear_error();
  612. else
  613. ret = 0; /* some real error */
  614. }
  615. end:
  616. X509_free(x);
  617. BIO_free(in);
  618. return ret;
  619. }
  620. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
  621. {
  622. return use_certificate_chain_file(ctx, NULL, file);
  623. }
  624. int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
  625. {
  626. return use_certificate_chain_file(NULL, ssl, file);
  627. }
  628. static int serverinfo_find_extension(const unsigned char *serverinfo,
  629. size_t serverinfo_length,
  630. unsigned int extension_type,
  631. const unsigned char **extension_data,
  632. size_t *extension_length)
  633. {
  634. PACKET pkt, data;
  635. *extension_data = NULL;
  636. *extension_length = 0;
  637. if (serverinfo == NULL || serverinfo_length == 0)
  638. return -1;
  639. if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
  640. return -1;
  641. for (;;) {
  642. unsigned int type = 0;
  643. unsigned long context = 0;
  644. /* end of serverinfo */
  645. if (PACKET_remaining(&pkt) == 0)
  646. return 0; /* Extension not found */
  647. if (!PACKET_get_net_4(&pkt, &context)
  648. || !PACKET_get_net_2(&pkt, &type)
  649. || !PACKET_get_length_prefixed_2(&pkt, &data))
  650. return -1;
  651. if (type == extension_type) {
  652. *extension_data = PACKET_data(&data);
  653. *extension_length = PACKET_remaining(&data);;
  654. return 1; /* Success */
  655. }
  656. }
  657. /* Unreachable */
  658. }
  659. static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
  660. unsigned int context,
  661. const unsigned char *in,
  662. size_t inlen, X509 *x, size_t chainidx,
  663. int *al, void *arg)
  664. {
  665. if (inlen != 0) {
  666. *al = SSL_AD_DECODE_ERROR;
  667. return 0;
  668. }
  669. return 1;
  670. }
  671. static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
  672. const unsigned char *in,
  673. size_t inlen, int *al, void *arg)
  674. {
  675. return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
  676. arg);
  677. }
  678. static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
  679. unsigned int context,
  680. const unsigned char **out,
  681. size_t *outlen, X509 *x, size_t chainidx,
  682. int *al, void *arg)
  683. {
  684. const unsigned char *serverinfo = NULL;
  685. size_t serverinfo_length = 0;
  686. /* We only support extensions for the first Certificate */
  687. if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
  688. return 0;
  689. /* Is there serverinfo data for the chosen server cert? */
  690. if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
  691. &serverinfo_length)) != 0) {
  692. /* Find the relevant extension from the serverinfo */
  693. int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
  694. ext_type, out, outlen);
  695. if (retval == -1) {
  696. *al = SSL_AD_INTERNAL_ERROR;
  697. return -1; /* Error */
  698. }
  699. if (retval == 0)
  700. return 0; /* No extension found, don't send extension */
  701. return 1; /* Send extension */
  702. }
  703. return 0; /* No serverinfo data found, don't send
  704. * extension */
  705. }
  706. static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
  707. const unsigned char **out, size_t *outlen,
  708. int *al, void *arg)
  709. {
  710. return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
  711. arg);
  712. }
  713. /*
  714. * With a NULL context, this function just checks that the serverinfo data
  715. * parses correctly. With a non-NULL context, it registers callbacks for
  716. * the included extensions.
  717. */
  718. static int serverinfo_process_buffer(unsigned int version,
  719. const unsigned char *serverinfo,
  720. size_t serverinfo_length, SSL_CTX *ctx)
  721. {
  722. PACKET pkt;
  723. if (serverinfo == NULL || serverinfo_length == 0)
  724. return 0;
  725. if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
  726. return 0;
  727. if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
  728. return 0;
  729. while (PACKET_remaining(&pkt)) {
  730. unsigned long context = 0;
  731. unsigned int ext_type = 0;
  732. PACKET data;
  733. if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
  734. || !PACKET_get_net_2(&pkt, &ext_type)
  735. || !PACKET_get_length_prefixed_2(&pkt, &data))
  736. return 0;
  737. if (ctx == NULL)
  738. continue;
  739. /*
  740. * The old style custom extensions API could be set separately for
  741. * server/client, i.e. you could set one custom extension for a client,
  742. * and *for the same extension in the same SSL_CTX* you could set a
  743. * custom extension for the server as well. It seems quite weird to be
  744. * setting a custom extension for both client and server in a single
  745. * SSL_CTX - but theoretically possible. This isn't possible in the
  746. * new API. Therefore, if we have V1 serverinfo we use the old API. We
  747. * also use the old API even if we have V2 serverinfo but the context
  748. * looks like an old style <= TLSv1.2 extension.
  749. */
  750. if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
  751. if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
  752. serverinfo_srv_add_cb,
  753. NULL, NULL,
  754. serverinfo_srv_parse_cb,
  755. NULL))
  756. return 0;
  757. } else {
  758. if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
  759. serverinfoex_srv_add_cb,
  760. NULL, NULL,
  761. serverinfoex_srv_parse_cb,
  762. NULL))
  763. return 0;
  764. }
  765. }
  766. return 1;
  767. }
  768. int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
  769. const unsigned char *serverinfo,
  770. size_t serverinfo_length)
  771. {
  772. unsigned char *new_serverinfo;
  773. if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
  774. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
  775. return 0;
  776. }
  777. if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
  778. NULL)) {
  779. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
  780. return 0;
  781. }
  782. if (ctx->cert->key == NULL) {
  783. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
  784. return 0;
  785. }
  786. new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
  787. serverinfo_length);
  788. if (new_serverinfo == NULL) {
  789. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
  790. return 0;
  791. }
  792. ctx->cert->key->serverinfo = new_serverinfo;
  793. memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
  794. ctx->cert->key->serverinfo_length = serverinfo_length;
  795. /*
  796. * Now that the serverinfo is validated and stored, go ahead and
  797. * register callbacks.
  798. */
  799. if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
  800. ctx)) {
  801. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
  802. return 0;
  803. }
  804. return 1;
  805. }
  806. int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
  807. size_t serverinfo_length)
  808. {
  809. return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
  810. serverinfo_length);
  811. }
  812. int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
  813. {
  814. unsigned char *serverinfo = NULL;
  815. unsigned char *tmp;
  816. size_t serverinfo_length = 0;
  817. unsigned char *extension = 0;
  818. long extension_length = 0;
  819. char *name = NULL;
  820. char *header = NULL;
  821. static const char namePrefix1[] = "SERVERINFO FOR ";
  822. static const char namePrefix2[] = "SERVERINFOV2 FOR ";
  823. unsigned int name_len;
  824. int ret = 0;
  825. BIO *bin = NULL;
  826. size_t num_extensions = 0, contextoff = 0;
  827. if (ctx == NULL || file == NULL) {
  828. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
  829. goto end;
  830. }
  831. bin = BIO_new(BIO_s_file());
  832. if (bin == NULL) {
  833. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
  834. goto end;
  835. }
  836. if (BIO_read_filename(bin, file) <= 0) {
  837. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
  838. goto end;
  839. }
  840. for (num_extensions = 0;; num_extensions++) {
  841. unsigned int version;
  842. if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
  843. == 0) {
  844. /*
  845. * There must be at least one extension in this file
  846. */
  847. if (num_extensions == 0) {
  848. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
  849. SSL_R_NO_PEM_EXTENSIONS);
  850. goto end;
  851. } else /* End of file, we're done */
  852. break;
  853. }
  854. /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
  855. name_len = strlen(name);
  856. if (name_len < sizeof(namePrefix1) - 1) {
  857. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
  858. goto end;
  859. }
  860. if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
  861. version = SSL_SERVERINFOV1;
  862. } else {
  863. if (name_len < sizeof(namePrefix2) - 1) {
  864. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
  865. SSL_R_PEM_NAME_TOO_SHORT);
  866. goto end;
  867. }
  868. if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
  869. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
  870. SSL_R_PEM_NAME_BAD_PREFIX);
  871. goto end;
  872. }
  873. version = SSL_SERVERINFOV2;
  874. }
  875. /*
  876. * Check that the decoded PEM data is plausible (valid length field)
  877. */
  878. if (version == SSL_SERVERINFOV1) {
  879. /* 4 byte header: 2 bytes type, 2 bytes len */
  880. if (extension_length < 4
  881. || (extension[2] << 8) + extension[3]
  882. != extension_length - 4) {
  883. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
  884. goto end;
  885. }
  886. /*
  887. * File does not have a context value so we must take account of
  888. * this later.
  889. */
  890. contextoff = 4;
  891. } else {
  892. /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
  893. if (extension_length < 8
  894. || (extension[6] << 8) + extension[7]
  895. != extension_length - 8) {
  896. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
  897. goto end;
  898. }
  899. }
  900. /* Append the decoded extension to the serverinfo buffer */
  901. tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
  902. + contextoff);
  903. if (tmp == NULL) {
  904. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
  905. goto end;
  906. }
  907. serverinfo = tmp;
  908. if (contextoff > 0) {
  909. unsigned char *sinfo = serverinfo + serverinfo_length;
  910. /* We know this only uses the last 2 bytes */
  911. sinfo[0] = 0;
  912. sinfo[1] = 0;
  913. sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
  914. sinfo[3] = SYNTHV1CONTEXT & 0xff;
  915. }
  916. memcpy(serverinfo + serverinfo_length + contextoff,
  917. extension, extension_length);
  918. serverinfo_length += extension_length + contextoff;
  919. OPENSSL_free(name);
  920. name = NULL;
  921. OPENSSL_free(header);
  922. header = NULL;
  923. OPENSSL_free(extension);
  924. extension = NULL;
  925. }
  926. ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
  927. serverinfo_length);
  928. end:
  929. /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
  930. OPENSSL_free(name);
  931. OPENSSL_free(header);
  932. OPENSSL_free(extension);
  933. OPENSSL_free(serverinfo);
  934. BIO_free(bin);
  935. return ret;
  936. }
  937. static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
  938. STACK_OF(X509) *chain, int override)
  939. {
  940. int ret = 0;
  941. size_t i;
  942. int j;
  943. int rv;
  944. CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
  945. SSL_CTX *actualctx = ssl == NULL ? ctx : ssl->ctx;
  946. STACK_OF(X509) *dup_chain = NULL;
  947. EVP_PKEY *pubkey = NULL;
  948. if (!X509v3_cache_extensions(x509, actualctx->libctx, actualctx->propq)) {
  949. SSLerr(0, ERR_R_X509_LIB);
  950. goto out;
  951. }
  952. /* Do all security checks before anything else */
  953. rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
  954. if (rv != 1) {
  955. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
  956. goto out;
  957. }
  958. for (j = 0; j < sk_X509_num(chain); j++) {
  959. rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
  960. if (rv != 1) {
  961. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
  962. goto out;
  963. }
  964. }
  965. pubkey = X509_get_pubkey(x509); /* bumps reference */
  966. if (pubkey == NULL)
  967. goto out;
  968. if (privatekey == NULL) {
  969. privatekey = pubkey;
  970. } else {
  971. /* For RSA, which has no parameters, missing returns 0 */
  972. if (EVP_PKEY_missing_parameters(privatekey)) {
  973. if (EVP_PKEY_missing_parameters(pubkey)) {
  974. /* nobody has parameters? - error */
  975. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_MISSING_PARAMETERS);
  976. goto out;
  977. } else {
  978. /* copy to privatekey from pubkey */
  979. EVP_PKEY_copy_parameters(privatekey, pubkey);
  980. }
  981. } else if (EVP_PKEY_missing_parameters(pubkey)) {
  982. /* copy to pubkey from privatekey */
  983. EVP_PKEY_copy_parameters(pubkey, privatekey);
  984. } /* else both have parameters */
  985. /* Copied from ssl_set_cert/pkey */
  986. #ifndef OPENSSL_NO_RSA
  987. if ((EVP_PKEY_id(privatekey) == EVP_PKEY_RSA) &&
  988. ((RSA_flags(EVP_PKEY_get0_RSA(privatekey)) & RSA_METHOD_FLAG_NO_CHECK)))
  989. /* no-op */ ;
  990. else
  991. #endif
  992. /* check that key <-> cert match */
  993. if (EVP_PKEY_cmp(pubkey, privatekey) != 1) {
  994. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_PRIVATE_KEY_MISMATCH);
  995. goto out;
  996. }
  997. }
  998. if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
  999. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1000. goto out;
  1001. }
  1002. if (!override && (c->pkeys[i].x509 != NULL
  1003. || c->pkeys[i].privatekey != NULL
  1004. || c->pkeys[i].chain != NULL)) {
  1005. /* No override, and something already there */
  1006. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_NOT_REPLACING_CERTIFICATE);
  1007. goto out;
  1008. }
  1009. if (chain != NULL) {
  1010. dup_chain = X509_chain_up_ref(chain);
  1011. if (dup_chain == NULL) {
  1012. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, ERR_R_MALLOC_FAILURE);
  1013. goto out;
  1014. }
  1015. }
  1016. sk_X509_pop_free(c->pkeys[i].chain, X509_free);
  1017. c->pkeys[i].chain = dup_chain;
  1018. X509_free(c->pkeys[i].x509);
  1019. X509_up_ref(x509);
  1020. c->pkeys[i].x509 = x509;
  1021. EVP_PKEY_free(c->pkeys[i].privatekey);
  1022. EVP_PKEY_up_ref(privatekey);
  1023. c->pkeys[i].privatekey = privatekey;
  1024. c->key = &(c->pkeys[i]);
  1025. ret = 1;
  1026. out:
  1027. EVP_PKEY_free(pubkey);
  1028. return ret;
  1029. }
  1030. int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
  1031. STACK_OF(X509) *chain, int override)
  1032. {
  1033. return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
  1034. }
  1035. int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
  1036. STACK_OF(X509) *chain, int override)
  1037. {
  1038. return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);
  1039. }