9a972437c5051a6a1a5539463c22d7892daa2daf
[cyrus-sasl.git] / plugins / gs2_token.c
1 /*
2  * Copyright 1993 by OpenVision Technologies, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without fee,
6  * provided that the above copyright notice appears in all copies and
7  * that both that copyright notice and this permission notice appear in
8  * supporting documentation, and that the name of OpenVision not be used
9  * in advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission. OpenVision makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20  * PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <string.h>
24 #include <gssapi/gssapi.h>
25 #include "gs2_token.h"
26
27 /*
28  * $Id: util_token.c 23457 2009-12-08 00:04:48Z tlyu $
29  */
30
31 /* XXXX this code currently makes the assumption that a mech oid will
32    never be longer than 127 bytes.  This assumption is not inherent in
33    the interfaces, so the code can be fixed if the OSI namespace
34    balloons unexpectedly. */
35
36 /*
37  * Each token looks like this:
38  * 0x60                 tag for APPLICATION 0, SEQUENCE
39  *                              (constructed, definite-length)
40  * <length>             possible multiple bytes, need to parse/generate
41  * 0x06                 tag for OBJECT IDENTIFIER
42  * <moid_length>        compile-time constant string (assume 1 byte)
43  * <moid_bytes>         compile-time constant string
44  * <inner_bytes>        the ANY containing the application token
45  * bytes 0,1 are the token type
46  * bytes 2,n are the token data
47  *
48  * Note that the token type field is a feature of RFC 1964 mechanisms and
49  * is not used by other GSSAPI mechanisms.  As such, a token type of -1
50  * is interpreted to mean that no token type should be expected or
51  * generated.
52  *
53  * For the purposes of this abstraction, the token "header" consists of
54  * the sequence tag and length octets, the mech OID DER encoding, and the
55  * first two inner bytes, which indicate the token type.  The token
56  * "body" consists of everything else.
57  */
58
59 static size_t
60 der_length_size(size_t length)
61 {
62     if (length < (1<<7))
63         return 1;
64     else if (length < (1<<8))
65         return 2;
66 #if INT_MAX == 0x7fff
67     else
68         return 3;
69 #else
70     else if (length < (1<<16))
71         return 3;
72     else if (length < (1<<24))
73         return 4;
74     else
75         return 5;
76 #endif
77 }
78
79 static void
80 der_write_length(unsigned char **buf, size_t length)
81 {
82     if (length < (1<<7)) {
83         *(*buf)++ = (unsigned char)length;
84     } else {
85         *(*buf)++ = (unsigned char)(der_length_size(length)+127);
86 #if INT_MAX > 0x7fff
87         if (length >= (1<<24))
88             *(*buf)++ = (unsigned char)(length>>24);
89         if (length >= (1<<16))
90             *(*buf)++ = (unsigned char)((length>>16)&0xff);
91 #endif
92         if (length >= (1<<8))
93             *(*buf)++ = (unsigned char)((length>>8)&0xff);
94         *(*buf)++ = (unsigned char)(length&0xff);
95     }
96 }
97
98 /* returns decoded length, or < 0 on failure.  Advances buf and
99    decrements bufsize */
100
101 static int
102 der_read_length(unsigned char **buf, ssize_t *bufsize)
103 {
104     unsigned char sf;
105     int ret;
106
107     if (*bufsize < 1)
108         return -1;
109
110     sf = *(*buf)++;
111     (*bufsize)--;
112     if (sf & 0x80) {
113         if ((sf &= 0x7f) > ((*bufsize)-1))
114             return -1;
115         if (sf > sizeof(int))
116             return -1;
117         ret = 0;
118         for (; sf; sf--) {
119             ret = (ret<<8) + (*(*buf)++);
120             (*bufsize)--;
121         }
122     } else {
123         ret = sf;
124     }
125
126     return ret;
127 }
128
129 /* returns the length of a token, given the mech oid and the body size */
130
131 size_t
132 gs2_token_size(const gss_OID_desc *mech, size_t body_size)
133 {
134     /* set body_size to sequence contents size */
135     body_size += 2 + (size_t) mech->length;         /* NEED overflow check */
136     return 1 + der_length_size(body_size) + body_size;
137 }
138
139 /* fills in a buffer with the token header.  The buffer is assumed to
140    be the right size.  buf is advanced past the token header */
141
142 void
143 gs2_make_token_header(
144     const gss_OID_desc *mech,
145     size_t body_size,
146     unsigned char **buf)
147 {
148     *(*buf)++ = 0x60;
149     der_write_length(buf, 2 + mech->length + body_size);
150     *(*buf)++ = 0x06;
151     *(*buf)++ = (unsigned char)mech->length;
152     memcpy(*buf, mech->elements, mech->length);
153     *buf += mech->length;
154 }
155
156 /*
157  * Given a buffer containing a token, reads and verifies the token,
158  * leaving buf advanced past the token header, and setting body_size
159  * to the number of remaining bytes.  Returns 0 on success,
160  * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
161  * mechanism in the token does not match the mech argument.  buf and
162  * *body_size are left unmodified on error.
163  */
164
165 OM_uint32
166 gs2_verify_token_header(OM_uint32 *minor,
167                         gss_const_OID mech,
168                         size_t *body_size,
169                         unsigned char **buf_in,
170                         size_t toksize_in)
171 {
172     unsigned char *buf = *buf_in;
173     ssize_t seqsize;
174     gss_OID_desc toid;
175     ssize_t toksize = (ssize_t)toksize_in;
176
177     *minor = 0;
178
179     if ((toksize -= 1) < 0)
180         return GSS_S_DEFECTIVE_TOKEN;
181
182     if (*buf++ != 0x60)
183         return GSS_S_DEFECTIVE_TOKEN;
184
185     seqsize = der_read_length(&buf, &toksize);
186     if (seqsize < 0)
187         return GSS_S_DEFECTIVE_TOKEN;
188
189     if (seqsize != toksize)
190         return GSS_S_DEFECTIVE_TOKEN;
191
192     if ((toksize -= 1) < 0)
193         return GSS_S_DEFECTIVE_TOKEN;
194
195     if (*buf++ != 0x06)
196         return GSS_S_DEFECTIVE_TOKEN;
197
198     if ((toksize -= 1) < 0)
199         return GSS_S_DEFECTIVE_TOKEN;
200
201     toid.length = *buf++;
202
203     if ((toksize -= toid.length) < 0)
204         return GSS_S_DEFECTIVE_TOKEN;
205
206     toid.elements = buf;
207     buf += toid.length;
208
209     if (!g_OID_equal(&toid, mech))
210         return GSS_S_BAD_MECH;
211
212     *buf_in = buf;
213     *body_size = toksize;
214
215     return GSS_S_COMPLETE;
216 }