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
24 #include <glib/gprintf.h>
28 static const char *epid_ns_uuid
= "fcacfb03-8a73-46ef-91b1-e5ebeeaba4fe";
31 * This assumes that the structure is correctly packed on all target
32 * platforms, i.e. sizeof(uuid_t) == 16
34 * See also the test added to "configure". On Windows platform we know
35 * that #pragma pack() exists and therefore can use it in the code.
44 guint16 time_hi_and_version
;
45 guint8 clock_seq_hi_and_reserved
;
53 #define UUID_OFFSET_TO_LAST_SEGMENT 24
55 static void readUUID(const char *string
, uuid_t
*uuid
)
58 sscanf(string
, "%08x-%04hx-%04hx-%02hhx%02hhx-", &uuid
->time_low
59 , &uuid
->time_mid
, &uuid
->time_hi_and_version
60 , &uuid
->clock_seq_hi_and_reserved
61 , &uuid
->clock_seq_low
);
65 sscanf(&string
[UUID_OFFSET_TO_LAST_SEGMENT
+i
*2], "%02hhx", &uuid
->node
[i
]);
69 static void printUUID(uuid_t
*uuid
, char *string
)
73 sprintf(string
, "%08x-%04x-%04x-%02x%02x-", uuid
->time_low
74 , uuid
->time_mid
, uuid
->time_hi_and_version
75 , uuid
->clock_seq_hi_and_reserved
81 pos
+= sprintf(&string
[pos
], "%02x", uuid
->node
[i
]);
85 static void createUUIDfromHash(uuid_t
*uuid
, const unsigned char *hash
)
87 memcpy(uuid
, hash
, sizeof(uuid_t
));
88 uuid
->time_hi_and_version
&= 0x0FFF;
89 uuid
->time_hi_and_version
|= 0x5000;
90 uuid
->clock_seq_hi_and_reserved
&= 0x3F;
91 uuid
->clock_seq_hi_and_reserved
|= 0x80;
94 char *generateUUIDfromEPID(const gchar
*epid
)
97 PurpleCipherContext
*ctx
;
98 unsigned char hash
[20];
101 readUUID(epid_ns_uuid
, &result
);
102 memcpy(buf
, &result
, sizeof(uuid_t
));
103 strcpy(&buf
[sizeof(uuid_t
)], epid
);
105 ctx
= purple_cipher_context_new_by_name("sha1", NULL
);
106 purple_cipher_context_append(ctx
, (guchar
*) buf
, strlen(buf
));
107 purple_cipher_context_digest(ctx
, sizeof(hash
), hash
, NULL
);
108 purple_cipher_context_destroy(ctx
);
110 createUUIDfromHash(&result
, hash
);
111 printUUID(&result
, buf
);
112 return g_strdup(buf
);
116 * Generates epid from user SIP URI, hostname and IP address.
117 * Thus epid will be the same each start and
118 * not needed to be persistent.
120 * Using MAC address proved to be poorly portable solution.
124 char *sipe_get_epid(const char *self_sip_uri
,
125 const char *hostname
,
126 const char *ip_address
)
128 /* 6 last digits of hash */
129 #define SIPE_EPID_HASH_START 14
130 #define SIPE_EPID_HASH_END 20
131 #define SIPE_EPID_LENGTH (2 * (SIPE_EPID_HASH_END - SIPE_EPID_HASH_START + 1))
134 PurpleCipherContext
*ctx
;
135 unsigned char hash
[SIPE_EPID_HASH_END
];
136 char out
[SIPE_EPID_LENGTH
+ 1];
137 char *buf
= g_strdup_printf("%s:%s:%s", self_sip_uri
, hostname
, ip_address
);
139 ctx
= purple_cipher_context_new_by_name("sha1", NULL
);
140 purple_cipher_context_append(ctx
, (guchar
*)buf
, strlen(buf
));
141 purple_cipher_context_digest(ctx
, sizeof(hash
), hash
, NULL
);
142 purple_cipher_context_destroy(ctx
);
144 for (i
= SIPE_EPID_HASH_START
, j
= 0;
145 i
< SIPE_EPID_HASH_END
;
147 g_sprintf(&out
[j
], "%02x", hash
[i
]);
149 out
[SIPE_EPID_LENGTH
] = 0;
152 return g_strdup(out
);