Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-wood committed May 3, 2016
1 parent 6039235 commit 0e2a959
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
30 changes: 15 additions & 15 deletions dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@ dtls_End()
}

static int
_ssl_verify_peer(int ok, X509_STORE_CTX* ctx)
_ssl_verify_peer(int ok, X509_STORE_CTX* ctx)
{
return 1;
}

int
dtls_InitContextFromKeystore(DTLSParams* k, const char* keyname)
dtls_InitContextFromKeystore(DTLSParams* params, const char* keyname)
{
int result = 0;

// Create a new context using DTLS
k->ctx = SSL_CTX_new(DTLSv1_method());
if (k->ctx == NULL) {
params->ctx = SSL_CTX_new(DTLSv1_method());
if (params->ctx == NULL) {
printf("Error: cannot create SSL_CTX.\n");
ERR_print_errors_fp(stderr);
return -1;
}

// Set our supported ciphers
result = SSL_CTX_set_cipher_list(k->ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
result = SSL_CTX_set_cipher_list(params->ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
if (result != 1) {
printf("Error: cannot set the cipher list.\n");
ERR_print_errors_fp(stderr);
return -2;
}

// The client doesn't have to send it's certificate
SSL_CTX_set_verify(k->ctx, SSL_VERIFY_PEER, _ssl_verify_peer);
SSL_CTX_set_verify(params->ctx, SSL_VERIFY_PEER, _ssl_verify_peer);

// Load key and certificate
char certfile[1024];
Expand All @@ -61,23 +61,23 @@ dtls_InitContextFromKeystore(DTLSParams* k, const char* keyname)
sprintf(keyfile, "./%s-key.pem", keyname);

// Load the certificate file; contains also the public key
result = SSL_CTX_use_certificate_file(k->ctx, certfile, SSL_FILETYPE_PEM);
result = SSL_CTX_use_certificate_file(params->ctx, certfile, SSL_FILETYPE_PEM);
if (result != 1) {
printf("Error: cannot load certificate file.\n");
ERR_print_errors_fp(stderr);
return -4;
}

// Load private key
result = SSL_CTX_use_PrivateKey_file(k->ctx, keyfile, SSL_FILETYPE_PEM);
result = SSL_CTX_use_PrivateKey_file(params->ctx, keyfile, SSL_FILETYPE_PEM);
if (result != 1) {
printf("Error: cannot load private key file.\n");
ERR_print_errors_fp(stderr);
return -5;
}

// Check if the private key is valid
result = SSL_CTX_check_private_key(k->ctx);
result = SSL_CTX_check_private_key(params->ctx);
if (result != 1) {
printf("Error: checking the private key failed. \n");
ERR_print_errors_fp(stderr);
Expand Down Expand Up @@ -136,13 +136,13 @@ dtls_Shutdown(DTLSParams* k)
return;
}

if (k->ctx != NULL) {
SSL_CTX_free(k->ctx);
k->ctx = NULL;
if (params->ctx != NULL) {

This comment has been minimized.

Copy link
@vi

vi Aug 11, 2017

Why k only partially changed to params here?

SSL_CTX_free(params->ctx);
params->ctx = NULL;
}

if (k->ssl != NULL) {
SSL_free(k->ssl);
k->ssl = NULL;
if (params->ssl != NULL) {
SSL_free(params->ssl);
params->ssl = NULL;
}
}
4 changes: 2 additions & 2 deletions dtls_client.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "dtls.h"

#define IP_ADDRESS "127.0.0.1:4433"
#define IP_PORT "127.0.0.1:4433"

int
main(int argc, char *argv[argc])
Expand All @@ -20,7 +20,7 @@ main(int argc, char *argv[argc])
if (dtls_InitContextFromKeystore(&client, "client") < 0) {
exit(EXIT_FAILURE);
}
if (dtls_InitClient(&client, IP_ADDRESS) < 0) {
if (dtls_InitClient(&client, IP_PORT) < 0) {
exit(EXIT_FAILURE);
}

Expand Down

0 comments on commit 0e2a959

Please sign in to comment.