cmake: adjust libraries linking
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index 585e75e..9d08a4a 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -86,10 +86,6 @@
     ${src_tls}
 )
 
-if(WIN32)
-    set(libs ws2_32)
-endif(WIN32)
-
 if(CMAKE_COMPILER_IS_GNUCC)
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
 endif(CMAKE_COMPILER_IS_GNUCC)
@@ -98,13 +94,27 @@
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
 endif(CMAKE_COMPILER_IS_CLANG)
 
+if(WIN32)
+    set(libs ${libs} ws2_32)
+endif(WIN32)
+
+if(USE_PKCS11_HELPER_LIBRARY)
+    set(libs ${libs} pkcs11-helper)
+endif(USE_PKCS11_HELPER_LIBRARY)
+
+if(ENABLE_ZLIB_SUPPORT)
+    set(libs ${libs} ${ZLIB_LIBRARIES})
+endif(ENABLE_ZLIB_SUPPORT)
+
+if(LINK_WITH_PTHREAD)
+    set(libs ${libs} pthread)
+endif()
+
 if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
     message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
 endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
 
 if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
-    # if we build both static an shared, then let
-    # tests and programs link to the shared lib target
     set(mbedtls_static_target "mbedtls_static")
 elseif(USE_STATIC_MBEDTLS_LIBRARY)
     set(mbedtls_static_target "mbedtls")
@@ -115,33 +125,16 @@
     set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
     target_link_libraries(${mbedtls_static_target} ${libs})
 
-    if(ZLIB_FOUND)
-        target_link_libraries(${mbedtls_static_target} ${ZLIB_LIBRARIES})
-    endif(ZLIB_FOUND)
-
-    if(LINK_WITH_PTHREAD)
-        target_link_libraries(${mbedtls_static_target} pthread)
-    endif()
-
     install(TARGETS ${mbedtls_static_target}
-        DESTINATION ${LIB_INSTALL_DIR}
-        PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+            DESTINATION ${LIB_INSTALL_DIR}
+            PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 endif()
 
 if(USE_SHARED_MBEDTLS_LIBRARY)
     add_library(mbedtls SHARED ${src})
     set_target_properties(mbedtls PROPERTIES VERSION 1.4.0 SOVERSION 8)
-
     target_link_libraries(mbedtls ${libs})
 
-    if(ZLIB_FOUND)
-        target_link_libraries(mbedtls ${ZLIB_LIBRARIES})
-    endif(ZLIB_FOUND)
-
-    if(LINK_WITH_PTHREAD)
-        target_link_libraries(mbedtls pthread)
-    endif()
-
     install(TARGETS mbedtls
             DESTINATION ${LIB_INSTALL_DIR}
             PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)