1 /* $OpenBSD: auth2-gss.c,v 1.41 2026/07/06 07:53:30 djm Exp $ */ 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 27 #include "includes.h" 28 29 #ifdef GSSAPI 30 31 #include <sys/types.h> 32 33 #include <stdarg.h> 34 35 #include "xmalloc.h" 36 #include "sshkey.h" 37 #include "hostfile.h" 38 #include "auth.h" 39 #include "ssh2.h" 40 #include "log.h" 41 #include "dispatch.h" 42 #include "sshbuf.h" 43 #include "ssherr.h" 44 #include "misc.h" 45 #include "servconf.h" 46 #include "packet.h" 47 #include "kex.h" 48 #include "ssh-gss.h" 49 #include "monitor_wrap.h" 50 51 #define SSH_GSSAPI_MAX_MECHS 2048 52 53 extern ServerOptions options; 54 extern struct authmethod_cfg methodcfg_gssapi; 55 56 static int input_gssapi_token(int type, uint32_t plen, struct ssh *ssh); 57 static int input_gssapi_mic(int type, uint32_t plen, struct ssh *ssh); 58 static int input_gssapi_exchange_complete(int type, uint32_t plen, struct ssh *ssh); 59 static int input_gssapi_errtok(int, uint32_t, struct ssh *); 60 61 /* 62 * We only support those mechanisms that we know about (ie ones that we know 63 * how to check local user kuserok and the like) 64 */ 65 static int 66 userauth_gssapi(struct ssh *ssh, const char *method) 67 { 68 Authctxt *authctxt = ssh->authctxt; 69 gss_OID_desc goid = {0, NULL}; 70 Gssctxt *ctxt = NULL; 71 int r, present; 72 u_int mechs; 73 OM_uint32 ms; 74 size_t len; 75 u_char *doid = NULL; 76 77 if ((r = sshpkt_get_u32(ssh, &mechs)) != 0) 78 fatal_fr(r, "parse packet"); 79 80 if (mechs == 0) { 81 logit_f("mechanism negotiation is not supported"); 82 return (0); 83 } else if (mechs > SSH_GSSAPI_MAX_MECHS) { 84 logit_f("too many mechanisms requested %u > %u", mechs, 85 SSH_GSSAPI_MAX_MECHS); 86 return (0); 87 } 88 89 do { 90 mechs--; 91 92 free(doid); 93 94 present = 0; 95 if ((r = sshpkt_get_string(ssh, &doid, &len)) != 0) 96 fatal_fr(r, "parse oid"); 97 98 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE && 99 doid[1] == len - 2) { 100 goid.elements = doid + 2; 101 goid.length = len - 2; 102 ssh_gssapi_test_oid_supported(&ms, &goid, &present); 103 } else { 104 logit_f("badly formed OID received"); 105 } 106 } while (mechs > 0 && !present); 107 108 if (!present) { 109 free(doid); 110 authctxt->server_caused_failure = 1; 111 return (0); 112 } 113 114 if (GSS_ERROR(mm_ssh_gssapi_server_ctx(&ctxt, &goid))) { 115 if (ctxt != NULL) 116 ssh_gssapi_delete_ctx(&ctxt); 117 free(doid); 118 authctxt->server_caused_failure = 1; 119 return (0); 120 } 121 122 authctxt->methoddata = (void *)ctxt; 123 124 /* Return the OID that we received */ 125 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE)) != 0 || 126 (r = sshpkt_put_string(ssh, doid, len)) != 0 || 127 (r = sshpkt_send(ssh)) != 0) 128 fatal_fr(r, "send packet"); 129 130 free(doid); 131 132 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); 133 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); 134 authctxt->postponed = 1; 135 136 return (0); 137 } 138 139 static int 140 input_gssapi_token(int type, uint32_t plen, struct ssh *ssh) 141 { 142 Authctxt *authctxt = ssh->authctxt; 143 Gssctxt *gssctxt; 144 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 145 gss_buffer_desc recv_tok; 146 OM_uint32 maj_status, min_status, flags; 147 u_char *p; 148 size_t len; 149 int r; 150 151 if (authctxt == NULL) 152 fatal("No authentication or GSSAPI context"); 153 154 gssctxt = authctxt->methoddata; 155 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 || 156 (r = sshpkt_get_end(ssh)) != 0) 157 fatal_fr(r, "parse packet"); 158 159 recv_tok.value = p; 160 recv_tok.length = len; 161 maj_status = mm_ssh_gssapi_accept_ctx(gssctxt, &recv_tok, 162 &send_tok, &flags); 163 164 free(p); 165 166 if (GSS_ERROR(maj_status)) { 167 if (send_tok.length != 0) { 168 if ((r = sshpkt_start(ssh, 169 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK)) != 0 || 170 (r = sshpkt_put_string(ssh, send_tok.value, 171 send_tok.length)) != 0 || 172 (r = sshpkt_send(ssh)) != 0) 173 fatal_fr(r, "send ERRTOK packet"); 174 } 175 logit("Failed gssapi-with-mic for %s%.100s " 176 "from %.200s port %d ssh2", 177 authctxt->valid ? "" : "invalid user ", 178 authctxt->user, 179 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 180 authctxt->postponed = 0; 181 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 182 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); 183 userauth_finish(ssh, 0, "gssapi-with-mic", NULL); 184 } else { 185 if (send_tok.length != 0) { 186 if ((r = sshpkt_start(ssh, 187 SSH2_MSG_USERAUTH_GSSAPI_TOKEN)) != 0 || 188 (r = sshpkt_put_string(ssh, send_tok.value, 189 send_tok.length)) != 0 || 190 (r = sshpkt_send(ssh)) != 0) 191 fatal_fr(r, "send TOKEN packet"); 192 } 193 if (maj_status == GSS_S_COMPLETE) { 194 ssh_dispatch_set(ssh, 195 SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 196 /* note: keep ERRTOK handler as per RFC 4462 s3.4 */ 197 if (flags & GSS_C_INTEG_FLAG) { 198 ssh_dispatch_set(ssh, 199 SSH2_MSG_USERAUTH_GSSAPI_MIC, 200 &input_gssapi_mic); 201 } else { 202 ssh_dispatch_set(ssh, 203 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, 204 &input_gssapi_exchange_complete); 205 } 206 } 207 } 208 209 gss_release_buffer(&min_status, &send_tok); 210 return 0; 211 } 212 213 static int 214 input_gssapi_errtok(int type, uint32_t plen, struct ssh *ssh) 215 { 216 Authctxt *authctxt = ssh->authctxt; 217 int r; 218 u_char *p; 219 size_t len; 220 221 if (authctxt == NULL) 222 fatal("No authentication or GSSAPI context"); 223 224 /* Minimal error handling - just cancel auth and return FAILURE */ 225 if ((r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0 || 226 (r = sshpkt_get_end(ssh)) != 0) 227 fatal_fr(r, "parse packet"); 228 229 logit("Failed gssapi-with-mic for %s%.100s from %.200s port %d ssh2", 230 authctxt->valid ? "" : "invalid user ", 231 authctxt->user, 232 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 233 authctxt->postponed = 0; 234 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 235 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); 236 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL); 237 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 238 userauth_finish(ssh, 0, "gssapi-with-mic", NULL); 239 return 0; 240 } 241 242 /* 243 * This is called when the client thinks we've completed authentication. 244 * It should only be enabled in the dispatch handler by the function above, 245 * which only enables it once the GSSAPI exchange is complete. 246 */ 247 248 static int 249 input_gssapi_exchange_complete(int type, uint32_t plen, struct ssh *ssh) 250 { 251 Authctxt *authctxt = ssh->authctxt; 252 int r, authenticated; 253 double tstart = monotime_double(); 254 255 if (authctxt == NULL) 256 fatal("No authentication or GSSAPI context"); 257 258 /* 259 * We don't need to check the status, because we're only enabled in 260 * the dispatcher once the exchange is complete 261 */ 262 263 if ((r = sshpkt_get_end(ssh)) != 0) 264 fatal_fr(r, "parse packet"); 265 266 authenticated = mm_ssh_gssapi_userok(authctxt->user); 267 if (!authenticated) 268 auth_failure_delay(authctxt, tstart); 269 270 authctxt->postponed = 0; 271 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 272 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); 273 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL); 274 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 275 userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL); 276 return 0; 277 } 278 279 static int 280 input_gssapi_mic(int type, uint32_t plen, struct ssh *ssh) 281 { 282 Authctxt *authctxt = ssh->authctxt; 283 Gssctxt *gssctxt; 284 int r, authenticated = 0; 285 struct sshbuf *b; 286 gss_buffer_desc mic, gssbuf; 287 u_char *p; 288 size_t len; 289 double tstart = monotime_double(); 290 291 if (authctxt == NULL) 292 fatal("No authentication or GSSAPI context"); 293 294 gssctxt = authctxt->methoddata; 295 296 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0) 297 fatal_fr(r, "parse packet"); 298 if ((b = sshbuf_new()) == NULL) 299 fatal_f("sshbuf_new failed"); 300 mic.value = p; 301 mic.length = len; 302 ssh_gssapi_buildmic(b, authctxt->user, authctxt->service, 303 "gssapi-with-mic", ssh->kex->session_id); 304 305 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL) 306 fatal_f("sshbuf_mutable_ptr failed"); 307 gssbuf.length = sshbuf_len(b); 308 309 if (!GSS_ERROR(mm_ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))) 310 authenticated = mm_ssh_gssapi_userok(authctxt->user); 311 else 312 logit("GSSAPI MIC check failed"); 313 314 sshbuf_free(b); 315 free(mic.value); 316 317 if (!authenticated) 318 auth_failure_delay(authctxt, tstart); 319 320 authctxt->postponed = 0; 321 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 322 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL); 323 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL); 324 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 325 userauth_finish(ssh, authenticated, "gssapi-with-mic", NULL); 326 return 0; 327 } 328 329 Authmethod method_gssapi = { 330 &methodcfg_gssapi, 331 userauth_gssapi, 332 }; 333 #endif 334