From ac16cf8a1bb1c3260ef6dc39e1dcd67cadc93a73 Mon Sep 17 00:00:00 2001 From: J Harper Date: Tue, 3 May 2016 17:36:14 -0700 Subject: [PATCH] Coverity scan fixes --- Makefile | 32 +++++++++-- apps/dtls/Makefile | 3 +- apps/dtls/dtlsClient.c | 11 ++-- apps/dtls/dtlsServer.c | 17 +++--- apps/ssl/Makefile | 10 ++-- apps/ssl/app.h | 1 - apps/ssl/client.c | 20 ++++--- apps/ssl/server.c | 84 ++++++++++++++++++++-------- common.mk | 31 +++++----- core/Makefile | 2 - core/POSIX/osdep.c | 15 ++--- core/coreApi.h | 1 - core/coreConfig.h | 7 --- core/corelib.c | 2 - core/psmalloc.h | 1 - crypto/Makefile | 9 +-- crypto/cryptoApi.h | 6 -- crypto/cryptoConfig.h | 5 +- crypto/cryptolib.h | 3 - crypto/digest/digest_matrix.h | 1 - crypto/digest/sha256.c | 1 - crypto/keyformat/asn1.c | 22 ++------ crypto/keyformat/asn1.h | 2 - crypto/keyformat/base64.c | 1 - crypto/keyformat/pkcs.c | 1 - crypto/keyformat/x509.c | 9 +-- crypto/keyformat/x509.h | 1 - crypto/layer/layer.h | 4 -- crypto/layer/matrix.c | 2 - crypto/math/pstm.c | 27 ++++++--- crypto/math/pstm.h | 10 +--- crypto/math/pstm_montgomery_reduce.c | 4 +- crypto/prng/prng.c | 2 - crypto/prng/prng.h | 2 - crypto/pubkey/dh.c | 1 - crypto/pubkey/ecc.c | 35 +++++------- crypto/pubkey/rsa.c | 1 - crypto/symmetric/aes_matrix.h | 1 - crypto/symmetric/symmetric.h | 1 - crypto/test/Makefile | 25 +++------ crypto/test/algorithmTest.c | 14 ----- crypto/test/dhperf/dhperf.c | 1 - crypto/test/eccperf/eccperf.c | 2 +- crypto/test/rsaperf/rsaperf.c | 3 +- crypto/test/throughputTest.c | 7 ++- matrixssl/Makefile | 4 +- matrixssl/cipherSuite.c | 20 ++++--- matrixssl/dtls.c | 8 ++- matrixssl/extDecode.c | 4 -- matrixssl/hsDecode.c | 43 +++++--------- matrixssl/hsHash.c | 2 - matrixssl/matrixssl.c | 59 ++++++++----------- matrixssl/matrixsslApi.c | 3 - matrixssl/matrixsslApi.h | 1 - matrixssl/matrixsslCheck.h | 2 - matrixssl/matrixsslConfig.h | 3 - matrixssl/matrixssllib.h | 18 ------ matrixssl/prf.c | 4 +- matrixssl/sslDecode.c | 36 +++++++----- matrixssl/sslEncode.c | 38 ++++++------- matrixssl/test/Makefile | 10 ---- matrixssl/test/sslTest.c | 2 - matrixssl/tls.c | 1 - matrixssl/version.h | 2 +- 64 files changed, 306 insertions(+), 394 deletions(-) diff --git a/Makefile b/Makefile index af5dc1b..7911caa 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,38 @@ -# Recursively build the directories below, in one of modes -# fips, nonfips, combined -# $Format:%h%d$ -all: +# +# Recursively build the directories below +# @version $Format:%h%d$ +# + +ifneq (,$(findstring clean,$(MAKECMDGOALS))) + SUBARGS:=clean +endif + +default: all + +.PHONY: all libs tests apps clean + +all: libs tests apps + +libs: $(MAKE) --directory=core $(MAKE) --directory=crypto $(MAKE) --directory=matrixssl -test: +tests: $(MAKE) --directory=crypto/test $(MAKE) --directory=matrixssl/test +# Note apps is also a direct subdirectory +apps: + $(MAKE) --directory=apps/ssl + $(MAKE) --directory=apps/dtls + clean: $(MAKE) clean --directory=core $(MAKE) clean --directory=crypto - $(MAKE) clean --directory=crypto/test $(MAKE) clean --directory=matrixssl + $(MAKE) clean --directory=crypto/test $(MAKE) clean --directory=matrixssl/test + $(MAKE) clean --directory=apps/ssl + $(MAKE) clean --directory=apps/dtls + diff --git a/apps/dtls/Makefile b/apps/dtls/Makefile index 4ff6147..6d8527b 100755 --- a/apps/dtls/Makefile +++ b/apps/dtls/Makefile @@ -7,10 +7,10 @@ # MATRIXSSL_ROOT:=../.. + SERVER_SRC:=dtlsServer.c dtlsCommon.c CLIENT_SRC:=dtlsClient.c dtlsCommon.c -# Generated files SERVER_EXE:=dtlsServer$(E) CLIENT_EXE:=dtlsClient$(E) @@ -38,7 +38,6 @@ STATIC:=\ CIPHER_OPTION=ID_RSA # The default cipher option CFLAGS+=-D$(CIPHER_OPTION) - ifdef DTLS_PACKET_LOSS_TEST # Enable all packet-loss related tests CFLAGS+=-DDTLS_PACKET_LOSS_TEST -DTEST_DTLS_CLIENT_REHANDSHAKE=1 -DDTLS_TEST_LOST_CIPHERSPEC_CHANGE_REHANDSHAKE diff --git a/apps/dtls/dtlsClient.c b/apps/dtls/dtlsClient.c index 29cbc15..b725e9a 100644 --- a/apps/dtls/dtlsClient.c +++ b/apps/dtls/dtlsClient.c @@ -36,7 +36,6 @@ #ifdef USE_CLIENT_SIDE_SSL - static int packet_loss_prob = 0; /* Reciprocal of packet loss probability (i.e. P(packet loss) = 1/x). Default value is 0 (no packet loss). */ @@ -221,7 +220,6 @@ static int32 certCb(ssl_t *ssl, psX509Cert_t *cert, int32 alert); static void closeConn(sslDtls_t *dtls, SOCKET fd); static int32 sendHelloWorld(sslDtls_t *dtlsCtx); - /******************************************************************************/ /* Allocate the data structure to manage the socket and ssl combo of @@ -555,7 +553,9 @@ static int32 sendHelloWorld(sslDtls_t *dtlsCtx) avail = min(avail, len); strncpy((char*)buf, (char*)helloWorld, avail); - matrixSslEncodeWritebuf(dtlsCtx->ssl, avail); + if ((ret = matrixSslEncodeWritebuf(dtlsCtx->ssl, avail)) < 0) { + return ret; + } /* Get the encoded buffer and write it out */ @@ -563,8 +563,8 @@ static int32 sendHelloWorld(sslDtls_t *dtlsCtx) ret = (int32)sendto(dtlsCtx->fd, buf, len, 0, (struct sockaddr*)&dtlsCtx->addr, sizeof(struct sockaddr_in)); if (ret == -1) { - perror("sendto"); - exit(1); + perror("sendto"); + exit(1); } matrixDtlsSentData(dtlsCtx->ssl, len); } @@ -855,6 +855,7 @@ int32 main(int32 argc, char **argv) if (CAstreamLen > 0) { CAstream = psMalloc(NULL, CAstreamLen); } else { + /* coverity[dead_error_line] */ CAstream = NULL; } diff --git a/apps/dtls/dtlsServer.c b/apps/dtls/dtlsServer.c index 2440a70..8515adf 100644 --- a/apps/dtls/dtlsServer.c +++ b/apps/dtls/dtlsServer.c @@ -41,7 +41,6 @@ #include "dtlsCommon.h" #include "../../crypto/cryptoApi.h" - /* #define USE_CERT_VALIDATOR */ #define DTLS_PORT 4433 @@ -980,9 +979,9 @@ static int32 handleResends(SOCKET sock) ssl_t *ssl; psTime_t now; unsigned char *sslBuf; - int16 i; - int32 sendLen, rc; - uint32 timeout, sslBufLen, clientCount; + int16_t i; + int32_t sendLen, sslBufLen, rc; + uint32_t timeout, clientCount; clientCount = 0; /* return code is number of active clients or < 0 on error */ psGetTime(&now, NULL); @@ -1071,7 +1070,7 @@ static void setSocketOptions(SOCKET fd) static SOCKET newUdpSocket(char *ip, short port, int *err) { - struct sockaddr_in addr; + struct sockaddr_in addr = { 0 }; SOCKET fd; if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { @@ -1087,6 +1086,7 @@ static SOCKET newUdpSocket(char *ip, short port, int *err) if (ip == NULL) { addr.sin_addr.s_addr = INADDR_ANY; if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + close(fd); _psTrace("Can't bind socket. Port in use or permission problem\n"); *err = SOCKET_ERRNO; return INVALID_SOCKET; @@ -1234,9 +1234,10 @@ static void clearClient(serverDtls_t *dtls) /* Quick attempt to send a closure alert, don't worry about failure */ if (matrixSslEncodeClosureAlert(ssl) >= 0) { if ((len = matrixDtlsGetOutdata(ssl, &buf)) > 0) { - sendto(dtls->fd, buf, len, 0, (struct sockaddr*)&dtls->addr, - sizeof(struct sockaddr_in)); - matrixDtlsSentData(ssl, len); + if (sendto(dtls->fd, buf, len, 0, (struct sockaddr*)&dtls->addr, + sizeof(struct sockaddr_in)) >= 0) { + matrixDtlsSentData(ssl, len); + } } } matrixSslDeleteSession(ssl); diff --git a/apps/ssl/Makefile b/apps/ssl/Makefile index 8b17611..d4b8e9f 100755 --- a/apps/ssl/Makefile +++ b/apps/ssl/Makefile @@ -9,10 +9,11 @@ MATRIXSSL_ROOT:=../.. SERVER_SRC:=server.c http.c CLIENT_SRC:=client.c http.c +SRC=$(SERVER_SRC) $(CLIENT_SRC) -# Generated files SERVER_EXE:=server$(E) CLIENT_EXE:=client$(E) +EXE=$(SERVER_EXE) $(CLIENT_EXE) #The Mac OS X Xcode project has a target name of 'server' or 'client' ifneq (,$(TARGET_NAME)) @@ -25,8 +26,6 @@ ifneq (,$(TARGET_NAME)) endif endif -SRC:=$(SERVER_SRC) $(CLIENT_SRC) - include $(MATRIXSSL_ROOT)/common.mk # Linked files @@ -35,13 +34,12 @@ STATIC:=\ $(MATRIXSSL_ROOT)/crypto/libcrypt_s.a \ $(MATRIXSSL_ROOT)/core/libcore_s.a - CIPHER_OPTION=ID_RSA # The default cipher option CFLAGS+=-D$(CIPHER_OPTION) all: compile -compile: $(OBJS) $(SERVER_EXE) $(CLIENT_EXE) +compile: $(OBJS) $(EXE) # Additional Dependencies $(OBJS): $(MATRIXSSL_ROOT)/common.mk Makefile $(wildcard *.h) @@ -53,5 +51,5 @@ $(CLIENT_EXE): $(CLIENT_SRC:.c=.o) $(STATIC) $(CC) -o $@ $^ $(LDFLAGS) $(CFLAGS) clean: - rm -f $(SERVER_EXE) $(CLIENT_EXE) $(OBJS) TLS_*.tmp SSL_*.tmp + rm -f $(EXE) $(OBJS) TLS_*.tmp SSL_*.tmp diff --git a/apps/ssl/app.h b/apps/ssl/app.h index 9dd4a4c..182a3c4 100644 --- a/apps/ssl/app.h +++ b/apps/ssl/app.h @@ -67,7 +67,6 @@ extern "C" { #endif #endif /* WIN32 */ - /******************************************************************************/ /* Platform independent socket defines for convenience diff --git a/apps/ssl/client.c b/apps/ssl/client.c index bbb6a25..3de5393 100644 --- a/apps/ssl/client.c +++ b/apps/ssl/client.c @@ -876,7 +876,6 @@ int32 main(int32 argc, char **argv) return rc; } - if (matrixSslNewKeys(&keys, NULL) < 0) { _psTrace("MatrixSSL library key init failure. Exiting\n"); return -1; @@ -924,6 +923,7 @@ int32 main(int32 argc, char **argv) if (CAstreamLen > 0) { CAstream = psMalloc(NULL, CAstreamLen); } else { + /* coverity[dead_error_line] */ CAstream = NULL; } @@ -993,6 +993,7 @@ int32 main(int32 argc, char **argv) CAstream = psMalloc(NULL, CAstreamLen); memset(CAstream, 0x0, CAstreamLen); } else { + /* coverity[dead_error_line] */ CAstream = NULL; } @@ -1160,17 +1161,19 @@ int32 main(int32 argc, char **argv) static void closeConn(ssl_t *ssl, SOCKET fd) { unsigned char *buf; - int32 len; + int32 len, rc; if (g_send_closure_alert) { #if 1 /* Set the socket to non-blocking to flush remaining data */ #ifdef POSIX - fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + rc = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + psAssert(rc >= 0); #endif #ifdef WIN32 len = 1; /* 1 for non-block, 0 for block */ - ioctlsocket(fd, FIONBIO, &len); + rc = ioctlsocket(fd, FIONBIO, &len); + psAssert(rc); #endif /* Quick attempt to send a closure alert, don't worry about failure */ if (matrixSslEncodeClosureAlert(ssl) >= 0) { @@ -1505,7 +1508,8 @@ static SOCKET lsocketConnect(char *ip, int32 port, int32 *err) return INVALID_SOCKET; } #ifdef POSIX - fcntl(fd, F_SETFD, FD_CLOEXEC); + rc = fcntl(fd, F_SETFD, FD_CLOEXEC); + psAssert(rc >= 0); #endif #if 0 { @@ -1525,24 +1529,24 @@ static SOCKET lsocketConnect(char *ip, int32 port, int32 *err) } #endif #ifdef POSIX - rc = 1; +// rc = 1; // setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&rc, sizeof(rc)); // fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); #elif defined(WIN32) - rc = 1; /* 1 for non-block, 0 for block */ +// rc = 1; /* 1 for non-block, 0 for block */ // ioctlsocket(fd, FIONBIO, &rc); #endif #ifdef __APPLE__ /* MAC OS X */ rc = 1; setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&rc, sizeof(rc)); #endif - memset((char *) &addr, 0x0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons((short)port); addr.sin_addr.s_addr = inet_addr(ip); rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); if (rc < 0) { + close(fd); perror("connect()"); *err = SOCKET_ERRNO; } else { diff --git a/apps/ssl/server.c b/apps/ssl/server.c index 6969762..77bdd9d 100644 --- a/apps/ssl/server.c +++ b/apps/ssl/server.c @@ -92,7 +92,7 @@ static unsigned char g_httpResponseHdr[] = "HTTP/1.0 200 OK\r\n" static int32 selectLoop(sslKeys_t *keys, SOCKET lfd); static int32 httpWriteResponse(httpConn_t *conn); -static void setSocketOptions(SOCKET fd); +static int setSocketOptions(SOCKET fd); static SOCKET lsocketListen(short port, int32 *err); static void closeConn(httpConn_t *cp, int32 reason); @@ -116,7 +116,8 @@ static void displayStats(void) if (g_handshakes > s_handshakes) { t = time(NULL); if (t > s_t) { - printf("%llu CPS\n", (g_handshakes - s_handshakes) / (t - s_t)); + printf("%u CPS\n", + (uint32_t)(g_handshakes - s_handshakes) / (uint32_t)(t - s_t)); s_handshakes = g_handshakes; s_t = t; } @@ -215,8 +216,15 @@ static int32 selectLoop(sslKeys_t *keys, SOCKET lfd) if (fd == INVALID_SOCKET) { break; /* Nothing more to accept; next listener */ } - setSocketOptions(fd); + if (setSocketOptions(fd) < 0) { + close(fd); + return PS_PLATFORM_FAIL; + } cp = malloc(sizeof(httpConn_t)); + if (cp == NULL) { + close(fd); + return PS_MEM_FAIL; + } memset(cp, 0x0, sizeof(httpConn_t)); memset(&options, 0x0, sizeof(sslSessOpts_t)); @@ -224,12 +232,11 @@ static int32 selectLoop(sslKeys_t *keys, SOCKET lfd) if ((rc = matrixSslNewServerSession(&cp->ssl, keys, NULL, &options)) < 0) { - close(fd); fd = INVALID_SOCKET; + close(fd); continue; } cp->fd = fd; - fd = INVALID_SOCKET; cp->timeout = SSL_TIMEOUT; psGetTime(&cp->time, NULL); cp->parsebuf = NULL; @@ -406,7 +413,8 @@ PROCESS_MORE: if (len >= 15 && strncmp((char*)buf, "MATRIX_SHUTDOWN", 15) == 0) { g_exitFlag = 1; - matrixSslEncodeClosureAlert(cp->ssl); + rc = matrixSslEncodeClosureAlert(cp->ssl); + psAssert(rc >= 0); _psTrace("Got MATRIX_SHUTDOWN. Exiting\n"); goto WRITE_MORE; } @@ -428,7 +436,8 @@ PROCESS_MORE: close after parsing a single HTTP request */ /* Ignore return of closure alert, it's optional */ #ifdef SEND_CLOSURE_ALERT -// matrixSslEncodeClosureAlert(cp->ssl); +// rc = matrixSslEncodeClosureAlert(cp->ssl); +// psAssert(rc >= 0); #endif rc = matrixSslProcessedData(cp->ssl, &buf, (uint32*)&len); if (rc > 0) { @@ -513,11 +522,15 @@ static int32 httpWriteResponse(httpConn_t *cp) */ while (cp->bytes_sent < cp->bytes_requested) { len = cp->bytes_requested - cp->bytes_sent; + if (len < 0) { + return PS_MEM_FAIL; + } if (len > RESPONSE_REC_LEN) { len = RESPONSE_REC_LEN; } - psAssert(len > 0); - rc = matrixSslGetWritebuf(ssl, &buf, len); + if ((rc = matrixSslGetWritebuf(ssl, &buf, len)) < 1) { + return PS_MEM_FAIL; + } if (rc < len) { len = rc; /* could have been shortened due to max_frag */ } @@ -536,12 +549,11 @@ static int32 httpWriteResponse(httpConn_t *cp) generated records. We could flush after each record encode, or only on a multiple of record encodes. */ - if ((len = matrixSslGetOutdata(ssl, &buf)) > (RESPONSE_REC_LEN * 4)) { + if (matrixSslGetOutdata(ssl, &buf) > (RESPONSE_REC_LEN * 4)) { if ((len = (int32)send(cp->fd, buf, len, MSG_DONTWAIT)) > 0) { rc = matrixSslSentData(ssl, len); // psAssert(rc != MATRIXSSL_REQUEST_SEND); /* Some data remains */ } -// psAssert(len > 0); /* Probably an EWOULDBLOCK */ } } return MATRIXSSL_REQUEST_SEND; @@ -591,7 +603,6 @@ int32 main(int32 argc, char **argv) WSAStartup(MAKEWORD(1, 1), &wsaData); #endif - DLListInit(&g_conns); g_exitFlag = 0; lfd = INVALID_SOCKET; @@ -607,7 +618,6 @@ int32 main(int32 argc, char **argv) return rc; } - if (matrixSslNewKeys(&keys, NULL) < 0) { return -1; } @@ -638,6 +648,15 @@ int32 main(int32 argc, char **argv) displayStats(); } + /* Close any active connections */ + while (!DLListIsEmpty(&g_conns)) { + httpConn_t *cp; + DLListEntry *pList; + pList = DLListGetHead(&g_conns); + cp = DLListGetContainer(pList, httpConn_t, List); + closeConn(cp, PS_SUCCESS); + } + L_EXIT: if (lfd != INVALID_SOCKET) close(lfd); matrixSslClose(); @@ -693,7 +712,7 @@ static void closeConn(httpConn_t *cp, int32 reason) */ static SOCKET lsocketListen(short port, int32 *err) { - struct sockaddr_in addr; + struct sockaddr_in addr = { 0 }; SOCKET fd; if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { @@ -702,17 +721,21 @@ static SOCKET lsocketListen(short port, int32 *err) return INVALID_SOCKET; } - setSocketOptions(fd); - + if (setSocketOptions(fd) < 0) { + close(fd); + return INVALID_SOCKET; + } addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + close(fd); _psTrace("Can't bind socket. Port in use or insufficient privilege\n"); *err = SOCKET_ERRNO; return INVALID_SOCKET; } if (listen(fd, SOMAXCONN) < 0) { + close(fd); _psTrace("Error listening on socket\n"); *err = SOCKET_ERRNO; return INVALID_SOCKET; @@ -727,27 +750,40 @@ static SOCKET lsocketListen(short port, int32 *err) Set the REUSE flag to minimize the number of sockets in TIME_WAIT Then we set REUSEADDR, NODELAY and NONBLOCK on the socket */ -static void setSocketOptions(SOCKET fd) +static int setSocketOptions(SOCKET fd) { - int32 rc; + int rc; #ifdef POSIX - fcntl(fd, F_SETFD, FD_CLOEXEC); + if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { + return PS_PLATFORM_FAIL; + } #endif rc = 1; - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&rc, sizeof(rc)); + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&rc, sizeof(rc)) < 0) { + return PS_PLATFORM_FAIL; + } #ifdef POSIX rc = 1; - setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&rc, sizeof(rc)); - fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&rc, sizeof(rc)) < 0) { + return PS_PLATFORM_FAIL; + } + if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) { + return PS_PLATFORM_FAIL; + } #elif defined(WIN32) rc = 1; /* 1 for non-block, 0 for block */ - ioctlsocket(fd, FIONBIO, &rc); + if (ioctlsocket(fd, FIONBIO, &rc) < 0) { + return PS_PLATFORM_FAIL; + } #endif #ifdef __APPLE__ /* MAC OS X */ rc = 1; - setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&rc, sizeof(rc)); + if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&rc, sizeof(rc)) < 0) { + return PS_PLATFORM_FAIL; + } #endif + return PS_SUCCESS; } #ifdef POSIX diff --git a/common.mk b/common.mk index 893aa01..a730a69 100755 --- a/common.mk +++ b/common.mk @@ -1,6 +1,7 @@ ## -# Common Makefile definitions. -# Copyright (c) 2013-2016 INSIDE Secure Corporation. All Rights Reserved. +# Common Makefile definitions. +# @version $Format:%h%d$ +# Copyright (c) 2013-2016 INSIDE Secure Corporation. All Rights Reserved. # #------------------------------------------------------------------------------- @@ -94,12 +95,12 @@ else endif CFLAGS+=$(OPT) -# Detect multicore and do parallel build. Uncomment if desired -#ifneq (,$(findstring -linux,$(CCARCH))) -# JOBS:=-j$(shell grep -ic processor /proc/cpuinfo) -#else ifneq (,$(findstring apple,$(CCARCH))) -# JOBS:=-j$(shell sysctl -n machdep.cpu.thread_count) -#endif +# Detect multicore and do parallel build. Uncomment if desired: +#> ifneq (,$(findstring -linux,$(CCARCH))) +#> JOBS:=-j$(shell grep -ic processor /proc/cpuinfo) +#> else ifneq (,$(findstring apple,$(CCARCH))) +#> JOBS:=-j$(shell sysctl -n machdep.cpu.thread_count) +#> endif default: $(BUILD) @@ -212,9 +213,8 @@ endif CFLAGS+=-I$(MATRIXSSL_ROOT) -#OPENSSL -#PS_OPENSSL:=1 -ifdef PS_OPENSSL +#USE_OPENSSL_CRYPTO:=1 +ifdef USE_OPENSSL_CRYPTO OPENSSL_ROOT:=/opt/openssl-1.0.2d ifdef OPENSSL_ROOT # Statically link against a given openssl tree @@ -234,12 +234,10 @@ ifdef PS_OPENSSL CFLAGS+=-DUSE_OPENSSL_CRYPTO STROPTS+=", USE_OPENSSL_CRYPTO" endif -#OPENSSL -#LIBSODIUM -#PS_LIBSODIUM:=1 -ifdef PS_LIBSODIUM - LIBSODIUM_ROOT:=/opt/libsodium-1.0.10/src/libsodium +#USE_LIBSODIUM_CRYPTO:=1 +ifdef USE_LIBSODIUM_CRYPTO + LIBSODIUM_ROOT:=/opt/libsodium-1.0.8/src/libsodium ifdef LIBSODIUM_ROOT # Statically link against a given libsodium CFLAGS+=-I$(LIBSODIUM_ROOT)/include @@ -250,7 +248,6 @@ ifdef PS_LIBSODIUM CFLAGS+=-DUSE_LIBSODIUM_CRYPTO STROPTS+=", USE_LIBSODIUM_CRYPTO" endif -#LIBSODIUM # Linux Target ifneq (,$(findstring -linux,$(CCARCH))) diff --git a/core/Makefile b/core/Makefile index c431cf4..0688979 100755 --- a/core/Makefile +++ b/core/Makefile @@ -12,8 +12,6 @@ SRC:=\ corelib.c \ $(OSDEP)/osdep.c - - ASM:=memset_s.s # Generated files diff --git a/core/POSIX/osdep.c b/core/POSIX/osdep.c index 9135657..861ff3f 100644 --- a/core/POSIX/osdep.c +++ b/core/POSIX/osdep.c @@ -476,9 +476,9 @@ void osdepBreak(void) int32 psGetFileBuf(psPool_t *pool, const char *fileName, unsigned char **buf, int32 *bufLen) { - FILE *fp; - struct stat fstat; - size_t tmp = 0; + FILE *fp; + struct stat f_stat; + size_t tmp = 0; *bufLen = 0; *buf = NULL; @@ -486,19 +486,20 @@ int32 psGetFileBuf(psPool_t *pool, const char *fileName, unsigned char **buf, if (fileName == NULL) { return PS_ARG_FAIL; } - if ((stat(fileName, &fstat) != 0) || (fp = fopen(fileName, "r")) == NULL) { + if ((fp = fopen(fileName, "r")) == NULL || fstat(fileno(fp), &f_stat) != 0) { psTraceStrCore("Unable to open %s\n", (char*)fileName); return PS_PLATFORM_FAIL; } - *buf = psMalloc(pool, (size_t)(fstat.st_size + 1)); + *buf = psMalloc(pool, (size_t)(f_stat.st_size + 1)); if (*buf == NULL) { + fclose(fp); return PS_MEM_FAIL; } - memset(*buf, 0x0, (size_t)fstat.st_size + 1); + memset(*buf, 0x0, (size_t)f_stat.st_size + 1); while (((tmp = fread(*buf + *bufLen, sizeof(char), 512, fp)) > 0) && - (*bufLen < fstat.st_size)) { + (*bufLen < f_stat.st_size)) { *bufLen += (int32)tmp; } fclose(fp); diff --git a/core/coreApi.h b/core/coreApi.h index 7f54f89..ae39025 100644 --- a/core/coreApi.h +++ b/core/coreApi.h @@ -142,7 +142,6 @@ PSPUBLIC void psCoreClose(void); PSPUBLIC void psBurnStack(uint32 len); PSPUBLIC int32 memcmpct(const void *s1, const void *s2, size_t len); - /******************************************************************************/ /* Public interface to OS-dependant core functionality diff --git a/core/coreConfig.h b/core/coreConfig.h index 60e85ca..c48aca9 100644 --- a/core/coreConfig.h +++ b/core/coreConfig.h @@ -35,7 +35,6 @@ #ifndef _h_PS_CORECONFIG #define _h_PS_CORECONFIG - /******************************************************************************/ /* Configurable features */ /******************************************************************************/ @@ -60,16 +59,10 @@ //#define HALT_ON_PS_ERROR /* NOT RECOMMENDED FOR PRODUCTION BUILDS */ #endif - /** Include the psCoreOsdepMutex family of APIs */ -#ifndef NO_MULTITHREADING //#define USE_MULTITHREADING -#endif /* NO_MULTITHREADING */ - - - #endif /* _h_PS_CORECONFIG */ diff --git a/core/corelib.c b/core/corelib.c index 3fac4a7..57c5675 100644 --- a/core/corelib.c +++ b/core/corelib.c @@ -56,7 +56,6 @@ int32 psCoreOpen(const char *config) return -1; } - if (osdepTimeOpen() < 0) { psTraceCore("osdepTimeOpen failed\n"); return PS_FAILURE; @@ -76,7 +75,6 @@ int32 psCoreOpen(const char *config) } #endif /* USE_MULTITHREADING */ - return PS_SUCCESS; } diff --git a/core/psmalloc.h b/core/psmalloc.h index 1268fdb..e101dbc 100644 --- a/core/psmalloc.h +++ b/core/psmalloc.h @@ -70,7 +70,6 @@ typedef int32 psPool_t; - /******************************************************************************/ #endif /* !PS_UNSUPPORTED_OS */ diff --git a/crypto/Makefile b/crypto/Makefile index 2988150..36a4d6d 100755 --- a/crypto/Makefile +++ b/crypto/Makefile @@ -42,21 +42,17 @@ SRC:=\ pubkey/ecc.c \ pubkey/pubkey.c \ pubkey/rsa.c -#OPENSSL -ifdef PS_OPENSSL +ifdef USE_OPENSSL_CRYPTO SRC+=\ digest/digest_openssl.c \ symmetric/symmetric_openssl.c \ pubkey/rsa_openssl.c endif -#OPENSSL -#LIBSODIUM -ifdef PS_LIBSODIUM +ifdef USE_LIBSODIUM_CRYPTO SRC+=\ symmetric/symmetric_libsodium.c \ digest/digest_libsodium.c endif -#LIBSODIUM # Generated files STATIC:=libcrypt_s.a @@ -80,3 +76,4 @@ clean: # Allows to check configuration options. parse-config: echo '#include "cryptoConfig.h"' | $(CC) $(CFLAGS) -dM -E -x c - + diff --git a/crypto/cryptoApi.h b/crypto/cryptoApi.h index 9421a38..af0323e 100644 --- a/crypto/cryptoApi.h +++ b/crypto/cryptoApi.h @@ -210,8 +210,6 @@ PSPUBLIC void psAesClearGCM(psAesGcm_t *ctx); #endif /* USE_AES_GCM */ - - #endif /* USE_AES */ #ifdef USE_CHACHA20_POLY1305 @@ -443,8 +441,6 @@ PSPUBLIC void psHmacSha384Final(psHmacSha384_t *ctx, unsigned char hash[SHA384_HASHLEN]); #endif - - /******************************************************************************/ /* Private Key Parsing @@ -530,7 +526,6 @@ PSPUBLIC int32_t psRsaDecryptPub(psPool_t *pool, psRsaKey_t *key, unsigned char *out, uint16_t outlen, void *data); - PSPUBLIC int32_t psRsaCrypt(psPool_t *pool, psRsaKey_t *key, const unsigned char *in, uint16_t inlen, unsigned char *out, uint16_t *outlen, @@ -678,7 +673,6 @@ PSPUBLIC int32_t psInitPrng(psRandom_t *ctx, void *userPtr); PSPUBLIC int32_t psGetPrng(psRandom_t *ctx, unsigned char *bytes, uint16_t size, void *userPtr); - /******************************************************************************/ /* Deprecated Algorithms diff --git a/crypto/cryptoConfig.h b/crypto/cryptoConfig.h index 73ee731..05dbc2e 100644 --- a/crypto/cryptoConfig.h +++ b/crypto/cryptoConfig.h @@ -62,8 +62,6 @@ #define USE_BURN_STACK /**< @security Zero sensitive data from the stack. */ - - /******************************************************************************/ /** Public-Key Algorithm Support. @@ -105,6 +103,7 @@ */ #define USE_AES_CBC #define USE_AES_GCM +//#define USE_AES_GCM_GIV #ifdef USE_LIBSODIUM_CRYPTO #define USE_CHACHA20_POLY1305 @@ -155,7 +154,6 @@ #define USE_MD5SHA1 /* Required for < TLS 1.2 Handshake */ #define USE_HMAC_MD5 /* TODO currently needed for prf */ - /******************************************************************************/ /** X.509 Certificates/PKI @@ -182,7 +180,6 @@ //#define USE_PKCS1_OAEP /* OAEP padding algorithm */ //#define USE_PKCS1_PSS /* PSS padding algorithm */ - #endif /* _h_PS_CRYPTOCONFIG */ /******************************************************************************/ diff --git a/crypto/cryptolib.h b/crypto/cryptolib.h index 7530701..b4a65f3 100644 --- a/crypto/cryptolib.h +++ b/crypto/cryptolib.h @@ -68,7 +68,6 @@ */ #include "layer/layer.h" - /* Configuration validation/sanity checks */ #include "cryptoCheck.h" @@ -552,8 +551,6 @@ x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48)| \ BLOCKSIZE <= 1 ? (unsigned char)0 : \ (unsigned char)(BLOCKSIZE - ((LEN) & (BLOCKSIZE - 1))) - - #endif /* _h_PS_CRYPTOLIB */ /******************************************************************************/ diff --git a/crypto/digest/digest_matrix.h b/crypto/digest/digest_matrix.h index 69839ed..82fcb61 100644 --- a/crypto/digest/digest_matrix.h +++ b/crypto/digest/digest_matrix.h @@ -119,7 +119,6 @@ typedef struct { } psMd2_t; #endif - /******************************************************************************/ #ifdef USE_MATRIX_HMAC_MD5 diff --git a/crypto/digest/sha256.c b/crypto/digest/sha256.c index 97fcab4..7a0387d 100644 --- a/crypto/digest/sha256.c +++ b/crypto/digest/sha256.c @@ -68,7 +68,6 @@ static const uint32_t K[64] = { #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) - /* compress 512-bits */ diff --git a/crypto/keyformat/asn1.c b/crypto/keyformat/asn1.c index fc04f38..81b04f7 100644 --- a/crypto/keyformat/asn1.c +++ b/crypto/keyformat/asn1.c @@ -44,15 +44,13 @@ */ int32_t getAsnLength(const unsigned char **pp, uint16_t size, uint16_t *len) { - uint32_t len32; + uint32_t len32 = 0; int32_t rc; - len32 = *len; if ((rc = getAsnLength32(pp, size, &len32, 0)) < 0) { return rc; } - /* @note len32 is < size here, so it is <= 0xFFFF */ - *len = (uint16_t)len32; + *len = (uint16_t)(len32 & 0xFFFF); return PS_SUCCESS; } @@ -172,15 +170,13 @@ int32_t getAsnSequence32(const unsigned char **pp, uint32_t size, int32_t getAsnSequence(const unsigned char **pp, uint16_t size, uint16_t *len) { - uint32_t len32; + uint32_t len32 = 0; int32_t rc; - len32 = *len; if ((rc = getAsnSequence32(pp, size, &len32, 0)) < 0) { return rc; } - /* @note len32 is < size here, so it is <= 0xFFFF */ - *len = (uint16_t)len32; + *len = (uint16_t)(len32 & 0xFFFF); return PS_SUCCESS; } @@ -211,15 +207,13 @@ int32_t getAsnSet32(const unsigned char **pp, uint32_t size, uint32_t *len, int32_t getAsnSet(const unsigned char **pp, uint16_t size, uint16_t *len) { - uint32_t len32; + uint32_t len32 = 0; int32_t rc; - len32 = *len; if ((rc = getAsnSet32(pp, size, &len32, 0)) < 0) { return rc; } - /* @note len32 is < size here, so it is <= 0xFFFF */ - *len = (uint16_t)len32; + *len = (uint16_t)(len32 & 0xFFFF); return PS_SUCCESS; } /******************************************************************************/ @@ -330,7 +324,6 @@ int32_t getAsnInteger(const unsigned char **pp, uint32_t len, int32_t *val) return PS_SUCCESS; } - /******************************************************************************/ /* Implementation specific OID parser @@ -417,8 +410,5 @@ int32_t getAsnOID(const unsigned char **pp, uint32_t len, int32_t *oi, return PS_SUCCESS; } - -#ifdef USE_RSA -#endif /* USE_RSA */ /******************************************************************************/ diff --git a/crypto/keyformat/asn1.h b/crypto/keyformat/asn1.h index 6600f34..ca41c22 100644 --- a/crypto/keyformat/asn1.h +++ b/crypto/keyformat/asn1.h @@ -98,8 +98,6 @@ extern int32_t getAsnOID(const unsigned char **pp, uint32_t len, int32_t *oi, /******************************************************************************/ - - #endif /* _h_PS_ASN1 */ /******************************************************************************/ diff --git a/crypto/keyformat/base64.c b/crypto/keyformat/base64.c index 98e2095..202b819 100644 --- a/crypto/keyformat/base64.c +++ b/crypto/keyformat/base64.c @@ -128,6 +128,5 @@ int32_t psBase64decode(const unsigned char *in, uint16_t len, /******************************************************************************/ - /******************************************************************************/ diff --git a/crypto/keyformat/pkcs.c b/crypto/keyformat/pkcs.c index c118c77..74c333f 100644 --- a/crypto/keyformat/pkcs.c +++ b/crypto/keyformat/pkcs.c @@ -2666,6 +2666,5 @@ LBL_DB: /******************************************************************************/ - /******************************************************************************/ diff --git a/crypto/keyformat/x509.c b/crypto/keyformat/x509.c index 76b30e8..c482d58 100644 --- a/crypto/keyformat/x509.c +++ b/crypto/keyformat/x509.c @@ -245,7 +245,7 @@ static int32_t pemCertFileBufToX509(psPool_t *pool, const unsigned char *fileBuf *x509certList = NULL; prev = NULL; - if (fileBufLen < 0 || fileBuf == NULL) { + if (fileBuf == NULL) { psTraceCrypto("Bad parameters to pemCertFileBufToX509\n"); return PS_ARG_FAIL; } @@ -2668,7 +2668,9 @@ MORE_IN_SET: case ASN_PRINTABLESTRING: case ASN_UTF8STRING: case ASN_IA5STRING: + /* coverity[unterminated_case] */ checkHiddenNull = PS_TRUE; + /* fall through */ case ASN_T61STRING: case ASN_BMPSTRING: case ASN_BIT_STRING: @@ -3079,7 +3081,6 @@ int32 psX509AuthenticateCert(psPool_t *pool, psX509Cert_t *subjectCert, if ((rc = psRsaDecryptPub(pkiPool, &ic->publicKey.key.rsa, tempSig, sc->signatureLen, sigOut, sigLen, rsaData)) < 0) { - psTraceCrypto("Unable to RSA decrypt certificate signature\n"); sc->authStatus = PS_CERT_AUTH_FAIL_SIG; psFree(tempSig, pool); @@ -3139,7 +3140,6 @@ int32 psX509AuthenticateCert(psPool_t *pool, psX509Cert_t *subjectCert, } #endif /* USE_ECC */ - /* Test what happen in the signature test? */ @@ -3605,7 +3605,6 @@ int32 psX509ParseCrl(psPool_t *pool, psX509Cert_t *CA, int append, } - if ((rc = pubRsaDecryptSignedElement(pkiPool, &CA->publicKey.key.rsa, revStart, ilen, sigOut, sigLen, NULL)) < 0) { x509FreeRevoked(&CA->revoked); @@ -3613,7 +3612,6 @@ int32 psX509ParseCrl(psPool_t *pool, psX509Cert_t *CA, int append, return rc; } - if (memcmp(sigHash, sigOut, sigLen) != 0) { x509FreeRevoked(&CA->revoked); psTraceCrypto("Unable to verify CRL signature\n"); @@ -4411,7 +4409,6 @@ int32_t validateOCSPResponse(psPool_t *pool, psX509Cert_t *trustedOCSP, } #endif - /* Was able to successfully confirm OCSP signature for our subject */ return PS_SUCCESS; } diff --git a/crypto/keyformat/x509.h b/crypto/keyformat/x509.h index a5a4e5c..bc0910f 100644 --- a/crypto/keyformat/x509.h +++ b/crypto/keyformat/x509.h @@ -438,7 +438,6 @@ extern int32_t validateOCSPResponse(psPool_t *pool, psX509Cert_t *trustedOCSP, /******************************************************************************/ - /******************************************************************************/ #endif /* USE_X509 */ diff --git a/crypto/layer/layer.h b/crypto/layer/layer.h index 92b5644..d258bfa 100644 --- a/crypto/layer/layer.h +++ b/crypto/layer/layer.h @@ -137,7 +137,6 @@ #define USE_MATRIX_PRNG #endif - #ifdef USE_LIBSODIUM_CRYPTO /******************************************************************************/ /** @@ -259,9 +258,6 @@ #endif #endif /* __AES__ */ - - - /******************************************************************************/ /* Enable algorithm optimizations based on the compiler optimization settings. diff --git a/crypto/layer/matrix.c b/crypto/layer/matrix.c index 584ea50..2a9218c 100644 --- a/crypto/layer/matrix.c +++ b/crypto/layer/matrix.c @@ -76,6 +76,4 @@ void psCryptoClose(void) } } - - /******************************************************************************/ diff --git a/crypto/math/pstm.c b/crypto/math/pstm.c index 5548377..8e8f7df 100644 --- a/crypto/math/pstm.c +++ b/crypto/math/pstm.c @@ -34,10 +34,7 @@ #include "../cryptoApi.h" -#if defined(USE_MATRIX_RSA) || defined(USE_CL_RSA) \ - || defined(USE_MATRIX_ECC) \ - || defined(USE_MATRIX_DH) || defined(USE_CL_DH) \ - || defined(USE_QUICK_ASSIST_RSA) || defined(USE_QUICK_ASSIST_ECC) +#if defined(USE_MATRIX_RSA) || defined(USE_MATRIX_ECC) || defined(USE_MATRIX_DH) || defined(USE_CL_RSA) || defined(USE_CL_DH) || defined(USE_QUICK_ASSIST_RSA) || defined(USE_QUICK_ASSIST_ECC) static int32_t pstm_mul_2d(const pstm_int *a, int16_t b, pstm_int *c); @@ -167,6 +164,20 @@ int32_t pstm_copy(const pstm_int * a, pstm_int * b) return PSTM_OKAY; } +/******************************************************************************/ +/** + b = |a|. + Copy 'a' to 'b' and make positive. +*/ +int32_t pstm_abs(const pstm_int *a, pstm_int *b) +{ + if (pstm_copy(a, b) != PSTM_OKAY) { + return PSTM_MEM; + } + b->sign = 0; + return PSTM_OKAY; +} + /******************************************************************************/ /** Trim unused digits. @@ -2242,7 +2253,9 @@ int32_t pstm_invmod(psPool_t *pool, const pstm_int *a, const pstm_int *b, pstm_i } /* we need y = |a| */ - pstm_abs(a, &y); + if ((res = pstm_abs(a, &y)) != PSTM_OKAY) { + goto LBL_X; + } /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ if ((res = pstm_init_copy(pool, &u, &x, 0)) != PSTM_OKAY) { @@ -2360,9 +2373,7 @@ LBL_X: pstm_clear(&x); /******************************************************************************/ - -#endif /* USE_MATRIX_RSA || USE_CL_RSA || USE_MATRIX_ECC || USE_MATRIX_DH \ - || USE_CL_DH || USE_QUICK_ASSIST_RSA || USE_QUICK_ASSIST_ECC */ +#endif /* USE_MATRIX_RSA || USE_MATRIX_ECC || USE_MATRIX_DH || USE_CL_RSA || USE_CL_DH || USE_QUICK_ASSIST_RSA || USE_QUICK_ASSIST_ECC */ /******************************************************************************/ diff --git a/crypto/math/pstm.h b/crypto/math/pstm.h index ef30fc6..fd74fbd 100644 --- a/crypto/math/pstm.h +++ b/crypto/math/pstm.h @@ -37,10 +37,7 @@ #include "../cryptoApi.h" -#if defined(USE_MATRIX_RSA) || defined(USE_CL_RSA) \ - || defined(USE_MATRIX_ECC) \ - || defined(USE_MATRIX_DH) || defined(USE_CL_DH) \ - || defined(USE_QUICK_ASSIST_RSA) || defined(USE_QUICK_ASSIST_ECC) +#if defined(USE_MATRIX_RSA) || defined(USE_MATRIX_ECC) || defined(USE_MATRIX_DH) || defined(USE_CL_RSA) || defined(USE_CL_DH) || defined(USE_QUICK_ASSIST_RSA) || defined(USE_QUICK_ASSIST_ECC) #if defined(PS_PUBKEY_OPTIMIZE_FOR_FASTER_SPEED) && defined(PS_PUBKEY_OPTIMIZE_FOR_SMALLER_RAM) #error "May only enable either PS_PUBKEY_OPTIMIZE_FOR_FASTER_SPEED or PS_PUBKEY_OPTIMIZE_FOR_SMALLER_RAM" @@ -155,7 +152,6 @@ typedef struct { #define pstm_iszero(a) (((a)->used == 0) ? PS_TRUE : PS_FALSE) #define pstm_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? PS_TRUE : PS_FALSE) #define pstm_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? PS_TRUE : PS_FALSE) -#define pstm_abs(a, b) { pstm_copy(a, b); (b)->sign = 0; } extern void pstm_set(pstm_int *a, pstm_digit b); extern void pstm_zero(pstm_int *a); @@ -172,6 +168,7 @@ extern void pstm_clamp(pstm_int *a); extern int32_t pstm_copy(const pstm_int *a, pstm_int *b); extern void pstm_exch(pstm_int *a, pstm_int *b); +extern int32_t pstm_abs(const pstm_int *a, pstm_int *b); extern void pstm_clear(pstm_int *a); extern void pstm_clear_multi( @@ -238,8 +235,7 @@ extern int32_t pstm_montgomery_reduce(psPool_t *pool, pstm_int *a, const pstm_in pstm_digit mp, pstm_digit *paD, uint16_t paDlen); extern int32_t pstm_montgomery_calc_normalization(pstm_int *a, const pstm_int *b); +#endif /* USE_MATRIX_RSA || USE_MATRIX_ECC || USE_MATRIX_DH || USE_CL_RSA || USE_CL_DH || USE_QUICK_ASSIST_RSA || USE_QUICK_ASSIST_ECC */ -#endif /* USE_MATRIX_RSA || USE_CL_RSA || USE_MATRIX_ECC || USE_MATRIX_DH \ - || USE_CL_DH || USE_QUICK_ASSIST_RSA || USE_QUICK_ASSIST_ECC */ #endif /* _h_PSTMATH */ diff --git a/crypto/math/pstm_montgomery_reduce.c b/crypto/math/pstm_montgomery_reduce.c index 0fd1466..c02a980 100644 --- a/crypto/math/pstm_montgomery_reduce.c +++ b/crypto/math/pstm_montgomery_reduce.c @@ -261,7 +261,6 @@ asm( \ :"r0","%cc"); #endif /* __thumb2__ */ - /******************************************************************************/ #elif defined(PSTM_MIPS) /* MIPS32 */ @@ -300,7 +299,6 @@ asm( \ :"r"(cy),"r"(_c[0])\ :"$10"); - /******************************************************************************/ #else @@ -414,7 +412,7 @@ int32_t pstm_montgomery_reduce(psPool_t *pool, pstm_int *a, const pstm_int *m, x = PS_MEM_FAIL; } } - if (paDlen < cSize) { + if (c && c != paD) { psFree(c, pool); } return x; diff --git a/crypto/prng/prng.c b/crypto/prng/prng.c index 8aba720..863d759 100644 --- a/crypto/prng/prng.c +++ b/crypto/prng/prng.c @@ -104,7 +104,6 @@ int32_t psInitPrng(psRandom_t *ctx, void *userPtr) ctx->bytecount = 0; - #if defined(USE_FORTUNA) || defined(USE_YARROW) if ((rc = psGetEntropy(entropyBytes, RANDOM_ENTROPY_BYTES, userPtr)) < 0) { return rc; @@ -144,7 +143,6 @@ static int32_t readRandomData(psRandom_t *ctx, unsigned char *bytes, uint16_t si ctx->bytecount += size; - #ifdef USE_YARROW if (ctx->bytecount >= RANDOM_BYTES_BEFORE_ENTROPY) { ctx->bytecount = 0; diff --git a/crypto/prng/prng.h b/crypto/prng/prng.h index bb6b2b6..d0c3ece 100644 --- a/crypto/prng/prng.h +++ b/crypto/prng/prng.h @@ -35,7 +35,6 @@ #ifndef _h_PS_PRNG #define _h_PS_PRNG - #ifdef USE_YARROW /* AES SHA-1 implementation @@ -74,7 +73,6 @@ typedef struct { uint32 bytecount; /* number of bytes read from this context */ } psRandom_t; - /******************************************************************************/ #endif /* _h_PS_PRNG */ diff --git a/crypto/pubkey/dh.c b/crypto/pubkey/dh.c index e1cc9c7..a5ec6f8 100644 --- a/crypto/pubkey/dh.c +++ b/crypto/pubkey/dh.c @@ -403,7 +403,6 @@ int32_t psDhGenSharedSecret(psPool_t *pool, return PS_ARG_FAIL; } - /* compute y^x mod p */ if ((err = pstm_init(pool, &tmp)) != PS_SUCCESS) { return err; diff --git a/crypto/pubkey/ecc.c b/crypto/pubkey/ecc.c index 5371562..7463a0f 100644 --- a/crypto/pubkey/ecc.c +++ b/crypto/pubkey/ecc.c @@ -449,8 +449,6 @@ int32_t getEcPubKey(psPool_t *pool, const unsigned char **pp, uint16_t len, return 0; } - - /** Initialize an ECC key and generate a public/private keypair for the given curve. @@ -579,7 +577,6 @@ RETRY_RAND: goto ERR_BASE; } - /* make the public key */ if (pstm_init_size(pool, &key->pubkey.x, (key->k.used * 2) + 1) < 0) { err = PS_MEM_FAIL; @@ -1009,7 +1006,7 @@ static uint8_t get_digit_count(const pstm_int *a) static pstm_digit get_digit(const pstm_int *a, uint8_t n) { - return (n >= a->used || n < 0) ? (pstm_digit)0 : a->dp[n]; + return (n >= a->used) ? (pstm_digit)0 : a->dp[n]; } /******************************************************************************/ @@ -1387,8 +1384,8 @@ int32_t psEccX963ImportKey(psPool_t *pool, case ANSI_COMPRESSED1: case ANSI_HYBRID0: case ANSI_HYBRID1: - psTraceCrypto("ERROR: ECC compressed/hybrid formats unsupported\n"); default: + psTraceCrypto("ERROR: ECC compressed/hybrid formats unsupported\n"); err = PS_UNSUPPORTED_FAIL; goto error; } @@ -2121,7 +2118,6 @@ done: return err; } - /******************************************************************************/ /** Allocate a new ECC point. @@ -2138,32 +2134,33 @@ static psEccPoint_t *eccNewPoint(psPool_t *pool, short size) p->pool = pool; if (size == 0) { if (pstm_init(pool, &p->x) != PSTM_OKAY) { - return NULL; + goto ERR; } if (pstm_init(pool, &p->y) != PSTM_OKAY) { - pstm_clear(&p->x); - return NULL; + goto ERR_X; } if (pstm_init(pool, &p->z) != PSTM_OKAY) { - pstm_clear(&p->x); - pstm_clear(&p->y); - return NULL; + goto ERR_Y; } } else { if (pstm_init_size(pool, &p->x, size) != PSTM_OKAY) { - return NULL; + goto ERR; } if (pstm_init_size(pool, &p->y, size) != PSTM_OKAY) { - pstm_clear(&p->x); - return NULL; + goto ERR_X; } if (pstm_init_size(pool, &p->z, size) != PSTM_OKAY) { - pstm_clear(&p->x); - pstm_clear(&p->y); - return NULL; + goto ERR_Y; } } return p; +ERR_Y: + pstm_clear(&p->y); +ERR_X: + pstm_clear(&p->x); +ERR: + psFree(p, pool); + return NULL; } /** @@ -2304,7 +2301,6 @@ int32_t psEccDsaVerify(psPool_t *pool, const psEccKey_t *key, return err; } - /* allocate ints */ radlen = key->curve->size * 2; if (pstm_init_for_read_unsigned_bin(pool, &p, key->curve->size) < 0) { @@ -2690,6 +2686,5 @@ LBL_P: return err; } - #endif /* USE_MATRIX_ECC */ diff --git a/crypto/pubkey/rsa.c b/crypto/pubkey/rsa.c index 7483715..2a354ff 100644 --- a/crypto/pubkey/rsa.c +++ b/crypto/pubkey/rsa.c @@ -855,7 +855,6 @@ int32_t psRsaDecryptPub(psPool_t *pool, psRsaKey_t *key, return PS_SUCCESS; } - #endif /* USE_MATRIX_RSA */ /******************************************************************************/ diff --git a/crypto/symmetric/aes_matrix.h b/crypto/symmetric/aes_matrix.h index 5cb2d9c..801dd1b 100644 --- a/crypto/symmetric/aes_matrix.h +++ b/crypto/symmetric/aes_matrix.h @@ -86,7 +86,6 @@ typedef struct { } psAesGcm_t; #endif - /******************************************************************************/ #ifdef USE_MATRIX_3DES diff --git a/crypto/symmetric/symmetric.h b/crypto/symmetric/symmetric.h index 8a2e9cb..cc743a4 100644 --- a/crypto/symmetric/symmetric.h +++ b/crypto/symmetric/symmetric.h @@ -59,7 +59,6 @@ #define MAX_KEYLEN AES256_KEYLEN #define AES_MAXKEYLEN AES256_KEYLEN - /******************************************************************************/ /* Layer includes */ diff --git a/crypto/test/Makefile b/crypto/test/Makefile index ccb4780..54e34d9 100755 --- a/crypto/test/Makefile +++ b/crypto/test/Makefile @@ -8,23 +8,23 @@ # MATRIXSSL_ROOT:=../.. + VECTOR_SRC:=algorithmTest.c SPEED_SRC:=throughputTest.c -SRC:=$(VECTOR_SRC) $(SPEED_SRC) +SRC:=$(VECTOR_SRC) $(SPEED_SRC) + +VECTOR_EXE:=algorithmTest$(E) +SPEED_EXE:=throughputTest$(E) +EXE:=$(VECTOR_EXE) $(SPEED_EXE) include $(MATRIXSSL_ROOT)/common.mk -# Generated files -VECTOR_EXE:=algorithmTest$(E) -SPEED_EXE:=throughputTest$(E) - # Linked files STATICS:=../libcrypt_s.a $(MATRIXSSL_ROOT)/core/libcore_s.a -#STATICS+=$(MATRIXSSL_ROOT)/matrixssl/libssl_s.a all: compile -compile: $(OBJS) $(SPEED_EXE) $(VECTOR_EXE) +compile: $(OBJS) $(EXE) $(MAKE) --directory=rsaperf $(MAKE) --directory=eccperf $(MAKE) --directory=dhperf @@ -38,17 +38,8 @@ $(SPEED_EXE): $(SPEED_SRC:.c=.o) $(STATICS) $(VECTOR_EXE): $(VECTOR_SRC:.c=.o) $(STATICS) $(CC) -o $@ $^ $(LDFLAGS) - -ifneq (,$(findstring -octeon,$(CCARCH))) - SIM:=oct-sim - SIMFLAGS:=-quiet -numcores=1 -noperf -endif - -test: - $(SIM) ./$(VECTOR_EXE) $(SIMFLAGS) - clean: - rm -f $(VECTOR_EXE) $(SPEED_EXE) $(OBJS) + rm -f $(EXE) $(OBJS) $(MAKE) clean --directory=rsaperf $(MAKE) clean --directory=eccperf $(MAKE) clean --directory=dhperf diff --git a/crypto/test/algorithmTest.c b/crypto/test/algorithmTest.c index bfa35f0..3d689b4 100644 --- a/crypto/test/algorithmTest.c +++ b/crypto/test/algorithmTest.c @@ -86,11 +86,6 @@ static int32 psPrngTests(void) return res < 0 ? res : PS_SUCCESS; } - - - - - /******************************************************************************/ #ifdef USE_AES #define AES_ITER 1000 /* For AES Block mode test */ @@ -893,10 +888,6 @@ int32 psAesTestGCM(void) } #endif /* USE_AES_GCM */ - - - - #endif /* USE_AES */ #if 0 @@ -2474,7 +2465,6 @@ static int32 psRsaEncryptTest(void) i++) { _psTraceInt(" %d bit test...", rsa[i].size * 8); - /* Start with getting both key halfs from the same source */ if (psRsaInitKey(pool, &privkey) < 0) { return -1; @@ -2519,7 +2509,6 @@ static int32 psRsaEncryptTest(void) } /* key loop */ - return PS_SUCCESS; } @@ -2542,7 +2531,6 @@ static int32 psRsaSignTest(void) i++) { _psTraceInt(" %d bit test...", rsa[i].size * 8); - psRsaInitKey(pool, &privkey); psRsaParsePkcs1PrivKey(pool, rsa[i].key, rsa[i].keysize, &privkey); if (psRsaEncryptPriv(pool, &privkey, in, sizeof(in), out, rsa[i].size, @@ -4524,7 +4512,6 @@ static int32_t psEccTest(void) /******************************************************************************/ - /******************************************************************************/ typedef struct { @@ -4690,7 +4677,6 @@ static test_t tests[] = { {NULL , "***** PRF2 TESTS *****"}, - {NULL, ""} }; diff --git a/crypto/test/dhperf/dhperf.c b/crypto/test/dhperf/dhperf.c index 8e49141..d85f9bb 100644 --- a/crypto/test/dhperf/dhperf.c +++ b/crypto/test/dhperf/dhperf.c @@ -130,7 +130,6 @@ static keyList_t keys[] = { Main */ - int main(int argc, char **argv) { psPool_t *pool, *misc; diff --git a/crypto/test/eccperf/eccperf.c b/crypto/test/eccperf/eccperf.c index 9475a93..032ed4a 100644 --- a/crypto/test/eccperf/eccperf.c +++ b/crypto/test/eccperf/eccperf.c @@ -199,7 +199,6 @@ const static keyList_t keys[] = { Main */ - #ifdef STATS #include #include @@ -250,6 +249,7 @@ int main(int argc, char **argv) if (psEccParsePrivKey(misc, (unsigned char*)keys[i].key, keys[i].len, &privkey, NULL) < 0) { _psTrace(" FAILED OPERATION:ParsePriv\n"); + exit(0); } /* Get random data to sign */ diff --git a/crypto/test/rsaperf/rsaperf.c b/crypto/test/rsaperf/rsaperf.c index 4d92a02..2c1dcb3 100644 --- a/crypto/test/rsaperf/rsaperf.c +++ b/crypto/test/rsaperf/rsaperf.c @@ -336,7 +336,6 @@ static keyList_t keys[] = { Main */ - #ifdef STATS #include #ifdef USE_HIGHRES_TIME @@ -451,6 +450,7 @@ int main(int argc, char **argv) memset(in, 0x0, keysize); psGetTime(&start, NULL); + /* coverity[swapped_arguments] */ if (psRsaDecryptPub(pool, &privkey, out, keysize, in, 5, pkaInfo) < 0) { _psTrace(" FAILED VERIFY OPERATION\n"); } @@ -538,7 +538,6 @@ int main(int argc, char **argv) i++; } - #ifdef STATS fclose(sfd); #endif diff --git a/crypto/test/throughputTest.c b/crypto/test/throughputTest.c index d1ebe56..b96b298 100644 --- a/crypto/test/throughputTest.c +++ b/crypto/test/throughputTest.c @@ -49,7 +49,6 @@ static unsigned char key[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; - enum { AES_ENC_ALG = 1, AES_DEC_ALG, @@ -132,6 +131,7 @@ static void runWithHmac(psCipherContext_t *ctx, psHmac_t *hmac, #endif default: printf("Skipping HMAC Test\n"); + psFree(dataChunk, NULL); return; } @@ -147,6 +147,7 @@ static void runWithHmac(psCipherContext_t *ctx, psHmac_t *hmac, printf("%d byte chunks in %d msecs total for rate of %d MB/sec\n", chunk, diffm, round); #endif + psFree(dataChunk, NULL); } #endif /* USE_HMAC */ @@ -241,6 +242,7 @@ static void runTime(psCipherContext_t *ctx, psCipherGivContext_t *ctx_giv, break; #endif default: + psFree(dataChunk, NULL); return; } @@ -620,6 +622,7 @@ void runDigestTime(psDigestContext_t *ctx, int32 chunk, int32 alg) break; #endif default: + psFree(dataChunk, NULL); printf("Skipping Digest Tests\n"); return; } @@ -636,7 +639,7 @@ void runDigestTime(psDigestContext_t *ctx, int32 chunk, int32 alg) printf("%d byte chunks in %d msecs total for rate of %d MB/sec\n", chunk, diffm, round); #endif - + psFree(dataChunk, NULL); } /******************************************************************************/ diff --git a/matrixssl/Makefile b/matrixssl/Makefile index 9bf56ec..30efe1c 100755 --- a/matrixssl/Makefile +++ b/matrixssl/Makefile @@ -11,6 +11,7 @@ include $(MATRIXSSL_ROOT)/common.mk SRC:=\ cipherSuite.c \ + dtls.c \ extDecode.c \ hsDecode.c \ hsHash.c \ @@ -22,9 +23,6 @@ SRC:=\ sslEncode.c \ sslv3.c \ tls.c -#DTLS -SRC+=dtls.c -#DTLS # Generated files STATIC:=libssl_s.a diff --git a/matrixssl/cipherSuite.c b/matrixssl/cipherSuite.c index 7e1fa93..bffeee5 100644 --- a/matrixssl/cipherSuite.c +++ b/matrixssl/cipherSuite.c @@ -680,29 +680,31 @@ static int32 csShaVerifyMac(void *sslv, unsigned char type, unsigned char buf[MAX_HASH_SIZE]; ssl_t *ssl = (ssl_t*)sslv; -#ifdef USE_TLS if (ssl->flags & SSL_FLAGS_TLS) { + switch (ssl->nativeDeMacSize) { #ifdef USE_SHA256 - if (ssl->nativeDeMacSize == SHA256_HASH_SIZE || - ssl->nativeDeMacSize == SHA384_HASH_SIZE) { + case SHA256_HASH_SIZE: + case SHA384_HASH_SIZE: tlsHMACSha2(ssl, HMAC_VERIFY, type, data, len, buf, ssl->nativeDeMacSize); - } else { + break; #endif #ifdef USE_SHA1 + case SHA1_HASH_SIZE: tlsHMACSha1(ssl, HMAC_VERIFY, type, data, len, buf); + break; #endif -#ifdef USE_SHA256 + default: + memzero_s(buf, ssl->nativeDeMacSize); /* Will fail below */ + break; } -#endif } else { -#endif /* USE_TLS */ #ifndef DISABLE_SSLV3 ssl3HMACSha1(ssl->sec.readMAC, ssl->sec.remSeq, type, data, len, buf); +#else + memzero_s(buf, SHA1_HASH_SIZE); /* Will fail below */ #endif /* DISABLE_SSLV3 */ -#ifdef USE_TLS } -#endif /* USE_TLS */ if (memcmpct(buf, mac, ssl->deMacSize) == 0) { return PS_SUCCESS; } diff --git a/matrixssl/dtls.c b/matrixssl/dtls.c index 766c3ce..efb2c8d 100644 --- a/matrixssl/dtls.c +++ b/matrixssl/dtls.c @@ -589,15 +589,17 @@ enum { }; int32 dtlsChkReplayWindow(ssl_t *ssl, unsigned char *seq64) { - unsigned long diff, seq, lastSeq; + uint32_t diff, seq, lastSeq; unsigned char *ls64; /* TODO DTLS - We truncate 48 bit sequence to 32bits to make it simpler here */ - seq = (seq64[2] << 24) + (seq64[3] << 16) + (seq64[4] << 8) + seq64[5]; + seq = ((uint32_t)seq64[2] << 24) + ((uint32_t)seq64[3] << 16) + + ((uint32_t)seq64[4] << 8) + (uint32_t)seq64[5]; ls64 = ssl->lastRsn; - lastSeq = (ls64[2] << 24) + (ls64[3] << 16) + (ls64[4] << 8) + ls64[5]; + lastSeq = ((uint32_t)ls64[2] << 24) + ((uint32_t)ls64[3] << 16) + + ((uint32_t)ls64[4] << 8) + (uint32_t)ls64[5]; if (seq == 0) { /* Need to differentiate between initial, duplicate, and epoch shift */ diff --git a/matrixssl/extDecode.c b/matrixssl/extDecode.c index a9ae8db..d5c1292 100644 --- a/matrixssl/extDecode.c +++ b/matrixssl/extDecode.c @@ -587,8 +587,6 @@ static int ClientHelloExt(ssl_t *ssl, unsigned short extType, unsigned short ext #endif - - /**************************************************************************/ default: @@ -1010,7 +1008,6 @@ static int ServerHelloExt(ssl_t *ssl, unsigned short extType, unsigned short ext break; #endif /* ENABLE_SECURE_REHANDSHAKES */ - case EXT_STATUS_REQUEST: if (ssl->extFlags.req_status_request) { /* Weed out the unsolicited status_request */ @@ -1046,6 +1043,5 @@ static int ServerHelloExt(ssl_t *ssl, unsigned short extType, unsigned short ext } #endif - /**************************************************************************/ diff --git a/matrixssl/hsDecode.c b/matrixssl/hsDecode.c index 5fc0633..7165de8 100644 --- a/matrixssl/hsDecode.c +++ b/matrixssl/hsDecode.c @@ -55,7 +55,6 @@ int32 parseClientHello(ssl_t *ssl, unsigned char **cp, unsigned char *end) void *pkiData = ssl->userPtr; #endif - c = *cp; /* First two bytes are the highest supported major and minor SSL versions */ @@ -144,8 +143,9 @@ int32 parseClientHello(ssl_t *ssl, unsigned char **cp, unsigned char *end) ssl->err = SSL_ALERT_PROTOCOL_VERSION; psTraceInfo("Can't support client's SSL version\n"); return MATRIXSSL_ERROR; -#endif +#else ssl->minVer = SSL3_MIN_VER; +#endif } } else if (compareMin == 0) { #ifdef DISABLE_SSLV3 @@ -515,7 +515,6 @@ SKIP_STANDARD_RESUMPTION: } } - matrixSslSetKexFlags(ssl); /* If we're resuming a handshake, then the next handshake message we @@ -627,7 +626,7 @@ int32 parseClientKeyExchange(ssl_t *ssl, int32 hsLen, unsigned char **cp, int32 rc, pubKeyLen; unsigned char *c; #ifdef USE_RSA_CIPHER_SUITE - unsigned char R[SSL_HS_RSA_PREMASTER_SIZE - 2]; + unsigned char R[SSL_HS_RSA_PREMASTER_SIZE - 2]; psPool_t *ckepkiPool = NULL; #endif #ifdef USE_PSK_CIPHER_SUITE @@ -636,12 +635,11 @@ int32 parseClientKeyExchange(ssl_t *ssl, int32 hsLen, unsigned char **cp, #endif void *pkiData = ssl->userPtr; - c = *cp; /* RSA: This message contains the premaster secret encrypted with the server's public key (from the Certificate). The premaster - secret is 48 bytes of random data, but the message may be longer + secret is 48 bytes of random data, but the message will be longer than that because the 48 bytes are padded before encryption according to PKCS#1v1.5. After encryption, we should have the correct length. */ @@ -679,7 +677,6 @@ int32 parseClientKeyExchange(ssl_t *ssl, int32 hsLen, unsigned char **cp, } #endif /* USE_TLS */ - #ifdef USE_DHE_CIPHER_SUITE if (ssl->flags & SSL_FLAGS_DHE_KEY_EXCH) { if (ssl->majVer == SSL3_MAJ_VER && ssl->minVer == SSL3_MIN_VER) { @@ -968,8 +965,8 @@ int32 parseClientKeyExchange(ssl_t *ssl, int32 hsLen, unsigned char **cp, return SSL_MEM_ERROR; } - /** - @security Caution - the results of an RSA private key +/** + @security Caution - the results of an RSA private key decryption should never have any bearing on timing or response, otherwise we can be vulnerable to a side channel attack. @see http://web-in-security.blogspot.co.at/2014/08/old-attacks-on-new-tls-implementations.html @@ -983,9 +980,8 @@ int32 parseClientKeyExchange(ssl_t *ssl, int32 hsLen, unsigned char **cp, (through, e.g., timing, log files, or other channels.)" */ rc = psRsaDecryptPriv(ckepkiPool, &ssl->keys->privKey.key.rsa, c, - pubKeyLen, ssl->sec.premaster, ssl->sec.premasterSize, - pkiData); - + pubKeyLen, ssl->sec.premaster, ssl->sec.premasterSize, + pkiData); /* Step 1 of Bleichenbacher attack mitigation. We do it here after the RSA op, but regardless of the result of the op. */ if (matrixCryptoGetPrngData(R, sizeof(R), ssl->userPtr) < 0) { @@ -996,12 +992,13 @@ int32 parseClientKeyExchange(ssl_t *ssl, int32 hsLen, unsigned char **cp, /* Step 3 If the PKCS#1 padding is not correct, or the length of message M is not exactly 48 bytes: - pre_master_secret = ClientHello.client_version || R + pre_master_secret = ClientHello.client_version || R else - pre_master_secret = ClientHello.client_version || M[2..47] + pre_master_secret = ClientHello.client_version || M[2..47] + Note that explicitly constructing the pre_master_secret with the - client_version produces an invalid master_secret if the - client has sent the wrong version in the original pre_master_secret. + ClientHello.client_version produces an invalid master_secret if the + client has sent the wrong version in the original pre_master_secret. Note: The version number in the PreMasterSecret is the version offered by the client in the ClientHello.client_version, not the @@ -1122,7 +1119,6 @@ int32 parseCertificateVerify(ssl_t *ssl, psPool_t *cvpkiPool = NULL; void *pkiData = ssl->userPtr; - c = *cp; rc = 0; @@ -1189,7 +1185,6 @@ int32 parseCertificateVerify(ssl_t *ssl, certVerifyLen = MD5_HASH_SIZE + SHA1_HASH_SIZE; #endif /* USE_TLS_1_2 */ - if ((uint32)(end - c) < 2) { ssl->err = SSL_ALERT_DECODE_ERROR; psTraceInfo("Invalid Certificate Verify message\n"); @@ -1253,8 +1248,6 @@ int32 parseCertificateVerify(ssl_t *ssl, #endif /* USE_ECC */ #ifdef USE_RSA - - #ifdef USE_TLS_1_2 if (ssl->flags & SSL_FLAGS_TLS_1_2) { if ((i = pubRsaDecryptSignedElement(cvpkiPool, @@ -1387,10 +1380,11 @@ int32 parseServerHello(ssl_t *ssl, int32 hsLen, unsigned char **cp, ssl->err = SSL_ALERT_PROTOCOL_VERSION; psTraceInfo("Server wants to talk TLS1.1 but it's disabled\n"); return MATRIXSSL_ERROR; -#endif +#else ssl->reqMinVer = ssl->minVer; ssl->minVer = TLS_1_1_MIN_VER; goto PROTOCOL_DETERMINED; +#endif } } #endif /* USE_TLS_1_2 */ @@ -1711,7 +1705,6 @@ int32 parseServerKeyExchange(ssl_t *ssl, /* Check the DH status. Could also be a PSK_DHE suite */ if (ssl->flags & SSL_FLAGS_DHE_KEY_EXCH) { - #ifdef USE_PSK_CIPHER_SUITE if (ssl->flags & SSL_FLAGS_PSK_CIPHER) { /* Using the value of MAX_HINT_SIZE to know if the user is @@ -2054,10 +2047,6 @@ int32 parseServerKeyExchange(ssl_t *ssl, #endif /* USE_TLS_1_2 */ - - - - #ifdef USE_TLS_1_2 if (ssl->flags & SSL_FLAGS_TLS_1_2) { /* TLS 1.2 doesn't just sign the straight hash so we can't @@ -2798,7 +2787,6 @@ int32 parseCertificate(ssl_t *ssl, unsigned char **cp, unsigned char *end) int32 rc, i, certChainLen, parseLen = 0; void *pkiData = ssl->userPtr; - psTraceStrHs(">>> %s parsing CERTIFICATE message\n", (ssl->flags & SSL_FLAGS_SERVER) ? "Server" : "Client"); @@ -2942,7 +2930,6 @@ SKIP_CERT_CHAIN_INIT: ssl->keys == NULL ? NULL : ssl->keys->CAcerts, ssl->expectedName, &foundIssuer, pkiData, ssl->memAllocPtr); - if (rc == PS_MEM_FAIL) { ssl->err = SSL_ALERT_INTERNAL_ERROR; return MATRIXSSL_ERROR; diff --git a/matrixssl/hsHash.c b/matrixssl/hsHash.c index a88c071..df62db2 100644 --- a/matrixssl/hsHash.c +++ b/matrixssl/hsHash.c @@ -33,7 +33,6 @@ #include "matrixsslApi.h" - #define FINISHED_LABEL_SIZE 15 #define LABEL_CLIENT "client finished" #define LABEL_SERVER "server finished" @@ -466,6 +465,5 @@ int32_t sslSnapshotHSHash(ssl_t *ssl, unsigned char *out, int32 senderFlag) return len; } - /******************************************************************************/ diff --git a/matrixssl/matrixssl.c b/matrixssl/matrixssl.c index 6796d1c..12c9d9d 100644 --- a/matrixssl/matrixssl.c +++ b/matrixssl/matrixssl.c @@ -44,7 +44,6 @@ static int32 verifyReadKeys(psPool_t *pool, sslKeys_t *keys, void *poolUserPtr); #endif /* USE_SERVER_SIDE_SSL || USE_CLIENT_AUTH */ #endif /* USE_RSA || USE_ECC */ - #ifdef USE_SERVER_SIDE_SSL #ifndef SSL_SESSION_TICKET_LIST_LEN @@ -80,7 +79,6 @@ static int32 matrixSslLoadKeyMaterialMem(sslKeys_t *keys, int32 privKeyType); #endif /* USE_RSA || USE_ECC */ - /******************************************************************************/ /* Open and close the SSL module. These routines are called once in the @@ -117,7 +115,6 @@ int32 matrixSslOpenWithConfig(const char *config) } - #ifdef USE_SERVER_SIDE_SSL memset(sessionTable, 0x0, sizeof(sslSessionEntry_t) * SSL_SESSION_TABLE_SIZE); @@ -141,7 +138,6 @@ int32 matrixSslOpenWithConfig(const char *config) return PS_SUCCESS; } - /* matrixSslClose */ @@ -185,7 +181,6 @@ int32_t matrixSslNewKeys(sslKeys_t **keys, void *memAllocUserPtr) sslKeys_t *lkeys; int32_t rc; - lkeys = psMalloc(pool, sizeof(sslKeys_t)); if (lkeys == NULL) { return PS_MEM_FAIL; @@ -1075,7 +1070,6 @@ int32 matrixSslNewSession(ssl_t **ssl, const sslKeys_t *keys, return PS_ARG_FAIL; } - lssl = psMalloc(pool, sizeof(ssl_t)); if (lssl == NULL) { psTraceInfo("Out of memory for ssl_t in matrixSslNewSession\n"); @@ -1103,7 +1097,6 @@ int32 matrixSslNewSession(ssl_t **ssl, const sslKeys_t *keys, } #endif - /* Data buffers */ @@ -1139,7 +1132,11 @@ int32 matrixSslNewSession(ssl_t **ssl, const sslKeys_t *keys, lssl->sPool = pool; lssl->keys = (sslKeys_t*)keys; - lssl->cipher = sslGetCipherSpec(lssl, SSL_NULL_WITH_NULL_NULL); + if ((lssl->cipher = sslGetCipherSpec(lssl, SSL_NULL_WITH_NULL_NULL)) == NULL) { + psFree(lssl->outbuf, lssl->bufferPool); + psFree(lssl, pool); + return PS_MEM_FAIL; + } sslActivateReadCipher(lssl); sslActivateWriteCipher(lssl); @@ -1166,7 +1163,6 @@ int32 matrixSslNewSession(ssl_t **ssl, const sslKeys_t *keys, } #endif /* USE_DTLS */ - if (flags & SSL_FLAGS_SERVER) { lssl->flags |= SSL_FLAGS_SERVER; /* @@ -1416,7 +1412,6 @@ int32 matrixSslNewSession(ssl_t **ssl, const sslKeys_t *keys, return PS_SUCCESS; } - /******************************************************************************/ /* Delete an SSL session. Some information on the session may stay around @@ -1431,7 +1426,6 @@ void matrixSslDeleteSession(ssl_t *ssl) return; } - ssl->flags |= SSL_FLAGS_CLOSED; /* Synchronize all digests, in case some of them have been updated, but @@ -1442,7 +1436,6 @@ void matrixSslDeleteSession(ssl_t *ssl) psSha1Sync(NULL, 1); #endif /* USE_TLS_1_2 */ - /* If we have a sessionId, for servers we need to clear the inUse flag in the session cache so the ID can be replaced if needed. In the client case @@ -1537,8 +1530,6 @@ void matrixSslDeleteSession(ssl_t *ssl) #endif #endif /* USE_DTLS */ - - /* Free the data buffers, clear any remaining user data */ @@ -1547,7 +1538,6 @@ void matrixSslDeleteSession(ssl_t *ssl) psFree(ssl->outbuf, ssl->bufferPool); psFree(ssl->inbuf, ssl->bufferPool); - freePkaAfter(ssl); clearFlightList(ssl); @@ -1564,7 +1554,6 @@ void matrixSslDeleteSession(ssl_t *ssl) psFree(ssl, pool); } - /******************************************************************************/ /* Generic session option control for changing already connected sessions. @@ -2315,7 +2304,7 @@ int32 matrixSessionTicketLen(void) */ int32 matrixCreateSessionTicket(ssl_t *ssl, unsigned char *out, int32 *outLen) { - int32 len, ticketLen, pad; + int32 len, ticketLen, pad, rc; uint32 timeSecs; psTime_t t; psAesCbc_t ctx; @@ -2351,9 +2340,7 @@ int32 matrixCreateSessionTicket(ssl_t *ssl, unsigned char *out, int32 *outLen) psTraceInfo("WARNING: matrixCryptoGetPrngData failed\n"); } -#ifdef USE_MULTITHREADING psLockMutex(&g_sessTicketLock); -#endif /* Ticket itself */ keys = ssl->keys->sessTickets; /* name */ @@ -2383,26 +2370,33 @@ int32 matrixCreateSessionTicket(ssl_t *ssl, unsigned char *out, int32 *outLen) pad = psPadLenPwr2(len, AES_BLOCKLEN); c += sslWritePad(c, (unsigned char)pad); len += pad; /* out + 6 + 16 (name) is pointing at IV */ - psAesInitCBC(&ctx, out + 6 + 16, keys->symkey, keys->symkeyLen, PS_AES_ENCRYPT); + if ((rc = psAesInitCBC(&ctx, out + 6 + 16, keys->symkey, keys->symkeyLen, PS_AES_ENCRYPT)) < 0) { + goto ERR_LOCKED; + } psAesEncryptCBC(&ctx, enc, enc, len); psAesClearCBC(&ctx); /* HMAC starting from the Name */ #ifdef USE_HMAC_SHA256 - psHmacSha256Init(&dgst, keys->hashkey, keys->hashkeyLen); + if ((rc = psHmacSha256Init(&dgst, keys->hashkey, keys->hashkeyLen)) < 0) { + goto ERR_LOCKED; + } psHmacSha256Update(&dgst, out + 6, len + 16 + 16); psHmacSha256Final(&dgst, c); *outLen = len + SHA256_HASHLEN + 16 + 16 + 6; #else - psHmacSha1Init(&dgst, keys->hashkey, keys->hashkeyLen); + if ((rc = psHmacSha1Init(&dgst, keys->hashkey, keys->hashkeyLen)) < 0) { + goto ERR_LOCKED; + } psHmacSha1Update(&dgst, out + 6, len + 16 + 16); psHmacSha1Final(&dgst, c); *outLen = len + SHA1_HASHLEN + 16 + 16 + 6; #endif -#ifdef USE_MULTITHREADING + rc = PS_SUCCESS; +ERR_LOCKED: + memzero_s(randno, sizeof(randno)); psUnlockMutex(&g_sessTicketLock); -#endif - return PS_SUCCESS; + return rc; } /******************************************************************************/ @@ -2419,7 +2413,6 @@ static int32 getTicketKeys(ssl_t *ssl, unsigned char *c, /* First 16 bytes are the key name */ memcpy(name, c, 16); - *keys = NULL; /* check our cached list beginning with our own encryption key */ lkey = ssl->keys->sessTickets; @@ -2439,36 +2432,31 @@ static int32 getTicketKeys(ssl_t *ssl, unsigned char *c, } /* didn't find it. Ask user */ if (ssl->keys->ticket_cb) { -#ifdef USE_MULTITHREADING /* Unlock. Cback will likely call matrixSslLoadSessionTicketKeys */ psUnlockMutex(&g_sessTicketLock); -#endif if (ssl->keys->ticket_cb((struct sslKeys_t*)ssl->keys, name, cachedTicket) < 0) { - lkey->inUse = 0; /* inUse could be set in the odd case where we + if (lkey) { + /* inUse could be set in the odd case where we found the cached key but the user didn't want to use it. */ + lkey->inUse = 0; + } return PS_FAILURE; /* user couldn't find it either */ } else { /* found it */ -#ifdef USE_MULTITHREADING psLockMutex(&g_sessTicketLock); -#endif if (cachedTicket == 0) { /* it's been found and added at end of list. confirm this */ lkey = ssl->keys->sessTickets; if (lkey == NULL) { -#ifdef USE_MULTITHREADING psUnlockMutex(&g_sessTicketLock); -#endif return PS_FAILURE; /* user claims they added, but empty */ } while (lkey->next) { lkey = lkey->next; } if (memcmp(lkey->name, c, 16) != 0) { -#ifdef USE_MULTITHREADING psUnlockMutex(&g_sessTicketLock); -#endif return PS_FAILURE; /* user claims to have added, but... */ } lkey->inUse = 1; @@ -3027,7 +3015,6 @@ int32 matrixUserCertValidator(ssl_t *ssl, int32 alert, } #endif /* !USE_ONLY_PSK_CIPHER_SUITE */ - /******************************************************************************/ #ifdef USE_MATRIXSSL_STATS void matrixSslRegisterStatCallback(ssl_t *ssl, void (*stat_cb)(void *ssl, diff --git a/matrixssl/matrixsslApi.c b/matrixssl/matrixsslApi.c index 7dddb69..991eaa5 100644 --- a/matrixssl/matrixsslApi.c +++ b/matrixssl/matrixsslApi.c @@ -437,7 +437,6 @@ int32 matrixSslGetWritebuf(ssl_t *ssl, unsigned char **buf, uint32 requestedLen) } psAssert(ssl->outsize > 0 && ssl->outbuf != NULL); - #ifdef USE_BEAST_WORKAROUND /* This is a client-only feature */ if (!(ssl->flags & SSL_FLAGS_SERVER)) { @@ -992,7 +991,6 @@ DECODE_MORE: /* Don't need to change inlen */ } - rc = MATRIXSSL_REQUEST_RECV; /* Expecting more data */ break; @@ -1512,7 +1510,6 @@ int32 matrixSslIsSessionCompressionOn(ssl_t *ssl) } #endif - #ifdef USE_CRL /* Called after key load if CRL location is expected to be embedded in the CA. diff --git a/matrixssl/matrixsslApi.h b/matrixssl/matrixsslApi.h index b64890d..21d12b9 100644 --- a/matrixssl/matrixsslApi.h +++ b/matrixssl/matrixsslApi.h @@ -224,7 +224,6 @@ PSPUBLIC int32 matrixSslDeleteSessionTicketKey(sslKeys_t *keys, #endif /* USE_SERVER_SIDE_SSL */ - #ifdef USE_DTLS /******************************************************************************/ /* diff --git a/matrixssl/matrixsslCheck.h b/matrixssl/matrixsslCheck.h index 2a87702..e7ea37b 100644 --- a/matrixssl/matrixsslCheck.h +++ b/matrixssl/matrixsslCheck.h @@ -66,8 +66,6 @@ extern "C" { #endif #endif - - /******************************************************************************/ /* SHA1 and MD5 are essential elements for SSL key derivation during protocol diff --git a/matrixssl/matrixsslConfig.h b/matrixssl/matrixsslConfig.h index 1d3b18e..fd1df00 100644 --- a/matrixssl/matrixsslConfig.h +++ b/matrixssl/matrixsslConfig.h @@ -35,7 +35,6 @@ #ifndef _h_MATRIXSSLCONFIG #define _h_MATRIXSSLCONFIG - #ifdef __cplusplus extern "C" { #endif @@ -324,8 +323,6 @@ extern "C" { //#define DTLS_SEND_RECORDS_INDIVIDUALLY /* Max one record per datagram */ #endif - - #ifdef __cplusplus } #endif diff --git a/matrixssl/matrixssllib.h b/matrixssl/matrixssllib.h index 21b0f59..0d991f5 100644 --- a/matrixssl/matrixssllib.h +++ b/matrixssl/matrixssllib.h @@ -90,7 +90,6 @@ extern "C" { */ //#define USE_ZLIB_COMPRESSION /**< @security OFF NIST_SHOULD_NOT */ - /******************************************************************************/ /** Rehandshaking support. @@ -203,7 +202,6 @@ extern "C" { #define DISABLE_SSLV3 /**< DO NOT DISABLE, undef below if required @security NIST_SHALL_NOT */ - #if defined USE_TLS_1_2_AND_ABOVE #define DISABLE_TLS_1_1 #define DISABLE_TLS_1_0 @@ -215,7 +213,6 @@ extern "C" { #error Must define USE_TLS_1_x_AND_ABOVE #endif - #ifdef USE_DTLS /******************************************************************************/ /** DTLS definitions */ @@ -231,7 +228,6 @@ extern "C" { #include "zlib.h" #endif - #if defined(USE_AES_GCM) || defined(USE_AES_CCM) || defined(USE_CHACHA20_POLY1305) #define USE_AEAD_CIPHER #endif @@ -294,7 +290,6 @@ extern "C" { #define DTLS_RETRANSMIT -61 /* Received a duplicate hs msg from peer */ #endif /* USE_DTLS */ - /* Magic numbers for handshake header lengths */ @@ -658,11 +653,9 @@ static __inline uint16_t HASH_SIG_MASK(uint8_t hash, uint8_t sig) #define EXT_SESSION_TICKET 35 #define EXT_RENEGOTIATION_INFO 0xFF01 - /* How large the ALPN extension arrary is. Number of protos client can talk */ #define MAX_PROTO_EXT 8 - /* Maximum key block size for any defined cipher This must be validated if new ciphers are added @@ -936,7 +929,6 @@ typedef struct { psCipherContext_t encryptCtx; psCipherContext_t decryptCtx; - #ifndef USE_ONLY_TLS_1_2 psMd5Sha1_t msgHashMd5Sha1; #endif @@ -955,8 +947,6 @@ typedef struct { #endif #endif /* USE_TLS_1_2 */ - - #if defined(USE_SERVER_SIDE_SSL) && defined(USE_CLIENT_AUTH) unsigned char sha1Snapshot[SHA1_HASH_SIZE]; unsigned char sha384Snapshot[SHA384_HASH_SIZE]; /* HW crypto uses @@ -1057,7 +1047,6 @@ typedef struct tlsHelloExt { struct tlsHelloExt *next; } tlsExtension_t; - /* Hold the info needed to perform a public key operation for flight writes until the very end. This is an architectural change that was added to aid the integration of non-blocking hardware acceleration */ @@ -1142,7 +1131,6 @@ struct ssl { uint32 bFlagsBk; #endif /* USE_CLIENT_SIDE_SSL */ - unsigned char *inbuf; unsigned char *outbuf; int32 inlen; /* Bytes unprocessed in inbuf */ @@ -1507,7 +1495,6 @@ extern int32 dtlsEncryptFragRecord(ssl_t *ssl, flightEncode_t *msg, sslBuf_t *out, unsigned char **c); #endif /* USE_DTLS */ - /* cipherSuite.c */ @@ -1592,7 +1579,6 @@ extern void sslSha512SnapshotHSHash(ssl_t *ssl, unsigned char *out); extern int32_t extMasterSecretSnapshotHSHash(ssl_t *ssl, unsigned char *out, uint32 *outLen); - /******************************************************************************/ /* prf.c @@ -1670,9 +1656,6 @@ extern int32_t eccSuitesSupported(const ssl_t *ssl, const uint16_t cipherSpecs[], uint8_t cipherSpecLen); #endif /* USE_ECC_CIPHER_SUITE */ - - - /******************************************************************************/ /* Deprected defines for compatibility */ #define CH_RECV_STAT 1 @@ -1694,7 +1677,6 @@ void matrixsslUpdateStat(ssl_t *ssl, int32_t type, int32_t value) } #endif /* USE_MATRIXSSL_STATS */ - #ifdef __cplusplus } #endif diff --git a/matrixssl/prf.c b/matrixssl/prf.c index c2fb31e..cbb0529 100644 --- a/matrixssl/prf.c +++ b/matrixssl/prf.c @@ -294,9 +294,9 @@ __inline static int32_t pSha2(const unsigned char *key, uint16_t keyLen, } if (i == keyIter - 1) { memcpy(out + (hashSize * i), mac, - outLen - (hashSize * i)); + outLen - ((uint32_t)hashSize * i)); } else { - memcpy(out + (hashSize * i), mac, hashSize); + memcpy(out + ((uint32_t)hashSize * i), mac, hashSize); #ifdef USE_SHA384 if (flags & CRYPTO_FLAGS_SHA3) { if ((rc = psHmacSha384(key, keyLen, a, hashSize, a, diff --git a/matrixssl/sslDecode.c b/matrixssl/sslDecode.c index 37c345a..9704ac8 100644 --- a/matrixssl/sslDecode.c +++ b/matrixssl/sslDecode.c @@ -116,7 +116,8 @@ int32 matrixSslDecode(ssl_t *ssl, unsigned char **buf, uint32 *len, unsigned char *alertDescription) { unsigned char *c, *p, *end, *pend, *ctStart, *origbuf; - unsigned char *mac, macError; + unsigned char *mac; + volatile unsigned char macError; int32 rc; unsigned char padLen; #ifdef USE_CLIENT_SIDE_SSL @@ -145,7 +146,6 @@ int32 matrixSslDecode(ssl_t *ssl, unsigned char **buf, uint32 *len, p = pend = mac = ctStart = NULL; padLen = 0; - /* This flag is set if the previous call to this routine returned an SSL_FULL error from encodeResponse, indicating that there is data to be encoded, @@ -588,26 +588,27 @@ ADVANCE_TO_APP_DATA: /* Sanity check ct len. Step 1 of Lucky 13 MEE-TLS-CBC decryption. max{b, t + 1} is always "t + 1" because largest possible blocksize is 16 and smallest possible tag len is 16. Multiple of block size test - is done in decrypt */ + is done in decrypt. We return the identical error as if the mac failed, + since this is a sanity check for pad and mac verification. */ if ((ssl->flags & SSL_FLAGS_READ_SECURE) && (ssl->deBlockSize > 1) && !(ssl->flags & SSL_FLAGS_AEAD_R)) { #ifdef USE_TLS_1_1 if (ssl->flags & SSL_FLAGS_TLS_1_1) { if (ssl->rec.len < (ssl->deMacSize + 1 + ssl->deBlockSize)) { - ssl->err = SSL_ALERT_DECODE_ERROR; + ssl->err = SSL_ALERT_BAD_RECORD_MAC; psTraceInfo("Ciphertext length failed sanity\n"); goto encodeResponse; } } else { if (ssl->rec.len < (ssl->deMacSize + 1)) { - ssl->err = SSL_ALERT_DECODE_ERROR; + ssl->err = SSL_ALERT_BAD_RECORD_MAC; psTraceInfo("Ciphertext length failed sanity\n"); goto encodeResponse; } } #else if (ssl->rec.len < (ssl->deMacSize + 1)) { - ssl->err = SSL_ALERT_DECODE_ERROR; + ssl->err = SSL_ALERT_BAD_RECORD_MAC; psTraceInfo("Ciphertext length failed sanity\n"); goto encodeResponse; } @@ -756,6 +757,7 @@ ADVANCE_TO_APP_DATA: for (rc = (256 - padLen) - 1; rc > 0; rc--) { /* make this test look like the others */ if ((unsigned char)rc == padLen) { + /* coverity[unused_value] */ macError = 1; /* not really an error. reset below */ } } @@ -1154,6 +1156,18 @@ ADVANCE_TO_APP_DATA: *alertLevel = *p; p++; *alertDescription = *p; p++; *len = 2; +#ifdef USE_SSL_HANDSHAKE_MSG_TRACE + if (ssl->flags & SSL_FLAGS_SERVER) { + psTraceHs(">>> Server"); + } else { + psTraceHs(">>> Client"); + } + if (*alertDescription == SSL_ALERT_CLOSE_NOTIFY) { + psTraceHs(" parsing ALERT (CLOSE_NOTIFY) message\n"); + } else { + psTraceHs(" parsing ALERT message\n"); + } +#endif psTraceIntInfo("Received alert %d\n", (int32)(*alertDescription)); /* If the alert is fatal, or is a close message (usually a warning), @@ -1371,7 +1385,6 @@ ADVANCE_TO_APP_DATA: ssl->decState = SSL_HS_DONE; return SSL_PROCESS_DATA; - default: /* Falls to error below */ break; @@ -1566,7 +1579,6 @@ static int32 parseSSLHandshake(ssl_t *ssl, char *inbuf, uint32 len) uint32 hsLen; unsigned char hsMsgHash[SHA512_HASH_SIZE]; - #ifdef USE_DTLS uint32 fragLen; int32 msn, fragOffset, j; @@ -1849,8 +1861,6 @@ parseHandshake: } #endif /* USE_DTLS */ - - ssl->err = SSL_ALERT_UNEXPECTED_MESSAGE; psTraceIntInfo("Out-of-order handshake message: %d\n", hsType); psTraceIntInfo("Wanted: %d\n", ssl->hsState); @@ -2408,10 +2418,11 @@ SKIP_HSHEADER_PARSE: } #ifdef USE_DTLS - ssl->lastMsn = msn; /* MSN of last message sucessfully parsed */ + if (ssl->flags & SSL_FLAGS_DTLS) { + ssl->lastMsn = msn; /* MSN of last message sucessfully parsed */ + } #endif /* USE_DTLS */ - /* if we've got more data in the record, the sender has packed multiple handshake messages in one record. Parse the next one. @@ -2457,6 +2468,5 @@ static int32 parseSingleCert(ssl_t *ssl, unsigned char *c, unsigned char *end, #endif /* USE_CERT_CHAIN_PARSING */ #endif /* USE_CLIENT_SIDE_SSL || USE_CLIENT_AUTH */ - /******************************************************************************/ diff --git a/matrixssl/sslEncode.c b/matrixssl/sslEncode.c index 133e5d8..cd220a3 100644 --- a/matrixssl/sslEncode.c +++ b/matrixssl/sslEncode.c @@ -1077,7 +1077,6 @@ int32 sslEncodeResponse(ssl_t *ssl, psBuf_t *out, uint32 *requiredLen) return rc; } - #ifdef USE_DTLS if (ssl->flags & SSL_FLAGS_DTLS) { /* This function takes care of writing out entire flights so we know @@ -1487,7 +1486,6 @@ int32 sslEncodeResponse(ssl_t *ssl, psBuf_t *out, uint32 *requiredLen) } #endif - #ifdef ENABLE_SECURE_REHANDSHAKES /* The RenegotiationInfo extension lengths are well known @@ -2128,7 +2126,6 @@ int32 sslEncodeResponse(ssl_t *ssl, psBuf_t *out, uint32 *requiredLen) return sslEncodeResponse(ssl, out, &alertReqLen); } - #ifdef USE_SERVER_SIDE_SSL /* Post-flight write PKA operation. Support is for the signature generation during ServerKeyExchange write. */ @@ -3190,7 +3187,6 @@ static int32 writeServerHello(ssl_t *ssl, sslBuf_t *out) extLen += 4; } - #ifdef USE_STATELESS_SESSION_TICKETS if (ssl->sid && ssl->sid->sessionTicketState == SESS_TICKET_STATE_RECVD_EXT) { if (extLen == 0) { @@ -3356,7 +3352,6 @@ static int32 writeServerHello(ssl_t *ssl, sslBuf_t *out) *c = 0; c++; } - #ifdef USE_STATELESS_SESSION_TICKETS if (ssl->sid && ssl->sid->sessionTicketState == SESS_TICKET_STATE_RECVD_EXT) { @@ -3909,7 +3904,6 @@ static int32 writeServerKeyExchange(ssl_t *ssl, sslBuf_t *out, uint32 pLen, *c = (ssl->keys->privKey.keysize & 0xFF00) >> 8; c++; *c = ssl->keys->privKey.keysize & 0xFF; c++; - #ifdef USE_DTLS if ((ssl->flags & SSL_FLAGS_DTLS) && (ssl->retransmit == 1)) { /* It is not optimal to have run through the above digest updates @@ -4132,7 +4126,8 @@ static int32 writeMultiRecordCertificate(ssl_t *ssl, sslBuf_t *out, psX509Cert_t *cert, *future; unsigned char *c, *end, *encryptStart; uint8_t padLen; - uint16_t messageSize, certLen; + uint16_t messageSize; + uint32_t certLen; int32_t rc; int32 midWrite, midSizeWrite, countDown, firstOne = 1; @@ -4286,6 +4281,9 @@ static int32 writeMultiRecordCertificate(ssl_t *ssl, sslBuf_t *out, *c = (certLen & 0xFF00) >> 8; c++; countDown--; midSizeWrite = 1; if (countDown != 0) { +#ifdef TODO +/* Cannot reach here!, countdown is always zero */ +#endif *c = (certLen & 0xFF); c++; countDown--; midSizeWrite = 0; } @@ -4653,6 +4651,19 @@ static int32 writeAlert(ssl_t *ssl, unsigned char level, uint16_t messageSize; int32_t rc; +#ifdef USE_SSL_HANDSHAKE_MSG_TRACE + if (ssl->flags & SSL_FLAGS_SERVER) { + psTraceHs("<<< Server"); + } else { + psTraceHs("<<< Client"); + } + if (description == SSL_ALERT_CLOSE_NOTIFY) { + psTraceHs(" creating ALERT (CLOSE_NOTIFY) message\n"); + } else { + psTraceHs(" creating ALERT message\n"); + } +#endif + psTraceIntInfo("Creating alert %d\n", description); c = out->end; end = out->buf + out->size; messageSize = 2 + ssl->recordHeadLen; @@ -4683,7 +4694,6 @@ static int32 writeAlert(ssl_t *ssl, unsigned char level, return MATRIXSSL_SUCCESS; } - #ifdef USE_CLIENT_SIDE_SSL #ifdef USE_TRUSTED_CA_INDICATION static int32_t trustedCAindicationExtLen(psX509Cert_t *certs) @@ -4773,7 +4783,6 @@ int32_t matrixSslEncodeClientHello(ssl_t *ssl, sslBuf_t *out, sslInitHSHash(ssl); - cookieLen = 0; #ifdef USE_DTLS if (ssl->flags & SSL_FLAGS_DTLS) { @@ -4912,7 +4921,6 @@ int32_t matrixSslEncodeClientHello(ssl_t *ssl, sslBuf_t *out, } #endif - #ifdef ENABLE_SECURE_REHANDSHAKES /* Subsequent CLIENT_HELLOs must use a populated RenegotiationInfo extension */ if (ssl->myVerifyDataLen != 0) { @@ -5309,7 +5317,7 @@ int32_t matrixSslEncodeClientHello(ssl_t *ssl, sslBuf_t *out, *c = EXT_ELLIPTIC_CURVE & 0xFF; c++; *c = ((curveListLen + 2) & 0xFF00) >> 8; c++; *c = (curveListLen + 2) & 0xFF; c++; - *c = (curveListLen & 0xFF00) >> 8; c++; + *c = 0; c++; /* High byte always zero */ *c = curveListLen & 0xFF; c++; memcpy(c, eccCurveList, curveListLen); c += curveListLen; @@ -5486,7 +5494,6 @@ static int32 writeClientKeyExchange(ssl_t *ssl, sslBuf_t *out) return PS_PLATFORM_FAIL; } - #ifdef USE_PSK_CIPHER_SUITE if (ssl->flags & SSL_FLAGS_PSK_CIPHER) { /* Get the key id to send in the clientKeyExchange message. */ @@ -5643,7 +5650,6 @@ static int32 writeClientKeyExchange(ssl_t *ssl, sslBuf_t *out) } } - #ifdef USE_DTLS if ((ssl->flags & SSL_FLAGS_DTLS) && (ssl->retransmit == 1)) { /* @@ -5929,7 +5935,6 @@ static int32 nowDoCvPka(ssl_t *ssl, psBuf_t *out) return PS_MEM_FAIL; } - #ifdef USE_TLS_1_2 /* Tweak if needed */ if (ssl->flags & SSL_FLAGS_TLS_1_2) { @@ -6006,7 +6011,6 @@ static int32 nowDoCvPka(ssl_t *ssl, psBuf_t *out) #endif /* USE_DTLS */ clearPkaAfter(ssl); - } else { #endif /* USE_ECC */ @@ -6090,7 +6094,6 @@ static int32 nowDoCvPka(ssl_t *ssl, psBuf_t *out) #endif /* USE_TLS_1_2 */ - #else /* RSA is the 'default' so if that didn't get hit there is a problem */ psTraceInfo("There is no handler for writeCertificateVerify. ERROR\n"); return MATRIXSSL_ERROR; @@ -6122,7 +6125,6 @@ static int32 writeCertificateVerify(ssl_t *ssl, sslBuf_t *out) c = out->end; end = out->buf + out->size; - if ((pkaAfter = getPkaAfter(ssl)) == NULL) { psTraceInfo("getPkaAfter error for certVerify\n"); return MATRIXSSL_ERROR; @@ -6397,7 +6399,6 @@ static int32 writeCertificateVerify(ssl_t *ssl, sslBuf_t *out) #endif /* USE_CLIENT_AUTH */ #endif /* !USE_ONLY_PSK_CIPHER_SUITE */ - #else /* USE_CLIENT_SIDE_SSL */ /******************************************************************************/ /* @@ -6413,7 +6414,6 @@ int32_t matrixSslEncodeClientHello(ssl_t *ssl, sslBuf_t *out, } #endif /* USE_CLIENT_SIDE_SSL */ - #ifndef USE_ONLY_PSK_CIPHER_SUITE #if defined(USE_SERVER_SIDE_SSL) && defined(USE_CLIENT_AUTH) /******************************************************************************/ diff --git a/matrixssl/test/Makefile b/matrixssl/test/Makefile index 21d304f..f478b95 100755 --- a/matrixssl/test/Makefile +++ b/matrixssl/test/Makefile @@ -18,7 +18,6 @@ TEST_EXE:=sslTest$(E) # Linked files STATIC:=../libssl_s.a $(MATRIXSSL_ROOT)/crypto/libcrypt_s.a $(MATRIXSSL_ROOT)/core/libcore_s.a - # Generate map file with sections and C source reference for sslTest ifndef MATRIX_DEBUG ifeq (,$(findstring -apple,$(CCARCH))) @@ -29,7 +28,6 @@ LDFLAGS += \ endif endif - all: compile compile: $(OBJS) $(CERT_EXE) $(TEST_EXE) @@ -43,14 +41,6 @@ $(TEST_EXE): $(TEST_SRC:.c=.o) $(STATIC) $(CERT_EXE):$(CERT_SRC:.c=.o) $(STATIC) $(CC) -o $@ $^ $(LDFLAGS) -ifneq (,$(findstring -octeon,$(CCARCH))) - SIM:=oct-sim - SIMFLAGS:=-quiet -numcores=1 -noperf -endif - -test: - $(SIM) ./$(TEST_EXE) $(SIMFLAGS) - clean: rm -f $(TEST_EXE) $(CERT_EXE) $(OBJS) diff --git a/matrixssl/test/sslTest.c b/matrixssl/test/sslTest.c index b50ea4f..033053d 100644 --- a/matrixssl/test/sslTest.c +++ b/matrixssl/test/sslTest.c @@ -673,13 +673,11 @@ int sslTest(void) testResult_t *result = g_results; #endif /* ENABLE_PERF_TIMING */ - if (matrixSslOpen() < 0) { fprintf(stderr, "matrixSslOpen failed, exiting...\n"); return -1; } - svrConn = psMalloc(MATRIX_NO_POOL, sizeof(sslConn_t)); clnConn = psMalloc(MATRIX_NO_POOL, sizeof(sslConn_t)); memset(svrConn, 0, sizeof(sslConn_t)); diff --git a/matrixssl/tls.c b/matrixssl/tls.c index 02fd8a7..8571fb1 100644 --- a/matrixssl/tls.c +++ b/matrixssl/tls.c @@ -714,7 +714,6 @@ int32 matrixSslNewHelloExtension(tlsExtension_t **extension, void *userPoolPtr) psPool_t *pool = NULL; tlsExtension_t *ext; - ext = psMalloc(pool, sizeof(tlsExtension_t)); if (ext == NULL) { return PS_MEM_FAIL; diff --git a/matrixssl/version.h b/matrixssl/version.h index 22cbfcb..f0a822f 100644 --- a/matrixssl/version.h +++ b/matrixssl/version.h @@ -1,6 +1,6 @@ /** * @file version.h - * @version + * @version $Format:%h%d$ * * Autogenerated version file. */