Path: blob/main/crypto/krb5/src/tests/gssapi/t_lifetime.c
34907 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/gssapi/t_lifetime.c - display cred and context lifetimes */2/*3* Copyright (C) 2017 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#include <stdio.h>33#include <stdlib.h>34#include <assert.h>35#include "common.h"3637/*38* Using the default credential, exercise the GSS functions which accept or39* produce lifetimes. Display the following results, one per line, as ASCII40* integers or the string "indefinite":41*42* initiator cred lifetime according to gss_acquire_cred()43* initiator cred lifetime according to gss_inquire_cred()44* acceptor cred lifetime according to gss_acquire_cred()45* acceptor cred lifetime according to gss_inquire_cred()46* initiator context lifetime according to gss_init_sec_context()47* initiator context lifetime according to gss_inquire_context()48* initiator context lifetime according to gss_context_time()49* acceptor context lifetime according to gss_init_sec_context()50* acceptor context lifetime according to gss_inquire_context()51* acceptor context lifetime according to gss_context_time()52*/5354static void55display_time(OM_uint32 tval)56{57if (tval == GSS_C_INDEFINITE)58puts("indefinite");59else60printf("%u\n", (unsigned int)tval);61}6263int64main(int argc, char *argv[])65{66OM_uint32 minor, major;67gss_cred_id_t icred, acred;68gss_name_t tname;69gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;70gss_buffer_desc itok = GSS_C_EMPTY_BUFFER, atok = GSS_C_EMPTY_BUFFER;71OM_uint32 time_req = GSS_C_INDEFINITE, time_rec;7273if (argc < 2 || argc > 3) {74fprintf(stderr, "Usage: %s targetname [time_req]\n", argv[0]);75return 1;76}77tname = import_name(argv[1]);78if (argc >= 3)79time_req = atoll(argv[2]);8081/* Get initiator cred and display its lifetime according to82* gss_acquire_cred and gss_inquire_cred. */83major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,84GSS_C_INITIATE, &icred, NULL, &time_rec);85check_gsserr("gss_acquire_cred(initiate)", major, minor);86display_time(time_rec);87major = gss_inquire_cred(&minor, icred, NULL, &time_rec, NULL, NULL);88check_gsserr("gss_inquire_cred(initiate)", major, minor);89display_time(time_rec);9091/* Get acceptor cred and display its lifetime according to gss_acquire_cred92* and gss_inquire_cred. */93major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,94GSS_C_ACCEPT, &acred, NULL, &time_rec);95check_gsserr("gss_acquire_cred(accept)", major, minor);96display_time(time_rec);97major = gss_inquire_cred(&minor, acred, NULL, &time_rec, NULL, NULL);98check_gsserr("gss_inquire_cred(accept)", major, minor);99display_time(time_rec);100101/* Make an initiator context and display its lifetime according to102* gss_init_sec_context, gss_inquire_context, and gss_context_time. */103major = gss_init_sec_context(&minor, icred, &ictx, tname, &mech_krb5, 0,104time_req, GSS_C_NO_CHANNEL_BINDINGS, &atok,105NULL, &itok, NULL, &time_rec);106check_gsserr("gss_init_sec_context", major, minor);107assert(major == GSS_S_COMPLETE);108display_time(time_rec);109major = gss_inquire_context(&minor, ictx, NULL, NULL, &time_rec, NULL,110NULL, NULL, NULL);111check_gsserr("gss_inquire_context(initiate)", major, minor);112display_time(time_rec);113major = gss_context_time(&minor, ictx, &time_rec);114check_gsserr("gss_context_time(initiate)", major, minor);115display_time(time_rec);116117major = gss_accept_sec_context(&minor, &actx, acred, &itok,118GSS_C_NO_CHANNEL_BINDINGS, NULL,119NULL, &atok, NULL, &time_rec, NULL);120check_gsserr("gss_accept_sec_context", major, minor);121assert(major == GSS_S_COMPLETE);122display_time(time_rec);123major = gss_inquire_context(&minor, actx, NULL, NULL, &time_rec, NULL,124NULL, NULL, NULL);125check_gsserr("gss_inquire_context(accept)", major, minor);126display_time(time_rec);127major = gss_context_time(&minor, actx, &time_rec);128check_gsserr("gss_context_time(accept)", major, minor);129display_time(time_rec);130131(void)gss_release_buffer(&minor, &itok);132(void)gss_release_buffer(&minor, &atok);133(void)gss_release_name(&minor, &tname);134(void)gss_release_cred(&minor, &icred);135(void)gss_release_cred(&minor, &acred);136(void)gss_delete_sec_context(&minor, &ictx, NULL);137(void)gss_delete_sec_context(&minor, &actx, NULL);138return 0;139}140141142