Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gccore.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "ssl.h"
- #define ISALIGNED(x) ((((u32)x)&0x1F)==0)
- static char __ssl_fs[] ATTRIBUTE_ALIGN(32) = "/dev/net/ssl";
- static s32 __ssl_fd = -1;
- static s32 __ssl_hid = -1;
- u32 ssl_init(){
- if(__ssl_hid < 0 ) {
- __ssl_hid = iosCreateHeap(SSL_HEAP_SIZE);
- if(__ssl_hid < 0){
- return __ssl_hid;
- }
- }
- return 0;
- }
- u32 ssl_open(){
- s32 ret;
- if (__ssl_fd < 0) {
- ret = IOS_Open(__ssl_fs,0);
- if(ret<0){
- return ret;
- }
- __ssl_fd = ret;
- }
- return 0;
- }
- u32 ssl_close(){
- s32 ret;
- if(__ssl_fd < 0) return 0;
- ret = IOS_Close(__ssl_fd);
- __ssl_fd = -1;
- if(ret<0)
- return ret;
- return 0;
- }
- s32 ssl_new(u8 * CN, u32 ssl_verify_options){
- s32 ret;
- s32 aContext[8] ATTRIBUTE_ALIGN(32);
- u32 aVerify_options[8] ATTRIBUTE_ALIGN(32);
- u8 aCN[256] ATTRIBUTE_ALIGN(32);
- if(ssl_open()) return -1;
- aVerify_options[0] = ssl_verify_options;
- memcpy(aCN, CN, 256);
- ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_NEW, "d:dd", aContext, 32, aVerify_options, 32, aCN, 256);
- ssl_close();
- if(ret == IPC_OK){
- return aContext[0];
- }
- return ret;
- }
- s32 ssl_setbuiltinclientcert(s32 ssl_context, s32 index){
- s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
- s32 aIndex[8] ATTRIBUTE_ALIGN(32);
- s32 aResponse[8] ATTRIBUTE_ALIGN(32);
- s32 ret;
- if(ssl_open()) return -1;
- aSsl_context[0] = ssl_context;
- aIndex[0] = index;
- ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_SETBUILTINCLIENTCERT, "d:dd", aResponse, 32, aSsl_context, 32, aIndex, 32);
- ssl_close();
- if(ret == IPC_OK){
- return aResponse[0];
- }
- return ret;
- }
- s32 ssl_setrootca(s32 ssl_context, const void *root, u32 length){
- s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
- s32 aResponse[8] ATTRIBUTE_ALIGN(32);
- s32 ret;
- if(ssl_open()) return -1;
- aSsl_context[0] = ssl_context;
- if(ISALIGNED(root)){ //Avoid expensive alignment
- ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_SETROOTCA, "d:dd", aResponse, 32, aSsl_context, 32, root, length);
- }else{
- u8 aRoot[length] ATTRIBUTE_ALIGN(32);
- memcpy(aRoot, root, length);
- ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_SETROOTCA, "d:dd", aResponse, 32, aSsl_context, 32, aRoot, length);
- }
- ssl_close();
- if(ret == IPC_OK){
- return aResponse[0];
- }
- return ret;
- }
- s32 ssl_connect(s32 ssl_context, s32 socket){
- s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
- s32 aSocket[8] ATTRIBUTE_ALIGN(32);
- s32 aResponse[8] ATTRIBUTE_ALIGN(32);
- s32 ret;
- if(ssl_open()) return -1;
- aSsl_context[0] = ssl_context;
- aSocket[0] = socket;
- ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_CONNECT, "d:dd", aResponse, 32, aSsl_context, 32, aSocket, 32);
- ssl_close();
- if(ret == IPC_OK){
- return aResponse[0];
- }
- return ret;
- }
- s32 ssl_handshake( s32 ssl_context ){
- s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
- s32 aResponse[8] ATTRIBUTE_ALIGN(32);
- s32 ret;
- if(ssl_open()) return -1;
- aSsl_context[0] = ssl_context;
- ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_HANDSHAKE, "d:d", aResponse, 32, aSsl_context, 32);
- ssl_close();
- if(ret == IPC_OK){
- return aResponse[0];
- }
- return ret;
- }
- s32 ssl_read(s32 ssl_context){
- //In: s32 ssl_context[8]
- //Out: s32 response[8], void buffer[length]
- //The number of bytes actually read is returned in response[0]. The maximum value for length is 32768
- //STUB
- return 0;
- }
- s32 ssl_write(s32 ssl_context, const void * buffer, u32 len){
- //In: s32 ssl_context[8], const void buffer[length]
- //Out: s32 response[8]
- //Attempt to write length bytes from buffer to the connected host. The number of bytes actually written is returned in response[0].
- //STUB
- return 0;
- }
- s32 ssl_shutdown( s32 ssl_context){
- s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
- s32 aResponse[8] ATTRIBUTE_ALIGN(32);
- s32 ret;
- if(ssl_open()) return -1;
- aSsl_context[0] = ssl_context;
- ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_SHUTDOWN, "d:d", aResponse, 32, aSsl_context, 32);
- ssl_close();
- if(ret == IPC_OK){
- return aResponse[0];
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement