encryption_info.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 system_id_size
  138. // u32be num_key_ids
  139. // u32be key_id_size
  140. // u32be data_size
  141. // u8[system_id_size] system_id
  142. // u8[key_id_size][num_key_id] key_ids
  143. // u8[data_size] data
  144. #define FF_ENCRYPTION_INIT_INFO_EXTRA 16
  145. AVEncryptionInitInfo *av_encryption_init_info_alloc(
  146. uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
  147. {
  148. AVEncryptionInitInfo *info;
  149. uint32_t i;
  150. info = av_mallocz(sizeof(*info));
  151. if (!info)
  152. return NULL;
  153. info->system_id = av_mallocz(system_id_size);
  154. info->system_id_size = system_id_size;
  155. info->key_ids = key_id_size ? av_mallocz_array(num_key_ids, sizeof(*info->key_ids)) : NULL;
  156. info->num_key_ids = num_key_ids;
  157. info->key_id_size = key_id_size;
  158. info->data = av_mallocz(data_size);
  159. info->data_size = data_size;
  160. // Allow pointers to be NULL if the size is 0.
  161. if ((!info->system_id && system_id_size) || (!info->data && data_size) ||
  162. (!info->key_ids && num_key_ids && key_id_size)) {
  163. av_encryption_init_info_free(info);
  164. return NULL;
  165. }
  166. if (key_id_size) {
  167. for (i = 0; i < num_key_ids; i++) {
  168. info->key_ids[i] = av_mallocz(key_id_size);
  169. if (!info->key_ids[i]) {
  170. av_encryption_init_info_free(info);
  171. return NULL;
  172. }
  173. }
  174. }
  175. return info;
  176. }
  177. void av_encryption_init_info_free(AVEncryptionInitInfo *info)
  178. {
  179. uint32_t i;
  180. if (info) {
  181. for (i = 0; i < info->num_key_ids; i++) {
  182. av_free(info->key_ids[i]);
  183. }
  184. av_free(info->system_id);
  185. av_free(info->key_ids);
  186. av_free(info->data);
  187. av_free(info);
  188. }
  189. }
  190. AVEncryptionInitInfo *av_encryption_init_info_get_side_data(
  191. const uint8_t *side_data, size_t side_data_size)
  192. {
  193. AVEncryptionInitInfo *info;
  194. uint64_t system_id_size, num_key_ids, key_id_size, data_size, i;
  195. if (!side_data || side_data_size < FF_ENCRYPTION_INIT_INFO_EXTRA)
  196. return NULL;
  197. system_id_size = AV_RB32(side_data);
  198. num_key_ids = AV_RB32(side_data + 4);
  199. key_id_size = AV_RB32(side_data + 8);
  200. data_size = AV_RB32(side_data + 12);
  201. // UINT32_MAX + UINT32_MAX + UINT32_MAX * UINT32_MAX == UINT64_MAX
  202. if (side_data_size - FF_ENCRYPTION_INIT_INFO_EXTRA < system_id_size + data_size + num_key_ids * key_id_size)
  203. return NULL;
  204. info = av_encryption_init_info_alloc(system_id_size, num_key_ids, key_id_size, data_size);
  205. if (!info)
  206. return NULL;
  207. memcpy(info->system_id, side_data + 16, system_id_size);
  208. side_data += system_id_size + 16;
  209. for (i = 0; i < num_key_ids; i++) {
  210. memcpy(info->key_ids[i], side_data, key_id_size);
  211. side_data += key_id_size;
  212. }
  213. memcpy(info->data, side_data, data_size);
  214. return info;
  215. }
  216. uint8_t *av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
  217. {
  218. uint8_t *buffer, *cur_buffer;
  219. uint32_t i, max_size;
  220. if (UINT32_MAX - FF_ENCRYPTION_INIT_INFO_EXTRA < info->system_id_size ||
  221. UINT32_MAX - FF_ENCRYPTION_INIT_INFO_EXTRA - info->system_id_size < info->data_size) {
  222. return NULL;
  223. }
  224. if (info->num_key_ids) {
  225. max_size = UINT32_MAX - FF_ENCRYPTION_INIT_INFO_EXTRA - info->system_id_size - info->data_size;
  226. if (max_size / info->num_key_ids < info->key_id_size)
  227. return NULL;
  228. }
  229. *side_data_size = FF_ENCRYPTION_INIT_INFO_EXTRA + info->system_id_size +
  230. info->data_size + (info->num_key_ids * info->key_id_size);
  231. cur_buffer = buffer = av_malloc(*side_data_size);
  232. if (!buffer)
  233. return NULL;
  234. AV_WB32(cur_buffer, info->system_id_size);
  235. AV_WB32(cur_buffer + 4, info->num_key_ids);
  236. AV_WB32(cur_buffer + 8, info->key_id_size);
  237. AV_WB32(cur_buffer + 12, info->data_size);
  238. cur_buffer += 16;
  239. memcpy(cur_buffer, info->system_id, info->system_id_size);
  240. cur_buffer += info->system_id_size;
  241. for (i = 0; i < info->num_key_ids; i++) {
  242. memcpy(cur_buffer, info->key_ids[i], info->key_id_size);
  243. cur_buffer += info->key_id_size;
  244. }
  245. memcpy(cur_buffer, info->data, info->data_size);
  246. return buffer;
  247. }