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