Path: blob/main/crypto/krb5/src/tests/gssapi/t_pcontok.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/gssapi/t_pcontok.c - gss_process_context_token tests */2/*3* Copyright (C) 2014 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 exercises krb5 gss_process_context_token. It first34* establishes a context to a named target. Then, if the resulting context35* uses RFC 1964, it creates a context deletion token from the acceptor to the36* initiator and passes it to the initiator using gss_process_context_token.37* If the established context uses RFC 4121, this program feeds a made-up38* context token to gss_process_context_token and checks for the expected39* error.40*/4142#include "k5-int.h"43#include "common.h"4445#define SGN_ALG_HMAC_SHA1_DES3_KD 0x0446#define SGN_ALG_HMAC_MD5 0x114748/*49* Create a valid RFC 1964 context deletion token using the information in *50* lctx. We must do this by hand since we no longer create context deletion51* tokens from gss_delete_sec_context.52*/53static void54make_delete_token(gss_krb5_lucid_context_v1_t *lctx, gss_buffer_desc *out)55{56krb5_error_code ret;57krb5_context context;58krb5_keyblock seqkb;59krb5_key seq;60krb5_checksum cksum;61krb5_cksumtype cktype;62krb5_keyusage ckusage;63krb5_crypto_iov iov;64krb5_data d;65size_t cksize, tlen;66unsigned char *token, *ptr, iv[8];67gss_krb5_lucid_key_t *lkey = &lctx->rfc1964_kd.ctx_key;68int signalg = lctx->rfc1964_kd.sign_alg;6970ret = krb5_init_context(&context);71check_k5err(context, "krb5_init_context", ret);7273seqkb.enctype = lkey->type;74seqkb.length = lkey->length;75seqkb.contents = lkey->data;76ret = krb5_k_create_key(context, &seqkb, &seq);77check_k5err(context, "krb5_k_create_key", ret);7879if (signalg == SGN_ALG_HMAC_SHA1_DES3_KD) {80cktype = CKSUMTYPE_HMAC_SHA1_DES3;81cksize = 20;82ckusage = 23;83} else if (signalg == SGN_ALG_HMAC_MD5) {84cktype = CKSUMTYPE_HMAC_MD5_ARCFOUR;85cksize = 8;86ckusage = 15;87} else {88abort();89}9091tlen = 20 + mech_krb5.length + cksize;92token = malloc(tlen);93assert(token != NULL);9495/* Create the ASN.1 wrapper (4 + mech_krb5.length bytes). Assume the ASN.196* lengths fit in one byte since deletion tokens are short. */97ptr = token;98*ptr++ = 0x60;99*ptr++ = tlen - 2;100*ptr++ = 0x06;101*ptr++ = mech_krb5.length;102memcpy(ptr, mech_krb5.elements, mech_krb5.length);103ptr += mech_krb5.length;104105/* Create the RFC 1964 token header (8 bytes). */106*ptr++ = 0x01;107*ptr++ = 0x02;108store_16_le(signalg, ptr);109ptr += 2;110*ptr++ = 0xFF;111*ptr++ = 0xFF;112*ptr++ = 0xFF;113*ptr++ = 0xFF;114115/* Create the checksum (cksize bytes at offset 8 from the header). */116d = make_data(ptr - 8, 8);117ret = krb5_k_make_checksum(context, cktype, seq, ckusage, &d, &cksum);118check_k5err(context, "krb5_k_make_checksum", ret);119memcpy(ptr + 8, cksum.contents, cksize);120121/* Create the sequence number (8 bytes). */122iov.flags = KRB5_CRYPTO_TYPE_DATA;123iov.data = make_data(ptr, 8);124ptr[4] = ptr[5] = ptr[6] = ptr[7] = lctx->initiate ? 0 : 0xFF;125memcpy(iv, ptr + 8, 8);126d = make_data(iv, 8);127if (signalg == SGN_ALG_HMAC_MD5) {128store_32_be(lctx->send_seq, ptr);129ret = krb5int_arcfour_gsscrypt(&seq->keyblock, 0, &d, &iov, 1);130check_k5err(context, "krb5int_arcfour_gsscrypt(seq)", ret);131} else {132store_32_le(lctx->send_seq, ptr);133ret = krb5_k_encrypt_iov(context, seq, 24, &d, &iov, 1);134check_k5err(context, "krb5_k_encrypt_iov(seq)", ret);135}136137krb5_free_checksum_contents(context, &cksum);138krb5_k_free_key(context, seq);139krb5_free_context(context);140141out->length = tlen;142out->value = token;143}144145int146main(int argc, char *argv[])147{148OM_uint32 minor, major, flags;149gss_name_t tname;150gss_buffer_desc token, in = GSS_C_EMPTY_BUFFER, out;151gss_ctx_id_t ictx, actx;152gss_krb5_lucid_context_v1_t *lctx;153void *lptr;154155assert(argc == 2);156tname = import_name(argv[1]);157158flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;159establish_contexts(&mech_krb5, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL,160tname, flags, &ictx, &actx, NULL, NULL, NULL);161162/* Export the acceptor context to a lucid context so we can look inside. */163major = gss_krb5_export_lucid_sec_context(&minor, &actx, 1, &lptr);164check_gsserr("gss_export_lucid_sec_context", major, minor);165lctx = lptr;166if (!lctx->protocol) {167/* Make an RFC 1964 context deletion token and pass it to168* gss_process_context_token. */169make_delete_token(lctx, &token);170major = gss_process_context_token(&minor, ictx, &token);171free(token.value);172check_gsserr("gss_process_context_token", major, minor);173/* Check for the appropriate major code from gss_wrap. */174major = gss_wrap(&minor, ictx, 1, GSS_C_QOP_DEFAULT, &in, NULL, &out);175assert(major == GSS_S_NO_CONTEXT);176} else {177/* RFC 4121 defines no context deletion token, so try passing something178* arbitrary and check for the appropriate major code. */179token.value = "abcd";180token.length = 4;181major = gss_process_context_token(&minor, ictx, &token);182assert(major == GSS_S_DEFECTIVE_TOKEN);183}184185(void)gss_release_name(&minor, &tname);186(void)gss_delete_sec_context(&minor, &ictx, NULL);187(void)gss_krb5_free_lucid_sec_context(&minor, lptr);188return 0;189}190191192