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