]> git.sesse.net Git - vlc/blob - src/misc/update.c
use a pointer to a function instead of a callback
[vlc] / src / misc / update.c
1 /*****************************************************************************
2  * update.c: VLC update checking and downloading
3  *****************************************************************************
4  * Copyright © 2005-2007 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 and plugins update management
29  */
30
31 /* TODO: pgp verification of the status file, and downloaded binaries */
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36
37 #include <vlc/vlc.h>
38
39 #ifdef UPDATE_CHECK
40
41 #include <assert.h>
42
43 #include <vlc_update.h>
44 #include <vlc_stream.h>
45 #include <vlc_interface.h>
46
47 /*****************************************************************************
48  * Misc defines
49  *****************************************************************************/
50
51 /*
52  * Here is the format of these "status files" :
53  * First line is the last version: "X.Y.Ze" where:
54  *      * X is the major number
55  *      * Y is the minor number
56  *      * Z is the revision number
57  *      * e is an OPTIONAL extra letter
58  *      * AKA "0.8.6d" or "0.9.0"
59  * Second line is an url to the last binary
60  * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
61  */
62
63 #if defined( UNDER_CE )
64 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
65 #elif defined( WIN32 )
66 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
67 #elif defined( __APPLE__ )
68 #   if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
69 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
70 #   else
71 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
72 #   endif
73 #elif defined( SYS_BEOS )
74 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
75 #else
76 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
77 #endif
78
79
80 /*****************************************************************************
81  * Local Prototypes
82  *****************************************************************************/
83 static void EmptyRelease( update_t *p_update );
84 static void GetUpdateFile( update_t *p_update );
85 static int CompareReleases( const struct update_release_t *p1,
86                             const struct update_release_t *p2 );
87 static char * size_str( long int l_size );
88
89
90 /*****************************************************************************
91  * OpenPGP functions
92  *****************************************************************************/
93
94 #define packet_type( c ) ( ( c & 0x3c ) >> 2 )      /* 0x3C = 00111100 */
95 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
96
97 static inline int scalar_number( uint8_t *p, int header_len )
98 {
99     if( header_len == 1 )
100         return( p[0] );
101     else if( header_len == 2 )
102         return( (p[0] << 8) + p[1] );
103     else if( header_len == 4 )
104         return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
105     else
106         abort();
107 }
108
109 /* number of data bytes in a MPI */
110 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
111
112 /* 
113  * fill a public_key_packet_t structure from public key packet data
114  * verify that it is a version 4 public key packet, using DSA
115  */
116 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
117                                     size_t i_packet_len )
118 {
119     if( i_packet_len != 418 )
120         return VLC_EGENERIC;
121
122     p_key->version   = *p_buf++;
123     if( p_key->version != 4 )
124         return VLC_EGENERIC;
125
126     /* warn when timestamp is > date ? */
127     memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;
128
129     p_key->algo      = *p_buf++;
130     if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
131         return VLC_EGENERIC;
132
133     memcpy( p_key->p, p_buf, 2+128 ); p_buf += 2+128;
134     if( mpi_len( p_key->p ) != 128 )
135         return VLC_EGENERIC;
136
137     memcpy( p_key->q, p_buf, 2+20 );  p_buf += 2+20;
138     if( mpi_len( p_key->q ) != 20 )
139         return VLC_EGENERIC;
140
141     memcpy( p_key->g, p_buf, 2+128 ); p_buf += 2+128;
142     if( mpi_len( p_key->g ) != 128 )
143         return VLC_EGENERIC;
144
145     memcpy( p_key->y, p_buf, 2+128 ); p_buf += 2+128;
146     if( mpi_len( p_key->y ) != 128 )
147         return VLC_EGENERIC;
148
149     return VLC_SUCCESS;
150 }
151
152 /*
153  * fill a signature_packet_v4_t from signature packet data
154  * verify that it was used with a DSA public key, using SHA-1 digest
155  */
156 static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
157                                       uint8_t *p_buf, size_t i_sig_len )
158 {
159     if( i_sig_len < 54 )
160         return VLC_EGENERIC;
161
162     p_sig->version = *p_buf++;
163     if( p_sig->version != 4 )
164         return VLC_EGENERIC;
165
166     p_sig->type = *p_buf++;
167     if( p_sig->type < GENERIC_KEY_SIGNATURE ||
168         p_sig->type > POSITIVE_KEY_SIGNATURE )
169         return VLC_EGENERIC;
170
171     p_sig->public_key_algo = *p_buf++;
172     if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
173         return VLC_EGENERIC;
174
175     p_sig->digest_algo = *p_buf++;
176     if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
177         return VLC_EGENERIC;
178
179     memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;
180
181     size_t i_pos = 6;
182     size_t i_hashed_data_len = scalar_number( p_sig->hashed_data_len, 2 );
183     i_pos += i_hashed_data_len;
184     if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total, 
185                               * + the unhashed data length (2 bytes)
186                               * + the hash verification (2 bytes) */
187         return VLC_EGENERIC;
188
189     p_sig->hashed_data = (uint8_t*) malloc( i_hashed_data_len );
190     if( !p_sig->hashed_data )
191         return VLC_ENOMEM;
192     memcpy( p_sig->hashed_data, p_buf, i_hashed_data_len );
193     p_buf += i_hashed_data_len;
194
195     memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;
196
197     size_t i_unhashed_data_len = scalar_number( p_sig->unhashed_data_len, 2 );
198     i_pos += 2 + i_unhashed_data_len;
199     if( i_pos != i_sig_len - 46 )
200     {
201         free( p_sig->hashed_data );
202         return VLC_EGENERIC;
203     }
204
205     p_sig->unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
206     if( !p_sig->unhashed_data )
207     {
208         free( p_sig->hashed_data );
209         return VLC_ENOMEM;
210     }
211     memcpy( p_sig->unhashed_data, p_buf, i_unhashed_data_len );
212     p_buf += i_unhashed_data_len;
213
214     memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
215
216     memcpy( p_sig->r, p_buf, 22 ); p_buf += 22;
217     if( mpi_len( p_sig->r ) != 20 )
218     {
219         free( p_sig->hashed_data );
220         free( p_sig->unhashed_data );
221         return VLC_EGENERIC;
222     }
223
224     memcpy( p_sig->s, p_buf, 22 );
225     if( mpi_len( p_sig->s ) != 20 )
226     {
227         free( p_sig->hashed_data );
228         free( p_sig->unhashed_data );
229         return VLC_EGENERIC;
230     }
231
232     return VLC_SUCCESS;
233 }
234
235 /*
236  * crc_octets() was lamely copied from rfc 2440
237  * Copyright (C) The Internet Society (1998).  All Rights Reserved.
238  */
239 #define CRC24_INIT 0xB704CEL
240 #define CRC24_POLY 0x1864CFBL
241
242 static long crc_octets( uint8_t *octets, size_t len )
243 {
244     long crc = CRC24_INIT;
245     int i;
246     while (len--)
247     {
248         crc ^= (*octets++) << 16;
249         for (i = 0; i < 8; i++)
250         {
251             crc <<= 1;
252             if (crc & 0x1000000)
253                 crc ^= CRC24_POLY;
254         }
255     }
256     return crc & 0xFFFFFFL;
257 }
258
259 /*
260  * Transform an armored document in binary format
261  * Used on public keys and signatures
262  */
263 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
264                         uint8_t *p_obuf, size_t i_obuf_len )
265 {
266     char *p_ipos = p_ibuf;
267     uint8_t *p_opos = p_obuf;
268     int i_end = 0;
269
270     int i_header_skipped = 0;
271
272     while( !i_end && p_ipos < p_ibuf + i_ibuf_len )
273     {
274         if( *p_ipos == '\r' || *p_ipos == '\n' )
275         {
276             p_ipos++;
277             continue;
278         }
279
280         size_t i_line_len = strcspn( p_ipos, "\r\n" );
281         if( i_line_len == 0 )
282             continue;
283
284         if( !i_header_skipped )
285         {
286             if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
287                 i_header_skipped = 1;
288
289             p_ipos += i_line_len + 1;
290             continue;
291         }
292         
293         if( !strncmp( p_ipos, "Version:", 8 ) )
294         {
295             p_ipos += i_line_len + 1;
296             continue;
297         }
298
299         if( p_ipos[i_line_len - 1] == '=' )
300         {
301             i_end = 1;
302             p_ipos[i_line_len - 1] = '\0';
303         }
304         else
305             p_ipos[i_line_len] = '\0';
306
307         p_opos += vlc_b64_decode_binary_to_buffer(  p_opos,
308                                                     p_obuf - p_opos + i_obuf_len,
309                                                     p_ipos );
310
311         p_ipos += i_line_len + 1;
312     }
313
314     /* XXX: the CRC is OPTIONAL, really require it ? */
315     if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
316         return 0;
317
318     uint8_t p_crc[3];
319     if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
320         return 0;
321
322     long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
323     long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
324
325     return l_crc2 == l_crc ? p_opos - p_obuf : 0;
326 }
327
328 /*
329  * Download the signature associated to a document or a binary file.
330  * We're given the file's url, we just append ".asc" to it and download 
331  */
332 static int download_signature(  vlc_object_t *p_this,
333                                 signature_packet_v3_t *p_sig, char *psz_url )
334 {
335     char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
336     if( !psz_sig )
337         return VLC_ENOMEM;
338
339     strcpy( psz_sig, psz_url );
340     strcat( psz_sig, ".asc" );
341
342     stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
343     free( psz_sig );
344
345     if( !p_stream )
346         return VLC_ENOMEM;
347
348     int64_t i_size = stream_Size( p_stream );
349     if( i_size < 65 )
350     {
351         stream_Delete( p_stream );
352         return VLC_EGENERIC;
353     }
354     else if( i_size == 65 ) /* binary format signature */
355     {
356         int i_read = stream_Read( p_stream, p_sig, (int)i_size );
357         stream_Delete( p_stream );
358         if( i_read != i_size )
359             return VLC_EGENERIC;
360         else
361             return VLC_SUCCESS;
362     }
363
364     char *p_buf = (char*)malloc( i_size );
365     if( !p_buf )
366     {
367         stream_Delete( p_stream );
368         return VLC_ENOMEM;
369     }
370     
371     int i_read = stream_Read( p_stream, p_buf, (int)i_size );
372
373     stream_Delete( p_stream );
374
375     if( i_read != i_size )
376     {
377         free( p_buf );
378         return VLC_EGENERIC;
379     }
380     
381     int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
382     free( p_buf );
383
384     if( i_bytes != 65 )
385         return VLC_EGENERIC;
386     else
387         return VLC_SUCCESS;
388 }
389
390 /*
391  * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
392  */
393 static int verify_signature( vlc_object_t *p_this, uint8_t *p_r, uint8_t *p_s,
394         public_key_packet_t *p_key, uint8_t *p_hash )
395 {
396     /* the data to be verified (a SHA-1 hash) */
397     const char *hash_sexp_s = "(data(flags raw)(value %m))";
398     /* the public key */
399     const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
400     /* the signature */
401     const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
402
403     size_t erroff;
404     gcry_mpi_t p, q, g, y, r, s, hash;
405     p = q = g = y = r = s = hash = NULL;
406     gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
407     key_sexp = hash_sexp = sig_sexp = NULL;
408
409     if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, 128, NULL ) ||
410         gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, 20, NULL ) ||
411         gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, 128, NULL ) ||
412         gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, 128, NULL ) ||
413         gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
414         goto problem;
415
416     if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, 20, NULL ) ||
417         gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, 20, NULL ) ||
418         gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
419         goto problem;
420
421     if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, 20, NULL ) ||
422         gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
423         goto problem;
424
425     if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
426         goto problem;
427
428     return VLC_SUCCESS;
429
430 problem:
431     if( p ) gcry_mpi_release( p );
432     if( q ) gcry_mpi_release( q );
433     if( g ) gcry_mpi_release( g );
434     if( y ) gcry_mpi_release( y );
435     if( r ) gcry_mpi_release( r );
436     if( s ) gcry_mpi_release( s );
437     if( hash ) gcry_mpi_release( hash );
438     if( key_sexp ) gcry_sexp_release( key_sexp );
439     if( sig_sexp ) gcry_sexp_release( sig_sexp );
440     if( hash_sexp ) gcry_sexp_release( hash_sexp );
441     return VLC_EGENERIC;
442 }
443
444 /*
445  * Return the long id (8 bytes) of the public key used to generate a signature
446  */
447 static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
448 {
449     uint8_t *p = p_sig->unhashed_data;
450     uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );
451
452     while( p < max_pos )
453     {
454         int i_subpacket_len = *p < 192 ? *p++ :
455                 *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
456                 ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;
457
458         if( p >= max_pos - 1 )
459             return NULL;
460
461         if( *p == ISSUER_SUBPACKET )
462             return p+1;
463         else
464             p += i_subpacket_len;
465     }
466     return NULL;
467 }
468
469 /*
470  * fill a public_key_t with public key data, including:
471  *   * public key packet
472  *   * signature packet issued by key which long id is p_sig_issuer
473  *   * user id packet
474  */
475 static int parse_public_key( const uint8_t *p_key_data, size_t i_key_len, public_key_t *p_key, const uint8_t *p_sig_issuer )
476 {
477     uint8_t *pos = (uint8_t*) p_key_data;
478     uint8_t *max_pos = pos + i_key_len;
479
480     int i_status = 0;
481 #define PUBLIC_KEY_FOUND    0x01
482 #define USER_ID_FOUND       0x02
483 #define SIGNATURE_FOUND     0X04
484
485     uint8_t *p_key_unarmored = NULL;
486
487     signature_packet_v4_t sig;
488
489     p_key->psz_username = NULL;
490     p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;
491
492     if( !( *pos & 0x80 ) )
493     {   /* first byte is ASCII, unarmoring */
494         p_key_unarmored = (uint8_t*)malloc( i_key_len );
495         if( !p_key_unarmored )
496             return VLC_ENOMEM;
497         int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
498                                  p_key_unarmored, i_key_len );
499
500         if( i_len == 0 )
501             goto error;
502
503         pos = p_key_unarmored;
504         max_pos = pos + i_len;
505     }
506
507     while( pos < max_pos )
508     {
509         if( !(*pos & 0x80) || *pos & 0x40 )
510             goto error;
511
512         int i_type = packet_type( *pos );
513
514         int i_header_len = packet_header_len( *pos++ );
515         if( pos + i_header_len > max_pos )
516             goto error;
517
518         int i_packet_len = scalar_number( pos, i_header_len );
519         pos += i_header_len;
520
521         if( pos + i_packet_len > max_pos )
522             goto error;
523
524         switch( i_type )
525         {
526             uint8_t *p_issuer;
527
528             case PUBLIC_KEY_PACKET:
529                 i_status |= PUBLIC_KEY_FOUND;
530                 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
531                     goto error;
532                 break;
533
534             case SIGNATURE_PACKET:
535                 if( !p_sig_issuer || i_status & SIGNATURE_FOUND ||
536                     parse_signature_v4_packet( &sig, pos, i_packet_len ) != VLC_SUCCESS )
537                     break;
538                 p_issuer = get_issuer_from_signature_v4( &sig );
539                 if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
540                 {
541                     memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
542                     i_status |= SIGNATURE_FOUND;
543                 }
544                 else
545                 {
546                     free( sig.hashed_data );
547                     free( sig.unhashed_data );
548                 }
549                 break;
550
551             case USER_ID_PACKET:
552                 if( p_key->psz_username ) /* save only the first User ID */
553                     break;
554                 i_status |= USER_ID_FOUND;
555                 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
556                 if( !p_key->psz_username )
557                     goto error;
558
559                 memcpy( p_key->psz_username, pos, i_packet_len );
560                 p_key->psz_username[i_packet_len] = '\0';
561                 break;
562             
563             default:
564                 break;
565         }
566         pos += i_packet_len;
567     }
568     free( p_key_unarmored );
569
570     if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
571         return VLC_EGENERIC;
572
573     if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
574         return VLC_EGENERIC;
575
576     return VLC_SUCCESS;
577
578 error:
579     free( p_key->sig.hashed_data );
580     free( p_key->sig.unhashed_data );
581     free( p_key->psz_username );
582     free( p_key_unarmored );
583     return VLC_EGENERIC;
584 }
585
586 /*
587  * return a sha1 hash of a file
588  */
589 static uint8_t *hash_sha1_from_file( const char *psz_file,
590                             signature_packet_v3_t *p_sig )
591 {
592     FILE *f = utf8_fopen( psz_file, "r" );
593     if( !f )
594         return NULL;
595
596     uint8_t buffer[4096]; //FIXME
597
598     gcry_md_hd_t hd;
599     if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
600     {
601         fclose( f );
602         return NULL;
603     } 
604
605     size_t i_read;
606     while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
607         gcry_md_write( hd, buffer, i_read );
608
609     gcry_md_putc( hd, p_sig->type );
610     gcry_md_write( hd, &p_sig->timestamp, 4 );
611
612     fclose( f );
613     gcry_md_final( hd );
614
615     return( (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1) );
616 }
617
618 /*
619  * download a public key (the last one) from videolan server, and parse it
620  */
621 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
622 {
623     char *psz_url;
624     if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x.asc",
625                             p_longid[0], p_longid[1],
626                             p_longid[2], p_longid[3],
627                             p_longid[4], p_longid[5],
628                             p_longid[6], p_longid[7] ) == -1 )
629         return NULL;
630
631     stream_t *p_stream = stream_UrlNew( p_this, psz_url );
632     free( psz_url );
633     if( !p_stream )
634         return NULL;
635
636     int64_t i_size = stream_Size( p_stream );
637     if( i_size < 0 )
638     {
639         stream_Delete( p_stream );
640         return NULL;
641     }
642
643     uint8_t *p_buf = (uint8_t*)malloc( i_size );
644     if( !p_buf )
645     {
646         stream_Delete( p_stream );
647         return NULL;
648     }
649
650     int i_read = stream_Read( p_stream, p_buf, (int)i_size );
651     stream_Delete( p_stream );
652
653     if( i_read != (int)i_size )
654     {
655         free( p_buf );
656         return NULL;
657     }
658
659     public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
660     if( !p_pkey )
661     {
662         free( p_buf );
663         return NULL;
664     }
665
666     int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
667     free( p_buf );
668
669     if( i_error != VLC_SUCCESS )
670     {
671         free( p_pkey );
672         return NULL;
673     }
674
675     return p_pkey;
676 }
677
678 /*
679  * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
680  * Note that we need the signature to compute the hash
681  */
682 static uint8_t *key_sign_hash( public_key_t *p_pkey )
683 {
684     gcry_error_t error = 0;
685     gcry_md_hd_t hd;
686
687     error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
688     if( error )
689         return NULL;
690
691     gcry_md_putc( hd, 0x99 );
692
693     gcry_md_putc( hd, (418 >> 8) & 0xff );
694     gcry_md_putc( hd, 418 & 0xff );
695
696     gcry_md_write( hd, (uint8_t*)&p_pkey->key, 418 );
697
698     gcry_md_putc( hd, 0xb4 );
699
700     int i_len = strlen((char*)p_pkey->psz_username);
701
702     gcry_md_putc( hd, (i_len << 24) & 0xff );
703     gcry_md_putc( hd, (i_len << 16) & 0xff );
704     gcry_md_putc( hd, (i_len << 8) & 0xff );
705     gcry_md_putc( hd, (i_len) & 0xff );
706
707     gcry_md_write( hd, p_pkey->psz_username, i_len );
708
709     size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );
710
711     gcry_md_putc( hd, p_pkey->sig.version );
712     gcry_md_putc( hd, p_pkey->sig.type );
713     gcry_md_putc( hd, p_pkey->sig.public_key_algo );
714     gcry_md_putc( hd, p_pkey->sig.digest_algo );
715     gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
716     gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );
717
718     gcry_md_putc( hd, 0x04 );
719     gcry_md_putc( hd, 0xff );
720
721     i_hashed_data_len += 6; /* hashed data + 6 bytes header */
722
723     gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
724     gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
725     gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
726     gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
727
728     gcry_md_final( hd );
729
730     uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
731
732     if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
733         p_hash[1] != p_pkey->sig.hash_verification[1] )
734     {
735         free( p_hash );
736         return NULL;
737     }
738
739     return p_hash;
740 }
741
742
743 /*****************************************************************************
744  * Update_t functions
745  *****************************************************************************/
746
747 /**
748  * Create a new update VLC struct
749  *
750  * \param p_this the calling vlc_object
751  * \return pointer to new update_t or NULL
752  */
753 update_t *__update_New( vlc_object_t *p_this )
754 {
755     update_t *p_update;
756     assert( p_this );
757
758     p_update = (update_t *)malloc( sizeof( update_t ) );
759     if( !p_update ) return NULL;
760
761     vlc_mutex_init( p_this, &p_update->lock );
762
763     p_update->p_libvlc = p_this->p_libvlc;
764
765     p_update->release.psz_url = NULL;
766     p_update->release.psz_desc = NULL;
767
768     return p_update;
769 }
770
771 /**
772  * Delete an update_t struct
773  *
774  * \param p_update update_t* pointer
775  * \return nothing
776  */
777 void update_Delete( update_t *p_update )
778 {
779     assert( p_update );
780
781     vlc_mutex_destroy( &p_update->lock );
782
783     FREENULL( p_update->release.psz_url );
784     FREENULL( p_update->release.psz_desc );
785
786     free( p_update );
787 }
788
789 /**
790  * Empty the release struct
791  *
792  * \param p_update update_t* pointer
793  * \return nothing
794  */
795 static void EmptyRelease( update_t *p_update )
796 {
797     p_update->release.i_major = 0;
798     p_update->release.i_minor = 0;
799     p_update->release.i_revision = 0;
800
801     FREENULL( p_update->release.psz_url );
802     FREENULL( p_update->release.psz_desc );
803 }
804
805 /**
806  * Get the update file and parse it
807  * *p_update has to be locked when calling this function
808  *
809  * \param p_update pointer to update struct
810  * \return nothing
811  */
812 static void GetUpdateFile( update_t *p_update )
813 {
814     stream_t *p_stream = NULL;
815     int i_major = 0;
816     int i_minor = 0;
817     int i_revision = 0;
818     unsigned char extra;
819     char *psz_line = NULL;
820
821     p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
822     if( !p_stream )
823     {
824         msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
825                  UPDATE_VLC_STATUS_URL );
826         goto error;
827     }
828
829     /* Try to read three lines */
830     if( !( psz_line = stream_ReadLine( p_stream ) ) )
831     {
832         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
833                  UPDATE_VLC_STATUS_URL );
834         goto error;
835     }
836
837     /* first line : version number */
838     p_update->release.extra = 0;
839     switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
840     {
841         case 4:
842             p_update->release.extra = extra;
843         case 3:
844             p_update->release.i_major = i_major;
845             p_update->release.i_minor = i_minor;
846             p_update->release.i_revision = i_revision;
847             break;
848         default:
849             msg_Err( p_update->p_libvlc, "Update version false formated" );
850             free( psz_line );
851             goto error;
852     }
853
854     /* Second line : URL */
855     if( !( psz_line = stream_ReadLine( p_stream ) ) )
856     {
857         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
858                  UPDATE_VLC_STATUS_URL );
859         goto error;
860     }
861     p_update->release.psz_url = psz_line;
862
863
864     /* Third line : description */
865     if( !( psz_line = stream_ReadLine( p_stream ) ) )
866     {
867         msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
868                  UPDATE_VLC_STATUS_URL );
869         goto error;
870     }
871     p_update->release.psz_desc = psz_line;
872
873 error:
874     if( p_stream )
875         stream_Delete( p_stream );
876 }
877
878
879 /**
880  * Struct to launch the check in an other thread
881  */
882 typedef struct
883 {
884     VLC_COMMON_MEMBERS
885     update_t *p_update;
886     void (*pf_callback)( void * );
887     void *p_data;
888 } update_check_thread_t;
889
890 void update_CheckReal( update_check_thread_t *p_uct );
891
892 /**
893  * Check for updates
894  *
895  * \param p_update pointer to update struct
896  * \param pf_callback pointer to a function to call when the update_check is finished
897  * \param p_data pointer to some datas to give to the callback
898  * \returns nothing
899  */
900 void update_Check( update_t *p_update, void (*pf_callback)( void* ), void *p_data )
901 {
902     assert( p_update );
903
904     update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
905                                             sizeof( update_check_thread_t ) );
906     p_uct->p_update = p_update;
907     p_uct->pf_callback = pf_callback;
908     p_uct->p_data = p_data;
909
910     vlc_thread_create( p_uct, "check for update", update_CheckReal,
911                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
912 }
913
914 void update_CheckReal( update_check_thread_t *p_uct )
915 {
916     vlc_mutex_lock( &p_uct->p_update->lock );
917
918     EmptyRelease( p_uct->p_update );
919     GetUpdateFile( p_uct->p_update );
920
921     vlc_mutex_unlock( &p_uct->p_update->lock );
922
923     if( p_uct->pf_callback )
924         (p_uct->pf_callback)( p_uct->p_data );
925 }
926
927 /**
928  * Compare two release numbers
929  *
930  * \param p1 first release
931  * \param p2 second release
932  * \return UpdateReleaseStatus(Older|Equal|Newer)
933  */
934 static int CompareReleases( const struct update_release_t *p1,
935                             const struct update_release_t *p2 )
936 {
937     int32_t d;
938     d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
939       - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
940       + ( p1->extra ) - ( p2->extra );
941
942     if( d < 0 )
943         return UpdateReleaseStatusOlder;
944     else if( d == 0 )
945         return UpdateReleaseStatusEqual;
946     else
947         return UpdateReleaseStatusNewer;
948 }
949
950 /**
951  * Compare a given release's version number to the current VLC's one
952  *
953  * \param p_update structure
954  * \return UpdateReleaseStatus(Older|Equal|Newer)
955  */
956 int update_CompareReleaseToCurrent( update_t *p_update )
957 {
958     assert( p_update );
959
960     struct update_release_t c;
961
962     /* get the current version number */
963     c.i_major = *PACKAGE_VERSION_MAJOR - '0';
964     c.i_minor = *PACKAGE_VERSION_MINOR - '0';
965     c.i_revision = *PACKAGE_VERSION_REVISION - '0';
966     c.extra = *PACKAGE_VERSION_EXTRA;
967
968     return CompareReleases( &p_update->release, &c );
969 }
970
971 /**
972  * Convert a long int size in bytes to a string
973  *
974  * \param l_size the size in bytes
975  * \return the size as a string
976  */
977 static char *size_str( long int l_size )
978 {
979     char *psz_tmp = NULL;
980     if( l_size >> 30 )
981         asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
982     else if( l_size >> 20 )
983         asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
984     else if( l_size >> 10 )
985         asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
986     else
987         asprintf( &psz_tmp, "%ld B", l_size );
988     return psz_tmp;
989 }
990
991
992 /*
993  * Struct to launch the download in a thread
994  */
995 typedef struct
996 {
997     VLC_COMMON_MEMBERS
998     update_t *p_update;
999     char *psz_destdir;
1000 } update_download_thread_t;
1001
1002 void update_DownloadReal( update_download_thread_t *p_udt );
1003
1004 /**
1005  * Download the file given in the update_t
1006  *
1007  * \param p_update structure
1008  * \param dir to store the download file
1009  * \return nothing
1010  */
1011 void update_Download( update_t *p_update, char *psz_destdir )
1012 {
1013     assert( p_update );
1014
1015     update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1016                                                       sizeof( update_download_thread_t ) );
1017
1018     p_udt->p_update = p_update;
1019     p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1020
1021     vlc_thread_create( p_udt, "download update", update_DownloadReal,
1022                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1023 }
1024 void update_DownloadReal( update_download_thread_t *p_udt )
1025 {
1026     int i_progress = 0;
1027     long int l_size;
1028     long int l_downloaded = 0;
1029     float f_progress;
1030     char *psz_status = NULL;
1031     char *psz_downloaded = NULL;
1032     char *psz_size = NULL;
1033     char *psz_destfile = NULL;
1034     char *psz_tmpdestfile = NULL;
1035
1036     FILE *p_file = NULL;
1037     stream_t *p_stream = NULL;
1038     void* p_buffer = NULL;
1039     int i_read;
1040
1041     update_t *p_update = p_udt->p_update;
1042     char *psz_destdir = p_udt->psz_destdir;
1043
1044     /* Open the stream */
1045     p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1046     if( !p_stream )
1047     {
1048         msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1049         goto error;
1050     }
1051
1052     /* Get the stream size */
1053     l_size = stream_Size( p_stream );
1054
1055     /* Get the file name and open it*/
1056     psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1057     if( !psz_tmpdestfile )
1058     {
1059         msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1060         goto error;
1061     }
1062     psz_tmpdestfile++;
1063     if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1064         goto error;
1065
1066     p_file = utf8_fopen( psz_destfile, "w" );
1067     if( !p_file )
1068     {
1069         msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1070         goto error;
1071     }
1072
1073     /* Create a buffer and fill it with the downloaded file */
1074     p_buffer = (void *)malloc( 1 << 10 );
1075     if( !p_buffer )
1076         goto error;
1077
1078     psz_size = size_str( l_size );
1079     if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",  p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1080     {
1081         i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1082         free( psz_status );
1083     }
1084
1085     while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1086                                    !intf_ProgressIsCancelled( p_udt, i_progress ) )
1087     {
1088         fwrite( p_buffer, i_read, 1, p_file );
1089
1090         l_downloaded += i_read;
1091         psz_downloaded = size_str( l_downloaded );
1092         f_progress = 100.0*(float)l_downloaded/(float)l_size;
1093
1094         if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done", p_update->release.psz_url,
1095                       psz_size, psz_downloaded, f_progress ) != -1 )
1096         {
1097             intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1098             free( psz_status );
1099         }
1100         free( psz_downloaded );
1101     }
1102
1103     /* Finish the progress bar or delete the file if the user had canceled */
1104     fclose( p_file );
1105     p_file = NULL;
1106     if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
1107     {
1108         if( asprintf( &psz_status, "%s\nDone %s (100.0%%)", p_update->release.psz_url, psz_size ) != -1 )
1109         {
1110             intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1111             free( psz_status );
1112         }
1113     }
1114     else
1115         remove( psz_destfile );
1116
1117 error:
1118     if( p_stream )
1119         stream_Delete( p_stream );
1120     if( p_file )
1121         fclose( p_file );
1122     free( psz_destdir );
1123     free( psz_destfile );
1124     free( p_buffer );
1125     free( psz_size );
1126 }
1127
1128 #endif