gssc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "putty.h"
  2. #include <string.h>
  3. #include <limits.h>
  4. #include "gssc.h"
  5. #include "misc.h"
  6. #ifndef NO_GSSAPI
  7. static Ssh_gss_stat ssh_gssapi_indicate_mech(struct ssh_gss_library *lib,
  8. Ssh_gss_buf *mech)
  9. {
  10. /* Copy constant into mech */
  11. mech->length = GSS_MECH_KRB5->length;
  12. mech->value = GSS_MECH_KRB5->elements;
  13. return SSH_GSS_OK;
  14. }
  15. static Ssh_gss_stat ssh_gssapi_import_name(struct ssh_gss_library *lib,
  16. char *host,
  17. Ssh_gss_name *srv_name)
  18. {
  19. struct gssapi_functions *gss = &lib->u.gssapi;
  20. OM_uint32 min_stat,maj_stat;
  21. gss_buffer_desc host_buf;
  22. char *pStr;
  23. pStr = dupcat("host@", host);
  24. host_buf.value = pStr;
  25. host_buf.length = strlen(pStr);
  26. maj_stat = gss->import_name(&min_stat, &host_buf,
  27. GSS_C_NT_HOSTBASED_SERVICE, srv_name);
  28. /* Release buffer */
  29. sfree(pStr);
  30. if (maj_stat == GSS_S_COMPLETE) return SSH_GSS_OK;
  31. return SSH_GSS_FAILURE;
  32. }
  33. static Ssh_gss_stat ssh_gssapi_acquire_cred(struct ssh_gss_library *lib,
  34. Ssh_gss_ctx *ctx,
  35. time_t *expiry)
  36. {
  37. struct gssapi_functions *gss = &lib->u.gssapi;
  38. #ifdef MPEXT
  39. gss_OID_set_desc k5only;
  40. #else
  41. gss_OID_set_desc k5only = { 1, GSS_MECH_KRB5 };
  42. #endif
  43. gss_cred_id_t cred;
  44. OM_uint32 dummy;
  45. OM_uint32 time_rec;
  46. gssapi_ssh_gss_ctx *gssctx = snew(gssapi_ssh_gss_ctx);
  47. #ifdef MPEXT
  48. k5only.count = 1;
  49. k5only.elements = GSS_MECH_KRB5;
  50. #endif
  51. gssctx->ctx = GSS_C_NO_CONTEXT;
  52. gssctx->expiry = 0;
  53. gssctx->maj_stat =
  54. gss->acquire_cred(&gssctx->min_stat, GSS_C_NO_NAME, GSS_C_INDEFINITE,
  55. &k5only, GSS_C_INITIATE, &cred,
  56. (gss_OID_set *)0, &time_rec);
  57. if (gssctx->maj_stat != GSS_S_COMPLETE) {
  58. sfree(gssctx);
  59. return SSH_GSS_FAILURE;
  60. }
  61. /*
  62. * When the credential lifetime is not yet available due to deferred
  63. * processing, gss_acquire_cred should return a 0 lifetime which is
  64. * distinct from GSS_C_INDEFINITE which signals a crential that never
  65. * expires. However, not all implementations get this right, and with
  66. * Kerberos, initiator credentials always expire at some point. So when
  67. * lifetime is 0 or GSS_C_INDEFINITE we call gss_inquire_cred_by_mech() to
  68. * complete deferred processing.
  69. */
  70. if (time_rec == GSS_C_INDEFINITE || time_rec == 0) {
  71. gssctx->maj_stat =
  72. gss->inquire_cred_by_mech(&gssctx->min_stat, cred,
  73. (gss_OID) GSS_MECH_KRB5,
  74. NULL,
  75. &time_rec,
  76. NULL,
  77. NULL);
  78. }
  79. (void) gss->release_cred(&dummy, &cred);
  80. if (gssctx->maj_stat != GSS_S_COMPLETE) {
  81. sfree(gssctx);
  82. return SSH_GSS_FAILURE;
  83. }
  84. if (time_rec != GSS_C_INDEFINITE)
  85. gssctx->expiry = time(NULL) + time_rec;
  86. else
  87. gssctx->expiry = GSS_NO_EXPIRATION;
  88. if (expiry) {
  89. *expiry = gssctx->expiry;
  90. }
  91. *ctx = (Ssh_gss_ctx) gssctx;
  92. return SSH_GSS_OK;
  93. }
  94. static Ssh_gss_stat ssh_gssapi_init_sec_context(struct ssh_gss_library *lib,
  95. Ssh_gss_ctx *ctx,
  96. Ssh_gss_name srv_name,
  97. int to_deleg,
  98. Ssh_gss_buf *recv_tok,
  99. Ssh_gss_buf *send_tok,
  100. time_t *expiry,
  101. unsigned long *lifetime)
  102. {
  103. struct gssapi_functions *gss = &lib->u.gssapi;
  104. gssapi_ssh_gss_ctx *gssctx = (gssapi_ssh_gss_ctx*) *ctx;
  105. OM_uint32 ret_flags;
  106. OM_uint32 lifetime_rec;
  107. if (to_deleg) to_deleg = GSS_C_DELEG_FLAG;
  108. gssctx->maj_stat = gss->init_sec_context(&gssctx->min_stat,
  109. GSS_C_NO_CREDENTIAL,
  110. &gssctx->ctx,
  111. srv_name,
  112. (gss_OID) GSS_MECH_KRB5,
  113. GSS_C_MUTUAL_FLAG |
  114. GSS_C_INTEG_FLAG | to_deleg,
  115. 0,
  116. GSS_C_NO_CHANNEL_BINDINGS,
  117. recv_tok,
  118. NULL, /* ignore mech type */
  119. send_tok,
  120. &ret_flags,
  121. &lifetime_rec);
  122. if (lifetime) {
  123. if (lifetime_rec == GSS_C_INDEFINITE)
  124. *lifetime = ULONG_MAX;
  125. else
  126. *lifetime = lifetime_rec;
  127. }
  128. if (expiry) {
  129. if (lifetime_rec == GSS_C_INDEFINITE)
  130. *expiry = GSS_NO_EXPIRATION;
  131. else
  132. *expiry = time(NULL) + lifetime_rec;
  133. }
  134. if (gssctx->maj_stat == GSS_S_COMPLETE) return SSH_GSS_S_COMPLETE;
  135. if (gssctx->maj_stat == GSS_S_CONTINUE_NEEDED) return SSH_GSS_S_CONTINUE_NEEDED;
  136. return SSH_GSS_FAILURE;
  137. }
  138. static Ssh_gss_stat ssh_gssapi_display_status(struct ssh_gss_library *lib,
  139. Ssh_gss_ctx ctx,
  140. Ssh_gss_buf *buf)
  141. {
  142. struct gssapi_functions *gss = &lib->u.gssapi;
  143. gssapi_ssh_gss_ctx *gssctx = (gssapi_ssh_gss_ctx *) ctx;
  144. OM_uint32 lmin,lmax;
  145. OM_uint32 ccc;
  146. gss_buffer_desc msg_maj=GSS_C_EMPTY_BUFFER;
  147. gss_buffer_desc msg_min=GSS_C_EMPTY_BUFFER;
  148. /* Return empty buffer in case of failure */
  149. SSH_GSS_CLEAR_BUF(buf);
  150. /* get first mesg from GSS */
  151. ccc=0;
  152. lmax=gss->display_status(&lmin,gssctx->maj_stat,GSS_C_GSS_CODE,(gss_OID) GSS_MECH_KRB5,&ccc,&msg_maj);
  153. if (lmax != GSS_S_COMPLETE) return SSH_GSS_FAILURE;
  154. /* get first mesg from Kerberos */
  155. ccc=0;
  156. lmax=gss->display_status(&lmin,gssctx->min_stat,GSS_C_MECH_CODE,(gss_OID) GSS_MECH_KRB5,&ccc,&msg_min);
  157. if (lmax != GSS_S_COMPLETE) {
  158. gss->release_buffer(&lmin, &msg_maj);
  159. return SSH_GSS_FAILURE;
  160. }
  161. /* copy data into buffer */
  162. buf->length = msg_maj.length + msg_min.length + 1;
  163. buf->value = snewn(buf->length + 1, char);
  164. /* copy mem */
  165. memcpy((char *)buf->value, msg_maj.value, msg_maj.length);
  166. ((char *)buf->value)[msg_maj.length] = ' ';
  167. memcpy((char *)buf->value + msg_maj.length + 1, msg_min.value, msg_min.length);
  168. ((char *)buf->value)[buf->length] = 0;
  169. /* free mem & exit */
  170. gss->release_buffer(&lmin, &msg_maj);
  171. gss->release_buffer(&lmin, &msg_min);
  172. return SSH_GSS_OK;
  173. }
  174. static Ssh_gss_stat ssh_gssapi_free_tok(struct ssh_gss_library *lib,
  175. Ssh_gss_buf *send_tok)
  176. {
  177. struct gssapi_functions *gss = &lib->u.gssapi;
  178. OM_uint32 min_stat,maj_stat;
  179. maj_stat = gss->release_buffer(&min_stat, send_tok);
  180. if (maj_stat == GSS_S_COMPLETE) return SSH_GSS_OK;
  181. return SSH_GSS_FAILURE;
  182. }
  183. static Ssh_gss_stat ssh_gssapi_release_cred(struct ssh_gss_library *lib,
  184. Ssh_gss_ctx *ctx)
  185. {
  186. struct gssapi_functions *gss = &lib->u.gssapi;
  187. gssapi_ssh_gss_ctx *gssctx = (gssapi_ssh_gss_ctx *) *ctx;
  188. OM_uint32 min_stat;
  189. OM_uint32 maj_stat=GSS_S_COMPLETE;
  190. if (gssctx == NULL) return SSH_GSS_FAILURE;
  191. if (gssctx->ctx != GSS_C_NO_CONTEXT)
  192. maj_stat = gss->delete_sec_context(&min_stat,&gssctx->ctx,GSS_C_NO_BUFFER);
  193. sfree(gssctx);
  194. *ctx = NULL;
  195. if (maj_stat == GSS_S_COMPLETE) return SSH_GSS_OK;
  196. return SSH_GSS_FAILURE;
  197. }
  198. static Ssh_gss_stat ssh_gssapi_release_name(struct ssh_gss_library *lib,
  199. Ssh_gss_name *srv_name)
  200. {
  201. struct gssapi_functions *gss = &lib->u.gssapi;
  202. OM_uint32 min_stat,maj_stat;
  203. maj_stat = gss->release_name(&min_stat, srv_name);
  204. if (maj_stat == GSS_S_COMPLETE) return SSH_GSS_OK;
  205. return SSH_GSS_FAILURE;
  206. }
  207. static Ssh_gss_stat ssh_gssapi_get_mic(struct ssh_gss_library *lib,
  208. Ssh_gss_ctx ctx, Ssh_gss_buf *buf,
  209. Ssh_gss_buf *hash)
  210. {
  211. struct gssapi_functions *gss = &lib->u.gssapi;
  212. gssapi_ssh_gss_ctx *gssctx = (gssapi_ssh_gss_ctx *) ctx;
  213. if (gssctx == NULL) return SSH_GSS_FAILURE;
  214. return gss->get_mic(&(gssctx->min_stat), gssctx->ctx, 0, buf, hash);
  215. }
  216. static Ssh_gss_stat ssh_gssapi_verify_mic(struct ssh_gss_library *lib,
  217. Ssh_gss_ctx ctx, Ssh_gss_buf *buf,
  218. Ssh_gss_buf *hash)
  219. {
  220. struct gssapi_functions *gss = &lib->u.gssapi;
  221. gssapi_ssh_gss_ctx *gssctx = (gssapi_ssh_gss_ctx *) ctx;
  222. if (gssctx == NULL) return SSH_GSS_FAILURE;
  223. return gss->verify_mic(&(gssctx->min_stat), gssctx->ctx, buf, hash, NULL);
  224. }
  225. static Ssh_gss_stat ssh_gssapi_free_mic(struct ssh_gss_library *lib,
  226. Ssh_gss_buf *hash)
  227. {
  228. /* On Unix this is the same freeing process as ssh_gssapi_free_tok. */
  229. return ssh_gssapi_free_tok(lib, hash);
  230. }
  231. void ssh_gssapi_bind_fns(struct ssh_gss_library *lib)
  232. {
  233. lib->indicate_mech = ssh_gssapi_indicate_mech;
  234. lib->import_name = ssh_gssapi_import_name;
  235. lib->release_name = ssh_gssapi_release_name;
  236. lib->init_sec_context = ssh_gssapi_init_sec_context;
  237. lib->free_tok = ssh_gssapi_free_tok;
  238. lib->acquire_cred = ssh_gssapi_acquire_cred;
  239. lib->release_cred = ssh_gssapi_release_cred;
  240. lib->get_mic = ssh_gssapi_get_mic;
  241. lib->verify_mic = ssh_gssapi_verify_mic;
  242. lib->free_mic = ssh_gssapi_free_mic;
  243. lib->display_status = ssh_gssapi_display_status;
  244. }
  245. #else
  246. /* Dummy function so this source file defines something if NO_GSSAPI
  247. is defined. */
  248. int ssh_gssapi_init(void)
  249. {
  250. return 0;
  251. }
  252. #endif