1178825Sdfr/*
2233294Sstas * Copyright (c) 2003 - 2005 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7178825Sdfr *
8233294Sstas * Redistribution and use in source and binary forms, with or without
9233294Sstas * modification, are permitted provided that the following conditions
10233294Sstas * are met:
11178825Sdfr *
12233294Sstas * 1. Redistributions of source code must retain the above copyright
13233294Sstas *    notice, this list of conditions and the following disclaimer.
14178825Sdfr *
15233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
16233294Sstas *    notice, this list of conditions and the following disclaimer in the
17233294Sstas *    documentation and/or other materials provided with the distribution.
18178825Sdfr *
19233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
20233294Sstas *    may be used to endorse or promote products derived from this software
21233294Sstas *    without specific prior written permission.
22233294Sstas *
23233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33233294Sstas * SUCH DAMAGE.
34178825Sdfr */
35178825Sdfr
36178825Sdfr#include "der_locl.h"
37178825Sdfr#include "heim_asn1.h"
38178825Sdfr
39233294SstasRCSID("$Id$");
40178825Sdfr
41178825Sdfrint
42233294Sstasencode_heim_any(unsigned char *p, size_t len,
43178825Sdfr		const heim_any *data, size_t *size)
44178825Sdfr{
45233294Sstas    return der_put_octet_string (p, len, data, size);
46178825Sdfr}
47178825Sdfr
48178825Sdfrint
49233294Sstasdecode_heim_any(const unsigned char *p, size_t len,
50178825Sdfr		heim_any *data, size_t *size)
51178825Sdfr{
52178825Sdfr    size_t len_len, length, l;
53178825Sdfr    Der_class thisclass;
54178825Sdfr    Der_type thistype;
55178825Sdfr    unsigned int thistag;
56178825Sdfr    int e;
57178825Sdfr
58178825Sdfr    memset(data, 0, sizeof(*data));
59178825Sdfr
60178825Sdfr    e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
61178825Sdfr    if (e) return e;
62178825Sdfr    if (l > len)
63178825Sdfr	return ASN1_OVERFLOW;
64178825Sdfr    e = der_get_length(p + l, len - l, &length, &len_len);
65178825Sdfr    if (e) return e;
66233294Sstas    if (length == ASN1_INDEFINITE) {
67233294Sstas        if (len < len_len + l)
68233294Sstas	    return ASN1_OVERFLOW;
69233294Sstas	length = len - (len_len + l);
70233294Sstas    } else {
71233294Sstas	if (len < length + len_len + l)
72233294Sstas	    return ASN1_OVERFLOW;
73233294Sstas    }
74178825Sdfr
75178825Sdfr    data->data = malloc(length + len_len + l);
76178825Sdfr    if (data->data == NULL)
77178825Sdfr	return ENOMEM;
78178825Sdfr    data->length = length + len_len + l;
79178825Sdfr    memcpy(data->data, p, length + len_len + l);
80178825Sdfr
81178825Sdfr    if (size)
82178825Sdfr	*size = length + len_len + l;
83178825Sdfr
84178825Sdfr    return 0;
85178825Sdfr}
86178825Sdfr
87178825Sdfrvoid
88178825Sdfrfree_heim_any(heim_any *data)
89178825Sdfr{
90233294Sstas    der_free_octet_string(data);
91178825Sdfr}
92178825Sdfr
93178825Sdfrsize_t
94178825Sdfrlength_heim_any(const heim_any *data)
95178825Sdfr{
96178825Sdfr    return data->length;
97178825Sdfr}
98178825Sdfr
99178825Sdfrint
100178825Sdfrcopy_heim_any(const heim_any *from, heim_any *to)
101178825Sdfr{
102233294Sstas    return der_copy_octet_string(from, to);
103178825Sdfr}
104178825Sdfr
105178825Sdfrint
106233294Sstasencode_heim_any_set(unsigned char *p, size_t len,
107178825Sdfr		    const heim_any_set *data, size_t *size)
108178825Sdfr{
109233294Sstas    return der_put_octet_string (p, len, data, size);
110178825Sdfr}
111178825Sdfr
112178825Sdfrint
113233294Sstasdecode_heim_any_set(const unsigned char *p, size_t len,
114178825Sdfr		heim_any_set *data, size_t *size)
115178825Sdfr{
116233294Sstas    return der_get_octet_string(p, len, data, size);
117178825Sdfr}
118178825Sdfr
119178825Sdfrvoid
120178825Sdfrfree_heim_any_set(heim_any_set *data)
121178825Sdfr{
122233294Sstas    der_free_octet_string(data);
123178825Sdfr}
124178825Sdfr
125178825Sdfrsize_t
126178825Sdfrlength_heim_any_set(const heim_any *data)
127178825Sdfr{
128233294Sstas    return data->length;
129178825Sdfr}
130178825Sdfr
131178825Sdfrint
132178825Sdfrcopy_heim_any_set(const heim_any_set *from, heim_any_set *to)
133178825Sdfr{
134233294Sstas    return der_copy_octet_string(from, to);
135178825Sdfr}
136178825Sdfr
137178825Sdfrint
138178825Sdfrheim_any_cmp(const heim_any_set *p, const heim_any_set *q)
139178825Sdfr{
140233294Sstas    return der_heim_octet_string_cmp(p, q);
141178825Sdfr}
142