FFmpeg
Loading...
Searching...
No Matches
encryption_info.c
Go to the documentation of this file.
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
42AVEncryptionInfo *av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size)
43{
44 AVEncryptionInfo *info;
45
46 info = av_mallocz(sizeof(*info));
47 if (!info)
48 return NULL;
49
50 info->key_id = av_mallocz(key_id_size);
51 info->key_id_size = key_id_size;
52 info->iv = av_mallocz(iv_size);
53 info->iv_size = iv_size;
54 info->subsamples = av_calloc(subsample_count, sizeof(*info->subsamples));
55 info->subsample_count = subsample_count;
56
57 // Allow info->subsamples to be NULL if there are no subsamples.
58 if (!info->key_id || !info->iv || (!info->subsamples && subsample_count)) {
60 return NULL;
61 }
62
63 return info;
64}
65
67{
69
71 if (!ret)
72 return NULL;
73
74 ret->scheme = info->scheme;
75 ret->crypt_byte_block = info->crypt_byte_block;
76 ret->skip_byte_block = info->skip_byte_block;
77 memcpy(ret->iv, info->iv, info->iv_size);
78 memcpy(ret->key_id, info->key_id, info->key_id_size);
79 memcpy(ret->subsamples, info->subsamples, sizeof(*info->subsamples) * info->subsample_count);
80 return ret;
81}
82
84{
85 if (info) {
86 av_free(info->key_id);
87 av_free(info->iv);
88 av_free(info->subsamples);
89 av_free(info);
90 }
91}
92
94{
95 AVEncryptionInfo *info;
96 uint64_t key_id_size, iv_size, subsample_count, i;
97
99 return NULL;
100
101 key_id_size = AV_RB32(buffer + 12);
102 iv_size = AV_RB32(buffer + 16);
103 subsample_count = AV_RB32(buffer + 20);
104
105 if (size < FF_ENCRYPTION_INFO_EXTRA + key_id_size + iv_size + subsample_count * 8)
106 return NULL;
107
108 info = av_encryption_info_alloc(subsample_count, key_id_size, iv_size);
109 if (!info)
110 return NULL;
111
112 info->scheme = AV_RB32(buffer);
113 info->crypt_byte_block = AV_RB32(buffer + 4);
114 info->skip_byte_block = AV_RB32(buffer + 8);
115 memcpy(info->key_id, buffer + 24, key_id_size);
116 memcpy(info->iv, buffer + key_id_size + 24, iv_size);
117
118 buffer += key_id_size + iv_size + 24;
119 for (i = 0; i < subsample_count; i++) {
122 buffer += 8;
123 }
124
125 return info;
126}
127
129{
130 uint8_t *buffer, *cur_buffer;
131 uint32_t i;
132
133 if (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA < info->key_id_size ||
134 UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA - info->key_id_size < info->iv_size ||
135 (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA - info->key_id_size - info->iv_size) / 8 < info->subsample_count) {
136 return NULL;
137 }
138
140 (info->subsample_count * 8);
141 cur_buffer = buffer = av_malloc(*size);
142 if (!buffer)
143 return NULL;
144
145 AV_WB32(cur_buffer, info->scheme);
146 AV_WB32(cur_buffer + 4, info->crypt_byte_block);
147 AV_WB32(cur_buffer + 8, info->skip_byte_block);
148 AV_WB32(cur_buffer + 12, info->key_id_size);
149 AV_WB32(cur_buffer + 16, info->iv_size);
150 AV_WB32(cur_buffer + 20, info->subsample_count);
151 cur_buffer += 24;
152 memcpy(cur_buffer, info->key_id, info->key_id_size);
153 cur_buffer += info->key_id_size;
154 memcpy(cur_buffer, info->iv, info->iv_size);
155 cur_buffer += info->iv_size;
156 for (i = 0; i < info->subsample_count; i++) {
157 AV_WB32(cur_buffer, info->subsamples[i].bytes_of_clear_data);
158 AV_WB32(cur_buffer + 4, info->subsamples[i].bytes_of_protected_data);
159 cur_buffer += 8;
160 }
161
162 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
180 uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
181{
183 uint32_t i;
184
185 if (num_key_ids && !key_id_size)
186 return NULL;
187
188 info = av_mallocz(sizeof(*info));
189 if (!info)
190 return NULL;
191
192 info->system_id = av_mallocz(system_id_size);
193 info->key_ids = key_id_size ? av_calloc(num_key_ids, sizeof(*info->key_ids)) : NULL;
194 info->data = av_mallocz(data_size);
195
196 // Allow pointers to be NULL if the size is 0.
197 if ((!info->system_id && system_id_size) || (!info->data && data_size) ||
198 (!info->key_ids && num_key_ids && key_id_size)) {
200 return NULL;
201 }
202
203 info->system_id_size = system_id_size;
204 info->num_key_ids = num_key_ids;
205 info->key_id_size = key_id_size;
206 info->data_size = data_size;
207
208 if (key_id_size) {
209 for (i = 0; i < num_key_ids; i++) {
210 info->key_ids[i] = av_mallocz(key_id_size);
211 if (!info->key_ids[i]) {
213 return NULL;
214 }
215 }
216 }
217
218 return info;
219}
220
222{
223 while (info) {
224 AVEncryptionInitInfo *next = info->next;
225
226 for (uint32_t i = 0; i < info->num_key_ids; i++) {
227 av_free(info->key_ids[i]);
228 }
229 av_free(info->system_id);
230 av_free(info->key_ids);
231 av_free(info->data);
232 av_free(info);
233
234 info = next;
235 }
236}
237
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 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 if (!side_data || side_data_size < 4)
247 return NULL;
248
249 init_info_count = AV_RB32(side_data);
250 side_data += 4;
251 side_data_size -= 4;
252 for (i = 0; i < init_info_count; i++) {
253 if (side_data_size < FF_ENCRYPTION_INIT_INFO_EXTRA) {
255 return NULL;
256 }
257
258 system_id_size = AV_RB32(side_data);
259 num_key_ids = AV_RB32(side_data + 4);
260 key_id_size = AV_RB32(side_data + 8);
261 data_size = AV_RB32(side_data + 12);
262
263 // UINT32_MAX + UINT32_MAX + UINT32_MAX * UINT32_MAX == UINT64_MAX
264 if (side_data_size - FF_ENCRYPTION_INIT_INFO_EXTRA < system_id_size + data_size + num_key_ids * key_id_size) {
266 return NULL;
267 }
269 side_data_size -= FF_ENCRYPTION_INIT_INFO_EXTRA;
270
271 temp_info = av_encryption_init_info_alloc(system_id_size, num_key_ids, key_id_size, data_size);
272 if (!temp_info) {
274 return NULL;
275 }
276 if (i == 0) {
277 info = ret = temp_info;
278 } else {
279 info->next = temp_info;
280 info = temp_info;
281 }
282
283 memcpy(info->system_id, side_data, system_id_size);
284 side_data += system_id_size;
285 side_data_size -= system_id_size;
286 for (j = 0; j < num_key_ids; j++) {
287 memcpy(info->key_ids[j], side_data, key_id_size);
288 side_data += key_id_size;
289 side_data_size -= key_id_size;
290 }
291 memcpy(info->data, side_data, data_size);
292 side_data += data_size;
293 side_data_size -= data_size;
294 }
295
296 return ret;
297}
298
299int 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 uint64_t init_info_count = *side_data ? AV_RB32(*side_data) : 0;
307 size_t old_side_data_size = *side_data ? *side_data_size : 4;
308 uint64_t new_side_data_size = old_side_data_size;
309
310 for (cur_info = info; cur_info; cur_info = cur_info->next) {
311 new_side_data_size += (uint64_t)FF_ENCRYPTION_INIT_INFO_EXTRA + cur_info->system_id_size + cur_info->data_size;
312 if (init_info_count == UINT32_MAX || new_side_data_size > UINT32_MAX)
313 return AVERROR(EINVAL);
314 init_info_count++;
315
316 if (cur_info->num_key_ids) {
317 new_side_data_size += (uint64_t)cur_info->num_key_ids * cur_info->key_id_size;
318 if (new_side_data_size > UINT32_MAX)
319 return AVERROR(EINVAL);
320 }
321 }
322
323 buffer = av_fast_realloc(*side_data, allocated_size, new_side_data_size);
324 if (!buffer)
325 return AVERROR(ENOMEM);
326
327 AV_WB32(buffer, init_info_count);
328 cur_buffer = buffer + old_side_data_size;
329 for (cur_info = info; cur_info; cur_info = cur_info->next) {
330 AV_WB32(cur_buffer, cur_info->system_id_size);
331 AV_WB32(cur_buffer + 4, cur_info->num_key_ids);
332 AV_WB32(cur_buffer + 8, cur_info->key_id_size);
333 AV_WB32(cur_buffer + 12, cur_info->data_size);
334 cur_buffer += 16;
335
336 memcpy(cur_buffer, cur_info->system_id, cur_info->system_id_size);
337 cur_buffer += cur_info->system_id_size;
338 for (i = 0; i < cur_info->num_key_ids; i++) {
339 memcpy(cur_buffer, cur_info->key_ids[i], cur_info->key_id_size);
340 cur_buffer += cur_info->key_id_size;
341 }
342 if (cur_info->data_size > 0) {
343 memcpy(cur_buffer, cur_info->data, cur_info->data_size);
344 cur_buffer += cur_info->data_size;
345 }
346 }
347
348 *side_data = buffer;
349 *side_data_size = new_side_data_size;
350 return 0;
351}
352
353uint8_t *av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
354{
355 uint8_t *side_data = NULL;
356 unsigned int allocated_size = 0;
357
358 if (av_encryption_init_info_append_side_data(&side_data, side_data_size, &allocated_size, info) < 0)
359 return NULL;
360 return side_data;
361}
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
#define FF_ENCRYPTION_INIT_INFO_EXTRA
AVEncryptionInfo * av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size)
Allocates an AVEncryptionInfo structure and sub-pointers to hold the given number of subsamples.
AVEncryptionInfo * av_encryption_info_get_side_data(const uint8_t *buffer, size_t size)
Creates a copy of the AVEncryptionInfo that is contained in the given side data.
#define FF_ENCRYPTION_INFO_EXTRA
This file is part of FFmpeg.
AVEncryptionInfo * av_encryption_info_clone(const AVEncryptionInfo *info)
Allocates an AVEncryptionInfo structure with a copy of the given data.
AVEncryptionInitInfo * av_encryption_init_info_get_side_data(const uint8_t *side_data, size_t side_data_size)
Creates a copy of the AVEncryptionInitInfo that is contained in the given side data.
int av_encryption_init_info_append_side_data(uint8_t **side_data, size_t *side_data_size, unsigned int *allocated_size, const AVEncryptionInitInfo *info)
Appends a copy of the given encryption init info list to encryption init info side data,...
uint8_t * av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
Allocates and initializes side data that holds a copy of the given encryption init info.
uint8_t * av_encryption_info_add_side_data(const AVEncryptionInfo *info, size_t *size)
Allocates and initializes side data that holds a copy of the given encryption info.
AVEncryptionInitInfo * av_encryption_init_info_alloc(uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
Allocates an AVEncryptionInitInfo structure and sub-pointers to hold the given sizes.
void av_encryption_init_info_free(AVEncryptionInitInfo *info)
Frees the given encryption init info object.
void av_encryption_info_free(AVEncryptionInfo *info)
Frees the given encryption info object.
error code definitions
int ret
Definition ffplay.c:391
#define AVERROR(e)
Definition error.h:45
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:601
#define AV_WB32(p, v)
#define AV_RB32(p)
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
This describes encryption info for a packet.
uint32_t skip_byte_block
Only used for pattern encryption.
uint8_t * iv
The initialization vector.
uint32_t crypt_byte_block
Only used for pattern encryption.
uint8_t * key_id
The ID of the key used to encrypt the packet.
AVSubsampleEncryptionInfo * subsamples
An array of subsample encryption info specifying how parts of the sample are encrypted.
uint32_t scheme
The fourcc encryption scheme, in big-endian byte order.
This describes info used to initialize an encryption key system.
uint8_t * data
Key-system specific initialization data.
struct AVEncryptionInitInfo * next
An optional pointer to the next initialization info in the list.
uint8_t ** key_ids
An array of key IDs this initialization data is for.
uint8_t * system_id
A unique identifier for the key system this is for, can be NULL if it is not known.
uint32_t key_id_size
The number of bytes in each key ID.
uint32_t num_key_ids
The number of key IDs.
unsigned int bytes_of_clear_data
The number of bytes that are clear.
unsigned int bytes_of_protected_data
The number of bytes that are protected.
#define av_free(p)
#define av_mallocz(s)
static char buffer[20]
Definition seek.c:33
int size