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