| 1 |
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
|
| 6 |
#include <openssl/bio.h> |
| 7 |
|
| 8 |
// We intentionally redefine |iovec| here without header guards or redefinition |
| 9 |
// protection to prevent "bio.h" from inadvertently including system headers |
| 10 |
// (like <sys/socket.h>) in consuming applications. This avoids potential |
| 11 |
// conflicts where system headers might define types that interfere with the |
| 12 |
// consumer's code. Consumers should ideally handle potential struct |
| 13 |
// redefinitions themselves, but unfortunately most legacy codebases do not |
| 14 |
// implement such checks, making this approach necessary for compatibility. |
| 15 |
// |
| 16 |
// See commit aws-lc@9db959e for more details. |
| 17 |
struct iovec { |
| 18 |
void* iov_base; |
| 19 |
size_t iov_len; |
| 20 |
}; |
| 21 |
|
| 22 |
static SSL *get_ssl(BIO *bio) { |
| 23 |
return reinterpret_cast<SSL *>(bio->ptr); |
| 24 |
} |
| 25 |
|
| 26 |
static int ssl_read(BIO *bio, char *out, int outl) { |
| 27 |
SSL *ssl = get_ssl(bio); |
| 28 |
if (ssl == NULL) { |
| 29 |
return 0; |
| 30 |
} |
| 31 |
|
| 32 |
BIO_clear_retry_flags(bio); |
| 33 |
|
| 34 |
const int ret = SSL_read(ssl, out, outl); |
| 35 |
|
| 36 |
switch (SSL_get_error(ssl, ret)) { |
| 37 |
case SSL_ERROR_WANT_READ: |
| 38 |
BIO_set_retry_read(bio); |
| 39 |
break; |
| 40 |
|
| 41 |
case SSL_ERROR_WANT_WRITE: |
| 42 |
BIO_set_retry_write(bio); |
| 43 |
break; |
| 44 |
|
| 45 |
case SSL_ERROR_WANT_ACCEPT: |
| 46 |
BIO_set_retry_special(bio); |
| 47 |
BIO_set_retry_reason(bio, BIO_RR_ACCEPT); |
| 48 |
break; |
| 49 |
|
| 50 |
case SSL_ERROR_WANT_CONNECT: |
| 51 |
BIO_set_retry_special(bio); |
| 52 |
BIO_set_retry_reason(bio, BIO_RR_CONNECT); |
| 53 |
break; |
| 54 |
|
| 55 |
case SSL_ERROR_NONE: |
| 56 |
case SSL_ERROR_SYSCALL: |
| 57 |
case SSL_ERROR_SSL: |
| 58 |
case SSL_ERROR_ZERO_RETURN: |
| 59 |
default: |
| 60 |
break; |
| 61 |
} |
| 62 |
|
| 63 |
return ret; |
| 64 |
} |
| 65 |
|
| 66 |
static int ssl_write(BIO *bio, const char *out, int outl) { |
| 67 |
SSL *ssl = get_ssl(bio); |
| 68 |
if (ssl == NULL) { |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
BIO_clear_retry_flags(bio); |
| 73 |
|
| 74 |
const int ret = SSL_write(ssl, out, outl); |
| 75 |
|
| 76 |
switch (SSL_get_error(ssl, ret)) { |
| 77 |
case SSL_ERROR_WANT_WRITE: |
| 78 |
BIO_set_retry_write(bio); |
| 79 |
break; |
| 80 |
|
| 81 |
case SSL_ERROR_WANT_READ: |
| 82 |
BIO_set_retry_read(bio); |
| 83 |
break; |
| 84 |
|
| 85 |
case SSL_ERROR_WANT_CONNECT: |
| 86 |
BIO_set_retry_special(bio); |
| 87 |
BIO_set_retry_reason(bio, BIO_RR_CONNECT); |
| 88 |
break; |
| 89 |
|
| 90 |
case SSL_ERROR_NONE: |
| 91 |
case SSL_ERROR_SYSCALL: |
| 92 |
case SSL_ERROR_SSL: |
| 93 |
default: |
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
return ret; |
| 98 |
} |
| 99 |
|
| 100 |
static long ssl_ctrl(BIO *bio, int cmd, long num, void *ptr) { |
| 101 |
SSL *ssl = get_ssl(bio); |
| 102 |
if (ssl == NULL && cmd != BIO_C_SET_SSL) { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
switch (cmd) { |
| 107 |
case BIO_C_SET_SSL: |
| 108 |
if (ssl != NULL) { |
| 109 |
// OpenSSL allows reusing an SSL BIO with a different SSL object. We do |
| 110 |
// not support this. |
| 111 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
// Note this differs from upstream OpenSSL, which synchronizes |
| 116 |
// |bio->next_bio| with |ssl|'s rbio here, and on |BIO_CTRL_PUSH|. We call |
| 117 |
// into the corresponding |BIO| directly. (We can implement the upstream |
| 118 |
// behavior if it ends up necessary.) |
| 119 |
bio->shutdown = static_cast<int>(num); |
| 120 |
bio->ptr = ptr; |
| 121 |
bio->init = 1; |
| 122 |
return 1; |
| 123 |
|
| 124 |
case BIO_C_GET_SSL: |
| 125 |
if (ptr != nullptr) { |
| 126 |
auto sslp = static_cast<SSL **>(ptr); |
| 127 |
*sslp = ssl; |
| 128 |
return 1; |
| 129 |
} |
| 130 |
return 0; |
| 131 |
|
| 132 |
case BIO_CTRL_GET_CLOSE: |
| 133 |
return bio->shutdown; |
| 134 |
|
| 135 |
case BIO_CTRL_SET_CLOSE: |
| 136 |
bio->shutdown = static_cast<int>(num); |
| 137 |
return 1; |
| 138 |
|
| 139 |
case BIO_CTRL_WPENDING: { |
| 140 |
BIO *wbio = SSL_get_wbio(ssl); |
| 141 |
if (wbio == NULL) { |
| 142 |
return 0; |
| 143 |
} |
| 144 |
return BIO_ctrl(wbio, cmd, num, ptr); |
| 145 |
} |
| 146 |
|
| 147 |
case BIO_CTRL_PENDING: |
| 148 |
return SSL_pending(ssl); |
| 149 |
|
| 150 |
case BIO_CTRL_FLUSH: { |
| 151 |
BIO *wbio = SSL_get_wbio(ssl); |
| 152 |
if (wbio == NULL) { |
| 153 |
return 0; |
| 154 |
} |
| 155 |
BIO_clear_retry_flags(bio); |
| 156 |
long ret = BIO_ctrl(wbio, cmd, num, ptr); |
| 157 |
BIO_set_flags(bio, BIO_get_retry_flags(wbio)); |
| 158 |
BIO_set_retry_reason(bio, BIO_get_retry_reason(wbio)); |
| 159 |
return ret; |
| 160 |
} |
| 161 |
|
| 162 |
case BIO_CTRL_PUSH: |
| 163 |
case BIO_CTRL_POP: |
| 164 |
case BIO_CTRL_DUP: |
| 165 |
return -1; |
| 166 |
|
| 167 |
default: { |
| 168 |
BIO *rbio = SSL_get_rbio(ssl); |
| 169 |
if (rbio == NULL) { |
| 170 |
return 0; |
| 171 |
} |
| 172 |
return BIO_ctrl(rbio, cmd, num, ptr); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static int ssl_new(BIO *bio) { |
| 178 |
return 1; |
| 179 |
} |
| 180 |
|
| 181 |
static int ssl_free(BIO *bio) { |
| 182 |
SSL *ssl = get_ssl(bio); |
| 183 |
|
| 184 |
if (ssl == NULL) { |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
SSL_shutdown(ssl); |
| 189 |
if (bio->shutdown) { |
| 190 |
SSL_free(ssl); |
| 191 |
} |
| 192 |
bio->ptr = NULL; |
| 193 |
|
| 194 |
return 1; |
| 195 |
} |
| 196 |
|
| 197 |
static long ssl_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) { |
| 198 |
SSL *ssl = get_ssl(bio); |
| 199 |
if (ssl == NULL) { |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
switch (cmd) { |
| 204 |
case BIO_CTRL_SET_CALLBACK: |
| 205 |
return -1; |
| 206 |
|
| 207 |
default: { |
| 208 |
BIO *rbio = SSL_get_rbio(ssl); |
| 209 |
if (rbio == NULL) { |
| 210 |
return 0; |
| 211 |
} |
| 212 |
return BIO_callback_ctrl(rbio, cmd, fp); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
static const BIO_METHOD ssl_method = { |
| 218 |
BIO_TYPE_SSL, "SSL", ssl_write, ssl_read, NULL, |
| 219 |
NULL, ssl_ctrl, ssl_new, ssl_free, ssl_callback_ctrl, |
| 220 |
}; |
| 221 |
|
| 222 |
const BIO_METHOD *BIO_f_ssl(void) { return &ssl_method; } |
| 223 |
|
| 224 |
long BIO_set_ssl(BIO *bio, SSL *ssl, int take_owership) { |
| 225 |
return BIO_ctrl(bio, BIO_C_SET_SSL, take_owership, ssl); |
| 226 |
} |
| 227 |
|
| 228 |
long BIO_get_ssl(BIO *bio, SSL **ssl) { |
| 229 |
return BIO_ctrl(bio, BIO_C_GET_SSL, 0, ssl); |
| 230 |
} |
| 231 |
|
| 232 |
#if !defined(OPENSSL_NO_SOCK) |
| 233 |
BIO *BIO_new_ssl_connect(SSL_CTX *ctx) { |
| 234 |
bssl::UniquePtr<BIO> con(BIO_new(BIO_s_connect())); |
| 235 |
bssl::UniquePtr<BIO> ssl(BIO_new_ssl(ctx, 1)); |
| 236 |
if (!con || !ssl) { |
| 237 |
return nullptr; |
| 238 |
} |
| 239 |
bssl::UniquePtr<BIO> ret(BIO_push(ssl.get(), con.get())); |
| 240 |
if (!ret) { |
| 241 |
return nullptr; |
| 242 |
} |
| 243 |
|
| 244 |
con.release(); |
| 245 |
ssl.release(); |
| 246 |
return ret.release(); |
| 247 |
} |
| 248 |
#endif // !OPENSSL_NO_SOCK |
| 249 |
|
| 250 |
BIO *BIO_new_ssl(SSL_CTX *ctx, int client) { |
| 251 |
bssl::UniquePtr<BIO> ret(BIO_new(BIO_f_ssl())); |
| 252 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx)); |
| 253 |
|
| 254 |
if (!ret || !ssl) { |
| 255 |
return nullptr; |
| 256 |
} |
| 257 |
if (client) { |
| 258 |
SSL_set_connect_state(ssl.get()); |
| 259 |
} else { |
| 260 |
SSL_set_accept_state(ssl.get()); |
| 261 |
} |
| 262 |
|
| 263 |
if (BIO_set_ssl(ret.get(), ssl.get(), BIO_CLOSE) <= 0) { |
| 264 |
return nullptr; |
| 265 |
} |
| 266 |
ssl.release(); // Ownership transferred to BIO |
| 267 |
return ret.release(); |
| 268 |
} |