wingss.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. #undef BIND_GSS_FN
  151. ssh_gssapi_bind_fns(lib);
  152. }
  153. /* Microsoft SSPI Implementation */
  154. module = load_system32_dll("secur32.dll");
  155. if (module) {
  156. struct ssh_gss_library *lib =
  157. &list->libraries[list->nlibraries++];
  158. lib->id = 1;
  159. lib->gsslogmsg = "Using SSPI from SECUR32.DLL";
  160. lib->handle = (void *)module;
  161. GET_WINDOWS_FUNCTION(module, AcquireCredentialsHandleA);
  162. GET_WINDOWS_FUNCTION(module, InitializeSecurityContextA);
  163. GET_WINDOWS_FUNCTION(module, FreeContextBuffer);
  164. GET_WINDOWS_FUNCTION(module, FreeCredentialsHandle);
  165. GET_WINDOWS_FUNCTION(module, DeleteSecurityContext);
  166. GET_WINDOWS_FUNCTION(module, QueryContextAttributesA);
  167. GET_WINDOWS_FUNCTION(module, MakeSignature);
  168. GET_WINDOWS_FUNCTION(module, VerifySignature);
  169. ssh_sspi_bind_fns(lib);
  170. }
  171. /*
  172. * Custom GSSAPI DLL.
  173. */
  174. module = NULL;
  175. path = conf_get_filename(conf, CONF_ssh_gss_custom)->path;
  176. if (*path) {
  177. if(p_AddDllDirectory) {
  178. /* Add the custom directory as well in case it chainloads
  179. * some other DLLs (e.g a non-installed MIT Kerberos
  180. * instance) */
  181. int pathlen = strlen(path);
  182. while (pathlen > 0 && path[pathlen-1] != ':' &&
  183. path[pathlen-1] != '\\')
  184. pathlen--;
  185. if (pathlen > 0 && path[pathlen-1] != '\\')
  186. pathlen--;
  187. if (pathlen > 0) {
  188. char *dirpath = dupprintf("%.*s", pathlen, path);
  189. wchar_t *dllPath = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, dirpath);
  190. p_AddDllDirectory(dllPath);
  191. sfree(dllPath);
  192. sfree(dirpath);
  193. }
  194. }
  195. module = LoadLibraryEx(path, NULL,
  196. LOAD_LIBRARY_SEARCH_SYSTEM32 |
  197. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
  198. LOAD_LIBRARY_SEARCH_USER_DIRS);
  199. }
  200. if (module) {
  201. struct ssh_gss_library *lib =
  202. &list->libraries[list->nlibraries++];
  203. lib->id = 2;
  204. lib->gsslogmsg = dupprintf("Using GSSAPI from user-specified"
  205. " library '%s'", path);
  206. lib->handle = (void *)module;
  207. #define BIND_GSS_FN(name) \
  208. lib->u.gssapi.name = (t_gss_##name) GetProcAddress(module, "gss_" #name)
  209. BIND_GSS_FN(delete_sec_context);
  210. BIND_GSS_FN(display_status);
  211. BIND_GSS_FN(get_mic);
  212. BIND_GSS_FN(verify_mic);
  213. BIND_GSS_FN(import_name);
  214. BIND_GSS_FN(init_sec_context);
  215. BIND_GSS_FN(release_buffer);
  216. BIND_GSS_FN(release_cred);
  217. BIND_GSS_FN(release_name);
  218. #undef BIND_GSS_FN
  219. ssh_gssapi_bind_fns(lib);
  220. }
  221. return list;
  222. }
  223. void ssh_gss_cleanup(struct ssh_gss_liblist *list)
  224. {
  225. int i;
  226. /*
  227. * LoadLibrary and FreeLibrary are defined to employ reference
  228. * counting in the case where the same library is repeatedly
  229. * loaded, so even in a multiple-sessions-per-process context
  230. * (not that we currently expect ever to have such a thing on
  231. * Windows) it's safe to naively FreeLibrary everything here
  232. * without worrying about destroying it under the feet of
  233. * another SSH instance still using it.
  234. */
  235. for (i = 0; i < list->nlibraries; i++) {
  236. FreeLibrary((HMODULE)list->libraries[i].handle);
  237. if (list->libraries[i].id == 2) {
  238. /* The 'custom' id involves a dynamically allocated message.
  239. * Note that we must cast away the 'const' to free it. */
  240. sfree((char *)list->libraries[i].gsslogmsg);
  241. }
  242. }
  243. sfree(list->libraries);
  244. sfree(list);
  245. }
  246. static Ssh_gss_stat ssh_sspi_indicate_mech(struct ssh_gss_library *lib,
  247. Ssh_gss_buf *mech)
  248. {
  249. *mech = gss_mech_krb5;
  250. return SSH_GSS_OK;
  251. }
  252. static Ssh_gss_stat ssh_sspi_import_name(struct ssh_gss_library *lib,
  253. char *host, Ssh_gss_name *srv_name)
  254. {
  255. char *pStr;
  256. /* Check hostname */
  257. if (host == NULL) return SSH_GSS_FAILURE;
  258. /* copy it into form host/FQDN */
  259. pStr = dupcat("host/", host, NULL);
  260. *srv_name = (Ssh_gss_name) pStr;
  261. return SSH_GSS_OK;
  262. }
  263. static Ssh_gss_stat ssh_sspi_acquire_cred(struct ssh_gss_library *lib,
  264. Ssh_gss_ctx *ctx,
  265. time_t *expiry)
  266. {
  267. winSsh_gss_ctx *winctx = snew(winSsh_gss_ctx);
  268. memset(winctx, 0, sizeof(winSsh_gss_ctx));
  269. /* prepare our "wrapper" structure */
  270. winctx->maj_stat = winctx->min_stat = SEC_E_OK;
  271. winctx->context_handle = NULL;
  272. /* Specifying no principal name here means use the credentials of
  273. the current logged-in user */
  274. winctx->maj_stat = p_AcquireCredentialsHandleA(NULL,
  275. "Kerberos",
  276. SECPKG_CRED_OUTBOUND,
  277. NULL,
  278. NULL,
  279. NULL,
  280. NULL,
  281. &winctx->cred_handle,
  282. NULL);
  283. if (winctx->maj_stat != SEC_E_OK) {
  284. p_FreeCredentialsHandle(&winctx->cred_handle);
  285. sfree(winctx);
  286. return SSH_GSS_FAILURE;
  287. }
  288. /* Windows does not return a valid expiration from AcquireCredentials */
  289. if (expiry)
  290. *expiry = GSS_NO_EXPIRATION;
  291. *ctx = (Ssh_gss_ctx) winctx;
  292. return SSH_GSS_OK;
  293. }
  294. static void localexp_to_exp_lifetime(TimeStamp *localexp,
  295. time_t *expiry, unsigned long *lifetime)
  296. {
  297. FILETIME nowUTC;
  298. FILETIME expUTC;
  299. time_t now;
  300. time_t exp;
  301. time_t delta;
  302. if (!lifetime && !expiry)
  303. return;
  304. GetSystemTimeAsFileTime(&nowUTC);
  305. TIME_WIN_TO_POSIX(nowUTC, now);
  306. if (lifetime)
  307. *lifetime = 0;
  308. if (expiry)
  309. *expiry = GSS_NO_EXPIRATION;
  310. /*
  311. * Type oddity: localexp is a pointer to 'TimeStamp', whereas
  312. * LocalFileTimeToFileTime expects a pointer to FILETIME. However,
  313. * despite having different formal type names from the compiler's
  314. * point of view, these two structures are specified to be
  315. * isomorphic in the MS documentation, so it's legitimate to copy
  316. * between them:
  317. *
  318. * https://msdn.microsoft.com/en-us/library/windows/desktop/aa380511(v=vs.85).aspx
  319. */
  320. {
  321. FILETIME localexp_ft;
  322. enum { vorpal_sword = 1 / (sizeof(*localexp) == sizeof(localexp_ft)) };
  323. memcpy(&localexp_ft, localexp, sizeof(localexp_ft));
  324. if (!LocalFileTimeToFileTime(&localexp_ft, &expUTC))
  325. return;
  326. }
  327. TIME_WIN_TO_POSIX(expUTC, exp);
  328. delta = exp - now;
  329. if (exp == 0 || delta <= 0)
  330. return;
  331. if (expiry)
  332. *expiry = exp;
  333. if (lifetime) {
  334. if (delta <= ULONG_MAX)
  335. *lifetime = (unsigned long)delta;
  336. else
  337. *lifetime = ULONG_MAX;
  338. }
  339. }
  340. static Ssh_gss_stat ssh_sspi_init_sec_context(struct ssh_gss_library *lib,
  341. Ssh_gss_ctx *ctx,
  342. Ssh_gss_name srv_name,
  343. int to_deleg,
  344. Ssh_gss_buf *recv_tok,
  345. Ssh_gss_buf *send_tok,
  346. time_t *expiry,
  347. unsigned long *lifetime)
  348. {
  349. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) *ctx;
  350. SecBuffer wsend_tok = {send_tok->length,SECBUFFER_TOKEN,send_tok->value};
  351. SecBuffer wrecv_tok = {recv_tok->length,SECBUFFER_TOKEN,recv_tok->value};
  352. SecBufferDesc output_desc={SECBUFFER_VERSION,1,&wsend_tok};
  353. SecBufferDesc input_desc ={SECBUFFER_VERSION,1,&wrecv_tok};
  354. unsigned long flags=ISC_REQ_MUTUAL_AUTH|ISC_REQ_REPLAY_DETECT|
  355. ISC_REQ_CONFIDENTIALITY|ISC_REQ_ALLOCATE_MEMORY;
  356. unsigned long ret_flags=0;
  357. TimeStamp localexp;
  358. /* check if we have to delegate ... */
  359. if (to_deleg) flags |= ISC_REQ_DELEGATE;
  360. winctx->maj_stat = p_InitializeSecurityContextA(&winctx->cred_handle,
  361. winctx->context_handle,
  362. (char*) srv_name,
  363. flags,
  364. 0, /* reserved */
  365. SECURITY_NATIVE_DREP,
  366. &input_desc,
  367. 0, /* reserved */
  368. &winctx->context,
  369. &output_desc,
  370. &ret_flags,
  371. &localexp);
  372. localexp_to_exp_lifetime(&localexp, expiry, lifetime);
  373. /* prepare for the next round */
  374. winctx->context_handle = &winctx->context;
  375. send_tok->value = wsend_tok.pvBuffer;
  376. send_tok->length = wsend_tok.cbBuffer;
  377. /* check & return our status */
  378. if (winctx->maj_stat==SEC_E_OK) return SSH_GSS_S_COMPLETE;
  379. if (winctx->maj_stat==SEC_I_CONTINUE_NEEDED) return SSH_GSS_S_CONTINUE_NEEDED;
  380. return SSH_GSS_FAILURE;
  381. }
  382. static Ssh_gss_stat ssh_sspi_free_tok(struct ssh_gss_library *lib,
  383. Ssh_gss_buf *send_tok)
  384. {
  385. /* check input */
  386. if (send_tok == NULL) return SSH_GSS_FAILURE;
  387. /* free Windows buffer */
  388. p_FreeContextBuffer(send_tok->value);
  389. SSH_GSS_CLEAR_BUF(send_tok);
  390. return SSH_GSS_OK;
  391. }
  392. static Ssh_gss_stat ssh_sspi_release_cred(struct ssh_gss_library *lib,
  393. Ssh_gss_ctx *ctx)
  394. {
  395. winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) *ctx;
  396. /* check input */
  397. if (winctx == NULL) return SSH_GSS_FAILURE;
  398. /* free Windows data */
  399. p_FreeCredentialsHandle(&winctx->cred_handle);
  400. p_DeleteSecurityContext(&winctx->context);
  401. /* delete our "wrapper" structure */
  402. sfree(winctx);
  403. *ctx = (Ssh_gss_ctx) NULL;
  404. return SSH_GSS_OK;
  405. }
  406. static Ssh_gss_stat ssh_sspi_release_name(struct ssh_gss_library *lib,
  407. Ssh_gss_name *srv_name)
  408. {
  409. char *pStr= (char *) *srv_name;
  410. if (pStr == NULL) return SSH_GSS_FAILURE;
  411. sfree(pStr);
  412. *srv_name = (Ssh_gss_name) NULL;
  413. return SSH_GSS_OK;
  414. }
  415. static Ssh_gss_stat ssh_sspi_display_status(struct ssh_gss_library *lib,
  416. Ssh_gss_ctx ctx, Ssh_gss_buf *buf)
  417. {
  418. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) ctx;
  419. const char *msg;
  420. if (winctx == NULL) return SSH_GSS_FAILURE;
  421. /* decode the error code */
  422. switch (winctx->maj_stat) {
  423. case SEC_E_OK: msg="SSPI status OK"; break;
  424. case SEC_E_INVALID_HANDLE: msg="The handle passed to the function"
  425. " is invalid.";
  426. break;
  427. case SEC_E_TARGET_UNKNOWN: msg="The target was not recognized."; break;
  428. case SEC_E_LOGON_DENIED: msg="The logon failed."; break;
  429. case SEC_E_INTERNAL_ERROR: msg="The Local Security Authority cannot"
  430. " be contacted.";
  431. break;
  432. case SEC_E_NO_CREDENTIALS: msg="No credentials are available in the"
  433. " security package.";
  434. break;
  435. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  436. msg="No authority could be contacted for authentication."
  437. "The domain name of the authenticating party could be wrong,"
  438. " the domain could be unreachable, or there might have been"
  439. " a trust relationship failure.";
  440. break;
  441. case SEC_E_INSUFFICIENT_MEMORY:
  442. msg="One or more of the SecBufferDesc structures passed as"
  443. " an OUT parameter has a buffer that is too small.";
  444. break;
  445. case SEC_E_INVALID_TOKEN:
  446. msg="The error is due to a malformed input token, such as a"
  447. " token corrupted in transit, a token"
  448. " of incorrect size, or a token passed into the wrong"
  449. " security package. Passing a token to"
  450. " the wrong package can happen if client and server did not"
  451. " negotiate the proper security package.";
  452. break;
  453. default:
  454. msg = "Internal SSPI error";
  455. break;
  456. }
  457. buf->value = dupstr(msg);
  458. buf->length = strlen(buf->value);
  459. return SSH_GSS_OK;
  460. }
  461. static Ssh_gss_stat ssh_sspi_get_mic(struct ssh_gss_library *lib,
  462. Ssh_gss_ctx ctx, Ssh_gss_buf *buf,
  463. Ssh_gss_buf *hash)
  464. {
  465. winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) ctx;
  466. SecPkgContext_Sizes ContextSizes;
  467. SecBufferDesc InputBufferDescriptor;
  468. SecBuffer InputSecurityToken[2];
  469. if (winctx == NULL) return SSH_GSS_FAILURE;
  470. winctx->maj_stat = 0;
  471. memset(&ContextSizes, 0, sizeof(ContextSizes));
  472. winctx->maj_stat = p_QueryContextAttributesA(&winctx->context,
  473. SECPKG_ATTR_SIZES,
  474. &ContextSizes);
  475. if (winctx->maj_stat != SEC_E_OK ||
  476. ContextSizes.cbMaxSignature == 0)
  477. return winctx->maj_stat;
  478. InputBufferDescriptor.cBuffers = 2;
  479. InputBufferDescriptor.pBuffers = InputSecurityToken;
  480. InputBufferDescriptor.ulVersion = SECBUFFER_VERSION;
  481. InputSecurityToken[0].BufferType = SECBUFFER_DATA;
  482. InputSecurityToken[0].cbBuffer = buf->length;
  483. InputSecurityToken[0].pvBuffer = buf->value;
  484. InputSecurityToken[1].BufferType = SECBUFFER_TOKEN;
  485. InputSecurityToken[1].cbBuffer = ContextSizes.cbMaxSignature;
  486. InputSecurityToken[1].pvBuffer = snewn(ContextSizes.cbMaxSignature, char);
  487. winctx->maj_stat = p_MakeSignature(&winctx->context,
  488. 0,
  489. &InputBufferDescriptor,
  490. 0);
  491. if (winctx->maj_stat == SEC_E_OK) {
  492. hash->length = InputSecurityToken[1].cbBuffer;
  493. hash->value = InputSecurityToken[1].pvBuffer;
  494. }
  495. return winctx->maj_stat;
  496. }
  497. static Ssh_gss_stat ssh_sspi_verify_mic(struct ssh_gss_library *lib,
  498. Ssh_gss_ctx ctx,
  499. Ssh_gss_buf *buf,
  500. Ssh_gss_buf *mic)
  501. {
  502. winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) ctx;
  503. SecBufferDesc InputBufferDescriptor;
  504. SecBuffer InputSecurityToken[2];
  505. ULONG qop;
  506. if (winctx == NULL) return SSH_GSS_FAILURE;
  507. winctx->maj_stat = 0;
  508. InputBufferDescriptor.cBuffers = 2;
  509. InputBufferDescriptor.pBuffers = InputSecurityToken;
  510. InputBufferDescriptor.ulVersion = SECBUFFER_VERSION;
  511. InputSecurityToken[0].BufferType = SECBUFFER_DATA;
  512. InputSecurityToken[0].cbBuffer = buf->length;
  513. InputSecurityToken[0].pvBuffer = buf->value;
  514. InputSecurityToken[1].BufferType = SECBUFFER_TOKEN;
  515. InputSecurityToken[1].cbBuffer = mic->length;
  516. InputSecurityToken[1].pvBuffer = mic->value;
  517. winctx->maj_stat = p_VerifySignature(&winctx->context,
  518. &InputBufferDescriptor,
  519. 0, &qop);
  520. return winctx->maj_stat;
  521. }
  522. static Ssh_gss_stat ssh_sspi_free_mic(struct ssh_gss_library *lib,
  523. Ssh_gss_buf *hash)
  524. {
  525. sfree(hash->value);
  526. return SSH_GSS_OK;
  527. }
  528. static void ssh_sspi_bind_fns(struct ssh_gss_library *lib)
  529. {
  530. lib->indicate_mech = ssh_sspi_indicate_mech;
  531. lib->import_name = ssh_sspi_import_name;
  532. lib->release_name = ssh_sspi_release_name;
  533. lib->init_sec_context = ssh_sspi_init_sec_context;
  534. lib->free_tok = ssh_sspi_free_tok;
  535. lib->acquire_cred = ssh_sspi_acquire_cred;
  536. lib->release_cred = ssh_sspi_release_cred;
  537. lib->get_mic = ssh_sspi_get_mic;
  538. lib->verify_mic = ssh_sspi_verify_mic;
  539. lib->free_mic = ssh_sspi_free_mic;
  540. lib->display_status = ssh_sspi_display_status;
  541. }
  542. #else
  543. /* Dummy function so this source file defines something if NO_GSSAPI
  544. is defined. */
  545. void ssh_gss_init(void)
  546. {
  547. }
  548. #endif