kerberos_sspi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "kerberos_sspi.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. static HINSTANCE _sspi_security_dll = NULL;
  5. static HINSTANCE _sspi_secur32_dll = NULL;
  6. /**
  7. * Encrypt A Message
  8. */
  9. SECURITY_STATUS SEC_ENTRY _sspi_EncryptMessage(PCtxtHandle phContext, unsigned long fQOP, PSecBufferDesc pMessage, unsigned long MessageSeqNo) {
  10. // Create function pointer instance
  11. encryptMessage_fn pfn_encryptMessage = NULL;
  12. // Return error if library not loaded
  13. if(_sspi_security_dll == NULL) return -1;
  14. // Map function to library method
  15. pfn_encryptMessage = (encryptMessage_fn)GetProcAddress(_sspi_security_dll, "EncryptMessage");
  16. // Check if the we managed to map function pointer
  17. if(!pfn_encryptMessage) {
  18. printf("GetProcAddress failed.\n");
  19. return -2;
  20. }
  21. // Call the function
  22. return (*pfn_encryptMessage)(phContext, fQOP, pMessage, MessageSeqNo);
  23. }
  24. /**
  25. * Acquire Credentials
  26. */
  27. SECURITY_STATUS SEC_ENTRY _sspi_AcquireCredentialsHandle(
  28. LPSTR pszPrincipal, LPSTR pszPackage, unsigned long fCredentialUse,
  29. void * pvLogonId, void * pAuthData, SEC_GET_KEY_FN pGetKeyFn, void * pvGetKeyArgument,
  30. PCredHandle phCredential, PTimeStamp ptsExpiry
  31. ) {
  32. SECURITY_STATUS status;
  33. // Create function pointer instance
  34. acquireCredentialsHandle_fn pfn_acquireCredentialsHandle = NULL;
  35. // Return error if library not loaded
  36. if(_sspi_security_dll == NULL) return -1;
  37. // Map function
  38. #ifdef _UNICODE
  39. pfn_acquireCredentialsHandle = (acquireCredentialsHandle_fn)GetProcAddress(_sspi_security_dll, "AcquireCredentialsHandleW");
  40. #else
  41. pfn_acquireCredentialsHandle = (acquireCredentialsHandle_fn)GetProcAddress(_sspi_security_dll, "AcquireCredentialsHandleA");
  42. #endif
  43. // Check if the we managed to map function pointer
  44. if(!pfn_acquireCredentialsHandle) {
  45. printf("GetProcAddress failed.\n");
  46. return -2;
  47. }
  48. // Status
  49. status = (*pfn_acquireCredentialsHandle)(pszPrincipal, pszPackage, fCredentialUse,
  50. pvLogonId, pAuthData, pGetKeyFn, pvGetKeyArgument, phCredential, ptsExpiry
  51. );
  52. // Call the function
  53. return status;
  54. }
  55. /**
  56. * Delete Security Context
  57. */
  58. SECURITY_STATUS SEC_ENTRY _sspi_DeleteSecurityContext(PCtxtHandle phContext) {
  59. // Create function pointer instance
  60. deleteSecurityContext_fn pfn_deleteSecurityContext = NULL;
  61. // Return error if library not loaded
  62. if(_sspi_security_dll == NULL) return -1;
  63. // Map function
  64. pfn_deleteSecurityContext = (deleteSecurityContext_fn)GetProcAddress(_sspi_security_dll, "DeleteSecurityContext");
  65. // Check if the we managed to map function pointer
  66. if(!pfn_deleteSecurityContext) {
  67. printf("GetProcAddress failed.\n");
  68. return -2;
  69. }
  70. // Call the function
  71. return (*pfn_deleteSecurityContext)(phContext);
  72. }
  73. /**
  74. * Decrypt Message
  75. */
  76. SECURITY_STATUS SEC_ENTRY _sspi_DecryptMessage(PCtxtHandle phContext, PSecBufferDesc pMessage, unsigned long MessageSeqNo, unsigned long pfQOP) {
  77. // Create function pointer instance
  78. decryptMessage_fn pfn_decryptMessage = NULL;
  79. // Return error if library not loaded
  80. if(_sspi_security_dll == NULL) return -1;
  81. // Map function
  82. pfn_decryptMessage = (decryptMessage_fn)GetProcAddress(_sspi_security_dll, "DecryptMessage");
  83. // Check if the we managed to map function pointer
  84. if(!pfn_decryptMessage) {
  85. printf("GetProcAddress failed.\n");
  86. return -2;
  87. }
  88. // Call the function
  89. return (*pfn_decryptMessage)(phContext, pMessage, MessageSeqNo, pfQOP);
  90. }
  91. /**
  92. * Initialize Security Context
  93. */
  94. SECURITY_STATUS SEC_ENTRY _sspi_initializeSecurityContext(
  95. PCredHandle phCredential, PCtxtHandle phContext,
  96. LPSTR pszTargetName, unsigned long fContextReq,
  97. unsigned long Reserved1, unsigned long TargetDataRep,
  98. PSecBufferDesc pInput, unsigned long Reserved2,
  99. PCtxtHandle phNewContext, PSecBufferDesc pOutput,
  100. unsigned long * pfContextAttr, PTimeStamp ptsExpiry
  101. ) {
  102. SECURITY_STATUS status;
  103. // Create function pointer instance
  104. initializeSecurityContext_fn pfn_initializeSecurityContext = NULL;
  105. // Return error if library not loaded
  106. if(_sspi_security_dll == NULL) return -1;
  107. // Map function
  108. #ifdef _UNICODE
  109. pfn_initializeSecurityContext = (initializeSecurityContext_fn)GetProcAddress(_sspi_security_dll, "InitializeSecurityContextW");
  110. #else
  111. pfn_initializeSecurityContext = (initializeSecurityContext_fn)GetProcAddress(_sspi_security_dll, "InitializeSecurityContextA");
  112. #endif
  113. // Check if the we managed to map function pointer
  114. if(!pfn_initializeSecurityContext) {
  115. printf("GetProcAddress failed.\n");
  116. return -2;
  117. }
  118. // Execute intialize context
  119. status = (*pfn_initializeSecurityContext)(
  120. phCredential, phContext, pszTargetName, fContextReq,
  121. Reserved1, TargetDataRep, pInput, Reserved2,
  122. phNewContext, pOutput, pfContextAttr, ptsExpiry
  123. );
  124. // Call the function
  125. return status;
  126. }
  127. /**
  128. * Query Context Attributes
  129. */
  130. SECURITY_STATUS SEC_ENTRY _sspi_QueryContextAttributes(
  131. PCtxtHandle phContext, unsigned long ulAttribute, void * pBuffer
  132. ) {
  133. // Create function pointer instance
  134. queryContextAttributes_fn pfn_queryContextAttributes = NULL;
  135. // Return error if library not loaded
  136. if(_sspi_security_dll == NULL) return -1;
  137. #ifdef _UNICODE
  138. pfn_queryContextAttributes = (queryContextAttributes_fn)GetProcAddress(_sspi_security_dll, "QueryContextAttributesW");
  139. #else
  140. pfn_queryContextAttributes = (queryContextAttributes_fn)GetProcAddress(_sspi_security_dll, "QueryContextAttributesA");
  141. #endif
  142. // Check if the we managed to map function pointer
  143. if(!pfn_queryContextAttributes) {
  144. printf("GetProcAddress failed.\n");
  145. return -2;
  146. }
  147. // Call the function
  148. return (*pfn_queryContextAttributes)(
  149. phContext, ulAttribute, pBuffer
  150. );
  151. }
  152. /**
  153. * InitSecurityInterface
  154. */
  155. PSecurityFunctionTable _ssip_InitSecurityInterface() {
  156. INIT_SECURITY_INTERFACE InitSecurityInterface;
  157. PSecurityFunctionTable pSecurityInterface = NULL;
  158. // Return error if library not loaded
  159. if(_sspi_security_dll == NULL) return NULL;
  160. #ifdef _UNICODE
  161. // Get the address of the InitSecurityInterface function.
  162. InitSecurityInterface = (INIT_SECURITY_INTERFACE) GetProcAddress (
  163. _sspi_secur32_dll,
  164. TEXT("InitSecurityInterfaceW"));
  165. #else
  166. // Get the address of the InitSecurityInterface function.
  167. InitSecurityInterface = (INIT_SECURITY_INTERFACE) GetProcAddress (
  168. _sspi_secur32_dll,
  169. TEXT("InitSecurityInterfaceA"));
  170. #endif
  171. if(!InitSecurityInterface) {
  172. printf (TEXT("Failed in getting the function address, Error: %x"), GetLastError ());
  173. return NULL;
  174. }
  175. // Use InitSecurityInterface to get the function table.
  176. pSecurityInterface = (*InitSecurityInterface)();
  177. if(!pSecurityInterface) {
  178. printf (TEXT("Failed in getting the function table, Error: %x"), GetLastError ());
  179. return NULL;
  180. }
  181. return pSecurityInterface;
  182. }
  183. /**
  184. * Load security.dll dynamically
  185. */
  186. int load_library() {
  187. DWORD err;
  188. // Load the library
  189. _sspi_security_dll = LoadLibrary("security.dll");
  190. // Check if the library loaded
  191. if(_sspi_security_dll == NULL) {
  192. err = GetLastError();
  193. return err;
  194. }
  195. // Load the library
  196. _sspi_secur32_dll = LoadLibrary("secur32.dll");
  197. // Check if the library loaded
  198. if(_sspi_secur32_dll == NULL) {
  199. err = GetLastError();
  200. return err;
  201. }
  202. return 0;
  203. }