[vlc-commits] keystore: Add a win32 crypt_file implementation

Hugo Beauzée-Luyssen git at videolan.org
Wed Nov 9 13:32:54 CET 2016


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Tue Nov  8 15:36:08 2016 +0100| [b9777ec6680ad812cd51c7f13db2688681a71a42] | committer: Hugo Beauzée-Luyssen

keystore: Add a win32 crypt_file implementation

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b9777ec6680ad812cd51c7f13db2688681a71a42
---

 modules/keystore/Makefile.am        |  4 ++
 modules/keystore/file_crypt.h       |  2 +-
 modules/keystore/file_crypt_win32.c | 81 +++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)

diff --git a/modules/keystore/Makefile.am b/modules/keystore/Makefile.am
index a985350..c7a3867 100644
--- a/modules/keystore/Makefile.am
+++ b/modules/keystore/Makefile.am
@@ -10,6 +10,10 @@ libfile_keystore_plugin_la_SOURCES = keystore/file.c \
 if HAVE_ANDROID
 libfile_keystore_plugin_la_SOURCES += keystore/file_crypt_android.c
 endif
+if HAVE_WIN32
+libfile_keystore_plugin_la_SOURCES += keystore/file_crypt_win32.c
+libfile_keystore_plugin_la_LIBADD = -lcrypt32
+endif
 keystore_LTLIBRARIES += libfile_keystore_plugin.la
 
 libsecret_plugin_la_SOURCES = keystore/secret.c
diff --git a/modules/keystore/file_crypt.h b/modules/keystore/file_crypt.h
index d62f03d..0d5ca7b 100644
--- a/modules/keystore/file_crypt.h
+++ b/modules/keystore/file_crypt.h
@@ -18,7 +18,7 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#if defined(__ANDROID__) /* TODO || defined(_WIN32) */
+#if defined(__ANDROID__) || defined(_WIN32)
 # define CRYPTFILE
 
 struct crypt
diff --git a/modules/keystore/file_crypt_win32.c b/modules/keystore/file_crypt_win32.c
new file mode 100644
index 0000000..2d4cf1f
--- /dev/null
+++ b/modules/keystore/file_crypt_win32.c
@@ -0,0 +1,81 @@
+/*****************************************************************************
+ * file_crypt_win32.c: Crypt using CryptProtectData
+ *****************************************************************************
+ * Copyright © 2016 VLC authors, VideoLAN and VideoLabs
+ *
+ * This program 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 program 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 program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_keystore.h>
+#include "file_crypt.h"
+
+#include <windows.h>
+#include <wincrypt.h>
+
+typedef WINBOOL WINAPI (*ProcessFunc)(DATA_BLOB*, LPCWSTR, DATA_BLOB*, PVOID, CRYPTPROTECT_PROMPTSTRUCT*,
+                                      DWORD, DATA_BLOB*);
+
+static size_t Process(const uint8_t *p_src, size_t i_src_len, uint8_t **pp_dst, ProcessFunc pf_process)
+{
+    DATA_BLOB input_blob =
+    {
+        .cbData = i_src_len,
+        .pbData = (BYTE*)p_src
+    };
+    DATA_BLOB output_blob;
+
+    if (pf_process( &input_blob, NULL, NULL, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, &output_blob) == FALSE)
+        return 0;
+    *pp_dst = malloc(output_blob.cbData);
+    if( unlikely( *pp_dst == NULL ) )
+    {
+        LocalFree( output_blob.pbData );
+        return 0;
+    }
+    memcpy( *pp_dst, output_blob.pbData, output_blob.cbData );
+    LocalFree( output_blob.pbData );
+    return output_blob.cbData;
+}
+
+static size_t Decrypt( vlc_keystore *p_keystore, void *p_ctx, const uint8_t *p_src,
+                      size_t i_src_len, uint8_t ** pp_dst )
+{
+    VLC_UNUSED( p_keystore );
+    VLC_UNUSED( p_ctx );
+    // Cast the function pointer to avoid an invalid parameter warning, regarding the "description"
+    // parameter. It's LPCWSTR in the case of CryptProtectData, and LPWSTR* in the case of CryptUnprotect
+    // Since we pass NULL anyway, we don't care
+    return Process( p_src, i_src_len, pp_dst, (ProcessFunc)&CryptUnprotectData );
+}
+
+static size_t Encrypt( vlc_keystore *p_keystore, void *p_ctx, const uint8_t *p_src,
+                       size_t i_src_len, uint8_t ** pp_dst )
+{
+    VLC_UNUSED( p_keystore );
+    VLC_UNUSED( p_ctx );
+    return Process( p_src, i_src_len, pp_dst, CryptProtectData );
+}
+
+int CryptInit(vlc_keystore *p_keystore, struct crypt *p_crypt)
+{
+    VLC_UNUSED( p_keystore );
+    p_crypt->pf_decrypt = Decrypt;
+    p_crypt->pf_encrypt = Encrypt;
+    return VLC_SUCCESS;
+}



More information about the vlc-commits mailing list