1/*
2 * Copyright (c) 2011-12 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright (c) 2003 - 2005 Kungliga Tekniska Högskolan
25 * (Royal Institute of Technology, Stockholm, Sweden).
26 * All rights reserved.
27 *
28 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 *
34 * 1. Redistributions of source code must retain the above copyright
35 *    notice, this list of conditions and the following disclaimer.
36 *
37 * 2. Redistributions in binary form must reproduce the above copyright
38 *    notice, this list of conditions and the following disclaimer in the
39 *    documentation and/or other materials provided with the distribution.
40 *
41 * 3. Neither the name of the Institute nor the names of its contributors
42 *    may be used to endorse or promote products derived from this software
43 *    without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58#include "asn1-der_locl.h"
59#include "asn1-heim_asn1.h"
60
61RCSID("$Id$");
62
63int
64encode_heim_any(unsigned char *p, size_t len,
65		const heim_any *data, size_t *size)
66{
67    return der_put_octet_string (p, len, data, size);
68}
69
70int
71decode_heim_any(const unsigned char *p, size_t len,
72		heim_any *data, size_t *size)
73{
74    size_t len_len, length, l;
75    Der_class thisclass;
76    Der_type thistype;
77    unsigned int thistag;
78    int e;
79
80    memset(data, 0, sizeof(*data));
81
82    e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
83    if (e) return e;
84    if (l > len)
85	return ASN1_OVERFLOW;
86    e = der_get_length(p + l, len - l, &length, &len_len);
87    if (e) return e;
88    if (length == ASN1_INDEFINITE) {
89        if (len < len_len + l)
90	    return ASN1_OVERFLOW;
91	length = len - (len_len + l);
92    } else {
93	if (len < length + len_len + l)
94	    return ASN1_OVERFLOW;
95    }
96
97    data->data = malloc(length + len_len + l);
98    if (data->data == NULL)
99	return ENOMEM;
100    data->length = length + len_len + l;
101    memcpy(data->data, p, length + len_len + l);
102
103    if (size)
104	*size = length + len_len + l;
105
106    return 0;
107}
108
109void
110free_heim_any(heim_any *data)
111{
112    der_free_octet_string(data);
113}
114
115size_t
116length_heim_any(const heim_any *data)
117{
118    return data->length;
119}
120
121int
122copy_heim_any(const heim_any *from, heim_any *to)
123{
124    return der_copy_octet_string(from, to);
125}
126
127int
128encode_heim_any_set(unsigned char *p, size_t len,
129		    const heim_any_set *data, size_t *size)
130{
131    return der_put_octet_string (p, len, data, size);
132}
133
134int
135decode_heim_any_set(const unsigned char *p, size_t len,
136		heim_any_set *data, size_t *size)
137{
138    return der_get_octet_string(p, len, data, size);
139}
140
141void
142free_heim_any_set(heim_any_set *data)
143{
144    der_free_octet_string(data);
145}
146
147size_t
148length_heim_any_set(const heim_any *data)
149{
150    return data->length;
151}
152
153int
154copy_heim_any_set(const heim_any_set *from, heim_any_set *to)
155{
156    return der_copy_octet_string(from, to);
157}
158
159int
160heim_any_cmp(const heim_any_set *p, const heim_any_set *q)
161{
162    return der_heim_octet_string_cmp(p, q);
163}
164