Line data Source code
1 : /*
2 : * Copyright (c) 2003 - 2005 Kungliga Tekniska Högskolan
3 : * (Royal Institute of Technology, Stockholm, Sweden).
4 : * All rights reserved.
5 : *
6 : * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 : *
8 : * Redistribution and use in source and binary forms, with or without
9 : * modification, are permitted provided that the following conditions
10 : * are met:
11 : *
12 : * 1. Redistributions of source code must retain the above copyright
13 : * notice, this list of conditions and the following disclaimer.
14 : *
15 : * 2. Redistributions in binary form must reproduce the above copyright
16 : * notice, this list of conditions and the following disclaimer in the
17 : * documentation and/or other materials provided with the distribution.
18 : *
19 : * 3. Neither the name of the Institute nor the names of its contributors
20 : * may be used to endorse or promote products derived from this software
21 : * without specific prior written permission.
22 : *
23 : * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 : * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 : * SUCH DAMAGE.
34 : */
35 :
36 : #include "der_locl.h"
37 : #include "heim_asn1.h"
38 : #include <vis.h>
39 : #include <vis-extras.h>
40 :
41 : RCSID("$Id$");
42 :
43 : int ASN1CALL
44 2626 : encode_heim_any(unsigned char *p, size_t len,
45 : const heim_any *data, size_t *size)
46 : {
47 2626 : return der_put_octet_string (p, len, data, size);
48 : }
49 :
50 : int ASN1CALL
51 2284 : decode_heim_any(const unsigned char *p, size_t len,
52 : heim_any *data, size_t *size)
53 : {
54 72 : size_t len_len, length, l;
55 72 : Der_class thisclass;
56 72 : Der_type thistype;
57 72 : unsigned int thistag;
58 72 : int e;
59 :
60 2284 : memset(data, 0, sizeof(*data));
61 :
62 2284 : e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
63 2284 : if (e) return e;
64 2050 : if (l > len)
65 0 : return ASN1_OVERFLOW;
66 2050 : e = der_get_length(p + l, len - l, &length, &len_len);
67 2050 : if (e) return e;
68 2050 : if (length == ASN1_INDEFINITE) {
69 0 : if (len < len_len + l)
70 0 : return ASN1_OVERFLOW;
71 0 : length = len - (len_len + l);
72 : } else {
73 2050 : if (len < length + len_len + l)
74 0 : return ASN1_OVERFLOW;
75 : }
76 :
77 2050 : data->data = malloc(length + len_len + l);
78 2050 : if (data->data == NULL)
79 0 : return ENOMEM;
80 2050 : data->length = length + len_len + l;
81 2050 : memcpy(data->data, p, length + len_len + l);
82 :
83 2050 : if (size)
84 2050 : *size = length + len_len + l;
85 :
86 1978 : return 0;
87 : }
88 :
89 : void ASN1CALL
90 0 : free_heim_any(heim_any *data)
91 : {
92 0 : der_free_octet_string(data);
93 0 : }
94 :
95 : char * ASN1CALL
96 0 : print_heim_any(const heim_any *data, int flags)
97 : {
98 0 : char *s2 = NULL;
99 0 : char *s = der_print_octet_string(data, 0);
100 0 : int r = -1;
101 :
102 0 : (void)flags;
103 0 : if (s)
104 0 : r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
105 0 : free(s);
106 0 : s = NULL;
107 0 : if (r > -1)
108 0 : (void) asprintf(&s, "\"%s\"", s2);
109 0 : free(s2);
110 0 : return s;
111 : }
112 :
113 : size_t ASN1CALL
114 0 : length_heim_any(const heim_any *data)
115 : {
116 0 : return data->length;
117 : }
118 :
119 : int ASN1CALL
120 0 : copy_heim_any(const heim_any *from, heim_any *to)
121 : {
122 0 : return der_copy_octet_string(from, to);
123 : }
124 :
125 : int ASN1CALL
126 2626 : encode_HEIM_ANY(unsigned char *p, size_t len,
127 : const heim_any *data, size_t *size)
128 : {
129 2626 : return encode_heim_any(p, len, data, size);
130 : }
131 :
132 : int ASN1CALL
133 2284 : decode_HEIM_ANY(const unsigned char *p, size_t len,
134 : heim_any *data, size_t *size)
135 : {
136 2284 : return decode_heim_any(p, len, data, size);
137 : }
138 :
139 : void ASN1CALL
140 6223 : free_HEIM_ANY(heim_any *data)
141 : {
142 6223 : der_free_octet_string(data);
143 6223 : }
144 :
145 : char * ASN1CALL
146 0 : print_HEIM_ANY(const heim_any *data, int flags)
147 : {
148 0 : char *s2 = NULL;
149 0 : char *s = der_print_octet_string(data, 0);
150 0 : int r = -1;
151 :
152 0 : (void)flags;
153 0 : if (s)
154 0 : r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
155 0 : free(s);
156 0 : s = NULL;
157 0 : if (r > -1)
158 0 : (void) asprintf(&s, "\"%s\"", s2);
159 0 : free(s2);
160 0 : return s;
161 : }
162 :
163 : size_t ASN1CALL
164 3795 : length_HEIM_ANY(const heim_any *data)
165 : {
166 3795 : return data->length;
167 : }
168 :
169 : int ASN1CALL
170 3049 : copy_HEIM_ANY(const heim_any *from, heim_any *to)
171 : {
172 3049 : return der_copy_octet_string(from, to);
173 : }
174 :
175 : int ASN1CALL
176 0 : encode_heim_any_set(unsigned char *p, size_t len,
177 : const heim_any_set *data, size_t *size)
178 : {
179 0 : return der_put_octet_string (p, len, data, size);
180 : }
181 :
182 : int ASN1CALL
183 0 : decode_heim_any_set(const unsigned char *p, size_t len,
184 : heim_any_set *data, size_t *size)
185 : {
186 0 : return der_get_octet_string(p, len, data, size);
187 : }
188 :
189 : void ASN1CALL
190 0 : free_heim_any_set(heim_any_set *data)
191 : {
192 0 : der_free_octet_string(data);
193 0 : }
194 :
195 : char * ASN1CALL
196 0 : print_heim_any_set(const heim_any_set *data, int flags)
197 : {
198 0 : char *s2 = NULL;
199 0 : char *s = der_print_octet_string(data, 0);
200 0 : int r = -1;
201 :
202 0 : (void)flags;
203 0 : if (s)
204 0 : r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
205 0 : free(s);
206 0 : s = NULL;
207 0 : if (r > -1)
208 0 : (void) asprintf(&s, "\"%s\"", s2);
209 0 : free(s2);
210 0 : return s;
211 : }
212 :
213 : size_t ASN1CALL
214 0 : length_heim_any_set(const heim_any *data)
215 : {
216 0 : return data->length;
217 : }
218 :
219 : int ASN1CALL
220 0 : copy_heim_any_set(const heim_any_set *from, heim_any_set *to)
221 : {
222 0 : return der_copy_octet_string(from, to);
223 : }
224 :
225 : int ASN1CALL
226 74 : heim_any_cmp(const heim_any_set *p, const heim_any_set *q)
227 : {
228 74 : return der_heim_octet_string_cmp(p, q);
229 : }
230 :
231 : int ASN1CALL
232 0 : encode_HEIM_ANY_SET(unsigned char *p, size_t len,
233 : const heim_any_set *data, size_t *size)
234 : {
235 0 : return encode_heim_any_set(p, len, data, size);
236 : }
237 :
238 : int ASN1CALL
239 0 : decode_HEIM_ANY_SET(const unsigned char *p, size_t len,
240 : heim_any_set *data, size_t *size)
241 : {
242 0 : return decode_heim_any_set(p, len, data, size);
243 : }
244 :
245 : void ASN1CALL
246 0 : free_HEIM_ANY_SET(heim_any_set *data)
247 : {
248 0 : der_free_octet_string(data);
249 0 : }
250 :
251 : char * ASN1CALL
252 0 : print_HEIM_ANY_SET(const heim_any_set *data, int flags)
253 : {
254 0 : char *s2 = NULL;
255 0 : char *s = der_print_octet_string(data, 0);
256 0 : int r = -1;
257 :
258 0 : (void)flags;
259 0 : if (s)
260 0 : r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
261 0 : free(s);
262 0 : s = NULL;
263 0 : if (r > -1)
264 0 : (void) asprintf(&s, "\"%s\"", s2);
265 0 : free(s2);
266 0 : return s;
267 : }
268 :
269 : size_t ASN1CALL
270 0 : length_HEIM_ANY_SET(const heim_any *data)
271 : {
272 0 : return data->length;
273 : }
274 :
275 : int ASN1CALL
276 0 : copy_HEIM_ANY_SET(const heim_any_set *from, heim_any_set *to)
277 : {
278 0 : return der_copy_octet_string(from, to);
279 : }
280 :
281 : int ASN1CALL
282 0 : HEIM_ANY_cmp(const heim_any_set *p, const heim_any_set *q)
283 : {
284 0 : return der_heim_octet_string_cmp(p, q);
285 : }
|