/* getsubopt-digestmd5.c --- DIGEST-MD5 getsubopt NULL de-reference trigger
 * Copyright (C) 2002-2026 Simon Josefsson
 *
 * This file is part of GNU SASL.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <gsasl.h>

#include "utils.h"

/* https://lists.gnu.org/archive/html/help-gsasl/2026-05/msg00000.html */

void
doit (void)
{
  Gsasl *ctx = NULL;
  Gsasl_session *sctx = NULL;
  char *out = NULL;
  size_t outlen = 0;
  int res;

  res = gsasl_init (&ctx);
  if (res != GSASL_OK)
    {
      fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
      return;
    }

  res = gsasl_server_start (ctx, "DIGEST-MD5", &sctx);
  if (res != GSASL_OK)
    {
      fail ("gsasl_server_start() failed (%d):\n%s\n",
	    res, gsasl_strerror (res));
      return;
    }

  res = gsasl_step (sctx, NULL, 0, &out, &outlen);
  if (res != GSASL_NEEDS_MORE)
    {
      fail ("First gsasl_step() != GSASL_NEEDS_MORE (%d):\n%s\n",
	    res, gsasl_strerror (res));
      return;
    }

  gsasl_free (out);
  out = NULL;
  outlen = 0;

  gsasl_step (sctx, "nonce,", 6, &out, &outlen);
  if (res != GSASL_NEEDS_MORE)
    {
      fail ("Second gsasl_step() != GSASL_NEEDS_MORE (%d):\n%s\n",
	    res, gsasl_strerror (res));
      return;
    }

  gsasl_finish (sctx);
  gsasl_done (ctx);

  success ("DIGEST-MD5 getsubopt not vulnerable");
}
