1
0

wingss.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #ifndef NO_GSSAPI
  2. #include "putty.h"
  3. #define SECURITY_WIN32
  4. #include <security.h>
  5. #include "pgssapi.h"
  6. #include "sshgss.h"
  7. #include "sshgssc.h"
  8. #include "misc.h"
  9. /* Windows code to set up the GSSAPI library list. */
  10. const int ngsslibs = 3;
  11. const char *const gsslibnames[3] = {
  12. "MIT Kerberos GSSAPI32.DLL",
  13. "Microsoft SSPI SECUR32.DLL",
  14. "User-specified GSSAPI DLL",
  15. };
  16. const struct keyvalwhere gsslibkeywords[] = {
  17. { "gssapi32", 0, -1, -1 },
  18. { "sspi", 1, -1, -1 },
  19. { "custom", 2, -1, -1 },
  20. };
  21. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  22. AcquireCredentialsHandleA,
  23. (SEC_CHAR *, SEC_CHAR *, ULONG, PLUID,
  24. PVOID, SEC_GET_KEY_FN, PVOID, PCredHandle, PTimeStamp));
  25. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  26. InitializeSecurityContextA,
  27. (PCredHandle, PCtxtHandle, SEC_CHAR *, ULONG, ULONG,
  28. ULONG, PSecBufferDesc, ULONG, PCtxtHandle,
  29. PSecBufferDesc, PULONG, PTimeStamp));
  30. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  31. FreeContextBuffer,
  32. (PVOID));
  33. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  34. FreeCredentialsHandle,
  35. (PCredHandle));
  36. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  37. DeleteSecurityContext,
  38. (PCtxtHandle));
  39. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  40. QueryContextAttributesA,
  41. (PCtxtHandle, ULONG, PVOID));
  42. DECL_WINDOWS_FUNCTION(static, SECURITY_STATUS,
  43. MakeSignature,
  44. (PCtxtHandle, ULONG, PSecBufferDesc, ULONG));
  45. DECL_WINDOWS_FUNCTION(static, DLL_DIRECTORY_COOKIE,
  46. AddDllDirectory,
  47. (PCWSTR));
  48. typedef struct winSsh_gss_ctx {
  49. unsigned long maj_stat;
  50. unsigned long min_stat;
  51. CredHandle cred_handle;
  52. CtxtHandle context;
  53. PCtxtHandle context_handle;
  54. TimeStamp expiry;
  55. } winSsh_gss_ctx;
  56. const Ssh_gss_buf gss_mech_krb5={9,"\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"};
  57. const char *gsslogmsg = NULL;
  58. static void ssh_sspi_bind_fns(struct ssh_gss_library *lib);
  59. struct ssh_gss_liblist *ssh_gss_setup(Conf *conf)
  60. {
  61. HMODULE module;
  62. HKEY regkey;
  63. struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist);
  64. char *path;
  65. static HMODULE kernel32_module;
  66. if (!kernel32_module) {
  67. kernel32_module = load_system32_dll("kernel32.dll");
  68. }
  69. #if defined _MSC_VER && _MSC_VER < 1900
  70. /* Omit the type-check because older MSVCs don't have this function */
  71. GET_WINDOWS_FUNCTION_NO_TYPECHECK(kernel32_module, AddDllDirectory);
  72. #else
  73. GET_WINDOWS_FUNCTION(kernel32_module, AddDllDirectory);
  74. #endif
  75. list->libraries = snewn(3, struct ssh_gss_library);
  76. list->nlibraries = 0;
  77. /* MIT Kerberos GSSAPI implementation */
  78. /* TODO: For 64-bit builds, check for gssapi64.dll */
  79. module = NULL;
  80. if (RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\MIT\\Kerberos", &regkey)
  81. == ERROR_SUCCESS) {
  82. DWORD type, size;
  83. LONG ret;
  84. char *buffer;
  85. /* Find out the string length */
  86. ret = RegQueryValueEx(regkey, "InstallDir", NULL, &type, NULL, &size);
  87. if (ret == ERROR_SUCCESS && type == REG_SZ) {
  88. buffer = snewn(size + 20, char);
  89. ret = RegQueryValueEx(regkey, "InstallDir", NULL,
  90. &type, (LPBYTE)buffer, &size);
  91. if (ret == ERROR_SUCCESS && type == REG_SZ) {
  92. strcat (buffer, "\\bin");
  93. if(p_AddDllDirectory) {
  94. /* Add MIT Kerberos' path to the DLL search path,
  95. * it loads its own DLLs further down the road */
  96. wchar_t *dllPath =
  97. dup_mb_to_wc(DEFAULT_CODEPAGE, 0, buffer);
  98. p_AddDllDirectory(dllPath);
  99. sfree(dllPath);
  100. }
  101. strcat (buffer, "\\gssapi32.dll");
  102. module = LoadLibraryEx (buffer, NULL,
  103. LOAD_LIBRARY_SEARCH_SYSTEM32 |
  104. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
  105. LOAD_LIBRARY_SEARCH_USER_DIRS);
  106. }
  107. sfree(buffer);
  108. }
  109. RegCloseKey(regkey);
  110. }
  111. if (module) {
  112. struct ssh_gss_library *lib =
  113. &list->libraries[list->nlibraries++];
  114. lib->id = 0;
  115. lib->gsslogmsg = "Using GSSAPI from GSSAPI32.DLL";
  116. lib->handle = (void *)module;
  117. #define BIND_GSS_FN(name) \
  118. lib->u.gssapi.name = (t_gss_##name) GetProcAddress(module, "gss_" #name)
  119. BIND_GSS_FN(delete_sec_context);
  120. BIND_GSS_FN(display_status);
  121. BIND_GSS_FN(get_mic);
  122. BIND_GSS_FN(import_name);
  123. BIND_GSS_FN(init_sec_context);
  124. BIND_GSS_FN(release_buffer);
  125. BIND_GSS_FN(release_cred);
  126. BIND_GSS_FN(release_name);
  127. #undef BIND_GSS_FN
  128. ssh_gssapi_bind_fns(lib);
  129. }
  130. /* Microsoft SSPI Implementation */
  131. module = load_system32_dll("secur32.dll");
  132. if (module) {
  133. struct ssh_gss_library *lib =
  134. &list->libraries[list->nlibraries++];
  135. lib->id = 1;
  136. lib->gsslogmsg = "Using SSPI from SECUR32.DLL";
  137. lib->handle = (void *)module;
  138. GET_WINDOWS_FUNCTION(module, AcquireCredentialsHandleA);
  139. GET_WINDOWS_FUNCTION(module, InitializeSecurityContextA);
  140. GET_WINDOWS_FUNCTION(module, FreeContextBuffer);
  141. GET_WINDOWS_FUNCTION(module, FreeCredentialsHandle);
  142. GET_WINDOWS_FUNCTION(module, DeleteSecurityContext);
  143. GET_WINDOWS_FUNCTION(module, QueryContextAttributesA);
  144. GET_WINDOWS_FUNCTION(module, MakeSignature);
  145. ssh_sspi_bind_fns(lib);
  146. }
  147. /*
  148. * Custom GSSAPI DLL.
  149. */
  150. module = NULL;
  151. path = conf_get_filename(conf, CONF_ssh_gss_custom)->path;
  152. if (*path) {
  153. if(p_AddDllDirectory) {
  154. /* Add the custom directory as well in case it chainloads
  155. * some other DLLs (e.g a non-installed MIT Kerberos
  156. * instance) */
  157. int pathlen = strlen(path);
  158. while (pathlen > 0 && path[pathlen-1] != ':' &&
  159. path[pathlen-1] != '\\')
  160. pathlen--;
  161. if (pathlen > 0 && path[pathlen-1] != '\\')
  162. pathlen--;
  163. if (pathlen > 0) {
  164. char *dirpath = dupprintf("%.*s", pathlen, path);
  165. wchar_t *dllPath = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, dirpath);
  166. p_AddDllDirectory(dllPath);
  167. sfree(dllPath);
  168. sfree(dirpath);
  169. }
  170. }
  171. module = LoadLibraryEx(path, NULL,
  172. LOAD_LIBRARY_SEARCH_SYSTEM32 |
  173. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
  174. LOAD_LIBRARY_SEARCH_USER_DIRS);
  175. }
  176. if (module) {
  177. struct ssh_gss_library *lib =
  178. &list->libraries[list->nlibraries++];
  179. lib->id = 2;
  180. lib->gsslogmsg = dupprintf("Using GSSAPI from user-specified"
  181. " library '%s'", path);
  182. lib->handle = (void *)module;
  183. #define BIND_GSS_FN(name) \
  184. lib->u.gssapi.name = (t_gss_##name) GetProcAddress(module, "gss_" #name)
  185. BIND_GSS_FN(delete_sec_context);
  186. BIND_GSS_FN(display_status);
  187. BIND_GSS_FN(get_mic);
  188. BIND_GSS_FN(import_name);
  189. BIND_GSS_FN(init_sec_context);
  190. BIND_GSS_FN(release_buffer);
  191. BIND_GSS_FN(release_cred);
  192. BIND_GSS_FN(release_name);
  193. #undef BIND_GSS_FN
  194. ssh_gssapi_bind_fns(lib);
  195. }
  196. return list;
  197. }
  198. void ssh_gss_cleanup(struct ssh_gss_liblist *list)
  199. {
  200. int i;
  201. /*
  202. * LoadLibrary and FreeLibrary are defined to employ reference
  203. * counting in the case where the same library is repeatedly
  204. * loaded, so even in a multiple-sessions-per-process context
  205. * (not that we currently expect ever to have such a thing on
  206. * Windows) it's safe to naively FreeLibrary everything here
  207. * without worrying about destroying it under the feet of
  208. * another SSH instance still using it.
  209. */
  210. for (i = 0; i < list->nlibraries; i++) {
  211. FreeLibrary((HMODULE)list->libraries[i].handle);
  212. if (list->libraries[i].id == 2) {
  213. /* The 'custom' id involves a dynamically allocated message.
  214. * Note that we must cast away the 'const' to free it. */
  215. sfree((char *)list->libraries[i].gsslogmsg);
  216. }
  217. }
  218. sfree(list->libraries);
  219. sfree(list);
  220. }
  221. static Ssh_gss_stat ssh_sspi_indicate_mech(struct ssh_gss_library *lib,
  222. Ssh_gss_buf *mech)
  223. {
  224. *mech = gss_mech_krb5;
  225. return SSH_GSS_OK;
  226. }
  227. static Ssh_gss_stat ssh_sspi_import_name(struct ssh_gss_library *lib,
  228. char *host, Ssh_gss_name *srv_name)
  229. {
  230. char *pStr;
  231. /* Check hostname */
  232. if (host == NULL) return SSH_GSS_FAILURE;
  233. /* copy it into form host/FQDN */
  234. pStr = dupcat("host/", host, NULL);
  235. *srv_name = (Ssh_gss_name) pStr;
  236. return SSH_GSS_OK;
  237. }
  238. static Ssh_gss_stat ssh_sspi_acquire_cred(struct ssh_gss_library *lib,
  239. Ssh_gss_ctx *ctx)
  240. {
  241. winSsh_gss_ctx *winctx = snew(winSsh_gss_ctx);
  242. memset(winctx, 0, sizeof(winSsh_gss_ctx));
  243. /* prepare our "wrapper" structure */
  244. winctx->maj_stat = winctx->min_stat = SEC_E_OK;
  245. winctx->context_handle = NULL;
  246. /* Specifying no principal name here means use the credentials of
  247. the current logged-in user */
  248. winctx->maj_stat = p_AcquireCredentialsHandleA(NULL,
  249. "Kerberos",
  250. SECPKG_CRED_OUTBOUND,
  251. NULL,
  252. NULL,
  253. NULL,
  254. NULL,
  255. &winctx->cred_handle,
  256. &winctx->expiry);
  257. if (winctx->maj_stat != SEC_E_OK) return SSH_GSS_FAILURE;
  258. *ctx = (Ssh_gss_ctx) winctx;
  259. return SSH_GSS_OK;
  260. }
  261. static Ssh_gss_stat ssh_sspi_init_sec_context(struct ssh_gss_library *lib,
  262. Ssh_gss_ctx *ctx,
  263. Ssh_gss_name srv_name,
  264. int to_deleg,
  265. Ssh_gss_buf *recv_tok,
  266. Ssh_gss_buf *send_tok)
  267. {
  268. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) *ctx;
  269. SecBuffer wsend_tok = {send_tok->length,SECBUFFER_TOKEN,send_tok->value};
  270. SecBuffer wrecv_tok = {recv_tok->length,SECBUFFER_TOKEN,recv_tok->value};
  271. SecBufferDesc output_desc={SECBUFFER_VERSION,1,&wsend_tok};
  272. SecBufferDesc input_desc ={SECBUFFER_VERSION,1,&wrecv_tok};
  273. unsigned long flags=ISC_REQ_MUTUAL_AUTH|ISC_REQ_REPLAY_DETECT|
  274. ISC_REQ_CONFIDENTIALITY|ISC_REQ_ALLOCATE_MEMORY;
  275. unsigned long ret_flags=0;
  276. /* check if we have to delegate ... */
  277. if (to_deleg) flags |= ISC_REQ_DELEGATE;
  278. winctx->maj_stat = p_InitializeSecurityContextA(&winctx->cred_handle,
  279. winctx->context_handle,
  280. (char*) srv_name,
  281. flags,
  282. 0, /* reserved */
  283. SECURITY_NATIVE_DREP,
  284. &input_desc,
  285. 0, /* reserved */
  286. &winctx->context,
  287. &output_desc,
  288. &ret_flags,
  289. &winctx->expiry);
  290. /* prepare for the next round */
  291. winctx->context_handle = &winctx->context;
  292. send_tok->value = wsend_tok.pvBuffer;
  293. send_tok->length = wsend_tok.cbBuffer;
  294. /* check & return our status */
  295. if (winctx->maj_stat==SEC_E_OK) return SSH_GSS_S_COMPLETE;
  296. if (winctx->maj_stat==SEC_I_CONTINUE_NEEDED) return SSH_GSS_S_CONTINUE_NEEDED;
  297. return SSH_GSS_FAILURE;
  298. }
  299. static Ssh_gss_stat ssh_sspi_free_tok(struct ssh_gss_library *lib,
  300. Ssh_gss_buf *send_tok)
  301. {
  302. /* check input */
  303. if (send_tok == NULL) return SSH_GSS_FAILURE;
  304. /* free Windows buffer */
  305. p_FreeContextBuffer(send_tok->value);
  306. SSH_GSS_CLEAR_BUF(send_tok);
  307. return SSH_GSS_OK;
  308. }
  309. static Ssh_gss_stat ssh_sspi_release_cred(struct ssh_gss_library *lib,
  310. Ssh_gss_ctx *ctx)
  311. {
  312. winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) *ctx;
  313. /* check input */
  314. if (winctx == NULL) return SSH_GSS_FAILURE;
  315. /* free Windows data */
  316. p_FreeCredentialsHandle(&winctx->cred_handle);
  317. p_DeleteSecurityContext(&winctx->context);
  318. /* delete our "wrapper" structure */
  319. sfree(winctx);
  320. *ctx = (Ssh_gss_ctx) NULL;
  321. return SSH_GSS_OK;
  322. }
  323. static Ssh_gss_stat ssh_sspi_release_name(struct ssh_gss_library *lib,
  324. Ssh_gss_name *srv_name)
  325. {
  326. char *pStr= (char *) *srv_name;
  327. if (pStr == NULL) return SSH_GSS_FAILURE;
  328. sfree(pStr);
  329. *srv_name = (Ssh_gss_name) NULL;
  330. return SSH_GSS_OK;
  331. }
  332. static Ssh_gss_stat ssh_sspi_display_status(struct ssh_gss_library *lib,
  333. Ssh_gss_ctx ctx, Ssh_gss_buf *buf)
  334. {
  335. winSsh_gss_ctx *winctx = (winSsh_gss_ctx *) ctx;
  336. const char *msg;
  337. if (winctx == NULL) return SSH_GSS_FAILURE;
  338. /* decode the error code */
  339. switch (winctx->maj_stat) {
  340. case SEC_E_OK: msg="SSPI status OK"; break;
  341. case SEC_E_INVALID_HANDLE: msg="The handle passed to the function"
  342. " is invalid.";
  343. break;
  344. case SEC_E_TARGET_UNKNOWN: msg="The target was not recognized."; break;
  345. case SEC_E_LOGON_DENIED: msg="The logon failed."; break;
  346. case SEC_E_INTERNAL_ERROR: msg="The Local Security Authority cannot"
  347. " be contacted.";
  348. break;
  349. case SEC_E_NO_CREDENTIALS: msg="No credentials are available in the"
  350. " security package.";
  351. break;
  352. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  353. msg="No authority could be contacted for authentication."
  354. "The domain name of the authenticating party could be wrong,"
  355. " the domain could be unreachable, or there might have been"
  356. " a trust relationship failure.";
  357. break;
  358. case SEC_E_INSUFFICIENT_MEMORY:
  359. msg="One or more of the SecBufferDesc structures passed as"
  360. " an OUT parameter has a buffer that is too small.";
  361. break;
  362. case SEC_E_INVALID_TOKEN:
  363. msg="The error is due to a malformed input token, such as a"
  364. " token corrupted in transit, a token"
  365. " of incorrect size, or a token passed into the wrong"
  366. " security package. Passing a token to"
  367. " the wrong package can happen if client and server did not"
  368. " negotiate the proper security package.";
  369. break;
  370. default:
  371. msg = "Internal SSPI error";
  372. break;
  373. }
  374. buf->value = dupstr(msg);
  375. buf->length = strlen(buf->value);
  376. return SSH_GSS_OK;
  377. }
  378. static Ssh_gss_stat ssh_sspi_get_mic(struct ssh_gss_library *lib,
  379. Ssh_gss_ctx ctx, Ssh_gss_buf *buf,
  380. Ssh_gss_buf *hash)
  381. {
  382. winSsh_gss_ctx *winctx= (winSsh_gss_ctx *) ctx;
  383. SecPkgContext_Sizes ContextSizes;
  384. SecBufferDesc InputBufferDescriptor;
  385. SecBuffer InputSecurityToken[2];
  386. if (winctx == NULL) return SSH_GSS_FAILURE;
  387. winctx->maj_stat = 0;
  388. memset(&ContextSizes, 0, sizeof(ContextSizes));
  389. winctx->maj_stat = p_QueryContextAttributesA(&winctx->context,
  390. SECPKG_ATTR_SIZES,
  391. &ContextSizes);
  392. if (winctx->maj_stat != SEC_E_OK ||
  393. ContextSizes.cbMaxSignature == 0)
  394. return winctx->maj_stat;
  395. InputBufferDescriptor.cBuffers = 2;
  396. InputBufferDescriptor.pBuffers = InputSecurityToken;
  397. InputBufferDescriptor.ulVersion = SECBUFFER_VERSION;
  398. InputSecurityToken[0].BufferType = SECBUFFER_DATA;
  399. InputSecurityToken[0].cbBuffer = buf->length;
  400. InputSecurityToken[0].pvBuffer = buf->value;
  401. InputSecurityToken[1].BufferType = SECBUFFER_TOKEN;
  402. InputSecurityToken[1].cbBuffer = ContextSizes.cbMaxSignature;
  403. InputSecurityToken[1].pvBuffer = snewn(ContextSizes.cbMaxSignature, char);
  404. winctx->maj_stat = p_MakeSignature(&winctx->context,
  405. 0,
  406. &InputBufferDescriptor,
  407. 0);
  408. if (winctx->maj_stat == SEC_E_OK) {
  409. hash->length = InputSecurityToken[1].cbBuffer;
  410. hash->value = InputSecurityToken[1].pvBuffer;
  411. }
  412. return winctx->maj_stat;
  413. }
  414. static Ssh_gss_stat ssh_sspi_free_mic(struct ssh_gss_library *lib,
  415. Ssh_gss_buf *hash)
  416. {
  417. sfree(hash->value);
  418. return SSH_GSS_OK;
  419. }
  420. static void ssh_sspi_bind_fns(struct ssh_gss_library *lib)
  421. {
  422. lib->indicate_mech = ssh_sspi_indicate_mech;
  423. lib->import_name = ssh_sspi_import_name;
  424. lib->release_name = ssh_sspi_release_name;
  425. lib->init_sec_context = ssh_sspi_init_sec_context;
  426. lib->free_tok = ssh_sspi_free_tok;
  427. lib->acquire_cred = ssh_sspi_acquire_cred;
  428. lib->release_cred = ssh_sspi_release_cred;
  429. lib->get_mic = ssh_sspi_get_mic;
  430. lib->free_mic = ssh_sspi_free_mic;
  431. lib->display_status = ssh_sspi_display_status;
  432. }
  433. #else
  434. /* Dummy function so this source file defines something if NO_GSSAPI
  435. is defined. */
  436. void ssh_gss_init(void)
  437. {
  438. }
  439. #endif