/*1* Copyright (c) 2003 - 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "der_locl.h"36#include "heim_asn1.h"3738RCSID("$Id$");3940int41encode_heim_any(unsigned char *p, size_t len,42const heim_any *data, size_t *size)43{44return der_put_octet_string (p, len, data, size);45}4647int48decode_heim_any(const unsigned char *p, size_t len,49heim_any *data, size_t *size)50{51size_t len_len, length, l;52Der_class thisclass;53Der_type thistype;54unsigned int thistag;55int e;5657memset(data, 0, sizeof(*data));5859e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);60if (e) return e;61if (l > len)62return ASN1_OVERFLOW;63e = der_get_length(p + l, len - l, &length, &len_len);64if (e) return e;65if (length == ASN1_INDEFINITE) {66if (len < len_len + l)67return ASN1_OVERFLOW;68length = len - (len_len + l);69} else {70if (len < length + len_len + l)71return ASN1_OVERFLOW;72}7374data->data = malloc(length + len_len + l);75if (data->data == NULL)76return ENOMEM;77data->length = length + len_len + l;78memcpy(data->data, p, length + len_len + l);7980if (size)81*size = length + len_len + l;8283return 0;84}8586void87free_heim_any(heim_any *data)88{89der_free_octet_string(data);90}9192size_t93length_heim_any(const heim_any *data)94{95return data->length;96}9798int99copy_heim_any(const heim_any *from, heim_any *to)100{101return der_copy_octet_string(from, to);102}103104int105encode_heim_any_set(unsigned char *p, size_t len,106const heim_any_set *data, size_t *size)107{108return der_put_octet_string (p, len, data, size);109}110111int112decode_heim_any_set(const unsigned char *p, size_t len,113heim_any_set *data, size_t *size)114{115return der_get_octet_string(p, len, data, size);116}117118void119free_heim_any_set(heim_any_set *data)120{121der_free_octet_string(data);122}123124size_t125length_heim_any_set(const heim_any *data)126{127return data->length;128}129130int131copy_heim_any_set(const heim_any_set *from, heim_any_set *to)132{133return der_copy_octet_string(from, to);134}135136int137heim_any_cmp(const heim_any_set *p, const heim_any_set *q)138{139return der_heim_octet_string_cmp(p, q);140}141142143