]> git.sesse.net Git - vlc/blob - src/misc/update.c
l10n string fixes
[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  * Remaining text is a required description of the update
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_download = NULL;
1053     p_update->p_check = NULL;
1054
1055     p_update->p_pkey = NULL;
1056     vlc_gcrypt_init();
1057
1058     return p_update;
1059 }
1060
1061 /**
1062  * Delete an update_t struct
1063  *
1064  * \param p_update update_t* pointer
1065  * \return nothing
1066  */
1067 void update_Delete( update_t *p_update )
1068 {
1069     assert( p_update );
1070
1071     if( p_update->p_check )
1072     {
1073         assert( !p_update->p_download );
1074         vlc_object_kill( p_update->p_check );
1075         vlc_thread_join( p_update->p_check );
1076     }
1077     else if( p_update->p_download )
1078     {
1079         vlc_object_kill( p_update->p_download );
1080         vlc_thread_join( p_update->p_download );
1081     }
1082
1083     vlc_mutex_destroy( &p_update->lock );
1084
1085     free( p_update->release.psz_url );
1086     free( p_update->release.psz_desc );
1087     free( p_update->p_pkey );
1088
1089     free( p_update );
1090 }
1091
1092 /**
1093  * Empty the release struct
1094  *
1095  * \param p_update update_t* pointer
1096  * \return nothing
1097  */
1098 static void EmptyRelease( update_t *p_update )
1099 {
1100     p_update->release.i_major = 0;
1101     p_update->release.i_minor = 0;
1102     p_update->release.i_revision = 0;
1103
1104     FREENULL( p_update->release.psz_url );
1105     FREENULL( p_update->release.psz_desc );
1106 }
1107
1108 /**
1109  * Get the update file and parse it
1110  * p_update has to be locked when calling this function
1111  *
1112  * \param p_update pointer to update struct
1113  * \return true if the update is valid and authenticated
1114  */
1115 static bool GetUpdateFile( update_t *p_update )
1116 {
1117     stream_t *p_stream = NULL;
1118     int i_major = 0;
1119     int i_minor = 0;
1120     int i_revision = 0;
1121     unsigned char extra;
1122     char *psz_version_line = NULL;
1123
1124     p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
1125     if( !p_stream )
1126     {
1127         msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
1128                  UPDATE_VLC_STATUS_URL );
1129         goto error;
1130     }
1131
1132     /* Start reading the status file */
1133     if( !( psz_version_line = stream_ReadLine( p_stream ) ) )
1134     {
1135         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
1136                  UPDATE_VLC_STATUS_URL );
1137         goto error;
1138     }
1139
1140     /* first line : version number */
1141     p_update->release.extra = 0;
1142     switch( sscanf( psz_version_line, "%i.%i.%i%c",
1143                     &i_major, &i_minor, &i_revision, &extra ) )
1144     {
1145         case 4:
1146             p_update->release.extra = extra;
1147         case 3:
1148             p_update->release.i_major = i_major;
1149             p_update->release.i_minor = i_minor;
1150             p_update->release.i_revision = i_revision;
1151             break;
1152         default:
1153             msg_Err( p_update->p_libvlc, "Update version false formated" );
1154             goto error;
1155     }
1156
1157     /* second line : URL */
1158     if( !( p_update->release.psz_url = stream_ReadLine( p_stream ) ) )
1159     {
1160         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
1161                  UPDATE_VLC_STATUS_URL );
1162         goto error;
1163     }
1164
1165     /* Remaining data : description */
1166     int i_read = stream_Size( p_stream ) - stream_Tell( p_stream );
1167     if( i_read <= 0 )
1168     {
1169         msg_Err( p_update->p_libvlc,
1170                 "Update file %s is corrupted: description missing",
1171                 UPDATE_VLC_STATUS_URL );
1172         goto error;
1173     }
1174
1175     p_update->release.psz_desc = (char*) malloc( i_read + 1 );
1176     if( !p_update->release.psz_desc )
1177         goto error;
1178
1179     if( stream_Read( p_stream, p_update->release.psz_desc, i_read ) != i_read )
1180     {
1181         msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
1182                 UPDATE_VLC_STATUS_URL );
1183         goto error;
1184     }
1185     p_update->release.psz_desc[i_read] = '\0';
1186
1187     stream_Delete( p_stream );
1188     p_stream = NULL;
1189
1190     /* Now that we know the status is valid, we must download its signature
1191      * to authenticate it */
1192     signature_packet_t sign;
1193     if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
1194             UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
1195     {
1196         msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
1197         goto error;
1198     }
1199
1200     if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
1201     {
1202         msg_Err( p_update->p_libvlc, "Invalid signature type" );
1203         goto error;
1204     }
1205
1206     p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
1207     if( !p_update->p_pkey )
1208         goto error;
1209
1210     if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
1211                         p_update->p_pkey, NULL ) != VLC_SUCCESS )
1212     {
1213         msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
1214         FREENULL( p_update->p_pkey );
1215         goto error;
1216     }
1217
1218     memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
1219
1220     if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
1221     {
1222         msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
1223         public_key_t *p_new_pkey = download_key(
1224                 VLC_OBJECT(p_update->p_libvlc),
1225                 sign.issuer_longid, videolan_public_key_longid );
1226         if( !p_new_pkey )
1227         {
1228             msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
1229             FREENULL( p_update->p_pkey );
1230             goto error;
1231         }
1232
1233         uint8_t *p_hash = key_sign_hash( p_new_pkey );
1234         if( !p_hash )
1235         {
1236             msg_Err( p_update->p_libvlc, "Failed to hash signature" );
1237             free( p_new_pkey );
1238             FREENULL( p_update->p_pkey );
1239             goto error;
1240         }
1241
1242         if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
1243                     &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
1244         {
1245             free( p_hash );
1246             msg_Info( p_update->p_libvlc, "Key authenticated" );
1247             free( p_update->p_pkey );
1248             p_update->p_pkey = p_new_pkey;
1249         }
1250         else
1251         {
1252             free( p_hash );
1253             msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
1254             goto error;
1255         }
1256     }
1257
1258     gcry_md_hd_t hd;
1259     if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
1260         goto error_hd;
1261
1262     gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
1263     FREENULL( psz_version_line );
1264     if( sign.type == TEXT_SIGNATURE )
1265         gcry_md_putc( hd, '\r' );
1266     gcry_md_putc( hd, '\n' );
1267     gcry_md_write( hd, p_update->release.psz_url,
1268                         strlen( p_update->release.psz_url ) );
1269     if( sign.type == TEXT_SIGNATURE )
1270         gcry_md_putc( hd, '\r' );
1271     gcry_md_putc( hd, '\n' );
1272
1273     char *psz_desc = p_update->release.psz_desc;
1274     while( *psz_desc )
1275     {
1276         size_t i_len = strcspn( psz_desc, "\r\n" );
1277         if( !i_len )
1278             break;
1279
1280         gcry_md_write( hd, psz_desc, i_len );
1281         if( sign.type == TEXT_SIGNATURE )
1282             gcry_md_putc( hd, '\r' );
1283         gcry_md_putc( hd, '\n' );
1284
1285         psz_desc += i_len;
1286         while( *psz_desc == '\r' || *psz_desc == '\n' )
1287             psz_desc++;
1288     }
1289
1290     if( sign.version == 3 )
1291     {
1292         gcry_md_putc( hd, sign.type );
1293         gcry_md_write( hd, &sign.specific.v3.timestamp, 4 );
1294     }
1295     else if( sign.version == 4 )
1296     {
1297         gcry_md_putc( hd, sign.version );
1298         gcry_md_putc( hd, sign.type );
1299         gcry_md_putc( hd, sign.public_key_algo );
1300         gcry_md_putc( hd, sign.digest_algo );
1301         gcry_md_write( hd, sign.specific.v4.hashed_data_len, 2 );
1302         size_t i_len = scalar_number( sign.specific.v4.hashed_data_len, 2 );
1303         gcry_md_write( hd, sign.specific.v4.hashed_data, i_len );
1304         gcry_md_putc( hd, 0x04 );
1305         gcry_md_putc( hd, 0xFF );
1306
1307         i_len += 6; /* hashed data + 6 bytes header */
1308
1309         gcry_md_putc( hd, (i_len << 24) & 0xff);
1310         gcry_md_putc( hd, (i_len << 16) &0xff );
1311         gcry_md_putc( hd, (i_len << 8) & 0xff );
1312         gcry_md_putc( hd, (i_len) & 0xff );
1313     }
1314     else
1315     {   /* RFC 4880 only tells about versions 3 and 4 */
1316         msg_Warn( p_update->p_libvlc, "Invalid signature version %d",
1317                 sign.version);
1318         goto error_hd;
1319     }
1320
1321     gcry_md_final( hd );
1322
1323     uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );
1324
1325     if( p_hash[0] != sign.hash_verification[0] ||
1326         p_hash[1] != sign.hash_verification[1] )
1327     {
1328         msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
1329         goto error_hd;
1330     }
1331
1332     if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
1333             != VLC_SUCCESS )
1334     {
1335         msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
1336         goto error_hd;
1337     }
1338     else
1339     {
1340         msg_Info( p_update->p_libvlc, "Status file authenticated" );
1341         gcry_md_close( hd );
1342         return true;
1343     }
1344
1345 error_hd:
1346     gcry_md_close( hd );
1347 error:
1348     if( p_stream )
1349         stream_Delete( p_stream );
1350     free( psz_version_line );
1351     return false;
1352 }
1353
1354 static void update_CheckReal( update_check_thread_t *p_uct );
1355
1356 /**
1357  * Check for updates
1358  *
1359  * \param p_update pointer to update struct
1360  * \param pf_callback pointer to a function to call when the update_check is finished
1361  * \param p_data pointer to some datas to give to the callback
1362  * \returns nothing
1363  */
1364 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
1365 {
1366     assert( p_update );
1367
1368     update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
1369                                             sizeof( update_check_thread_t ) );
1370     if( !p_uct ) return;
1371
1372     p_uct->p_update = p_update;
1373     p_update->p_check = p_uct;
1374     p_uct->pf_callback = pf_callback;
1375     p_uct->p_data = p_data;
1376
1377     vlc_thread_create( p_uct, "check for update", update_CheckReal,
1378                        VLC_THREAD_PRIORITY_LOW, false );
1379 }
1380
1381 void update_CheckReal( update_check_thread_t *p_uct )
1382 {
1383     bool b_ret;
1384     vlc_mutex_lock( &p_uct->p_update->lock );
1385
1386     EmptyRelease( p_uct->p_update );
1387     b_ret = GetUpdateFile( p_uct->p_update );
1388     vlc_mutex_unlock( &p_uct->p_update->lock );
1389
1390     if( p_uct->pf_callback )
1391         (p_uct->pf_callback)( p_uct->p_data, b_ret );
1392
1393     p_uct->p_update->p_check = NULL;
1394
1395     vlc_object_release( p_uct );
1396 }
1397
1398 /**
1399  * Compare a given release's version number to the current VLC's one
1400  *
1401  * \param p_update structure
1402  * \return true if we have to upgrade to the given version to be up to date
1403  */
1404 bool update_NeedUpgrade( update_t *p_update )
1405 {
1406     assert( p_update );
1407
1408     return  p_update->release.i_major    < *PACKAGE_VERSION_MAJOR    - '0'  ||
1409             p_update->release.i_minor    < *PACKAGE_VERSION_MINOR    - '0'  ||
1410             p_update->release.i_revision < *PACKAGE_VERSION_REVISION - '0'  ||
1411             p_update->release.extra      < *PACKAGE_VERSION_EXTRA;
1412 }
1413
1414 /**
1415  * Convert a long int size in bytes to a string
1416  *
1417  * \param l_size the size in bytes
1418  * \return the size as a string
1419  */
1420 static char *size_str( long int l_size )
1421 {
1422     char *psz_tmp = NULL;
1423     int i_retval = 0;
1424     if( l_size >> 30 )
1425         i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
1426     else if( l_size >> 20 )
1427         i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1428     else if( l_size >> 10 )
1429         i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1430     else
1431         i_retval = asprintf( &psz_tmp, "%ld B", l_size );
1432
1433     return i_retval == -1 ? NULL : psz_tmp;
1434 }
1435
1436 static void update_DownloadReal( update_download_thread_t *p_udt );
1437
1438 /**
1439  * Download the file given in the update_t
1440  *
1441  * \param p_update structure
1442  * \param dir to store the download file
1443  * \return nothing
1444  */
1445 void update_Download( update_t *p_update, const char *psz_destdir )
1446 {
1447     assert( p_update );
1448
1449     update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1450                                         sizeof( update_download_thread_t ) );
1451     if( !p_udt )
1452         return;
1453
1454     p_udt->p_update = p_update;
1455     p_update->p_download = p_udt;
1456     p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1457
1458     vlc_thread_create( p_udt, "download update", update_DownloadReal,
1459                        VLC_THREAD_PRIORITY_LOW, false );
1460 }
1461
1462 static void update_DownloadReal( update_download_thread_t *p_udt )
1463 {
1464     int i_progress = 0;
1465     long int l_size;
1466     long int l_downloaded = 0;
1467     float f_progress;
1468     char *psz_status = NULL;
1469     char *psz_downloaded = NULL;
1470     char *psz_size = NULL;
1471     char *psz_destfile = NULL;
1472     char *psz_tmpdestfile = NULL;
1473
1474     FILE *p_file = NULL;
1475     stream_t *p_stream = NULL;
1476     void* p_buffer = NULL;
1477     int i_read;
1478
1479     update_t *p_update = p_udt->p_update;
1480     char *psz_destdir = p_udt->psz_destdir;
1481
1482     /* Open the stream */
1483     p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1484     if( !p_stream )
1485     {
1486         msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1487         goto end;
1488     }
1489
1490     /* Get the stream size */
1491     l_size = stream_Size( p_stream );
1492
1493     /* Get the file name and open it*/
1494     psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1495     if( !psz_tmpdestfile )
1496     {
1497         msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1498         goto end;
1499     }
1500     psz_tmpdestfile++;
1501     if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1502         goto end;
1503
1504     p_file = utf8_fopen( psz_destfile, "w" );
1505     if( !p_file )
1506     {
1507         msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1508         goto end;
1509     }
1510
1511     /* Create a buffer and fill it with the downloaded file */
1512     p_buffer = (void *)malloc( 1 << 10 );
1513     if( !p_buffer )
1514         goto end;
1515
1516     psz_size = size_str( l_size );
1517     if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",
1518         p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1519     {
1520         i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1521         free( psz_status );
1522     }
1523
1524     vlc_object_lock( p_udt );
1525     while( vlc_object_alive( p_udt ) &&
1526            ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1527            !intf_ProgressIsCancelled( p_udt, i_progress ) )
1528     {
1529         vlc_object_unlock( p_udt );
1530         if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
1531         {
1532             msg_Err( p_udt, "Failed to write into %s", psz_destfile );
1533             break;
1534         }
1535
1536         l_downloaded += i_read;
1537         psz_downloaded = size_str( l_downloaded );
1538         f_progress = 100.0*(float)l_downloaded/(float)l_size;
1539
1540         if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done",
1541                       p_update->release.psz_url, psz_downloaded, psz_size,
1542                       f_progress ) != -1 )
1543         {
1544             intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1545             free( psz_status );
1546         }
1547         free( psz_downloaded );
1548         vlc_object_lock( p_udt );
1549     }
1550
1551     /* Finish the progress bar or delete the file if the user had canceled */
1552     fclose( p_file );
1553     p_file = NULL;
1554
1555     if( vlc_object_alive( p_udt ) &&
1556         !intf_ProgressIsCancelled( p_udt, i_progress ) )
1557     {
1558         vlc_object_unlock( p_udt );
1559         if( asprintf( &psz_status, "%s\nDone %s (100.0%%)",
1560             p_update->release.psz_url, psz_size ) != -1 )
1561         {
1562             intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1563             free( psz_status );
1564         }
1565     }
1566     else
1567     {
1568         vlc_object_unlock( p_udt );
1569         utf8_unlink( psz_destfile );
1570         goto end;
1571     }
1572
1573     signature_packet_t sign;
1574     if( download_signature( VLC_OBJECT( p_udt ), &sign,
1575             p_update->release.psz_url ) != VLC_SUCCESS )
1576     {
1577         utf8_unlink( psz_destfile );
1578
1579         intf_UserFatal( p_udt, true, _("File could not be verified"),
1580             _("It was not possible to download a cryptographic signature for "
1581               "the downloaded file \"%s\". Thus, it was deleted."),
1582             psz_destfile );
1583         msg_Err( p_udt, "Couldn't download signature of downloaded file" );
1584         goto end;
1585     }
1586
1587     if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
1588     {
1589         utf8_unlink( psz_destfile );
1590         msg_Err( p_udt, "Invalid signature issuer" );
1591         intf_UserFatal( p_udt, true, _("Invalid signature"),
1592             _("The cryptographic signature for the downloaded file \"%s\" was "
1593               "invalid and could not be used to securely verify it. Thus, the "
1594               "file was deleted."),
1595             psz_destfile );
1596         goto end;
1597     }
1598
1599     if( sign.type != BINARY_SIGNATURE )
1600     {
1601         utf8_unlink( psz_destfile );
1602         msg_Err( p_udt, "Invalid signature type" );
1603         intf_UserFatal( p_udt, true, _("Invalid signature"),
1604             _("The cryptographic signature for the downloaded file \"%s\" was "
1605               "invalid and could not be used to securely verify it. Thus, the "
1606               "file was deleted."),,
1607             psz_destfile );
1608         goto end;
1609     }
1610
1611     uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
1612     if( !p_hash )
1613     {
1614         msg_Err( p_udt, "Unable to hash %s", psz_destfile );
1615         utf8_unlink( psz_destfile );
1616         intf_UserFatal( p_udt, true, _("File not verifiable"),
1617             _("It was not possible to securely verify the downloaded file"
1618               " \"%s\". Thus, it was VLC deleted."),
1619             psz_destfile );
1620
1621         goto end;
1622     }
1623
1624     if( p_hash[0] != sign.hash_verification[0] ||
1625         p_hash[1] != sign.hash_verification[1] )
1626     {
1627         utf8_unlink( psz_destfile );
1628         intf_UserFatal( p_udt, true, _("File corrupted"),
1629             _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
1630              psz_destfile );
1631         msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
1632         free( p_hash );
1633         goto end;
1634     }
1635
1636     if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
1637             != VLC_SUCCESS )
1638     {
1639         utf8_unlink( psz_destfile );
1640         intf_UserFatal( p_udt, true, _("File corrupted"),
1641             _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
1642              psz_destfile );
1643         msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
1644         free( p_hash );
1645         goto end;
1646     }
1647
1648     msg_Info( p_udt, "%s authenticated", psz_destfile );
1649     free( p_hash );
1650
1651 end:
1652     if( p_stream )
1653         stream_Delete( p_stream );
1654     if( p_file )
1655         fclose( p_file );
1656     free( psz_destdir );
1657     free( psz_destfile );
1658     free( p_buffer );
1659     free( psz_size );
1660
1661     p_udt->p_update->p_download = NULL;
1662
1663     vlc_object_release( p_udt );
1664 }
1665
1666 update_release_t *update_GetRelease( update_t *p_update )
1667 {
1668     return &p_update->release;
1669 }
1670
1671 #else
1672 update_t *__update_New( vlc_object_t *p_this )
1673 {
1674     (void)p_this;
1675     return NULL;
1676 }
1677
1678 void update_Delete( update_t *p_update )
1679 {
1680     (void)p_update;
1681 }
1682
1683 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
1684                    void *p_data )
1685 {
1686     (void)p_update; (void)pf_callback; (void)p_data;
1687 }
1688
1689 bool update_NeedUpgrade( update_t *p_update )
1690 {
1691     (void)p_update;
1692     return false;
1693 }
1694
1695 void update_Download( update_t *p_update, const char *psz_destdir )
1696 {
1697     (void)p_update; (void)psz_destdir;
1698 }
1699
1700 update_release_t *update_GetRelease( update_t *p_update )
1701 {
1702     (void)p_update;
1703     return NULL;
1704 }
1705 #endif