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