Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <unistd.h>
- #include <malloc.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <resolv.h>
- #include <netdb.h>
- #include <openssl/ssl.h>
- #include <openssl/err.h>
- using namespace std;
- char* host;
- int port;
- SSL_CTX *ctx;
- int client;
- SSL *ssl;
- int MAX_LINE = 1024;
- int bytes;
- void InitCTX(void) {
- const SSL_METHOD *method;
- OpenSSL_add_all_algorithms();
- SSL_load_error_strings();
- method = SSLv2_client_method();
- ctx = SSL_CTX_new(method);
- if ( ctx == NULL ) {
- ERR_print_errors_fp(stderr);
- abort();
- }
- }
- void ShowCerts() {
- X509 *cert;
- char *line;
- cert = SSL_get_peer_certificate(ssl);
- if ( cert != NULL ) {
- printf("Server certificates:\n");
- line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
- printf("Subject: %s\n", line);
- free(line);
- line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
- printf("Issuer: %s\n", line);
- free(line);
- X509_free(cert);
- } else {
- printf("No certificates.\n");
- }
- }
- void HTTPS_disconnect() {
- close(client);
- SSL_CTX_free(ctx);
- }
- void HTTPS_connect() {
- InitCTX();
- client = socket(AF_INET, SOCK_STREAM, 0);
- if (static_cast<int>(client) < 0) {
- perror("socket()");
- HTTPS_disconnect();
- exit(1);
- }
- hostent *hp = gethostbyname(host);
- if (!hp) {
- cerr << "gethostbyname()" << endl;
- HTTPS_disconnect();
- exit(1);
- }
- sockaddr_in sin;
- memset((char*)&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- memcpy((char*)&sin.sin_addr, hp->h_addr, hp->h_length);
- sin.sin_port = htons(port);
- memset(&(sin.sin_zero), 0, 8 * sizeof(char));
- if (connect(client, (sockaddr*)&sin, sizeof(sin)) == -1) {
- perror("connect()");
- HTTPS_disconnect();
- exit(1);
- }
- ssl = SSL_new(ctx);
- SSL_set_fd(ssl, client);
- if (SSL_connect(ssl) == -1) {
- ERR_print_errors_fp(stderr);
- } else {
- char buf[MAX_LINE];
- const char *msg = "Hello???";
- printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
- ShowCerts();
- SSL_write(ssl, msg, strlen(msg));
- bytes = SSL_read(ssl, buf, sizeof(buf));
- buf[bytes] = 0;
- printf("Received: \"%s\"\n", buf);
- SSL_free(ssl);
- }
- HTTPS_disconnect();
- }
- int main() {
- host = "discordapp.com";
- port = 443;
- HTTPS_connect();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement