Annotation of embedaddon/coova-chilli/src/main-response.c, revision 1.1.1.1

1.1       misho       1: /* 
                      2:  *
                      3:  * Copyright (C) 2003, 2004, 2005 Mondru AB.
                      4:  * Copyright (c) 2006-2007 David Bird <david@coova.com>
                      5:  *
                      6:  * The contents of this file may be used under the terms of the GNU
                      7:  * General Public License Version 2, provided that the above copyright
                      8:  * notice and this permission notice is included in all copies or
                      9:  * substantial portions of the software.
                     10:  * 
                     11:  */
                     12: 
                     13: #include "system.h"
                     14: #include "md5.h"
                     15: #define MD5LEN 16
                     16: 
                     17: static int usage(char *program) {
                     18:   fprintf(stderr, "Usage: %s <challenge> <uamsecret> <password>\n", program);
                     19:   return 1;
                     20: }
                     21: 
                     22: static int hextochar(char *src, unsigned char * dst) {
                     23:   char x[3];
                     24:   int n;
                     25:   int y;
                     26: 
                     27:   for (n=0; n < MD5LEN; n++) {
                     28:     x[0] = src[n*2+0];
                     29:     x[1] = src[n*2+1];
                     30:     x[2] = 0;
                     31: 
                     32:     if (sscanf(x, "%2x", &y) != 1)
                     33:       return -1;
                     34: 
                     35:     dst[n] = (unsigned char) y;
                     36:   }
                     37: 
                     38:   return 0;
                     39: }
                     40: 
                     41: static int chartohex(unsigned char *src, char *dst) {
                     42:   char x[3];
                     43:   int n;
                     44:   
                     45:   for (n=0; n < MD5LEN; n++) {
                     46:     snprintf(x, 3, "%.2x", src[n]);
                     47:     dst[n*2+0] = x[0];
                     48:     dst[n*2+1] = x[1];
                     49:   }
                     50:   dst[MD5LEN*2] = 0;
                     51:   return 0;
                     52: }
                     53: 
                     54: int main(int argc, char **argv) {
                     55:   unsigned char chap_ident = 0;
                     56:   unsigned char challenge[MD5LEN];
                     57:   unsigned char response[MD5LEN];
                     58:   char buffer[MD5LEN*3];
                     59:   MD5_CTX context;
                     60: 
                     61:   if (argc != 4 && argc != 5) 
                     62:     return usage(argv[0]);
                     63: 
                     64:   if (argc == 5) 
                     65:     chap_ident = atoi(argv[4]);
                     66: 
                     67:   /* challeng - argv 1 */
                     68:   memset(buffer, 0, sizeof(buffer));
                     69:   /*fprintf(stderr,"challenge: %s\n",argv[1]);*/
                     70:   strcpy(buffer, argv[1]);
                     71:   hextochar(buffer, challenge);
                     72: 
                     73:   MD5Init(&context);
                     74:   MD5Update(&context, challenge, MD5LEN);
                     75:   /* uamsecret - argv 2 */
                     76:   /*fprintf(stderr,"uamsecret: %s\n",argv[2]);*/
                     77:   MD5Update(&context, (uint8_t*)argv[2], strlen(argv[2]));
                     78:   MD5Final(challenge, &context);
                     79: 
                     80:   MD5Init(&context);
                     81:   MD5Update(&context, (uint8_t*)&chap_ident, 1);         
                     82:   /* password - argv 3 */
                     83:   /*fprintf(stderr,"password: %s\n",argv[3]);*/
                     84:   MD5Update(&context, (uint8_t*)argv[3], strlen(argv[3]));
                     85:   MD5Update(&context, challenge, MD5LEN);
                     86:   MD5Final(response, &context);
                     87: 
                     88:   chartohex(response, buffer);
                     89:   printf("%s\n", buffer);
                     90:   return 0;
                     91: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>