s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / source4 / heimdal / lib / asn1 / extra.c
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 encode_heim_any(unsigned char *p, size_t len,
45                 const heim_any *data, size_t *size)
46 {
47     return der_put_octet_string (p, len, data, size);
48 }
49
50 int ASN1CALL
51 decode_heim_any(const unsigned char *p, size_t len,
52                 heim_any *data, size_t *size)
53 {
54     size_t len_len, length, l;
55     Der_class thisclass;
56     Der_type thistype;
57     unsigned int thistag;
58     int e;
59
60     memset(data, 0, sizeof(*data));
61
62     e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
63     if (e) return e;
64     if (l > len)
65         return ASN1_OVERFLOW;
66     e = der_get_length(p + l, len - l, &length, &len_len);
67     if (e) return e;
68     if (length == ASN1_INDEFINITE) {
69         if (len < len_len + l)
70             return ASN1_OVERFLOW;
71         length = len - (len_len + l);
72     } else {
73         if (len < length + len_len + l)
74             return ASN1_OVERFLOW;
75     }
76
77     data->data = malloc(length + len_len + l);
78     if (data->data == NULL)
79         return ENOMEM;
80     data->length = length + len_len + l;
81     memcpy(data->data, p, length + len_len + l);
82
83     if (size)
84         *size = length + len_len + l;
85
86     return 0;
87 }
88
89 void ASN1CALL
90 free_heim_any(heim_any *data)
91 {
92     der_free_octet_string(data);
93 }
94
95 char * ASN1CALL
96 print_heim_any(const heim_any *data, int flags)
97 {
98     char *s2 = NULL;
99     char *s = der_print_octet_string(data, 0);
100     int r = -1;
101
102     (void)flags;
103     if (s)
104         r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
105     free(s);
106     s = NULL;
107     if (r > -1)
108         r = asprintf(&s, "\"%s\"", s2);
109     free(s2);
110     return s;
111 }
112
113 size_t ASN1CALL
114 length_heim_any(const heim_any *data)
115 {
116     return data->length;
117 }
118
119 int ASN1CALL
120 copy_heim_any(const heim_any *from, heim_any *to)
121 {
122     return der_copy_octet_string(from, to);
123 }
124
125 int ASN1CALL
126 encode_HEIM_ANY(unsigned char *p, size_t len,
127                 const heim_any *data, size_t *size)
128 {
129     return encode_heim_any(p, len, data, size);
130 }
131
132 int ASN1CALL
133 decode_HEIM_ANY(const unsigned char *p, size_t len,
134                 heim_any *data, size_t *size)
135 {
136     return decode_heim_any(p, len, data, size);
137 }
138
139 void ASN1CALL
140 free_HEIM_ANY(heim_any *data)
141 {
142     der_free_octet_string(data);
143 }
144
145 char * ASN1CALL
146 print_HEIM_ANY(const heim_any *data, int flags)
147 {
148     char *s2 = NULL;
149     char *s = der_print_octet_string(data, 0);
150     int r = -1;
151
152     (void)flags;
153     if (s)
154         r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
155     free(s);
156     s = NULL;
157     if (r > -1)
158         r = asprintf(&s, "\"%s\"", s2);
159     free(s2);
160     return s;
161 }
162
163 size_t ASN1CALL
164 length_HEIM_ANY(const heim_any *data)
165 {
166     return data->length;
167 }
168
169 int ASN1CALL
170 copy_HEIM_ANY(const heim_any *from, heim_any *to)
171 {
172     return der_copy_octet_string(from, to);
173 }
174
175 int ASN1CALL
176 encode_heim_any_set(unsigned char *p, size_t len,
177                     const heim_any_set *data, size_t *size)
178 {
179     return der_put_octet_string (p, len, data, size);
180 }
181
182 int ASN1CALL
183 decode_heim_any_set(const unsigned char *p, size_t len,
184                 heim_any_set *data, size_t *size)
185 {
186     return der_get_octet_string(p, len, data, size);
187 }
188
189 void ASN1CALL
190 free_heim_any_set(heim_any_set *data)
191 {
192     der_free_octet_string(data);
193 }
194
195 char * ASN1CALL
196 print_heim_any_set(const heim_any_set *data, int flags)
197 {
198     char *s2 = NULL;
199     char *s = der_print_octet_string(data, 0);
200     int r = -1;
201
202     (void)flags;
203     if (s)
204         r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
205     free(s);
206     s = NULL;
207     if (r > -1)
208         r = asprintf(&s, "\"%s\"", s2);
209     free(s2);
210     return s;
211 }
212
213 size_t ASN1CALL
214 length_heim_any_set(const heim_any *data)
215 {
216     return data->length;
217 }
218
219 int ASN1CALL
220 copy_heim_any_set(const heim_any_set *from, heim_any_set *to)
221 {
222     return der_copy_octet_string(from, to);
223 }
224
225 int ASN1CALL
226 heim_any_cmp(const heim_any_set *p, const heim_any_set *q)
227 {
228     return der_heim_octet_string_cmp(p, q);
229 }
230
231 int ASN1CALL
232 encode_HEIM_ANY_SET(unsigned char *p, size_t len,
233                     const heim_any_set *data, size_t *size)
234 {
235     return encode_heim_any_set(p, len, data, size);
236 }
237
238 int ASN1CALL
239 decode_HEIM_ANY_SET(const unsigned char *p, size_t len,
240                     heim_any_set *data, size_t *size)
241 {
242     return decode_heim_any_set(p, len, data, size);
243 }
244
245 void ASN1CALL
246 free_HEIM_ANY_SET(heim_any_set *data)
247 {
248     der_free_octet_string(data);
249 }
250
251 char * ASN1CALL
252 print_HEIM_ANY_SET(const heim_any_set *data, int flags)
253 {
254     char *s2 = NULL;
255     char *s = der_print_octet_string(data, 0);
256     int r = -1;
257
258     (void)flags;
259     if (s)
260         r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
261     free(s);
262     s = NULL;
263     if (r > -1)
264         r = asprintf(&s, "\"%s\"", s2);
265     free(s2);
266     return s;
267 }
268
269 size_t ASN1CALL
270 length_HEIM_ANY_SET(const heim_any *data)
271 {
272     return data->length;
273 }
274
275 int ASN1CALL
276 copy_HEIM_ANY_SET(const heim_any_set *from, heim_any_set *to)
277 {
278     return der_copy_octet_string(from, to);
279 }
280
281 int ASN1CALL
282 HEIM_ANY_cmp(const heim_any_set *p, const heim_any_set *q)
283 {
284     return der_heim_octet_string_cmp(p, q);
285 }