Path: blob/main/crypto/krb5/src/tests/gssapi/t_credstore.c
34890 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 2011 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>2829#include "common.h"3031static void32usage(void)33{34fprintf(stderr,35"Usage: t_credstore [-sabi] principal [{key value} ...]\n");36exit(1);37}3839int40main(int argc, char *argv[])41{42OM_uint32 minor, major;43gss_key_value_set_desc store;44gss_name_t name = GSS_C_NO_NAME;45gss_cred_usage_t cred_usage = GSS_C_BOTH;46gss_OID_set mechs = GSS_C_NO_OID_SET;47gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;48gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;49gss_buffer_desc itok, atok;50krb5_boolean store_creds = FALSE, replay = FALSE;51char opt;5253/* Parse options. */54for (argv++; *argv != NULL && **argv == '-'; argv++) {55opt = (*argv)[1];56if (opt == 's')57store_creds = TRUE;58else if (opt == 'r')59replay = TRUE;60else if (opt == 'a')61cred_usage = GSS_C_ACCEPT;62else if (opt == 'b')63cred_usage = GSS_C_BOTH;64else if (opt == 'i')65cred_usage = GSS_C_INITIATE;66else67usage();68}6970/* Get the principal name. */71if (*argv == NULL)72usage();73if (**argv != '\0')74name = import_name(*argv);75argv++;7677/* Put any remaining arguments into the store. */78store.elements = calloc(argc, sizeof(struct gss_key_value_element_struct));79if (!store.elements)80errout("OOM");81store.count = 0;82while (*argv != NULL) {83if (*(argv + 1) == NULL)84usage();85store.elements[store.count].key = *argv;86store.elements[store.count].value = *(argv + 1);87store.count++;88argv += 2;89}9091if (store_creds) {92/* Acquire default creds and try to store them in the cred store. */93major = gss_acquire_cred(&minor, GSS_C_NO_NAME, 0, GSS_C_NO_OID_SET,94GSS_C_INITIATE, &cred, NULL, NULL);95check_gsserr("gss_acquire_cred", major, minor);9697major = gss_store_cred_into(&minor, cred, GSS_C_INITIATE,98GSS_C_NO_OID, 1, 0, &store, NULL, NULL);99check_gsserr("gss_store_cred_into", major, minor);100101gss_release_cred(&minor, &cred);102}103104/* Try to acquire creds from store. */105major = gss_acquire_cred_from(&minor, name, 0, mechs, cred_usage,106&store, &cred, NULL, NULL);107check_gsserr("gss_acquire_cred_from", major, minor);108109if (replay) {110/* Induce a replay using cred as the acceptor cred, to test the replay111* cache indicated by the store. */112major = gss_init_sec_context(&minor, GSS_C_NO_CREDENTIAL, &ictx, name,113&mech_krb5, 0, GSS_C_INDEFINITE,114GSS_C_NO_CHANNEL_BINDINGS,115GSS_C_NO_BUFFER, NULL, &itok, NULL, NULL);116check_gsserr("gss_init_sec_context", major, minor);117(void)gss_delete_sec_context(&minor, &ictx, NULL);118119major = gss_accept_sec_context(&minor, &actx, cred, &itok,120GSS_C_NO_CHANNEL_BINDINGS, NULL, NULL,121&atok, NULL, NULL, NULL);122check_gsserr("gss_accept_sec_context(1)", major, minor);123(void)gss_release_buffer(&minor, &atok);124(void)gss_delete_sec_context(&minor, &actx, NULL);125126major = gss_accept_sec_context(&minor, &actx, cred, &itok,127GSS_C_NO_CHANNEL_BINDINGS, NULL, NULL,128&atok, NULL, NULL, NULL);129check_gsserr("gss_accept_sec_context(2)", major, minor);130(void)gss_release_buffer(&minor, &itok);131(void)gss_release_buffer(&minor, &atok);132(void)gss_delete_sec_context(&minor, &actx, NULL);133}134135gss_release_name(&minor, &name);136gss_release_cred(&minor, &cred);137free(store.elements);138return 0;139}140141142