xref: /freebsd/crypto/heimdal/lib/asn1/der_length.c (revision 580744621f33383027108364dcadad718df46ffe)
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
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
59 _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 
85 static size_t
86 len_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 
102 size_t
103 der_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 
117 size_t
118 der_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 
131 size_t
132 der_length_integer (const int *data)
133 {
134     return _heim_len_int (*data);
135 }
136 
137 size_t
138 der_length_unsigned (const unsigned *data)
139 {
140     return _heim_len_unsigned(*data);
141 }
142 
143 size_t
144 der_length_enumerated (const unsigned *data)
145 {
146   return _heim_len_int (*data);
147 }
148 
149 size_t
150 der_length_general_string (const heim_general_string *data)
151 {
152     return strlen(*data);
153 }
154 
155 size_t
156 der_length_utf8string (const heim_utf8_string *data)
157 {
158     return strlen(*data);
159 }
160 
161 size_t
162 der_length_printable_string (const heim_printable_string *data)
163 {
164     return data->length;
165 }
166 
167 size_t
168 der_length_ia5_string (const heim_ia5_string *data)
169 {
170     return data->length;
171 }
172 
173 size_t
174 der_length_bmp_string (const heim_bmp_string *data)
175 {
176     return data->length * 2;
177 }
178 
179 size_t
180 der_length_universal_string (const heim_universal_string *data)
181 {
182     return data->length * 4;
183 }
184 
185 size_t
186 der_length_visible_string (const heim_visible_string *data)
187 {
188     return strlen(*data);
189 }
190 
191 size_t
192 der_length_octet_string (const heim_octet_string *k)
193 {
194     return k->length;
195 }
196 
197 size_t
198 der_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 
208 size_t
209 der_length_oid (const heim_oid *k)
210 {
211     return len_oid (k);
212 }
213 
214 size_t
215 der_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 
226 size_t
227 der_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 
238 size_t
239 der_length_boolean (const int *k)
240 {
241     return 1;
242 }
243 
244 size_t
245 der_length_bit_string (const heim_bit_string *k)
246 {
247     return (k->length + 7) / 8 + 1;
248 }
249