gss.c 26 KB

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