wingss.c 24 KB

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