export_sec_context.c revision 159d09a20817016f09b3ea28d1bdada4a336bb91
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim/*
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * lib/gssapi/krb5/export_sec_context.c
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * Copyright 1995 by the Massachusetts Institute of Technology.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * All Rights Reserved.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * Export of this software from the United States of America may
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * require a specific license from the United States Government.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * It is the responsibility of any person or organization contemplating
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * export to obtain such a license before exporting.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * distribute this software and its documentation for any purpose and
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * without fee is hereby granted, provided that the above copyright
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * notice appear in all copies and that both that copyright notice and
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * this permission notice appear in supporting documentation, and that
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * the name of M.I.T. not be used in advertising or publicity pertaining
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * to distribution of the software without specific, written prior
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * permission. Furthermore if you modify this software you must label
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * your software as modified software and not distribute it in such a
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * fashion that it might be confused with the original M.I.T. software.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * M.I.T. makes no representations about the suitability of
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * this software for any purpose. It is provided "as is" without express
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * or implied warranty.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim/*
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * export_sec_context.c - Externalize the security context.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim#include "gssapiP_krb5.h"
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon KimOM_uint32
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kimkrb5_gss_export_sec_context(minor_status, context_handle, interprocess_token)
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim OM_uint32 *minor_status;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim gss_ctx_id_t *context_handle;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim gss_buffer_t interprocess_token;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim{
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim krb5_context context;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim krb5_error_code kret;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim OM_uint32 retval;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim size_t bufsize, blen;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim krb5_gss_ctx_id_t ctx;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim krb5_octet *obuffer, *obp;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Assume a tragic failure */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim obuffer = (krb5_octet *) NULL;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim retval = GSS_S_FAILURE;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *minor_status = 0;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (!kg_validate_ctx_id(*context_handle)) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim kret = (OM_uint32) G_VALIDATE_FAILED;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim retval = GSS_S_NO_CONTEXT;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim goto error_out;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim ctx = (krb5_gss_ctx_id_t) *context_handle;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim context = ctx->k5_context;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim kret = krb5_gss_ser_init(context);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (kret)
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim goto error_out;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Determine size needed for externalization of context */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim bufsize = 0;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if ((kret = kg_ctx_size(context, (krb5_pointer) ctx,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim &bufsize)))
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim goto error_out;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Allocate the buffer */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if ((obuffer = (krb5_octet *) xmalloc(bufsize)) == NULL) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim kret = ENOMEM;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim goto error_out;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim obp = obuffer;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim blen = bufsize;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Externalize the context */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if ((kret = kg_ctx_externalize(context,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim (krb5_pointer) ctx, &obp, &blen)))
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim goto error_out;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Success! Return the buffer */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim interprocess_token->length = bufsize - blen;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim interprocess_token->value = obuffer;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *minor_status = 0;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim retval = GSS_S_COMPLETE;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Now, clean up the context state */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim (void)krb5_gss_delete_sec_context(minor_status, context_handle, NULL);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *context_handle = GSS_C_NO_CONTEXT;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (GSS_S_COMPLETE);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kimerror_out:
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (obuffer && bufsize) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim memset(obuffer, 0, bufsize);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim xfree(obuffer);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (*minor_status == 0)
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *minor_status = (OM_uint32) kret;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return(retval);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim}
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim