Created
April 12, 2022 03:02
-
-
Save u-r-w/e371c6f15b5edcc7793b76f651007d80 to your computer and use it in GitHub Desktop.
Simplest https client with C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile : $ gcc file.c -lssl -lcrypto | |
#include <openssl/bio.h> | |
#include <openssl/ssl.h> | |
#include <openssl/x509v3.h> | |
#include <string.h> | |
#include <stdio.h> | |
#define DEB printf("%d\n",__LINE__ ); | |
SSL *get_ssl(BIO *bio) { | |
SSL *ssl = NULL; | |
BIO_get_ssl(bio, &ssl); | |
if (ssl == NULL) printf("Error in BIO_get_ssl\n"); | |
return ssl; | |
} | |
int main() { | |
BIO *sbio, *out, *bio, *ssl_bio; | |
int len; | |
char tmpbuf[1024], buffer[4024]; | |
SSL_CTX *ctx; | |
SSL *ssl; | |
ctx = SSL_CTX_new(TLS_client_method()); | |
if (SSL_CTX_set_default_verify_paths(ctx) != 1) { DEB } | |
bio = BIO_new_connect("gitlab.com:443"); | |
if (BIO_do_connect(bio) <= 0) { DEB } | |
ssl_bio = BIO_push( BIO_new_ssl(ctx , 1), bio ); | |
SSL_set_tlsext_host_name( get_ssl(ssl_bio), "gitlab.com" ); | |
SSL_set1_host( get_ssl(ssl_bio), "gitlab.com" ); | |
printf( "line %d\n", __LINE__ ); | |
if (BIO_do_handshake(ssl_bio) <= 0) { DEB } | |
//my::verify_the_certificate(my::get_ssl(ssl_bio.get()), "gitlab.com" ); | |
char data[200] = "GET /api/v4/users/umarudy/events HTTP/1.1\r\nHost: gitlab.com\r\nPRIVATE-TOKEN: <CHANGE TOKEN HERE>\r\n\r\n"; | |
len = strlen( data ); | |
BIO_write(ssl_bio, data, len ); | |
BIO_flush(ssl_bio); | |
do { | |
len = BIO_read(ssl_bio, buffer, sizeof(buffer)); | |
buffer[len] = 0; | |
// if( buffer[len-1] == '\n' && buffer[len-2] == '\r') break; | |
printf( "%s", buffer); | |
} while ( len > 0 ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment