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