1 /*
2 * Copyright (c) 1997-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
38 RCSID("$Id$");
39
40 size_t
_heim_len_unsigned(unsigned val)41 _heim_len_unsigned (unsigned val)
42 {
43 size_t ret = 0;
44 int last_val_gt_128;
45
46 do {
47 ++ret;
48 last_val_gt_128 = (val >= 128);
49 val /= 256;
50 } while (val);
51
52 if(last_val_gt_128)
53 ret++;
54
55 return ret;
56 }
57
58 size_t
_heim_len_unsigned64(uint64_t val)59 _heim_len_unsigned64 (uint64_t val)
60 {
61 size_t ret = 0;
62 int last_val_gt_128;
63
64 do {
65 ++ret;
66 last_val_gt_128 = (val >= 128);
67 val /= 256;
68 } while (val);
69
70 if(last_val_gt_128)
71 ret++;
72
73 return ret;
74 }
75
76 size_t
_heim_len_int(int val)77 _heim_len_int (int val)
78 {
79 unsigned char q;
80 size_t ret = 0;
81
82 if (val >= 0) {
83 do {
84 q = val % 256;
85 ret++;
86 val /= 256;
87 } while(val);
88 if(q >= 128)
89 ret++;
90 } else {
91 val = ~val;
92 do {
93 q = ~(val % 256);
94 ret++;
95 val /= 256;
96 } while(val);
97 if(q < 128)
98 ret++;
99 }
100 return ret;
101 }
102
103 size_t
_heim_len_int64(int64_t val)104 _heim_len_int64 (int64_t val)
105 {
106 unsigned char q;
107 size_t ret = 0;
108
109 if (val >= 0) {
110 do {
111 q = val % 256;
112 ret++;
113 val /= 256;
114 } while(val);
115 if(q >= 128)
116 ret++;
117 } else {
118 val = ~val;
119 do {
120 q = ~(val % 256);
121 ret++;
122 val /= 256;
123 } while(val);
124 if(q < 128)
125 ret++;
126 }
127 return ret;
128 }
129
130 static size_t
len_oid(const heim_oid * oid)131 len_oid (const heim_oid *oid)
132 {
133 size_t ret = 1;
134 size_t n;
135
136 for (n = 2; n < oid->length; ++n) {
137 unsigned u = oid->components[n];
138
139 do {
140 ++ret;
141 u /= 128;
142 } while(u > 0);
143 }
144 return ret;
145 }
146
147 size_t
der_length_len(size_t len)148 der_length_len (size_t len)
149 {
150 if (len < 128)
151 return 1;
152 else {
153 int ret = 0;
154 do {
155 ++ret;
156 len /= 256;
157 } while (len);
158 return ret + 1;
159 }
160 }
161
162 size_t
der_length_tag(unsigned int tag)163 der_length_tag(unsigned int tag)
164 {
165 size_t len = 0;
166
167 if(tag <= 30)
168 return 1;
169 while(tag) {
170 tag /= 128;
171 len++;
172 }
173 return len + 1;
174 }
175
176 size_t
der_length_integer(const int * data)177 der_length_integer (const int *data)
178 {
179 return _heim_len_int (*data);
180 }
181
182 size_t
der_length_integer64(const int64_t * data)183 der_length_integer64 (const int64_t *data)
184 {
185 return _heim_len_int64 (*data);
186 }
187
188 size_t
der_length_unsigned(const unsigned * data)189 der_length_unsigned (const unsigned *data)
190 {
191 return _heim_len_unsigned(*data);
192 }
193
194 size_t
der_length_unsigned64(const uint64_t * data)195 der_length_unsigned64 (const uint64_t *data)
196 {
197 return _heim_len_unsigned64(*data);
198 }
199
200 size_t
der_length_enumerated(const unsigned * data)201 der_length_enumerated (const unsigned *data)
202 {
203 return _heim_len_int (*data);
204 }
205
206 size_t
der_length_general_string(const heim_general_string * data)207 der_length_general_string (const heim_general_string *data)
208 {
209 return strlen(*data);
210 }
211
212 size_t
der_length_utf8string(const heim_utf8_string * data)213 der_length_utf8string (const heim_utf8_string *data)
214 {
215 return strlen(*data);
216 }
217
218 size_t
der_length_printable_string(const heim_printable_string * data)219 der_length_printable_string (const heim_printable_string *data)
220 {
221 return data->length;
222 }
223
224 size_t
der_length_ia5_string(const heim_ia5_string * data)225 der_length_ia5_string (const heim_ia5_string *data)
226 {
227 return data->length;
228 }
229
230 size_t
der_length_bmp_string(const heim_bmp_string * data)231 der_length_bmp_string (const heim_bmp_string *data)
232 {
233 return data->length * 2;
234 }
235
236 size_t
der_length_universal_string(const heim_universal_string * data)237 der_length_universal_string (const heim_universal_string *data)
238 {
239 return data->length * 4;
240 }
241
242 size_t
der_length_visible_string(const heim_visible_string * data)243 der_length_visible_string (const heim_visible_string *data)
244 {
245 return strlen(*data);
246 }
247
248 size_t
der_length_octet_string(const heim_octet_string * k)249 der_length_octet_string (const heim_octet_string *k)
250 {
251 return k->length;
252 }
253
254 size_t
der_length_heim_integer(const heim_integer * k)255 der_length_heim_integer (const heim_integer *k)
256 {
257 if (k->length == 0)
258 return 1;
259 if (k->negative)
260 return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
261 else
262 return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
263 }
264
265 size_t
der_length_oid(const heim_oid * k)266 der_length_oid (const heim_oid *k)
267 {
268 return len_oid (k);
269 }
270
271 size_t
der_length_generalized_time(const time_t * t)272 der_length_generalized_time (const time_t *t)
273 {
274 heim_octet_string k;
275 size_t ret;
276
277 _heim_time2generalizedtime (*t, &k, 1);
278 ret = k.length;
279 free(k.data);
280 return ret;
281 }
282
283 size_t
der_length_utctime(const time_t * t)284 der_length_utctime (const time_t *t)
285 {
286 heim_octet_string k;
287 size_t ret;
288
289 _heim_time2generalizedtime (*t, &k, 0);
290 ret = k.length;
291 free(k.data);
292 return ret;
293 }
294
295 size_t
der_length_boolean(const int * k)296 der_length_boolean (const int *k)
297 {
298 return 1;
299 }
300
301 size_t
der_length_bit_string(const heim_bit_string * k)302 der_length_bit_string (const heim_bit_string *k)
303 {
304 return (k->length + 7) / 8 + 1;
305 }
306