| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/ssl.h> |
| 16 |
| 17 | #include <openssl/bio.h> |
| 18 |
| 19 |
| 20 | static SSL *get_ssl(BIO *bio) { return reinterpret_cast<SSL *>(bio->ptr); } |
| 21 |
| 22 | static int ssl_read(BIO *bio, char *out, int outl) { |
| 23 | SSL *ssl = get_ssl(bio); |
| 24 | if (ssl == NULL) { |
| 25 | return 0; |
| 26 | } |
| 27 |
| 28 | BIO_clear_retry_flags(bio); |
| 29 |
| 30 | const int ret = SSL_read(ssl, out, outl); |
| 31 |
| 32 | switch (SSL_get_error(ssl, ret)) { |
| 33 | case SSL_ERROR_WANT_READ: |
| 34 | BIO_set_retry_read(bio); |
| 35 | break; |
| 36 |
| 37 | case SSL_ERROR_WANT_WRITE: |
| 38 | BIO_set_retry_write(bio); |
| 39 | break; |
| 40 |
| 41 | case SSL_ERROR_WANT_ACCEPT: |
| 42 | BIO_set_retry_special(bio); |
| 43 | BIO_set_retry_reason(bio, BIO_RR_ACCEPT); |
| 44 | break; |
| 45 |
| 46 | case SSL_ERROR_WANT_CONNECT: |
| 47 | BIO_set_retry_special(bio); |
| 48 | BIO_set_retry_reason(bio, BIO_RR_CONNECT); |
| 49 | break; |
| 50 |
| 51 | case SSL_ERROR_NONE: |
| 52 | case SSL_ERROR_SYSCALL: |
| 53 | case SSL_ERROR_SSL: |
| 54 | case SSL_ERROR_ZERO_RETURN: |
| 55 | default: |
| 56 | break; |
| 57 | } |
| 58 |
| 59 | return ret; |
| 60 | } |
| 61 |
| 62 | static int ssl_write(BIO *bio, const char *out, int outl) { |
| 63 | SSL *ssl = get_ssl(bio); |
| 64 | if (ssl == NULL) { |
| 65 | return 0; |
| 66 | } |
| 67 |
| 68 | BIO_clear_retry_flags(bio); |
| 69 |
| 70 | const int ret = SSL_write(ssl, out, outl); |
| 71 |
| 72 | switch (SSL_get_error(ssl, ret)) { |
| 73 | case SSL_ERROR_WANT_WRITE: |
| 74 | BIO_set_retry_write(bio); |
| 75 | break; |
| 76 |
| 77 | case SSL_ERROR_WANT_READ: |
| 78 | BIO_set_retry_read(bio); |
| 79 | break; |
| 80 |
| 81 | case SSL_ERROR_WANT_CONNECT: |
| 82 | BIO_set_retry_special(bio); |
| 83 | BIO_set_retry_reason(bio, BIO_RR_CONNECT); |
| 84 | break; |
| 85 |
| 86 | case SSL_ERROR_NONE: |
| 87 | case SSL_ERROR_SYSCALL: |
| 88 | case SSL_ERROR_SSL: |
| 89 | default: |
| 90 | break; |
| 91 | } |
| 92 |
| 93 | return ret; |
| 94 | } |
| 95 |
| 96 | static long ssl_ctrl(BIO *bio, int cmd, long num, void *ptr) { |
| 97 | SSL *ssl = get_ssl(bio); |
| 98 | if (ssl == NULL && cmd != BIO_C_SET_SSL) { |
| 99 | return 0; |
| 100 | } |
| 101 |
| 102 | switch (cmd) { |
| 103 | case BIO_C_SET_SSL: |
| 104 | if (ssl != NULL) { |
| 105 | // OpenSSL allows reusing an SSL BIO with a different SSL object. We do |
| 106 | // not support this. |
| 107 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 108 | return 0; |
| 109 | } |
| 110 |
| 111 | // Note this differs from upstream OpenSSL, which synchronizes |
| 112 | // |bio->next_bio| with |ssl|'s rbio here, and on |BIO_CTRL_PUSH|. We call |
| 113 | // into the corresponding |BIO| directly. (We can implement the upstream |
| 114 | // behavior if it ends up necessary.) |
| 115 | bio->shutdown = static_cast<int>(num); |
| 116 | bio->ptr = ptr; |
| 117 | bio->init = 1; |
| 118 | return 1; |
| 119 |
| 120 | case BIO_CTRL_GET_CLOSE: |
| 121 | return bio->shutdown; |
| 122 |
| 123 | case BIO_CTRL_SET_CLOSE: |
| 124 | bio->shutdown = static_cast<int>(num); |
| 125 | return 1; |
| 126 |
| 127 | case BIO_CTRL_WPENDING: |
| 128 | return BIO_ctrl(SSL_get_wbio(ssl), cmd, num, ptr); |
| 129 |
| 130 | case BIO_CTRL_PENDING: |
| 131 | return SSL_pending(ssl); |
| 132 |
| 133 | case BIO_CTRL_FLUSH: { |
| 134 | BIO *wbio = SSL_get_wbio(ssl); |
| 135 | BIO_clear_retry_flags(bio); |
| 136 | long ret = BIO_ctrl(wbio, cmd, num, ptr); |
| 137 | BIO_set_flags(bio, BIO_get_retry_flags(wbio)); |
| 138 | BIO_set_retry_reason(bio, BIO_get_retry_reason(wbio)); |
| 139 | return ret; |
| 140 | } |
| 141 |
| 142 | case BIO_CTRL_PUSH: |
| 143 | case BIO_CTRL_POP: |
| 144 | case BIO_CTRL_DUP: |
| 145 | return -1; |
| 146 |
| 147 | default: |
| 148 | return BIO_ctrl(SSL_get_rbio(ssl), cmd, num, ptr); |
| 149 | } |
| 150 | } |
| 151 |
| 152 | static int ssl_new(BIO *bio) { return 1; } |
| 153 |
| 154 | static int ssl_free(BIO *bio) { |
| 155 | SSL *ssl = get_ssl(bio); |
| 156 |
| 157 | if (ssl == NULL) { |
| 158 | return 1; |
| 159 | } |
| 160 |
| 161 | SSL_shutdown(ssl); |
| 162 | if (bio->shutdown) { |
| 163 | SSL_free(ssl); |
| 164 | } |
| 165 |
| 166 | return 1; |
| 167 | } |
| 168 |
| 169 | static long ssl_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) { |
| 170 | SSL *ssl = get_ssl(bio); |
| 171 | if (ssl == NULL) { |
| 172 | return 0; |
| 173 | } |
| 174 |
| 175 | switch (cmd) { |
| 176 | case BIO_CTRL_SET_CALLBACK: |
| 177 | return -1; |
| 178 |
| 179 | default: |
| 180 | return BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp); |
| 181 | } |
| 182 | } |
| 183 |
| 184 | static const BIO_METHOD ssl_method = { |
| 185 | BIO_TYPE_SSL, "SSL", ssl_write, ssl_read, NULL, |
| 186 | NULL, ssl_ctrl, ssl_new, ssl_free, ssl_callback_ctrl, |
| 187 | }; |
| 188 |
| 189 | const BIO_METHOD *BIO_f_ssl(void) { return &ssl_method; } |
| 190 |
| 191 | long BIO_set_ssl(BIO *bio, SSL *ssl, int take_owership) { |
| 192 | return BIO_ctrl(bio, BIO_C_SET_SSL, take_owership, ssl); |
| 193 | } |
| 194 |