Path: blob/main/crypto/krb5/src/tests/gssapi/t_inq_ctx.c
34907 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 2015 Red Hat, Inc.3*4* Permission is hereby granted, free of charge, to any person5* obtaining a copy of this software and associated documentation files6* (the "Software"), to deal in the Software without restriction,7* including without limitation the rights to use, copy, modify, merge,8* publish, distribute, sublicense, and/or sell copies of the Software,9* and to permit persons to whom the Software is furnished to do so,10* subject to the following conditions:11*12* The above copyright notice and this permission notice shall be13* included in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,16* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF17* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS19* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN20* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN21* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22* SOFTWARE.23*/2425#include <stdio.h>26#include <stdlib.h>27#include <string.h>28#include <assert.h>2930#include "common.h"313233/*34* Test program for inquiring about a security context, intended to be run from35* a Python test script. Partially establishes a context to test inquiring36* about an incomplete context, and then establishes full contexts and inquires37* them. Exits with status 0 if all operations are successful, or 1 if not.38*39* Usage: ./t_inq_ctx target_name40*/4142static void43check_inq_context(gss_ctx_id_t context, int incomplete, gss_OID expected_mech,44OM_uint32 expected_flags, int expected_locally_init)45{46OM_uint32 major, minor;47gss_name_t out_init_name, out_accept_name;48OM_uint32 out_lifetime;49gss_OID out_mech_type;50OM_uint32 out_flags;51int out_locally_init;52int out_open;5354major = gss_inquire_context(&minor, context, &out_init_name,55&out_accept_name, &out_lifetime,56&out_mech_type, &out_flags, &out_locally_init,57&out_open);58check_gsserr("gss_inquire_context", major, minor);5960assert(gss_oid_equal(out_mech_type, expected_mech));61assert(out_flags == expected_flags);62assert(out_locally_init == expected_locally_init);63if (incomplete) {64assert(!out_open);65assert(out_lifetime == 0);66assert(out_init_name == GSS_C_NO_NAME);67assert(out_accept_name == GSS_C_NO_NAME);68} else {69assert(out_open);70assert(out_lifetime > 0);71assert(out_init_name != GSS_C_NO_NAME);72assert(out_accept_name != GSS_C_NO_NAME);73}7475(void)gss_release_name(&minor, &out_accept_name);76(void)gss_release_name(&minor, &out_init_name);77}7879/* Call gss_init_sec_context() once to create an initiator context (which will80* be partial if flags includes GSS_C_MUTUAL_FLAG and the mech is krb5). */81static void82start_init_context(gss_OID mech, gss_cred_id_t cred, gss_name_t tname,83OM_uint32 flags, gss_ctx_id_t *ctx)84{85OM_uint32 major, minor;86gss_buffer_desc itok = GSS_C_EMPTY_BUFFER;8788*ctx = GSS_C_NO_CONTEXT;89major = gss_init_sec_context(&minor, cred, ctx, tname, mech, flags,90GSS_C_INDEFINITE, GSS_C_NO_CHANNEL_BINDINGS,91NULL, NULL, &itok, NULL, NULL);92check_gsserr("gss_init_sec_context", major, minor);93(void)gss_release_buffer(&minor, &itok);94}9596/* Call gss_init_sec_context() and gss_accept_sec_context() once to create an97* acceptor context. */98static void99start_accept_context(gss_OID mech, gss_cred_id_t icred, gss_cred_id_t acred,100gss_name_t tname, OM_uint32 flags, gss_ctx_id_t *ctx)101{102OM_uint32 major, minor;103gss_ctx_id_t ictx = GSS_C_NO_CONTEXT;104gss_buffer_desc itok = GSS_C_EMPTY_BUFFER, atok = GSS_C_EMPTY_BUFFER;105106major = gss_init_sec_context(&minor, icred, &ictx, tname, mech, flags,107GSS_C_INDEFINITE, GSS_C_NO_CHANNEL_BINDINGS,108NULL, NULL, &itok, NULL, NULL);109check_gsserr("gss_init_sec_context", major, minor);110111*ctx = GSS_C_NO_CONTEXT;112major = gss_accept_sec_context(&minor, ctx, acred, &itok,113GSS_C_NO_CHANNEL_BINDINGS, NULL, NULL,114&atok, NULL, NULL, NULL);115check_gsserr("gss_accept_sec_context", major, minor);116117(void)gss_release_buffer(&minor, &itok);118(void)gss_release_buffer(&minor, &atok);119(void)gss_delete_sec_context(&minor, &ictx, NULL);120}121122static void123partial_iakerb_acceptor(const char *username, const char *password,124gss_name_t tname, OM_uint32 flags, gss_ctx_id_t *ctx)125{126OM_uint32 major, minor;127gss_name_t name;128gss_buffer_desc ubuf, pwbuf;129gss_OID_set_desc mechlist;130gss_cred_id_t icred, acred;131132mechlist.count = 1;133mechlist.elements = &mech_iakerb;134135/* Import the username. */136ubuf.value = (void *)username;137ubuf.length = strlen(username);138major = gss_import_name(&minor, &ubuf, GSS_C_NT_USER_NAME, &name);139check_gsserr("gss_import_name", major, minor);140141/* Create an IAKERB initiator cred with the username and password. */142pwbuf.value = (void *)password;143pwbuf.length = strlen(password);144major = gss_acquire_cred_with_password(&minor, name, &pwbuf, 0,145&mechlist, GSS_C_INITIATE, &icred,146NULL, NULL);147check_gsserr("gss_acquire_cred_with_password", major, minor);148149/* Create an acceptor cred with support for IAKERB. */150major = gss_acquire_cred(&minor, GSS_C_NO_NAME, GSS_C_INDEFINITE,151&mechlist, GSS_C_ACCEPT, &acred, NULL, NULL);152check_gsserr("gss_acquire_cred", major, minor);153154/* Begin context establishment to get a partial acceptor context. */155start_accept_context(&mech_iakerb, icred, acred, tname, flags, ctx);156157(void)gss_release_name(&minor, &name);158(void)gss_release_cred(&minor, &icred);159(void)gss_release_cred(&minor, &acred);160}161162/* Create a partially established SPNEGO acceptor. */163static void164partial_spnego_acceptor(gss_name_t tname, gss_ctx_id_t *ctx)165{166OM_uint32 major, minor;167gss_buffer_desc itok = GSS_C_EMPTY_BUFFER, atok;168169/*170* We could construct a fixed SPNEGO initiator token which forces a171* renegotiation, but a simpler approach is to pass an empty token to172* gss_accept_sec_context(), taking advantage of our compatibility support173* for SPNEGO NegHints.174*/175*ctx = GSS_C_NO_CONTEXT;176major = gss_accept_sec_context(&minor, ctx, GSS_C_NO_CREDENTIAL, &itok,177GSS_C_NO_CHANNEL_BINDINGS, NULL, NULL,178&atok, NULL, NULL, NULL);179check_gsserr("gss_accept_sec_context(neghints)", major, minor);180181(void)gss_release_buffer(&minor, &atok);182}183184int185main(int argc, char *argv[])186{187OM_uint32 minor, flags, dce_flags;188gss_name_t tname;189gss_ctx_id_t ictx, actx;190const char *username, *password;191192if (argc != 4) {193fprintf(stderr, "Usage: %s username password targetname\n", argv[0]);194return 1;195}196username = argv[1];197password = argv[2];198tname = import_name(argv[3]);199200flags = GSS_C_SEQUENCE_FLAG | GSS_C_MUTUAL_FLAG | GSS_C_CONF_FLAG |201GSS_C_INTEG_FLAG;202start_init_context(&mech_krb5, GSS_C_NO_CREDENTIAL, tname, flags, &ictx);203check_inq_context(ictx, 1, &mech_krb5, flags | GSS_C_TRANS_FLAG, 1);204(void)gss_delete_sec_context(&minor, &ictx, NULL);205206start_init_context(&mech_iakerb, GSS_C_NO_CREDENTIAL, tname, flags, &ictx);207check_inq_context(ictx, 1, &mech_iakerb, flags, 1);208(void)gss_delete_sec_context(&minor, &ictx, NULL);209210start_init_context(&mech_spnego, GSS_C_NO_CREDENTIAL, tname, flags, &ictx);211check_inq_context(ictx, 1, &mech_spnego, flags, 1);212(void)gss_delete_sec_context(&minor, &ictx, NULL);213214dce_flags = flags | GSS_C_DCE_STYLE;215start_accept_context(&mech_krb5, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL,216tname, dce_flags, &actx);217check_inq_context(actx, 1, &mech_krb5, dce_flags | GSS_C_TRANS_FLAG, 0);218(void)gss_delete_sec_context(&minor, &actx, NULL);219220partial_iakerb_acceptor(username, password, tname, flags, &actx);221check_inq_context(actx, 1, &mech_iakerb, 0, 0);222(void)gss_delete_sec_context(&minor, &actx, NULL);223224partial_spnego_acceptor(tname, &actx);225check_inq_context(actx, 1, &mech_spnego, 0, 0);226(void)gss_delete_sec_context(&minor, &actx, NULL);227228establish_contexts(&mech_krb5, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL,229tname, flags, &ictx, &actx, NULL, NULL, NULL);230231check_inq_context(ictx, 0, &mech_krb5, flags | GSS_C_TRANS_FLAG, 1);232check_inq_context(actx, 0, &mech_krb5,233flags | GSS_C_TRANS_FLAG | GSS_C_PROT_READY_FLAG, 0);234235(void)gss_delete_sec_context(&minor, &ictx, NULL);236(void)gss_delete_sec_context(&minor, &actx, NULL);237238(void)gss_release_name(&minor, &tname);239return 0;240}241242243