diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee2fe8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build +.vscode +*.o +*.debhelper.log +obj-x86_64-linux-gnu diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..81214c4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.12) + +project(ibmswtpm2 VERSION 1563 LANGUAGES C) + +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) + +include (GNUInstallDirs) +include(TestBigEndian) + +if(APPLE OR MSVC) + include(ConanAutoSetup) +endif() + +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +find_package(OpenSSL REQUIRED) +if(UNIX) + find_package(Threads REQUIRED) +endif() + +add_subdirectory(src) +add_subdirectory(utils) \ No newline at end of file diff --git a/README.md b/README.md index c2ca143..b0213a7 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,64 @@ This project is an implementation of the TCG TPM 2.0 specification. It is based on the TPM specification Parts 3 and 4 source code donated by Microsoft, with additional files to complete the implementation. See the companion IBM TSS at https://sourceforge.net/projects/ibmtpm20tss/ + +# Building +## Dependencies +* For all platforms +* * cmake - http://cmake.org +* * openssl ( See conan instructions for Mac and Windows easy deps ) +* Mac ( XCode and brew clang/gcc ) and Windows ( MSVC ) +* * conan - http://conan.io + +## Easy openssl dependency for Mac and Windows with Conan +* After install conan do: +``` +mkdir build +cd build +conan profile new default --detect +conan install .. +``` +On Windows Conan has preset binary dependencies for openssl 64 bits and MSVC 16 and up +If you get an error of non availability of openssl for your old MSVC build, you can execute +``` +conan install .. --build * +``` +This will rebuild the dependencies for your compiler + +## Simple build instructions for Linux and Mac +Building is straightforward, as long you installed all dependencies: +``` +mkdir build +cd build +cmake .. +make +``` + +## Simple build instructions for Windows Visual Studio +Build for Windows Visual Studio must be "almost" straightforward. +Builds are tested with MSVC +``` +mkdir build +cmake -DCMAKE_BUILD_TYPE=Release .. +cmake --build . +``` + +## Install +* Linux +On Linux, binary is installed in default sbin dir and an extra systemd service file is installed to start and stop the service. +No firewall exclusion files are included, so remember to open the required ports + +* Mac and Windows +Install works, but only mac is guaranteed to end up in proper path + +# Troubleshoot + +## Windows has symbols / 32/64 bits mismatch +Only 64 bits compilations are tested with conan deps. If you installed openssl by your own, some possible issues: +* Your compiler is 64 bits and you have 32 bits deps, configure this way: +``` +cmake -DCMAKE_BUILD_TYPE=Release -A win32 .. +``` + +## Mac You are using gcc or clang from Homebrew,ad Connan detected XCode Clang as default compiler +* Read profile section of Conan - https://docs.conan.io/en/latest/reference/profiles.html diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..7979f21 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,16 @@ +version: 1.0.{build} +image: Visual Studio 2019 + +install: +- cmd: echo "Downloading conan..." +- cmd: set PATH=%PATH%;%PYTHON%/Scripts/ +- cmd: pip.exe install conan +- cmd: conan user # Create the conan data directory +- cmd: conan --version + +build_script: +- cmd: mkdir build +- cmd: cd build +- cmd: conan install .. --build=missing +- cmd: cmake .. +- cmd: cmake --build . --config Release diff --git a/cmake/ConanAutoSetup.cmake b/cmake/ConanAutoSetup.cmake new file mode 100644 index 0000000..0ccf11c --- /dev/null +++ b/cmake/ConanAutoSetup.cmake @@ -0,0 +1,2 @@ +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup() \ No newline at end of file diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 0000000..d4e2417 --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,5 @@ + [requires] + openssl/1.1.1d + + [generators] + cmake \ No newline at end of file diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..fb84c72 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,4 @@ +ibmswtpm2 (0.1.1563) unstable; urgency=low + * First cmake port release + + -- Helio Chissini de Castro Mon, 10 Feb 2020 20:20:00 +0100 \ No newline at end of file diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..835287a --- /dev/null +++ b/debian/control @@ -0,0 +1,19 @@ +Source: ibmswtpm2 +Maintainer: Helio Chissini de Castro +Description: TPM2 Simulator +Priority: optional +Homepage: https://github.com/heliocastro/ibmswtpm2 +Build-Depends: cmake (>= 3.0~), + libssl-dev (>= 1.1.0~), + debhelper-compat (= 12) +Section: Miscellaneous +Rules-Requires-Root: no +Standards-Version: 4.4.0 + +Package: tpm-server +Architecture: any +Section: Miscellaneous +Depends: ${misc:Depends}, ${shlibs:Depends} +Description: This project is an implementation of the TCG TPM 2.0 specification. + It is based on the TPM specification Parts 3 and 4 source code + donated by Microsoft, with additional files to complete the implementation. \ No newline at end of file diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..96ac203 --- /dev/null +++ b/debian/rules @@ -0,0 +1,9 @@ +#!/usr/bin/make -f + +export DH_VERBOSE = 1 + +%: + dh $@ + +#override_dh_auto_install: +# dh_auto_install -- prefix=/usr diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..dcb810d --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,170 @@ +test_big_endian(BIG_ENDIAN) + +add_executable(tpm_server + ACTCommands.c + ACT_spt.c + AlgorithmCap.c + AlgorithmTests.c + AsymmetricCommands.c + Attest_spt.c + AttestationCommands.c + AuditCommands.c + Bits.c + BnConvert.c + BnMath.c + BnMemory.c + Cancel.c + CapabilityCommands.c + Clock.c + ClockCommands.c + CommandAudit.c + CommandCodeAttributes.c + CommandDispatcher.c + ContextCommands.c + Context_spt.c + CryptDes.c + CryptCmac.c + CryptEccData.c + CryptEccKeyExchange.c + CryptEccMain.c + CryptEccSignature.c + CryptHash.c + CryptPrime.c + CryptPrimeSieve.c + CryptRand.c + CryptRsa.c + CryptSelfTest.c + CryptSmac.c + CryptSym.c + CryptUtil.c + DA.c + DebugHelpers.c + DictionaryCommands.c + DuplicationCommands.c + EACommands.c + EncryptDecrypt_spt.c + Entity.c + Entropy.c + EphemeralCommands.c + ExecCommand.c + Global.c + Handle.c + HashCommands.c + Hierarchy.c + HierarchyCommands.c + IoBuffers.c + IntegrityCommands.c + Locality.c + LocalityPlat.c + ManagementCommands.c + Manufacture.c + Marshal.c + MathOnByteBuffers.c + Memory.c + NVCommands.c + NVDynamic.c + NVMem.c + NVReserved.c + NV_spt.c + Object.c + ObjectCommands.c + Object_spt.c + PCR.c + PP.c + PPPlat.c + PlatformACT.c + PlatformData.c + Policy_spt.c + Power.c + PowerPlat.c + PrimeData.c + PropertyCap.c + RandomCommands.c + Response.c + ResponseCodeProcessing.c + RsaKeyCache.c + RunCommand.c + Session.c + SessionCommands.c + SessionProcess.c + SigningCommands.c + StartupCommands.c + SymmetricCommands.c + TPMCmdp.c + TPMCmds.c + TestingCommands.c + Ticket.c + Time.c + TpmAsn1.c + TpmFail.c + TpmSizeChecks.c + TpmToOsslDesSupport.c + TpmToOsslMath.c + TpmToOsslSupport.c + Unique.c + Unmarshal.c + Vendor_TCG_Test.c + X509_ECC.c + X509_RSA.c + X509_spt.c + ntc2lib.c + ntc2.c + $<$:TcpServerPosix.c> + $<$:TcpServer.c> + $<$:applink.c> +) + +target_compile_definitions(tpm_server + PRIVATE + # Unix defines + $<$:_POSIX_> + $<$:TPM_POSIX> + # Windows defines + $<$:WIN32> + $<$:TPM_WINDOWS> + $<$:_CRT_SECURE_NO_WARNINGS> + $<$,$>:NO_FAIL_TRACE> + # Big endian + $<$:BIG_ENDIAN_TPM=YES> + # Common + USE_BIT_FIELD_STRUCTURES=NO + TPM_NUVOTON +) + +if(UNIX OR MINGW) + target_compile_options(tpm_server + PRIVATE + -Wall + -Werror + -Wmissing-declarations + -Wmissing-prototypes + -Wsign-compare + -Wnested-externs + $<$:-fno-strict-aliasing> + ) +else(MSVC) + target_compile_options(tpm_server + PRIVATE + /W3 + # Enable if you want warning as error + # /WX + ) +endif() + +target_include_directories(tpm_server + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} +) + +target_link_libraries(tpm_server + PRIVATE + OpenSSL::Crypto + $<$:Threads::Threads> + $<$:wsock32> + $<$:ws2_32> + $<$:crypt32> +) + +install(TARGETS tpm_server + RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR} +) diff --git a/src/CryptHash.h b/src/CryptHash.h index de9a060..adec8ab 100644 --- a/src/CryptHash.h +++ b/src/CryptHash.h @@ -88,7 +88,11 @@ typedef struct sequenceMethods { SMAC_DATA_METHOD data; SMAC_END_METHOD end; } SMAC_METHODS; -#define SMAC_IMPLEMENTED (defined CC_MAC || defined CC_MAC_Start) +#if (defined CC_MAC || defined CC_MAC_Start) +#define SMAC_IMPLEMENTED 1 +#else +#define SMAC_IMPLEMENTED 0 +#endif /* These definitions are here because the SMAC state is in the union of hash states. */ diff --git a/src/Marshal.c b/src/Marshal.c index cd6f3ec..a6181f1 100644 --- a/src/Marshal.c +++ b/src/Marshal.c @@ -1382,9 +1382,10 @@ TPMS_ENC_SCHEME_OAEP_Marshal(TPMS_ENC_SCHEME_OAEP *source, BYTE **buffer, INT32 UINT16 TPMS_ENC_SCHEME_RSAES_Marshal(TPMS_ENC_SCHEME_RSAES *source, BYTE **buffer, INT32 *size) { - source = source; - buffer = buffer; - size = size; + (void)source; + (void)buffer; + (void)size; + return 0; } diff --git a/src/TcpServer.c b/src/TcpServer.c index 9d0163e..25fcc7f 100644 --- a/src/TcpServer.c +++ b/src/TcpServer.c @@ -220,7 +220,7 @@ PlatformServer( break; case TPM_ACT_GET_SIGNALED: { - UINT32 actHandle; + UINT32 actHandle = 0; OK = ReadUINT32(s, &actHandle); WriteUINT32(s, _rpc__ACT_GetSignaled(actHandle)); break; diff --git a/src/TcpServerPosix.c b/src/TcpServerPosix.c index 20fcb29..ac87fa6 100644 --- a/src/TcpServerPosix.c +++ b/src/TcpServerPosix.c @@ -276,7 +276,7 @@ PlatformServer( break; case TPM_ACT_GET_SIGNALED: { - UINT32 actHandle; + UINT32 actHandle = 0; ok = ReadUINT32(s, &actHandle); WriteUINT32(s, _rpc__ACT_GetSignaled(actHandle)); break; diff --git a/src/Unmarshal.c b/src/Unmarshal.c index 2143ef9..218518b 100644 --- a/src/Unmarshal.c +++ b/src/Unmarshal.c @@ -767,7 +767,7 @@ TPM_RC TPMI_DH_SAVED_Unmarshal(TPMI_DH_SAVED *target, BYTE **buffer, INT32 *size, BOOL allowNull) { TPM_RC rc = TPM_RC_SUCCESS; - allowNull = allowNull; + (void)allowNull; if (rc == TPM_RC_SUCCESS) { rc = TPM_HANDLE_Unmarshal(target, buffer, size); @@ -1466,9 +1466,10 @@ TPM_RC TPMS_EMPTY_Unmarshal(TPMS_EMPTY *target, BYTE **buffer, INT32 *size) { TPM_RC rc = TPM_RC_SUCCESS; - target = target; - buffer = buffer; - size = size; + (void)target; + (void)buffer; + (void)size; + return rc; } diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt new file mode 100644 index 0000000..bf65eda --- /dev/null +++ b/utils/CMakeLists.txt @@ -0,0 +1,6 @@ +# Install systemd files for server if running on Linux +if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + configure_file(tpm_server.service.in tpm_server.service @ONLY) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tpm_server.service + DESTINATION /lib/systemd/system ) +endif() diff --git a/utils/tpm_server.service.in b/utils/tpm_server.service.in new file mode 100644 index 0000000..3429957 --- /dev/null +++ b/utils/tpm_server.service.in @@ -0,0 +1,8 @@ +[Unit] +Description=Implementation of the TCG TPM 2.0 specification + +[Service] +ExecStart=@CMAKE_INSTALL_FULL_SBINDIR@/tpm_server + +[Install] +WantedBy=multi-user.target