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