]> git.sesse.net Git - vlc/blob - src/misc/update.c
update: do not make wrong assumptions about mpis' size
[vlc] / src / misc / update.c
1 /*****************************************************************************
2  * update.c: VLC update checking and downloading
3  *****************************************************************************
4  * Copyright © 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *          Rémi Duraffort <ivoire at via.ecp.fr>
9             Rafaël Carré <funman@videolanorg>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either release 2 of the License, or
14  * (at your option) any later release.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /**
27  *   \file
28  *   This file contains functions related to VLC update management
29  */
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 #include <vlc/vlc.h>
36
37 #ifdef UPDATE_CHECK
38
39 #include <assert.h>
40
41 #include <vlc_update.h>
42 #include <vlc_pgpkey.h>
43 #include <vlc_stream.h>
44 #include <vlc_interface.h>
45
46
47 /*****************************************************************************
48  * Misc defines
49  *****************************************************************************/
50
51 /*
52  * Here is the format of these "status files" :
53  * First line is the last version: "X.Y.Ze" where:
54  *      * X is the major number
55  *      * Y is the minor number
56  *      * Z is the revision number
57  *      * e is an OPTIONAL extra letter
58  *      * AKA "0.8.6d" or "0.9.0"
59  * Second line is an url of the binary for this last version
60  * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
61  */
62
63 #if defined( UNDER_CE )
64 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
65 #elif defined( WIN32 )
66 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
67 #elif defined( __APPLE__ )
68 #   if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
69 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
70 #   else
71 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
72 #   endif
73 #elif defined( SYS_BEOS )
74 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
75 #else
76 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
77 #endif
78
79
80 /*****************************************************************************
81  * Local Prototypes
82  *****************************************************************************/
83 static void EmptyRelease( update_t *p_update );
84 static vlc_bool_t GetUpdateFile( update_t *p_update );
85 static int CompareReleases( const struct update_release_t *p1,
86                             const struct update_release_t *p2 );
87 static char * size_str( long int l_size );
88
89
90 /*****************************************************************************
91  * OpenPGP functions
92  *****************************************************************************/
93
94 #define packet_type( c ) ( ( c & 0x3c ) >> 2 )      /* 0x3C = 00111100 */
95 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
96
97 static inline int scalar_number( uint8_t *p, int header_len )
98 {
99     if( header_len == 1 )
100         return( p[0] );
101     else if( header_len == 2 )
102         return( (p[0] << 8) + p[1] );
103     else if( header_len == 4 )
104         return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
105     else
106         abort();
107 }
108
109 /* number of data bytes in a MPI */
110 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
111
112 /* 
113  * fill a public_key_packet_t structure from public key packet data
114  * verify that it is a version 4 public key packet, using DSA
115  */
116 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
117                                     size_t i_packet_len )
118 {
119     if( i_packet_len > 418 )
120         return VLC_EGENERIC;
121
122     p_key->version   = *p_buf++;
123     if( p_key->version != 4 )
124         return VLC_EGENERIC;
125
126     /* warn when timestamp is > date ? */
127     memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;
128
129     p_key->algo      = *p_buf++;
130     if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
131         return VLC_EGENERIC;
132
133     int i_p_len = mpi_len( p_buf );
134     if( i_p_len > 128 )
135         return VLC_EGENERIC;
136     else
137     {
138         memcpy( p_key->p, p_buf, 2+i_p_len ); p_buf += 2+i_p_len;
139         if( i_p_len < 128 )
140             memmove( p_key->q, p_key->p + 2+i_p_len, 2+20 + 2+128 + 2+128 );
141     }
142
143     int i_q_len = mpi_len( p_buf );
144     if( i_q_len > 20 )
145         return VLC_EGENERIC;
146     else
147     {
148         memcpy( p_key->q, p_buf, 2+i_q_len );  p_buf += 2+i_q_len;
149         if( i_p_len < 20 )
150             memmove( p_key->g, p_key->q + 2+i_q_len, 2+128 + 2+128 );
151     }
152
153     int i_g_len = mpi_len( p_buf );
154     if( i_g_len > 128 )
155         return VLC_EGENERIC;
156     else
157     {
158         memcpy( p_key->g, p_buf, 2+i_g_len ); p_buf += 2+i_g_len;
159         if( i_g_len < 128 )
160             memmove( p_key->y, p_key->g + 2+i_g_len, 2+128 );
161     }
162     
163     int i_y_len = mpi_len( p_buf );
164     if( i_y_len > 128 )
165         return VLC_EGENERIC;
166     else
167         memcpy( p_key->y, p_buf, 2+i_y_len );
168
169     return VLC_SUCCESS;
170 }
171
172 /*
173  * fill a signature_packet_v4_t from signature packet data
174  * verify that it was used with a DSA public key, using SHA-1 digest
175  */
176 static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
177                                       uint8_t *p_buf, size_t i_sig_len )
178 {
179     if( i_sig_len < 54 )
180         return VLC_EGENERIC;
181
182     p_sig->version = *p_buf++;
183     if( p_sig->version != 4 )
184         return VLC_EGENERIC;
185
186     p_sig->type = *p_buf++;
187     if( p_sig->type < GENERIC_KEY_SIGNATURE ||
188         p_sig->type > POSITIVE_KEY_SIGNATURE )
189         return VLC_EGENERIC;
190
191     p_sig->public_key_algo = *p_buf++;
192     if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
193         return VLC_EGENERIC;
194
195     p_sig->digest_algo = *p_buf++;
196     if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
197         return VLC_EGENERIC;
198
199     memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;
200
201     size_t i_pos = 6;
202     size_t i_hashed_data_len = scalar_number( p_sig->hashed_data_len, 2 );
203     i_pos += i_hashed_data_len;
204     if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total, 
205                               * + the unhashed data length (2 bytes)
206                               * + the hash verification (2 bytes) */
207         return VLC_EGENERIC;
208
209     p_sig->hashed_data = (uint8_t*) malloc( i_hashed_data_len );
210     if( !p_sig->hashed_data )
211         return VLC_ENOMEM;
212     memcpy( p_sig->hashed_data, p_buf, i_hashed_data_len );
213     p_buf += i_hashed_data_len;
214
215     memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;
216
217     size_t i_unhashed_data_len = scalar_number( p_sig->unhashed_data_len, 2 );
218     i_pos += 2 + i_unhashed_data_len;
219     if( i_pos != i_sig_len - 46 )
220     {
221         free( p_sig->hashed_data );
222         return VLC_EGENERIC;
223     }
224
225     p_sig->unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
226     if( !p_sig->unhashed_data )
227     {
228         free( p_sig->hashed_data );
229         return VLC_ENOMEM;
230     }
231     memcpy( p_sig->unhashed_data, p_buf, i_unhashed_data_len );
232     p_buf += i_unhashed_data_len;
233
234     memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
235
236     int i_r_len = mpi_len( p_buf );
237     if( i_r_len > 20 )
238     {
239         free( p_sig->hashed_data );
240         free( p_sig->unhashed_data );
241         return VLC_EGENERIC;
242     }
243     else
244     {
245         memcpy( p_sig->r, p_buf, 2 + i_r_len );
246         p_buf += 2 + i_r_len;
247     }
248
249     int i_s_len = mpi_len( p_buf );
250     if( i_s_len > 20 )
251     {
252         free( p_sig->hashed_data );
253         free( p_sig->unhashed_data );
254         return VLC_EGENERIC;
255     }
256     else
257     {
258         memcpy( p_sig->s, p_buf, 2 + i_s_len );
259         p_buf += 2 + i_s_len;
260     }
261
262     return VLC_SUCCESS;
263 }
264
265 /*
266  * crc_octets() was lamely copied from rfc 2440
267  * Copyright (C) The Internet Society (1998).  All Rights Reserved.
268  */
269 #define CRC24_INIT 0xB704CEL
270 #define CRC24_POLY 0x1864CFBL
271
272 static long crc_octets( uint8_t *octets, size_t len )
273 {
274     long crc = CRC24_INIT;
275     int i;
276     while (len--)
277     {
278         crc ^= (*octets++) << 16;
279         for (i = 0; i < 8; i++)
280         {
281             crc <<= 1;
282             if (crc & 0x1000000)
283                 crc ^= CRC24_POLY;
284         }
285     }
286     return crc & 0xFFFFFFL;
287 }
288
289 /*
290  * Transform an armored document in binary format
291  * Used on public keys and signatures
292  */
293 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
294                         uint8_t *p_obuf, size_t i_obuf_len )
295 {
296     char *p_ipos = p_ibuf;
297     uint8_t *p_opos = p_obuf;
298     int i_end = 0;
299     int i_header_skipped = 0;
300
301     while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
302     {
303         if( *p_ipos == '\r' || *p_ipos == '\n' )
304         {
305             p_ipos++;
306             continue;
307         }
308
309         size_t i_line_len = strcspn( p_ipos, "\r\n" );
310         if( i_line_len == 0 )
311             continue;
312
313         if( !i_header_skipped )
314         {
315             if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
316                 i_header_skipped = 1;
317
318             p_ipos += i_line_len + 1;
319             continue;
320         }
321         
322         if( !strncmp( p_ipos, "Version:", 8 ) )
323         {
324             p_ipos += i_line_len + 1;
325             continue;
326         }
327
328         if( p_ipos[i_line_len - 1] == '=' )
329         {
330             i_end = 1;
331             p_ipos[i_line_len - 1] = '\0';
332         }
333         else
334             p_ipos[i_line_len] = '\0';
335
336         p_opos += vlc_b64_decode_binary_to_buffer(  p_opos,
337                         p_obuf - p_opos + i_obuf_len, p_ipos );
338         p_ipos += i_line_len + 1;
339     }
340
341     /* XXX: the CRC is OPTIONAL, really require it ? */
342     if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
343         return 0;
344
345     uint8_t p_crc[3];
346     if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
347         return 0;
348
349     long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
350     long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
351
352     return l_crc2 == l_crc ? p_opos - p_obuf : 0;
353 }
354
355 /*
356  * Download the signature associated to a document or a binary file.
357  * We're given the file's url, we just append ".asc" to it and download 
358  */
359 static int download_signature(  vlc_object_t *p_this,
360                                 signature_packet_v3_t *p_sig,
361                                 const char *psz_url )
362 {
363     char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
364     if( !psz_sig )
365         return VLC_ENOMEM;
366
367     strcpy( psz_sig, psz_url );
368     strcat( psz_sig, ".asc" );
369
370     stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
371     free( psz_sig );
372
373     if( !p_stream )
374         return VLC_ENOMEM;
375
376     int64_t i_size = stream_Size( p_stream );
377     if( i_size <= 65 ) /* binary format signature */
378     {
379         msg_Dbg( p_this, "Downloading unarmored signature" );
380         int i_read = stream_Read( p_stream, p_sig, (int)i_size );
381         stream_Delete( p_stream );
382         if( i_read != i_size )
383         {
384             msg_Dbg( p_this, "Couldn't read full signature" );
385             return VLC_EGENERIC;
386         }
387         else
388             return VLC_SUCCESS;
389     }
390
391     msg_Dbg( p_this, "Downloading armored signature" );
392     char *p_buf = (char*)malloc( i_size );
393     if( !p_buf )
394     {
395         stream_Delete( p_stream );
396         return VLC_ENOMEM;
397     }
398     
399     int i_read = stream_Read( p_stream, p_buf, (int)i_size );
400
401     stream_Delete( p_stream );
402
403     if( i_read != i_size )
404     {
405         msg_Dbg( p_this, "Couldn't read full signature" );
406         free( p_buf );
407         return VLC_EGENERIC;
408     }
409     
410     int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
411     free( p_buf );
412
413     if( i_bytes > 65 )
414     {
415         msg_Dbg( p_this, "Signature is too big: %d bytes", i_bytes );
416         return VLC_EGENERIC;
417     }
418     else
419     {
420         int i_r_len = mpi_len( p_sig->r );
421         if( i_r_len > 20 )
422         {
423             msg_Dbg( p_this, "Signature invalid" );
424             return VLC_EGENERIC;
425         }
426         else if( i_r_len < 20 )
427             /* move s to the right place if r is less than 20 bytes */
428             memmove( p_sig->s, p_sig->r + 2 + i_r_len, 20 + 2 );
429
430         return VLC_SUCCESS;
431     }
432 }
433
434 /*
435  * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
436  */
437 static int verify_signature( vlc_object_t *p_this, uint8_t *p_r, uint8_t *p_s,
438         public_key_packet_t *p_key, uint8_t *p_hash )
439 {
440     /* the data to be verified (a SHA-1 hash) */
441     const char *hash_sexp_s = "(data(flags raw)(value %m))";
442     /* the public key */
443     const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
444     /* the signature */
445     const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
446
447     size_t erroff;
448     gcry_mpi_t p, q, g, y, r, s, hash;
449     p = q = g = y = r = s = hash = NULL;
450     gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
451     key_sexp = hash_sexp = sig_sexp = NULL;
452
453     int i_p_len = mpi_len( p_key->p );
454     int i_q_len = mpi_len( p_key->q );
455     int i_g_len = mpi_len( p_key->g );
456     int i_y_len = mpi_len( p_key->y );
457     if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, i_p_len, NULL ) ||
458         gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, i_q_len, NULL ) ||
459         gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, i_g_len, NULL ) ||
460         gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, i_y_len, NULL ) ||
461         gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
462         goto problem;
463
464     int i_r_len = mpi_len( p_r );
465     int i_s_len = mpi_len( p_s );
466     if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, i_r_len, NULL ) ||
467         gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, i_s_len, NULL ) ||
468         gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
469         goto problem;
470
471     int i_hash_len = 20;
472     if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
473         gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
474         goto problem;
475
476     if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
477         goto problem;
478
479     return VLC_SUCCESS;
480
481 problem:
482     if( p ) gcry_mpi_release( p );
483     if( q ) gcry_mpi_release( q );
484     if( g ) gcry_mpi_release( g );
485     if( y ) gcry_mpi_release( y );
486     if( r ) gcry_mpi_release( r );
487     if( s ) gcry_mpi_release( s );
488     if( hash ) gcry_mpi_release( hash );
489     if( key_sexp ) gcry_sexp_release( key_sexp );
490     if( sig_sexp ) gcry_sexp_release( sig_sexp );
491     if( hash_sexp ) gcry_sexp_release( hash_sexp );
492     return VLC_EGENERIC;
493 }
494
495 /*
496  * Return the long id (8 bytes) of the public key used to generate a signature
497  */
498 static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
499 {
500     uint8_t *p = p_sig->unhashed_data;
501     uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );
502
503     while( p < max_pos )
504     {
505         int i_subpacket_len = *p < 192 ? *p++ :
506                 *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
507                 ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;
508
509         if( p >= max_pos - 1 )
510             return NULL;
511
512         if( *p == ISSUER_SUBPACKET )
513             return p+1;
514         else
515             p += i_subpacket_len;
516     }
517     return NULL;
518 }
519
520 /*
521  * fill a public_key_t with public key data, including:
522  *   * public key packet
523  *   * signature packet issued by key which long id is p_sig_issuer
524  *   * user id packet
525  */
526 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 )
527 {
528     uint8_t *pos = (uint8_t*) p_key_data;
529     uint8_t *max_pos = pos + i_key_len;
530
531     int i_status = 0;
532 #define PUBLIC_KEY_FOUND    0x01
533 #define USER_ID_FOUND       0x02
534 #define SIGNATURE_FOUND     0X04
535
536     uint8_t *p_key_unarmored = NULL;
537
538     signature_packet_v4_t sig;
539
540     p_key->psz_username = NULL;
541     p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;
542
543     if( !( *pos & 0x80 ) )
544     {   /* first byte is ASCII, unarmoring */
545         p_key_unarmored = (uint8_t*)malloc( i_key_len );
546         if( !p_key_unarmored )
547             return VLC_ENOMEM;
548         int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
549                                  p_key_unarmored, i_key_len );
550
551         if( i_len == 0 )
552             goto error;
553
554         pos = p_key_unarmored;
555         max_pos = pos + i_len;
556     }
557
558     while( pos < max_pos )
559     {
560         if( !(*pos & 0x80) || *pos & 0x40 )
561             goto error;
562
563         int i_type = packet_type( *pos );
564
565         int i_header_len = packet_header_len( *pos++ );
566         if( pos + i_header_len > max_pos )
567             goto error;
568
569         int i_packet_len = scalar_number( pos, i_header_len );
570         pos += i_header_len;
571
572         if( pos + i_packet_len > max_pos )
573             goto error;
574
575         switch( i_type )
576         {
577             uint8_t *p_issuer;
578
579             case PUBLIC_KEY_PACKET:
580                 i_status |= PUBLIC_KEY_FOUND;
581                 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
582                     goto error;
583                 break;
584
585             case SIGNATURE_PACKET:
586                 if( !p_sig_issuer || i_status & SIGNATURE_FOUND ||
587                     parse_signature_v4_packet( &sig, pos, i_packet_len ) != VLC_SUCCESS )
588                     break;
589                 p_issuer = get_issuer_from_signature_v4( &sig );
590                 if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
591                 {
592                     memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
593                     i_status |= SIGNATURE_FOUND;
594                 }
595                 else
596                 {
597                     free( sig.hashed_data );
598                     free( sig.unhashed_data );
599                 }
600                 break;
601
602             case USER_ID_PACKET:
603                 if( p_key->psz_username ) /* save only the first User ID */
604                     break;
605                 i_status |= USER_ID_FOUND;
606                 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
607                 if( !p_key->psz_username )
608                     goto error;
609
610                 memcpy( p_key->psz_username, pos, i_packet_len );
611                 p_key->psz_username[i_packet_len] = '\0';
612                 break;
613             
614             default:
615                 break;
616         }
617         pos += i_packet_len;
618     }
619     free( p_key_unarmored );
620
621     if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
622         return VLC_EGENERIC;
623
624     if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
625         return VLC_EGENERIC;
626
627     return VLC_SUCCESS;
628
629 error:
630     free( p_key->sig.hashed_data );
631     free( p_key->sig.unhashed_data );
632     free( p_key->psz_username );
633     free( p_key_unarmored );
634     return VLC_EGENERIC;
635 }
636
637 /*
638  * return a sha1 hash of a file
639  */
640 static uint8_t *hash_sha1_from_file( const char *psz_file,
641                             signature_packet_v3_t *p_sig )
642 {
643     FILE *f = utf8_fopen( psz_file, "r" );
644     if( !f )
645         return NULL;
646
647     uint8_t buffer[4096];
648
649     gcry_md_hd_t hd;
650     if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
651     {
652         fclose( f );
653         return NULL;
654     } 
655
656     size_t i_read;
657     while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
658         gcry_md_write( hd, buffer, i_read );
659
660     gcry_md_putc( hd, p_sig->type );
661     gcry_md_write( hd, &p_sig->timestamp, 4 );
662
663     fclose( f );
664     gcry_md_final( hd );
665
666     /* FIXME: is it always padded to 20 bytes ? */
667     return( (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1) );
668 }
669
670 /*
671  * download a public key (the last one) from videolan server, and parse it
672  */
673 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
674 {
675     char *psz_url;
676     if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
677                     p_longid[0], p_longid[1], p_longid[2], p_longid[3],
678                     p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
679         return NULL;
680
681     stream_t *p_stream = stream_UrlNew( p_this, psz_url );
682     free( psz_url );
683     if( !p_stream )
684         return NULL;
685
686     int64_t i_size = stream_Size( p_stream );
687     if( i_size < 0 )
688     {
689         stream_Delete( p_stream );
690         return NULL;
691     }
692
693     uint8_t *p_buf = (uint8_t*)malloc( i_size );
694     if( !p_buf )
695     {
696         stream_Delete( p_stream );
697         return NULL;
698     }
699
700     int i_read = stream_Read( p_stream, p_buf, (int)i_size );
701     stream_Delete( p_stream );
702
703     if( i_read != (int)i_size )
704     {
705         msg_Dbg( p_this, "Couldn't read full GPG key" );
706         free( p_buf );
707         return NULL;
708     }
709
710     public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
711     if( !p_pkey )
712     {
713         free( p_buf );
714         return NULL;
715     }
716
717     int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
718     free( p_buf );
719
720     if( i_error != VLC_SUCCESS )
721     {
722         msg_Dbg( p_this, "Couldn't parse GPG key" );
723         free( p_pkey );
724         return NULL;
725     }
726
727     return p_pkey;
728 }
729
730 /*
731  * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
732  * Note that we need the signature to compute the hash
733  */
734 static uint8_t *key_sign_hash( public_key_t *p_pkey )
735 {
736     gcry_error_t error = 0;
737     gcry_md_hd_t hd;
738
739     error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
740     if( error )
741         return NULL;
742
743     gcry_md_putc( hd, 0x99 );
744
745     gcry_md_putc( hd, (418 >> 8) & 0xff );
746     gcry_md_putc( hd, 418 & 0xff );
747
748     gcry_md_write( hd, (uint8_t*)&p_pkey->key, 6 ); /* version,timestamp,algo */
749
750     int i_p_len = mpi_len( p_pkey->key.p );
751     gcry_md_write( hd, (uint8_t*)&p_pkey->key.p, 2 );
752     gcry_md_write( hd, (uint8_t*)&p_pkey->key.p + 2, i_p_len );
753
754     int i_g_len = mpi_len( p_pkey->key.g );
755     gcry_md_write( hd, (uint8_t*)&p_pkey->key.g, 2 );
756     gcry_md_write( hd, (uint8_t*)&p_pkey->key.g + 2, i_g_len );
757
758     int i_q_len = mpi_len( p_pkey->key.q );
759     gcry_md_write( hd, (uint8_t*)&p_pkey->key.q, 2 );
760     gcry_md_write( hd, (uint8_t*)&p_pkey->key.q + 2, i_q_len );
761
762     int i_y_len = mpi_len( p_pkey->key.y );
763     gcry_md_write( hd, (uint8_t*)&p_pkey->key.y, 2 );
764     gcry_md_write( hd, (uint8_t*)&p_pkey->key.y + 2, i_y_len );
765
766     gcry_md_putc( hd, 0xb4 );
767
768     int i_len = strlen((char*)p_pkey->psz_username);
769
770     gcry_md_putc( hd, (i_len << 24) & 0xff );
771     gcry_md_putc( hd, (i_len << 16) & 0xff );
772     gcry_md_putc( hd, (i_len << 8) & 0xff );
773     gcry_md_putc( hd, (i_len) & 0xff );
774
775     gcry_md_write( hd, p_pkey->psz_username, i_len );
776
777     size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );
778
779     gcry_md_putc( hd, p_pkey->sig.version );
780     gcry_md_putc( hd, p_pkey->sig.type );
781     gcry_md_putc( hd, p_pkey->sig.public_key_algo );
782     gcry_md_putc( hd, p_pkey->sig.digest_algo );
783     gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
784     gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );
785
786     gcry_md_putc( hd, 0x04 );
787     gcry_md_putc( hd, 0xff );
788
789     i_hashed_data_len += 6; /* hashed data + 6 bytes header */
790
791     gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
792     gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
793     gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
794     gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
795
796     gcry_md_final( hd );
797
798     uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
799
800     if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
801         p_hash[1] != p_pkey->sig.hash_verification[1] )
802     {
803         free( p_hash );
804         return NULL;
805     }
806
807     return p_hash;
808 }
809
810
811 /*****************************************************************************
812  * Update_t functions
813  *****************************************************************************/
814
815 /**
816  * Create a new update VLC struct
817  *
818  * \param p_this the calling vlc_object
819  * \return pointer to new update_t or NULL
820  */
821 update_t *__update_New( vlc_object_t *p_this )
822 {
823     update_t *p_update;
824     assert( p_this );
825
826     p_update = (update_t *)malloc( sizeof( update_t ) );
827     if( !p_update ) return NULL;
828
829     vlc_mutex_init( p_this, &p_update->lock );
830
831     p_update->p_libvlc = p_this->p_libvlc;
832
833     p_update->release.psz_url = NULL;
834     p_update->release.psz_desc = NULL;
835     
836     p_update->p_pkey = NULL;
837
838     return p_update;
839 }
840
841 /**
842  * Delete an update_t struct
843  *
844  * \param p_update update_t* pointer
845  * \return nothing
846  */
847 void update_Delete( update_t *p_update )
848 {
849     assert( p_update );
850
851     vlc_mutex_destroy( &p_update->lock );
852
853     free( p_update->release.psz_url );
854     free( p_update->release.psz_desc );
855     free( p_update->p_pkey );
856
857     free( p_update );
858 }
859
860 /**
861  * Empty the release struct
862  *
863  * \param p_update update_t* pointer
864  * \return nothing
865  */
866 static void EmptyRelease( update_t *p_update )
867 {
868     p_update->release.i_major = 0;
869     p_update->release.i_minor = 0;
870     p_update->release.i_revision = 0;
871
872     FREENULL( p_update->release.psz_url );
873     FREENULL( p_update->release.psz_desc );
874 }
875
876 /**
877  * Get the update file and parse it
878  * *p_update has to be locked when calling this function
879  *
880  * \param p_update pointer to update struct
881  * \return VLC_TRUE if the update is valid and authenticated
882  */
883 static vlc_bool_t GetUpdateFile( update_t *p_update )
884 {
885     stream_t *p_stream = NULL;
886     int i_major = 0;
887     int i_minor = 0;
888     int i_revision = 0;
889     unsigned char extra;
890     char *psz_line = NULL;
891     char *psz_version_line = NULL;
892
893     p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
894     if( !p_stream )
895     {
896         msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
897                  UPDATE_VLC_STATUS_URL );
898         goto error;
899     }
900
901     /* Try to read three lines */
902     if( !( psz_line = stream_ReadLine( p_stream ) ) )
903     {
904         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
905                  UPDATE_VLC_STATUS_URL );
906         goto error;
907     }
908
909     psz_version_line = psz_line;
910     /* first line : version number */
911     p_update->release.extra = 0;
912     switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
913     {
914         case 4:
915             p_update->release.extra = extra;
916         case 3:
917             p_update->release.i_major = i_major;
918             p_update->release.i_minor = i_minor;
919             p_update->release.i_revision = i_revision;
920             break;
921         default:
922             msg_Err( p_update->p_libvlc, "Update version false formated" );
923             goto error;
924     }
925
926     /* Second line : URL */
927     if( !( psz_line = stream_ReadLine( p_stream ) ) )
928     {
929         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
930                  UPDATE_VLC_STATUS_URL );
931         goto error;
932     }
933     p_update->release.psz_url = psz_line;
934
935
936     /* Third line : description */
937     if( !( psz_line = stream_ReadLine( p_stream ) ) )
938     {
939         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
940                  UPDATE_VLC_STATUS_URL );
941         goto error;
942     }
943     p_update->release.psz_desc = psz_line;
944
945     stream_Delete( p_stream );
946     p_stream = NULL;
947
948     /* Now that we know the status is valid, we must download its signature 
949      * to authenticate it */
950     signature_packet_v3_t sign;
951     if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign, 
952             UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
953     {
954         msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
955         goto error;
956     }
957
958     if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
959     {
960         msg_Err( p_update->p_libvlc, "Invalid signature type" );
961         goto error;
962     }
963
964     p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
965     if( !p_update->p_pkey )
966         goto error;
967
968     if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
969                         p_update->p_pkey, NULL ) != VLC_SUCCESS )
970     {
971         msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
972         FREENULL( p_update->p_pkey );
973         goto error;
974     }
975
976     if( memcmp( sign.issuer_longid, videolan_public_key_longid , 8 ) != 0 )
977     {
978         msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
979         public_key_t *p_new_pkey = download_key(
980                 VLC_OBJECT(p_update->p_libvlc),
981                 sign.issuer_longid, videolan_public_key_longid );
982         if( !p_new_pkey )
983         {
984             msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
985             FREENULL( p_update->p_pkey );
986             goto error;
987         }
988
989         uint8_t *p_hash = key_sign_hash( p_new_pkey );
990         if( !p_hash )
991         {
992             msg_Err( p_update->p_libvlc, "Failed to hash signature" );
993             free( p_new_pkey );
994             FREENULL( p_update->p_pkey );
995             goto error;
996         }
997
998         if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
999                     p_new_pkey->sig.r, p_new_pkey->sig.s,
1000                     &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
1001         {
1002             free( p_hash );
1003             msg_Info( p_update->p_libvlc, "Key authenticated" );
1004             free( p_update->p_pkey );
1005             p_update->p_pkey = p_new_pkey;
1006         }
1007         else
1008         {
1009             free( p_hash );
1010             msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
1011             goto error;
1012         }
1013     }
1014
1015     gcry_md_hd_t hd;
1016     if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
1017         goto error;
1018
1019     gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
1020     FREENULL( psz_version_line );
1021     if( sign.type == TEXT_SIGNATURE )
1022         gcry_md_putc( hd, '\r' );
1023     gcry_md_putc( hd, '\n' );
1024     gcry_md_write( hd, p_update->release.psz_url,
1025                         strlen( p_update->release.psz_url ) );
1026     if( sign.type == TEXT_SIGNATURE )
1027         gcry_md_putc( hd, '\r' );
1028     gcry_md_putc( hd, '\n' );
1029     gcry_md_write( hd, p_update->release.psz_desc,
1030                         strlen( p_update->release.psz_desc ) );
1031     if( sign.type == TEXT_SIGNATURE )
1032         gcry_md_putc( hd, '\r' );
1033     gcry_md_putc( hd, '\n' );
1034
1035     gcry_md_putc( hd, sign.type );
1036     gcry_md_write( hd, &sign.timestamp, 4 );
1037
1038     gcry_md_final( hd );
1039
1040     uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );
1041
1042     if( p_hash[0] != sign.hash_verification[0] ||
1043         p_hash[1] != sign.hash_verification[1] )
1044     {
1045         msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
1046         free( p_hash );
1047         goto error;
1048     }
1049
1050     if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
1051             sign.r, sign.s, &p_update->p_pkey->key, p_hash ) != VLC_SUCCESS )
1052     {
1053         msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
1054         free( p_hash );
1055         goto error;
1056     }
1057     else
1058     {
1059         msg_Info( p_update->p_libvlc, "Status file authenticated" );
1060         free( p_hash );
1061         return VLC_TRUE;
1062     }
1063
1064 error:
1065     if( p_stream )
1066         stream_Delete( p_stream );
1067     free( psz_version_line );
1068     return VLC_FALSE;
1069 }
1070
1071
1072 /**
1073  * Struct to launch the check in an other thread
1074  */
1075 typedef struct
1076 {
1077     VLC_COMMON_MEMBERS
1078     update_t *p_update;
1079     void (*pf_callback)( void *, vlc_bool_t );
1080     void *p_data;
1081 } update_check_thread_t;
1082
1083 void update_CheckReal( update_check_thread_t *p_uct );
1084
1085 /**
1086  * Check for updates
1087  *
1088  * \param p_update pointer to update struct
1089  * \param pf_callback pointer to a function to call when the update_check is finished
1090  * \param p_data pointer to some datas to give to the callback
1091  * \returns nothing
1092  */
1093 void update_Check( update_t *p_update, void (*pf_callback)( void*, vlc_bool_t ), void *p_data )
1094 {
1095     assert( p_update );
1096
1097     update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
1098                                             sizeof( update_check_thread_t ) );
1099     p_uct->p_update = p_update;
1100     p_uct->pf_callback = pf_callback;
1101     p_uct->p_data = p_data;
1102
1103     vlc_thread_create( p_uct, "check for update", update_CheckReal,
1104                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1105 }
1106
1107 void update_CheckReal( update_check_thread_t *p_uct )
1108 {
1109     vlc_bool_t b_ret;
1110     vlc_mutex_lock( &p_uct->p_update->lock );
1111
1112     EmptyRelease( p_uct->p_update );
1113     b_ret = GetUpdateFile( p_uct->p_update );
1114     vlc_mutex_unlock( &p_uct->p_update->lock );
1115
1116     if( p_uct->pf_callback )
1117         (p_uct->pf_callback)( p_uct->p_data, b_ret );
1118
1119     vlc_object_destroy( p_uct );
1120 }
1121
1122 /**
1123  * Compare two release numbers
1124  *
1125  * \param p1 first release
1126  * \param p2 second release
1127  * \return UpdateReleaseStatus(Older|Equal|Newer)
1128  */
1129 static int CompareReleases( const struct update_release_t *p1,
1130                             const struct update_release_t *p2 )
1131 {
1132     int32_t d;
1133     d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
1134       - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
1135       + ( p1->extra ) - ( p2->extra );
1136
1137     if( d < 0 )
1138         return UpdateReleaseStatusOlder;
1139     else if( d == 0 )
1140         return UpdateReleaseStatusEqual;
1141     else
1142         return UpdateReleaseStatusNewer;
1143 }
1144
1145 /**
1146  * Compare a given release's version number to the current VLC's one
1147  *
1148  * \param p_update structure
1149  * \return UpdateReleaseStatus(Older|Equal|Newer)
1150  */
1151 int update_CompareReleaseToCurrent( update_t *p_update )
1152 {
1153     assert( p_update );
1154
1155     struct update_release_t c;
1156
1157     /* get the current version number */
1158     c.i_major = *PACKAGE_VERSION_MAJOR - '0';
1159     c.i_minor = *PACKAGE_VERSION_MINOR - '0';
1160     c.i_revision = *PACKAGE_VERSION_REVISION - '0';
1161     c.extra = *PACKAGE_VERSION_EXTRA;
1162
1163     return CompareReleases( &p_update->release, &c );
1164 }
1165
1166 /**
1167  * Convert a long int size in bytes to a string
1168  *
1169  * \param l_size the size in bytes
1170  * \return the size as a string
1171  */
1172 static char *size_str( long int l_size )
1173 {
1174     char *psz_tmp = NULL;
1175     if( l_size >> 30 )
1176         asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
1177     else if( l_size >> 20 )
1178         asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1179     else if( l_size >> 10 )
1180         asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1181     else
1182         asprintf( &psz_tmp, "%ld B", l_size );
1183     return psz_tmp;
1184 }
1185
1186
1187 /*
1188  * Struct to launch the download in a thread
1189  */
1190 typedef struct
1191 {
1192     VLC_COMMON_MEMBERS
1193     update_t *p_update;
1194     char *psz_destdir;
1195 } update_download_thread_t;
1196
1197 void update_DownloadReal( update_download_thread_t *p_udt );
1198
1199 /**
1200  * Download the file given in the update_t
1201  *
1202  * \param p_update structure
1203  * \param dir to store the download file
1204  * \return nothing
1205  */
1206 void update_Download( update_t *p_update, char *psz_destdir )
1207 {
1208     assert( p_update );
1209
1210     update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1211                                                       sizeof( update_download_thread_t ) );
1212
1213     p_udt->p_update = p_update;
1214     p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1215
1216     vlc_thread_create( p_udt, "download update", update_DownloadReal,
1217                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1218 }
1219
1220 void update_DownloadReal( update_download_thread_t *p_udt )
1221 {
1222     int i_progress = 0;
1223     long int l_size;
1224     long int l_downloaded = 0;
1225     float f_progress;
1226     char *psz_status = NULL;
1227     char *psz_downloaded = NULL;
1228     char *psz_size = NULL;
1229     char *psz_destfile = NULL;
1230     char *psz_tmpdestfile = NULL;
1231
1232     FILE *p_file = NULL;
1233     stream_t *p_stream = NULL;
1234     void* p_buffer = NULL;
1235     int i_read;
1236
1237     update_t *p_update = p_udt->p_update;
1238     char *psz_destdir = p_udt->psz_destdir;
1239
1240     /* Open the stream */
1241     p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1242     if( !p_stream )
1243     {
1244         msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1245         goto end;
1246     }
1247
1248     /* Get the stream size */
1249     l_size = stream_Size( p_stream );
1250
1251     /* Get the file name and open it*/
1252     psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1253     if( !psz_tmpdestfile )
1254     {
1255         msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1256         goto end;
1257     }
1258     psz_tmpdestfile++;
1259     if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1260         goto end;
1261
1262     p_file = utf8_fopen( psz_destfile, "w" );
1263     if( !p_file )
1264     {
1265         msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1266         goto end;
1267     }
1268
1269     /* Create a buffer and fill it with the downloaded file */
1270     p_buffer = (void *)malloc( 1 << 10 );
1271     if( !p_buffer )
1272         goto end;
1273
1274     psz_size = size_str( l_size );
1275     if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",  p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1276     {
1277         i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1278         free( psz_status );
1279     }
1280
1281     while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1282                                    !intf_ProgressIsCancelled( p_udt, i_progress ) )
1283     {
1284         fwrite( p_buffer, i_read, 1, p_file );
1285
1286         l_downloaded += i_read;
1287         psz_downloaded = size_str( l_downloaded );
1288         f_progress = 100.0*(float)l_downloaded/(float)l_size;
1289
1290         if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done", p_update->release.psz_url,
1291                       psz_downloaded, psz_size, f_progress ) != -1 )
1292         {
1293             intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1294             free( psz_status );
1295         }
1296         free( psz_downloaded );
1297     }
1298
1299     /* Finish the progress bar or delete the file if the user had canceled */
1300     fclose( p_file );
1301     p_file = NULL;
1302
1303     if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
1304     {
1305         if( asprintf( &psz_status, "%s\nDone %s (100.0%%)", p_update->release.psz_url, psz_size ) != -1 )
1306         {
1307             intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1308             free( psz_status );
1309         }
1310     }
1311     else
1312     {
1313         utf8_unlink( psz_destfile );
1314         goto end;
1315     }
1316
1317     signature_packet_v3_t sign;
1318     if( download_signature( VLC_OBJECT( p_udt ), &sign,
1319             p_update->release.psz_url ) != VLC_SUCCESS )
1320     {
1321         utf8_unlink( psz_destfile );
1322
1323         intf_UserFatal( p_udt, VLC_TRUE, _("File can not be verified"),
1324             _("It was not possible to downlaod a cryptographic signature for "
1325               "downloaded file \"%s\", and so VLC deleted it."),
1326             psz_destfile );
1327         msg_Err( p_udt, "Couldn't download signature of downloaded file" );
1328         goto end;
1329     }
1330
1331     if( sign.type != BINARY_SIGNATURE )
1332     {
1333         utf8_unlink( psz_destfile );
1334         msg_Err( p_udt, "Invalid signature type" );
1335         intf_UserFatal( p_udt, VLC_TRUE, _("Invalid signature"),
1336             _("The cryptographic signature for downloaded file \"%s\" was "
1337               "invalid and couldn't be used to securely verify it, and so "
1338               "VLC deleted it."),
1339             psz_destfile );
1340         goto end;
1341     }
1342
1343     uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
1344     if( !p_hash )
1345     {
1346         msg_Err( p_udt, "Unable to hash %s", psz_destfile );
1347         utf8_unlink( psz_destfile );
1348         intf_UserFatal( p_udt, VLC_TRUE, _("File not verifiable"),
1349             _("It was not possible to securely verify downloaded file \"%s\", "
1350               "and so VLC deleted it."),
1351             psz_destfile );
1352
1353         goto end;
1354     }
1355
1356     if( p_hash[0] != sign.hash_verification[0] ||
1357         p_hash[1] != sign.hash_verification[1] )
1358     {
1359         utf8_unlink( psz_destfile );
1360         intf_UserFatal( p_udt, VLC_TRUE, _("File corrupted"),
1361             _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
1362              psz_destfile );
1363         msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
1364         free( p_hash );
1365         goto end;
1366     }
1367
1368     if( verify_signature( VLC_OBJECT(p_udt), sign.r, sign.s,
1369                 &p_update->p_pkey->key, p_hash ) != VLC_SUCCESS )
1370     {
1371         utf8_unlink( psz_destfile );
1372         intf_UserFatal( p_udt, VLC_TRUE, _("File corrupted"),
1373             _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
1374              psz_destfile );
1375         msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
1376         free( p_hash );
1377         goto end;
1378     }
1379
1380     msg_Info( p_udt, "%s authenticated", psz_destfile );
1381     free( p_hash );
1382
1383 end:
1384     if( p_stream )
1385         stream_Delete( p_stream );
1386     if( p_file )
1387         fclose( p_file );
1388     free( psz_destdir );
1389     free( psz_destfile );
1390     free( p_buffer );
1391     free( psz_size );
1392
1393     vlc_object_destroy( p_udt );
1394 }
1395
1396 #endif