xref: /freebsd/crypto/krb5/src/lib/gssapi/generic/util_token.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright 1993 by OpenVision Technologies, Inc.
4*7f2fe78bSCy Schubert  *
5*7f2fe78bSCy Schubert  * Permission to use, copy, modify, distribute, and sell this software
6*7f2fe78bSCy Schubert  * and its documentation for any purpose is hereby granted without fee,
7*7f2fe78bSCy Schubert  * provided that the above copyright notice appears in all copies and
8*7f2fe78bSCy Schubert  * that both that copyright notice and this permission notice appear in
9*7f2fe78bSCy Schubert  * supporting documentation, and that the name of OpenVision not be used
10*7f2fe78bSCy Schubert  * in advertising or publicity pertaining to distribution of the software
11*7f2fe78bSCy Schubert  * without specific, written prior permission. OpenVision makes no
12*7f2fe78bSCy Schubert  * representations about the suitability of this software for any
13*7f2fe78bSCy Schubert  * purpose.  It is provided "as is" without express or implied warranty.
14*7f2fe78bSCy Schubert  *
15*7f2fe78bSCy Schubert  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16*7f2fe78bSCy Schubert  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17*7f2fe78bSCy Schubert  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18*7f2fe78bSCy Schubert  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19*7f2fe78bSCy Schubert  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20*7f2fe78bSCy Schubert  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*7f2fe78bSCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
22*7f2fe78bSCy Schubert  */
23*7f2fe78bSCy Schubert 
24*7f2fe78bSCy Schubert #include "gssapiP_generic.h"
25*7f2fe78bSCy Schubert #include "k5-der.h"
26*7f2fe78bSCy Schubert #ifdef HAVE_MEMORY_H
27*7f2fe78bSCy Schubert #include <memory.h>
28*7f2fe78bSCy Schubert #endif
29*7f2fe78bSCy Schubert #include <limits.h>
30*7f2fe78bSCy Schubert 
31*7f2fe78bSCy Schubert /*
32*7f2fe78bSCy Schubert  * $Id$
33*7f2fe78bSCy Schubert  */
34*7f2fe78bSCy Schubert 
35*7f2fe78bSCy Schubert /* Return the length of an RFC 4121 token with RFC 2743 token framing, given
36*7f2fe78bSCy Schubert  * the mech oid and the body size (without the two-byte RFC 4121 token ID). */
37*7f2fe78bSCy Schubert unsigned int
g_token_size(const gss_OID_desc * mech,unsigned int body_size)38*7f2fe78bSCy Schubert g_token_size(const gss_OID_desc * mech, unsigned int body_size)
39*7f2fe78bSCy Schubert {
40*7f2fe78bSCy Schubert     size_t mech_der_len = k5_der_value_len(mech->length);
41*7f2fe78bSCy Schubert 
42*7f2fe78bSCy Schubert     return k5_der_value_len(mech_der_len + 2 + body_size);
43*7f2fe78bSCy Schubert }
44*7f2fe78bSCy Schubert 
45*7f2fe78bSCy Schubert /*
46*7f2fe78bSCy Schubert  * Add RFC 2743 generic token framing to buf with room left for body_size bytes
47*7f2fe78bSCy Schubert  * in the sequence to be added by the caller.  If tok_type is not -1, add it as
48*7f2fe78bSCy Schubert  * a two-byte RFC 4121 token identifier after the framing and include room for
49*7f2fe78bSCy Schubert  * it in the sequence.
50*7f2fe78bSCy Schubert  */
51*7f2fe78bSCy Schubert void
g_make_token_header(struct k5buf * buf,const gss_OID_desc * mech,size_t body_size,int tok_type)52*7f2fe78bSCy Schubert g_make_token_header(struct k5buf *buf, const gss_OID_desc *mech,
53*7f2fe78bSCy Schubert                     size_t body_size, int tok_type)
54*7f2fe78bSCy Schubert {
55*7f2fe78bSCy Schubert     size_t tok_len = (tok_type == -1) ? 0 : 2;
56*7f2fe78bSCy Schubert     size_t seq_len = k5_der_value_len(mech->length) + body_size + tok_len;
57*7f2fe78bSCy Schubert 
58*7f2fe78bSCy Schubert     k5_der_add_taglen(buf, 0x60, seq_len);
59*7f2fe78bSCy Schubert     k5_der_add_value(buf, 0x06, mech->elements, mech->length);
60*7f2fe78bSCy Schubert     if (tok_type != -1)
61*7f2fe78bSCy Schubert         k5_buf_add_uint16_be(buf, tok_type);
62*7f2fe78bSCy Schubert }
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert /*
65*7f2fe78bSCy Schubert  * Given a buffer containing a token, reads and verifies the token,
66*7f2fe78bSCy Schubert  * leaving buf advanced past the token header, and setting body_size
67*7f2fe78bSCy Schubert  * to the number of remaining bytes.  Returns 0 on success,
68*7f2fe78bSCy Schubert  * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
69*7f2fe78bSCy Schubert  * mechanism in the token does not match the mech argument.  buf and
70*7f2fe78bSCy Schubert  * *body_size are left unmodified on error.
71*7f2fe78bSCy Schubert  */
72*7f2fe78bSCy Schubert 
73*7f2fe78bSCy Schubert gss_int32
g_verify_token_header(const gss_OID_desc * mech,unsigned int * body_size,unsigned char ** buf_in,int tok_type,unsigned int toksize_in,int flags)74*7f2fe78bSCy Schubert g_verify_token_header(
75*7f2fe78bSCy Schubert     const gss_OID_desc * mech,
76*7f2fe78bSCy Schubert     unsigned int *body_size,
77*7f2fe78bSCy Schubert     unsigned char **buf_in,
78*7f2fe78bSCy Schubert     int tok_type,
79*7f2fe78bSCy Schubert     unsigned int toksize_in,
80*7f2fe78bSCy Schubert     int flags)
81*7f2fe78bSCy Schubert {
82*7f2fe78bSCy Schubert     struct k5input in, mech_der;
83*7f2fe78bSCy Schubert     gss_OID_desc toid;
84*7f2fe78bSCy Schubert 
85*7f2fe78bSCy Schubert     k5_input_init(&in, *buf_in, toksize_in);
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert     if (k5_der_get_value(&in, 0x60, &in)) {
88*7f2fe78bSCy Schubert         if (in.ptr + in.len != *buf_in + toksize_in)
89*7f2fe78bSCy Schubert             return G_BAD_TOK_HEADER;
90*7f2fe78bSCy Schubert         if (!k5_der_get_value(&in, 0x06, &mech_der))
91*7f2fe78bSCy Schubert             return G_BAD_TOK_HEADER;
92*7f2fe78bSCy Schubert         toid.elements = (uint8_t *)mech_der.ptr;
93*7f2fe78bSCy Schubert         toid.length = mech_der.len;
94*7f2fe78bSCy Schubert         if (!g_OID_equal(&toid, mech))
95*7f2fe78bSCy Schubert             return G_WRONG_MECH;
96*7f2fe78bSCy Schubert     } else if (flags & G_VFY_TOKEN_HDR_WRAPPER_REQUIRED) {
97*7f2fe78bSCy Schubert         return G_BAD_TOK_HEADER;
98*7f2fe78bSCy Schubert     }
99*7f2fe78bSCy Schubert 
100*7f2fe78bSCy Schubert     if (tok_type != -1) {
101*7f2fe78bSCy Schubert         if (k5_input_get_uint16_be(&in) != tok_type)
102*7f2fe78bSCy Schubert             return in.status ? G_BAD_TOK_HEADER : G_WRONG_TOKID;
103*7f2fe78bSCy Schubert     }
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert     *buf_in = (uint8_t *)in.ptr;
106*7f2fe78bSCy Schubert     *body_size = in.len;
107*7f2fe78bSCy Schubert     return 0;
108*7f2fe78bSCy Schubert }
109