v2.6 changes
[rtmpdump.git] / librtmp / hashswf.c
blobfc5f8246aa1f7c075235a27b641183a5e4b2ccba
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>
28 #include <limits.h>
30 #include "rtmp_sys.h"
31 #include "log.h"
32 #include "http.h"
34 #ifdef CRYPTO
35 #ifdef USE_POLARSSL
36 #include <polarssl/sha2.h>
37 #ifndef SHA256_DIGEST_LENGTH
38 #define SHA256_DIGEST_LENGTH 32
39 #endif
40 #define HMAC_CTX sha2_context
41 #define HMAC_setup(ctx, key, len) sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0)
42 #define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len)
43 #define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; sha2_hmac_finish(&ctx, dig)
44 #define HMAC_close(ctx)
45 #elif defined(USE_GNUTLS)
46 #include <nettle/hmac.h>
47 #ifndef SHA256_DIGEST_LENGTH
48 #define SHA256_DIGEST_LENGTH 32
49 #endif
50 #undef HMAC_CTX
51 #define HMAC_CTX struct hmac_sha256_ctx
52 #define HMAC_setup(ctx, key, len) hmac_sha256_set_key(&ctx, len, key)
53 #define HMAC_crunch(ctx, buf, len) hmac_sha256_update(&ctx, len, buf)
54 #define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; hmac_sha256_digest(&ctx, SHA256_DIGEST_LENGTH, dig)
55 #define HMAC_close(ctx)
56 #else /* USE_OPENSSL */
57 #include <openssl/ssl.h>
58 #include <openssl/sha.h>
59 #include <openssl/hmac.h>
60 #include <openssl/rc4.h>
61 #if OPENSSL_VERSION_NUMBER >= 0x10100000
62 #define HMAC_CTX HMAC_CTX *
63 #define HMAC_setup(ctx, key, len) ctx = HMAC_CTX_new(); HMAC_Init_ex(ctx, key, len, EVP_sha256(), 0)
64 #define HMAC_crunch(ctx, buf, len) HMAC_Update(ctx, buf, len)
65 #define HMAC_finish(ctx, dig, dlen) HMAC_Final(ctx, dig, &dlen)
66 #define HMAC_close(ctx) HMAC_CTX_free(ctx)
67 #else
68 #define HMAC_setup(ctx, key, len) HMAC_CTX_init(&ctx); HMAC_Init_ex(&ctx, (unsigned char *)key, len, EVP_sha256(), 0)
69 #define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, (unsigned char *)buf, len)
70 #define HMAC_finish(ctx, dig, dlen) HMAC_Final(&ctx, (unsigned char *)dig, &dlen);
71 #define HMAC_close(ctx) HMAC_CTX_cleanup(&ctx)
72 #endif
73 #endif
75 extern void RTMP_TLS_Init();
76 extern TLS_CTX RTMP_TLS_ctx;
78 #include <zlib.h>
80 #endif /* CRYPTO */
82 #define DATELEN 64
84 #define AGENT "Mozilla/5.0"
86 HTTPResult
87 HTTP_get(struct HTTP_ctx *http, const char *url, HTTP_read_callback *cb)
89 char *host, *path;
90 char *p1, *p2;
91 char hbuf[256];
92 int port = 80;
93 #ifdef CRYPTO
94 int ssl = 0;
95 #endif
96 int hlen;
97 long flen = 0;
98 int rc, i;
99 int len_known;
100 HTTPResult ret = HTTPRES_OK;
101 struct sockaddr_in sa;
102 RTMPSockBuf sb = {0};
104 http->status = -1;
106 memset(&sa, 0, sizeof(struct sockaddr_in));
107 sa.sin_family = AF_INET;
109 /* we only handle http here */
110 if (strncasecmp(url, "http", 4))
111 return HTTPRES_BAD_REQUEST;
113 if (url[4] == 's')
115 #ifdef CRYPTO
116 ssl = 1;
117 port = 443;
118 if (!RTMP_TLS_ctx)
119 RTMP_TLS_Init();
120 #else
121 return HTTPRES_BAD_REQUEST;
122 #endif
125 p1 = strchr(url + 4, ':');
126 if (!p1 || strncmp(p1, "://", 3))
127 return HTTPRES_BAD_REQUEST;
129 host = p1 + 3;
130 path = strchr(host, '/');
131 hlen = path - host;
132 strncpy(hbuf, host, hlen);
133 hbuf[hlen] = '\0';
134 host = hbuf;
135 p1 = strrchr(host, ':');
136 if (p1)
138 *p1++ = '\0';
139 port = atoi(p1);
142 sa.sin_addr.s_addr = inet_addr(host);
143 if (sa.sin_addr.s_addr == INADDR_NONE)
145 struct hostent *hp = gethostbyname(host);
146 if (!hp || !hp->h_addr)
147 return HTTPRES_LOST_CONNECTION;
148 sa.sin_addr = *(struct in_addr *)hp->h_addr;
150 sa.sin_port = htons(port);
151 sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
152 if (sb.sb_socket == -1)
153 return HTTPRES_LOST_CONNECTION;
155 sprintf(sb.sb_buf,
156 "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\nReferer: %.*s\r\n",
157 path, AGENT, host, (int)(path - url + 1), url);
158 if (http->date[0])
159 i += sprintf(sb.sb_buf + i, "If-Modified-Since: %s\r\n", http->date);
160 i += sprintf(sb.sb_buf + i, "\r\n");
162 if (connect
163 (sb.sb_socket, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0)
165 ret = HTTPRES_LOST_CONNECTION;
166 goto leave;
168 #ifdef CRYPTO
169 if (ssl)
171 #ifdef NO_SSL
172 RTMP_Log(RTMP_LOGERROR, "%s, No SSL/TLS support", __FUNCTION__);
173 ret = HTTPRES_BAD_REQUEST;
174 goto leave;
175 #else
176 TLS_client(RTMP_TLS_ctx, sb.sb_ssl);
177 TLS_setfd(sb.sb_ssl, sb.sb_socket);
178 if (TLS_connect(sb.sb_ssl) < 0)
180 RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__);
181 ret = HTTPRES_LOST_CONNECTION;
182 goto leave;
184 #endif
186 #endif
187 RTMPSockBuf_Send(&sb, sb.sb_buf, i);
189 /* set timeout */
190 #define HTTP_TIMEOUT 5
192 SET_RCVTIMEO(tv, HTTP_TIMEOUT);
193 if (setsockopt
194 (sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)))
196 RTMP_Log(RTMP_LOGERROR, "%s, Setting socket timeout to %ds failed!",
197 __FUNCTION__, HTTP_TIMEOUT);
201 sb.sb_size = 0;
202 sb.sb_timedout = FALSE;
203 if (RTMPSockBuf_Fill(&sb) < 1)
205 ret = HTTPRES_LOST_CONNECTION;
206 goto leave;
208 if (strncmp(sb.sb_buf, "HTTP/1", 6))
210 ret = HTTPRES_BAD_REQUEST;
211 goto leave;
214 p1 = strchr(sb.sb_buf, ' ');
215 rc = atoi(p1 + 1);
216 http->status = rc;
218 if (rc >= 300)
220 if (rc == 304)
222 ret = HTTPRES_OK_NOT_MODIFIED;
223 goto leave;
225 else if (rc == 404)
226 ret = HTTPRES_NOT_FOUND;
227 else if (rc >= 500)
228 ret = HTTPRES_SERVER_ERROR;
229 else if (rc >= 400)
230 ret = HTTPRES_BAD_REQUEST;
231 else
232 ret = HTTPRES_REDIRECTED;
235 p1 = memchr(sb.sb_buf, '\n', sb.sb_size);
236 if (!p1)
238 ret = HTTPRES_BAD_REQUEST;
239 goto leave;
241 sb.sb_start = p1 + 1;
242 sb.sb_size -= sb.sb_start - sb.sb_buf;
244 while ((p2 = memchr(sb.sb_start, '\r', sb.sb_size)))
246 if (*sb.sb_start == '\r')
248 sb.sb_start += 2;
249 sb.sb_size -= 2;
250 break;
252 else
253 if (!strncasecmp
254 (sb.sb_start, "Content-Length: ", sizeof("Content-Length: ") - 1))
256 flen = strtol(sb.sb_start + sizeof("Content-Length: ") - 1, NULL, 10);
257 if (flen < 1 || flen > INT_MAX)
259 ret = HTTPRES_BAD_REQUEST;
260 goto leave;
263 else
264 if (!strncasecmp
265 (sb.sb_start, "Last-Modified: ", sizeof("Last-Modified: ") - 1))
267 *p2 = '\0';
268 strncpy(http->date, sb.sb_start + sizeof("Last-Modified: ") - 1, DATELEN-1);
269 http->date[DATELEN-1] = '\0';
271 p2 += 2;
272 sb.sb_size -= p2 - sb.sb_start;
273 sb.sb_start = p2;
274 if (sb.sb_size < 1)
276 if (RTMPSockBuf_Fill(&sb) < 1)
278 ret = HTTPRES_LOST_CONNECTION;
279 goto leave;
284 len_known = flen > 0;
285 while ((!len_known || flen > 0) &&
286 (sb.sb_size > 0 || RTMPSockBuf_Fill(&sb) > 0))
288 cb(sb.sb_start, 1, sb.sb_size, http->data);
289 if (len_known)
290 flen -= sb.sb_size;
291 http->size += sb.sb_size;
292 sb.sb_size = 0;
295 if (flen > 0)
296 ret = HTTPRES_LOST_CONNECTION;
298 leave:
299 RTMPSockBuf_Close(&sb);
300 return ret;
303 #ifdef CRYPTO
305 #define CHUNK 16384
307 struct info
309 z_stream *zs;
310 HMAC_CTX ctx;
311 int first;
312 int zlib;
313 int size;
316 static size_t
317 swfcrunch(void *ptr, size_t size, size_t nmemb, void *stream)
319 struct info *i = stream;
320 char *p = ptr;
321 size_t len = size * nmemb;
323 if (i->first)
325 i->first = 0;
326 /* compressed? */
327 if (!strncmp(p, "CWS", 3))
329 *p = 'F';
330 i->zlib = 1;
332 HMAC_crunch(i->ctx, (unsigned char *)p, 8);
333 p += 8;
334 len -= 8;
335 i->size = 8;
338 if (i->zlib)
340 unsigned char out[CHUNK];
341 i->zs->next_in = (unsigned char *)p;
342 i->zs->avail_in = len;
345 i->zs->avail_out = CHUNK;
346 i->zs->next_out = out;
347 inflate(i->zs, Z_NO_FLUSH);
348 len = CHUNK - i->zs->avail_out;
349 i->size += len;
350 HMAC_crunch(i->ctx, out, len);
352 while (i->zs->avail_out == 0);
354 else
356 i->size += len;
357 HMAC_crunch(i->ctx, (unsigned char *)p, len);
359 return size * nmemb;
362 static int tzoff;
363 static int tzchecked;
365 #define JAN02_1980 318340800
367 static const char *monthtab[12] = { "Jan", "Feb", "Mar",
368 "Apr", "May", "Jun",
369 "Jul", "Aug", "Sep",
370 "Oct", "Nov", "Dec"
372 static const char *days[] =
373 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
375 /* Parse an HTTP datestamp into Unix time */
376 static time_t
377 make_unix_time(char *s)
379 struct tm time;
380 int i, ysub = 1900, fmt = 0;
381 char *month;
382 char *n;
383 time_t res;
385 if (s[3] != ' ')
387 fmt = 1;
388 if (s[3] != ',')
389 ysub = 0;
391 for (n = s; *n; ++n)
392 if (*n == '-' || *n == ':')
393 *n = ' ';
395 time.tm_mon = 0;
396 n = strchr(s, ' ');
397 if (fmt)
399 /* Day, DD-MMM-YYYY HH:MM:SS GMT */
400 time.tm_mday = strtol(n + 1, &n, 0);
401 month = n + 1;
402 n = strchr(month, ' ');
403 time.tm_year = strtol(n + 1, &n, 0);
404 time.tm_hour = strtol(n + 1, &n, 0);
405 time.tm_min = strtol(n + 1, &n, 0);
406 time.tm_sec = strtol(n + 1, NULL, 0);
408 else
410 /* Unix ctime() format. Does not conform to HTTP spec. */
411 /* Day MMM DD HH:MM:SS YYYY */
412 month = n + 1;
413 n = strchr(month, ' ');
414 while (isspace(*n))
415 n++;
416 time.tm_mday = strtol(n, &n, 0);
417 time.tm_hour = strtol(n + 1, &n, 0);
418 time.tm_min = strtol(n + 1, &n, 0);
419 time.tm_sec = strtol(n + 1, &n, 0);
420 time.tm_year = strtol(n + 1, NULL, 0);
422 if (time.tm_year > 100)
423 time.tm_year -= ysub;
425 for (i = 0; i < 12; i++)
426 if (!strncasecmp(month, monthtab[i], 3))
428 time.tm_mon = i;
429 break;
431 time.tm_isdst = 0; /* daylight saving is never in effect in GMT */
433 /* this is normally the value of extern int timezone, but some
434 * braindead C libraries don't provide it.
436 if (!tzchecked)
438 struct tm *tc;
439 time_t then = JAN02_1980;
440 tc = localtime(&then);
441 tzoff = (12 - tc->tm_hour) * 3600 + tc->tm_min * 60 + tc->tm_sec;
442 tzchecked = 1;
444 res = mktime(&time);
445 /* Unfortunately, mktime() assumes the input is in local time,
446 * not GMT, so we have to correct it here.
448 if (res != -1)
449 res += tzoff;
450 return res;
453 /* Convert a Unix time to a network time string
454 * Weekday, DD-MMM-YYYY HH:MM:SS GMT
456 static void
457 strtime(time_t * t, char *s)
459 struct tm *tm;
461 tm = gmtime((time_t *) t);
462 sprintf(s, "%s, %02d %s %d %02d:%02d:%02d GMT",
463 days[tm->tm_wday], tm->tm_mday, monthtab[tm->tm_mon],
464 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
467 #define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf))
470 RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash,
471 int age)
473 FILE *f = NULL;
474 char *path, date[DATELEN], cctim[DATELEN];
475 long pos = 0;
476 time_t ctim = -1, cnow;
477 int i, got = 0, ret = 0;
478 unsigned int hlen;
479 struct info in = { 0 };
480 struct HTTP_ctx http = { 0 };
481 HTTPResult httpres;
482 z_stream zs = { 0 };
483 AVal home, hpre;
485 date[0] = '\0';
486 #ifdef _WIN32
487 #ifdef XBMC4XBOX
488 hpre.av_val = "Q:";
489 hpre.av_len = 2;
490 home.av_val = "\\UserData";
491 #else
492 hpre.av_val = getenv("HOMEDRIVE");
493 hpre.av_len = strlen(hpre.av_val);
494 home.av_val = getenv("HOMEPATH");
495 #endif
496 #define DIRSEP "\\"
498 #else /* !_WIN32 */
499 hpre.av_val = "";
500 hpre.av_len = 0;
501 home.av_val = getenv("HOME");
502 #define DIRSEP "/"
503 #endif
504 if (!home.av_val)
505 home.av_val = ".";
506 home.av_len = strlen(home.av_val);
508 /* SWF hash info is cached in a fixed-format file.
509 * url: <url of SWF file>
510 * ctim: HTTP datestamp of when we last checked it.
511 * date: HTTP datestamp of the SWF's last modification.
512 * size: SWF size in hex
513 * hash: SWF hash in hex
515 * These fields must be present in this order. All fields
516 * besides URL are fixed size.
518 path = malloc(hpre.av_len + home.av_len + sizeof(DIRSEP ".swfinfo"));
519 sprintf(path, "%s%s" DIRSEP ".swfinfo", hpre.av_val, home.av_val);
521 f = fopen(path, "r+");
522 while (f)
524 char buf[4096], *file, *p;
526 file = strchr(url, '/');
527 if (!file)
528 break;
529 file += 2;
530 file = strchr(file, '/');
531 if (!file)
532 break;
533 file++;
534 hlen = file - url;
535 p = strrchr(file, '/');
536 if (p)
537 file = p;
538 else
539 file--;
541 while (fgets(buf, sizeof(buf), f))
543 char *r1;
545 got = 0;
547 if (strncmp(buf, "url: ", 5))
548 continue;
549 if (strncmp(buf + 5, url, hlen))
550 continue;
551 r1 = strrchr(buf, '/');
552 i = strlen(r1);
553 r1[--i] = '\0';
554 if (strncmp(r1, file, i))
555 continue;
556 pos = ftell(f);
557 while (got < 4 && fgets(buf, sizeof(buf), f))
559 if (!strncmp(buf, "size: ", 6))
561 *size = strtol(buf + 6, NULL, 16);
562 got++;
564 else if (!strncmp(buf, "hash: ", 6))
566 unsigned char *ptr = hash, *in = (unsigned char *)buf + 6;
567 int l = strlen((char *)in) - 1;
568 for (i = 0; i < l; i += 2)
569 *ptr++ = (HEX2BIN(in[i]) << 4) | HEX2BIN(in[i + 1]);
570 got++;
572 else if (!strncmp(buf, "date: ", 6))
574 buf[strlen(buf) - 1] = '\0';
575 strncpy(date, buf + 6, sizeof(date)-1);
576 date[DATELEN-1] = '\0';
577 got++;
579 else if (!strncmp(buf, "ctim: ", 6))
581 buf[strlen(buf) - 1] = '\0';
582 ctim = make_unix_time(buf + 6);
583 got++;
585 else if (!strncmp(buf, "url: ", 5))
586 break;
588 break;
590 break;
593 cnow = time(NULL);
594 /* If we got a cache time, see if it's young enough to use directly */
595 if (age && ctim > 0)
597 ctim = cnow - ctim;
598 ctim /= 3600 * 24; /* seconds to days */
599 if (ctim < age) /* ok, it's new enough */
600 goto out;
603 in.first = 1;
604 HMAC_setup(in.ctx, "Genuine Adobe Flash Player 001", 30);
605 inflateInit(&zs);
606 in.zs = &zs;
608 http.date = date;
609 http.data = &in;
611 httpres = HTTP_get(&http, url, swfcrunch);
613 inflateEnd(&zs);
615 if (httpres != HTTPRES_OK && httpres != HTTPRES_OK_NOT_MODIFIED)
617 ret = -1;
618 if (httpres == HTTPRES_LOST_CONNECTION)
619 RTMP_Log(RTMP_LOGERROR, "%s: connection lost while downloading swfurl %s",
620 __FUNCTION__, url);
621 else if (httpres == HTTPRES_NOT_FOUND)
622 RTMP_Log(RTMP_LOGERROR, "%s: swfurl %s not found", __FUNCTION__, url);
623 else
624 RTMP_Log(RTMP_LOGERROR, "%s: couldn't contact swfurl %s (HTTP error %d)",
625 __FUNCTION__, url, http.status);
627 else
629 if (got && pos)
630 fseek(f, pos, SEEK_SET);
631 else
633 char *q;
634 if (!f)
635 f = fopen(path, "w");
636 if (!f)
638 int err = errno;
639 RTMP_Log(RTMP_LOGERROR,
640 "%s: couldn't open %s for writing, errno %d (%s)",
641 __FUNCTION__, path, err, strerror(err));
642 ret = -1;
643 goto out;
645 fseek(f, 0, SEEK_END);
646 q = strchr(url, '?');
647 if (q)
648 i = q - url;
649 else
650 i = strlen(url);
652 fprintf(f, "url: %.*s\n", i, url);
654 strtime(&cnow, cctim);
655 fprintf(f, "ctim: %s\n", cctim);
657 if (!in.first)
659 HMAC_finish(in.ctx, hash, hlen);
660 *size = in.size;
662 fprintf(f, "date: %s\n", date);
663 fprintf(f, "size: %08x\n", in.size);
664 fprintf(f, "hash: ");
665 for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
666 fprintf(f, "%02x", hash[i]);
667 fprintf(f, "\n");
670 HMAC_close(in.ctx);
671 out:
672 free(path);
673 if (f)
674 fclose(f);
675 return ret;
677 #else
679 RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash,
680 int age)
682 return -1;
684 #endif