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>
45 #include <unistd.h> /* unlink() */
47 /*****************************************************************************
49 *****************************************************************************/
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)
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"
71 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
73 #elif defined( SYS_BEOS )
74 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
76 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
80 /*****************************************************************************
82 *****************************************************************************/
83 static void EmptyRelease( update_t *p_update );
84 static vlc_bool_t 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 );
90 /*****************************************************************************
92 *****************************************************************************/
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 */
97 static inline int scalar_number( uint8_t *p, int header_len )
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] );
109 /* number of data bytes in a MPI */
110 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
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
116 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
117 size_t i_packet_len )
119 if( i_packet_len != 418 )
122 p_key->version = *p_buf++;
123 if( p_key->version != 4 )
126 /* warn when timestamp is > date ? */
127 memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;
129 p_key->algo = *p_buf++;
130 if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
133 memcpy( p_key->p, p_buf, 2+128 ); p_buf += 2+128;
134 if( mpi_len( p_key->p ) != 128 )
137 memcpy( p_key->q, p_buf, 2+20 ); p_buf += 2+20;
138 if( mpi_len( p_key->q ) != 20 )
141 memcpy( p_key->g, p_buf, 2+128 ); p_buf += 2+128;
142 if( mpi_len( p_key->g ) != 128 )
145 memcpy( p_key->y, p_buf, 2+128 ); p_buf += 2+128;
146 if( mpi_len( p_key->y ) != 128 )
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
156 static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
157 uint8_t *p_buf, size_t i_sig_len )
162 p_sig->version = *p_buf++;
163 if( p_sig->version != 4 )
166 p_sig->type = *p_buf++;
167 if( p_sig->type < GENERIC_KEY_SIGNATURE ||
168 p_sig->type > POSITIVE_KEY_SIGNATURE )
171 p_sig->public_key_algo = *p_buf++;
172 if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
175 p_sig->digest_algo = *p_buf++;
176 if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
179 memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;
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) */
189 p_sig->hashed_data = (uint8_t*) malloc( i_hashed_data_len );
190 if( !p_sig->hashed_data )
192 memcpy( p_sig->hashed_data, p_buf, i_hashed_data_len );
193 p_buf += i_hashed_data_len;
195 memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;
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 )
201 free( p_sig->hashed_data );
205 p_sig->unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
206 if( !p_sig->unhashed_data )
208 free( p_sig->hashed_data );
211 memcpy( p_sig->unhashed_data, p_buf, i_unhashed_data_len );
212 p_buf += i_unhashed_data_len;
214 memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
216 memcpy( p_sig->r, p_buf, 22 ); p_buf += 22;
217 if( mpi_len( p_sig->r ) != 20 )
219 free( p_sig->hashed_data );
220 free( p_sig->unhashed_data );
224 memcpy( p_sig->s, p_buf, 22 );
225 if( mpi_len( p_sig->s ) != 20 )
227 free( p_sig->hashed_data );
228 free( p_sig->unhashed_data );
236 * crc_octets() was lamely copied from rfc 2440
237 * Copyright (C) The Internet Society (1998). All Rights Reserved.
239 #define CRC24_INIT 0xB704CEL
240 #define CRC24_POLY 0x1864CFBL
242 static long crc_octets( uint8_t *octets, size_t len )
244 long crc = CRC24_INIT;
248 crc ^= (*octets++) << 16;
249 for (i = 0; i < 8; i++)
256 return crc & 0xFFFFFFL;
260 * Transform an armored document in binary format
261 * Used on public keys and signatures
263 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
264 uint8_t *p_obuf, size_t i_obuf_len )
266 char *p_ipos = p_ibuf;
267 uint8_t *p_opos = p_obuf;
270 int i_header_skipped = 0;
272 while( !i_end && p_ipos < p_ibuf + i_ibuf_len )
274 if( *p_ipos == '\r' || *p_ipos == '\n' )
280 size_t i_line_len = strcspn( p_ipos, "\r\n" );
281 if( i_line_len == 0 )
284 if( !i_header_skipped )
286 if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
287 i_header_skipped = 1;
289 p_ipos += i_line_len + 1;
293 if( !strncmp( p_ipos, "Version:", 8 ) )
295 p_ipos += i_line_len + 1;
299 if( p_ipos[i_line_len - 1] == '=' )
302 p_ipos[i_line_len - 1] = '\0';
305 p_ipos[i_line_len] = '\0';
307 p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
308 p_obuf - p_opos + i_obuf_len,
311 p_ipos += i_line_len + 1;
314 /* XXX: the CRC is OPTIONAL, really require it ? */
315 if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
319 if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
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];
325 return l_crc2 == l_crc ? p_opos - p_obuf : 0;
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
332 static int download_signature( vlc_object_t *p_this,
333 signature_packet_v3_t *p_sig,
334 const char *psz_url )
336 char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
340 strcpy( psz_sig, psz_url );
341 strcat( psz_sig, ".asc" );
343 stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
349 int64_t i_size = stream_Size( p_stream );
352 stream_Delete( p_stream );
355 else if( i_size == 65 ) /* binary format signature */
357 int i_read = stream_Read( p_stream, p_sig, (int)i_size );
358 stream_Delete( p_stream );
359 if( i_read != i_size )
365 char *p_buf = (char*)malloc( i_size );
368 stream_Delete( p_stream );
372 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
374 stream_Delete( p_stream );
376 if( i_read != i_size )
382 int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
392 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
394 static int verify_signature( vlc_object_t *p_this, uint8_t *p_r, uint8_t *p_s,
395 public_key_packet_t *p_key, uint8_t *p_hash )
397 /* the data to be verified (a SHA-1 hash) */
398 const char *hash_sexp_s = "(data(flags raw)(value %m))";
400 const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
402 const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
405 gcry_mpi_t p, q, g, y, r, s, hash;
406 p = q = g = y = r = s = hash = NULL;
407 gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
408 key_sexp = hash_sexp = sig_sexp = NULL;
410 if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, 128, NULL ) ||
411 gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, 20, NULL ) ||
412 gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, 128, NULL ) ||
413 gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, 128, NULL ) ||
414 gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
417 if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, 20, NULL ) ||
418 gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, 20, NULL ) ||
419 gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
422 if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, 20, NULL ) ||
423 gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
426 if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
432 if( p ) gcry_mpi_release( p );
433 if( q ) gcry_mpi_release( q );
434 if( g ) gcry_mpi_release( g );
435 if( y ) gcry_mpi_release( y );
436 if( r ) gcry_mpi_release( r );
437 if( s ) gcry_mpi_release( s );
438 if( hash ) gcry_mpi_release( hash );
439 if( key_sexp ) gcry_sexp_release( key_sexp );
440 if( sig_sexp ) gcry_sexp_release( sig_sexp );
441 if( hash_sexp ) gcry_sexp_release( hash_sexp );
446 * Return the long id (8 bytes) of the public key used to generate a signature
448 static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
450 uint8_t *p = p_sig->unhashed_data;
451 uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );
455 int i_subpacket_len = *p < 192 ? *p++ :
456 *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
457 ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;
459 if( p >= max_pos - 1 )
462 if( *p == ISSUER_SUBPACKET )
465 p += i_subpacket_len;
471 * fill a public_key_t with public key data, including:
472 * * public key packet
473 * * signature packet issued by key which long id is p_sig_issuer
476 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 )
478 uint8_t *pos = (uint8_t*) p_key_data;
479 uint8_t *max_pos = pos + i_key_len;
482 #define PUBLIC_KEY_FOUND 0x01
483 #define USER_ID_FOUND 0x02
484 #define SIGNATURE_FOUND 0X04
486 uint8_t *p_key_unarmored = NULL;
488 signature_packet_v4_t sig;
490 p_key->psz_username = NULL;
491 p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;
493 if( !( *pos & 0x80 ) )
494 { /* first byte is ASCII, unarmoring */
495 p_key_unarmored = (uint8_t*)malloc( i_key_len );
496 if( !p_key_unarmored )
498 int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
499 p_key_unarmored, i_key_len );
504 pos = p_key_unarmored;
505 max_pos = pos + i_len;
508 while( pos < max_pos )
510 if( !(*pos & 0x80) || *pos & 0x40 )
513 int i_type = packet_type( *pos );
515 int i_header_len = packet_header_len( *pos++ );
516 if( pos + i_header_len > max_pos )
519 int i_packet_len = scalar_number( pos, i_header_len );
522 if( pos + i_packet_len > max_pos )
529 case PUBLIC_KEY_PACKET:
530 i_status |= PUBLIC_KEY_FOUND;
531 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
535 case SIGNATURE_PACKET:
536 if( !p_sig_issuer || i_status & SIGNATURE_FOUND ||
537 parse_signature_v4_packet( &sig, pos, i_packet_len ) != VLC_SUCCESS )
539 p_issuer = get_issuer_from_signature_v4( &sig );
540 if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
542 memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
543 i_status |= SIGNATURE_FOUND;
547 free( sig.hashed_data );
548 free( sig.unhashed_data );
553 if( p_key->psz_username ) /* save only the first User ID */
555 i_status |= USER_ID_FOUND;
556 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
557 if( !p_key->psz_username )
560 memcpy( p_key->psz_username, pos, i_packet_len );
561 p_key->psz_username[i_packet_len] = '\0';
569 free( p_key_unarmored );
571 if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
574 if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
580 free( p_key->sig.hashed_data );
581 free( p_key->sig.unhashed_data );
582 free( p_key->psz_username );
583 free( p_key_unarmored );
588 * return a sha1 hash of a file
590 static uint8_t *hash_sha1_from_file( const char *psz_file,
591 signature_packet_v3_t *p_sig )
593 FILE *f = utf8_fopen( psz_file, "r" );
597 uint8_t buffer[4096];
600 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
607 while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
608 gcry_md_write( hd, buffer, i_read );
610 gcry_md_putc( hd, p_sig->type );
611 gcry_md_write( hd, &p_sig->timestamp, 4 );
616 return( (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1) );
620 * download a public key (the last one) from videolan server, and parse it
622 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
625 if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
626 p_longid[0], p_longid[1], p_longid[2], p_longid[3],
627 p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
630 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
635 int64_t i_size = stream_Size( p_stream );
638 stream_Delete( p_stream );
642 uint8_t *p_buf = (uint8_t*)malloc( i_size );
645 stream_Delete( p_stream );
649 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
650 stream_Delete( p_stream );
652 if( i_read != (int)i_size )
658 public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
665 int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
668 if( i_error != VLC_SUCCESS )
678 * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
679 * Note that we need the signature to compute the hash
681 static uint8_t *key_sign_hash( public_key_t *p_pkey )
683 gcry_error_t error = 0;
686 error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
690 gcry_md_putc( hd, 0x99 );
692 gcry_md_putc( hd, (418 >> 8) & 0xff );
693 gcry_md_putc( hd, 418 & 0xff );
695 gcry_md_write( hd, (uint8_t*)&p_pkey->key, 418 );
697 gcry_md_putc( hd, 0xb4 );
699 int i_len = strlen((char*)p_pkey->psz_username);
701 gcry_md_putc( hd, (i_len << 24) & 0xff );
702 gcry_md_putc( hd, (i_len << 16) & 0xff );
703 gcry_md_putc( hd, (i_len << 8) & 0xff );
704 gcry_md_putc( hd, (i_len) & 0xff );
706 gcry_md_write( hd, p_pkey->psz_username, i_len );
708 size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );
710 gcry_md_putc( hd, p_pkey->sig.version );
711 gcry_md_putc( hd, p_pkey->sig.type );
712 gcry_md_putc( hd, p_pkey->sig.public_key_algo );
713 gcry_md_putc( hd, p_pkey->sig.digest_algo );
714 gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
715 gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );
717 gcry_md_putc( hd, 0x04 );
718 gcry_md_putc( hd, 0xff );
720 i_hashed_data_len += 6; /* hashed data + 6 bytes header */
722 gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
723 gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
724 gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
725 gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
729 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
731 if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
732 p_hash[1] != p_pkey->sig.hash_verification[1] )
742 /*****************************************************************************
744 *****************************************************************************/
747 * Create a new update VLC struct
749 * \param p_this the calling vlc_object
750 * \return pointer to new update_t or NULL
752 update_t *__update_New( vlc_object_t *p_this )
757 p_update = (update_t *)malloc( sizeof( update_t ) );
758 if( !p_update ) return NULL;
760 vlc_mutex_init( p_this, &p_update->lock );
762 p_update->p_libvlc = p_this->p_libvlc;
764 p_update->release.psz_url = NULL;
765 p_update->release.psz_desc = NULL;
767 p_update->p_pkey = NULL;
773 * Delete an update_t struct
775 * \param p_update update_t* pointer
778 void update_Delete( update_t *p_update )
782 vlc_mutex_destroy( &p_update->lock );
784 free( p_update->release.psz_url );
785 free( p_update->release.psz_desc );
786 free( p_update->p_pkey );
792 * Empty the release struct
794 * \param p_update update_t* pointer
797 static void EmptyRelease( update_t *p_update )
799 p_update->release.i_major = 0;
800 p_update->release.i_minor = 0;
801 p_update->release.i_revision = 0;
803 FREENULL( p_update->release.psz_url );
804 FREENULL( p_update->release.psz_desc );
808 * Get the update file and parse it
809 * *p_update has to be locked when calling this function
811 * \param p_update pointer to update struct
812 * \return VLC_TRUE if the update is valid and authenticated
814 static vlc_bool_t GetUpdateFile( update_t *p_update )
816 stream_t *p_stream = NULL;
821 char *psz_line = NULL;
822 char *psz_version_line = NULL;
824 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
827 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
828 UPDATE_VLC_STATUS_URL );
832 /* Try to read three lines */
833 if( !( psz_line = stream_ReadLine( p_stream ) ) )
835 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
836 UPDATE_VLC_STATUS_URL );
840 psz_version_line = psz_line;
841 /* first line : version number */
842 p_update->release.extra = 0;
843 switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
846 p_update->release.extra = extra;
848 p_update->release.i_major = i_major;
849 p_update->release.i_minor = i_minor;
850 p_update->release.i_revision = i_revision;
853 msg_Err( p_update->p_libvlc, "Update version false formated" );
857 /* Second line : URL */
858 if( !( psz_line = stream_ReadLine( p_stream ) ) )
860 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
861 UPDATE_VLC_STATUS_URL );
864 p_update->release.psz_url = psz_line;
867 /* Third line : description */
868 if( !( psz_line = stream_ReadLine( p_stream ) ) )
870 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
871 UPDATE_VLC_STATUS_URL );
874 p_update->release.psz_desc = psz_line;
876 stream_Delete( p_stream );
879 /* Now that we know the status is valid, we must download its signature
880 * to authenticate it */
881 signature_packet_v3_t sign;
882 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
883 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
885 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
889 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
891 msg_Err( p_update->p_libvlc, "Invalid signature type" );
895 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
896 if( !p_update->p_pkey )
899 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
900 p_update->p_pkey, NULL ) != VLC_SUCCESS )
902 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
903 FREENULL( p_update->p_pkey );
907 if( memcmp( sign.issuer_longid, videolan_public_key_longid , 8 ) != 0 )
909 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
910 public_key_t *p_new_pkey = download_key(
911 VLC_OBJECT(p_update->p_libvlc),
912 sign.issuer_longid, videolan_public_key_longid );
915 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
916 FREENULL( p_update->p_pkey );
920 uint8_t *p_hash = key_sign_hash( p_new_pkey );
923 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
925 FREENULL( p_update->p_pkey );
929 if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
930 p_new_pkey->sig.r, p_new_pkey->sig.s,
931 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
934 msg_Info( p_update->p_libvlc, "Key authenticated" );
935 free( p_update->p_pkey );
936 p_update->p_pkey = p_new_pkey;
941 msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
947 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
950 gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
951 FREENULL( psz_version_line );
952 if( sign.type == TEXT_SIGNATURE )
953 gcry_md_putc( hd, '\r' );
954 gcry_md_putc( hd, '\n' );
955 gcry_md_write( hd, p_update->release.psz_url,
956 strlen( p_update->release.psz_url ) );
957 if( sign.type == TEXT_SIGNATURE )
958 gcry_md_putc( hd, '\r' );
959 gcry_md_putc( hd, '\n' );
960 gcry_md_write( hd, p_update->release.psz_desc,
961 strlen( p_update->release.psz_desc ) );
962 if( sign.type == TEXT_SIGNATURE )
963 gcry_md_putc( hd, '\r' );
964 gcry_md_putc( hd, '\n' );
966 gcry_md_putc( hd, sign.type );
967 gcry_md_write( hd, &sign.timestamp, 4 );
971 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );
973 if( p_hash[0] != sign.hash_verification[0] ||
974 p_hash[1] != sign.hash_verification[1] )
976 msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
981 if( verify_signature( VLC_OBJECT(p_update->p_libvlc),
982 sign.r, sign.s, &p_update->p_pkey->key, p_hash ) != VLC_SUCCESS )
984 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
990 msg_Info( p_update->p_libvlc, "Status file authenticated" );
997 stream_Delete( p_stream );
998 free( psz_version_line );
1004 * Struct to launch the check in an other thread
1010 void (*pf_callback)( void * );
1012 } update_check_thread_t;
1014 void update_CheckReal( update_check_thread_t *p_uct );
1019 * \param p_update pointer to update struct
1020 * \param pf_callback pointer to a function to call when the update_check is finished
1021 * \param p_data pointer to some datas to give to the callback
1024 void update_Check( update_t *p_update, void (*pf_callback)( void* ), void *p_data )
1028 update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
1029 sizeof( update_check_thread_t ) );
1030 p_uct->p_update = p_update;
1031 p_uct->pf_callback = pf_callback;
1032 p_uct->p_data = p_data;
1034 vlc_thread_create( p_uct, "check for update", update_CheckReal,
1035 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1038 void update_CheckReal( update_check_thread_t *p_uct )
1041 vlc_mutex_lock( &p_uct->p_update->lock );
1043 EmptyRelease( p_uct->p_update );
1044 b_ret = GetUpdateFile( p_uct->p_update );
1045 vlc_mutex_unlock( &p_uct->p_update->lock );
1047 /* FIXME: return b_ret in pf_callback */
1048 if( b_ret && p_uct->pf_callback )
1049 (p_uct->pf_callback)( p_uct->p_data );
1051 vlc_object_destroy( p_uct );
1055 * Compare two release numbers
1057 * \param p1 first release
1058 * \param p2 second release
1059 * \return UpdateReleaseStatus(Older|Equal|Newer)
1061 static int CompareReleases( const struct update_release_t *p1,
1062 const struct update_release_t *p2 )
1065 d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
1066 - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
1067 + ( p1->extra ) - ( p2->extra );
1070 return UpdateReleaseStatusOlder;
1072 return UpdateReleaseStatusEqual;
1074 return UpdateReleaseStatusNewer;
1078 * Compare a given release's version number to the current VLC's one
1080 * \param p_update structure
1081 * \return UpdateReleaseStatus(Older|Equal|Newer)
1083 int update_CompareReleaseToCurrent( update_t *p_update )
1087 struct update_release_t c;
1089 /* get the current version number */
1090 c.i_major = *PACKAGE_VERSION_MAJOR - '0';
1091 c.i_minor = *PACKAGE_VERSION_MINOR - '0';
1092 c.i_revision = *PACKAGE_VERSION_REVISION - '0';
1093 c.extra = *PACKAGE_VERSION_EXTRA;
1095 return CompareReleases( &p_update->release, &c );
1099 * Convert a long int size in bytes to a string
1101 * \param l_size the size in bytes
1102 * \return the size as a string
1104 static char *size_str( long int l_size )
1106 char *psz_tmp = NULL;
1108 asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
1109 else if( l_size >> 20 )
1110 asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1111 else if( l_size >> 10 )
1112 asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1114 asprintf( &psz_tmp, "%ld B", l_size );
1120 * Struct to launch the download in a thread
1127 } update_download_thread_t;
1129 void update_DownloadReal( update_download_thread_t *p_udt );
1132 * Download the file given in the update_t
1134 * \param p_update structure
1135 * \param dir to store the download file
1138 void update_Download( update_t *p_update, char *psz_destdir )
1142 update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1143 sizeof( update_download_thread_t ) );
1145 p_udt->p_update = p_update;
1146 p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1148 vlc_thread_create( p_udt, "download update", update_DownloadReal,
1149 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1152 void update_DownloadReal( update_download_thread_t *p_udt )
1156 long int l_downloaded = 0;
1158 char *psz_status = NULL;
1159 char *psz_downloaded = NULL;
1160 char *psz_size = NULL;
1161 char *psz_destfile = NULL;
1162 char *psz_tmpdestfile = NULL;
1164 FILE *p_file = NULL;
1165 stream_t *p_stream = NULL;
1166 void* p_buffer = NULL;
1169 update_t *p_update = p_udt->p_update;
1170 char *psz_destdir = p_udt->psz_destdir;
1172 /* Open the stream */
1173 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1176 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1180 /* Get the stream size */
1181 l_size = stream_Size( p_stream );
1183 /* Get the file name and open it*/
1184 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1185 if( !psz_tmpdestfile )
1187 msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1191 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1194 p_file = utf8_fopen( psz_destfile, "w" );
1197 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1201 /* Create a buffer and fill it with the downloaded file */
1202 p_buffer = (void *)malloc( 1 << 10 );
1206 psz_size = size_str( l_size );
1207 if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done", p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1209 i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1213 while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1214 !intf_ProgressIsCancelled( p_udt, i_progress ) )
1216 fwrite( p_buffer, i_read, 1, p_file );
1218 l_downloaded += i_read;
1219 psz_downloaded = size_str( l_downloaded );
1220 f_progress = 100.0*(float)l_downloaded/(float)l_size;
1222 if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done", p_update->release.psz_url,
1223 psz_downloaded, psz_size, f_progress ) != -1 )
1225 intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1228 free( psz_downloaded );
1231 /* Finish the progress bar or delete the file if the user had canceled */
1235 if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
1237 if( asprintf( &psz_status, "%s\nDone %s (100.0%%)", p_update->release.psz_url, psz_size ) != -1 )
1239 intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1245 unlink( psz_destfile ); /* FIXME: use (and write) utf8_unlink() ? */
1249 signature_packet_v3_t sign;
1250 if( download_signature( VLC_OBJECT( p_udt ), &sign,
1251 p_update->release.psz_url ) != VLC_SUCCESS )
1253 msg_Err( p_udt, "Couldn't download signature of status file" );
1257 if( sign.type != BINARY_SIGNATURE )
1259 msg_Err( p_udt, "Invalid signature type" );
1263 uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
1266 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
1267 unlink( psz_destfile );
1271 if( p_hash[0] != sign.hash_verification[0] ||
1272 p_hash[1] != sign.hash_verification[1] )
1274 msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
1275 unlink( psz_destfile );
1279 if( verify_signature( VLC_OBJECT(p_udt), sign.r, sign.s,
1280 &p_update->p_pkey->key, p_hash ) != VLC_SUCCESS )
1282 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
1284 unlink( psz_destfile );
1288 msg_Info( p_udt, "%s authenticated", psz_destfile );
1293 stream_Delete( p_stream );
1296 free( psz_destdir );
1297 free( psz_destfile );
1301 vlc_object_destroy( p_udt );