gss.c 24 KB

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