1184588Sdfr/*-
2184588Sdfr * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3184588Sdfr * Authors: Doug Rabson <dfr@rabson.org>
4184588Sdfr * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
5184588Sdfr *
6184588Sdfr * Redistribution and use in source and binary forms, with or without
7184588Sdfr * modification, are permitted provided that the following conditions
8184588Sdfr * are met:
9184588Sdfr * 1. Redistributions of source code must retain the above copyright
10184588Sdfr *    notice, this list of conditions and the following disclaimer.
11184588Sdfr * 2. Redistributions in binary form must reproduce the above copyright
12184588Sdfr *    notice, this list of conditions and the following disclaimer in the
13184588Sdfr *    documentation and/or other materials provided with the distribution.
14184588Sdfr *
15184588Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16184588Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17184588Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18184588Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19184588Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20184588Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21184588Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22184588Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23184588Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24184588Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25184588Sdfr * SUCH DAMAGE.
26184588Sdfr */
27184588Sdfr
28184588Sdfr#include <sys/cdefs.h>
29184588Sdfr__FBSDID("$FreeBSD$");
30184588Sdfr
31184588Sdfr#include <sys/param.h>
32184588Sdfr#include <sys/kernel.h>
33184588Sdfr#include <sys/kobj.h>
34184588Sdfr#include <sys/malloc.h>
35184588Sdfr#include <sys/mbuf.h>
36184588Sdfr
37184588Sdfr#include <kgssapi/gssapi.h>
38184588Sdfr#include <kgssapi/gssapi_impl.h>
39184588Sdfr
40184588Sdfr#include "kgss_if.h"
41184588Sdfr
42184588SdfrOM_uint32
43184588Sdfrgss_wrap(OM_uint32 *minor_status,
44184588Sdfr    const gss_ctx_id_t ctx,
45184588Sdfr    int conf_req_flag,
46184588Sdfr    gss_qop_t qop_req,
47184588Sdfr    const gss_buffer_t input_message_buffer,
48184588Sdfr    int *conf_state,
49184588Sdfr    gss_buffer_t output_message_buffer)
50184588Sdfr{
51184588Sdfr	OM_uint32 maj_stat;
52184588Sdfr	struct mbuf *m;
53184588Sdfr
54184588Sdfr	if (!ctx) {
55184588Sdfr		*minor_status = 0;
56184588Sdfr		return (GSS_S_NO_CONTEXT);
57184588Sdfr	}
58184588Sdfr
59184588Sdfr	MGET(m, M_WAITOK, MT_DATA);
60184588Sdfr	if (input_message_buffer->length > MLEN)
61184588Sdfr		MCLGET(m, M_WAITOK);
62184588Sdfr	m_append(m, input_message_buffer->length, input_message_buffer->value);
63184588Sdfr
64184588Sdfr	maj_stat = KGSS_WRAP(ctx, minor_status, conf_req_flag, qop_req,
65184588Sdfr	    &m, conf_state);
66184588Sdfr
67184588Sdfr	/*
68184588Sdfr	 * On success, m is the wrapped message, on failure, m is
69184588Sdfr	 * freed.
70184588Sdfr	 */
71184588Sdfr	if (maj_stat == GSS_S_COMPLETE) {
72184588Sdfr		output_message_buffer->length = m_length(m, NULL);
73184588Sdfr		output_message_buffer->value =
74184588Sdfr			malloc(output_message_buffer->length,
75184588Sdfr			    M_GSSAPI, M_WAITOK);
76184588Sdfr		m_copydata(m, 0, output_message_buffer->length,
77184588Sdfr		    output_message_buffer->value);
78184588Sdfr		m_freem(m);
79184588Sdfr	}
80184588Sdfr
81184588Sdfr	return (maj_stat);
82184588Sdfr}
83184588Sdfr
84184588SdfrOM_uint32
85184588Sdfrgss_wrap_mbuf(OM_uint32 *minor_status, const gss_ctx_id_t ctx,
86184588Sdfr    int conf_req_flag, gss_qop_t qop_req, struct mbuf **mp, int *conf_state)
87184588Sdfr{
88184588Sdfr
89184588Sdfr	if (!ctx) {
90184588Sdfr		*minor_status = 0;
91184588Sdfr		return (GSS_S_NO_CONTEXT);
92184588Sdfr	}
93184588Sdfr
94184588Sdfr	return (KGSS_WRAP(ctx, minor_status, conf_req_flag, qop_req,
95184588Sdfr		mp, conf_state));
96184588Sdfr}
97