wined3d: Pass a wined3d_device_context to wined3d_cs_emit_set_light_enable().
[wine.git] / dlls / dssenh / main.c
blob6cfa04393fd1011f9c4b65ad6d2bf740742c3f7f
1 /*
2 * Copyright 2008 Maarten Lankhorst
3 * Copyright 2020 Hans Leidekker for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #include "ntstatus.h"
23 #define WIN32_NO_STATUS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wincrypt.h"
27 #include "winreg.h"
28 #include "bcrypt.h"
29 #include "objbase.h"
30 #include "rpcproxy.h"
31 #include "ntsecapi.h"
33 #include "wine/debug.h"
34 #include "wine/heap.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dssenh);
38 #define MAGIC_KEY (('K' << 24) | ('E' << 16) | ('Y' << 8) | '0')
39 struct key
41 DWORD magic;
42 DWORD algid;
43 DWORD flags;
44 BCRYPT_ALG_HANDLE alg_handle;
45 BCRYPT_KEY_HANDLE handle;
48 #define MAGIC_CONTAINER (('C' << 24) | ('O' << 16) | ('N' << 8) | 'T')
49 struct container
51 DWORD magic;
52 DWORD flags;
53 struct key *exch_key;
54 struct key *sign_key;
55 char name[MAX_PATH];
58 #define MAGIC_HASH (('H' << 24) | ('A' << 16) | ('S' << 8) | 'H')
59 struct hash
61 DWORD magic;
62 BCRYPT_HASH_HANDLE handle;
63 DWORD len;
64 UCHAR value[64];
65 BOOL finished;
68 static const char dss_path_fmt[] = "Software\\Wine\\Crypto\\DSS\\%s";
70 static BOOL create_container_regkey( struct container *container, REGSAM sam, HKEY *hkey )
72 char path[sizeof(dss_path_fmt) + MAX_PATH];
73 HKEY rootkey;
75 sprintf( path, dss_path_fmt, container->name );
77 if (container->flags & CRYPT_MACHINE_KEYSET)
78 rootkey = HKEY_LOCAL_MACHINE;
79 else
80 rootkey = HKEY_CURRENT_USER;
82 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\DSS */
83 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\DSS */
84 return !RegCreateKeyExA( rootkey, path, 0, NULL, REG_OPTION_NON_VOLATILE, sam, NULL, hkey, NULL );
87 static struct container *create_key_container( const char *name, DWORD flags )
89 struct container *ret;
91 if (!(ret = heap_alloc_zero( sizeof(*ret) ))) return NULL;
92 ret->magic = MAGIC_CONTAINER;
93 ret->flags = flags;
94 if (name) strcpy( ret->name, name );
96 if (!(flags & CRYPT_VERIFYCONTEXT))
98 HKEY hkey;
99 if (create_container_regkey( ret, KEY_WRITE, &hkey )) RegCloseKey( hkey );
101 return ret;
104 static BOOL open_container_regkey( const char *name, DWORD flags, REGSAM access, HKEY *hkey )
106 char path[sizeof(dss_path_fmt) + MAX_PATH];
107 HKEY rootkey;
109 sprintf( path, dss_path_fmt, name );
111 if (flags & CRYPT_MACHINE_KEYSET)
112 rootkey = HKEY_LOCAL_MACHINE;
113 else
114 rootkey = HKEY_CURRENT_USER;
116 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\DSS */
117 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\DSS */
118 return !RegOpenKeyExA( rootkey, path, 0, access, hkey );
121 static const WCHAR *map_keyspec_to_keypair_name( DWORD keyspec )
123 const WCHAR *name;
125 switch (keyspec)
127 case AT_KEYEXCHANGE:
128 name = L"KeyExchangeKeyPair";
129 break;
130 case AT_SIGNATURE:
131 name = L"SignatureKeyPair";
132 break;
133 default:
134 ERR( "invalid key spec %u\n", keyspec );
135 return NULL;
137 return name;
140 static struct key *create_key( ALG_ID algid, DWORD flags )
142 struct key *ret;
143 const WCHAR *alg;
145 switch (algid)
147 case AT_SIGNATURE:
148 case CALG_DSS_SIGN:
149 alg = BCRYPT_DSA_ALGORITHM;
150 break;
152 default:
153 FIXME( "unhandled algorithm %08x\n", algid );
154 return NULL;
157 if (!(ret = heap_alloc_zero( sizeof(*ret) ))) return NULL;
159 ret->magic = MAGIC_KEY;
160 ret->algid = algid;
161 ret->flags = flags;
162 if (BCryptOpenAlgorithmProvider( &ret->alg_handle, alg, MS_PRIMITIVE_PROVIDER, 0 ))
164 heap_free( ret );
165 return NULL;
167 return ret;
170 static void destroy_key( struct key *key )
172 if (!key) return;
173 BCryptDestroyKey( key->handle );
174 BCryptCloseAlgorithmProvider( key->alg_handle, 0 );
175 key->magic = 0;
176 heap_free( key );
179 static struct key *import_key( DWORD keyspec, BYTE *data, DWORD len )
181 struct key *ret;
183 if (!(ret = create_key( keyspec, 0 ))) return NULL;
185 if (BCryptImportKeyPair( ret->alg_handle, NULL, LEGACY_DSA_V2_PRIVATE_BLOB, &ret->handle, data, len, 0 ))
187 WARN( "failed to import key\n" );
188 destroy_key( ret );
189 return NULL;
191 return ret;
194 static struct key *read_key( HKEY hkey, DWORD keyspec, DWORD flags )
196 const WCHAR *value;
197 DWORD type, len;
198 BYTE *data;
199 DATA_BLOB blob_in, blob_out;
200 struct key *ret = NULL;
202 if (!(value = map_keyspec_to_keypair_name( keyspec ))) return NULL;
203 if (RegQueryValueExW( hkey, value, 0, &type, NULL, &len )) return NULL;
204 if (!(data = heap_alloc( len ))) return NULL;
206 if (!RegQueryValueExW( hkey, value, 0, &type, data, &len ))
208 blob_in.pbData = data;
209 blob_in.cbData = len;
210 if (CryptUnprotectData( &blob_in, NULL, NULL, NULL, NULL, flags, &blob_out ))
212 ret = import_key( keyspec, blob_out.pbData, blob_out.cbData );
213 LocalFree( blob_out.pbData );
217 heap_free( data );
218 return ret;
221 static void destroy_container( struct container *container )
223 if (!container) return;
224 destroy_key( container->exch_key );
225 destroy_key( container->sign_key );
226 container->magic = 0;
227 heap_free( container );
230 static struct container *read_key_container( const char *name, DWORD flags )
232 DWORD protect_flags = (flags & CRYPT_MACHINE_KEYSET) ? CRYPTPROTECT_LOCAL_MACHINE : 0;
233 struct container *ret;
234 HKEY hkey;
236 if (!open_container_regkey( name, flags, KEY_READ, &hkey )) return NULL;
238 if ((ret = create_key_container( name, flags )))
240 ret->exch_key = read_key( hkey, AT_KEYEXCHANGE, protect_flags );
241 ret->sign_key = read_key( hkey, AT_SIGNATURE, protect_flags );
244 RegCloseKey( hkey );
245 return ret;
248 static void delete_key_container( const char *name, DWORD flags )
250 char path[sizeof(dss_path_fmt) + MAX_PATH];
251 HKEY rootkey;
253 sprintf( path, dss_path_fmt, name );
255 if (flags & CRYPT_MACHINE_KEYSET)
256 rootkey = HKEY_LOCAL_MACHINE;
257 else
258 rootkey = HKEY_CURRENT_USER;
260 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\DSS */
261 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\DSS */
262 RegDeleteKeyExA( rootkey, path, 0, 0 );
265 BOOL WINAPI CPAcquireContext( HCRYPTPROV *ret_prov, LPSTR container, DWORD flags, PVTableProvStruc vtable )
267 struct container *ret;
268 char name[MAX_PATH];
270 TRACE( "%p, %s, %08x, %p\n", ret_prov, debugstr_a(container), flags, vtable );
272 if (container && *container)
274 if (lstrlenA( container ) >= sizeof(name)) return FALSE;
275 lstrcpyA( name, container );
277 else
279 DWORD len = sizeof(name);
280 if (!GetUserNameA( name, &len )) return FALSE;
283 switch (flags)
285 case 0:
286 case 0 | CRYPT_MACHINE_KEYSET:
287 ret = read_key_container( name, flags );
288 break;
290 case CRYPT_NEWKEYSET:
291 case CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET:
292 if ((ret = read_key_container( name, flags )))
294 heap_free( ret );
295 SetLastError( NTE_EXISTS );
296 return FALSE;
298 ret = create_key_container( name, flags );
299 break;
301 case CRYPT_VERIFYCONTEXT:
302 case CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET:
303 ret = create_key_container( "", flags );
304 break;
306 case CRYPT_DELETEKEYSET:
307 case CRYPT_DELETEKEYSET | CRYPT_MACHINE_KEYSET:
308 delete_key_container( name, flags );
309 *ret_prov = 0;
310 return TRUE;
312 default:
313 FIXME( "unsupported flags %08x\n", flags );
314 return FALSE;
317 if (!ret) return FALSE;
318 *ret_prov = (HCRYPTPROV)ret;
319 return TRUE;
322 BOOL WINAPI CPReleaseContext( HCRYPTPROV hprov, DWORD flags )
324 struct container *container = (struct container *)hprov;
326 TRACE( "%p, %08x\n", (void *)hprov, flags );
328 if (container->magic != MAGIC_CONTAINER) return FALSE;
329 destroy_container( container );
330 return TRUE;
333 BOOL WINAPI CPGetProvParam( HCRYPTPROV hprov, DWORD param, BYTE *data, DWORD *len, DWORD flags )
335 return FALSE;
338 static BOOL store_key_pair( struct key *key, HKEY hkey, DWORD keyspec, DWORD flags )
340 const WCHAR *value;
341 DATA_BLOB blob_in, blob_out;
342 DWORD len;
343 BYTE *data;
344 BOOL ret = TRUE;
346 if (!key) return TRUE;
347 if (!(value = map_keyspec_to_keypair_name( keyspec ))) return FALSE;
349 if (BCryptExportKey( key->handle, NULL, LEGACY_DSA_V2_PRIVATE_BLOB, NULL, 0, &len, 0 )) return FALSE;
350 if (!(data = heap_alloc( len ))) return FALSE;
352 if (!BCryptExportKey( key->handle, NULL, LEGACY_DSA_V2_PRIVATE_BLOB, data, len, &len, 0 ))
354 blob_in.pbData = data;
355 blob_in.cbData = len;
356 if ((ret = CryptProtectData( &blob_in, NULL, NULL, NULL, NULL, flags, &blob_out )))
358 ret = !RegSetValueExW( hkey, value, 0, REG_BINARY, blob_out.pbData, blob_out.cbData );
359 LocalFree( blob_out.pbData );
363 heap_free( data );
364 return ret;
367 static BOOL store_key_container_keys( struct container *container )
369 HKEY hkey;
370 DWORD flags;
371 BOOL ret;
373 if (container->flags & CRYPT_MACHINE_KEYSET)
374 flags = CRYPTPROTECT_LOCAL_MACHINE;
375 else
376 flags = 0;
378 if (!create_container_regkey( container, KEY_WRITE, &hkey )) return FALSE;
380 ret = store_key_pair( container->exch_key, hkey, AT_KEYEXCHANGE, flags );
381 if (ret) store_key_pair( container->sign_key, hkey, AT_SIGNATURE, flags );
382 RegCloseKey( hkey );
383 return ret;
386 static struct key *duplicate_key( const struct key *key )
388 struct key *ret;
390 if (!(ret = create_key( key->algid, key->flags ))) return NULL;
392 if (BCryptDuplicateKey( key->handle, &ret->handle, NULL, 0, 0 ))
394 heap_free( ret );
395 return NULL;
397 return ret;
400 static BOOL generate_key( struct container *container, ALG_ID algid, DWORD bitlen, DWORD flags, HCRYPTKEY *ret_key )
402 struct key *key, *sign_key;
403 NTSTATUS status;
405 if (!(key = create_key( algid, flags ))) return FALSE;
407 if ((status = BCryptGenerateKeyPair( key->alg_handle, &key->handle, bitlen, 0 )))
409 ERR( "failed to generate key %08x\n", status );
410 destroy_key( key );
411 return FALSE;
413 if ((status = BCryptFinalizeKeyPair( key->handle, 0 )))
415 ERR( "failed to finalize key %08x\n", status );
416 destroy_key( key );
417 return FALSE;
420 switch (algid)
422 case AT_SIGNATURE:
423 case CALG_DSS_SIGN:
424 if (!(sign_key = duplicate_key( key )))
426 destroy_key( key );
427 return FALSE;
429 destroy_key( container->sign_key );
430 container->sign_key = sign_key;
431 break;
433 default:
434 FIXME( "unhandled algorithm %08x\n", algid );
435 return FALSE;
438 if (!store_key_container_keys( container )) return FALSE;
440 *ret_key = (HCRYPTKEY)key;
441 return TRUE;
444 BOOL WINAPI CPGenKey( HCRYPTPROV hprov, ALG_ID algid, DWORD flags, HCRYPTKEY *ret_key )
446 static const unsigned int supported_key_lengths[] = { 512, 768, 1024 };
447 struct container *container = (struct container *)hprov;
448 ULONG i, bitlen = HIWORD(flags) ? HIWORD(flags) : 1024;
450 TRACE( "%p, %08x, %08x, %p\n", (void *)hprov, algid, flags, ret_key );
452 if (container->magic != MAGIC_CONTAINER) return FALSE;
454 if (bitlen % 2)
456 SetLastError( STATUS_INVALID_PARAMETER );
457 return FALSE;
459 for (i = 0; i < ARRAY_SIZE(supported_key_lengths); i++)
461 if (bitlen == supported_key_lengths[i]) break;
463 if (i >= ARRAY_SIZE(supported_key_lengths))
465 SetLastError( NTE_BAD_FLAGS );
466 return FALSE;
469 return generate_key( container, algid, bitlen, LOWORD(flags), ret_key );
472 BOOL WINAPI CPDestroyKey( HCRYPTPROV hprov, HCRYPTKEY hkey )
474 struct key *key = (struct key *)hkey;
476 TRACE( "%p, %p\n", (void *)hprov, (void *)hkey );
478 if (key->magic != MAGIC_KEY)
480 SetLastError( NTE_BAD_KEY );
481 return FALSE;
484 destroy_key( key );
485 return TRUE;
488 #define MAGIC_DSS1 ('D' | ('S' << 8) | ('S' << 16) | ('1' << 24))
489 #define MAGIC_DSS2 ('D' | ('S' << 8) | ('S' << 16) | ('2' << 24))
490 #define MAGIC_DSS3 ('D' | ('S' << 8) | ('S' << 16) | ('3' << 24))
492 static BOOL import_key_dss2( struct container *container, ALG_ID algid, const BYTE *data, DWORD len, DWORD flags,
493 HCRYPTKEY *ret_key )
495 const BLOBHEADER *hdr = (const BLOBHEADER *)data;
496 const DSSPUBKEY *pubkey = (const DSSPUBKEY *)(hdr + 1);
497 const WCHAR *type;
498 struct key *key, *exch_key, *sign_key;
499 NTSTATUS status;
501 if (len < sizeof(*hdr) + sizeof(*pubkey)) return FALSE;
503 switch (pubkey->magic)
505 case MAGIC_DSS1:
506 type = LEGACY_DSA_V2_PUBLIC_BLOB;
507 break;
509 case MAGIC_DSS2:
510 type = LEGACY_DSA_V2_PRIVATE_BLOB;
511 break;
513 default:
514 FIXME( "unsupported key magic %08x\n", pubkey->magic );
515 return FALSE;
518 if (!(key = create_key( CALG_DSS_SIGN, flags ))) return FALSE;
520 if ((status = BCryptImportKeyPair( key->alg_handle, NULL, type, &key->handle, (UCHAR *)data, len, 0 )))
522 TRACE( "failed to import key %08x\n", status );
523 destroy_key( key );
524 return FALSE;
527 if (!wcscmp(type, LEGACY_DSA_V2_PRIVATE_BLOB))
529 switch (algid)
531 case AT_KEYEXCHANGE:
532 case CALG_DH_SF:
533 if (!(exch_key = duplicate_key( key )))
535 destroy_key( key );
536 return FALSE;
538 destroy_key( container->exch_key );
539 container->exch_key = exch_key;
540 break;
542 case AT_SIGNATURE:
543 case CALG_DSS_SIGN:
544 if (!(sign_key = duplicate_key( key )))
546 destroy_key( key );
547 return FALSE;
549 destroy_key( container->sign_key );
550 container->sign_key = sign_key;
551 break;
553 default:
554 FIXME( "unhandled key algorithm %u\n", algid );
555 destroy_key( key );
556 return FALSE;
559 if (!store_key_container_keys( container )) return FALSE;
562 *ret_key = (HCRYPTKEY)key;
563 return TRUE;
566 static BOOL import_key_dss3( struct container *container, ALG_ID algid, const BYTE *data, DWORD len, DWORD flags,
567 HCRYPTKEY *ret_key )
569 const BLOBHEADER *hdr = (const BLOBHEADER *)data;
570 const DSSPUBKEY_VER3 *pubkey = (const DSSPUBKEY_VER3 *)(hdr + 1);
571 BCRYPT_DSA_KEY_BLOB *blob;
572 struct key *key;
573 BYTE *src, *dst;
574 ULONG i, size, size_q;
575 NTSTATUS status;
577 if (len < sizeof(*hdr) + sizeof(*pubkey)) return FALSE;
579 switch (pubkey->magic)
581 case MAGIC_DSS3:
582 break;
584 default:
585 FIXME( "unsupported key magic %08x\n", pubkey->magic );
586 return FALSE;
589 if ((size_q = pubkey->bitlenQ / 8) > sizeof(blob->q))
591 FIXME( "q too large\n" );
592 return FALSE;
595 if (!(key = create_key( CALG_DSS_SIGN, flags ))) return FALSE;
597 size = sizeof(*blob) + (pubkey->bitlenP / 8) * 3;
598 if (!(blob = heap_alloc_zero( size )))
600 destroy_key( key );
601 return FALSE;
603 blob->dwMagic = BCRYPT_DSA_PUBLIC_MAGIC;
604 blob->cbKey = pubkey->bitlenP / 8;
605 memcpy( blob->Count, &pubkey->DSSSeed.counter, sizeof(blob->Count) );
606 memcpy( blob->Seed, pubkey->DSSSeed.seed, sizeof(blob->Seed) );
608 /* q */
609 src = (BYTE *)(pubkey + 1) + blob->cbKey;
610 for (i = 0; i < size_q; i++) blob->q[i] = src[size_q - i - 1];
612 /* p */
613 src -= blob->cbKey;
614 dst = (BYTE *)(blob + 1);
615 for (i = 0; i < blob->cbKey; i++) dst[i] = src[blob->cbKey - i - 1];
617 /* g */
618 src += blob->cbKey + size_q;
619 dst += blob->cbKey;
620 for (i = 0; i < blob->cbKey; i++) dst[i] = src[blob->cbKey - i - 1];
622 /* y */
623 src += blob->cbKey + pubkey->bitlenJ / 8;
624 dst += blob->cbKey;
625 for (i = 0; i < blob->cbKey; i++) dst[i] = src[blob->cbKey - i - 1];
627 if ((status = BCryptImportKeyPair( key->alg_handle, NULL, BCRYPT_DSA_PUBLIC_BLOB, &key->handle, (UCHAR *)blob,
628 size, 0 )))
630 WARN( "failed to import key %08x\n", status );
631 destroy_key( key );
632 heap_free( blob );
633 return FALSE;
636 heap_free( blob );
637 *ret_key = (HCRYPTKEY)key;
638 return TRUE;
641 BOOL WINAPI CPImportKey( HCRYPTPROV hprov, const BYTE *data, DWORD len, HCRYPTKEY hpubkey, DWORD flags,
642 HCRYPTKEY *ret_key )
644 struct container *container = (struct container *)hprov;
645 const BLOBHEADER *hdr;
646 BOOL ret;
648 TRACE( "%p, %p, %u, %p, %08x, %p\n", (void *)hprov, data, len, (void *)hpubkey, flags, ret_key );
650 if (container->magic != MAGIC_CONTAINER) return FALSE;
651 if (len < sizeof(*hdr)) return FALSE;
653 hdr = (const BLOBHEADER *)data;
654 if ((hdr->bType != PRIVATEKEYBLOB && hdr->bType != PUBLICKEYBLOB) || hdr->aiKeyAlg != CALG_DSS_SIGN)
656 FIXME( "bType %u aiKeyAlg %08x not supported\n", hdr->bType, hdr->aiKeyAlg );
657 return FALSE;
660 switch (hdr->bVersion)
662 case 2:
663 ret = import_key_dss2( container, hdr->aiKeyAlg, data, len, flags, ret_key );
664 break;
666 case 3:
667 ret = import_key_dss3( container, hdr->aiKeyAlg, data, len, flags, ret_key );
668 break;
670 default:
671 FIXME( "version %u not supported\n", hdr->bVersion );
672 return FALSE;
675 return ret;
678 BOOL WINAPI CPExportKey( HCRYPTPROV hprov, HCRYPTKEY hkey, HCRYPTKEY hexpkey, DWORD blobtype, DWORD flags,
679 BYTE *data, DWORD *len )
681 struct key *key = (struct key *)hkey;
682 const WCHAR *type;
684 TRACE( "%p, %p, %p, %08x, %08x, %p, %p\n", (void *)hprov, (void *)hkey, (void *)hexpkey, blobtype, flags,
685 data, len );
687 if (key->magic != MAGIC_KEY) return FALSE;
688 if (hexpkey)
690 FIXME( "export key not supported\n" );
691 return FALSE;
693 if (flags)
695 FIXME( "flags %08x not supported\n", flags );
696 return FALSE;
699 switch (blobtype)
701 case PUBLICKEYBLOB:
702 type = LEGACY_DSA_V2_PUBLIC_BLOB;
703 break;
705 case PRIVATEKEYBLOB:
706 type = LEGACY_DSA_V2_PRIVATE_BLOB;
707 break;
709 default:
710 FIXME( "blob type %u not supported\n", blobtype );
711 return FALSE;
714 return !BCryptExportKey( key->handle, NULL, type, data, *len, len, 0 );
717 BOOL WINAPI CPDuplicateKey( HCRYPTPROV hprov, HCRYPTKEY hkey, DWORD *reserved, DWORD flags, HCRYPTKEY *ret_key )
719 struct key *key = (struct key *)hkey, *ret;
721 TRACE( "%p, %p, %p, %08x, %p\n", (void *)hprov, (void *)hkey, reserved, flags, ret_key );
723 if (key->magic != MAGIC_KEY) return FALSE;
725 if (!(ret = duplicate_key( key ))) return FALSE;
726 *ret_key = (HCRYPTKEY)ret;
727 return TRUE;
730 BOOL WINAPI CPGetUserKey( HCRYPTPROV hprov, DWORD keyspec, HCRYPTKEY *ret_key )
732 struct container *container = (struct container *)hprov;
733 BOOL ret = FALSE;
735 TRACE( "%p, %08x, %p\n", (void *)hprov, keyspec, ret_key );
737 if (container->magic != MAGIC_CONTAINER) return FALSE;
739 switch (keyspec)
741 case AT_KEYEXCHANGE:
742 if (!container->exch_key) SetLastError( NTE_NO_KEY );
743 else if ((*ret_key = (HCRYPTKEY)duplicate_key( container->exch_key ))) ret = TRUE;
744 break;
746 case AT_SIGNATURE:
747 if (!container->sign_key) SetLastError( NTE_NO_KEY );
748 else if ((*ret_key = (HCRYPTKEY)duplicate_key( container->sign_key ))) ret = TRUE;
749 break;
751 default:
752 SetLastError( NTE_NO_KEY );
753 return FALSE;
756 return ret;
759 BOOL WINAPI CPGenRandom( HCRYPTPROV hprov, DWORD len, BYTE *buffer )
761 struct container *container = (struct container *)hprov;
763 TRACE( "%p, %u, %p\n", (void *)hprov, len, buffer );
765 if (container->magic != MAGIC_CONTAINER) return FALSE;
767 return RtlGenRandom( buffer, len );
770 static struct hash *create_hash( ALG_ID algid )
772 struct hash *ret;
773 BCRYPT_ALG_HANDLE alg_handle;
774 const WCHAR *alg;
775 DWORD len;
777 switch (algid)
779 case CALG_MD5:
780 alg = BCRYPT_MD5_ALGORITHM;
781 len = 16;
782 break;
784 case CALG_SHA1:
785 alg = BCRYPT_SHA1_ALGORITHM;
786 len = 20;
787 break;
789 default:
790 FIXME( "unhandled algorithm %u\n", algid );
791 return NULL;
794 if (!(ret = heap_alloc_zero( sizeof(*ret) ))) return NULL;
796 ret->magic = MAGIC_HASH;
797 ret->len = len;
798 if (BCryptOpenAlgorithmProvider( &alg_handle, alg, MS_PRIMITIVE_PROVIDER, 0 ))
800 heap_free( ret );
801 return NULL;
803 if (BCryptCreateHash( alg_handle, &ret->handle, NULL, 0, NULL, 0, 0 ))
805 BCryptCloseAlgorithmProvider( alg_handle, 0 );
806 heap_free( ret );
807 return NULL;
810 BCryptCloseAlgorithmProvider( alg_handle, 0 );
811 return ret;
814 BOOL WINAPI CPCreateHash( HCRYPTPROV hprov, ALG_ID algid, HCRYPTKEY hkey, DWORD flags, HCRYPTHASH *ret_hash )
816 struct hash *hash;
818 TRACE( "%p, %08x, %p, %08x, %p\n", (void *)hprov, algid, (void *)hkey, flags, ret_hash );
820 switch (algid)
822 case CALG_MD5:
823 case CALG_SHA1:
824 break;
826 default:
827 FIXME( "algorithm %u not supported\n", algid );
828 SetLastError( NTE_BAD_ALGID );
829 return FALSE;
832 if (!(hash = create_hash( algid ))) return FALSE;
834 *ret_hash = (HCRYPTHASH)hash;
835 return TRUE;
838 static void destroy_hash( struct hash *hash )
840 if (!hash) return;
841 BCryptDestroyHash( hash->handle );
842 hash->magic = 0;
843 heap_free( hash );
846 BOOL WINAPI CPDestroyHash( HCRYPTPROV hprov, HCRYPTHASH hhash )
848 struct hash *hash = (struct hash *)hhash;
850 TRACE( "%p, %p\n", (void *)hprov, (void *)hhash);
852 if (hash->magic != MAGIC_HASH)
854 SetLastError( NTE_BAD_HASH );
855 return FALSE;
858 destroy_hash( hash );
859 return TRUE;
862 static struct hash *duplicate_hash( const struct hash *hash )
864 struct hash *ret;
866 if (!(ret = heap_alloc( sizeof(*ret) ))) return NULL;
868 ret->magic = hash->magic;
869 ret->len = hash->len;
870 if (BCryptDuplicateHash( hash->handle, &ret->handle, NULL, 0, 0 ))
872 heap_free( ret );
873 return NULL;
875 memcpy( ret->value, hash->value, sizeof(hash->value) );
876 ret->finished = hash->finished;
877 return ret;
880 BOOL WINAPI CPDuplicateHash( HCRYPTPROV hprov, HCRYPTHASH hhash, DWORD *reserved, DWORD flags, HCRYPTHASH *ret_hash )
882 struct hash *hash = (struct hash *)hhash, *ret;
884 TRACE( "%p, %p, %p, %08x, %p\n", (void *)hprov, (void *)hhash, reserved, flags, ret_hash );
886 if (hash->magic != MAGIC_HASH) return FALSE;
888 if (!(ret = duplicate_hash( hash ))) return FALSE;
889 *ret_hash = (HCRYPTHASH)ret;
890 return TRUE;
893 BOOL WINAPI CPHashData( HCRYPTPROV hprov, HCRYPTHASH hhash, const BYTE *data, DWORD len, DWORD flags )
895 struct hash *hash = (struct hash *)hhash;
897 TRACE("%p, %p, %p, %u, %08x\n", (void *)hprov, (void *)hhash, data, len, flags );
899 if (hash->magic != MAGIC_HASH) return FALSE;
901 if (hash->finished)
903 SetLastError( NTE_BAD_HASH_STATE );
904 return FALSE;
906 return !BCryptHashData( hash->handle, (UCHAR *)data, len, 0 );
909 BOOL WINAPI CPGetHashParam( HCRYPTPROV hprov, HCRYPTHASH hhash, DWORD param, BYTE *data, DWORD *len, DWORD flags )
911 struct hash *hash = (struct hash *)hhash;
913 TRACE( "%p, %p, %08x, %p, %p, %08x\n", (void *)hprov, (void *)hhash, param, data, len, flags );
915 if (hash->magic != MAGIC_HASH) return FALSE;
917 switch (param)
919 case HP_HASHSIZE:
920 if (sizeof(hash->len) > *len)
922 *len = sizeof(hash->len);
923 SetLastError( ERROR_MORE_DATA );
924 return FALSE;
926 *(DWORD *)data = hash->len;
927 *len = sizeof(hash->len);
928 return TRUE;
930 case HP_HASHVAL:
931 if (!hash->finished)
933 if (BCryptFinishHash( hash->handle, hash->value, hash->len, 0 )) return FALSE;
934 hash->finished = TRUE;
936 if (hash->len > *len)
938 *len = hash->len;
939 SetLastError( ERROR_MORE_DATA );
940 return FALSE;
942 memcpy( data, hash->value, hash->len );
943 *len = hash->len;
944 return TRUE;
946 default:
947 SetLastError( NTE_BAD_TYPE );
948 return FALSE;
952 BOOL WINAPI CPSetHashParam( HCRYPTPROV hprov, HCRYPTHASH hhash, DWORD param, const BYTE *data, DWORD flags )
954 struct hash *hash = (struct hash *)hhash;
956 TRACE( "%p, %p, %08x, %p, %08x\n", (void *)hprov, (void *)hhash, param, data, flags );
958 if (hash->magic != MAGIC_HASH) return FALSE;
960 switch (param)
962 case HP_HASHVAL:
963 memcpy( hash->value, data, hash->len );
964 return TRUE;
966 default:
967 FIXME( "param %u not supported\n", param );
968 SetLastError( NTE_BAD_TYPE );
969 return FALSE;
973 BOOL WINAPI CPDeriveKey( HCRYPTPROV hprov, ALG_ID algid, HCRYPTHASH hhash, DWORD flags, HCRYPTKEY *ret_key )
975 return FALSE;
978 static DWORD get_signature_length( DWORD algid )
980 switch (algid)
982 case AT_SIGNATURE:
983 case CALG_DSS_SIGN: return 40;
984 default:
985 FIXME( "unhandled algorithm %u\n", algid );
986 return 0;
990 #define MAX_HASH_LEN 20
991 BOOL WINAPI CPSignHash( HCRYPTPROV hprov, HCRYPTHASH hhash, DWORD keyspec, const WCHAR *desc, DWORD flags, BYTE *sig,
992 DWORD *siglen )
994 struct container *container = (struct container *)hprov;
995 struct hash *hash = (struct hash *)hhash;
996 ULONG len;
998 TRACE( "%p, %p, %u, %s, %08x, %p, %p\n", (void *)hprov, (void *)hhash, keyspec, debugstr_w(desc), flags, sig,
999 siglen );
1001 if (container->magic != MAGIC_CONTAINER || !container->sign_key) return FALSE;
1002 if (hash->magic != MAGIC_HASH) return FALSE;
1004 if (!(len = get_signature_length( container->sign_key->algid ))) return FALSE;
1005 if (*siglen < len)
1007 *siglen = len;
1008 return TRUE;
1011 return !BCryptSignHash( container->sign_key->handle, NULL, hash->value, hash->len, sig, *siglen, siglen, 0 );
1014 BOOL WINAPI CPVerifySignature( HCRYPTPROV hprov, HCRYPTHASH hhash, const BYTE *sig, DWORD siglen, HCRYPTKEY hpubkey,
1015 const WCHAR *desc, DWORD flags )
1017 struct hash *hash = (struct hash *)hhash;
1018 struct key *key = (struct key *)hpubkey;
1020 TRACE( "%p, %p, %p, %u %p, %s, %08x\n", (void *)hprov, (void *)hhash, sig, siglen, (void *)hpubkey,
1021 debugstr_w(desc), flags );
1023 if (hash->magic != MAGIC_HASH || key->magic != MAGIC_KEY) return FALSE;
1024 if (flags)
1026 FIXME( "flags %08x not supported\n", flags );
1027 return FALSE;
1030 return !BCryptVerifySignature( key->handle, NULL, hash->value, hash->len, (UCHAR *)sig, siglen, 0 );