Advertisement
Guest User

ssl_ios

a guest
Jun 18th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. #include <gccore.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "ssl.h"
  6.  
  7. #define ISALIGNED(x) ((((u32)x)&0x1F)==0)
  8.  
  9. static char __ssl_fs[] ATTRIBUTE_ALIGN(32) = "/dev/net/ssl";
  10.  
  11. static s32 __ssl_fd = -1;
  12. static s32 __ssl_hid = -1;
  13.  
  14. u32 ssl_init(){
  15.  
  16.     if(__ssl_hid < 0 ) {
  17.         __ssl_hid = iosCreateHeap(SSL_HEAP_SIZE);
  18.         if(__ssl_hid < 0){
  19.             return __ssl_hid;
  20.         }
  21.     }
  22.  
  23.     return 0;
  24. }
  25.  
  26. u32 ssl_open(){
  27.  
  28.     s32 ret;
  29.  
  30.     if (__ssl_fd < 0) {
  31.         ret = IOS_Open(__ssl_fs,0);
  32.         if(ret<0){
  33.             return ret;
  34.         }
  35.         __ssl_fd = ret;
  36.     }
  37.  
  38.     return 0;
  39. }
  40.  
  41. u32 ssl_close(){
  42.     s32 ret;
  43.  
  44.     if(__ssl_fd < 0) return 0;
  45.  
  46.     ret = IOS_Close(__ssl_fd);
  47.  
  48.     __ssl_fd = -1;
  49.  
  50.     if(ret<0)
  51.         return ret;
  52.  
  53.     return 0;
  54. }
  55.  
  56. s32 ssl_new(u8 * CN, u32 ssl_verify_options){
  57.  
  58.     s32 ret;
  59.     s32 aContext[8] ATTRIBUTE_ALIGN(32);
  60.     u32 aVerify_options[8] ATTRIBUTE_ALIGN(32);
  61.     u8 aCN[256] ATTRIBUTE_ALIGN(32);
  62.  
  63.     if(ssl_open()) return -1;
  64.  
  65.     aVerify_options[0] = ssl_verify_options;
  66.     memcpy(aCN, CN, 256);
  67.     ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_NEW, "d:dd", aContext, 32, aVerify_options, 32, aCN, 256);
  68.  
  69.     ssl_close();
  70.  
  71.     if(ret == IPC_OK){
  72.         return aContext[0];
  73.     }
  74.  
  75. return ret;
  76. }
  77.  
  78.  
  79. s32 ssl_setbuiltinclientcert(s32 ssl_context, s32 index){
  80.  
  81.     s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
  82.     s32 aIndex[8] ATTRIBUTE_ALIGN(32);
  83.     s32 aResponse[8] ATTRIBUTE_ALIGN(32);
  84.     s32 ret;
  85.  
  86.     if(ssl_open()) return -1;
  87.  
  88.     aSsl_context[0] = ssl_context;
  89.     aIndex[0] = index;
  90.  
  91.     ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_SETBUILTINCLIENTCERT, "d:dd", aResponse, 32, aSsl_context, 32, aIndex, 32);
  92.  
  93.     ssl_close();
  94.  
  95.     if(ret == IPC_OK){
  96.         return aResponse[0];
  97.     }
  98.  
  99. return ret;
  100. }
  101.  
  102.  
  103. s32 ssl_setrootca(s32 ssl_context, const void *root, u32 length){
  104.  
  105.     s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
  106.     s32 aResponse[8] ATTRIBUTE_ALIGN(32);
  107.     s32 ret;
  108.  
  109.     if(ssl_open()) return -1;
  110.  
  111.     aSsl_context[0] = ssl_context;
  112.  
  113.     if(ISALIGNED(root)){ //Avoid expensive alignment
  114.         ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_SETROOTCA, "d:dd", aResponse, 32, aSsl_context, 32, root, length);
  115.     }else{
  116.         u8 aRoot[length] ATTRIBUTE_ALIGN(32);
  117.         memcpy(aRoot, root, length);
  118.         ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_SETROOTCA, "d:dd", aResponse, 32, aSsl_context, 32, aRoot, length);
  119.     }
  120.  
  121.     ssl_close();
  122.  
  123.     if(ret == IPC_OK){
  124.         return aResponse[0];
  125.     }
  126.  
  127. return ret;
  128. }
  129.  
  130.  
  131. s32 ssl_connect(s32 ssl_context, s32 socket){
  132.  
  133.     s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
  134.     s32 aSocket[8] ATTRIBUTE_ALIGN(32);
  135.     s32 aResponse[8] ATTRIBUTE_ALIGN(32);
  136.     s32 ret;
  137.  
  138.     if(ssl_open()) return -1;
  139.  
  140.     aSsl_context[0] = ssl_context;
  141.     aSocket[0] = socket;
  142.  
  143.     ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_CONNECT, "d:dd", aResponse, 32, aSsl_context, 32, aSocket, 32);
  144.  
  145.     ssl_close();
  146.  
  147.     if(ret == IPC_OK){
  148.         return aResponse[0];
  149.     }
  150.  
  151. return ret;
  152. }
  153.  
  154.  
  155. s32 ssl_handshake( s32 ssl_context ){
  156.  
  157.     s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
  158.     s32 aResponse[8] ATTRIBUTE_ALIGN(32);
  159.     s32 ret;
  160.  
  161.     if(ssl_open()) return -1;
  162.  
  163.     aSsl_context[0] = ssl_context;
  164.  
  165.     ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_HANDSHAKE, "d:d", aResponse, 32, aSsl_context, 32);
  166.    
  167.     ssl_close();
  168.  
  169.     if(ret == IPC_OK){
  170.         return aResponse[0];
  171.     }
  172.  
  173. return ret;
  174. }
  175.  
  176.  
  177. s32 ssl_read(s32 ssl_context){
  178. //In: s32 ssl_context[8]
  179. //Out: s32 response[8], void buffer[length]
  180. //The number of bytes actually read is returned in response[0]. The maximum value for length is 32768
  181.  
  182. //STUB
  183. return 0;
  184. }
  185.  
  186. s32 ssl_write(s32 ssl_context, const void * buffer, u32 len){
  187. //In: s32 ssl_context[8], const void buffer[length]
  188. //Out: s32 response[8]
  189. //Attempt to write length bytes from buffer to the connected host. The number of bytes actually written is returned in response[0].
  190.  
  191. //STUB
  192. return 0;
  193. }
  194.  
  195. s32 ssl_shutdown( s32 ssl_context){
  196.     s32 aSsl_context[8] ATTRIBUTE_ALIGN(32);
  197.     s32 aResponse[8] ATTRIBUTE_ALIGN(32);
  198.     s32 ret;
  199.  
  200.     if(ssl_open()) return -1;
  201.  
  202.     aSsl_context[0] = ssl_context;
  203.  
  204.     ret = IOS_IoctlvFormat(__ssl_hid, __ssl_fd, IOCTLV_SSL_SHUTDOWN, "d:d", aResponse, 32, aSsl_context, 32);
  205.  
  206.     ssl_close();
  207.  
  208.     if(ret == IPC_OK){
  209.         return aResponse[0];
  210.     }
  211.  
  212. return ret;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement