-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathresponse.c
376 lines (324 loc) · 9.53 KB
/
response.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* frontier client response handler
*
* Author: Sergey Kosyakov
*
* $Id$
*
* Copyright (c) 2009, FERMI NATIONAL ACCELERATOR LABORATORY
* All rights reserved.
*
* For details of the Fermitools (BSD) license see Fermilab-2009.txt or
* http://fermitools.fnal.gov/about/terms.html
*
*/
#include <stdio.h>
#include <string.h>
#include <frontier_client/frontier.h>
#include "fn-internal.h"
#include "expat.h"
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
static void XMLCALL
xml_cdata(void *userData,const XML_Char *s,int len)
{
FrontierResponse *fr=(FrontierResponse*)userData;
frontierPayload_append(fr->payload[fr->payload_num-1],s,len);
//printf("xml_cdata\n");
}
static void XMLCALL
xml_startElement(void *userData,const char *name,const char **atts)
{
int i;
FrontierResponse *fr=(FrontierResponse*)userData;
//printf("xml_start %s\n",name);
if(strcmp(name,"keepalive")==0)
fr->keepalives++;
else if(fr->keepalives!=0)
{
frontier_log(FRONTIER_LOGLEVEL_DEBUG,__FILE__,__LINE__,"received %d keepalives",fr->keepalives);
fr->keepalives=0;
}
if(strcmp(name,"global_error")==0)
{
fr->error=FRONTIER_EPROTO;
frontier_setErrorMsg(__FILE__,__LINE__,"Server has signalled Global Error [%s]",atts[1]);
return;
}
if(strcmp(name,"frontier")==0)
{
char buf[80];
int i;
size_t l=0;
buf[0] = '\0';
for(i=0;atts[i];i+=2)
{
l+=snprintf(&buf[l],sizeof(buf)-l," %s=%s",atts[i],atts[i+1]);
if (l>=sizeof(buf))
break;
}
frontier_log(FRONTIER_LOGLEVEL_DEBUG,__FILE__,__LINE__,"frontier server%s",buf);
return;
}
if(strcmp(name,"payload")==0)
{
const char *encoding=NULL;
fr->payload_num++;
for(i=0;atts[i];i+=2)
{
if(strcmp(atts[i],"encoding")==0)
{
encoding=atts[i+1];
continue;
}
}
fr->payload[fr->payload_num-1]=frontierPayload_create(encoding,(fr->srv_rsakey!=0),fr->params1,fr->params2);
fr->p_state=FNTR_WITHIN_PAYLOAD;
return;
}
if(strcmp(name,"data")==0)
{
XML_SetCharacterDataHandler(fr->parser,xml_cdata);
return;
}
if(strcmp(name,"quality")==0 && fr->p_state==FNTR_WITHIN_PAYLOAD)
{
for(i=0;atts[i];i+=2)
{
//printf("attr <%s><%s>\n",atts[i],atts[i+1]);
fflush(stdout);
if(strcmp(atts[i],"error")==0)
{
fr->payload[fr->payload_num-1]->error_code=atoi(atts[i+1]);
continue;
}
if(strcmp(atts[i],"message")==0)
{
fr->payload[fr->payload_num-1]->error_msg=frontier_str_copy(atts[i+1]);
continue;
}
if(strcmp(atts[i],"max_age")==0)
{
int max_age=atoi(atts[i+1]);
if((fr->max_age<=0)||(max_age<fr->max_age))
fr->max_age=max_age;
continue;
}
if(strcmp(atts[i],"records")==0)
{
//printf("Number of records: %d\n", atoi(atts[i+1]));
fr->payload[fr->payload_num-1]->nrec=atoi(atts[i+1]);
continue;
}
if(strcmp(atts[i],"full_size")==0)
{
fr->payload[fr->payload_num-1]->full_size=atoi(atts[i+1]);
continue;
}
if(strcmp(atts[i],"md5")==0)
{
bcopy(atts[i+1],fr->payload[fr->payload_num-1]->srv_md5_str,MD5_DIGEST_LENGTH*2);
fr->payload[fr->payload_num-1]->srv_md5_str[MD5_DIGEST_LENGTH*2]=0;
continue;
}
if(strcmp(atts[i],"sig")==0)
{
int len=strlen(atts[i+1]);
unsigned char *srv_sig=frontier_mem_alloc(len*3/4);
if(srv_sig)
{
fr->payload[fr->payload_num-1]->srv_sig_len=
fn_base64_ascii2bin((unsigned char *)atts[i+1],len,srv_sig,len*3/4);
fr->payload[fr->payload_num-1]->srv_sig=srv_sig;
}
}
}
}
}
static void XMLCALL
xml_endElement(void *userData,const char *name)
{
int ret;
FrontierResponse *fr=(FrontierResponse*)userData;
//printf("xml_end %s\n",name);
if(strcmp(name,"data")==0)
{
XML_SetCharacterDataHandler(fr->parser,(void*)0);
return;
}
if(strcmp(name,"payload")==0)
{
ret=frontierPayload_finalize(fr->payload[fr->payload_num-1]);
fr->p_state=0;
fr->error=ret;
}
}
FrontierResponse *frontierResponse_create(int *ec,void *srv_rsakey,const char *params1,const char *params2)
{
FrontierResponse *fr;
int i;
fr=frontier_mem_alloc(sizeof(FrontierResponse));
if(!fr)
{
*ec=FRONTIER_EMEM;
FRONTIER_MSG(*ec);
return fr;
}
fr->error=0;
fr->payload_num=0;
fr->error_payload_ind=-1;
fr->keepalives=0;
fr->max_age=-1;
fr->srv_rsakey=srv_rsakey;
fr->params1=params1;
fr->params2=params2;
fr->parser=XML_ParserCreate(NULL);
if(!fr->parser)
{
frontier_mem_free(fr);
*ec=FRONTIER_EUNKNOWN;
frontier_setErrorMsg(__FILE__,__LINE__,"Can not create XML parser instance.");
return (void*)0;
}
XML_SetUserData(fr->parser,fr);
XML_SetElementHandler(fr->parser,xml_startElement,xml_endElement);
fr->p_state=0;
for(i=0;i<FRONTIER_MAX_PAYLOADNUM;i++)
{
fr->payload[i]=(void*)0;
}
return fr;
}
void frontierResponse_delete(FrontierResponse *fr)
{
int i;
if(!fr) return;
if(fr->parser)
{
XML_SetUserData(fr->parser,(void*)0);
XML_ParserFree(fr->parser);
}
for(i=0;i<fr->payload_num;i++)
{
frontierPayload_delete(fr->payload[i]);
}
frontier_mem_free(fr);
}
int FrontierResponse_append(FrontierResponse *fr,char *buf,int len)
{
if(XML_Parse(fr->parser,buf,len,0)==XML_STATUS_ERROR)
{
int xml_err=XML_GetErrorCode(fr->parser);
frontier_setErrorMsg(__FILE__,__LINE__,"XML parse error %d:%s at line %d",xml_err,XML_ErrorString(xml_err),XML_GetCurrentLineNumber(fr->parser));
return FRONTIER_EPROTO;
}
if(fr->error)
{
return fr->error;
}
return FRONTIER_OK;
}
int frontierResponse_finalize(FrontierResponse *fr)
{
int i;
unsigned char *digest;
if(XML_Parse(fr->parser,"",0,1)==XML_STATUS_ERROR)
{
int xml_err=XML_GetErrorCode(fr->parser);
frontier_setErrorMsg(__FILE__,__LINE__,"XML parse error %d:%s at line %d",xml_err,XML_ErrorString(xml_err),XML_GetCurrentLineNumber(fr->parser));
return FRONTIER_EPROTO;
}
XML_SetUserData(fr->parser,(void*)0);
XML_ParserFree(fr->parser);
fr->parser=(void*)0;
for(i=0;i<fr->payload_num;i++)
{
//printf("%d r:[%s] l:[%s]\n",i,fr->payload[i]->srv_md5_str);
frontier_log(FRONTIER_LOGLEVEL_DEBUG,__FILE__,__LINE__,"Payload[%d] error %d error code %d",(i+1),fr->payload[i]->error,fr->payload[i]->error_code);
if(fr->payload[i]->error)
{
frontier_setErrorMsg(__FILE__,__LINE__,"Payload[%d] got error %d",(i+1),fr->payload[i]->error);
return fr->payload[i]->error;
}
if(fr->payload[i]->error_code)
{
frontier_setErrorMsg(__FILE__,__LINE__,"Payload[%d] has error code %d",(i+1),fr->payload[i]->error_code);
return FRONTIER_EPROTO;
}
digest=fr->payload[i]->digest;
if(fr->srv_rsakey!=0)
{
unsigned char *sha256_buf=0;
int ret=FRONTIER_OK;
int siglen=fr->payload[i]->srv_sig_len;
int declen;
if(siglen==0)
{
frontier_setErrorMsg(__FILE__,__LINE__,"Payload[%d]: no signature found",i+1);
return FRONTIER_EPROTO;
}
// If rsa decrypt fails, the output can be as big as the input.
// If it succeeds, the length should be SHA256_DIGEST_LENGTH.
// So malloc a buffer of the bigger size just in case
sha256_buf=frontier_mem_alloc(siglen);
if(!sha256_buf) {ret=FRONTIER_EMEM;FRONTIER_MSG(ret);return ret;};
declen=RSA_public_decrypt(siglen,fr->payload[i]->srv_sig,sha256_buf,
(RSA *)fr->srv_rsakey,RSA_PKCS1_PADDING);
if(declen==-1)
{
SSL_load_error_strings();
frontier_setErrorMsg(__FILE__,__LINE__,"Payload[%d]: error decrypting signature: %s",i+1,ERR_error_string(ERR_get_error(),0));
ERR_free_strings();
ret=FRONTIER_EPROTO;
goto endsigned;
}
if(declen!=SHA256_DIGEST_LENGTH)
{
frontier_setErrorMsg(__FILE__,__LINE__,"Payload[%d]: decrypted signature wrong length; expect %d, got %d",i+1,SHA256_DIGEST_LENGTH,declen);
ret=FRONTIER_EPROTO;
goto endsigned;
}
if(memcmp(digest,sha256_buf,declen)==0)
frontier_log(FRONTIER_LOGLEVEL_DEBUG,__FILE__,__LINE__,"Payload[%d] signature passed",i+1);
else
{
char digest_str[SHA256_DIGEST_LENGTH*2+1];
char sha256_str[SHA256_DIGEST_LENGTH*2+1];
for(i=0;i<SHA256_DIGEST_LENGTH;i++)
{
snprintf(&digest_str[i*2],3,"%02x",digest[i]);
snprintf(&sha256_str[i*2],3,"%02x",sha256_buf[i]);
}
if(strcmp(digest_str,sha256_str)!=0)
{
frontier_setErrorMsg(__FILE__,__LINE__,"Payload[%d]: signature hash mismatch: server [%s], local [%s]",(i+1),sha256_str,digest_str);
ret=FRONTIER_EPROTO;
goto endsigned;
}
}
endsigned:
if(sha256_buf)frontier_mem_free(sha256_buf);
if(ret!=FRONTIER_OK)return ret;
}
else
{
char md5_str[sizeof(fr->payload[0]->srv_md5_str)];
char *srv_md5_str;
// this variable is set here to avoid a compiler bug; moving this to
// just before the strncmp below (or eliminating the variable) loses
// the upper bits & causes a seg fault, at least on gcc 4.1.2
srv_md5_str=fr->payload[i]->srv_md5_str;
bzero(md5_str,sizeof(md5_str));
// convert the binary md5 characters into printable
for(i=0;i<MD5_DIGEST_LENGTH;i++)
snprintf(&md5_str[i*2],3,"%02x",digest[i]);
if(strcmp(srv_md5_str,md5_str)!=0)
{
frontier_setErrorMsg(__FILE__,__LINE__,"Payload[%d]: MD5 hash mismatch: server [%s], local [%s]",(i+1),srv_md5_str,md5_str);
return FRONTIER_EPROTO;
}
}
}
return FRONTIER_OK;
}