file_crypt_win32.c 2.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*****************************************************************************
 * 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>

31 32
typedef BOOL (WINAPI *ProcessFunc)(DATA_BLOB*, LPCWSTR, DATA_BLOB*, PVOID,
                                   CRYPTPROTECT_PROMPTSTRUCT*, DWORD, DATA_BLOB*);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

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;
}