gss_unwrap.c revision 285830
11902Swollman/*-
250476Speter * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
318718Swosch * Authors: Doug Rabson <dfr@rabson.org>
471216Sru * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
571216Sru *
671216Sru * Redistribution and use in source and binary forms, with or without
771216Sru * modification, are permitted provided that the following conditions
874462Salfred * are met:
974462Salfred * 1. Redistributions of source code must retain the above copyright
1074462Salfred *    notice, this list of conditions and the following disclaimer.
1174462Salfred * 2. Redistributions in binary form must reproduce the above copyright
1274462Salfred *    notice, this list of conditions and the following disclaimer in the
1374462Salfred *    documentation and/or other materials provided with the distribution.
1474462Salfred *
1574462Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1674462Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1774462Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1874462Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1974462Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2074462Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2174462Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2274462Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2374462Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2474462Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2574462Salfred * SUCH DAMAGE.
2674462Salfred */
2774462Salfred
2874462Salfred#include <sys/cdefs.h>
2974462Salfred__FBSDID("$FreeBSD: releng/10.2/sys/kgssapi/gss_unwrap.c 184588 2008-11-03 10:38:00Z dfr $");
3074462Salfred
3174462Salfred#include <sys/param.h>
3274462Salfred#include <sys/kernel.h>
3374462Salfred#include <sys/kobj.h>
34223877Skevlo#include <sys/malloc.h>
3574462Salfred#include <sys/mbuf.h>
3674462Salfred
3774462Salfred#include <kgssapi/gssapi.h>
3874462Salfred#include <kgssapi/gssapi_impl.h>
3974462Salfred
4074462Salfred#include "kgss_if.h"
4174462Salfred
4274462SalfredOM_uint32
4374462Salfredgss_unwrap(OM_uint32 *minor_status,
4474462Salfred    const gss_ctx_id_t ctx,
4574462Salfred    const gss_buffer_t input_message_buffer,
4674462Salfred    gss_buffer_t output_message_buffer,
4771216Sru    int *conf_state,
4871216Sru    gss_qop_t *qop_state)
4971216Sru{
5071216Sru	OM_uint32 maj_stat;
5184306Sru	struct mbuf *m;
5284306Sru
5371216Sru	if (!ctx) {
5471216Sru		*minor_status = 0;
5571216Sru		return (GSS_S_NO_CONTEXT);
5671216Sru	}
5771216Sru
581902Swollman	MGET(m, M_WAITOK, MT_DATA);
591902Swollman	if (input_message_buffer->length > MLEN)
601902Swollman		MCLGET(m, M_WAITOK);
611902Swollman	m_append(m, input_message_buffer->length, input_message_buffer->value);
6271216Sru
6371216Sru	maj_stat = KGSS_UNWRAP(ctx, minor_status, &m, conf_state, qop_state);
6471216Sru
6571216Sru	/*
6671216Sru	 * On success, m is the wrapped message, on failure, m is
6771216Sru	 * freed.
6871216Sru	 */
6971216Sru	if (maj_stat == GSS_S_COMPLETE) {
7071216Sru		output_message_buffer->length = m_length(m, NULL);
7171216Sru		output_message_buffer->value =
7271216Sru			malloc(output_message_buffer->length,
7371216Sru			    M_GSSAPI, M_WAITOK);
7471216Sru		m_copydata(m, 0, output_message_buffer->length,
7571216Sru		    output_message_buffer->value);
7671216Sru		m_freem(m);
7771216Sru	}
781902Swollman
791902Swollman	return (maj_stat);
8057686Ssheldonh}
8157686Ssheldonh
8271216SruOM_uint32
83108087Srugss_unwrap_mbuf(OM_uint32 *minor_status,
841902Swollman    const gss_ctx_id_t ctx,
8571216Sru    struct mbuf **mp,
861902Swollman    int *conf_state,
871902Swollman    gss_qop_t *qop_state)
8871216Sru{
89108087Sru
9071216Sru	if (!ctx) {
91108087Sru		*minor_status = 0;
921902Swollman		return (GSS_S_NO_CONTEXT);
9371216Sru	}
941902Swollman
9571216Sru	return (KGSS_UNWRAP(ctx, minor_status, mp, conf_state, qop_state));
961902Swollman}
9771216Sru
981902Swollman