gss.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. #ifndef NO_GSSAPI
  2. #include <limits.h>
  3. #include "putty.h"
  4. #define SECURITY_WIN32
  5. #include <security.h>
  6. #include "ssh/pgssapi.h"
  7. #include "ssh/gss.h"
  8. #include "ssh/gssc.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. #ifdef MPEXT
  80. static
  81. #endif
  82. const Ssh_gss_buf gss_mech_krb5={9,"\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"};
  83. static void ssh_sspi_bind_fns(struct ssh_gss_library *lib);
  84. static tree234 *libraries_to_never_unload;
  85. static int library_to_never_unload_cmp(void *av, void *bv)
  86. {
  87. uintptr_t a = (uintptr_t)av, b = (uintptr_t)bv;
  88. return a < b ? -1 : a > b ? +1 : 0;
  89. }
  90. static void ensure_library_tree_exists(void)
  91. {
  92. if (!libraries_to_never_unload)
  93. libraries_to_never_unload = newtree234(library_to_never_unload_cmp);
  94. }
  95. static bool library_is_in_never_unload_tree(HMODULE module)
  96. {
  97. ensure_library_tree_exists();
  98. return find234(libraries_to_never_unload, module, NULL);
  99. }
  100. static void add_library_to_never_unload_tree(HMODULE module)
  101. {
  102. ensure_library_tree_exists();
  103. add234(libraries_to_never_unload, module);
  104. }
  105. struct ssh_gss_liblist *ssh_gss_setup(Conf *conf, LogContext *logctx) // MPEXT
  106. {
  107. HMODULE module;
  108. struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist);
  109. static HMODULE kernel32_module;
  110. if (!kernel32_module) {
  111. kernel32_module = load_system32_dll("kernel32.dll");
  112. }
  113. #if !HAVE_ADDDLLDIRECTORY
  114. /* Omit the type-check because older MSVCs don't have this function */
  115. GET_WINDOWS_FUNCTION_NO_TYPECHECK(kernel32_module, AddDllDirectory);
  116. #else
  117. GET_WINDOWS_FUNCTION(kernel32_module, AddDllDirectory);
  118. #endif
  119. list->libraries = snewn(3, struct ssh_gss_library);
  120. list->nlibraries = 0;
  121. /* MIT Kerberos GSSAPI implementation */
  122. module = NULL;
  123. HKEY regkey = open_regkey_ro(HKEY_LOCAL_MACHINE,
  124. "SOFTWARE\\MIT\\Kerberos");
  125. if (regkey) {
  126. char *installdir = get_reg_sz(regkey, "InstallDir");
  127. if (installdir) {
  128. char *bindir = dupcat(installdir, "\\bin");
  129. if (p_AddDllDirectory) {
  130. /* Add MIT Kerberos' path to the DLL search path,
  131. * it loads its own DLLs further down the road */
  132. wchar_t *dllPath = dup_mb_to_wc(DEFAULT_CODEPAGE, bindir);
  133. p_AddDllDirectory(dllPath);
  134. sfree(dllPath);
  135. }
  136. char *dllfile = dupcat(bindir, "\\gssapi"MIT_KERB_SUFFIX".dll");
  137. module = LoadLibraryEx(dllfile, NULL,
  138. LOAD_LIBRARY_SEARCH_SYSTEM32 |
  139. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
  140. LOAD_LIBRARY_SEARCH_USER_DIRS);
  141. /*
  142. * The MIT Kerberos DLL suffers an internal segfault for
  143. * some reason if you unload and reload one within the
  144. * same process. So, make sure that after we load this
  145. * library, we never free it.
  146. *
  147. * Or rather: after we've loaded it once, if any _further_
  148. * load returns the same module handle, we immediately
  149. * free it again (to prevent the Windows API's internal
  150. * reference count growing without bound). But on the
  151. * other hand we never free it in ssh_gss_cleanup.
  152. */
  153. if (library_is_in_never_unload_tree(module))
  154. FreeLibrary(module);
  155. add_library_to_never_unload_tree(module);
  156. sfree(dllfile);
  157. sfree(bindir);
  158. sfree(installdir);
  159. }
  160. close_regkey(regkey);
  161. }
  162. if (module) {
  163. struct ssh_gss_library *lib =
  164. &list->libraries[list->nlibraries++];
  165. lib->id = 0;
  166. lib->gsslogmsg = "Using GSSAPI from GSSAPI"MIT_KERB_SUFFIX".DLL";
  167. lib->handle = (void *)module;
  168. #define BIND_GSS_FN(name) \
  169. lib->u.gssapi.name = (t_gss_##name) GetProcAddress(module, "gss_" #name)
  170. BIND_GSS_FN(delete_sec_context);
  171. BIND_GSS_FN(display_status);
  172. BIND_GSS_FN(get_mic);
  173. BIND_GSS_FN(verify_mic);
  174. BIND_GSS_FN(import_name);
  175. BIND_GSS_FN(init_sec_context);
  176. BIND_GSS_FN(release_buffer);
  177. BIND_GSS_FN(release_cred);
  178. BIND_GSS_FN(release_name);
  179. BIND_GSS_FN(acquire_cred);
  180. BIND_GSS_FN(inquire_cred_by_mech);
  181. #undef BIND_GSS_FN
  182. ssh_gssapi_bind_fns(lib);
  183. }
  184. /* Microsoft SSPI Implementation */
  185. module = load_system32_dll("secur32.dll");
  186. if (module) {
  187. struct ssh_gss_library *lib =
  188. &list->libraries[list->nlibraries++];
  189. lib->id = 1;
  190. lib->gsslogmsg = "Using SSPI from SECUR32.DLL";
  191. lib->handle = (void *)module;
  192. /* No typecheck because Winelib thinks one PVOID is a PLUID */
  193. GET_WINDOWS_FUNCTION_NO_TYPECHECK(module, AcquireCredentialsHandleA);
  194. GET_WINDOWS_FUNCTION(module, InitializeSecurityContextA);
  195. GET_WINDOWS_FUNCTION(module, FreeContextBuffer);
  196. GET_WINDOWS_FUNCTION(module, FreeCredentialsHandle);
  197. GET_WINDOWS_FUNCTION(module, DeleteSecurityContext);
  198. GET_WINDOWS_FUNCTION(module, QueryContextAttributesA);
  199. GET_WINDOWS_FUNCTION(module, MakeSignature);
  200. GET_WINDOWS_FUNCTION(module, VerifySignature);
  201. ssh_sspi_bind_fns(lib);
  202. }
  203. /*
  204. * Custom GSSAPI DLL.
  205. */
  206. module = NULL;
  207. Filename *customlib = conf_get_filename(conf, CONF_ssh_gss_custom);
  208. if (!filename_is_null(customlib)) {
  209. const wchar_t *path = customlib->wpath;
  210. if (p_AddDllDirectory) {
  211. /* Add the custom directory as well in case it chainloads
  212. * some other DLLs (e.g a non-installed MIT Kerberos
  213. * instance) */
  214. int pathlen = wcslen(path);
  215. while (pathlen > 0 && path[pathlen-1] != L':' &&
  216. path[pathlen-1] != L'\\')
  217. pathlen--;
  218. if (pathlen > 0 && path[pathlen-1] != L'\\')
  219. pathlen--;
  220. if (pathlen > 0) {
  221. wchar_t *dirpath = snewn(pathlen + 1, wchar_t);
  222. memcpy(dirpath, path, pathlen * sizeof(wchar_t));
  223. dirpath[pathlen] = L'\0';
  224. p_AddDllDirectory(dirpath);
  225. sfree(dirpath);
  226. }
  227. }
  228. module = LoadLibraryExW(path, NULL,
  229. LOAD_LIBRARY_SEARCH_SYSTEM32 |
  230. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
  231. LOAD_LIBRARY_SEARCH_USER_DIRS);
  232. // MPEXT
  233. if (!module && logctx) {
  234. char *buf = dupprintf("Cannot load GSSAPI from user-specified library '%s': %s", path, win_strerror(GetLastError()));
  235. logevent(logctx, buf);
  236. sfree(buf);
  237. }
  238. }
  239. if (module) {
  240. struct ssh_gss_library *lib =
  241. &list->libraries[list->nlibraries++];
  242. lib->id = 2;
  243. lib->gsslogmsg = dupprintf("Using GSSAPI from user-specified"
  244. " library '%s'", customlib->cpath);
  245. lib->handle = (void *)module;
  246. #define BIND_GSS_FN(name) \
  247. lib->u.gssapi.name = (t_gss_##name) GetProcAddress(module, "gss_" #name)
  248. BIND_GSS_FN(delete_sec_context);
  249. BIND_GSS_FN(display_status);
  250. BIND_GSS_FN(get_mic);
  251. BIND_GSS_FN(verify_mic);
  252. BIND_GSS_FN(import_name);
  253. BIND_GSS_FN(init_sec_context);
  254. BIND_GSS_FN(release_buffer);
  255. BIND_GSS_FN(release_cred);
  256. BIND_GSS_FN(release_name);
  257. BIND_GSS_FN(acquire_cred);
  258. BIND_GSS_FN(inquire_cred_by_mech);
  259. #undef BIND_GSS_FN
  260. ssh_gssapi_bind_fns(lib);
  261. }
  262. return list;
  263. }
  264. void ssh_gss_cleanup(struct ssh_gss_liblist *list)
  265. {
  266. int i;
  267. /*
  268. * LoadLibrary and FreeLibrary are defined to employ reference
  269. * counting in the case where the same library is repeatedly
  270. * loaded, so even in a multiple-sessions-per-process context
  271. * (not that we currently expect ever to have such a thing on
  272. * Windows) it's safe to naively FreeLibrary everything here
  273. * without worrying about destroying it under the feet of
  274. * another SSH instance still using it.
  275. */
  276. for (i = 0; i < list->nlibraries; i++) {
  277. if (list->libraries[i].id != 0) {
  278. HMODULE module = (HMODULE)list->libraries[i].handle;
  279. if (!library_is_in_never_unload_tree(module))
  280. FreeLibrary(module);
  281. }
  282. if (list->libraries[i].id == 2) {
  283. /* The 'custom' id involves a dynamically allocated message.
  284. * Note that we must cast away the 'const' to free it. */
  285. sfree((char *)list->libraries[i].gsslogmsg);
  286. }
  287. }
  288. sfree(list->libraries);
  289. sfree(list);
  290. }
  291. static Ssh_gss_stat ssh_sspi_indicate_mech(struct ssh_gss_library *lib,
  292. Ssh_gss_buf *mech)
  293. {
  294. *mech = gss_mech_krb5;
  295. return SSH_GSS_OK;
  296. }
  297. static Ssh_gss_stat ssh_sspi_import_name(struct ssh_gss_library *lib,
  298. char *host, Ssh_gss_name *srv_name)
  299. {
  300. char *pStr;
  301. /* Check hostname */
  302. if (host == NULL) return SSH_GSS_FAILURE;
  303. /* copy it into form host/FQDN */
  304. pStr = dupcat("host/", host);
  305. *srv_name = (Ssh_gss_name) pStr;
  306. return SSH_GSS_OK;
  307. }
  308. static Ssh_gss_stat ssh_sspi_acquire_cred(struct ssh_gss_library *lib,
  309. Ssh_gss_ctx *ctx,
  310. time_t *expiry)
  311. {
  312. winSsh_gss_ctx *winctx = snew(winSsh_gss_ctx);
  313. memset(winctx, 0, sizeof(winSsh_gss_ctx));
  314. /* prepare our "wrapper" structure */
  315. winctx->maj_stat = winctx->min_stat = SEC_E_OK;
  316. winctx->context_handle = NULL;
  317. /* Specifying no principal name here means use the credentials of
  318. the current logged-in user */
  319. winctx->maj_stat = p_AcquireCredentialsHandleA(NULL,
  320. "Kerberos",
  321. SECPKG_CRED_OUTBOUND,
  322. NULL,
  323. NULL,
  324. NULL,
  325. NULL,
  326. &winctx->cred_handle,
  327. NULL);
  328. if (winctx->maj_stat != SEC_E_OK) {
  329. p_FreeCredentialsHandle(&winctx->cred_handle);
  330. sfree(winctx);
  331. return SSH_GSS_FAILURE;
  332. }
  333. /* Windows does not return a valid expiration from AcquireCredentials */
  334. if (expiry)
  335. *expiry = GSS_NO_EXPIRATION;
  336. *ctx = (Ssh_gss_ctx) winctx;
  337. return SSH_GSS_OK;
  338. }
  339. #ifdef MPEXT
  340. static SecBuffer ssh_gss_init_sec_buffer(unsigned long buffersize,
  341. unsigned long buffertype, void * buffer)
  342. {
  343. SecBuffer result;
  344. result.cbBuffer = buffersize;
  345. result.BufferType = buffertype;
  346. result.pvBuffer = buffer;
  347. return result;
  348. }
  349. static SecBufferDesc ssh_gss_init_sec_buffer_desc(unsigned long version,
  350. unsigned long bufferscount, PSecBuffer buffers)
  351. {
  352. SecBufferDesc result;
  353. result.ulVersion = version;
  354. result.cBuffers = bufferscount;
  355. result.pBuffers = buffers;
  356. return result;
  357. }
  358. #define MPEXT_INIT_SEC_BUFFER(BUFFERSIZE, BUFFERTYPE, BUFFER) \
  359. ssh_gss_init_sec_buffer(BUFFERSIZE, BUFFERTYPE, BUFFER)
  360. #define MPEXT_INIT_SEC_BUFFERDESC(VERSION, BUFFERSCOUNT, BUFFERS) \
  361. ssh_gss_init_sec_buffer_desc(VERSION, BUFFERSCOUNT, BUFFERS)
  362. #else
  363. #define MPEXT_INIT_SEC_BUFFER(BUFFERSIZE, BUFFERTYPE, BUFFER) \
  364. {BUFFERSIZE, BUFFERTYPE, BUFFER}
  365. #define MPEXT_INIT_SEC_BUFFERDESC(VERSION, BUFFERSCOUNT, BUFFERS) \
  366. {VERSION, BUFFERSCOUNT, BUFFERS}
  367. #endif
  368. static void localexp_to_exp_lifetime(TimeStamp *localexp,
  369. time_t *expiry, unsigned long *lifetime)
  370. {
  371. FILETIME nowUTC;
  372. FILETIME expUTC;
  373. time_t now;
  374. time_t exp;
  375. time_t delta;
  376. if (!lifetime && !expiry)
  377. return;
  378. GetSystemTimeAsFileTime(&nowUTC);
  379. TIME_WIN_TO_POSIX(nowUTC, now);
  380. if (lifetime)
  381. *lifetime = 0;
  382. if (expiry)
  383. *expiry = GSS_NO_EXPIRATION;
  384. /*
  385. * Type oddity: localexp is a pointer to 'TimeStamp', whereas
  386. * LocalFileTimeToFileTime expects a pointer to FILETIME. However,
  387. * despite having different formal type names from the compiler's
  388. * point of view, these two structures are specified to be
  389. * isomorphic in the MS documentation, so it's legitimate to copy
  390. * between them:
  391. *
  392. * https://msdn.microsoft.com/en-us/library/windows/desktop/aa380511(v=vs.85).aspx
  393. */
  394. {
  395. FILETIME localexp_ft;
  396. enum { vorpal_sword = 1 / (sizeof(*localexp) == sizeof(localexp_ft)) };
  397. memcpy(&localexp_ft, localexp, sizeof(localexp_ft));
  398. if (!LocalFileTimeToFileTime(&localexp_ft, &expUTC))
  399. return;
  400. }
  401. TIME_WIN_TO_POSIX(expUTC, exp);
  402. delta = exp - now;
  403. if (exp == 0 || delta <= 0)
  404. return;
  405. if (expiry)
  406. *expiry = exp;
  407. if (lifetime) {
  408. if (delta <= ULONG_MAX)
  409. *lifetime = (unsigned long)delta;
  410. else
  411. *lifetime = ULONG_MAX;
  412. }
  413. }
  414. static Ssh_gss_stat ssh_sspi_init_sec_context(struct ssh_gss_library *lib,
  415. Ssh_gss_ctx *ctx,
  416. Ssh_gss_name srv_name,
  417. int to_deleg,
  418. Ssh_gss_buf *recv_tok,
  419. Ssh_gss_buf *send_tok,
  420. time_t *expiry,
  421. unsigned long *lifetime)
  422. {
  423. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) *ctx;
  424. SecBuffer wsend_tok = MPEXT_INIT_SEC_BUFFER(send_tok->length,SECBUFFER_TOKEN,send_tok->value);
  425. SecBuffer wrecv_tok = MPEXT_INIT_SEC_BUFFER(recv_tok->length,SECBUFFER_TOKEN,recv_tok->value);
  426. SecBufferDesc output_desc = MPEXT_INIT_SEC_BUFFERDESC(SECBUFFER_VERSION,1,&wsend_tok);
  427. SecBufferDesc input_desc = MPEXT_INIT_SEC_BUFFERDESC(SECBUFFER_VERSION,1,&wrecv_tok);
  428. unsigned long flags=ISC_REQ_MUTUAL_AUTH|ISC_REQ_REPLAY_DETECT|
  429. ISC_REQ_CONFIDENTIALITY|ISC_REQ_ALLOCATE_MEMORY;
  430. ULONG ret_flags=0;
  431. TimeStamp localexp;
  432. /* check if we have to delegate ... */
  433. if (to_deleg) flags |= ISC_REQ_DELEGATE;
  434. winctx->maj_stat = p_InitializeSecurityContextA(&winctx->cred_handle,
  435. winctx->context_handle,
  436. (char*) srv_name,
  437. flags,
  438. 0, /* reserved */
  439. SECURITY_NATIVE_DREP,
  440. &input_desc,
  441. 0, /* reserved */
  442. &winctx->context,
  443. &output_desc,
  444. &ret_flags,
  445. &localexp);
  446. localexp_to_exp_lifetime(&localexp, expiry, lifetime);
  447. /* prepare for the next round */
  448. winctx->context_handle = &winctx->context;
  449. send_tok->value = wsend_tok.pvBuffer;
  450. send_tok->length = wsend_tok.cbBuffer;
  451. /* check & return our status */
  452. if (winctx->maj_stat==SEC_E_OK) return SSH_GSS_S_COMPLETE;
  453. if (winctx->maj_stat==SEC_I_CONTINUE_NEEDED) return SSH_GSS_S_CONTINUE_NEEDED;
  454. return SSH_GSS_FAILURE;
  455. }
  456. static Ssh_gss_stat ssh_sspi_free_tok(struct ssh_gss_library *lib,
  457. Ssh_gss_buf *send_tok)
  458. {
  459. /* check input */
  460. if (send_tok == NULL) return SSH_GSS_FAILURE;
  461. /* free Windows buffer */
  462. p_FreeContextBuffer(send_tok->value);
  463. SSH_GSS_CLEAR_BUF(send_tok);
  464. return SSH_GSS_OK;
  465. }
  466. static Ssh_gss_stat ssh_sspi_release_cred(struct ssh_gss_library *lib,
  467. Ssh_gss_ctx *ctx)
  468. {
  469. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) *ctx;
  470. /* check input */
  471. if (winctx == NULL) return SSH_GSS_FAILURE;
  472. /* free Windows data */
  473. p_FreeCredentialsHandle(&winctx->cred_handle);
  474. #ifdef MPEXT
  475. if (winctx->context_handle != NULL)
  476. #endif
  477. p_DeleteSecurityContext(&winctx->context);
  478. /* delete our "wrapper" structure */
  479. sfree(winctx);
  480. *ctx = (Ssh_gss_ctx) NULL;
  481. return SSH_GSS_OK;
  482. }
  483. static Ssh_gss_stat ssh_sspi_release_name(struct ssh_gss_library *lib,
  484. Ssh_gss_name *srv_name)
  485. {
  486. char *pStr = (char *) *srv_name;
  487. if (pStr == NULL) return SSH_GSS_FAILURE;
  488. sfree(pStr);
  489. *srv_name = (Ssh_gss_name) NULL;
  490. return SSH_GSS_OK;
  491. }
  492. static Ssh_gss_stat ssh_sspi_display_status(struct ssh_gss_library *lib,
  493. Ssh_gss_ctx ctx, Ssh_gss_buf *buf)
  494. {
  495. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) ctx;
  496. const char *msg;
  497. if (winctx == NULL) return SSH_GSS_FAILURE;
  498. /* decode the error code */
  499. switch (winctx->maj_stat) {
  500. case SEC_E_OK: msg="SSPI status OK"; break;
  501. case SEC_E_INVALID_HANDLE: msg="The handle passed to the function"
  502. " is invalid.";
  503. break;
  504. case SEC_E_TARGET_UNKNOWN: msg="The target was not recognized."; break;
  505. case SEC_E_LOGON_DENIED: msg="The logon failed."; break;
  506. case SEC_E_INTERNAL_ERROR: msg="The Local Security Authority cannot"
  507. " be contacted.";
  508. break;
  509. case SEC_E_NO_CREDENTIALS: msg="No credentials are available in the"
  510. " security package.";
  511. break;
  512. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  513. msg="No authority could be contacted for authentication."
  514. "The domain name of the authenticating party could be wrong,"
  515. " the domain could be unreachable, or there might have been"
  516. " a trust relationship failure.";
  517. break;
  518. case SEC_E_INSUFFICIENT_MEMORY:
  519. msg="One or more of the SecBufferDesc structures passed as"
  520. " an OUT parameter has a buffer that is too small.";
  521. break;
  522. case SEC_E_INVALID_TOKEN:
  523. msg="The error is due to a malformed input token, such as a"
  524. " token corrupted in transit, a token"
  525. " of incorrect size, or a token passed into the wrong"
  526. " security package. Passing a token to"
  527. " the wrong package can happen if client and server did not"
  528. " negotiate the proper security package.";
  529. break;
  530. default:
  531. msg = "Internal SSPI error";
  532. break;
  533. }
  534. buf->value = dupstr(msg);
  535. buf->length = strlen(buf->value);
  536. return SSH_GSS_OK;
  537. }
  538. static Ssh_gss_stat ssh_sspi_get_mic(struct ssh_gss_library *lib,
  539. Ssh_gss_ctx ctx, Ssh_gss_buf *buf,
  540. Ssh_gss_buf *hash)
  541. {
  542. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) ctx;
  543. SecPkgContext_Sizes ContextSizes;
  544. SecBufferDesc InputBufferDescriptor;
  545. SecBuffer InputSecurityToken[2];
  546. if (winctx == NULL) return SSH_GSS_FAILURE;
  547. winctx->maj_stat = 0;
  548. memset(&ContextSizes, 0, sizeof(ContextSizes));
  549. winctx->maj_stat = p_QueryContextAttributesA(&winctx->context,
  550. SECPKG_ATTR_SIZES,
  551. &ContextSizes);
  552. if (winctx->maj_stat != SEC_E_OK ||
  553. ContextSizes.cbMaxSignature == 0)
  554. return winctx->maj_stat;
  555. InputBufferDescriptor.cBuffers = 2;
  556. InputBufferDescriptor.pBuffers = InputSecurityToken;
  557. InputBufferDescriptor.ulVersion = SECBUFFER_VERSION;
  558. InputSecurityToken[0].BufferType = SECBUFFER_DATA;
  559. InputSecurityToken[0].cbBuffer = buf->length;
  560. InputSecurityToken[0].pvBuffer = buf->value;
  561. InputSecurityToken[1].BufferType = SECBUFFER_TOKEN;
  562. InputSecurityToken[1].cbBuffer = ContextSizes.cbMaxSignature;
  563. InputSecurityToken[1].pvBuffer = snewn(ContextSizes.cbMaxSignature, char);
  564. winctx->maj_stat = p_MakeSignature(&winctx->context,
  565. 0,
  566. &InputBufferDescriptor,
  567. 0);
  568. if (winctx->maj_stat == SEC_E_OK) {
  569. hash->length = InputSecurityToken[1].cbBuffer;
  570. hash->value = InputSecurityToken[1].pvBuffer;
  571. }
  572. return winctx->maj_stat;
  573. }
  574. static Ssh_gss_stat ssh_sspi_verify_mic(struct ssh_gss_library *lib,
  575. Ssh_gss_ctx ctx,
  576. Ssh_gss_buf *buf,
  577. Ssh_gss_buf *mic)
  578. {
  579. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) ctx;
  580. SecBufferDesc InputBufferDescriptor;
  581. SecBuffer InputSecurityToken[2];
  582. ULONG qop;
  583. if (winctx == NULL) return SSH_GSS_FAILURE;
  584. winctx->maj_stat = 0;
  585. InputBufferDescriptor.cBuffers = 2;
  586. InputBufferDescriptor.pBuffers = InputSecurityToken;
  587. InputBufferDescriptor.ulVersion = SECBUFFER_VERSION;
  588. InputSecurityToken[0].BufferType = SECBUFFER_DATA;
  589. InputSecurityToken[0].cbBuffer = buf->length;
  590. InputSecurityToken[0].pvBuffer = buf->value;
  591. InputSecurityToken[1].BufferType = SECBUFFER_TOKEN;
  592. InputSecurityToken[1].cbBuffer = mic->length;
  593. InputSecurityToken[1].pvBuffer = mic->value;
  594. winctx->maj_stat = p_VerifySignature(&winctx->context,
  595. &InputBufferDescriptor,
  596. 0, &qop);
  597. return winctx->maj_stat;
  598. }
  599. static Ssh_gss_stat ssh_sspi_free_mic(struct ssh_gss_library *lib,
  600. Ssh_gss_buf *hash)
  601. {
  602. sfree(hash->value);
  603. return SSH_GSS_OK;
  604. }
  605. static void ssh_sspi_bind_fns(struct ssh_gss_library *lib)
  606. {
  607. lib->indicate_mech = ssh_sspi_indicate_mech;
  608. lib->import_name = ssh_sspi_import_name;
  609. lib->release_name = ssh_sspi_release_name;
  610. lib->init_sec_context = ssh_sspi_init_sec_context;
  611. lib->free_tok = ssh_sspi_free_tok;
  612. lib->acquire_cred = ssh_sspi_acquire_cred;
  613. lib->release_cred = ssh_sspi_release_cred;
  614. lib->get_mic = ssh_sspi_get_mic;
  615. lib->verify_mic = ssh_sspi_verify_mic;
  616. lib->free_mic = ssh_sspi_free_mic;
  617. lib->display_status = ssh_sspi_display_status;
  618. }
  619. #else
  620. /* Dummy function so this source file defines something if NO_GSSAPI
  621. is defined. */
  622. void ssh_gss_init(void)
  623. {
  624. }
  625. #endif
  626. #ifdef WINSCP
  627. void wingss_cleanup(void)
  628. {
  629. if (libraries_to_never_unload != NULL)
  630. {
  631. freetree234(libraries_to_never_unload);
  632. libraries_to_never_unload = NULL;
  633. }
  634. }
  635. #endif