155682Smarkm/*-
2233294Sstas * Copyright (c) 2008 Doug Rabson
3233294Sstas * All rights reserved.
4233294Sstas *
555682Smarkm * Redistribution and use in source and binary forms, with or without
6233294Sstas * modification, are permitted provided that the following conditions
7233294Sstas * are met:
8233294Sstas * 1. Redistributions of source code must retain the above copyright
955682Smarkm *    notice, this list of conditions and the following disclaimer.
10233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer in the
1255682Smarkm *    documentation and/or other materials provided with the distribution.
13233294Sstas *
14233294Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1655682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2055682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24233294Sstas * SUCH DAMAGE.
25233294Sstas *
26233294Sstas * $FreeBSD$
27233294Sstas */
28233294Sstas
29233294Sstas#include <gssapi/gssapi.h>
30233294Sstas#include <stdlib.h>
31233294Sstas#include <string.h>
3255682Smarkm
3355682Smarkm#include "utils.h"
3455682Smarkm
35233294SstasOM_uint32
36233294Sstasgss_encapsulate_token(const gss_buffer_t input_token, gss_OID oid,
37233294Sstas    gss_buffer_t output_token)
3855682Smarkm{
39233294Sstas	unsigned char *p;
40233294Sstas	size_t len, inside_len;
41233294Sstas	size_t a, b;
42178825Sdfr	int i;
43178825Sdfr
44178825Sdfr	_gss_buffer_zero(output_token);
45178825Sdfr
46178825Sdfr	/*
47178825Sdfr	 * First time around, we calculate the size, second time, we
48233294Sstas	 * encode the token.
49178825Sdfr	 */
50178825Sdfr	p = NULL;
51178825Sdfr	for (i = 0; i < 2; i++) {
52233294Sstas		len = 0;
53178825Sdfr
54178825Sdfr		/*
55178825Sdfr		 * Token starts with [APPLICATION 0] SEQUENCE.
56178825Sdfr		 */
57178825Sdfr		if (p)
58178825Sdfr			*p++ = 0x60;
59178825Sdfr		len++;
60178825Sdfr
61178825Sdfr		/*
62233294Sstas		 * The length embedded in the token is the space
63178825Sdfr		 * needed for the encapsulated oid plus the length of
64178825Sdfr		 * the inner token.
65178825Sdfr		 */
66178825Sdfr		if (oid->length > 127)
67178825Sdfr			return (GSS_S_DEFECTIVE_TOKEN);
68178825Sdfr
69233294Sstas		inside_len = 2 + oid->length + input_token->length;
70178825Sdfr
71178825Sdfr		/*
72178825Sdfr		 * Figure out how to encode the length
73178825Sdfr		 */
74178825Sdfr		if (inside_len < 128) {
75233294Sstas			if (p)
76178825Sdfr				*p++ = inside_len;
77178825Sdfr			len++;
78178825Sdfr		} else {
79178825Sdfr			b = 1;
80178825Sdfr			if (inside_len >= 0x100)
81178825Sdfr				b++;
82178825Sdfr			if (inside_len >= 0x10000)
83178825Sdfr				b++;
84178825Sdfr			if (inside_len >= 0x1000000)
85233294Sstas				b++;
86178825Sdfr			if (p)
87178825Sdfr				*p++ = b | 0x80;
88233294Sstas			len++;
89233294Sstas			a = inside_len << 8*(4 - b);
90233294Sstas			while (b) {
91233294Sstas				if (p)
92233294Sstas					*p++ = (a >> 24);
93233294Sstas				a <<= 8;
94233294Sstas				len++;
95233294Sstas				b--;
96233294Sstas			}
97233294Sstas		}
98233294Sstas
99233294Sstas		/*
100233294Sstas		 * Encode the OID for the mechanism. Simplify life by
101233294Sstas		 * assuming that the OID length is less than 128 bytes.
102233294Sstas		 */
103233294Sstas		if (p)
104233294Sstas			*p++ = 0x06;
105233294Sstas		len++;
106233294Sstas		if (p)
107233294Sstas			*p++ = oid->length;
108233294Sstas		len++;
109233294Sstas		if (p) {
110233294Sstas			memcpy(p, oid->elements, oid->length);
111233294Sstas			p += oid->length;
112233294Sstas		}
113233294Sstas		len += oid->length;
114233294Sstas
115233294Sstas		if (p) {
116233294Sstas			memcpy(p, input_token->value, input_token->length);
117233294Sstas			p += input_token->length;
118233294Sstas		}
119233294Sstas		len += input_token->length;
120233294Sstas
121233294Sstas		if (i == 0) {
122233294Sstas			output_token->length = len;
123233294Sstas			output_token->value = malloc(len);
124233294Sstas			if (!output_token->value)
125233294Sstas				return (GSS_S_DEFECTIVE_TOKEN);
126233294Sstas			p = output_token->value;
127233294Sstas		}
128233294Sstas	}
129
130	return (GSS_S_COMPLETE);
131}
132