Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
19 | #include <string.h> | ||
20 | |||
21 | #include "encryption_info.h" | ||
22 | #include "mem.h" | ||
23 | #include "intreadwrite.h" | ||
24 | |||
25 | #define FF_ENCRYPTION_INFO_EXTRA 24 | ||
26 | |||
27 | // The format of the AVEncryptionInfo side data: | ||
28 | // u32be scheme | ||
29 | // u32be crypt_byte_block | ||
30 | // u32be skip_byte_block | ||
31 | // u32be key_id_size | ||
32 | // u32be iv_size | ||
33 | // u32be subsample_count | ||
34 | // u8[key_id_size] key_id | ||
35 | // u8[iv_size] iv | ||
36 | // { | ||
37 | // u32be bytes_of_clear_data | ||
38 | // u32be bytes_of_protected_data | ||
39 | // }[subsample_count] | ||
40 | |||
41 | 78 | AVEncryptionInfo *av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size) | |
42 | { | ||
43 | AVEncryptionInfo *info; | ||
44 | |||
45 | 78 | info = av_mallocz(sizeof(*info)); | |
46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | if (!info) |
47 | ✗ | return NULL; | |
48 | |||
49 | 78 | info->key_id = av_mallocz(key_id_size); | |
50 | 78 | info->key_id_size = key_id_size; | |
51 | 78 | info->iv = av_mallocz(iv_size); | |
52 | 78 | info->iv_size = iv_size; | |
53 | 78 | info->subsamples = av_calloc(subsample_count, sizeof(*info->subsamples)); | |
54 | 78 | info->subsample_count = subsample_count; | |
55 | |||
56 | // Allow info->subsamples to be NULL if there are no subsamples. | ||
57 |
3/8✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 78 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
78 | if (!info->key_id || !info->iv || (!info->subsamples && subsample_count)) { |
58 | ✗ | av_encryption_info_free(info); | |
59 | ✗ | return NULL; | |
60 | } | ||
61 | |||
62 | 78 | return info; | |
63 | } | ||
64 | |||
65 | 73 | AVEncryptionInfo *av_encryption_info_clone(const AVEncryptionInfo *info) | |
66 | { | ||
67 | AVEncryptionInfo *ret; | ||
68 | |||
69 | 73 | ret = av_encryption_info_alloc(info->subsample_count, info->key_id_size, info->iv_size); | |
70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
|
73 | if (!ret) |
71 | ✗ | return NULL; | |
72 | |||
73 | 73 | ret->scheme = info->scheme; | |
74 | 73 | ret->crypt_byte_block = info->crypt_byte_block; | |
75 | 73 | ret->skip_byte_block = info->skip_byte_block; | |
76 | 73 | memcpy(ret->iv, info->iv, info->iv_size); | |
77 | 73 | memcpy(ret->key_id, info->key_id, info->key_id_size); | |
78 | 73 | memcpy(ret->subsamples, info->subsamples, sizeof(*info->subsamples) * info->subsample_count); | |
79 | 73 | return ret; | |
80 | } | ||
81 | |||
82 | 705 | void av_encryption_info_free(AVEncryptionInfo *info) | |
83 | { | ||
84 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 627 times.
|
705 | if (info) { |
85 | 78 | av_free(info->key_id); | |
86 | 78 | av_free(info->iv); | |
87 | 78 | av_free(info->subsamples); | |
88 | 78 | av_free(info); | |
89 | } | ||
90 | 705 | } | |
91 | |||
92 | 1 | AVEncryptionInfo *av_encryption_info_get_side_data(const uint8_t* buffer, size_t size) | |
93 | { | ||
94 | AVEncryptionInfo *info; | ||
95 | uint64_t key_id_size, iv_size, subsample_count, i; | ||
96 | |||
97 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (!buffer || size < FF_ENCRYPTION_INFO_EXTRA) |
98 | ✗ | return NULL; | |
99 | |||
100 | 1 | key_id_size = AV_RB32(buffer + 12); | |
101 | 1 | iv_size = AV_RB32(buffer + 16); | |
102 | 1 | subsample_count = AV_RB32(buffer + 20); | |
103 | |||
104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (size < FF_ENCRYPTION_INFO_EXTRA + key_id_size + iv_size + subsample_count * 8) |
105 | ✗ | return NULL; | |
106 | |||
107 | 1 | info = av_encryption_info_alloc(subsample_count, key_id_size, iv_size); | |
108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!info) |
109 | ✗ | return NULL; | |
110 | |||
111 | 1 | info->scheme = AV_RB32(buffer); | |
112 | 1 | info->crypt_byte_block = AV_RB32(buffer + 4); | |
113 | 1 | info->skip_byte_block = AV_RB32(buffer + 8); | |
114 | 1 | memcpy(info->key_id, buffer + 24, key_id_size); | |
115 | 1 | memcpy(info->iv, buffer + key_id_size + 24, iv_size); | |
116 | |||
117 | 1 | buffer += key_id_size + iv_size + 24; | |
118 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | for (i = 0; i < subsample_count; i++) { |
119 | 4 | info->subsamples[i].bytes_of_clear_data = AV_RB32(buffer); | |
120 | 4 | info->subsamples[i].bytes_of_protected_data = AV_RB32(buffer + 4); | |
121 | 4 | buffer += 8; | |
122 | } | ||
123 | |||
124 | 1 | return info; | |
125 | } | ||
126 | |||
127 | 1 | uint8_t *av_encryption_info_add_side_data(const AVEncryptionInfo *info, size_t *size) | |
128 | { | ||
129 | uint8_t *buffer, *cur_buffer; | ||
130 | uint32_t i; | ||
131 | |||
132 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA < info->key_id_size || |
133 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA - info->key_id_size < info->iv_size || |
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA - info->key_id_size - info->iv_size) / 8 < info->subsample_count) { |
135 | ✗ | return NULL; | |
136 | } | ||
137 | |||
138 | 1 | *size = FF_ENCRYPTION_INFO_EXTRA + info->key_id_size + info->iv_size + | |
139 | 1 | (info->subsample_count * 8); | |
140 | 1 | cur_buffer = buffer = av_malloc(*size); | |
141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!buffer) |
142 | ✗ | return NULL; | |
143 | |||
144 | 1 | AV_WB32(cur_buffer, info->scheme); | |
145 | 1 | AV_WB32(cur_buffer + 4, info->crypt_byte_block); | |
146 | 1 | AV_WB32(cur_buffer + 8, info->skip_byte_block); | |
147 | 1 | AV_WB32(cur_buffer + 12, info->key_id_size); | |
148 | 1 | AV_WB32(cur_buffer + 16, info->iv_size); | |
149 | 1 | AV_WB32(cur_buffer + 20, info->subsample_count); | |
150 | 1 | cur_buffer += 24; | |
151 | 1 | memcpy(cur_buffer, info->key_id, info->key_id_size); | |
152 | 1 | cur_buffer += info->key_id_size; | |
153 | 1 | memcpy(cur_buffer, info->iv, info->iv_size); | |
154 | 1 | cur_buffer += info->iv_size; | |
155 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | for (i = 0; i < info->subsample_count; i++) { |
156 | 4 | AV_WB32(cur_buffer, info->subsamples[i].bytes_of_clear_data); | |
157 | 4 | AV_WB32(cur_buffer + 4, info->subsamples[i].bytes_of_protected_data); | |
158 | 4 | cur_buffer += 8; | |
159 | } | ||
160 | |||
161 | 1 | return buffer; | |
162 | } | ||
163 | |||
164 | // The format of the AVEncryptionInitInfo side data: | ||
165 | // u32be init_info_count | ||
166 | // { | ||
167 | // u32be system_id_size | ||
168 | // u32be num_key_ids | ||
169 | // u32be key_id_size | ||
170 | // u32be data_size | ||
171 | // u8[system_id_size] system_id | ||
172 | // u8[key_id_size][num_key_id] key_ids | ||
173 | // u8[data_size] data | ||
174 | // }[init_info_count] | ||
175 | |||
176 | #define FF_ENCRYPTION_INIT_INFO_EXTRA 16 | ||
177 | |||
178 | 7 | AVEncryptionInitInfo *av_encryption_init_info_alloc( | |
179 | uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size) | ||
180 | { | ||
181 | AVEncryptionInitInfo *info; | ||
182 | uint32_t i; | ||
183 | |||
184 | 7 | info = av_mallocz(sizeof(*info)); | |
185 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!info) |
186 | ✗ | return NULL; | |
187 | |||
188 | 7 | info->system_id = av_mallocz(system_id_size); | |
189 | 7 | info->system_id_size = system_id_size; | |
190 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | info->key_ids = key_id_size ? av_calloc(num_key_ids, sizeof(*info->key_ids)) : NULL; |
191 | 7 | info->num_key_ids = num_key_ids; | |
192 | 7 | info->key_id_size = key_id_size; | |
193 | 7 | info->data = av_mallocz(data_size); | |
194 | 7 | info->data_size = data_size; | |
195 | |||
196 | // Allow pointers to be NULL if the size is 0. | ||
197 |
2/8✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
7 | if ((!info->system_id && system_id_size) || (!info->data && data_size) || |
198 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
7 | (!info->key_ids && num_key_ids && key_id_size)) { |
199 | ✗ | av_encryption_init_info_free(info); | |
200 | ✗ | return NULL; | |
201 | } | ||
202 | |||
203 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (key_id_size) { |
204 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7 times.
|
17 | for (i = 0; i < num_key_ids; i++) { |
205 | 10 | info->key_ids[i] = av_mallocz(key_id_size); | |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!info->key_ids[i]) { |
207 | ✗ | av_encryption_init_info_free(info); | |
208 | ✗ | return NULL; | |
209 | } | ||
210 | } | ||
211 | } | ||
212 | |||
213 | 7 | return info; | |
214 | } | ||
215 | |||
216 | 12 | void av_encryption_init_info_free(AVEncryptionInitInfo *info) | |
217 | { | ||
218 | uint32_t i; | ||
219 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
|
12 | if (info) { |
220 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7 times.
|
19 | for (i = 0; i < info->num_key_ids; i++) { |
221 | 12 | av_free(info->key_ids[i]); | |
222 | } | ||
223 | 7 | av_encryption_init_info_free(info->next); | |
224 | 7 | av_free(info->system_id); | |
225 | 7 | av_free(info->key_ids); | |
226 | 7 | av_free(info->data); | |
227 | 7 | av_free(info); | |
228 | } | ||
229 | 12 | } | |
230 | |||
231 | 2 | AVEncryptionInitInfo *av_encryption_init_info_get_side_data( | |
232 | const uint8_t *side_data, size_t side_data_size) | ||
233 | { | ||
234 | // |ret| tracks the front of the list, |info| tracks the back. | ||
235 | 2 | AVEncryptionInitInfo *ret = NULL, *info, *temp_info; | |
236 | uint64_t system_id_size, num_key_ids, key_id_size, data_size, i, j; | ||
237 | uint64_t init_info_count; | ||
238 | |||
239 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!side_data || side_data_size < 4) |
240 | ✗ | return NULL; | |
241 | |||
242 | 2 | init_info_count = AV_RB32(side_data); | |
243 | 2 | side_data += 4; | |
244 | 2 | side_data_size -= 4; | |
245 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
|
5 | for (i = 0; i < init_info_count; i++) { |
246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (side_data_size < FF_ENCRYPTION_INIT_INFO_EXTRA) { |
247 | ✗ | av_encryption_init_info_free(ret); | |
248 | ✗ | return NULL; | |
249 | } | ||
250 | |||
251 | 3 | system_id_size = AV_RB32(side_data); | |
252 | 3 | num_key_ids = AV_RB32(side_data + 4); | |
253 | 3 | key_id_size = AV_RB32(side_data + 8); | |
254 | 3 | data_size = AV_RB32(side_data + 12); | |
255 | |||
256 | // UINT32_MAX + UINT32_MAX + UINT32_MAX * UINT32_MAX == UINT64_MAX | ||
257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (side_data_size - FF_ENCRYPTION_INIT_INFO_EXTRA < system_id_size + data_size + num_key_ids * key_id_size) { |
258 | ✗ | av_encryption_init_info_free(ret); | |
259 | ✗ | return NULL; | |
260 | } | ||
261 | 3 | side_data += FF_ENCRYPTION_INIT_INFO_EXTRA; | |
262 | 3 | side_data_size -= FF_ENCRYPTION_INIT_INFO_EXTRA; | |
263 | |||
264 | 3 | temp_info = av_encryption_init_info_alloc(system_id_size, num_key_ids, key_id_size, data_size); | |
265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!temp_info) { |
266 | ✗ | av_encryption_init_info_free(ret); | |
267 | ✗ | return NULL; | |
268 | } | ||
269 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (i == 0) { |
270 | 2 | info = ret = temp_info; | |
271 | } else { | ||
272 | 1 | info->next = temp_info; | |
273 | 1 | info = temp_info; | |
274 | } | ||
275 | |||
276 | 3 | memcpy(info->system_id, side_data, system_id_size); | |
277 | 3 | side_data += system_id_size; | |
278 | 3 | side_data_size -= system_id_size; | |
279 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (j = 0; j < num_key_ids; j++) { |
280 | 6 | memcpy(info->key_ids[j], side_data, key_id_size); | |
281 | 6 | side_data += key_id_size; | |
282 | 6 | side_data_size -= key_id_size; | |
283 | } | ||
284 | 3 | memcpy(info->data, side_data, data_size); | |
285 | 3 | side_data += data_size; | |
286 | 3 | side_data_size -= data_size; | |
287 | } | ||
288 | |||
289 | 2 | return ret; | |
290 | } | ||
291 | |||
292 | 4 | uint8_t *av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size) | |
293 | { | ||
294 | const AVEncryptionInitInfo *cur_info; | ||
295 | uint8_t *buffer, *cur_buffer; | ||
296 | uint32_t i, init_info_count; | ||
297 | uint64_t temp_side_data_size; | ||
298 | |||
299 | 4 | temp_side_data_size = 4; | |
300 | 4 | init_info_count = 0; | |
301 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
|
9 | for (cur_info = info; cur_info; cur_info = cur_info->next) { |
302 | 5 | temp_side_data_size += (uint64_t)FF_ENCRYPTION_INIT_INFO_EXTRA + cur_info->system_id_size + cur_info->data_size; | |
303 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | if (init_info_count == UINT32_MAX || temp_side_data_size > UINT32_MAX) { |
304 | ✗ | return NULL; | |
305 | } | ||
306 | 5 | init_info_count++; | |
307 | |||
308 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (cur_info->num_key_ids) { |
309 | 5 | temp_side_data_size += (uint64_t)cur_info->num_key_ids * cur_info->key_id_size; | |
310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (temp_side_data_size > UINT32_MAX) { |
311 | ✗ | return NULL; | |
312 | } | ||
313 | } | ||
314 | } | ||
315 | 4 | *side_data_size = temp_side_data_size; | |
316 | |||
317 | 4 | cur_buffer = buffer = av_malloc(*side_data_size); | |
318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!buffer) |
319 | ✗ | return NULL; | |
320 | |||
321 | 4 | AV_WB32(cur_buffer, init_info_count); | |
322 | 4 | cur_buffer += 4; | |
323 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
|
9 | for (cur_info = info; cur_info; cur_info = cur_info->next) { |
324 | 5 | AV_WB32(cur_buffer, cur_info->system_id_size); | |
325 | 5 | AV_WB32(cur_buffer + 4, cur_info->num_key_ids); | |
326 | 5 | AV_WB32(cur_buffer + 8, cur_info->key_id_size); | |
327 | 5 | AV_WB32(cur_buffer + 12, cur_info->data_size); | |
328 | 5 | cur_buffer += 16; | |
329 | |||
330 | 5 | memcpy(cur_buffer, cur_info->system_id, cur_info->system_id_size); | |
331 | 5 | cur_buffer += cur_info->system_id_size; | |
332 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
|
13 | for (i = 0; i < cur_info->num_key_ids; i++) { |
333 | 8 | memcpy(cur_buffer, cur_info->key_ids[i], cur_info->key_id_size); | |
334 | 8 | cur_buffer += cur_info->key_id_size; | |
335 | } | ||
336 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
|
5 | if (cur_info->data_size > 0) { |
337 | 3 | memcpy(cur_buffer, cur_info->data, cur_info->data_size); | |
338 | 3 | cur_buffer += cur_info->data_size; | |
339 | } | ||
340 | } | ||
341 | |||
342 | 4 | return buffer; | |
343 | } | ||
344 |