Advertisement
MrPoxipol

SSL WinSock IMAP Connection

Apr 15th, 2013
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.42 KB | None | 0 0
  1. #include <winsock2.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7.  
  8. #include <openssl/rand.h>
  9. #include <openssl/ssl.h>
  10. #include <openssl/err.h>
  11.  
  12. #define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
  13.  
  14. #define SERVER "imap.gmail.com"
  15. #define PORT 993
  16.  
  17. typedef struct
  18. {
  19.     int socket;
  20.     SSL *sslHandle;
  21.     SSL_CTX *sslContext;
  22. } connection;
  23.  
  24. int tcpConnect ()
  25. {
  26.     int error;
  27.     SOCKET handle;
  28.     struct hostent *host;
  29.     SOCKADDR_IN server;
  30.  
  31.     host = gethostbyname (SERVER);
  32.     if(host==NULL)
  33.     {
  34.         printf("gethostbyname failed: %d\n", WSAGetLastError());
  35.         return -1;
  36.     }
  37.     handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  38.     if (handle == -1)
  39.     {
  40.         perror ("Socket");
  41.         return -1;
  42.     }
  43.     else
  44.     {
  45.         server.sin_family = AF_INET;
  46.         server.sin_port = htons (PORT);
  47.         server.sin_addr.s_addr=*((unsigned long*)host->h_addr);
  48.         memset( &( server.sin_zero ), '\0', 8 );
  49.         error=connect(handle, (SOCKADDR*)&server,
  50.                       sizeof (server));
  51.  
  52.         if (error == -1)
  53.         {
  54.             perror ("Connect");
  55.             return -1;
  56.         }
  57.     }
  58.  
  59.     return handle;
  60. }
  61.  
  62. // Establish a connection using an SSL layer
  63. connection *sslConnect (void)
  64. {
  65.     connection *c;
  66.  
  67.     c = malloc (sizeof (connection));
  68.     c->sslHandle = NULL;
  69.     c->sslContext = NULL;
  70.  
  71.     c->socket = tcpConnect ();
  72.  
  73.     if(c->socket==-1)
  74.         return NULL;
  75.  
  76.     if (c->socket)
  77.     {
  78.         // Register the error strings for libcrypto & libssl
  79.         SSL_load_error_strings ();
  80.         // Register the available ciphers and digests
  81.         SSL_library_init ();
  82.  
  83.         // New context saying we are a client, and using SSL 2 or 3
  84.         c->sslContext = SSL_CTX_new (SSLv23_client_method ());
  85.         if (c->sslContext == NULL)
  86.             ERR_print_errors_fp (stderr);
  87.  
  88.         // Create an SSL struct for the connection
  89.         c->sslHandle = SSL_new (c->sslContext);
  90.         if (c->sslHandle == NULL)
  91.             ERR_print_errors_fp (stderr);
  92.  
  93.         // Connect the SSL struct to our connection
  94.         if (!SSL_set_fd (c->sslHandle, c->socket))
  95.             ERR_print_errors_fp (stderr);
  96.  
  97.         // Initiate SSL handshake
  98.         if (SSL_connect (c->sslHandle) != 1)
  99.             ERR_print_errors_fp (stderr);
  100.     }
  101.     else
  102.     {
  103.         perror ("Connect failed");
  104.     }
  105.  
  106.     return c;
  107. }
  108.  
  109. // Disconnect & free connection struct
  110. void sslDisconnect (connection *c)
  111. {
  112.     if (c->socket)
  113.         close (c->socket);
  114.     if (c->sslHandle)
  115.     {
  116.         SSL_shutdown (c->sslHandle);
  117.         SSL_free (c->sslHandle);
  118.     }
  119.     if (c->sslContext)
  120.         SSL_CTX_free (c->sslContext);
  121.  
  122.     free (c);
  123. }
  124.  
  125. // Read all available text from the connection
  126. char *sslRead (connection *c)
  127. {
  128.     const int readSize = 1024;
  129.     char *rc = NULL;
  130.     int received, count = 0;
  131.     char buffer[1024];
  132.  
  133.     if (c)
  134.     {
  135.         while (1)
  136.         {
  137.             if (!rc)
  138.                 rc = malloc (readSize * sizeof (char) + 1);
  139.             else
  140.                 rc = realloc (rc, (count + 1) *
  141.                               readSize * sizeof (char) + 1);
  142.  
  143.             received = SSL_read (c->sslHandle, buffer, readSize);
  144.             buffer[received] = '\0';
  145.  
  146.             if (received > 0)
  147.                 strcat (rc, buffer);
  148.  
  149.             if (received < readSize)
  150.                 break;
  151.             count++;
  152.         }
  153.     }
  154.  
  155.     return rc;
  156. }
  157.  
  158. // Write text to the connection
  159. void sslWrite (connection *c, char *text)
  160. {
  161.     if (c)
  162.         SSL_write (c->sslHandle, text, strlen (text));
  163. }
  164.  
  165. // Very basic main: we send GET / and print the response.
  166. int main (int argc, char **argv)
  167. {
  168.     connection *c;
  169.     char *response;
  170.  
  171.     WSADATA WsaDat;
  172.  
  173.     if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
  174.     {
  175.         fprintf(stderr, "WSAStartup failed: ");
  176.         return 1;
  177.     }
  178.  
  179.     c = sslConnect ();
  180.  
  181.     if(c==NULL)
  182.     {
  183.         printf("Connection Failed..\n");
  184.         WSACleanup();
  185.         return 1;
  186.  
  187.     }
  188.     response = sslRead (c);
  189.     sslWrite (c, "a LOGIN adres@email haslo\r\n");
  190.     response = sslRead (c);
  191.     sslWrite (c, "b SELECT INBOX\r\n");
  192.     response = sslRead (c);
  193.     sslWrite (c, "d SEARCH UNSEEN\r\n");
  194.     response = sslRead (c);
  195.     printf ("%s\n", response);
  196.     sslWrite (c, "a LOGOUT\r\n");
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement