1 /* $NetBSD: der_length.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2
3 /*
4 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include "der_locl.h"
39
40 __RCSID("NetBSD");
41
42 size_t
_heim_len_unsigned(unsigned val)43 _heim_len_unsigned (unsigned val)
44 {
45 size_t ret = 0;
46 int last_val_gt_128;
47
48 do {
49 ++ret;
50 last_val_gt_128 = (val >= 128);
51 val /= 256;
52 } while (val);
53
54 if(last_val_gt_128)
55 ret++;
56
57 return ret;
58 }
59
60 size_t
_heim_len_int(int val)61 _heim_len_int (int val)
62 {
63 unsigned char q;
64 size_t ret = 0;
65
66 if (val >= 0) {
67 do {
68 q = val % 256;
69 ret++;
70 val /= 256;
71 } while(val);
72 if(q >= 128)
73 ret++;
74 } else {
75 val = ~val;
76 do {
77 q = ~(val % 256);
78 ret++;
79 val /= 256;
80 } while(val);
81 if(q < 128)
82 ret++;
83 }
84 return ret;
85 }
86
87 static size_t
len_oid(const heim_oid * oid)88 len_oid (const heim_oid *oid)
89 {
90 size_t ret = 1;
91 size_t n;
92
93 for (n = 2; n < oid->length; ++n) {
94 unsigned u = oid->components[n];
95
96 do {
97 ++ret;
98 u /= 128;
99 } while(u > 0);
100 }
101 return ret;
102 }
103
104 size_t
der_length_len(size_t len)105 der_length_len (size_t len)
106 {
107 if (len < 128)
108 return 1;
109 else {
110 int ret = 0;
111 do {
112 ++ret;
113 len /= 256;
114 } while (len);
115 return ret + 1;
116 }
117 }
118
119 size_t
der_length_tag(unsigned int tag)120 der_length_tag(unsigned int tag)
121 {
122 size_t len = 0;
123
124 if(tag <= 30)
125 return 1;
126 while(tag) {
127 tag /= 128;
128 len++;
129 }
130 return len + 1;
131 }
132
133 size_t
der_length_integer(const int * data)134 der_length_integer (const int *data)
135 {
136 return _heim_len_int (*data);
137 }
138
139 size_t
der_length_unsigned(const unsigned * data)140 der_length_unsigned (const unsigned *data)
141 {
142 return _heim_len_unsigned(*data);
143 }
144
145 size_t
der_length_enumerated(const unsigned * data)146 der_length_enumerated (const unsigned *data)
147 {
148 return _heim_len_int (*data);
149 }
150
151 size_t
der_length_general_string(const heim_general_string * data)152 der_length_general_string (const heim_general_string *data)
153 {
154 return strlen(*data);
155 }
156
157 size_t
der_length_utf8string(const heim_utf8_string * data)158 der_length_utf8string (const heim_utf8_string *data)
159 {
160 return strlen(*data);
161 }
162
163 size_t
der_length_printable_string(const heim_printable_string * data)164 der_length_printable_string (const heim_printable_string *data)
165 {
166 return data->length;
167 }
168
169 size_t
der_length_ia5_string(const heim_ia5_string * data)170 der_length_ia5_string (const heim_ia5_string *data)
171 {
172 return data->length;
173 }
174
175 size_t
der_length_bmp_string(const heim_bmp_string * data)176 der_length_bmp_string (const heim_bmp_string *data)
177 {
178 return data->length * 2;
179 }
180
181 size_t
der_length_universal_string(const heim_universal_string * data)182 der_length_universal_string (const heim_universal_string *data)
183 {
184 return data->length * 4;
185 }
186
187 size_t
der_length_visible_string(const heim_visible_string * data)188 der_length_visible_string (const heim_visible_string *data)
189 {
190 return strlen(*data);
191 }
192
193 size_t
der_length_octet_string(const heim_octet_string * k)194 der_length_octet_string (const heim_octet_string *k)
195 {
196 return k->length;
197 }
198
199 size_t
der_length_heim_integer(const heim_integer * k)200 der_length_heim_integer (const heim_integer *k)
201 {
202 if (k->length == 0)
203 return 1;
204 if (k->negative)
205 return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
206 else
207 return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
208 }
209
210 size_t
der_length_oid(const heim_oid * k)211 der_length_oid (const heim_oid *k)
212 {
213 return len_oid (k);
214 }
215
216 size_t
der_length_generalized_time(const time_t * t)217 der_length_generalized_time (const time_t *t)
218 {
219 heim_octet_string k;
220 size_t ret;
221
222 _heim_time2generalizedtime (*t, &k, 1);
223 ret = k.length;
224 free(k.data);
225 return ret;
226 }
227
228 size_t
der_length_utctime(const time_t * t)229 der_length_utctime (const time_t *t)
230 {
231 heim_octet_string k;
232 size_t ret;
233
234 _heim_time2generalizedtime (*t, &k, 0);
235 ret = k.length;
236 free(k.data);
237 return ret;
238 }
239
240 size_t
der_length_boolean(const int * k)241 der_length_boolean (const int *k)
242 {
243 return 1;
244 }
245
246 size_t
der_length_bit_string(const heim_bit_string * k)247 der_length_bit_string (const heim_bit_string *k)
248 {
249 return (k->length + 7) / 8 + 1;
250 }
251