Path: blob/main/crypto/krb5/src/tests/gssapi/t_err.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/gssapi/t_err.c - Test accept_sec_context error generation */2/*3* Copyright (C) 2013 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* This test program verifies that the krb5 gss_accept_sec_context can produce34* error tokens and that gss_init_sec_context can interpret them.35*/3637#include <stdio.h>38#include <stdlib.h>39#include <string.h>40#include <assert.h>4142#include "common.h"4344static void45check_replay_error(const char *msg, OM_uint32 major, OM_uint32 minor)46{47OM_uint32 tmpmin, msg_ctx = 0;48const char *replay = "Request is a replay";49gss_buffer_desc m;5051if (major != GSS_S_FAILURE) {52fprintf(stderr, "%s: expected major code GSS_S_FAILURE\n", msg);53check_gsserr(msg, major, minor);54exit(1);55}5657(void)gss_display_status(&tmpmin, minor, GSS_C_MECH_CODE, GSS_C_NULL_OID,58&msg_ctx, &m);59if (m.length != strlen(replay) || memcmp(m.value, replay, m.length) != 0) {60fprintf(stderr, "%s: expected replay error; got %.*s\n", msg,61(int)m.length, (char *)m.value);62exit(1);63}64(void)gss_release_buffer(&tmpmin, &m);65}6667int68main(int argc, char *argv[])69{70OM_uint32 minor, major, flags;71gss_OID mech = &mech_krb5;72gss_name_t tname;73gss_buffer_desc itok, atok;74gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;7576argv++;77if (*argv != NULL && strcmp(*argv, "--spnego") == 0) {78mech = &mech_spnego;79argv++;80}81if (*argv == NULL || argv[1] != NULL) {82fprintf(stderr, "Usage: t_err targetname\n");83return 1;84}85tname = import_name(*argv);8687/* Get the initial context token. */88flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_MUTUAL_FLAG;89major = gss_init_sec_context(&minor, GSS_C_NO_CREDENTIAL, &ictx, tname,90mech, flags, GSS_C_INDEFINITE,91GSS_C_NO_CHANNEL_BINDINGS, GSS_C_NO_BUFFER,92NULL, &itok, NULL, NULL);93check_gsserr("gss_init_sec_context(1)", major, minor);94assert(major == GSS_S_CONTINUE_NEEDED);9596/* Process this token into an acceptor context, then discard it. */97major = gss_accept_sec_context(&minor, &actx, GSS_C_NO_CREDENTIAL, &itok,98GSS_C_NO_CHANNEL_BINDINGS, NULL,99NULL, &atok, NULL, NULL, NULL);100check_gsserr("gss_accept_sec_context(1)", major, minor);101(void)gss_release_buffer(&minor, &atok);102(void)gss_delete_sec_context(&minor, &actx, NULL);103104/* Process the same token again, producing a replay error. */105major = gss_accept_sec_context(&minor, &actx, GSS_C_NO_CREDENTIAL, &itok,106GSS_C_NO_CHANNEL_BINDINGS, NULL,107NULL, &atok, NULL, NULL, NULL);108check_replay_error("gss_accept_sec_context(2)", major, minor);109assert(atok.length != 0);110111/* Send the error token back the initiator. */112(void)gss_release_buffer(&minor, &itok);113major = gss_init_sec_context(&minor, GSS_C_NO_CREDENTIAL, &ictx, tname,114mech, flags, GSS_C_INDEFINITE,115GSS_C_NO_CHANNEL_BINDINGS, &atok,116NULL, &itok, NULL, NULL);117check_replay_error("gss_init_sec_context(2)", major, minor);118119(void)gss_release_name(&minor, &tname);120(void)gss_release_buffer(&minor, &itok);121(void)gss_release_buffer(&minor, &atok);122(void)gss_delete_sec_context(&minor, &ictx, NULL);123(void)gss_delete_sec_context(&minor, &actx, NULL);124return 0;125}126127128