1 /*****************************************************************************
2 * update.c: VLC update checking and downloading
3 *****************************************************************************
4 * Copyright © 2005-2007 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8 * Rémi Duraffort <ivoire at via.ecp.fr>
9 Rafaël Carré <funman@videolanorg>
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.
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.
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 *****************************************************************************/
28 * This file contains functions related to VLC and plugins update management
31 /*****************************************************************************
33 *****************************************************************************/
41 #include <vlc_update.h>
42 #include <vlc_stream.h>
43 #include <vlc_interface.h>
46 /*****************************************************************************
48 *****************************************************************************/
51 * Here is the format of these "status files" :
52 * First line is the last version: "X.Y.Ze" where:
53 * * X is the major number
54 * * Y is the minor number
55 * * Z is the revision number
56 * * e is an OPTIONAL extra letter
57 * * AKA "0.8.6d" or "0.9.0"
58 * Second line is an url to the last binary
59 * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
62 #if defined( UNDER_CE )
63 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
64 #elif defined( WIN32 )
65 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
66 #elif defined( __APPLE__ )
67 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
68 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
70 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
72 #elif defined( SYS_BEOS )
73 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
75 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
79 /*****************************************************************************
81 *****************************************************************************/
82 static void EmptyRelease( update_t *p_update );
83 static vlc_bool_t GetUpdateFile( update_t *p_update );
84 static int CompareReleases( const struct update_release_t *p1,
85 const struct update_release_t *p2 );
86 static char * size_str( long int l_size );
89 /*****************************************************************************
91 *****************************************************************************/
93 #define packet_type( c ) ( ( c & 0x3c ) >> 2 ) /* 0x3C = 00111100 */
94 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
96 static inline int scalar_number( uint8_t *p, int header_len )
100 else if( header_len == 2 )
101 return( (p[0] << 8) + p[1] );
102 else if( header_len == 4 )
103 return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
108 /* number of data bytes in a MPI */
109 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
112 * fill a public_key_packet_t structure from public key packet data
113 * verify that it is a version 4 public key packet, using DSA
115 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
116 size_t i_packet_len )
118 if( i_packet_len != 418 )
121 p_key->version = *p_buf++;
122 if( p_key->version != 4 )
125 /* warn when timestamp is > date ? */
126 memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;
128 p_key->algo = *p_buf++;
129 if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
132 memcpy( p_key->p, p_buf, 2+128 ); p_buf += 2+128;
133 if( mpi_len( p_key->p ) != 128 )
136 memcpy( p_key->q, p_buf, 2+20 ); p_buf += 2+20;
137 if( mpi_len( p_key->q ) != 20 )
140 memcpy( p_key->g, p_buf, 2+128 ); p_buf += 2+128;
141 if( mpi_len( p_key->g ) != 128 )
144 memcpy( p_key->y, p_buf, 2+128 ); p_buf += 2+128;
145 if( mpi_len( p_key->y ) != 128 )
152 * fill a signature_packet_v4_t from signature packet data
153 * verify that it was used with a DSA public key, using SHA-1 digest
155 static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
156 uint8_t *p_buf, size_t i_sig_len )
161 p_sig->version = *p_buf++;
162 if( p_sig->version != 4 )
165 p_sig->type = *p_buf++;
166 if( p_sig->type < GENERIC_KEY_SIGNATURE ||
167 p_sig->type > POSITIVE_KEY_SIGNATURE )
170 p_sig->public_key_algo = *p_buf++;
171 if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
174 p_sig->digest_algo = *p_buf++;
175 if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
178 memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;
181 size_t i_hashed_data_len = scalar_number( p_sig->hashed_data_len, 2 );
182 i_pos += i_hashed_data_len;
183 if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total,
184 * + the unhashed data length (2 bytes)
185 * + the hash verification (2 bytes) */
188 p_sig->hashed_data = (uint8_t*) malloc( i_hashed_data_len );
189 if( !p_sig->hashed_data )
191 memcpy( p_sig->hashed_data, p_buf, i_hashed_data_len );
192 p_buf += i_hashed_data_len;
194 memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;
196 size_t i_unhashed_data_len = scalar_number( p_sig->unhashed_data_len, 2 );
197 i_pos += 2 + i_unhashed_data_len;
198 if( i_pos != i_sig_len - 46 )
200 free( p_sig->hashed_data );
204 p_sig->unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
205 if( !p_sig->unhashed_data )
207 free( p_sig->hashed_data );
210 memcpy( p_sig->unhashed_data, p_buf, i_unhashed_data_len );
211 p_buf += i_unhashed_data_len;
213 memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
215 memcpy( p_sig->r, p_buf, 22 ); p_buf += 22;
216 if( mpi_len( p_sig->r ) != 20 )
218 free( p_sig->hashed_data );
219 free( p_sig->unhashed_data );
223 memcpy( p_sig->s, p_buf, 22 );
224 if( mpi_len( p_sig->s ) != 20 )
226 free( p_sig->hashed_data );
227 free( p_sig->unhashed_data );
235 * crc_octets() was lamely copied from rfc 2440
236 * Copyright (C) The Internet Society (1998). All Rights Reserved.
238 #define CRC24_INIT 0xB704CEL
239 #define CRC24_POLY 0x1864CFBL
241 static long crc_octets( uint8_t *octets, size_t len )
243 long crc = CRC24_INIT;
247 crc ^= (*octets++) << 16;
248 for (i = 0; i < 8; i++)
255 return crc & 0xFFFFFFL;
259 * Transform an armored document in binary format
260 * Used on public keys and signatures
262 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
263 uint8_t *p_obuf, size_t i_obuf_len )
265 char *p_ipos = p_ibuf;
266 uint8_t *p_opos = p_obuf;
269 int i_header_skipped = 0;
271 while( !i_end && p_ipos < p_ibuf + i_ibuf_len )
273 if( *p_ipos == '\r' || *p_ipos == '\n' )
279 size_t i_line_len = strcspn( p_ipos, "\r\n" );
280 if( i_line_len == 0 )
283 if( !i_header_skipped )
285 if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
286 i_header_skipped = 1;
288 p_ipos += i_line_len + 1;
292 if( !strncmp( p_ipos, "Version:", 8 ) )
294 p_ipos += i_line_len + 1;
298 if( p_ipos[i_line_len - 1] == '=' )
301 p_ipos[i_line_len - 1] = '\0';
304 p_ipos[i_line_len] = '\0';
306 p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
307 p_obuf - p_opos + i_obuf_len,
310 p_ipos += i_line_len + 1;
313 /* XXX: the CRC is OPTIONAL, really require it ? */
314 if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
318 if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
321 long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
322 long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
324 return l_crc2 == l_crc ? p_opos - p_obuf : 0;
328 * Download the signature associated to a document or a binary file.
329 * We're given the file's url, we just append ".asc" to it and download
331 static int download_signature( vlc_object_t *p_this,
332 signature_packet_v3_t *p_sig,
333 const char *psz_url )
335 char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
339 strcpy( psz_sig, psz_url );
340 strcat( psz_sig, ".asc" );
342 stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
348 int64_t i_size = stream_Size( p_stream );
351 stream_Delete( p_stream );
354 else if( i_size == 65 ) /* binary format signature */
356 int i_read = stream_Read( p_stream, p_sig, (int)i_size );
357 stream_Delete( p_stream );
358 if( i_read != i_size )
364 char *p_buf = (char*)malloc( i_size );
367 stream_Delete( p_stream );
371 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
373 stream_Delete( p_stream );
375 if( i_read != i_size )
381 int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
391 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
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 )
396 /* the data to be verified (a SHA-1 hash) */
397 const char *hash_sexp_s = "(data(flags raw)(value %m))";
399 const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
401 const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
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;
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 ) )
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 ) )
421 if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, 20, NULL ) ||
422 gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
425 if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
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 );
445 * Return the long id (8 bytes) of the public key used to generate a signature
447 static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
449 uint8_t *p = p_sig->unhashed_data;
450 uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );
454 int i_subpacket_len = *p < 192 ? *p++ :
455 *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
456 ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;
458 if( p >= max_pos - 1 )
461 if( *p == ISSUER_SUBPACKET )
464 p += i_subpacket_len;
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
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 )
477 uint8_t *pos = (uint8_t*) p_key_data;
478 uint8_t *max_pos = pos + i_key_len;
481 #define PUBLIC_KEY_FOUND 0x01
482 #define USER_ID_FOUND 0x02
483 #define SIGNATURE_FOUND 0X04
485 uint8_t *p_key_unarmored = NULL;
487 signature_packet_v4_t sig;
489 p_key->psz_username = NULL;
490 p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;
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 )
497 int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
498 p_key_unarmored, i_key_len );
503 pos = p_key_unarmored;
504 max_pos = pos + i_len;
507 while( pos < max_pos )
509 if( !(*pos & 0x80) || *pos & 0x40 )
512 int i_type = packet_type( *pos );
514 int i_header_len = packet_header_len( *pos++ );
515 if( pos + i_header_len > max_pos )
518 int i_packet_len = scalar_number( pos, i_header_len );
521 if( pos + i_packet_len > max_pos )
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 )
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 )
538 p_issuer = get_issuer_from_signature_v4( &sig );
539 if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
541 memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
542 i_status |= SIGNATURE_FOUND;
546 free( sig.hashed_data );
547 free( sig.unhashed_data );
552 if( p_key->psz_username ) /* save only the first User ID */
554 i_status |= USER_ID_FOUND;
555 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
556 if( !p_key->psz_username )
559 memcpy( p_key->psz_username, pos, i_packet_len );
560 p_key->psz_username[i_packet_len] = '\0';
568 free( p_key_unarmored );
570 if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
573 if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
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 );
587 * return a sha1 hash of a file
589 static uint8_t *hash_sha1_from_file( const char *psz_file,
590 signature_packet_v3_t *p_sig )
592 FILE *f = utf8_fopen( psz_file, "r" );
596 uint8_t buffer[4096];
599 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
606 while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
607 gcry_md_write( hd, buffer, i_read );
609 gcry_md_putc( hd, p_sig->type );
610 gcry_md_write( hd, &p_sig->timestamp, 4 );
615 return( (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1) );
619 * download a public key (the last one) from videolan server, and parse it
621 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
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], p_longid[2], p_longid[3],
626 p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
629 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
634 int64_t i_size = stream_Size( p_stream );
637 stream_Delete( p_stream );
641 uint8_t *p_buf = (uint8_t*)malloc( i_size );
644 stream_Delete( p_stream );
648 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
649 stream_Delete( p_stream );
651 if( i_read != (int)i_size )
657 public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
664 int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
667 if( i_error != VLC_SUCCESS )
677 * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
678 * Note that we need the signature to compute the hash
680 static uint8_t *key_sign_hash( public_key_t *p_pkey )
682 gcry_error_t error = 0;
685 error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
689 gcry_md_putc( hd, 0x99 );
691 gcry_md_putc( hd, (418 >> 8) & 0xff );
692 gcry_md_putc( hd, 418 & 0xff );
694 gcry_md_write( hd, (uint8_t*)&p_pkey->key, 418 );
696 gcry_md_putc( hd, 0xb4 );
698 int i_len = strlen((char*)p_pkey->psz_username);
700 gcry_md_putc( hd, (i_len << 24) & 0xff );
701 gcry_md_putc( hd, (i_len << 16) & 0xff );
702 gcry_md_putc( hd, (i_len << 8) & 0xff );
703 gcry_md_putc( hd, (i_len) & 0xff );
705 gcry_md_write( hd, p_pkey->psz_username, i_len );
707 size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );
709 gcry_md_putc( hd, p_pkey->sig.version );
710 gcry_md_putc( hd, p_pkey->sig.type );
711 gcry_md_putc( hd, p_pkey->sig.public_key_algo );
712 gcry_md_putc( hd, p_pkey->sig.digest_algo );
713 gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
714 gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );
716 gcry_md_putc( hd, 0x04 );
717 gcry_md_putc( hd, 0xff );
719 i_hashed_data_len += 6; /* hashed data + 6 bytes header */
721 gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
722 gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
723 gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
724 gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
728 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
730 if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
731 p_hash[1] != p_pkey->sig.hash_verification[1] )
741 /*****************************************************************************
743 *****************************************************************************/
746 * Create a new update VLC struct
748 * \param p_this the calling vlc_object
749 * \return pointer to new update_t or NULL
751 update_t *__update_New( vlc_object_t *p_this )
756 p_update = (update_t *)malloc( sizeof( update_t ) );
757 if( !p_update ) return NULL;
759 vlc_mutex_init( p_this, &p_update->lock );
761 p_update->p_libvlc = p_this->p_libvlc;
763 p_update->release.psz_url = NULL;
764 p_update->release.psz_desc = NULL;
766 p_update->p_pkey = NULL;
772 * Delete an update_t struct
774 * \param p_update update_t* pointer
777 void update_Delete( update_t *p_update )
781 vlc_mutex_destroy( &p_update->lock );
783 free( p_update->release.psz_url );
784 free( p_update->release.psz_desc );
785 free( p_update->p_pkey );
791 * Empty the release struct
793 * \param p_update update_t* pointer
796 static void EmptyRelease( update_t *p_update )
798 p_update->release.i_major = 0;
799 p_update->release.i_minor = 0;
800 p_update->release.i_revision = 0;
802 FREENULL( p_update->release.psz_url );
803 FREENULL( p_update->release.psz_desc );
807 * Get the update file and parse it
808 * *p_update has to be locked when calling this function
810 * \param p_update pointer to update struct
811 * \return VLC_TRUE if the update is valid and authenticated
813 static vlc_bool_t GetUpdateFile( update_t *p_update )
815 stream_t *p_stream = NULL;
820 char *psz_line = NULL;
821 char *psz_version_line = NULL;
823 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
826 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
827 UPDATE_VLC_STATUS_URL );
831 /* Try to read three lines */
832 if( !( psz_line = stream_ReadLine( p_stream ) ) )
834 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
835 UPDATE_VLC_STATUS_URL );
839 psz_version_line = psz_line;
840 /* first line : version number */
841 p_update->release.extra = 0;
842 switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
845 p_update->release.extra = extra;
847 p_update->release.i_major = i_major;
848 p_update->release.i_minor = i_minor;
849 p_update->release.i_revision = i_revision;
852 msg_Err( p_update->p_libvlc, "Update version false formated" );
856 /* Second line : URL */
857 if( !( psz_line = stream_ReadLine( p_stream ) ) )
859 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
860 UPDATE_VLC_STATUS_URL );
863 p_update->release.psz_url = psz_line;
866 /* Third line : description */
867 if( !( psz_line = stream_ReadLine( p_stream ) ) )
869 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
870 UPDATE_VLC_STATUS_URL );
873 p_update->release.psz_desc = psz_line;
875 stream_Delete( p_stream );
878 /* Now that we know the status is valid, we must download its signature
879 * to authenticate it */
880 signature_packet_v3_t sign;
881 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
882 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
884 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
888 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
890 msg_Err( p_update->p_libvlc, "Invalid signature type" );
894 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
895 if( !p_update->p_pkey )
898 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
899 p_update->p_pkey, NULL ) != VLC_SUCCESS )
901 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
902 FREENULL( p_update->p_pkey );
906 if( memcmp( sign.issuer_longid, videolan_public_key_longid , 8 ) != 0 )
908 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
909 public_key_t *p_new_pkey = download_key(
910 VLC_OBJECT(p_update->p_libvlc),
911 sign.issuer_longid, videolan_public_key_longid );
914 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
915 FREENULL( p_update->p_pkey );
919 uint8_t *p_hash = key_sign_hash( p_new_pkey );
922 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
924 FREENULL( p_update->p_pkey );
928 if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
929 p_new_pkey->sig.r, p_new_pkey->sig.s,
930 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
933 msg_Info( p_update->p_libvlc, "Key authenticated" );
934 free( p_update->p_pkey );
935 p_update->p_pkey = p_new_pkey;
940 msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
946 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
949 gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
950 FREENULL( psz_version_line );
951 if( sign.type == TEXT_SIGNATURE )
952 gcry_md_putc( hd, '\r' );
953 gcry_md_putc( hd, '\n' );
954 gcry_md_write( hd, p_update->release.psz_url,
955 strlen( p_update->release.psz_url ) );
956 if( sign.type == TEXT_SIGNATURE )
957 gcry_md_putc( hd, '\r' );
958 gcry_md_putc( hd, '\n' );
959 gcry_md_write( hd, p_update->release.psz_desc,
960 strlen( p_update->release.psz_desc ) );
961 if( sign.type == TEXT_SIGNATURE )
962 gcry_md_putc( hd, '\r' );
963 gcry_md_putc( hd, '\n' );
965 gcry_md_putc( hd, sign.type );
966 gcry_md_write( hd, &sign.timestamp, 4 );
970 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );
972 if( p_hash[0] != sign.hash_verification[0] ||
973 p_hash[1] != sign.hash_verification[1] )
975 msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
980 if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
981 sign.r, sign.s, &p_update->p_pkey->key, p_hash ) != VLC_SUCCESS )
983 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
989 msg_Info( p_update->p_libvlc, "Status file authenticated" );
996 stream_Delete( p_stream );
997 free( psz_version_line );
1003 * Struct to launch the check in an other thread
1009 void (*pf_callback)( void *, vlc_bool_t );
1011 } update_check_thread_t;
1013 void update_CheckReal( update_check_thread_t *p_uct );
1018 * \param p_update pointer to update struct
1019 * \param pf_callback pointer to a function to call when the update_check is finished
1020 * \param p_data pointer to some datas to give to the callback
1023 void update_Check( update_t *p_update, void (*pf_callback)( void*, vlc_bool_t ), void *p_data )
1027 update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
1028 sizeof( update_check_thread_t ) );
1029 p_uct->p_update = p_update;
1030 p_uct->pf_callback = pf_callback;
1031 p_uct->p_data = p_data;
1033 vlc_thread_create( p_uct, "check for update", update_CheckReal,
1034 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1037 void update_CheckReal( update_check_thread_t *p_uct )
1040 vlc_mutex_lock( &p_uct->p_update->lock );
1042 EmptyRelease( p_uct->p_update );
1043 b_ret = GetUpdateFile( p_uct->p_update );
1044 vlc_mutex_unlock( &p_uct->p_update->lock );
1046 if( p_uct->pf_callback )
1047 (p_uct->pf_callback)( p_uct->p_data, b_ret );
1049 vlc_object_destroy( p_uct );
1053 * Compare two release numbers
1055 * \param p1 first release
1056 * \param p2 second release
1057 * \return UpdateReleaseStatus(Older|Equal|Newer)
1059 static int CompareReleases( const struct update_release_t *p1,
1060 const struct update_release_t *p2 )
1063 d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
1064 - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
1065 + ( p1->extra ) - ( p2->extra );
1068 return UpdateReleaseStatusOlder;
1070 return UpdateReleaseStatusEqual;
1072 return UpdateReleaseStatusNewer;
1076 * Compare a given release's version number to the current VLC's one
1078 * \param p_update structure
1079 * \return UpdateReleaseStatus(Older|Equal|Newer)
1081 int update_CompareReleaseToCurrent( update_t *p_update )
1085 struct update_release_t c;
1087 /* get the current version number */
1088 c.i_major = *PACKAGE_VERSION_MAJOR - '0';
1089 c.i_minor = *PACKAGE_VERSION_MINOR - '0';
1090 c.i_revision = *PACKAGE_VERSION_REVISION - '0';
1091 c.extra = *PACKAGE_VERSION_EXTRA;
1093 return CompareReleases( &p_update->release, &c );
1097 * Convert a long int size in bytes to a string
1099 * \param l_size the size in bytes
1100 * \return the size as a string
1102 static char *size_str( long int l_size )
1104 char *psz_tmp = NULL;
1106 asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
1107 else if( l_size >> 20 )
1108 asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1109 else if( l_size >> 10 )
1110 asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1112 asprintf( &psz_tmp, "%ld B", l_size );
1118 * Struct to launch the download in a thread
1125 } update_download_thread_t;
1127 void update_DownloadReal( update_download_thread_t *p_udt );
1130 * Download the file given in the update_t
1132 * \param p_update structure
1133 * \param dir to store the download file
1136 void update_Download( update_t *p_update, char *psz_destdir )
1140 update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1141 sizeof( update_download_thread_t ) );
1143 p_udt->p_update = p_update;
1144 p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1146 vlc_thread_create( p_udt, "download update", update_DownloadReal,
1147 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1150 void update_DownloadReal( update_download_thread_t *p_udt )
1154 long int l_downloaded = 0;
1156 char *psz_status = NULL;
1157 char *psz_downloaded = NULL;
1158 char *psz_size = NULL;
1159 char *psz_destfile = NULL;
1160 char *psz_tmpdestfile = NULL;
1162 FILE *p_file = NULL;
1163 stream_t *p_stream = NULL;
1164 void* p_buffer = NULL;
1167 update_t *p_update = p_udt->p_update;
1168 char *psz_destdir = p_udt->psz_destdir;
1170 /* Open the stream */
1171 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1174 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1178 /* Get the stream size */
1179 l_size = stream_Size( p_stream );
1181 /* Get the file name and open it*/
1182 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1183 if( !psz_tmpdestfile )
1185 msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1189 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1192 p_file = utf8_fopen( psz_destfile, "w" );
1195 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1199 /* Create a buffer and fill it with the downloaded file */
1200 p_buffer = (void *)malloc( 1 << 10 );
1204 psz_size = size_str( l_size );
1205 if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done", p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1207 i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1211 while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1212 !intf_ProgressIsCancelled( p_udt, i_progress ) )
1214 fwrite( p_buffer, i_read, 1, p_file );
1216 l_downloaded += i_read;
1217 psz_downloaded = size_str( l_downloaded );
1218 f_progress = 100.0*(float)l_downloaded/(float)l_size;
1220 if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done", p_update->release.psz_url,
1221 psz_downloaded, psz_size, f_progress ) != -1 )
1223 intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1226 free( psz_downloaded );
1229 /* Finish the progress bar or delete the file if the user had canceled */
1233 if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
1235 if( asprintf( &psz_status, "%s\nDone %s (100.0%%)", p_update->release.psz_url, psz_size ) != -1 )
1237 intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1243 utf8_unlink( psz_destfile );
1247 signature_packet_v3_t sign;
1248 if( download_signature( VLC_OBJECT( p_udt ), &sign,
1249 p_update->release.psz_url ) != VLC_SUCCESS )
1251 msg_Err( p_udt, "Couldn't download signature of status file" );
1255 if( sign.type != BINARY_SIGNATURE )
1257 msg_Err( p_udt, "Invalid signature type" );
1261 uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
1264 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
1265 utf8_unlink( psz_destfile );
1269 if( p_hash[0] != sign.hash_verification[0] ||
1270 p_hash[1] != sign.hash_verification[1] )
1272 msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
1273 utf8_unlink( psz_destfile );
1277 if( verify_signature( VLC_OBJECT(p_udt), sign.r, sign.s,
1278 &p_update->p_pkey->key, p_hash ) != VLC_SUCCESS )
1280 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
1282 utf8_unlink( psz_destfile );
1286 msg_Info( p_udt, "%s authenticated", psz_destfile );
1291 stream_Delete( p_stream );
1294 free( psz_destdir );
1295 free( psz_destfile );
1299 vlc_object_destroy( p_udt );