Annotation of src/usr.bin/ssh/auth2-gss.c, Revision 1.28
1.28 ! djm 1: /* $OpenBSD: auth2-gss.c,v 1.27 2018/07/09 21:37:55 markus Exp $ */
1.1 markus 2:
3: /*
4: * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.16 dtucker 27: #ifdef GSSAPI
28:
1.15 deraadt 29: #include <sys/types.h>
1.1 markus 30:
1.15 deraadt 31: #include "xmalloc.h"
1.27 markus 32: #include "sshkey.h"
1.15 deraadt 33: #include "hostfile.h"
1.1 markus 34: #include "auth.h"
35: #include "ssh2.h"
36: #include "log.h"
37: #include "dispatch.h"
1.27 markus 38: #include "sshbuf.h"
39: #include "ssherr.h"
1.1 markus 40: #include "servconf.h"
41: #include "packet.h"
1.15 deraadt 42: #include "ssh-gss.h"
1.1 markus 43: #include "monitor_wrap.h"
44:
45: extern ServerOptions options;
46:
1.24 markus 47: static int input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh);
48: static int input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh);
49: static int input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh);
50: static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
1.1 markus 51:
52: /*
53: * We only support those mechanisms that we know about (ie ones that we know
1.12 stevesk 54: * how to check local user kuserok and the like)
1.1 markus 55: */
56: static int
1.25 markus 57: userauth_gssapi(struct ssh *ssh)
1.1 markus 58: {
1.25 markus 59: Authctxt *authctxt = ssh->authctxt;
1.8 avsm 60: gss_OID_desc goid = {0, NULL};
1.1 markus 61: Gssctxt *ctxt = NULL;
1.27 markus 62: int r, present;
63: u_int mechs;
1.1 markus 64: OM_uint32 ms;
1.27 markus 65: size_t len;
1.9 djm 66: u_char *doid = NULL;
1.1 markus 67:
68: if (!authctxt->valid || authctxt->user == NULL)
69: return (0);
70:
1.27 markus 71: if ((r = sshpkt_get_u32(ssh, &mechs)) != 0)
72: fatal("%s: %s", __func__, ssh_err(r));
73:
1.1 markus 74: if (mechs == 0) {
75: debug("Mechanism negotiation is not supported");
76: return (0);
77: }
78:
79: do {
80: mechs--;
81:
1.20 djm 82: free(doid);
1.1 markus 83:
1.5 markus 84: present = 0;
1.27 markus 85: if ((r = sshpkt_get_string(ssh, &doid, &len)) != 0)
86: fatal("%s: %s", __func__, ssh_err(r));
1.1 markus 87:
1.10 djm 88: if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
89: doid[1] == len - 2) {
1.8 avsm 90: goid.elements = doid + 2;
91: goid.length = len - 2;
1.21 djm 92: ssh_gssapi_test_oid_supported(&ms, &goid, &present);
1.1 markus 93: } else {
1.5 markus 94: logit("Badly formed OID received");
1.1 markus 95: }
96: } while (mechs > 0 && !present);
97:
98: if (!present) {
1.20 djm 99: free(doid);
1.17 djm 100: authctxt->server_caused_failure = 1;
1.1 markus 101: return (0);
102: }
103:
1.8 avsm 104: if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
1.13 djm 105: if (ctxt != NULL)
106: ssh_gssapi_delete_ctx(&ctxt);
1.20 djm 107: free(doid);
1.17 djm 108: authctxt->server_caused_failure = 1;
1.1 markus 109: return (0);
1.3 markus 110: }
1.1 markus 111:
1.12 stevesk 112: authctxt->methoddata = (void *)ctxt;
1.1 markus 113:
1.5 markus 114: /* Return the OID that we received */
1.27 markus 115: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE)) != 0 ||
116: (r = sshpkt_put_string(ssh, doid, len)) != 0 ||
117: (r = sshpkt_send(ssh)) != 0)
118: fatal("%s: %s", __func__, ssh_err(r));
1.1 markus 119:
1.20 djm 120: free(doid);
1.1 markus 121:
1.25 markus 122: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
123: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
1.1 markus 124: authctxt->postponed = 1;
125:
126: return (0);
127: }
128:
1.22 markus 129: static int
1.24 markus 130: input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
1.1 markus 131: {
1.23 markus 132: Authctxt *authctxt = ssh->authctxt;
1.1 markus 133: Gssctxt *gssctxt;
134: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
135: gss_buffer_desc recv_tok;
1.6 markus 136: OM_uint32 maj_status, min_status, flags;
1.27 markus 137: u_char *p;
138: size_t len;
139: int r;
1.1 markus 140:
141: if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
142: fatal("No authentication or GSSAPI context");
143:
144: gssctxt = authctxt->methoddata;
1.27 markus 145: if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
146: (r = sshpkt_get_end(ssh)) != 0)
147: fatal("%s: %s", __func__, ssh_err(r));
1.1 markus 148:
1.27 markus 149: recv_tok.value = p;
150: recv_tok.length = len;
1.1 markus 151: maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
1.6 markus 152: &send_tok, &flags));
1.1 markus 153:
1.27 markus 154: free(p);
1.1 markus 155:
156: if (GSS_ERROR(maj_status)) {
157: if (send_tok.length != 0) {
1.27 markus 158: if ((r = sshpkt_start(ssh,
159: SSH2_MSG_USERAUTH_GSSAPI_ERRTOK)) != 0 ||
160: (r = sshpkt_put_string(ssh, send_tok.value,
161: send_tok.length)) != 0 ||
162: (r = sshpkt_send(ssh)) != 0)
163: fatal("%s: %s", __func__, ssh_err(r));
1.1 markus 164: }
165: authctxt->postponed = 0;
1.25 markus 166: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
167: userauth_finish(ssh, 0, "gssapi-with-mic", NULL);
1.1 markus 168: } else {
169: if (send_tok.length != 0) {
1.27 markus 170: if ((r = sshpkt_start(ssh,
171: SSH2_MSG_USERAUTH_GSSAPI_TOKEN)) != 0 ||
172: (r = sshpkt_put_string(ssh, send_tok.value,
173: send_tok.length)) != 0 ||
174: (r = sshpkt_send(ssh)) != 0)
175: fatal("%s: %s", __func__, ssh_err(r));
1.1 markus 176: }
177: if (maj_status == GSS_S_COMPLETE) {
1.25 markus 178: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
1.6 markus 179: if (flags & GSS_C_INTEG_FLAG)
1.25 markus 180: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC,
1.6 markus 181: &input_gssapi_mic);
182: else
1.25 markus 183: ssh_dispatch_set(ssh,
1.6 markus 184: SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
185: &input_gssapi_exchange_complete);
1.1 markus 186: }
187: }
188:
189: gss_release_buffer(&min_status, &send_tok);
1.22 markus 190: return 0;
1.1 markus 191: }
192:
1.22 markus 193: static int
1.24 markus 194: input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
1.1 markus 195: {
1.23 markus 196: Authctxt *authctxt = ssh->authctxt;
1.1 markus 197: Gssctxt *gssctxt;
198: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
199: gss_buffer_desc recv_tok;
200: OM_uint32 maj_status;
1.27 markus 201: int r;
1.28 ! djm 202: u_char *p;
! 203: size_t len;
1.1 markus 204:
205: if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
206: fatal("No authentication or GSSAPI context");
207:
208: gssctxt = authctxt->methoddata;
1.28 ! djm 209: if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
1.27 markus 210: (r = sshpkt_get_end(ssh)) != 0)
211: fatal("%s: %s", __func__, ssh_err(r));
1.28 ! djm 212: recv_tok.value = p;
! 213: recv_tok.length = len;
1.1 markus 214:
215: /* Push the error token into GSSAPI to see what it says */
216: maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
217: &send_tok, NULL));
218:
1.20 djm 219: free(recv_tok.value);
1.1 markus 220:
221: /* We can't return anything to the client, even if we wanted to */
1.25 markus 222: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
223: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
1.1 markus 224:
225: /* The client will have already moved on to the next auth */
226:
227: gss_release_buffer(&maj_status, &send_tok);
1.22 markus 228: return 0;
1.1 markus 229: }
230:
231: /*
232: * This is called when the client thinks we've completed authentication.
233: * It should only be enabled in the dispatch handler by the function above,
234: * which only enables it once the GSSAPI exchange is complete.
235: */
236:
1.22 markus 237: static int
1.24 markus 238: input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh)
1.1 markus 239: {
1.23 markus 240: Authctxt *authctxt = ssh->authctxt;
1.28 ! djm 241: int r, authenticated;
1.26 djm 242: const char *displayname;
1.1 markus 243:
244: if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
245: fatal("No authentication or GSSAPI context");
246:
247: /*
1.6 markus 248: * We don't need to check the status, because we're only enabled in
249: * the dispatcher once the exchange is complete
1.1 markus 250: */
251:
1.27 markus 252: if ((r = sshpkt_get_end(ssh)) != 0)
253: fatal("%s: %s", __func__, ssh_err(r));
1.1 markus 254:
255: authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
256:
1.26 djm 257: if ((!use_privsep || mm_is_monitor()) &&
258: (displayname = ssh_gssapi_displayname()) != NULL)
259: auth2_record_info(authctxt, "%s", displayname);
260:
1.1 markus 261: authctxt->postponed = 0;
1.25 markus 262: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
263: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
264: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
265: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
266: userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
1.22 markus 267: return 0;
1.6 markus 268: }
269:
1.22 markus 270: static int
1.24 markus 271: input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh)
1.6 markus 272: {
1.23 markus 273: Authctxt *authctxt = ssh->authctxt;
1.6 markus 274: Gssctxt *gssctxt;
1.27 markus 275: int r, authenticated = 0;
276: struct sshbuf *b;
1.6 markus 277: gss_buffer_desc mic, gssbuf;
1.26 djm 278: const char *displayname;
1.28 ! djm 279: u_char *p;
! 280: size_t len;
1.7 djm 281:
1.6 markus 282: if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
283: fatal("No authentication or GSSAPI context");
1.7 djm 284:
1.6 markus 285: gssctxt = authctxt->methoddata;
1.7 djm 286:
1.28 ! djm 287: if ((r = sshpkt_get_string(ssh, &p, &len)) != 0)
1.27 markus 288: fatal("%s: %s", __func__, ssh_err(r));
289: if ((b = sshbuf_new()) == NULL)
290: fatal("%s: sshbuf_new failed", __func__);
1.28 ! djm 291: mic.value = p;
! 292: mic.length = len;
1.27 markus 293: ssh_gssapi_buildmic(b, authctxt->user, authctxt->service,
1.6 markus 294: "gssapi-with-mic");
1.7 djm 295:
1.27 markus 296: if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
297: fatal("%s: sshbuf_mutable_ptr failed", __func__);
298: gssbuf.length = sshbuf_len(b);
1.7 djm 299:
1.6 markus 300: if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
301: authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
302: else
303: logit("GSSAPI MIC check failed");
304:
1.27 markus 305: sshbuf_free(b);
1.20 djm 306: free(mic.value);
1.26 djm 307:
308: if ((!use_privsep || mm_is_monitor()) &&
309: (displayname = ssh_gssapi_displayname()) != NULL)
310: auth2_record_info(authctxt, "%s", displayname);
1.7 djm 311:
1.6 markus 312: authctxt->postponed = 0;
1.25 markus 313: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
314: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
315: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
316: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
317: userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL);
1.22 markus 318: return 0;
1.1 markus 319: }
320:
321: Authmethod method_gssapi = {
1.6 markus 322: "gssapi-with-mic",
1.1 markus 323: userauth_gssapi,
324: &options.gss_authentication
325: };
1.16 dtucker 326: #endif