Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dtls handshake process optimization #1824

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ jobs:
undefined-behavior-sanitizer:
runs-on: ubuntu-20.04
env:
UBSAN_OPTIONS: halt_on_error=1
UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1
CC: clang
CXX: clang++
AWS_KVS_LOG_LEVEL: 2
Expand Down
3 changes: 2 additions & 1 deletion samples/kvsWebRTCClientMaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ INT32 main(INT32 argc, CHAR* argv[])
#endif

#ifdef IOT_CORE_ENABLE_CREDENTIALS
CHK_ERR((pChannelName = argc > 1 ? argv[1] : getenv(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION, "AWS_IOT_CORE_THING_NAME must be set");
CHK_ERR((pChannelName = argc > 1 ? argv[1] : getenv(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION,
"AWS_IOT_CORE_THING_NAME must be set");
#else
pChannelName = argc > 1 ? argv[1] : SAMPLE_CHANNEL_NAME;
#endif
Expand Down
3 changes: 2 additions & 1 deletion samples/kvsWebRTCClientViewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ INT32 main(INT32 argc, CHAR* argv[])
#endif

#ifdef IOT_CORE_ENABLE_CREDENTIALS
CHK_ERR((pChannelName = argc > 1 ? argv[1] : getenv(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION, "AWS_IOT_CORE_THING_NAME must be set");
CHK_ERR((pChannelName = argc > 1 ? argv[1] : getenv(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION,
"AWS_IOT_CORE_THING_NAME must be set");
#else
pChannelName = argc > 1 ? argv[1] : SAMPLE_CHANNEL_NAME;
#endif
Expand Down
3 changes: 2 additions & 1 deletion samples/kvsWebrtcClientMasterGstSample.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ INT32 main(INT32 argc, CHAR* argv[])
signal(SIGINT, sigintHandler);

#ifdef IOT_CORE_ENABLE_CREDENTIALS
CHK_ERR((pChannelName = argc > 1 ? argv[1] : getenv(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION, "AWS_IOT_CORE_THING_NAME must be set");
CHK_ERR((pChannelName = argc > 1 ? argv[1] : getenv(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION,
"AWS_IOT_CORE_THING_NAME must be set");
#else
pChannelName = argc > 1 ? argv[1] : SAMPLE_CHANNEL_NAME;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ extern "C" {
#define STATUS_SSL_PACKET_BEFORE_DTLS_READY STATUS_DTLS_BASE + 0x00000004
#define STATUS_SSL_UNKNOWN_SRTP_PROFILE STATUS_DTLS_BASE + 0x00000005
#define STATUS_SSL_INVALID_CERTIFICATE_BITS STATUS_DTLS_BASE + 0x00000006

#define STATUS_DTLS_SESSION_ALREADY_FREED STATUS_DTLS_BASE + 0x00000007
/*!@} */

/////////////////////////////////////////////////////
Expand Down
14 changes: 13 additions & 1 deletion src/source/Crypto/Dtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ typedef enum {
RTC_DTLS_TRANSPORT_STATE_FAILED, /* The transport has failed as the result of an error */
} RTC_DTLS_TRANSPORT_STATE;

typedef enum {
DTLS_STATE_HANDSHAKE_NEW,
DTLS_STATE_HANDSHAKE_IN_PROGRESS,
DTLS_STATE_HANDSHAKE_COMPLETED,
DTLS_STATE_HANDSHAKE_ERROR,
} DTLS_HANDSHAKE_STATE;

/* Callback that is fired when Dtls Server wishes to send packet */
typedef VOID (*DtlsSessionOutboundPacketFunc)(UINT64, PBYTE, UINT32);

Expand Down Expand Up @@ -98,18 +105,22 @@ typedef struct {
typedef struct __DtlsSession DtlsSession, *PDtlsSession;
struct __DtlsSession {
volatile ATOMIC_BOOL isStarted;
volatile ATOMIC_BOOL shutdown;
volatile ATOMIC_BOOL isShutdown;
volatile ATOMIC_BOOL isCleanUp;
UINT32 certificateCount;
DtlsSessionCallbacks dtlsSessionCallbacks;
TIMER_QUEUE_HANDLE timerQueueHandle;
UINT32 timerId;
UINT64 dtlsSessionStartTime;
UINT64 dtlsSessionSetupTime;
RTC_DTLS_TRANSPORT_STATE state;
DTLS_HANDSHAKE_STATE handshakeState;
MUTEX sslLock;

#ifdef KVS_USE_OPENSSL
volatile ATOMIC_BOOL sslInitFinished;
volatile SIZE_T objRefCount;
CVAR receivePacketCvar;
// dtls message must fit into a UDP packet
BYTE outgoingDataBuffer[MAX_UDP_PACKET_SIZE];
UINT32 outgoingDataLen;
Expand Down Expand Up @@ -168,6 +179,7 @@ STATUS dtlsSessionShutdown(PDtlsSession);

STATUS dtlsSessionOnOutBoundData(PDtlsSession, UINT64, DtlsSessionOutboundPacketFunc);
STATUS dtlsSessionOnStateChange(PDtlsSession, UINT64, DtlsSessionOnStateChange);
STATUS dtlsSessionHandshakeInThread(PDtlsSession, BOOL);

/******** Internal Functions **********/
STATUS dtlsValidateRtcCertificates(PRtcCertificate, PUINT32);
Expand Down
14 changes: 10 additions & 4 deletions src/source/Crypto/Dtls_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ INT32 dtlsSessionKeyDerivationCallback(PVOID customData, const unsigned char* pM
return 0;
}

STATUS dtlsSessionHandshakeInThread(PDtlsSession pDtlsSession, BOOL isServer)
{
DLOGI("Threadpool based DTLS handshake not supported for mbedtls");
return STATUS_SUCCESS;
}

STATUS dtlsSessionStart(PDtlsSession pDtlsSession, BOOL isServer)
{
ENTERS();
Expand Down Expand Up @@ -327,7 +333,7 @@ STATUS dtlsSessionProcessPacket(PDtlsSession pDtlsSession, PBYTE pData, PINT32 p

CHK(pDtlsSession != NULL && pData != NULL && pDataLen != NULL, STATUS_NULL_ARG);
CHK(ATOMIC_LOAD_BOOL(&pDtlsSession->isStarted), STATUS_SSL_PACKET_BEFORE_DTLS_READY);
CHK(!ATOMIC_LOAD_BOOL(&pDtlsSession->shutdown), retStatus);
CHK(!ATOMIC_LOAD_BOOL(&pDtlsSession->isShutdown), retStatus);

MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;
Expand Down Expand Up @@ -384,7 +390,7 @@ STATUS dtlsSessionPutApplicationData(PDtlsSession pDtlsSession, PBYTE pData, INT
BOOL iterate = TRUE;

CHK(pData != NULL, STATUS_NULL_ARG);
CHK(!ATOMIC_LOAD_BOOL(&pDtlsSession->shutdown), retStatus);
CHK(!ATOMIC_LOAD_BOOL(&pDtlsSession->isShutdown), retStatus);

MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;
Expand Down Expand Up @@ -530,13 +536,13 @@ STATUS dtlsSessionShutdown(PDtlsSession pDtlsSession)
MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;

CHK(!ATOMIC_LOAD_BOOL(&pDtlsSession->shutdown), retStatus);
CHK(!ATOMIC_LOAD_BOOL(&pDtlsSession->isShutdown), retStatus);

while (mbedtls_ssl_close_notify(&pDtlsSession->sslCtx) == MBEDTLS_ERR_SSL_WANT_WRITE) {
// keep flushing outgoing buffer until nothing left
}

ATOMIC_STORE_BOOL(&pDtlsSession->shutdown, TRUE);
ATOMIC_STORE_BOOL(&pDtlsSession->isShutdown, TRUE);
CHK_STATUS(dtlsSessionChangeState(pDtlsSession, RTC_DTLS_TRANSPORT_STATE_CLOSED));

CleanUp:
Expand Down
Loading