wingss.c 22 KB

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