No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

176 líneas
3.5 KiB

  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <openssl/ssl.h>
  10. #include <openssl/bio.h>
  11. static int ssl_read(BIO *bio, char *out, int outl) {
  12. SSL *ssl = bio->ptr;
  13. if (ssl == NULL) {
  14. return 0;
  15. }
  16. BIO_clear_retry_flags(bio);
  17. const int ret = SSL_read(ssl, out, outl);
  18. switch (SSL_get_error(ssl, ret)) {
  19. case SSL_ERROR_WANT_READ:
  20. BIO_set_retry_read(bio);
  21. break;
  22. case SSL_ERROR_WANT_WRITE:
  23. BIO_set_retry_write(bio);
  24. break;
  25. case SSL_ERROR_WANT_ACCEPT:
  26. BIO_set_retry_special(bio);
  27. bio->retry_reason = BIO_RR_ACCEPT;
  28. break;
  29. case SSL_ERROR_WANT_CONNECT:
  30. BIO_set_retry_special(bio);
  31. bio->retry_reason = BIO_RR_CONNECT;
  32. break;
  33. case SSL_ERROR_NONE:
  34. case SSL_ERROR_SYSCALL:
  35. case SSL_ERROR_SSL:
  36. case SSL_ERROR_ZERO_RETURN:
  37. default:
  38. break;
  39. }
  40. return ret;
  41. }
  42. static int ssl_write(BIO *bio, const char *out, int outl) {
  43. SSL *ssl = bio->ptr;
  44. if (ssl == NULL) {
  45. return 0;
  46. }
  47. BIO_clear_retry_flags(bio);
  48. const int ret = SSL_write(ssl, out, outl);
  49. switch (SSL_get_error(ssl, ret)) {
  50. case SSL_ERROR_WANT_WRITE:
  51. BIO_set_retry_write(bio);
  52. break;
  53. case SSL_ERROR_WANT_READ:
  54. BIO_set_retry_read(bio);
  55. break;
  56. case SSL_ERROR_WANT_CONNECT:
  57. BIO_set_retry_special(bio);
  58. bio->retry_reason = BIO_RR_CONNECT;
  59. break;
  60. case SSL_ERROR_NONE:
  61. case SSL_ERROR_SYSCALL:
  62. case SSL_ERROR_SSL:
  63. default:
  64. break;
  65. }
  66. return ret;
  67. }
  68. static long ssl_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  69. SSL *ssl = bio->ptr;
  70. if (ssl == NULL && cmd != BIO_C_SET_SSL) {
  71. return 0;
  72. }
  73. switch (cmd) {
  74. case BIO_C_SET_SSL:
  75. bio->shutdown = num;
  76. bio->ptr = ptr;
  77. bio->init = 1;
  78. return 1;
  79. case BIO_CTRL_GET_CLOSE:
  80. return bio->shutdown;
  81. case BIO_CTRL_SET_CLOSE:
  82. bio->shutdown = num;
  83. return 1;
  84. case BIO_CTRL_WPENDING:
  85. return BIO_ctrl(SSL_get_wbio(ssl), cmd, num, ptr);
  86. case BIO_CTRL_PENDING:
  87. return SSL_pending(ssl);
  88. case BIO_CTRL_FLUSH: {
  89. BIO_clear_retry_flags(bio);
  90. long ret = BIO_ctrl(SSL_get_wbio(ssl), cmd, num, ptr);
  91. BIO_copy_next_retry(bio);
  92. return ret;
  93. }
  94. case BIO_CTRL_PUSH:
  95. case BIO_CTRL_POP:
  96. case BIO_CTRL_DUP:
  97. return -1;
  98. default:
  99. return BIO_ctrl(SSL_get_rbio(ssl), cmd, num, ptr);
  100. }
  101. }
  102. static int ssl_new(BIO *bio) {
  103. return 1;
  104. }
  105. static int ssl_free(BIO *bio) {
  106. SSL *ssl = bio->ptr;
  107. if (ssl == NULL) {
  108. return 1;
  109. }
  110. SSL_shutdown(ssl);
  111. if (bio->shutdown) {
  112. SSL_free(ssl);
  113. }
  114. return 1;
  115. }
  116. static long ssl_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  117. SSL *ssl = bio->ptr;
  118. if (ssl == NULL) {
  119. return 0;
  120. }
  121. switch (cmd) {
  122. case BIO_CTRL_SET_CALLBACK:
  123. return -1;
  124. default:
  125. return BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp);
  126. }
  127. }
  128. static const BIO_METHOD ssl_method = {
  129. BIO_TYPE_SSL, "SSL", ssl_write, ssl_read, NULL,
  130. NULL, ssl_ctrl, ssl_new, ssl_free, ssl_callback_ctrl,
  131. };
  132. const BIO_METHOD *BIO_f_ssl(void) { return &ssl_method; }
  133. long BIO_set_ssl(BIO *bio, SSL *ssl, int take_owership) {
  134. return BIO_ctrl(bio, BIO_C_SET_SSL, take_owership, ssl);
  135. }