gss.c 26 KB

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