Commit 614e91f2 authored by Jon Lech Johansen's avatar Jon Lech Johansen

* ./modules/demux/mp4/drms.c: improved sci handling.

  * ./modules/demux/mp4/mp4.c: if sample has not been decrypted, do not
                               pass it on to decoder.
parent 7c300cca
......@@ -2,7 +2,7 @@
* drms.c : DRMS
*****************************************************************************
* Copyright (C) 2004 VideoLAN
* $Id: drms.c,v 1.1 2004/01/05 12:37:52 jlj Exp $
* $Id: drms.c,v 1.2 2004/01/09 04:37:43 jlj Exp $
*
* Author: Jon Lech Johansen <jon-vl@nanocrew.net>
*
......@@ -23,8 +23,18 @@
#include <stdlib.h> /* malloc(), free() */
#ifdef WIN32
#include <io.h>
#else
#include <stdio.h>
#endif
#include <vlc/vlc.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef WIN32
#include <tchar.h>
#include <shlobj.h>
......@@ -34,6 +44,8 @@
#include "drms.h"
#include "drmstables.h"
#include "libmp4.h"
#define TAOS_INIT( tmp, i ) \
memset( tmp, 0, sizeof(tmp) ); \
tmp[ i + 0 ] = 0x67452301; \
......@@ -584,7 +596,7 @@ static int taos_osi( uint32_t *p_buffer )
return( i_ret );
}
static int get_sci_data( uint32_t p_sci[ 11 ][ 4 ] )
static int get_sci_data( uint32_t **pp_sci, uint32_t *p_sci_size )
{
int i_ret = -1;
......@@ -604,18 +616,24 @@ static int get_sci_data( uint32_t p_sci[ 11 ][ 4 ] )
OPEN_EXISTING, 0, NULL );
if( i_file != INVALID_HANDLE_VALUE )
{
i_read = sizeof(p_sci[ 0 ]) * 11;
i_size = GetFileSize( i_file, NULL );
if( i_size != INVALID_FILE_SIZE && i_size >= i_read )
if( i_size != INVALID_FILE_SIZE &&
i_size > (sizeof(*pp_sci[ 0 ]) * 22) )
{
i_size = SetFilePointer( i_file, 4, NULL, FILE_BEGIN );
if( i_size != INVALID_SET_FILE_POINTER )
*pp_sci = malloc( i_size * 2 );
if( *pp_sci != NULL )
{
if( ReadFile( i_file, p_sci, i_read, &i_size, NULL ) &&
i_size == i_read )
if( ReadFile( i_file, *pp_sci, i_size, &i_read, NULL ) &&
i_read == i_size )
{
*p_sci_size = i_size;
i_ret = 0;
}
else
{
free( (void *)*pp_sci );
*pp_sci = NULL;
}
}
}
......@@ -749,7 +767,7 @@ static inline void block_xor( uint32_t *p_in, uint32_t *p_key,
}
}
int drms_get_sys_key( uint32_t *p_sys_key )
static int get_sys_key( uint32_t *p_sys_key )
{
uint32_t p_tmp[ 128 ];
uint32_t p_tmp_key[ 4 ];
......@@ -774,77 +792,197 @@ int drms_get_sys_key( uint32_t *p_sys_key )
return( 0 );
}
int drms_get_user_key( uint32_t *p_sys_key, uint32_t *p_user_key )
struct drms_s
{
uint32_t i;
uint32_t p_tmp[ 4 ];
uint32_t i_user;
uint32_t i_key;
uint8_t *p_iviv;
uint8_t *p_name;
uint32_t i_name_len;
uint32_t *p_tmp;
uint32_t i_tmp_len;
uint32_t p_key[ 4 ];
uint32_t p_ctx[ 128 ];
char *psz_homedir;
};
#define P_DRMS ((struct drms_s *)p_drms)
static int rw_user_key( void *p_drms, uint32_t i_rw, uint32_t *p_user_key )
{
FILE *file;
int i_ret = -1;
char sz_path[ MAX_PATH ];
#define DRMS_PI_DIRNAME "drms"
#ifdef WIN32
#define DRMS_DIRNAME DRMS_PI_DIRNAME
#else
#define DRMS_DIRNAME "." DRMS_PI_DIRNAME
#endif
if( i_rw )
{
snprintf( sz_path, (sizeof(sz_path)/sizeof(sz_path[ 0 ])) - 1,
"%s/" DRMS_DIRNAME "/%08X.%03d", P_DRMS->psz_homedir,
P_DRMS->i_user, P_DRMS->i_key );
file = fopen( sz_path, "r" );
if( file != NULL )
{
i_ret = fread( p_user_key, sizeof(*p_user_key),
4, file ) == 4 ? 0 : -1;
fclose( file );
}
}
else
{
snprintf( sz_path, (sizeof(sz_path)/sizeof(sz_path[ 0 ])) - 1,
"%s/" DRMS_DIRNAME, P_DRMS->psz_homedir );
#if defined( HAVE_ERRNO_H )
# if defined( WIN32 )
if( !mkdir( sz_path ) || errno == EEXIST )
# else
if( !mkdir( sz_path, 0755 ) || errno == EEXIST )
# endif
#else
if( !mkdir( sz_path ) )
#endif
{
snprintf( sz_path, (sizeof(sz_path)/sizeof(sz_path[ 0 ])) - 1,
"%s/" DRMS_DIRNAME "/%08X.%03d", P_DRMS->psz_homedir,
P_DRMS->i_user, P_DRMS->i_key );
file = fopen( sz_path, "w" );
if( file != NULL )
{
i_ret = fwrite( p_user_key, sizeof(*p_user_key),
4, file ) == 4 ? 0 : -1;
fclose( file );
}
}
}
return( i_ret );
}
static int get_user_key( void *p_drms, uint32_t *p_user_key )
{
uint32_t i, y;
uint32_t *p_tmp;
uint32_t *p_cur_key;
uint32_t p_acei[ 41 ];
uint32_t p_ctx[ 128 ];
uint32_t p_sci[ 2 ][ 11 ][ 4 ];
uint32_t p_sys_key[ 4 ];
uint32_t i_sci_size;
uint32_t *p_sci[ 2 ];
int i_ret = -1;
uint32_t p_sci_key[ 4 ] =
{
0x6E66556D, 0x6E676F70, 0x67666461, 0x33373866
};
if( p_sys_key == NULL )
if( !rw_user_key( p_drms, 1, p_user_key ) )
{
if( drms_get_sys_key( p_tmp ) )
{
return( -1 );
}
return( 0 );
}
p_sys_key = p_tmp;
if( get_sys_key( p_sys_key ) )
{
return( -1 );
}
if( get_sci_data( p_sci[ 0 ] ) )
if( get_sci_data( &p_sci[ 0 ], &i_sci_size ) )
{
return( -1 );
}
p_tmp = p_sci[ 0 ];
p_sci[ 1 ] = (uint32_t *)(((uint8_t *)p_sci[ 0 ]) + i_sci_size);
i_sci_size -= sizeof(*p_sci[ 0 ]);
init_ctx( p_ctx, p_sys_key );
for( i = 0, p_cur_key = p_sci_key;
i < sizeof(p_sci[ 0 ])/sizeof(p_sci[ 0 ][ 0 ]); i++ )
i < i_sci_size / sizeof(P_DRMS->p_key); i++ )
{
ctx_xor( p_ctx, &p_sci[ 0 ][ i ][ 0 ], &p_sci[ 1 ][ i ][ 0 ],
y = i * sizeof(*p_sci[ 0 ]);
ctx_xor( p_ctx, p_sci[ 0 ] + y + 1, p_sci[ 1 ] + y + 1,
p_drms_tab3, p_drms_tab4 );
block_xor( &p_sci[ 1 ][ i ][ 0 ], p_cur_key, &p_sci[ 1 ][ i ][ 0 ] );
block_xor( p_sci[ 1 ] + y + 1, p_cur_key, p_sci[ 1 ] + y + 1 );
p_cur_key = &p_sci[ 0 ][ i ][ 0 ];
p_cur_key = p_sci[ 0 ] + y + 1;
}
acei_init( p_acei, p_sys_key );
for( i = 0; i < sizeof(p_sci[ 1 ])/sizeof(p_sci[ 1 ][ 0 ]); i++ )
for( i = 0; i < i_sci_size / sizeof(P_DRMS->p_key); i++ )
{
acei( p_acei, (uint8_t *)&p_sci[ 1 ][ i ][ 0 ],
sizeof(p_sci[ 1 ][ i ]) );
y = i * sizeof(*p_sci[ 1 ]);
acei( p_acei, (uint8_t *)(p_sci[ 1 ] + y + 1),
sizeof(P_DRMS->p_key) );
}
memcpy( p_user_key, &p_sci[ 1 ][ 10 ][ 0 ], sizeof(p_sci[ 1 ][ i ]) );
y = 0;
i = U32_AT( &p_sci[ 1 ][ 5 ] );
i_sci_size -= 21 * sizeof(*p_sci[ 1 ]);
p_sci[ 1 ] += 22;
p_sci[ 0 ] = NULL;
return( 0 );
}
while( i_sci_size > 0 && i > 0 )
{
if( p_sci[ 0 ] == NULL )
{
i_sci_size -= 18 * sizeof(*p_sci[ 1 ]);
if( i_sci_size <= 0 )
{
break;
}
struct drms_s
{
uint8_t *p_iviv;
uint32_t i_iviv_len;
uint8_t *p_name;
uint32_t i_name_len;
p_sci[ 0 ] = p_sci[ 1 ];
y = U32_AT( &p_sci[ 1 ][ 17 ] );
p_sci[ 1 ] += 18;
}
uint32_t *p_tmp;
uint32_t i_tmp_len;
if( !y )
{
i--;
p_sci[ 0 ] = NULL;
continue;
}
uint32_t p_key[ 4 ];
uint32_t p_ctx[ 128 ];
};
if( U32_AT( &p_sci[ 0 ][ 0 ] ) == P_DRMS->i_user &&
( i_sci_size >=
(sizeof(P_DRMS->p_key) + sizeof(p_sci[ 1 ][ 0 ]) ) ) &&
( ( U32_AT( &p_sci[ 1 ][ 0 ] ) == P_DRMS->i_key ) ||
( !P_DRMS->i_key ) || ( p_sci[ 1 ] == (p_sci[ 0 ] + 18) ) ) )
{
memcpy( p_user_key, &p_sci[ 1 ][ 1 ], sizeof(P_DRMS->p_key) );
rw_user_key( p_drms, 0, p_user_key );
i_ret = 0;
break;
}
#define P_DRMS ((struct drms_s *)p_drms)
y--;
p_sci[ 1 ] += 5;
i_sci_size -= 5 * sizeof(*p_sci[ 1 ]);
}
free( (void *)p_tmp );
return( i_ret );
}
void *drms_alloc()
void *drms_alloc( char *psz_homedir )
{
struct drms_s *p_drms;
......@@ -861,6 +999,19 @@ void *drms_alloc()
free( (void *)p_drms );
p_drms = NULL;
}
p_drms->psz_homedir = malloc( MAX_PATH );
if( p_drms->psz_homedir != NULL )
{
strncpy( p_drms->psz_homedir, psz_homedir, MAX_PATH );
p_drms->psz_homedir[ MAX_PATH - 1 ] = '\0';
}
else
{
free( (void *)p_drms->p_tmp );
free( (void *)p_drms );
p_drms = NULL;
}
}
return( (void *)p_drms );
......@@ -878,6 +1029,11 @@ void drms_free( void *p_drms )
free( (void *)P_DRMS->p_iviv );
}
if( P_DRMS->psz_homedir != NULL )
{
free( (void *)P_DRMS->psz_homedir );
}
if( P_DRMS->p_tmp != NULL )
{
free( (void *)P_DRMS->p_tmp );
......@@ -925,53 +1081,65 @@ int drms_init( void *p_drms, uint32_t i_type,
switch( i_type )
{
case DRMS_INIT_UKEY:
case FOURCC_user:
{
if( i_len < sizeof(P_DRMS->i_user) )
{
i_ret = -1;
break;
}
P_DRMS->i_user = U32_AT( p_info );
}
break;
case FOURCC_key:
{
if( i_len != sizeof(P_DRMS->p_key) )
if( i_len < sizeof(P_DRMS->i_key) )
{
i_ret = -1;
break;
}
init_ctx( P_DRMS->p_ctx, (uint32_t *)p_info );
P_DRMS->i_key = U32_AT( p_info );
}
break;
case DRMS_INIT_IVIV:
case FOURCC_iviv:
{
if( i_len != sizeof(P_DRMS->p_key) )
if( i_len < sizeof(P_DRMS->p_key) )
{
i_ret = -1;
break;
}
P_DRMS->p_iviv = malloc( i_len );
P_DRMS->p_iviv = malloc( sizeof(P_DRMS->p_key) );
if( P_DRMS->p_iviv == NULL )
{
i_ret = -1;
break;
}
memcpy( P_DRMS->p_iviv, p_info, i_len );
P_DRMS->i_iviv_len = i_len;
memcpy( P_DRMS->p_iviv, p_info, sizeof(P_DRMS->p_key) );
}
break;
case DRMS_INIT_NAME:
case FOURCC_name:
{
P_DRMS->p_name = malloc( i_len );
P_DRMS->i_name_len = strlen( p_info );
P_DRMS->p_name = malloc( P_DRMS->i_name_len );
if( P_DRMS->p_name == NULL )
{
i_ret = -1;
break;
}
memcpy( P_DRMS->p_name, p_info, i_len );
P_DRMS->i_name_len = i_len;
memcpy( P_DRMS->p_name, p_info, P_DRMS->i_name_len );
}
break;
case DRMS_INIT_PRIV:
case FOURCC_priv:
{
uint32_t i;
uint32_t p_priv[ 64 ];
......@@ -985,13 +1153,21 @@ int drms_init( void *p_drms, uint32_t i_type,
TAOS_INIT( p_tmp, 0 );
taos_add3( p_tmp, P_DRMS->p_name, P_DRMS->i_name_len );
taos_add3( p_tmp, P_DRMS->p_iviv, P_DRMS->i_iviv_len );
taos_add3( p_tmp, P_DRMS->p_iviv, sizeof(P_DRMS->p_key) );
memcpy( p_priv, &p_tmp[ 4 ], sizeof(p_priv[ 0 ]) * 2 );
i = (p_tmp[ 4 ] / 8) & 63;
i = i >= 56 ? 120 - i : 56 - i;
taos_add3( p_tmp, p_drms_tab_tend, i );
taos_add3( p_tmp, (uint8_t *)p_priv, sizeof(p_priv[ 0 ]) * 2 );
if( get_user_key( p_drms, P_DRMS->p_key ) )
{
i_ret = -1;
break;
}
init_ctx( P_DRMS->p_ctx, P_DRMS->p_key );
memcpy( p_priv, p_info, 64 );
memcpy( P_DRMS->p_key, p_tmp, sizeof(P_DRMS->p_key) );
drms_decrypt( p_drms, p_priv, sizeof(p_priv) );
......@@ -999,6 +1175,8 @@ int drms_init( void *p_drms, uint32_t i_type,
init_ctx( P_DRMS->p_ctx, &p_priv[ 6 ] );
memcpy( P_DRMS->p_key, &p_priv[ 12 ], sizeof(P_DRMS->p_key) );
free( (void *)P_DRMS->psz_homedir );
P_DRMS->psz_homedir = NULL;
free( (void *)P_DRMS->p_name );
P_DRMS->p_name = NULL;
free( (void *)P_DRMS->p_iviv );
......
......@@ -2,7 +2,7 @@
* drms.h : DRMS
*****************************************************************************
* Copyright (C) 2004 VideoLAN
* $Id: drms.h,v 1.1 2004/01/05 12:37:52 jlj Exp $
* $Id: drms.h,v 1.2 2004/01/09 04:37:43 jlj Exp $
*
* Author: Jon Lech Johansen <jon-vl@nanocrew.net>
*
......@@ -21,16 +21,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#define DRMS_INIT_UKEY 0
#define DRMS_INIT_IVIV 1
#define DRMS_INIT_NAME 2
#define DRMS_INIT_PRIV 3
extern int drms_get_sys_key( uint32_t *p_sys_key );
extern int drms_get_user_key( uint32_t *p_sys_key,
uint32_t *p_user_key );
extern void *drms_alloc();
extern void *drms_alloc( char *psz_homedir );
extern void drms_free( void *p_drms );
extern int drms_init( void *p_drms, uint32_t i_type,
uint8_t *p_info, uint32_t i_len );
......
......@@ -2,7 +2,7 @@
* libmp4.c : LibMP4 library for mp4 module for vlc
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: libmp4.c,v 1.42 2004/01/08 00:37:18 fenrir Exp $
* $Id: libmp4.c,v 1.43 2004/01/09 04:37:43 jlj Exp $
*
* Author: Laurent Aimar <fenrir@via.ecp.fr>
*
......@@ -1148,77 +1148,14 @@ static int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
}
p_box->data.p_sample_soun->p_drms =
p_box->i_type == FOURCC_drms ? drms_alloc() : NULL;
if( p_box->data.p_sample_soun->p_drms )
if( p_box->i_type == FOURCC_drms )
{
FILE *file;
char *psz_homedir;
char *psz_filename;
uint32_t p_user_key[ 4 ];
int i_ret = 0;
vlc_bool_t b_key = VLC_FALSE;
psz_filename = NULL;
psz_homedir = p_stream->p_input->p_vlc->psz_homedir;
#define DRMS_FILENAME "drms"
if( psz_homedir != NULL )
{
psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/"
DRMS_FILENAME) +
strlen( psz_homedir ) );
if( psz_filename != NULL )
{
sprintf( psz_filename, "%s/" CONFIG_DIR "/" DRMS_FILENAME,
psz_homedir );
file = fopen( psz_filename, "r" );
if( file != NULL )
{
b_key = fread( p_user_key, sizeof(uint32_t),
4, file ) == 4 ? VLC_TRUE : VLC_FALSE;
fclose( file );
}
}
}
if( b_key == VLC_FALSE )
{
i_ret = drms_get_user_key( NULL, p_user_key );
}
if( !i_ret )
{
if( b_key == VLC_FALSE && psz_filename != NULL )
{
file = fopen( psz_filename, "w" );
if( file != NULL )
{
fwrite( p_user_key, sizeof(uint32_t), 4, file );
fclose( file );
}
}
p_box->data.p_sample_soun->p_drms =
drms_alloc( p_stream->p_input->p_vlc->psz_homedir );
i_ret = drms_init( p_box->data.p_sample_soun->p_drms,
DRMS_INIT_UKEY, (uint8_t *)p_user_key,
sizeof(p_user_key) );
}
if( psz_filename != NULL )
if( p_box->data.p_sample_soun->p_drms == NULL )
{
free( (void *)psz_filename );
}
if( i_ret )
{
msg_Dbg( p_stream->p_input, "drms_init( UKEY ) failed" );
drms_free( p_box->data.p_sample_soun->p_drms );
p_box->data.p_sample_soun->p_drms = NULL;
msg_Err( p_stream->p_input, "drms_alloc() failed" );
}
}
......@@ -1967,36 +1904,7 @@ static int MP4_ReadBox_rmvc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
MP4_READBOX_EXIT( 1 );
}
static int MP4_ReadBox_iviv( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
MP4_READBOX_ENTER( uint8_t );
if( i_read >= sizeof(uint32_t) * 4 )
{
MP4_Box_t *p_drms_box = p_box;
do
{
p_drms_box = p_drms_box->p_father;
} while( p_drms_box && p_drms_box->i_type != FOURCC_drms );
if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
{
if( drms_init( p_drms_box->data.p_sample_soun->p_drms,
DRMS_INIT_IVIV, p_peek, sizeof(uint32_t) * 4 ) )
{
msg_Dbg( p_stream->p_input, "drms_init( IVIV ) failed" );
drms_free( p_drms_box->data.p_sample_soun->p_drms );
p_drms_box->data.p_sample_soun->p_drms = NULL;
}
}
}
MP4_READBOX_EXIT( 1 );
}
static int MP4_ReadBox_name( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
static int MP4_ReadBox_drms( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
{
MP4_Box_t *p_drms_box = p_box;
......@@ -2010,9 +1918,10 @@ static int MP4_ReadBox_name( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
{
if( drms_init( p_drms_box->data.p_sample_soun->p_drms,
DRMS_INIT_NAME, p_peek, strlen( p_peek