1 /*****************************************************************************
2 * update.c: VLC update checking and downloading
3 *****************************************************************************
4 * Copyright © 2005-2008 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 update management
31 /*****************************************************************************
33 *****************************************************************************/
45 #include <vlc_update.h>
46 #include <vlc_pgpkey.h>
47 #include <vlc_stream.h>
48 #include <vlc_strings.h>
49 #include <vlc_charset.h>
50 #include <vlc_interface.h>
53 #include <vlc_gcrypt.h>
56 /*****************************************************************************
58 *****************************************************************************/
61 * Here is the format of these "status files" :
62 * First line is the last version: "X.Y.Ze" where:
63 * * X is the major number
64 * * Y is the minor number
65 * * Z is the revision number
66 * * e is an OPTIONAL extra letter
67 * * AKA "0.8.6d" or "0.9.0"
68 * Second line is an url of the binary for this last version
69 * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
72 #if defined( UNDER_CE )
73 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
74 #elif defined( WIN32 )
75 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
76 #elif defined( __APPLE__ )
77 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
78 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
80 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
82 #elif defined( SYS_BEOS )
83 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
85 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
89 /*****************************************************************************
91 *****************************************************************************/
92 static void EmptyRelease( update_t *p_update );
93 static bool GetUpdateFile( update_t *p_update );
94 static char * size_str( long int l_size );
97 /*****************************************************************************
99 *****************************************************************************/
101 #define packet_type( c ) ( ( c & 0x3c ) >> 2 ) /* 0x3C = 00111100 */
102 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
104 static inline int scalar_number( uint8_t *p, int header_len )
106 assert( header_len == 1 || header_len == 2 || header_len == 4 );
108 if( header_len == 1 )
110 else if( header_len == 2 )
111 return( (p[0] << 8) + p[1] );
112 else if( header_len == 4 )
113 return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
115 abort(); /* to shut up GCC warning */
118 /* number of data bytes in a MPI */
119 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
122 * fill a public_key_packet_t structure from public key packet data
123 * verify that it is a version 4 public key packet, using DSA
125 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
126 size_t i_packet_len )
129 if( i_packet_len > 418 || i_packet_len < 6 )
134 p_key->version = *p_buf++; i_read++;
135 if( p_key->version != 4 )
138 /* XXX: warn when timestamp is > date ? */
139 memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4; i_read += 4;
141 p_key->algo = *p_buf++; i_read++;
142 if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
146 if( i_read + 2 > i_packet_len )
149 int i_p_len = mpi_len( p_buf );
151 if( i_p_len > 128 || i_read + 2 + i_p_len > i_packet_len )
154 memcpy( p_key->p, p_buf, 2+i_p_len );
155 p_buf += 2+i_p_len; i_read += 2+i_p_len;
158 if( i_read + 2 > i_packet_len )
161 int i_q_len = mpi_len( p_buf );
163 if( i_q_len > 20 || i_read+2+i_q_len > i_packet_len )
166 memcpy( p_key->q, p_buf, 2+i_q_len );
167 p_buf += 2+i_q_len; i_read += 2+i_q_len;
170 if( i_read + 2 > i_packet_len )
173 int i_g_len = mpi_len( p_buf );
175 if( i_g_len > 128 || i_read+2+i_g_len > i_packet_len )
178 memcpy( p_key->g, p_buf, 2+i_g_len );
179 p_buf += 2+i_g_len; i_read += 2+i_g_len;
182 if( i_read + 2 > i_packet_len )
185 int i_y_len = mpi_len( p_buf );
188 if( i_y_len > 128 || i_read+2+i_y_len > i_packet_len )
191 memcpy( p_key->y, p_buf, 2+i_y_len );
194 if( i_read != i_packet_len ) /* some extra data eh ? */
200 static size_t parse_signature_v3_packet( signature_packet_t *p_sig,
201 uint8_t *p_buf, size_t i_sig_len )
203 size_t i_read = 1; /* we already read the version byte */
205 if( i_sig_len < 19 ) /* signature is at least 19 bytes + the 2 MPIs */
208 p_sig->specific.v3.hashed_data_len = *p_buf++; i_read++;
209 if( p_sig->specific.v3.hashed_data_len != 5 )
212 p_sig->type = *p_buf++; i_read++;
214 memcpy( p_sig->specific.v3.timestamp, p_buf, 4 );
215 p_buf += 4; i_read += 4;
217 memcpy( p_sig->issuer_longid, p_buf, 8 );
218 p_buf += 8; i_read += 8;
220 p_sig->public_key_algo = *p_buf++; i_read++;
222 p_sig->digest_algo = *p_buf++; i_read++;
224 p_sig->hash_verification[0] = *p_buf++; i_read++;
225 p_sig->hash_verification[1] = *p_buf++; i_read++;
227 assert( i_read == 19 );
233 * fill a signature_packet_v4_t from signature packet data
234 * verify that it was used with a DSA public key, using SHA-1 digest
236 static size_t parse_signature_v4_packet( signature_packet_t *p_sig,
237 uint8_t *p_buf, size_t i_sig_len )
239 size_t i_read = 1; /* we already read the version byte */
241 if( i_sig_len < 10 ) /* signature is at least 10 bytes + the 2 MPIs */
244 p_sig->type = *p_buf++; i_read++;
246 p_sig->public_key_algo = *p_buf++; i_read++;
248 p_sig->digest_algo = *p_buf++; i_read++;
250 memcpy( p_sig->specific.v4.hashed_data_len, p_buf, 2 );
251 p_buf += 2; i_read += 2;
253 size_t i_hashed_data_len =
254 scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
255 i_read += i_hashed_data_len;
256 if( i_read + 4 > i_sig_len )
259 p_sig->specific.v4.hashed_data = (uint8_t*) malloc( i_hashed_data_len );
260 if( !p_sig->specific.v4.hashed_data )
262 memcpy( p_sig->specific.v4.hashed_data, p_buf, i_hashed_data_len );
263 p_buf += i_hashed_data_len;
265 memcpy( p_sig->specific.v4.unhashed_data_len, p_buf, 2 );
266 p_buf += 2; i_read += 2;
268 size_t i_unhashed_data_len =
269 scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
270 i_read += i_unhashed_data_len;
271 if( i_read + 2 > i_sig_len )
274 p_sig->specific.v4.unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
275 if( !p_sig->specific.v4.unhashed_data )
278 memcpy( p_sig->specific.v4.unhashed_data, p_buf, i_unhashed_data_len );
279 p_buf += i_unhashed_data_len;
281 memcpy( p_sig->hash_verification, p_buf, 2 );
282 p_buf += 2; i_read += 2;
284 uint8_t *p, *max_pos;
285 p = p_sig->specific.v4.unhashed_data;
286 max_pos = p + scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
293 size_t i_subpacket_len;
296 if( p + 1 > max_pos )
298 i_subpacket_len = *p++;
302 if( p + 2 > max_pos )
304 i_subpacket_len = (*p++ - 192) << 8;
305 i_subpacket_len += *p++ + 192;
309 if( p + 4 > max_pos )
311 i_subpacket_len = *++p << 24;
312 i_subpacket_len += *++p << 16;
313 i_subpacket_len += *++p << 8;
314 i_subpacket_len += *++p;
317 if( *p == ISSUER_SUBPACKET )
319 if( p + 9 > max_pos )
322 memcpy( &p_sig->issuer_longid, p+1, 8 );
327 p += i_subpacket_len;
331 static int parse_signature_packet( signature_packet_t *p_sig,
332 uint8_t *p_buf, size_t i_sig_len )
334 if( !i_sig_len ) /* 1st sanity check, we need at least the version */
337 p_sig->version = *p_buf++;
340 switch( p_sig->version )
343 i_read = parse_signature_v3_packet( p_sig, p_buf, i_sig_len );
346 p_sig->specific.v4.hashed_data = NULL;
347 p_sig->specific.v4.unhashed_data = NULL;
348 i_read = parse_signature_v4_packet( p_sig, p_buf, i_sig_len );
354 if( i_read == 0 ) /* signature packet parsing has failed */
357 if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
360 if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
363 switch( p_sig->type )
365 case BINARY_SIGNATURE:
367 case GENERIC_KEY_SIGNATURE:
368 case PERSONA_KEY_SIGNATURE:
369 case CASUAL_KEY_SIGNATURE:
370 case POSITIVE_KEY_SIGNATURE:
376 p_buf--; /* rewind to the version byte */
379 if( i_read + 2 > i_sig_len )
382 size_t i_r_len = mpi_len( p_buf ); i_read += 2;
383 if( i_read + i_r_len > i_sig_len || i_r_len > 20 )
386 memcpy( p_sig->r, p_buf, 2 + i_r_len );
387 p_buf += 2 + i_r_len;
390 if( i_read + 2 > i_sig_len )
393 size_t i_s_len = mpi_len( p_buf ); i_read += 2;
394 if( i_read + i_s_len > i_sig_len || i_s_len > 20 )
397 memcpy( p_sig->s, p_buf, 2 + i_s_len );
398 p_buf += 2 + i_s_len;
401 assert( i_read == i_sig_len );
402 if( i_read < i_sig_len ) /* some extra data, hm ? */
409 if( p_sig->version == 4 )
411 free( p_sig->specific.v4.hashed_data );
412 free( p_sig->specific.v4.unhashed_data );
419 * crc_octets() was lamely copied from rfc 2440
420 * Copyright (C) The Internet Society (1998). All Rights Reserved.
422 #define CRC24_INIT 0xB704CEL
423 #define CRC24_POLY 0x1864CFBL
425 static long crc_octets( uint8_t *octets, size_t len )
427 long crc = CRC24_INIT;
431 crc ^= (*octets++) << 16;
432 for (i = 0; i < 8; i++)
439 return crc & 0xFFFFFFL;
443 * Transform an armored document in binary format
444 * Used on public keys and signatures
446 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
447 uint8_t *p_obuf, size_t i_obuf_len )
449 char *p_ipos = p_ibuf;
450 uint8_t *p_opos = p_obuf;
452 int i_header_skipped = 0;
454 while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
456 if( *p_ipos == '\r' || *p_ipos == '\n' )
462 size_t i_line_len = strcspn( p_ipos, "\r\n" );
463 if( i_line_len == 0 )
466 if( !i_header_skipped )
468 if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
469 i_header_skipped = 1;
471 p_ipos += i_line_len + 1;
475 if( !strncmp( p_ipos, "Version:", 8 ) )
477 p_ipos += i_line_len + 1;
481 if( p_ipos[i_line_len - 1] == '=' )
484 p_ipos[i_line_len - 1] = '\0';
487 p_ipos[i_line_len] = '\0';
489 p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
490 p_obuf - p_opos + i_obuf_len, p_ipos );
491 p_ipos += i_line_len + 1;
494 /* XXX: the CRC is OPTIONAL, really require it ? */
495 if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
499 if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
502 long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
503 long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
505 return l_crc2 == l_crc ? p_opos - p_obuf : 0;
509 * Download the signature associated to a document or a binary file.
510 * We're given the file's url, we just append ".asc" to it and download
512 static int download_signature( vlc_object_t *p_this,
513 signature_packet_t *p_sig,
514 const char *psz_url )
516 char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
520 strcpy( psz_sig, psz_url );
521 strcat( psz_sig, ".asc" );
523 stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
529 int64_t i_size = stream_Size( p_stream );
531 msg_Dbg( p_this, "Downloading signature (%"PRId64" bytes)", i_size );
532 uint8_t *p_buf = (uint8_t*)malloc( i_size );
535 stream_Delete( p_stream );
539 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
541 stream_Delete( p_stream );
543 if( i_read != (int)i_size )
546 "Couldn't download full signature (only %d bytes)", i_read );
551 if( (uint8_t)*p_buf < 0x80 ) /* ASCII */
553 msg_Dbg( p_this, "Unarmoring signature" );
555 uint8_t* p_unarmored = (uint8_t*) malloc( ( i_size * 3 ) / 4 + 1 );
562 int i_bytes = pgp_unarmor( (char*)p_buf, i_size, p_unarmored, i_size );
571 msg_Dbg( p_this, "Unarmoring failed : corrupted signature ?" );
576 if( packet_type( *p_buf ) != SIGNATURE_PACKET )
579 msg_Dbg( p_this, "Not a signature: %d", *p_buf );
583 size_t i_header_len = packet_header_len( *p_buf );
584 if( ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) ||
585 i_header_len + 1 > (size_t)i_size )
588 msg_Dbg( p_this, "Invalid signature packet header" );
592 size_t i_len = scalar_number( p_buf+1, i_header_len );
593 if( i_len + i_header_len + 1 != (size_t)i_size )
596 msg_Dbg( p_this, "Invalid signature packet" );
600 int i_ret = parse_signature_packet( p_sig, p_buf+1+i_header_len, i_len );
602 if( i_ret != VLC_SUCCESS )
604 msg_Dbg( p_this, "Couldn't parse signature" );
608 if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
610 msg_Dbg( p_this, "Invalid signature type: %d", p_sig->type );
611 if( p_sig->version == 4 )
613 free( p_sig->specific.v4.hashed_data );
614 free( p_sig->specific.v4.unhashed_data );
623 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
625 static int verify_signature( uint8_t *p_r, uint8_t *p_s,
626 public_key_packet_t *p_key, uint8_t *p_hash )
628 /* the data to be verified (a SHA-1 hash) */
629 const char *hash_sexp_s = "(data(flags raw)(value %m))";
631 const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
633 const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
636 gcry_mpi_t p, q, g, y, r, s, hash;
637 p = q = g = y = r = s = hash = NULL;
638 gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
639 key_sexp = hash_sexp = sig_sexp = NULL;
641 int i_p_len = mpi_len( p_key->p );
642 int i_q_len = mpi_len( p_key->q );
643 int i_g_len = mpi_len( p_key->g );
644 int i_y_len = mpi_len( p_key->y );
645 if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, i_p_len, NULL ) ||
646 gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, i_q_len, NULL ) ||
647 gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, i_g_len, NULL ) ||
648 gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, i_y_len, NULL ) ||
649 gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
652 int i_r_len = mpi_len( p_r );
653 int i_s_len = mpi_len( p_s );
654 if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, i_r_len, NULL ) ||
655 gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, i_s_len, NULL ) ||
656 gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
660 if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
661 gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
664 if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
670 if( p ) gcry_mpi_release( p );
671 if( q ) gcry_mpi_release( q );
672 if( g ) gcry_mpi_release( g );
673 if( y ) gcry_mpi_release( y );
674 if( r ) gcry_mpi_release( r );
675 if( s ) gcry_mpi_release( s );
676 if( hash ) gcry_mpi_release( hash );
677 if( key_sexp ) gcry_sexp_release( key_sexp );
678 if( sig_sexp ) gcry_sexp_release( sig_sexp );
679 if( hash_sexp ) gcry_sexp_release( hash_sexp );
684 * fill a public_key_t with public key data, including:
685 * * public key packet
686 * * signature packet issued by key which long id is p_sig_issuer
689 static int parse_public_key( const uint8_t *p_key_data, size_t i_key_len,
690 public_key_t *p_key, const uint8_t *p_sig_issuer )
692 uint8_t *pos = (uint8_t*) p_key_data;
693 uint8_t *max_pos = pos + i_key_len;
696 #define PUBLIC_KEY_FOUND 0x01
697 #define USER_ID_FOUND 0x02
698 #define SIGNATURE_FOUND 0X04
700 uint8_t *p_key_unarmored = NULL;
702 p_key->psz_username = NULL;
703 p_key->sig.specific.v4.hashed_data = NULL;
704 p_key->sig.specific.v4.unhashed_data = NULL;
706 if( !( *pos & 0x80 ) )
707 { /* first byte is ASCII, unarmoring */
708 p_key_unarmored = (uint8_t*)malloc( i_key_len );
709 if( !p_key_unarmored )
711 int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
712 p_key_unarmored, i_key_len );
717 pos = p_key_unarmored;
718 max_pos = pos + i_len;
721 while( pos < max_pos )
723 if( !(*pos & 0x80) || *pos & 0x40 )
726 int i_type = packet_type( *pos );
728 int i_header_len = packet_header_len( *pos++ );
729 if( pos + i_header_len > max_pos ||
730 ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) )
733 int i_packet_len = scalar_number( pos, i_header_len );
736 if( pos + i_packet_len > max_pos )
741 case PUBLIC_KEY_PACKET:
742 i_status |= PUBLIC_KEY_FOUND;
743 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
747 case SIGNATURE_PACKET: /* we accept only v4 signatures here */
748 if( i_status & SIGNATURE_FOUND || !p_sig_issuer )
750 int i_ret = parse_signature_packet( &p_key->sig, pos,
752 if( i_ret == VLC_SUCCESS )
754 if( p_key->sig.version != 4 )
756 if( memcmp( p_key->sig.issuer_longid, p_sig_issuer, 8 ) )
758 free( p_key->sig.specific.v4.hashed_data );
759 free( p_key->sig.specific.v4.unhashed_data );
760 p_key->sig.specific.v4.hashed_data = NULL;
761 p_key->sig.specific.v4.unhashed_data = NULL;
764 i_status |= SIGNATURE_FOUND;
769 if( p_key->psz_username ) /* save only the first User ID */
771 i_status |= USER_ID_FOUND;
772 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
773 if( !p_key->psz_username )
776 memcpy( p_key->psz_username, pos, i_packet_len );
777 p_key->psz_username[i_packet_len] = '\0';
785 free( p_key_unarmored );
787 if( !( i_status & ( PUBLIC_KEY_FOUND | USER_ID_FOUND ) ) )
790 if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
796 if( p_key->sig.version == 4 )
798 free( p_key->sig.specific.v4.hashed_data );
799 free( p_key->sig.specific.v4.unhashed_data );
801 free( p_key->psz_username );
802 free( p_key_unarmored );
807 * return a sha1 hash of a file
809 static uint8_t *hash_sha1_from_file( const char *psz_file,
810 signature_packet_t *p_sig )
812 if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
815 FILE *f = utf8_fopen( psz_file, "r" );
819 uint8_t buffer[4096];
822 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
829 while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
830 gcry_md_write( hd, buffer, i_read );
832 if( p_sig->version == 3 )
834 gcry_md_putc( hd, p_sig->type );
835 gcry_md_write( hd, &p_sig->specific.v3.timestamp, 4 );
837 else if( p_sig->version == 4 )
839 gcry_md_putc( hd, p_sig->version );
840 gcry_md_putc( hd, p_sig->type );
841 gcry_md_putc( hd, p_sig->public_key_algo );
842 gcry_md_putc( hd, p_sig->digest_algo );
843 gcry_md_write( hd, p_sig->specific.v4.hashed_data_len, 2 );
844 size_t i_len = scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
845 gcry_md_write( hd, p_sig->specific.v4.hashed_data, i_len );
848 { /* RFC 4880 only tells about versions 3 and 4 */
856 uint8_t *p_tmp = (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1);
857 uint8_t *p_hash = malloc( 20 );
859 memcpy( p_hash, p_tmp, 20 );
865 * download a public key (the last one) from videolan server, and parse it
867 static public_key_t *download_key( vlc_object_t *p_this,
868 const uint8_t *p_longid, const uint8_t *p_signature_issuer )
871 if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
872 p_longid[0], p_longid[1], p_longid[2], p_longid[3],
873 p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
876 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
881 int64_t i_size = stream_Size( p_stream );
884 stream_Delete( p_stream );
888 uint8_t *p_buf = (uint8_t*)malloc( i_size );
891 stream_Delete( p_stream );
895 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
896 stream_Delete( p_stream );
898 if( i_read != (int)i_size )
900 msg_Dbg( p_this, "Couldn't read full GPG key" );
905 public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
912 memcpy( p_pkey->longid, p_longid, 8 );
914 int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
917 if( i_error != VLC_SUCCESS )
919 msg_Dbg( p_this, "Couldn't parse GPG key" );
928 * Generate a SHA1 hash on a public key, to verify a signature made on that hash
929 * Note that we need the signature (v4) to compute the hash
931 static uint8_t *key_sign_hash( public_key_t *p_pkey )
933 if( p_pkey->sig.version != 4 )
936 if( p_pkey->sig.type < GENERIC_KEY_SIGNATURE ||
937 p_pkey->sig.type > POSITIVE_KEY_SIGNATURE )
940 gcry_error_t error = 0;
943 error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
947 gcry_md_putc( hd, 0x99 );
949 size_t i_p_len = mpi_len( p_pkey->key.p );
950 size_t i_g_len = mpi_len( p_pkey->key.g );
951 size_t i_q_len = mpi_len( p_pkey->key.q );
952 size_t i_y_len = mpi_len( p_pkey->key.y );
954 size_t i_size = 6 + 2*4 + i_p_len + i_g_len + i_q_len + i_y_len;
956 gcry_md_putc( hd, (i_size >> 8) & 0xff );
957 gcry_md_putc( hd, i_size & 0xff );
959 gcry_md_putc( hd, p_pkey->key.version );
960 gcry_md_write( hd, p_pkey->key.timestamp, 4 );
961 gcry_md_putc( hd, p_pkey->key.algo );
963 gcry_md_write( hd, (uint8_t*)&p_pkey->key.p, 2 );
964 gcry_md_write( hd, (uint8_t*)&p_pkey->key.p + 2, i_p_len );
966 gcry_md_write( hd, (uint8_t*)&p_pkey->key.q, 2 );
967 gcry_md_write( hd, (uint8_t*)&p_pkey->key.q + 2, i_q_len );
969 gcry_md_write( hd, (uint8_t*)&p_pkey->key.g, 2 );
970 gcry_md_write( hd, (uint8_t*)&p_pkey->key.g + 2, i_g_len );
972 gcry_md_write( hd, (uint8_t*)&p_pkey->key.y, 2 );
973 gcry_md_write( hd, (uint8_t*)&p_pkey->key.y + 2, i_y_len );
975 gcry_md_putc( hd, 0xb4 );
977 size_t i_len = strlen((char*)p_pkey->psz_username);
979 gcry_md_putc( hd, (i_len << 24) & 0xff );
980 gcry_md_putc( hd, (i_len << 16) & 0xff );
981 gcry_md_putc( hd, (i_len << 8) & 0xff );
982 gcry_md_putc( hd, (i_len) & 0xff );
984 gcry_md_write( hd, p_pkey->psz_username, i_len );
986 size_t i_hashed_data_len =
987 scalar_number( p_pkey->sig.specific.v4.hashed_data_len, 2 );
989 gcry_md_putc( hd, p_pkey->sig.version );
990 gcry_md_putc( hd, p_pkey->sig.type );
991 gcry_md_putc( hd, p_pkey->sig.public_key_algo );
992 gcry_md_putc( hd, p_pkey->sig.digest_algo );
993 gcry_md_write( hd, p_pkey->sig.specific.v4.hashed_data_len, 2 );
994 gcry_md_write( hd, p_pkey->sig.specific.v4.hashed_data, i_hashed_data_len );
996 gcry_md_putc( hd, 0x04 );
997 gcry_md_putc( hd, 0xff );
999 i_hashed_data_len += 6; /* hashed data + 6 bytes header */
1001 gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
1002 gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
1003 gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
1004 gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
1006 gcry_md_final( hd );
1008 uint8_t *p_tmp = gcry_md_read( hd, GCRY_MD_SHA1);
1011 p_tmp[0] != p_pkey->sig.hash_verification[0] ||
1012 p_tmp[1] != p_pkey->sig.hash_verification[1] )
1014 gcry_md_close( hd );
1018 uint8_t *p_hash = malloc( 20 );
1020 memcpy( p_hash, p_tmp, 20 );
1021 gcry_md_close( hd );
1026 /*****************************************************************************
1027 * Update_t functions
1028 *****************************************************************************/
1031 * Create a new update VLC struct
1033 * \param p_this the calling vlc_object
1034 * \return pointer to new update_t or NULL
1036 update_t *__update_New( vlc_object_t *p_this )
1041 p_update = (update_t *)malloc( sizeof( update_t ) );
1042 if( !p_update ) return NULL;
1044 vlc_mutex_init( &p_update->lock );
1046 p_update->p_libvlc = p_this->p_libvlc;
1048 p_update->release.psz_url = NULL;
1049 p_update->release.psz_desc = NULL;
1051 p_update->p_pkey = NULL;
1058 * Delete an update_t struct
1060 * \param p_update update_t* pointer
1063 void update_Delete( update_t *p_update )
1067 vlc_mutex_destroy( &p_update->lock );
1069 free( p_update->release.psz_url );
1070 free( p_update->release.psz_desc );
1071 free( p_update->p_pkey );
1077 * Empty the release struct
1079 * \param p_update update_t* pointer
1082 static void EmptyRelease( update_t *p_update )
1084 p_update->release.i_major = 0;
1085 p_update->release.i_minor = 0;
1086 p_update->release.i_revision = 0;
1088 FREENULL( p_update->release.psz_url );
1089 FREENULL( p_update->release.psz_desc );
1093 * Get the update file and parse it
1094 * p_update has to be locked when calling this function
1096 * \param p_update pointer to update struct
1097 * \return true if the update is valid and authenticated
1099 static bool GetUpdateFile( update_t *p_update )
1101 stream_t *p_stream = NULL;
1105 unsigned char extra;
1106 char *psz_line = NULL;
1107 char *psz_version_line = NULL;
1109 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
1112 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
1113 UPDATE_VLC_STATUS_URL );
1117 /* Try to read three lines */
1118 if( !( psz_line = stream_ReadLine( p_stream ) ) )
1120 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
1121 UPDATE_VLC_STATUS_URL );
1125 psz_version_line = psz_line;
1126 /* first line : version number */
1127 p_update->release.extra = 0;
1128 switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
1131 p_update->release.extra = extra;
1133 p_update->release.i_major = i_major;
1134 p_update->release.i_minor = i_minor;
1135 p_update->release.i_revision = i_revision;
1138 msg_Err( p_update->p_libvlc, "Update version false formated" );
1142 /* Second line : URL */
1143 if( !( psz_line = stream_ReadLine( p_stream ) ) )
1145 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
1146 UPDATE_VLC_STATUS_URL );
1149 p_update->release.psz_url = psz_line;
1152 /* Third line : description */
1153 if( !( psz_line = stream_ReadLine( p_stream ) ) )
1155 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
1156 UPDATE_VLC_STATUS_URL );
1159 p_update->release.psz_desc = psz_line;
1161 stream_Delete( p_stream );
1164 /* Now that we know the status is valid, we must download its signature
1165 * to authenticate it */
1166 signature_packet_t sign;
1167 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
1168 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
1170 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
1174 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
1176 msg_Err( p_update->p_libvlc, "Invalid signature type" );
1180 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
1181 if( !p_update->p_pkey )
1184 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
1185 p_update->p_pkey, NULL ) != VLC_SUCCESS )
1187 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
1188 FREENULL( p_update->p_pkey );
1192 memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
1194 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
1196 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
1197 public_key_t *p_new_pkey = download_key(
1198 VLC_OBJECT(p_update->p_libvlc),
1199 sign.issuer_longid, videolan_public_key_longid );
1202 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
1203 FREENULL( p_update->p_pkey );
1207 uint8_t *p_hash = key_sign_hash( p_new_pkey );
1210 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
1212 FREENULL( p_update->p_pkey );
1216 if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
1217 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
1220 msg_Info( p_update->p_libvlc, "Key authenticated" );
1221 free( p_update->p_pkey );
1222 p_update->p_pkey = p_new_pkey;
1227 msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
1233 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
1236 gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
1237 FREENULL( psz_version_line );
1238 if( sign.type == TEXT_SIGNATURE )
1239 gcry_md_putc( hd, '\r' );
1240 gcry_md_putc( hd, '\n' );
1241 gcry_md_write( hd, p_update->release.psz_url,
1242 strlen( p_update->release.psz_url ) );
1243 if( sign.type == TEXT_SIGNATURE )
1244 gcry_md_putc( hd, '\r' );
1245 gcry_md_putc( hd, '\n' );
1246 gcry_md_write( hd, p_update->release.psz_desc,
1247 strlen( p_update->release.psz_desc ) );
1248 if( sign.type == TEXT_SIGNATURE )
1249 gcry_md_putc( hd, '\r' );
1250 gcry_md_putc( hd, '\n' );
1253 if( sign.version == 3 )
1255 gcry_md_putc( hd, sign.type );
1256 gcry_md_write( hd, &sign.specific.v3.timestamp, 4 );
1258 else if( sign.version == 4 )
1260 gcry_md_putc( hd, sign.version );
1261 gcry_md_putc( hd, sign.type );
1262 gcry_md_putc( hd, sign.public_key_algo );
1263 gcry_md_putc( hd, sign.digest_algo );
1264 gcry_md_write( hd, sign.specific.v4.hashed_data_len, 2 );
1265 size_t i_len = scalar_number( sign.specific.v4.hashed_data_len, 2 );
1266 gcry_md_write( hd, sign.specific.v4.hashed_data, i_len );
1267 gcry_md_putc( hd, 0x04 );
1268 gcry_md_putc( hd, 0xFF );
1270 i_len += 6; /* hashed data + 6 bytes header */
1272 gcry_md_putc( hd, (i_len << 24) & 0xff);
1273 gcry_md_putc( hd, (i_len << 16) &0xff );
1274 gcry_md_putc( hd, (i_len << 8) & 0xff );
1275 gcry_md_putc( hd, (i_len) & 0xff );
1278 { /* RFC 4880 only tells about versions 3 and 4 */
1279 msg_Warn( p_update->p_libvlc, "Invalid signature version %d",
1284 gcry_md_final( hd );
1286 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );
1288 if( p_hash[0] != sign.hash_verification[0] ||
1289 p_hash[1] != sign.hash_verification[1] )
1291 msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
1295 if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
1298 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
1303 msg_Info( p_update->p_libvlc, "Status file authenticated" );
1304 gcry_md_close( hd );
1309 gcry_md_close( hd );
1312 stream_Delete( p_stream );
1313 free( psz_version_line );
1319 * Struct to launch the check in an other thread
1325 void (*pf_callback)( void *, bool );
1327 } update_check_thread_t;
1329 void update_CheckReal( update_check_thread_t *p_uct );
1334 * \param p_update pointer to update struct
1335 * \param pf_callback pointer to a function to call when the update_check is finished
1336 * \param p_data pointer to some datas to give to the callback
1339 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
1343 update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
1344 sizeof( update_check_thread_t ) );
1345 if( !p_uct ) return;
1347 p_uct->p_update = p_update;
1348 p_uct->pf_callback = pf_callback;
1349 p_uct->p_data = p_data;
1351 vlc_thread_create( p_uct, "check for update", update_CheckReal,
1352 VLC_THREAD_PRIORITY_LOW, false );
1355 void update_CheckReal( update_check_thread_t *p_uct )
1358 vlc_mutex_lock( &p_uct->p_update->lock );
1360 EmptyRelease( p_uct->p_update );
1361 b_ret = GetUpdateFile( p_uct->p_update );
1362 vlc_mutex_unlock( &p_uct->p_update->lock );
1364 if( p_uct->pf_callback )
1365 (p_uct->pf_callback)( p_uct->p_data, b_ret );
1367 vlc_object_release( p_uct );
1371 * Compare a given release's version number to the current VLC's one
1373 * \param p_update structure
1374 * \return true if we have to upgrade to the given version to be up to date
1376 bool update_NeedUpgrade( update_t *p_update )
1380 return p_update->release.i_major < *PACKAGE_VERSION_MAJOR - '0' ||
1381 p_update->release.i_minor < *PACKAGE_VERSION_MINOR - '0' ||
1382 p_update->release.i_revision < *PACKAGE_VERSION_REVISION - '0' ||
1383 p_update->release.extra < *PACKAGE_VERSION_EXTRA;
1387 * Convert a long int size in bytes to a string
1389 * \param l_size the size in bytes
1390 * \return the size as a string
1392 static char *size_str( long int l_size )
1394 char *psz_tmp = NULL;
1397 i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
1398 else if( l_size >> 20 )
1399 i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1400 else if( l_size >> 10 )
1401 i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1403 i_retval = asprintf( &psz_tmp, "%ld B", l_size );
1405 return i_retval == -1 ? NULL : psz_tmp;
1410 * Struct to launch the download in a thread
1417 } update_download_thread_t;
1419 void update_DownloadReal( update_download_thread_t *p_udt );
1422 * Download the file given in the update_t
1424 * \param p_update structure
1425 * \param dir to store the download file
1428 void update_Download( update_t *p_update, char *psz_destdir )
1432 update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1433 sizeof( update_download_thread_t ) );
1436 msg_Err( p_update->p_libvlc, "out of memory" );
1440 p_udt->p_update = p_update;
1441 p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1443 vlc_thread_create( p_udt, "download update", update_DownloadReal,
1444 VLC_THREAD_PRIORITY_LOW, false );
1447 void update_DownloadReal( update_download_thread_t *p_udt )
1451 long int l_downloaded = 0;
1453 char *psz_status = NULL;
1454 char *psz_downloaded = NULL;
1455 char *psz_size = NULL;
1456 char *psz_destfile = NULL;
1457 char *psz_tmpdestfile = NULL;
1459 FILE *p_file = NULL;
1460 stream_t *p_stream = NULL;
1461 void* p_buffer = NULL;
1464 update_t *p_update = p_udt->p_update;
1465 char *psz_destdir = p_udt->psz_destdir;
1467 /* Open the stream */
1468 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1471 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1475 /* Get the stream size */
1476 l_size = stream_Size( p_stream );
1478 /* Get the file name and open it*/
1479 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1480 if( !psz_tmpdestfile )
1482 msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1486 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1489 p_file = utf8_fopen( psz_destfile, "w" );
1492 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1496 /* Create a buffer and fill it with the downloaded file */
1497 p_buffer = (void *)malloc( 1 << 10 );
1501 psz_size = size_str( l_size );
1502 if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",
1503 p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1505 i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1509 while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1510 !intf_ProgressIsCancelled( p_udt, i_progress ) )
1512 if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
1514 msg_Err( p_udt, "Failed to write into %s", psz_destfile );
1518 l_downloaded += i_read;
1519 psz_downloaded = size_str( l_downloaded );
1520 f_progress = 100.0*(float)l_downloaded/(float)l_size;
1522 if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done",
1523 p_update->release.psz_url, psz_downloaded, psz_size,
1524 f_progress ) != -1 )
1526 intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1529 free( psz_downloaded );
1532 /* Finish the progress bar or delete the file if the user had canceled */
1536 if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
1538 if( asprintf( &psz_status, "%s\nDone %s (100.0%%)",
1539 p_update->release.psz_url, psz_size ) != -1 )
1541 intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1547 utf8_unlink( psz_destfile );
1551 signature_packet_t sign;
1552 if( download_signature( VLC_OBJECT( p_udt ), &sign,
1553 p_update->release.psz_url ) != VLC_SUCCESS )
1555 utf8_unlink( psz_destfile );
1557 intf_UserFatal( p_udt, true, _("File can not be verified"),
1558 _("It was not possible to download a cryptographic signature for "
1559 "downloaded file \"%s\", and so VLC deleted it."),
1561 msg_Err( p_udt, "Couldn't download signature of downloaded file" );
1565 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
1567 utf8_unlink( psz_destfile );
1568 msg_Err( p_udt, "Invalid signature issuer" );
1569 intf_UserFatal( p_udt, true, _("Invalid signature"),
1570 _("The cryptographic signature for downloaded file \"%s\" was "
1571 "invalid and couldn't be used to securely verify it, and so "
1577 if( sign.type != BINARY_SIGNATURE )
1579 utf8_unlink( psz_destfile );
1580 msg_Err( p_udt, "Invalid signature type" );
1581 intf_UserFatal( p_udt, true, _("Invalid signature"),
1582 _("The cryptographic signature for downloaded file \"%s\" was "
1583 "invalid and couldn't be used to securely verify it, and so "
1589 uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
1592 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
1593 utf8_unlink( psz_destfile );
1594 intf_UserFatal( p_udt, true, _("File not verifiable"),
1595 _("It was not possible to securely verify downloaded file \"%s\", "
1596 "and so VLC deleted it."),
1602 if( p_hash[0] != sign.hash_verification[0] ||
1603 p_hash[1] != sign.hash_verification[1] )
1605 utf8_unlink( psz_destfile );
1606 intf_UserFatal( p_udt, true, _("File corrupted"),
1607 _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
1609 msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
1614 if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
1617 utf8_unlink( psz_destfile );
1618 intf_UserFatal( p_udt, true, _("File corrupted"),
1619 _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
1621 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
1626 msg_Info( p_udt, "%s authenticated", psz_destfile );
1631 stream_Delete( p_stream );
1634 free( psz_destdir );
1635 free( psz_destfile );
1639 vlc_object_release( p_udt );
1642 update_t *__update_New( vlc_object_t *p_this )
1648 void update_Delete( update_t *p_update )
1653 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
1656 (void)p_update; (void)pf_callback; (void)p_data;
1659 bool update_NeedUpgrade( update_t *p_update )
1665 void update_Download( update_t *p_update, char *psz_destdir )
1667 (void)p_update; (void)psz_destdir;