Haiku Book: added Certificate, ProxySecureSocket, SecureSocket, Socket

Change-Id: I65a6374ff08bb7d2217bc303eb0939a6c8c7c792
Reviewed-on: https://review.haiku-os.org/c/haiku/+/9522
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
cafeina
2026-03-21 09:10:32 +00:00
committed by Adrien Destugues
parent 013c5814ff
commit d6a2ec35b9
5 changed files with 514 additions and 1 deletions
+1 -1
View File
@@ -23,7 +23,7 @@
BAbstractSocket provides a common interface for all socket-based
communication streams. These include BDatagramSocket, BSocket,
BSecureSocket and BServerSocket.
BSecureSocket and BProxySecureSocket.
BAbstractSocket implements common behavior between these different socket
types. This includes management of a BSD socket integer handle, knowledge
+148
View File
@@ -0,0 +1,148 @@
/*
* Copyright 2025 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* cafeina
*
* Corresponds to:
* headers/os/net/Certificate.h hrev58979
* src/kits/network/libnetapi/Certificate.cpp hrev58979
*/
/*!
\file Certificate.h
\ingroup network
\brief Provides the BCertificate class.
*/
/*!
\class BCertificate
\ingroup network
\brief BCertificate is a class that represents a digital certificate used in
encrypted network connections, such as X.509 certificates.
It is aimed to retrieve information from a certificate, including the
date of validity and of expiration, the issuer, subject and the
signature algorithm. It also checks if the certificate is a Certificate
Authority (CA) certificate as well as if the certificate is self-signed.
\since Haiku R1
*/
/*!
\fn BCertificate::BCertificate(const BCertificate& other)
\brief Copy constructor.
It creates a deep copy of the certificate data.
\param[in] other The other BCertificate object from where to initialize this
BCertificate instance.
\since Haiku R1
*/
/*!
\fn int BCertificate::Version() const
\brief Returns the numerical value of the certificate's version field.
\since Haiku R1
*/
/*!
\fn time_t BCertificate::StartDate() const
\brief Returns the certificate's \c notBefore field timestamp.
This is the date when the certificate starts to be valid.
\since Haiku R1
*/
/*!
\fn time_t BCertificate::ExpirationDate() const
\brief Returns the certificate's \c notAfter field timestamp.
This is the date when the certificate is no longer valid, that is, its
expiration date.
\since Haiku R1
*/
/*!
\fn bool BCertificate::IsValidAuthority() const
\brief Checks if the certificate is a Certificate Authority certificate.
CA certificates are those that can be used to sign other certificates.
\since Haiku R1
*/
/*!
\fn bool BCertificate::IsSelfSigned() const
\brief Checks if the certificate was self-signed.
A self-signed certificate is one where the CA certificate is the same as
the certificated subject.
\since Haiku R1
*/
/*!
\fn BString BCertificate::Issuer() const
\brief Returns the name of the Certificate Authority that issued the
certificate.
\since Haiku R1
*/
/*!
\fn BString BCertificate::Subject() const
\brief Returns the certificate's subject name.
\since Haiku R1
*/
/*!
\fn BString BCertificate::SignatureAlgorithm() const
\brief Returns the name of the certificate's signature algorithm.
\since Haiku R1
*/
/*!
\fn BString BCertificate::String() const
\brief Returns the information contained in the certificate in a
human-readable form.
This includes the certificate's version, the name of the signature
algorithm, the issuer name, the validity period, the subject name, the
public key and its algorithm, hexadecimal dump of any unique identifier of
the issuer or the subject, any signature algorithm extensions, the signature
dump and any non-standard data fields.
\since Haiku R1
*/
/*!
\fn bool BCertificate::operator==(const BCertificate& other) const
\brief Compares if this certificate is the same as \a other certificate.
\retval true Both BCertificate objects refer to the same certificate.
\retval false The certificates are different.
\since Haiku R1
*/
+85
View File
@@ -0,0 +1,85 @@
/*
* Copyright 2025 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* cafeina
*
* Corresponds to:
* headers/os/net/ProxySecureSocket.h hrev58979
* src/kits/network/libnetapi/ProxySecureSocket.cpp hrev58979
*/
/*!
\file ProxySecureSocket.h
\ingroup network
\brief Provides the BProxySecureSocket class.
*/
/*!
\class BProxySecureSocket
\ingroup network
\brief BProxySecureSocket is a class that extends BSecureSocket to
have an encrypted connection via an HTTP proxy server.
\since Haiku R1
*/
/*!
\fn BProxySecureSocket::BProxySecureSocket(const BNetworkAddress& proxy)
\brief Creates an uninitialized socket in disconnected and unbound state.
\a proxy is set as the proxy server through where the encrypted
communications are channelled.
\param[in] proxy The proxy network address.
\since Haiku R1
*/
/*!
\fn BProxySecureSocket::BProxySecureSocket(const BNetworkAddress& proxy, const BNetworkAddress& peer, bigtime_t timeout = B_INFINITE_TIMEOUT)
\brief Creates a socket to \a peer and tries to connect to that endpoint
via \a proxy until \a timeout is reached.
It initializes an SSL session by which the connection should be channeled.
\param[in] proxy The proxy's network address.
\param[in] peer The peer's network address.
\param[in] timeout The timeout in microseconds or \c B_INFINITE_TIMEOUT.
This is used for subsequent reads and writes as well.
\since Haiku R1
*/
/*!
\fn BProxySecureSocket::BProxySecureSocket(const BProxySecureSocket& other)
\brief Copy constructor.
The copied object accesses the same underlying socket.
\param[in] other The other BProxySecureSocket object.
\since Haiku R1
*/
/*!
\fn virtual status_t BProxySecureSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout = B_INFINITE_TIMEOUT)
\brief Connect the socket to the given \a peer.
It also creates an SSL session for encrypted communication.
The socket is disconnected from any previous connections.
\returns \c B_OK if the connection was performed successfully or an error
code otherwise.
\since Haiku R1
*/
+130
View File
@@ -0,0 +1,130 @@
/*
* Copyright 2025 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* cafeina
*
* Corresponds to:
* headers/os/net/SecureSocket.h hrev58979
* src/kits/network/libnetapi/SecureSocket.cpp hrev58979
*/
/*!
\file SecureSocket.h
\ingroup network
\brief Provides the BSecureSocket class.
*/
/*!
\class BSecureSocket
\ingroup network
\brief BSecureSocket is a class that extends BSocket to provide
encrypted connection using the TLS or SSL protocols.
\since Haiku R1
*/
/*!
\fn BSecureSocket::BSecureSocket()
\brief Creates an uninitialized socket in disconnected and unbound state.
\since Haiku R1
*/
/*!
\fn BSecureSocket::BSecureSocket(const BNetworkAddress& peer, bigtime_t timeout = B_INFINITE_TIMEOUT)
\brief Creates a socket to \a peer and tries to connect to that endpoint
until \a timeout is reached.
It initializes an SSL session by which the connection should be channeled.
\param[in] peer The peer's network address.
\param[in] timeout The timeout in microseconds or \c B_INFINITE_TIMEOUT.
This is used for subsequent reads and writes as well.
\since Haiku R1
*/
/*!
\fn BSecureSocket::BSecureSocket(const BSecureSocket& other)
\brief Copy constructor.
The copied object accesses the same underlying socket.
\param[in] other The other BSecureSocket object.
\since Haiku R1
*/
/*!
\fn virtual BSecureSocket::~BSecureSocket()
\brief Destructor.
Disconnects the socket and releases any SSL resources.
\since Haiku R1
*/
/*!
\fn virtual bool BSecureSocket::CertificateVerificationFailed(BCertificate& certificate, const char* message)
\brief Callback method triggered when a certificate verification fails.
The default implementation returns \c false.
This will cancel the connection. Applications could subclass BSecureSocket
to allow the user to check the certificate manually, or validate it on
their own, before letting the connection continue anyways.
\param[out] certificate The certificate in the certificate chain that
could not be validated.
\param message The error message.
\since Haiku R1
*/
/*!
\fn status_t BSecureSocket::InitCheck()
\brief Returns the initialization status.
\returns \c B_OK if the object was properly initialized or an error code
otherwise.
\since Haiku R1
*/
/*!
\fn virtual status_t BSecureSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout = B_INFINITE_TIMEOUT)
\brief Connect the socket to the given \a peer.
It also creates an SSL session for encrypted communication.
The socket is disconnected from any previous connections.
\returns \c B_OK if the connection was performed successfully or an error
code otherwise.
\since Haiku R1
*/
/*!
\fn virtual void BSecureSocket::Disconnect()
\brief Close the connection.
It also closes the current SSL session.
The socket becomes disconnected and unbound. You can Connect or Bind it
again, either to the same or another peer.
\since Haiku R1
*/
+150
View File
@@ -0,0 +1,150 @@
/*
* Copyright 2025 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* cafeina
*
* Corresponds to:
* headers/os/net/Socket.h hrev58979
* src/kits/network/libnetapi/Socket.cpp hrev58979
*/
/*!
\file Socket.h
\ingroup network
\brief Provides the BSocket class.
*/
/*!
\class BSocket
\ingroup network
\brief BSocket is a class used to perform stream-based socket connections.
\since Haiku R1
*/
/*!
\fn BSocket::BSocket()
\brief Creates an uninitialized socket in disconnected and unbound state.
\since Haiku R1
*/
/*!
\fn BSocket::BSocket(const BNetworkAddress& peer, bigtime_t timeout = B_INFINITE_TIMEOUT)
\brief Creates a socket to \a peer and tries to connect to that endpoint
until \a timeout is reached.
\param[in] peer A network address.
\param[in] timeout The timeout in microseconds or \c B_INFINITE_TIMEOUT.
This is used for subsequent reads and writes as well.
\since Haiku R1
*/
/*!
\fn BSocket::BSocket(const BSocket& other)
\brief Copy constructor.
The copied object accesses the same underlying socket.
\param[in] other The other BSocket object.
\since Haiku R1
*/
/*!
\fn virtual BSocket::~BSocket()
\brief Destructor.
Disconnects the socket.
\since Haiku R1
*/
/*!
\fn virtual status_t BSocket::Bind(const BNetworkAddress& peer, bool reuseAddr = true)
\brief Assigns a local address \a peer to this socket.
If \a reuseAddr is \c true, it should allow the reuse of the local address.
If the binding is successful, the object is left in a bound state.
\param[in] peer The local address to be bound to this socket object.
\param[in] reuseAddr Whether the reuse of local addresses should be allowed in non-zero requests.
\retval B_OK Binding was successful.
\retval EADDRINUSE The specified address is already in use and \a reuseAddr is false.
\retval EADDRNOTAVAIL The specified address is not available from the local machine.
\retval EAFNOSUPPORT The specified address is not of a family address accepted by the socket.
\since Haiku R1
*/
/*!
\fn virtual status_t BSocket::Accept(BAbstractSocket*& _socket)
\brief Accepts an incoming connection to this socket and initializes
\a _socket to that remote endpoint.
This method extracts the first connection from the pending incoming
connections' queue and fills \a _socket with the peer's information.
\retval B_OK The connection was accepted and the other object was
initialized with the peer's information.
\retval B_NO_MEMORY Not enough memory to allocate for the other socket object.
\retval -1 Failure to accept the incoming connection.
\since Haiku R1
*/
/*!
\fn virtual status_t BSocket::Connect(const BNetworkAddress& peer, bigtime_t timeout = B_INFINITE_TIMEOUT)
\brief Connects the socket to the given \a peer.
The socket is disconnected from any previous connections.
\param[in] peer The peer's address to connect to.
\param[in] timeout The timeout in microseconds or \c B_INFINITE_TIMEOUT.
This is used for subsequent reads and writes as well.
\returns B_OK if the connection was performed successfully or an error
code otherwise.
\since Haiku R1
*/
/*!
\fn virtual ssize_t BSocket::Read(void* buffer, size_t size)
\brief Receives from the socket's peer and stores it in \a buffer.
\param[out] buffer A buffer where the data should be stored.
\param[in] size The length in bytes of the buffer.
\returns The length of bytes received, or an error code.
\since Haiku R1
*/
/*!
\fn virtual ssize_t BSocket::Write(const void* buffer, size_t size)
\brief Sends data from the socket to its peer.
\param[in] buffer The data to be sent.
\param[in] size The length in bytes of the data.
\returns The length of bytes sent, or an error code.
\since Haiku R1
*/