simolus3 / sqlite3.dart

sqlite3 bindings for Dart
MIT License
205 stars 70 forks source link

SQLCipher doesn't work on windows #177

Open elvinasn opened 11 months ago

elvinasn commented 11 months ago

When trying to use sqlcipher_flutter_libs package with sqlite3 and drift on windows, the build fails. Android, iOS and macOS are working. On windows I get the following errors:

libcrypto64MDd.lib(libcrypto-lib-e_capi.obj) : error LNK2019: unresolved external symbol __imp_CertOpenStore referenced in function capi_open_store [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-e_capi.obj) : error LNK2019: unresolved external symbol __imp_CertCloseStore referenced in function capi_find_key [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-e_capi.obj) : error LNK2019: unresolved external symbol __imp_CertEnumCertificatesInStore referenced in function capi_find_cert [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-e_capi.obj) : error LNK2019: unresolved external symbol __imp_CertFindCertificateInStore referenced in function capi_find_cert [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-e_capi.obj) : error LNK2019: unresolved external symbol __imp_CertDuplicateCertificateContext referenced in function capi_load_ssl_client_cert [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-e_capi.obj) : error LNK2019: unresolved external symbol __imp_CertFreeCertificateContext referenced in function capi_find_key [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-e_capi.obj) : error LNK2019: unresolved external symbol __imp_CertGetCertificateContextProperty referenced in function capi_cert_get_fname [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2019: unresolved external symbol __imp_ioctlsocket referenced in function BIO_socket_ioctl [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2019: unresolved external symbol __imp_getsockname referenced in function BIO_sock_info [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2019: unresolved external symbol __imp_getsockopt referenced in function BIO_sock_error [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2001: unresolved external symbol __imp_getsockopt [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2019: unresolved external symbol __imp_ntohs referenced in function BIO_get_port [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2001: unresolved external symbol __imp_ntohs [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2019: unresolved external symbol __imp_select referenced in function BIO_socket_wait [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2019: unresolved external symbol __imp_gethostbyname referenced in function BIO_gethostbyname [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2001: unresolved external symbol __imp_gethostbyname [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2019: unresolved external symbol __imp_WSAStartup referenced in function BIO_sock_init [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2019: unresolved external symbol __imp_WSACleanup referenced in function bio_sock_cleanup_int [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_conn.obj) : error LNK2019: unresolved external symbol __imp_WSAGetLastError referenced in function conn_read [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_sock.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2001: unresolved external symbol __imp_WSAGetLastError [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2019: unresolved external symbol __imp_htonl referenced in function WspiapiLegacyGetAddrInfo [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2019: unresolved external symbol __imp_htons referenced in function BIO_lookup_ex [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2019: unresolved external symbol __imp_inet_addr referenced in function WspiapiParseV4Address [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2019: unresolved external symbol __imp_inet_ntoa referenced in function WspiapiLegacyGetAddrInfo [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2019: unresolved external symbol __imp_gethostbyaddr referenced in function WspiapiLegacyGetNameInfo [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2019: unresolved external symbol __imp_getservbyport referenced in function WspiapiLegacyGetNameInfo [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2019: unresolved external symbol __imp_getservbyname referenced in function BIO_lookup_ex [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_addr.obj) : error LNK2019: unresolved external symbol __imp_WSASetLastError referenced in function WspiapiGetAddrInfo [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_sock.obj) : error LNK2001: unresolved external symbol __imp_WSASetLastError [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_conn.obj) : error LNK2001: unresolved external symbol __imp_WSASetLastError [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_sock.obj) : error LNK2019: unresolved external symbol __imp_recv referenced in function sock_read [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_conn.obj) : error LNK2001: unresolved external symbol __imp_recv [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_sock.obj) : error LNK2019: unresolved external symbol __imp_send referenced in function sock_write [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_conn.obj) : error LNK2001: unresolved external symbol __imp_send [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2019: unresolved external symbol __imp_accept referenced in function BIO_accept_ex [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2019: unresolved external symbol __imp_bind referenced in function BIO_bind [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2019: unresolved external symbol __imp_closesocket referenced in function BIO_accept_ex [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2019: unresolved external symbol __imp_connect referenced in function BIO_connect [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2019: unresolved external symbol __imp_listen referenced in function BIO_listen [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2019: unresolved external symbol __imp_setsockopt referenced in function BIO_connect [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bio_sock2.obj) : error LNK2019: unresolved external symbol __imp_socket referenced in function BIO_socket [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] libcrypto64MDd.lib(libcrypto-lib-bss_conn.obj) : error LNK2019: unresolved external symbol __imp_shutdown referenced in function conn_close_socket [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\Debug\sqlite3.dll : fatal error LNK1120: 34 unresolved externals [C:\Users\elviz\OneDrive\Desktop\New folder (2)\app\build\windows\plugins\sqlcipher_flutter_libs\sqlite3.vcxproj] Exception: Build process failed.

simolus3 commented 11 months ago

It looks like statically linking OpenSSL fails. Did you install OpenSSL development libraries on your Windows build machine? I'll also take another look at this, but it will take a few days until I have access to a Windows VM again.

elvinasn commented 11 months ago

Yes, I did.

openssl version
OpenSSL 3.1.1 30 May 2023 (Library: OpenSSL 3.1.1 30 May 2023)
elvinasn commented 11 months ago

I fixed this by adding following lines into ./windows/CMakeLists.txt

include_directories("C:\\Program Files\\OpenSSL-Win64\\include")
link_directories("C:\\Program Files\\OpenSSL-Win64\\lib")
link_libraries(libssl libcrypto)
simolus3 commented 11 months ago

Did you install OpenSSL manually or through a package manager like Chocolatey? I think there's a way to add OpenSSL to PATH so that it gets found by CMake by default.

elvinasn commented 11 months ago

I installed it through choco, and it was added to PATH automatically.

vannakmini commented 4 months ago

CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:230 (message): Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR) Call Stack (most recent call first): C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE) C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.25/Modules/FindOpenSSL.cmake:613 (find_package_handle_standard_args) flutter/ephemeral/.plugin_symlinks/sqlcipher_flutter_libs/windows/CMakeLists.txt:27 (find_package)

Error: Unable to generate build files

may i know how to fix it i have ready install openssl

saadinstitute commented 2 months ago

I have the same issue, it keep tell me the following:

CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
  system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY) (found
  version "3.3.0")
Call Stack (most recent call first):
  C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)     
  C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.28/Modules/FindOpenSSL.cmake:668 (find_package_handle_standard_args)
  flutter/ephemeral/.plugin_symlinks/sqlcipher_flutter_libs/windows/CMakeLists.txt:27 (find_package)

I have openssl already been installed.

How can I resolve this issue?

velten-dev commented 2 months ago

@simolus3 I am seeing the same issue as @saadinstitute with a fresh install of Windows 10. I used chocolatey to install the SSL libs, but it seems that CMAKE can't find it. I tried setting an environment variable for the OPENSSL_ROOT_DIR in the user env, the system env, and setting a variable with that name in the windows CMAKE file. I also tried uninstalling VS2022 for VS2019 as I remember seeing a warning about VS2022 not being supported.

I am unfortunately not that familiar with how to resolve an issue like this, especially from windows. Most of the help you find online assume Linux for a CMAKE issue. I verified a non-encrypted Drift project does compile on the system. Any suggestions on other tests to make or configuration to change to let if find the SSL libraries?

awaters-meltin commented 1 month ago

I had the same problem (Windows 11) - after some digging it appears to be an issue with latest Chocolatey OpenSSL versions (from 3.2+).

If you install version 3.1.1 that works:

choco install openssl --version 3.1.1 -y
anisalibegic commented 1 month ago

I managed it to work by adding what @elvinasn said AND by adding sqlcipher_library_windows package.

@simolus3 Is this some weird workaround or it's intended to work this way? I feel like I'm "cheating" but @elvinasn's comment fixed those linking errors and sqlcipher_library_windows fixed an error where sqlcipher.dll was not found. I'm also using OpenSSL 3.3.1 installed via Chocolatey.

simolus3 commented 1 month ago

Sorry, I'm not very familiar with Windows development. I think that CMake should be able to find OpenSSL when installed through Chocolatey, at least it behaved like that in my VM. But I also don't know what could cause that to break.

Obviously the solution isn't great, but if it works and the compiled app also runs on other Windows machines without the workaround, I don't see any potential for this to go wrong or be compiled/linked in a broken way.

anisalibegic commented 1 month ago

Sorry, I'm not very familiar with Windows development. I think that CMake should be able to find OpenSSL when installed through Chocolatey, at least it behaved like that in my VM. But I also don't know what could cause that to break.

Obviously the solution isn't great, but if it works and the compiled app also runs on other Windows machines without the workaround, I don't see any potential for this to go wrong or be compiled/linked in a broken way.

I went through sqlcipher_library_windows and I see that it's keeping required DLLs in it's assets folder, and it copies them manually to the same place where executable is generated.

Do you think it would be better to create a separated plugin for Windows platform only, where we can write a POST BUILD script that will copy all DLLs where binary is generated? With this approach we don't need to copy DLLs on other platforms since they don't need them. I can definitely do that since it's more lightweight than the mentioned package, which helps in this case.