LCOV - code coverage report
Current view: top level - libavutil - encryption_info.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 155 18.1 %
Date: 2018-05-20 11:54:08 Functions: 3 9 33.3 %

          Line data    Source code
       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             : 
      39          75 : AVEncryptionInfo *av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size)
      40             : {
      41             :     AVEncryptionInfo *info;
      42             : 
      43          75 :     info = av_mallocz(sizeof(*info));
      44          75 :     if (!info)
      45           0 :         return NULL;
      46             : 
      47          75 :     info->key_id = av_mallocz(key_id_size);
      48          75 :     info->key_id_size = key_id_size;
      49          75 :     info->iv = av_mallocz(iv_size);
      50          75 :     info->iv_size = iv_size;
      51          75 :     info->subsamples = av_mallocz_array(subsample_count, sizeof(*info->subsamples));
      52          75 :     info->subsample_count = subsample_count;
      53             : 
      54             :     // Allow info->subsamples to be NULL if there are no subsamples.
      55          75 :     if (!info->key_id || !info->iv || (!info->subsamples && subsample_count)) {
      56           0 :         av_encryption_info_free(info);
      57           0 :         return NULL;
      58             :     }
      59             : 
      60          75 :     return info;
      61             : }
      62             : 
      63          72 : AVEncryptionInfo *av_encryption_info_clone(const AVEncryptionInfo *info)
      64             : {
      65             :     AVEncryptionInfo *ret;
      66             : 
      67          72 :     ret = av_encryption_info_alloc(info->subsample_count, info->key_id_size, info->iv_size);
      68          72 :     if (!ret)
      69           0 :         return NULL;
      70             : 
      71          72 :     ret->scheme = info->scheme;
      72          72 :     ret->crypt_byte_block = info->crypt_byte_block;
      73          72 :     ret->skip_byte_block = info->skip_byte_block;
      74          72 :     memcpy(ret->iv, info->iv, info->iv_size);
      75          72 :     memcpy(ret->key_id, info->key_id, info->key_id_size);
      76          72 :     memcpy(ret->subsamples, info->subsamples, sizeof(*info->subsamples) * info->subsample_count);
      77          72 :     return ret;
      78             : }
      79             : 
      80         513 : void av_encryption_info_free(AVEncryptionInfo *info)
      81             : {
      82         513 :     if (info) {
      83          75 :         av_free(info->key_id);
      84          75 :         av_free(info->iv);
      85          75 :         av_free(info->subsamples);
      86          75 :         av_free(info);
      87             :     }
      88         513 : }
      89             : 
      90           0 : AVEncryptionInfo *av_encryption_info_get_side_data(const uint8_t* buffer, size_t size)
      91             : {
      92             :     AVEncryptionInfo *info;
      93             :     uint64_t key_id_size, iv_size, subsample_count, i;
      94             : 
      95           0 :     if (!buffer || size < FF_ENCRYPTION_INFO_EXTRA)
      96           0 :         return NULL;
      97             : 
      98           0 :     key_id_size = AV_RB32(buffer + 12);
      99           0 :     iv_size = AV_RB32(buffer + 16);
     100           0 :     subsample_count = AV_RB32(buffer + 20);
     101             : 
     102           0 :     if (size < FF_ENCRYPTION_INFO_EXTRA + key_id_size + iv_size + subsample_count * 8)
     103           0 :         return NULL;
     104             : 
     105           0 :     info = av_encryption_info_alloc(subsample_count, key_id_size, iv_size);
     106           0 :     if (!info)
     107           0 :         return NULL;
     108             : 
     109           0 :     info->scheme = AV_RB32(buffer);
     110           0 :     info->crypt_byte_block = AV_RB32(buffer + 4);
     111           0 :     info->skip_byte_block = AV_RB32(buffer + 8);
     112           0 :     memcpy(info->key_id, buffer + 24, key_id_size);
     113           0 :     memcpy(info->iv, buffer + key_id_size + 24, iv_size);
     114             : 
     115           0 :     buffer += key_id_size + iv_size + 24;
     116           0 :     for (i = 0; i < subsample_count; i++) {
     117           0 :         info->subsamples[i].bytes_of_clear_data = AV_RB32(buffer);
     118           0 :         info->subsamples[i].bytes_of_protected_data = AV_RB32(buffer + 4);
     119           0 :         buffer += 8;
     120             :     }
     121             : 
     122           0 :     return info;
     123             : }
     124             : 
     125           0 : uint8_t *av_encryption_info_add_side_data(const AVEncryptionInfo *info, size_t *size)
     126             : {
     127             :     uint8_t *buffer, *cur_buffer;
     128             :     uint32_t i;
     129             : 
     130           0 :     if (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA < info->key_id_size ||
     131           0 :         UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA - info->key_id_size < info->iv_size ||
     132           0 :         (UINT32_MAX - FF_ENCRYPTION_INFO_EXTRA - info->key_id_size - info->iv_size) / 8 < info->subsample_count) {
     133           0 :         return NULL;
     134             :     }
     135             : 
     136           0 :     *size = FF_ENCRYPTION_INFO_EXTRA + info->key_id_size + info->iv_size +
     137           0 :             (info->subsample_count * 8);
     138           0 :     cur_buffer = buffer = av_malloc(*size);
     139           0 :     if (!buffer)
     140           0 :         return NULL;
     141             : 
     142           0 :     AV_WB32(cur_buffer,      info->scheme);
     143           0 :     AV_WB32(cur_buffer +  4, info->crypt_byte_block);
     144           0 :     AV_WB32(cur_buffer +  8, info->skip_byte_block);
     145           0 :     AV_WB32(cur_buffer + 12, info->key_id_size);
     146           0 :     AV_WB32(cur_buffer + 16, info->iv_size);
     147           0 :     AV_WB32(cur_buffer + 20, info->subsample_count);
     148           0 :     cur_buffer += 24;
     149           0 :     memcpy(cur_buffer, info->key_id, info->key_id_size);
     150           0 :     cur_buffer += info->key_id_size;
     151           0 :     memcpy(cur_buffer, info->iv, info->iv_size);
     152           0 :     cur_buffer += info->iv_size;
     153           0 :     for (i = 0; i < info->subsample_count; i++) {
     154           0 :         AV_WB32(cur_buffer, info->subsamples[i].bytes_of_clear_data);
     155           0 :         AV_WB32(cur_buffer + 4, info->subsamples[i].bytes_of_protected_data);
     156           0 :         cur_buffer += 8;
     157             :     }
     158             : 
     159           0 :     return buffer;
     160             : }
     161             : 
     162             : // The format of the AVEncryptionInitInfo side data:
     163             : // u32be system_id_size
     164             : // u32be num_key_ids
     165             : // u32be key_id_size
     166             : // u32be data_size
     167             : // u8[system_id_size] system_id
     168             : // u8[key_id_size][num_key_id] key_ids
     169             : // u8[data_size] data
     170             : 
     171             : #define FF_ENCRYPTION_INIT_INFO_EXTRA 16
     172             : 
     173           0 : AVEncryptionInitInfo *av_encryption_init_info_alloc(
     174             :     uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
     175             : {
     176             :     AVEncryptionInitInfo *info;
     177             :     uint32_t i;
     178             : 
     179           0 :     info = av_mallocz(sizeof(*info));
     180           0 :     if (!info)
     181           0 :         return NULL;
     182             : 
     183           0 :     info->system_id = av_mallocz(system_id_size);
     184           0 :     info->system_id_size = system_id_size;
     185           0 :     info->key_ids = key_id_size ? av_mallocz_array(num_key_ids, sizeof(*info->key_ids)) : NULL;
     186           0 :     info->num_key_ids = num_key_ids;
     187           0 :     info->key_id_size = key_id_size;
     188           0 :     info->data = av_mallocz(data_size);
     189           0 :     info->data_size = data_size;
     190             : 
     191             :     // Allow pointers to be NULL if the size is 0.
     192           0 :     if ((!info->system_id && system_id_size) || (!info->data && data_size) ||
     193           0 :         (!info->key_ids && num_key_ids && key_id_size)) {
     194           0 :         av_encryption_init_info_free(info);
     195           0 :         return NULL;
     196             :     }
     197             : 
     198           0 :     if (key_id_size) {
     199           0 :         for (i = 0; i < num_key_ids; i++) {
     200           0 :             info->key_ids[i] = av_mallocz(key_id_size);
     201           0 :             if (!info->key_ids[i]) {
     202           0 :                 av_encryption_init_info_free(info);
     203           0 :                 return NULL;
     204             :             }
     205             :         }
     206             :     }
     207             : 
     208           0 :     return info;
     209             : }
     210             : 
     211           0 : void av_encryption_init_info_free(AVEncryptionInitInfo *info)
     212             : {
     213             :     uint32_t i;
     214           0 :     if (info) {
     215           0 :         for (i = 0; i < info->num_key_ids; i++) {
     216           0 :             av_free(info->key_ids[i]);
     217             :         }
     218           0 :         av_free(info->system_id);
     219           0 :         av_free(info->key_ids);
     220           0 :         av_free(info->data);
     221           0 :         av_free(info);
     222             :     }
     223           0 : }
     224             : 
     225           0 : AVEncryptionInitInfo *av_encryption_init_info_get_side_data(
     226             :     const uint8_t *side_data, size_t side_data_size)
     227             : {
     228             :     AVEncryptionInitInfo *info;
     229             :     uint64_t system_id_size, num_key_ids, key_id_size, data_size, i;
     230             : 
     231           0 :     if (!side_data || side_data_size < FF_ENCRYPTION_INIT_INFO_EXTRA)
     232           0 :         return NULL;
     233             : 
     234           0 :     system_id_size = AV_RB32(side_data);
     235           0 :     num_key_ids = AV_RB32(side_data + 4);
     236           0 :     key_id_size = AV_RB32(side_data + 8);
     237           0 :     data_size = AV_RB32(side_data + 12);
     238             : 
     239             :     // UINT32_MAX + UINT32_MAX + UINT32_MAX * UINT32_MAX == UINT64_MAX
     240           0 :     if (side_data_size - FF_ENCRYPTION_INIT_INFO_EXTRA < system_id_size + data_size + num_key_ids * key_id_size)
     241           0 :         return NULL;
     242             : 
     243           0 :     info = av_encryption_init_info_alloc(system_id_size, num_key_ids, key_id_size, data_size);
     244           0 :     if (!info)
     245           0 :         return NULL;
     246             : 
     247           0 :     memcpy(info->system_id, side_data + 16, system_id_size);
     248           0 :     side_data += system_id_size + 16;
     249           0 :     for (i = 0; i < num_key_ids; i++) {
     250           0 :       memcpy(info->key_ids[i], side_data, key_id_size);
     251           0 :       side_data += key_id_size;
     252             :     }
     253           0 :     memcpy(info->data, side_data, data_size);
     254             : 
     255           0 :     return info;
     256             : }
     257             : 
     258           0 : uint8_t *av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
     259             : {
     260             :     uint8_t *buffer, *cur_buffer;
     261             :     uint32_t i, max_size;
     262             : 
     263           0 :     if (UINT32_MAX - FF_ENCRYPTION_INIT_INFO_EXTRA < info->system_id_size ||
     264           0 :         UINT32_MAX - FF_ENCRYPTION_INIT_INFO_EXTRA - info->system_id_size < info->data_size) {
     265           0 :         return NULL;
     266             :     }
     267             : 
     268           0 :     if (info->num_key_ids) {
     269           0 :         max_size = UINT32_MAX - FF_ENCRYPTION_INIT_INFO_EXTRA - info->system_id_size - info->data_size;
     270           0 :         if (max_size / info->num_key_ids < info->key_id_size)
     271           0 :             return NULL;
     272             :     }
     273             : 
     274           0 :     *side_data_size = FF_ENCRYPTION_INIT_INFO_EXTRA + info->system_id_size +
     275           0 :                       info->data_size + (info->num_key_ids * info->key_id_size);
     276           0 :     cur_buffer = buffer = av_malloc(*side_data_size);
     277           0 :     if (!buffer)
     278           0 :         return NULL;
     279             : 
     280           0 :     AV_WB32(cur_buffer,      info->system_id_size);
     281           0 :     AV_WB32(cur_buffer +  4, info->num_key_ids);
     282           0 :     AV_WB32(cur_buffer +  8, info->key_id_size);
     283           0 :     AV_WB32(cur_buffer + 12, info->data_size);
     284           0 :     cur_buffer += 16;
     285             : 
     286           0 :     memcpy(cur_buffer, info->system_id, info->system_id_size);
     287           0 :     cur_buffer += info->system_id_size;
     288           0 :     for (i = 0; i < info->num_key_ids; i++) {
     289           0 :         memcpy(cur_buffer, info->key_ids[i], info->key_id_size);
     290           0 :         cur_buffer += info->key_id_size;
     291             :     }
     292           0 :     memcpy(cur_buffer, info->data, info->data_size);
     293             : 
     294           0 :     return buffer;
     295             : }

Generated by: LCOV version 1.13