xref: /freebsd/crypto/krb5/src/lib/gssapi/krb5/export_sec_context.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/gssapi/krb5/export_sec_context.c - Externalize a security context */
3 /*
4  * Copyright 1995, 2008 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 #include "gssapiP_krb5.h"
28 #ifndef LEAN_CLIENT
29 OM_uint32 KRB5_CALLCONV
krb5_gss_export_sec_context(OM_uint32 * minor_status,gss_ctx_id_t * context_handle,gss_buffer_t interprocess_token)30 krb5_gss_export_sec_context(OM_uint32 *minor_status,
31                             gss_ctx_id_t *context_handle,
32                             gss_buffer_t interprocess_token)
33 {
34     krb5_context        context = NULL;
35     krb5_error_code     kret;
36     OM_uint32           retval;
37     size_t              bufsize, blen;
38     krb5_gss_ctx_id_t   ctx;
39     krb5_octet          *obuffer, *obp;
40 
41     /* Assume a tragic failure */
42     obuffer = (krb5_octet *) NULL;
43     retval = GSS_S_FAILURE;
44     *minor_status = 0;
45 
46     ctx = (krb5_gss_ctx_id_t) *context_handle;
47     if (ctx->terminated) {
48         *minor_status = KG_CTX_INCOMPLETE;
49         return (GSS_S_NO_CONTEXT);
50     }
51 
52     context = ctx->k5_context;
53 
54     /* Determine size needed for externalization of context */
55     bufsize = 0;
56     if ((kret = kg_ctx_size(context, ctx, &bufsize)))
57         goto error_out;
58 
59     /* Allocate the buffer */
60     if ((obuffer = gssalloc_malloc(bufsize)) == NULL) {
61         kret = ENOMEM;
62         goto error_out;
63     }
64 
65     obp = obuffer;
66     blen = bufsize;
67     /* Externalize the context */
68     if ((kret = kg_ctx_externalize(context, ctx, &obp, &blen)))
69         goto error_out;
70 
71     /* Success!  Return the buffer */
72     interprocess_token->length = bufsize - blen;
73     interprocess_token->value = obuffer;
74     *minor_status = 0;
75     retval = GSS_S_COMPLETE;
76 
77     /* Now, clean up the context state */
78     (void)krb5_gss_delete_sec_context(minor_status, context_handle, NULL);
79     *context_handle = GSS_C_NO_CONTEXT;
80 
81     return (GSS_S_COMPLETE);
82 
83 error_out:
84     if (retval != GSS_S_COMPLETE)
85         if (kret != 0 && context != 0)
86             save_error_info((OM_uint32)kret, context);
87     if (obuffer && bufsize) {
88         zap(obuffer, bufsize);
89         xfree(obuffer);
90     }
91     if (*minor_status == 0)
92         *minor_status = (OM_uint32) kret;
93     return(retval);
94 }
95 #endif /* LEAN_CLIENT */
96