der_length.c revision 285830
11412Ssundar/*
21412Ssundar * Copyright (c) 1997-2005 Kungliga Tekniska H��gskolan
31412Ssundar * (Royal Institute of Technology, Stockholm, Sweden).
41412Ssundar * All rights reserved.
51412Ssundar *
61412Ssundar * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
71412Ssundar *
81412Ssundar * Redistribution and use in source and binary forms, with or without
91412Ssundar * modification, are permitted provided that the following conditions
101412Ssundar * are met:
111412Ssundar *
121412Ssundar * 1. Redistributions of source code must retain the above copyright
131412Ssundar *    notice, this list of conditions and the following disclaimer.
141412Ssundar *
151412Ssundar * 2. Redistributions in binary form must reproduce the above copyright
161412Ssundar *    notice, this list of conditions and the following disclaimer in the
171412Ssundar *    documentation and/or other materials provided with the distribution.
181412Ssundar *
191412Ssundar * 3. Neither the name of the Institute nor the names of its contributors
201412Ssundar *    may be used to endorse or promote products derived from this software
211412Ssundar *    without specific prior written permission.
221412Ssundar *
231412Ssundar * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
241412Ssundar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251412Ssundar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261412Ssundar * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
271412Ssundar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281412Ssundar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291412Ssundar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301412Ssundar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311412Ssundar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321412Ssundar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331412Ssundar * SUCH DAMAGE.
341412Ssundar */
351412Ssundar
361412Ssundar#include "der_locl.h"
371412Ssundar
381412SsundarRCSID("$Id$");
391412Ssundar
401412Ssundarsize_t
411412Ssundar_heim_len_unsigned (unsigned val)
421412Ssundar{
431412Ssundar    size_t ret = 0;
441412Ssundar    int last_val_gt_128;
451412Ssundar
461412Ssundar    do {
471412Ssundar	++ret;
481412Ssundar	last_val_gt_128 = (val >= 128);
491412Ssundar	val /= 256;
501412Ssundar    } while (val);
511412Ssundar
521412Ssundar    if(last_val_gt_128)
531412Ssundar	ret++;
541412Ssundar
551412Ssundar    return ret;
561412Ssundar}
571412Ssundar
581412Ssundarsize_t
591412Ssundar_heim_len_int (int val)
60{
61    unsigned char q;
62    size_t ret = 0;
63
64    if (val >= 0) {
65	do {
66	    q = val % 256;
67	    ret++;
68	    val /= 256;
69	} while(val);
70	if(q >= 128)
71	    ret++;
72    } else {
73	val = ~val;
74	do {
75	    q = ~(val % 256);
76	    ret++;
77	    val /= 256;
78	} while(val);
79	if(q < 128)
80	    ret++;
81    }
82    return ret;
83}
84
85static size_t
86len_oid (const heim_oid *oid)
87{
88    size_t ret = 1;
89    size_t n;
90
91    for (n = 2; n < oid->length; ++n) {
92	unsigned u = oid->components[n];
93
94	do {
95	    ++ret;
96	    u /= 128;
97	} while(u > 0);
98    }
99    return ret;
100}
101
102size_t
103der_length_len (size_t len)
104{
105    if (len < 128)
106	return 1;
107    else {
108	int ret = 0;
109	do {
110	    ++ret;
111	    len /= 256;
112	} while (len);
113	return ret + 1;
114    }
115}
116
117size_t
118der_length_tag(unsigned int tag)
119{
120    size_t len = 0;
121
122    if(tag <= 30)
123	return 1;
124    while(tag) {
125	tag /= 128;
126	len++;
127    }
128    return len + 1;
129}
130
131size_t
132der_length_integer (const int *data)
133{
134    return _heim_len_int (*data);
135}
136
137size_t
138der_length_unsigned (const unsigned *data)
139{
140    return _heim_len_unsigned(*data);
141}
142
143size_t
144der_length_enumerated (const unsigned *data)
145{
146  return _heim_len_int (*data);
147}
148
149size_t
150der_length_general_string (const heim_general_string *data)
151{
152    return strlen(*data);
153}
154
155size_t
156der_length_utf8string (const heim_utf8_string *data)
157{
158    return strlen(*data);
159}
160
161size_t
162der_length_printable_string (const heim_printable_string *data)
163{
164    return data->length;
165}
166
167size_t
168der_length_ia5_string (const heim_ia5_string *data)
169{
170    return data->length;
171}
172
173size_t
174der_length_bmp_string (const heim_bmp_string *data)
175{
176    return data->length * 2;
177}
178
179size_t
180der_length_universal_string (const heim_universal_string *data)
181{
182    return data->length * 4;
183}
184
185size_t
186der_length_visible_string (const heim_visible_string *data)
187{
188    return strlen(*data);
189}
190
191size_t
192der_length_octet_string (const heim_octet_string *k)
193{
194    return k->length;
195}
196
197size_t
198der_length_heim_integer (const heim_integer *k)
199{
200    if (k->length == 0)
201	return 1;
202    if (k->negative)
203	return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
204    else
205	return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
206}
207
208size_t
209der_length_oid (const heim_oid *k)
210{
211    return len_oid (k);
212}
213
214size_t
215der_length_generalized_time (const time_t *t)
216{
217    heim_octet_string k;
218    size_t ret;
219
220    _heim_time2generalizedtime (*t, &k, 1);
221    ret = k.length;
222    free(k.data);
223    return ret;
224}
225
226size_t
227der_length_utctime (const time_t *t)
228{
229    heim_octet_string k;
230    size_t ret;
231
232    _heim_time2generalizedtime (*t, &k, 0);
233    ret = k.length;
234    free(k.data);
235    return ret;
236}
237
238size_t
239der_length_boolean (const int *k)
240{
241    return 1;
242}
243
244size_t
245der_length_bit_string (const heim_bit_string *k)
246{
247    return (k->length + 7) / 8 + 1;
248}
249