Path: blob/main/crypto/krb5/src/lib/gssapi/generic/util_token.c
39562 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 1993 by OpenVision Technologies, Inc.3*4* Permission to use, copy, modify, distribute, and sell this software5* and its documentation for any purpose is hereby granted without fee,6* provided that the above copyright notice appears in all copies and7* that both that copyright notice and this permission notice appear in8* supporting documentation, and that the name of OpenVision not be used9* in advertising or publicity pertaining to distribution of the software10* without specific, written prior permission. OpenVision makes no11* representations about the suitability of this software for any12* 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 NO16* EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR17* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF18* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR19* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR20* PERFORMANCE OF THIS SOFTWARE.21*/2223#include "gssapiP_generic.h"24#include "k5-der.h"25#ifdef HAVE_MEMORY_H26#include <memory.h>27#endif28#include <limits.h>2930/*31* $Id$32*/3334/* Return the length of an RFC 4121 token with RFC 2743 token framing, given35* the mech oid and the body size (without the two-byte RFC 4121 token ID). */36unsigned int37g_token_size(const gss_OID_desc * mech, unsigned int body_size)38{39size_t mech_der_len = k5_der_value_len(mech->length);4041return k5_der_value_len(mech_der_len + 2 + body_size);42}4344/*45* Add RFC 2743 generic token framing to buf with room left for body_size bytes46* in the sequence to be added by the caller. If tok_type is not -1, add it as47* a two-byte RFC 4121 token identifier after the framing and include room for48* it in the sequence.49*/50void51g_make_token_header(struct k5buf *buf, const gss_OID_desc *mech,52size_t body_size, int tok_type)53{54size_t tok_len = (tok_type == -1) ? 0 : 2;55size_t seq_len = k5_der_value_len(mech->length) + body_size + tok_len;5657k5_der_add_taglen(buf, 0x60, seq_len);58k5_der_add_value(buf, 0x06, mech->elements, mech->length);59if (tok_type != -1)60k5_buf_add_uint16_be(buf, tok_type);61}6263/*64* If a valid GSSAPI generic token header is present at the beginning of *in,65* advance past it, set *oid_out to the mechanism OID in the header, set66* *token_len_out to the total token length (including the header) as indicated67* by length of the outermost DER value, and return true. Otherwise return68* false, leaving *in unchanged if it did not begin with a 0x60 byte.69*70* Do not verify that the outermost length matches or fits within in->len, as71* we need to be able to handle a detached header for krb5 IOV unwrap. It is72* the caller's responsibility to validate *token_len_out if necessary.73*/74int75g_get_token_header(struct k5input *in, gss_OID oid_out, size_t *token_len_out)76{77size_t len, tlen;78const uint8_t *orig_ptr = in->ptr;79struct k5input oidbytes;8081/* Read the outermost tag and length, and compute the full token length. */82if (!k5_der_get_taglen(in, 0x60, &len))83return 0;84tlen = len + (in->ptr - orig_ptr);8586/* Read the mechanism OID. */87if (!k5_der_get_value(in, 0x06, &oidbytes))88return 0;89oid_out->length = oidbytes.len;90oid_out->elements = (uint8_t *)oidbytes.ptr;9192*token_len_out = tlen;93return 1;94}9596/*97* If a token header for expected_mech is present in *in and the token length98* indicated by the header is equal to in->len, advance past the header and99* return true. Otherwise return false. Leave *in unmodified if no token100* header is present or it is for a different mechanism.101*/102int103g_verify_token_header(struct k5input *in, gss_const_OID expected_mech)104{105struct k5input orig = *in;106gss_OID_desc mech;107size_t tlen, orig_len = in->len;108109if (!g_get_token_header(in, &mech, &tlen) || tlen != orig_len ||110!g_OID_equal(&mech, expected_mech)) {111*in = orig;112return 0;113}114return 1;115}116117118