Wii/GameCube: Switch to using BearSSL for SSL support

This commit is contained in:
UnknownShadow200 2023-09-14 23:19:55 +10:00
parent 734330823d
commit 2f2d6aadbb
9 changed files with 221 additions and 288 deletions

View File

@ -152,9 +152,9 @@ ps3:
3ds:
$(MAKE) -f misc/3ds/Makefile PLAT=3ds
wii:
$(MAKE) -f src/Makefile_wii PLAT=wii
$(MAKE) -f misc/wii/Makefile PLAT=wii
gamecube:
$(MAKE) -f src/Makefile_gamecube PLAT=gamecube
$(MAKE) -f misc/gc/Makefile PLAT=gamecube
dreamcast:
$(MAKE) -f src/Makefile_dreamcast PLAT=dreamcast
xbox:

View File

@ -24,7 +24,7 @@ TARGET := ClassiCube-3ds
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing header files
#---------------------------------------------------------------------------------
BUILD := build
BUILD := build-3ds
SOURCES := src misc/3ds third_party/bearssl/src
INCLUDES := third_party/bearssl/inc

103
misc/gc/Makefile Normal file
View File

@ -0,0 +1,103 @@
#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
.SECONDARY:
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif
include $(DEVKITPPC)/gamecube_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET := ClassiCube-gc
BUILD := build-gc
SOURCES := src third_party/bearssl/src
INCLUDES := third_party/bearssl/inc
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
CXXFLAGS = $(CFLAGS)
LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lbba -lfat -logc -lm
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
export LD := $(CC)
export OFILES := $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
#---------------------------------------------------------------------------------
# build a list of include paths
#---------------------------------------------------------------------------------
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
-I$(CURDIR)/$(BUILD) \
-I$(LIBOGC_INC)
#---------------------------------------------------------------------------------
# build a list of library paths
#---------------------------------------------------------------------------------
export LIBPATHS := -L$(LIBOGC_LIB)
export OUTPUT := $(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean
#---------------------------------------------------------------------------------
all: $(BUILD)
$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/misc/gc/Makefile
$(BUILD):
mkdir -p $@
#---------------------------------------------------------------------------------
clean:
echo clean ...
rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)
-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

103
misc/wii/Makefile Normal file
View File

@ -0,0 +1,103 @@
#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
.SECONDARY:
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif
include $(DEVKITPPC)/wii_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET := ClassiCube-wii
BUILD := build-wii
SOURCES := src third_party/bearssl/src
INCLUDES := third_party/bearssl/inc
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
CXXFLAGS = $(CFLAGS)
LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lwiikeyboard -lwiiuse -lbte -lfat -logc -lm
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
export LD := $(CC)
export OFILES := $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
#---------------------------------------------------------------------------------
# build a list of include paths
#---------------------------------------------------------------------------------
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
-I$(CURDIR)/$(BUILD) \
-I$(LIBOGC_INC)
#---------------------------------------------------------------------------------
# build a list of library paths
#---------------------------------------------------------------------------------
export LIBPATHS := -L$(LIBOGC_LIB)
export OUTPUT := $(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean
#---------------------------------------------------------------------------------
all: $(BUILD)
$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/misc/wii/Makefile
$(BUILD):
mkdir -p $@
#---------------------------------------------------------------------------------
clean:
echo clean ...
rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)
-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

View File

@ -279,6 +279,7 @@ typedef cc_uint8 cc_bool;
#define CC_BUILD_HTTPCLIENT
#define CC_BUILD_OPENAL
#define CC_BUILD_GCWII
#define CC_BUILD_BEARSSL
#define CC_BUILD_LOWMEM
#undef CC_BUILD_FREETYPE
#elif defined __vita__

View File

@ -1,52 +0,0 @@
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif
include $(DEVKITPPC)/gamecube_rules
#---------------------------------------------------------------------------------
# configurable options
#---------------------------------------------------------------------------------
TARGET := ClassiCube-gc
SOURCES := src
#---------------------------------------------------------------------------------
# compilation input
#---------------------------------------------------------------------------------
CFILES := $(wildcard src/*.c)
sFILES := $(wildcard src/*.s)
SFILES := $(wildcard src/*.S)
OFILES := $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
INCLUDE := -I$(LIBOGC_INC)
LIBPATHS := -L$(LIBOGC_LIB)
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
DEPSDIR := $(CURDIR)/build
CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
LIBS := -lbba -lfat -logc -lm
LD := $(CC)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
default: $(DEPSDIR) $(TARGET).dol
$(DEPSDIR):
mkdir -p $(DEPSDIR)
mkdir -p $(DEPSDIR)/src
clean:
@rm $(OFILES) $(TARGET).elf $(TARGET).dol
$(TARGET).elf: $(OFILES)
$(TARGET).dol: $(TARGET).elf

View File

@ -1,52 +0,0 @@
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif
include $(DEVKITPPC)/wii_rules
#---------------------------------------------------------------------------------
# configurable options
#---------------------------------------------------------------------------------
TARGET := ClassiCube-wii
SOURCES := src
#---------------------------------------------------------------------------------
# compilation input
#---------------------------------------------------------------------------------
CFILES := $(wildcard src/*.c)
sFILES := $(wildcard src/*.s)
SFILES := $(wildcard src/*.S)
OFILES := $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
INCLUDE := -I$(LIBOGC_INC)
LIBPATHS := -L$(LIBOGC_LIB)
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
DEPSDIR := $(CURDIR)/build
CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
LIBS := -lwiikeyboard -lwiiuse -lbte -lfat -logc -lm
LD := $(CC)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
default: $(DEPSDIR) $(TARGET).dol
$(DEPSDIR):
mkdir -p $(DEPSDIR)
mkdir -p $(DEPSDIR)/src
clean:
@rm $(OFILES) $(TARGET).elf $(TARGET).dol
$(TARGET).elf: $(OFILES)
$(TARGET).dol: $(TARGET).elf

View File

@ -1050,7 +1050,9 @@ static const struct AssetSet* const asset_sets[] = {
&ccTexsAssetSet,
&mccTexsAssetSet,
&mccMusicAssetSet,
#ifndef CC_BUILD_LOWMEM
&mccSoundAssetSet
#endif /* TODO: Vorbis decoding */
};
void Resources_CheckExistence(void) {

190
src/SSL.c
View File

@ -440,10 +440,10 @@ void SSLBackend_Init(cc_bool verifyCerts) {
}
cc_bool SSLBackend_DescribeError(cc_result res, cc_string* dst) {
return false;
return false; // TODO: error codes
}
#ifdef CC_BUILD_3DS
#if defined CC_BUILD_3DS
#include <3ds.h>
static void InjectEntropy(SSLContext* ctx) {
char buf[32];
@ -452,6 +452,13 @@ static void InjectEntropy(SSLContext* ctx) {
br_ssl_engine_inject_entropy(&ctx->sc.eng, buf, 32);
}
#elif defined CC_BUILD_GCWII
static void InjectEntropy(SSLContext* ctx) {
char buf[32];
// TODO: Is there an actual API to retrieve random data?
br_ssl_engine_inject_entropy(&ctx->sc.eng, buf, 32);
}
#else
static void InjectEntropy(SSLContext* ctx) { }
#endif
@ -527,185 +534,6 @@ cc_result SSL_Free(void* ctx_) {
Mem_Free(ctx_);
return 0;
}
#elif defined CC_BUILD_GCWII && defined HW_RVL
/* Based off https://wiibrew.org/wiki//dev/net/ssl/code */
#include <gccore.h>
#include "SSL.h"
#include "Platform.h"
#include "Logger.h"
#include "String.h"
#define IOCTLV_SSL_NEW 1
#define IOCTLV_SSL_CONNECT 2
#define IOCTLV_SSL_HANDSHAKE 3
#define IOCTLV_SSL_READ 4
#define IOCTLV_SSL_WRITE 5
#define IOCTLV_SSL_SHUTDOWN 6
#define SSL_HEAP_SIZE 0xB000
#define CERT_ATTRIBUTES ATTRIBUTE_ALIGN(32)
//#include "../misc/RootCerts.h"
static char SSL_fs[] ATTRIBUTE_ALIGN(32) = "/dev/net/ssl";
static s32 SSL_fd = -1;
static s32 SSL_hid = -1;
void SSLBackend_Init(cc_bool verifyCerts) {
if (SSL_hid >= 0) return;
SSL_hid = iosCreateHeap(SSL_HEAP_SIZE);
if (SSL_hid < 0) Logger_Abort("Failed to create SSL heap");
}
cc_bool SSLBackend_DescribeError(cc_result res, cc_string* dst) { return false; }
static u32 ssl_open(void) {
s32 ret;
if (SSL_fd >= 0) return 0;
if (SSL_hid < 0) return ERR_OUT_OF_MEMORY;
ret = IOS_Open(SSL_fs, 0);
if (ret < 0) return ret;
SSL_fd = ret;
return 0;
}
static u32 ssl_close(void) {
s32 ret;
if (SSL_fd < 0) return 0;
ret = IOS_Close(SSL_fd);
SSL_fd = -1;
return ret;
}
static s32 ssl_new(const cc_string* host, u32 ssl_verify_options) {
static cc_string ccnet_cert_CN = String_FromConst("sni.cloudflaressl.com");
s32 ret;
u8 aCN[1024] ATTRIBUTE_ALIGN(32);
s32 aContext[8] ATTRIBUTE_ALIGN(32);
u32 aVerify_options[8] ATTRIBUTE_ALIGN(32);
// classicube.net's SSL certificate CN is actually "sni.cloudflaressl.com"
if (String_CaselessEqualsConst(host, "www.classicube.net")) {
String_EncodeUtf8(aCN, &ccnet_cert_CN);
} else {
String_EncodeUtf8(aCN, host);
}
if ((ret = ssl_open())) return ret;
aVerify_options[0] = ssl_verify_options;
ret = IOS_IoctlvFormat(SSL_hid, SSL_fd, IOCTLV_SSL_NEW, "d:dd", aContext, 0x20, aVerify_options, 0x20, aCN, 0x100);
ssl_close();
return ret ? ret : aContext[0];
}
static s32 ssl_connect(s32 ssl_context, s32 socket) {
s32 ret;
s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
s32 aSocket[8] ATTRIBUTE_ALIGN(32);
s32 aResponse[8] ATTRIBUTE_ALIGN(32);
if ((ret = ssl_open())) return ret;
aSsl_context[0] = ssl_context;
aSocket[0] = socket;
ret = IOS_IoctlvFormat(SSL_hid, SSL_fd, IOCTLV_SSL_CONNECT, "d:dd", aResponse, 0x20, aSsl_context, 0x20, aSocket, 0x20);
ssl_close();
return ret ? ret : aResponse[0];
}
static s32 ssl_handshake(s32 ssl_context) {
s32 ret;
s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
s32 aResponse[8] ATTRIBUTE_ALIGN(32);
if ((ret = ssl_open())) return ret;
aSsl_context[0] = ssl_context;
ret = IOS_IoctlvFormat(SSL_hid, SSL_fd, IOCTLV_SSL_HANDSHAKE, "d:d", aResponse, 0x20, aSsl_context, 0x20);
ssl_close();
return ret ? ret : aResponse[0];
}
cc_result SSL_Init(cc_socket socket, const cc_string* host, void** ctx) {
int sslCtx, ret;
sslCtx = ssl_new(host, 0);
if (sslCtx < 0) return sslCtx;
int* mem = Mem_Alloc(1, sizeof(int), "SSL context");
*mem = sslCtx;
*ctx = mem;
if ((ret = ssl_connect(sslCtx, socket))) return ret;
if ((ret = ssl_handshake(sslCtx))) return ret;
return 0;
}
cc_result SSL_Read(void* ctx, cc_uint8* data, cc_uint32 count, cc_uint32* read) {
int sslCtx = *(int*)ctx;
*read = 0;
s32 ret;
s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
s32 aResponse[8] ATTRIBUTE_ALIGN(32);
if ((ret = ssl_open())) return ret;
u8* aBuffer = NULL;
aBuffer = iosAlloc(SSL_hid, count);
if (!aBuffer) return IPC_ENOMEM;
aSsl_context[0] = sslCtx;
ret = IOS_IoctlvFormat(SSL_hid, SSL_fd, IOCTLV_SSL_READ, "dd:d", aResponse, 0x20, aBuffer, count, aSsl_context, 0x20);
ssl_close();
if (ret == IPC_OK) {
Mem_Copy(data, aBuffer, aResponse[0]);
}
*read = aResponse[0];
iosFree(SSL_hid, aBuffer);
return ret;
}
cc_result SSL_Write(void* ctx, const cc_uint8* data, cc_uint32 count, cc_uint32* wrote) {
int sslCtx = *(int*)ctx;
*wrote = 0;
s32 ret;
s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
s32 aResponse[8] ATTRIBUTE_ALIGN(32);
if ((ret = ssl_open())) return ret;
u8* aBuffer = NULL;
aBuffer = iosAlloc(SSL_hid, count);
if (!aBuffer) return IPC_ENOMEM;
aSsl_context[0] = sslCtx;
Mem_Copy(aBuffer, data, count);
ret = IOS_IoctlvFormat(SSL_hid, SSL_fd, IOCTLV_SSL_WRITE, "d:dd", aResponse, 0x20, aSsl_context, 0x20, aBuffer, count);
ssl_close();
*wrote = aResponse[0];
iosFree(SSL_hid, aBuffer);
return ret;
}
cc_result SSL_Free(void* ctx) {
int sslCtx = *(int*)ctx;
s32 ret;
s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
s32 aResponse[8] ATTRIBUTE_ALIGN(32);
if ((ret = ssl_open())) return ret;
aSsl_context[0] = sslCtx;
ret = IOS_IoctlvFormat(SSL_hid, SSL_fd, IOCTLV_SSL_SHUTDOWN, "d:d", aResponse, 0x20, aSsl_context, 0x20);
ssl_close();
return ret;
}
#else
void SSLBackend_Init(cc_bool verifyCerts) { }
cc_bool SSLBackend_DescribeError(cc_result res, cc_string* dst) { return false; }