]> git.sesse.net Git - vlc/blobdiff - src/misc/update.c
Get rid of HAVE_SOCKLEN_T - refs #297
[vlc] / src / misc / update.c
index 14a930b3115e77c6aa66cdb01d1409505367a859..af16fdb1281ad5d4cb4127a7c090c9fe2ae6fe6c 100644 (file)
@@ -1,10 +1,12 @@
 /*****************************************************************************
- * update.c: VLC update and plugins download
+ * update.c: VLC update checking and downloading
  *****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
+ * 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
 
 /**
  *   \file
- *   This file contains functions related to VLC and plugins update management
- */
-
-/*
- * TODO:  * pgp verification of the update file
-          * binary download, and pgp verification
+ *   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
 
-#include <ctype.h>                                              /* tolower() */
 #include <assert.h>
 
-
 #include <vlc_update.h>
-
-#include <vlc_block.h>
+#include <vlc_pgpkey.h>
 #include <vlc_stream.h>
 #include <vlc_interface.h>
-#include <vlc_charset.h>
+
 
 /*****************************************************************************
  * Misc defines
  *****************************************************************************/
 
+/*
+ * 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
+ * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
+ */
+
 #if defined( UNDER_CE )
 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
 #elif defined( WIN32 )
 #   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_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
 #   else
 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
 #endif
 
-#define STRDUP( a ) ( a ? strdup( a ) : NULL )
-
 
 /*****************************************************************************
  * Local Prototypes
  *****************************************************************************/
 static void EmptyRelease( update_t *p_update );
-static void GetUpdateFile( update_t *p_update );
-static int extracmp( char *psz_1, char *psz_2 );
+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 char * size_str( long int l_size );
+
 
 /*****************************************************************************
  * OpenPGP functions
@@ -112,7 +120,7 @@ static inline int scalar_number( uint8_t *p, int header_len )
 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
                                     size_t i_packet_len )
 {
-    if( i_packet_len != 418 )
+    if( i_packet_len > 418 )
         return VLC_EGENERIC;
 
     p_key->version   = *p_buf++;
@@ -126,21 +134,41 @@ static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
     if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
         return VLC_EGENERIC;
 
-    memcpy( p_key->p, p_buf, 2+128 ); p_buf += 2+128;
-    if( mpi_len( p_key->p ) != 128 )
+    int i_p_len = mpi_len( p_buf );
+    if( i_p_len > 128 )
         return VLC_EGENERIC;
+    else
+    {
+        memcpy( p_key->p, p_buf, 2+i_p_len ); p_buf += 2+i_p_len;
+        if( i_p_len < 128 )
+            memmove( p_key->q, p_key->p + 2+i_p_len, 2+20 + 2+128 + 2+128 );
+    }
 
-    memcpy( p_key->q, p_buf, 2+20 );  p_buf += 2+20;
-    if( mpi_len( p_key->q ) != 20 )
+    int i_q_len = mpi_len( p_buf );
+    if( i_q_len > 20 )
         return VLC_EGENERIC;
+    else
+    {
+        memcpy( p_key->q, p_buf, 2+i_q_len );  p_buf += 2+i_q_len;
+        if( i_p_len < 20 )
+            memmove( p_key->g, p_key->q + 2+i_q_len, 2+128 + 2+128 );
+    }
 
-    memcpy( p_key->g, p_buf, 2+128 ); p_buf += 2+128;
-    if( mpi_len( p_key->g ) != 128 )
+    int i_g_len = mpi_len( p_buf );
+    if( i_g_len > 128 )
         return VLC_EGENERIC;
-
-    memcpy( p_key->y, p_buf, 2+128 ); p_buf += 2+128;
-    if( mpi_len( p_key->y ) != 128 )
+    else
+    {
+        memcpy( p_key->g, p_buf, 2+i_g_len ); p_buf += 2+i_g_len;
+        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;
+    else
+        memcpy( p_key->y, p_buf, 2+i_y_len );
 
     return VLC_SUCCESS;
 }
@@ -209,21 +237,31 @@ static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
 
     memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
 
-    memcpy( p_sig->r, p_buf, 22 ); p_buf += 22;
-    if( mpi_len( p_sig->r ) != 20 )
+    int i_r_len = mpi_len( p_buf );
+    if( i_r_len > 20 )
     {
         free( p_sig->hashed_data );
         free( p_sig->unhashed_data );
         return VLC_EGENERIC;
     }
+    else
+    {
+        memcpy( p_sig->r, p_buf, 2 + i_r_len );
+        p_buf += 2 + i_r_len;
+    }
 
-    memcpy( p_sig->s, p_buf, 22 );
-    if( mpi_len( p_sig->s ) != 20 )
+    int i_s_len = mpi_len( p_buf );
+    if( i_s_len > 20 )
     {
         free( p_sig->hashed_data );
         free( p_sig->unhashed_data );
         return VLC_EGENERIC;
     }
+    else
+    {
+        memcpy( p_sig->s, p_buf, 2 + i_s_len );
+        p_buf += 2 + i_s_len;
+    }
 
     return VLC_SUCCESS;
 }
@@ -262,10 +300,9 @@ static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
     char *p_ipos = p_ibuf;
     uint8_t *p_opos = p_obuf;
     int i_end = 0;
-
     int i_header_skipped = 0;
 
-    while( !i_end && p_ipos < p_ibuf + i_ibuf_len )
+    while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
     {
         if( *p_ipos == '\r' || *p_ipos == '\n' )
         {
@@ -301,9 +338,7 @@ static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
             p_ipos[i_line_len] = '\0';
 
         p_opos += vlc_b64_decode_binary_to_buffer(  p_opos,
-                                                    p_obuf - p_opos + i_obuf_len,
-                                                    p_ipos );
-
+                        p_obuf - p_opos + i_obuf_len, p_ipos );
         p_ipos += i_line_len + 1;
     }
 
@@ -326,7 +361,8 @@ static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
  * 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, char *psz_url )
+                                signature_packet_v3_t *p_sig,
+                                const char *psz_url )
 {
     char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
     if( !psz_sig )
@@ -342,21 +378,21 @@ static int download_signature(  vlc_object_t *p_this,
         return VLC_ENOMEM;
 
     int64_t i_size = stream_Size( p_stream );
-    if( i_size < 65 )
-    {
-        stream_Delete( p_stream );
-        return VLC_EGENERIC;
-    }
-    else if( i_size == 65 ) /* binary format signature */
+    if( i_size <= 65 ) /* binary format signature */
     {
+        msg_Dbg( p_this, "Downloading unarmored signature" );
         int i_read = stream_Read( p_stream, p_sig, (int)i_size );
         stream_Delete( p_stream );
         if( i_read != i_size )
+        {
+            msg_Dbg( p_this, "Couldn't read full signature" );
             return VLC_EGENERIC;
+        }
         else
             return VLC_SUCCESS;
     }
 
+    msg_Dbg( p_this, "Downloading armored signature" );
     char *p_buf = (char*)malloc( i_size );
     if( !p_buf )
     {
@@ -370,6 +406,7 @@ static int download_signature(  vlc_object_t *p_this,
 
     if( i_read != i_size )
     {
+        msg_Dbg( p_this, "Couldn't read full signature" );
         free( p_buf );
         return VLC_EGENERIC;
     }
@@ -377,10 +414,25 @@ static int download_signature(  vlc_object_t *p_this,
     int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
     free( p_buf );
 
-    if( i_bytes != 65 )
+    if( i_bytes > 65 )
+    {
+        msg_Dbg( p_this, "Signature is too big: %d bytes", i_bytes );
         return VLC_EGENERIC;
+    }
     else
+    {
+        int i_r_len = mpi_len( p_sig->r );
+        if( i_r_len > 20 )
+        {
+            msg_Dbg( p_this, "Signature invalid" );
+            return VLC_EGENERIC;
+        }
+        else if( i_r_len < 20 )
+            /* move s to the right place if r is less than 20 bytes */
+            memmove( p_sig->s, p_sig->r + 2 + i_r_len, 20 + 2 );
+
         return VLC_SUCCESS;
+    }
 }
 
 /*
@@ -402,19 +454,26 @@ static int verify_signature( vlc_object_t *p_this, uint8_t *p_r, uint8_t *p_s,
     gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
     key_sexp = hash_sexp = sig_sexp = NULL;
 
-    if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, 128, NULL ) ||
-        gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, 20, NULL ) ||
-        gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, 128, NULL ) ||
-        gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, 128, 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;
 
-    if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, 20, NULL ) ||
-        gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, 20, NULL ) ||
+    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;
 
-    if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, 20, NULL ) ||
+    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;
 
@@ -589,7 +648,7 @@ static uint8_t *hash_sha1_from_file( const char *psz_file,
     if( !f )
         return NULL;
 
-    uint8_t buffer[4096]; //FIXME
+    uint8_t buffer[4096];
 
     gcry_md_hd_t hd;
     if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
@@ -617,11 +676,9 @@ static uint8_t *hash_sha1_from_file( const char *psz_file,
 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
 {
     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 )
+    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;
 
     stream_t *p_stream = stream_UrlNew( p_this, psz_url );
@@ -648,6 +705,7 @@ static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid
 
     if( i_read != (int)i_size )
     {
+        msg_Dbg( p_this, "Couldn't read full GPG key" );
         free( p_buf );
         return NULL;
     }
@@ -664,6 +722,7 @@ static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid
 
     if( i_error != VLC_SUCCESS )
     {
+        msg_Dbg( p_this, "Couldn't parse GPG key" );
         free( p_pkey );
         return NULL;
     }
@@ -689,7 +748,23 @@ static uint8_t *key_sign_hash( public_key_t *p_pkey )
     gcry_md_putc( hd, (418 >> 8) & 0xff );
     gcry_md_putc( hd, 418 & 0xff );
 
-    gcry_md_write( hd, (uint8_t*)&p_pkey->key, 418 );
+    gcry_md_write( hd, (uint8_t*)&p_pkey->key, 6 ); /* version,timestamp,algo */
+
+    int i_p_len = mpi_len( p_pkey->key.p );
+    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 );
+
+    int i_g_len = mpi_len( p_pkey->key.g );
+    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 );
+
+    int i_q_len = mpi_len( p_pkey->key.q );
+    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 );
+
+    int i_y_len = mpi_len( p_pkey->key.y );
+    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 );
 
@@ -749,8 +824,7 @@ static uint8_t *key_sign_hash( public_key_t *p_pkey )
 update_t *__update_New( vlc_object_t *p_this )
 {
     update_t *p_update;
-
-    if( p_this == NULL ) return NULL;
+    assert( p_this );
 
     p_update = (update_t *)malloc( sizeof( update_t ) );
     if( !p_update ) return NULL;
@@ -759,10 +833,10 @@ update_t *__update_New( vlc_object_t *p_this )
 
     p_update->p_libvlc = p_this->p_libvlc;
 
-    p_update->release.psz_svnrev = NULL;
-    p_update->release.psz_extra = NULL;
     p_update->release.psz_url = NULL;
     p_update->release.psz_desc = NULL;
+    
+    p_update->p_pkey = NULL;
 
     return p_update;
 }
@@ -779,10 +853,9 @@ void update_Delete( update_t *p_update )
 
     vlc_mutex_destroy( &p_update->lock );
 
-    FREENULL( p_update->release.psz_svnrev );
-    FREENULL( p_update->release.psz_extra );
-    FREENULL( p_update->release.psz_url );
-    FREENULL( p_update->release.psz_desc );
+    free( p_update->release.psz_url );
+    free( p_update->release.psz_desc );
+    free( p_update->p_pkey );
 
     free( p_update );
 }
@@ -799,30 +872,26 @@ static void EmptyRelease( update_t *p_update )
     p_update->release.i_minor = 0;
     p_update->release.i_revision = 0;
 
-    FREENULL( p_update->release.psz_svnrev );
-    FREENULL( p_update->release.psz_extra );
     FREENULL( p_update->release.psz_url );
     FREENULL( p_update->release.psz_desc );
 }
 
 /**
  * Get the update file and parse it
- * *p_update has to be unlocked when calling this function
+ * *p_update has to be locked when calling this function
  *
  * \param p_update pointer to update struct
- * \return nothing
+ * \return VLC_TRUE if the update is valid and authenticated
  */
-static void GetUpdateFile( update_t *p_update )
+static vlc_bool_t GetUpdateFile( update_t *p_update )
 {
     stream_t *p_stream = NULL;
     int i_major = 0;
     int i_minor = 0;
     int i_revision = 0;
-    char *psz_extra = NULL;
-    char *psz_svnrev = NULL;
+    unsigned char extra;
     char *psz_line = NULL;
-
-    vlc_mutex_lock( &p_update->lock );
+    char *psz_version_line = NULL;
 
     p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
     if( !p_stream )
@@ -840,21 +909,21 @@ static void GetUpdateFile( update_t *p_update )
         goto error;
     }
 
+    psz_version_line = psz_line;
     /* first line : version number */
-    if( sscanf( psz_line, "%i.%i.%i%as %as", &i_major, &i_minor, &i_revision, &psz_extra, &psz_svnrev ) )
+    p_update->release.extra = 0;
+    switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
     {
-        p_update->release.i_major = i_major;
-        p_update->release.i_minor = i_minor;
-        p_update->release.i_revision = i_revision;
-
-        p_update->release.psz_svnrev = psz_svnrev ? psz_svnrev : STRDUP( "" );
-        p_update->release.psz_extra = psz_extra ? psz_extra : STRDUP( "" );
-    }
-    else
-    {
-        msg_Err( p_update->p_libvlc, "Update version false formated" );
-        free( psz_line );
-        goto error;
+        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 */
@@ -876,52 +945,183 @@ static void GetUpdateFile( update_t *p_update )
     }
     p_update->release.psz_desc = psz_line;
 
-    error:
-        vlc_mutex_unlock( &p_update->lock );
+    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_v3_t 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" );
+        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;
+    }
+
+    if( memcmp( sign.issuer_longid, videolan_public_key_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 )
+        {
+            msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
+            FREENULL( p_update->p_pkey );
+            goto error;
+        }
+
+        uint8_t *p_hash = key_sign_hash( p_new_pkey );
+        if( !p_hash )
+        {
+            msg_Err( p_update->p_libvlc, "Failed to hash signature" );
+            free( p_new_pkey );
+            FREENULL( p_update->p_pkey );
+            goto error;
+        }
+
+        if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
+                    p_new_pkey->sig.r, p_new_pkey->sig.s,
+                    &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
+        {
+            free( p_hash );
+            msg_Info( p_update->p_libvlc, "Key authenticated" );
+            free( p_update->p_pkey );
+            p_update->p_pkey = p_new_pkey;
+        }
+        else
+        {
+            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;
+
+    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' );
+    gcry_md_write( hd, p_update->release.psz_desc,
+                        strlen( p_update->release.psz_desc ) );
+    if( sign.type == TEXT_SIGNATURE )
+        gcry_md_putc( hd, '\r' );
+    gcry_md_putc( hd, '\n' );
+
+    gcry_md_putc( hd, sign.type );
+    gcry_md_write( hd, &sign.timestamp, 4 );
+
+    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" );
+        free( p_hash );
+        goto error;
+    }
 
-        if( p_stream )
-            stream_Delete( p_stream );
+    if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
+            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;
+    }
+    else
+    {
+        msg_Info( p_update->p_libvlc, "Status file authenticated" );
+        free( p_hash );
+        return VLC_TRUE;
+    }
+
+error:
+    if( p_stream )
+        stream_Delete( p_stream );
+    free( psz_version_line );
+    return VLC_FALSE;
 }
 
+
+/**
+ * Struct to launch the check in an other thread
+ */
+typedef struct
+{
+    VLC_COMMON_MEMBERS
+    update_t *p_update;
+    void (*pf_callback)( void *, vlc_bool_t );
+    void *p_data;
+} update_check_thread_t;
+
+void update_CheckReal( update_check_thread_t *p_uct );
+
 /**
  * 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
  */
-void update_Check( update_t *p_update )
+void update_Check( update_t *p_update, void (*pf_callback)( void*, vlc_bool_t ), void *p_data )
 {
     assert( p_update );
 
-    EmptyRelease( p_update );
+    update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
+                                            sizeof( update_check_thread_t ) );
+    p_uct->p_update = p_update;
+    p_uct->pf_callback = pf_callback;
+    p_uct->p_data = p_data;
 
-    GetUpdateFile( p_update );
+    vlc_thread_create( p_uct, "check for update", update_CheckReal,
+                       VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
 }
 
-/**
- * Compare two extra
- *
- * \param p1 first integer
- * \param p2 second integer
- * \return like strcmp
- */
-static int extracmp( char *psz_1, char *psz_2 )
+void update_CheckReal( update_check_thread_t *p_uct )
 {
-    if( psz_1[0] == '-' )
-    {
-        if( psz_2[0] == '-' )
-            return strcmp( psz_1, psz_2 );
-        else
-            return 1;
-    }
-    else
-    {
-        if( psz_2[0] == '-' )
-            return -1;
-        else
-            return strcmp( psz_1, psz_2 );
-    }
+    vlc_bool_t 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 );
+
+    vlc_object_destroy( p_uct );
 }
+
 /**
  * Compare two release numbers
  *
@@ -933,12 +1133,9 @@ 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 );
-    d = d - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 );
-    d += extracmp( p1->psz_extra, p2->psz_extra );
-
-    if( d == 0 )
-        d = strcmp( p1->psz_svnrev, p2->psz_svnrev );
+    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;
@@ -951,7 +1148,7 @@ static int CompareReleases( const struct update_release_t *p1,
 /**
  * Compare a given release's version number to the current VLC's one
  *
- * \param p a release
+ * \param p_update structure
  * \return UpdateReleaseStatus(Older|Equal|Newer)
  */
 int update_CompareReleaseToCurrent( update_t *p_update )
@@ -959,30 +1156,244 @@ int update_CompareReleaseToCurrent( update_t *p_update )
     assert( p_update );
 
     struct update_release_t c;
-    int i_major = 0;
-    int i_minor = 0;
-    int i_revision = 0;
-    char *psz_extra;
-    int i_result = UpdateReleaseStatusOlder;
 
     /* get the current version number */
-    if( sscanf( PACKAGE_VERSION, "%i.%i.%i%as", &i_major, &i_minor, &i_revision, &psz_extra ) )
+    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 );
+}
+
+/**
+ * Convert a long int size in bytes to a string
+ *
+ * \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;
+    if( l_size >> 30 )
+        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) );
+    else if( l_size >> 10 )
+        asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
+    else
+        asprintf( &psz_tmp, "%ld B", l_size );
+    return psz_tmp;
+}
+
+
+/*
+ * Struct to launch the download in a thread
+ */
+typedef struct
+{
+    VLC_COMMON_MEMBERS
+    update_t *p_update;
+    char *psz_destdir;
+} update_download_thread_t;
+
+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( 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 ) );
+
+    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 );
+}
+
+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;
+
+    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 )
     {
-        c.i_major = i_major;
-        c.i_minor = i_minor;
-        c.i_revision = i_revision;
-        if( psz_extra )
-            c.psz_extra = psz_extra;
-        else
-            c.psz_extra = STRDUP( "" );
-        c.psz_svnrev = STRDUP( VLC_Changeset() );
+        msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
+        goto end;
+    }
+
+    /* 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 = 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 );
+    }
+
+    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 );
 
-        i_result = CompareReleases( &p_update->release, &c );
+        l_downloaded += i_read;
+        psz_downloaded = size_str( l_downloaded );
+        f_progress = 100.0*(float)l_downloaded/(float)l_size;
 
-        free( c.psz_extra );
-        free( c.psz_svnrev );
+        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 );
     }
-    return i_result;
+
+    /* Finish the progress bar or delete the file if the user had canceled */
+    fclose( p_file );
+    p_file = NULL;
+
+    if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
+    {
+        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
+    {
+        utf8_unlink( psz_destfile );
+        goto end;
+    }
+
+    signature_packet_v3_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, VLC_TRUE, _("File can not be verified"),
+            _("It was not possible to downlaod 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( sign.type != BINARY_SIGNATURE )
+    {
+        utf8_unlink( psz_destfile );
+        msg_Err( p_udt, "Invalid signature type" );
+        intf_UserFatal( p_udt, VLC_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;
+    }
+
+    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, VLC_TRUE, _("File not verifiable"),
+            _("It was not possible to securely verify downloaded file \"%s\", "
+              "and so VLC deleted it."),
+            psz_destfile );
+
+        goto end;
+    }
+
+    if( p_hash[0] != sign.hash_verification[0] ||
+        p_hash[1] != sign.hash_verification[1] )
+    {
+        utf8_unlink( psz_destfile );
+        intf_UserFatal( p_udt, VLC_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 );
+        free( p_hash );
+        goto end;
+    }
+
+    if( verify_signature( VLC_OBJECT(p_udt), 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"),
+            _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
+             psz_destfile );
+        msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
+        free( p_hash );
+        goto end;
+    }
+
+    msg_Info( p_udt, "%s authenticated", psz_destfile );
+    free( p_hash );
+
+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 );
+
+    vlc_object_destroy( p_udt );
 }
 
 #endif