r19644: Merge up to current lorikeet-heimdal, incling adding
[Samba/ekacnet.git] / source / heimdal / lib / asn1 / der_put.c
blobb006f233ca62ab8ab9395fc74c8bf61837501782
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("$Id: der_put.c,v 1.33 2005/07/12 06:27:23 lha Exp $");
39 * All encoding functions take a pointer `p' to first position in
40 * which to write, from the right, `len' which means the maximum
41 * number of characters we are able to write. The function returns
42 * the number of characters written in `size' (if non-NULL).
43 * The return value is 0 or an error.
46 int
47 der_put_unsigned (unsigned char *p, size_t len, const unsigned *v, size_t *size)
49 unsigned char *base = p;
50 unsigned val = *v;
52 if (val) {
53 while (len > 0 && val) {
54 *p-- = val % 256;
55 val /= 256;
56 --len;
58 if (val != 0)
59 return ASN1_OVERFLOW;
60 else {
61 if(p[1] >= 128) {
62 if(len < 1)
63 return ASN1_OVERFLOW;
64 *p-- = 0;
66 *size = base - p;
67 return 0;
69 } else if (len < 1)
70 return ASN1_OVERFLOW;
71 else {
72 *p = 0;
73 *size = 1;
74 return 0;
78 int
79 der_put_integer (unsigned char *p, size_t len, const int *v, size_t *size)
81 unsigned char *base = p;
82 int val = *v;
84 if(val >= 0) {
85 do {
86 if(len < 1)
87 return ASN1_OVERFLOW;
88 *p-- = val % 256;
89 len--;
90 val /= 256;
91 } while(val);
92 if(p[1] >= 128) {
93 if(len < 1)
94 return ASN1_OVERFLOW;
95 *p-- = 0;
96 len--;
98 } else {
99 val = ~val;
100 do {
101 if(len < 1)
102 return ASN1_OVERFLOW;
103 *p-- = ~(val % 256);
104 len--;
105 val /= 256;
106 } while(val);
107 if(p[1] < 128) {
108 if(len < 1)
109 return ASN1_OVERFLOW;
110 *p-- = 0xff;
111 len--;
114 *size = base - p;
115 return 0;
120 der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)
122 if (len < 1)
123 return ASN1_OVERFLOW;
125 if (val < 128) {
126 *p = val;
127 *size = 1;
128 } else {
129 size_t l = 0;
131 while(val > 0) {
132 if(len < 2)
133 return ASN1_OVERFLOW;
134 *p-- = val % 256;
135 val /= 256;
136 len--;
137 l++;
139 *p = 0x80 | l;
140 if(size)
141 *size = l + 1;
143 return 0;
147 der_put_boolean(unsigned char *p, size_t len, const int *data, size_t *size)
149 if(len < 1)
150 return ASN1_OVERFLOW;
151 if(*data != 0)
152 *p = 0xff;
153 else
154 *p = 0;
155 *size = 1;
156 return 0;
160 der_put_general_string (unsigned char *p, size_t len,
161 const heim_general_string *str, size_t *size)
163 size_t slen = strlen(*str);
165 if (len < slen)
166 return ASN1_OVERFLOW;
167 p -= slen;
168 len -= slen;
169 memcpy (p+1, *str, slen);
170 *size = slen;
171 return 0;
175 der_put_utf8string (unsigned char *p, size_t len,
176 const heim_utf8_string *str, size_t *size)
178 return der_put_general_string(p, len, str, size);
182 der_put_printable_string (unsigned char *p, size_t len,
183 const heim_printable_string *str, size_t *size)
185 return der_put_general_string(p, len, str, size);
189 der_put_ia5_string (unsigned char *p, size_t len,
190 const heim_ia5_string *str, size_t *size)
192 return der_put_general_string(p, len, str, size);
196 der_put_bmp_string (unsigned char *p, size_t len,
197 const heim_bmp_string *data, size_t *size)
199 size_t i;
200 if (len / 2 < data->length)
201 return ASN1_OVERFLOW;
202 p -= data->length * 2;
203 len -= data->length * 2;
204 for (i = 0; i < data->length; i++) {
205 p[1] = (data->data[i] >> 8) & 0xff;
206 p[2] = data->data[i] & 0xff;
207 p += 2;
209 if (size) *size = data->length * 2;
210 return 0;
214 der_put_universal_string (unsigned char *p, size_t len,
215 const heim_universal_string *data, size_t *size)
217 size_t i;
218 if (len / 4 < data->length)
219 return ASN1_OVERFLOW;
220 p -= data->length * 4;
221 len -= data->length * 4;
222 for (i = 0; i < data->length; i++) {
223 p[1] = (data->data[i] >> 24) & 0xff;
224 p[2] = (data->data[i] >> 16) & 0xff;
225 p[3] = (data->data[i] >> 8) & 0xff;
226 p[4] = data->data[i] & 0xff;
227 p += 4;
229 if (size) *size = data->length * 4;
230 return 0;
234 der_put_octet_string (unsigned char *p, size_t len,
235 const heim_octet_string *data, size_t *size)
237 if (len < data->length)
238 return ASN1_OVERFLOW;
239 p -= data->length;
240 len -= data->length;
241 memcpy (p+1, data->data, data->length);
242 *size = data->length;
243 return 0;
247 der_put_heim_integer (unsigned char *p, size_t len,
248 const heim_integer *data, size_t *size)
250 unsigned char *buf = data->data;
251 int hibitset = 0;
253 if (data->length == 0) {
254 if (len < 1)
255 return ASN1_OVERFLOW;
256 *p-- = 0;
257 if (size)
258 *size = 1;
259 return 0;
261 if (len < data->length)
262 return ASN1_OVERFLOW;
264 len -= data->length;
266 if (data->negative) {
267 int i, carry;
268 for (i = data->length - 1, carry = 1; i >= 0; i--) {
269 *p = buf[i] ^ 0xff;
270 if (carry)
271 carry = !++*p;
272 p--;
274 if (p[1] < 128) {
275 if (len < 1)
276 return ASN1_OVERFLOW;
277 *p-- = 0xff;
278 len--;
279 hibitset = 1;
281 } else {
282 p -= data->length;
283 memcpy(p + 1, buf, data->length);
285 if (p[1] >= 128) {
286 if (len < 1)
287 return ASN1_OVERFLOW;
288 p[0] = 0;
289 len--;
290 hibitset = 1;
293 if (size)
294 *size = data->length + hibitset;
295 return 0;
299 der_put_generalized_time (unsigned char *p, size_t len,
300 const time_t *data, size_t *size)
302 heim_octet_string k;
303 size_t l;
304 int e;
306 e = _heim_time2generalizedtime (*data, &k, 1);
307 if (e)
308 return e;
309 e = der_put_octet_string(p, len, &k, &l);
310 free(k.data);
311 if(e)
312 return e;
313 if(size)
314 *size = l;
315 return 0;
319 der_put_utctime (unsigned char *p, size_t len,
320 const time_t *data, size_t *size)
322 heim_octet_string k;
323 size_t l;
324 int e;
326 e = _heim_time2generalizedtime (*data, &k, 0);
327 if (e)
328 return e;
329 e = der_put_octet_string(p, len, &k, &l);
330 free(k.data);
331 if(e)
332 return e;
333 if(size)
334 *size = l;
335 return 0;
339 der_put_oid (unsigned char *p, size_t len,
340 const heim_oid *data, size_t *size)
342 unsigned char *base = p;
343 int n;
345 for (n = data->length - 1; n >= 2; --n) {
346 unsigned u = data->components[n];
348 if (len < 1)
349 return ASN1_OVERFLOW;
350 *p-- = u % 128;
351 u /= 128;
352 --len;
353 while (u > 0) {
354 if (len < 1)
355 return ASN1_OVERFLOW;
356 *p-- = 128 + u % 128;
357 u /= 128;
358 --len;
361 if (len < 1)
362 return ASN1_OVERFLOW;
363 *p-- = 40 * data->components[0] + data->components[1];
364 *size = base - p;
365 return 0;
369 der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
370 unsigned int tag, size_t *size)
372 if (tag <= 30) {
373 if (len < 1)
374 return ASN1_OVERFLOW;
375 *p = MAKE_TAG(class, type, tag);
376 *size = 1;
377 } else {
378 size_t ret = 0;
379 unsigned int continuation = 0;
381 do {
382 if (len < 1)
383 return ASN1_OVERFLOW;
384 *p-- = tag % 128 | continuation;
385 len--;
386 ret++;
387 tag /= 128;
388 continuation = 0x80;
389 } while(tag > 0);
390 if (len < 1)
391 return ASN1_OVERFLOW;
392 *p-- = MAKE_TAG(class, type, 0x1f);
393 ret++;
394 *size = ret;
396 return 0;
400 der_put_length_and_tag (unsigned char *p, size_t len, size_t len_val,
401 Der_class class, Der_type type,
402 unsigned int tag, size_t *size)
404 size_t ret = 0;
405 size_t l;
406 int e;
408 e = der_put_length (p, len, len_val, &l);
409 if(e)
410 return e;
411 p -= l;
412 len -= l;
413 ret += l;
414 e = der_put_tag (p, len, class, type, tag, &l);
415 if(e)
416 return e;
417 p -= l;
418 len -= l;
419 ret += l;
420 *size = ret;
421 return 0;
425 _heim_time2generalizedtime (time_t t, heim_octet_string *s, int gtimep)
427 struct tm *tm;
428 const size_t len = gtimep ? 15 : 13;
430 s->data = malloc(len + 1);
431 if (s->data == NULL)
432 return ENOMEM;
433 s->length = len;
434 tm = gmtime (&t);
435 if (gtimep)
436 snprintf (s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
437 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
438 tm->tm_hour, tm->tm_min, tm->tm_sec);
439 else
440 snprintf (s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
441 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
442 tm->tm_hour, tm->tm_min, tm->tm_sec);
444 return 0;
448 der_put_bit_string (unsigned char *p, size_t len,
449 const heim_bit_string *data, size_t *size)
451 size_t data_size = (data->length + 7) / 8;
452 if (len < data_size + 1)
453 return ASN1_OVERFLOW;
454 p -= data_size + 1;
455 len -= data_size + 1;
456 memcpy (p+2, data->data, data_size);
457 if (data->length && (data->length % 8) != 0)
458 p[1] = 8 - (data->length % 8);
459 else
460 p[1] = 0;
461 *size = data_size + 1;
462 return 0;
465 int
466 _heim_der_set_sort(const void *a1, const void *a2)
468 const struct heim_octet_string *s1 = a1, *s2 = a2;
469 int ret;
471 ret = memcmp(s1->data, s2->data,
472 s1->length < s2->length ? s1->length : s2->length);
473 if(ret)
474 return ret;
475 return s1->length - s2->length;