Path: blob/main/crypto/krb5/src/tests/gssapi/t_export_name.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/gssapi/t_export_name.c - Test program for gss_export_name behavior */2/*3* Copyright 2012 by the Massachusetts Institute of Technology.4* All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/*27* Test program for gss_export_name, intended to be run from a Python test28* script. Imports a name, canonicalizes it to a mech, exports it,29* re-imports/exports it to compare results, and then prints the hex form of30* the exported name followed by a newline.31*32* Usage: ./t_export_name [-k|-s] user:username|krb5:princ|host:service@host33*34* The name is imported as a username, krb5 principal, or hostbased name.35* By default or with -k, the name is canonicalized to the krb5 mech; -s36* indicates SPNEGO instead.37*/3839#include <stdio.h>40#include <stdlib.h>41#include <string.h>4243#include "common.h"4445static void46usage(void)47{48fprintf(stderr, "Usage: t_export_name [-k|-s] name\n");49exit(1);50}5152int53main(int argc, char *argv[])54{55OM_uint32 minor, major;56gss_OID mech = (gss_OID)gss_mech_krb5;57gss_name_t name, mechname, impname;58gss_buffer_desc buf, buf2;59krb5_boolean use_composite = FALSE;60gss_OID ntype;61const char *name_arg;62char opt;6364/* Parse arguments. */65while (argc > 1 && argv[1][0] == '-') {66opt = argv[1][1];67argc--, argv++;68if (opt == 'k')69mech = &mech_krb5;70else if (opt == 's')71mech = &mech_spnego;72else if (opt == 'c')73use_composite = TRUE;74else75usage();76}77if (argc != 2)78usage();79name_arg = argv[1];8081/* Import the name. */82name = import_name(name_arg);8384/* Canonicalize and export the name. */85major = gss_canonicalize_name(&minor, name, mech, &mechname);86check_gsserr("gss_canonicalize_name", major, minor);87if (use_composite)88major = gss_export_name_composite(&minor, mechname, &buf);89else90major = gss_export_name(&minor, mechname, &buf);91check_gsserr("gss_export_name", major, minor);9293/* Import and re-export the name, and compare the results. */94ntype = use_composite ? GSS_C_NT_COMPOSITE_EXPORT : GSS_C_NT_EXPORT_NAME;95major = gss_import_name(&minor, &buf, ntype, &impname);96check_gsserr("gss_import_name", major, minor);97if (use_composite)98major = gss_export_name_composite(&minor, impname, &buf2);99else100major = gss_export_name(&minor, impname, &buf2);101check_gsserr("gss_export_name", major, minor);102if (buf.length != buf2.length ||103memcmp(buf.value, buf2.value, buf.length) != 0) {104fprintf(stderr, "Mismatched results:\n");105print_hex(stderr, &buf);106print_hex(stderr, &buf2);107return 1;108}109110print_hex(stdout, &buf);111112(void)gss_release_name(&minor, &name);113(void)gss_release_name(&minor, &mechname);114(void)gss_release_name(&minor, &impname);115(void)gss_release_buffer(&minor, &buf);116(void)gss_release_buffer(&minor, &buf2);117return 0;118}119120121