1 /*****************************************************************************
2 * file_crypt_win32.c: Crypt using CryptProtectData
3 *****************************************************************************
4 * Copyright © 2016 VLC authors, VideoLAN and VideoLabs
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include <vlc_keystore.h>
26 #include "file_crypt.h"
31 typedef BOOL (WINAPI
*ProcessFunc
)(DATA_BLOB
*, LPCWSTR
, DATA_BLOB
*, PVOID
,
32 CRYPTPROTECT_PROMPTSTRUCT
*, DWORD
, DATA_BLOB
*);
34 static size_t Process(const uint8_t *p_src
, size_t i_src_len
, uint8_t **pp_dst
, ProcessFunc pf_process
)
36 DATA_BLOB input_blob
=
39 .pbData
= (BYTE
*)p_src
41 DATA_BLOB output_blob
;
43 if (pf_process( &input_blob
, NULL
, NULL
, NULL
, NULL
, CRYPTPROTECT_UI_FORBIDDEN
, &output_blob
) == FALSE
)
45 *pp_dst
= malloc(output_blob
.cbData
);
46 if( unlikely( *pp_dst
== NULL
) )
48 LocalFree( output_blob
.pbData
);
51 memcpy( *pp_dst
, output_blob
.pbData
, output_blob
.cbData
);
52 LocalFree( output_blob
.pbData
);
53 return output_blob
.cbData
;
56 static size_t Decrypt( vlc_keystore
*p_keystore
, void *p_ctx
, const uint8_t *p_src
,
57 size_t i_src_len
, uint8_t ** pp_dst
)
59 VLC_UNUSED( p_keystore
);
61 // Cast the function pointer to avoid an invalid parameter warning, regarding the "description"
62 // parameter. It's LPCWSTR in the case of CryptProtectData, and LPWSTR* in the case of CryptUnprotect
63 // Since we pass NULL anyway, we don't care
64 return Process( p_src
, i_src_len
, pp_dst
, (ProcessFunc
)&CryptUnprotectData
);
67 static size_t Encrypt( vlc_keystore
*p_keystore
, void *p_ctx
, const uint8_t *p_src
,
68 size_t i_src_len
, uint8_t ** pp_dst
)
70 VLC_UNUSED( p_keystore
);
72 return Process( p_src
, i_src_len
, pp_dst
, CryptProtectData
);
75 int CryptInit(vlc_keystore
*p_keystore
, struct crypt
*p_crypt
)
77 VLC_UNUSED( p_keystore
);
78 p_crypt
->pf_decrypt
= Decrypt
;
79 p_crypt
->pf_encrypt
= Encrypt
;