hashswf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * Copyright (C) 2009-2010 Howard Chu
  3. *
  4. * This file is part of librtmp.
  5. *
  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.
  10. *
  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.
  15. *
  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
  21. */
  22. #include "rtmp_sys.h"
  23. #include "log.h"
  24. #include "http.h"
  25. #ifdef CRYPTO
  26. #ifdef __APPLE__
  27. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  28. #endif
  29. #if defined(USE_MBEDTLS)
  30. #include <mbedtls/md.h>
  31. #ifndef SHA256_DIGEST_LENGTH
  32. #define SHA256_DIGEST_LENGTH 32
  33. #endif
  34. typedef mbedtls_md_context_t *HMAC_CTX;
  35. #define HMAC_setup(ctx, key, len) ctx = malloc(sizeof(mbedtls_md_context_t)); mbedtls_md_init(ctx); \
  36. mbedtls_md_setup(ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1); \
  37. mbedtls_md_hmac_starts(ctx, (const unsigned char *)key, len)
  38. #define HMAC_crunch(ctx, buf, len) mbedtls_md_hmac_update(ctx, buf, len)
  39. #define HMAC_finish(ctx, dig) mbedtls_md_hmac_finish(ctx, dig)
  40. #define HMAC_close(ctx) free(ctx); mbedtls_md_free(ctx); ctx = NULL
  41. #elif defined(USE_POLARSSL)
  42. #include <polarssl/sha2.h>
  43. #ifndef SHA256_DIGEST_LENGTH
  44. #define SHA256_DIGEST_LENGTH 32
  45. #endif
  46. #define HMAC_CTX sha2_context
  47. #define HMAC_setup(ctx, key, len) sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0)
  48. #define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len)
  49. #define HMAC_finish(ctx, dig) sha2_hmac_finish(&ctx, dig)
  50. #define HMAC_close(ctx)
  51. #elif defined(USE_GNUTLS)
  52. #include <nettle/hmac.h>
  53. #ifndef SHA256_DIGEST_LENGTH
  54. #define SHA256_DIGEST_LENGTH 32
  55. #endif
  56. #undef HMAC_CTX
  57. #define HMAC_CTX struct hmac_sha256_ctx
  58. #define HMAC_setup(ctx, key, len) hmac_sha256_set_key(&ctx, len, key)
  59. #define HMAC_crunch(ctx, buf, len) hmac_sha256_update(&ctx, len, buf)
  60. #define HMAC_finish(ctx, dig) hmac_sha256_digest(&ctx, SHA256_DIGEST_LENGTH, dig)
  61. #define HMAC_close(ctx)
  62. #else /* USE_OPENSSL */
  63. #include <openssl/ssl.h>
  64. #include <openssl/sha.h>
  65. #include <openssl/hmac.h>
  66. #include <openssl/rc4.h>
  67. #define HMAC_setup(ctx, key, len) HMAC_CTX_init(&ctx); HMAC_Init_ex(&ctx, (unsigned char *)key, len, EVP_sha256(), 0)
  68. #define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, (unsigned char *)buf, len)
  69. #define HMAC_finish(ctx, dig, len) HMAC_Final(&ctx, (unsigned char *)dig, &len);
  70. #define HMAC_close(ctx) HMAC_CTX_cleanup(&ctx)
  71. #endif
  72. extern void RTMP_TLS_Init();
  73. extern TLS_CTX RTMP_TLS_ctx;
  74. #include <zlib.h>
  75. #endif /* CRYPTO */
  76. #define AGENT "Mozilla/5.0"
  77. HTTPResult
  78. HTTP_get(struct HTTP_ctx *http, const char *url, HTTP_read_callback *cb)
  79. {
  80. char *host, *path;
  81. char *p1, *p2;
  82. char hbuf[256];
  83. int port = 80;
  84. #ifdef CRYPTO
  85. int ssl = 0;
  86. #endif
  87. int hlen, flen = 0;
  88. int rc, i;
  89. int len_known;
  90. HTTPResult ret = HTTPRES_OK;
  91. struct sockaddr_in sa;
  92. RTMPSockBuf sb = {0};
  93. http->status = -1;
  94. memset(&sa, 0, sizeof(struct sockaddr_in));
  95. sa.sin_family = AF_INET;
  96. /* we only handle http here */
  97. if (strncasecmp(url, "http", 4))
  98. return HTTPRES_BAD_REQUEST;
  99. if (url[4] == 's')
  100. {
  101. #ifdef CRYPTO
  102. ssl = 1;
  103. port = 443;
  104. if (!RTMP_TLS_ctx)
  105. RTMP_TLS_Init();
  106. #else
  107. return HTTPRES_BAD_REQUEST;
  108. #endif
  109. }
  110. p1 = strchr(url + 4, ':');
  111. if (!p1 || strncmp(p1, "://", 3))
  112. return HTTPRES_BAD_REQUEST;
  113. host = p1 + 3;
  114. path = strchr(host, '/');
  115. hlen = path - host;
  116. strncpy(hbuf, host, hlen);
  117. hbuf[hlen] = '\0';
  118. host = hbuf;
  119. p1 = strrchr(host, ':');
  120. if (p1)
  121. {
  122. *p1++ = '\0';
  123. port = atoi(p1);
  124. }
  125. sa.sin_addr.s_addr = inet_addr(host);
  126. if (sa.sin_addr.s_addr == INADDR_NONE)
  127. {
  128. struct hostent *hp = gethostbyname(host);
  129. if (!hp || !hp->h_addr)
  130. return HTTPRES_LOST_CONNECTION;
  131. sa.sin_addr = *(struct in_addr *)hp->h_addr;
  132. }
  133. sa.sin_port = htons(port);
  134. sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  135. if (sb.sb_socket == INVALID_SOCKET)
  136. return HTTPRES_LOST_CONNECTION;
  137. i =
  138. sprintf(sb.sb_buf,
  139. "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\nReferer: %.*s\r\n",
  140. path, AGENT, host, (int)(path - url + 1), url);
  141. if (http->date[0])
  142. i += sprintf(sb.sb_buf + i, "If-Modified-Since: %s\r\n", http->date);
  143. i += sprintf(sb.sb_buf + i, "\r\n");
  144. if (connect
  145. (sb.sb_socket, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0)
  146. {
  147. ret = HTTPRES_LOST_CONNECTION;
  148. goto leave;
  149. }
  150. #ifdef CRYPTO
  151. if (ssl)
  152. {
  153. #ifdef NO_SSL
  154. RTMP_Log(RTMP_LOGERROR, "%s, No SSL/TLS support", __FUNCTION__);
  155. ret = HTTPRES_BAD_REQUEST;
  156. goto leave;
  157. #else
  158. TLS_client(RTMP_TLS_ctx, sb.sb_ssl);
  159. #if defined(USE_MBEDTLS)
  160. mbedtls_net_context *server_fd = &RTMP_TLS_ctx->net;
  161. server_fd->fd = sb.sb_socket;
  162. TLS_setfd(sb.sb_ssl, server_fd);
  163. #else
  164. TLS_setfd(sb.sb_ssl, sb.sb_socket);
  165. #endif
  166. int connect_return = TLS_connect(sb.sb_ssl);
  167. if (connect_return < 0)
  168. {
  169. RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__);
  170. ret = HTTPRES_LOST_CONNECTION;
  171. goto leave;
  172. }
  173. #endif
  174. }
  175. #endif
  176. RTMPSockBuf_Send(&sb, sb.sb_buf, i);
  177. /* set timeout */
  178. #define HTTP_TIMEOUT 5
  179. {
  180. SET_RCVTIMEO(tv, HTTP_TIMEOUT);
  181. if (setsockopt
  182. (sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)))
  183. {
  184. RTMP_Log(RTMP_LOGERROR, "%s, Setting socket timeout to %ds failed!",
  185. __FUNCTION__, HTTP_TIMEOUT);
  186. }
  187. }
  188. sb.sb_size = 0;
  189. sb.sb_timedout = FALSE;
  190. if (RTMPSockBuf_Fill(&sb) < 1)
  191. {
  192. ret = HTTPRES_LOST_CONNECTION;
  193. goto leave;
  194. }
  195. if (strncmp(sb.sb_buf, "HTTP/1", 6))
  196. {
  197. ret = HTTPRES_BAD_REQUEST;
  198. goto leave;
  199. }
  200. p1 = strchr(sb.sb_buf, ' ');
  201. rc = atoi(p1 + 1);
  202. http->status = rc;
  203. if (rc >= 300)
  204. {
  205. if (rc == 304)
  206. {
  207. ret = HTTPRES_OK_NOT_MODIFIED;
  208. goto leave;
  209. }
  210. else if (rc == 404)
  211. ret = HTTPRES_NOT_FOUND;
  212. else if (rc >= 500)
  213. ret = HTTPRES_SERVER_ERROR;
  214. else if (rc >= 400)
  215. ret = HTTPRES_BAD_REQUEST;
  216. else
  217. ret = HTTPRES_REDIRECTED;
  218. }
  219. p1 = memchr(sb.sb_buf, '\n', sb.sb_size);
  220. if (!p1)
  221. {
  222. ret = HTTPRES_BAD_REQUEST;
  223. goto leave;
  224. }
  225. sb.sb_start = p1 + 1;
  226. sb.sb_size -= sb.sb_start - sb.sb_buf;
  227. while ((p2 = memchr(sb.sb_start, '\r', sb.sb_size)))
  228. {
  229. if (*sb.sb_start == '\r')
  230. {
  231. sb.sb_start += 2;
  232. sb.sb_size -= 2;
  233. break;
  234. }
  235. else if (!strncasecmp
  236. (sb.sb_start, "Content-Length: ", sizeof("Content-Length: ") - 1))
  237. {
  238. flen = atoi(sb.sb_start + sizeof("Content-Length: ") - 1);
  239. }
  240. else if (!strncasecmp
  241. (sb.sb_start, "Last-Modified: ", sizeof("Last-Modified: ") - 1))
  242. {
  243. *p2 = '\0';
  244. strcpy(http->date, sb.sb_start + sizeof("Last-Modified: ") - 1);
  245. }
  246. p2 += 2;
  247. sb.sb_size -= p2 - sb.sb_start;
  248. sb.sb_start = p2;
  249. if (sb.sb_size < 1)
  250. {
  251. if (RTMPSockBuf_Fill(&sb) < 1)
  252. {
  253. ret = HTTPRES_LOST_CONNECTION;
  254. goto leave;
  255. }
  256. }
  257. }
  258. len_known = flen > 0;
  259. while ((!len_known || flen > 0) &&
  260. (sb.sb_size > 0 || RTMPSockBuf_Fill(&sb) > 0))
  261. {
  262. cb(sb.sb_start, 1, sb.sb_size, http->data);
  263. if (len_known)
  264. flen -= sb.sb_size;
  265. http->size += sb.sb_size;
  266. sb.sb_size = 0;
  267. }
  268. if (flen > 0)
  269. ret = HTTPRES_LOST_CONNECTION;
  270. leave:
  271. RTMPSockBuf_Close(&sb);
  272. return ret;
  273. }
  274. #ifdef CRYPTO
  275. #define CHUNK 16384
  276. struct info
  277. {
  278. z_stream *zs;
  279. HMAC_CTX ctx;
  280. int first;
  281. int zlib;
  282. int size;
  283. };
  284. static size_t
  285. swfcrunch(void *ptr, size_t size, size_t nmemb, void *stream)
  286. {
  287. struct info *i = stream;
  288. char *p = ptr;
  289. size_t len = size * nmemb;
  290. if (i->first)
  291. {
  292. i->first = 0;
  293. /* compressed? */
  294. if (!strncmp(p, "CWS", 3))
  295. {
  296. *p = 'F';
  297. i->zlib = 1;
  298. }
  299. HMAC_crunch(i->ctx, (unsigned char *)p, 8);
  300. p += 8;
  301. len -= 8;
  302. i->size = 8;
  303. }
  304. if (i->zlib)
  305. {
  306. unsigned char out[CHUNK];
  307. i->zs->next_in = (unsigned char *)p;
  308. i->zs->avail_in = (uInt)len;
  309. do
  310. {
  311. i->zs->avail_out = CHUNK;
  312. i->zs->next_out = out;
  313. inflate(i->zs, Z_NO_FLUSH);
  314. len = CHUNK - i->zs->avail_out;
  315. i->size += (int)len;
  316. HMAC_crunch(i->ctx, out, len);
  317. }
  318. while (i->zs->avail_out == 0);
  319. }
  320. else
  321. {
  322. i->size += (int)len;
  323. HMAC_crunch(i->ctx, (unsigned char *)p, len);
  324. }
  325. return size * nmemb;
  326. }
  327. static int tzoff;
  328. static int tzchecked;
  329. #define JAN02_1980 318340800
  330. static const char *monthtab[12] = { "Jan", "Feb", "Mar",
  331. "Apr", "May", "Jun",
  332. "Jul", "Aug", "Sep",
  333. "Oct", "Nov", "Dec"
  334. };
  335. static const char *days[] =
  336. { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  337. /* Parse an HTTP datestamp into Unix time */
  338. static time_t
  339. make_unix_time(char *s)
  340. {
  341. struct tm time;
  342. int i, ysub = 1900, fmt = 0;
  343. char *month;
  344. char *n;
  345. time_t res;
  346. if (s[3] != ' ')
  347. {
  348. fmt = 1;
  349. if (s[3] != ',')
  350. ysub = 0;
  351. }
  352. for (n = s; *n; ++n)
  353. if (*n == '-' || *n == ':')
  354. *n = ' ';
  355. time.tm_mon = 0;
  356. n = strchr(s, ' ');
  357. if (fmt)
  358. {
  359. /* Day, DD-MMM-YYYY HH:MM:SS GMT */
  360. time.tm_mday = strtol(n + 1, &n, 0);
  361. month = n + 1;
  362. n = strchr(month, ' ');
  363. time.tm_year = strtol(n + 1, &n, 0);
  364. time.tm_hour = strtol(n + 1, &n, 0);
  365. time.tm_min = strtol(n + 1, &n, 0);
  366. time.tm_sec = strtol(n + 1, NULL, 0);
  367. }
  368. else
  369. {
  370. /* Unix ctime() format. Does not conform to HTTP spec. */
  371. /* Day MMM DD HH:MM:SS YYYY */
  372. month = n + 1;
  373. n = strchr(month, ' ');
  374. while (isspace(*n))
  375. n++;
  376. time.tm_mday = strtol(n, &n, 0);
  377. time.tm_hour = strtol(n + 1, &n, 0);
  378. time.tm_min = strtol(n + 1, &n, 0);
  379. time.tm_sec = strtol(n + 1, &n, 0);
  380. time.tm_year = strtol(n + 1, NULL, 0);
  381. }
  382. if (time.tm_year > 100)
  383. time.tm_year -= ysub;
  384. for (i = 0; i < 12; i++)
  385. if (!strncasecmp(month, monthtab[i], 3))
  386. {
  387. time.tm_mon = i;
  388. break;
  389. }
  390. time.tm_isdst = 0; /* daylight saving is never in effect in GMT */
  391. /* this is normally the value of extern int timezone, but some
  392. * braindead C libraries don't provide it.
  393. */
  394. if (!tzchecked)
  395. {
  396. struct tm *tc;
  397. time_t then = JAN02_1980;
  398. tc = localtime(&then);
  399. tzoff = (12 - tc->tm_hour) * 3600 + tc->tm_min * 60 + tc->tm_sec;
  400. tzchecked = 1;
  401. }
  402. res = mktime(&time);
  403. /* Unfortunately, mktime() assumes the input is in local time,
  404. * not GMT, so we have to correct it here.
  405. */
  406. if (res != -1)
  407. res += tzoff;
  408. return res;
  409. }
  410. /* Convert a Unix time to a network time string
  411. * Weekday, DD-MMM-YYYY HH:MM:SS GMT
  412. */
  413. static void
  414. strtime(time_t * t, char *s)
  415. {
  416. struct tm *tm;
  417. tm = gmtime((time_t *) t);
  418. sprintf(s, "%s, %02d %s %d %02d:%02d:%02d GMT",
  419. days[tm->tm_wday], tm->tm_mday, monthtab[tm->tm_mon],
  420. tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
  421. }
  422. #define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf))
  423. int
  424. RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash,
  425. int age)
  426. {
  427. FILE *f = NULL;
  428. char *path, date[64], cctim[64];
  429. long pos = 0;
  430. time_t ctim = -1, cnow;
  431. int i, got = 0, ret = 0;
  432. unsigned int hlen;
  433. struct info in = { 0 };
  434. struct HTTP_ctx http = { 0 };
  435. HTTPResult httpres;
  436. z_stream zs = { 0 };
  437. AVal home, hpre;
  438. date[0] = '\0';
  439. #ifdef _WIN32
  440. #ifdef XBMC4XBOX
  441. hpre.av_val = "Q:";
  442. hpre.av_len = 2;
  443. home.av_val = "\\UserData";
  444. #else
  445. hpre.av_val = getenv("HOMEDRIVE");
  446. hpre.av_len = (int)strlen(hpre.av_val);
  447. home.av_val = getenv("HOMEPATH");
  448. #endif
  449. #define DIRSEP "\\"
  450. #else /* !_WIN32 */
  451. hpre.av_val = "";
  452. hpre.av_len = 0;
  453. home.av_val = getenv("HOME");
  454. #define DIRSEP "/"
  455. #endif
  456. if (!home.av_val)
  457. home.av_val = ".";
  458. home.av_len = (int)strlen(home.av_val);
  459. /* SWF hash info is cached in a fixed-format file.
  460. * url: <url of SWF file>
  461. * ctim: HTTP datestamp of when we last checked it.
  462. * date: HTTP datestamp of the SWF's last modification.
  463. * size: SWF size in hex
  464. * hash: SWF hash in hex
  465. *
  466. * These fields must be present in this order. All fields
  467. * besides URL are fixed size.
  468. */
  469. path = malloc(hpre.av_len + home.av_len + sizeof(DIRSEP ".swfinfo"));
  470. sprintf(path, "%s%s" DIRSEP ".swfinfo", hpre.av_val, home.av_val);
  471. f = fopen(path, "r+");
  472. while (f)
  473. {
  474. char buf[4096], *file, *p;
  475. file = strchr(url, '/');
  476. if (!file)
  477. break;
  478. file += 2;
  479. file = strchr(file, '/');
  480. if (!file)
  481. break;
  482. file++;
  483. hlen = file - url;
  484. p = strrchr(file, '/');
  485. if (p)
  486. file = p;
  487. else
  488. file--;
  489. while (fgets(buf, sizeof(buf), f))
  490. {
  491. char *r1;
  492. got = 0;
  493. if (strncmp(buf, "url: ", 5))
  494. continue;
  495. if (strncmp(buf + 5, url, hlen))
  496. continue;
  497. r1 = strrchr(buf, '/');
  498. i = (int)strlen(r1);
  499. r1[--i] = '\0';
  500. if (strncmp(r1, file, i))
  501. continue;
  502. pos = ftell(f);
  503. while (got < 4 && fgets(buf, sizeof(buf), f))
  504. {
  505. if (!strncmp(buf, "size: ", 6))
  506. {
  507. *size = strtol(buf + 6, NULL, 16);
  508. got++;
  509. }
  510. else if (!strncmp(buf, "hash: ", 6))
  511. {
  512. unsigned char *ptr = hash, *in = (unsigned char *)buf + 6;
  513. int l = (int)strlen((char *)in) - 1;
  514. for (i = 0; i < l; i += 2)
  515. *ptr++ = (HEX2BIN(in[i]) << 4) | HEX2BIN(in[i + 1]);
  516. got++;
  517. }
  518. else if (!strncmp(buf, "date: ", 6))
  519. {
  520. buf[strlen(buf) - 1] = '\0';
  521. strncpy(date, buf + 6, sizeof(date));
  522. got++;
  523. }
  524. else if (!strncmp(buf, "ctim: ", 6))
  525. {
  526. buf[strlen(buf) - 1] = '\0';
  527. ctim = make_unix_time(buf + 6);
  528. got++;
  529. }
  530. else if (!strncmp(buf, "url: ", 5))
  531. break;
  532. }
  533. break;
  534. }
  535. break;
  536. }
  537. cnow = time(NULL);
  538. /* If we got a cache time, see if it's young enough to use directly */
  539. if (age && ctim > 0)
  540. {
  541. ctim = cnow - ctim;
  542. ctim /= 3600 * 24; /* seconds to days */
  543. if (ctim < age) /* ok, it's new enough */
  544. goto out;
  545. }
  546. in.first = 1;
  547. HMAC_setup(in.ctx, "Genuine Adobe Flash Player 001", 30);
  548. inflateInit(&zs);
  549. in.zs = &zs;
  550. http.date = date;
  551. http.data = &in;
  552. httpres = HTTP_get(&http, url, swfcrunch);
  553. inflateEnd(&zs);
  554. if (httpres != HTTPRES_OK && httpres != HTTPRES_OK_NOT_MODIFIED)
  555. {
  556. ret = -1;
  557. if (httpres == HTTPRES_LOST_CONNECTION)
  558. RTMP_Log(RTMP_LOGERROR, "%s: connection lost while downloading swfurl %s",
  559. __FUNCTION__, url);
  560. else if (httpres == HTTPRES_NOT_FOUND)
  561. RTMP_Log(RTMP_LOGERROR, "%s: swfurl %s not found", __FUNCTION__, url);
  562. else
  563. RTMP_Log(RTMP_LOGERROR, "%s: couldn't contact swfurl %s (HTTP error %d)",
  564. __FUNCTION__, url, http.status);
  565. }
  566. else
  567. {
  568. if (got && pos)
  569. fseek(f, pos, SEEK_SET);
  570. else
  571. {
  572. char *q;
  573. if (!f)
  574. f = fopen(path, "w");
  575. if (!f)
  576. {
  577. int err = errno;
  578. RTMP_Log(RTMP_LOGERROR,
  579. "%s: couldn't open %s for writing, errno %d (%s)",
  580. __FUNCTION__, path, err, strerror(err));
  581. ret = -1;
  582. goto out;
  583. }
  584. fseek(f, 0, SEEK_END);
  585. q = strchr(url, '?');
  586. if (q)
  587. i = q - url;
  588. else
  589. i = (int)strlen(url);
  590. fprintf(f, "url: %.*s\n", i, url);
  591. }
  592. strtime(&cnow, cctim);
  593. fprintf(f, "ctim: %s\n", cctim);
  594. if (!in.first)
  595. {
  596. #if defined(USE_MBEDTLS) || defined(USE_POLARSSL) || defined(USE_GNUTLS)
  597. HMAC_finish(in.ctx, hash);
  598. #else
  599. HMAC_finish(in.ctx, hash, hlen);
  600. #endif
  601. *size = in.size;
  602. fprintf(f, "date: %s\n", date);
  603. fprintf(f, "size: %08x\n", in.size);
  604. fprintf(f, "hash: ");
  605. for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
  606. fprintf(f, "%02x", hash[i]);
  607. fprintf(f, "\n");
  608. }
  609. }
  610. HMAC_close(in.ctx);
  611. out:
  612. free(path);
  613. if (f)
  614. fclose(f);
  615. return ret;
  616. }
  617. #else
  618. int
  619. RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash,
  620. int age)
  621. {
  622. (void)url;
  623. (void)size;
  624. (void)hash;
  625. (void)age;
  626. return -1;
  627. }
  628. #endif