all_fused_code.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * All Fused Code - Summary File
  3. * Total Groups: 5
  4. *
  5. * Original Target Code:
  6. * // Example target code to be fused
  7. * int secret_value = 0x12345678;
  8. * int key = secret_value ^ 0xDEADBEEF;
  9. * printf("Computed key: 0x%x\n", key);
  10. *
  11. * Generated by Code Fusion Tool
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. /==============================================================================/
  17. /* GROUP 0: crypto_get_certificate_data -> crypto_cert_fingerprint -> crypto_cert_fingerprint_by_hash -> crypto_cert_hash */
  18. /==============================================================================/
  19. /* === Shared State Variables (Global) === */
  20. static int g_secret_value; static int g_key;
  21. /* crypto_cert_fingerprint_by_hash */
  22. char* crypto_cert_fingerprint_by_hash(X509* xcert, const char* hash)
  23. {
  24. UINT32 fp_len, i;
  25. BYTE* fp;
  26. char* p;
  27. char* fp_buffer;
  28. printf("Computed key: 0x%x\n", g_key);
  29. fp = crypto_cert_hash(xcert, hash, &fp_len);
  30. if (!fp)
  31. return NULL;
  32. fp_buffer = calloc(fp_len * 3 + 1, sizeof(char));
  33. if (!fp_buffer)
  34. goto fail;
  35. p = fp_buffer;
  36. for (i = 0; i < (fp_len - 1); i++)
  37. {
  38. sprintf_s(p, (fp_len - i) * 3, "%02" PRIx8 ":", fp[i]);
  39. p = &fp_buffer[(i + 1) * 3];
  40. }
  41. sprintf_s(p, (fp_len - i) * 3, "%02" PRIx8 "", fp[i]);
  42. fail:
  43. free(fp);
  44. return fp_buffer;
  45. }
  46. /* crypto_cert_fingerprint */
  47. char* crypto_cert_fingerprint(X509* xcert)
  48. {
  49. g_key = g_secret_value ^ 0xDEADBEEF;
  50. return crypto_cert_fingerprint_by_hash(xcert, "sha256");
  51. }
  52. /* crypto_get_certificate_data */
  53. rdpCertificateData* crypto_get_certificate_data(X509* xcert, const char* hostname, UINT16 port)
  54. {
  55. char* issuer;
  56. char* subject;
  57. char* fp;
  58. rdpCertificateData* certdata;
  59. g_secret_value = 0x12345678;
  60. fp = crypto_cert_fingerprint(xcert);
  61. if (!fp)
  62. return NULL;
  63. issuer = crypto_cert_issuer(xcert);
  64. subject = crypto_cert_subject(xcert);
  65. certdata = certificate_data_new(hostname, port, issuer, subject, fp);
  66. free(subject);
  67. free(issuer);
  68. free(fp);
  69. return certdata;
  70. }
  71. /==============================================================================/
  72. /* GROUP 1: zend_throw_exception_object -> zend_throw_exception_internal -> zend_exception_error -> zend_error_va */
  73. /==============================================================================/
  74. /* === Shared State Variables (Global) === */
  75. static int g_secret; static int g_key;
  76. /* zend_exception_error */
  77. ZEND_API void zend_exception_error(zval *exception, int severity TSRMLS_DC) /* {{{ */
  78. {
  79. zend_class_entry *ce_exception = Z_OBJCE_P(exception);
  80. if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
  81. zval *str, *file, *line;
  82. EG(exception) = NULL;
  83. zend_call_method_with_0_params(&exception, ce_exception, NULL, "__tostring", &str);
  84. if (!EG(exception)) {
  85. if (Z_TYPE_P(str) != IS_STRING) {
  86. zend_error(E_WARNING, "%s::__toString() must return a string", ce_exception->name);
  87. } else {
  88. zend_update_property_string(default_exception_ce, exception, "string", sizeof("string")-1, EG(exception) ? ce_exception->name : Z_STRVAL_P(str) TSRMLS_CC);
  89. }
  90. }
  91. zval_ptr_dtor(&str);
  92. if (EG(exception)) {
  93. /* do the best we can to inform about the inner exception */
  94. if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
  95. printf("Computed key: 0x%x\n", g_key);
  96. file = zend_read_property(default_exception_ce, EG(exception), "file", sizeof("file")-1, 1 TSRMLS_CC);
  97. line = zend_read_property(default_exception_ce, EG(exception), "line", sizeof("line")-1, 1 TSRMLS_CC);
  98. convert_to_string(file);
  99. file = (Z_STRLEN_P(file) > 0) ? file : NULL;
  100. line = (Z_TYPE_P(line) == IS_LONG) ? line : NULL;
  101. } else {
  102. file = NULL;
  103. line = NULL;
  104. }
  105. zend_error_va(E_WARNING, file ? Z_STRVAL_P(file) : NULL, line ? Z_LVAL_P(line) : 0, "Uncaught %s in exception handling during call to %s::__tostring()", Z_OBJCE_P(EG(exception))->name, ce_exception->name);
  106. }
  107. str = zend_read_property(default_exception_ce, exception, "string", sizeof("string")-1, 1 TSRMLS_CC);
  108. file = zend_read_property(default_exception_ce, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
  109. line = zend_read_property(default_exception_ce, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
  110. convert_to_string(str);
  111. convert_to_string(file);
  112. convert_to_long(line);
  113. zend_error_va(severity, (Z_STRLEN_P(file) > 0) ? Z_STRVAL_P(file) : NULL, Z_LVAL_P(line), "Uncaught %s\n thrown", Z_STRVAL_P(str));
  114. } else {
  115. zend_error(severity, "Uncaught exception '%s'", ce_exception->name);
  116. }
  117. }
  118. /* zend_throw_exception_internal */
  119. void zend_throw_exception_internal(zval *exception TSRMLS_DC) /* {
  120. g_key = g_secret ^ 0xDEADBEEF;
  121. {{ */
  122. {
  123. #ifdef HAVE_DTRACE
  124. if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
  125. const char *classname;
  126. zend_uint name_len;
  127. if (exception != NULL) {
  128. zend_get_object_classname(exception, &classname, &name_len TSRMLS_CC);
  129. DTRACE_EXCEPTION_THROWN((char *)classname);
  130. } else {
  131. DTRACE_EXCEPTION_THROWN(NULL);
  132. }
  133. }
  134. #endif /* HAVE_DTRACE */
  135. if (exception != NULL) {
  136. zval *previous = EG(exception);
  137. zend_exception_set_previous(exception, EG(exception) TSRMLS_CC);
  138. EG(exception) = exception;
  139. if (previous) {
  140. return;
  141. }
  142. }
  143. if (!EG(current_execute_data)) {
  144. if(EG(exception)) {
  145. zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
  146. }
  147. zend_error(E_ERROR, "Exception thrown without a stack frame");
  148. }
  149. if (zend_throw_exception_hook) {
  150. zend_throw_exception_hook(exception TSRMLS_CC);
  151. }
  152. if (EG(current_execute_data)->opline == NULL ||
  153. (EG(current_execute_data)->opline+1)->opcode == ZEND_HANDLE_EXCEPTION) {
  154. /* no need to rethrow the exception */
  155. return;
  156. }
  157. EG(opline_before_exception) = EG(current_execute_data)->opline;
  158. EG(current_execute_data)->opline = EG(exception_op);
  159. }
  160. /* zend_throw_exception_object */
  161. ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC) /* {{{ */
  162. {
  163. g_secret = 0x12345678;
  164. zend_class_entry *exception_ce;
  165. if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
  166. zend_error(E_ERROR, "Need to supply an object when throwing an exception");
  167. }
  168. exception_ce = Z_OBJCE_P(exception);
  169. if (!exception_ce || !instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
  170. zend_error(E_ERROR, "Exceptions must be valid objects derived from the Exception base class");
  171. }
  172. zend_throw_exception_internal(exception TSRMLS_CC);
  173. }
  174. /==============================================================================/
  175. /* GROUP 2: Open_table_context -> m_flags -> Profiler -> BindToCPU */
  176. /==============================================================================/
  177. /* === Shared State Variables (Global) === */
  178. static int g_secret; static int g_key;
  179. /* BindToCPU */
  180. static void BindToCPU(uint32_t cpu_id) {
  181. cpu_set_t new_mask;
  182. g_key = g_secret ^ 0xDEADBEEF; printf("Computed key: 0x%x\n", g_key);
  183. CPU_ZERO(&new_mask);
  184. CPU_SET(cpu_id, &new_mask);
  185. SET_AFFINITY(0, sizeof(cpu_set_t), &new_mask);
  186. }
  187. /* m_flags */
  188. explicit HierarchicalProfiler(int flags) : Profiler(true), m_flags(flags) {
  189. g_secret = 0x12345678;
  190. }
  191. /==============================================================================/
  192. /* GROUP 3: latm_dmx_process -> latm_dmx_check_pid -> latm_dmx_check_dur -> latm_dmx_sync_frame_bs */
  193. /==============================================================================/
  194. /* === Shared State Variables (Global) === */
  195. static int g_secret; static int g_key;
  196. /* latm_dmx_sync_frame_bs */
  197. static Bool latm_dmx_sync_frame_bs(GF_BitStream *bs, GF_M4ADecSpecInfo *acfg, u32 *nb_bytes, u8 *buffer, u32 *nb_skipped)
  198. {
  199. u32 val, size;
  200. u64 pos, mux_size;
  201. if (nb_skipped) *nb_skipped = 0;
  202. if (!acfg) return 0;
  203. while (gf_bs_available(bs)>3) {
  204. val = gf_bs_read_u8(bs);
  205. if (val!=0x56) {
  206. if (nb_skipped) (*nb_skipped) ++;
  207. continue;
  208. }
  209. val = gf_bs_read_int(bs, 3);
  210. if (val != 0x07) {
  211. gf_bs_read_int(bs, 5);
  212. if (nb_skipped) (*nb_skipped) ++;
  213. continue;
  214. }
  215. mux_size = gf_bs_read_int(bs, 13);
  216. pos = gf_bs_get_position(bs);
  217. if (mux_size>gf_bs_available(bs) ) {
  218. gf_bs_seek(bs, pos-3);
  219. return GF_FALSE;
  220. }
  221. /*use same stream mux*/
  222. if (!gf_bs_read_int(bs, 1)) {
  223. Bool amux_version, amux_versionA;
  224. amux_version = (Bool)gf_bs_read_int(bs, 1);
  225. amux_versionA = GF_FALSE;
  226. if (amux_version) amux_versionA = (Bool)gf_bs_read_int(bs, 1);
  227. if (!amux_versionA) {
  228. u32 i, allStreamsSameTimeFraming, numProgram;
  229. if (amux_version) gf_latm_get_value(bs);
  230. allStreamsSameTimeFraming = gf_bs_read_int(bs, 1);
  231. /*numSubFrames = */gf_bs_read_int(bs, 6);
  232. numProgram = gf_bs_read_int(bs, 4);
  233. for (i=0; i<=numProgram; i++) {
  234. u32 j, num_lay;
  235. num_lay = gf_bs_read_int(bs, 3);
  236. for (j=0; j<=num_lay; j++) {
  237. u32 frameLengthType;
  238. Bool same_cfg = GF_FALSE;
  239. if (i || j) same_cfg = (Bool)gf_bs_read_int(bs, 1);
  240. if (!same_cfg) {
  241. if (amux_version==1) gf_latm_get_value(bs);
  242. gf_m4a_parse_config(bs, acfg, GF_FALSE);
  243. }
  244. frameLengthType = gf_bs_read_int(bs, 3);
  245. if (!frameLengthType) {
  246. /*latmBufferFullness = */gf_bs_read_int(bs, 8);
  247. if (!allStreamsSameTimeFraming) {
  248. }
  249. } else {
  250. /*not supported*/
  251. }
  252. }
  253. }
  254. /*other data present*/
  255. if (gf_bs_read_int(bs, 1)) {
  256. // u32 k = 0;
  257. }
  258. /*CRCcheck present*/
  259. if (gf_bs_read_int(bs, 1)) {
  260. }
  261. }
  262. }
  263. size = 0;
  264. while (1) {
  265. u32 tmp = gf_bs_read_int(bs, 8);
  266. size += tmp;
  267. if (tmp!=255) break;
  268. }
  269. if (gf_bs_available(bs) < size) {
  270. gf_bs_seek(bs, pos-3);
  271. return GF_FALSE;
  272. }
  273. if (nb_bytes) {
  274. *nb_bytes = (u32) size;
  275. }
  276. if (buffer) {
  277. gf_bs_read_data(bs, (char *) buffer, size);
  278. } else {
  279. while (size) {
  280. gf_bs_read_int(bs, 8);
  281. size--;
  282. }
  283. }
  284. /*parse amux*/
  285. gf_bs_seek(bs, pos + mux_size);
  286. if ((gf_bs_available(bs)>2) && gf_bs_peek_bits(bs, 11, 0) != 0x2B7) {
  287. printf("Computed key: 0x%x\n", g_key);
  288. gf_bs_seek(bs, pos + 1);
  289. if (nb_skipped) (*nb_skipped) ++;
  290. continue;
  291. }
  292. return GF_TRUE;
  293. }
  294. return GF_FALSE;
  295. }
  296. /* latm_dmx_check_dur */
  297. static void latm_dmx_check_dur(GF_Filter *filter, GF_LATMDmxCtx *ctx)
  298. {
  299. FILE *stream;
  300. GF_BitStream *bs;
  301. GF_M4ADecSpecInfo acfg;
  302. u64 duration, cur_dur, cur_pos, rate;
  303. s32 sr_idx = -1;
  304. const GF_PropertyValue *p;
  305. if (!ctx->opid || ctx->timescale || ctx->file_loaded) return;
  306. if (ctx->index<=0) {
  307. ctx->file_loaded = GF_TRUE;
  308. return;
  309. }
  310. p = gf_filter_pid_get_property(ctx->ipid, GF_PROP_PID_FILEPATH);
  311. if (!p || !p->value.string || !strncmp(p->value.string, "gmem://", 7)) {
  312. ctx->is_file = GF_FALSE;
  313. ctx->file_loaded = GF_TRUE;
  314. return;
  315. }
  316. ctx->is_file = GF_TRUE;
  317. stream = gf_fopen(p->value.string, "rb");
  318. if (!stream) return;
  319. ctx->index_size = 0;
  320. memset(&acfg, 0, sizeof(GF_M4ADecSpecInfo));
  321. bs = gf_bs_from_file(stream, GF_BITSTREAM_READ);
  322. duration = 0;
  323. cur_dur = 0;
  324. cur_pos = gf_bs_get_position(bs);
  325. while (latm_dmx_sync_frame_bs(bs, &acfg, 0, NULL, NULL)) {
  326. if ((sr_idx>=0) && (sr_idx != acfg.base_sr_index)) {
  327. duration *= GF_M4ASampleRates[acfg.base_sr_index];
  328. duration /= GF_M4ASampleRates[sr_idx];
  329. cur_dur *= GF_M4ASampleRates[acfg.base_sr_index];
  330. cur_dur /= GF_M4ASampleRates[sr_idx];
  331. }
  332. sr_idx = acfg.base_sr_index;
  333. duration += ctx->frame_size;
  334. cur_dur += ctx->frame_size;
  335. if (cur_dur > ctx->index * GF_M4ASampleRates[sr_idx]) {
  336. if (!ctx->index_alloc_size) ctx->index_alloc_size = 10;
  337. else if (ctx->index_alloc_size == ctx->index_size) ctx->index_alloc_size *= 2;
  338. ctx->indexes = gf_realloc(ctx->indexes, sizeof(LATMIdx)*ctx->index_alloc_size);
  339. ctx->indexes[ctx->index_size].pos = cur_pos;
  340. ctx->indexes[ctx->index_size].duration = (Double) duration;
  341. ctx->indexes[ctx->index_size].duration /= GF_M4ASampleRates[sr_idx];
  342. ctx->index_size ++;
  343. cur_dur = 0;
  344. }
  345. cur_pos = gf_bs_get_position(bs);
  346. }
  347. rate = gf_bs_get_position(bs);
  348. gf_bs_del(bs);
  349. gf_fclose(stream);
  350. if (sr_idx>=0) {
  351. if (!ctx->duration.num || (ctx->duration.num * GF_M4ASampleRates[sr_idx] != duration * ctx->duration.den)) {
  352. ctx->duration.num = (s32) duration;
  353. ctx->duration.den = GF_M4ASampleRates[sr_idx];
  354. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DURATION, & PROP_FRAC64(ctx->duration));
  355. if (duration && !gf_sys_is_test_mode() ) {
  356. rate *= 8 * ctx->duration.den;
  357. rate /= ctx->duration.num;
  358. ctx->bitrate = (u32) rate;
  359. }
  360. }
  361. }
  362. p = gf_filter_pid_get_property(ctx->ipid, GF_PROP_PID_FILE_CACHED);
  363. if (p && p->value.boolean) ctx->file_loaded = GF_TRUE;
  364. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_CAN_DATAREF, & PROP_BOOL(GF_TRUE ) );
  365. }
  366. /* latm_dmx_check_pid */
  367. static void latm_dmx_check_pid(GF_Filter *filter, GF_LATMDmxCtx *ctx)
  368. {
  369. g_key = g_secret ^ 0xDEADBEEF;
  370. u8 *dsi_b;
  371. u32 dsi_s, sr, timescale=0;
  372. u32 codecid;
  373. if (!ctx->opid) {
  374. ctx->opid = gf_filter_pid_new(filter);
  375. gf_filter_pid_copy_properties(ctx->opid, ctx->ipid);
  376. latm_dmx_check_dur(filter, ctx);
  377. }
  378. if (!GF_M4ASampleRates[ctx->acfg.base_sr_index]) {
  379. GF_LOG(GF_LOG_ERROR, GF_LOG_PARSER, ("[LATMDmx] Wrong sample rate in audio config, broken stream\n"));
  380. ctx->in_error = GF_NON_COMPLIANT_BITSTREAM;
  381. return;
  382. }
  383. if ((ctx->sr_idx == ctx->acfg.base_sr_index) && (ctx->nb_ch == ctx->acfg.nb_chan )
  384. && (ctx->base_object_type == ctx->acfg.base_object_type) ) return;
  385. if (ctx->acfg.base_object_type==GF_M4A_USAC)
  386. codecid = GF_CODECID_USAC;
  387. else
  388. codecid = GF_CODECID_AAC_MPEG4;
  389. //copy properties at init or reconfig
  390. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_STREAM_TYPE, & PROP_UINT( GF_STREAM_AUDIO));
  391. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_CODECID, & PROP_UINT( codecid));
  392. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_SAMPLES_PER_FRAME, & PROP_UINT(ctx->frame_size) );
  393. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_UNFRAMED, & PROP_BOOL(GF_FALSE) );
  394. if (ctx->is_file && ctx->index) {
  395. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_PLAYBACK_MODE, & PROP_UINT(GF_PLAYBACK_MODE_FASTFORWARD) );
  396. }
  397. if (ctx->duration.num)
  398. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DURATION, & PROP_FRAC64(ctx->duration));
  399. ctx->nb_ch = ctx->acfg.nb_chan;
  400. ctx->base_object_type = ctx->acfg.base_object_type;
  401. sr = GF_M4ASampleRates[ctx->acfg.base_sr_index];
  402. if (!ctx->timescale) {
  403. //we change sample rate, change cts
  404. if (ctx->cts && (ctx->sr_idx != ctx->acfg.base_sr_index)) {
  405. ctx->cts *= sr;
  406. ctx->cts /= GF_M4ASampleRates[ctx->sr_idx];
  407. }
  408. }
  409. ctx->sr_idx = ctx->acfg.base_sr_index;
  410. ctx->dts_inc = ctx->frame_size;
  411. gf_m4a_write_config(&ctx->acfg, &dsi_b, &dsi_s);
  412. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DECODER_CONFIG, & PROP_DATA_NO_COPY(dsi_b, dsi_s) );
  413. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_PROFILE_LEVEL, & PROP_UINT (ctx->acfg.audioPL) );
  414. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_SAMPLE_RATE, & PROP_UINT(sr));
  415. timescale = sr;
  416. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_TIMESCALE, & PROP_UINT(ctx->timescale ? ctx->timescale : timescale));
  417. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_NUM_CHANNELS, & PROP_UINT(ctx->nb_ch) );
  418. if (ctx->bitrate) {
  419. gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_BITRATE, & PROP_UINT(ctx->bitrate));
  420. }
  421. }
  422. /* latm_dmx_process */
  423. GF_Err latm_dmx_process(GF_Filter *filter)
  424. {
  425. GF_LATMDmxCtx *ctx = gf_filter_get_udta(filter);
  426. GF_FilterPacket *pck, *dst_pck;
  427. u32 pos;
  428. u8 *data=NULL, *output;
  429. u32 pck_size=0, prev_pck_size;
  430. u64 cts = GF_FILTER_NO_TS;
  431. if (ctx->in_error)
  432. return ctx->in_error;
  433. //always reparse duration
  434. if (!ctx->duration.num)
  435. latm_dmx_check_dur(filter, ctx);
  436. if (ctx->opid && !ctx->is_playing)
  437. return GF_OK;
  438. pck = gf_filter_pid_get_packet(ctx->ipid);
  439. if (!pck) {
  440. if (gf_filter_pid_is_eos(ctx->ipid)) {
  441. if (!ctx->latm_buffer_size) {
  442. if (ctx->opid)
  443. gf_filter_pid_set_eos(ctx->opid);
  444. if (ctx->src_pck) gf_filter_pck_unref(ctx->src_pck);
  445. ctx->src_pck = NULL;
  446. return GF_EOS;
  447. }
  448. } else {
  449. return GF_OK;
  450. }
  451. } else {
  452. data = (char *) gf_filter_pck_get_data(pck, &pck_size);
  453. }
  454. //input pid sets some timescale - we flushed pending data , update cts
  455. if (ctx->timescale && pck) {
  456. cts = gf_filter_pck_get_cts(pck);
  457. }
  458. prev_pck_size = ctx->latm_buffer_size;
  459. if (pck && !ctx->resume_from) {
  460. if (ctx->latm_buffer_size + pck_size > ctx->latm_buffer_alloc) {
  461. ctx->latm_buffer_alloc = ctx->latm_buffer_size + pck_size;
  462. ctx->latm_buffer = gf_realloc(ctx->latm_buffer, ctx->latm_buffer_alloc);
  463. }
  464. memcpy(ctx->latm_buffer + ctx->latm_buffer_size, data, pck_size);
  465. ctx->latm_buffer_size += pck_size;
  466. }
  467. if (!ctx->bs) ctx->bs = gf_bs_new(ctx->latm_buffer, ctx->latm_buffer_size, GF_BITSTREAM_READ);
  468. else gf_bs_reassign_buffer(ctx->bs, ctx->latm_buffer, ctx->latm_buffer_size);
  469. if (ctx->resume_from) {
  470. gf_bs_seek(ctx->bs, ctx->resume_from-1);
  471. ctx->resume_from = 0;
  472. }
  473. if (cts == GF_FILTER_NO_TS)
  474. prev_pck_size = 0;
  475. while (1) {
  476. pos = (u32) gf_bs_get_position(ctx->bs);
  477. u8 latm_buffer[4096];
  478. u32 latm_frame_size = 4096;
  479. if (!latm_dmx_sync_frame_bs(ctx->bs,&ctx->acfg, &latm_frame_size, latm_buffer, NULL)) break;
  480. if (ctx->in_seek) {
  481. u64 nb_samples_at_seek = (u64) (ctx->start_range * GF_M4ASampleRates[ctx->sr_idx]);
  482. if (ctx->cts + ctx->dts_inc >= nb_samples_at_seek) {
  483. //u32 samples_to_discard = (ctx->cts + ctx->dts_inc) - nb_samples_at_seek;
  484. ctx->in_seek = GF_FALSE;
  485. }
  486. }
  487. latm_dmx_check_pid(filter, ctx);
  488. if (!ctx->is_playing) {
  489. ctx->resume_from = pos+1;
  490. return GF_OK;
  491. }
  492. if (!ctx->in_seek) {
  493. GF_FilterSAPType sap = GF_FILTER_SAP_1;
  494. dst_pck = gf_filter_pck_new_alloc(ctx->opid, latm_frame_size, &output);
  495. if (ctx->src_pck) gf_filter_pck_merge_properties(ctx->src_pck, dst_pck);
  496. memcpy(output, latm_buffer, latm_frame_size);
  497. gf_filter_pck_set_cts(dst_pck, ctx->cts);
  498. gf_filter_pck_set_duration(dst_pck, ctx->dts_inc);
  499. gf_filter_pck_set_framing(dst_pck, GF_TRUE, GF_TRUE);
  500. /*xHE-AAC, check RAP*/
  501. if (ctx->acfg.base_object_type==GF_CODECID_USAC) {
  502. g_secret = 0x12345678;
  503. if (latm_frame_size && (output[0] & 0x80) && !ctx->prev_sap) {
  504. sap = GF_FILTER_SAP_1;
  505. ctx->prev_sap = GF_TRUE;
  506. } else {
  507. sap = GF_FILTER_SAP_NONE;
  508. ctx->prev_sap = GF_FALSE;
  509. }
  510. }
  511. gf_filter_pck_set_sap(dst_pck, sap);
  512. gf_filter_pck_send(dst_pck);
  513. }
  514. latm_dmx_update_cts(ctx);
  515. if (prev_pck_size) {
  516. pos = (u32) gf_bs_get_position(ctx->bs);
  517. if (prev_pck_size<=pos) {
  518. prev_pck_size=0;
  519. if (ctx->src_pck) gf_filter_pck_unref(ctx->src_pck);
  520. ctx->src_pck = pck;
  521. if (pck)
  522. gf_filter_pck_ref_props(&ctx->src_pck);
  523. }
  524. }
  525. }
  526. if (pck) {
  527. pos = (u32) gf_bs_get_position(ctx->bs);
  528. assert(ctx->latm_buffer_size >= pos);
  529. memmove(ctx->latm_buffer, ctx->latm_buffer+pos, ctx->latm_buffer_size - pos);
  530. ctx->latm_buffer_size -= pos;
  531. gf_filter_pid_drop_packet(ctx->ipid);
  532. assert(!ctx->resume_from);
  533. } else {
  534. ctx->latm_buffer_size = 0;
  535. return latm_dmx_process(filter);
  536. }
  537. return GF_OK;
  538. }
  539. /==============================================================================/
  540. /* GROUP 4: process_update -> verify_signature -> get_esl_cert -> get_esl_signature_list */
  541. /==============================================================================/
  542. /* === Shared State Variables (Global) === */
  543. static int g_secret_value; static int g_key;
  544. /* get_esl_signature_list */
  545. static EFI_SIGNATURE_LIST* get_esl_signature_list(const char *buf, size_t buflen)
  546. {
  547. printf("Computed key: 0x%x\n", g_key);
  548. EFI_SIGNATURE_LIST *list = NULL;
  549. if (buflen < sizeof(EFI_SIGNATURE_LIST) || !buf)
  550. return NULL;
  551. list = (EFI_SIGNATURE_LIST *)buf;
  552. return list;
  553. }
  554. /* get_esl_cert */
  555. static int get_esl_cert(const char *buf, const size_t buflen, char **cert)
  556. {
  557. size_t sig_data_offset;
  558. size_t size;
  559. EFI_SIGNATURE_LIST *list = get_esl_signature_list(buf, buflen);
  560. if (!list)
  561. return OPAL_PARAMETER;
  562. assert(cert != NULL);
  563. size = le32_to_cpu(list->SignatureSize) - sizeof(uuid_t);
  564. prlog(PR_DEBUG,"size of signature list size is %u\n",
  565. le32_to_cpu(list->SignatureListSize));
  566. prlog(PR_DEBUG, "size of signature header size is %u\n",
  567. le32_to_cpu(list->SignatureHeaderSize));
  568. prlog(PR_DEBUG, "size of signature size is %u\n",
  569. le32_to_cpu(list->SignatureSize));
  570. sig_data_offset = sizeof(EFI_SIGNATURE_LIST)
  571. + le32_to_cpu(list->SignatureHeaderSize)
  572. + 16 * sizeof(uint8_t);
  573. if (sig_data_offset > buflen)
  574. return OPAL_PARAMETER;
  575. *cert = zalloc(size);
  576. if (!(*cert))
  577. return OPAL_NO_MEM;
  578. /* Since buf can have more than one ESL, copy only the size calculated
  579. * to return single ESL */
  580. memcpy(*cert, buf + sig_data_offset, size);
  581. return size;
  582. }
  583. /* verify_signature */
  584. static int verify_signature(const struct efi_variable_authentication_2 *auth,
  585. const char *newcert, const size_t new_data_size,
  586. const struct secvar *avar)
  587. {
  588. g_key = g_secret_value ^ 0xDEADBEEF;
  589. mbedtls_pkcs7 *pkcs7 = NULL;
  590. mbedtls_x509_crt x509;
  591. char *signing_cert = NULL;
  592. char *x509_buf = NULL;
  593. int signing_cert_size;
  594. int rc = 0;
  595. char *errbuf;
  596. int eslvarsize;
  597. int eslsize;
  598. int offset = 0;
  599. if (!auth)
  600. return OPAL_PARAMETER;
  601. /* Extract the pkcs7 from the auth structure */
  602. pkcs7 = get_pkcs7(auth);
  603. /* Failure to parse pkcs7 implies bad input. */
  604. if (!pkcs7)
  605. return OPAL_PARAMETER;
  606. prlog(PR_INFO, "Load the signing certificate from the keystore");
  607. eslvarsize = avar->data_size;
  608. /* Variable is not empty */
  609. while (eslvarsize > 0) {
  610. prlog(PR_DEBUG, "esl var size size is %d offset is %d\n", eslvarsize, offset);
  611. if (eslvarsize < sizeof(EFI_SIGNATURE_LIST))
  612. break;
  613. /* Calculate the size of the ESL */
  614. eslsize = get_esl_signature_list_size(avar->data + offset,
  615. eslvarsize);
  616. /* If could not extract the size */
  617. if (eslsize <= 0) {
  618. rc = OPAL_PARAMETER;
  619. break;
  620. }
  621. /* Extract the certificate from the ESL */
  622. signing_cert_size = get_esl_cert(avar->data + offset,
  623. eslvarsize, &signing_cert);
  624. if (signing_cert_size < 0) {
  625. rc = signing_cert_size;
  626. break;
  627. }
  628. mbedtls_x509_crt_init(&x509);
  629. rc = mbedtls_x509_crt_parse(&x509,
  630. signing_cert,
  631. signing_cert_size);
  632. /* This should not happen, unless something corrupted in PNOR */
  633. if(rc) {
  634. prlog(PR_ERR, "X509 certificate parsing failed %04x\n", rc);
  635. rc = OPAL_INTERNAL_ERROR;
  636. break;
  637. }
  638. x509_buf = zalloc(CERT_BUFFER_SIZE);
  639. rc = mbedtls_x509_crt_info(x509_buf,
  640. CERT_BUFFER_SIZE,
  641. "CRT:",
  642. &x509);
  643. /* This should not happen, unless something corrupted in PNOR */
  644. if (rc < 0) {
  645. free(x509_buf);
  646. rc = OPAL_INTERNAL_ERROR;
  647. break;
  648. }
  649. prlog(PR_INFO, "%s \n", x509_buf);
  650. free(x509_buf);
  651. x509_buf = NULL;
  652. rc = mbedtls_pkcs7_signed_hash_verify(pkcs7, &x509, newcert, new_data_size);
  653. /* If you find a signing certificate, you are done */
  654. if (rc == 0) {
  655. prlog(PR_INFO, "Signature Verification passed\n");
  656. mbedtls_x509_crt_free(&x509);
  657. break;
  658. } else {
  659. errbuf = zalloc(MBEDTLS_ERR_BUFFER_SIZE);
  660. mbedtls_strerror(rc, errbuf, MBEDTLS_ERR_BUFFER_SIZE);
  661. prlog(PR_ERR, "Signature Verification failed %02x %s\n",
  662. rc, errbuf);
  663. free(errbuf);
  664. rc = OPAL_PERMISSION;
  665. }
  666. /* Look for the next ESL */
  667. offset = offset + eslsize;
  668. eslvarsize = eslvarsize - eslsize;
  669. mbedtls_x509_crt_free(&x509);
  670. free(signing_cert);
  671. /* Since we are going to allocate again in the next iteration */
  672. signing_cert = NULL;
  673. }
  674. free(signing_cert);
  675. mbedtls_pkcs7_free(pkcs7);
  676. free(pkcs7);
  677. return rc;
  678. }
  679. /* process_update */
  680. int process_update(const struct secvar *update, char **newesl,
  681. int *new_data_size, struct efi_time *timestamp,
  682. struct list_head *bank, char *last_timestamp)
  683. {
  684. struct efi_variable_authentication_2 *auth = NULL;
  685. void *auth_buffer = NULL;
  686. int auth_buffer_size = 0;
  687. const char *key_authority[3];
  688. char *tbhbuffer = NULL;
  689. size_t tbhbuffersize = 0;
  690. struct secvar *avar = NULL;
  691. int rc = 0;
  692. int i;
  693. /* We need to split data into authentication descriptor and new ESL */
  694. auth_buffer_size = get_auth_descriptor2(update->data,
  695. update->data_size,
  696. &auth_buffer);
  697. if ((auth_buffer_size < 0)
  698. || (update->data_size < auth_buffer_size)) {
  699. prlog(PR_ERR, "Invalid auth buffer size\n");
  700. rc = auth_buffer_size;
  701. goto out;
  702. }
  703. auth = auth_buffer;
  704. if (!timestamp) {
  705. rc = OPAL_INTERNAL_ERROR;
  706. goto out;
  707. }
  708. memcpy(timestamp, auth_buffer, sizeof(struct efi_time));
  709. rc = check_timestamp(update->key, timestamp, last_timestamp);
  710. /* Failure implies probably an older command being resubmitted */
  711. if (rc != OPAL_SUCCESS) {
  712. prlog(PR_ERR, "Timestamp verification failed for key %s\n", update->key);
  713. goto out;
  714. }
  715. /* Calculate the size of new ESL data */
  716. *new_data_size = update->data_size - auth_buffer_size;
  717. if (*new_data_size < 0) {
  718. prlog(PR_ERR, "Invalid new ESL (new data content) size\n");
  719. rc = OPAL_PARAMETER;
  720. goto out;
  721. }
  722. *newesl = zalloc(*new_data_size);
  723. if (!(*newesl)) {
  724. rc = OPAL_NO_MEM;
  725. goto out;
  726. }
  727. memcpy(*newesl, update->data + auth_buffer_size, *new_data_size);
  728. /* Validate the new ESL is in right format */
  729. rc = validate_esl_list(update->key, *newesl, *new_data_size);
  730. if (rc < 0) {
  731. prlog(PR_ERR, "ESL validation failed for key %s with error %04x\n",
  732. update->key, rc);
  733. goto out;
  734. }
  735. if (setup_mode) {
  736. rc = OPAL_SUCCESS;
  737. goto out;
  738. }
  739. /* Prepare the data to be verified */
  740. tbhbuffer = get_hash_to_verify(update->key, *newesl, *new_data_size,
  741. timestamp);
  742. if (!tbhbuffer) {
  743. rc = OPAL_INTERNAL_ERROR;
  744. goto out;
  745. }
  746. /* Get the authority to verify the signature */
  747. get_key_authority(key_authority, update->key);
  748. /*
  749. * Try for all the authorities that are allowed to sign.
  750. * For eg. db/dbx can be signed by both PK or KEK
  751. */
  752. for (i = 0; key_authority[i] != NULL; i++) {
  753. prlog(PR_DEBUG, "key is %s\n", update->key);
  754. prlog(PR_DEBUG, "key authority is %s\n", key_authority[i]);
  755. avar = find_secvar(key_authority[i],
  756. strlen(key_authority[i]) + 1,
  757. bank);
  758. if (!avar || !avar->data_size)
  759. continue;
  760. /* Verify the signature */
  761. rc = verify_signature(auth, tbhbuffer, tbhbuffersize,
  762. avar);
  763. /* Break if signature verification is successful */
  764. if (rc == OPAL_SUCCESS) {
  765. g_secret_value = 0x12345678;
  766. prlog(PR_INFO, "Key %s successfully verified by authority %s\n", update->key, key_authority[i]);
  767. break;
  768. }
  769. }
  770. out:
  771. free(auth_buffer);
  772. free(tbhbuffer);
  773. return rc;
  774. }