gss-genr.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* $OpenBSD: gss-genr.c,v 1.26 2018/07/10 09:13:30 djm Exp $ */
  2. /*
  3. * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "includes.h"
  26. #ifdef GSSAPI
  27. #include <sys/types.h>
  28. #include <limits.h>
  29. #include <stdarg.h>
  30. #include <string.h>
  31. #include <signal.h>
  32. #include <unistd.h>
  33. #include "xmalloc.h"
  34. #include "ssherr.h"
  35. #include "sshbuf.h"
  36. #include "log.h"
  37. #include "ssh2.h"
  38. #include "ssh-gss.h"
  39. /* sshbuf_get for gss_buffer_desc */
  40. int
  41. ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
  42. {
  43. int r;
  44. u_char *p;
  45. size_t len;
  46. if ((r = sshbuf_get_string(b, &p, &len)) != 0)
  47. return r;
  48. g->value = p;
  49. g->length = len;
  50. return 0;
  51. }
  52. /* Check that the OID in a data stream matches that in the context */
  53. int
  54. ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
  55. {
  56. return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
  57. ctx->oid->length == len &&
  58. memcmp(ctx->oid->elements, data, len) == 0);
  59. }
  60. /* Set the contexts OID from a data stream */
  61. void
  62. ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
  63. {
  64. if (ctx->oid != GSS_C_NO_OID) {
  65. free(ctx->oid->elements);
  66. free(ctx->oid);
  67. }
  68. ctx->oid = xcalloc(1, sizeof(gss_OID_desc));
  69. ctx->oid->length = len;
  70. ctx->oid->elements = xmalloc(len);
  71. memcpy(ctx->oid->elements, data, len);
  72. }
  73. /* Set the contexts OID */
  74. void
  75. ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
  76. {
  77. ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
  78. }
  79. /* All this effort to report an error ... */
  80. void
  81. ssh_gssapi_error(Gssctxt *ctxt)
  82. {
  83. char *s;
  84. s = ssh_gssapi_last_error(ctxt, NULL, NULL);
  85. debug("%s", s);
  86. free(s);
  87. }
  88. char *
  89. ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
  90. OM_uint32 *minor_status)
  91. {
  92. OM_uint32 lmin;
  93. gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
  94. OM_uint32 ctx;
  95. struct sshbuf *b;
  96. char *ret;
  97. int r;
  98. if ((b = sshbuf_new()) == NULL)
  99. fatal("%s: sshbuf_new failed", __func__);
  100. if (major_status != NULL)
  101. *major_status = ctxt->major;
  102. if (minor_status != NULL)
  103. *minor_status = ctxt->minor;
  104. ctx = 0;
  105. /* The GSSAPI error */
  106. do {
  107. gss_display_status(&lmin, ctxt->major,
  108. GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
  109. if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
  110. (r = sshbuf_put_u8(b, '\n')) != 0)
  111. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  112. gss_release_buffer(&lmin, &msg);
  113. } while (ctx != 0);
  114. /* The mechanism specific error */
  115. do {
  116. gss_display_status(&lmin, ctxt->minor,
  117. GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
  118. if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
  119. (r = sshbuf_put_u8(b, '\n')) != 0)
  120. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  121. gss_release_buffer(&lmin, &msg);
  122. } while (ctx != 0);
  123. if ((r = sshbuf_put_u8(b, '\n')) != 0)
  124. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  125. ret = xstrdup((const char *)sshbuf_ptr(b));
  126. sshbuf_free(b);
  127. return (ret);
  128. }
  129. /*
  130. * Initialise our GSSAPI context. We use this opaque structure to contain all
  131. * of the data which both the client and server need to persist across
  132. * {accept,init}_sec_context calls, so that when we do it from the userauth
  133. * stuff life is a little easier
  134. */
  135. void
  136. ssh_gssapi_build_ctx(Gssctxt **ctx)
  137. {
  138. *ctx = xcalloc(1, sizeof (Gssctxt));
  139. (*ctx)->context = GSS_C_NO_CONTEXT;
  140. (*ctx)->name = GSS_C_NO_NAME;
  141. (*ctx)->oid = GSS_C_NO_OID;
  142. (*ctx)->creds = GSS_C_NO_CREDENTIAL;
  143. (*ctx)->client = GSS_C_NO_NAME;
  144. (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
  145. }
  146. /* Delete our context, providing it has been built correctly */
  147. void
  148. ssh_gssapi_delete_ctx(Gssctxt **ctx)
  149. {
  150. OM_uint32 ms;
  151. if ((*ctx) == NULL)
  152. return;
  153. if ((*ctx)->context != GSS_C_NO_CONTEXT)
  154. gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
  155. if ((*ctx)->name != GSS_C_NO_NAME)
  156. gss_release_name(&ms, &(*ctx)->name);
  157. if ((*ctx)->oid != GSS_C_NO_OID) {
  158. free((*ctx)->oid->elements);
  159. free((*ctx)->oid);
  160. (*ctx)->oid = GSS_C_NO_OID;
  161. }
  162. if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
  163. gss_release_cred(&ms, &(*ctx)->creds);
  164. if ((*ctx)->client != GSS_C_NO_NAME)
  165. gss_release_name(&ms, &(*ctx)->client);
  166. if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
  167. gss_release_cred(&ms, &(*ctx)->client_creds);
  168. free(*ctx);
  169. *ctx = NULL;
  170. }
  171. /*
  172. * Wrapper to init_sec_context
  173. * Requires that the context contains:
  174. * oid
  175. * server name (from ssh_gssapi_import_name)
  176. */
  177. OM_uint32
  178. ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
  179. gss_buffer_desc* send_tok, OM_uint32 *flags)
  180. {
  181. int deleg_flag = 0;
  182. if (deleg_creds) {
  183. deleg_flag = GSS_C_DELEG_FLAG;
  184. debug("Delegating credentials");
  185. }
  186. ctx->major = gss_init_sec_context(&ctx->minor,
  187. GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
  188. GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
  189. 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
  190. if (GSS_ERROR(ctx->major))
  191. ssh_gssapi_error(ctx);
  192. return (ctx->major);
  193. }
  194. /* Create a service name for the given host */
  195. OM_uint32
  196. ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
  197. {
  198. gss_buffer_desc gssbuf;
  199. char *val;
  200. xasprintf(&val, "host@%s", host);
  201. gssbuf.value = val;
  202. gssbuf.length = strlen(gssbuf.value);
  203. if ((ctx->major = gss_import_name(&ctx->minor,
  204. &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
  205. ssh_gssapi_error(ctx);
  206. free(gssbuf.value);
  207. return (ctx->major);
  208. }
  209. OM_uint32
  210. ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
  211. {
  212. if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
  213. GSS_C_QOP_DEFAULT, buffer, hash)))
  214. ssh_gssapi_error(ctx);
  215. return (ctx->major);
  216. }
  217. void
  218. ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
  219. const char *context, const struct sshbuf *session_id)
  220. {
  221. int r;
  222. sshbuf_reset(b);
  223. if ((r = sshbuf_put_stringb(b, session_id)) != 0 ||
  224. (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
  225. (r = sshbuf_put_cstring(b, user)) != 0 ||
  226. (r = sshbuf_put_cstring(b, service)) != 0 ||
  227. (r = sshbuf_put_cstring(b, context)) != 0)
  228. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  229. }
  230. int
  231. ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
  232. {
  233. gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
  234. OM_uint32 major, minor;
  235. gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
  236. /* RFC 4462 says we MUST NOT do SPNEGO */
  237. if (oid->length == spnego_oid.length &&
  238. (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
  239. return 0; /* false */
  240. ssh_gssapi_build_ctx(ctx);
  241. ssh_gssapi_set_oid(*ctx, oid);
  242. major = ssh_gssapi_import_name(*ctx, host);
  243. if (!GSS_ERROR(major)) {
  244. major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
  245. NULL);
  246. gss_release_buffer(&minor, &token);
  247. if ((*ctx)->context != GSS_C_NO_CONTEXT)
  248. gss_delete_sec_context(&minor, &(*ctx)->context,
  249. GSS_C_NO_BUFFER);
  250. }
  251. if (GSS_ERROR(major))
  252. ssh_gssapi_delete_ctx(ctx);
  253. return (!GSS_ERROR(major));
  254. }
  255. #endif /* GSSAPI */