Blame SOURCES/rsyslog-8.2102.0-rhbz2046158-correct-custom-ciphers-behaviour.patch

537b07
diff -up rsyslog-8.2102.0/runtime/nsd_ossl.c.orig rsyslog-8.2102.0/runtime/nsd_ossl.c
537b07
--- rsyslog-8.2102.0/runtime/nsd_ossl.c.orig	2022-04-15 13:42:05.320615894 +0200
537b07
+++ rsyslog-8.2102.0/runtime/nsd_ossl.c	2022-04-15 14:33:43.472482696 +0200
537b07
@@ -609,10 +609,10 @@ finalize_it:
537b07
 }
537b07
 
537b07
 static rsRetVal
537b07
-osslInitSession(nsd_ossl_t *pThis) /* , nsd_ossl_t *pServer) */
537b07
+osslInitSession(nsd_ossl_t *pThis, osslSslState_t osslType) /* , nsd_ossl_t *pServer) */
537b07
 {
537b07
 	DEFiRet;
537b07
-	BIO *client;
537b07
+	BIO *conn;
537b07
 	char pristringBuf[4096];
537b07
 	nsd_ptcp_t *pPtcp = (nsd_ptcp_t*) pThis->pTcp;
537b07
 
537b07
@@ -633,10 +633,8 @@ osslInitSession(nsd_ossl_t *pThis) /* ,
537b07
 		if (pThis->DrvrVerifyDepth != 0) {
537b07
 			SSL_set_verify_depth(pThis->ssl, pThis->DrvrVerifyDepth);
537b07
 		}
537b07
-	}
537b07
-
537b07
-	if (bAnonInit == 1) { /* no mutex needed, read-only after init */
537b07
-		/* Allow ANON Ciphers */
537b07
+	} else 	if (bAnonInit == 1 && pThis->gnutlsPriorityString == NULL) {
537b07
+		/* Allow ANON Ciphers only in ANON Mode and if no custom priority string is defined */
537b07
 		#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
537b07
 		 /* NOTE: do never use: +eNULL, it DISABLES encryption! */
537b07
 		strncpy(pristringBuf, "ALL:+COMPLEMENTOFDEFAULT:+ADH:+ECDH:+aNULL@SECLEVEL=0",
537b07
@@ -653,21 +651,28 @@ osslInitSession(nsd_ossl_t *pThis) /* ,
537b07
 		}
537b07
 	}
537b07
 
537b07
-	/* Create BIO from ptcp socket! */
537b07
-	client = BIO_new_socket(pPtcp->sock, BIO_CLOSE /*BIO_NOCLOSE*/);
537b07
-	dbgprintf("osslInitSession: Init client BIO[%p] done\n", (void *)client);
537b07
 
537b07
-	/* Set debug Callback for client BIO as well! */
537b07
-	BIO_set_callback(client, BIO_debug_callback);
537b07
+	/* Create BIO from ptcp socket! */
537b07
+	conn = BIO_new_socket(pPtcp->sock, BIO_CLOSE /*BIO_NOCLOSE*/);
537b07
+	dbgprintf("osslInitSession: Init conn BIO[%p] done\n", (void *)conn);
537b07
 
537b07
-/* TODO: still needed? Set to NON blocking ! */
537b07
-BIO_set_nbio( client, 1 );
537b07
+	/* Set debug Callback for conn BIO as well! */
537b07
+	BIO_set_callback(conn, BIO_debug_callback);
537b07
 
537b07
-	SSL_set_bio(pThis->ssl, client, client);
537b07
-	SSL_set_accept_state(pThis->ssl); /* sets ssl to work in server mode. */
537b07
+	/* TODO: still needed? Set to NON blocking ! */
537b07
+	BIO_set_nbio( conn, 1 );
537b07
+	SSL_set_bio(pThis->ssl, conn, conn);
537b07
 
537b07
+	if (osslType == osslServer) {
537b07
+		/* Server Socket */
537b07
+		SSL_set_accept_state(pThis->ssl); /* sets ssl to work in server mode. */
537b07
+		pThis->sslState = osslServer; /*set Server state */
537b07
+	} else {
537b07
+		/* Client Socket */
537b07
+		SSL_set_connect_state(pThis->ssl); /*sets ssl to work in client mode.*/
537b07
+		pThis->sslState = osslClient; /*set Client state */
537b07
+	}
537b07
 	pThis->bHaveSess = 1;
537b07
-	pThis->sslState = osslServer; /*set Server state */
537b07
 
537b07
 	/* we are done */
537b07
 	FINALIZE;
537b07
@@ -1136,8 +1141,8 @@ SetAuthMode(nsd_t *const pNsd, uchar *co
537b07
 		ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED);
537b07
 	}
537b07
 
537b07
-		/* Init Anon OpenSSL stuff */
537b07
-		CHKiRet(osslAnonInit());
537b07
+	/* Init Anon OpenSSL stuff */
537b07
+	CHKiRet(osslAnonInit());
537b07
 
537b07
 	dbgprintf("SetAuthMode: Set Mode %s/%d\n", mode, pThis->authMode);
537b07
 
537b07
@@ -1394,8 +1399,9 @@ osslPostHandshakeCheck(nsd_ossl_t *pNsd)
537b07
 
537b07
 	#if OPENSSL_VERSION_NUMBER >= 0x10002000L
537b07
 	if(SSL_get_shared_curve(pNsd->ssl, -1) == 0) {
537b07
-		LogError(0, RS_RET_NO_ERRCODE, "nsd_ossl:"
537b07
-		"No shared curve between syslog client and server.");
537b07
+		// This is not a failure
537b07
+		LogMsg(0, RS_RET_NO_ERRCODE, LOG_INFO, "nsd_ossl: "
537b07
+		"Information, no shared curve between syslog client and server");
537b07
 	}
537b07
 	#endif
537b07
 	sslCipher = (const SSL_CIPHER*) SSL_get_current_cipher(pNsd->ssl);
537b07
@@ -1518,7 +1524,7 @@ AcceptConnReq(nsd_t *pNsd, nsd_t **ppNew
537b07
 	pNew->permitExpiredCerts = pThis->permitExpiredCerts;
537b07
 	pNew->pPermPeers = pThis->pPermPeers;
537b07
 	pNew->DrvrVerifyDepth = pThis->DrvrVerifyDepth;
537b07
-	CHKiRet(osslInitSession(pNew));
537b07
+	CHKiRet(osslInitSession(pNew, osslServer));
537b07
 
537b07
 	/* Store nsd_ossl_t* reference in SSL obj */
537b07
 	SSL_set_ex_data(pNew->ssl, 0, pThis);
537b07
@@ -1729,9 +1735,6 @@ Connect(nsd_t *pNsd, int family, uchar *
537b07
 	DEFiRet;
537b07
 	DBGPRINTF("openssl: entering Connect family=%d, device=%s\n", family, device);
537b07
 	nsd_ossl_t* pThis = (nsd_ossl_t*) pNsd;
537b07
-	nsd_ptcp_t* pPtcp = (nsd_ptcp_t*) pThis->pTcp;
537b07
-	BIO *conn;
537b07
-	char pristringBuf[4096];
537b07
 
537b07
 	ISOBJ_TYPE_assert(pThis, nsd_ossl);
537b07
 	assert(port != NULL);
537b07
@@ -1745,61 +1748,13 @@ Connect(nsd_t *pNsd, int family, uchar *
537b07
 		FINALIZE;
537b07
 	}
537b07
 
537b07
-	/* Create BIO from ptcp socket! */
537b07
-	conn = BIO_new_socket(pPtcp->sock, BIO_CLOSE /*BIO_NOCLOSE*/);
537b07
-	dbgprintf("Connect: Init conn BIO[%p] done\n", (void *)conn);
537b07
-
537b07
 	LogMsg(0, RS_RET_NO_ERRCODE, LOG_INFO, "nsd_ossl: "
537b07
 		"TLS Connection initiated with remote syslog server.");
537b07
 	/*if we reach this point we are in tls mode */
537b07
 	DBGPRINTF("Connect: TLS Mode\n");
537b07
-	if(!(pThis->ssl = SSL_new(ctx))) {
537b07
-		pThis->ssl = NULL;
537b07
-		osslLastSSLErrorMsg(0, pThis->ssl, LOG_ERR, "Connect");
537b07
-		ABORT_FINALIZE(RS_RET_NO_ERRCODE);
537b07
-	}
537b07
 
537b07
-	// Set SSL_MODE_AUTO_RETRY to SSL obj
537b07
-	SSL_set_mode(pThis->ssl, SSL_MODE_AUTO_RETRY);
537b07
-
537b07
-	if (pThis->authMode != OSSL_AUTH_CERTANON) {
537b07
-		dbgprintf("Connect: enable certificate checking (Mode=%d, VerifyDepth=%d)\n",
537b07
-			pThis->authMode, pThis->DrvrVerifyDepth);
537b07
-		/* Enable certificate valid checking */
537b07
-		SSL_set_verify(pThis->ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback);
537b07
-		if (pThis->DrvrVerifyDepth != 0) {
537b07
-			SSL_set_verify_depth(pThis->ssl, pThis->DrvrVerifyDepth);
537b07
-		}
537b07
-	}
537b07
-
537b07
-	if (bAnonInit == 1) { /* no mutex needed, read-only after init */
537b07
-		/* Allow ANON Ciphers */
537b07
-		#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
537b07
-		 /* NOTE: do never use: +eNULL, it DISABLES encryption! */
537b07
-		strncpy(pristringBuf, "ALL:+COMPLEMENTOFDEFAULT:+ADH:+ECDH:+aNULL@SECLEVEL=0",
537b07
-			sizeof(pristringBuf));
537b07
-		#else
537b07
-		strncpy(pristringBuf, "ALL:+COMPLEMENTOFDEFAULT:+ADH:+ECDH:+aNULL",
537b07
-			sizeof(pristringBuf));
537b07
-		#endif
537b07
-
537b07
-		dbgprintf("Connect: setting anon ciphers: %s\n", pristringBuf);
537b07
-		if ( SSL_set_cipher_list(pThis->ssl, pristringBuf) == 0 ){
537b07
-			dbgprintf("Connect: Error setting ciphers '%s'\n", pristringBuf);
537b07
-			ABORT_FINALIZE(RS_RET_SYS_ERR);
537b07
-		}
537b07
-	}
537b07
-
537b07
-	/* Set debug Callback for client BIO as well! */
537b07
-	BIO_set_callback(conn, BIO_debug_callback);
537b07
-
537b07
-/* TODO: still needed? Set to NON blocking ! */
537b07
-BIO_set_nbio( conn, 1 );
537b07
-
537b07
-	SSL_set_bio(pThis->ssl, conn, conn);
537b07
-	SSL_set_connect_state(pThis->ssl); /*sets ssl to work in client mode.*/
537b07
-	pThis->sslState = osslClient; /*set Client state */
537b07
-	pThis->bHaveSess = 1;
537b07
+	/* Do SSL Session init */
537b07
+	CHKiRet(osslInitSession(pThis, osslClient));
537b07
 
537b07
 	/* Store nsd_ossl_t* reference in SSL obj */
537b07
 	SSL_set_ex_data(pThis->ssl, 0, pThis);
537b07
@@ -1828,90 +1783,106 @@ SetGnutlsPriorityString(nsd_t *const pNs
537b07
 	nsd_ossl_t* pThis = (nsd_ossl_t*) pNsd;
537b07
 	ISOBJ_TYPE_assert(pThis, nsd_ossl);
537b07
 
537b07
-	pThis->gnutlsPriorityString = gnutlsPriorityString;
537b07
+	dbgprintf("gnutlsPriorityString: set to '%s'\n",
537b07
+		(gnutlsPriorityString != NULL ? (char*)gnutlsPriorityString : "NULL"));
537b07
 
537b07
 	/* Skip function if function is NULL gnutlsPriorityString */
537b07
-	if (gnutlsPriorityString == NULL) {
537b07
-		RETiRet;
537b07
-	} else {
537b07
-		dbgprintf("gnutlsPriorityString: set to '%s'\n", gnutlsPriorityString);
537b07
 #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
537b07
-		char *pCurrentPos;
537b07
-		char *pNextPos;
537b07
-		char *pszCmd;
537b07
-		char *pszValue;
537b07
-		int iConfErr;
537b07
-
537b07
-		/* Set working pointer */
537b07
-		pCurrentPos = (char*) pThis->gnutlsPriorityString;
537b07
-		if (pCurrentPos != NULL && strlen(pCurrentPos) > 0) {
537b07
-			// Create CTX Config Helper
537b07
-			SSL_CONF_CTX *cctx;
537b07
-			cctx = SSL_CONF_CTX_new();
537b07
-			if (pThis->sslState == osslServer) {
537b07
-				SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
537b07
-			} else {
537b07
-				SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
537b07
-			}
537b07
-			SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
537b07
-			SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SHOW_ERRORS);
537b07
-			SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
537b07
-
537b07
-			do
537b07
-			{
537b07
-				pNextPos = index(pCurrentPos, '=');
537b07
-				if (pNextPos != NULL) {
537b07
-					while (	*pCurrentPos != '\0' &&
537b07
-						(*pCurrentPos == ' ' || *pCurrentPos == '\t') )
537b07
-						pCurrentPos++;
537b07
-					pszCmd = strndup(pCurrentPos, pNextPos-pCurrentPos);
537b07
-					pCurrentPos = pNextPos+1;
537b07
-					pNextPos = index(pCurrentPos, '\n');
537b07
-					pszValue = (pNextPos == NULL ?
537b07
-							strdup(pCurrentPos) :
537b07
-							strndup(pCurrentPos, pNextPos - pCurrentPos));
537b07
-					pCurrentPos = (pNextPos == NULL ? NULL : pNextPos+1);
537b07
-
537b07
-					/* Add SSL Conf Command */
537b07
-					iConfErr = SSL_CONF_cmd(cctx, pszCmd, pszValue);
537b07
-					if (iConfErr > 0) {
537b07
-						dbgprintf("gnutlsPriorityString: Successfully added Command "
537b07
-							"'%s':'%s'\n",
537b07
-							pszCmd, pszValue);
537b07
-					}
537b07
-					else {
537b07
-						LogError(0, RS_RET_SYS_ERR, "Failed to added Command: %s:'%s' "
537b07
-							"in gnutlsPriorityString with error '%d'",
537b07
-							pszCmd, pszValue, iConfErr);
537b07
-					}
537b07
+	sbool ApplySettings = 0;
537b07
+	if ((gnutlsPriorityString != NULL && pThis->gnutlsPriorityString == NULL) ||
537b07
+		(gnutlsPriorityString != NULL &&
537b07
+		strcmp( (const char*)pThis->gnutlsPriorityString, (const char*)gnutlsPriorityString) != 0)
537b07
+		) {
537b07
+		ApplySettings = 1;
537b07
+	}
537b07
+
537b07
+	pThis->gnutlsPriorityString = gnutlsPriorityString;
537b07
+	dbgprintf("gnutlsPriorityString: set to '%s' Apply %s\n",
537b07
+		(gnutlsPriorityString != NULL ? (char*)gnutlsPriorityString : "NULL"),
537b07
+		(ApplySettings == 1? "TRUE" : "FALSE"));
537b07
 
537b07
-					free(pszCmd);
537b07
-					free(pszValue);
537b07
+	if (ApplySettings) {
537b07
+
537b07
+		if (gnutlsPriorityString == NULL || ctx == NULL) {
537b07
+			RETiRet;
537b07
+		} else {
537b07
+			dbgprintf("gnutlsPriorityString: set to '%s'\n", gnutlsPriorityString);
537b07
+			char *pCurrentPos;
537b07
+			char *pNextPos;
537b07
+			char *pszCmd;
537b07
+			char *pszValue;
537b07
+			int iConfErr;
537b07
+
537b07
+			/* Set working pointer */
537b07
+			pCurrentPos = (char*) pThis->gnutlsPriorityString;
537b07
+			if (pCurrentPos != NULL && strlen(pCurrentPos) > 0) {
537b07
+				// Create CTX Config Helper
537b07
+				SSL_CONF_CTX *cctx;
537b07
+				cctx = SSL_CONF_CTX_new();
537b07
+				if (pThis->sslState == osslServer) {
537b07
+					SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
537b07
 				} else {
537b07
-					/* Abort further parsing */
537b07
-					pCurrentPos = NULL;
537b07
+					SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
537b07
 				}
537b07
-			}
537b07
-			while (pCurrentPos != NULL);
537b07
+				SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
537b07
+				SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SHOW_ERRORS);
537b07
+				SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
537b07
+
537b07
+				do
537b07
+				{
537b07
+					pNextPos = index(pCurrentPos, '=');
537b07
+					if (pNextPos != NULL) {
537b07
+						while (	*pCurrentPos != '\0' &&
537b07
+							(*pCurrentPos == ' ' || *pCurrentPos == '\t') )
537b07
+							pCurrentPos++;
537b07
+						pszCmd = strndup(pCurrentPos, pNextPos-pCurrentPos);
537b07
+						pCurrentPos = pNextPos+1;
537b07
+						pNextPos = index(pCurrentPos, '\n');
537b07
+						pszValue = (pNextPos == NULL ?
537b07
+								strdup(pCurrentPos) :
537b07
+								strndup(pCurrentPos, pNextPos - pCurrentPos));
537b07
+						pCurrentPos = (pNextPos == NULL ? NULL : pNextPos+1);
537b07
+
537b07
+						/* Add SSL Conf Command */
537b07
+						iConfErr = SSL_CONF_cmd(cctx, pszCmd, pszValue);
537b07
+						if (iConfErr > 0) {
537b07
+							dbgprintf("gnutlsPriorityString: Successfully added Command "
537b07
+								"'%s':'%s'\n",
537b07
+								pszCmd, pszValue);
537b07
+						}
537b07
+						else {
537b07
+							LogError(0, RS_RET_SYS_ERR, "Failed to added Command: %s:'%s' "
537b07
+								"in gnutlsPriorityString with error '%d'",
537b07
+								pszCmd, pszValue, iConfErr);
537b07
+						}
537b07
+
537b07
+						free(pszCmd);
537b07
+						free(pszValue);
537b07
+					} else {
537b07
+						/* Abort further parsing */
537b07
+						pCurrentPos = NULL;
537b07
+					}
537b07
+				}
537b07
+				while (pCurrentPos != NULL);
537b07
 
537b07
-			/* Finalize SSL Conf */
537b07
-			iConfErr = SSL_CONF_CTX_finish(cctx);
537b07
-			if (!iConfErr) {
537b07
-				LogError(0, RS_RET_SYS_ERR, "Error: setting openssl command parameters: %s"
537b07
-						"Open ssl error info may follow in next messages",
537b07
-						pThis->gnutlsPriorityString);
537b07
-				osslLastSSLErrorMsg(0, NULL, LOG_ERR, "SetGnutlsPriorityString");
537b07
+				/* Finalize SSL Conf */
537b07
+				iConfErr = SSL_CONF_CTX_finish(cctx);
537b07
+				if (!iConfErr) {
537b07
+					LogError(0, RS_RET_SYS_ERR, "Error: setting openssl command parameters: %s"
537b07
+							"Open ssl error info may follow in next messages",
537b07
+							pThis->gnutlsPriorityString);
537b07
+					osslLastSSLErrorMsg(0, NULL, LOG_ERR, "SetGnutlsPriorityString");
537b07
+				}
537b07
+				SSL_CONF_CTX_free(cctx);
537b07
 			}
537b07
-			SSL_CONF_CTX_free(cctx);
537b07
 		}
537b07
+	}
537b07
 #else
537b07
-		dbgprintf("gnutlsPriorityString: set to '%s'\n", gnutlsPriorityString);
537b07
-		LogError(0, RS_RET_SYS_ERR, "Warning: TLS library does not support SSL_CONF_cmd API"
537b07
-			"(maybe it is too old?). Cannot use gnutlsPriorityString ('%s'). For more see: "
537b07
-			"https://www.rsyslog.com/doc/master/configuration/modules/imtcp.html#gnutlsprioritystring",
537b07
-			gnutlsPriorityString);
537b07
+	LogError(0, RS_RET_SYS_ERR, "Warning: TLS library does not support SSL_CONF_cmd API"
537b07
+		"(maybe it is too old?). Cannot use gnutlsPriorityString ('%s'). For more see: "
537b07
+		"https://www.rsyslog.com/doc/master/configuration/modules/imtcp.html#gnutlsprioritystring",
537b07
+		gnutlsPriorityString);
537b07
 #endif
537b07
-	}
537b07
 
537b07
 	RETiRet;
537b07
 }