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