From b8d9214d969775e409e1408ecdf0d58fad99b344 Mon Sep 17 00:00:00 2001 From: "markus@openbsd.org" Date: Mon, 9 Jul 2018 21:37:55 +0000 Subject: [PATCH] upstream: sshd: switch GSSAPI to sshbuf API; ok djm@ OpenBSD-Commit-ID: e48449ab4be3f006f7ba33c66241b7d652973e30 --- auth2-gss.c | 95 ++++++++++++++++++++++++++++--------------------- gss-genr.c | 46 ++++++++++++++---------- gss-serv-krb5.c | 5 ++- gss-serv.c | 5 ++- 4 files changed, 86 insertions(+), 65 deletions(-) diff --git a/auth2-gss.c b/auth2-gss.c index 589283b72..a6f2a7125 100644 --- a/auth2-gss.c +++ b/auth2-gss.c @@ -1,4 +1,4 @@ -/* $OpenBSD: auth2-gss.c,v 1.26 2017/06/24 06:34:38 djm Exp $ */ +/* $OpenBSD: auth2-gss.c,v 1.27 2018/07/09 21:37:55 markus Exp $ */ /* * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. @@ -33,13 +33,14 @@ #include #include "xmalloc.h" -#include "key.h" +#include "sshkey.h" #include "hostfile.h" #include "auth.h" #include "ssh2.h" #include "log.h" #include "dispatch.h" -#include "buffer.h" +#include "sshbuf.h" +#include "ssherr.h" #include "misc.h" #include "servconf.h" #include "packet.h" @@ -63,16 +64,18 @@ userauth_gssapi(struct ssh *ssh) Authctxt *authctxt = ssh->authctxt; gss_OID_desc goid = {0, NULL}; Gssctxt *ctxt = NULL; - int mechs; - int present; + int r, present; + u_int mechs; OM_uint32 ms; - u_int len; + size_t len; u_char *doid = NULL; if (!authctxt->valid || authctxt->user == NULL) return (0); - mechs = packet_get_int(); + if ((r = sshpkt_get_u32(ssh, &mechs)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); + if (mechs == 0) { debug("Mechanism negotiation is not supported"); return (0); @@ -84,7 +87,8 @@ userauth_gssapi(struct ssh *ssh) free(doid); present = 0; - doid = packet_get_string(&len); + if ((r = sshpkt_get_string(ssh, &doid, &len)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); if (len > 2 && doid[0] == SSH_GSS_OIDTYPE && doid[1] == len - 2) { @@ -112,12 +116,12 @@ userauth_gssapi(struct ssh *ssh) authctxt->methoddata = (void *)ctxt; - packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE); - /* Return the OID that we received */ - packet_put_string(doid, len); + if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE)) != 0 || + (r = sshpkt_put_string(ssh, doid, len)) != 0 || + (r = sshpkt_send(ssh)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); - packet_send(); free(doid); ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); @@ -135,36 +139,45 @@ input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh) gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; gss_buffer_desc recv_tok; OM_uint32 maj_status, min_status, flags; - u_int len; + u_char *p; + size_t len; + int r; if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) fatal("No authentication or GSSAPI context"); gssctxt = authctxt->methoddata; - recv_tok.value = packet_get_string(&len); - recv_tok.length = len; /* u_int vs. size_t */ - - packet_check_eom(); + if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 || + (r = sshpkt_get_end(ssh)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); + recv_tok.value = p; + recv_tok.length = len; maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok, &send_tok, &flags)); - free(recv_tok.value); + free(p); if (GSS_ERROR(maj_status)) { if (send_tok.length != 0) { - packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); - packet_put_string(send_tok.value, send_tok.length); - packet_send(); + if ((r = sshpkt_start(ssh, + SSH2_MSG_USERAUTH_GSSAPI_ERRTOK)) != 0 || + (r = sshpkt_put_string(ssh, send_tok.value, + send_tok.length)) != 0 || + (r = sshpkt_send(ssh)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); } authctxt->postponed = 0; ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); userauth_finish(ssh, 0, "gssapi-with-mic", NULL); } else { if (send_tok.length != 0) { - packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); - packet_put_string(send_tok.value, send_tok.length); - packet_send(); + if ((r = sshpkt_start(ssh, + SSH2_MSG_USERAUTH_GSSAPI_TOKEN)) != 0 || + (r = sshpkt_put_string(ssh, send_tok.value, + send_tok.length)) != 0 || + (r = sshpkt_send(ssh)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); } if (maj_status == GSS_S_COMPLETE) { ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); @@ -190,16 +203,16 @@ input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh) gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; gss_buffer_desc recv_tok; OM_uint32 maj_status; - u_int len; + int r; if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) fatal("No authentication or GSSAPI context"); gssctxt = authctxt->methoddata; - recv_tok.value = packet_get_string(&len); - recv_tok.length = len; - - packet_check_eom(); + if ((r = sshpkt_get_string(ssh, + &recv_tok.value, &recv_tok.length)) != 0 || + (r = sshpkt_get_end(ssh)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); /* Push the error token into GSSAPI to see what it says */ maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok, @@ -238,7 +251,8 @@ input_gssapi_exchange_complete(int type, u_int32_t plen, struct ssh *ssh) * the dispatcher once the exchange is complete */ - packet_check_eom(); + if ((r = sshpkt_get_end(ssh)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user)); @@ -260,10 +274,9 @@ input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh) { Authctxt *authctxt = ssh->authctxt; Gssctxt *gssctxt; - int authenticated = 0; - Buffer b; + int r, authenticated = 0; + struct sshbuf *b; gss_buffer_desc mic, gssbuf; - u_int len; const char *displayname; if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep)) @@ -271,21 +284,23 @@ input_gssapi_mic(int type, u_int32_t plen, struct ssh *ssh) gssctxt = authctxt->methoddata; - mic.value = packet_get_string(&len); - mic.length = len; - - ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service, + if ((r = sshpkt_get_string(ssh, &mic.value, &mic.length)) != 0) + fatal("%s: %s", __func__, ssh_err(r)); + if ((b = sshbuf_new()) == NULL) + fatal("%s: sshbuf_new failed", __func__); + ssh_gssapi_buildmic(b, authctxt->user, authctxt->service, "gssapi-with-mic"); - gssbuf.value = buffer_ptr(&b); - gssbuf.length = buffer_len(&b); + if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL) + fatal("%s: sshbuf_mutable_ptr failed", __func__); + gssbuf.length = sshbuf_len(b); if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic)))) authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user)); else logit("GSSAPI MIC check failed"); - buffer_free(&b); + sshbuf_free(b); free(mic.value); if ((!use_privsep || mm_is_monitor()) && diff --git a/gss-genr.c b/gss-genr.c index 62559ed9e..f794e05b7 100644 --- a/gss-genr.c +++ b/gss-genr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gss-genr.c,v 1.24 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: gss-genr.c,v 1.25 2018/07/09 21:37:55 markus Exp $ */ /* * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved. @@ -37,7 +37,8 @@ #include #include "xmalloc.h" -#include "buffer.h" +#include "ssherr.h" +#include "sshbuf.h" #include "log.h" #include "ssh2.h" @@ -94,10 +95,12 @@ ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status, OM_uint32 lmin; gss_buffer_desc msg = GSS_C_EMPTY_BUFFER; OM_uint32 ctx; - Buffer b; + struct sshbuf *b; char *ret; + int r; - buffer_init(&b); + if ((b = sshbuf_new()) == NULL) + fatal("%s: sshbuf_new failed", __func__); if (major_status != NULL) *major_status = ctxt->major; @@ -110,8 +113,9 @@ ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status, gss_display_status(&lmin, ctxt->major, GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg); - buffer_append(&b, msg.value, msg.length); - buffer_put_char(&b, '\n'); + if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 || + (r = sshbuf_put_u8(b, '\n')) != 0) + fatal("%s: buffer error: %s", __func__, ssh_err(r)); gss_release_buffer(&lmin, &msg); } while (ctx != 0); @@ -121,16 +125,17 @@ ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status, gss_display_status(&lmin, ctxt->minor, GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg); - buffer_append(&b, msg.value, msg.length); - buffer_put_char(&b, '\n'); + if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 || + (r = sshbuf_put_u8(b, '\n')) != 0) + fatal("%s: buffer error: %s", __func__, ssh_err(r)); gss_release_buffer(&lmin, &msg); } while (ctx != 0); - buffer_put_char(&b, '\0'); - ret = xmalloc(buffer_len(&b)); - buffer_get(&b, ret, buffer_len(&b)); - buffer_free(&b); + if ((r = sshbuf_put_u8(b, '\n')) != 0) + fatal("%s: buffer error: %s", __func__, ssh_err(r)); + ret = xstrdup((const char *)sshbuf_ptr(b)); + sshbuf_free(b); return (ret); } @@ -238,15 +243,18 @@ ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash) } void -ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service, +ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service, const char *context) { - buffer_init(b); - buffer_put_string(b, session_id2, session_id2_len); - buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST); - buffer_put_cstring(b, user); - buffer_put_cstring(b, service); - buffer_put_cstring(b, context); + int r; + + sshbuf_reset(b); + if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 || + (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || + (r = sshbuf_put_cstring(b, user)) != 0 || + (r = sshbuf_put_cstring(b, service)) != 0 || + (r = sshbuf_put_cstring(b, context)) != 0) + fatal("%s: buffer error: %s", __func__, ssh_err(r)); } int diff --git a/gss-serv-krb5.c b/gss-serv-krb5.c index 795992d9f..a151bc1e4 100644 --- a/gss-serv-krb5.c +++ b/gss-serv-krb5.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gss-serv-krb5.c,v 1.8 2013/07/20 01:55:13 djm Exp $ */ +/* $OpenBSD: gss-serv-krb5.c,v 1.9 2018/07/09 21:37:55 markus Exp $ */ /* * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. @@ -35,14 +35,13 @@ #include #include "xmalloc.h" -#include "key.h" +#include "sshkey.h" #include "hostfile.h" #include "auth.h" #include "log.h" #include "misc.h" #include "servconf.h" -#include "buffer.h" #include "ssh-gss.h" extern ServerOptions options; diff --git a/gss-serv.c b/gss-serv.c index 6cae720e5..ab3a15f0f 100644 --- a/gss-serv.c +++ b/gss-serv.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gss-serv.c,v 1.30 2017/06/24 06:34:38 djm Exp $ */ +/* $OpenBSD: gss-serv.c,v 1.31 2018/07/09 21:37:55 markus Exp $ */ /* * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. @@ -36,8 +36,7 @@ #include "openbsd-compat/sys-queue.h" #include "xmalloc.h" -#include "buffer.h" -#include "key.h" +#include "sshkey.h" #include "hostfile.h" #include "auth.h" #include "log.h"