BIO *BIO_new_file(const char *filename, const char *mode) { BIO *ret; FILE *file; file = fopen(filename, mode); if (file == NULL) { OPENSSL_PUT_SYSTEM_ERROR(); ERR_add_error_data(5, "fopen('", filename, "','", mode, "')"); if (errno == ENOENT) { OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE); } else { OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); } return NULL; } ret = BIO_new(BIO_s_file()); if (ret == NULL) { fclose(file); return NULL; } BIO_set_fp(ret, file, BIO_CLOSE); return ret; }
static int file_read(BIO *b, char *out, int outl) { if (!b->init) { return 0; } size_t ret = fread(out, 1, outl, (FILE *)b->ptr); if (ret == 0 && ferror((FILE *)b->ptr)) { OPENSSL_PUT_SYSTEM_ERROR(); OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); return -1; } /* fread reads at most |outl| bytes, so |ret| fits in an int. */ return (int)ret; }
static int file_read(BIO *b, char *out, int outl) { int ret = 0; if (!b->init) { return 0; } ret = fread(out, 1, outl, (FILE *)b->ptr); if (ret == 0 && ferror((FILE *)b->ptr)) { OPENSSL_PUT_SYSTEM_ERROR(fread); OPENSSL_PUT_ERROR(BIO, file_read, ERR_R_SYS_LIB); ret = -1; } return ret; }
int bio_ip_and_port_to_socket_and_addr(int *out_sock, struct sockaddr_storage *out_addr, socklen_t *out_addr_length, const char *hostname, const char *port_str) { struct addrinfo hint, *result, *cur; int ret; *out_sock = -1; memset(&hint, 0, sizeof(hint)); hint.ai_family = AF_UNSPEC; hint.ai_socktype = SOCK_STREAM; ret = getaddrinfo(hostname, port_str, &hint, &result); if (ret != 0) { OPENSSL_PUT_ERROR(SYS, 0); ERR_add_error_data(1, gai_strerror(ret)); return 0; } ret = 0; for (cur = result; cur; cur = cur->ai_next) { if (cur->ai_addrlen > sizeof(struct sockaddr_storage)) { continue; } memset(out_addr, 0, sizeof(struct sockaddr_storage)); memcpy(out_addr, cur->ai_addr, cur->ai_addrlen); *out_addr_length = cur->ai_addrlen; *out_sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); if (*out_sock < 0) { OPENSSL_PUT_SYSTEM_ERROR(socket); goto out; } ret = 1; break; } out: freeaddrinfo(result); return ret; }
static int conn_state(BIO *bio, BIO_CONNECT *c) { int ret = -1, i; int (*cb)(const BIO *, int, int) = NULL; if (c->info_callback != NULL) { cb = c->info_callback; } for (;;) { switch (c->state) { case BIO_CONN_S_BEFORE: /* If there's a hostname and a port, assume that both are * exactly what they say. If there is only a hostname, try * (just once) to split it into a hostname and port. */ if (c->param_hostname == NULL) { OPENSSL_PUT_ERROR(BIO, BIO_R_NO_HOSTNAME_SPECIFIED); goto exit_loop; } if (c->param_port == NULL) { char *host, *port; if (!split_host_and_port(&host, &port, c->param_hostname) || port == NULL) { OPENSSL_free(host); OPENSSL_free(port); OPENSSL_PUT_ERROR(BIO, BIO_R_NO_PORT_SPECIFIED); ERR_add_error_data(2, "host=", c->param_hostname); goto exit_loop; } OPENSSL_free(c->param_port); c->param_port = port; OPENSSL_free(c->param_hostname); c->param_hostname = host; } if (!bio_ip_and_port_to_socket_and_addr( &bio->num, &c->them, &c->them_length, c->param_hostname, c->param_port)) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNABLE_TO_CREATE_SOCKET); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); goto exit_loop; } if (c->nbio) { if (!bio_socket_nbio(bio->num, 1)) { OPENSSL_PUT_ERROR(BIO, BIO_R_ERROR_SETTING_NBIO); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); goto exit_loop; } } i = 1; ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i)); if (ret < 0) { OPENSSL_PUT_SYSTEM_ERROR(); OPENSSL_PUT_ERROR(BIO, BIO_R_KEEPALIVE); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); goto exit_loop; } BIO_clear_retry_flags(bio); ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length); if (ret < 0) { if (bio_fd_should_retry(ret)) { BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY)); c->state = BIO_CONN_S_BLOCKED_CONNECT; bio->retry_reason = BIO_RR_CONNECT; } else { OPENSSL_PUT_SYSTEM_ERROR(); OPENSSL_PUT_ERROR(BIO, BIO_R_CONNECT_ERROR); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); } goto exit_loop; } else { c->state = BIO_CONN_S_OK; } break; case BIO_CONN_S_BLOCKED_CONNECT: i = bio_sock_error(bio->num); if (i) { if (bio_fd_should_retry(ret)) { BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY)); c->state = BIO_CONN_S_BLOCKED_CONNECT; bio->retry_reason = BIO_RR_CONNECT; ret = -1; } else { BIO_clear_retry_flags(bio); OPENSSL_PUT_SYSTEM_ERROR(); OPENSSL_PUT_ERROR(BIO, BIO_R_NBIO_CONNECT_ERROR); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); ret = 0; } goto exit_loop; } else { c->state = BIO_CONN_S_OK; } break; case BIO_CONN_S_OK: ret = 1; goto exit_loop; default: assert(0); goto exit_loop; } if (cb != NULL) { ret = cb((BIO *)bio, c->state, ret); if (ret == 0) { goto end; } } } exit_loop: if (cb != NULL) { ret = cb((BIO *)bio, c->state, ret); } end: return ret; }
static int conn_state(BIO *bio, BIO_CONNECT *c) { int ret = -1, i; char *p, *q; int (*cb)(const BIO *, int, int) = NULL; if (c->info_callback != NULL) { cb = c->info_callback; } for (;;) { switch (c->state) { case BIO_CONN_S_BEFORE: p = c->param_hostname; if (p == NULL) { OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_HOSTNAME_SPECIFIED); goto exit_loop; } for (; *p != 0; p++) { if (*p == ':' || *p == '/') { break; } } i = *p; if (i == ':' || i == '/') { *(p++) = 0; if (i == ':') { for (q = p; *q; q++) { if (*q == '/') { *q = 0; break; } } if (c->param_port != NULL) { OPENSSL_free(c->param_port); } c->param_port = BUF_strdup(p); } } if (c->param_port == NULL) { OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_PORT_SPECIFIED); ERR_add_error_data(2, "host=", c->param_hostname); goto exit_loop; } if (!bio_ip_and_port_to_socket_and_addr( &bio->num, &c->them, &c->them_length, c->param_hostname, c->param_port)) { OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_UNABLE_TO_CREATE_SOCKET); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); goto exit_loop; } memset(c->ip, 0, 4); maybe_copy_ipv4_address(c->ip, &c->them); if (c->nbio) { if (!bio_socket_nbio(bio->num, 1)) { OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_ERROR_SETTING_NBIO); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); goto exit_loop; } } i = 1; ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i)); if (ret < 0) { OPENSSL_PUT_SYSTEM_ERROR(setsockopt); OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_KEEPALIVE); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); goto exit_loop; } BIO_clear_retry_flags(bio); ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length); if (ret < 0) { if (bio_fd_should_retry(ret)) { BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY)); c->state = BIO_CONN_S_BLOCKED_CONNECT; bio->retry_reason = BIO_RR_CONNECT; } else { OPENSSL_PUT_SYSTEM_ERROR(connect); OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_CONNECT_ERROR); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); } goto exit_loop; } else { c->state = BIO_CONN_S_OK; } break; case BIO_CONN_S_BLOCKED_CONNECT: i = bio_sock_error(bio->num); if (i) { if (bio_fd_should_retry(ret)) { BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY)); c->state = BIO_CONN_S_BLOCKED_CONNECT; bio->retry_reason = BIO_RR_CONNECT; ret = -1; } else { BIO_clear_retry_flags(bio); OPENSSL_PUT_SYSTEM_ERROR(connect); OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NBIO_CONNECT_ERROR); ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); ret = 0; } goto exit_loop; } else { c->state = BIO_CONN_S_OK; } break; case BIO_CONN_S_OK: ret = 1; goto exit_loop; default: assert(0); goto exit_loop; } if (cb != NULL) { ret = cb((BIO *)bio, c->state, ret); if (ret == 0) { goto end; } } } exit_loop: if (cb != NULL) { ret = cb((BIO *)bio, c->state, ret); } end: return ret; }
static long file_ctrl(BIO *b, int cmd, long num, void *ptr) { long ret = 1; FILE *fp = (FILE *)b->ptr; FILE **fpp; char p[4]; switch (cmd) { case BIO_CTRL_RESET: num = 0; case BIO_C_FILE_SEEK: ret = (long)fseek(fp, num, 0); break; case BIO_CTRL_EOF: ret = (long)feof(fp); break; case BIO_C_FILE_TELL: case BIO_CTRL_INFO: ret = ftell(fp); break; case BIO_C_SET_FILE_PTR: file_free(b); b->shutdown = (int)num & BIO_CLOSE; b->ptr = ptr; b->init = 1; break; case BIO_C_SET_FILENAME: file_free(b); b->shutdown = (int)num & BIO_CLOSE; if (num & BIO_FP_APPEND) { if (num & BIO_FP_READ) { BUF_strlcpy(p, "a+", sizeof(p)); } else { BUF_strlcpy(p, "a", sizeof(p)); } } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) { BUF_strlcpy(p, "r+", sizeof(p)); } else if (num & BIO_FP_WRITE) { BUF_strlcpy(p, "w", sizeof(p)); } else if (num & BIO_FP_READ) { BUF_strlcpy(p, "r", sizeof(p)); } else { OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE); ret = 0; break; } fp = fopen(ptr, p); if (fp == NULL) { OPENSSL_PUT_SYSTEM_ERROR(); ERR_add_error_data(5, "fopen('", ptr, "','", p, "')"); OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); ret = 0; break; } b->ptr = fp; b->init = 1; break; case BIO_C_GET_FILE_PTR: /* the ptr parameter is actually a FILE ** in this case. */ if (ptr != NULL) { fpp = (FILE **)ptr; *fpp = (FILE *)b->ptr; } break; case BIO_CTRL_GET_CLOSE: ret = (long)b->shutdown; break; case BIO_CTRL_SET_CLOSE: b->shutdown = (int)num; break; case BIO_CTRL_FLUSH: ret = 0 == fflush((FILE *)b->ptr); break; case BIO_CTRL_WPENDING: case BIO_CTRL_PENDING: default: ret = 0; break; } return ret; }