Mercurial > openid
comparison src/c/openid.c @ 7:976121190b2d
Authentication verification almost working: signatures not computing correctly
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Tue, 28 Dec 2010 19:57:25 -0500 |
parents | 99496175078b |
children | 870d99055dd1 |
comparison
equal
deleted
inserted
replaced
6:99496175078b | 7:976121190b2d |
---|---|
2 | 2 |
3 #include <openssl/bio.h> | 3 #include <openssl/bio.h> |
4 #include <openssl/evp.h> | 4 #include <openssl/evp.h> |
5 #include <openssl/buffer.h> | 5 #include <openssl/buffer.h> |
6 #include <openssl/sha.h> | 6 #include <openssl/sha.h> |
7 #include <openssl/hmac.h> | |
7 #include <curl/curl.h> | 8 #include <curl/curl.h> |
8 #include <expat.h> | 9 #include <expat.h> |
9 | 10 |
10 #include <openid.h> | 11 #include <openid.h> |
11 | 12 |
23 uw_Basis_string uw_OpenidFfi_localId(uw_context ctx, uw_OpenidFfi_discovery d) { | 24 uw_Basis_string uw_OpenidFfi_localId(uw_context ctx, uw_OpenidFfi_discovery d) { |
24 return d->localId; | 25 return d->localId; |
25 } | 26 } |
26 | 27 |
27 uw_unit uw_OpenidFfi_init(uw_context ctx) { | 28 uw_unit uw_OpenidFfi_init(uw_context ctx) { |
28 | |
29 | |
30 curl_global_init(CURL_GLOBAL_ALL); | 29 curl_global_init(CURL_GLOBAL_ALL); |
31 | 30 |
32 return uw_unit_v; | 31 return uw_unit_v; |
33 } | 32 } |
34 | 33 |
70 return; | 69 return; |
71 } | 70 } |
72 } | 71 } |
73 } | 72 } |
74 } | 73 } |
75 } | |
76 | |
77 static void XMLCALL endElement(void *userData, const XML_Char *name) { | |
78 } | 74 } |
79 | 75 |
80 typedef struct { | 76 typedef struct { |
81 XML_Parser parser; | 77 XML_Parser parser; |
82 int any_errors; | 78 int any_errors; |
113 } | 109 } |
114 | 110 |
115 cd.parser = XML_ParserCreate(NULL); | 111 cd.parser = XML_ParserCreate(NULL); |
116 XML_SetUserData(cd.parser, &ep); | 112 XML_SetUserData(cd.parser, &ep); |
117 uw_push_cleanup(ctx, (void (*)(void *))XML_ParserFree, cd.parser); | 113 uw_push_cleanup(ctx, (void (*)(void *))XML_ParserFree, cd.parser); |
118 XML_SetElementHandler(cd.parser, startElement, endElement); | 114 XML_SetStartElementHandler(cd.parser, startElement); |
119 | 115 |
120 curl_easy_setopt(c, CURLOPT_URL, id); | 116 curl_easy_setopt(c, CURLOPT_URL, id); |
121 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_discovery_data); | 117 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_discovery_data); |
122 curl_easy_setopt(c, CURLOPT_WRITEDATA, &cd); | 118 curl_easy_setopt(c, CURLOPT_WRITEDATA, &cd); |
123 | 119 |
213 if (!colon) { | 209 if (!colon) { |
214 *s = 0; | 210 *s = 0; |
215 break; | 211 break; |
216 } | 212 } |
217 | 213 |
214 *colon = 0; | |
215 | |
218 newline = strchr(colon+1, '\n'); | 216 newline = strchr(colon+1, '\n'); |
219 | 217 |
220 if (!newline) { | 218 if (!newline) |
221 *s = 0; | |
222 break; | 219 break; |
223 } | 220 |
224 | |
225 *colon = 0; | |
226 *newline = 0; | 221 *newline = 0; |
227 s = newline+1; | 222 s = newline+1; |
228 } | 223 } |
229 } | 224 } |
230 | 225 |
282 return b; | 277 return b; |
283 } | 278 } |
284 | 279 |
285 static uw_Basis_string base64(uw_context ctx, unsigned char *input, int length) { | 280 static uw_Basis_string base64(uw_context ctx, unsigned char *input, int length) { |
286 BIO *bmem, *b64; | 281 BIO *bmem, *b64; |
287 BUF_MEM *bptr; | |
288 | 282 |
289 b64 = BIO_new(BIO_f_base64()); | 283 b64 = BIO_new(BIO_f_base64()); |
284 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); | |
290 bmem = BIO_new(BIO_s_mem()); | 285 bmem = BIO_new(BIO_s_mem()); |
291 b64 = BIO_push(b64, bmem); | 286 BIO_push(b64, bmem); |
292 BIO_write(b64, input, length); | 287 BIO_write(b64, input, length); |
293 (void)BIO_flush(b64); | 288 (void)BIO_flush(b64); |
294 BIO_get_mem_ptr(b64, &bptr); | 289 |
295 | 290 int len = BIO_ctrl_pending(bmem); |
296 char *buff = uw_malloc(ctx, bptr->length); | 291 char *buff = uw_malloc(ctx, len+1); |
297 memcpy(buff, bptr->data, bptr->length-1); | 292 BIO_read(bmem, buff, len); |
298 buff[bptr->length-1] = 0; | 293 buff[len] = 0; |
299 | 294 |
300 BIO_free_all(b64); | 295 BIO_free_all(b64); |
301 | 296 |
302 return buff; | 297 return buff; |
303 } | 298 } |
304 | 299 |
305 uw_Basis_string uw_OpenidFfi_sha256(uw_context ctx, uw_Basis_string s) { | 300 static void unbase64(unsigned char *input, int length, unsigned char *buffer, int bufferLength) |
306 unsigned char out[SHA256_DIGEST_LENGTH]; | 301 { |
307 | 302 BIO *b64, *bmem; |
308 SHA256((unsigned char *)s, strlen(s), out); | 303 |
309 | 304 b64 = BIO_new(BIO_f_base64()); |
310 return base64(ctx, out, sizeof out); | 305 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
311 } | 306 bmem = BIO_new_mem_buf(input, length); |
307 BIO_push(b64, bmem); | |
308 BIO_read(b64, buffer, bufferLength); | |
309 | |
310 BIO_free_all(bmem); | |
311 } | |
312 | |
313 uw_Basis_string uw_OpenidFfi_sha256(uw_context ctx, uw_Basis_string key, uw_Basis_string data) { | |
314 unsigned char keyBin[SHA256_DIGEST_LENGTH], out[EVP_MAX_MD_SIZE]; | |
315 unsigned outLen; | |
316 | |
317 unbase64((unsigned char *)key, strlen(key), keyBin, sizeof keyBin); | |
318 memset(key, sizeof key, 0); | |
319 | |
320 HMAC(EVP_sha256(), keyBin, sizeof keyBin, (unsigned char *)data, strlen(data), out, &outLen); | |
321 return base64(ctx, out, outLen); | |
322 } |