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 "encryption_info.h"
20 #include "mem.h"
21 #include "intreadwrite.h"
22 
23 #define FF_ENCRYPTION_INFO_EXTRA 24
24 
25 // The format of the AVEncryptionInfo side data:
26 // u32be scheme
27 // u32be crypt_byte_block
28 // u32be skip_byte_block
29 // u32be key_id_size
30 // u32be iv_size
31 // u32be subsample_count
32 // u8[key_id_size] key_id
33 // u8[iv_size] iv
34 // {
35 //   u32be bytes_of_clear_data
36 //   u32be bytes_of_protected_data
37 // }[subsample_count]
38 
av_encryption_info_alloc(uint32_t subsample_count,uint32_t key_id_size,uint32_t iv_size)39 AVEncryptionInfo *av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size)
40 {
41     AVEncryptionInfo *info;
42 
43     info = av_mallocz(sizeof(*info));
44     if (!info)
45         return NULL;
46 
47     info->key_id = av_mallocz(key_id_size);
48     info->key_id_size = key_id_size;
49     info->iv = av_mallocz(iv_size);
50     info->iv_size = iv_size;
51     info->subsamples = av_mallocz_array(subsample_count, sizeof(*info->subsamples));
52     info->subsample_count = subsample_count;
53 
54     // Allow info->subsamples to be NULL if there are no subsamples.
55     if (!info->key_id || !info->iv || (!info->subsamples && subsample_count)) {
56         av_encryption_info_free(info);
57         return NULL;
58     }
59 
60     return info;
61 }
62 
av_encryption_info_clone(const AVEncryptionInfo * info)63 AVEncryptionInfo *av_encryption_info_clone(const AVEncryptionInfo *info)
64 {
65     AVEncryptionInfo *ret;
66 
67     if (!info)
68         return NULL;
69 
70     ret = av_encryption_info_alloc(info->subsample_count, info->key_id_size, info->iv_size);
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 
av_encryption_info_free(AVEncryptionInfo * info)83 void av_encryption_info_free(AVEncryptionInfo *info)
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 
av_encryption_info_get_side_data(const uint8_t * buffer,size_t size)93 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     if (!buffer || size < FF_ENCRYPTION_INFO_EXTRA)
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++) {
120         info->subsamples[i].bytes_of_clear_data = AV_RB32(buffer);
121         info->subsamples[i].bytes_of_protected_data = AV_RB32(buffer + 4);
122         buffer += 8;
123     }
124 
125     return info;
126 }
127 
av_encryption_info_add_side_data(const AVEncryptionInfo * info,size_t * size)128 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     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 
139     *size = FF_ENCRYPTION_INFO_EXTRA + info->key_id_size + info->iv_size +
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 
av_encryption_init_info_alloc(uint32_t system_id_size,uint32_t num_key_ids,uint32_t key_id_size,uint32_t data_size)179 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     info = av_mallocz(sizeof(*info));
186     if (!info)
187         return NULL;
188 
189     info->system_id = av_mallocz(system_id_size);
190     info->system_id_size = system_id_size;
191     info->key_ids = key_id_size ? av_mallocz_array(num_key_ids, sizeof(*info->key_ids)) : NULL;
192     info->num_key_ids = num_key_ids;
193     info->key_id_size = key_id_size;
194     info->data = av_mallocz(data_size);
195     info->data_size = data_size;
196 
197     // Allow pointers to be NULL if the size is 0.
198     if ((!info->system_id && system_id_size) || (!info->data && data_size) ||
199         (!info->key_ids && num_key_ids && key_id_size)) {
200         av_encryption_init_info_free(info);
201         return NULL;
202     }
203 
204     if (key_id_size) {
205         for (i = 0; i < num_key_ids; i++) {
206             info->key_ids[i] = av_mallocz(key_id_size);
207             if (!info->key_ids[i]) {
208                 av_encryption_init_info_free(info);
209                 return NULL;
210             }
211         }
212     }
213 
214     return info;
215 }
216 
av_encryption_init_info_free(AVEncryptionInitInfo * info)217 void av_encryption_init_info_free(AVEncryptionInitInfo *info)
218 {
219     uint32_t i;
220     if (info) {
221         for (i = 0; i < info->num_key_ids; i++) {
222             av_free(info->key_ids[i]);
223         }
224         av_encryption_init_info_free(info->next);
225         av_free(info->system_id);
226         av_free(info->key_ids);
227         av_free(info->data);
228         av_free(info);
229     }
230 }
231 
av_encryption_init_info_get_side_data(const uint8_t * side_data,size_t side_data_size)232 AVEncryptionInitInfo *av_encryption_init_info_get_side_data(
233     const uint8_t *side_data, size_t side_data_size)
234 {
235     // |ret| tracks the front of the list, |info| tracks the back.
236     AVEncryptionInitInfo *ret = NULL, *info, *temp_info;
237     uint64_t system_id_size, num_key_ids, key_id_size, data_size, i, j;
238     uint64_t init_info_count;
239 
240     if (!side_data || side_data_size < 4)
241         return NULL;
242 
243     init_info_count = AV_RB32(side_data);
244     side_data += 4;
245     side_data_size -= 4;
246     for (i = 0; i < init_info_count; i++) {
247         if (side_data_size < FF_ENCRYPTION_INIT_INFO_EXTRA) {
248             av_encryption_init_info_free(ret);
249             return NULL;
250         }
251 
252         system_id_size = AV_RB32(side_data);
253         num_key_ids = AV_RB32(side_data + 4);
254         key_id_size = AV_RB32(side_data + 8);
255         data_size = AV_RB32(side_data + 12);
256 
257         // UINT32_MAX + UINT32_MAX + UINT32_MAX * UINT32_MAX == UINT64_MAX
258         if (side_data_size - FF_ENCRYPTION_INIT_INFO_EXTRA < system_id_size + data_size + num_key_ids * key_id_size) {
259             av_encryption_init_info_free(ret);
260             return NULL;
261         }
262         side_data += FF_ENCRYPTION_INIT_INFO_EXTRA;
263         side_data_size -= FF_ENCRYPTION_INIT_INFO_EXTRA;
264 
265         temp_info = av_encryption_init_info_alloc(system_id_size, num_key_ids, key_id_size, data_size);
266         if (!temp_info) {
267             av_encryption_init_info_free(ret);
268             return NULL;
269         }
270         if (i == 0) {
271             info = ret = temp_info;
272         } else {
273             info->next = temp_info;
274             info = temp_info;
275         }
276 
277         memcpy(info->system_id, side_data, system_id_size);
278         side_data += system_id_size;
279         side_data_size -= system_id_size;
280         for (j = 0; j < num_key_ids; j++) {
281             memcpy(info->key_ids[j], side_data, key_id_size);
282             side_data += key_id_size;
283             side_data_size -= key_id_size;
284         }
285         memcpy(info->data, side_data, data_size);
286         side_data += data_size;
287         side_data_size -= data_size;
288     }
289 
290     return ret;
291 }
292 
av_encryption_init_info_add_side_data(const AVEncryptionInitInfo * info,size_t * side_data_size)293 uint8_t *av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
294 {
295     const AVEncryptionInitInfo *cur_info;
296     uint8_t *buffer, *cur_buffer;
297     uint32_t i, init_info_count;
298     uint64_t temp_side_data_size;
299 
300     temp_side_data_size = 4;
301     init_info_count = 0;
302     for (cur_info = info; cur_info; cur_info = cur_info->next) {
303         temp_side_data_size += (uint64_t)FF_ENCRYPTION_INIT_INFO_EXTRA + cur_info->system_id_size + cur_info->data_size;
304         if (init_info_count == UINT32_MAX || temp_side_data_size > UINT32_MAX) {
305             return NULL;
306         }
307         init_info_count++;
308 
309         if (cur_info->num_key_ids) {
310             temp_side_data_size += (uint64_t)cur_info->num_key_ids * cur_info->key_id_size;
311             if (temp_side_data_size > UINT32_MAX) {
312                 return NULL;
313             }
314         }
315     }
316     *side_data_size = temp_side_data_size;
317 
318     cur_buffer = buffer = av_malloc(*side_data_size);
319     if (!buffer)
320         return NULL;
321 
322     AV_WB32(cur_buffer, init_info_count);
323     cur_buffer += 4;
324     for (cur_info = info; cur_info; cur_info = cur_info->next) {
325         AV_WB32(cur_buffer,      cur_info->system_id_size);
326         AV_WB32(cur_buffer +  4, cur_info->num_key_ids);
327         AV_WB32(cur_buffer +  8, cur_info->key_id_size);
328         AV_WB32(cur_buffer + 12, cur_info->data_size);
329         cur_buffer += 16;
330 
331         memcpy(cur_buffer, cur_info->system_id, cur_info->system_id_size);
332         cur_buffer += cur_info->system_id_size;
333         for (i = 0; i < cur_info->num_key_ids; i++) {
334             memcpy(cur_buffer, cur_info->key_ids[i], cur_info->key_id_size);
335             cur_buffer += cur_info->key_id_size;
336         }
337         if (cur_info->data_size > 0) {
338             memcpy(cur_buffer, cur_info->data, cur_info->data_size);
339             cur_buffer += cur_info->data_size;
340         }
341     }
342 
343     return buffer;
344 }
345