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