Last active
February 6, 2019 20:53
-
-
Save Jnchi/40b16d3998fe8148ae28e52b45920c5f to your computer and use it in GitHub Desktop.
OpenSSL Example - Github API
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
/* | |
* This is free and unencumbered software released into the public domain. | |
* gcc -o openssl_example.c openssl_example -lssl -lcrypto | |
*/ | |
#include <string.h> | |
#include <stdio.h> | |
#include <openssl/ssl.h> | |
#include <openssl/err.h> | |
#include <openssl/bio.h> | |
#define USERNAME "jnchi" | |
int main() | |
{ | |
BIO* bio; | |
SSL_CTX* ctx; | |
char buf[2048]; | |
char response_buf[2048]; | |
char write_buf[2048]; | |
int size, start, end; | |
strcpy(response_buf, " "); | |
SSL_library_init(); | |
ctx = SSL_CTX_new(SSLv23_client_method()); | |
if (ctx == NULL) | |
printf("ctx is null\n"); | |
bio = BIO_new_ssl_connect(ctx); | |
BIO_set_conn_hostname(bio, "api.github.com:443"); | |
if(BIO_do_connect(bio) <= 0) | |
{ | |
printf("Failed connection\n"); | |
return 1; | |
} | |
else | |
{ | |
printf("Connected\n"); | |
} | |
/* https://developer.github.com/v3/users/#get-a-single-user */ | |
strcpy(write_buf, "GET /users/"); | |
strcat(write_buf, USERNAME); | |
strcat(write_buf, " HTTP/1.1\r\n"); | |
strcat(write_buf, "User-Agent: openssl_example/1.0 \r\n"); | |
strcat(write_buf, "Host: api.github.com\r\n"); | |
strcat(write_buf, "Connection: close \r\n"); | |
strcat(write_buf, "\r\n"); | |
printf("write_buf: %s\n", write_buf); | |
if(BIO_write(bio, write_buf, strlen(write_buf)) <= 0) | |
{ | |
if(!BIO_should_retry(bio)) | |
{ | |
printf("Do retry\n"); | |
} | |
printf("Failed write\n"); | |
} | |
printf("passed BIO_write\n"); | |
for (;;) | |
{ | |
size = BIO_read(bio, buf, sizeof(buf)); | |
printf("size %d\n", size); | |
if(size <= 0 ) | |
{ | |
break; | |
} | |
buf[size] = 0; | |
strcat(response_buf, buf); | |
printf("buf %s\n", buf); | |
} | |
printf("passed BIO_read\n"); | |
BIO_free_all(bio); | |
SSL_CTX_free(ctx); | |
printf("passed BIO_free_all and SSL_CTX_free\n"); | |
/* TODO: Extract JSON from response */ | |
printf("response_buf %s\n", response_buf); | |
printf("sizeof(response_buf) %ld\n", sizeof(response_buf)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment