More for input buffer checks
[rtmpdump.git] / librtmp / hashswf.c
blob9f4e2c02bbe755b6afbbd8f8440ee42e6a3d5556
1 /*
2 * Copyright (C) 2009-2010 Howard Chu
4 * This file is part of librtmp.
6 * librtmp is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1,
9 * or (at your option) any later version.
11 * librtmp is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with librtmp see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/lgpl.html
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <time.h>
29 #include "rtmp_sys.h"
30 #include "log.h"
31 #include "http.h"
33 #ifdef CRYPTO
34 #ifdef USE_POLARSSL
35 #include <polarssl/sha2.h>
36 #ifndef SHA256_DIGEST_LENGTH
37 #define SHA256_DIGEST_LENGTH 32
38 #endif
39 #define HMAC_CTX sha2_context
40 #define HMAC_setup(ctx, key, len) sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0)
41 #define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len)
42 #define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; sha2_hmac_finish(&ctx, dig)
43 #define HMAC_close(ctx)
44 #elif defined(USE_GNUTLS)
45 #include <nettle/hmac.h>
46 #ifndef SHA256_DIGEST_LENGTH
47 #define SHA256_DIGEST_LENGTH 32
48 #endif
49 #undef HMAC_CTX
50 #define HMAC_CTX struct hmac_sha256_ctx
51 #define HMAC_setup(ctx, key, len) hmac_sha256_set_key(&ctx, len, key)
52 #define HMAC_crunch(ctx, buf, len) hmac_sha256_update(&ctx, len, buf)
53 #define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; hmac_sha256_digest(&ctx, SHA256_DIGEST_LENGTH, dig)
54 #define HMAC_close(ctx)
55 #else /* USE_OPENSSL */
56 #include <openssl/ssl.h>
57 #include <openssl/sha.h>
58 #include <openssl/hmac.h>
59 #include <openssl/rc4.h>
60 #define HMAC_setup(ctx, key, len) HMAC_CTX_init(&ctx); HMAC_Init_ex(&ctx, (unsigned char *)key, len, EVP_sha256(), 0)
61 #define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, (unsigned char *)buf, len)
62 #define HMAC_finish(ctx, dig, dlen) HMAC_Final(&ctx, (unsigned char *)dig, &dlen);
63 #define HMAC_close(ctx) HMAC_CTX_cleanup(&ctx)
64 #endif
66 extern void RTMP_TLS_Init();
67 extern TLS_CTX RTMP_TLS_ctx;
69 #include <zlib.h>
71 #endif /* CRYPTO */
73 #define AGENT "Mozilla/5.0"
75 HTTPResult
76 HTTP_get(struct HTTP_ctx *http, const char *url, HTTP_read_callback *cb)
78 char *host, *path;
79 char *p1, *p2;
80 char hbuf[256];
81 int port = 80;
82 #ifdef CRYPTO
83 int ssl = 0;
84 #endif
85 int hlen, flen = 0;
86 int rc, i;
87 int len_known;
88 HTTPResult ret = HTTPRES_OK;
89 struct sockaddr_in sa;
90 RTMPSockBuf sb = {0};
92 http->status = -1;
94 memset(&sa, 0, sizeof(struct sockaddr_in));
95 sa.sin_family = AF_INET;
97 /* we only handle http here */
98 if (strncasecmp(url, "http", 4))
99 return HTTPRES_BAD_REQUEST;
101 if (url[4] == 's')
103 #ifdef CRYPTO
104 ssl = 1;
105 port = 443;
106 if (!RTMP_TLS_ctx)
107 RTMP_TLS_Init();
108 #else
109 return HTTPRES_BAD_REQUEST;
110 #endif
113 p1 = strchr(url + 4, ':');
114 if (!p1 || strncmp(p1, "://", 3))
115 return HTTPRES_BAD_REQUEST;
117 host = p1 + 3;
118 path = strchr(host, '/');
119 hlen = path - host;
120 strncpy(hbuf, host, hlen);
121 hbuf[hlen] = '\0';
122 host = hbuf;
123 p1 = strrchr(host, ':');
124 if (p1)
126 *p1++ = '\0';
127 port = atoi(p1);
130 sa.sin_addr.s_addr = inet_addr(host);
131 if (sa.sin_addr.s_addr == INADDR_NONE)
133 struct hostent *hp = gethostbyname(host);
134 if (!hp || !hp->h_addr)
135 return HTTPRES_LOST_CONNECTION;
136 sa.sin_addr = *(struct in_addr *)hp->h_addr;
138 sa.sin_port = htons(port);
139 sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
140 if (sb.sb_socket == -1)
141 return HTTPRES_LOST_CONNECTION;
143 sprintf(sb.sb_buf,
144 "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\nReferer: %.*s\r\n",
145 path, AGENT, host, (int)(path - url + 1), url);
146 if (http->date[0])
147 i += sprintf(sb.sb_buf + i, "If-Modified-Since: %s\r\n", http->date);
148 i += sprintf(sb.sb_buf + i, "\r\n");
150 if (connect
151 (sb.sb_socket, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0)
153 ret = HTTPRES_LOST_CONNECTION;
154 goto leave;
156 #ifdef CRYPTO
157 if (ssl)
159 #ifdef NO_SSL
160 RTMP_Log(RTMP_LOGERROR, "%s, No SSL/TLS support", __FUNCTION__);
161 ret = HTTPRES_BAD_REQUEST;
162 goto leave;
163 #else
164 TLS_client(RTMP_TLS_ctx, sb.sb_ssl);
165 TLS_setfd(sb.sb_ssl, sb.sb_socket);
166 if (TLS_connect(sb.sb_ssl) < 0)
168 RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__);
169 ret = HTTPRES_LOST_CONNECTION;
170 goto leave;
172 #endif
174 #endif
175 RTMPSockBuf_Send(&sb, sb.sb_buf, i);
177 /* set timeout */
178 #define HTTP_TIMEOUT 5
180 SET_RCVTIMEO(tv, HTTP_TIMEOUT);
181 if (setsockopt
182 (sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)))
184 RTMP_Log(RTMP_LOGERROR, "%s, Setting socket timeout to %ds failed!",
185 __FUNCTION__, HTTP_TIMEOUT);
189 sb.sb_size = 0;
190 sb.sb_timedout = FALSE;
191 if (RTMPSockBuf_Fill(&sb) < 1)
193 ret = HTTPRES_LOST_CONNECTION;
194 goto leave;
196 if (strncmp(sb.sb_buf, "HTTP/1", 6))
198 ret = HTTPRES_BAD_REQUEST;
199 goto leave;
202 p1 = strchr(sb.sb_buf, ' ');
203 rc = atoi(p1 + 1);
204 http->status = rc;
206 if (rc >= 300)
208 if (rc == 304)
210 ret = HTTPRES_OK_NOT_MODIFIED;
211 goto leave;
213 else if (rc == 404)
214 ret = HTTPRES_NOT_FOUND;
215 else if (rc >= 500)
216 ret = HTTPRES_SERVER_ERROR;
217 else if (rc >= 400)
218 ret = HTTPRES_BAD_REQUEST;
219 else
220 ret = HTTPRES_REDIRECTED;
223 p1 = memchr(sb.sb_buf, '\n', sb.sb_size);
224 if (!p1)
226 ret = HTTPRES_BAD_REQUEST;
227 goto leave;
229 sb.sb_start = p1 + 1;
230 sb.sb_size -= sb.sb_start - sb.sb_buf;
232 while ((p2 = memchr(sb.sb_start, '\r', sb.sb_size)))
234 if (*sb.sb_start == '\r')
236 sb.sb_start += 2;
237 sb.sb_size -= 2;
238 break;
240 else
241 if (!strncasecmp
242 (sb.sb_start, "Content-Length: ", sizeof("Content-Length: ") - 1))
244 flen = atoi(sb.sb_start + sizeof("Content-Length: ") - 1);
246 else
247 if (!strncasecmp
248 (sb.sb_start, "Last-Modified: ", sizeof("Last-Modified: ") - 1))
250 *p2 = '\0';
251 strcpy(http->date, sb.sb_start + sizeof("Last-Modified: ") - 1);
253 p2 += 2;
254 sb.sb_size -= p2 - sb.sb_start;
255 sb.sb_start = p2;
256 if (sb.sb_size < 1)
258 if (RTMPSockBuf_Fill(&sb) < 1)
260 ret = HTTPRES_LOST_CONNECTION;
261 goto leave;
266 len_known = flen > 0;
267 while ((!len_known || flen > 0) &&
268 (sb.sb_size > 0 || RTMPSockBuf_Fill(&sb) > 0))
270 cb(sb.sb_start, 1, sb.sb_size, http->data);
271 if (len_known)
272 flen -= sb.sb_size;
273 http->size += sb.sb_size;
274 sb.sb_size = 0;
277 if (flen > 0)
278 ret = HTTPRES_LOST_CONNECTION;
280 leave:
281 RTMPSockBuf_Close(&sb);
282 return ret;
285 #ifdef CRYPTO
287 #define CHUNK 16384
289 struct info
291 z_stream *zs;
292 HMAC_CTX ctx;
293 int first;
294 int zlib;
295 int size;
298 static size_t
299 swfcrunch(void *ptr, size_t size, size_t nmemb, void *stream)
301 struct info *i = stream;
302 char *p = ptr;
303 size_t len = size * nmemb;
305 if (i->first)
307 i->first = 0;
308 /* compressed? */
309 if (!strncmp(p, "CWS", 3))
311 *p = 'F';
312 i->zlib = 1;
314 HMAC_crunch(i->ctx, (unsigned char *)p, 8);
315 p += 8;
316 len -= 8;
317 i->size = 8;
320 if (i->zlib)
322 unsigned char out[CHUNK];
323 i->zs->next_in = (unsigned char *)p;
324 i->zs->avail_in = len;
327 i->zs->avail_out = CHUNK;
328 i->zs->next_out = out;
329 inflate(i->zs, Z_NO_FLUSH);
330 len = CHUNK - i->zs->avail_out;
331 i->size += len;
332 HMAC_crunch(i->ctx, out, len);
334 while (i->zs->avail_out == 0);
336 else
338 i->size += len;
339 HMAC_crunch(i->ctx, (unsigned char *)p, len);
341 return size * nmemb;
344 static int tzoff;
345 static int tzchecked;
347 #define JAN02_1980 318340800
349 static const char *monthtab[12] = { "Jan", "Feb", "Mar",
350 "Apr", "May", "Jun",
351 "Jul", "Aug", "Sep",
352 "Oct", "Nov", "Dec"
354 static const char *days[] =
355 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
357 /* Parse an HTTP datestamp into Unix time */
358 static time_t
359 make_unix_time(char *s)
361 struct tm time;
362 int i, ysub = 1900, fmt = 0;
363 char *month;
364 char *n;
365 time_t res;
367 if (s[3] != ' ')
369 fmt = 1;
370 if (s[3] != ',')
371 ysub = 0;
373 for (n = s; *n; ++n)
374 if (*n == '-' || *n == ':')
375 *n = ' ';
377 time.tm_mon = 0;
378 n = strchr(s, ' ');
379 if (fmt)
381 /* Day, DD-MMM-YYYY HH:MM:SS GMT */
382 time.tm_mday = strtol(n + 1, &n, 0);
383 month = n + 1;
384 n = strchr(month, ' ');
385 time.tm_year = strtol(n + 1, &n, 0);
386 time.tm_hour = strtol(n + 1, &n, 0);
387 time.tm_min = strtol(n + 1, &n, 0);
388 time.tm_sec = strtol(n + 1, NULL, 0);
390 else
392 /* Unix ctime() format. Does not conform to HTTP spec. */
393 /* Day MMM DD HH:MM:SS YYYY */
394 month = n + 1;
395 n = strchr(month, ' ');
396 while (isspace(*n))
397 n++;
398 time.tm_mday = strtol(n, &n, 0);
399 time.tm_hour = strtol(n + 1, &n, 0);
400 time.tm_min = strtol(n + 1, &n, 0);
401 time.tm_sec = strtol(n + 1, &n, 0);
402 time.tm_year = strtol(n + 1, NULL, 0);
404 if (time.tm_year > 100)
405 time.tm_year -= ysub;
407 for (i = 0; i < 12; i++)
408 if (!strncasecmp(month, monthtab[i], 3))
410 time.tm_mon = i;
411 break;
413 time.tm_isdst = 0; /* daylight saving is never in effect in GMT */
415 /* this is normally the value of extern int timezone, but some
416 * braindead C libraries don't provide it.
418 if (!tzchecked)
420 struct tm *tc;
421 time_t then = JAN02_1980;
422 tc = localtime(&then);
423 tzoff = (12 - tc->tm_hour) * 3600 + tc->tm_min * 60 + tc->tm_sec;
424 tzchecked = 1;
426 res = mktime(&time);
427 /* Unfortunately, mktime() assumes the input is in local time,
428 * not GMT, so we have to correct it here.
430 if (res != -1)
431 res += tzoff;
432 return res;
435 /* Convert a Unix time to a network time string
436 * Weekday, DD-MMM-YYYY HH:MM:SS GMT
438 static void
439 strtime(time_t * t, char *s)
441 struct tm *tm;
443 tm = gmtime((time_t *) t);
444 sprintf(s, "%s, %02d %s %d %02d:%02d:%02d GMT",
445 days[tm->tm_wday], tm->tm_mday, monthtab[tm->tm_mon],
446 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
449 #define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf))
452 RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash,
453 int age)
455 FILE *f = NULL;
456 char *path, date[64], cctim[64];
457 long pos = 0;
458 time_t ctim = -1, cnow;
459 int i, got = 0, ret = 0;
460 unsigned int hlen;
461 struct info in = { 0 };
462 struct HTTP_ctx http = { 0 };
463 HTTPResult httpres;
464 z_stream zs = { 0 };
465 AVal home, hpre;
467 date[0] = '\0';
468 #ifdef _WIN32
469 #ifdef XBMC4XBOX
470 hpre.av_val = "Q:";
471 hpre.av_len = 2;
472 home.av_val = "\\UserData";
473 #else
474 hpre.av_val = getenv("HOMEDRIVE");
475 hpre.av_len = strlen(hpre.av_val);
476 home.av_val = getenv("HOMEPATH");
477 #endif
478 #define DIRSEP "\\"
480 #else /* !_WIN32 */
481 hpre.av_val = "";
482 hpre.av_len = 0;
483 home.av_val = getenv("HOME");
484 #define DIRSEP "/"
485 #endif
486 if (!home.av_val)
487 home.av_val = ".";
488 home.av_len = strlen(home.av_val);
490 /* SWF hash info is cached in a fixed-format file.
491 * url: <url of SWF file>
492 * ctim: HTTP datestamp of when we last checked it.
493 * date: HTTP datestamp of the SWF's last modification.
494 * size: SWF size in hex
495 * hash: SWF hash in hex
497 * These fields must be present in this order. All fields
498 * besides URL are fixed size.
500 path = malloc(hpre.av_len + home.av_len + sizeof(DIRSEP ".swfinfo"));
501 sprintf(path, "%s%s" DIRSEP ".swfinfo", hpre.av_val, home.av_val);
503 f = fopen(path, "r+");
504 while (f)
506 char buf[4096], *file, *p;
508 file = strchr(url, '/');
509 if (!file)
510 break;
511 file += 2;
512 file = strchr(file, '/');
513 if (!file)
514 break;
515 file++;
516 hlen = file - url;
517 p = strrchr(file, '/');
518 if (p)
519 file = p;
520 else
521 file--;
523 while (fgets(buf, sizeof(buf), f))
525 char *r1;
527 got = 0;
529 if (strncmp(buf, "url: ", 5))
530 continue;
531 if (strncmp(buf + 5, url, hlen))
532 continue;
533 r1 = strrchr(buf, '/');
534 i = strlen(r1);
535 r1[--i] = '\0';
536 if (strncmp(r1, file, i))
537 continue;
538 pos = ftell(f);
539 while (got < 4 && fgets(buf, sizeof(buf), f))
541 if (!strncmp(buf, "size: ", 6))
543 *size = strtol(buf + 6, NULL, 16);
544 got++;
546 else if (!strncmp(buf, "hash: ", 6))
548 unsigned char *ptr = hash, *in = (unsigned char *)buf + 6;
549 int l = strlen((char *)in) - 1;
550 for (i = 0; i < l; i += 2)
551 *ptr++ = (HEX2BIN(in[i]) << 4) | HEX2BIN(in[i + 1]);
552 got++;
554 else if (!strncmp(buf, "date: ", 6))
556 buf[strlen(buf) - 1] = '\0';
557 strncpy(date, buf + 6, sizeof(date));
558 got++;
560 else if (!strncmp(buf, "ctim: ", 6))
562 buf[strlen(buf) - 1] = '\0';
563 ctim = make_unix_time(buf + 6);
564 got++;
566 else if (!strncmp(buf, "url: ", 5))
567 break;
569 break;
571 break;
574 cnow = time(NULL);
575 /* If we got a cache time, see if it's young enough to use directly */
576 if (age && ctim > 0)
578 ctim = cnow - ctim;
579 ctim /= 3600 * 24; /* seconds to days */
580 if (ctim < age) /* ok, it's new enough */
581 goto out;
584 in.first = 1;
585 HMAC_setup(in.ctx, "Genuine Adobe Flash Player 001", 30);
586 inflateInit(&zs);
587 in.zs = &zs;
589 http.date = date;
590 http.data = &in;
592 httpres = HTTP_get(&http, url, swfcrunch);
594 inflateEnd(&zs);
596 if (httpres != HTTPRES_OK && httpres != HTTPRES_OK_NOT_MODIFIED)
598 ret = -1;
599 if (httpres == HTTPRES_LOST_CONNECTION)
600 RTMP_Log(RTMP_LOGERROR, "%s: connection lost while downloading swfurl %s",
601 __FUNCTION__, url);
602 else if (httpres == HTTPRES_NOT_FOUND)
603 RTMP_Log(RTMP_LOGERROR, "%s: swfurl %s not found", __FUNCTION__, url);
604 else
605 RTMP_Log(RTMP_LOGERROR, "%s: couldn't contact swfurl %s (HTTP error %d)",
606 __FUNCTION__, url, http.status);
608 else
610 if (got && pos)
611 fseek(f, pos, SEEK_SET);
612 else
614 char *q;
615 if (!f)
616 f = fopen(path, "w");
617 if (!f)
619 int err = errno;
620 RTMP_Log(RTMP_LOGERROR,
621 "%s: couldn't open %s for writing, errno %d (%s)",
622 __FUNCTION__, path, err, strerror(err));
623 ret = -1;
624 goto out;
626 fseek(f, 0, SEEK_END);
627 q = strchr(url, '?');
628 if (q)
629 i = q - url;
630 else
631 i = strlen(url);
633 fprintf(f, "url: %.*s\n", i, url);
635 strtime(&cnow, cctim);
636 fprintf(f, "ctim: %s\n", cctim);
638 if (!in.first)
640 HMAC_finish(in.ctx, hash, hlen);
641 *size = in.size;
643 fprintf(f, "date: %s\n", date);
644 fprintf(f, "size: %08x\n", in.size);
645 fprintf(f, "hash: ");
646 for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
647 fprintf(f, "%02x", hash[i]);
648 fprintf(f, "\n");
651 HMAC_close(in.ctx);
652 out:
653 free(path);
654 if (f)
655 fclose(f);
656 return ret;
658 #else
660 RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash,
661 int age)
663 return -1;
665 #endif