ssl/socket: Added socketSslConnectionSetSocketDescriptor/socketSslConnectionGetSocketDescriptor wrappers, and updated ssl for this.

This commit is contained in:
yellows8 2020-04-22 23:40:45 -04:00
parent 613fa855da
commit 41aefdc5ee
4 changed files with 70 additions and 15 deletions

View File

@ -39,3 +39,10 @@ void socketExit(void);
NX_INLINE Result socketInitializeDefault(void) {
return socketInitialize(NULL);
}
/// Wrapper for \ref sslConnectionSetSocketDescriptor. Returns the output sockfd on success.
int socketSslConnectionSetSocketDescriptor(SslConnection *c, int sockfd);
/// Wrapper for \ref sslConnectionGetSocketDescriptor. Returns the output sockfd on success.
int socketSslConnectionGetSocketDescriptor(SslConnection *c);

View File

@ -135,7 +135,7 @@ typedef enum {
/// OptionType. The default bool flags value for these at the time of \ref sslContextCreateConnection is cleared.
typedef enum {
SslOptionType_DoNotCloseSocket = 0, ///< DoNotCloseSocket. See \ref sslConnectionClose. This is only available if \ref sslConnectionSetSocketDescriptor wasn't used yet.
SslOptionType_DoNotCloseSocket = 0, ///< DoNotCloseSocket. See \ref sslConnectionSetSocketDescriptor. This is only available if \ref sslConnectionSetSocketDescriptor wasn't used yet.
SslOptionType_GetServerCertChain = 1, ///< [3.0.0+] GetServerCertChain
SslOptionType_SkipDefaultVerify = 2, ///< [5.0.0+] SkipDefaultVerify. Checked by \ref sslConnectionSetVerifyOption, see \ref SslVerifyOption.
SslOptionType_EnableAlpn = 3, ///< [9.0.0+] EnableAlpn. Only available with \ref sslConnectionSetOption. \ref sslConnectionSetSocketDescriptor should have been used prior to this - this will optionally use state setup by that, without throwing an error if that cmd wasn't used.
@ -158,7 +158,6 @@ typedef struct {
/// SslConnection
typedef struct {
Service s; ///< ISslConnection
int sockfd; ///< sockfd returned by the SetSocketDescriptor cmd.
} SslConnection;
/// BuiltInCertificateInfo
@ -348,18 +347,18 @@ Result sslContextImportCrl(SslContext *c, const void* buffer, u32 size, u64 *id)
/**
* @brief Closes a Connection object.
* @note This will use close() with the sockfd previously set by \ref sslConnectionSetSocketDescriptor if needed, hence sockets must have been initialized prior to using this. This can essentially be disabled via ::SslOptionType_DoNotCloseSocket.
* @param c \ref SslConnection
*/
void sslConnectionClose(SslConnection *c);
/**
* @brief SetSocketDescriptor
* @brief SetSocketDescriptor. Do not use directly, use \ref socketSslConnectionSetSocketDescriptor instead.
* @note An error is thrown if this was used previously.
* @param c \ref SslConnection
* @param[in] sockfd sockfd
* @param[out] out_sockfd sockfd. Prior to using \ref sslConnectionClose, this must be closed if it's not negative (it will be -1 if ::SslOptionType_DoNotCloseSocket is set).
*/
Result sslConnectionSetSocketDescriptor(SslConnection *c, int sockfd);
Result sslConnectionSetSocketDescriptor(SslConnection *c, int sockfd, int *out_sockfd);
/**
* @brief SetHostName
@ -385,7 +384,8 @@ Result sslConnectionSetVerifyOption(SslConnection *c, u32 verify_option);
Result sslConnectionSetIoMode(SslConnection *c, SslIoMode mode);
/**
* @brief GetSocketDescriptor
* @brief GetSocketDescriptor. Do not use directly, use \ref socketSslConnectionGetSocketDescriptor instead.
* @note This gets the input sockfd which was previously saved in state by \ref sslConnectionSetSocketDescriptor.
* @note \ref sslConnectionSetSocketDescriptor must have been used prior to this successfully.
* @param c \ref SslConnection
* @param[out] sockfd Output sockfd.

View File

@ -16,6 +16,7 @@
#include "result.h"
#include "services/bsd.h"
#include "services/ssl.h"
#include "runtime/devices/socket.h"
#include "runtime/hosversion.h"
@ -28,6 +29,8 @@ static int _socketClose(struct _reent *r, void *fdptr);
static ssize_t _socketWrite(struct _reent *r, void *fdptr, const char *buf, size_t count);
static ssize_t _socketRead(struct _reent *r, void *fdptr, char *buf, size_t count);
static int _socketGetFd(int fd);
static const devoptab_t g_socketDevoptab = {
.name = "soc",
.structSize = sizeof(int),
@ -138,6 +141,58 @@ Result socketGetLastResult(void) {
return g_bsdResult;
}
int socketSslConnectionSetSocketDescriptor(SslConnection *c, int sockfd) {
int dev;
int fd = _socketGetFd(sockfd);
if (fd==-1)
return -1;
int tmpfd=0;
Result rc = sslConnectionSetSocketDescriptor(c, fd, &tmpfd);
if (R_FAILED(rc)) {
g_bsdResult = rc;
errno = EIO;
return -1;
}
dev = FindDevice("soc:");
if(dev == -1)
return -1;
fd = __alloc_handle(dev);
if(fd == -1)
return -1;
*(int *)__get_handle(fd)->fileStruct = tmpfd;
return fd;
}
int socketSslConnectionGetSocketDescriptor(SslConnection *c) {
int fd, dev;
int tmpfd=0;
Result rc = sslConnectionGetSocketDescriptor(c, &tmpfd);
if (R_FAILED(rc)) {
g_bsdResult = rc;
errno = EIO;
return -1;
}
dev = FindDevice("soc:");
if(dev == -1)
return -1;
fd = __alloc_handle(dev);
if(fd == -1)
return -1;
*(int *)__get_handle(fd)->fileStruct = tmpfd;
return fd;
}
/***********************************************************************************************************************/
static int _socketGetFd(int fd) {

View File

@ -351,9 +351,6 @@ Result sslContextCreateConnection(SslContext *c, SslConnection *conn) {
if (!serviceIsActive(&c->s))
return MAKERESULT(Module_Libnx, LibnxError_NotInitialized);
memset(conn, 0, sizeof(*conn));
conn->sockfd = -1;
return _sslObjectDispatch(&c->s, 2,
.out_num_objects = 1,
.out_objects = &conn->s,
@ -461,18 +458,14 @@ Result sslContextImportCrl(SslContext *c, const void* buffer, u32 size, u64 *id)
// ISslConnection
void sslConnectionClose(SslConnection *c) {
if (c->sockfd >= 0) close(c->sockfd);
_sslObjectClose(&c->s);
memset(c, 0, sizeof(*c));
c->sockfd = -1;
}
Result sslConnectionSetSocketDescriptor(SslConnection *c, int sockfd) {
Result sslConnectionSetSocketDescriptor(SslConnection *c, int sockfd, int *out_sockfd) {
if (!serviceIsActive(&c->s))
return MAKERESULT(Module_Libnx, LibnxError_NotInitialized);
return _sslObjectDispatchInOut(&c->s, 0, sockfd, c->sockfd);
return _sslObjectDispatchInOut(&c->s, 0, sockfd, *out_sockfd);
}
Result sslConnectionSetHostName(SslConnection *c, const char* str, u32 str_bufsize) {