fix unused functions, make WOLFSSL first parameter, add comments

This commit is contained in:
Jacob Barthelmeh
2016-05-16 23:48:26 -06:00
parent c8576566cc
commit 1b278edfd0
5 changed files with 50 additions and 40 deletions

View File

@@ -357,6 +357,7 @@ static INLINE void c32toa(word32 u32, byte* c)
c[3] = u32 & 0xff; c[3] = u32 & 0xff;
} }
#if defined(WOLFSSL_SESSION_EXPORT)
/* convert 64 bit integer to opaque */ /* convert 64 bit integer to opaque */
static INLINE void c64toa(word64 u64, byte* c) static INLINE void c64toa(word64 u64, byte* c)
{ {
@@ -369,6 +370,7 @@ static INLINE void c64toa(word64 u64, byte* c)
c[6] = (u64 >> 8) & 0xff; c[6] = (u64 >> 8) & 0xff;
c[7] = u64 & 0xff; c[7] = u64 & 0xff;
} }
#endif /* WOLFSSL_SESSION_EXPORT */
#endif #endif
@@ -395,6 +397,7 @@ static INLINE void ato32(const byte* c, word32* u32)
*u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
} }
#if defined(WOLFSSL_SESSION_EXPORT)
/* convert opaque to word64 type */ /* convert opaque to word64 type */
static INLINE void ato64(const byte* c, word64* u64) static INLINE void ato64(const byte* c, word64* u64)
{ {
@@ -408,7 +411,7 @@ static INLINE void ato64(const byte* c, word64* u64)
(((word64)c[6] << 8) & 0x000000000000ff00) | (((word64)c[6] << 8) & 0x000000000000ff00) |
((word64)c[7] & 0x00000000000000ff); ((word64)c[7] & 0x00000000000000ff);
} }
#endif /* WOLFSSL_SESSION_EXPORT */
#endif /* WOLFSSL_DTLS */ #endif /* WOLFSSL_DTLS */
@@ -501,7 +504,7 @@ static INLINE void ato64(const byte* c, word64* u64)
#ifdef WOLFSSL_SESSION_EXPORT #ifdef WOLFSSL_SESSION_EXPORT
#ifdef WOLFSSL_DTLS #ifdef WOLFSSL_DTLS
/* serializes the cipher specs struct for exporting */ /* serializes the cipher specs struct for exporting */
static int ExportCipherSpecState(byte* exp, word32 len, byte ver, WOLFSSL* ssl) static int ExportCipherSpecState(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
{ {
word32 idx = 0; word32 idx = 0;
CipherSpecs* specs; CipherSpecs* specs;
@@ -545,7 +548,7 @@ static int ExportCipherSpecState(byte* exp, word32 len, byte ver, WOLFSSL* ssl)
/* serializes the key struct for exporting */ /* serializes the key struct for exporting */
static int ExportKeyState(byte* exp, word32 len, byte ver, WOLFSSL* ssl) static int ExportKeyState(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
{ {
word32 idx = 0; word32 idx = 0;
byte sz; byte sz;
@@ -636,7 +639,7 @@ static int ExportKeyState(byte* exp, word32 len, byte ver, WOLFSSL* ssl)
return idx; return idx;
} }
static int ImportCipherSpecState(byte* exp, word32 len, byte ver, WOLFSSL* ssl) static int ImportCipherSpecState(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
{ {
word32 idx = 0; word32 idx = 0;
CipherSpecs* specs; CipherSpecs* specs;
@@ -672,7 +675,7 @@ static int ImportCipherSpecState(byte* exp, word32 len, byte ver, WOLFSSL* ssl)
} }
static int ImportKeyState(byte* exp, word32 len, byte ver, WOLFSSL* ssl) static int ImportKeyState(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
{ {
word32 idx = 0; word32 idx = 0;
byte sz; byte sz;
@@ -765,8 +768,8 @@ static int ImportKeyState(byte* exp, word32 len, byte ver, WOLFSSL* ssl)
/* copy over necessary information from Options struct to buffer /* copy over necessary information from Options struct to buffer
* On success returns 0 on failure returns a negative value */ * On success returns size of buffer used on failure returns a negative value */
static int dtls_export_new(byte* exp, word32 len, byte ver, WOLFSSL* ssl) static int dtls_export_new(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
{ {
int idx = 0; int idx = 0;
word16 zero = 0; word16 zero = 0;
@@ -886,8 +889,8 @@ static int dtls_export_new(byte* exp, word32 len, byte ver, WOLFSSL* ssl)
/* copy items from Export struct to Options struct /* copy items from Export struct to Options struct
* On success returns 0 on failure returns a negative value */ * On success returns size of buffer used on failure returns a negative value */
static int dtls_export_load(byte* exp, word32 len, byte ver, WOLFSSL* ssl) static int dtls_export_load(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
{ {
int idx = 0; int idx = 0;
Options* options = &ssl->options; Options* options = &ssl->options;
@@ -1001,7 +1004,7 @@ static int dtls_export_load(byte* exp, word32 len, byte ver, WOLFSSL* ssl)
* buf is used to hold the serialized WOLFSSL struct and sz is the size of buf * buf is used to hold the serialized WOLFSSL struct and sz is the size of buf
* passed in. * passed in.
* On success returns the size of serialized session.*/ * On success returns the size of serialized session.*/
int wolfSSL_dtls_export_internal(byte* buf, word32 sz, WOLFSSL* ssl) int wolfSSL_dtls_export_internal(WOLFSSL* ssl, byte* buf, word32 sz)
{ {
int ret; int ret;
word32 idx = 0; word32 idx = 0;
@@ -1033,8 +1036,8 @@ int wolfSSL_dtls_export_internal(byte* buf, word32 sz, WOLFSSL* ssl)
idx += DTLS_EXPORT_LEN; /* leave spot for length */ idx += DTLS_EXPORT_LEN; /* leave spot for length */
c16toa((word16)DTLS_EXPORT_OPT_SZ, buf + idx); idx += DTLS_EXPORT_LEN; c16toa((word16)DTLS_EXPORT_OPT_SZ, buf + idx); idx += DTLS_EXPORT_LEN;
if ((ret = dtls_export_new(buf + idx, sz - idx, DTLS_EXPORT_VERSION, if ((ret = dtls_export_new(ssl, buf + idx, sz - idx,
ssl)) < 0) { DTLS_EXPORT_VERSION)) < 0) {
WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret); WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret);
return ret; return ret;
} }
@@ -1042,8 +1045,8 @@ int wolfSSL_dtls_export_internal(byte* buf, word32 sz, WOLFSSL* ssl)
/* export keys struct and dtls state -- variable length stored in ret */ /* export keys struct and dtls state -- variable length stored in ret */
idx += DTLS_EXPORT_LEN; /* leave room for length */ idx += DTLS_EXPORT_LEN; /* leave room for length */
if ((ret = ExportKeyState(buf + idx, sz - idx, if ((ret = ExportKeyState(ssl, buf + idx, sz - idx,
DTLS_EXPORT_VERSION, ssl)) < 0) { DTLS_EXPORT_VERSION)) < 0) {
WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret); WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret);
return ret; return ret;
} }
@@ -1051,8 +1054,8 @@ int wolfSSL_dtls_export_internal(byte* buf, word32 sz, WOLFSSL* ssl)
/* export of cipher specs struct */ /* export of cipher specs struct */
c16toa((word16)DTLS_EXPORT_SPC_SZ, buf + idx); idx += DTLS_EXPORT_LEN; c16toa((word16)DTLS_EXPORT_SPC_SZ, buf + idx); idx += DTLS_EXPORT_LEN;
if ((ret = ExportCipherSpecState(buf + idx, sz - idx, if ((ret = ExportCipherSpecState(ssl, buf + idx, sz - idx,
DTLS_EXPORT_VERSION, ssl)) < 0) { DTLS_EXPORT_VERSION)) < 0) {
WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret); WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret);
return ret; return ret;
} }
@@ -1085,7 +1088,7 @@ int wolfSSL_dtls_export_internal(byte* buf, word32 sz, WOLFSSL* ssl)
/* On success return amount of buffer consumed */ /* On success return amount of buffer consumed */
int wolfSSL_dtls_import_internal(byte* buf, word32 sz, WOLFSSL* ssl) int wolfSSL_dtls_import_internal(WOLFSSL* ssl, byte* buf, word32 sz)
{ {
word32 idx = 0; word32 idx = 0;
word16 length = 0; word16 length = 0;
@@ -1133,7 +1136,7 @@ int wolfSSL_dtls_import_internal(byte* buf, word32 sz, WOLFSSL* ssl)
WOLFSSL_MSG("Import Options struct error"); WOLFSSL_MSG("Import Options struct error");
return BUFFER_E; return BUFFER_E;
} }
if ((ret = dtls_export_load(buf + idx, length, version, ssl)) < 0) { if ((ret = dtls_export_load(ssl, buf + idx, length, version)) < 0) {
WOLFSSL_MSG("Import Options struct error"); WOLFSSL_MSG("Import Options struct error");
return ret; return ret;
} }
@@ -1149,7 +1152,7 @@ int wolfSSL_dtls_import_internal(byte* buf, word32 sz, WOLFSSL* ssl)
WOLFSSL_MSG("Import Key struct error"); WOLFSSL_MSG("Import Key struct error");
return BUFFER_E; return BUFFER_E;
} }
if ((ret = ImportKeyState(buf + idx, length, version, ssl)) < 0) { if ((ret = ImportKeyState(ssl, buf + idx, length, version)) < 0) {
WOLFSSL_MSG("Import Key struct error"); WOLFSSL_MSG("Import Key struct error");
return ret; return ret;
} }
@@ -1165,7 +1168,7 @@ int wolfSSL_dtls_import_internal(byte* buf, word32 sz, WOLFSSL* ssl)
WOLFSSL_MSG("Import CipherSpecs struct error"); WOLFSSL_MSG("Import CipherSpecs struct error");
return BUFFER_E; return BUFFER_E;
} }
if ((ret = ImportCipherSpecState(buf + idx, length, version, ssl)) < 0) { if ((ret = ImportCipherSpecState(ssl, buf + idx, length, version)) < 0) {
WOLFSSL_MSG("Import CipherSpecs struct error"); WOLFSSL_MSG("Import CipherSpecs struct error");
return ret; return ret;
} }

View File

@@ -164,10 +164,12 @@ int wolfSSL_dtls_import(WOLFSSL* ssl, unsigned char* buf, unsigned int sz)
} }
/* sanity checks on buffer and protocol are done in internal function */ /* sanity checks on buffer and protocol are done in internal function */
return wolfSSL_dtls_import_internal(buf, sz, ssl); return wolfSSL_dtls_import_internal(ssl, buf, sz);
} }
/* Sets the function to call for serializing the session. This function is
* called right after the handshake is completed. */
int wolfSSL_CTX_dtls_set_export(WOLFSSL_CTX* ctx, wc_dtls_export func) int wolfSSL_CTX_dtls_set_export(WOLFSSL_CTX* ctx, wc_dtls_export func)
{ {
@@ -184,6 +186,8 @@ int wolfSSL_CTX_dtls_set_export(WOLFSSL_CTX* ctx, wc_dtls_export func)
} }
/* Sets the function in WOLFSSL struct to call for serializing the session. This
* function is called right after the handshake is completed. */
int wolfSSL_dtls_set_export(WOLFSSL* ssl, wc_dtls_export func) int wolfSSL_dtls_set_export(WOLFSSL* ssl, wc_dtls_export func)
{ {
@@ -200,10 +204,18 @@ int wolfSSL_dtls_set_export(WOLFSSL* ssl, wc_dtls_export func)
} }
int wolfSSL_dtls_export(unsigned char* buf, unsigned int* sz, WOLFSSL* ssl) /* This function allows for directly serializing a session rather than using
* callbacks. It has less overhead by removing a temporary buffer and gives
* control over when the session gets serialized. When using callbacks the
* session is always serialized immediatly after the handshake is finished.
*
* buf is the argument to contain the serialized session
* sz is the size of the buffer passed in
* ssl is the WOLFSSL struct to serialize
* returns the size of serialized session on success, 0 on no action, and
* negative value on error */
int wolfSSL_dtls_export(WOLFSSL* ssl, unsigned char* buf, unsigned int* sz)
{ {
int ret;
WOLFSSL_ENTER("wolfSSL_dtls_export"); WOLFSSL_ENTER("wolfSSL_dtls_export");
if (ssl == NULL || sz == NULL) { if (ssl == NULL || sz == NULL) {
@@ -222,12 +234,7 @@ int wolfSSL_dtls_export(unsigned char* buf, unsigned int* sz, WOLFSSL* ssl)
} }
/* copy over keys, options, and dtls state struct */ /* copy over keys, options, and dtls state struct */
ret = wolfSSL_dtls_export_internal(buf, *sz, ssl); return wolfSSL_dtls_export_internal(ssl, buf, *sz);
if (ret < 0) {
return ret;
}
return ret;
} }
@@ -257,7 +264,7 @@ int wolfSSL_send_session(WOLFSSL* ssl)
} }
/* copy over keys, options, and dtls state struct */ /* copy over keys, options, and dtls state struct */
ret = wolfSSL_dtls_export_internal(buf, bufSz, ssl); ret = wolfSSL_dtls_export_internal(ssl, buf, bufSz);
if (ret < 0) { if (ret < 0) {
XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret; return ret;

View File

@@ -839,15 +839,15 @@ static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
} }
AssertIntEQ(len, wolfSSL_write(ssl, msg, len)); AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
#ifdef WOLFSSL_SESSION_EXPORT #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL)
if (wolfSSL_dtls(ssl)) { if (wolfSSL_dtls(ssl)) {
byte* import; byte* import;
word32 sz; word32 sz;
wolfSSL_dtls_export(NULL, &sz, ssl); wolfSSL_dtls_export(ssl, NULL, &sz);
import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER); import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
AssertNotNull(import); AssertNotNull(import);
idx = wolfSSL_dtls_export(import, &sz, ssl); idx = wolfSSL_dtls_export(ssl, import, &sz);
AssertIntGE(idx, 0); AssertIntGE(idx, 0);
AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0); AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);

View File

@@ -1213,10 +1213,10 @@ WOLFSSL_LOCAL ProtocolVersion MakeTLSv1_2(void);
WOLFSSL_LOCAL ProtocolVersion MakeDTLSv1_2(void); WOLFSSL_LOCAL ProtocolVersion MakeDTLSv1_2(void);
#ifdef WOLFSSL_SESSION_EXPORT #ifdef WOLFSSL_SESSION_EXPORT
WOLFSSL_LOCAL int wolfSSL_dtls_import_internal(byte* buf, word32 sz, WOLFSSL_LOCAL int wolfSSL_dtls_import_internal(WOLFSSL* ssl, byte* buf,
WOLFSSL* ssl); word32 sz);
WOLFSSL_LOCAL int wolfSSL_dtls_export_internal(byte* buf, word32 sz, WOLFSSL_LOCAL int wolfSSL_dtls_export_internal(WOLFSSL* ssl, byte* buf,
WOLFSSL* ssl); word32 sz);
WOLFSSL_LOCAL int wolfSSL_send_session(WOLFSSL* ssl); WOLFSSL_LOCAL int wolfSSL_send_session(WOLFSSL* ssl);
#endif #endif
#endif #endif

View File

@@ -234,8 +234,8 @@ WOLFSSL_API int wolfSSL_dtls_import(WOLFSSL* ssl, unsigned char* buf,
WOLFSSL_API int wolfSSL_CTX_dtls_set_export(WOLFSSL_CTX* ctx, WOLFSSL_API int wolfSSL_CTX_dtls_set_export(WOLFSSL_CTX* ctx,
wc_dtls_export func); wc_dtls_export func);
WOLFSSL_API int wolfSSL_dtls_set_export(WOLFSSL* ssl, wc_dtls_export func); WOLFSSL_API int wolfSSL_dtls_set_export(WOLFSSL* ssl, wc_dtls_export func);
WOLFSSL_API int wolfSSL_dtls_export(unsigned char* buf, unsigned int* sz, WOLFSSL_API int wolfSSL_dtls_export(WOLFSSL* ssl, unsigned char* buf,
WOLFSSL* ssl); unsigned int* sz);
#endif /* WOLFSSL_DTLS */ #endif /* WOLFSSL_DTLS */
#endif /* WOLFSSL_SESSION_EXPORT */ #endif /* WOLFSSL_SESSION_EXPORT */