kernel32/tests: Add tests for console codepages.
[wine] / dlls / secur32 / wrapper.c
1 /* Copyright (C) 2004 Juan Lang
2  *
3  * Implements secur32 functions that forward to (wrap) an SSP's implementation.
4  *
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.
9  *
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.
14  *
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
18  */
19 #include <stdarg.h>
20 #include "windef.h"
21 #include "winbase.h"
22 #include "winnls.h"
23 #include "sspi.h"
24 #include "secur32_priv.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(secur32);
29
30 /* Tries to allocate a new SecHandle, into which it stores package (in
31  * phSec->dwUpper) and a copy of realHandle (allocated with SECUR32_ALLOC,
32  * and stored in phSec->dwLower).  SecHandle is equivalent to both a
33  * CredHandle and a CtxtHandle.
34  */
35 static SECURITY_STATUS SECUR32_makeSecHandle(PSecHandle phSec,
36  SecurePackage *package, PSecHandle realHandle)
37 {
38     SECURITY_STATUS ret;
39
40     TRACE("%p %p %p\n", phSec, package, realHandle);
41
42     if (phSec && package && realHandle)
43     {
44         PSecHandle newSec = (PSecHandle)SECUR32_ALLOC(sizeof(SecHandle));
45
46         if (newSec)
47         {
48             memcpy(newSec, realHandle, sizeof(*realHandle));
49             phSec->dwUpper = (ULONG_PTR)package;
50             phSec->dwLower = (ULONG_PTR)newSec;
51             ret = SEC_E_OK;
52         }
53         else
54             ret = SEC_E_INSUFFICIENT_MEMORY;
55     }
56     else
57         ret = SEC_E_INVALID_HANDLE;
58     return ret;
59 }
60
61 /***********************************************************************
62  *              AcquireCredentialsHandleA (SECUR32.@)
63  */
64 SECURITY_STATUS WINAPI AcquireCredentialsHandleA(
65  SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialsUse,
66  PLUID pvLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
67  PVOID pvGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
68 {
69     SECURITY_STATUS ret;
70
71     TRACE("%s %s %d %p %p %p %p %p %p\n", debugstr_a(pszPrincipal),
72      debugstr_a(pszPackage), fCredentialsUse, pvLogonID, pAuthData, pGetKeyFn,
73      pvGetKeyArgument, phCredential, ptsExpiry);
74     if (pszPackage)
75     {
76         SecurePackage *package = SECUR32_findPackageA(pszPackage);
77
78         if (package && package->provider)
79         {
80             if (package->provider->fnTableA.AcquireCredentialsHandleA)
81             {
82                 CredHandle myCred;
83
84                 ret = package->provider->fnTableA.AcquireCredentialsHandleA(
85                  pszPrincipal, pszPackage, fCredentialsUse, pvLogonID,
86                  pAuthData, pGetKeyFn, pvGetKeyArgument, &myCred,
87                  ptsExpiry);
88                 if (ret == SEC_E_OK)
89                 {
90                     ret = SECUR32_makeSecHandle(phCredential, package, &myCred);
91                     if (ret != SEC_E_OK)
92                         package->provider->fnTableW.FreeCredentialsHandle(
93                          &myCred);
94                 }
95             }
96             else
97                 ret = SEC_E_UNSUPPORTED_FUNCTION;
98         }
99         else
100             ret = SEC_E_SECPKG_NOT_FOUND;
101     }
102     else
103         ret = SEC_E_SECPKG_NOT_FOUND;
104     return ret;
105 }
106
107 /***********************************************************************
108  *              AcquireCredentialsHandleW (SECUR32.@)
109  */
110 SECURITY_STATUS WINAPI AcquireCredentialsHandleW(
111  SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialsUse,
112  PLUID pvLogonID, PVOID pAuthData, SEC_GET_KEY_FN pGetKeyFn,
113  PVOID pvGetKeyArgument, PCredHandle phCredential, PTimeStamp ptsExpiry)
114 {
115     SECURITY_STATUS ret;
116
117     TRACE("%s %s %d %p %p %p %p %p %p\n", debugstr_w(pszPrincipal),
118      debugstr_w(pszPackage), fCredentialsUse, pvLogonID, pAuthData, pGetKeyFn,
119      pvGetKeyArgument, phCredential, ptsExpiry);
120     if (pszPackage)
121     {
122         SecurePackage *package = SECUR32_findPackageW(pszPackage);
123
124         if (package && package->provider)
125         {
126             if (package->provider->fnTableW.AcquireCredentialsHandleW)
127             {
128                 CredHandle myCred;
129
130                 ret = package->provider->fnTableW.AcquireCredentialsHandleW(
131                  pszPrincipal, pszPackage, fCredentialsUse, pvLogonID,
132                  pAuthData, pGetKeyFn, pvGetKeyArgument, &myCred,
133                  ptsExpiry);
134                 if (ret == SEC_E_OK)
135                 {
136                     ret = SECUR32_makeSecHandle(phCredential, package, &myCred);
137                     if (ret != SEC_E_OK)
138                         package->provider->fnTableW.FreeCredentialsHandle(
139                          &myCred);
140                 }
141             }
142             else
143                 ret = SEC_E_UNSUPPORTED_FUNCTION;
144         }
145         else
146             ret = SEC_E_SECPKG_NOT_FOUND;
147     }
148     else
149         ret = SEC_E_SECPKG_NOT_FOUND;
150     return ret;
151 }
152
153 /***********************************************************************
154  *              FreeCredentialsHandle (SECUR32.@)
155  */
156 SECURITY_STATUS WINAPI FreeCredentialsHandle(
157  PCredHandle phCredential)
158 {
159     SECURITY_STATUS ret;
160
161     TRACE("%p\n", phCredential);
162     if (phCredential)
163     {
164         SecurePackage *package = (SecurePackage *)phCredential->dwUpper;
165         PCredHandle cred = (PCredHandle)phCredential->dwLower;
166
167         if (package && package->provider &&
168          package->provider->fnTableW.FreeCredentialsHandle)
169             ret = package->provider->fnTableW.FreeCredentialsHandle(cred);
170         else
171             ret = SEC_E_INVALID_HANDLE;
172         SECUR32_FREE(cred);
173     }
174     else
175         ret = SEC_E_INVALID_HANDLE;
176     return ret;
177 }
178
179 /***********************************************************************
180  *              QueryCredentialsAttributesA (SECUR32.@)
181  */
182 SECURITY_STATUS WINAPI QueryCredentialsAttributesA(
183  PCredHandle phCredential, ULONG ulAttribute, void *pBuffer)
184 {
185     SECURITY_STATUS ret;
186
187     TRACE("%p %d %p\n", phCredential, ulAttribute, pBuffer);
188     if (phCredential)
189     {
190         SecurePackage *package = (SecurePackage *)phCredential->dwUpper;
191         PCredHandle cred = (PCredHandle)phCredential->dwLower;
192
193         if (package && package->provider)
194         {
195             if (package->provider->fnTableA.QueryCredentialsAttributesA)
196                 ret = package->provider->fnTableA.QueryCredentialsAttributesA(
197                  cred, ulAttribute, pBuffer);
198             else
199                 ret = SEC_E_UNSUPPORTED_FUNCTION;
200         }
201         else
202             ret = SEC_E_INVALID_HANDLE;
203     }
204     else
205         ret = SEC_E_INVALID_HANDLE;
206     return ret;
207 }
208
209 /***********************************************************************
210  *              QueryCredentialsAttributesW (SECUR32.@)
211  */
212 SECURITY_STATUS WINAPI QueryCredentialsAttributesW(
213  PCredHandle phCredential, ULONG ulAttribute, void *pBuffer)
214 {
215     SECURITY_STATUS ret;
216
217     TRACE("%p %d %p\n", phCredential, ulAttribute, pBuffer);
218     if (phCredential)
219     {
220         SecurePackage *package = (SecurePackage *)phCredential->dwUpper;
221         PCredHandle cred = (PCredHandle)phCredential->dwLower;
222
223         if (package && package->provider)
224         {
225             if (package->provider->fnTableW.QueryCredentialsAttributesW)
226                 ret = package->provider->fnTableW.QueryCredentialsAttributesW(
227                  cred, ulAttribute, pBuffer);
228             else
229                 ret = SEC_E_UNSUPPORTED_FUNCTION;
230         }
231         else
232             ret = SEC_E_INVALID_HANDLE;
233     }
234     else
235         ret = SEC_E_INVALID_HANDLE;
236     return ret;
237 }
238
239 /***********************************************************************
240  *              InitializeSecurityContextA (SECUR32.@)
241  */
242 SECURITY_STATUS WINAPI InitializeSecurityContextA(
243  PCredHandle phCredential, PCtxtHandle phContext,
244  SEC_CHAR *pszTargetName, ULONG fContextReq,
245  ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput,
246  ULONG Reserved2, PCtxtHandle phNewContext, PSecBufferDesc pOutput,
247  ULONG *pfContextAttr, PTimeStamp ptsExpiry)
248 {
249     SECURITY_STATUS ret;
250
251     TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
252      debugstr_a(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
253      Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
254     if (phCredential)
255     {
256         SecurePackage *package = (SecurePackage *)phCredential->dwUpper;
257         PCredHandle cred = (PCredHandle)phCredential->dwLower;
258
259         if (package && package->provider)
260         {
261             if (package->provider->fnTableA.InitializeSecurityContextA)
262             {
263                 CtxtHandle myCtxt;
264
265                 if(phContext)
266                 {
267                     PCtxtHandle realCtxt = (PCtxtHandle)phContext->dwLower;
268                     myCtxt.dwUpper = realCtxt->dwUpper;
269                     myCtxt.dwLower = realCtxt->dwLower;
270                 }
271
272                 ret = package->provider->fnTableA.InitializeSecurityContextA(
273                  cred, phContext ? &myCtxt : NULL, pszTargetName, fContextReq,
274                  Reserved1, TargetDataRep, pInput, Reserved2, &myCtxt,
275                  pOutput, pfContextAttr, ptsExpiry);
276                 if (ret == SEC_E_OK || ret == SEC_I_CONTINUE_NEEDED)
277                 {
278                     SECURITY_STATUS ret2;
279                     ret2 = SECUR32_makeSecHandle(phNewContext, package, &myCtxt);
280                     if (ret2 != SEC_E_OK)
281                         package->provider->fnTableW.DeleteSecurityContext(
282                          &myCtxt);
283                 }
284             }
285             else
286                 ret = SEC_E_UNSUPPORTED_FUNCTION;
287         }
288         else
289             ret = SEC_E_INVALID_HANDLE;
290     }
291     else
292         ret = SEC_E_INVALID_HANDLE;
293     return ret;
294 }
295
296 /***********************************************************************
297  *              InitializeSecurityContextW (SECUR32.@)
298  */
299 SECURITY_STATUS WINAPI InitializeSecurityContextW(
300  PCredHandle phCredential, PCtxtHandle phContext,
301  SEC_WCHAR *pszTargetName, ULONG fContextReq,
302  ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput,
303  ULONG Reserved2, PCtxtHandle phNewContext, PSecBufferDesc pOutput,
304  ULONG *pfContextAttr, PTimeStamp ptsExpiry)
305 {
306     SECURITY_STATUS ret;
307
308     TRACE("%p %p %s %d %d %d %p %d %p %p %p %p\n", phCredential, phContext,
309      debugstr_w(pszTargetName), fContextReq, Reserved1, TargetDataRep, pInput,
310      Reserved1, phNewContext, pOutput, pfContextAttr, ptsExpiry);
311     if (phCredential)
312     {
313         SecurePackage *package = (SecurePackage *)phCredential->dwUpper;
314         PCredHandle cred = (PCredHandle)phCredential->dwLower;
315
316         if (package && package->provider)
317         {
318             if (package->provider->fnTableW.QueryCredentialsAttributesW)
319             {
320                 CtxtHandle myCtxt;
321
322                 if(phContext)
323                 {
324                     PCtxtHandle realCtxt = (PCtxtHandle)phContext->dwLower;
325                     myCtxt.dwUpper = realCtxt->dwUpper;
326                     myCtxt.dwLower = realCtxt->dwLower;
327                 }
328
329                 ret = package->provider->fnTableW.InitializeSecurityContextW(
330                  cred, phContext ? &myCtxt : NULL, pszTargetName, fContextReq,
331                  Reserved1, TargetDataRep, pInput, Reserved2, &myCtxt,
332                  pOutput, pfContextAttr, ptsExpiry);
333                 if (ret == SEC_E_OK || ret == SEC_I_CONTINUE_NEEDED)
334                 {
335                     SECURITY_STATUS ret2;
336                     ret2 = SECUR32_makeSecHandle(phNewContext, package, &myCtxt);
337                     if (ret2 != SEC_E_OK)
338                         package->provider->fnTableW.DeleteSecurityContext(
339                          &myCtxt);
340                 }
341             }
342             else
343                 ret = SEC_E_UNSUPPORTED_FUNCTION;
344         }
345         else
346             ret = SEC_E_INVALID_HANDLE;
347     }
348     else
349         ret = SEC_E_INVALID_HANDLE;
350     return ret;
351 }
352
353 /***********************************************************************
354  *              AcceptSecurityContext (SECUR32.@)
355  */
356 SECURITY_STATUS WINAPI AcceptSecurityContext(
357  PCredHandle phCredential, PCtxtHandle phContext, PSecBufferDesc pInput,
358  ULONG fContextReq, ULONG TargetDataRep, PCtxtHandle phNewContext,
359  PSecBufferDesc pOutput, ULONG *pfContextAttr, PTimeStamp ptsExpiry)
360 {
361     SECURITY_STATUS ret;
362
363     TRACE("%p %p %p %d %d %p %p %p %p\n", phCredential, phContext, pInput,
364      fContextReq, TargetDataRep, phNewContext, pOutput, pfContextAttr,
365      ptsExpiry);
366     if (phCredential)
367     {
368         SecurePackage *package = (SecurePackage *)phCredential->dwUpper;
369         PCredHandle cred = (PCredHandle)phCredential->dwLower;
370
371         if (package && package->provider)
372         {
373             if (package->provider->fnTableW.AcceptSecurityContext)
374             {
375                 CtxtHandle myCtxt;
376
377                 if(phContext)
378                 {
379                     PCtxtHandle realCtxt = (PCtxtHandle)phContext->dwLower;
380                     TRACE("realCtx: %p\n", realCtxt);
381                     myCtxt.dwUpper = realCtxt->dwUpper;
382                     myCtxt.dwLower = realCtxt->dwLower;
383                 }
384                 
385                 ret = package->provider->fnTableW.AcceptSecurityContext(
386                  cred, phContext ? &myCtxt : NULL, pInput, fContextReq,
387                  TargetDataRep, &myCtxt, pOutput, pfContextAttr, ptsExpiry);
388                 if (ret == SEC_E_OK || ret == SEC_I_CONTINUE_NEEDED)
389                 {
390                     SECURITY_STATUS ret2;
391                     ret2 = SECUR32_makeSecHandle(phNewContext, package, &myCtxt);
392                     if (ret2 != SEC_E_OK)
393                         package->provider->fnTableW.DeleteSecurityContext(
394                          &myCtxt);
395                 }
396             }
397             else
398                 ret = SEC_E_UNSUPPORTED_FUNCTION;
399         }
400         else
401             ret = SEC_E_INVALID_HANDLE;
402     }
403     else
404         ret = SEC_E_INVALID_HANDLE;
405     return ret;
406 }
407
408 /***********************************************************************
409  *              CompleteAuthToken (SECUR32.@)
410  */
411 SECURITY_STATUS WINAPI CompleteAuthToken(PCtxtHandle phContext,
412  PSecBufferDesc pToken)
413 {
414     SECURITY_STATUS ret;
415
416     TRACE("%p %p\n", phContext, pToken);
417     if (phContext)
418     {
419         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
420         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
421
422         if (package && package->provider)
423         {
424             if (package->provider->fnTableW.CompleteAuthToken)
425                 ret = package->provider->fnTableW.CompleteAuthToken(ctxt,
426                  pToken);
427             else
428                 ret = SEC_E_UNSUPPORTED_FUNCTION;
429         }
430         else
431             ret = SEC_E_INVALID_HANDLE;
432     }
433     else
434         ret = SEC_E_INVALID_HANDLE;
435     return ret;
436 }
437
438 /***********************************************************************
439  *              DeleteSecurityContext (SECUR32.@)
440  */
441 SECURITY_STATUS WINAPI DeleteSecurityContext(PCtxtHandle phContext)
442 {
443     SECURITY_STATUS ret;
444
445     TRACE("%p\n", phContext);
446     if (phContext)
447     {
448         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
449         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
450
451         if (package && package->provider &&
452          package->provider->fnTableW.DeleteSecurityContext)
453             ret = package->provider->fnTableW.DeleteSecurityContext(ctxt);
454         else
455             ret = SEC_E_INVALID_HANDLE;
456         SECUR32_FREE(ctxt);
457     }
458     else
459         ret = SEC_E_INVALID_HANDLE;
460     return ret;
461 }
462
463 /***********************************************************************
464  *              ApplyControlToken (SECUR32.@)
465  */
466 SECURITY_STATUS WINAPI ApplyControlToken(PCtxtHandle phContext,
467  PSecBufferDesc pInput)
468 {
469     SECURITY_STATUS ret;
470
471     TRACE("%p %p\n", phContext, pInput);
472     if (phContext)
473     {
474         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
475         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
476
477         if (package && package->provider)
478         {
479             if (package->provider->fnTableW.ApplyControlToken)
480                 ret = package->provider->fnTableW.ApplyControlToken(
481                  ctxt, pInput);
482             else
483                 ret = SEC_E_UNSUPPORTED_FUNCTION;
484         }
485         else
486             ret = SEC_E_INVALID_HANDLE;
487     }
488     else
489         ret = SEC_E_INVALID_HANDLE;
490     return ret;
491 }
492
493 /***********************************************************************
494  *              QueryContextAttributesA (SECUR32.@)
495  */
496 SECURITY_STATUS WINAPI QueryContextAttributesA(PCtxtHandle phContext,
497  ULONG ulAttribute, void *pBuffer)
498 {
499     SECURITY_STATUS ret;
500
501     TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
502     if (phContext)
503     {
504         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
505         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
506
507         if (package && package->provider)
508         {
509             if (package->provider->fnTableA.QueryContextAttributesA)
510                 ret = package->provider->fnTableA.QueryContextAttributesA(
511                  ctxt, ulAttribute, pBuffer);
512             else
513                 ret = SEC_E_UNSUPPORTED_FUNCTION;
514         }
515         else
516             ret = SEC_E_INVALID_HANDLE;
517     }
518     else
519         ret = SEC_E_INVALID_HANDLE;
520     return ret;
521 }
522
523 /***********************************************************************
524  *              QueryContextAttributesW (SECUR32.@)
525  */
526 SECURITY_STATUS WINAPI QueryContextAttributesW(PCtxtHandle phContext,
527  ULONG ulAttribute, void *pBuffer)
528 {
529     SECURITY_STATUS ret;
530
531     TRACE("%p %d %p\n", phContext, ulAttribute, pBuffer);
532     if (phContext)
533     {
534         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
535         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
536
537         if (package && package->provider)
538         {
539             if (package->provider->fnTableW.QueryContextAttributesW)
540                 ret = package->provider->fnTableW.QueryContextAttributesW(
541                  ctxt, ulAttribute, pBuffer);
542             else
543                 ret = SEC_E_UNSUPPORTED_FUNCTION;
544         }
545         else
546             ret = SEC_E_INVALID_HANDLE;
547     }
548     else
549         ret = SEC_E_INVALID_HANDLE;
550     return ret;
551 }
552
553 /***********************************************************************
554  *              ImpersonateSecurityContext (SECUR32.@)
555  */
556 SECURITY_STATUS WINAPI ImpersonateSecurityContext(PCtxtHandle phContext)
557 {
558     SECURITY_STATUS ret;
559
560     TRACE("%p\n", phContext);
561     if (phContext)
562     {
563         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
564         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
565
566         if (package && package->provider)
567         {
568             if (package->provider->fnTableW.ImpersonateSecurityContext)
569                 ret = package->provider->fnTableW.ImpersonateSecurityContext(
570                  ctxt);
571             else
572                 ret = SEC_E_UNSUPPORTED_FUNCTION;
573         }
574         else
575             ret = SEC_E_INVALID_HANDLE;
576     }
577     else
578         ret = SEC_E_INVALID_HANDLE;
579     return ret;
580 }
581
582 /***********************************************************************
583  *              RevertSecurityContext (SECUR32.@)
584  */
585 SECURITY_STATUS WINAPI RevertSecurityContext(PCtxtHandle phContext)
586 {
587     SECURITY_STATUS ret;
588
589     TRACE("%p\n", phContext);
590     if (phContext)
591     {
592         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
593         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
594
595         if (package && package->provider)
596         {
597             if (package->provider->fnTableW.RevertSecurityContext)
598                 ret = package->provider->fnTableW.RevertSecurityContext(
599                  ctxt);
600             else
601                 ret = SEC_E_UNSUPPORTED_FUNCTION;
602         }
603         else
604             ret = SEC_E_INVALID_HANDLE;
605     }
606     else
607         ret = SEC_E_INVALID_HANDLE;
608     return ret;
609 }
610
611 /***********************************************************************
612  *              MakeSignature (SECUR32.@)
613  */
614 SECURITY_STATUS WINAPI MakeSignature(PCtxtHandle phContext, ULONG fQOP,
615  PSecBufferDesc pMessage, ULONG MessageSeqNo)
616 {
617     SECURITY_STATUS ret;
618
619     TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
620     if (phContext)
621     {
622         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
623         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
624
625         if (package && package->provider)
626         {
627             if (package->provider->fnTableW.MakeSignature)
628                 ret = package->provider->fnTableW.MakeSignature(
629                  ctxt, fQOP, pMessage, MessageSeqNo);
630             else
631                 ret = SEC_E_UNSUPPORTED_FUNCTION;
632         }
633         else
634             ret = SEC_E_INVALID_HANDLE;
635     }
636     else
637         ret = SEC_E_INVALID_HANDLE;
638     return ret;
639 }
640
641 /***********************************************************************
642  *              VerifySignature (SECUR32.@)
643  */
644 SECURITY_STATUS WINAPI VerifySignature(PCtxtHandle phContext,
645  PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
646 {
647     SECURITY_STATUS ret;
648
649     TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
650     if (phContext)
651     {
652         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
653         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
654
655         if (package && package->provider)
656         {
657             if (package->provider->fnTableW.VerifySignature)
658                 ret = package->provider->fnTableW.VerifySignature(
659                  ctxt, pMessage, MessageSeqNo, pfQOP);
660             else
661                 ret = SEC_E_UNSUPPORTED_FUNCTION;
662         }
663         else
664             ret = SEC_E_INVALID_HANDLE;
665     }
666     else
667         ret = SEC_E_INVALID_HANDLE;
668     return ret;
669 }
670
671 /***********************************************************************
672  *              QuerySecurityPackageInfoA (SECUR32.@)
673  */
674 SECURITY_STATUS WINAPI QuerySecurityPackageInfoA(SEC_CHAR *pszPackageName,
675  PSecPkgInfoA *ppPackageInfo)
676 {
677     SECURITY_STATUS ret;
678    
679     TRACE("%s %p\n", debugstr_a(pszPackageName), ppPackageInfo);
680     if (pszPackageName)
681     {
682         SecurePackage *package = SECUR32_findPackageA(pszPackageName);
683
684         if (package)
685         {
686             size_t bytesNeeded = sizeof(SecPkgInfoA);
687             int nameLen = 0, commentLen = 0;
688
689             if (package->infoW.Name)
690             {
691                 nameLen = WideCharToMultiByte(CP_ACP, 0,
692                  package->infoW.Name, -1, NULL, 0, NULL, NULL);
693                 bytesNeeded += nameLen;
694             }
695             if (package->infoW.Comment)
696             {
697                 commentLen = WideCharToMultiByte(CP_ACP, 0,
698                  package->infoW.Comment, -1, NULL, 0, NULL, NULL);
699                 bytesNeeded += commentLen;
700             }
701             *ppPackageInfo = (PSecPkgInfoA)SECUR32_ALLOC(bytesNeeded);
702             if (*ppPackageInfo)
703             {
704                 PSTR nextString = (PSTR)((PBYTE)*ppPackageInfo +
705                  sizeof(SecPkgInfoA));
706
707                 memcpy(*ppPackageInfo, &package->infoW, sizeof(package->infoW));
708                 if (package->infoW.Name)
709                 {
710                     (*ppPackageInfo)->Name = nextString;
711                     nextString += WideCharToMultiByte(CP_ACP, 0,
712                      package->infoW.Name, -1, nextString, nameLen, NULL, NULL);
713                 }
714                 else
715                     (*ppPackageInfo)->Name = NULL;
716                 if (package->infoW.Comment)
717                 {
718                     (*ppPackageInfo)->Comment = nextString;
719                     nextString += WideCharToMultiByte(CP_ACP, 0,
720                      package->infoW.Comment, -1, nextString, commentLen, NULL,
721                      NULL);
722                 }
723                 else
724                     (*ppPackageInfo)->Comment = NULL;
725                 ret = SEC_E_OK;
726             }
727             else
728                 ret = SEC_E_INSUFFICIENT_MEMORY;
729         }
730         else
731             ret = SEC_E_SECPKG_NOT_FOUND;
732     }
733     else
734         ret = SEC_E_SECPKG_NOT_FOUND;
735     return ret;
736 }
737
738 /***********************************************************************
739  *              QuerySecurityPackageInfoW (SECUR32.@)
740  */
741 SECURITY_STATUS WINAPI QuerySecurityPackageInfoW(SEC_WCHAR *pszPackageName,
742  PSecPkgInfoW *ppPackageInfo)
743 {
744     SECURITY_STATUS ret;
745     SecurePackage *package = SECUR32_findPackageW(pszPackageName);
746
747     TRACE("%s %p\n", debugstr_w(pszPackageName), ppPackageInfo);
748     if (package)
749     {
750         size_t bytesNeeded = sizeof(SecPkgInfoW);
751         int nameLen = 0, commentLen = 0;
752
753         if (package->infoW.Name)
754         {
755             nameLen = lstrlenW(package->infoW.Name) + 1;
756             bytesNeeded += nameLen * sizeof(WCHAR);
757         }
758         if (package->infoW.Comment)
759         {
760             commentLen = lstrlenW(package->infoW.Comment) + 1;
761             bytesNeeded += commentLen * sizeof(WCHAR);
762         }
763         *ppPackageInfo = (PSecPkgInfoW)SECUR32_ALLOC(bytesNeeded);
764         if (*ppPackageInfo)
765         {
766             PWSTR nextString = (PWSTR)((PBYTE)*ppPackageInfo +
767              sizeof(SecPkgInfoW));
768
769             memcpy(*ppPackageInfo, &package->infoW, sizeof(package->infoW));
770             if (package->infoW.Name)
771             {
772                 (*ppPackageInfo)->Name = nextString;
773                 lstrcpynW(nextString, package->infoW.Name, nameLen);
774                 nextString += nameLen;
775             }
776             else
777                 (*ppPackageInfo)->Name = NULL;
778             if (package->infoW.Comment)
779             {
780                 (*ppPackageInfo)->Comment = nextString;
781                 lstrcpynW(nextString, package->infoW.Comment, commentLen);
782                 nextString += commentLen;
783             }
784             else
785                 (*ppPackageInfo)->Comment = NULL;
786             ret = SEC_E_OK;
787         }
788         else
789             ret = SEC_E_INSUFFICIENT_MEMORY;
790     }
791     else
792         ret = SEC_E_SECPKG_NOT_FOUND;
793     return ret;
794 }
795
796 /***********************************************************************
797  *              ExportSecurityContext (SECUR32.@)
798  */
799 SECURITY_STATUS WINAPI ExportSecurityContext(PCtxtHandle phContext,
800  ULONG fFlags, PSecBuffer pPackedContext, void **pToken)
801 {
802     SECURITY_STATUS ret;
803
804     TRACE("%p %d %p %p\n", phContext, fFlags, pPackedContext, pToken);
805     if (phContext)
806     {
807         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
808         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
809
810         if (package && package->provider)
811         {
812             if (package->provider->fnTableW.ExportSecurityContext)
813                 ret = package->provider->fnTableW.ExportSecurityContext(
814                  ctxt, fFlags, pPackedContext, pToken);
815             else
816                 ret = SEC_E_UNSUPPORTED_FUNCTION;
817         }
818         else
819             ret = SEC_E_INVALID_HANDLE;
820     }
821     else
822         ret = SEC_E_INVALID_HANDLE;
823     return ret;
824 }
825
826 /***********************************************************************
827  *              ImportSecurityContextA (SECUR32.@)
828  */
829 SECURITY_STATUS WINAPI ImportSecurityContextA(SEC_CHAR *pszPackage,
830  PSecBuffer pPackedContext, void *Token, PCtxtHandle phContext)
831 {
832     SECURITY_STATUS ret;
833     SecurePackage *package = SECUR32_findPackageA(pszPackage);
834  
835     TRACE("%s %p %p %p\n", debugstr_a(pszPackage), pPackedContext, Token,
836      phContext);
837     if (package && package->provider)
838     {
839         if (package->provider->fnTableA.ImportSecurityContextA)
840         {
841             CtxtHandle myCtxt;
842
843             ret = package->provider->fnTableA.ImportSecurityContextA(
844              pszPackage, pPackedContext, Token, &myCtxt);
845             if (ret == SEC_E_OK)
846             {
847                 ret = SECUR32_makeSecHandle(phContext, package, &myCtxt);
848                 if (ret != SEC_E_OK)
849                     package->provider->fnTableW.DeleteSecurityContext(&myCtxt);
850             }
851         }
852         else
853             ret = SEC_E_UNSUPPORTED_FUNCTION;
854     }
855     else
856         ret = SEC_E_SECPKG_NOT_FOUND;
857     return ret;
858
859 }
860
861 /***********************************************************************
862  *              ImportSecurityContextW (SECUR32.@)
863  */
864 SECURITY_STATUS WINAPI ImportSecurityContextW(SEC_WCHAR *pszPackage,
865  PSecBuffer pPackedContext, void *Token, PCtxtHandle phContext)
866 {
867     SECURITY_STATUS ret;
868     SecurePackage *package = SECUR32_findPackageW(pszPackage);
869
870     TRACE("%s %p %p %p\n", debugstr_w(pszPackage), pPackedContext, Token,
871      phContext);
872     if (package && package->provider)
873     {
874         if (package->provider->fnTableW.ImportSecurityContextW)
875         {
876             CtxtHandle myCtxt;
877
878             ret = package->provider->fnTableW.ImportSecurityContextW(
879              pszPackage, pPackedContext, Token, &myCtxt);
880             if (ret == SEC_E_OK)
881             {
882                 ret = SECUR32_makeSecHandle(phContext, package, &myCtxt);
883                 if (ret != SEC_E_OK)
884                     package->provider->fnTableW.DeleteSecurityContext(&myCtxt);
885             }
886         }
887         else
888             ret = SEC_E_UNSUPPORTED_FUNCTION;
889     }
890     else
891         ret = SEC_E_SECPKG_NOT_FOUND;
892     return ret;
893 }
894
895 /***********************************************************************
896  *              AddCredentialsA (SECUR32.@)
897  */
898 SECURITY_STATUS WINAPI AddCredentialsA(PCredHandle hCredentials,
899  SEC_CHAR *pszPrincipal, SEC_CHAR *pszPackage, ULONG fCredentialUse,
900  void *pAuthData, SEC_GET_KEY_FN pGetKeyFn, void *pvGetKeyArgument,
901  PTimeStamp ptsExpiry)
902 {
903     SECURITY_STATUS ret;
904
905     TRACE("%p %s %s %d %p %p %p %p\n", hCredentials, debugstr_a(pszPrincipal),
906      debugstr_a(pszPackage), fCredentialUse, pAuthData, pGetKeyFn,
907      pvGetKeyArgument, ptsExpiry);
908     if (hCredentials)
909     {
910         SecurePackage *package = (SecurePackage *)hCredentials->dwUpper;
911         PCredHandle cred = (PCtxtHandle)hCredentials->dwLower;
912
913         if (package && package->provider)
914         {
915             if (package->provider->fnTableA.AddCredentialsA)
916                 ret = package->provider->fnTableA.AddCredentialsA(
917                  cred, pszPrincipal, pszPackage, fCredentialUse, pAuthData,
918                  pGetKeyFn, pvGetKeyArgument, ptsExpiry);
919             else
920                 ret = SEC_E_UNSUPPORTED_FUNCTION;
921         }
922         else
923             ret = SEC_E_INVALID_HANDLE;
924     }
925     else
926         ret = SEC_E_INVALID_HANDLE;
927     return ret;
928 }
929
930 /***********************************************************************
931  *              AddCredentialsW (SECUR32.@)
932  */
933 SECURITY_STATUS WINAPI AddCredentialsW(PCredHandle hCredentials,
934  SEC_WCHAR *pszPrincipal, SEC_WCHAR *pszPackage, ULONG fCredentialUse,
935  void *pAuthData, SEC_GET_KEY_FN pGetKeyFn, void *pvGetKeyArgument,
936  PTimeStamp ptsExpiry)
937 {
938     SECURITY_STATUS ret;
939
940     TRACE("%p %s %s %d %p %p %p %p\n", hCredentials, debugstr_w(pszPrincipal),
941      debugstr_w(pszPackage), fCredentialUse, pAuthData, pGetKeyFn,
942      pvGetKeyArgument, ptsExpiry);
943     if (hCredentials)
944     {
945         SecurePackage *package = (SecurePackage *)hCredentials->dwUpper;
946         PCredHandle cred = (PCtxtHandle)hCredentials->dwLower;
947
948         if (package && package->provider)
949         {
950             if (package->provider->fnTableW.AddCredentialsW)
951                 ret = package->provider->fnTableW.AddCredentialsW(
952                  cred, pszPrincipal, pszPackage, fCredentialUse, pAuthData,
953                  pGetKeyFn, pvGetKeyArgument, ptsExpiry);
954             else
955                 ret = SEC_E_UNSUPPORTED_FUNCTION;
956         }
957         else
958             ret = SEC_E_INVALID_HANDLE;
959     }
960     else
961         ret = SEC_E_INVALID_HANDLE;
962     return ret;
963 }
964
965 /***********************************************************************
966  *              QuerySecurityContextToken (SECUR32.@)
967  */
968 SECURITY_STATUS WINAPI QuerySecurityContextToken(PCtxtHandle phContext,
969  HANDLE *phToken)
970 {
971     SECURITY_STATUS ret;
972
973     TRACE("%p %p\n", phContext, phToken);
974     if (phContext)
975     {
976         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
977         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
978
979         if (package && package->provider)
980         {
981             if (package->provider->fnTableW.QuerySecurityContextToken)
982                 ret = package->provider->fnTableW.QuerySecurityContextToken(
983                  ctxt, phToken);
984             else
985                 ret = SEC_E_UNSUPPORTED_FUNCTION;
986         }
987         else
988             ret = SEC_E_INVALID_HANDLE;
989     }
990     else
991         ret = SEC_E_INVALID_HANDLE;
992     return ret;
993 }
994
995 /***********************************************************************
996  *              EncryptMessage (SECUR32.@)
997  */
998 SECURITY_STATUS WINAPI EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
999  PSecBufferDesc pMessage, ULONG MessageSeqNo)
1000 {
1001     SECURITY_STATUS ret;
1002
1003     TRACE("%p %d %p %d\n", phContext, fQOP, pMessage, MessageSeqNo);
1004     if (phContext)
1005     {
1006         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
1007         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
1008
1009         if (package && package->provider)
1010         {
1011             if (package->provider->fnTableW.EncryptMessage)
1012                 ret = package->provider->fnTableW.EncryptMessage(
1013                  ctxt, fQOP, pMessage, MessageSeqNo);
1014             else
1015                 ret = SEC_E_UNSUPPORTED_FUNCTION;
1016         }
1017         else
1018             ret = SEC_E_INVALID_HANDLE;
1019     }
1020     else
1021         ret = SEC_E_INVALID_HANDLE;
1022     return ret;
1023 }
1024
1025 /***********************************************************************
1026  *              DecryptMessage (SECUR32.@)
1027  */
1028 SECURITY_STATUS WINAPI DecryptMessage(PCtxtHandle phContext,
1029  PSecBufferDesc pMessage, ULONG MessageSeqNo, PULONG pfQOP)
1030 {
1031     SECURITY_STATUS ret;
1032
1033     TRACE("%p %p %d %p\n", phContext, pMessage, MessageSeqNo, pfQOP);
1034     if (phContext)
1035     {
1036         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
1037         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
1038
1039         if (package && package->provider)
1040         {
1041             if (package->provider->fnTableW.DecryptMessage)
1042                 ret = package->provider->fnTableW.DecryptMessage(
1043                  ctxt, pMessage, MessageSeqNo, pfQOP);
1044             else
1045                 ret = SEC_E_UNSUPPORTED_FUNCTION;
1046         }
1047         else
1048             ret = SEC_E_INVALID_HANDLE;
1049     }
1050     else
1051         ret = SEC_E_INVALID_HANDLE;
1052     return ret;
1053 }
1054
1055 /***********************************************************************
1056  *              SetContextAttributesA (SECUR32.@)
1057  */
1058 SECURITY_STATUS WINAPI SetContextAttributesA(PCtxtHandle phContext,
1059  ULONG ulAttribute, void *pBuffer, ULONG cbBuffer)
1060 {
1061     SECURITY_STATUS ret;
1062
1063     TRACE("%p %d %p %d\n", phContext, ulAttribute, pBuffer, cbBuffer);
1064     if (phContext)
1065     {
1066         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
1067         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
1068
1069         if (package && package->provider)
1070         {
1071             if (package->provider->fnTableA.SetContextAttributesA)
1072                 ret = package->provider->fnTableA.SetContextAttributesA(
1073                  ctxt, ulAttribute, pBuffer, cbBuffer);
1074             else
1075                 ret = SEC_E_UNSUPPORTED_FUNCTION;
1076         }
1077         else
1078             ret = SEC_E_INVALID_HANDLE;
1079     }
1080     else
1081         ret = SEC_E_INVALID_HANDLE;
1082     return ret;
1083 }
1084
1085 /***********************************************************************
1086  *              SetContextAttributesW (SECUR32.@)
1087  */
1088 SECURITY_STATUS WINAPI SetContextAttributesW(PCtxtHandle phContext,
1089  ULONG ulAttribute, void *pBuffer, ULONG cbBuffer)
1090 {
1091     SECURITY_STATUS ret;
1092
1093     TRACE("%p %d %p %d\n", phContext, ulAttribute, pBuffer, cbBuffer);
1094     if (phContext)
1095     {
1096         SecurePackage *package = (SecurePackage *)phContext->dwUpper;
1097         PCtxtHandle ctxt = (PCtxtHandle)phContext->dwLower;
1098
1099         if (package && package->provider)
1100         {
1101             if (package->provider->fnTableW.SetContextAttributesW)
1102                 ret = package->provider->fnTableW.SetContextAttributesW(
1103                  ctxt, ulAttribute, pBuffer, cbBuffer);
1104             else
1105                 ret = SEC_E_UNSUPPORTED_FUNCTION;
1106         }
1107         else
1108             ret = SEC_E_INVALID_HANDLE;
1109     }
1110     else
1111         ret = SEC_E_INVALID_HANDLE;
1112     return ret;
1113 }