1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
//////////////////////////////////////////////////////////////////////////////////////////////////
// author: croqueta
// programme: client TLS with certificate verification
//
//Compilation of the client:
//
//Ajust your own environnement variable:
// set LIB_PATH=C:\your\library\path\to\openssl
// set INCLUDE_PATH=C:\your\include\path\to\openssl
//
//command line compilaton:
// cl.exe /MD sample_ssl.c /I"%INCLUDE_PATH%" /Dinline= /link /libpath:"%LIB_PATH%" libeay32.lib ssleay32.lib Crypt32.lib Ws2_32.lib
//
///////////////////////////////////////////////////////////////////////////////////////////////////
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <windows.h>
#include <wincrypt.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>
typedef struct
{
int verbose_mode;
int verify_depth;
int always_continue;
} mydata_t;
int mydata_index;
int openssl_init(void)
{
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
SSL_load_error_strings();
if(SSL_library_init() < 0)
return -1;
return 0;
}
//
// This function load the certificate from the windows certicate store
// modification of a part of the original source from:
// http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/external/bsd/wpa/dist/src/crypto/tls_openssl.c
//
int cryptoapi_ca_cert(SSL_CTX *ssl_ctx, const char *store_name)
{
HCERTSTORE cs;
PCCERT_CONTEXT ctx = NULL;
X509 *cert;
char buf[128];
ssl_ctx->cert_store = X509_STORE_new();
cs = CertOpenSystemStore(0, store_name);
if (cs == NULL)
{
printf("[-] CryptoAPI: failed to open system cert store '%s': error=%d\n", store_name,(int) GetLastError());
return -1;
}
while((ctx = CertEnumCertificatesInStore(cs, ctx)))
{
cert = d2i_X509(NULL, &ctx->pbCertEncoded,ctx->cbCertEncoded);
if (cert == NULL)
{
printf("[-] CryptoAPI: Could not process X509 DER encoding for CA cert\n");
continue;
}
X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
printf("[+] OpenSSL: Loaded CA certificate for system certificate store:\n subject='%s'\n\n", buf);
if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert))
printf("[-] Failed to add ca_cert to OpenSSL certificate store");
X509_free(cert);
}
if (!CertCloseStore(cs, 0))
printf("[-] failed to close system cert store '%s': error=%d", store_name,(int) GetLastError());
return 0;
}
//
// The certificate verification callback
// example from openssl documentation:
// https://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
//
static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
{
char buf[256];
X509 *err_cert;
int err, depth;
SSL *ssl;
mydata_t *mydata;
err_cert = X509_STORE_CTX_get_current_cert(ctx);
err = X509_STORE_CTX_get_error(ctx);
depth = X509_STORE_CTX_get_error_depth(ctx);
/*
* Retrieve the pointer to the SSL of the connection currently treated
* and the application specific data stored into the SSL object.
*/
ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
mydata = SSL_get_ex_data(ssl, mydata_index);
X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
/*
* Catch a too long certificate chain. The depth limit set using
* SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
* that whenever the "depth>verify_depth" condition is met, we
* have violated the limit and want to log this error condition.
* We must do it here, because the CHAIN_TOO_LONG error would not
* be found explicitly; only errors introduced by cutting off the
* additional certificates would be logged.
*/
if(depth > mydata->verify_depth)
{
preverify_ok = 0;
err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
X509_STORE_CTX_set_error(ctx, err);
}
if(!preverify_ok)
{
printf("[-] verify error:num=%d:%s:depth=%d:%s\n", err,
X509_verify_cert_error_string(err), depth, buf);
}
else if (mydata->verbose_mode)
{
printf("[+] depth=%d:%s\n", depth, buf);
}
/*
* At this point, err contains the last verification error. We can use
* it for something special
*/
if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT))
{
X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), buf, 256);
printf("[-] issuer= %s\n", buf);
}
if (mydata->always_continue)
{
return 1;
}
else
{
return preverify_ok;
}
}
int connect_socket(const char url[], const short port)
{
WSADATA wsa;
char hostname[256]={0};
struct hostent *host;
struct sockaddr_in dest_addr;
int sockfd;
if(WSAStartup(MAKEWORD(2,2), &wsa))
return -1;
strncpy(hostname, strstr(url, "://")+3, sizeof(hostname));
if((host = gethostbyname(hostname)) == NULL)
return -1;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
dest_addr.sin_family=AF_INET;
dest_addr.sin_port=htons(port);
dest_addr.sin_addr.s_addr = *(long*)(host->h_addr);
memset(&(dest_addr.sin_zero), '\0', 8);
if(connect(sockfd, (struct sockaddr *) &dest_addr, sizeof(struct sockaddr)) != 0)
return -1;
return sockfd;
}
int main(void)
{
BIO *outbio = NULL;
BIO *certbio = NULL;
const SSL_METHOD *method;
SSL_CTX *ctx;
X509_STORE *root_certs= NULL;
SSL *ssl=NULL;
int sockfd;
char url[] = "pop3s://pop3.live.com\0";
short port = 995;
X509 *server_cert = NULL;
X509_NAME *server_certname = NULL;
mydata_t mydata;
certbio = BIO_new(BIO_s_file());
outbio = BIO_new_fp(stdout, BIO_NOCLOSE);
// OpenSSL initialisation
if(openssl_init() != 0)
exit(0);
// TLS context initialisation
method = TLSv1_client_method();
if((ctx = SSL_CTX_new(method)) == NULL)
exit(0);
// load the Windows CA certificates
if(cryptoapi_ca_cert(ctx, "ROOT") == 0)
printf("\n[+] CA certificates loaded\n");
else
printf("\n[-] CA certificates not loaded\n");
// create a new SSL structure
// and set the certificat verification callback
mydata_index = SSL_get_ex_new_index(0, "mydata index", NULL, NULL, NULL);
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, verify_callback);
ssl = SSL_new(ctx);
SSL_set_ex_data(ssl, mydata_index, &mydata);
// Connection to the server
if((sockfd = connect_socket(url, port)) == -1)
{
printf("[-] Failed to connecte at %s\n", url);
exit(0);
}
else
printf("[+] Connected at %s\n", url);
// attach the socket at lib openSSL
SSL_set_fd(ssl, sockfd);
// establish the SSL layer
if( SSL_connect(ssl) != 1)
{
printf("[-] Failed to establish ssl layer\n");
exit(0);
}
else
printf("[+] ssl layer established\n");
// get the server certificate
if((server_cert = SSL_get_peer_certificate(ssl)) == NULL)
printf("[-] Error: Could not get a certificate from: %s.\n", url);
else
printf("[+] Retrieved the server's certificate from: %s.\n", url);
// verify the validity of the SSL server certificate
if(SSL_get_verify_result(ssl) == X509_V_OK)
printf("[+] Server certificates X509 is trust!\n\n");
else
printf("[-] Server certificates X509 is not trust...\n\n");
// print server certificate informations
server_certname = X509_NAME_new();
server_certname = X509_get_subject_name(server_cert);
printf("the certificate subject data:\n\n");
X509_NAME_print_ex(outbio, server_certname, 0, 0);
printf("\n");
// close the connexion
SSL_free(ssl);
closesocket(sockfd);
X509_free(server_cert);
SSL_CTX_free(ctx);
return 0;
} |
Partager