Skip to content

Commit

Permalink
Add support for TLS SNI
Browse files Browse the repository at this point in the history
Signed-off-by: Augustin Cavalier <waddlesplash@gmail.com>
  • Loading branch information
markhellegers authored and waddlesplash committed Jun 20, 2016
1 parent 82f44f2 commit e1c98ce
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
1 change: 1 addition & 0 deletions headers/os/net/NetworkAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class BNetworkAddress : public BFlattenable {
private:
sockaddr_storage fAddress;
status_t fStatus;
BString fHostName;
};


Expand Down
4 changes: 2 additions & 2 deletions headers/os/net/SecureSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class BSecureSocket : public BSocket {
virtual ssize_t Write(const void* buffer, size_t size);

protected:
status_t _SetupCommon();
status_t _SetupConnect();
status_t _SetupCommon(const char* host = NULL);
status_t _SetupConnect(const char* host = NULL);
status_t _SetupAccept();

private:
Expand Down
40 changes: 22 additions & 18 deletions src/kits/network/libnetapi/NetworkAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ BNetworkAddress::BNetworkAddress(const in6_addr& address, uint16 port)
BNetworkAddress::BNetworkAddress(const BNetworkAddress& other)
:
fAddress(other.fAddress),
fStatus(other.fStatus)
fStatus(other.fStatus),
fHostName(other.fHostName)
{
}

Expand All @@ -151,6 +152,7 @@ BNetworkAddress::Unset()
{
fAddress.ss_family = AF_UNSPEC;
fAddress.ss_len = 2;
fHostName = "";
fStatus = B_OK;
}

Expand All @@ -170,15 +172,13 @@ BNetworkAddress::SetTo(const char* host, uint16 port, uint32 flags)

uint32 cookie = 0;
status = resolver->GetNextAddress(AF_INET6, &cookie, *this);
if (status == B_OK) {
fStatus = B_OK;
return B_OK;
if (status != B_OK) {
cookie = 0;
status = resolver->GetNextAddress(&cookie, *this);
if (status != B_OK)
Unset();
}

cookie = 0;
status = resolver->GetNextAddress(&cookie, *this);
if (status != B_OK)
Unset();
fHostName = host;
fStatus = status;
return status;
}
Expand All @@ -199,15 +199,13 @@ BNetworkAddress::SetTo(const char* host, const char* service, uint32 flags)

uint32 cookie = 0;
status = resolver->GetNextAddress(AF_INET6, &cookie, *this);
if (status == B_OK) {
fStatus = B_OK;
return B_OK;
if (status != B_OK) {
cookie = 0;
status = resolver->GetNextAddress(&cookie, *this);
if (status != B_OK)
Unset();
}

cookie = 0;
status = resolver->GetNextAddress(&cookie, *this);
if (status != B_OK)
Unset();
fHostName = host;
fStatus = status;
return status;
}
Expand Down Expand Up @@ -235,6 +233,7 @@ BNetworkAddress::SetTo(int family, const char* host, uint16 port, uint32 flags)
status = resolver->GetNextAddress(&cookie, *this);
if (status != B_OK)
Unset();
fHostName = host;
fStatus = status;
return status;
}
Expand Down Expand Up @@ -263,6 +262,7 @@ BNetworkAddress::SetTo(int family, const char* host, const char* service,
status = resolver->GetNextAddress(&cookie, *this);
if (status != B_OK)
Unset();
fHostName = host;
fStatus = status;
return status;
}
Expand Down Expand Up @@ -372,6 +372,7 @@ BNetworkAddress::SetTo(const BNetworkAddress& other)
{
fAddress = other.fAddress;
fStatus = other.fStatus;
fHostName = other.fHostName;
}


Expand Down Expand Up @@ -1047,7 +1048,7 @@ BString
BNetworkAddress::HostName() const
{
// TODO: implement host name lookup
return ToString(false);
return fHostName;
}


Expand Down Expand Up @@ -1159,6 +1160,7 @@ BNetworkAddress&
BNetworkAddress::operator=(const BNetworkAddress& other)
{
memcpy(&fAddress, &other.fAddress, other.fAddress.ss_len);
fHostName = other.fHostName;
fStatus = other.fStatus;

return *this;
Expand Down Expand Up @@ -1291,6 +1293,8 @@ BNetworkAddress::_ParseLinkAddress(const char* address)

address += 3;
}

fHostName = address;

SetToLinkLevel(linkAddress, length);
return B_OK;
Expand Down
17 changes: 11 additions & 6 deletions src/kits/network/libnetapi/SecureSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ BSecureSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout)
if (status != B_OK)
return status;

return _SetupConnect();
return _SetupConnect(peer.HostName().String());
}


Expand Down Expand Up @@ -381,7 +381,7 @@ BSecureSocket::Write(const void* buffer, size_t size)


status_t
BSecureSocket::_SetupCommon()
BSecureSocket::_SetupCommon(const char* host)
{
// Do this only after BSocket::Connect has checked wether we're already
// connected. We don't want to kill an existing SSL session, as that would
Expand All @@ -399,15 +399,20 @@ BSecureSocket::_SetupCommon()
BIO_set_fd(fPrivate->fBIO, fSocket, BIO_NOCLOSE);
SSL_set_bio(fPrivate->fSSL, fPrivate->fBIO, fPrivate->fBIO);
SSL_set_ex_data(fPrivate->fSSL, Private::sDataIndex, this);
if (host != NULL) {
BString hostString = host;
if (hostString != "")
SSL_set_tlsext_host_name(fPrivate->fSSL, host);
}

return B_OK;
}


status_t
BSecureSocket::_SetupConnect()
BSecureSocket::_SetupConnect(const char* host)
{
status_t error = _SetupCommon();
status_t error = _SetupCommon(host);
if (error != B_OK)
return error;

Expand Down Expand Up @@ -529,14 +534,14 @@ BSecureSocket::InitCheck()


status_t
BSecureSocket::_SetupCommon()
BSecureSocket::_SetupCommon(const char* host)
{
return B_UNSUPPORTED;
}


status_t
BSecureSocket::_SetupConnect()
BSecureSocket::_SetupConnect(const char* host)
{
return B_UNSUPPORTED;
}
Expand Down

0 comments on commit e1c98ce

Please sign in to comment.