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.
 
 
 
 
 
 

689 lines
18 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <stdio.h>
  57. #include <openssl/bio.h>
  58. #include <openssl/err.h>
  59. #include <openssl/evp.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. #include <openssl/pem.h>
  63. #include <openssl/x509.h>
  64. #include "internal.h"
  65. static int ssl_set_cert(CERT *c, X509 *x509);
  66. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
  67. static int is_key_type_supported(int key_type) {
  68. return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC;
  69. }
  70. int SSL_use_certificate(SSL *ssl, X509 *x) {
  71. if (x == NULL) {
  72. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  73. return 0;
  74. }
  75. return ssl_set_cert(ssl->cert, x);
  76. }
  77. int SSL_use_certificate_file(SSL *ssl, const char *file, int type) {
  78. int reason_code;
  79. BIO *in;
  80. int ret = 0;
  81. X509 *x = NULL;
  82. in = BIO_new(BIO_s_file());
  83. if (in == NULL) {
  84. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  85. goto end;
  86. }
  87. if (BIO_read_filename(in, file) <= 0) {
  88. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  89. goto end;
  90. }
  91. if (type == SSL_FILETYPE_ASN1) {
  92. reason_code = ERR_R_ASN1_LIB;
  93. x = d2i_X509_bio(in, NULL);
  94. } else if (type == SSL_FILETYPE_PEM) {
  95. reason_code = ERR_R_PEM_LIB;
  96. x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
  97. ssl->ctx->default_passwd_callback_userdata);
  98. } else {
  99. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  100. goto end;
  101. }
  102. if (x == NULL) {
  103. OPENSSL_PUT_ERROR(SSL, reason_code);
  104. goto end;
  105. }
  106. ret = SSL_use_certificate(ssl, x);
  107. end:
  108. X509_free(x);
  109. BIO_free(in);
  110. return ret;
  111. }
  112. int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *d, int len) {
  113. X509 *x;
  114. int ret;
  115. x = d2i_X509(NULL, &d, (long)len);
  116. if (x == NULL) {
  117. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  118. return 0;
  119. }
  120. ret = SSL_use_certificate(ssl, x);
  121. X509_free(x);
  122. return ret;
  123. }
  124. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  125. EVP_PKEY *pkey;
  126. int ret;
  127. if (rsa == NULL) {
  128. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  129. return 0;
  130. }
  131. pkey = EVP_PKEY_new();
  132. if (pkey == NULL) {
  133. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  134. return 0;
  135. }
  136. RSA_up_ref(rsa);
  137. EVP_PKEY_assign_RSA(pkey, rsa);
  138. ret = ssl_set_pkey(ssl->cert, pkey);
  139. EVP_PKEY_free(pkey);
  140. return ret;
  141. }
  142. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey) {
  143. if (!is_key_type_supported(pkey->type)) {
  144. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  145. return 0;
  146. }
  147. if (c->x509 != NULL) {
  148. /* Sanity-check that the private key and the certificate match, unless the
  149. * key is opaque (in case of, say, a smartcard). */
  150. if (!EVP_PKEY_is_opaque(pkey) &&
  151. !X509_check_private_key(c->x509, pkey)) {
  152. X509_free(c->x509);
  153. c->x509 = NULL;
  154. return 0;
  155. }
  156. }
  157. EVP_PKEY_free(c->privatekey);
  158. c->privatekey = EVP_PKEY_up_ref(pkey);
  159. return 1;
  160. }
  161. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type) {
  162. int reason_code, ret = 0;
  163. BIO *in;
  164. RSA *rsa = NULL;
  165. in = BIO_new(BIO_s_file());
  166. if (in == NULL) {
  167. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  168. goto end;
  169. }
  170. if (BIO_read_filename(in, file) <= 0) {
  171. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  172. goto end;
  173. }
  174. if (type == SSL_FILETYPE_ASN1) {
  175. reason_code = ERR_R_ASN1_LIB;
  176. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  177. } else if (type == SSL_FILETYPE_PEM) {
  178. reason_code = ERR_R_PEM_LIB;
  179. rsa =
  180. PEM_read_bio_RSAPrivateKey(in, NULL, ssl->ctx->default_passwd_callback,
  181. ssl->ctx->default_passwd_callback_userdata);
  182. } else {
  183. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  184. goto end;
  185. }
  186. if (rsa == NULL) {
  187. OPENSSL_PUT_ERROR(SSL, reason_code);
  188. goto end;
  189. }
  190. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  191. RSA_free(rsa);
  192. end:
  193. BIO_free(in);
  194. return ret;
  195. }
  196. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  197. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  198. if (rsa == NULL) {
  199. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  200. return 0;
  201. }
  202. int ret = SSL_use_RSAPrivateKey(ssl, rsa);
  203. RSA_free(rsa);
  204. return ret;
  205. }
  206. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  207. int ret;
  208. if (pkey == NULL) {
  209. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  210. return 0;
  211. }
  212. ret = ssl_set_pkey(ssl->cert, pkey);
  213. return ret;
  214. }
  215. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type) {
  216. int reason_code, ret = 0;
  217. BIO *in;
  218. EVP_PKEY *pkey = NULL;
  219. in = BIO_new(BIO_s_file());
  220. if (in == NULL) {
  221. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  222. goto end;
  223. }
  224. if (BIO_read_filename(in, file) <= 0) {
  225. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  226. goto end;
  227. }
  228. if (type == SSL_FILETYPE_PEM) {
  229. reason_code = ERR_R_PEM_LIB;
  230. pkey = PEM_read_bio_PrivateKey(in, NULL, ssl->ctx->default_passwd_callback,
  231. ssl->ctx->default_passwd_callback_userdata);
  232. } else if (type == SSL_FILETYPE_ASN1) {
  233. reason_code = ERR_R_ASN1_LIB;
  234. pkey = d2i_PrivateKey_bio(in, NULL);
  235. } else {
  236. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  237. goto end;
  238. }
  239. if (pkey == NULL) {
  240. OPENSSL_PUT_ERROR(SSL, reason_code);
  241. goto end;
  242. }
  243. ret = SSL_use_PrivateKey(ssl, pkey);
  244. EVP_PKEY_free(pkey);
  245. end:
  246. BIO_free(in);
  247. return ret;
  248. }
  249. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *d, long len) {
  250. int ret;
  251. const uint8_t *p;
  252. EVP_PKEY *pkey;
  253. p = d;
  254. pkey = d2i_PrivateKey(type, NULL, &p, (long)len);
  255. if (pkey == NULL) {
  256. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  257. return 0;
  258. }
  259. ret = SSL_use_PrivateKey(ssl, pkey);
  260. EVP_PKEY_free(pkey);
  261. return ret;
  262. }
  263. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
  264. if (x == NULL) {
  265. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  266. return 0;
  267. }
  268. return ssl_set_cert(ctx->cert, x);
  269. }
  270. static int ssl_set_cert(CERT *c, X509 *x) {
  271. EVP_PKEY *pkey = X509_get_pubkey(x);
  272. if (pkey == NULL) {
  273. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_LIB);
  274. return 0;
  275. }
  276. if (!is_key_type_supported(pkey->type)) {
  277. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  278. EVP_PKEY_free(pkey);
  279. return 0;
  280. }
  281. if (c->privatekey != NULL) {
  282. /* Sanity-check that the private key and the certificate match, unless the
  283. * key is opaque (in case of, say, a smartcard). */
  284. if (!EVP_PKEY_is_opaque(c->privatekey) &&
  285. !X509_check_private_key(x, c->privatekey)) {
  286. /* don't fail for a cert/key mismatch, just free current private key
  287. * (when switching to a different cert & key, first this function should
  288. * be used, then ssl_set_pkey */
  289. EVP_PKEY_free(c->privatekey);
  290. c->privatekey = NULL;
  291. /* clear error queue */
  292. ERR_clear_error();
  293. }
  294. }
  295. EVP_PKEY_free(pkey);
  296. X509_free(c->x509);
  297. c->x509 = X509_up_ref(x);
  298. return 1;
  299. }
  300. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type) {
  301. int reason_code;
  302. BIO *in;
  303. int ret = 0;
  304. X509 *x = NULL;
  305. in = BIO_new(BIO_s_file());
  306. if (in == NULL) {
  307. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  308. goto end;
  309. }
  310. if (BIO_read_filename(in, file) <= 0) {
  311. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  312. goto end;
  313. }
  314. if (type == SSL_FILETYPE_ASN1) {
  315. reason_code = ERR_R_ASN1_LIB;
  316. x = d2i_X509_bio(in, NULL);
  317. } else if (type == SSL_FILETYPE_PEM) {
  318. reason_code = ERR_R_PEM_LIB;
  319. x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  320. ctx->default_passwd_callback_userdata);
  321. } else {
  322. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  323. goto end;
  324. }
  325. if (x == NULL) {
  326. OPENSSL_PUT_ERROR(SSL, reason_code);
  327. goto end;
  328. }
  329. ret = SSL_CTX_use_certificate(ctx, x);
  330. end:
  331. X509_free(x);
  332. BIO_free(in);
  333. return ret;
  334. }
  335. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const uint8_t *d) {
  336. X509 *x;
  337. int ret;
  338. x = d2i_X509(NULL, &d, (long)len);
  339. if (x == NULL) {
  340. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  341. return 0;
  342. }
  343. ret = SSL_CTX_use_certificate(ctx, x);
  344. X509_free(x);
  345. return ret;
  346. }
  347. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  348. int ret;
  349. EVP_PKEY *pkey;
  350. if (rsa == NULL) {
  351. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  352. return 0;
  353. }
  354. pkey = EVP_PKEY_new();
  355. if (pkey == NULL) {
  356. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  357. return 0;
  358. }
  359. RSA_up_ref(rsa);
  360. EVP_PKEY_assign_RSA(pkey, rsa);
  361. ret = ssl_set_pkey(ctx->cert, pkey);
  362. EVP_PKEY_free(pkey);
  363. return ret;
  364. }
  365. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type) {
  366. int reason_code, ret = 0;
  367. BIO *in;
  368. RSA *rsa = NULL;
  369. in = BIO_new(BIO_s_file());
  370. if (in == NULL) {
  371. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  372. goto end;
  373. }
  374. if (BIO_read_filename(in, file) <= 0) {
  375. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  376. goto end;
  377. }
  378. if (type == SSL_FILETYPE_ASN1) {
  379. reason_code = ERR_R_ASN1_LIB;
  380. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  381. } else if (type == SSL_FILETYPE_PEM) {
  382. reason_code = ERR_R_PEM_LIB;
  383. rsa = PEM_read_bio_RSAPrivateKey(in, NULL, ctx->default_passwd_callback,
  384. ctx->default_passwd_callback_userdata);
  385. } else {
  386. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  387. goto end;
  388. }
  389. if (rsa == NULL) {
  390. OPENSSL_PUT_ERROR(SSL, reason_code);
  391. goto end;
  392. }
  393. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  394. RSA_free(rsa);
  395. end:
  396. BIO_free(in);
  397. return ret;
  398. }
  399. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
  400. size_t der_len) {
  401. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  402. if (rsa == NULL) {
  403. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  404. return 0;
  405. }
  406. int ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  407. RSA_free(rsa);
  408. return ret;
  409. }
  410. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  411. if (pkey == NULL) {
  412. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  413. return 0;
  414. }
  415. return ssl_set_pkey(ctx->cert, pkey);
  416. }
  417. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type) {
  418. int reason_code, ret = 0;
  419. BIO *in;
  420. EVP_PKEY *pkey = NULL;
  421. in = BIO_new(BIO_s_file());
  422. if (in == NULL) {
  423. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  424. goto end;
  425. }
  426. if (BIO_read_filename(in, file) <= 0) {
  427. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  428. goto end;
  429. }
  430. if (type == SSL_FILETYPE_PEM) {
  431. reason_code = ERR_R_PEM_LIB;
  432. pkey = PEM_read_bio_PrivateKey(in, NULL, ctx->default_passwd_callback,
  433. ctx->default_passwd_callback_userdata);
  434. } else if (type == SSL_FILETYPE_ASN1) {
  435. reason_code = ERR_R_ASN1_LIB;
  436. pkey = d2i_PrivateKey_bio(in, NULL);
  437. } else {
  438. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  439. goto end;
  440. }
  441. if (pkey == NULL) {
  442. OPENSSL_PUT_ERROR(SSL, reason_code);
  443. goto end;
  444. }
  445. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  446. EVP_PKEY_free(pkey);
  447. end:
  448. BIO_free(in);
  449. return ret;
  450. }
  451. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *d,
  452. long len) {
  453. int ret;
  454. const uint8_t *p;
  455. EVP_PKEY *pkey;
  456. p = d;
  457. pkey = d2i_PrivateKey(type, NULL, &p, (long)len);
  458. if (pkey == NULL) {
  459. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  460. return 0;
  461. }
  462. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  463. EVP_PKEY_free(pkey);
  464. return ret;
  465. }
  466. /* Read a file that contains our certificate in "PEM" format, possibly followed
  467. * by a sequence of CA certificates that should be sent to the peer in the
  468. * Certificate message. */
  469. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) {
  470. BIO *in;
  471. int ret = 0;
  472. X509 *x = NULL;
  473. ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
  474. in = BIO_new(BIO_s_file());
  475. if (in == NULL) {
  476. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  477. goto end;
  478. }
  479. if (BIO_read_filename(in, file) <= 0) {
  480. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  481. goto end;
  482. }
  483. x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
  484. ctx->default_passwd_callback_userdata);
  485. if (x == NULL) {
  486. OPENSSL_PUT_ERROR(SSL, ERR_R_PEM_LIB);
  487. goto end;
  488. }
  489. ret = SSL_CTX_use_certificate(ctx, x);
  490. if (ERR_peek_error() != 0) {
  491. ret = 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
  492. }
  493. if (ret) {
  494. /* If we could set up our certificate, now proceed to the CA
  495. * certificates. */
  496. X509 *ca;
  497. int r;
  498. uint32_t err;
  499. SSL_CTX_clear_chain_certs(ctx);
  500. while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  501. ctx->default_passwd_callback_userdata)) !=
  502. NULL) {
  503. r = SSL_CTX_add0_chain_cert(ctx, ca);
  504. if (!r) {
  505. X509_free(ca);
  506. ret = 0;
  507. goto end;
  508. }
  509. /* Note that we must not free r if it was successfully added to the chain
  510. * (while we must free the main certificate, since its reference count is
  511. * increased by SSL_CTX_use_certificate). */
  512. }
  513. /* When the while loop ends, it's usually just EOF. */
  514. err = ERR_peek_last_error();
  515. if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
  516. ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
  517. ERR_clear_error();
  518. } else {
  519. ret = 0; /* some real error */
  520. }
  521. }
  522. end:
  523. X509_free(x);
  524. BIO_free(in);
  525. return ret;
  526. }
  527. void SSL_set_private_key_method(SSL *ssl,
  528. const SSL_PRIVATE_KEY_METHOD *key_method) {
  529. ssl->cert->key_method = key_method;
  530. }
  531. int ssl_private_key_type(SSL *ssl) {
  532. if (ssl->cert->key_method != NULL) {
  533. return ssl->cert->key_method->type(ssl);
  534. }
  535. return EVP_PKEY_id(ssl->cert->privatekey);
  536. }
  537. int ssl_private_key_supports_digest(SSL *ssl, const EVP_MD *md) {
  538. if (ssl->cert->key_method != NULL) {
  539. return ssl->cert->key_method->supports_digest(ssl, md);
  540. }
  541. return EVP_PKEY_supports_digest(ssl->cert->privatekey, md);
  542. }
  543. size_t ssl_private_key_max_signature_len(SSL *ssl) {
  544. if (ssl->cert->key_method != NULL) {
  545. return ssl->cert->key_method->max_signature_len(ssl);
  546. }
  547. return EVP_PKEY_size(ssl->cert->privatekey);
  548. }
  549. enum ssl_private_key_result_t ssl_private_key_sign(
  550. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, const EVP_MD *md,
  551. const uint8_t *in, size_t in_len) {
  552. if (ssl->cert->key_method != NULL) {
  553. return ssl->cert->key_method->sign(ssl, out, out_len, max_out, md, in,
  554. in_len);
  555. }
  556. enum ssl_private_key_result_t ret = ssl_private_key_failure;
  557. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
  558. if (ctx == NULL) {
  559. goto end;
  560. }
  561. size_t len = max_out;
  562. if (!EVP_PKEY_sign_init(ctx) ||
  563. !EVP_PKEY_CTX_set_signature_md(ctx, md) ||
  564. !EVP_PKEY_sign(ctx, out, &len, in, in_len)) {
  565. goto end;
  566. }
  567. *out_len = len;
  568. ret = ssl_private_key_success;
  569. end:
  570. EVP_PKEY_CTX_free(ctx);
  571. return ret;
  572. }
  573. enum ssl_private_key_result_t ssl_private_key_sign_complete(
  574. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
  575. /* Only custom keys may be asynchronous. */
  576. return ssl->cert->key_method->sign_complete(ssl, out, out_len, max_out);
  577. }