-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdHttpUtils.cc
400 lines (284 loc) · 7.27 KB
/
XrdHttpUtils.cc
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//------------------------------------------------------------------------------
// This file is part of XrdHTTP: A pragmatic implementation of the
// HTTP/WebDAV protocol for the Xrootd framework
//
// Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
// Author: Fabrizio Furano <furano@cern.ch>
// File Date: Apr 2013
//------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
/** @file XrdHttpUtils.cc
* @brief Utility functions for XrdHTTP
* @author Fabrizio Furano
* @date April 2013
*
*
*
*/
#include <string.h>
#include <openssl/hmac.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <pthread.h>
#include <memory>
#include <vector>
#include <algorithm>
#include "XProtocol/XPtypes.hh"
#include "XrdSec/XrdSecEntity.hh"
# include "sys/param.h"
#include "XrdOuc/XrdOucString.hh"
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static HMAC_CTX* HMAC_CTX_new() {
HMAC_CTX *ctx = (HMAC_CTX *)OPENSSL_malloc(sizeof(HMAC_CTX));
if (ctx) HMAC_CTX_init(ctx);
return ctx;
}
static void HMAC_CTX_free(HMAC_CTX *ctx) {
if (ctx) {
HMAC_CTX_cleanup(ctx);
OPENSSL_free(ctx);
}
}
#endif
// GetHost from URL
// Parse an URL and extract the host name and port
// Return 0 if OK
int parseURL(char *url, char *host, int &port, char **path) {
// http://x.y.z.w:p/path
*path = 0;
// look for the second slash
char *p = strstr(url, "//");
if (!p) return -1;
p += 2;
// look for the end of the host:port
char *p2 = strchr(p, '/');
if (!p2) return -1;
*path = p2;
char buf[256];
int l = min((int)(p2 - p), (int)sizeof (buf));
strncpy(buf, p, l);
buf[l] = '\0';
// Now look for :
p = strchr(buf, ':');
if (p) {
int l = min((int)(p - buf), (int)sizeof (buf));
strncpy(host, buf, l);
host[l] = '\0';
port = atoi(p + 1);
} else {
port = 0;
strcpy(host, buf);
}
return 0;
}
// Encode an array of bytes to base64
void Tobase64(const unsigned char *input, int length, char *out) {
BIO *bmem, *b64;
BUF_MEM *bptr;
if (!out) return;
out[0] = '\0';
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new(BIO_s_mem());
BIO_push(b64, bmem);
BIO_write(b64, input, length);
if (BIO_flush(b64) <= 0) {
BIO_free_all(b64);
return;
}
BIO_get_mem_ptr(b64, &bptr);
memcpy(out, bptr->data, bptr->length);
out[bptr->length] = '\0';
BIO_free_all(b64);
return;
}
static int
char_to_int(int c)
{
if (isdigit(c)) {
return c - '0';
} else {
c = tolower(c);
if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
return -1;
}
}
// Decode a hex digest array to raw bytes.
//
bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out) {
for (int idx=0; idx < length; idx += 2) {
int upper = char_to_int(input[idx]);
int lower = char_to_int(input[idx+1]);
if ((upper < 0) || (lower < 0)) {
return false;
}
out[idx/2] = (upper << 4) + lower;
}
return true;
}
// Simple itoa function
std::string itos(long i) {
char buf[128];
sprintf(buf, "%ld", i);
return buf;
}
// Home made implementation of strchrnul
char *mystrchrnul(const char *s, int c) {
char *ptr = strchr((char *)s, c);
if (!ptr)
return strchr((char *)s, '\0');
return ptr;
}
// Calculates the opaque arguments hash, needed for a secure redirection
//
// - hash is a string that will be filled with the hash
//
// - fn: the original filename that was requested
// - dhost: target redirection hostname
// - client: address:port of the client
// - tim: creation time of the url
// - tim_grace: validity time before and after creation time
//
// Input for the key (simple shared secret)
// - key
// - key length
//
void calcHashes(
char *hash,
const char *fn,
kXR_int16 request,
XrdSecEntity *secent,
time_t tim,
const char *key) {
HMAC_CTX *ctx;
unsigned int len;
unsigned char mdbuf[EVP_MAX_MD_SIZE];
char buf[64];
struct tm tms;
if (!hash) {
return;
}
hash[0] = '\0';
if (!key) {
return;
}
if (!fn || !secent) {
return;
}
ctx = HMAC_CTX_new();
if (!ctx) {
return;
}
HMAC_Init_ex(ctx, (const void *) key, strlen(key), EVP_sha256(), 0);
if (fn)
HMAC_Update(ctx, (const unsigned char *) fn,
strlen(fn) + 1);
HMAC_Update(ctx, (const unsigned char *) &request,
sizeof (request));
if (secent->name)
HMAC_Update(ctx, (const unsigned char *) secent->name,
strlen(secent->name) + 1);
if (secent->vorg)
HMAC_Update(ctx, (const unsigned char *) secent->vorg,
strlen(secent->vorg) + 1);
if (secent->host)
HMAC_Update(ctx, (const unsigned char *) secent->host,
strlen(secent->host) + 1);
if (secent->moninfo)
HMAC_Update(ctx, (const unsigned char *) secent->moninfo,
strlen(secent->moninfo) + 1);
localtime_r(&tim, &tms);
strftime(buf, sizeof (buf), "%s", &tms);
HMAC_Update(ctx, (const unsigned char *) buf,
strlen(buf) + 1);
HMAC_Final(ctx, mdbuf, &len);
Tobase64(mdbuf, len / 2, hash);
HMAC_CTX_free(ctx);
}
int compareHash(
const char *h1,
const char *h2) {
if (h1 == h2) return 0;
if (!h1 || !h2)
return 1;
return strcmp(h1, h2);
}
// unquote a string and return a new one
char *unquote(char *str) {
int l = strlen(str);
char *r = (char *) malloc(l + 1);
r[0] = '\0';
int i, j = 0;
for (i = 0; i < l; i++) {
if (str[i] == '%') {
char savec = str[i + 3];
str[i + 3] = '\0';
r[j] = strtol(str + i + 1, 0, 16);
str[i + 3] = savec;
i += 2;
} else r[j] = str[i];
j++;
}
r[j] = '\0';
return r;
}
// Quote a string and return a new one
char *quote(const char *str) {
int l = strlen(str);
char *r = (char *) malloc(l*3 + 1);
r[0] = '\0';
int i, j = 0;
for (i = 0; i < l; i++) {
char c = str[i];
switch (c) {
case ' ':
strcpy(r + j, "%20");
j += 3;
break;
case '[':
strcpy(r + j, "%5B");
j += 3;
break;
case ']':
strcpy(r + j, "%5D");
j += 3;
break;
case ':':
strcpy(r + j, "%3A");
j += 3;
break;
case '/':
strcpy(r + j, "%2F");
j += 3;
break;
case '\n':
strcpy(r + j, "%0A");
j += 3;
break;
case '\r':
strcpy(r + j, "%0D");
j += 3;
break;
default:
r[j++] = c;
}
}
r[j] = '\0';
return r;
}