FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/encryption_info.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 169 195 86.7%
Functions: 10 10 100.0%
Branches: 64 106 60.4%

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