Merged silc_1_0_branch to trunk.
[silc.git] / lib / silccore / silcattrs.h
1 /*
2
3   silcattrs.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 - 2003 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 /****h* silccore/SILC Attribute Interface
21  *
22  * DESCRIPTION
23  *
24  * Implementation of the Attribute Payload that may be used to send and
25  * retrieve user online precense information in the SILC network.  This
26  * implements the draft-riikonen-precense-attrs draft.
27  *
28  ***/
29
30 #ifndef SILCATTRS_H
31 #define SILCATTRS_H
32
33 /****s* silccore/SilcAttributesAPI/SilcAttributePayload
34  *
35  * NAME
36  *
37  *    typedef struct SilcAttributePayloadStruct *SilcAttributePayload;
38  *
39  * DESCRIPTION
40  *
41  *    This context is the actual Attribute Payload and is allocated
42  *    by silc_attribute_payload_parse and given as attribute usually to
43  *    all silc_attribute_payload_* functions.  It is freed by the
44  *    silc_attribute_payload_free function.
45  *
46  ***/
47 typedef struct SilcAttributePayloadStruct *SilcAttributePayload;
48
49 /****d* silccore/SilcAttributesAPI/SilcAttribute
50  *
51  * NAME
52  *
53  *    typedef SilcUInt8 SilcAttribute;
54  *
55  * DESCRIPTION
56  *
57  *    The SilcAttribute type definition and the attributes. The attributes
58  *    listed here are the official attributes defined in the internet
59  *    draft.  They define the contents of the attribute payload and the
60  *    type of the attribute.
61  *
62  * SOURCE
63  */
64 typedef SilcUInt8 SilcAttribute;
65
66 /* All defined attributes.  See the specs for detailed information.  The
67    comment is the structure or data type that must be used with the
68    silc_attribute_get_object function to fetch parsed attribute. */
69 #define SILC_ATTRIBUTE_NONE                   0
70 #define SILC_ATTRIBUTE_USER_INFO              1 /* SilcVCard */
71 #define SILC_ATTRIBUTE_SERVICE                2 /* SilcAttributeObjService */
72 #define SILC_ATTRIBUTE_STATUS_MOOD            3 /* SilcAttributeMood */
73 #define SILC_ATTRIBUTE_STATUS_FREETEXT        4 /* char * (UTF-8 string) */
74 #define SILC_ATTRIBUTE_STATUS_MESSAGE         5 /* SilcAttributeObjMime */
75 #define SILC_ATTRIBUTE_PREFERRED_LANGUAGE     6 /* char * (UTF-8 string) */
76 #define SILC_ATTRIBUTE_PREFERRED_CONTACT      7 /* SilcAttributeContact */
77 #define SILC_ATTRIBUTE_TIMEZONE               8 /* char * (UTF-8 string) */
78 #define SILC_ATTRIBUTE_GEOLOCATION            9 /* SilcAttributeObjGeo */
79 #define SILC_ATTRIBUTE_DEVICE_INFO            10 /* SilcAttributeObjDevice */
80 #define SILC_ATTRIBUTE_EXTENSION              11 /* SilcAttributeObjMime */
81 #define SILC_ATTRIBUTE_USER_PUBLIC_KEY        12 /* SilcAttributeObjPk */
82 #define SILC_ATTRIBUTE_SERVER_PUBLIC_KEY      13 /* SilcAttributeObjPk */
83 #define SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE 14 /* SilcAttributeObjPk */
84 #define SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE 15 /* SilcAttributeObjPk */
85 /***/
86
87 /* Maximum length of attribute request packet */
88 #define SILC_ATTRIBUTE_MAX_REQUEST_LEN (4 * 255)
89
90 /****d* silccore/SilcAttributesAPI/SilcAttributeFlags
91  *
92  * NAME
93  *
94  *    typedef SilcUInt8 SilcAttributeFlags;
95  *
96  * DESCRIPTION
97  *
98  *    Attribute Payload flags defined by the specification.
99  *
100  * SOURCE
101  */
102 typedef SilcUInt8 SilcAttributeFlags;
103
104 /* All defined flags */
105 #define SILC_ATTRIBUTE_FLAG_NONE          0x00    /* No flags */
106 #define SILC_ATTRIBUTE_FLAG_INVALID       0x01    /* Invalid attribute */
107 #define SILC_ATTRIBUTE_FLAG_VALID         0x02    /* Valid attribute */
108 /***/
109
110 /****d* silccore/SilcAttributesAPI/SilcAttributeMood
111  *
112  * NAME
113  *
114  *    typedef enum { ... } SilcAttributeMood;
115  *
116  * DESCRIPTION
117  *
118  *    The user mood indicators defined by the specification.  This is
119  *    bit mask.
120  *
121  * SOURCE
122  */
123 typedef enum {
124   SILC_ATTRIBUTE_MOOD_NORMAL      = 0x00000000,   /* normal mood */
125   SILC_ATTRIBUTE_MOOD_HAPPY       = 0x00000001,   /* user feel happy */
126   SILC_ATTRIBUTE_MOOD_SAD         = 0x00000002,   /* user feel sad */
127   SILC_ATTRIBUTE_MOOD_ANGRY       = 0x00000004,   /* user feel angry */
128   SILC_ATTRIBUTE_MOOD_JEALOUS     = 0x00000008,   /* user feel jealous */
129   SILC_ATTRIBUTE_MOOD_ASHAMED     = 0x00000010,   /* user feel ashamed */
130   SILC_ATTRIBUTE_MOOD_INVINCIBLE  = 0x00000020,   /* user feel invincible */
131   SILC_ATTRIBUTE_MOOD_INLOVE      = 0x00000040,   /* user feel in love */
132   SILC_ATTRIBUTE_MOOD_SLEEPY      = 0x00000080,   /* user feel sleepy */
133   SILC_ATTRIBUTE_MOOD_BORED       = 0x00000100,   /* user feel bored */
134   SILC_ATTRIBUTE_MOOD_EXCITED     = 0x00000200,   /* user feel exited */
135   SILC_ATTRIBUTE_MOOD_ANXIOUS     = 0x00000400,   /* user feel anxious */
136 } SilcAttributeMood;
137 /***/
138
139 /****d* silccore/SilcAttributesAPI/SilcAttributeContact
140  *
141  * NAME
142  *
143  *    typedef enum { ... } SilcAttributeContact;
144  *
145  * DESCRIPTION
146  *
147  *    The defined preferred contact methods defined by the specification.
148  *    This is bit mask.
149  *
150  * SOURCE
151  */
152 typedef enum {
153   SILC_ATTRIBUTE_CONTACT_NONE    = 0x00000000,    /* no specific method */
154   SILC_ATTRIBUTE_CONTACT_EMAIL   = 0x00000001,    /* email preferred */
155   SILC_ATTRIBUTE_CONTACT_CALL    = 0x00000002,    /* phone call preferred */
156   SILC_ATTRIBUTE_CONTACT_PAGE    = 0x00000004,    /* "paging" preferred */
157   SILC_ATTRIBUTE_CONTACT_SMS     = 0x00000008,    /* SMS preferred */
158   SILC_ATTRIBUTE_CONTACT_MMS     = 0x00000010,    /* MMS preferred */
159   SILC_ATTRIBUTE_CONTACT_CHAT    = 0x00000020,    /* chatting preferred */
160   SILC_ATTRIBUTE_CONTACT_VIDEO   = 0x00000040,    /* video conferencing */
161 } SilcAttributeContact;
162 /***/
163
164 /****d* silccore/SilcAttributesAPI/SilcAttributeDevice
165  *
166  * NAME
167  *
168  *    typedef enum { ... } SilcAttributeDevice;
169  *
170  * DESCRIPTION
171  *
172  *    The defined device types defined by the specification.
173  *
174  * SOURCE
175  */
176 typedef enum {
177   SILC_ATTRIBUTE_DEVICE_COMPUTER      = 0,        /* device is computer */
178   SILC_ATTRIBUTE_DEVICE_MOBILE_PHONE  = 1,        /* device is mobile phone */
179   SILC_ATTRIBUTE_DEVICE_PDA           = 2,        /* device is PDA */
180   SILC_ATTRIBUTE_DEVICE_TERMINAL      = 3,        /* device is terminal */
181 } SilcAttributeDevice;
182 /***/
183
184 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_alloc
185  *
186  * SYNOPSIS
187  *
188  *    SilcAttributesPayload
189  *    silc_attribute_payload_alloc(SilcAttribute attribute,
190  *                                 SilcAttributeFlags flags,
191  *                                 void *object,
192  *                                 SilcUInt32 object_size);
193  *
194  * DESCRIPTION
195  *
196  *    Allocates and encodes the attribute indicated by `attribute' and
197  *    returns pointer to the attribute.
198  *
199  *    The `object' must always be the same data type as defined with
200  *    SilcAttribute (see the comments) for all attributes.
201  *
202  ***/
203 SilcAttributePayload silc_attribute_payload_alloc(SilcAttribute attribute,
204                                                   SilcAttributeFlags flags,
205                                                   void *object,
206                                                   SilcUInt32 object_size);
207
208 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_parse
209  *
210  * SYNOPSIS
211  *
212  *    SilcDList
213  *    silc_attribute_payload_parse(const unsigned char *payload,
214  *                                 SilcUInt32 payload_len);
215  *
216  * DESCRIPTION
217  *
218  *    Parses list of Attribute payloads returning list of payloads.
219  *    One entry in the returned list is SilcAttributesPayload.  You
220  *    can produce such a list with silc_attribute_payload_encode
221  *    function.
222  *
223  ***/
224 SilcDList silc_attribute_payload_parse(const unsigned char *payload,
225                                        SilcUInt32 payload_len);
226
227 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_encode
228  *
229  * SYNOPSIS
230  *
231  *    SilcBuffer silc_attribute_payload_encode(SilcBuffer attrs,
232  *                                             SilcAttribute attribute,
233  *                                             SilcAttributeFlags flags,
234  *                                             void *object,
235  *                                             SilcUInt32 object_size);
236  *
237  * DESCRIPTION
238  *
239  *    Encodes one attribute payload into the `attrs' buffer and returns
240  *    pointer to the buffer, which may be different in case the buffer
241  *    was reallocated.  If `attrs' is NULL for first attribute this
242  *    allocates the buffer and returns it.  This can be called multiple
243  *    times to add multiple attributes to the `attrs' buffer.  The `flags'
244  *    indicates the validity of the added attribute.  Returns NULL on
245  *    error.
246  *
247  *    The `object' must always be the same data type as defined with
248  *    SilcAttribute (see the comments) for all attributes.
249  *
250  ***/
251 SilcBuffer silc_attribute_payload_encode(SilcBuffer attrs,
252                                          SilcAttribute attribute,
253                                          SilcAttributeFlags flags,
254                                          void *object,
255                                          SilcUInt32 object_size);
256
257 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_encode_data
258  *
259  * SYNOPSIS
260  *
261  *    SilcBuffer
262  *    silc_attribute_payload_encode_data(SilcBuffer attrs,
263  *                                       SilcAttribute attribute,
264  *                                       SilcAttributeFlags flags,
265  *                                       const unsigned char *data,
266  *                                       SilcUInt32 data_len);
267  *
268  * DESCRIPTION
269  *
270  *    Same function as silc_attribute_payload_encode except the attribute
271  *    is already encoded into `data' of `data_len' bytes in length.
272  *    Encodes the attribute into the `attrs' buffer and returns pointer
273  *    to the buffer, which may be different in case the buffer was
274  *    reallocated.  If `attrs' is NULL for first attribute this allocates
275  *    the buffer and returns it.  Returns NULL on error.
276  *
277  ***/
278 SilcBuffer silc_attribute_payload_encode_data(SilcBuffer attrs,
279                                               SilcAttribute attribute,
280                                               SilcAttributeFlags flags,
281                                               const unsigned char *data,
282                                               SilcUInt32 data_len);
283
284 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_free
285  *
286  * SYNOPSIS
287  *
288  *    void silc_attribute_payload_free(SilcAttributePayload payload);
289  *
290  * DESCRIPTION
291  *
292  *    Frees the Attribute Payload and all data in it.
293  *
294  ***/
295 void silc_attribute_payload_free(SilcAttributePayload payload);
296
297 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_list_free
298  *
299  * SYNOPSIS
300  *
301  *    void silc_attribute_payload_list_free(SilcDList list);
302  *
303  * DESCRIPTION
304  *
305  *    Frees list of Attribute Payloads and all data in them.
306  *
307  ***/
308 void silc_attribute_payload_list_free(SilcDList list);
309
310 /****f* silccore/SilcAttributesAPI/silc_attribute_get_attribute
311  *
312  * SYNOPSIS
313  *
314  *    SilcAttribute silc_attribute_get_attribute(SilcAttributePayload payload);
315  *
316  * DESCRIPTION
317  *
318  *    Return the attribute type from the payload indicated by `payload'.
319  *
320  ***/
321 SilcAttribute silc_attribute_get_attribute(SilcAttributePayload payload);
322
323 /****f* silccore/SilcAttributesAPI/silc_attribute_get_flags
324  *
325  * SYNOPSIS
326  *
327  *    SilcAttributeFlags
328  *    silc_attribute_get_flags(SilcAttributePayload payload);
329  *
330  * DESCRIPTION
331  *
332  *    Return the attribute flags from the payload indicated by `payload'.
333  *
334  ***/
335 SilcAttributeFlags silc_attribute_get_flags(SilcAttributePayload payload);
336
337 /****f* silccore/SilcAttributesAPI/silc_attribute_get_data
338  *
339  * SYNOPSIS
340  *
341  *    const unsigned char *
342  *    silc_attribute_get_data(SilcAttributePayload payload,
343  *                            SilcUInt32 *data_len);
344  *
345  * DESCRIPTION
346  *
347  *    Returns the attribute data from the payload indicated by the `payload'
348  *    The caller must not free the returned data pointer.
349  *
350  ***/
351 const unsigned char *silc_attribute_get_data(SilcAttributePayload payload,
352                                              SilcUInt32 *data_len);
353
354 /****f* silccore/SilcAttributesAPI/silc_attribute_get_verify_data
355  *
356  * SYNOPSIS
357  *
358  *    unsigned char *
359  *    silc_attribute_get_verify_data(SilcDList attrs,
360  *                                   bool server_verification,
361  *                                   SilcUInt32 *data_len);
362  *
363  * DESCRIPTION
364  *
365  *    Constructs the data to be verified with the sender's digital
366  *    signature and sender's public key.  This allocates the data from
367  *    the list of attribute payloads and returns the buffer.  The caller
368  *    must free it.  If `server_verification' is FALSE then data is
369  *    constructed for user's digital signature verification, if it is
370  *    TRUE then it is constructed for server's digital signature
371  *    verification.
372  *
373  ***/
374 unsigned char *silc_attribute_get_verify_data(SilcDList attrs,
375                                               bool server_verification,
376                                               SilcUInt32 *data_len);
377
378 /* Object structures */
379
380 /****s* silccore/SilcAttributesAPI/SilcAttributeObjService
381  *
382  * NAME
383  *
384  *    typedef struct { ... } SilcAttributeObjService;
385  *
386  * DESCRIPTION
387  *
388  *    SILC_ATTRIBUTE_SERVICE type object structure.
389  *
390  * SOURCE
391  */
392 typedef struct SilcAttributeObjServiceStruct {
393   SilcUInt32 port;              /* IANA specified service port */
394   SilcUInt32 idle;              /* Idle time in the service */
395   char signon[64];              /* Signon date and time (UTC) */
396   char address[256];            /* service address */
397   bool status;                  /* online status (TRUE present in service) */
398 } SilcAttributeObjService;
399 /***/
400
401 /****s* silccore/SilcAttributesAPI/SilcAttributeObjMime
402  *
403  * NAME
404  *
405  *    typedef struct { ... } SilcAttributeObjMime;
406  *
407  * DESCRIPTION
408  *
409  *    Data type for MIME object as attribute.  The data in the structure
410  *    is valid as long as the payload structure is valid.
411  *
412  * SOURCE
413  */
414 typedef struct SilcAttributeObjMimeStruct {
415   const unsigned char *mime;    /* MIME buffer */
416   SilcUInt32 mime_len;          /* length of the MIME buffer */
417 } SilcAttributeObjMime;
418 /***/
419
420 /****s* silccore/SilcAttributesAPI/SilcAttributeObjGeo
421  *
422  * NAME
423  *
424  *    typedef struct { ... } SilcAttributeObjGeo;
425  *
426  * DESCRIPTION
427  *
428  *    SILC_ATTRIBUTE_GEOLOCATION type object.  The caller must free the
429  *    strings inside the structure.
430  *
431  * SOURCE
432  */
433 typedef struct SilcAttributeObjGeoStruct {
434   char *longitude;              /* Longitude */
435   char *latitude;               /* Latitude */
436   char *altitude;               /* Altitude */
437   char *accuracy;               /* Accuracy in meters */
438 } SilcAttributeObjGeo;
439 /***/
440
441 /****s* silccore/SilcAttributesAPI/SilcAttributeObjDevice
442  *
443  * NAME
444  *
445  *    typedef struct { ... } SilcAttributeObjDevice;
446  *
447  * DESCRIPTION
448  *
449  *    SILC_ATTRIBUTE_DEVICE_INFO type object.  The caller must free the
450  *    strings inside the structure.
451  *
452  * SOURCE
453  */
454 typedef struct SilcAttributeObjDeviceStruct {
455   SilcAttributeDevice type;     /* device type */
456   char *manufacturer;           /* manufacturer of the device */
457   char *version;                /* device version string */
458   char *model;                  /* device model string */
459   char *language;               /* device language (ISO 639-2/T) */
460 } SilcAttributeObjDevice;
461 /***/
462
463 /****s* silccore/SilcAttributesAPI/SilcAttributeObjPk
464  *
465  * NAME
466  *
467  *    typedef struct { ... } SilcAttributeObjPk;
468  *
469  * DESCRIPTION
470  *
471  *    Data type for public key, certificate or digital signatures.  The
472  *    caller must free the data inside the structure.  The 'type' is one
473  *    of following: "silc-rsa", "silc-dss, "ssh-rsa", "ssh-dss",
474  *    "pgp-sign-rsa", "pgp-sign-dss", "x509v3-sign-rsa", "x509v3-sign-dss".
475  *    See the draft-riikonen-precense-attrs draft for more detailed
476  *    information.  The 'type' is NULL when this structure includes a
477  *    digital signature.
478  *
479  *    In SILC, at least the "silc-rsa" must be supported.  In this case
480  *    the key is normal SILC Public key.  To verify a signature with the
481  *    SILC Public key, construct the signature data with the
482  *    silc_attribute_get_verify_data and verify the signature with
483  *    for example silc_pkcs_verify_with_hash function.  The public key
484  *    to the verification is the `data' and `data_len', and can be decoded
485  *    with silc_pkcs_public_key_decode function.
486  *
487  * SOURCE
488  */
489 typedef struct SilcAttributeObjPkStruct {
490   char *type;                   /* public key/certificate type, NULL
491                                    when contains digital signature. */
492   unsigned char *data;          /* public key/cert/signature data. The
493                                    encoding depends of the `type'. */
494   SilcUInt32 data_len;          /* data length */
495 } SilcAttributeObjPk;
496 /***/
497
498 /****f* silccore/SilcAttributesAPI/silc_attribute_get_object
499  *
500  * SYNOPSIS
501  *
502  *    bool silc_attribute_get_object(SilcAttributePayload payload,
503  *                                   void *object,
504  *                                   SilcUInt32 object_size);
505  *
506  * DESCRIPTION
507  *
508  *    Returns the already parsed attribute object from the payload
509  *    indicated by `payload'.  Copies the data into the `object' which
510  *    must be sent as argument (and must be of correct type and size).
511  *    The `object_size' indicates the size of the `*object' sent.
512  *    Returns TRUE if the `attribute' attribute was found and FALSE
513  *    if such attribute is not present in the `payload', or the `object_size'
514  *    is not sufficient.  See the definition of SilcAttribute for the
515  *    list of attributes and the required object types for attributes.
516  *    You can use silc_attribute_get_attribute to get the SilcAttribute
517  *    type from the `payload'.
518  *
519  * EXAMPLE
520  *
521  *    SilcAttributeObjDevice dev;
522  *
523  *    ...
524  *    case SILC_ATTRIBUTE_DEVICE_INFO:
525  *    memset(&dev, 0, sizeof(dev));
526  *    if (!silc_attribute_get_object(payload, (void *)&dev, sizeof(dev)))
527  *      error();
528  *    ...
529  *
530  ***/
531 bool silc_attribute_get_object(SilcAttributePayload payload,
532                                void *object, SilcUInt32 object_size);
533
534 #endif /* SILCATTRS_H */