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>
57 /*****************************************************************************
59 *****************************************************************************/
62 * Here is the format of these "status files" :
63 * First line is the last version: "X.Y.Ze" where:
64 * * X is the major number
65 * * Y is the minor number
66 * * Z is the revision number
67 * * e is an OPTIONAL extra letter
68 * * AKA "0.8.6d" or "0.9.0"
69 * Second line is an url of the binary for this last version
70 * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
73 #if defined( UNDER_CE )
74 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
75 #elif defined( WIN32 )
76 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
77 #elif defined( __APPLE__ )
78 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
79 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
81 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
83 #elif defined( SYS_BEOS )
84 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
86 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
90 /*****************************************************************************
92 *****************************************************************************/
93 static void EmptyRelease( update_t *p_update );
94 static bool GetUpdateFile( update_t *p_update );
95 static char * size_str( long int l_size );
98 /*****************************************************************************
100 *****************************************************************************/
102 #define packet_type( c ) ( ( c & 0x3c ) >> 2 ) /* 0x3C = 00111100 */
103 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
105 static inline int scalar_number( uint8_t *p, int header_len )
107 assert( header_len == 1 || header_len == 2 || header_len == 4 );
109 if( header_len == 1 )
111 else if( header_len == 2 )
112 return( (p[0] << 8) + p[1] );
113 else if( header_len == 4 )
114 return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
116 abort(); /* to shut up GCC warning */
119 /* number of data bytes in a MPI */
120 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
123 * fill a public_key_packet_t structure from public key packet data
124 * verify that it is a version 4 public key packet, using DSA
126 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
127 size_t i_packet_len )
130 if( i_packet_len > 418 || i_packet_len < 6 )
135 p_key->version = *p_buf++; i_read++;
136 if( p_key->version != 4 )
139 /* XXX: warn when timestamp is > date ? */
140 memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4; i_read += 4;
142 p_key->algo = *p_buf++; i_read++;
143 if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
147 if( i_read + 2 > i_packet_len )
150 int i_p_len = mpi_len( p_buf );
152 if( i_p_len > 128 || i_read + 2 + i_p_len > i_packet_len )
155 memcpy( p_key->p, p_buf, 2+i_p_len );
156 p_buf += 2+i_p_len; i_read += 2+i_p_len;
159 if( i_read + 2 > i_packet_len )
162 int i_q_len = mpi_len( p_buf );
164 if( i_q_len > 20 || i_read+2+i_q_len > i_packet_len )
167 memcpy( p_key->q, p_buf, 2+i_q_len );
168 p_buf += 2+i_q_len; i_read += 2+i_q_len;
171 if( i_read + 2 > i_packet_len )
174 int i_g_len = mpi_len( p_buf );
176 if( i_g_len > 128 || i_read+2+i_g_len > i_packet_len )
179 memcpy( p_key->g, p_buf, 2+i_g_len );
180 p_buf += 2+i_g_len; i_read += 2+i_g_len;
183 if( i_read + 2 > i_packet_len )
186 int i_y_len = mpi_len( p_buf );
189 if( i_y_len > 128 || i_read+2+i_y_len > i_packet_len )
192 memcpy( p_key->y, p_buf, 2+i_y_len );
195 if( i_read != i_packet_len ) /* some extra data eh ? */
201 static size_t parse_signature_v3_packet( signature_packet_t *p_sig,
202 uint8_t *p_buf, size_t i_sig_len )
204 size_t i_read = 1; /* we already read the version byte */
206 if( i_sig_len < 19 ) /* signature is at least 19 bytes + the 2 MPIs */
209 p_sig->specific.v3.hashed_data_len = *p_buf++; i_read++;
210 if( p_sig->specific.v3.hashed_data_len != 5 )
213 p_sig->type = *p_buf++; i_read++;
215 memcpy( p_sig->specific.v3.timestamp, p_buf, 4 );
216 p_buf += 4; i_read += 4;
218 memcpy( p_sig->issuer_longid, p_buf, 8 );
219 p_buf += 8; i_read += 8;
221 p_sig->public_key_algo = *p_buf++; i_read++;
223 p_sig->digest_algo = *p_buf++; i_read++;
225 p_sig->hash_verification[0] = *p_buf++; i_read++;
226 p_sig->hash_verification[1] = *p_buf++; i_read++;
228 assert( i_read == 19 );
234 * fill a signature_packet_v4_t from signature packet data
235 * verify that it was used with a DSA public key, using SHA-1 digest
237 static size_t parse_signature_v4_packet( signature_packet_t *p_sig,
238 uint8_t *p_buf, size_t i_sig_len )
240 size_t i_read = 1; /* we already read the version byte */
242 if( i_sig_len < 10 ) /* signature is at least 10 bytes + the 2 MPIs */
245 p_sig->type = *p_buf++; i_read++;
247 p_sig->public_key_algo = *p_buf++; i_read++;
249 p_sig->digest_algo = *p_buf++; i_read++;
251 memcpy( p_sig->specific.v4.hashed_data_len, p_buf, 2 );
252 p_buf += 2; i_read += 2;
254 size_t i_hashed_data_len =
255 scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
256 i_read += i_hashed_data_len;
257 if( i_read + 4 > i_sig_len )
260 p_sig->specific.v4.hashed_data = (uint8_t*) malloc( i_hashed_data_len );
261 if( !p_sig->specific.v4.hashed_data )
263 memcpy( p_sig->specific.v4.hashed_data, p_buf, i_hashed_data_len );
264 p_buf += i_hashed_data_len;
266 memcpy( p_sig->specific.v4.unhashed_data_len, p_buf, 2 );
267 p_buf += 2; i_read += 2;
269 size_t i_unhashed_data_len =
270 scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
271 i_read += i_unhashed_data_len;
272 if( i_read + 2 > i_sig_len )
275 p_sig->specific.v4.unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
276 if( !p_sig->specific.v4.unhashed_data )
279 memcpy( p_sig->specific.v4.unhashed_data, p_buf, i_unhashed_data_len );
280 p_buf += i_unhashed_data_len;
282 memcpy( p_sig->hash_verification, p_buf, 2 );
283 p_buf += 2; i_read += 2;
285 uint8_t *p, *max_pos;
286 p = p_sig->specific.v4.unhashed_data;
287 max_pos = p + scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
294 size_t i_subpacket_len;
297 if( p + 1 > max_pos )
299 i_subpacket_len = *p++;
303 if( p + 2 > max_pos )
305 i_subpacket_len = (*p++ - 192) << 8;
306 i_subpacket_len += *p++ + 192;
310 if( p + 4 > max_pos )
312 i_subpacket_len = *++p << 24;
313 i_subpacket_len += *++p << 16;
314 i_subpacket_len += *++p << 8;
315 i_subpacket_len += *++p;
318 if( *p == ISSUER_SUBPACKET )
320 if( p + 9 > max_pos )
323 memcpy( &p_sig->issuer_longid, p+1, 8 );
328 p += i_subpacket_len;
332 static int parse_signature_packet( signature_packet_t *p_sig,
333 uint8_t *p_buf, size_t i_sig_len )
335 if( !i_sig_len ) /* 1st sanity check, we need at least the version */
338 p_sig->version = *p_buf++;
341 switch( p_sig->version )
344 i_read = parse_signature_v3_packet( p_sig, p_buf, i_sig_len );
347 p_sig->specific.v4.hashed_data = NULL;
348 p_sig->specific.v4.unhashed_data = NULL;
349 i_read = parse_signature_v4_packet( p_sig, p_buf, i_sig_len );
355 if( i_read == 0 ) /* signature packet parsing has failed */
358 if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
361 if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
364 switch( p_sig->type )
366 case BINARY_SIGNATURE:
368 case GENERIC_KEY_SIGNATURE:
369 case PERSONA_KEY_SIGNATURE:
370 case CASUAL_KEY_SIGNATURE:
371 case POSITIVE_KEY_SIGNATURE:
377 p_buf--; /* rewind to the version byte */
380 if( i_read + 2 > i_sig_len )
383 size_t i_r_len = mpi_len( p_buf ); i_read += 2;
384 if( i_read + i_r_len > i_sig_len || i_r_len > 20 )
387 memcpy( p_sig->r, p_buf, 2 + i_r_len );
388 p_buf += 2 + i_r_len;
391 if( i_read + 2 > i_sig_len )
394 size_t i_s_len = mpi_len( p_buf ); i_read += 2;
395 if( i_read + i_s_len > i_sig_len || i_s_len > 20 )
398 memcpy( p_sig->s, p_buf, 2 + i_s_len );
399 p_buf += 2 + i_s_len;
402 assert( i_read == i_sig_len );
403 if( i_read < i_sig_len ) /* some extra data, hm ? */
410 if( p_sig->version == 4 )
412 free( p_sig->specific.v4.hashed_data );
413 free( p_sig->specific.v4.unhashed_data );
420 * crc_octets() was lamely copied from rfc 2440
421 * Copyright (C) The Internet Society (1998). All Rights Reserved.
423 #define CRC24_INIT 0xB704CEL
424 #define CRC24_POLY 0x1864CFBL
426 static long crc_octets( uint8_t *octets, size_t len )
428 long crc = CRC24_INIT;
432 crc ^= (*octets++) << 16;
433 for (i = 0; i < 8; i++)
440 return crc & 0xFFFFFFL;
444 * Transform an armored document in binary format
445 * Used on public keys and signatures
447 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
448 uint8_t *p_obuf, size_t i_obuf_len )
450 char *p_ipos = p_ibuf;
451 uint8_t *p_opos = p_obuf;
453 int i_header_skipped = 0;
455 while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
457 if( *p_ipos == '\r' || *p_ipos == '\n' )
463 size_t i_line_len = strcspn( p_ipos, "\r\n" );
464 if( i_line_len == 0 )
467 if( !i_header_skipped )
469 if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
470 i_header_skipped = 1;
472 p_ipos += i_line_len + 1;
476 if( !strncmp( p_ipos, "Version:", 8 ) )
478 p_ipos += i_line_len + 1;
482 if( p_ipos[i_line_len - 1] == '=' )
485 p_ipos[i_line_len - 1] = '\0';
488 p_ipos[i_line_len] = '\0';
490 p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
491 p_obuf - p_opos + i_obuf_len, p_ipos );
492 p_ipos += i_line_len + 1;
495 /* XXX: the CRC is OPTIONAL, really require it ? */
496 if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
500 if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
503 long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
504 long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
506 return l_crc2 == l_crc ? p_opos - p_obuf : 0;
510 * Download the signature associated to a document or a binary file.
511 * We're given the file's url, we just append ".asc" to it and download
513 static int download_signature( vlc_object_t *p_this,
514 signature_packet_t *p_sig,
515 const char *psz_url )
517 char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
521 strcpy( psz_sig, psz_url );
522 strcat( psz_sig, ".asc" );
524 stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
530 int64_t i_size = stream_Size( p_stream );
532 msg_Dbg( p_this, "Downloading signature (%"PRId64" bytes)", i_size );
533 uint8_t *p_buf = (uint8_t*)malloc( i_size );
536 stream_Delete( p_stream );
540 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
542 stream_Delete( p_stream );
544 if( i_read != (int)i_size )
547 "Couldn't download full signature (only %d bytes)", i_read );
552 if( (uint8_t)*p_buf < 0x80 ) /* ASCII */
554 msg_Dbg( p_this, "Unarmoring signature" );
556 uint8_t* p_unarmored = (uint8_t*) malloc( ( i_size * 3 ) / 4 + 1 );
563 int i_bytes = pgp_unarmor( (char*)p_buf, i_size, p_unarmored, i_size );
572 msg_Dbg( p_this, "Unarmoring failed : corrupted signature ?" );
577 if( packet_type( *p_buf ) != SIGNATURE_PACKET )
580 msg_Dbg( p_this, "Not a signature: %d", *p_buf );
584 size_t i_header_len = packet_header_len( *p_buf );
585 if( ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) ||
586 i_header_len + 1 > (size_t)i_size )
589 msg_Dbg( p_this, "Invalid signature packet header" );
593 size_t i_len = scalar_number( p_buf+1, i_header_len );
594 if( i_len + i_header_len + 1 != (size_t)i_size )
597 msg_Dbg( p_this, "Invalid signature packet" );
601 int i_ret = parse_signature_packet( p_sig, p_buf+1+i_header_len, i_len );
603 if( i_ret != VLC_SUCCESS )
605 msg_Dbg( p_this, "Couldn't parse signature" );
609 if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
611 msg_Dbg( p_this, "Invalid signature type: %d", p_sig->type );
612 if( p_sig->version == 4 )
614 free( p_sig->specific.v4.hashed_data );
615 free( p_sig->specific.v4.unhashed_data );
624 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
626 static int verify_signature( uint8_t *p_r, uint8_t *p_s,
627 public_key_packet_t *p_key, uint8_t *p_hash )
629 /* the data to be verified (a SHA-1 hash) */
630 const char *hash_sexp_s = "(data(flags raw)(value %m))";
632 const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
634 const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
637 gcry_mpi_t p, q, g, y, r, s, hash;
638 p = q = g = y = r = s = hash = NULL;
639 gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
640 key_sexp = hash_sexp = sig_sexp = NULL;
642 int i_p_len = mpi_len( p_key->p );
643 int i_q_len = mpi_len( p_key->q );
644 int i_g_len = mpi_len( p_key->g );
645 int i_y_len = mpi_len( p_key->y );
646 if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, i_p_len, NULL ) ||
647 gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, i_q_len, NULL ) ||
648 gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, i_g_len, NULL ) ||
649 gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, i_y_len, NULL ) ||
650 gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
653 int i_r_len = mpi_len( p_r );
654 int i_s_len = mpi_len( p_s );
655 if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, i_r_len, NULL ) ||
656 gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, i_s_len, NULL ) ||
657 gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
661 if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
662 gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
665 if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
671 if( p ) gcry_mpi_release( p );
672 if( q ) gcry_mpi_release( q );
673 if( g ) gcry_mpi_release( g );
674 if( y ) gcry_mpi_release( y );
675 if( r ) gcry_mpi_release( r );
676 if( s ) gcry_mpi_release( s );
677 if( hash ) gcry_mpi_release( hash );
678 if( key_sexp ) gcry_sexp_release( key_sexp );
679 if( sig_sexp ) gcry_sexp_release( sig_sexp );
680 if( hash_sexp ) gcry_sexp_release( hash_sexp );
685 * fill a public_key_t with public key data, including:
686 * * public key packet
687 * * signature packet issued by key which long id is p_sig_issuer
690 static int parse_public_key( const uint8_t *p_key_data, size_t i_key_len,
691 public_key_t *p_key, const uint8_t *p_sig_issuer )
693 uint8_t *pos = (uint8_t*) p_key_data;
694 uint8_t *max_pos = pos + i_key_len;
697 #define PUBLIC_KEY_FOUND 0x01
698 #define USER_ID_FOUND 0x02
699 #define SIGNATURE_FOUND 0X04
701 uint8_t *p_key_unarmored = NULL;
703 p_key->psz_username = NULL;
704 p_key->sig.specific.v4.hashed_data = NULL;
705 p_key->sig.specific.v4.unhashed_data = NULL;
707 if( !( *pos & 0x80 ) )
708 { /* first byte is ASCII, unarmoring */
709 p_key_unarmored = (uint8_t*)malloc( i_key_len );
710 if( !p_key_unarmored )
712 int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
713 p_key_unarmored, i_key_len );
718 pos = p_key_unarmored;
719 max_pos = pos + i_len;
722 while( pos < max_pos )
724 if( !(*pos & 0x80) || *pos & 0x40 )
727 int i_type = packet_type( *pos );
729 int i_header_len = packet_header_len( *pos++ );
730 if( pos + i_header_len > max_pos ||
731 ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) )
734 int i_packet_len = scalar_number( pos, i_header_len );
737 if( pos + i_packet_len > max_pos )
742 case PUBLIC_KEY_PACKET:
743 i_status |= PUBLIC_KEY_FOUND;
744 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
748 case SIGNATURE_PACKET: /* we accept only v4 signatures here */
749 if( i_status & SIGNATURE_FOUND || !p_sig_issuer )
751 int i_ret = parse_signature_packet( &p_key->sig, pos,
753 if( i_ret == VLC_SUCCESS )
755 if( p_key->sig.version != 4 )
757 if( memcmp( p_key->sig.issuer_longid, p_sig_issuer, 8 ) )
759 free( p_key->sig.specific.v4.hashed_data );
760 free( p_key->sig.specific.v4.unhashed_data );
761 p_key->sig.specific.v4.hashed_data = NULL;
762 p_key->sig.specific.v4.unhashed_data = NULL;
765 i_status |= SIGNATURE_FOUND;
770 if( p_key->psz_username ) /* save only the first User ID */
772 i_status |= USER_ID_FOUND;
773 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
774 if( !p_key->psz_username )
777 memcpy( p_key->psz_username, pos, i_packet_len );
778 p_key->psz_username[i_packet_len] = '\0';
786 free( p_key_unarmored );
788 if( !( i_status & ( PUBLIC_KEY_FOUND | USER_ID_FOUND ) ) )
791 if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
797 if( p_key->sig.version == 4 )
799 free( p_key->sig.specific.v4.hashed_data );
800 free( p_key->sig.specific.v4.unhashed_data );
802 free( p_key->psz_username );
803 free( p_key_unarmored );
808 * return a sha1 hash of a file
810 static uint8_t *hash_sha1_from_file( const char *psz_file,
811 signature_packet_t *p_sig )
813 if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
816 FILE *f = utf8_fopen( psz_file, "r" );
820 uint8_t buffer[4096];
823 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
830 while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
831 gcry_md_write( hd, buffer, i_read );
833 if( p_sig->version == 3 )
835 gcry_md_putc( hd, p_sig->type );
836 gcry_md_write( hd, &p_sig->specific.v3.timestamp, 4 );
838 else if( p_sig->version == 4 )
840 gcry_md_putc( hd, p_sig->version );
841 gcry_md_putc( hd, p_sig->type );
842 gcry_md_putc( hd, p_sig->public_key_algo );
843 gcry_md_putc( hd, p_sig->digest_algo );
844 gcry_md_write( hd, p_sig->specific.v4.hashed_data_len, 2 );
845 size_t i_len = scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
846 gcry_md_write( hd, p_sig->specific.v4.hashed_data, i_len );
849 { /* RFC 4880 only tells about versions 3 and 4 */
857 uint8_t *p_tmp = (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1);
858 uint8_t *p_hash = malloc( 20 );
860 memcpy( p_hash, p_tmp, 20 );
866 * download a public key (the last one) from videolan server, and parse it
868 static public_key_t *download_key( vlc_object_t *p_this,
869 const uint8_t *p_longid, const uint8_t *p_signature_issuer )
872 if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
873 p_longid[0], p_longid[1], p_longid[2], p_longid[3],
874 p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
877 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
882 int64_t i_size = stream_Size( p_stream );
885 stream_Delete( p_stream );
889 uint8_t *p_buf = (uint8_t*)malloc( i_size );
892 stream_Delete( p_stream );
896 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
897 stream_Delete( p_stream );
899 if( i_read != (int)i_size )
901 msg_Dbg( p_this, "Couldn't read full GPG key" );
906 public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
913 memcpy( p_pkey->longid, p_longid, 8 );
915 int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
918 if( i_error != VLC_SUCCESS )
920 msg_Dbg( p_this, "Couldn't parse GPG key" );
929 * Generate a SHA1 hash on a public key, to verify a signature made on that hash
930 * Note that we need the signature (v4) to compute the hash
932 static uint8_t *key_sign_hash( public_key_t *p_pkey )
934 if( p_pkey->sig.version != 4 )
937 if( p_pkey->sig.type < GENERIC_KEY_SIGNATURE ||
938 p_pkey->sig.type > POSITIVE_KEY_SIGNATURE )
941 gcry_error_t error = 0;
944 error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
948 gcry_md_putc( hd, 0x99 );
950 size_t i_p_len = mpi_len( p_pkey->key.p );
951 size_t i_g_len = mpi_len( p_pkey->key.g );
952 size_t i_q_len = mpi_len( p_pkey->key.q );
953 size_t i_y_len = mpi_len( p_pkey->key.y );
955 size_t i_size = 6 + 2*4 + i_p_len + i_g_len + i_q_len + i_y_len;
957 gcry_md_putc( hd, (i_size >> 8) & 0xff );
958 gcry_md_putc( hd, i_size & 0xff );
960 gcry_md_putc( hd, p_pkey->key.version );
961 gcry_md_write( hd, p_pkey->key.timestamp, 4 );
962 gcry_md_putc( hd, p_pkey->key.algo );
964 gcry_md_write( hd, (uint8_t*)&p_pkey->key.p, 2 );
965 gcry_md_write( hd, (uint8_t*)&p_pkey->key.p + 2, i_p_len );
967 gcry_md_write( hd, (uint8_t*)&p_pkey->key.q, 2 );
968 gcry_md_write( hd, (uint8_t*)&p_pkey->key.q + 2, i_q_len );
970 gcry_md_write( hd, (uint8_t*)&p_pkey->key.g, 2 );
971 gcry_md_write( hd, (uint8_t*)&p_pkey->key.g + 2, i_g_len );
973 gcry_md_write( hd, (uint8_t*)&p_pkey->key.y, 2 );
974 gcry_md_write( hd, (uint8_t*)&p_pkey->key.y + 2, i_y_len );
976 gcry_md_putc( hd, 0xb4 );
978 size_t i_len = strlen((char*)p_pkey->psz_username);
980 gcry_md_putc( hd, (i_len << 24) & 0xff );
981 gcry_md_putc( hd, (i_len << 16) & 0xff );
982 gcry_md_putc( hd, (i_len << 8) & 0xff );
983 gcry_md_putc( hd, (i_len) & 0xff );
985 gcry_md_write( hd, p_pkey->psz_username, i_len );
987 size_t i_hashed_data_len =
988 scalar_number( p_pkey->sig.specific.v4.hashed_data_len, 2 );
990 gcry_md_putc( hd, p_pkey->sig.version );
991 gcry_md_putc( hd, p_pkey->sig.type );
992 gcry_md_putc( hd, p_pkey->sig.public_key_algo );
993 gcry_md_putc( hd, p_pkey->sig.digest_algo );
994 gcry_md_write( hd, p_pkey->sig.specific.v4.hashed_data_len, 2 );
995 gcry_md_write( hd, p_pkey->sig.specific.v4.hashed_data, i_hashed_data_len );
997 gcry_md_putc( hd, 0x04 );
998 gcry_md_putc( hd, 0xff );
1000 i_hashed_data_len += 6; /* hashed data + 6 bytes header */
1002 gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
1003 gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
1004 gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
1005 gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
1007 gcry_md_final( hd );
1009 uint8_t *p_tmp = gcry_md_read( hd, GCRY_MD_SHA1);
1012 p_tmp[0] != p_pkey->sig.hash_verification[0] ||
1013 p_tmp[1] != p_pkey->sig.hash_verification[1] )
1015 gcry_md_close( hd );
1019 uint8_t *p_hash = malloc( 20 );
1021 memcpy( p_hash, p_tmp, 20 );
1022 gcry_md_close( hd );
1027 /*****************************************************************************
1028 * Update_t functions
1029 *****************************************************************************/
1032 * Create a new update VLC struct
1034 * \param p_this the calling vlc_object
1035 * \return pointer to new update_t or NULL
1037 update_t *__update_New( vlc_object_t *p_this )
1042 p_update = (update_t *)malloc( sizeof( update_t ) );
1043 if( !p_update ) return NULL;
1045 vlc_mutex_init( &p_update->lock );
1047 p_update->p_libvlc = p_this->p_libvlc;
1049 p_update->release.psz_url = NULL;
1050 p_update->release.psz_desc = NULL;
1052 p_update->p_pkey = NULL;
1059 * Delete an update_t struct
1061 * \param p_update update_t* pointer
1064 void update_Delete( update_t *p_update )
1068 vlc_mutex_destroy( &p_update->lock );
1070 free( p_update->release.psz_url );
1071 free( p_update->release.psz_desc );
1072 free( p_update->p_pkey );
1078 * Empty the release struct
1080 * \param p_update update_t* pointer
1083 static void EmptyRelease( update_t *p_update )
1085 p_update->release.i_major = 0;
1086 p_update->release.i_minor = 0;
1087 p_update->release.i_revision = 0;
1089 FREENULL( p_update->release.psz_url );
1090 FREENULL( p_update->release.psz_desc );
1094 * Get the update file and parse it
1095 * p_update has to be locked when calling this function
1097 * \param p_update pointer to update struct
1098 * \return true if the update is valid and authenticated
1100 static bool GetUpdateFile( update_t *p_update )
1102 stream_t *p_stream = NULL;
1106 unsigned char extra;
1107 char *psz_line = NULL;
1108 char *psz_version_line = NULL;
1110 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
1113 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
1114 UPDATE_VLC_STATUS_URL );
1118 /* Try to read three lines */
1119 if( !( psz_line = stream_ReadLine( p_stream ) ) )
1121 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
1122 UPDATE_VLC_STATUS_URL );
1126 psz_version_line = psz_line;
1127 /* first line : version number */
1128 p_update->release.extra = 0;
1129 switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
1132 p_update->release.extra = extra;
1134 p_update->release.i_major = i_major;
1135 p_update->release.i_minor = i_minor;
1136 p_update->release.i_revision = i_revision;
1139 msg_Err( p_update->p_libvlc, "Update version false formated" );
1143 /* Second line : URL */
1144 if( !( psz_line = stream_ReadLine( p_stream ) ) )
1146 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
1147 UPDATE_VLC_STATUS_URL );
1150 p_update->release.psz_url = psz_line;
1153 /* Third line : description */
1154 if( !( psz_line = stream_ReadLine( p_stream ) ) )
1156 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
1157 UPDATE_VLC_STATUS_URL );
1160 p_update->release.psz_desc = psz_line;
1162 stream_Delete( p_stream );
1165 /* Now that we know the status is valid, we must download its signature
1166 * to authenticate it */
1167 signature_packet_t sign;
1168 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
1169 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
1171 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
1175 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
1177 msg_Err( p_update->p_libvlc, "Invalid signature type" );
1181 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
1182 if( !p_update->p_pkey )
1185 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
1186 p_update->p_pkey, NULL ) != VLC_SUCCESS )
1188 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
1189 FREENULL( p_update->p_pkey );
1193 memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
1195 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
1197 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
1198 public_key_t *p_new_pkey = download_key(
1199 VLC_OBJECT(p_update->p_libvlc),
1200 sign.issuer_longid, videolan_public_key_longid );
1203 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
1204 FREENULL( p_update->p_pkey );
1208 uint8_t *p_hash = key_sign_hash( p_new_pkey );
1211 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
1213 FREENULL( p_update->p_pkey );
1217 if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
1218 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
1221 msg_Info( p_update->p_libvlc, "Key authenticated" );
1222 free( p_update->p_pkey );
1223 p_update->p_pkey = p_new_pkey;
1228 msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
1234 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
1237 gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
1238 FREENULL( psz_version_line );
1239 if( sign.type == TEXT_SIGNATURE )
1240 gcry_md_putc( hd, '\r' );
1241 gcry_md_putc( hd, '\n' );
1242 gcry_md_write( hd, p_update->release.psz_url,
1243 strlen( p_update->release.psz_url ) );
1244 if( sign.type == TEXT_SIGNATURE )
1245 gcry_md_putc( hd, '\r' );
1246 gcry_md_putc( hd, '\n' );
1247 gcry_md_write( hd, p_update->release.psz_desc,
1248 strlen( p_update->release.psz_desc ) );
1249 if( sign.type == TEXT_SIGNATURE )
1250 gcry_md_putc( hd, '\r' );
1251 gcry_md_putc( hd, '\n' );
1254 if( sign.version == 3 )
1256 gcry_md_putc( hd, sign.type );
1257 gcry_md_write( hd, &sign.specific.v3.timestamp, 4 );
1259 else if( sign.version == 4 )
1261 gcry_md_putc( hd, sign.version );
1262 gcry_md_putc( hd, sign.type );
1263 gcry_md_putc( hd, sign.public_key_algo );
1264 gcry_md_putc( hd, sign.digest_algo );
1265 gcry_md_write( hd, sign.specific.v4.hashed_data_len, 2 );
1266 size_t i_len = scalar_number( sign.specific.v4.hashed_data_len, 2 );
1267 gcry_md_write( hd, sign.specific.v4.hashed_data, i_len );
1268 gcry_md_putc( hd, 0x04 );
1269 gcry_md_putc( hd, 0xFF );
1271 i_len += 6; /* hashed data + 6 bytes header */
1273 gcry_md_putc( hd, (i_len << 24) & 0xff);
1274 gcry_md_putc( hd, (i_len << 16) &0xff );
1275 gcry_md_putc( hd, (i_len << 8) & 0xff );
1276 gcry_md_putc( hd, (i_len) & 0xff );
1279 { /* RFC 4880 only tells about versions 3 and 4 */
1280 msg_Warn( p_update->p_libvlc, "Invalid signature version %d",
1285 gcry_md_final( hd );
1287 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );
1289 if( p_hash[0] != sign.hash_verification[0] ||
1290 p_hash[1] != sign.hash_verification[1] )
1292 msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
1296 if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
1299 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
1304 msg_Info( p_update->p_libvlc, "Status file authenticated" );
1305 gcry_md_close( hd );
1310 gcry_md_close( hd );
1313 stream_Delete( p_stream );
1314 free( psz_version_line );
1320 * Struct to launch the check in an other thread
1326 void (*pf_callback)( void *, bool );
1328 } update_check_thread_t;
1330 void update_CheckReal( update_check_thread_t *p_uct );
1335 * \param p_update pointer to update struct
1336 * \param pf_callback pointer to a function to call when the update_check is finished
1337 * \param p_data pointer to some datas to give to the callback
1340 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
1344 update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
1345 sizeof( update_check_thread_t ) );
1346 if( !p_uct ) return;
1348 p_uct->p_update = p_update;
1349 p_uct->pf_callback = pf_callback;
1350 p_uct->p_data = p_data;
1352 vlc_thread_create( p_uct, "check for update", update_CheckReal,
1353 VLC_THREAD_PRIORITY_LOW, false );
1356 void update_CheckReal( update_check_thread_t *p_uct )
1359 vlc_mutex_lock( &p_uct->p_update->lock );
1361 EmptyRelease( p_uct->p_update );
1362 b_ret = GetUpdateFile( p_uct->p_update );
1363 vlc_mutex_unlock( &p_uct->p_update->lock );
1365 if( p_uct->pf_callback )
1366 (p_uct->pf_callback)( p_uct->p_data, b_ret );
1368 vlc_object_release( p_uct );
1372 * Compare a given release's version number to the current VLC's one
1374 * \param p_update structure
1375 * \return true if we have to upgrade to the given version to be up to date
1377 bool update_NeedUpgrade( update_t *p_update )
1381 return p_update->release.i_major < *PACKAGE_VERSION_MAJOR - '0' ||
1382 p_update->release.i_minor < *PACKAGE_VERSION_MINOR - '0' ||
1383 p_update->release.i_revision < *PACKAGE_VERSION_REVISION - '0' ||
1384 p_update->release.extra < *PACKAGE_VERSION_EXTRA;
1388 * Convert a long int size in bytes to a string
1390 * \param l_size the size in bytes
1391 * \return the size as a string
1393 static char *size_str( long int l_size )
1395 char *psz_tmp = NULL;
1398 i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
1399 else if( l_size >> 20 )
1400 i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1401 else if( l_size >> 10 )
1402 i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1404 i_retval = asprintf( &psz_tmp, "%ld B", l_size );
1406 return i_retval == -1 ? NULL : psz_tmp;
1411 * Struct to launch the download in a thread
1418 } update_download_thread_t;
1420 void update_DownloadReal( update_download_thread_t *p_udt );
1423 * Download the file given in the update_t
1425 * \param p_update structure
1426 * \param dir to store the download file
1429 void update_Download( update_t *p_update, char *psz_destdir )
1433 update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1434 sizeof( update_download_thread_t ) );
1437 msg_Err( p_update->p_libvlc, "out of memory" );
1441 p_udt->p_update = p_update;
1442 p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1444 vlc_thread_create( p_udt, "download update", update_DownloadReal,
1445 VLC_THREAD_PRIORITY_LOW, false );
1448 void update_DownloadReal( update_download_thread_t *p_udt )
1452 long int l_downloaded = 0;
1454 char *psz_status = NULL;
1455 char *psz_downloaded = NULL;
1456 char *psz_size = NULL;
1457 char *psz_destfile = NULL;
1458 char *psz_tmpdestfile = NULL;
1460 FILE *p_file = NULL;
1461 stream_t *p_stream = NULL;
1462 void* p_buffer = NULL;
1465 update_t *p_update = p_udt->p_update;
1466 char *psz_destdir = p_udt->psz_destdir;
1468 /* Open the stream */
1469 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1472 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1476 /* Get the stream size */
1477 l_size = stream_Size( p_stream );
1479 /* Get the file name and open it*/
1480 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1481 if( !psz_tmpdestfile )
1483 msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1487 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1490 p_file = utf8_fopen( psz_destfile, "w" );
1493 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1497 /* Create a buffer and fill it with the downloaded file */
1498 p_buffer = (void *)malloc( 1 << 10 );
1502 psz_size = size_str( l_size );
1503 if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",
1504 p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1506 i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1510 while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1511 !intf_ProgressIsCancelled( p_udt, i_progress ) )
1513 if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
1515 msg_Err( p_udt, "Failed to write into %s", psz_destfile );
1519 l_downloaded += i_read;
1520 psz_downloaded = size_str( l_downloaded );
1521 f_progress = 100.0*(float)l_downloaded/(float)l_size;
1523 if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done",
1524 p_update->release.psz_url, psz_downloaded, psz_size,
1525 f_progress ) != -1 )
1527 intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1530 free( psz_downloaded );
1533 /* Finish the progress bar or delete the file if the user had canceled */
1537 if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
1539 if( asprintf( &psz_status, "%s\nDone %s (100.0%%)",
1540 p_update->release.psz_url, psz_size ) != -1 )
1542 intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1548 utf8_unlink( psz_destfile );
1552 signature_packet_t sign;
1553 if( download_signature( VLC_OBJECT( p_udt ), &sign,
1554 p_update->release.psz_url ) != VLC_SUCCESS )
1556 utf8_unlink( psz_destfile );
1558 intf_UserFatal( p_udt, true, _("File can not be verified"),
1559 _("It was not possible to download a cryptographic signature for "
1560 "downloaded file \"%s\", and so VLC deleted it."),
1562 msg_Err( p_udt, "Couldn't download signature of downloaded file" );
1566 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
1568 utf8_unlink( psz_destfile );
1569 msg_Err( p_udt, "Invalid signature issuer" );
1570 intf_UserFatal( p_udt, true, _("Invalid signature"),
1571 _("The cryptographic signature for downloaded file \"%s\" was "
1572 "invalid and couldn't be used to securely verify it, and so "
1578 if( sign.type != BINARY_SIGNATURE )
1580 utf8_unlink( psz_destfile );
1581 msg_Err( p_udt, "Invalid signature type" );
1582 intf_UserFatal( p_udt, true, _("Invalid signature"),
1583 _("The cryptographic signature for downloaded file \"%s\" was "
1584 "invalid and couldn't be used to securely verify it, and so "
1590 uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
1593 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
1594 utf8_unlink( psz_destfile );
1595 intf_UserFatal( p_udt, true, _("File not verifiable"),
1596 _("It was not possible to securely verify downloaded file \"%s\", "
1597 "and so VLC deleted it."),
1603 if( p_hash[0] != sign.hash_verification[0] ||
1604 p_hash[1] != sign.hash_verification[1] )
1606 utf8_unlink( psz_destfile );
1607 intf_UserFatal( p_udt, true, _("File corrupted"),
1608 _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
1610 msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
1615 if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
1618 utf8_unlink( psz_destfile );
1619 intf_UserFatal( p_udt, true, _("File corrupted"),
1620 _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
1622 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
1627 msg_Info( p_udt, "%s authenticated", psz_destfile );
1632 stream_Delete( p_stream );
1635 free( psz_destdir );
1636 free( psz_destfile );
1640 vlc_object_release( p_udt );
1643 update_release_t *update_GetRelease( update_t *p_update )
1645 return &p_update->release;
1649 update_t *__update_New( vlc_object_t *p_this )
1655 void update_Delete( update_t *p_update )
1660 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
1663 (void)p_update; (void)pf_callback; (void)p_data;
1666 bool update_NeedUpgrade( update_t *p_update )
1672 void update_Download( update_t *p_update, char *psz_destdir )
1674 (void)p_update; (void)psz_destdir;
1677 update_release_t *update_GetRelease( update_t *p_update )