wingss.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #ifndef NO_GSSAPI
  2. #include <limits.h>
  3. #include "putty.h"
  4. #define SECURITY_WIN32
  5. #include <security.h>
  6. #include "pgssapi.h"
  7. #include "sshgss.h"
  8. #include "sshgssc.h"
  9. #include "misc.h"
  10. #define UNIX_EPOCH 11644473600ULL /* Seconds from Windows epoch */
  11. #define CNS_PERSEC 10000000ULL /* # 100ns per second */
  12. /*
  13. * Note, as a special case, 0 relative to the Windows epoch (unspecified) maps
  14. * to 0 relative to the POSIX epoch (unspecified)!
  15. */
  16. #define TIME_WIN_TO_POSIX(ft, t) do { \
  17. ULARGE_INTEGER uli; \
  18. uli.LowPart = (ft).dwLowDateTime; \
  19. uli.HighPart = (ft).dwHighDateTime; \
  20. if (uli.QuadPart != 0) \
  21. uli.QuadPart = uli.QuadPart / CNS_PERSEC - UNIX_EPOCH; \
  22. (t) = (time_t) uli.QuadPart; \
  23. } while(0)
  24. /* Windows code to set up the GSSAPI library list. */
  25. #ifdef _WIN64
  26. #define MIT_KERB_SUFFIX "64"
  27. #else
  28. #define MIT_KERB_SUFFIX "32"
  29. #endif
  30. const int ngsslibs = 3;
  31. const char *const gsslibnames[3] = {
  32. "MIT Kerberos GSSAPI"MIT_KERB_SUFFIX".DLL",
  33. "Microsoft SSPI SECUR32.DLL",
  34. "User-specified GSSAPI DLL",
  35. };
  36. const struct keyvalwhere gsslibkeywords[] = {
  37. { "gssapi32", 0, -1, -1 },
  38. { "sspi", 1, -1, -1 },
  39. { "custom", 2, -1, -1 },
  40. };
  41. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  42. AcquireCredentialsHandleA,
  43. (SEC_CHAR *, SEC_CHAR *, ULONG, PVOID,
  44. PVOID, SEC_GET_KEY_FN, PVOID, PCredHandle, PTimeStamp));
  45. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  46. InitializeSecurityContextA,
  47. (PCredHandle, PCtxtHandle, SEC_CHAR *, ULONG, ULONG,
  48. ULONG, PSecBufferDesc, ULONG, PCtxtHandle,
  49. PSecBufferDesc, PULONG, PTimeStamp));
  50. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  51. FreeContextBuffer,
  52. (PVOID));
  53. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  54. FreeCredentialsHandle,
  55. (PCredHandle));
  56. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  57. DeleteSecurityContext,
  58. (PCtxtHandle));
  59. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  60. QueryContextAttributesA,
  61. (PCtxtHandle, ULONG, PVOID));
  62. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  63. MakeSignature,
  64. (PCtxtHandle, ULONG, PSecBufferDesc, ULONG));
  65. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  66. VerifySignature,
  67. (PCtxtHandle, PSecBufferDesc, ULONG, PULONG));
  68. DECL_WINDOWS_FUNCTION(static, DLL_DIRECTORY_COOKIE,
  69. AddDllDirectory,
  70. (PCWSTR));
  71. typedef struct winSsh_gss_ctx {
  72. unsigned long maj_stat;
  73. unsigned long min_stat;
  74. CredHandle cred_handle;
  75. CtxtHandle context;
  76. PCtxtHandle context_handle;
  77. TimeStamp expiry;
  78. } winSsh_gss_ctx;
  79. const Ssh_gss_buf gss_mech_krb5={9,"\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"};
  80. const char *gsslogmsg = NULL;
  81. static void ssh_sspi_bind_fns(struct ssh_gss_library *lib);
  82. struct ssh_gss_liblist *ssh_gss_setup(Conf *conf)
  83. {
  84. HMODULE module;
  85. HKEY regkey;
  86. struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist);
  87. char *path;
  88. static HMODULE kernel32_module;
  89. if (!kernel32_module) {
  90. kernel32_module = load_system32_dll("kernel32.dll");
  91. }
  92. #if defined _MSC_VER && _MSC_VER < 1900
  93. /* Omit the type-check because older MSVCs don't have this function */
  94. GET_WINDOWS_FUNCTION_NO_TYPECHECK(kernel32_module, AddDllDirectory);
  95. #else
  96. GET_WINDOWS_FUNCTION(kernel32_module, AddDllDirectory);
  97. #endif
  98. list->libraries = snewn(3, struct ssh_gss_library);
  99. list->nlibraries = 0;
  100. /* MIT Kerberos GSSAPI implementation */
  101. module = NULL;
  102. if (RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\MIT\\Kerberos", &regkey)
  103. == ERROR_SUCCESS) {
  104. DWORD type, size;
  105. LONG ret;
  106. char *buffer;
  107. /* Find out the string length */
  108. ret = RegQueryValueEx(regkey, "InstallDir", NULL, &type, NULL, &size);
  109. if (ret == ERROR_SUCCESS && type == REG_SZ) {
  110. buffer = snewn(size + 20, char);
  111. ret = RegQueryValueEx(regkey, "InstallDir", NULL,
  112. &type, (LPBYTE)buffer, &size);
  113. if (ret == ERROR_SUCCESS && type == REG_SZ) {
  114. strcat (buffer, "\\bin");
  115. if(p_AddDllDirectory) {
  116. /* Add MIT Kerberos' path to the DLL search path,
  117. * it loads its own DLLs further down the road */
  118. wchar_t *dllPath =
  119. dup_mb_to_wc(DEFAULT_CODEPAGE, 0, buffer);
  120. p_AddDllDirectory(dllPath);
  121. sfree(dllPath);
  122. }
  123. strcat (buffer, "\\gssapi"MIT_KERB_SUFFIX".dll");
  124. module = LoadLibraryEx (buffer, NULL,
  125. LOAD_LIBRARY_SEARCH_SYSTEM32 |
  126. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
  127. LOAD_LIBRARY_SEARCH_USER_DIRS);
  128. }
  129. sfree(buffer);
  130. }
  131. RegCloseKey(regkey);
  132. }
  133. if (module) {
  134. struct ssh_gss_library *lib =
  135. &list->libraries[list->nlibraries++];
  136. lib->id = 0;
  137. lib->gsslogmsg = "Using GSSAPI from GSSAPI"MIT_KERB_SUFFIX".DLL";
  138. lib->handle = (void *)module;
  139. #define BIND_GSS_FN(name) \
  140. lib->u.gssapi.name = (t_gss_##name) GetProcAddress(module, "gss_" #name)
  141. BIND_GSS_FN(delete_sec_context);
  142. BIND_GSS_FN(display_status);
  143. BIND_GSS_FN(get_mic);
  144. BIND_GSS_FN(verify_mic);
  145. BIND_GSS_FN(import_name);
  146. BIND_GSS_FN(init_sec_context);
  147. BIND_GSS_FN(release_buffer);
  148. BIND_GSS_FN(release_cred);
  149. BIND_GSS_FN(release_name);
  150. BIND_GSS_FN(acquire_cred);
  151. BIND_GSS_FN(inquire_cred_by_mech);
  152. #undef BIND_GSS_FN
  153. ssh_gssapi_bind_fns(lib);
  154. }
  155. /* Microsoft SSPI Implementation */
  156. module = load_system32_dll("secur32.dll");
  157. if (module) {
  158. struct ssh_gss_library *lib =
  159. &list->libraries[list->nlibraries++];
  160. lib->id = 1;
  161. lib->gsslogmsg = "Using SSPI from SECUR32.DLL";
  162. lib->handle = (void *)module;
  163. GET_WINDOWS_FUNCTION(module, AcquireCredentialsHandleA);
  164. GET_WINDOWS_FUNCTION(module, InitializeSecurityContextA);
  165. GET_WINDOWS_FUNCTION(module, FreeContextBuffer);
  166. GET_WINDOWS_FUNCTION(module, FreeCredentialsHandle);
  167. GET_WINDOWS_FUNCTION(module, DeleteSecurityContext);
  168. GET_WINDOWS_FUNCTION(module, QueryContextAttributesA);
  169. GET_WINDOWS_FUNCTION(module, MakeSignature);
  170. GET_WINDOWS_FUNCTION(module, VerifySignature);
  171. ssh_sspi_bind_fns(lib);
  172. }
  173. /*
  174. * Custom GSSAPI DLL.
  175. */
  176. module = NULL;
  177. path = conf_get_filename(conf, CONF_ssh_gss_custom)->path;
  178. if (*path) {
  179. if(p_AddDllDirectory) {
  180. /* Add the custom directory as well in case it chainloads
  181. * some other DLLs (e.g a non-installed MIT Kerberos
  182. * instance) */
  183. int pathlen = strlen(path);
  184. while (pathlen > 0 && path[pathlen-1] != ':' &&
  185. path[pathlen-1] != '\\')
  186. pathlen--;
  187. if (pathlen > 0 && path[pathlen-1] != '\\')
  188. pathlen--;
  189. if (pathlen > 0) {
  190. char *dirpath = dupprintf("%.*s", pathlen, path);
  191. wchar_t *dllPath = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, dirpath);
  192. p_AddDllDirectory(dllPath);
  193. sfree(dllPath);
  194. sfree(dirpath);
  195. }
  196. }
  197. module = LoadLibraryEx(path, NULL,
  198. LOAD_LIBRARY_SEARCH_SYSTEM32 |
  199. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
  200. LOAD_LIBRARY_SEARCH_USER_DIRS);
  201. }
  202. if (module) {
  203. struct ssh_gss_library *lib =
  204. &list->libraries[list->nlibraries++];
  205. lib->id = 2;
  206. lib->gsslogmsg = dupprintf("Using GSSAPI from user-specified"
  207. " library '%s'", path);
  208. lib->handle = (void *)module;
  209. #define BIND_GSS_FN(name) \
  210. lib->u.gssapi.name = (t_gss_##name) GetProcAddress(module, "gss_" #name)
  211. BIND_GSS_FN(delete_sec_context);
  212. BIND_GSS_FN(display_status);
  213. BIND_GSS_FN(get_mic);
  214. BIND_GSS_FN(verify_mic);
  215. BIND_GSS_FN(import_name);
  216. BIND_GSS_FN(init_sec_context);
  217. BIND_GSS_FN(release_buffer);
  218. BIND_GSS_FN(release_cred);
  219. BIND_GSS_FN(release_name);
  220. BIND_GSS_FN(acquire_cred);
  221. BIND_GSS_FN(inquire_cred_by_mech);
  222. #undef BIND_GSS_FN
  223. ssh_gssapi_bind_fns(lib);
  224. }
  225. return list;
  226. }
  227. void ssh_gss_cleanup(struct ssh_gss_liblist *list)
  228. {
  229. int i;
  230. /*
  231. * LoadLibrary and FreeLibrary are defined to employ reference
  232. * counting in the case where the same library is repeatedly
  233. * loaded, so even in a multiple-sessions-per-process context
  234. * (not that we currently expect ever to have such a thing on
  235. * Windows) it's safe to naively FreeLibrary everything here
  236. * without worrying about destroying it under the feet of
  237. * another SSH instance still using it.
  238. */
  239. for (i = 0; i < list->nlibraries; i++) {
  240. FreeLibrary((HMODULE)list->libraries[i].handle);
  241. if (list->libraries[i].id == 2) {
  242. /* The 'custom' id involves a dynamically allocated message.
  243. * Note that we must cast away the 'const' to free it. */
  244. sfree((char *)list->libraries[i].gsslogmsg);
  245. }
  246. }
  247. sfree(list->libraries);
  248. sfree(list);
  249. }
  250. static Ssh_gss_stat ssh_sspi_indicate_mech(struct ssh_gss_library *lib,
  251. Ssh_gss_buf *mech)
  252. {
  253. *mech = gss_mech_krb5;
  254. return SSH_GSS_OK;
  255. }
  256. static Ssh_gss_stat ssh_sspi_import_name(struct ssh_gss_library *lib,
  257. char *host, Ssh_gss_name *srv_name)
  258. {
  259. char *pStr;
  260. /* Check hostname */
  261. if (host == NULL) return SSH_GSS_FAILURE;
  262. /* copy it into form host/FQDN */
  263. pStr = dupcat("host/", host);
  264. *srv_name = (Ssh_gss_name) pStr;
  265. return SSH_GSS_OK;
  266. }
  267. static Ssh_gss_stat ssh_sspi_acquire_cred(struct ssh_gss_library *lib,
  268. Ssh_gss_ctx *ctx,
  269. time_t *expiry)
  270. {
  271. winSsh_gss_ctx *winctx = snew(winSsh_gss_ctx);
  272. memset(winctx, 0, sizeof(winSsh_gss_ctx));
  273. /* prepare our "wrapper" structure */
  274. winctx->maj_stat = winctx->min_stat = SEC_E_OK;
  275. winctx->context_handle = NULL;
  276. /* Specifying no principal name here means use the credentials of
  277. the current logged-in user */
  278. winctx->maj_stat = p_AcquireCredentialsHandleA(NULL,
  279. "Kerberos",
  280. SECPKG_CRED_OUTBOUND,
  281. NULL,
  282. NULL,
  283. NULL,
  284. NULL,
  285. &winctx->cred_handle,
  286. NULL);
  287. if (winctx->maj_stat != SEC_E_OK) {
  288. p_FreeCredentialsHandle(&winctx->cred_handle);
  289. sfree(winctx);
  290. return SSH_GSS_FAILURE;
  291. }
  292. /* Windows does not return a valid expiration from AcquireCredentials */
  293. if (expiry)
  294. *expiry = GSS_NO_EXPIRATION;
  295. *ctx = (Ssh_gss_ctx) winctx;
  296. return SSH_GSS_OK;
  297. }
  298. static void localexp_to_exp_lifetime(TimeStamp *localexp,
  299. time_t *expiry, unsigned long *lifetime)
  300. {
  301. FILETIME nowUTC;
  302. FILETIME expUTC;
  303. time_t now;
  304. time_t exp;
  305. time_t delta;
  306. if (!lifetime && !expiry)
  307. return;
  308. GetSystemTimeAsFileTime(&nowUTC);
  309. TIME_WIN_TO_POSIX(nowUTC, now);
  310. if (lifetime)
  311. *lifetime = 0;
  312. if (expiry)
  313. *expiry = GSS_NO_EXPIRATION;
  314. /*
  315. * Type oddity: localexp is a pointer to 'TimeStamp', whereas
  316. * LocalFileTimeToFileTime expects a pointer to FILETIME. However,
  317. * despite having different formal type names from the compiler's
  318. * point of view, these two structures are specified to be
  319. * isomorphic in the MS documentation, so it's legitimate to copy
  320. * between them:
  321. *
  322. * https://msdn.microsoft.com/en-us/library/windows/desktop/aa380511(v=vs.85).aspx
  323. */
  324. {
  325. FILETIME localexp_ft;
  326. enum { vorpal_sword = 1 / (sizeof(*localexp) == sizeof(localexp_ft)) };
  327. memcpy(&localexp_ft, localexp, sizeof(localexp_ft));
  328. if (!LocalFileTimeToFileTime(&localexp_ft, &expUTC))
  329. return;
  330. }
  331. TIME_WIN_TO_POSIX(expUTC, exp);
  332. delta = exp - now;
  333. if (exp == 0 || delta <= 0)
  334. return;
  335. if (expiry)
  336. *expiry = exp;
  337. if (lifetime) {
  338. if (delta <= ULONG_MAX)
  339. *lifetime = (unsigned long)delta;
  340. else
  341. *lifetime = ULONG_MAX;
  342. }
  343. }
  344. static Ssh_gss_stat ssh_sspi_init_sec_context(struct ssh_gss_library *lib,
  345. Ssh_gss_ctx *ctx,
  346. Ssh_gss_name srv_name,
  347. int to_deleg,
  348. Ssh_gss_buf *recv_tok,
  349. Ssh_gss_buf *send_tok,
  350. time_t *expiry,
  351. unsigned long *lifetime)
  352. {
  353. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) *ctx;
  354. SecBuffer wsend_tok = {send_tok->length,SECBUFFER_TOKEN,send_tok->value};
  355. SecBuffer wrecv_tok = {recv_tok->length,SECBUFFER_TOKEN,recv_tok->value};
  356. SecBufferDesc output_desc={SECBUFFER_VERSION,1,&wsend_tok};
  357. SecBufferDesc input_desc ={SECBUFFER_VERSION,1,&wrecv_tok};
  358. unsigned long flags=ISC_REQ_MUTUAL_AUTH|ISC_REQ_REPLAY_DETECT|
  359. ISC_REQ_CONFIDENTIALITY|ISC_REQ_ALLOCATE_MEMORY;
  360. unsigned long ret_flags=0;
  361. TimeStamp localexp;
  362. /* check if we have to delegate ... */
  363. if (to_deleg) flags |= ISC_REQ_DELEGATE;
  364. winctx->maj_stat = p_InitializeSecurityContextA(&winctx->cred_handle,
  365. winctx->context_handle,
  366. (char*) srv_name,
  367. flags,
  368. 0, /* reserved */
  369. SECURITY_NATIVE_DREP,
  370. &input_desc,
  371. 0, /* reserved */
  372. &winctx->context,
  373. &output_desc,
  374. &ret_flags,
  375. &localexp);
  376. localexp_to_exp_lifetime(&localexp, expiry, lifetime);
  377. /* prepare for the next round */
  378. winctx->context_handle = &winctx->context;
  379. send_tok->value = wsend_tok.pvBuffer;
  380. send_tok->length = wsend_tok.cbBuffer;
  381. /* check & return our status */
  382. if (winctx->maj_stat==SEC_E_OK) return SSH_GSS_S_COMPLETE;
  383. if (winctx->maj_stat==SEC_I_CONTINUE_NEEDED) return SSH_GSS_S_CONTINUE_NEEDED;
  384. return SSH_GSS_FAILURE;
  385. }
  386. static Ssh_gss_stat ssh_sspi_free_tok(struct ssh_gss_library *lib,
  387. Ssh_gss_buf *send_tok)
  388. {
  389. /* check input */
  390. if (send_tok == NULL) return SSH_GSS_FAILURE;
  391. /* free Windows buffer */
  392. p_FreeContextBuffer(send_tok->value);
  393. SSH_GSS_CLEAR_BUF(send_tok);
  394. return SSH_GSS_OK;
  395. }
  396. static Ssh_gss_stat ssh_sspi_release_cred(struct ssh_gss_library *lib,
  397. Ssh_gss_ctx *ctx)
  398. {
  399. winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) *ctx;
  400. /* check input */
  401. if (winctx == NULL) return SSH_GSS_FAILURE;
  402. /* free Windows data */
  403. p_FreeCredentialsHandle(&winctx->cred_handle);
  404. p_DeleteSecurityContext(&winctx->context);
  405. /* delete our "wrapper" structure */
  406. sfree(winctx);
  407. *ctx = (Ssh_gss_ctx) NULL;
  408. return SSH_GSS_OK;
  409. }
  410. static Ssh_gss_stat ssh_sspi_release_name(struct ssh_gss_library *lib,
  411. Ssh_gss_name *srv_name)
  412. {
  413. char *pStr= (char *) *srv_name;
  414. if (pStr == NULL) return SSH_GSS_FAILURE;
  415. sfree(pStr);
  416. *srv_name = (Ssh_gss_name) NULL;
  417. return SSH_GSS_OK;
  418. }
  419. static Ssh_gss_stat ssh_sspi_display_status(struct ssh_gss_library *lib,
  420. Ssh_gss_ctx ctx, Ssh_gss_buf *buf)
  421. {
  422. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) ctx;
  423. const char *msg;
  424. if (winctx == NULL) return SSH_GSS_FAILURE;
  425. /* decode the error code */
  426. switch (winctx->maj_stat) {
  427. case SEC_E_OK: msg="SSPI status OK"; break;
  428. case SEC_E_INVALID_HANDLE: msg="The handle passed to the function"
  429. " is invalid.";
  430. break;
  431. case SEC_E_TARGET_UNKNOWN: msg="The target was not recognized."; break;
  432. case SEC_E_LOGON_DENIED: msg="The logon failed."; break;
  433. case SEC_E_INTERNAL_ERROR: msg="The Local Security Authority cannot"
  434. " be contacted.";
  435. break;
  436. case SEC_E_NO_CREDENTIALS: msg="No credentials are available in the"
  437. " security package.";
  438. break;
  439. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  440. msg="No authority could be contacted for authentication."
  441. "The domain name of the authenticating party could be wrong,"
  442. " the domain could be unreachable, or there might have been"
  443. " a trust relationship failure.";
  444. break;
  445. case SEC_E_INSUFFICIENT_MEMORY:
  446. msg="One or more of the SecBufferDesc structures passed as"
  447. " an OUT parameter has a buffer that is too small.";
  448. break;
  449. case SEC_E_INVALID_TOKEN:
  450. msg="The error is due to a malformed input token, such as a"
  451. " token corrupted in transit, a token"
  452. " of incorrect size, or a token passed into the wrong"
  453. " security package. Passing a token to"
  454. " the wrong package can happen if client and server did not"
  455. " negotiate the proper security package.";
  456. break;
  457. default:
  458. msg = "Internal SSPI error";
  459. break;
  460. }
  461. buf->value = dupstr(msg);
  462. buf->length = strlen(buf->value);
  463. return SSH_GSS_OK;
  464. }
  465. static Ssh_gss_stat ssh_sspi_get_mic(struct ssh_gss_library *lib,
  466. Ssh_gss_ctx ctx, Ssh_gss_buf *buf,
  467. Ssh_gss_buf *hash)
  468. {
  469. winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) ctx;
  470. SecPkgContext_Sizes ContextSizes;
  471. SecBufferDesc InputBufferDescriptor;
  472. SecBuffer InputSecurityToken[2];
  473. if (winctx == NULL) return SSH_GSS_FAILURE;
  474. winctx->maj_stat = 0;
  475. memset(&ContextSizes, 0, sizeof(ContextSizes));
  476. winctx->maj_stat = p_QueryContextAttributesA(&winctx->context,
  477. SECPKG_ATTR_SIZES,
  478. &ContextSizes);
  479. if (winctx->maj_stat != SEC_E_OK ||
  480. ContextSizes.cbMaxSignature == 0)
  481. return winctx->maj_stat;
  482. InputBufferDescriptor.cBuffers = 2;
  483. InputBufferDescriptor.pBuffers = InputSecurityToken;
  484. InputBufferDescriptor.ulVersion = SECBUFFER_VERSION;
  485. InputSecurityToken[0].BufferType = SECBUFFER_DATA;
  486. InputSecurityToken[0].cbBuffer = buf->length;
  487. InputSecurityToken[0].pvBuffer = buf->value;
  488. InputSecurityToken[1].BufferType = SECBUFFER_TOKEN;
  489. InputSecurityToken[1].cbBuffer = ContextSizes.cbMaxSignature;
  490. InputSecurityToken[1].pvBuffer = snewn(ContextSizes.cbMaxSignature, char);
  491. winctx->maj_stat = p_MakeSignature(&winctx->context,
  492. 0,
  493. &InputBufferDescriptor,
  494. 0);
  495. if (winctx->maj_stat == SEC_E_OK) {
  496. hash->length = InputSecurityToken[1].cbBuffer;
  497. hash->value = InputSecurityToken[1].pvBuffer;
  498. }
  499. return winctx->maj_stat;
  500. }
  501. static Ssh_gss_stat ssh_sspi_verify_mic(struct ssh_gss_library *lib,
  502. Ssh_gss_ctx ctx,
  503. Ssh_gss_buf *buf,
  504. Ssh_gss_buf *mic)
  505. {
  506. winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) ctx;
  507. SecBufferDesc InputBufferDescriptor;
  508. SecBuffer InputSecurityToken[2];
  509. ULONG qop;
  510. if (winctx == NULL) return SSH_GSS_FAILURE;
  511. winctx->maj_stat = 0;
  512. InputBufferDescriptor.cBuffers = 2;
  513. InputBufferDescriptor.pBuffers = InputSecurityToken;
  514. InputBufferDescriptor.ulVersion = SECBUFFER_VERSION;
  515. InputSecurityToken[0].BufferType = SECBUFFER_DATA;
  516. InputSecurityToken[0].cbBuffer = buf->length;
  517. InputSecurityToken[0].pvBuffer = buf->value;
  518. InputSecurityToken[1].BufferType = SECBUFFER_TOKEN;
  519. InputSecurityToken[1].cbBuffer = mic->length;
  520. InputSecurityToken[1].pvBuffer = mic->value;
  521. winctx->maj_stat = p_VerifySignature(&winctx->context,
  522. &InputBufferDescriptor,
  523. 0, &qop);
  524. return winctx->maj_stat;
  525. }
  526. static Ssh_gss_stat ssh_sspi_free_mic(struct ssh_gss_library *lib,
  527. Ssh_gss_buf *hash)
  528. {
  529. sfree(hash->value);
  530. return SSH_GSS_OK;
  531. }
  532. static void ssh_sspi_bind_fns(struct ssh_gss_library *lib)
  533. {
  534. lib->indicate_mech = ssh_sspi_indicate_mech;
  535. lib->import_name = ssh_sspi_import_name;
  536. lib->release_name = ssh_sspi_release_name;
  537. lib->init_sec_context = ssh_sspi_init_sec_context;
  538. lib->free_tok = ssh_sspi_free_tok;
  539. lib->acquire_cred = ssh_sspi_acquire_cred;
  540. lib->release_cred = ssh_sspi_release_cred;
  541. lib->get_mic = ssh_sspi_get_mic;
  542. lib->verify_mic = ssh_sspi_verify_mic;
  543. lib->free_mic = ssh_sspi_free_mic;
  544. lib->display_status = ssh_sspi_display_status;
  545. }
  546. #else
  547. /* Dummy function so this source file defines something if NO_GSSAPI
  548. is defined. */
  549. void ssh_gss_init(void)
  550. {
  551. }
  552. #endif