Advertisement
Panakotta00

HTTPS_Client.cpp

Sep 15th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <unistd.h>
  4. #include <malloc.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <resolv.h>
  8. #include <netdb.h>
  9. #include <openssl/ssl.h>
  10. #include <openssl/err.h>
  11.  
  12. using namespace std;
  13.  
  14. char* host;
  15. int port;
  16.  
  17. SSL_CTX *ctx;
  18. int client;
  19. SSL *ssl;
  20. int MAX_LINE = 1024;
  21. int bytes;
  22.  
  23. void InitCTX(void) {
  24.     const SSL_METHOD *method;
  25.     OpenSSL_add_all_algorithms();
  26.     SSL_load_error_strings();
  27.     method = SSLv2_client_method();
  28.     ctx = SSL_CTX_new(method);
  29.    
  30.     if ( ctx == NULL ) {
  31.         ERR_print_errors_fp(stderr);
  32.         abort();
  33.     }
  34. }
  35.  
  36. void ShowCerts() {
  37.     X509 *cert;
  38.     char *line;
  39.    
  40.     cert = SSL_get_peer_certificate(ssl);
  41.    
  42.     if ( cert != NULL ) {
  43.         printf("Server certificates:\n");
  44.         line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
  45.         printf("Subject: %s\n", line);
  46.         free(line);
  47.         line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
  48.         printf("Issuer: %s\n", line);
  49.         free(line);
  50.         X509_free(cert);
  51.     } else {
  52.         printf("No certificates.\n");
  53.     }
  54. }
  55.  
  56. void HTTPS_disconnect() {
  57.     close(client);
  58.     SSL_CTX_free(ctx);
  59. }
  60.  
  61. void HTTPS_connect() {
  62.     InitCTX();
  63.    
  64.     client = socket(AF_INET, SOCK_STREAM, 0);
  65.  
  66.     if (static_cast<int>(client) < 0) {
  67.         perror("socket()");
  68.         HTTPS_disconnect();
  69.         exit(1);
  70.     }
  71.     hostent *hp = gethostbyname(host);
  72.     if (!hp) {
  73.         cerr << "gethostbyname()" << endl;
  74.         HTTPS_disconnect();
  75.         exit(1);
  76.     }
  77.     sockaddr_in sin;
  78.     memset((char*)&sin, 0, sizeof(sin));
  79.     sin.sin_family = AF_INET;
  80.     memcpy((char*)&sin.sin_addr, hp->h_addr, hp->h_length);
  81.     sin.sin_port = htons(port);
  82.     memset(&(sin.sin_zero), 0, 8 * sizeof(char));
  83.     if (connect(client, (sockaddr*)&sin, sizeof(sin)) == -1) {
  84.         perror("connect()");
  85.         HTTPS_disconnect();
  86.         exit(1);
  87.     }
  88.     ssl = SSL_new(ctx);
  89.     SSL_set_fd(ssl, client);
  90.    
  91.     if (SSL_connect(ssl) == -1) {
  92.         ERR_print_errors_fp(stderr);
  93.     } else {
  94.         char buf[MAX_LINE];
  95.         const char *msg = "Hello???";
  96.        
  97.         printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
  98.         ShowCerts();
  99.         SSL_write(ssl, msg, strlen(msg));
  100.         bytes = SSL_read(ssl, buf, sizeof(buf));
  101.         buf[bytes] = 0;
  102.         printf("Received: \"%s\"\n", buf);
  103.         SSL_free(ssl);
  104.     }
  105.    
  106.     HTTPS_disconnect();
  107. }
  108.  
  109. int main() {
  110.     host = "discordapp.com";
  111.     port = 443;
  112.    
  113.     HTTPS_connect();
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement