5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 /* Implementation of Attribute Payload routines */
23 #include "silcattrs.h"
25 /******************************************************************************
29 ******************************************************************************/
31 struct SilcAttributePayloadStruct {
32 SilcAttribute attribute;
33 SilcAttributeFlags flags;
38 /* Internal routine for encoding a attribute */
40 static unsigned char *
41 silc_attribute_payload_encode_int(SilcAttribute attribute,
42 SilcAttributeFlags flags,
44 SilcUInt32 object_size,
47 SilcBuffer tmpbuf = NULL;
48 unsigned char tmp[4], *str = NULL, *ret;
51 /* Encode according to attribute type */
52 if (flags & SILC_ATTRIBUTE_FLAG_VALID) {
53 if (!object && !object_size)
58 case SILC_ATTRIBUTE_USER_INFO:
60 SilcVCard vcard = object;
61 if (object_size != sizeof(*vcard))
63 str = silc_vcard_encode(vcard, &object_size);
70 case SILC_ATTRIBUTE_SERVICE:
72 SilcAttributeObjService *service = object;
74 if (object_size != sizeof(*service))
76 len = strlen(service->address);
77 len2 = strlen(service->signon);
78 tmpbuf = silc_buffer_alloc_size(13 + len + len2);
81 silc_buffer_format(tmpbuf,
82 SILC_STR_UI_INT(service->port),
83 SILC_STR_UI_SHORT(len),
84 SILC_STR_UI_XNSTRING(service->address, len),
85 SILC_STR_UI_CHAR(service->status),
86 SILC_STR_UI_SHORT(len2),
87 SILC_STR_UI_XNSTRING(service->signon, len2),
88 SILC_STR_UI_INT(service->idle),
90 object = tmpbuf->data;
91 object_size = silc_buffer_len(tmpbuf);
95 case SILC_ATTRIBUTE_STATUS_MOOD:
96 case SILC_ATTRIBUTE_PREFERRED_CONTACT:
98 SilcUInt32 mask = SILC_PTR_TO_32(object);
99 if (object_size != sizeof(SilcUInt32))
101 SILC_PUT32_MSB(mask, tmp);
103 object_size = sizeof(SilcUInt32);
107 case SILC_ATTRIBUTE_STATUS_FREETEXT:
108 case SILC_ATTRIBUTE_PREFERRED_LANGUAGE:
109 case SILC_ATTRIBUTE_TIMEZONE:
111 unsigned char *string = object;
112 str = silc_malloc(2 + object_size);
115 SILC_PUT16_MSB(object_size, str);
116 memcpy(str + 2, string, object_size);
122 case SILC_ATTRIBUTE_STATUS_MESSAGE:
123 case SILC_ATTRIBUTE_EXTENSION:
124 case SILC_ATTRIBUTE_USER_ICON:
126 SilcMime mime = object;
127 if (object_size != sizeof(*mime))
129 str = silc_mime_encode(mime, &object_size);
135 case SILC_ATTRIBUTE_GEOLOCATION:
137 SilcAttributeObjGeo *geo = object;
138 SilcUInt32 len1, len2, len3, len4;
139 if (object_size != sizeof(*geo))
141 len1 = (geo->longitude ? strlen(geo->longitude) : 0);
142 len2 = (geo->latitude ? strlen(geo->latitude) : 0);
143 len3 = (geo->altitude ? strlen(geo->altitude) : 0);
144 len4 = (geo->accuracy ? strlen(geo->accuracy) : 0);
145 if (len1 + len2 + len3 + len4 == 0)
147 len = len1 + len2 + len3 + len4;
148 tmpbuf = silc_buffer_alloc_size(8 + len);
151 silc_buffer_format(tmpbuf,
152 SILC_STR_UI_SHORT(len1),
153 SILC_STR_UI16_STRING(len1 ? geo->longitude : ""),
154 SILC_STR_UI_SHORT(len2),
155 SILC_STR_UI16_STRING(len2 ? geo->latitude : ""),
156 SILC_STR_UI_SHORT(len3),
157 SILC_STR_UI16_STRING(len3 ? geo->altitude : ""),
158 SILC_STR_UI_SHORT(len4),
159 SILC_STR_UI16_STRING(len4 ? geo->accuracy : ""),
161 object = tmpbuf->data;
162 object_size = silc_buffer_len(tmpbuf);
166 case SILC_ATTRIBUTE_DEVICE_INFO:
168 SilcAttributeObjDevice *dev = object;
169 SilcUInt32 len1, len2, len3, len4;
170 if (object_size != sizeof(*dev))
172 len1 = (dev->manufacturer ? strlen(dev->manufacturer) : 0);
173 len2 = (dev->version ? strlen(dev->version) : 0);
174 len3 = (dev->model ? strlen(dev->model) : 0);
175 len4 = (dev->language ? strlen(dev->language) : 0);
176 if (len1 + len2 + len3 + len4 == 0)
178 len = len1 + len2 + len3 + len4;
179 tmpbuf = silc_buffer_alloc_size(4 + 8 + len);
182 silc_buffer_format(tmpbuf,
183 SILC_STR_UI_INT(dev->type),
184 SILC_STR_UI_SHORT(len1),
185 SILC_STR_UI16_STRING(len1 ? dev->manufacturer : ""),
186 SILC_STR_UI_SHORT(len2),
187 SILC_STR_UI16_STRING(len2 ? dev->version : ""),
188 SILC_STR_UI_SHORT(len3),
189 SILC_STR_UI16_STRING(len3 ? dev->model : ""),
190 SILC_STR_UI_SHORT(len4),
191 SILC_STR_UI16_STRING(len4 ? dev->language : ""),
193 object = tmpbuf->data;
194 object_size = silc_buffer_len(tmpbuf);
198 case SILC_ATTRIBUTE_USER_PUBLIC_KEY:
199 case SILC_ATTRIBUTE_SERVER_PUBLIC_KEY:
201 SilcAttributeObjPk *pk = object;
202 if (object_size != sizeof(*pk))
204 len = (pk->type ? strlen(pk->type) : 0);
205 tmpbuf = silc_buffer_alloc_size(2 + len + pk->data_len);
208 silc_buffer_format(tmpbuf,
209 SILC_STR_UI_SHORT(len),
210 SILC_STR_UI16_STRING(pk->type),
211 SILC_STR_UI_XNSTRING(pk->data, pk->data_len),
213 object = tmpbuf->data;
214 object_size = silc_buffer_len(tmpbuf);
218 case SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE:
219 case SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE:
221 SilcAttributeObjPk *pk = object;
222 if (object_size != sizeof(*pk))
225 object_size = pk->data_len;
234 ret = silc_memdup(object, object_size);
237 silc_buffer_free(tmpbuf);
241 *ret_len = object_size;
249 /* Allocates attribute payload and encodes the attribute there */
251 SilcAttributePayload silc_attribute_payload_alloc(SilcAttribute attribute,
252 SilcAttributeFlags flags,
254 SilcUInt32 object_size)
256 SilcAttributePayload attr;
259 attr = silc_calloc(1, sizeof(*attr));
263 attr->attribute = attribute;
266 silc_attribute_payload_encode_int(attribute, flags, object,
267 object_size, &tmp_len);
268 attr->data_len = (SilcUInt16)tmp_len;
277 /* Parse list of payloads */
279 SilcDList silc_attribute_payload_parse(const unsigned char *payload,
280 SilcUInt32 payload_len)
282 SilcBufferStruct buffer;
284 SilcAttributePayload newp;
288 SILC_LOG_DEBUG(("Parsing Attribute Payload list"));
290 silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
291 list = silc_dlist_init();
293 while (silc_buffer_len(&buffer)) {
294 newp = silc_calloc(1, sizeof(*newp));
297 ret = silc_buffer_unformat(&buffer,
298 SILC_STR_UI_CHAR(&newp->attribute),
299 SILC_STR_UI_CHAR(&newp->flags),
300 SILC_STR_UI16_NSTRING_ALLOC(&newp->data,
306 if (newp->data_len > silc_buffer_len(&buffer) - 4) {
307 SILC_LOG_ERROR(("Incorrect attribute payload in list"));
311 len = 4 + newp->data_len;
312 if (silc_buffer_len(&buffer) < len)
314 silc_buffer_pull(&buffer, len);
316 silc_dlist_add(list, newp);
322 silc_attribute_payload_list_free(list);
326 /* Encode one attribute payload to buffer */
328 SilcBuffer silc_attribute_payload_encode(SilcBuffer attrs,
329 SilcAttribute attribute,
330 SilcAttributeFlags flags,
332 SilcUInt32 object_size)
334 object = silc_attribute_payload_encode_int(attribute, flags, object,
335 object_size, &object_size);
336 attrs = silc_attribute_payload_encode_data(attrs, attribute, flags,
337 (const unsigned char *)object,
343 /* Encoded the attribute data directly to buffer */
345 SilcBuffer silc_attribute_payload_encode_data(SilcBuffer attrs,
346 SilcAttribute attribute,
347 SilcAttributeFlags flags,
348 const unsigned char *data,
351 SilcBuffer buffer = attrs;
354 len = 4 + (SilcUInt16)data_len;
355 buffer = silc_buffer_realloc(buffer,
356 (buffer ? silc_buffer_truelen(buffer) + len : len));
359 silc_buffer_pull(buffer, silc_buffer_len(buffer));
360 silc_buffer_pull_tail(buffer, len);
361 silc_buffer_format(buffer,
362 SILC_STR_UI_CHAR(attribute),
363 SILC_STR_UI_CHAR(flags),
364 SILC_STR_UI_SHORT((SilcUInt16)data_len),
365 SILC_STR_UI_XNSTRING(data, (SilcUInt16)data_len),
367 silc_buffer_push(buffer, buffer->data - buffer->head);
372 /* Free Attribute Payload */
374 void silc_attribute_payload_free(SilcAttributePayload payload)
376 silc_free(payload->data);
380 /* Free's list of Attribute Payloads */
382 void silc_attribute_payload_list_free(SilcDList list)
384 SilcAttributePayload entry;
386 silc_dlist_start(list);
387 while ((entry = silc_dlist_get(list)) != SILC_LIST_END) {
388 silc_attribute_payload_free(entry);
389 silc_dlist_del(list, entry);
392 silc_dlist_uninit(list);
395 /* Return attribute type */
397 SilcAttribute silc_attribute_get_attribute(SilcAttributePayload payload)
399 return payload->attribute;
402 /* Return attribute flags */
404 SilcAttributeFlags silc_attribute_get_flags(SilcAttributePayload payload)
406 return payload->flags;
409 /* Return attribute data from the payload */
411 const unsigned char *silc_attribute_get_data(SilcAttributePayload payload,
412 SilcUInt32 *data_len)
415 *data_len = (SilcUInt32)payload->data_len;
416 return (const unsigned char *)payload->data;
419 /* Construct digital signature verification data */
421 unsigned char *silc_attribute_get_verify_data(SilcDList attrs,
422 SilcBool server_verification,
423 SilcUInt32 *data_len)
425 SilcAttributePayload attr;
426 SilcBufferStruct buffer;
427 unsigned char *data = NULL;
430 silc_dlist_start(attrs);
431 while ((attr = silc_dlist_get(attrs)) != SILC_LIST_END) {
432 switch (attr->attribute) {
433 case SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE:
434 /* Server signature is never part of the verification data */
437 case SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE:
438 /* For user signature verification this is not part of the data */
439 if (!server_verification)
442 /* Fallback, for server signature verification, user digital signature
443 is part of verification data. */
446 /* All other data is part of the verification data */
447 data = silc_realloc(data, sizeof(*data) * (4 + attr->data_len + len));
450 silc_buffer_set(&buffer, data + len, 4 + attr->data_len);
451 silc_buffer_format(&buffer,
452 SILC_STR_UI_CHAR(attr->attribute),
453 SILC_STR_UI_CHAR(attr->flags),
454 SILC_STR_UI_SHORT(attr->data_len),
455 SILC_STR_UI_XNSTRING(attr->data, attr->data_len),
457 len += 4 + attr->data_len;
468 /* Return parsed attribute object */
470 SilcBool silc_attribute_get_object(SilcAttributePayload payload,
471 void *object, SilcUInt32 object_size)
474 SilcBool ret = FALSE;
476 if (!object || payload->flags & SILC_ATTRIBUTE_FLAG_INVALID)
479 switch (payload->attribute) {
480 case SILC_ATTRIBUTE_USER_INFO:
482 SilcVCard vcard = object;
483 if (object_size != sizeof(*vcard))
485 if (!silc_vcard_decode(payload->data, payload->data_len, vcard))
491 case SILC_ATTRIBUTE_SERVICE:
493 SilcAttributeObjService *service = object;
494 SilcBufferStruct buf;
495 SilcUInt16 addr_len, signon_len;
498 if (object_size != sizeof(*service))
500 if (payload->data_len < 13)
502 silc_buffer_set(&buf, payload->data, payload->data_len);
503 res = silc_buffer_unformat(&buf,
504 SILC_STR_UI_INT(&service->port),
505 SILC_STR_UI16_NSTRING(&addr, &addr_len),
506 SILC_STR_UI_CHAR(&service->status),
507 SILC_STR_UI16_NSTRING(&signon, &signon_len),
508 SILC_STR_UI_INT(&service->idle),
512 memset(service->address, 0, sizeof(service->address));
513 memset(service->signon, 0, sizeof(service->signon));
514 memcpy(service->address, addr,
515 (addr_len < sizeof(service->address) - 1 ? addr_len :
516 sizeof(service->address) - 1));
517 memcpy(service->signon, signon,
518 (signon_len < sizeof(service->signon) - 1 ? signon_len :
519 sizeof(service->signon) - 1));
524 case SILC_ATTRIBUTE_STATUS_MOOD:
525 case SILC_ATTRIBUTE_PREFERRED_CONTACT:
527 SilcUInt32 *mask = (SilcUInt32 *)object;
528 if (object_size != sizeof(SilcUInt32))
530 if (payload->data_len < 4)
532 SILC_GET32_MSB(*mask, payload->data);
537 case SILC_ATTRIBUTE_STATUS_FREETEXT:
538 case SILC_ATTRIBUTE_PREFERRED_LANGUAGE:
539 case SILC_ATTRIBUTE_TIMEZONE:
541 char *string = object;
542 if (payload->data_len < 2)
544 SILC_GET16_MSB(len, payload->data);
545 if (payload->data_len < 2 + len)
547 if (object_size < len)
549 memcpy(string, payload->data + 2, len);
554 case SILC_ATTRIBUTE_STATUS_MESSAGE:
555 case SILC_ATTRIBUTE_EXTENSION:
556 case SILC_ATTRIBUTE_USER_ICON:
558 SilcMime mime = object;
559 if (object_size != sizeof(*mime))
561 if (!silc_mime_decode(mime, payload->data, payload->data_len))
567 case SILC_ATTRIBUTE_GEOLOCATION:
569 SilcAttributeObjGeo *geo = object;
570 SilcBufferStruct buffer;
572 if (object_size != sizeof(*geo))
574 silc_buffer_set(&buffer, (unsigned char *)payload->data,
576 res = silc_buffer_unformat(&buffer,
577 SILC_STR_UI16_STRING_ALLOC(&geo->longitude),
578 SILC_STR_UI16_STRING_ALLOC(&geo->latitude),
579 SILC_STR_UI16_STRING_ALLOC(&geo->altitude),
580 SILC_STR_UI16_STRING_ALLOC(&geo->accuracy),
588 case SILC_ATTRIBUTE_DEVICE_INFO:
590 SilcAttributeObjDevice *dev = object;
591 SilcBufferStruct buffer;
594 if (object_size != sizeof(*dev))
596 silc_buffer_set(&buffer, (unsigned char *)payload->data,
599 silc_buffer_unformat(&buffer,
600 SILC_STR_UI_INT(&type),
601 SILC_STR_UI16_STRING_ALLOC(&dev->manufacturer),
602 SILC_STR_UI16_STRING_ALLOC(&dev->version),
603 SILC_STR_UI16_STRING_ALLOC(&dev->model),
604 SILC_STR_UI16_STRING_ALLOC(&dev->language),
613 case SILC_ATTRIBUTE_USER_PUBLIC_KEY:
614 case SILC_ATTRIBUTE_SERVER_PUBLIC_KEY:
616 SilcAttributeObjPk *pk = object;
617 SilcBufferStruct buffer;
619 if (object_size != sizeof(*pk))
621 silc_buffer_set(&buffer, (unsigned char *)payload->data,
624 silc_buffer_unformat(&buffer,
625 SILC_STR_UI16_NSTRING_ALLOC(&pk->type, &len),
627 if (res == -1 || len > silc_buffer_len(&buffer) - 2)
629 pk->data = silc_memdup(payload->data + 2 + len,
630 payload->data_len - 2 - len);
631 pk->data_len = payload->data_len - 2 - len;
636 case SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE:
637 case SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE:
639 SilcAttributeObjPk *pk = object;
640 if (object_size != sizeof(*pk))
643 pk->data = silc_memdup(payload->data, payload->data_len);
644 pk->data_len = payload->data_len;