comparison src/c/driver.c @ 734:f2a2be93331c

Cookie signing working for forms
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Apr 2009 19:12:12 -0400
parents 1b1047992ecf
children d049d31a1966
comparison
equal deleted inserted replaced
733:15ddd64a5113 734:f2a2be93331c
7 #include <netinet/in.h> 7 #include <netinet/in.h>
8 #include <unistd.h> 8 #include <unistd.h>
9 #include <signal.h> 9 #include <signal.h>
10 10
11 #include <pthread.h> 11 #include <pthread.h>
12
13 #include <mhash.h>
12 14
13 #include "urweb.h" 15 #include "urweb.h"
14 16
15 int uw_backlog = 10; 17 int uw_backlog = 10;
16 int uw_bufsize = 1024; 18 int uw_bufsize = 1024;
100 } 102 }
101 103
102 return ctx; 104 return ctx;
103 } 105 }
104 106
107 #define KEYSIZE 16
108 #define PASSSIZE 4
109
110 #define HASH_ALGORITHM MHASH_SHA256
111 #define HASH_BLOCKSIZE 32
112 #define KEYGEN_ALGORITHM KEYGEN_MCRYPT
113
114 int uw_hash_blocksize = HASH_BLOCKSIZE;
115
116 static int password[PASSSIZE];
117 static unsigned char private_key[KEYSIZE];
118
119 static void init_crypto() {
120 KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
121 int i;
122
123 assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
124
125 for (i = 0; i < PASSSIZE; ++i)
126 password[i] = rand();
127
128 if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
129 private_key, sizeof(private_key),
130 (unsigned char*)password, sizeof(password)) < 0) {
131 printf("Key generation failed\n");
132 exit(1);
133 }
134 }
135
136 void uw_sign(const char *in, char *out) {
137 MHASH td;
138
139 td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
140 mhash_get_hash_pblock(HASH_ALGORITHM));
141
142 mhash(td, in, strlen(in));
143 if (mhash_hmac_deinit(td, out) < 0)
144 printf("Signing failed");
145 }
146
105 static void *worker(void *data) { 147 static void *worker(void *data) {
106 int me = *(int *)data, retries_left = MAX_RETRIES; 148 int me = *(int *)data, retries_left = MAX_RETRIES;
107 uw_context ctx = new_context(); 149 uw_context ctx = new_context();
108 150
109 while (1) { 151 while (1) {
342 printf("Exiting....\n"); 384 printf("Exiting....\n");
343 exit(0); 385 exit(0);
344 } 386 }
345 387
346 static void initialize() { 388 static void initialize() {
347 uw_context ctx = new_context(); 389 uw_context ctx;
348 failure_kind fk; 390 failure_kind fk;
391
392 init_crypto();
393
394 ctx = new_context();
349 395
350 if (!ctx) 396 if (!ctx)
351 exit(1); 397 exit(1);
352 398
353 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) { 399 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
409 fprintf(stderr, "Unexpected getopt() behavior\n"); 455 fprintf(stderr, "Unexpected getopt() behavior\n");
410 return 1; 456 return 1;
411 } 457 }
412 } 458 }
413 459
460 uw_global_init();
414 initialize(); 461 initialize();
415 462
416 names = calloc(nthreads, sizeof(int)); 463 names = calloc(nthreads, sizeof(int));
417 464
418 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking! 465 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
441 fprintf(stderr, "Socket listen failed\n"); 488 fprintf(stderr, "Socket listen failed\n");
442 return 1; 489 return 1;
443 } 490 }
444 491
445 sin_size = sizeof their_addr; 492 sin_size = sizeof their_addr;
446
447 uw_global_init();
448 493
449 printf("Listening on port %d....\n", uw_port); 494 printf("Listening on port %d....\n", uw_port);
450 495
451 { 496 {
452 pthread_t thread; 497 pthread_t thread;