Last active
January 28, 2017 12:57
-
-
Save vsajip/7e4ba0f06822b1b46815de5e48826c2c to your computer and use it in GitHub Desktop.
Simple program which demonstrates libtls issue #266
This file contains 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
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <tls.h> | |
#define MSGSIZE 1024 | |
static void | |
assert(BOOL condition, char * format, ...) | |
{ | |
if (!condition) { | |
va_list va; | |
char message[MSGSIZE]; | |
int len; | |
va_start(va, format); | |
len = vsnprintf_s(message, MSGSIZE, MSGSIZE - 1, format, va); | |
fprintf(stderr, "Fatal error in launcher: %s\n", message); | |
ExitProcess(1); | |
} | |
} | |
int main() | |
{ | |
WSADATA data; | |
int rc; | |
int sock; | |
struct addrinfo info; | |
struct addrinfo * result = NULL; | |
char * host = "bitbucket.org"; | |
char * port = "443"; | |
struct tls_config * config; | |
struct tls * context; | |
rc = WSAStartup(MAKEWORD(2, 2), &data); | |
assert(!rc, "WSAStartup failed"); | |
// get connection info | |
ZeroMemory(&info, sizeof(info)); | |
info.ai_family = AF_INET; | |
info.ai_socktype = SOCK_STREAM; | |
info.ai_protocol = IPPROTO_TCP; | |
rc = getaddrinfo(host, port, &info, &result); | |
assert(!rc, "getaddrinfo failed"); | |
// create socket | |
sock = (int) socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
assert(sock > 0, "socket failed"); | |
// connect socket | |
rc = connect(sock, result->ai_addr, (socklen_t) result->ai_addrlen); | |
assert(!rc, "connect failed"); | |
freeaddrinfo(result); | |
// init libtls | |
rc = tls_init(); | |
assert(!rc, "tls_init failed"); | |
// get config | |
config = tls_config_new(); | |
assert(config != NULL, "config creation failed"); | |
rc = tls_config_set_ca_file(config, "cert.pem"); | |
assert(!rc, "tls_config_set_ca_file failed: %s", tls_config_error(config)); | |
// get context and configure | |
context = tls_client(); | |
assert(context != NULL, "context creation failed"); | |
rc = tls_configure(context, config); | |
assert(!rc, "context configuration failed"); | |
// connect the socket to the context and do the handshake | |
rc = tls_connect_socket(context, sock, host); | |
assert(!rc, "tls_connect_socket failed"); | |
rc = tls_handshake(context); | |
assert(!rc, "tls_handshake failed"); | |
// print some info to show we actually connected | |
printf("issuer: %s\n", tls_peer_cert_issuer(context)); | |
// shutdown and close socket | |
shutdown(sock, SD_SEND); | |
closesocket(sock); | |
// free up context | |
rc = tls_close(context); | |
assert(!rc, "tls_close failed"); | |
tls_free(context); | |
// free up config | |
tls_config_free(config); | |
// last ... | |
WSACleanup(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment