/*****************************************************************************
- * update.c: VLC update and plugins download
+ * update.c: VLC update checking and downloading
*****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
- * $Id: $
+ * Copyright © 2005-2008 the VideoLAN team
+ * $Id$
*
* Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
+ * Rémi Duraffort <ivoire at via.ecp.fr>
+ Rafaël Carré <funman@videolanorg>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
-/* TODO
- * --> check release types.
- * --> make sure that the version comparision method is ok.
- */
-
/**
* \file
- * This file contains functions related to VLC and plugins update management
+ * This file contains functions related to VLC update management
*/
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <stdlib.h> /* malloc(), free() */
-#include <ctype.h> /* tolower() */
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_update.h>
-#include "vlc_update.h"
+#ifdef UPDATE_CHECK
-#include "vlc_block.h"
-#include "vlc_stream.h"
-#include "vlc_xml.h"
-#include "vlc_interaction.h"
+#include <assert.h>
+
+#include <vlc_pgpkey.h>
+#include <vlc_stream.h>
+#include <vlc_strings.h>
+#include <vlc_charset.h>
+#include <vlc_interface.h>
+
+#include <gcrypt.h>
+#include <vlc_gcrypt.h>
+
+#include "update.h"
+#include "../libvlc.h"
/*****************************************************************************
* Misc defines
*****************************************************************************/
-/* All release notes and source packages should match on "*"
- * Only binary installers are OS specific ( we only provide these
- * for Win32, Mac OS X, WincCE, beos(?) ) */
+/*
+ * Here is the format of these "status files" :
+ * First line is the last version: "X.Y.Ze" where:
+ * * X is the major number
+ * * Y is the minor number
+ * * Z is the revision number
+ * * e is an OPTIONAL extra letter
+ * * AKA "0.8.6d" or "0.9.0"
+ * Second line is an url of the binary for this last version
+ * Remaining text is a required description of the update
+ */
+
#if defined( UNDER_CE )
-# define UPDATE_VLC_OS "*"
-# define UPDATE_VLC_ARCH "*"
+# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
#elif defined( WIN32 )
-# define UPDATE_VLC_OS "windows"
-# define UPDATE_VLC_ARCH "i386"
+# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
#elif defined( __APPLE__ )
-# define UPDATE_VLC_OS "macosx"
# if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
-# define UPDATE_VLC_ARCH "ppc"
+# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
# else
-# define UPDATE_VLC_ARCH "x86"
+# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
# endif
#elif defined( SYS_BEOS )
-# define UPDATE_VLC_OS "beos"
-# define UPDATE_VLC_ARCH "i386"
+# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
#else
-# define UPDATE_VLC_OS "*"
-# define UPDATE_VLC_ARCH "*"
+# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
#endif
-#define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status.xml"
-#define UPDATE_VLC_MIRRORS_URL "http://update.videolan.org/mirrors.xml"
-
-#define FREE( a ) free(a);a=NULL;
-#define STRDUP( a ) ( a ? strdup( a ) : NULL )
/*****************************************************************************
* Local Prototypes
*****************************************************************************/
+static void EmptyRelease( update_t *p_update );
+static bool GetUpdateFile( update_t *p_update );
+static char * size_str( long int l_size );
-void FreeMirrorsList( update_t * );
-void FreeReleasesList( update_t * );
-void GetMirrorsList( update_t *, vlc_bool_t );
-void GetFilesList( update_t *, vlc_bool_t );
-int CompareReleases( struct update_release_t *, struct update_release_t * );
-int CompareReleaseToCurrent( struct update_release_t * );
+/*****************************************************************************
+ * OpenPGP functions
+ *****************************************************************************/
-unsigned int update_iterator_Reset( update_iterator_t * );
-unsigned int update_iterator_NextFile( update_iterator_t * );
-unsigned int update_iterator_PrevFile( update_iterator_t * );
-unsigned int update_iterator_NextMirror( update_iterator_t * );
-unsigned int update_iterator_PrevMirror( update_iterator_t * );
+#define packet_type( c ) ( ( c & 0x3c ) >> 2 ) /* 0x3C = 00111100 */
+#define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
-void update_iterator_GetData( update_iterator_t * );
-void update_iterator_ClearData( update_iterator_t * );
+static inline int scalar_number( uint8_t *p, int header_len )
+{
+ assert( header_len == 1 || header_len == 2 || header_len == 4 );
-/*****************************************************************************
- * Update_t functions
- *****************************************************************************/
+ if( header_len == 1 )
+ return( p[0] );
+ else if( header_len == 2 )
+ return( (p[0] << 8) + p[1] );
+ else if( header_len == 4 )
+ return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
-/**
- * Create a new update VLC struct
- *
- * \param p_this the calling vlc_object
- * \return pointer to new update_t or NULL
+ abort(); /* to shut up GCC warning */
+}
+
+/* number of data bytes in a MPI */
+#define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
+
+/*
+ * fill a public_key_packet_t structure from public key packet data
+ * verify that it is a version 4 public key packet, using DSA
*/
-update_t *__update_New( vlc_object_t *p_this )
+static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
+ size_t i_packet_len )
{
- update_t *p_update;
- if( p_this == NULL ) return NULL;
-
- p_update = (update_t *)malloc( sizeof( update_t ) );
+ if( i_packet_len > 418 || i_packet_len < 6 )
+ return VLC_EGENERIC;
- vlc_mutex_init( p_this, &p_update->lock );
+ size_t i_read = 0;
- p_update->p_vlc = p_this->p_vlc;
+ p_key->version = *p_buf++; i_read++;
+ if( p_key->version != 4 )
+ return VLC_EGENERIC;
- p_update->p_releases = NULL;
- p_update->i_releases = 0;
- p_update->b_releases = VLC_FALSE;
+ /* XXX: warn when timestamp is > date ? */
+ memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4; i_read += 4;
- p_update->p_mirrors = NULL;
- p_update->i_mirrors = 0;
- p_update->b_mirrors = VLC_FALSE;
+ p_key->algo = *p_buf++; i_read++;
+ if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
+ return VLC_EGENERIC;
- return p_update;
-}
+ /* read p */
+ if( i_read + 2 > i_packet_len )
+ return VLC_EGENERIC;
-/**
- * Delete an update_t struct
- *
- * \param p_update update_t* pointer
- * \return nothing
- */
-void update_Delete( update_t *p_update )
-{
- vlc_mutex_destroy( &p_update->lock );
- FreeMirrorsList( p_update );
- FreeReleasesList( p_update );
- free( p_update );
-}
+ int i_p_len = mpi_len( p_buf );
-/**
- * Empty the mirrors list
- * *p_update should be locked before using this function
- *
- * \param p_update pointer to the update struct
- * \return nothing
- */
-void FreeMirrorsList( update_t *p_update )
-{
- int i;
+ if( i_p_len > 128 || i_read + 2 + i_p_len > i_packet_len )
+ return VLC_EGENERIC;
- for( i = 0; i < p_update->i_mirrors; i++ )
- {
- free( p_update->p_mirrors[i].psz_name );
- free( p_update->p_mirrors[i].psz_location );
- free( p_update->p_mirrors[i].psz_type );
- free( p_update->p_mirrors[i].psz_base_url );
- }
- FREE( p_update->p_mirrors );
- p_update->i_mirrors = 0;
- p_update->b_mirrors = VLC_FALSE;
-}
+ memcpy( p_key->p, p_buf, 2+i_p_len );
+ p_buf += 2+i_p_len; i_read += 2+i_p_len;
-/**
- * Empty the releases list
- * *p_update should be locked before calling this function
- *
- * \param p_update pointer to the update struct
- * \return nothing
- */
-void FreeReleasesList( update_t *p_update )
-{
- int i;
+ /* read q */
+ if( i_read + 2 > i_packet_len )
+ return VLC_EGENERIC;
- for( i = 0; i < p_update->i_releases; i++ )
- {
- int j;
- struct update_release_t *p_release = (p_update->p_releases + i);
- for( j = 0; j < p_release->i_files; j++ )
- {
- free( p_release->p_files[j].psz_md5 );
- free( p_release->p_files[j].psz_url );
- free( p_release->p_files[j].psz_description );
- }
- free( p_release->psz_major );
- free( p_release->psz_minor );
- free( p_release->psz_revision );
- free( p_release->psz_extra );
- free( p_release->psz_svn_revision );
- free( p_release->p_files );
- }
- FREE( p_update->p_releases );
- p_update->i_releases = 0;
- p_update->b_releases = VLC_FALSE;
-}
+ int i_q_len = mpi_len( p_buf );
-/**
- * Get the mirrors list XML file and parse it
- * *p_update has to be unlocked when calling this function
- *
- * \param p_update pointer to the update struct
- * \param b_force set to VLC_TRUE if you want to force the mirrors list update
- * \return nothing
- */
-void GetMirrorsList( update_t *p_update, vlc_bool_t b_force )
-{
- stream_t *p_stream = NULL;
+ if( i_q_len > 20 || i_read+2+i_q_len > i_packet_len )
+ return VLC_EGENERIC;
- xml_t *p_xml = NULL;
- xml_reader_t *p_xml_reader = NULL;
+ memcpy( p_key->q, p_buf, 2+i_q_len );
+ p_buf += 2+i_q_len; i_read += 2+i_q_len;
- char *psz_eltname = NULL;
- //char *psz_eltvalue = NULL;
- char *psz_name = NULL;
- char *psz_value = NULL;
+ /* read g */
+ if( i_read + 2 > i_packet_len )
+ return VLC_EGENERIC;
- struct update_mirror_t tmp_mirror = {0};
+ int i_g_len = mpi_len( p_buf );
- vlc_mutex_lock( &p_update->lock );
+ if( i_g_len > 128 || i_read+2+i_g_len > i_packet_len )
+ return VLC_EGENERIC;
- if( p_update->b_mirrors && b_force == VLC_FALSE )
- {
- vlc_mutex_unlock( &p_update->lock );
- return;
- }
+ memcpy( p_key->g, p_buf, 2+i_g_len );
+ p_buf += 2+i_g_len; i_read += 2+i_g_len;
- p_xml = xml_Create( p_update->p_vlc );
- if( !p_xml )
- {
- msg_Err( p_update->p_vlc, "Failed to open XML parser" );
- goto error;
- }
+ /* read y */
+ if( i_read + 2 > i_packet_len )
+ return VLC_EGENERIC;
- p_stream = stream_UrlNew( p_update->p_vlc, UPDATE_VLC_MIRRORS_URL );
- if( !p_stream )
- {
- msg_Err( p_update->p_vlc, "Failed to open %s for reading",
- UPDATE_VLC_MIRRORS_URL );
- goto error;
- }
+ int i_y_len = mpi_len( p_buf );
- p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
- if( !p_xml_reader )
- {
- msg_Err( p_update->p_vlc, "Failed to open %s for parsing",
- UPDATE_VLC_MIRRORS_URL );
- goto error;
- }
+ if( i_y_len > 128 || i_read+2+i_y_len > i_packet_len )
+ return VLC_EGENERIC;
- if( p_update->p_mirrors )
- {
- FreeMirrorsList( p_update );
- }
+ memcpy( p_key->y, p_buf, 2+i_y_len );
+ i_read += 2+i_y_len;
- while( xml_ReaderRead( p_xml_reader ) == 1 )
- {
- switch( xml_ReaderNodeType( p_xml_reader ) )
- {
- case -1:
- msg_Err( p_update->p_vlc, "Error while parsing %s",
- UPDATE_VLC_MIRRORS_URL );
- goto error;
-
- case XML_READER_STARTELEM:
- psz_eltname = xml_ReaderName( p_xml_reader );
- if( !psz_eltname )
- {
- msg_Err( p_update->p_vlc, "Error while parsing %s",
- UPDATE_VLC_MIRRORS_URL );
- goto error;
- }
+ if( i_read != i_packet_len ) /* some extra data eh ? */
+ return VLC_EGENERIC;
- while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
- {
- psz_name = xml_ReaderName( p_xml_reader );
- psz_value = xml_ReaderValue( p_xml_reader );
+ return VLC_SUCCESS;
+}
- if( !psz_name || !psz_value )
- {
- msg_Err( p_update->p_vlc, "Error while parsing %s",
- UPDATE_VLC_MIRRORS_URL );
- goto error;
- }
+static size_t parse_signature_v3_packet( signature_packet_t *p_sig,
+ uint8_t *p_buf, size_t i_sig_len )
+{
+ size_t i_read = 1; /* we already read the version byte */
- if( !strcmp( psz_eltname, "mirror" ) )
- {
- if( !strcmp( psz_name, "name" ) )
- tmp_mirror.psz_name = STRDUP( psz_value );
- else if( !strcmp( psz_name, "location" ) )
- tmp_mirror.psz_location = STRDUP( psz_value );
- }
- else if( !strcmp( psz_eltname, "url" ) )
- {
- if( !strcmp( psz_name, "type" ) )
- tmp_mirror.psz_type = STRDUP( psz_value );
- else if( !strcmp( psz_name, "base" ) )
- tmp_mirror.psz_base_url = STRDUP( psz_value );
- }
- FREE( psz_name );
- FREE( psz_value );
- }
- if( !strcmp( psz_eltname, "url" ) )
- {
- /* append to mirrors list */
- p_update->p_mirrors =
- (struct update_mirror_t *)realloc( p_update->p_mirrors,
- (++(p_update->i_mirrors))
- *sizeof( struct update_mirror_t ) );
- p_update->p_mirrors[ p_update->i_mirrors - 1 ] =
- tmp_mirror;
- tmp_mirror.psz_name = STRDUP( tmp_mirror.psz_name );
- tmp_mirror.psz_location = STRDUP( tmp_mirror.psz_location );
- tmp_mirror.psz_type = NULL;
- tmp_mirror.psz_base_url = NULL;
- }
- FREE( psz_eltname );
- break;
+ if( i_sig_len < 19 ) /* signature is at least 19 bytes + the 2 MPIs */
+ return 0;
- case XML_READER_ENDELEM:
- psz_eltname = xml_ReaderName( p_xml_reader );
- if( !psz_eltname )
- {
- msg_Err( p_update->p_vlc, "Error while parsing %s",
- UPDATE_VLC_MIRRORS_URL );
- goto error;
- }
+ p_sig->specific.v3.hashed_data_len = *p_buf++; i_read++;
+ if( p_sig->specific.v3.hashed_data_len != 5 )
+ return 0;
- if( !strcmp( psz_eltname, "mirror" ) )
- {
- FREE( tmp_mirror.psz_name );
- FREE( tmp_mirror.psz_location );
- }
+ p_sig->type = *p_buf++; i_read++;
- FREE( psz_eltname );
- break;
+ memcpy( p_sig->specific.v3.timestamp, p_buf, 4 );
+ p_buf += 4; i_read += 4;
- /*case XML_READER_TEXT:
- psz_eltvalue = xml_ReaderValue( p_xml_reader );
- FREE( psz_eltvalue );
- break;*/
- }
- }
+ memcpy( p_sig->issuer_longid, p_buf, 8 );
+ p_buf += 8; i_read += 8;
- p_update->b_mirrors = VLC_TRUE;
+ p_sig->public_key_algo = *p_buf++; i_read++;
- error:
- vlc_mutex_unlock( &p_update->lock );
+ p_sig->digest_algo = *p_buf++; i_read++;
- free( psz_eltname );
- //free( psz_eltvalue );
- free( psz_name );
- free( psz_value );
+ p_sig->hash_verification[0] = *p_buf++; i_read++;
+ p_sig->hash_verification[1] = *p_buf++; i_read++;
- free( tmp_mirror.psz_name );
- free( tmp_mirror.psz_location );
- free( tmp_mirror.psz_type );
- free( tmp_mirror.psz_base_url );
+ assert( i_read == 19 );
- if( p_xml_reader && p_xml )
- xml_ReaderDelete( p_xml, p_xml_reader );
- if( p_stream )
- stream_Delete( p_stream );
- if( p_xml )
- xml_Delete( p_xml );
+ return i_read;
}
-/**
- * Get the files list XML file and parse it
- * *p_update has to be unlocked when calling this function
- *
- * \param p_update pointer to update struct
- * \param b_force set to VLC_TRUE if you want to force the files list update
- * \return nothing
+/*
+ * fill a signature_packet_v4_t from signature packet data
+ * verify that it was used with a DSA public key, using SHA-1 digest
*/
-void GetFilesList( update_t *p_update, vlc_bool_t b_force )
+static size_t parse_signature_v4_packet( signature_packet_t *p_sig,
+ uint8_t *p_buf, size_t i_sig_len )
{
- stream_t *p_stream = NULL;
+ size_t i_read = 1; /* we already read the version byte */
+
+ if( i_sig_len < 10 ) /* signature is at least 10 bytes + the 2 MPIs */
+ return 0;
+
+ p_sig->type = *p_buf++; i_read++;
+
+ p_sig->public_key_algo = *p_buf++; i_read++;
+
+ p_sig->digest_algo = *p_buf++; i_read++;
+
+ memcpy( p_sig->specific.v4.hashed_data_len, p_buf, 2 );
+ p_buf += 2; i_read += 2;
+
+ size_t i_hashed_data_len =
+ scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
+ i_read += i_hashed_data_len;
+ if( i_read + 4 > i_sig_len )
+ return 0;
+
+ p_sig->specific.v4.hashed_data = (uint8_t*) malloc( i_hashed_data_len );
+ if( !p_sig->specific.v4.hashed_data )
+ return 0;
+ memcpy( p_sig->specific.v4.hashed_data, p_buf, i_hashed_data_len );
+ p_buf += i_hashed_data_len;
- xml_t *p_xml = NULL;
- xml_reader_t *p_xml_reader = NULL;
+ memcpy( p_sig->specific.v4.unhashed_data_len, p_buf, 2 );
+ p_buf += 2; i_read += 2;
- char *psz_eltname = NULL;
- char *psz_eltvalue = NULL;
- char *psz_name = NULL;
- char *psz_value = NULL;
+ size_t i_unhashed_data_len =
+ scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
+ i_read += i_unhashed_data_len;
+ if( i_read + 2 > i_sig_len )
+ return 0;
- struct update_release_t *p_release = NULL;
- struct update_release_t tmp_release = {0};
- struct update_file_t tmp_file = {0};
- tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
+ p_sig->specific.v4.unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
+ if( !p_sig->specific.v4.unhashed_data )
+ return 0;
- vlc_bool_t b_os = VLC_FALSE, b_arch = VLC_FALSE;
+ memcpy( p_sig->specific.v4.unhashed_data, p_buf, i_unhashed_data_len );
+ p_buf += i_unhashed_data_len;
- vlc_mutex_lock( &p_update->lock );
+ memcpy( p_sig->hash_verification, p_buf, 2 );
+ p_buf += 2; i_read += 2;
- if( p_update->b_releases && b_force == VLC_FALSE )
+ uint8_t *p, *max_pos;
+ p = p_sig->specific.v4.unhashed_data;
+ max_pos = p + scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
+
+ for( ;; )
{
- vlc_mutex_unlock( &p_update->lock );
- return;
+ if( p > max_pos )
+ return 0;
+
+ size_t i_subpacket_len;
+ if( *p < 192 )
+ {
+ if( p + 1 > max_pos )
+ return 0;
+ i_subpacket_len = *p++;
+ }
+ else if( *p < 255 )
+ {
+ if( p + 2 > max_pos )
+ return 0;
+ i_subpacket_len = (*p++ - 192) << 8;
+ i_subpacket_len += *p++ + 192;
+ }
+ else
+ {
+ if( p + 4 > max_pos )
+ return 0;
+ i_subpacket_len = *++p << 24;
+ i_subpacket_len += *++p << 16;
+ i_subpacket_len += *++p << 8;
+ i_subpacket_len += *++p;
+ }
+
+ if( *p == ISSUER_SUBPACKET )
+ {
+ if( p + 9 > max_pos )
+ return 0;
+
+ memcpy( &p_sig->issuer_longid, p+1, 8 );
+
+ return i_read;
+ }
+
+ p += i_subpacket_len;
}
+}
+
+static int parse_signature_packet( signature_packet_t *p_sig,
+ uint8_t *p_buf, size_t i_sig_len )
+{
+ if( !i_sig_len ) /* 1st sanity check, we need at least the version */
+ return VLC_EGENERIC;
+
+ p_sig->version = *p_buf++;
- p_xml = xml_Create( p_update->p_vlc );
- if( !p_xml )
+ size_t i_read;
+ switch( p_sig->version )
{
- msg_Err( p_update->p_vlc, "Failed to open XML parser" );
- goto error;
+ case 3:
+ i_read = parse_signature_v3_packet( p_sig, p_buf, i_sig_len );
+ break;
+ case 4:
+ p_sig->specific.v4.hashed_data = NULL;
+ p_sig->specific.v4.unhashed_data = NULL;
+ i_read = parse_signature_v4_packet( p_sig, p_buf, i_sig_len );
+ break;
+ default:
+ return VLC_EGENERIC;
}
- p_stream = stream_UrlNew( p_update->p_vlc, UPDATE_VLC_STATUS_URL );
- if( !p_stream )
- {
- msg_Err( p_update->p_vlc, "Failed to open %s for reading",
- UPDATE_VLC_STATUS_URL );
+ if( i_read == 0 ) /* signature packet parsing has failed */
goto error;
- }
- p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
+ if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
+ goto error;
- if( !p_xml_reader )
- {
- msg_Err( p_update->p_vlc, "Failed to open %s for parsing",
- UPDATE_VLC_STATUS_URL );
+ if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
goto error;
- }
- if( p_update->p_releases )
+ switch( p_sig->type )
{
- FreeReleasesList( p_update );
+ case BINARY_SIGNATURE:
+ case TEXT_SIGNATURE:
+ case GENERIC_KEY_SIGNATURE:
+ case PERSONA_KEY_SIGNATURE:
+ case CASUAL_KEY_SIGNATURE:
+ case POSITIVE_KEY_SIGNATURE:
+ break;
+ default:
+ goto error;
}
- while( xml_ReaderRead( p_xml_reader ) == 1 )
- {
- switch( xml_ReaderNodeType( p_xml_reader ) )
- {
- case -1:
- msg_Err( p_update->p_vlc, "Error while parsing %s",
- UPDATE_VLC_STATUS_URL );
- goto error;
-
- case XML_READER_STARTELEM:
- psz_eltname = xml_ReaderName( p_xml_reader );
- if( !psz_eltname )
- {
- msg_Err( p_update->p_vlc, "Error while parsing %s",
- UPDATE_VLC_STATUS_URL );
- goto error;
- }
-
- while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
- {
- psz_name = xml_ReaderName( p_xml_reader );
- psz_value = xml_ReaderValue( p_xml_reader );
+ p_buf--; /* rewind to the version byte */
+ p_buf += i_read;
- if( !psz_name || !psz_value )
- {
- msg_Err( p_update->p_vlc, "Error while parsing %s",
- UPDATE_VLC_STATUS_URL );
- goto error;
- }
+ if( i_read + 2 > i_sig_len )
+ goto error;
- if( b_os && b_arch )
- {
- if( strcmp( psz_eltname, "version" ) == 0 )
- {
- if( !strcmp( psz_name, "major" ) )
- tmp_release.psz_major = STRDUP( psz_value );
- else if( !strcmp( psz_name, "minor" ) )
- tmp_release.psz_minor = STRDUP( psz_value );
- else if( !strcmp( psz_name, "revision" ) )
- tmp_release.psz_revision = STRDUP( psz_value );
- else if( !strcmp( psz_name, "extra" ) )
- tmp_release.psz_extra = STRDUP( psz_value );
- else if( !strcmp( psz_name, "svn" ) )
- tmp_release.psz_svn_revision =
- STRDUP( psz_value );
- else if( !strcmp( psz_name, "version" ) )
- {
- if( !strcmp( psz_value, "unstable" ) )
- tmp_release.i_type =
- UPDATE_RELEASE_TYPE_UNSTABLE;
- else if( !strcmp( psz_value, "testing" ) )
- tmp_release.i_type =
- UPDATE_RELEASE_TYPE_TESTING;
- else
- tmp_release.i_type =
- UPDATE_RELEASE_TYPE_STABLE;
- }
- }
- else if( !strcmp( psz_eltname, "file" ) )
- {
- if( !strcmp( psz_name, "type" ) )
- {
- if( !strcmp( psz_value, "info" ) )
- tmp_file.i_type = UPDATE_FILE_TYPE_INFO;
- else if( !strcmp( psz_value, "source" ) )
- tmp_file.i_type = UPDATE_FILE_TYPE_SOURCE;
- else if( !strcmp( psz_value, "binary" ) )
- tmp_file.i_type = UPDATE_FILE_TYPE_BINARY;
- else if( !strcmp( psz_value, "plugin" ) )
- tmp_file.i_type = UPDATE_FILE_TYPE_PLUGIN;
- else
- tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF;
- }
- else if( !strcmp( psz_name, "md5" ) )
- tmp_file.psz_md5 = STRDUP( psz_value );
- else if( !strcmp( psz_name, "size" ) )
- tmp_file.l_size = atol( psz_value );
- else if( !strcmp( psz_name, "url" ) )
- tmp_file.psz_url = STRDUP( psz_value );
- }
- }
- if( !strcmp( psz_name, "name" )
- && ( !strcmp( psz_value, UPDATE_VLC_OS )
- || !strcmp( psz_value, "*" ) )
- && !strcmp( psz_eltname, "os" ) )
- {
- b_os = VLC_TRUE;
- }
- if( b_os && !strcmp( psz_name, "name" )
- && ( !strcmp( psz_value, UPDATE_VLC_ARCH )
- || !strcmp( psz_value, "*" ) )
- && !strcmp( psz_eltname, "arch" ) )
- {
- b_arch = VLC_TRUE;
- }
- FREE( psz_name );
- FREE( psz_value );
- }
- if( ( b_os && b_arch && strcmp( psz_eltname, "arch" ) ) )
- {
- if( !strcmp( psz_eltname, "version" ) )
- {
- int i;
- /* look for a previous occurence of this release */
- for( i = 0; i < p_update->i_releases; i++ )
- {
- p_release = p_update->p_releases + i;
- if( CompareReleases( p_release, &tmp_release )
- == UPDATE_RELEASE_STATUS_EQUAL )
- {
- break;
- }
- }
- /* if this is the first time that we see this release,
- * append it to the list of releases */
- if( i == p_update->i_releases )
- {
- tmp_release.i_status =
- CompareReleaseToCurrent( &tmp_release );
- p_update->p_releases =
- (struct update_release_t *)realloc( p_update->p_releases,
- (++(p_update->i_releases))*sizeof( struct update_release_t ) );
- p_update->p_releases[ p_update->i_releases - 1 ] =
- tmp_release;
- p_release =
- p_update->p_releases + p_update->i_releases - 1;
- tmp_release.psz_major = NULL;
- tmp_release.psz_minor = NULL;
- tmp_release.psz_revision = NULL;
- tmp_release.psz_extra = NULL;
- tmp_release.psz_svn_revision = NULL;
- tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
- tmp_release.i_status = 0;
- tmp_release.p_files = NULL;
- tmp_release.i_files = 0;
- }
- else
- {
- FREE( tmp_release.psz_major );
- FREE( tmp_release.psz_minor );
- FREE( tmp_release.psz_revision );
- FREE( tmp_release.psz_extra );
- FREE( tmp_release.psz_svn_revision );
- tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
- FREE( tmp_release.p_files );
- tmp_release.i_files = 0;
- }
- }
- else if( !strcmp( psz_eltname, "file" ) )
- {
- /* append file to p_release's file list */
- if( p_release == NULL )
- {
- goto error;
- }
- p_release->p_files =
- (struct update_file_t *)realloc( p_release->p_files,
- (++(p_release->i_files))*sizeof( struct update_file_t ) );
- p_release->p_files[ p_release->i_files - 1 ] = tmp_file;
- tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF;
- tmp_file.psz_md5 = NULL;
- tmp_file.l_size = 0;
- tmp_file.psz_url = NULL;
- tmp_file.psz_description = NULL;
- }
- }
- FREE( psz_eltname );
- break;
+ size_t i_r_len = mpi_len( p_buf ); i_read += 2;
+ if( i_read + i_r_len > i_sig_len || i_r_len > 20 )
+ goto error;
- case XML_READER_ENDELEM:
- psz_eltname = xml_ReaderName( p_xml_reader );
- if( !psz_eltname )
- {
- msg_Err( p_update->p_vlc, "Error while parsing %s",
- UPDATE_VLC_STATUS_URL );
- goto error;
- }
+ memcpy( p_sig->r, p_buf, 2 + i_r_len );
+ p_buf += 2 + i_r_len;
+ i_read += i_r_len;
- if( !strcmp( psz_eltname, "os" ) )
- b_os = VLC_FALSE;
- else if( !strcmp( psz_eltname, "arch" ) )
- b_arch = VLC_FALSE;
- FREE( psz_eltname );
- break;
+ if( i_read + 2 > i_sig_len )
+ goto error;
- case XML_READER_TEXT:
- psz_eltvalue = xml_ReaderValue( p_xml_reader );
- if( p_release && p_release->i_files )
- p_release->p_files[ p_release->i_files - 1 ]
- .psz_description = STRDUP( psz_eltvalue );
- FREE( psz_eltvalue );
- break;
- }
- }
+ size_t i_s_len = mpi_len( p_buf ); i_read += 2;
+ if( i_read + i_s_len > i_sig_len || i_s_len > 20 )
+ goto error;
- p_update->b_releases = VLC_TRUE;
+ memcpy( p_sig->s, p_buf, 2 + i_s_len );
+ p_buf += 2 + i_s_len;
+ i_read += i_s_len;
- error:
- vlc_mutex_unlock( &p_update->lock );
+ assert( i_read == i_sig_len );
+ if( i_read < i_sig_len ) /* some extra data, hm ? */
+ goto error;
- free( psz_eltname );
- free( psz_eltvalue );
- free( psz_name );
- free( psz_value );
+ return VLC_SUCCESS;
- free( tmp_release.psz_major );
- free( tmp_release.psz_minor );
- free( tmp_release.psz_revision );
- free( tmp_release.psz_extra );
- free( tmp_release.psz_svn_revision );
+error:
- free( tmp_file.psz_md5 );
- free( tmp_file.psz_url );
- free( tmp_file.psz_description );
+ if( p_sig->version == 4 )
+ {
+ free( p_sig->specific.v4.hashed_data );
+ free( p_sig->specific.v4.unhashed_data );
+ }
- if( p_xml_reader && p_xml )
- xml_ReaderDelete( p_xml, p_xml_reader );
- if( p_stream )
- stream_Delete( p_stream );
- if( p_xml )
- xml_Delete( p_xml );
+ return VLC_EGENERIC;
}
-/**
- * Check for updates
- *
- * \param p_update pointer to update struct
- * \param b_force set to VLC_TRUE if you want to force the update
- * \returns nothing
+/*
+ * crc_octets() was lamely copied from rfc 2440
+ * Copyright (C) The Internet Society (1998). All Rights Reserved.
*/
-void update_Check( update_t *p_update, vlc_bool_t b_force )
-{
- if( p_update == NULL ) return;
- GetMirrorsList( p_update, b_force );
- GetFilesList( p_update, b_force );
-}
+#define CRC24_INIT 0xB704CEL
+#define CRC24_POLY 0x1864CFBL
-/**
- * Compare two release numbers
- * The comparision algorith basically performs an alphabetical order (strcmp)
- * comparision of each of the version number elements until it finds two
- * different ones. This is the tricky function.
- *
- * \param p1 first release
- * \param p2 second release
- * \return like strcmp
- */
-int CompareReleases( struct update_release_t *p1, struct update_release_t *p2 )
+static long crc_octets( uint8_t *octets, size_t len )
{
- int d;
- if( ( d = strcmp( p1->psz_major, p2->psz_major ) ) ) ;
- else if( ( d = strcmp( p1->psz_minor, p2->psz_minor ) ) ) ;
- else if( ( d = strcmp( p1->psz_revision, p2->psz_revision ) ) ) ;
- else
+ long crc = CRC24_INIT;
+ int i;
+ while (len--)
{
- d = strcmp( p1->psz_extra, p2->psz_extra );
- if( d<0 )
+ crc ^= (*octets++) << 16;
+ for (i = 0; i < 8; i++)
{
- /* FIXME:
- * not num < NULL < num
- * -test and -svn releases are thus always considered older than
- * -'' or -0 releases, which is the best i could come up with */
- char *psz_end1;
- char *psz_end2;
- strtol( p1->psz_extra, &psz_end1, 10 );
- strtol( p2->psz_extra, &psz_end2, 10 );
- if( psz_end2 == p2->psz_extra
- && ( psz_end1 != p1->psz_extra || *p1->psz_extra == '\0' ) )
- d = 1;
+ crc <<= 1;
+ if (crc & 0x1000000)
+ crc ^= CRC24_POLY;
}
}
- if( d < 0 )
- return UPDATE_RELEASE_STATUS_OLDER;
- else if( d == 0 )
- return UPDATE_RELEASE_STATUS_EQUAL;
- else
- return UPDATE_RELEASE_STATUS_NEWER;
+ return crc & 0xFFFFFFL;
}
-/**
- * Compare a given release's version number to the current VLC's one
- *
- * \param p a release
- * \return >0 if newer, 0 if equal and <0 if older
+/*
+ * Transform an armored document in binary format
+ * Used on public keys and signatures
*/
-int CompareReleaseToCurrent( struct update_release_t *p )
+static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
+ uint8_t *p_obuf, size_t i_obuf_len )
{
- struct update_release_t c = {0};
- int r;
- c.psz_major = STRDUP( PACKAGE_VERSION_MAJOR );
- c.psz_minor = STRDUP( PACKAGE_VERSION_MINOR );
- c.psz_revision = STRDUP( PACKAGE_VERSION_REVISION );
- c.psz_extra = STRDUP( PACKAGE_VERSION_EXTRA );
- r = CompareReleases( p, &c );
- free( c.psz_major );
- free( c.psz_minor );
- free( c.psz_revision );
- free( c.psz_extra );
- return r;
-}
+ char *p_ipos = p_ibuf;
+ uint8_t *p_opos = p_obuf;
+ int i_end = 0;
+ int i_header_skipped = 0;
-/*****************************************************************************
- * Updatei_iterator_t functions
- *****************************************************************************/
+ while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
+ {
+ if( *p_ipos == '\r' || *p_ipos == '\n' )
+ {
+ p_ipos++;
+ continue;
+ }
-/**
- * Create a new update iterator structure. This structure can then be used to
- * describe a position and move through the update and mirror trees/lists.
- * This will use an existing update struct or create a new one if none is
- * found
- *
- * \param p_u the calling update_t
- * \return a pointer to an update iterator
- */
-update_iterator_t *update_iterator_New( update_t *p_u )
-{
- update_iterator_t *p_uit = NULL;
+ size_t i_line_len = strcspn( p_ipos, "\r\n" );
+ if( i_line_len == 0 )
+ continue;
- if( p_u == NULL )
- return NULL;
+ if( !i_header_skipped )
+ {
+ if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
+ i_header_skipped = 1;
- p_uit = (update_iterator_t *)malloc( sizeof( update_iterator_t ) );
- if( p_uit == NULL ) return NULL;
+ p_ipos += i_line_len + 1;
+ continue;
+ }
- p_uit->p_u = p_u;
+ if( !strncmp( p_ipos, "Version:", 8 ) )
+ {
+ p_ipos += i_line_len + 1;
+ continue;
+ }
- p_uit->i_m = -1;
- p_uit->i_r = -1;
- p_uit->i_f = -1;
+ if( p_ipos[i_line_len - 1] == '=' )
+ {
+ i_end = 1;
+ p_ipos[i_line_len - 1] = '\0';
+ }
+ else
+ p_ipos[i_line_len] = '\0';
- p_uit->i_t = UPDATE_FILE_TYPE_ALL;
- p_uit->i_rs = UPDATE_RELEASE_STATUS_ALL;
- p_uit->i_rt = UPDATE_RELEASE_TYPE_STABLE;
+ p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
+ p_obuf - p_opos + i_obuf_len, p_ipos );
+ p_ipos += i_line_len + 1;
+ }
- p_uit->file.i_type = UPDATE_FILE_TYPE_NONE;
- p_uit->file.psz_md5 = NULL;
- p_uit->file.psz_url = NULL;
- p_uit->file.l_size = 0;
- p_uit->file.psz_description = NULL;
+ /* XXX: the CRC is OPTIONAL, really require it ? */
+ if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
+ return 0;
- p_uit->release.psz_version = NULL;
- p_uit->release.psz_svn_revision = NULL;
- p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE;
- p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE;
+ uint8_t p_crc[3];
+ if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
+ return 0;
- p_uit->mirror.psz_name = NULL;
- p_uit->mirror.psz_location = NULL;
- p_uit->mirror.psz_type = NULL;
+ long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
+ long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
- return p_uit;
+ return l_crc2 == l_crc ? p_opos - p_obuf : 0;
}
-/**
- * Delete an update iterator structure (duh!)
- *
- * \param p_uit pointer to an update iterator
- * \return nothing
+/*
+ * Download the signature associated to a document or a binary file.
+ * We're given the file's url, we just append ".asc" to it and download
*/
-void update_iterator_Delete( update_iterator_t *p_uit )
+static int download_signature( vlc_object_t *p_this,
+ signature_packet_t *p_sig,
+ const char *psz_url )
{
- if( !p_uit ) return;
- update_iterator_ClearData( p_uit );
- free( p_uit );
-}
+ char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
+ if( !psz_sig )
+ return VLC_ENOMEM;
-/**
- * Reset an update_iterator_t structure
- *
- * \param p_uit pointer to an update iterator
- * \return UPDATE_FAIL upon error, UPDATE_SUCCESS otherwise
- */
-unsigned int update_iterator_Reset( update_iterator_t *p_uit )
-{
- if( !p_uit ) return UPDATE_FAIL;
+ strcpy( psz_sig, psz_url );
+ strcat( psz_sig, ".asc" );
- p_uit->i_r = -1;
- p_uit->i_f = -1;
- p_uit->i_m = -1;
+ stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
+ free( psz_sig );
- update_iterator_ClearData( p_uit );
- return UPDATE_SUCCESS;
-}
+ if( !p_stream )
+ return VLC_ENOMEM;
-/**
- * Finds the next file in the update tree that matches status and type
- * requirements set in the update_iterator
- *
- * \param p_uit update iterator
- * \return UPDATE_FAIL if we can't find the next file, UPDATE_SUCCESS|UPDATE_FILE if we stay in the same release, UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE if we change the release index
- */
-unsigned int update_iterator_NextFile( update_iterator_t *p_uit )
-{
- int r,f=-1,old_r;
+ int64_t i_size = stream_Size( p_stream );
- if( !p_uit ) return UPDATE_FAIL;
+ msg_Dbg( p_this, "Downloading signature (%"PRId64" bytes)", i_size );
+ uint8_t *p_buf = (uint8_t*)malloc( i_size );
+ if( !p_buf )
+ {
+ stream_Delete( p_stream );
+ return VLC_ENOMEM;
+ }
- old_r=p_uit->i_r;
+ int i_read = stream_Read( p_stream, p_buf, (int)i_size );
- /* if the update iterator was already in a "no match" state, start over */
- if( p_uit->i_r == -1 ) p_uit->i_r = 0;
- //if( p_uit->i_f == -1 ) p_uit->i_f = 0;
+ stream_Delete( p_stream );
- vlc_mutex_lock( &p_uit->p_u->lock );
+ if( i_read != (int)i_size )
+ {
+ msg_Dbg( p_this,
+ "Couldn't download full signature (only %d bytes)", i_read );
+ free( p_buf );
+ return VLC_EGENERIC;
+ }
- for( r = p_uit->i_r; r < p_uit->p_u->i_releases; r++ )
+ if( (uint8_t)*p_buf < 0x80 ) /* ASCII */
{
- if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue;
- for( f = ( r == p_uit->i_r ? p_uit->i_f + 1 : 0 );
- f < p_uit->p_u->p_releases[r].i_files; f++ )
+ msg_Dbg( p_this, "Unarmoring signature" );
+
+ uint8_t* p_unarmored = (uint8_t*) malloc( ( i_size * 3 ) / 4 + 1 );
+ if( !p_unarmored )
{
- if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t )
- {
- goto done;/* "double break" */
- }
+ free( p_buf );
+ return VLC_EGENERIC;
}
+
+ int i_bytes = pgp_unarmor( (char*)p_buf, i_size, p_unarmored, i_size );
+ free( p_buf );
+
+ p_buf = p_unarmored;
+ i_size = i_bytes;
+
+ if( i_bytes < 2 )
+ {
+ free( p_buf );
+ msg_Dbg( p_this, "Unarmoring failed : corrupted signature ?" );
+ return VLC_EGENERIC;
+ }
+ }
+
+ if( packet_type( *p_buf ) != SIGNATURE_PACKET )
+ {
+ free( p_buf );
+ msg_Dbg( p_this, "Not a signature: %d", *p_buf );
+ return VLC_EGENERIC;
}
- done:
- p_uit->i_r = r;
- p_uit->i_f = f;
- r = p_uit->p_u->i_releases;
+ size_t i_header_len = packet_header_len( *p_buf );
+ if( ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) ||
+ i_header_len + 1 > (size_t)i_size )
+ {
+ free( p_buf );
+ msg_Dbg( p_this, "Invalid signature packet header" );
+ return VLC_EGENERIC;
+ }
- if( old_r == p_uit->i_r )
+ size_t i_len = scalar_number( p_buf+1, i_header_len );
+ if( i_len + i_header_len + 1 != (size_t)i_size )
{
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
- return UPDATE_SUCCESS|UPDATE_FILE;
+ free( p_buf );
+ msg_Dbg( p_this, "Invalid signature packet" );
+ return VLC_EGENERIC;
}
- else if( p_uit->i_r == r )
+
+ int i_ret = parse_signature_packet( p_sig, p_buf+1+i_header_len, i_len );
+ free( p_buf );
+ if( i_ret != VLC_SUCCESS )
{
- p_uit->i_r = -1;
- p_uit->i_f = -1;
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
- return UPDATE_FAIL;
+ msg_Dbg( p_this, "Couldn't parse signature" );
+ return i_ret;
}
- else
+
+ if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
{
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
- return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE;
+ msg_Dbg( p_this, "Invalid signature type: %d", p_sig->type );
+ if( p_sig->version == 4 )
+ {
+ free( p_sig->specific.v4.hashed_data );
+ free( p_sig->specific.v4.unhashed_data );
+ }
+ return VLC_EGENERIC;
}
+
+ return VLC_SUCCESS;
}
-/**
- * Finds the previous file in the update tree that matches status and type
- * requirements set in the update_iterator
- *
- * \param p_uit update iterator
- * \return UPDATE_FAIL if we can't find the previous file, UPDATE_SUCCESS|UPDATE_FILE if we stay in the same release, UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE if we change the release index
+/*
+ * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
+ */
+static int verify_signature( uint8_t *p_r, uint8_t *p_s,
+ public_key_packet_t *p_key, uint8_t *p_hash )
+{
+ /* the data to be verified (a SHA-1 hash) */
+ const char *hash_sexp_s = "(data(flags raw)(value %m))";
+ /* the public key */
+ const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
+ /* the signature */
+ const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
+
+ size_t erroff;
+ gcry_mpi_t p, q, g, y, r, s, hash;
+ p = q = g = y = r = s = hash = NULL;
+ gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
+ key_sexp = hash_sexp = sig_sexp = NULL;
+
+ int i_p_len = mpi_len( p_key->p );
+ int i_q_len = mpi_len( p_key->q );
+ int i_g_len = mpi_len( p_key->g );
+ int i_y_len = mpi_len( p_key->y );
+ if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, i_p_len, NULL ) ||
+ gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, i_q_len, NULL ) ||
+ gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, i_g_len, NULL ) ||
+ gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, i_y_len, NULL ) ||
+ gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
+ goto problem;
+
+ int i_r_len = mpi_len( p_r );
+ int i_s_len = mpi_len( p_s );
+ if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, i_r_len, NULL ) ||
+ gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, i_s_len, NULL ) ||
+ gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
+ goto problem;
+
+ int i_hash_len = 20;
+ if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
+ gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
+ goto problem;
+
+ if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
+ goto problem;
+
+ return VLC_SUCCESS;
+
+problem:
+ if( p ) gcry_mpi_release( p );
+ if( q ) gcry_mpi_release( q );
+ if( g ) gcry_mpi_release( g );
+ if( y ) gcry_mpi_release( y );
+ if( r ) gcry_mpi_release( r );
+ if( s ) gcry_mpi_release( s );
+ if( hash ) gcry_mpi_release( hash );
+ if( key_sexp ) gcry_sexp_release( key_sexp );
+ if( sig_sexp ) gcry_sexp_release( sig_sexp );
+ if( hash_sexp ) gcry_sexp_release( hash_sexp );
+ return VLC_EGENERIC;
+}
+
+/*
+ * fill a public_key_t with public key data, including:
+ * * public key packet
+ * * signature packet issued by key which long id is p_sig_issuer
+ * * user id packet
*/
-//TODO: test
-unsigned int update_iterator_PrevFile( update_iterator_t *p_uit )
+static int parse_public_key( const uint8_t *p_key_data, size_t i_key_len,
+ public_key_t *p_key, const uint8_t *p_sig_issuer )
{
- int r,f=-1,old_r;
+ uint8_t *pos = (uint8_t*) p_key_data;
+ uint8_t *max_pos = pos + i_key_len;
- if( !p_uit ) return UPDATE_FAIL;
+ int i_status = 0;
+#define PUBLIC_KEY_FOUND 0x01
+#define USER_ID_FOUND 0x02
+#define SIGNATURE_FOUND 0X04
- old_r=p_uit->i_r;
+ uint8_t *p_key_unarmored = NULL;
- /* if the update iterator was already in a "no match" state, start over
- * (begin at the end of the list) */
- if( p_uit->i_r == -1 ) p_uit->i_r = p_uit->p_u->i_releases - 1;
- p_uit->i_f = p_uit->p_u->p_releases[p_uit->i_r].i_files + 1;
+ p_key->psz_username = NULL;
+ p_key->sig.specific.v4.hashed_data = NULL;
+ p_key->sig.specific.v4.unhashed_data = NULL;
- vlc_mutex_lock( &p_uit->p_u->lock );
+ if( !( *pos & 0x80 ) )
+ { /* first byte is ASCII, unarmoring */
+ p_key_unarmored = (uint8_t*)malloc( i_key_len );
+ if( !p_key_unarmored )
+ return VLC_ENOMEM;
+ int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
+ p_key_unarmored, i_key_len );
- for( r = p_uit->i_r; r >= 0; r-- )
+ if( i_len == 0 )
+ goto error;
+
+ pos = p_key_unarmored;
+ max_pos = pos + i_len;
+ }
+
+ while( pos < max_pos )
{
- if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue;
- for( f =( r==p_uit->i_r ? p_uit->i_f - 1 : p_uit->p_u->p_releases[r].i_files );
- f >= 0; f-- )
+ if( !(*pos & 0x80) || *pos & 0x40 )
+ goto error;
+
+ int i_type = packet_type( *pos );
+
+ int i_header_len = packet_header_len( *pos++ );
+ if( pos + i_header_len > max_pos ||
+ ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) )
+ goto error;
+
+ int i_packet_len = scalar_number( pos, i_header_len );
+ pos += i_header_len;
+
+ if( pos + i_packet_len > max_pos )
+ goto error;
+
+ switch( i_type )
{
- if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t )
- {
- goto done;/* "double break" */
- }
+ case PUBLIC_KEY_PACKET:
+ i_status |= PUBLIC_KEY_FOUND;
+ if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
+ goto error;
+ break;
+
+ case SIGNATURE_PACKET: /* we accept only v4 signatures here */
+ if( i_status & SIGNATURE_FOUND || !p_sig_issuer )
+ break;
+ int i_ret = parse_signature_packet( &p_key->sig, pos,
+ i_packet_len );
+ if( i_ret == VLC_SUCCESS )
+ {
+ if( p_key->sig.version != 4 )
+ break;
+ if( memcmp( p_key->sig.issuer_longid, p_sig_issuer, 8 ) )
+ {
+ free( p_key->sig.specific.v4.hashed_data );
+ free( p_key->sig.specific.v4.unhashed_data );
+ p_key->sig.specific.v4.hashed_data = NULL;
+ p_key->sig.specific.v4.unhashed_data = NULL;
+ break;
+ }
+ i_status |= SIGNATURE_FOUND;
+ }
+ break;
+
+ case USER_ID_PACKET:
+ if( p_key->psz_username ) /* save only the first User ID */
+ break;
+ i_status |= USER_ID_FOUND;
+ p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
+ if( !p_key->psz_username )
+ goto error;
+
+ memcpy( p_key->psz_username, pos, i_packet_len );
+ p_key->psz_username[i_packet_len] = '\0';
+ break;
+
+ default:
+ break;
}
+ pos += i_packet_len;
}
- done:
- p_uit->i_r = r;
- p_uit->i_f = f;
+ free( p_key_unarmored );
+
+ if( !( i_status & ( PUBLIC_KEY_FOUND | USER_ID_FOUND ) ) )
+ return VLC_EGENERIC;
+
+ if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
+ return VLC_EGENERIC;
- r = p_uit->p_u->i_releases;
+ return VLC_SUCCESS;
- if( old_r == p_uit->i_r )
+error:
+ if( p_key->sig.version == 4 )
{
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
- return UPDATE_SUCCESS|UPDATE_FILE;
+ free( p_key->sig.specific.v4.hashed_data );
+ free( p_key->sig.specific.v4.unhashed_data );
}
- else if( p_uit->i_r == -1 )
+ free( p_key->psz_username );
+ free( p_key_unarmored );
+ return VLC_EGENERIC;
+}
+
+/*
+ * return a sha1 hash of a file
+ */
+static uint8_t *hash_sha1_from_file( const char *psz_file,
+ signature_packet_t *p_sig )
+{
+ if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
+ return NULL;
+
+ FILE *f = utf8_fopen( psz_file, "r" );
+ if( !f )
+ return NULL;
+
+ uint8_t buffer[4096];
+
+ gcry_md_hd_t hd;
+ if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
{
- p_uit->i_r = -1;
- p_uit->i_f = -1;
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
- return UPDATE_FAIL;
+ fclose( f );
+ return NULL;
}
- else
+
+ size_t i_read;
+ while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
+ gcry_md_write( hd, buffer, i_read );
+
+ if( p_sig->version == 3 )
{
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
- return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE;
+ gcry_md_putc( hd, p_sig->type );
+ gcry_md_write( hd, &p_sig->specific.v3.timestamp, 4 );
+ }
+ else if( p_sig->version == 4 )
+ {
+ gcry_md_putc( hd, p_sig->version );
+ gcry_md_putc( hd, p_sig->type );
+ gcry_md_putc( hd, p_sig->public_key_algo );
+ gcry_md_putc( hd, p_sig->digest_algo );
+ gcry_md_write( hd, p_sig->specific.v4.hashed_data_len, 2 );
+ size_t i_len = scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
+ gcry_md_write( hd, p_sig->specific.v4.hashed_data, i_len );
+ }
+ else
+ { /* RFC 4880 only tells about versions 3 and 4 */
+ gcry_md_close( hd );
+ return NULL;
}
-}
-/**
- * Finds the next mirror in the update tree
- *
- * \param update iterator
- * \return UPDATE_FAIL if we can't find the next mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise
- */
-unsigned int update_iterator_NextMirror( update_iterator_t *p_uit )
-{
- if( !p_uit ) return UPDATE_FAIL;
- vlc_mutex_lock( &p_uit->p_u->lock );
- p_uit->i_m++;
- if( p_uit->i_m >= p_uit->p_u->i_mirrors ) p_uit->i_m = -1;
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
- return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR;
-}
+ fclose( f );
+ gcry_md_final( hd );
-/**
- * Finds the previous mirror in the update tree
- *
- * \param update iterator
- * \return UPDATE_FAIL if we can't find a previous mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise
- */
-unsigned int update_iterator_PrevMirror( update_iterator_t *p_uit )
-{
- if( !p_uit ) return UPDATE_FAIL;
- vlc_mutex_lock( &p_uit->p_u->lock );
- p_uit->i_m--;
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
- return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR;
+ uint8_t *p_tmp = (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1);
+ uint8_t *p_hash = malloc( 20 );
+ if( p_hash )
+ memcpy( p_hash, p_tmp, 20 );
+ gcry_md_close( hd );
+ return p_hash;
}
-/**
- * Change the update iterator's position in the file and mirrors tree
- * If position is negative, don't change it
- *
- * \param i_m position in mirrors list
- * \param i_r position in releases list
- * \param i_f position in release's files list
- * \return UPDATE_FAIL when changing position fails or position wasn't changed, a combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE otherwise
+/*
+ * download a public key (the last one) from videolan server, and parse it
*/
-unsigned int update_iterator_ChooseMirrorAndFile( update_iterator_t *p_uit,
- int i_m, int i_r, int i_f )
+static public_key_t *download_key( vlc_object_t *p_this,
+ const uint8_t *p_longid, const uint8_t *p_signature_issuer )
{
- unsigned int i_val = 0;
+ char *psz_url;
+ if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
+ p_longid[0], p_longid[1], p_longid[2], p_longid[3],
+ p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
+ return NULL;
- if( !p_uit ) return 0;
- vlc_mutex_lock( &p_uit->p_u->lock );
+ stream_t *p_stream = stream_UrlNew( p_this, psz_url );
+ free( psz_url );
+ if( !p_stream )
+ return NULL;
- if( i_m >= 0 )
+ int64_t i_size = stream_Size( p_stream );
+ if( i_size < 0 )
{
- if( i_m < p_uit->p_u->i_mirrors )
- {
- if( i_m != p_uit->i_m )
- i_val |= UPDATE_MIRROR;
- p_uit->i_m = i_m;
- }
- else i_m = -1;
+ stream_Delete( p_stream );
+ return NULL;
}
- if( i_r >= 0 )
+ uint8_t *p_buf = (uint8_t*)malloc( i_size );
+ if( !p_buf )
{
- if( i_r < p_uit->p_u->i_releases )
- {
- if( i_r != p_uit->i_r )
- i_val |= UPDATE_FILE;
- p_uit->i_r = i_r;
- }
- else i_r = -1;
+ stream_Delete( p_stream );
+ return NULL;
}
- if( i_f >= 0 )
+ int i_read = stream_Read( p_stream, p_buf, (int)i_size );
+ stream_Delete( p_stream );
+
+ if( i_read != (int)i_size )
{
- if( i_r >= 0 && i_r < p_uit->p_u->i_releases
- && i_f < p_uit->p_u->p_releases[p_uit->i_r].i_files )
- {
- if( i_f != p_uit->i_f )
- i_val |= UPDATE_FILE;
- p_uit->i_f = i_f;
- }
- else i_f = -1;
+ msg_Dbg( p_this, "Couldn't read full GPG key" );
+ free( p_buf );
+ return NULL;
}
- update_iterator_GetData( p_uit );
- vlc_mutex_unlock( &p_uit->p_u->lock );
+ public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
+ if( !p_pkey )
+ {
+ free( p_buf );
+ return NULL;
+ }
+
+ memcpy( p_pkey->longid, p_longid, 8 );
+
+ int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
+ free( p_buf );
- if( ( i_m < 0 || p_uit->i_m >= 0 )
- && ( i_r < 0 || p_uit->i_r >= 0 )
- && ( i_f < 0 || p_uit->i_f >= 0 ) )
+ if( i_error != VLC_SUCCESS )
{
- /* Everything worked */
- return UPDATE_SUCCESS|i_val;
+ msg_Dbg( p_this, "Couldn't parse GPG key" );
+ free( p_pkey );
+ return NULL;
}
- else
+
+ return p_pkey;
+}
+
+/*
+ * Generate a SHA1 hash on a public key, to verify a signature made on that hash
+ * Note that we need the signature (v4) to compute the hash
+ */
+static uint8_t *key_sign_hash( public_key_t *p_pkey )
+{
+ if( p_pkey->sig.version != 4 )
+ return NULL;
+
+ if( p_pkey->sig.type < GENERIC_KEY_SIGNATURE ||
+ p_pkey->sig.type > POSITIVE_KEY_SIGNATURE )
+ return NULL;
+
+ gcry_error_t error = 0;
+ gcry_md_hd_t hd;
+
+ error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
+ if( error )
+ return NULL;
+
+ gcry_md_putc( hd, 0x99 );
+
+ size_t i_p_len = mpi_len( p_pkey->key.p );
+ size_t i_g_len = mpi_len( p_pkey->key.g );
+ size_t i_q_len = mpi_len( p_pkey->key.q );
+ size_t i_y_len = mpi_len( p_pkey->key.y );
+
+ size_t i_size = 6 + 2*4 + i_p_len + i_g_len + i_q_len + i_y_len;
+
+ gcry_md_putc( hd, (i_size >> 8) & 0xff );
+ gcry_md_putc( hd, i_size & 0xff );
+
+ gcry_md_putc( hd, p_pkey->key.version );
+ gcry_md_write( hd, p_pkey->key.timestamp, 4 );
+ gcry_md_putc( hd, p_pkey->key.algo );
+
+ gcry_md_write( hd, (uint8_t*)&p_pkey->key.p, 2 );
+ gcry_md_write( hd, (uint8_t*)&p_pkey->key.p + 2, i_p_len );
+
+ gcry_md_write( hd, (uint8_t*)&p_pkey->key.q, 2 );
+ gcry_md_write( hd, (uint8_t*)&p_pkey->key.q + 2, i_q_len );
+
+ gcry_md_write( hd, (uint8_t*)&p_pkey->key.g, 2 );
+ gcry_md_write( hd, (uint8_t*)&p_pkey->key.g + 2, i_g_len );
+
+ gcry_md_write( hd, (uint8_t*)&p_pkey->key.y, 2 );
+ gcry_md_write( hd, (uint8_t*)&p_pkey->key.y + 2, i_y_len );
+
+ gcry_md_putc( hd, 0xb4 );
+
+ size_t i_len = strlen((char*)p_pkey->psz_username);
+
+ gcry_md_putc( hd, (i_len << 24) & 0xff );
+ gcry_md_putc( hd, (i_len << 16) & 0xff );
+ gcry_md_putc( hd, (i_len << 8) & 0xff );
+ gcry_md_putc( hd, (i_len) & 0xff );
+
+ gcry_md_write( hd, p_pkey->psz_username, i_len );
+
+ size_t i_hashed_data_len =
+ scalar_number( p_pkey->sig.specific.v4.hashed_data_len, 2 );
+
+ gcry_md_putc( hd, p_pkey->sig.version );
+ gcry_md_putc( hd, p_pkey->sig.type );
+ gcry_md_putc( hd, p_pkey->sig.public_key_algo );
+ gcry_md_putc( hd, p_pkey->sig.digest_algo );
+ gcry_md_write( hd, p_pkey->sig.specific.v4.hashed_data_len, 2 );
+ gcry_md_write( hd, p_pkey->sig.specific.v4.hashed_data, i_hashed_data_len );
+
+ gcry_md_putc( hd, 0x04 );
+ gcry_md_putc( hd, 0xff );
+
+ i_hashed_data_len += 6; /* hashed data + 6 bytes header */
+
+ gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
+ gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
+ gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
+ gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
+
+ gcry_md_final( hd );
+
+ uint8_t *p_tmp = gcry_md_read( hd, GCRY_MD_SHA1);
+
+ if( !p_tmp ||
+ p_tmp[0] != p_pkey->sig.hash_verification[0] ||
+ p_tmp[1] != p_pkey->sig.hash_verification[1] )
{
- /* Something failed */
- return UPDATE_FAIL;
+ gcry_md_close( hd );
+ return NULL;
}
+
+ uint8_t *p_hash = malloc( 20 );
+ if( p_hash )
+ memcpy( p_hash, p_tmp, 20 );
+ gcry_md_close( hd );
+ return p_hash;
}
+
+/*****************************************************************************
+ * Update_t functions
+ *****************************************************************************/
+
/**
- * Fills the iterator data (file, release and mirror structs)
- * The update struct should be locked before calling this function.
+ * Create a new update VLC struct
*
- * \param p_uit update iterator
- * \return nothing
+ * \param p_this the calling vlc_object
+ * \return pointer to new update_t or NULL
*/
-void update_iterator_GetData( update_iterator_t *p_uit )
+update_t *__update_New( vlc_object_t *p_this )
{
- struct update_release_t *p_r = NULL;
- struct update_file_t *p_f = NULL;
- struct update_mirror_t *p_m = NULL;
+ update_t *p_update;
+ assert( p_this );
- update_iterator_ClearData( p_uit );
+ p_update = (update_t *)malloc( sizeof( update_t ) );
+ if( !p_update ) return NULL;
+
+ vlc_mutex_init( &p_update->lock );
+
+ p_update->p_libvlc = p_this->p_libvlc;
+
+ p_update->release.psz_url = NULL;
+ p_update->release.psz_desc = NULL;
+
+ p_update->p_download = NULL;
+ p_update->p_check = NULL;
+
+ p_update->p_pkey = NULL;
+ vlc_gcrypt_init();
- if( p_uit->i_m >= 0 )
+ return p_update;
+}
+
+/**
+ * Delete an update_t struct
+ *
+ * \param p_update update_t* pointer
+ * \return nothing
+ */
+void update_Delete( update_t *p_update )
+{
+ assert( p_update );
+
+ if( p_update->p_check )
{
- p_m = p_uit->p_u->p_mirrors + p_uit->i_m;
- p_uit->mirror.psz_name = STRDUP( p_m->psz_name );
- p_uit->mirror.psz_location = STRDUP( p_m->psz_location );
- p_uit->mirror.psz_type = STRDUP( p_m->psz_type );
+ assert( !p_update->p_download );
+ vlc_object_kill( p_update->p_check );
+ vlc_thread_join( p_update->p_check );
}
-
- if( p_uit->i_r >= 0 )
+ else if( p_update->p_download )
{
- p_r = p_uit->p_u->p_releases + p_uit->i_r;
- asprintf( &p_uit->release.psz_version, "%s.%s.%s-%s",
- p_r->psz_major,
- p_r->psz_minor,
- p_r->psz_revision,
- p_r->psz_extra );
- p_uit->release.psz_svn_revision = STRDUP( p_r->psz_svn_revision );
- p_uit->release.i_type = p_r->i_type;
- p_uit->release.i_status = p_r->i_status;
- if( p_uit->i_f >= 0 )
- {
- p_f = p_r->p_files + p_uit->i_f;
- p_uit->file.i_type = p_f->i_type;
- p_uit->file.psz_md5 = STRDUP( p_f->psz_md5 );
- p_uit->file.l_size = p_f->l_size;
- p_uit->file.psz_description = STRDUP( p_f->psz_description);
- if( p_f->psz_url[0] == '/' )
- {
- if( p_m )
- {
- asprintf( &p_uit->file.psz_url, "%s%s",
- p_m->psz_base_url, p_f->psz_url );
- }
- }
- else
- {
- p_uit->file.psz_url = STRDUP( p_f->psz_url );
- }
- }
+ vlc_object_kill( p_update->p_download );
+ vlc_thread_join( p_update->p_download );
}
+
+ vlc_mutex_destroy( &p_update->lock );
+
+ free( p_update->release.psz_url );
+ free( p_update->release.psz_desc );
+ free( p_update->p_pkey );
+
+ free( p_update );
}
/**
- * Clears the iterator data (file, release and mirror structs)
+ * Empty the release struct
*
- * \param p_uit update iterator
+ * \param p_update update_t* pointer
* \return nothing
*/
-void update_iterator_ClearData( update_iterator_t *p_uit )
+static void EmptyRelease( update_t *p_update )
{
- p_uit->file.i_type = UPDATE_FILE_TYPE_NONE;
- FREE( p_uit->file.psz_md5 );
- p_uit->file.l_size = 0;
- FREE( p_uit->file.psz_description );
- FREE( p_uit->file.psz_url );
- FREE( p_uit->release.psz_version );
- FREE( p_uit->release.psz_svn_revision );
- p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE;
- p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE;
- FREE( p_uit->mirror.psz_name );
- FREE( p_uit->mirror.psz_location );
- FREE( p_uit->mirror.psz_type );
+ p_update->release.i_major = 0;
+ p_update->release.i_minor = 0;
+ p_update->release.i_revision = 0;
+
+ FREENULL( p_update->release.psz_url );
+ FREENULL( p_update->release.psz_desc );
}
/**
- * Perform an action on the update iterator
- * Only the first matching action is performed.
+ * Get the update file and parse it
+ * p_update has to be locked when calling this function
*
- * \param p_uit update iterator
- * \param i_action update action bitmask. can be a combination of UPDATE_NEXT, UPDATE_PREV, UPDATE_MIRROR, UPDATE_RELEASE, UPDATE_FILE, UPDATE_RESET
- * \return UPDATE_FAIL if action fails, UPDATE_SUCCESS|(combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE if these changed) otherwise
+ * \param p_update pointer to update struct
+ * \return true if the update is valid and authenticated
*/
-unsigned int update_iterator_Action( update_iterator_t *p_uit, int i_action )
+static bool GetUpdateFile( update_t *p_update )
{
- if( i_action & UPDATE_RESET )
+ stream_t *p_stream = NULL;
+ int i_major = 0;
+ int i_minor = 0;
+ int i_revision = 0;
+ unsigned char extra;
+ char *psz_version_line = NULL;
+
+ p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
+ if( !p_stream )
{
- return update_iterator_Reset( p_uit );
+ msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
+ UPDATE_VLC_STATUS_URL );
+ goto error;
}
- else
- if( i_action & UPDATE_MIRROR )
+
+ /* Start reading the status file */
+ if( !( psz_version_line = stream_ReadLine( p_stream ) ) )
{
- if( i_action & UPDATE_PREV )
- {
- return update_iterator_PrevMirror( p_uit );
- }
- else
- {
- return update_iterator_NextMirror( p_uit );
- }
+ msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
+ UPDATE_VLC_STATUS_URL );
+ goto error;
+ }
+
+ /* first line : version number */
+ p_update->release.extra = 0;
+ switch( sscanf( psz_version_line, "%i.%i.%i%c",
+ &i_major, &i_minor, &i_revision, &extra ) )
+ {
+ case 4:
+ p_update->release.extra = extra;
+ case 3:
+ p_update->release.i_major = i_major;
+ p_update->release.i_minor = i_minor;
+ p_update->release.i_revision = i_revision;
+ break;
+ default:
+ msg_Err( p_update->p_libvlc, "Update version false formated" );
+ goto error;
+ }
+
+ /* second line : URL */
+ if( !( p_update->release.psz_url = stream_ReadLine( p_stream ) ) )
+ {
+ msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
+ UPDATE_VLC_STATUS_URL );
+ goto error;
+ }
+
+ /* Remaining data : description */
+ int i_read = stream_Size( p_stream ) - stream_Tell( p_stream );
+ if( i_read <= 0 )
+ {
+ msg_Err( p_update->p_libvlc,
+ "Update file %s is corrupted: description missing",
+ UPDATE_VLC_STATUS_URL );
+ goto error;
+ }
+
+ p_update->release.psz_desc = (char*) malloc( i_read + 1 );
+ if( !p_update->release.psz_desc )
+ goto error;
+
+ if( stream_Read( p_stream, p_update->release.psz_desc, i_read ) != i_read )
+ {
+ msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
+ UPDATE_VLC_STATUS_URL );
+ goto error;
}
- /*else if( i_action & UPDATE_RELEASE )
+ p_update->release.psz_desc[i_read] = '\0';
+
+ stream_Delete( p_stream );
+ p_stream = NULL;
+
+ /* Now that we know the status is valid, we must download its signature
+ * to authenticate it */
+ signature_packet_t sign;
+ if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
+ UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
{
- if( i_action & UPDATE_PREV )
+ msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
+ goto error;
+ }
+
+ if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
+ {
+ msg_Err( p_update->p_libvlc, "Invalid signature type" );
+ goto error;
+ }
+
+ p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
+ if( !p_update->p_pkey )
+ goto error;
+
+ if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
+ p_update->p_pkey, NULL ) != VLC_SUCCESS )
+ {
+ msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
+ FREENULL( p_update->p_pkey );
+ goto error;
+ }
+
+ memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
+
+ if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
+ {
+ msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
+ public_key_t *p_new_pkey = download_key(
+ VLC_OBJECT(p_update->p_libvlc),
+ sign.issuer_longid, videolan_public_key_longid );
+ if( !p_new_pkey )
{
- return update_iterator_PrevRelease( p_uit );
+ msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
+ FREENULL( p_update->p_pkey );
+ goto error;
}
- else
+
+ uint8_t *p_hash = key_sign_hash( p_new_pkey );
+ if( !p_hash )
{
- return update_iterator_NextRelease( p_uit );
+ msg_Err( p_update->p_libvlc, "Failed to hash signature" );
+ free( p_new_pkey );
+ FREENULL( p_update->p_pkey );
+ goto error;
}
- }*/
- else if( i_action & UPDATE_FILE )
- {
- if( i_action & UPDATE_PREV )
+
+ if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
+ &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
{
- return update_iterator_PrevFile( p_uit );
+ free( p_hash );
+ msg_Info( p_update->p_libvlc, "Key authenticated" );
+ free( p_update->p_pkey );
+ p_update->p_pkey = p_new_pkey;
}
else
{
- return update_iterator_NextFile( p_uit );
+ free( p_hash );
+ msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
+ goto error;
}
}
+
+ gcry_md_hd_t hd;
+ if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
+ goto error_hd;
+
+ gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
+ FREENULL( psz_version_line );
+ if( sign.type == TEXT_SIGNATURE )
+ gcry_md_putc( hd, '\r' );
+ gcry_md_putc( hd, '\n' );
+ gcry_md_write( hd, p_update->release.psz_url,
+ strlen( p_update->release.psz_url ) );
+ if( sign.type == TEXT_SIGNATURE )
+ gcry_md_putc( hd, '\r' );
+ gcry_md_putc( hd, '\n' );
+
+ char *psz_desc = p_update->release.psz_desc;
+ while( *psz_desc )
+ {
+ size_t i_len = strcspn( psz_desc, "\r\n" );
+ if( !i_len )
+ break;
+
+ gcry_md_write( hd, psz_desc, i_len );
+ if( sign.type == TEXT_SIGNATURE )
+ gcry_md_putc( hd, '\r' );
+ gcry_md_putc( hd, '\n' );
+
+ psz_desc += i_len;
+ while( *psz_desc == '\r' || *psz_desc == '\n' )
+ psz_desc++;
+ }
+
+ if( sign.version == 3 )
+ {
+ gcry_md_putc( hd, sign.type );
+ gcry_md_write( hd, &sign.specific.v3.timestamp, 4 );
+ }
+ else if( sign.version == 4 )
+ {
+ gcry_md_putc( hd, sign.version );
+ gcry_md_putc( hd, sign.type );
+ gcry_md_putc( hd, sign.public_key_algo );
+ gcry_md_putc( hd, sign.digest_algo );
+ gcry_md_write( hd, sign.specific.v4.hashed_data_len, 2 );
+ size_t i_len = scalar_number( sign.specific.v4.hashed_data_len, 2 );
+ gcry_md_write( hd, sign.specific.v4.hashed_data, i_len );
+ gcry_md_putc( hd, 0x04 );
+ gcry_md_putc( hd, 0xFF );
+
+ i_len += 6; /* hashed data + 6 bytes header */
+
+ gcry_md_putc( hd, (i_len << 24) & 0xff);
+ gcry_md_putc( hd, (i_len << 16) &0xff );
+ gcry_md_putc( hd, (i_len << 8) & 0xff );
+ gcry_md_putc( hd, (i_len) & 0xff );
+ }
else
+ { /* RFC 4880 only tells about versions 3 and 4 */
+ msg_Warn( p_update->p_libvlc, "Invalid signature version %d",
+ sign.version);
+ goto error_hd;
+ }
+
+ gcry_md_final( hd );
+
+ uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );
+
+ if( p_hash[0] != sign.hash_verification[0] ||
+ p_hash[1] != sign.hash_verification[1] )
+ {
+ msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
+ goto error_hd;
+ }
+
+ if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
+ != VLC_SUCCESS )
{
- return UPDATE_SUCCESS;
+ msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
+ goto error_hd;
}
+ else
+ {
+ msg_Info( p_update->p_libvlc, "Status file authenticated" );
+ gcry_md_close( hd );
+ return true;
+ }
+
+error_hd:
+ gcry_md_close( hd );
+error:
+ if( p_stream )
+ stream_Delete( p_stream );
+ free( psz_version_line );
+ return false;
}
+static void update_CheckReal( update_check_thread_t *p_uct );
+
/**
- * Object to launch download thread in a different object
+ * Check for updates
+ *
+ * \param p_update pointer to update struct
+ * \param pf_callback pointer to a function to call when the update_check is finished
+ * \param p_data pointer to some datas to give to the callback
+ * \returns nothing
*/
-typedef struct {
- VLC_COMMON_MEMBERS
- char *psz_dest; //< Download destination
- char *psz_src; //< Download source
- char *psz_status; //< Download status displayed in progress dialog
-} download_thread_t;
+void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
+{
+ assert( p_update );
+
+ update_check_thread_t *p_uct =
+ vlc_custom_create( p_update->p_libvlc, sizeof( *p_uct ),
+ VLC_OBJECT_GENERIC, "update check" );
+ if( !p_uct ) return;
+
+ p_uct->p_update = p_update;
+ p_update->p_check = p_uct;
+ p_uct->pf_callback = pf_callback;
+ p_uct->p_data = p_data;
+
+ vlc_thread_create( p_uct, "check for update", update_CheckReal,
+ VLC_THREAD_PRIORITY_LOW, false );
+}
+
+void update_CheckReal( update_check_thread_t *p_uct )
+{
+ bool b_ret;
+ vlc_mutex_lock( &p_uct->p_update->lock );
+
+ EmptyRelease( p_uct->p_update );
+ b_ret = GetUpdateFile( p_uct->p_update );
+ vlc_mutex_unlock( &p_uct->p_update->lock );
+
+ if( p_uct->pf_callback )
+ (p_uct->pf_callback)( p_uct->p_data, b_ret );
-void update_download_for_real( download_thread_t *p_this );
+ p_uct->p_update->p_check = NULL;
+
+ vlc_object_release( p_uct );
+}
/**
- * Download the file selected by the update iterator. This function will
- * launch the download in a new thread (downloads can be long)
+ * Compare a given release's version number to the current VLC's one
*
- * \param p_uit update iterator
- * \param psz_dest destination file path
- * \return nothing
+ * \param p_update structure
+ * \return true if we have to upgrade to the given version to be up to date
*/
-void update_download( update_iterator_t *p_uit, char *psz_dest )
+bool update_NeedUpgrade( update_t *p_update )
{
- download_thread_t *p_dt =
- vlc_object_create( p_uit->p_u->p_vlc, sizeof( download_thread_t ) );
-
- p_dt->psz_dest = strdup( psz_dest );
- p_dt->psz_src = strdup( p_uit->file.psz_url );
- asprintf( &p_dt->psz_status, "%s - %s (%s)\nSource: %s\nDestination: %s",
- p_uit->file.psz_description, p_uit->release.psz_version,
- p_uit->release.psz_svn_revision, p_uit->file.psz_url,
- psz_dest);
-
- vlc_thread_create( p_dt, "download thread", update_download_for_real,
- VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
+ assert( p_update );
+
+ return p_update->release.i_major < *PACKAGE_VERSION_MAJOR - '0' ||
+ p_update->release.i_minor < *PACKAGE_VERSION_MINOR - '0' ||
+ p_update->release.i_revision < *PACKAGE_VERSION_REVISION - '0' ||
+ p_update->release.extra < *PACKAGE_VERSION_EXTRA;
}
/**
- * The true download function.
+ * Convert a long int size in bytes to a string
*
- * \param p_this the download_thread_t object
+ * \param l_size the size in bytes
+ * \return the size as a string
+ */
+static char *size_str( long int l_size )
+{
+ char *psz_tmp = NULL;
+ int i_retval = 0;
+ if( l_size >> 30 )
+ i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
+ else if( l_size >> 20 )
+ i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
+ else if( l_size >> 10 )
+ i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
+ else
+ i_retval = asprintf( &psz_tmp, "%ld B", l_size );
+
+ return i_retval == -1 ? NULL : psz_tmp;
+}
+
+static void update_DownloadReal( update_download_thread_t *p_udt );
+
+/**
+ * Download the file given in the update_t
+ *
+ * \param p_update structure
+ * \param dir to store the download file
* \return nothing
*/
-void update_download_for_real( download_thread_t *p_this )
+void update_Download( update_t *p_update, const char *psz_destdir )
{
- char *psz_dest = p_this->psz_dest;
- char *psz_src = p_this->psz_src;
- stream_t *p_stream;
- vlc_t *p_vlc = p_this->p_vlc;
+ assert( p_update );
- FILE *p_file = NULL;
- void *p_buffer;
+ update_download_thread_t *p_udt =
+ vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),
+ VLC_OBJECT_GENERIC, "update download" );
+ if( !p_udt )
+ return;
- char *psz_status;
+ p_udt->p_update = p_update;
+ p_update->p_download = p_udt;
+ p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
- int i_progress;
- int i_size;
- int i_done = 0;
+ vlc_thread_create( p_udt, "download update", update_DownloadReal,
+ VLC_THREAD_PRIORITY_LOW, false );
+}
- vlc_thread_ready( p_this );
+static void update_DownloadReal( update_download_thread_t *p_udt )
+{
+ int i_progress = 0;
+ long int l_size;
+ long int l_downloaded = 0;
+ float f_progress;
+ char *psz_status = NULL;
+ char *psz_downloaded = NULL;
+ char *psz_size = NULL;
+ char *psz_destfile = NULL;
+ char *psz_tmpdestfile = NULL;
- p_stream = stream_UrlNew( p_vlc, psz_src );
+ FILE *p_file = NULL;
+ stream_t *p_stream = NULL;
+ void* p_buffer = NULL;
+ int i_read;
+
+ update_t *p_update = p_udt->p_update;
+ char *psz_destdir = p_udt->psz_destdir;
+
+ /* Open the stream */
+ p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
if( !p_stream )
{
- msg_Err( p_vlc, "Failed to open %s for reading", psz_src );
+ msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
+ goto end;
}
- else
+
+ /* Get the stream size */
+ l_size = stream_Size( p_stream );
+
+ /* Get the file name and open it*/
+ psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
+ if( !psz_tmpdestfile )
{
+ msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
+ goto end;
+ }
+ psz_tmpdestfile++;
+ if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
+ goto end;
- p_file = fopen( psz_dest, "w" );
- if( !p_file )
+ p_file = utf8_fopen( psz_destfile, "w" );
+ if( !p_file )
+ {
+ msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
+ goto end;
+ }
+
+ /* Create a buffer and fill it with the downloaded file */
+ p_buffer = (void *)malloc( 1 << 10 );
+ if( !p_buffer )
+ goto end;
+
+ psz_size = size_str( l_size );
+ if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",
+ p_update->release.psz_url, psz_size, 0.0 ) != -1 )
+ {
+ i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
+ free( psz_status );
+ }
+
+ vlc_object_lock( p_udt );
+ while( vlc_object_alive( p_udt ) &&
+ ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
+ !intf_ProgressIsCancelled( p_udt, i_progress ) )
+ {
+ vlc_object_unlock( p_udt );
+ if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
{
- msg_Err( p_vlc, "Failed to open %s for writing", psz_dest );
+ msg_Err( p_udt, "Failed to write into %s", psz_destfile );
+ break;
}
- else
+
+ l_downloaded += i_read;
+ psz_downloaded = size_str( l_downloaded );
+ f_progress = 100.0*(float)l_downloaded/(float)l_size;
+
+ if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done",
+ p_update->release.psz_url, psz_downloaded, psz_size,
+ f_progress ) != -1 )
{
+ intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
+ free( psz_status );
+ }
+ free( psz_downloaded );
+ vlc_object_lock( p_udt );
+ }
+
+ /* Finish the progress bar or delete the file if the user had canceled */
+ fclose( p_file );
+ p_file = NULL;
- i_size = (int)(stream_Size(p_stream)/(1<<10));
- p_buffer = (void *)malloc( 1<<10 );
+ if( vlc_object_alive( p_udt ) &&
+ !intf_ProgressIsCancelled( p_udt, i_progress ) )
+ {
+ vlc_object_unlock( p_udt );
+ if( asprintf( &psz_status, "%s\nDone %s (100.0%%)",
+ p_update->release.psz_url, psz_size ) != -1 )
+ {
+ intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
+ free( psz_status );
+ }
+ }
+ else
+ {
+ vlc_object_unlock( p_udt );
+ utf8_unlink( psz_destfile );
+ goto end;
+ }
- asprintf( &psz_status, "%s\nDownloading... %.1f%% done",
- p_this->psz_status, 0.0 );
- i_progress = intf_UserProgress( p_vlc, "Downloading...",
- psz_status, 0.0 );
+ signature_packet_t sign;
+ if( download_signature( VLC_OBJECT( p_udt ), &sign,
+ p_update->release.psz_url ) != VLC_SUCCESS )
+ {
+ utf8_unlink( psz_destfile );
+
+ intf_UserFatal( p_udt, true, _("File could not be verified"),
+ _("It was not possible to download a cryptographic signature for "
+ "the downloaded file \"%s\". Thus, it was deleted."),
+ psz_destfile );
+ msg_Err( p_udt, "Couldn't download signature of downloaded file" );
+ goto end;
+ }
- while( stream_Read( p_stream, p_buffer, 1<<10 ) )
- {
- float f_progress;
+ if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
+ {
+ utf8_unlink( psz_destfile );
+ msg_Err( p_udt, "Invalid signature issuer" );
+ intf_UserFatal( p_udt, true, _("Invalid signature"),
+ _("The cryptographic signature for the downloaded file \"%s\" was "
+ "invalid and could not be used to securely verify it. Thus, the "
+ "file was deleted."),
+ psz_destfile );
+ goto end;
+ }
- fwrite( p_buffer, 1<<10, 1, p_file );
+ if( sign.type != BINARY_SIGNATURE )
+ {
+ utf8_unlink( psz_destfile );
+ msg_Err( p_udt, "Invalid signature type" );
+ intf_UserFatal( p_udt, true, _("Invalid signature"),
+ _("The cryptographic signature for the downloaded file \"%s\" was "
+ "invalid and could not be used to securely verify it. Thus, the "
+ "file was deleted."),
+ psz_destfile );
+ goto end;
+ }
- i_done++;
- free( psz_status );
- f_progress = 100.0*(float)i_done/(float)i_size;
- asprintf( &psz_status, "%s\nDownloading... %.1f%% done",
- p_this->psz_status, f_progress );
- intf_UserProgressUpdate( p_vlc, i_progress,
- psz_status, f_progress );
- }
+ uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
+ if( !p_hash )
+ {
+ msg_Err( p_udt, "Unable to hash %s", psz_destfile );
+ utf8_unlink( psz_destfile );
+ intf_UserFatal( p_udt, true, _("File not verifiable"),
+ _("It was not possible to securely verify the downloaded file"
+ " \"%s\". Thus, it was VLC deleted."),
+ psz_destfile );
+
+ goto end;
+ }
- free( p_buffer );
- free( psz_status );
- fclose( p_file );
- stream_Delete( p_stream );
+ if( p_hash[0] != sign.hash_verification[0] ||
+ p_hash[1] != sign.hash_verification[1] )
+ {
+ utf8_unlink( psz_destfile );
+ intf_UserFatal( p_udt, true, _("File corrupted"),
+ _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
+ psz_destfile );
+ msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
+ free( p_hash );
+ goto end;
+ }
- }
+ if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
+ != VLC_SUCCESS )
+ {
+ utf8_unlink( psz_destfile );
+ intf_UserFatal( p_udt, true, _("File corrupted"),
+ _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
+ psz_destfile );
+ msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
+ free( p_hash );
+ goto end;
}
- free( p_this->psz_dest );
- free( p_this->psz_src );
- free( p_this->psz_status );
+ msg_Info( p_udt, "%s authenticated", psz_destfile );
+ free( p_hash );
-#ifdef WIN32
- CloseHandle( p_this->thread_id );
-#endif
+end:
+ if( p_stream )
+ stream_Delete( p_stream );
+ if( p_file )
+ fclose( p_file );
+ free( psz_destdir );
+ free( psz_destfile );
+ free( p_buffer );
+ free( psz_size );
+
+ p_udt->p_update->p_download = NULL;
- vlc_object_destroy( p_this );
+ vlc_object_release( p_udt );
}
+
+update_release_t *update_GetRelease( update_t *p_update )
+{
+ return &p_update->release;
+}
+
+#else
+update_t *__update_New( vlc_object_t *p_this )
+{
+ (void)p_this;
+ return NULL;
+}
+
+void update_Delete( update_t *p_update )
+{
+ (void)p_update;
+}
+
+void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
+ void *p_data )
+{
+ (void)p_update; (void)pf_callback; (void)p_data;
+}
+
+bool update_NeedUpgrade( update_t *p_update )
+{
+ (void)p_update;
+ return false;
+}
+
+void update_Download( update_t *p_update, const char *psz_destdir )
+{
+ (void)p_update; (void)psz_destdir;
+}
+
+update_release_t *update_GetRelease( update_t *p_update )
+{
+ (void)p_update;
+ return NULL;
+}
+#endif