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