wingss.c 14 KB

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