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