]> git.sesse.net Git - vlc/blobdiff - src/misc/update.c
Attach the sout object to libvlc
[vlc] / src / misc / update.c
index 6ed2d54c1c96f7bdc6092700f303547e2433c8c4..b84de8368cc3c5d9cca54f7be0abc2b4cb00a361 100644 (file)
@@ -22,7 +22,9 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
-
+/*
+ * XXX: should use v4 signatures for binary files (already used for public key)
+ */
 /**
  *   \file
  *   This file contains functions related to VLC update management
  * Preamble
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 
 #ifdef UPDATE_CHECK
@@ -81,9 +87,7 @@
  * Local Prototypes
  *****************************************************************************/
 static void EmptyRelease( update_t *p_update );
-static vlc_bool_t GetUpdateFile( update_t *p_update );
-static int CompareReleases( const struct update_release_t *p1,
-                            const struct update_release_t *p2 );
+static bool GetUpdateFile( update_t *p_update );
 static char * size_str( long int l_size );
 
 
@@ -109,7 +113,7 @@ static inline int scalar_number( uint8_t *p, int header_len )
 /* 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
  */
@@ -159,7 +163,7 @@ static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
         if( i_g_len < 128 )
             memmove( p_key->y, p_key->g + 2+i_g_len, 2+128 );
     }
-    
+
     int i_y_len = mpi_len( p_buf );
     if( i_y_len > 128 )
         return VLC_EGENERIC;
@@ -201,7 +205,7 @@ static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
     size_t i_pos = 6;
     size_t i_hashed_data_len = scalar_number( p_sig->hashed_data_len, 2 );
     i_pos += i_hashed_data_len;
-    if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total, 
+    if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total,
                               * + the unhashed data length (2 bytes)
                               * + the hash verification (2 bytes) */
         return VLC_EGENERIC;
@@ -318,7 +322,7 @@ static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
             p_ipos += i_line_len + 1;
             continue;
         }
-        
+
         if( !strncmp( p_ipos, "Version:", 8 ) )
         {
             p_ipos += i_line_len + 1;
@@ -354,7 +358,7 @@ static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
 
 /*
  * 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 
+ * We're given the file's url, we just append ".asc" to it and download
  */
 static int download_signature(  vlc_object_t *p_this,
                                 signature_packet_v3_t *p_sig,
@@ -395,7 +399,7 @@ static int download_signature(  vlc_object_t *p_this,
         stream_Delete( p_stream );
         return VLC_ENOMEM;
     }
-    
+
     int i_read = stream_Read( p_stream, p_buf, (int)i_size );
 
     stream_Delete( p_stream );
@@ -406,11 +410,16 @@ static int download_signature(  vlc_object_t *p_this,
         free( p_buf );
         return VLC_EGENERIC;
     }
-    
+
     int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
     free( p_buf );
 
-    if( i_bytes > 65 )
+    if( i_bytes == 0 )
+    {
+        msg_Dbg( p_this, "Unarmoring failed" );
+        return VLC_EGENERIC;
+    }
+    else if( i_bytes > 65 )
     {
         msg_Dbg( p_this, "Signature is too big: %d bytes", i_bytes );
         return VLC_EGENERIC;
@@ -420,7 +429,8 @@ static int download_signature(  vlc_object_t *p_this,
         int i_r_len = mpi_len( p_sig->r );
         if( i_r_len > 20 )
         {
-            msg_Dbg( p_this, "Signature invalid" );
+            msg_Dbg( p_this, "Invalid signature, r number too big: %d bytes",
+                                i_r_len );
             return VLC_EGENERIC;
         }
         else if( i_r_len < 20 )
@@ -434,7 +444,7 @@ static int download_signature(  vlc_object_t *p_this,
 /*
  * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
  */
-static int verify_signature( vlc_object_t *p_this, uint8_t *p_r, uint8_t *p_s,
+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) */
@@ -610,7 +620,7 @@ static int parse_public_key( const uint8_t *p_key_data, size_t i_key_len, public
                 memcpy( p_key->psz_username, pos, i_packet_len );
                 p_key->psz_username[i_packet_len] = '\0';
                 break;
-            
+
             default:
                 break;
         }
@@ -651,7 +661,7 @@ static uint8_t *hash_sha1_from_file( const char *psz_file,
     {
         fclose( f );
         return NULL;
-    } 
+    }
 
     size_t i_read;
     while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
@@ -663,8 +673,12 @@ static uint8_t *hash_sha1_from_file( const char *psz_file,
     fclose( f );
     gcry_md_final( hd );
 
-    /* FIXME: is it always padded to 20 bytes ? */
-    return( (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1) );
+    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;
 }
 
 /*
@@ -795,15 +809,20 @@ static uint8_t *key_sign_hash( public_key_t *p_pkey )
 
     gcry_md_final( hd );
 
-    uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
+    uint8_t *p_tmp = gcry_md_read( hd, GCRY_MD_SHA1);
 
-    if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
-        p_hash[1] != p_pkey->sig.hash_verification[1] )
+    if( !p_tmp ||
+        p_tmp[0] != p_pkey->sig.hash_verification[0] ||
+        p_tmp[1] != p_pkey->sig.hash_verification[1] )
     {
-        free( p_hash );
+        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;
 }
 
@@ -832,7 +851,7 @@ update_t *__update_New( vlc_object_t *p_this )
 
     p_update->release.psz_url = NULL;
     p_update->release.psz_desc = NULL;
-    
+
     p_update->p_pkey = NULL;
 
     return p_update;
@@ -875,12 +894,12 @@ static void EmptyRelease( update_t *p_update )
 
 /**
  * Get the update file and parse it
- * *p_update has to be locked when calling this function
+ * p_update has to be locked when calling this function
  *
  * \param p_update pointer to update struct
- * \return VLC_TRUE if the update is valid and authenticated
+ * \return true if the update is valid and authenticated
  */
-static vlc_bool_t GetUpdateFile( update_t *p_update )
+static bool GetUpdateFile( update_t *p_update )
 {
     stream_t *p_stream = NULL;
     int i_major = 0;
@@ -945,10 +964,10 @@ static vlc_bool_t GetUpdateFile( update_t *p_update )
     stream_Delete( p_stream );
     p_stream = NULL;
 
-    /* Now that we know the status is valid, we must download its signature 
+    /* Now that we know the status is valid, we must download its signature
      * to authenticate it */
     signature_packet_v3_t sign;
-    if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign, 
+    if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
             UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
     {
         msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
@@ -995,8 +1014,7 @@ static vlc_bool_t GetUpdateFile( update_t *p_update )
             goto error;
         }
 
-        if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
-                    p_new_pkey->sig.r, p_new_pkey->sig.s,
+        if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
                     &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
         {
             free( p_hash );
@@ -1014,7 +1032,7 @@ static vlc_bool_t GetUpdateFile( update_t *p_update )
 
     gcry_md_hd_t hd;
     if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
-        goto error;
+        goto error_hd;
 
     gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
     FREENULL( psz_version_line );
@@ -1043,29 +1061,29 @@ static vlc_bool_t GetUpdateFile( update_t *p_update )
         p_hash[1] != sign.hash_verification[1] )
     {
         msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
-        free( p_hash );
-        goto error;
+        goto error_hd;
     }
 
-    if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
-            sign.r, sign.s, &p_update->p_pkey->key, p_hash ) != VLC_SUCCESS )
+    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
+            != VLC_SUCCESS )
     {
         msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
-        free( p_hash );
-        goto error;
+        goto error_hd;
     }
     else
     {
         msg_Info( p_update->p_libvlc, "Status file authenticated" );
-        free( p_hash );
-        return VLC_TRUE;
+        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 VLC_FALSE;
+    return false;
 }
 
 
@@ -1076,7 +1094,7 @@ typedef struct
 {
     VLC_COMMON_MEMBERS
     update_t *p_update;
-    void (*pf_callback)( void *, vlc_bool_t );
+    void (*pf_callback)( void *, bool );
     void *p_data;
 } update_check_thread_t;
 
@@ -1090,23 +1108,29 @@ void update_CheckReal( update_check_thread_t *p_uct );
  * \param p_data pointer to some datas to give to the callback
  * \returns nothing
  */
-void update_Check( update_t *p_update, void (*pf_callback)( void*, vlc_bool_t ), void *p_data )
+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_object_create( p_update->p_libvlc,
                                             sizeof( update_check_thread_t ) );
+    if( !p_uct )
+    {
+        msg_Err( p_update->p_libvlc, "out of memory" );
+        return;
+    }
+
     p_uct->p_update = p_update;
     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, VLC_FALSE );
+                       VLC_THREAD_PRIORITY_LOW, false );
 }
 
 void update_CheckReal( update_check_thread_t *p_uct )
 {
-    vlc_bool_t b_ret;
+    bool b_ret;
     vlc_mutex_lock( &p_uct->p_update->lock );
 
     EmptyRelease( p_uct->p_update );
@@ -1116,51 +1140,23 @@ void update_CheckReal( update_check_thread_t *p_uct )
     if( p_uct->pf_callback )
         (p_uct->pf_callback)( p_uct->p_data, b_ret );
 
-    vlc_object_destroy( p_uct );
-}
-
-/**
- * Compare two release numbers
- *
- * \param p1 first release
- * \param p2 second release
- * \return UpdateReleaseStatus(Older|Equal|Newer)
- */
-static int CompareReleases( const struct update_release_t *p1,
-                            const struct update_release_t *p2 )
-{
-    int32_t d;
-    d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
-      - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
-      + ( p1->extra ) - ( p2->extra );
-
-    if( d < 0 )
-        return UpdateReleaseStatusOlder;
-    else if( d == 0 )
-        return UpdateReleaseStatusEqual;
-    else
-        return UpdateReleaseStatusNewer;
+    vlc_object_release( p_uct );
 }
 
 /**
  * Compare a given release's version number to the current VLC's one
  *
  * \param p_update structure
- * \return UpdateReleaseStatus(Older|Equal|Newer)
+ * \return true if we have to upgrade to the given version to be up to date
  */
-int update_CompareReleaseToCurrent( update_t *p_update )
+bool update_NeedUpgrade( update_t *p_update )
 {
     assert( p_update );
 
-    struct update_release_t c;
-
-    /* get the current version number */
-    c.i_major = *PACKAGE_VERSION_MAJOR - '0';
-    c.i_minor = *PACKAGE_VERSION_MINOR - '0';
-    c.i_revision = *PACKAGE_VERSION_REVISION - '0';
-    c.extra = *PACKAGE_VERSION_EXTRA;
-
-    return CompareReleases( &p_update->release, &c );
+    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 ||
+            p_update->release.extra < *PACKAGE_VERSION_EXTRA;
 }
 
 /**
@@ -1172,19 +1168,21 @@ int update_CompareReleaseToCurrent( update_t *p_update )
 static char *size_str( long int l_size )
 {
     char *psz_tmp = NULL;
+    int i_retval = 0;
     if( l_size >> 30 )
-        asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
+        i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
     else if( l_size >> 20 )
-        asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
+        i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
     else if( l_size >> 10 )
-        asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
+        i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
     else
-        asprintf( &psz_tmp, "%ld B", l_size );
-    return psz_tmp;
+        i_retval = asprintf( &psz_tmp, "%ld B", l_size );
+
+    return i_retval == -1 ? NULL : psz_tmp;
 }
 
 
-/*
+/**
  * Struct to launch the download in a thread
  */
 typedef struct
@@ -1208,13 +1206,18 @@ void update_Download( update_t *p_update, char *psz_destdir )
     assert( p_update );
 
     update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
-                                                      sizeof( update_download_thread_t ) );
+                                        sizeof( update_download_thread_t ) );
+    if( !p_udt )
+    {
+        msg_Err( p_update->p_libvlc, "out of memory" );
+        return;
+    }
 
     p_udt->p_update = p_update;
     p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
 
     vlc_thread_create( p_udt, "download update", update_DownloadReal,
-                       VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
+                       VLC_THREAD_PRIORITY_LOW, false );
 }
 
 void update_DownloadReal( update_download_thread_t *p_udt )
@@ -1272,7 +1275,8 @@ void update_DownloadReal( update_download_thread_t *p_udt )
         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 )
+    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 );
@@ -1281,14 +1285,19 @@ void update_DownloadReal( update_download_thread_t *p_udt )
     while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
                                    !intf_ProgressIsCancelled( p_udt, i_progress ) )
     {
-        fwrite( p_buffer, i_read, 1, p_file );
+        if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
+        {
+            msg_Err( p_udt, "Failed to write into %s", psz_destfile );
+            break;
+        }
 
         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 )
+        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 );
@@ -1302,7 +1311,8 @@ void update_DownloadReal( update_download_thread_t *p_udt )
 
     if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
     {
-        if( asprintf( &psz_status, "%s\nDone %s (100.0%%)", p_update->release.psz_url, psz_size ) != -1 )
+        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 );
@@ -1320,19 +1330,31 @@ void update_DownloadReal( update_download_thread_t *p_udt )
     {
         utf8_unlink( psz_destfile );
 
-        intf_UserFatal( p_udt, VLC_TRUE, _("File can not be verified"),
-            _("It was not possible to downlaod a cryptographic signature for "
+        intf_UserFatal( p_udt, true, _("File can not be verified"),
+            _("It was not possible to download a cryptographic signature for "
               "downloaded file \"%s\", and so VLC deleted it."),
             psz_destfile );
         msg_Err( p_udt, "Couldn't download signature of downloaded file" );
         goto end;
     }
 
+    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 downloaded file \"%s\" was "
+              "invalid and couldn't be used to securely verify it, and so "
+              "VLC deleted it."),
+            psz_destfile );
+        goto end;
+    }
+
     if( sign.type != BINARY_SIGNATURE )
     {
         utf8_unlink( psz_destfile );
         msg_Err( p_udt, "Invalid signature type" );
-        intf_UserFatal( p_udt, VLC_TRUE, _("Invalid signature"),
+        intf_UserFatal( p_udt, true, _("Invalid signature"),
             _("The cryptographic signature for downloaded file \"%s\" was "
               "invalid and couldn't be used to securely verify it, and so "
               "VLC deleted it."),
@@ -1345,7 +1367,7 @@ void update_DownloadReal( update_download_thread_t *p_udt )
     {
         msg_Err( p_udt, "Unable to hash %s", psz_destfile );
         utf8_unlink( psz_destfile );
-        intf_UserFatal( p_udt, VLC_TRUE, _("File not verifiable"),
+        intf_UserFatal( p_udt, true, _("File not verifiable"),
             _("It was not possible to securely verify downloaded file \"%s\", "
               "and so VLC deleted it."),
             psz_destfile );
@@ -1357,7 +1379,7 @@ void update_DownloadReal( update_download_thread_t *p_udt )
         p_hash[1] != sign.hash_verification[1] )
     {
         utf8_unlink( psz_destfile );
-        intf_UserFatal( p_udt, VLC_TRUE, _("File corrupted"),
+        intf_UserFatal( p_udt, true, _("File corrupted"),
             _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
              psz_destfile );
         msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
@@ -1365,11 +1387,11 @@ void update_DownloadReal( update_download_thread_t *p_udt )
         goto end;
     }
 
-    if( verify_signature( VLC_OBJECT(p_udt), sign.r, sign.s,
-                &p_update->p_pkey->key, p_hash ) != VLC_SUCCESS )
+    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
+            != VLC_SUCCESS )
     {
         utf8_unlink( psz_destfile );
-        intf_UserFatal( p_udt, VLC_TRUE, _("File corrupted"),
+        intf_UserFatal( p_udt, true, _("File corrupted"),
             _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
              psz_destfile );
         msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
@@ -1390,7 +1412,7 @@ end:
     free( p_buffer );
     free( psz_size );
 
-    vlc_object_destroy( p_udt );
+    vlc_object_release( p_udt );
 }
 
 #endif