2
0
Эх сурвалжийг харах

Added SHA1 support via OpenSSL or Nettle

Paul-Louis Ageneau 4 жил өмнө
parent
commit
052ce0010e

+ 9 - 3
CMakeLists.txt

@@ -115,6 +115,7 @@ set(LIBDATACHANNEL_IMPL_SOURCES
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/track.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/processor.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/base64.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/sha.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/tcptransport.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/tlstransport.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/verifiedtlstransport.cpp
@@ -140,6 +141,7 @@ set(LIBDATACHANNEL_IMPL_HEADERS
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/track.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/processor.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/base64.hpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/sha.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/tcptransport.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/tlstransport.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/verifiedtlstransport.hpp
@@ -278,13 +280,17 @@ if (USE_GNUTLS)
 			IMPORTED_LINK_INTERFACE_LANGUAGES C
 			IMPORTED_LOCATION "${GNUTLS_LIBRARIES}")
 	endif()
+	if (NOT NO_WEBSOCKET)
+		# Needed for SHA1
+		find_package(Nettle REQUIRED)
+	endif()
 	target_compile_definitions(datachannel PRIVATE USE_GNUTLS=1)
 	target_compile_definitions(datachannel-static PRIVATE USE_GNUTLS=1)
-	target_link_libraries(datachannel PRIVATE GnuTLS::GnuTLS)
-	target_link_libraries(datachannel-static PRIVATE GnuTLS::GnuTLS)
+	target_link_libraries(datachannel PRIVATE GnuTLS::GnuTLS Nettle::Nettle)
+	target_link_libraries(datachannel-static PRIVATE GnuTLS::GnuTLS Nettle::Nettle)
 else()
 	if(APPLE)
-		# This is a bug in CMake that causes it to prefer the system version over 
+		# This is a bug in CMake that causes it to prefer the system version over
 		# the one in the specified ROOT folder
 		if(EXISTS ${OPENSSL_ROOT_DIR})
 			set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_ROOT_DIR}/lib/libcrypto.dylib" CACHE FILEPATH "" FORCE)

+ 17 - 0
cmake/Modules/FindNettle.cmake

@@ -0,0 +1,17 @@
+if (NOT TARGET Nettle::Nettle)
+	find_path(NETTLE_INCLUDE_DIR nettle/hmac.h)
+	find_library(NETTLE_LIBRARY NAMES nettle libnettle)
+
+	include(FindPackageHandleStandardArgs)
+	find_package_handle_standard_args(Nettle DEFAULT_MSG NETTLE_LIBRARY NETTLE_INCLUDE_DIR)
+
+    if (Nettle_FOUND)
+        add_library(Nettle::Nettle UNKNOWN IMPORTED)
+        set_target_properties(Nettle::Nettle PROPERTIES
+            IMPORTED_LOCATION "${NETTLE_LIBRARY}"
+            INTERFACE_INCLUDE_DIRECTORIES "${NETTLE_INCLUDE_DIRS}"
+            INTERFACE_LINK_LIBRARIES "${NETTLE_LIBRARIES}"
+                IMPORTED_LINK_INTERFACE_LANGUAGES "C")
+    endif ()
+endif ()
+

+ 56 - 0
src/impl/sha.cpp

@@ -0,0 +1,56 @@
+/**
+ * Copyright (c) 2021 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "sha.hpp"
+
+#if RTC_ENABLE_WEBSOCKET
+
+#if USE_GNUTLS
+#include <nettle/sha1.h>
+#else
+#include <openssl/sha.h>
+#endif
+
+namespace rtc::impl {
+
+binary Sha1(const binary &input) {
+#if USE_GNUTLS
+
+binary output(SHA1_DIGEST_SIZE);
+struct sha1_ctx ctx;
+sha1_init(&ctx);
+sha1_update(&ctx, input.size(), input.data());
+sha1_digest(&ctx, SHA1_DIGEST_SIZE, output.size());
+return output;
+
+#else // USE_GNUTLS==0
+
+binary output(SHA_DIGEST_LENGTH);
+SHA_CTX ctx;
+SHA1_Init(&ctx);
+SHA1_Update(&ctx, input.data(), input.size());
+SHA1_Final(reinterpret_cast<unsigned char*>(output.data()), &ctx);
+return output;
+
+#endif
+}
+
+} // namespace rtc::impl
+
+#endif
+

+ 34 - 0
src/impl/sha.hpp

@@ -0,0 +1,34 @@
+/**
+ * Copyright (c) 2021 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RTC_IMPL_SHA_H
+#define RTC_IMPL_SHA_H
+
+#if RTC_ENABLE_WEBSOCKET
+
+#include "common.hpp"
+
+namespace rtc::impl {
+
+binary Sha1(const binary &input);
+
+} // namespace rtc::impl
+
+#endif
+
+#endif