adam@14
|
1 #include <ctype.h>
|
adam@1
|
2 #include <string.h>
|
adam@1
|
3
|
adam@6
|
4 #include <openssl/bio.h>
|
adam@6
|
5 #include <openssl/evp.h>
|
adam@6
|
6 #include <openssl/buffer.h>
|
adam@0
|
7 #include <openssl/sha.h>
|
adam@7
|
8 #include <openssl/hmac.h>
|
adam@8
|
9 #include <openssl/dh.h>
|
adam@0
|
10 #include <curl/curl.h>
|
adam@1
|
11 #include <expat.h>
|
adam@0
|
12
|
adam@26
|
13 #include <pthread.h>
|
adam@26
|
14
|
adam@1
|
15 #include <openid.h>
|
adam@0
|
16
|
adam@3
|
17 #define BUF_MAX 10240
|
adam@3
|
18 #define BUF_INIT 1024
|
adam@3
|
19
|
adam@26
|
20 #define DEFAULT_PRIME "DCF93A0B883972EC0E19989AC5A2CE310E1D37717E8D9571BB7623731866E61EF75A2E27898B057F9891C2E27A639C3F29B60814581CD3B2CA3986D2683705577D45C2E7E52DC81C7A171876E5CEA74B1448BFDFAF18828EFD2519F14E45E3826634AF1949E5B535CC829A483B8A76223E5D490A257F05BDFF16F2FB22C583AB"
|
adam@26
|
21 #define DEFAULT_GENERATOR "2"
|
adam@26
|
22
|
adam@26
|
23 static BIGNUM *default_prime, *default_generator;
|
adam@2
|
24
|
adam@2
|
25 uw_Basis_string uw_OpenidFfi_endpoint(uw_context ctx, uw_OpenidFfi_discovery d) {
|
adam@8
|
26 return d.endpoint;
|
adam@2
|
27 }
|
adam@2
|
28
|
adam@2
|
29 uw_Basis_string uw_OpenidFfi_localId(uw_context ctx, uw_OpenidFfi_discovery d) {
|
adam@8
|
30 return d.localId;
|
adam@2
|
31 }
|
adam@2
|
32
|
adam@26
|
33 static pthread_mutex_t *locks;
|
adam@26
|
34
|
adam@26
|
35 static void locking_function(int mode, int n, const char *file, int line) {
|
adam@26
|
36 if (mode & CRYPTO_LOCK)
|
adam@26
|
37 pthread_mutex_lock(&locks[n]);
|
adam@26
|
38 else
|
adam@26
|
39 pthread_mutex_unlock(&locks[n]);
|
adam@26
|
40 }
|
adam@26
|
41
|
adam@0
|
42 uw_unit uw_OpenidFfi_init(uw_context ctx) {
|
adam@26
|
43 int nl = CRYPTO_num_locks(), i;
|
adam@26
|
44 locks = malloc(sizeof(pthread_mutex_t) * nl);
|
adam@26
|
45 for (i = 0; i < nl; ++i)
|
adam@26
|
46 pthread_mutex_init(&locks[i], NULL);
|
adam@26
|
47
|
adam@26
|
48 CRYPTO_set_locking_callback(locking_function);
|
adam@0
|
49 curl_global_init(CURL_GLOBAL_ALL);
|
adam@0
|
50
|
adam@26
|
51 BN_hex2bn(&default_prime, DEFAULT_PRIME);
|
adam@26
|
52 BN_dec2bn(&default_generator, DEFAULT_GENERATOR);
|
adam@26
|
53
|
adam@0
|
54 return uw_unit_v;
|
adam@0
|
55 }
|
adam@1
|
56
|
adam@1
|
57 static CURL *curl(uw_context ctx) {
|
adam@1
|
58 CURL *r;
|
adam@1
|
59
|
adam@1
|
60 if (!(r = uw_get_global(ctx, "curl"))) {
|
adam@1
|
61 r = curl_easy_init();
|
adam@1
|
62 if (r)
|
adam@1
|
63 uw_set_global(ctx, "curl", r, curl_easy_cleanup);
|
adam@1
|
64 }
|
adam@1
|
65
|
adam@1
|
66 return r;
|
adam@1
|
67 }
|
adam@1
|
68
|
adam@26
|
69 typedef enum { NONE, SERVICE, TYPE, MATCHED, URI } xrds_mode;
|
adam@26
|
70
|
adam@1
|
71 typedef struct {
|
adam@1
|
72 uw_context ctx;
|
adam@8
|
73 uw_OpenidFfi_discovery *d;
|
adam@26
|
74 xrds_mode mode;
|
adam@27
|
75 int cur_priority, max_priority;
|
adam@1
|
76 } endpoint;
|
adam@1
|
77
|
adam@1
|
78 static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
|
adam@1
|
79 endpoint *ep = userData;
|
adam@1
|
80
|
adam@26
|
81 if (!strncmp(name, "xrd:", 4))
|
adam@26
|
82 name += 4;
|
adam@26
|
83
|
adam@1
|
84 if (!strcmp(name, "link")) {
|
adam@1
|
85 const XML_Char **attp;
|
adam@1
|
86 int found = 0;
|
adam@1
|
87
|
adam@1
|
88 for (attp = atts; *attp; attp += 2) {
|
adam@1
|
89 if (!strcmp(attp[0], "rel") && !strcmp(attp[1], "openid2.provider")) {
|
adam@1
|
90 found = 1;
|
adam@1
|
91 break;
|
adam@1
|
92 }
|
adam@1
|
93 }
|
adam@1
|
94
|
adam@1
|
95 if (found) {
|
adam@1
|
96 for (attp = atts; *attp; attp += 2) {
|
adam@1
|
97 if (!strcmp(attp[0], "href")) {
|
adam@2
|
98 ep->d->endpoint = uw_strdup(ep->ctx, attp[1]);
|
adam@1
|
99 return;
|
adam@1
|
100 }
|
adam@1
|
101 }
|
adam@1
|
102 }
|
adam@1
|
103 }
|
adam@27
|
104 else if (!strcmp(name, "Service")) {
|
adam@27
|
105 const XML_Char **attp;
|
adam@27
|
106
|
adam@27
|
107 ep->cur_priority = 0;
|
adam@27
|
108 for (attp = atts; *attp; attp += 2)
|
adam@27
|
109 if (!strcmp(attp[0], "priority")) {
|
adam@27
|
110 ep->cur_priority = atoi(attp[1]);
|
adam@27
|
111 break;
|
adam@27
|
112 }
|
adam@27
|
113
|
adam@26
|
114 ep->mode = SERVICE;
|
adam@27
|
115 } else if (!strcmp(name, "Type")) {
|
adam@26
|
116 if (ep->mode == SERVICE)
|
adam@26
|
117 ep->mode = TYPE;
|
adam@26
|
118 }
|
adam@26
|
119 else if (!strcmp(name, "URI")) {
|
adam@26
|
120 if (ep->mode == MATCHED)
|
adam@26
|
121 ep->mode = URI;
|
adam@26
|
122 }
|
adam@26
|
123 }
|
adam@26
|
124
|
adam@26
|
125 static char server[] = "http://specs.openid.net/auth/2.0/server";
|
adam@26
|
126 static char signon[] = "http://specs.openid.net/auth/2.0/signon";
|
adam@26
|
127
|
adam@26
|
128 static void XMLCALL cdata(void *userData, const XML_Char *s, int len) {
|
adam@26
|
129 endpoint *ep = userData;
|
adam@26
|
130
|
adam@26
|
131 switch (ep->mode) {
|
adam@26
|
132 case TYPE:
|
adam@26
|
133 if ((len == sizeof(server)-1 && !memcmp(server, s, sizeof(server)-1))
|
adam@26
|
134 || (len == sizeof(signon)-1 && !memcmp(signon, s, sizeof(signon)-1)))
|
adam@26
|
135 ep->mode = MATCHED;
|
adam@26
|
136 break;
|
adam@26
|
137 case URI:
|
adam@27
|
138 if (ep->cur_priority < ep->max_priority) {
|
adam@27
|
139 ep->d->endpoint = uw_malloc(ep->ctx, len+1);
|
adam@27
|
140 memcpy(ep->d->endpoint, s, len);
|
adam@27
|
141 ep->d->endpoint[len] = 0;
|
adam@27
|
142 ep->max_priority = ep->cur_priority;
|
adam@27
|
143 }
|
adam@26
|
144 break;
|
adam@26
|
145 default:
|
adam@26
|
146 break;
|
adam@26
|
147 }
|
adam@26
|
148 }
|
adam@26
|
149
|
adam@26
|
150 static void XMLCALL endElement(void *userData, const XML_Char *name) {
|
adam@26
|
151 endpoint *ep = userData;
|
adam@26
|
152
|
adam@26
|
153 if (!strncmp(name, "xrd:", 4))
|
adam@26
|
154 name += 4;
|
adam@26
|
155
|
adam@26
|
156 if (!strcmp(name, "Service"))
|
adam@26
|
157 ep->mode = NONE;
|
adam@26
|
158 else if (!strcmp(name, "Type")) {
|
adam@26
|
159 if (ep->mode != MATCHED)
|
adam@26
|
160 ep->mode = SERVICE;
|
adam@26
|
161 }
|
adam@26
|
162 else if (!strcmp(name, "URI"))
|
adam@26
|
163 ep->mode = MATCHED;
|
adam@1
|
164 }
|
adam@1
|
165
|
adam@1
|
166 typedef struct {
|
adam@1
|
167 XML_Parser parser;
|
adam@1
|
168 int any_errors;
|
adam@3
|
169 } curl_discovery_data;
|
adam@1
|
170
|
adam@3
|
171 static size_t write_discovery_data(void *buffer, size_t size, size_t nmemb, void *userp) {
|
adam@3
|
172 curl_discovery_data *d = userp;
|
adam@1
|
173
|
adam@1
|
174 if (!XML_Parse(d->parser, buffer, size * nmemb, 0))
|
adam@1
|
175 d->any_errors = 1;
|
adam@1
|
176
|
adam@1
|
177 return size * nmemb;
|
adam@1
|
178 }
|
adam@1
|
179
|
adam@2
|
180 uw_OpenidFfi_discovery *uw_OpenidFfi_discover(uw_context ctx, uw_Basis_string id) {
|
adam@1
|
181 char *s;
|
adam@1
|
182 CURL *c = curl(ctx);
|
adam@3
|
183 curl_discovery_data cd = {};
|
adam@8
|
184 uw_OpenidFfi_discovery *dy = uw_malloc(ctx, sizeof(uw_OpenidFfi_discovery));
|
adam@27
|
185 endpoint ep = {ctx, dy, NONE, 0, INT_MAX};
|
adam@1
|
186 CURLcode code;
|
adam@27
|
187 struct curl_slist *headers = NULL;
|
adam@1
|
188
|
adam@2
|
189 dy->endpoint = dy->localId = NULL;
|
adam@2
|
190
|
adam@1
|
191 if (!strchr(id, ':')) {
|
adam@1
|
192 id = uw_Basis_strcat(ctx, "http://", id);
|
adam@1
|
193 if ((s = strchr(id, '#')) != NULL)
|
adam@1
|
194 *s = 0;
|
adam@1
|
195 } else if ((s = strchr(id, '#')) != NULL) {
|
adam@1
|
196 char *id2 = uw_malloc(ctx, s - id + 1);
|
adam@1
|
197 memcpy(id2, s, s - id);
|
adam@1
|
198 id2[s - id] = 0;
|
adam@1
|
199 id = id2;
|
adam@1
|
200 }
|
adam@1
|
201
|
adam@1
|
202 cd.parser = XML_ParserCreate(NULL);
|
adam@1
|
203 XML_SetUserData(cd.parser, &ep);
|
adam@1
|
204 uw_push_cleanup(ctx, (void (*)(void *))XML_ParserFree, cd.parser);
|
adam@26
|
205 XML_SetElementHandler(cd.parser, startElement, endElement);
|
adam@26
|
206 XML_SetCharacterDataHandler(cd.parser, cdata);
|
adam@1
|
207
|
adam@26
|
208 curl_easy_reset(c);
|
adam@40
|
209 curl_easy_setopt(c, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
|
adam@1
|
210 curl_easy_setopt(c, CURLOPT_URL, id);
|
adam@3
|
211 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_discovery_data);
|
adam@1
|
212 curl_easy_setopt(c, CURLOPT_WRITEDATA, &cd);
|
adam@27
|
213 curl_slist_append(headers, "Accept: application/xrds+xml");
|
adam@27
|
214 uw_push_cleanup(ctx, (void (*)(void *))curl_slist_free_all, headers);
|
adam@27
|
215 curl_easy_setopt(c, CURLOPT_HTTPHEADER, headers);
|
adam@1
|
216
|
adam@1
|
217 code = curl_easy_perform(c);
|
adam@1
|
218 uw_pop_cleanup(ctx);
|
adam@27
|
219 uw_pop_cleanup(ctx);
|
adam@1
|
220
|
adam@40
|
221 if (code) {
|
adam@40
|
222 uw_Basis_debug(ctx, "CURL error:");
|
adam@40
|
223 uw_Basis_debug(ctx, (char*)curl_easy_strerror(code));
|
adam@40
|
224 }
|
adam@40
|
225 else if (!dy->endpoint)
|
adam@40
|
226 uw_Basis_debug(ctx, "Couldn't parse endpoint from page");
|
adam@40
|
227
|
adam@8
|
228 if (code || !dy->endpoint)
|
adam@1
|
229 return NULL;
|
adam@8
|
230 else
|
adam@8
|
231 return dy;
|
adam@1
|
232 }
|
adam@3
|
233
|
adam@3
|
234 uw_OpenidFfi_inputs uw_OpenidFfi_createInputs(uw_context ctx) {
|
adam@3
|
235 uw_buffer *r = uw_malloc(ctx, sizeof(uw_buffer));
|
adam@3
|
236 uw_buffer_init(BUF_MAX, r, BUF_INIT);
|
adam@3
|
237 return r;
|
adam@3
|
238 }
|
adam@3
|
239
|
adam@8
|
240 static void postify(uw_OpenidFfi_inputs buf, uw_Basis_string s) {
|
adam@14
|
241 char hex[4];
|
adam@14
|
242
|
adam@8
|
243 for (; *s; ++s) {
|
adam@14
|
244 if (isalnum(*s))
|
adam@14
|
245 uw_buffer_append(buf, s, 1);
|
adam@14
|
246 else {
|
adam@14
|
247 sprintf(hex, "%%%02X", (unsigned char)*s);
|
adam@14
|
248 uw_buffer_append(buf, hex, 3);
|
adam@14
|
249 }
|
adam@8
|
250 }
|
adam@3
|
251 }
|
adam@3
|
252
|
adam@3
|
253 uw_unit uw_OpenidFfi_addInput(uw_context ctx, uw_OpenidFfi_inputs buf, uw_Basis_string key, uw_Basis_string value) {
|
adam@3
|
254 if (uw_buffer_used(buf) > 0)
|
adam@3
|
255 uw_buffer_append(buf, "&", 1);
|
adam@3
|
256
|
adam@14
|
257 uw_buffer_append(buf, key, strlen(key));
|
adam@3
|
258 uw_buffer_append(buf, "=", 1);
|
adam@8
|
259 postify(buf, value);
|
adam@3
|
260
|
adam@3
|
261 return uw_unit_v;
|
adam@3
|
262 }
|
adam@3
|
263
|
adam@3
|
264 uw_Basis_string uw_OpenidFfi_getOutput(uw_context ctx, uw_OpenidFfi_outputs buf, uw_Basis_string key) {
|
adam@3
|
265 char *s = buf->start;
|
adam@3
|
266
|
adam@3
|
267 for (; *s; s = strchr(strchr(s, 0)+1, 0)+1)
|
adam@3
|
268 if (!strcmp(key, s))
|
adam@3
|
269 return strchr(s, 0)+1;
|
adam@3
|
270
|
adam@3
|
271 return NULL;
|
adam@3
|
272 }
|
adam@3
|
273
|
adam@27
|
274 uw_unit uw_OpenidFfi_printOutputs(uw_context ctx, uw_OpenidFfi_outputs buf) {
|
adam@27
|
275 char *s = buf->start;
|
adam@27
|
276
|
adam@27
|
277 for (; *s; s = strchr(strchr(s, 0)+1, 0)+1)
|
adam@27
|
278 fprintf(stderr, "%s => %s\n", s, strchr(s, 0)+1);
|
adam@27
|
279
|
adam@27
|
280 return uw_unit_v;
|
adam@27
|
281 }
|
adam@27
|
282
|
adam@3
|
283 static size_t write_buffer_data(void *buffer, size_t size, size_t nmemb, void *userp) {
|
adam@3
|
284 uw_buffer *buf = userp;
|
adam@3
|
285
|
adam@3
|
286 uw_buffer_append(buf, buffer, size * nmemb);
|
adam@3
|
287
|
adam@3
|
288 return size * nmemb;
|
adam@3
|
289 }
|
adam@3
|
290
|
adam@3
|
291 const char curl_failure[] = "error\0Error fetching URL";
|
adam@3
|
292
|
adam@4
|
293 uw_OpenidFfi_outputs uw_OpenidFfi_direct(uw_context ctx, uw_Basis_string url, uw_OpenidFfi_inputs inps) {
|
adam@3
|
294 uw_buffer *buf = uw_malloc(ctx, sizeof(uw_buffer));
|
adam@3
|
295 CURL *c = curl(ctx);
|
adam@3
|
296 CURLcode code;
|
adam@3
|
297
|
adam@3
|
298 uw_buffer_init(BUF_MAX, buf, BUF_INIT);
|
adam@3
|
299
|
adam@3
|
300 uw_buffer_append(inps, "", 1);
|
adam@3
|
301
|
adam@26
|
302 curl_easy_reset(c);
|
adam@40
|
303 curl_easy_setopt(c, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
|
adam@3
|
304 curl_easy_setopt(c, CURLOPT_URL, url);
|
adam@3
|
305 curl_easy_setopt(c, CURLOPT_POSTFIELDS, inps->start);
|
adam@3
|
306 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_buffer_data);
|
adam@3
|
307 curl_easy_setopt(c, CURLOPT_WRITEDATA, buf);
|
adam@3
|
308
|
adam@3
|
309 code = curl_easy_perform(c);
|
adam@3
|
310 uw_buffer_append(buf, "", 1);
|
adam@3
|
311
|
adam@3
|
312 if (code) {
|
adam@3
|
313 uw_buffer_reset(buf);
|
adam@3
|
314 uw_buffer_append(buf, curl_failure, sizeof curl_failure);
|
adam@3
|
315 } else {
|
adam@3
|
316 char *s;
|
adam@3
|
317
|
adam@3
|
318 s = buf->start;
|
adam@3
|
319 while (*s) {
|
adam@3
|
320 char *colon = strchr(s, ':'), *newline;
|
adam@3
|
321
|
adam@3
|
322 if (!colon) {
|
adam@3
|
323 *s = 0;
|
adam@3
|
324 break;
|
adam@3
|
325 }
|
adam@3
|
326
|
adam@7
|
327 *colon = 0;
|
adam@7
|
328
|
adam@3
|
329 newline = strchr(colon+1, '\n');
|
adam@3
|
330
|
adam@7
|
331 if (!newline)
|
adam@3
|
332 break;
|
adam@3
|
333
|
adam@3
|
334 *newline = 0;
|
adam@3
|
335 s = newline+1;
|
adam@3
|
336 }
|
adam@3
|
337 }
|
adam@3
|
338
|
adam@3
|
339 return buf;
|
adam@3
|
340 }
|
adam@4
|
341
|
adam@4
|
342 static uw_Basis_string deurl(uw_context ctx, uw_Basis_string s) {
|
adam@4
|
343 uw_Basis_string r = uw_malloc(ctx, strlen(s)), s2 = r;
|
adam@4
|
344
|
adam@4
|
345 for (; *s; ++s) {
|
adam@4
|
346 if (s[0] == '%' && s[1] && s[2]) {
|
adam@4
|
347 unsigned u;
|
adam@4
|
348
|
adam@4
|
349 sscanf(s+1, "%02x", &u);
|
adam@4
|
350 *s2++ = u;
|
adam@4
|
351 s += 2;
|
adam@4
|
352 } else
|
adam@4
|
353 *s2++ = *s;
|
adam@4
|
354 }
|
adam@4
|
355
|
adam@4
|
356 *s2 = 0;
|
adam@4
|
357 return r;
|
adam@4
|
358 }
|
adam@4
|
359
|
adam@4
|
360 uw_OpenidFfi_outputs uw_OpenidFfi_indirect(uw_context ctx, uw_Basis_string fields) {
|
adam@4
|
361 uw_OpenidFfi_outputs b = malloc(sizeof(uw_buffer));
|
adam@4
|
362
|
adam@4
|
363 uw_buffer_init(BUF_MAX, b, BUF_INIT);
|
adam@4
|
364
|
adam@6
|
365 fields = uw_strdup(ctx, fields);
|
adam@6
|
366
|
adam@4
|
367 while (*fields) {
|
adam@4
|
368 char *equal = strchr(fields, '='), *and, *s;
|
adam@4
|
369
|
adam@4
|
370 if (!equal)
|
adam@4
|
371 break;
|
adam@4
|
372
|
adam@4
|
373 *equal = 0;
|
adam@4
|
374 s = deurl(ctx, fields);
|
adam@4
|
375 uw_buffer_append(b, s, strlen(s));
|
adam@4
|
376 uw_buffer_append(b, "", 1);
|
adam@4
|
377
|
adam@4
|
378 and = strchr(equal+1, '&');
|
adam@4
|
379 if (and) {
|
adam@4
|
380 *and = 0;
|
adam@4
|
381 fields = and+1;
|
adam@4
|
382 } else
|
adam@4
|
383 fields = and = strchr(equal+1, 0);
|
adam@4
|
384 s = deurl(ctx, equal+1);
|
adam@4
|
385 uw_buffer_append(b, s, strlen(s));
|
adam@4
|
386 uw_buffer_append(b, "", 1);
|
adam@4
|
387 }
|
adam@4
|
388
|
adam@4
|
389 uw_buffer_append(b, "", 1);
|
adam@4
|
390 return b;
|
adam@4
|
391 }
|
adam@6
|
392
|
adam@6
|
393 static uw_Basis_string base64(uw_context ctx, unsigned char *input, int length) {
|
adam@6
|
394 BIO *bmem, *b64;
|
adam@6
|
395
|
adam@6
|
396 b64 = BIO_new(BIO_f_base64());
|
adam@7
|
397 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
adam@6
|
398 bmem = BIO_new(BIO_s_mem());
|
adam@7
|
399 BIO_push(b64, bmem);
|
adam@6
|
400 BIO_write(b64, input, length);
|
adam@6
|
401 (void)BIO_flush(b64);
|
adam@6
|
402
|
adam@7
|
403 int len = BIO_ctrl_pending(bmem);
|
adam@7
|
404 char *buff = uw_malloc(ctx, len+1);
|
adam@7
|
405 BIO_read(bmem, buff, len);
|
adam@7
|
406 buff[len] = 0;
|
adam@6
|
407
|
adam@6
|
408 BIO_free_all(b64);
|
adam@6
|
409
|
adam@6
|
410 return buff;
|
adam@6
|
411 }
|
adam@6
|
412
|
adam@8
|
413 static int unbase64(unsigned char *input, int length, unsigned char *buffer, int bufferLength)
|
adam@7
|
414 {
|
adam@7
|
415 BIO *b64, *bmem;
|
adam@8
|
416 int n;
|
adam@6
|
417
|
adam@7
|
418 b64 = BIO_new(BIO_f_base64());
|
adam@7
|
419 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
adam@7
|
420 bmem = BIO_new_mem_buf(input, length);
|
adam@7
|
421 BIO_push(b64, bmem);
|
adam@8
|
422 n = BIO_read(b64, buffer, bufferLength);
|
adam@6
|
423
|
adam@7
|
424 BIO_free_all(bmem);
|
adam@8
|
425
|
adam@8
|
426 return n;
|
adam@8
|
427 }
|
adam@8
|
428
|
adam@12
|
429 uw_Basis_string uw_OpenidFfi_hmac_sha1(uw_context ctx, uw_Basis_string key, uw_Basis_string data) {
|
adam@8
|
430 unsigned char keyBin[SHA_DIGEST_LENGTH], out[EVP_MAX_MD_SIZE];
|
adam@8
|
431 unsigned outLen;
|
adam@8
|
432
|
adam@8
|
433 unbase64((unsigned char *)key, strlen(key), keyBin, sizeof keyBin);
|
adam@8
|
434
|
adam@8
|
435 HMAC(EVP_sha1(), keyBin, sizeof keyBin, (unsigned char *)data, strlen(data), out, &outLen);
|
adam@8
|
436 return base64(ctx, out, outLen);
|
adam@6
|
437 }
|
adam@7
|
438
|
adam@12
|
439 uw_Basis_string uw_OpenidFfi_hmac_sha256(uw_context ctx, uw_Basis_string key, uw_Basis_string data) {
|
adam@7
|
440 unsigned char keyBin[SHA256_DIGEST_LENGTH], out[EVP_MAX_MD_SIZE];
|
adam@7
|
441 unsigned outLen;
|
adam@7
|
442
|
adam@7
|
443 unbase64((unsigned char *)key, strlen(key), keyBin, sizeof keyBin);
|
adam@7
|
444
|
adam@7
|
445 HMAC(EVP_sha256(), keyBin, sizeof keyBin, (unsigned char *)data, strlen(data), out, &outLen);
|
adam@7
|
446 return base64(ctx, out, outLen);
|
adam@7
|
447 }
|
adam@8
|
448
|
adam@8
|
449 static uw_Basis_string btwoc(uw_context ctx, const BIGNUM *n) {
|
adam@8
|
450 int len = BN_num_bytes(n), i;
|
adam@8
|
451 unsigned char bytes[len+1];
|
adam@8
|
452
|
adam@8
|
453 bytes[0] = 0;
|
adam@8
|
454 BN_bn2bin(n, bytes+1);
|
adam@8
|
455
|
adam@8
|
456 for (i = 1; i <= len; ++i)
|
adam@8
|
457 if (bytes[i]) {
|
adam@8
|
458 if (bytes[i] & 0x80)
|
adam@8
|
459 --i;
|
adam@8
|
460 break;
|
adam@8
|
461 }
|
adam@8
|
462
|
adam@8
|
463 if (i > len)
|
adam@8
|
464 i = len;
|
adam@8
|
465
|
adam@8
|
466 return base64(ctx, bytes+i, len+1-i);
|
adam@8
|
467 }
|
adam@8
|
468
|
adam@8
|
469 static BIGNUM *unbtwoc(uw_context ctx, uw_Basis_string s) {
|
adam@8
|
470 unsigned char bytes[1024];
|
adam@8
|
471 int len;
|
adam@8
|
472
|
adam@8
|
473 len = unbase64((unsigned char *)s, strlen(s), bytes, sizeof bytes);
|
adam@8
|
474 return BN_bin2bn(bytes, len, NULL);
|
adam@8
|
475 }
|
adam@8
|
476
|
adam@8
|
477 uw_Basis_string uw_OpenidFfi_modulus(uw_context ctx, uw_OpenidFfi_dh dh) {
|
adam@8
|
478 return btwoc(ctx, dh->p);
|
adam@8
|
479 }
|
adam@8
|
480
|
adam@8
|
481 uw_Basis_string uw_OpenidFfi_generator(uw_context ctx, uw_OpenidFfi_dh dh) {
|
adam@8
|
482 return btwoc(ctx, dh->g);
|
adam@8
|
483 }
|
adam@8
|
484
|
adam@8
|
485 uw_Basis_string uw_OpenidFfi_public(uw_context ctx, uw_OpenidFfi_dh dh) {
|
adam@8
|
486 return btwoc(ctx, dh->pub_key);
|
adam@8
|
487 }
|
adam@8
|
488
|
adam@8
|
489 static void free_DH(void *data, int will_retry) {
|
adam@8
|
490 DH *dh = data;
|
adam@26
|
491 dh->p = NULL;
|
adam@26
|
492 dh->g = NULL;
|
adam@8
|
493 DH_free(dh);
|
adam@8
|
494 }
|
adam@8
|
495
|
adam@8
|
496 uw_OpenidFfi_dh uw_OpenidFfi_generate(uw_context ctx) {
|
adam@8
|
497 DH *dh = DH_new();
|
adam@8
|
498
|
adam@26
|
499 dh->p = default_prime;
|
adam@26
|
500 dh->g = default_generator;
|
adam@8
|
501 uw_register_transactional(ctx, dh, NULL, NULL, free_DH);
|
adam@8
|
502
|
adam@8
|
503 if (DH_generate_key(dh) != 1)
|
adam@8
|
504 uw_error(ctx, FATAL, "Diffie-Hellman key generation failed");
|
adam@8
|
505
|
adam@8
|
506 return dh;
|
adam@8
|
507 }
|
adam@8
|
508
|
adam@8
|
509 uw_Basis_string uw_OpenidFfi_compute(uw_context ctx, uw_OpenidFfi_dh dh, uw_Basis_string server_pub) {
|
adam@8
|
510 BIGNUM *bn = unbtwoc(ctx, server_pub);
|
adam@12
|
511 unsigned char secret[DH_size(dh)+1], *secretP;
|
adam@8
|
512 int size;
|
adam@8
|
513
|
adam@8
|
514 uw_push_cleanup(ctx, (void (*)(void *))BN_free, bn);
|
adam@8
|
515
|
adam@12
|
516 size = DH_compute_key(secret+1, bn, dh);
|
adam@8
|
517 if (size == -1)
|
adam@8
|
518 uw_error(ctx, FATAL, "Diffie-Hellman key computation failed");
|
adam@8
|
519
|
adam@8
|
520 uw_pop_cleanup(ctx);
|
adam@8
|
521
|
adam@12
|
522 if (size > 0 && (secret[1] & 0x80)) {
|
adam@12
|
523 secret[0] = 0;
|
adam@12
|
524 secretP = secret;
|
adam@12
|
525 ++size;
|
adam@12
|
526 } else
|
adam@12
|
527 secretP = secret+1;
|
adam@12
|
528
|
adam@12
|
529 return base64(ctx, secretP, size);
|
adam@8
|
530 }
|
adam@12
|
531
|
adam@12
|
532 uw_Basis_string uw_OpenidFfi_sha1(uw_context ctx, uw_Basis_string data) {
|
adam@12
|
533 unsigned char dataBin[128], out[EVP_MAX_MD_SIZE];
|
adam@12
|
534 int len;
|
adam@12
|
535
|
adam@12
|
536 len = unbase64((unsigned char *)data, strlen(data), dataBin, sizeof dataBin);
|
adam@12
|
537
|
adam@12
|
538 SHA1(dataBin, len, out);
|
adam@12
|
539 return base64(ctx, out, SHA_DIGEST_LENGTH);
|
adam@12
|
540 }
|
adam@12
|
541
|
adam@12
|
542 uw_Basis_string uw_OpenidFfi_sha256(uw_context ctx, uw_Basis_string data) {
|
adam@12
|
543 unsigned char dataBin[128], out[EVP_MAX_MD_SIZE];
|
adam@12
|
544 int len;
|
adam@12
|
545
|
adam@12
|
546 len = unbase64((unsigned char *)data, strlen(data), dataBin, sizeof dataBin);
|
adam@12
|
547
|
adam@12
|
548 SHA256(dataBin, len, out);
|
adam@12
|
549 return base64(ctx, out, SHA256_DIGEST_LENGTH);
|
adam@12
|
550 }
|
adam@12
|
551
|
adam@12
|
552 uw_Basis_string uw_OpenidFfi_xor(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
|
adam@12
|
553 unsigned char buf1[128], buf2[128], bufO[128];
|
adam@12
|
554 int len1, len2, i;
|
adam@12
|
555
|
adam@12
|
556 len1 = unbase64((unsigned char *)s1, strlen(s1), buf1, sizeof buf1);
|
adam@12
|
557 len2 = unbase64((unsigned char *)s2, strlen(s2), buf2, sizeof buf2);
|
adam@12
|
558
|
adam@12
|
559 for (i = 0; i < len1; ++i)
|
adam@12
|
560 bufO[i] = buf1[i] ^ buf2[i % len2];
|
adam@12
|
561
|
adam@12
|
562 return base64(ctx, bufO, len1);
|
adam@12
|
563 }
|
adam@13
|
564
|
adam@13
|
565 uw_OpenidFfi_inputs uw_OpenidFfi_remode(uw_context ctx, uw_OpenidFfi_outputs out, uw_Basis_string mode) {
|
adam@13
|
566 uw_OpenidFfi_inputs in = uw_OpenidFfi_createInputs(ctx);
|
adam@13
|
567 char *s;
|
adam@13
|
568
|
adam@13
|
569 for (s = out->start; *s; s = strchr(strchr(s, 0)+1, 0)+1)
|
adam@13
|
570 if (!strcmp("openid.mode", s))
|
adam@13
|
571 uw_OpenidFfi_addInput(ctx, in, "openid.mode", mode);
|
adam@13
|
572 else
|
adam@13
|
573 uw_OpenidFfi_addInput(ctx, in, s, strchr(s, 0)+1);
|
adam@13
|
574
|
adam@13
|
575 return in;
|
adam@13
|
576 }
|