/src/samba/third_party/heimdal/lib/asn1/extra.c
Line | Count | Source |
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 | 0 | { |
47 | 0 | return der_put_octet_string (p, len, data, size); |
48 | 0 | } |
49 | | |
50 | | int ASN1CALL |
51 | | decode_heim_any(const unsigned char *p, size_t len, |
52 | | heim_any *data, size_t *size) |
53 | 0 | { |
54 | 0 | size_t len_len, length, l; |
55 | 0 | Der_class thisclass; |
56 | 0 | Der_type thistype; |
57 | 0 | unsigned int thistag; |
58 | 0 | int e; |
59 | |
|
60 | 0 | memset(data, 0, sizeof(*data)); |
61 | |
|
62 | 0 | e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l); |
63 | 0 | if (e) return e; |
64 | 0 | if (l > len) |
65 | 0 | return ASN1_OVERFLOW; |
66 | 0 | e = der_get_length(p + l, len - l, &length, &len_len); |
67 | 0 | if (e) return e; |
68 | 0 | if (length == ASN1_INDEFINITE) { |
69 | 0 | if (len < len_len + l) |
70 | 0 | return ASN1_OVERFLOW; |
71 | 0 | length = len - (len_len + l); |
72 | 0 | } else { |
73 | 0 | if (len < length + len_len + l) |
74 | 0 | return ASN1_OVERFLOW; |
75 | 0 | } |
76 | | |
77 | 0 | data->data = malloc(length + len_len + l); |
78 | 0 | if (data->data == NULL) |
79 | 0 | return ENOMEM; |
80 | 0 | data->length = length + len_len + l; |
81 | 0 | memcpy(data->data, p, length + len_len + l); |
82 | |
|
83 | 0 | if (size) |
84 | 0 | *size = length + len_len + l; |
85 | |
|
86 | 0 | return 0; |
87 | 0 | } |
88 | | |
89 | | void ASN1CALL |
90 | | free_heim_any(heim_any *data) |
91 | 0 | { |
92 | 0 | der_free_octet_string(data); |
93 | 0 | } |
94 | | |
95 | | char * ASN1CALL |
96 | | print_heim_any(const heim_any *data, int flags) |
97 | 0 | { |
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 | 0 | } |
112 | | |
113 | | size_t ASN1CALL |
114 | | length_heim_any(const heim_any *data) |
115 | 0 | { |
116 | 0 | return data->length; |
117 | 0 | } |
118 | | |
119 | | int ASN1CALL |
120 | | copy_heim_any(const heim_any *from, heim_any *to) |
121 | 0 | { |
122 | 0 | return der_copy_octet_string(from, to); |
123 | 0 | } |
124 | | |
125 | | int ASN1CALL |
126 | | encode_HEIM_ANY(unsigned char *p, size_t len, |
127 | | const heim_any *data, size_t *size) |
128 | 0 | { |
129 | 0 | return encode_heim_any(p, len, data, size); |
130 | 0 | } |
131 | | |
132 | | int ASN1CALL |
133 | | decode_HEIM_ANY(const unsigned char *p, size_t len, |
134 | | heim_any *data, size_t *size) |
135 | 0 | { |
136 | 0 | return decode_heim_any(p, len, data, size); |
137 | 0 | } |
138 | | |
139 | | void ASN1CALL |
140 | | free_HEIM_ANY(heim_any *data) |
141 | 0 | { |
142 | 0 | der_free_octet_string(data); |
143 | 0 | } |
144 | | |
145 | | char * ASN1CALL |
146 | | print_HEIM_ANY(const heim_any *data, int flags) |
147 | 0 | { |
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 | 0 | } |
162 | | |
163 | | size_t ASN1CALL |
164 | | length_HEIM_ANY(const heim_any *data) |
165 | 0 | { |
166 | 0 | return data->length; |
167 | 0 | } |
168 | | |
169 | | int ASN1CALL |
170 | | copy_HEIM_ANY(const heim_any *from, heim_any *to) |
171 | 0 | { |
172 | 0 | return der_copy_octet_string(from, to); |
173 | 0 | } |
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 | 0 | { |
179 | 0 | return der_put_octet_string (p, len, data, size); |
180 | 0 | } |
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 | 0 | { |
186 | 0 | return der_get_octet_string(p, len, data, size); |
187 | 0 | } |
188 | | |
189 | | void ASN1CALL |
190 | | free_heim_any_set(heim_any_set *data) |
191 | 0 | { |
192 | 0 | der_free_octet_string(data); |
193 | 0 | } |
194 | | |
195 | | char * ASN1CALL |
196 | | print_heim_any_set(const heim_any_set *data, int flags) |
197 | 0 | { |
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 | 0 | } |
212 | | |
213 | | size_t ASN1CALL |
214 | | length_heim_any_set(const heim_any *data) |
215 | 0 | { |
216 | 0 | return data->length; |
217 | 0 | } |
218 | | |
219 | | int ASN1CALL |
220 | | copy_heim_any_set(const heim_any_set *from, heim_any_set *to) |
221 | 0 | { |
222 | 0 | return der_copy_octet_string(from, to); |
223 | 0 | } |
224 | | |
225 | | int ASN1CALL |
226 | | heim_any_cmp(const heim_any_set *p, const heim_any_set *q) |
227 | 0 | { |
228 | 0 | return der_heim_octet_string_cmp(p, q); |
229 | 0 | } |
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 | 0 | { |
235 | 0 | return encode_heim_any_set(p, len, data, size); |
236 | 0 | } |
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 | 0 | { |
242 | 0 | return decode_heim_any_set(p, len, data, size); |
243 | 0 | } |
244 | | |
245 | | void ASN1CALL |
246 | | free_HEIM_ANY_SET(heim_any_set *data) |
247 | 0 | { |
248 | 0 | der_free_octet_string(data); |
249 | 0 | } |
250 | | |
251 | | char * ASN1CALL |
252 | | print_HEIM_ANY_SET(const heim_any_set *data, int flags) |
253 | 0 | { |
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 | 0 | } |
268 | | |
269 | | size_t ASN1CALL |
270 | | length_HEIM_ANY_SET(const heim_any *data) |
271 | 0 | { |
272 | 0 | return data->length; |
273 | 0 | } |
274 | | |
275 | | int ASN1CALL |
276 | | copy_HEIM_ANY_SET(const heim_any_set *from, heim_any_set *to) |
277 | 0 | { |
278 | 0 | return der_copy_octet_string(from, to); |
279 | 0 | } |
280 | | |
281 | | int ASN1CALL |
282 | | HEIM_ANY_cmp(const heim_any_set *p, const heim_any_set *q) |
283 | 0 | { |
284 | 0 | return der_heim_octet_string_cmp(p, q); |
285 | 0 | } |