Advertisement
Guest User

ssl_request.c

a guest
Feb 19th, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "openssl/ssl.h"
  6. #include "openssl/bio.h"
  7. #include "openssl/err.h"
  8.  
  9. #include "lua.h"
  10. #include "lauxlib.h"
  11.  
  12. static char* receive(BIO *bio, int chunksize) {
  13.     int  readbytes, count = 1;
  14.     char tmp[chunksize], *buf = NULL;
  15.  
  16.     buf = (char *) malloc(chunksize * sizeof(char));
  17.     buf[0] = '\0';
  18.     while(1) {
  19.         readbytes = BIO_read(bio, tmp, chunksize - 1);
  20.         if(readbytes <= 0) break;
  21.         tmp[readbytes] = '\0';
  22.  
  23.         buf = (char *) realloc(buf, chunksize * sizeof(char) * count);
  24.  
  25.         strncat(buf, tmp, strlen(tmp));
  26.  
  27.         count++;
  28.     }
  29.     return buf;
  30. }
  31.  
  32. static int ssl_get(const char *host, const char *port, const char *request, char **response, const int datasize, const int hostsize) {
  33.     char host_port[hostsize];
  34.     int host_size;
  35.  
  36.     /* ssl */
  37.     BIO *bio;
  38.     SSL *ssl;
  39.     SSL_CTX *ctx;
  40.  
  41.     /* init ssl lib */
  42.     SSL_library_init();
  43.     ERR_load_BIO_strings();
  44.     OpenSSL_add_all_algorithms();
  45.  
  46.     /* set up ssl context */
  47.     ctx = SSL_CTX_new(SSLv23_client_method());
  48.  
  49.     /* set up connection */
  50.     bio = BIO_new_ssl_connect(ctx);
  51.     BIO_get_ssl(bio, &ssl);
  52.     SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
  53.  
  54.     /* create connection */
  55.     /* host and port on the form of <host>:<port> */
  56.     strncpy(host_port, host, strlen(host));
  57.     host_size = strlen(host);
  58.     host_port[host_size] = ':';
  59.     host_port[host_size + 1] = '\0';
  60.     strncat(host_port, port, strlen(port));
  61.    
  62.  
  63.     BIO_set_conn_hostname(bio, host_port);
  64.    
  65.     if (BIO_do_connect(bio) <= 0) {
  66.         //fprintf(stderr, "Error attempting to connect\n");
  67.         //ERR_print_errors_fp(stderr);
  68.         BIO_free_all(bio);
  69.         SSL_CTX_free(ctx);
  70.         return -2;
  71.     }
  72.  
  73.     /* Send request */
  74.     BIO_write(bio, request, strlen(request));
  75.  
  76.     /* read response */
  77.     (*response) = receive(bio, datasize);
  78.    
  79.  
  80.     BIO_free_all(bio);
  81.     SSL_CTX_free(ctx);
  82.  
  83.     return 0;
  84. }
  85.  
  86. /* lua wrapper */
  87. static int l_ssl_get (lua_State *L) {
  88.     const char *host, *port, *request;
  89.     char *response;
  90.     int datasize, hostsize, isnumber_datasize, isnumber_hostsize, ssl_error;
  91.     int arguments = lua_gettop(L);
  92.  
  93.     /* check number of arguments */
  94.     if(arguments != 5){
  95.         lua_pushnil(L);
  96.         lua_pushstring(L, "Incorrect number of arguments");
  97.         return 2;
  98.     }
  99.  
  100.     /* get arguments */
  101.     host = lua_tostring(L, 1);
  102.     port = lua_tostring(L, 2);
  103.     request = lua_tostring(L, 3);
  104.     datasize = lua_tointegerx(L, 4, &isnumber_datasize);  
  105.     hostsize = lua_tointegerx(L, 5, &isnumber_hostsize);
  106.  
  107.     /* return error if wrong argument type */
  108.     if (!host) {
  109.         lua_pushnil(L);
  110.         lua_pushstring(L, "Argument 1 needs to be a string");
  111.         return 2;
  112.     }
  113.     if (!port) {
  114.         lua_pushnil(L);
  115.         lua_pushstring(L, "Argument 2 needs to a string");
  116.         return 2;
  117.     }
  118.     if (!request) {
  119.         lua_pushnil(L);
  120.         lua_pushstring(L, "Argument 3 needs to a string");
  121.     }
  122.     if (!isnumber_datasize) {
  123.         lua_pushnil(L);
  124.         lua_pushstring(L, "Argument 4 needs to be an integer");
  125.         return 2;
  126.     }
  127.     if (!isnumber_hostsize) {
  128.         lua_pushnil(L);
  129.         lua_pushstring(L, "Argument 5 needs to be an integer");
  130.         return 2;
  131.     }
  132.  
  133.     /* call function */
  134.     ssl_error = ssl_get(host, port, request, &response, datasize, hostsize);
  135.  
  136.     if(ssl_error == -2) {
  137.         lua_pushnil(L);
  138.         lua_pushstring(L, "Error attempting to connect");
  139.         return 2;
  140.     }
  141.  
  142.     lua_pushstring(L, response);
  143.     return 1;
  144. }
  145.  
  146. static const struct luaL_Reg sslrequest [] = {
  147.     {"ssl_get", l_ssl_get},
  148.     {NULL, NULL}
  149. };
  150.  
  151. int luaopen_sslrequest (lua_State *L) {
  152.     luaL_newlib(L, sslrequest);
  153.     return 1;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement