No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / asn1 / der_put.c
blob02dd3e3b23b699c05d1064320c54cae2357d2a35
1 /*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "der_locl.h"
36 __RCSID("$Heimdal: der_put.c 19539 2006-12-28 17:15:05Z lha $"
37 "$NetBSD$");
40 * All encoding functions take a pointer `p' to first position in
41 * which to write, from the right, `len' which means the maximum
42 * number of characters we are able to write. The function returns
43 * the number of characters written in `size' (if non-NULL).
44 * The return value is 0 or an error.
47 int
48 der_put_unsigned (unsigned char *p, size_t len, const unsigned *v, size_t *size)
50 unsigned char *base = p;
51 unsigned val = *v;
53 if (val) {
54 while (len > 0 && val) {
55 *p-- = val % 256;
56 val /= 256;
57 --len;
59 if (val != 0)
60 return ASN1_OVERFLOW;
61 else {
62 if(p[1] >= 128) {
63 if(len < 1)
64 return ASN1_OVERFLOW;
65 *p-- = 0;
67 *size = base - p;
68 return 0;
70 } else if (len < 1)
71 return ASN1_OVERFLOW;
72 else {
73 *p = 0;
74 *size = 1;
75 return 0;
79 int
80 der_put_integer (unsigned char *p, size_t len, const int *v, size_t *size)
82 unsigned char *base = p;
83 int val = *v;
85 if(val >= 0) {
86 do {
87 if(len < 1)
88 return ASN1_OVERFLOW;
89 *p-- = val % 256;
90 len--;
91 val /= 256;
92 } while(val);
93 if(p[1] >= 128) {
94 if(len < 1)
95 return ASN1_OVERFLOW;
96 *p-- = 0;
97 len--;
99 } else {
100 val = ~val;
101 do {
102 if(len < 1)
103 return ASN1_OVERFLOW;
104 *p-- = ~(val % 256);
105 len--;
106 val /= 256;
107 } while(val);
108 if(p[1] < 128) {
109 if(len < 1)
110 return ASN1_OVERFLOW;
111 *p-- = 0xff;
112 len--;
115 *size = base - p;
116 return 0;
121 der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)
123 if (len < 1)
124 return ASN1_OVERFLOW;
126 if (val < 128) {
127 *p = val;
128 *size = 1;
129 } else {
130 size_t l = 0;
132 while(val > 0) {
133 if(len < 2)
134 return ASN1_OVERFLOW;
135 *p-- = val % 256;
136 val /= 256;
137 len--;
138 l++;
140 *p = 0x80 | l;
141 if(size)
142 *size = l + 1;
144 return 0;
148 der_put_boolean(unsigned char *p, size_t len, const int *data, size_t *size)
150 if(len < 1)
151 return ASN1_OVERFLOW;
152 if(*data != 0)
153 *p = 0xff;
154 else
155 *p = 0;
156 *size = 1;
157 return 0;
161 der_put_general_string (unsigned char *p, size_t len,
162 const heim_general_string *str, size_t *size)
164 size_t slen = strlen(*str);
166 if (len < slen)
167 return ASN1_OVERFLOW;
168 p -= slen;
169 len -= slen;
170 memcpy (p+1, *str, slen);
171 *size = slen;
172 return 0;
176 der_put_utf8string (unsigned char *p, size_t len,
177 const heim_utf8_string *str, size_t *size)
179 return der_put_general_string(p, len, str, size);
183 der_put_printable_string (unsigned char *p, size_t len,
184 const heim_printable_string *str, size_t *size)
186 return der_put_general_string(p, len, str, size);
190 der_put_ia5_string (unsigned char *p, size_t len,
191 const heim_ia5_string *str, size_t *size)
193 return der_put_general_string(p, len, str, size);
197 der_put_bmp_string (unsigned char *p, size_t len,
198 const heim_bmp_string *data, size_t *size)
200 size_t i;
201 if (len / 2 < data->length)
202 return ASN1_OVERFLOW;
203 p -= data->length * 2;
204 len -= data->length * 2;
205 for (i = 0; i < data->length; i++) {
206 p[1] = (data->data[i] >> 8) & 0xff;
207 p[2] = data->data[i] & 0xff;
208 p += 2;
210 if (size) *size = data->length * 2;
211 return 0;
215 der_put_universal_string (unsigned char *p, size_t len,
216 const heim_universal_string *data, size_t *size)
218 size_t i;
219 if (len / 4 < data->length)
220 return ASN1_OVERFLOW;
221 p -= data->length * 4;
222 len -= data->length * 4;
223 for (i = 0; i < data->length; i++) {
224 p[1] = (data->data[i] >> 24) & 0xff;
225 p[2] = (data->data[i] >> 16) & 0xff;
226 p[3] = (data->data[i] >> 8) & 0xff;
227 p[4] = data->data[i] & 0xff;
228 p += 4;
230 if (size) *size = data->length * 4;
231 return 0;
235 der_put_visible_string (unsigned char *p, size_t len,
236 const heim_visible_string *str, size_t *size)
238 return der_put_general_string(p, len, str, size);
242 der_put_octet_string (unsigned char *p, size_t len,
243 const heim_octet_string *data, size_t *size)
245 if (len < data->length)
246 return ASN1_OVERFLOW;
247 p -= data->length;
248 len -= data->length;
249 memcpy (p+1, data->data, data->length);
250 *size = data->length;
251 return 0;
255 der_put_heim_integer (unsigned char *p, size_t len,
256 const heim_integer *data, size_t *size)
258 unsigned char *buf = data->data;
259 int hibitset = 0;
261 if (data->length == 0) {
262 if (len < 1)
263 return ASN1_OVERFLOW;
264 *p-- = 0;
265 if (size)
266 *size = 1;
267 return 0;
269 if (len < data->length)
270 return ASN1_OVERFLOW;
272 len -= data->length;
274 if (data->negative) {
275 int i, carry;
276 for (i = data->length - 1, carry = 1; i >= 0; i--) {
277 *p = buf[i] ^ 0xff;
278 if (carry)
279 carry = !++*p;
280 p--;
282 if (p[1] < 128) {
283 if (len < 1)
284 return ASN1_OVERFLOW;
285 *p-- = 0xff;
286 len--;
287 hibitset = 1;
289 } else {
290 p -= data->length;
291 memcpy(p + 1, buf, data->length);
293 if (p[1] >= 128) {
294 if (len < 1)
295 return ASN1_OVERFLOW;
296 p[0] = 0;
297 len--;
298 hibitset = 1;
301 if (size)
302 *size = data->length + hibitset;
303 return 0;
307 der_put_generalized_time (unsigned char *p, size_t len,
308 const time_t *data, size_t *size)
310 heim_octet_string k;
311 size_t l;
312 int e;
314 e = _heim_time2generalizedtime (*data, &k, 1);
315 if (e)
316 return e;
317 e = der_put_octet_string(p, len, &k, &l);
318 free(k.data);
319 if(e)
320 return e;
321 if(size)
322 *size = l;
323 return 0;
327 der_put_utctime (unsigned char *p, size_t len,
328 const time_t *data, size_t *size)
330 heim_octet_string k;
331 size_t l;
332 int e;
334 e = _heim_time2generalizedtime (*data, &k, 0);
335 if (e)
336 return e;
337 e = der_put_octet_string(p, len, &k, &l);
338 free(k.data);
339 if(e)
340 return e;
341 if(size)
342 *size = l;
343 return 0;
347 der_put_oid (unsigned char *p, size_t len,
348 const heim_oid *data, size_t *size)
350 unsigned char *base = p;
351 int n;
353 for (n = data->length - 1; n >= 2; --n) {
354 unsigned u = data->components[n];
356 if (len < 1)
357 return ASN1_OVERFLOW;
358 *p-- = u % 128;
359 u /= 128;
360 --len;
361 while (u > 0) {
362 if (len < 1)
363 return ASN1_OVERFLOW;
364 *p-- = 128 + u % 128;
365 u /= 128;
366 --len;
369 if (len < 1)
370 return ASN1_OVERFLOW;
371 *p-- = 40 * data->components[0] + data->components[1];
372 *size = base - p;
373 return 0;
377 der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
378 unsigned int tag, size_t *size)
380 if (tag <= 30) {
381 if (len < 1)
382 return ASN1_OVERFLOW;
383 *p = MAKE_TAG(class, type, tag);
384 *size = 1;
385 } else {
386 size_t ret = 0;
387 unsigned int continuation = 0;
389 do {
390 if (len < 1)
391 return ASN1_OVERFLOW;
392 *p-- = tag % 128 | continuation;
393 len--;
394 ret++;
395 tag /= 128;
396 continuation = 0x80;
397 } while(tag > 0);
398 if (len < 1)
399 return ASN1_OVERFLOW;
400 *p-- = MAKE_TAG(class, type, 0x1f);
401 ret++;
402 *size = ret;
404 return 0;
408 der_put_length_and_tag (unsigned char *p, size_t len, size_t len_val,
409 Der_class class, Der_type type,
410 unsigned int tag, size_t *size)
412 size_t ret = 0;
413 size_t l;
414 int e;
416 e = der_put_length (p, len, len_val, &l);
417 if(e)
418 return e;
419 p -= l;
420 len -= l;
421 ret += l;
422 e = der_put_tag (p, len, class, type, tag, &l);
423 if(e)
424 return e;
425 p -= l;
426 len -= l;
427 ret += l;
428 *size = ret;
429 return 0;
433 _heim_time2generalizedtime (time_t t, heim_octet_string *s, int gtimep)
435 struct tm *tm;
436 const size_t len = gtimep ? 15 : 13;
438 s->data = malloc(len + 1);
439 if (s->data == NULL)
440 return ENOMEM;
441 s->length = len;
442 tm = gmtime (&t);
443 if (gtimep)
444 snprintf (s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
445 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
446 tm->tm_hour, tm->tm_min, tm->tm_sec);
447 else
448 snprintf (s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
449 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
450 tm->tm_hour, tm->tm_min, tm->tm_sec);
452 return 0;
456 der_put_bit_string (unsigned char *p, size_t len,
457 const heim_bit_string *data, size_t *size)
459 size_t data_size = (data->length + 7) / 8;
460 if (len < data_size + 1)
461 return ASN1_OVERFLOW;
462 p -= data_size + 1;
463 len -= data_size + 1;
464 memcpy (p+2, data->data, data_size);
465 if (data->length && (data->length % 8) != 0)
466 p[1] = 8 - (data->length % 8);
467 else
468 p[1] = 0;
469 *size = data_size + 1;
470 return 0;
473 int
474 _heim_der_set_sort(const void *a1, const void *a2)
476 const struct heim_octet_string *s1 = a1, *s2 = a2;
477 int ret;
479 ret = memcmp(s1->data, s2->data,
480 s1->length < s2->length ? s1->length : s2->length);
481 if(ret)
482 return ret;
483 return s1->length - s2->length;