Skip to content

Commit

Permalink
Refactoring BIO: Adapt BIO_s_datagram and all that depends on it
Browse files Browse the repository at this point in the history
The control commands that previously took a struct sockaddr * have
been changed to take a BIO_ADDR * instead.

Reviewed-by: Kurt Roeckx <kurt@openssl.org>
  • Loading branch information
levitte committed Feb 3, 2016
1 parent 75d5bd4 commit d858c87
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 182 deletions.
60 changes: 18 additions & 42 deletions apps/s_cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,14 +737,9 @@ int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
unsigned int *cookie_len)
{
unsigned char *buffer;
unsigned int length;
union {
struct sockaddr sa;
struct sockaddr_in s4;
#if OPENSSL_USE_IPV6
struct sockaddr_in6 s6;
#endif
} peer;
size_t length;
unsigned short port;
BIO_ADDR *peer = NULL;

/* Initialize a random secret */
if (!cookie_initialized) {
Expand All @@ -755,50 +750,31 @@ int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
cookie_initialized = 1;
}

peer = BIO_ADDR_new();
if (peer == NULL) {
BIO_printf(bio_err, "memory full\n");
return 0;
}

/* Read peer information */
(void)BIO_dgram_get_peer(SSL_get_rbio(ssl), &peer);
(void)BIO_dgram_get_peer(SSL_get_rbio(ssl), peer);

/* Create buffer with peer's address and port */
length = 0;
switch (peer.sa.sa_family) {
case AF_INET:
length += sizeof(struct in_addr);
length += sizeof(peer.s4.sin_port);
break;
#if OPENSSL_USE_IPV6
case AF_INET6:
length += sizeof(struct in6_addr);
length += sizeof(peer.s6.sin6_port);
break;
#endif
default:
OPENSSL_assert(0);
break;
}
BIO_ADDR_rawaddress(peer, NULL, &length);
OPENSSL_assert(length != 0);
port = BIO_ADDR_rawport(peer);
length += sizeof(port);
buffer = app_malloc(length, "cookie generate buffer");

switch (peer.sa.sa_family) {
case AF_INET:
memcpy(buffer, &peer.s4.sin_port, sizeof(peer.s4.sin_port));
memcpy(buffer + sizeof(peer.s4.sin_port),
&peer.s4.sin_addr, sizeof(struct in_addr));
break;
#if OPENSSL_USE_IPV6
case AF_INET6:
memcpy(buffer, &peer.s6.sin6_port, sizeof(peer.s6.sin6_port));
memcpy(buffer + sizeof(peer.s6.sin6_port),
&peer.s6.sin6_addr, sizeof(struct in6_addr));
break;
#endif
default:
OPENSSL_assert(0);
break;
}
memcpy(buffer, &port, sizeof(port));
BIO_ADDR_rawaddress(peer, buffer + sizeof(port), NULL);

/* Calculate HMAC of buffer using the secret */
HMAC(EVP_sha1(), cookie_secret, COOKIE_SECRET_LENGTH,
buffer, length, cookie, cookie_len);

OPENSSL_free(buffer);
BIO_ADDR_free(peer);

return 1;
}
Expand Down
14 changes: 9 additions & 5 deletions apps/s_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2432,12 +2432,15 @@ static int init_ssl_connection(SSL *con)
unsigned next_proto_neg_len;
#endif
unsigned char *exportedkeymat;
#ifndef OPENSSL_NO_DTLS
struct sockaddr_storage client;
#endif

#ifndef OPENSSL_NO_DTLS
if(dtlslisten) {
BIO_ADDR *client = NULL;

if ((client = BIO_ADDR_new()) == NULL) {
BIO_printf(bio_err, "ERROR - memory\n");
return 0;
}
i = DTLSv1_listen(con, &client);
if (i > 0) {
BIO *wbio;
Expand All @@ -2448,11 +2451,12 @@ static int init_ssl_connection(SSL *con)
BIO_get_fd(wbio, &fd);
}

if(!wbio || connect(fd, (struct sockaddr *)&client,
sizeof(struct sockaddr_storage))) {
if(!wbio || BIO_connect(fd, client, 0) == 0) {
BIO_printf(bio_err, "ERROR - unable to connect\n");
BIO_ADDR_free(client);
return 0;
}
BIO_ADDR_free(client);
dtlslisten = 0;
i = SSL_accept(con);
}
Expand Down

0 comments on commit d858c87

Please sign in to comment.