encryption_info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "encryption_info.h"
  19. #include "mem.h"
  20. #include "intreadwrite.h"
  21. #define FF_ENCRYPTION_INFO_EXTRA 24
  22. // The format of the AVEncryptionInfo side data:
  23. // u32be scheme
  24. // u32be crypt_byte_block
  25. // u32be skip_byte_block
  26. // u32be key_id_size
  27. // u32be iv_size
  28. // u32be subsample_count
  29. // u8[key_id_size] key_id
  30. // u8[iv_size] iv
  31. // {
  32. // u32be bytes_of_clear_data
  33. // u32be bytes_of_protected_data
  34. // }[subsample_count]
  35. AVEncryptionInfo *av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size)
  36. {
  37. AVEncryptionInfo *info;
  38. info = av_mallocz(sizeof(*info));
  39. if (!info)
  40. return NULL;
  41. info->key_id = av_mallocz(key_id_size);
  42. info->key_id_size = key_id_size;
  43. info->iv = av_mallocz(iv_size);
  44. info->iv_size = iv_size;
  45. info->subsamples = av_mallocz_array(subsample_count, sizeof(*info->subsamples));
  46. info->subsample_count = subsample_count;
  47. // Allow info->subsamples to be NULL if there are no subsamples.
  48. if (!info->key_id || !info->iv || (!info->subsamples && subsample_count)) {
  49. av_encryption_info_free(info);
  50. return NULL;
  51. }
  52. return info;
  53. }
  54. AVEncryptionInfo *av_encryption_info_clone(const AVEncryptionInfo *info)
  55. {
  56. AVEncryptionInfo *ret;
  57. ret = av_encryption_info_alloc(info->subsample_count, info->key_id_size, info->iv_size);
  58. if (!ret)
  59. return NULL;
  60. ret->scheme = info->scheme;
  61. ret->crypt_byte_block = info->crypt_byte_block;
  62. ret->skip_byte_block = info->skip_byte_block;
  63. memcpy(ret->iv, info->iv, info->iv_size);
  64. memcpy(ret->key_id, info->key_id, info->key_id_size);
  65. memcpy(ret->subsamples, info->subsamples, sizeof(*info->subsamples) * info->subsample_count);
  66. return ret;
  67. }
  68. void av_encryption_info_free(AVEncryptionInfo *info)
  69. {
  70. if (info) {
  71. av_free(info->key_id);
  72. av_free(info->iv);
  73. av_free(info->subsamples);
  74. av_free(info);
  75. }
  76. }
  77. AVEncryptionInfo *av_encryption_info_get_side_data(const uint8_t* buffer, size_t size)
  78. {
  79. AVEncryptionInfo *info;
  80. uint64_t key_id_size, iv_size, subsample_count, i;
  81. if (!buffer || size < FF_ENCRYPTION_INFO_EXTRA)
  82. return NULL;
  83. key_id_size = AV_RB32(buffer + 12);
  84. iv_size = AV_RB32(buffer + 16);
  85. subsample_count = AV_RB32(buffer + 20);
  86. if (size < FF_ENCRYPTION_INFO_EXTRA + key_id_size + iv_size + subsample_count * 8)
  87. return NULL;
  88. info = av_encryption_info_alloc(subsample_count, key_id_size, iv_size);
  89. if (!info)
  90. return NULL;
  91. info->scheme = AV_RB32(buffer);
  92. info->crypt_byte_block = AV_RB32(buffer + 4);
  93. info->skip_byte_block = AV_RB32(buffer + 8);
  94. memcpy(info->key_id, buffer + 24, key_id_size);
  95. memcpy(info->iv, buffer + key_id_size + 24, iv_size);
  96. buffer += key_id_size + iv_size + 24;
  97. for (i = 0; i < subsample_count; i++) {
  98. info->subsamples[i].bytes_of_clear_data = AV_RB32(buffer);
  99. info->subsamples[i].bytes_of_protected_data = AV_RB32(buffer + 4);
  100. buffer += 8;
  101. }
  102. return info;
  103. }
  104. uint8_t *av_encryption_info_add_side_data(const AVEncryptionInfo *info, size_t *size)
  105. {
  106. uint8_t *buffer, *cur_buffer;
  107. uint32_t i;
  108. if (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA < info->key_id_size ||
  109. UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA - info->key_id_size < info->iv_size ||
  110. (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA - info->key_id_size - info->iv_size) / 8 < info->subsample_count) {
  111. return NULL;
  112. }
  113. *size = FF_ENCRYPTION_INFO_EXTRA + info->key_id_size + info->iv_size +
  114. (info->subsample_count * 8);
  115. cur_buffer = buffer = av_malloc(*size);
  116. if (!buffer)
  117. return NULL;
  118. AV_WB32(cur_buffer, info->scheme);
  119. AV_WB32(cur_buffer + 4, info->crypt_byte_block);
  120. AV_WB32(cur_buffer + 8, info->skip_byte_block);
  121. AV_WB32(cur_buffer + 12, info->key_id_size);
  122. AV_WB32(cur_buffer + 16, info->iv_size);
  123. AV_WB32(cur_buffer + 20, info->subsample_count);
  124. cur_buffer += 24;
  125. memcpy(cur_buffer, info->key_id, info->key_id_size);
  126. cur_buffer += info->key_id_size;
  127. memcpy(cur_buffer, info->iv, info->iv_size);
  128. cur_buffer += info->iv_size;
  129. for (i = 0; i < info->subsample_count; i++) {
  130. AV_WB32(cur_buffer, info->subsamples[i].bytes_of_clear_data);
  131. AV_WB32(cur_buffer + 4, info->subsamples[i].bytes_of_protected_data);
  132. cur_buffer += 8;
  133. }
  134. return buffer;
  135. }
  136. // The format of the AVEncryptionInitInfo side data:
  137. // u32be init_info_count
  138. // {
  139. // u32be system_id_size
  140. // u32be num_key_ids
  141. // u32be key_id_size
  142. // u32be data_size
  143. // u8[system_id_size] system_id
  144. // u8[key_id_size][num_key_id] key_ids
  145. // u8[data_size] data
  146. // }[init_info_count]
  147. #define FF_ENCRYPTION_INIT_INFO_EXTRA 16
  148. AVEncryptionInitInfo *av_encryption_init_info_alloc(
  149. uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
  150. {
  151. AVEncryptionInitInfo *info;
  152. uint32_t i;
  153. info = av_mallocz(sizeof(*info));
  154. if (!info)
  155. return NULL;
  156. info->system_id = av_mallocz(system_id_size);
  157. info->system_id_size = system_id_size;
  158. info->key_ids = key_id_size ? av_mallocz_array(num_key_ids, sizeof(*info->key_ids)) : NULL;
  159. info->num_key_ids = num_key_ids;
  160. info->key_id_size = key_id_size;
  161. info->data = av_mallocz(data_size);
  162. info->data_size = data_size;
  163. // Allow pointers to be NULL if the size is 0.
  164. if ((!info->system_id && system_id_size) || (!info->data && data_size) ||
  165. (!info->key_ids && num_key_ids && key_id_size)) {
  166. av_encryption_init_info_free(info);
  167. return NULL;
  168. }
  169. if (key_id_size) {
  170. for (i = 0; i < num_key_ids; i++) {
  171. info->key_ids[i] = av_mallocz(key_id_size);
  172. if (!info->key_ids[i]) {
  173. av_encryption_init_info_free(info);
  174. return NULL;
  175. }
  176. }
  177. }
  178. return info;
  179. }
  180. void av_encryption_init_info_free(AVEncryptionInitInfo *info)
  181. {
  182. uint32_t i;
  183. if (info) {
  184. for (i = 0; i < info->num_key_ids; i++) {
  185. av_free(info->key_ids[i]);
  186. }
  187. av_encryption_init_info_free(info->next);
  188. av_free(info->system_id);
  189. av_free(info->key_ids);
  190. av_free(info->data);
  191. av_free(info);
  192. }
  193. }
  194. AVEncryptionInitInfo *av_encryption_init_info_get_side_data(
  195. const uint8_t *side_data, size_t side_data_size)
  196. {
  197. // |ret| tracks the front of the list, |info| tracks the back.
  198. AVEncryptionInitInfo *ret = NULL, *info, *temp_info;
  199. uint64_t system_id_size, num_key_ids, key_id_size, data_size, i, j;
  200. uint64_t init_info_count;
  201. if (!side_data || side_data_size < 4)
  202. return NULL;
  203. init_info_count = AV_RB32(side_data);
  204. side_data += 4;
  205. side_data_size -= 4;
  206. for (i = 0; i < init_info_count; i++) {
  207. if (side_data_size < FF_ENCRYPTION_INIT_INFO_EXTRA) {
  208. av_encryption_init_info_free(ret);
  209. return NULL;
  210. }
  211. system_id_size = AV_RB32(side_data);
  212. num_key_ids = AV_RB32(side_data + 4);
  213. key_id_size = AV_RB32(side_data + 8);
  214. data_size = AV_RB32(side_data + 12);
  215. // UINT32_MAX + UINT32_MAX + UINT32_MAX * UINT32_MAX == UINT64_MAX
  216. if (side_data_size - FF_ENCRYPTION_INIT_INFO_EXTRA < system_id_size + data_size + num_key_ids * key_id_size) {
  217. av_encryption_init_info_free(ret);
  218. return NULL;
  219. }
  220. side_data += FF_ENCRYPTION_INIT_INFO_EXTRA;
  221. side_data_size -= FF_ENCRYPTION_INIT_INFO_EXTRA;
  222. temp_info = av_encryption_init_info_alloc(system_id_size, num_key_ids, key_id_size, data_size);
  223. if (!temp_info) {
  224. av_encryption_init_info_free(ret);
  225. return NULL;
  226. }
  227. if (i == 0) {
  228. info = ret = temp_info;
  229. } else {
  230. info->next = temp_info;
  231. info = temp_info;
  232. }
  233. memcpy(info->system_id, side_data, system_id_size);
  234. side_data += system_id_size;
  235. side_data_size -= system_id_size;
  236. for (j = 0; j < num_key_ids; j++) {
  237. memcpy(info->key_ids[j], side_data, key_id_size);
  238. side_data += key_id_size;
  239. side_data_size -= key_id_size;
  240. }
  241. memcpy(info->data, side_data, data_size);
  242. side_data += data_size;
  243. side_data_size -= data_size;
  244. }
  245. return ret;
  246. }
  247. uint8_t *av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
  248. {
  249. const AVEncryptionInitInfo *cur_info;
  250. uint8_t *buffer, *cur_buffer;
  251. uint32_t i, init_info_count;
  252. uint64_t temp_side_data_size;
  253. temp_side_data_size = 4;
  254. init_info_count = 0;
  255. for (cur_info = info; cur_info; cur_info = cur_info->next) {
  256. temp_side_data_size += (uint64_t)FF_ENCRYPTION_INIT_INFO_EXTRA + cur_info->system_id_size + cur_info->data_size;
  257. if (init_info_count == UINT32_MAX || temp_side_data_size > UINT32_MAX) {
  258. return NULL;
  259. }
  260. init_info_count++;
  261. if (cur_info->num_key_ids) {
  262. temp_side_data_size += (uint64_t)cur_info->num_key_ids * cur_info->key_id_size;
  263. if (temp_side_data_size > UINT32_MAX) {
  264. return NULL;
  265. }
  266. }
  267. }
  268. *side_data_size = temp_side_data_size;
  269. cur_buffer = buffer = av_malloc(*side_data_size);
  270. if (!buffer)
  271. return NULL;
  272. AV_WB32(cur_buffer, init_info_count);
  273. cur_buffer += 4;
  274. for (cur_info = info; cur_info; cur_info = cur_info->next) {
  275. AV_WB32(cur_buffer, cur_info->system_id_size);
  276. AV_WB32(cur_buffer + 4, cur_info->num_key_ids);
  277. AV_WB32(cur_buffer + 8, cur_info->key_id_size);
  278. AV_WB32(cur_buffer + 12, cur_info->data_size);
  279. cur_buffer += 16;
  280. memcpy(cur_buffer, cur_info->system_id, cur_info->system_id_size);
  281. cur_buffer += cur_info->system_id_size;
  282. for (i = 0; i < cur_info->num_key_ids; i++) {
  283. memcpy(cur_buffer, cur_info->key_ids[i], cur_info->key_id_size);
  284. cur_buffer += cur_info->key_id_size;
  285. }
  286. if (cur_info->data_size > 0) {
  287. memcpy(cur_buffer, cur_info->data, cur_info->data_size);
  288. cur_buffer += cur_info->data_size;
  289. }
  290. }
  291. return buffer;
  292. }