SSL Client HTTP Connection example getting information and verifying certificates. These are just some SSL notes. More...
#include <time.h>#include <unistd.h>#include <stdio.h>#include <string.h>#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <netdb.h>#include <stdlib.h>#include <errno.h>#include <openssl/rand.h>#include <openssl/ssl.h>#include <openssl/err.h>#include <sys/select.h>#include <sys/time.h>Go to the source code of this file.
Classes | |
| struct | Sslc |
Macros | |
| #define | CAPATH "/etc/ssl/certs" |
| #define | BUFFERSIZE 16384 |
| #define | STRBUFFERSIZE 256 |
| #define | USERAGENT "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0" |
| #define | CRLF "\r\n" |
Functions | |
| int | ASN1_TIME_to_time_t (ASN1_TIME *time, time_t *tmt) |
| char * | join (char *buffer, const char *origin, size_t *from, size_t size) |
| long int | check_certificate_validity (X509 *certificate) |
| int | TCP_Connection (Sslc *h, char *server, int port) |
| int | TCP_select (Sslc *h, double timeout) |
| int | SSL_Connection (Sslc *h) |
| int | SSL_send (Sslc *h, char *msg) |
| int | SSL_recv (Sslc *h, char **data) |
| void | SSL_print_info (Sslc *h) |
| void | SSL_print_certificate_info (X509 *cert) |
| char * | SSL_cipher_description (SSL_CIPHER *cipher) |
| char * | time_t_to_str (char *buffer, size_t bufsize, const char *format, time_t *tim) |
| void | print_time (ASN1_TIME *asn1time, char *pre_string, char *dateformat) |
| void | print_usage (char *executable) |
| void | panic (char *msg) |
| int | main (int argc, char *argv[]) |
SSL Client HTTP Connection example getting information and verifying certificates. These are just some SSL notes.
To compile: $ gcc -o myssl myssl.c -lcrypto -lssl
Definition in file myssl.c.
| #define BUFFERSIZE 16384 |
| #define CAPATH "/etc/ssl/certs" |
Where to look for the Certificate Authorities
Definition at line 34 of file myssl.c.
Referenced by check_certificate_validity(), and SSL_Connection().
| #define STRBUFFERSIZE 256 |
For temporary strings
Definition at line 38 of file myssl.c.
Referenced by print_time(), SSL_cipher_description(), and SSL_print_certificate_info().
| #define USERAGENT "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0" |
| int ASN1_TIME_to_time_t | ( | ASN1_TIME * | time, |
| time_t * | tmt | ||
| ) |
Transforms ASN1 time sring to time_t (except milliseconds and time zone) Ideas from: http://stackoverflow.com/questions/10975542/asn1-time-conversion
| time | SSL ASN1_TIME pointer |
| tmt | time_t pointer to write to |
Definition at line 279 of file myssl.c.
References join().
Referenced by print_time().
| long int check_certificate_validity | ( | X509 * | certificate | ) |
Check certificate validity
| certificate | Certificate to check |
Definition at line 325 of file myssl.c.
References CAPATH.
Referenced by SSL_print_certificate_info().
| char * join | ( | char * | buffer, |
| const char * | origin, | ||
| size_t * | from, | ||
| size_t | size | ||
| ) |
Extract a substring from origin into buffer, updating starting value to call in chain. Used by ASN1_TIME_to_time_t to extract substrings easyly
| buffer | Where to write to |
| origin | Original string |
| from | Where to start from. Updated to the last position after end. |
| size | Characters to extract. |
Definition at line 268 of file myssl.c.
Referenced by ASN1_TIME_to_time_t().
| void panic | ( | char * | msg | ) |
| void print_time | ( | ASN1_TIME * | asn1time, |
| char * | pre_string, | ||
| char * | dateformat | ||
| ) |
Prints ASN1_TIME on screen
| asn1time | Time to write |
| pre_string | String to write before the date |
| dateformat | Date format ( |
Definition at line 557 of file myssl.c.
References ASN1_TIME_to_time_t(), STRBUFFERSIZE, and time_t_to_str().
Referenced by SSL_print_certificate_info().
| void print_usage | ( | char * | executable | ) |
Prints program usage
| executable | Program executable (argv[0]) |
| char * SSL_cipher_description | ( | SSL_CIPHER * | cipher | ) |
Gets cipher description in a string Please free the resulting string, don't do it like me ;)
| cipher | Cipher |
Definition at line 568 of file myssl.c.
References STRBUFFERSIZE.
Referenced by SSL_print_info().
| int SSL_Connection | ( | Sslc * | h | ) |
| void SSL_print_certificate_info | ( | X509 * | cert | ) |
Prints out certificate information. Run throught the entries, print the not before and not after information and verify the certificate.
| cert | The certificate to check |
Definition at line 516 of file myssl.c.
References check_certificate_validity(), print_time(), and STRBUFFERSIZE.
Referenced by SSL_print_info().
| void SSL_print_info | ( | Sslc * | h | ) |
Prints out SSL information: SSL Version, cipher used and certificate information.
| h | Our structure. |
Definition at line 469 of file myssl.c.
References Sslc::ssl, SSL_cipher_description(), and SSL_print_certificate_info().
| int SSL_recv | ( | Sslc * | h, |
| char ** | data | ||
| ) |
SSL recv. To be called instead of recs. It will read the socket and decode information, or even perform a handshake if needed
| h | Our structure. |
| data | Data to be read (caution a pointer by reference that must be freed manually |
Definition at line 433 of file myssl.c.
References BUFFERSIZE, Sslc::ssl, and TCP_select().
| int SSL_send | ( | Sslc * | h, |
| char * | msg | ||
| ) |
| int TCP_Connection | ( | Sslc * | h, |
| char * | server, | ||
| int | port | ||
| ) |
Creates a basic TCP client connection to a server on a port. Uses simple sockets
| h | Our structure. Only the socket will be used |
| server | Where to connect |
| port | The port to use (443 for HTTPS) |
Definition at line 341 of file myssl.c.
References Sslc::err, and Sslc::skt.
| int TCP_select | ( | Sslc * | h, |
| double | timeout | ||
| ) |
Uses select to test if there is anything waiting to be read.
| h | Our structure. Only the socket will be used |
| timeout | Timeout before giving up |
Definition at line 417 of file myssl.c.
References Sslc::skt.
Referenced by SSL_recv().
| char * time_t_to_str | ( | char * | buffer, |
| size_t | bufsize, | ||
| const char * | format, | ||
| time_t * | tim | ||
| ) |
Gets a string with the time_t into a string
| buffer | Buffer to write to |
| bufsize | Total buffer size |
| format | Date/Time format ( |
| tim | Time |
Definition at line 549 of file myssl.c.
Referenced by print_time().
1.8.6
This doc is part of a project by Gaspar Fernández.