Change applicationlevelsecurity to use wpsutil api functions for input creation....
[igd2-for-linux:fabrice-wanipconnection2.git] / linuxigd2 / src / applicationlevelsecurity.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include "lanhostconfig.h"
6 #include "globals.h"
7 #include "util.h"
8 #include <wpsutil/enrollee_state_machine.h>
9 #include <wpsutil/base64mem.h>
10
11 // TODO Should these be in main? Or somewhere else. 
12 WPSuEnrolleeSM* esm;
13 unsigned char* Enrollee_send_msg;
14 int Enrollee_send_msg_len;
15 WPSuStationInput input;
16
17 int reactor_loop = 1;
18
19 int InitALS()
20 {   
21     // TODO these values. What those should be and move to config file 
22     // TODO: Should start new thread for multiple simultanious registration processes?
23     int err;
24     
25     unsigned char MAC[SC_MAC_LEN];
26     memset(MAC, 0xAB, SC_MAC_LEN);
27
28     err = wpsu_enrollee_station_input_add_device_info(&input, 
29                                         "stasecret",
30                                         "TestManufacturer", 
31                                         "TestModelName",
32                                         "TestModelNumber", 
33                                         "TestSerialNumber", 
34                                         "TestDeviceName", 
35                                         NULL,
36                                         0,
37                                         MAC,
38                                         SC_MAC_LEN,
39                                         "TestUUID",
40                                         8,
41                                         NULL,
42                                         0,
43                                         NULL,
44                                         0,
45                                         SC_CONF_METHOD_LABEL | SC_CONF_METHOD_PUSHBUTTON, 
46                                         SC_RFBAND_2_4GHZ);
47                                         
48                                 
49                                         
50     /*
51     strcpy(input.OwnInfo.Manufacturer, "TestManufacturer");
52     strcpy(input.OwnInfo.ModelName, "TestModelName");
53     strcpy(input.OwnInfo.ModelNumber, "TestModelNumber");
54     strcpy(input.OwnInfo.SerialNumber, "TestSerialNumber");
55     strcpy(input.OwnInfo.DeviceName, "TestDeviceName");
56     strcpy(input.OwnInfo.Uuid, "TestUUID");
57     memset(input.OwnInfo.MacAddress, 0xAB, SC_MAC_LEN);
58     input.OwnInfo.ConfigMethods = SC_CONF_METHOD_LABEL | SC_CONF_METHOD_PUSHBUTTON;
59     input.OwnInfo.RFBands = SC_RFBAND_2_4GHZ;
60     strcpy(input.DevicePIN, "stasecret");
61 */
62     // station has applications A, B and C
63     input.Apps = 3;
64
65 unsigned char UUID[SC_MAX_UUID_LEN];
66 unsigned char *DATA;
67
68     memset(UUID, 0xAA, SC_MAX_UUID_LEN);
69     //input.AppsList[0].DataLen = 0;
70
71 err =  wpsu_enrollee_station_input_add_app(&input,
72     UUID,SC_MAX_UUID_LEN,
73     NULL,0,
74     NULL,0);
75
76     //input.AppsList[1].Data = (unsigned char*) malloc(1000);
77     //memset(input.AppsList[1].UUID, 0xBB, SC_MAX_UUID_LEN);
78     //strcpy(input.AppsList[1].Data, "B data from STA");
79     //input.AppsList[1].DataLen = strlen("B data from STA") + 1;
80     
81     memset(UUID, 0xBB, SC_MAX_UUID_LEN);
82
83 err =  wpsu_enrollee_station_input_add_app(&input,
84     UUID,SC_MAX_UUID_LEN,
85     "B data from STA",strlen("B data from STA") + 1,
86     NULL,0);
87
88     //input.AppsList[2].Data = (unsigned char*) malloc(1000);
89     //memset(input.AppsList[2].UUID, 0xCC, SC_MAX_UUID_LEN);
90     //strcpy(input.AppsList[2].Data, "C data from STA");
91     //input.AppsList[2].DataLen = strlen("C data from STA") + 1;
92
93     memset(UUID, 0xCC, SC_MAX_UUID_LEN);
94     
95
96 err =  wpsu_enrollee_station_input_add_app(&input,
97     UUID,SC_MAX_UUID_LEN,
98     "C data from STA",strlen("C data from STA") + 1,
99     NULL,0);
100
101     // create enrollee state machine
102
103     esm = wpsu_create_enrollee_sm_station(&input, &err);
104     printf ("wpsu_create_enrollee_sm_station %d\n",err);
105     
106     return 0;
107 }
108
109 void FreeALS()
110 {
111     int error;
112     free(input.AppsList[1].Data);
113     free(input.AppsList[2].Data);
114     wpsu_cleanup_enrollee_sm(esm, &error);
115 }
116
117 /**
118  * When message M2, M2D, M4, M6, M8 or Done ACK is received, enrollee state machine is updated here
119  */
120 int message_received(int error, unsigned char *data, int len, void* control)
121 {
122     int status;
123
124     if (error)
125     {
126         trace(2,"Message receive failure! Error = %d", error);
127         return error;
128     }
129
130     wpsu_update_enrollee_sm(esm, data, len, &Enrollee_send_msg, &Enrollee_send_msg_len, &status, &error);
131
132     switch (status)
133     {
134         case SC_E_SUCCESS:
135         {
136             trace(3,"Last message received!\n");
137             FreeALS();
138             reactor_loop = 0;
139             break;
140         }
141         case SC_E_SUCCESSINFO:
142         {
143             trace(3,"Last message received M2D!\n");
144             FreeALS();
145             reactor_loop = 0;
146             break;
147         }
148
149         case SC_E_FAILURE:
150         {
151             trace(3,"Error in state machine. Terminating...\n");
152             FreeALS();
153             reactor_loop = 0;
154             break;
155         }
156
157         case SC_E_FAILUREEXIT:
158         {
159             trace(3,"Error in state machine. Terminating...\n");
160             FreeALS();
161             reactor_loop = 0;
162             break;
163         }
164         default:
165         {
166
167         }
168     }
169     return 0;
170 }
171
172
173 /**
174  * Action: GetDeviceInfo.
175  *
176  * Return M1 message for sender of action.
177  */
178 int GetDeviceInfo(struct Upnp_Action_Request *ca_event)
179 {
180     char resultStr[RESULT_LEN];
181     int error;
182     
183     // temporarly this is here. This should probably be somewhere else?
184     InitALS();
185     // start the state machine and create M1
186     wpsu_start_enrollee_sm(esm, &Enrollee_send_msg, &Enrollee_send_msg_len, &error);
187       
188     // to base64   
189     int maxb64len=2*Enrollee_send_msg_len; 
190     int b64len=0;    
191     unsigned char *pB64Msg=(unsigned char *)malloc(maxb64len); 
192
193     wpsu_bin_to_base64(Enrollee_send_msg_len,Enrollee_send_msg, &b64len, pB64Msg,maxb64len);
194     // return M1 as base64 encoded
195     trace(3,"Send M1 as response for GetDeviceInfo request\n");
196     //printf("M1 in base64: %s\n",pB64Msg);
197     
198     ca_event->ErrCode = UPNP_E_SUCCESS;
199     snprintf(resultStr, RESULT_LEN, "<u:%sResponse xmlns:u=\"%s\">\n<NewDeviceInfo>%s</NewDeviceInfo>\n</u:%sResponse>",
200              ca_event->ActionName, "urn:schemas-upnp-org:service:WFAWLANConfig:1", pB64Msg, ca_event->ActionName);
201     ca_event->ActionResult = ixmlParseBuffer(resultStr);    
202
203     free(pB64Msg);
204     return ca_event->ErrCode;
205 }
206
207 /**
208  * Action: PutMessage.
209  *
210  * After sending M1 as response for GetDeviceInfo-action, messages M3, M5, M7 and Done
211  * are send as response for this action.
212  */
213 int PutMessage(struct Upnp_Action_Request *ca_event)
214 {
215     char resultStr[RESULT_LEN];   
216     char *message = NULL;
217     
218     message = GetFirstDocumentItem(ca_event->ActionRequest, "NewInMessage");
219
220     // to bin
221     int b64msglen=strlen(message);
222     unsigned char *pBinMsg=(unsigned char *)malloc(b64msglen);
223     int outlen;
224     
225     wpsu_base64_to_bin(b64msglen,(const unsigned char *)message,&outlen,pBinMsg,b64msglen);
226     
227     //printf("Message in bin: %s\n",pBinMsg);
228     // update state machine
229     message_received(0, pBinMsg, outlen, NULL); 
230
231     // response (next message) to base64   
232     int maxb64len=2*Enrollee_send_msg_len; 
233     int b64len=0;    
234     unsigned char *pB64Msg=(unsigned char *)malloc(maxb64len); 
235     wpsu_bin_to_base64(Enrollee_send_msg_len,Enrollee_send_msg, &b64len, pB64Msg,maxb64len);
236     
237     trace(3,"Send response for PutMessage request\n");
238     
239     ca_event->ErrCode = UPNP_E_SUCCESS;
240     snprintf(resultStr, RESULT_LEN, "<u:%sResponse xmlns:u=\"%s\">\n<NewOutMessage>%s</NewOutMessage>\n</u:%sResponse>",
241              ca_event->ActionName, "urn:schemas-upnp-org:service:WFAWLANConfig:1", pB64Msg, ca_event->ActionName);
242     ca_event->ActionResult = ixmlParseBuffer(resultStr);       
243     
244     free(pBinMsg);
245     return ca_event->ErrCode;
246 }
247
248 /**
249  * Action: RequestCert.
250  *
251  */
252 int RequestCert(struct Upnp_Action_Request *ca_event)
253 {
254     return ca_event->ErrCode;
255 }
256
257 /**
258  * Action: GetRoles.
259  *
260  */
261 int GetRoles(struct Upnp_Action_Request *ca_event)
262 {
263     return ca_event->ErrCode;
264 }
265
266 /**
267  * Action: GetCACert.
268  *
269  */
270 int GetCACert(struct Upnp_Action_Request *ca_event)
271 {
272     return ca_event->ErrCode;
273 }
274
275 /**
276  * Action: GetKnownCAs.
277  *
278  */
279 int GetKnownCAs(struct Upnp_Action_Request *ca_event)
280 {
281     return ca_event->ErrCode;
282 }
283
284 // Admin interface
285
286 /**
287  * Action: AddACLEntry.
288  *
289  */
290 int AddACLEntry(struct Upnp_Action_Request *ca_event)
291 {
292     return ca_event->ErrCode;
293 }
294
295 /**
296  * Action: AddCACertHash.
297  *
298  */
299 int AddCACertHash(struct Upnp_Action_Request *ca_event)
300 {
301     return ca_event->ErrCode;
302 }