gssc.c 9.9 KB

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