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