Skip to content

Instantly share code, notes, and snippets.

@ty60
Last active July 6, 2023 15:02
Show Gist options
  • Save ty60/3d8fb44a27caa859ab15137795d61d97 to your computer and use it in GitHub Desktop.
Save ty60/3d8fb44a27caa859ab15137795d61d97 to your computer and use it in GitHub Desktop.
Boring ssl client
// ref: https://e-penguiner.com/encrypted-socket-programming-c-cpp/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "openssl/ssl.h"
#include "openssl/err.h"
const char *req = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
int main() {
int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("93.184.216.34"); // example.com
server.sin_port = htons(443);
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("connect");
exit(1);
}
SSL_library_init();
SSL_load_error_strings();
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
if (!ctx) {
perror("SSL_CTX_new");
exit(1);
}
SSL *ssl = SSL_new(ctx);
if (!SSL_set_fd(ssl, sock)) {
perror("SSL_set_fd");
exit(1);
}
if (!SSL_connect(ssl)) {
perror("SSL_connect");
exit(1);
}
SSL_write(ssl, req, strlen(req));
char buf[4096];
SSL_read(ssl, buf, sizeof(buf));
printf("%s\n", buf);
return 0;
}
# Requires an already built boringssl project as subdirectory
TGT := boringssl_client_example
OBJS := boringssl_client_example.o
CFLAGS := -g -I boringssl/include -pthread
LIBS := boringssl/build/ssl/libssl.a boringssl/build/crypto/libcrypto.a boringssl/build/libpki.a boringssl/build/decrepit/libdecrepit.a
${TGT}: ${OBJS}
gcc ${CFLAGS} -o $@ $^ ${LIBS}
%.o: %.c
gcc ${CFLAGS} -c $^
clean:
rm -f ${TGT} ${OBJS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment