add new header ident.h
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_psk / eap_psk.cpp
1 /* $Id$ */
2
3 /*
4  * eap_psk.cpp
5  *
6  * Implementation of the EAP-PSK packet management
7  *
8  * 
9  * Copyright (C) France Télécom R&D (DR&D/MAPS/NSS)
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  *
25  */
26
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "eap_psk.h"
32
33
34 /*
35  *
36  *  PSK Packet Format in EAP 
37  *  --- ------ ------ -- ---
38  * 0                   1                   2                   3
39  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
40  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  * |     Code      |   Identifier  |            Length             |
42  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43  * |     Type      |   Data
44  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45  *
46  */
47
48
49 int pskConvertHex(char *inbytes, char *outstr, int numbytes)
50 {
51         int i;
52         char buildstr[1024], tempstr[10];
53
54         memset(buildstr, 0, 1024);
55
56         for (i=0;i<numbytes;i++)
57         {
58                 sprintf((char *)tempstr, "%02X",(unsigned char)inbytes[i]);
59                 strcat((char *)buildstr, (char *)tempstr);
60         }
61         strcpy(outstr, (char *)buildstr);
62
63         return 1;
64 }
65
66
67 int pskHex2Bin(const char *hex, unsigned char *bin, int numbytes) {
68     int len = strlen(hex);
69     char c;
70     int i;
71     unsigned char v;
72     for (i = 0; i < numbytes; i++) {
73         c = hex[2*i];
74         if (c >= '0' && c <= '9') {
75             v = c - '0';
76         } else if (c >= 'A' && c <= 'F') {
77           v = c - 'A' + 10;
78         } else if (c >= 'a' && c <= 'f') {
79           v = c - 'a' + 10;
80         } else {
81           //v = 0;
82           return 0; // non hexa character
83         }
84         v <<= 4;
85         c = hex[2*i + 1];
86         if (c >= '0' && c <= '9') {
87           v += c - '0';
88         } else if (c >= 'A' && c <= 'F') {
89           v += c - 'A' + 10;
90         } else if (c >= 'a' && c <= 'f') {
91           v += c - 'a' + 10;
92         } else {
93           //v = 0;
94           return 0; // non hexa character
95         }
96         bin[i] = v;
97     }
98     return 1;
99 }
100
101
102 int pskGetRandomBytes(void *buf, int nbytes){
103   FILE *fptr=NULL;
104   int written=0;
105   
106   if((fptr = fopen("/dev/urandom","r")) == NULL) {
107     radlog(L_ERR,"pskGetRandomBytes: urandom device not accessible");
108     return 0;
109   }
110   
111   if((written = fread(buf,1,nbytes,fptr)) != nbytes) {
112     radlog(L_ERR,"pskGetRandomBytes: number not generated");
113     return 0;
114   }      
115   
116   fclose(fptr);
117   
118   return 1;
119 }