2014-07-11 22:12:11 +02:00
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
#include "dtls_openssl.h"
|
2015-04-10 17:52:01 +02:00
|
|
|
#include "log.h"
|
2015-04-11 19:00:51 +02:00
|
|
|
#include "dbg.h"
|
2015-02-08 14:00:29 +01:00
|
|
|
#include "cw_util.h"
|
2015-04-11 19:00:51 +02:00
|
|
|
#include "timer.h"
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
/*
|
2014-07-11 22:12:11 +02:00
|
|
|
static BIO_METHOD bio_methods = {
|
|
|
|
BIO_TYPE_DGRAM,
|
|
|
|
"cw packet",
|
|
|
|
dtls_openssl_bio_write,
|
|
|
|
dtls_openssl_bio_read,
|
|
|
|
dtls_openssl_bio_puts,
|
|
|
|
NULL, // dgram_gets
|
|
|
|
dtls_openssl_bio_ctrl,
|
|
|
|
dtls_openssl_bio_new,
|
|
|
|
dtls_openssl_bio_free,
|
|
|
|
NULL,
|
|
|
|
};
|
2015-02-03 00:27:58 +01:00
|
|
|
*/
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
unsigned int psk_client_cb(SSL * ssl,
|
|
|
|
const char *hint,
|
|
|
|
char *identity,
|
|
|
|
unsigned int max_identity_len,
|
|
|
|
unsigned char *psk, unsigned int max_psk_len)
|
|
|
|
{
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
BIO *b = SSL_get_rbio(ssl);
|
|
|
|
struct conn *conn = b->ptr;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
// printf("KEYY: %s\n",conn->dtls_psk);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
snprintf(identity, max_identity_len, "CLient_identity");
|
|
|
|
|
|
|
|
int l = conn->dtls_psk_len < max_psk_len ? conn->dtls_psk_len : max_psk_len;
|
2015-02-03 00:27:58 +01:00
|
|
|
memcpy(psk, conn->dtls_psk, l);
|
2014-07-11 22:12:11 +02:00
|
|
|
return l;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
int dtls_openssl_connect(struct conn *conn)
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
|
|
|
if (!conn->dtls_data)
|
2015-02-03 00:27:58 +01:00
|
|
|
conn->dtls_data =
|
|
|
|
dtls_openssl_data_create(conn, DTLSv1_client_method(),
|
|
|
|
dtls_openssl_bio_method());
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
struct dtls_openssl_data *d = (struct dtls_openssl_data *) conn->dtls_data;
|
2014-07-11 22:12:11 +02:00
|
|
|
if (!d)
|
|
|
|
return 0;
|
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
// if (conn->dtls_psk)
|
|
|
|
// SSL_set_psk_client_callback(d->ssl, psk_client_cb);
|
2014-08-02 09:43:20 +02:00
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
int rc;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-02-08 14:00:29 +01:00
|
|
|
errno =0;
|
|
|
|
time_t timer = cw_timer_start(10);
|
|
|
|
do {
|
|
|
|
rc = SSL_connect(d->ssl);
|
|
|
|
}while(rc!=1 && errno==EAGAIN && !cw_timer_timeout(timer));
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
if (rc == 1) {
|
2015-02-08 14:00:29 +01:00
|
|
|
cw_dbg(DBG_DTLS,"SSL connect successfull!");
|
2015-02-03 00:27:58 +01:00
|
|
|
conn->read = dtls_openssl_read;
|
|
|
|
conn->write = dtls_openssl_write;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-02-03 00:27:58 +01:00
|
|
|
rc = dtls_openssl_log_error(0, rc, "DTLS connect");
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|