access: remake of sipe_find_access_level()
[siplcs.git] / src / core / uuid.c
blobd6b416b087aed14cfa0fe6e588d4995662ac6d6d
1 /**
2 * @file uuid.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdio.h>
20 #include <string.h>
22 #include <glib.h>
23 #include <glib/gprintf.h>
25 #include "cipher.h"
27 #include "uuid.h"
29 static const char *epid_ns_uuid = "fcacfb03-8a73-46ef-91b1-e5ebeeaba4fe";
32 * This assumes that the structure is correctly packed on all target
33 * platforms, i.e. sizeof(uuid_t) == 16
35 * See also the test added to "configure". On Windows platform we know
36 * that #pragma pack() exists and therefore can use it in the code.
39 #ifdef _WIN32
40 #pragma pack(push, 1)
41 #endif
42 typedef struct {
43 guint32 time_low;
44 guint16 time_mid;
45 guint16 time_hi_and_version;
46 guint8 clock_seq_hi_and_reserved;
47 guint8 clock_seq_low;
48 guint8 node[6];
49 } uuid_t;
50 #ifdef _WIN32
51 #pragma pack(pop)
52 #endif
54 #define UUID_OFFSET_TO_LAST_SEGMENT 24
56 static void readUUID(const char *string, uuid_t *uuid)
58 int i;
59 /* Some platforms don't allow scanning to char using %02hhx */
60 short tmp1, tmp2;
62 sscanf(string, "%08x-%04hx-%04hx-%02hx%02hx-", &uuid->time_low
63 , &uuid->time_mid, &uuid->time_hi_and_version
64 , &tmp1, &tmp2);
65 uuid->clock_seq_hi_and_reserved = tmp1;
66 uuid->clock_seq_low = tmp2;
68 for(i=0;i<6;i++)
70 sscanf(&string[UUID_OFFSET_TO_LAST_SEGMENT+i*2], "%02hx", &tmp1);
71 uuid->node[i] = tmp1;
75 static void printUUID(uuid_t *uuid, char *string)
77 int i;
78 size_t pos;
79 sprintf(string, "%08x-%04x-%04x-%02x%02x-", uuid->time_low
80 , uuid->time_mid, uuid->time_hi_and_version
81 , uuid->clock_seq_hi_and_reserved
82 , uuid->clock_seq_low
84 pos = strlen(string);
85 for(i=0;i<6;i++)
87 pos += sprintf(&string[pos], "%02x", uuid->node[i]);
91 static void createUUIDfromHash(uuid_t *uuid, const unsigned char *hash)
93 memcpy(uuid, hash, sizeof(uuid_t));
94 uuid->time_hi_and_version &= GUINT16_TO_LE(0x0FFF);
95 uuid->time_hi_and_version |= GUINT16_TO_LE(0x5000);
96 uuid->clock_seq_hi_and_reserved &= 0x3F;
97 uuid->clock_seq_hi_and_reserved |= 0x80;
100 char *generateUUIDfromEPID(const gchar *epid)
102 uuid_t result;
103 PurpleCipherContext *ctx;
104 unsigned char hash[20];
105 char buf[512];
107 readUUID(epid_ns_uuid, &result);
109 result.time_low = GUINT32_FROM_LE(result.time_low);
110 result.time_mid = GUINT16_FROM_LE(result.time_mid);
111 result.time_hi_and_version = GUINT16_FROM_LE(result.time_hi_and_version);
113 memcpy(buf, &result, sizeof(uuid_t));
114 strcpy(&buf[sizeof(uuid_t)], epid);
116 ctx = purple_cipher_context_new_by_name("sha1", NULL);
117 purple_cipher_context_append(ctx, (guchar *) buf, strlen(buf));
118 purple_cipher_context_digest(ctx, sizeof(hash), hash, NULL);
119 purple_cipher_context_destroy(ctx);
121 createUUIDfromHash(&result, hash);
123 result.time_low = GUINT32_TO_LE(result.time_low);
124 result.time_mid = GUINT16_TO_LE(result.time_mid);
125 result.time_hi_and_version = GUINT16_TO_LE(result.time_hi_and_version);
127 printUUID(&result, buf);
128 return g_strdup(buf);
132 * Generates epid from user SIP URI, hostname and IP address.
133 * Thus epid will be the same each start and
134 * not needed to be persistent.
136 * Using MAC address proved to be poorly portable solution.
138 * Must be g_free()'d
140 char *sipe_get_epid(const char *self_sip_uri,
141 const char *hostname,
142 const char *ip_address)
144 /* 6 last digits of hash */
145 #define SIPE_EPID_HASH_START 14
146 #define SIPE_EPID_HASH_END 20
147 #define SIPE_EPID_LENGTH (2 * (SIPE_EPID_HASH_END - SIPE_EPID_HASH_START + 1))
149 int i,j;
150 PurpleCipherContext *ctx;
151 unsigned char hash[SIPE_EPID_HASH_END];
152 char out[SIPE_EPID_LENGTH + 1];
153 char *buf = g_strdup_printf("%s:%s:%s", self_sip_uri, hostname, ip_address);
155 ctx = purple_cipher_context_new_by_name("sha1", NULL);
156 purple_cipher_context_append(ctx, (guchar *)buf, strlen(buf));
157 purple_cipher_context_digest(ctx, sizeof(hash), hash, NULL);
158 purple_cipher_context_destroy(ctx);
160 for (i = SIPE_EPID_HASH_START, j = 0;
161 i < SIPE_EPID_HASH_END;
162 i++, j += 2) {
163 g_sprintf(&out[j], "%02x", hash[i]);
165 out[SIPE_EPID_LENGTH] = 0;
167 g_free(buf);
168 return g_strdup(out);
172 Local Variables:
173 mode: c
174 c-file-style: "bsd"
175 indent-tabs-mode: t
176 tab-width: 8
177 End: