gss.c 26 KB

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