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