Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
nickolasburr committed Oct 20, 2017
0 parents commit 904292d3289aaae567172dc7f4bdbf5fd2c9850f
Showing with 690 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +47 −0 Makefile
  3. +30 −0 include/common.h
  4. +14 −0 include/error.h
  5. +14 −0 include/main.h
  6. +20 −0 include/sock.h
  7. +17 −0 include/ssl.h
  8. +55 −0 include/utils.h
  9. +119 −0 src/main.c
  10. +79 −0 src/sock.c
  11. +269 −0 src/utils.c
  12. +14 −0 tools/clean.sh
  13. +5 −0 tools/install.sh
  14. +5 −0 tools/uninstall.sh
@@ -0,0 +1,2 @@
*.sw[po]
keuka
@@ -0,0 +1,47 @@
###
### Makefile
###

CC = gcc
TARGET = keuka
INSTALL = /usr/bin/install -c

prefix = /usr/local
bindir = $(prefix)/bin
binprefix =

ARFLAGS = rcs
RM = rm
RMFLAGS = -rf

INCLUDE = include
SOURCES = src
TOOLS = tools

CSFILES = $(wildcard $(SOURCES)/*.c)
OBFILES = $(patsubst %.c,%.o,$(CSFILES))

KERNEL := $(shell sh -c 'uname -s 2>/dev/null || echo unknown')

CFLAGS = -I$(INCLUDE) -I/usr/local/opt/openssl/include
LDFLAGS = -pthread -lssl -lcrypto -lz -L/usr/local/opt/openssl/lib

ifeq "$(KERNEL)" "Darwin"
LDFLAGS += -framework CoreFoundation -framework Security
endif

.PHONY: all clean install uninstall

all: $(TARGET)

$(TARGET): $(CSFILES)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

clean:
@cd $(TOOLS) && ./clean.sh

install:
@cd $(TOOLS) && ./install.sh

uninstall:
@cd $(TOOLS) && ./uninstall.sh
@@ -0,0 +1,30 @@
/**
* common.h
*
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#ifndef KEUKA_COMMON_H
#define KEUKA_COMMON_H

#include <ctype.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "error.h"
#include "utils.h"

#ifdef __APPLE__
#include <limits.h>
#else
#include <linux/limits.h>
#endif

#define _GNU_SOURCE
#define NULL_BYTE 1
#define NULL_DEVICE "/dev/null"

#endif /* KEUKA_COMMON_H */
@@ -0,0 +1,14 @@
/**
* error.h
*
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#ifndef KEUKA_ERROR_H
#define KEUKA_ERROR_H

#include "common.h"

int is_error(int);

#endif /* KEUKA_ERROR_H */
@@ -0,0 +1,14 @@
/**
* main.h
*
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#ifndef KEUKA_MAIN_H
#define KEUKA_MAIN_H

#include "common.h"
#include "sock.h"
#include "ssl.h"

#endif /* KEUKA_MAIN_H */
@@ -0,0 +1,20 @@
/**
* sock.h
*
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#ifndef KEUKA_SOCK_H
#define KEUKA_SOCK_H

#include "common.h"
#include "ssl.h"
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int mksock(char[], BIO *);

#endif /* KEUKA_SOCK_H */
@@ -0,0 +1,17 @@
/**
* ssl.h
*
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#ifndef KEUKA_SSL_H
#define KEUKA_SSL_H

#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>

#endif /* KEUKA_SSL_H */
@@ -0,0 +1,55 @@
/**
* utils.h
*
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#ifndef KEUKA_UTILS_H
#define KEUKA_UTILS_H

#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "common.h"

/**
* String utilities
*/

char *base_name(char *);
int compare(char *, char *);
char *concat(char *, char *);
char *copy(char *, char *);
char *dir_name(char *);
int index_of(char *, char **, size_t);
int in_array(char *, char **, size_t);
int length(char *);

/**
* Filesystem utilities
*/

DIR *get_dir(int *, const char *);
FILE *get_file(int *, const char *, const char *);

FILE *open_pipe(int *, const char *, const char *);
int close_pipe(FILE *);

int is_dir(const char *);
int is_file(const char *);
int is_link(const char *);
int is_sock(const char *);
int is_fifo(const char *);
int is_block(const char *);
int is_char(const char *);

/**
* Type utilities
*/

int is_digit(int);
int is_null(void *);
int is_numeric(char *);

#endif /* KEUKA_UTILS_H */
@@ -0,0 +1,119 @@
/**
* main.c
*
* Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
*/

#include "main.h"

int main () {
int server = 0;
char *dest_url = "https://www.example.com";
const SSL_METHOD *method;
BIO *certbio, *outbio;
X509 *cert = NULL;
X509_NAME *certname = NULL;
SSL_CTX *ctx;
SSL *ssl;

/* ---------------------------------------------------------- *
* These function calls initialize openssl for correct work. *
* ---------------------------------------------------------- */
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
SSL_load_error_strings();

/* ---------------------------------------------------------- *
* Create the Input/Output BIO's. *
* ---------------------------------------------------------- */
certbio = BIO_new(BIO_s_file());
outbio = BIO_new_fp(stdout, BIO_NOCLOSE);

/* ---------------------------------------------------------- *
* initialize SSL library and register algorithms *
* ---------------------------------------------------------- */
if (SSL_library_init() < 0) {
BIO_printf(outbio, "Could not initialize the OpenSSL library !\n");
}

/* ---------------------------------------------------------- *
* Set SSLv2 client hello, also announce SSLv3 and TLSv1 *
* ---------------------------------------------------------- */
method = SSLv23_client_method();

/* ---------------------------------------------------------- *
* Try to create a new SSL context *
* ---------------------------------------------------------- */
if (is_null(ctx = SSL_CTX_new(method))) {
BIO_printf(outbio, "Unable to create a new SSL context structure.\n");
}

/* ---------------------------------------------------------- *
* Disabling SSLv2 will leave v3 and TSLv1 for negotiation *
* ---------------------------------------------------------- */
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);

/* ---------------------------------------------------------- *
* Create new SSL connection state object *
* ---------------------------------------------------------- */
ssl = SSL_new(ctx);

/**
* Make TCP socket connection.
*/
server = mksock(dest_url, outbio);

if (server != 0) {
BIO_printf(outbio, "Successfully made the TCP connection to: %s.\n", dest_url);
}

/* ---------------------------------------------------------- *
* Attach the SSL session to the socket descriptor *
* ---------------------------------------------------------- */
SSL_set_fd(ssl, server);

/* ---------------------------------------------------------- *
* Try to SSL-connect here, returns 1 for success *
* ---------------------------------------------------------- */
if (SSL_connect(ssl) != 1) {
BIO_printf(outbio, "Error: Could not build a SSL session to: %s.\n", dest_url);
} else {
BIO_printf(outbio, "Successfully enabled SSL/TLS session to: %s.\n", dest_url);
}

/**
* Load remote certificate into X509 structure.
*/
cert = SSL_get_peer_certificate(ssl);

if (is_null(cert)) {
BIO_printf(outbio, "Error: Could not get a certificate from: %s.\n", dest_url);
} else {
BIO_printf(outbio, "Retrieved the server's certificate from: %s.\n", dest_url);
}

/* ---------------------------------------------------------- *
* extract various certificate information *
* -----------------------------------------------------------*/
certname = X509_NAME_new();
certname = X509_get_subject_name(cert);

/* ---------------------------------------------------------- *
* display the cert subject here *
* -----------------------------------------------------------*/
BIO_printf(outbio, "Displaying the certificate subject data:\n");
X509_NAME_print_ex(outbio, certname, 0, 0);
BIO_printf(outbio, "\n");

/* ---------------------------------------------------------- *
* Free the structures we don't need anymore *
* -----------------------------------------------------------*/
SSL_free(ssl);
close(server);
X509_free(cert);
SSL_CTX_free(ctx);
BIO_printf(outbio, "Finished SSL/TLS connection with server: %s.\n", dest_url);

return EXIT_SUCCESS;
}
Oops, something went wrong.

0 comments on commit 904292d

Please sign in to comment.
You can’t perform that action at this time.