]> git.sesse.net Git - vlc/blob - src/misc/update.c
Wrap and typo
[vlc] / src / misc / update.c
1 /*****************************************************************************
2  * update.c: VLC update checking and downloading
3  *****************************************************************************
4  * Copyright © 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *          Rémi Duraffort <ivoire at via.ecp.fr>
9             Rafaël Carré <funman@videolanorg>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either release 2 of the License, or
14  * (at your option) any later release.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /**
27  *   \file
28  *   This file contains functions related to VLC update management
29  */
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc/vlc.h>
40
41 #ifdef UPDATE_CHECK
42
43 #include <assert.h>
44
45 #include <vlc_update.h>
46 #include <vlc_pgpkey.h>
47 #include <vlc_stream.h>
48 #include <vlc_interface.h>
49
50
51 /*****************************************************************************
52  * Misc defines
53  *****************************************************************************/
54
55 /*
56  * Here is the format of these "status files" :
57  * First line is the last version: "X.Y.Ze" where:
58  *      * X is the major number
59  *      * Y is the minor number
60  *      * Z is the revision number
61  *      * e is an OPTIONAL extra letter
62  *      * AKA "0.8.6d" or "0.9.0"
63  * Second line is an url of the binary for this last version
64  * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
65  */
66
67 #if defined( UNDER_CE )
68 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
69 #elif defined( WIN32 )
70 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
71 #elif defined( __APPLE__ )
72 #   if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
73 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
74 #   else
75 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
76 #   endif
77 #elif defined( SYS_BEOS )
78 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
79 #else
80 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
81 #endif
82
83
84 /*****************************************************************************
85  * Local Prototypes
86  *****************************************************************************/
87 static void EmptyRelease( update_t *p_update );
88 static vlc_bool_t GetUpdateFile( update_t *p_update );
89 static int CompareReleases( const struct update_release_t *p1,
90                             const struct update_release_t *p2 );
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 > 65 )
418     {
419         msg_Dbg( p_this, "Signature is too big: %d bytes", i_bytes );
420         return VLC_EGENERIC;
421     }
422     else
423     {
424         int i_r_len = mpi_len( p_sig->r );
425         if( i_r_len > 20 )
426         {
427             msg_Dbg( p_this, "Signature invalid" );
428             return VLC_EGENERIC;
429         }
430         else if( i_r_len < 20 )
431             /* move s to the right place if r is less than 20 bytes */
432             memmove( p_sig->s, p_sig->r + 2 + i_r_len, 20 + 2 );
433
434         return VLC_SUCCESS;
435     }
436 }
437
438 /*
439  * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
440  */
441 static int verify_signature( uint8_t *p_r, uint8_t *p_s,
442         public_key_packet_t *p_key, uint8_t *p_hash )
443 {
444     /* the data to be verified (a SHA-1 hash) */
445     const char *hash_sexp_s = "(data(flags raw)(value %m))";
446     /* the public key */
447     const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
448     /* the signature */
449     const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
450
451     size_t erroff;
452     gcry_mpi_t p, q, g, y, r, s, hash;
453     p = q = g = y = r = s = hash = NULL;
454     gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
455     key_sexp = hash_sexp = sig_sexp = NULL;
456
457     int i_p_len = mpi_len( p_key->p );
458     int i_q_len = mpi_len( p_key->q );
459     int i_g_len = mpi_len( p_key->g );
460     int i_y_len = mpi_len( p_key->y );
461     if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, i_p_len, NULL ) ||
462         gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, i_q_len, NULL ) ||
463         gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, i_g_len, NULL ) ||
464         gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, i_y_len, NULL ) ||
465         gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
466         goto problem;
467
468     int i_r_len = mpi_len( p_r );
469     int i_s_len = mpi_len( p_s );
470     if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, i_r_len, NULL ) ||
471         gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, i_s_len, NULL ) ||
472         gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
473         goto problem;
474
475     int i_hash_len = 20;
476     if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
477         gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
478         goto problem;
479
480     if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
481         goto problem;
482
483     return VLC_SUCCESS;
484
485 problem:
486     if( p ) gcry_mpi_release( p );
487     if( q ) gcry_mpi_release( q );
488     if( g ) gcry_mpi_release( g );
489     if( y ) gcry_mpi_release( y );
490     if( r ) gcry_mpi_release( r );
491     if( s ) gcry_mpi_release( s );
492     if( hash ) gcry_mpi_release( hash );
493     if( key_sexp ) gcry_sexp_release( key_sexp );
494     if( sig_sexp ) gcry_sexp_release( sig_sexp );
495     if( hash_sexp ) gcry_sexp_release( hash_sexp );
496     return VLC_EGENERIC;
497 }
498
499 /*
500  * Return the long id (8 bytes) of the public key used to generate a signature
501  */
502 static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
503 {
504     uint8_t *p = p_sig->unhashed_data;
505     uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );
506
507     while( p < max_pos )
508     {
509         int i_subpacket_len = *p < 192 ? *p++ :
510                 *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
511                 ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;
512
513         if( p >= max_pos - 1 )
514             return NULL;
515
516         if( *p == ISSUER_SUBPACKET )
517             return p+1;
518         else
519             p += i_subpacket_len;
520     }
521     return NULL;
522 }
523
524 /*
525  * fill a public_key_t with public key data, including:
526  *   * public key packet
527  *   * signature packet issued by key which long id is p_sig_issuer
528  *   * user id packet
529  */
530 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 )
531 {
532     uint8_t *pos = (uint8_t*) p_key_data;
533     uint8_t *max_pos = pos + i_key_len;
534
535     int i_status = 0;
536 #define PUBLIC_KEY_FOUND    0x01
537 #define USER_ID_FOUND       0x02
538 #define SIGNATURE_FOUND     0X04
539
540     uint8_t *p_key_unarmored = NULL;
541
542     signature_packet_v4_t sig;
543
544     p_key->psz_username = NULL;
545     p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;
546
547     if( !( *pos & 0x80 ) )
548     {   /* first byte is ASCII, unarmoring */
549         p_key_unarmored = (uint8_t*)malloc( i_key_len );
550         if( !p_key_unarmored )
551             return VLC_ENOMEM;
552         int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
553                                  p_key_unarmored, i_key_len );
554
555         if( i_len == 0 )
556             goto error;
557
558         pos = p_key_unarmored;
559         max_pos = pos + i_len;
560     }
561
562     while( pos < max_pos )
563     {
564         if( !(*pos & 0x80) || *pos & 0x40 )
565             goto error;
566
567         int i_type = packet_type( *pos );
568
569         int i_header_len = packet_header_len( *pos++ );
570         if( pos + i_header_len > max_pos )
571             goto error;
572
573         int i_packet_len = scalar_number( pos, i_header_len );
574         pos += i_header_len;
575
576         if( pos + i_packet_len > max_pos )
577             goto error;
578
579         switch( i_type )
580         {
581             uint8_t *p_issuer;
582
583             case PUBLIC_KEY_PACKET:
584                 i_status |= PUBLIC_KEY_FOUND;
585                 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
586                     goto error;
587                 break;
588
589             case SIGNATURE_PACKET:
590                 if( !p_sig_issuer || i_status & SIGNATURE_FOUND ||
591                     parse_signature_v4_packet( &sig, pos, i_packet_len ) != VLC_SUCCESS )
592                     break;
593                 p_issuer = get_issuer_from_signature_v4( &sig );
594                 if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
595                 {
596                     memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
597                     i_status |= SIGNATURE_FOUND;
598                 }
599                 else
600                 {
601                     free( sig.hashed_data );
602                     free( sig.unhashed_data );
603                 }
604                 break;
605
606             case USER_ID_PACKET:
607                 if( p_key->psz_username ) /* save only the first User ID */
608                     break;
609                 i_status |= USER_ID_FOUND;
610                 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
611                 if( !p_key->psz_username )
612                     goto error;
613
614                 memcpy( p_key->psz_username, pos, i_packet_len );
615                 p_key->psz_username[i_packet_len] = '\0';
616                 break;
617
618             default:
619                 break;
620         }
621         pos += i_packet_len;
622     }
623     free( p_key_unarmored );
624
625     if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
626         return VLC_EGENERIC;
627
628     if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
629         return VLC_EGENERIC;
630
631     return VLC_SUCCESS;
632
633 error:
634     free( p_key->sig.hashed_data );
635     free( p_key->sig.unhashed_data );
636     free( p_key->psz_username );
637     free( p_key_unarmored );
638     return VLC_EGENERIC;
639 }
640
641 /*
642  * return a sha1 hash of a file
643  */
644 static uint8_t *hash_sha1_from_file( const char *psz_file,
645                             signature_packet_v3_t *p_sig )
646 {
647     FILE *f = utf8_fopen( psz_file, "r" );
648     if( !f )
649         return NULL;
650
651     uint8_t buffer[4096];
652
653     gcry_md_hd_t hd;
654     if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
655     {
656         fclose( f );
657         return NULL;
658     }
659
660     size_t i_read;
661     while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
662         gcry_md_write( hd, buffer, i_read );
663
664     gcry_md_putc( hd, p_sig->type );
665     gcry_md_write( hd, &p_sig->timestamp, 4 );
666
667     fclose( f );
668     gcry_md_final( hd );
669
670     uint8_t *p_hash = (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1);
671     p_hash = strdup( p_hash );
672     gcry_md_close( hd );
673     return p_hash;
674 }
675
676 /*
677  * download a public key (the last one) from videolan server, and parse it
678  */
679 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
680 {
681     char *psz_url;
682     if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
683                     p_longid[0], p_longid[1], p_longid[2], p_longid[3],
684                     p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
685         return NULL;
686
687     stream_t *p_stream = stream_UrlNew( p_this, psz_url );
688     free( psz_url );
689     if( !p_stream )
690         return NULL;
691
692     int64_t i_size = stream_Size( p_stream );
693     if( i_size < 0 )
694     {
695         stream_Delete( p_stream );
696         return NULL;
697     }
698
699     uint8_t *p_buf = (uint8_t*)malloc( i_size );
700     if( !p_buf )
701     {
702         stream_Delete( p_stream );
703         return NULL;
704     }
705
706     int i_read = stream_Read( p_stream, p_buf, (int)i_size );
707     stream_Delete( p_stream );
708
709     if( i_read != (int)i_size )
710     {
711         msg_Dbg( p_this, "Couldn't read full GPG key" );
712         free( p_buf );
713         return NULL;
714     }
715
716     public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
717     if( !p_pkey )
718     {
719         free( p_buf );
720         return NULL;
721     }
722
723     int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
724     free( p_buf );
725
726     if( i_error != VLC_SUCCESS )
727     {
728         msg_Dbg( p_this, "Couldn't parse GPG key" );
729         free( p_pkey );
730         return NULL;
731     }
732
733     return p_pkey;
734 }
735
736 /*
737  * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
738  * Note that we need the signature to compute the hash
739  */
740 static uint8_t *key_sign_hash( public_key_t *p_pkey )
741 {
742     gcry_error_t error = 0;
743     gcry_md_hd_t hd;
744
745     error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
746     if( error )
747         return NULL;
748
749     gcry_md_putc( hd, 0x99 );
750
751     gcry_md_putc( hd, (418 >> 8) & 0xff );
752     gcry_md_putc( hd, 418 & 0xff );
753
754     gcry_md_write( hd, (uint8_t*)&p_pkey->key, 6 ); /* version,timestamp,algo */
755
756     int i_p_len = mpi_len( p_pkey->key.p );
757     gcry_md_write( hd, (uint8_t*)&p_pkey->key.p, 2 );
758     gcry_md_write( hd, (uint8_t*)&p_pkey->key.p + 2, i_p_len );
759
760     int i_g_len = mpi_len( p_pkey->key.g );
761     gcry_md_write( hd, (uint8_t*)&p_pkey->key.g, 2 );
762     gcry_md_write( hd, (uint8_t*)&p_pkey->key.g + 2, i_g_len );
763
764     int i_q_len = mpi_len( p_pkey->key.q );
765     gcry_md_write( hd, (uint8_t*)&p_pkey->key.q, 2 );
766     gcry_md_write( hd, (uint8_t*)&p_pkey->key.q + 2, i_q_len );
767
768     int i_y_len = mpi_len( p_pkey->key.y );
769     gcry_md_write( hd, (uint8_t*)&p_pkey->key.y, 2 );
770     gcry_md_write( hd, (uint8_t*)&p_pkey->key.y + 2, i_y_len );
771
772     gcry_md_putc( hd, 0xb4 );
773
774     int i_len = strlen((char*)p_pkey->psz_username);
775
776     gcry_md_putc( hd, (i_len << 24) & 0xff );
777     gcry_md_putc( hd, (i_len << 16) & 0xff );
778     gcry_md_putc( hd, (i_len << 8) & 0xff );
779     gcry_md_putc( hd, (i_len) & 0xff );
780
781     gcry_md_write( hd, p_pkey->psz_username, i_len );
782
783     size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );
784
785     gcry_md_putc( hd, p_pkey->sig.version );
786     gcry_md_putc( hd, p_pkey->sig.type );
787     gcry_md_putc( hd, p_pkey->sig.public_key_algo );
788     gcry_md_putc( hd, p_pkey->sig.digest_algo );
789     gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
790     gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );
791
792     gcry_md_putc( hd, 0x04 );
793     gcry_md_putc( hd, 0xff );
794
795     i_hashed_data_len += 6; /* hashed data + 6 bytes header */
796
797     gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
798     gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
799     gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
800     gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
801
802     gcry_md_final( hd );
803
804     uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
805
806     if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
807         p_hash[1] != p_pkey->sig.hash_verification[1] )
808     {
809         gcry_md_close( hd );
810         return NULL;
811     }
812
813     p_hash = strdup( p_hash );
814     gcry_md_close( hd );
815     return p_hash;
816 }
817
818
819 /*****************************************************************************
820  * Update_t functions
821  *****************************************************************************/
822
823 /**
824  * Create a new update VLC struct
825  *
826  * \param p_this the calling vlc_object
827  * \return pointer to new update_t or NULL
828  */
829 update_t *__update_New( vlc_object_t *p_this )
830 {
831     update_t *p_update;
832     assert( p_this );
833
834     p_update = (update_t *)malloc( sizeof( update_t ) );
835     if( !p_update ) return NULL;
836
837     vlc_mutex_init( p_this, &p_update->lock );
838
839     p_update->p_libvlc = p_this->p_libvlc;
840
841     p_update->release.psz_url = NULL;
842     p_update->release.psz_desc = NULL;
843
844     p_update->p_pkey = NULL;
845
846     return p_update;
847 }
848
849 /**
850  * Delete an update_t struct
851  *
852  * \param p_update update_t* pointer
853  * \return nothing
854  */
855 void update_Delete( update_t *p_update )
856 {
857     assert( p_update );
858
859     vlc_mutex_destroy( &p_update->lock );
860
861     free( p_update->release.psz_url );
862     free( p_update->release.psz_desc );
863     free( p_update->p_pkey );
864
865     free( p_update );
866 }
867
868 /**
869  * Empty the release struct
870  *
871  * \param p_update update_t* pointer
872  * \return nothing
873  */
874 static void EmptyRelease( update_t *p_update )
875 {
876     p_update->release.i_major = 0;
877     p_update->release.i_minor = 0;
878     p_update->release.i_revision = 0;
879
880     FREENULL( p_update->release.psz_url );
881     FREENULL( p_update->release.psz_desc );
882 }
883
884 /**
885  * Get the update file and parse it
886  * p_update has to be locked when calling this function
887  *
888  * \param p_update pointer to update struct
889  * \return VLC_TRUE if the update is valid and authenticated
890  */
891 static vlc_bool_t GetUpdateFile( update_t *p_update )
892 {
893     stream_t *p_stream = NULL;
894     int i_major = 0;
895     int i_minor = 0;
896     int i_revision = 0;
897     unsigned char extra;
898     char *psz_line = NULL;
899     char *psz_version_line = NULL;
900
901     p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
902     if( !p_stream )
903     {
904         msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
905                  UPDATE_VLC_STATUS_URL );
906         goto error;
907     }
908
909     /* Try to read three lines */
910     if( !( psz_line = stream_ReadLine( p_stream ) ) )
911     {
912         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
913                  UPDATE_VLC_STATUS_URL );
914         goto error;
915     }
916
917     psz_version_line = psz_line;
918     /* first line : version number */
919     p_update->release.extra = 0;
920     switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
921     {
922         case 4:
923             p_update->release.extra = extra;
924         case 3:
925             p_update->release.i_major = i_major;
926             p_update->release.i_minor = i_minor;
927             p_update->release.i_revision = i_revision;
928             break;
929         default:
930             msg_Err( p_update->p_libvlc, "Update version false formated" );
931             goto error;
932     }
933
934     /* Second line : URL */
935     if( !( psz_line = stream_ReadLine( p_stream ) ) )
936     {
937         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
938                  UPDATE_VLC_STATUS_URL );
939         goto error;
940     }
941     p_update->release.psz_url = psz_line;
942
943
944     /* Third line : description */
945     if( !( psz_line = stream_ReadLine( p_stream ) ) )
946     {
947         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
948                  UPDATE_VLC_STATUS_URL );
949         goto error;
950     }
951     p_update->release.psz_desc = psz_line;
952
953     stream_Delete( p_stream );
954     p_stream = NULL;
955
956     /* Now that we know the status is valid, we must download its signature
957      * to authenticate it */
958     signature_packet_v3_t sign;
959     if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
960             UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
961     {
962         msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
963         goto error;
964     }
965
966     if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
967     {
968         msg_Err( p_update->p_libvlc, "Invalid signature type" );
969         goto error;
970     }
971
972     p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
973     if( !p_update->p_pkey )
974         goto error;
975
976     if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
977                         p_update->p_pkey, NULL ) != VLC_SUCCESS )
978     {
979         msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
980         FREENULL( p_update->p_pkey );
981         goto error;
982     }
983
984     if( memcmp( sign.issuer_longid, videolan_public_key_longid , 8 ) != 0 )
985     {
986         msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
987         public_key_t *p_new_pkey = download_key(
988                 VLC_OBJECT(p_update->p_libvlc),
989                 sign.issuer_longid, videolan_public_key_longid );
990         if( !p_new_pkey )
991         {
992             msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
993             FREENULL( p_update->p_pkey );
994             goto error;
995         }
996
997         uint8_t *p_hash = key_sign_hash( p_new_pkey );
998         if( !p_hash )
999         {
1000             msg_Err( p_update->p_libvlc, "Failed to hash signature" );
1001             free( p_new_pkey );
1002             FREENULL( p_update->p_pkey );
1003             goto error;
1004         }
1005
1006         if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
1007                     &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
1008         {
1009             free( p_hash );
1010             msg_Info( p_update->p_libvlc, "Key authenticated" );
1011             free( p_update->p_pkey );
1012             p_update->p_pkey = p_new_pkey;
1013         }
1014         else
1015         {
1016             free( p_hash );
1017             msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
1018             goto error;
1019         }
1020     }
1021
1022     gcry_md_hd_t hd;
1023     if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
1024         goto error;
1025
1026     gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
1027     FREENULL( psz_version_line );
1028     if( sign.type == TEXT_SIGNATURE )
1029         gcry_md_putc( hd, '\r' );
1030     gcry_md_putc( hd, '\n' );
1031     gcry_md_write( hd, p_update->release.psz_url,
1032                         strlen( p_update->release.psz_url ) );
1033     if( sign.type == TEXT_SIGNATURE )
1034         gcry_md_putc( hd, '\r' );
1035     gcry_md_putc( hd, '\n' );
1036     gcry_md_write( hd, p_update->release.psz_desc,
1037                         strlen( p_update->release.psz_desc ) );
1038     if( sign.type == TEXT_SIGNATURE )
1039         gcry_md_putc( hd, '\r' );
1040     gcry_md_putc( hd, '\n' );
1041
1042     gcry_md_putc( hd, sign.type );
1043     gcry_md_write( hd, &sign.timestamp, 4 );
1044
1045     gcry_md_final( hd );
1046
1047     uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );
1048
1049     if( p_hash[0] != sign.hash_verification[0] ||
1050         p_hash[1] != sign.hash_verification[1] )
1051     {
1052         msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
1053         goto error;
1054     }
1055
1056     if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
1057             != VLC_SUCCESS )
1058     {
1059         msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
1060         goto error;
1061     }
1062     else
1063     {
1064         msg_Info( p_update->p_libvlc, "Status file authenticated" );
1065         gcry_md_close( hd );
1066         return VLC_TRUE;
1067     }
1068
1069 error:
1070     gcry_md_close( hd );
1071     if( p_stream )
1072         stream_Delete( p_stream );
1073     free( psz_version_line );
1074     return VLC_FALSE;
1075 }
1076
1077
1078 /**
1079  * Struct to launch the check in an other thread
1080  */
1081 typedef struct
1082 {
1083     VLC_COMMON_MEMBERS
1084     update_t *p_update;
1085     void (*pf_callback)( void *, vlc_bool_t );
1086     void *p_data;
1087 } update_check_thread_t;
1088
1089 void update_CheckReal( update_check_thread_t *p_uct );
1090
1091 /**
1092  * Check for updates
1093  *
1094  * \param p_update pointer to update struct
1095  * \param pf_callback pointer to a function to call when the update_check is finished
1096  * \param p_data pointer to some datas to give to the callback
1097  * \returns nothing
1098  */
1099 void update_Check( update_t *p_update, void (*pf_callback)( void*, vlc_bool_t ), void *p_data )
1100 {
1101     assert( p_update );
1102
1103     update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
1104                                             sizeof( update_check_thread_t ) );
1105     if( !p_uct )
1106     {
1107         msg_Err( p_update->p_libvlc, "out of memory" );
1108         return;
1109     }
1110
1111     p_uct->p_update = p_update;
1112     p_uct->pf_callback = pf_callback;
1113     p_uct->p_data = p_data;
1114
1115     vlc_thread_create( p_uct, "check for update", update_CheckReal,
1116                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1117 }
1118
1119 void update_CheckReal( update_check_thread_t *p_uct )
1120 {
1121     vlc_bool_t b_ret;
1122     vlc_mutex_lock( &p_uct->p_update->lock );
1123
1124     EmptyRelease( p_uct->p_update );
1125     b_ret = GetUpdateFile( p_uct->p_update );
1126     vlc_mutex_unlock( &p_uct->p_update->lock );
1127
1128     if( p_uct->pf_callback )
1129         (p_uct->pf_callback)( p_uct->p_data, b_ret );
1130
1131     vlc_object_destroy( p_uct );
1132 }
1133
1134 /**
1135  * Compare two release numbers
1136  *
1137  * \param p1 first release
1138  * \param p2 second release
1139  * \return UpdateReleaseStatus(Older|Equal|Newer)
1140  */
1141 static int CompareReleases( const struct update_release_t *p1,
1142                             const struct update_release_t *p2 )
1143 {
1144     int32_t d;
1145     d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
1146       - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
1147       + ( p1->extra ) - ( p2->extra );
1148
1149     if( d < 0 )
1150         return UpdateReleaseStatusOlder;
1151     else if( d == 0 )
1152         return UpdateReleaseStatusEqual;
1153     else
1154         return UpdateReleaseStatusNewer;
1155 }
1156
1157 /**
1158  * Compare a given release's version number to the current VLC's one
1159  *
1160  * \param p_update structure
1161  * \return UpdateReleaseStatus(Older|Equal|Newer)
1162  */
1163 int update_CompareReleaseToCurrent( update_t *p_update )
1164 {
1165     assert( p_update );
1166
1167     struct update_release_t c;
1168
1169     /* get the current version number */
1170     c.i_major = *PACKAGE_VERSION_MAJOR - '0';
1171     c.i_minor = *PACKAGE_VERSION_MINOR - '0';
1172     c.i_revision = *PACKAGE_VERSION_REVISION - '0';
1173     c.extra = *PACKAGE_VERSION_EXTRA;
1174
1175     return CompareReleases( &p_update->release, &c );
1176 }
1177
1178 /**
1179  * Convert a long int size in bytes to a string
1180  *
1181  * \param l_size the size in bytes
1182  * \return the size as a string
1183  */
1184 static char *size_str( long int l_size )
1185 {
1186     char *psz_tmp = NULL;
1187     int i_retval = 0;
1188     if( l_size >> 30 )
1189         i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
1190     else if( l_size >> 20 )
1191         i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1192     else if( l_size >> 10 )
1193         i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1194     else
1195         i_retval = asprintf( &psz_tmp, "%ld B", l_size );
1196
1197     return i_retval == -1 ? NULL : psz_tmp;
1198 }
1199
1200
1201 /**
1202  * Struct to launch the download in a thread
1203  */
1204 typedef struct
1205 {
1206     VLC_COMMON_MEMBERS
1207     update_t *p_update;
1208     char *psz_destdir;
1209 } update_download_thread_t;
1210
1211 void update_DownloadReal( update_download_thread_t *p_udt );
1212
1213 /**
1214  * Download the file given in the update_t
1215  *
1216  * \param p_update structure
1217  * \param dir to store the download file
1218  * \return nothing
1219  */
1220 void update_Download( update_t *p_update, char *psz_destdir )
1221 {
1222     assert( p_update );
1223
1224     update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1225                                         sizeof( update_download_thread_t ) );
1226     if( !p_udt )
1227     {
1228         msg_Err( p_update->p_libvlc, "out of memory" );
1229         return;
1230     }
1231
1232     p_udt->p_update = p_update;
1233     p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1234
1235     vlc_thread_create( p_udt, "download update", update_DownloadReal,
1236                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1237 }
1238
1239 void update_DownloadReal( update_download_thread_t *p_udt )
1240 {
1241     int i_progress = 0;
1242     long int l_size;
1243     long int l_downloaded = 0;
1244     float f_progress;
1245     char *psz_status = NULL;
1246     char *psz_downloaded = NULL;
1247     char *psz_size = NULL;
1248     char *psz_destfile = NULL;
1249     char *psz_tmpdestfile = NULL;
1250
1251     FILE *p_file = NULL;
1252     stream_t *p_stream = NULL;
1253     void* p_buffer = NULL;
1254     int i_read;
1255
1256     update_t *p_update = p_udt->p_update;
1257     char *psz_destdir = p_udt->psz_destdir;
1258
1259     /* Open the stream */
1260     p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1261     if( !p_stream )
1262     {
1263         msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1264         goto end;
1265     }
1266
1267     /* Get the stream size */
1268     l_size = stream_Size( p_stream );
1269
1270     /* Get the file name and open it*/
1271     psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1272     if( !psz_tmpdestfile )
1273     {
1274         msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1275         goto end;
1276     }
1277     psz_tmpdestfile++;
1278     if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1279         goto end;
1280
1281     p_file = utf8_fopen( psz_destfile, "w" );
1282     if( !p_file )
1283     {
1284         msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1285         goto end;
1286     }
1287
1288     /* Create a buffer and fill it with the downloaded file */
1289     p_buffer = (void *)malloc( 1 << 10 );
1290     if( !p_buffer )
1291         goto end;
1292
1293     psz_size = size_str( l_size );
1294     if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",
1295         p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1296     {
1297         i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1298         free( psz_status );
1299     }
1300
1301     while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1302                                    !intf_ProgressIsCancelled( p_udt, i_progress ) )
1303     {
1304         if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
1305         {
1306             msg_Err( p_udt, "Failed to write into %s", psz_destfile );
1307             break;
1308         }
1309
1310         l_downloaded += i_read;
1311         psz_downloaded = size_str( l_downloaded );
1312         f_progress = 100.0*(float)l_downloaded/(float)l_size;
1313
1314         if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done",
1315                       p_update->release.psz_url, psz_downloaded, psz_size,
1316                       f_progress ) != -1 )
1317         {
1318             intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1319             free( psz_status );
1320         }
1321         free( psz_downloaded );
1322     }
1323
1324     /* Finish the progress bar or delete the file if the user had canceled */
1325     fclose( p_file );
1326     p_file = NULL;
1327
1328     if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
1329     {
1330         if( asprintf( &psz_status, "%s\nDone %s (100.0%%)",
1331             p_update->release.psz_url, psz_size ) != -1 )
1332         {
1333             intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1334             free( psz_status );
1335         }
1336     }
1337     else
1338     {
1339         utf8_unlink( psz_destfile );
1340         goto end;
1341     }
1342
1343     signature_packet_v3_t sign;
1344     if( download_signature( VLC_OBJECT( p_udt ), &sign,
1345             p_update->release.psz_url ) != VLC_SUCCESS )
1346     {
1347         utf8_unlink( psz_destfile );
1348
1349         intf_UserFatal( p_udt, VLC_TRUE, _("File can not be verified"),
1350             _("It was not possible to downlaod a cryptographic signature for "
1351               "downloaded file \"%s\", and so VLC deleted it."),
1352             psz_destfile );
1353         msg_Err( p_udt, "Couldn't download signature of downloaded file" );
1354         goto end;
1355     }
1356
1357     if( sign.type != BINARY_SIGNATURE )
1358     {
1359         utf8_unlink( psz_destfile );
1360         msg_Err( p_udt, "Invalid signature type" );
1361         intf_UserFatal( p_udt, VLC_TRUE, _("Invalid signature"),
1362             _("The cryptographic signature for downloaded file \"%s\" was "
1363               "invalid and couldn't be used to securely verify it, and so "
1364               "VLC deleted it."),
1365             psz_destfile );
1366         goto end;
1367     }
1368
1369     uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
1370     if( !p_hash )
1371     {
1372         msg_Err( p_udt, "Unable to hash %s", psz_destfile );
1373         utf8_unlink( psz_destfile );
1374         intf_UserFatal( p_udt, VLC_TRUE, _("File not verifiable"),
1375             _("It was not possible to securely verify downloaded file \"%s\", "
1376               "and so VLC deleted it."),
1377             psz_destfile );
1378
1379         goto end;
1380     }
1381
1382     if( p_hash[0] != sign.hash_verification[0] ||
1383         p_hash[1] != sign.hash_verification[1] )
1384     {
1385         utf8_unlink( psz_destfile );
1386         intf_UserFatal( p_udt, VLC_TRUE, _("File corrupted"),
1387             _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
1388              psz_destfile );
1389         msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
1390         free( p_hash );
1391         goto end;
1392     }
1393
1394     if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
1395             != VLC_SUCCESS )
1396     {
1397         utf8_unlink( psz_destfile );
1398         intf_UserFatal( p_udt, VLC_TRUE, _("File corrupted"),
1399             _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
1400              psz_destfile );
1401         msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
1402         free( p_hash );
1403         goto end;
1404     }
1405
1406     msg_Info( p_udt, "%s authenticated", psz_destfile );
1407     free( p_hash );
1408
1409 end:
1410     if( p_stream )
1411         stream_Delete( p_stream );
1412     if( p_file )
1413         fclose( p_file );
1414     free( psz_destdir );
1415     free( psz_destfile );
1416     free( p_buffer );
1417     free( psz_size );
1418
1419     vlc_object_destroy( p_udt );
1420 }
1421
1422 #endif