2018-07-18 11:20:12 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Henri Verbeet for CodeWeavers
|
|
|
|
* Copyright 2018 Hans Leidekker for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library 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 library 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 library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
#if 0
|
|
|
|
#pragma makedep unix
|
|
|
|
#endif
|
|
|
|
|
2018-07-18 11:20:12 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
|
|
|
|
#include <AvailabilityMacros.h>
|
|
|
|
#include <CommonCrypto/CommonCryptor.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "ntsecapi.h"
|
|
|
|
#include "bcrypt.h"
|
|
|
|
|
|
|
|
#include "bcrypt_internal.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
|
2018-07-20 14:51:14 +02:00
|
|
|
#if defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1080 && !defined(HAVE_GNUTLS_CIPHER_INIT)
|
2018-07-18 11:20:12 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(bcrypt);
|
|
|
|
|
2020-09-29 15:27:32 +02:00
|
|
|
struct key_data
|
|
|
|
{
|
|
|
|
CCCryptorRef ref_encrypt;
|
|
|
|
CCCryptorRef ref_decrypt;
|
|
|
|
};
|
|
|
|
C_ASSERT( sizeof(struct key_data) <= sizeof(((struct key *)0)->private) );
|
|
|
|
|
|
|
|
static struct key_data *key_data( struct key *key )
|
|
|
|
{
|
|
|
|
return (struct key_data *)key->private;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_set_property( struct key *key, const WCHAR *prop, UCHAR *value, ULONG size, ULONG flags )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
if (!strcmpW( prop, BCRYPT_CHAINING_MODE ))
|
|
|
|
{
|
2019-07-05 10:38:01 +02:00
|
|
|
if (!strcmpW( (WCHAR *)value, BCRYPT_CHAIN_MODE_ECB ))
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
key->u.s.mode = MODE_ID_ECB;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2019-07-05 10:38:01 +02:00
|
|
|
else if (!strcmpW( (WCHAR *)value, BCRYPT_CHAIN_MODE_CBC ))
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
key->u.s.mode = MODE_ID_CBC;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-05 10:38:01 +02:00
|
|
|
FIXME( "unsupported mode %s\n", debugstr_w((WCHAR *)value) );
|
2018-07-18 11:20:12 +02:00
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FIXME( "unsupported key property %s\n", debugstr_w(prop) );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_symmetric_init( struct key *key )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
2020-09-29 14:08:14 +02:00
|
|
|
switch (key->alg_id)
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
case ALG_ID_AES:
|
2020-09-29 14:08:14 +02:00
|
|
|
switch (key->u.s.mode)
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
case MODE_ID_ECB:
|
|
|
|
case MODE_ID_CBC:
|
|
|
|
break;
|
|
|
|
default:
|
2020-09-29 14:08:14 +02:00
|
|
|
FIXME( "mode %u not supported\n", key->u.s.mode );
|
2018-07-18 11:20:12 +02:00
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
}
|
2020-09-29 14:08:14 +02:00
|
|
|
return STATUS_SUCCESS;
|
2018-07-18 11:20:12 +02:00
|
|
|
|
|
|
|
default:
|
2020-09-29 14:08:14 +02:00
|
|
|
FIXME( "algorithm %u not supported\n", key->alg_id );
|
2018-07-18 11:20:12 +02:00
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCMode get_cryptor_mode( struct key *key )
|
|
|
|
{
|
|
|
|
switch (key->u.s.mode)
|
|
|
|
{
|
|
|
|
case MODE_ID_ECB: return kCCModeECB;
|
|
|
|
case MODE_ID_CBC: return kCCModeCBC;
|
|
|
|
default:
|
|
|
|
FIXME( "unsupported mode %u\n", key->u.s.mode );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static void CDECL key_symmetric_vector_reset( struct key *key )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
2020-09-29 15:27:32 +02:00
|
|
|
if (!key_data(key)->ref_encrypt) return;
|
2019-11-06 10:16:54 +01:00
|
|
|
|
2020-09-29 13:53:02 +02:00
|
|
|
TRACE( "invalidating cryptor handles\n" );
|
2020-09-29 15:27:32 +02:00
|
|
|
CCCryptorRelease( key_data(key)->ref_encrypt );
|
|
|
|
key_data(key)->ref_encrypt = NULL;
|
2018-07-18 11:20:12 +02:00
|
|
|
|
2020-09-29 15:27:32 +02:00
|
|
|
CCCryptorRelease( key_data(key)->ref_decrypt );
|
|
|
|
key_data(key)->ref_decrypt = NULL;
|
2019-11-06 10:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS init_cryptor_handles( struct key *key )
|
|
|
|
{
|
|
|
|
CCCryptorStatus status;
|
|
|
|
CCMode mode;
|
|
|
|
|
2020-09-29 15:27:32 +02:00
|
|
|
if (key_data(key)->ref_encrypt) return STATUS_SUCCESS;
|
2019-11-06 10:16:54 +01:00
|
|
|
if (!(mode = get_cryptor_mode( key ))) return STATUS_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
if ((status = CCCryptorCreateWithMode( kCCEncrypt, mode, kCCAlgorithmAES128, ccNoPadding, key->u.s.vector,
|
|
|
|
key->u.s.secret, key->u.s.secret_len, NULL, 0, 0, 0,
|
2020-09-29 15:27:32 +02:00
|
|
|
&key_data(key)->ref_encrypt )) != kCCSuccess)
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
WARN( "CCCryptorCreateWithMode failed %d\n", status );
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2019-11-06 10:16:54 +01:00
|
|
|
if ((status = CCCryptorCreateWithMode( kCCDecrypt, mode, kCCAlgorithmAES128, ccNoPadding, key->u.s.vector,
|
|
|
|
key->u.s.secret, key->u.s.secret_len, NULL, 0, 0, 0,
|
2020-09-29 15:27:32 +02:00
|
|
|
&key_data(key)->ref_decrypt )) != kCCSuccess)
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
WARN( "CCCryptorCreateWithMode failed %d\n", status );
|
2020-09-29 15:27:32 +02:00
|
|
|
CCCryptorRelease( key_data(key)->ref_encrypt );
|
|
|
|
key_data(key)->ref_encrypt = NULL;
|
2018-07-18 11:20:12 +02:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_symmetric_set_auth_data( struct key *key, UCHAR *auth_data, ULONG len )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_symmetric_encrypt( struct key *key, const UCHAR *input, ULONG input_len, UCHAR *output, ULONG output_len )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
CCCryptorStatus status;
|
2019-11-06 10:16:54 +01:00
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
if ((ret = init_cryptor_handles( key ))) return ret;
|
|
|
|
|
2020-09-29 15:27:32 +02:00
|
|
|
if ((status = CCCryptorUpdate( key_data(key)->ref_encrypt, input, input_len, output, output_len, NULL )) != kCCSuccess)
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
WARN( "CCCryptorUpdate failed %d\n", status );
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_symmetric_decrypt( struct key *key, const UCHAR *input, ULONG input_len, UCHAR *output, ULONG output_len )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
CCCryptorStatus status;
|
2019-11-06 10:16:54 +01:00
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
if ((ret = init_cryptor_handles( key ))) return ret;
|
|
|
|
|
2020-09-29 15:27:32 +02:00
|
|
|
if ((status = CCCryptorUpdate( key_data(key)->ref_decrypt, input, input_len, output, output_len, NULL )) != kCCSuccess)
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
WARN( "CCCryptorUpdate failed %d\n", status );
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_symmetric_get_tag( struct key *key, UCHAR *tag, ULONG len )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static void CDECL key_symmetric_destroy( struct key *key )
|
2020-09-29 14:16:17 +02:00
|
|
|
{
|
2020-09-29 15:27:32 +02:00
|
|
|
if (key_data(key)->ref_encrypt) CCCryptorRelease( key_data(key)->ref_encrypt );
|
|
|
|
if (key_data(key)->ref_decrypt) CCCryptorRelease( key_data(key)->ref_decrypt );
|
2020-09-29 14:16:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_asymmetric_init( struct key *key )
|
2019-09-23 15:48:52 +02:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_asymmetric_sign( struct key *key, void *padding, UCHAR *input, ULONG input_len, UCHAR *output,
|
|
|
|
ULONG output_len, ULONG *ret_len, ULONG flags )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_asymmetric_verify( struct key *key, void *padding, UCHAR *hash, ULONG hash_len,
|
|
|
|
UCHAR *signature, ULONG signature_len, DWORD flags )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_export_dsa_capi( struct key *key, UCHAR *buf, ULONG len, ULONG *ret_len )
|
2020-06-05 14:59:03 +02:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_export_ecc( struct key *key, UCHAR *output, ULONG len, ULONG *ret_len )
|
2019-02-13 10:21:17 +01:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_import_dsa_capi( struct key *key, UCHAR *buf, ULONG len )
|
2020-06-05 14:59:03 +02:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_import_ecc( struct key *key, UCHAR *input, ULONG len )
|
2019-02-13 10:21:17 +01:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-12-03 07:02:35 +01:00
|
|
|
static NTSTATUS CDECL key_import_rsa( struct key *key, UCHAR *input, ULONG len )
|
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static NTSTATUS CDECL key_asymmetric_generate( struct key *key )
|
2019-02-13 10:21:15 +01:00
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static void CDECL key_asymmetric_destroy( struct key *key )
|
2018-07-18 11:20:12 +02:00
|
|
|
{
|
|
|
|
}
|
2020-09-29 15:36:52 +02:00
|
|
|
|
2020-10-14 11:03:53 +02:00
|
|
|
static NTSTATUS CDECL key_asymmetric_duplicate( struct key *key_orig, struct key *key_copy )
|
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-12-04 14:40:06 +01:00
|
|
|
static NTSTATUS CDECL key_asymmetric_decrypt( struct key *key, UCHAR *input, ULONG input_len,
|
|
|
|
UCHAR *output, ULONG *output_len )
|
|
|
|
{
|
|
|
|
FIXME( "not implemented on Mac\n" );
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:36:52 +02:00
|
|
|
static const struct key_funcs key_funcs =
|
|
|
|
{
|
|
|
|
key_set_property,
|
|
|
|
key_symmetric_init,
|
|
|
|
key_symmetric_vector_reset,
|
|
|
|
key_symmetric_set_auth_data,
|
|
|
|
key_symmetric_encrypt,
|
|
|
|
key_symmetric_decrypt,
|
|
|
|
key_symmetric_get_tag,
|
|
|
|
key_symmetric_destroy,
|
|
|
|
key_asymmetric_init,
|
|
|
|
key_asymmetric_generate,
|
2020-12-04 14:40:06 +01:00
|
|
|
key_asymmetric_decrypt,
|
2020-10-14 11:03:53 +02:00
|
|
|
key_asymmetric_duplicate,
|
2020-09-29 15:36:52 +02:00
|
|
|
key_asymmetric_sign,
|
|
|
|
key_asymmetric_verify,
|
|
|
|
key_asymmetric_destroy,
|
|
|
|
key_export_dsa_capi,
|
|
|
|
key_export_ecc,
|
|
|
|
key_import_dsa_capi,
|
2020-12-03 07:02:35 +01:00
|
|
|
key_import_ecc,
|
|
|
|
key_import_rsa
|
2020-09-29 15:36:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
NTSTATUS CDECL __wine_init_unix_lib( HMODULE module, DWORD reason, const void *ptr_in, void *ptr_out )
|
|
|
|
{
|
|
|
|
if (reason != DLL_PROCESS_ATTACH) return STATUS_SUCCESS;
|
|
|
|
*(const struct key_funcs **)ptr_out = &key_funcs;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:20:12 +02:00
|
|
|
#endif
|