wingss.c 15 KB

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