wingss.c 15 KB

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