update.c 40.6 KB
Newer Older
1
/*****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
2
 * update.c: VLC update checking and downloading
3
 *****************************************************************************
4
 * Copyright © 2005-2008 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
Rafaël Carré's avatar
Rafaël Carré committed
8 9
 *          Rémi Duraffort <ivoire at via.ecp.fr>
            Rafaël Carré <funman@videolanorg>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either release 2 of the License, or
 * (at your option) any later release.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/**
 *   \file
28
 *   This file contains functions related to VLC update management
29 30 31 32 33
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/
34

35 36 37 38
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

39 40
#include <vlc/vlc.h>

41 42
#ifdef UPDATE_CHECK

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
43
#include <assert.h>
44

Clément Stenac's avatar
Clément Stenac committed
45
#include <vlc_update.h>
46
#include <vlc_pgpkey.h>
Clément Stenac's avatar
Clément Stenac committed
47 48
#include <vlc_stream.h>
#include <vlc_interface.h>
49

50

51 52 53 54
/*****************************************************************************
 * Misc defines
 *****************************************************************************/

Rafaël Carré's avatar
Rafaël Carré committed
55 56 57 58 59 60 61 62
/*
 * Here is the format of these "status files" :
 * First line is the last version: "X.Y.Ze" where:
 *      * X is the major number
 *      * Y is the minor number
 *      * Z is the revision number
 *      * e is an OPTIONAL extra letter
 *      * AKA "0.8.6d" or "0.9.0"
63
 * Second line is an url of the binary for this last version
Rafaël Carré's avatar
Rafaël Carré committed
64 65 66
 * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
 */

67
#if defined( UNDER_CE )
68
#   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
69
#elif defined( WIN32 )
70
#   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
71 72
#elif defined( __APPLE__ )
#   if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
73
#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
74
#   else
75
#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
76 77
#   endif
#elif defined( SYS_BEOS )
78
#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
79
#else
80
#   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
81 82
#endif

83

84 85 86
/*****************************************************************************
 * Local Prototypes
 *****************************************************************************/
87
static void EmptyRelease( update_t *p_update );
88
static vlc_bool_t GetUpdateFile( update_t *p_update );
89 90
static int CompareReleases( const struct update_release_t *p1,
                            const struct update_release_t *p2 );
91 92
static char * size_str( long int l_size );

93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*****************************************************************************
 * OpenPGP functions
 *****************************************************************************/

#define packet_type( c ) ( ( c & 0x3c ) >> 2 )      /* 0x3C = 00111100 */
#define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */

static inline int scalar_number( uint8_t *p, int header_len )
{
    if( header_len == 1 )
        return( p[0] );
    else if( header_len == 2 )
        return( (p[0] << 8) + p[1] );
    else if( header_len == 4 )
        return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
    else
        abort();
}

/* number of data bytes in a MPI */
#define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )

116
/*
117 118 119 120 121 122
 * fill a public_key_packet_t structure from public key packet data
 * verify that it is a version 4 public key packet, using DSA
 */
static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
                                    size_t i_packet_len )
{
123
    if( i_packet_len > 418 )
124 125 126 127 128 129 130 131 132 133 134 135 136
        return VLC_EGENERIC;

    p_key->version   = *p_buf++;
    if( p_key->version != 4 )
        return VLC_EGENERIC;

    /* warn when timestamp is > date ? */
    memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;

    p_key->algo      = *p_buf++;
    if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
        return VLC_EGENERIC;

137 138
    int i_p_len = mpi_len( p_buf );
    if( i_p_len > 128 )
139
        return VLC_EGENERIC;
140 141 142 143 144 145
    else
    {
        memcpy( p_key->p, p_buf, 2+i_p_len ); p_buf += 2+i_p_len;
        if( i_p_len < 128 )
            memmove( p_key->q, p_key->p + 2+i_p_len, 2+20 + 2+128 + 2+128 );
    }
146

147 148
    int i_q_len = mpi_len( p_buf );
    if( i_q_len > 20 )
149
        return VLC_EGENERIC;
150 151 152 153 154 155
    else
    {
        memcpy( p_key->q, p_buf, 2+i_q_len );  p_buf += 2+i_q_len;
        if( i_p_len < 20 )
            memmove( p_key->g, p_key->q + 2+i_q_len, 2+128 + 2+128 );
    }
156

157 158
    int i_g_len = mpi_len( p_buf );
    if( i_g_len > 128 )
159
        return VLC_EGENERIC;
160 161 162 163 164 165
    else
    {
        memcpy( p_key->g, p_buf, 2+i_g_len ); p_buf += 2+i_g_len;
        if( i_g_len < 128 )
            memmove( p_key->y, p_key->g + 2+i_g_len, 2+128 );
    }
166

167 168
    int i_y_len = mpi_len( p_buf );
    if( i_y_len > 128 )
169
        return VLC_EGENERIC;
170 171
    else
        memcpy( p_key->y, p_buf, 2+i_y_len );
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    return VLC_SUCCESS;
}

/*
 * fill a signature_packet_v4_t from signature packet data
 * verify that it was used with a DSA public key, using SHA-1 digest
 */
static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
                                      uint8_t *p_buf, size_t i_sig_len )
{
    if( i_sig_len < 54 )
        return VLC_EGENERIC;

    p_sig->version = *p_buf++;
    if( p_sig->version != 4 )
        return VLC_EGENERIC;

    p_sig->type = *p_buf++;
    if( p_sig->type < GENERIC_KEY_SIGNATURE ||
        p_sig->type > POSITIVE_KEY_SIGNATURE )
        return VLC_EGENERIC;

    p_sig->public_key_algo = *p_buf++;
    if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
        return VLC_EGENERIC;

    p_sig->digest_algo = *p_buf++;
    if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
        return VLC_EGENERIC;

    memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;

    size_t i_pos = 6;
    size_t i_hashed_data_len = scalar_number( p_sig->hashed_data_len, 2 );
    i_pos += i_hashed_data_len;
208
    if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total,
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                              * + the unhashed data length (2 bytes)
                              * + the hash verification (2 bytes) */
        return VLC_EGENERIC;

    p_sig->hashed_data = (uint8_t*) malloc( i_hashed_data_len );
    if( !p_sig->hashed_data )
        return VLC_ENOMEM;
    memcpy( p_sig->hashed_data, p_buf, i_hashed_data_len );
    p_buf += i_hashed_data_len;

    memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;

    size_t i_unhashed_data_len = scalar_number( p_sig->unhashed_data_len, 2 );
    i_pos += 2 + i_unhashed_data_len;
    if( i_pos != i_sig_len - 46 )
    {
        free( p_sig->hashed_data );
        return VLC_EGENERIC;
    }

    p_sig->unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
    if( !p_sig->unhashed_data )
    {
        free( p_sig->hashed_data );
        return VLC_ENOMEM;
    }
    memcpy( p_sig->unhashed_data, p_buf, i_unhashed_data_len );
    p_buf += i_unhashed_data_len;

    memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;

240 241
    int i_r_len = mpi_len( p_buf );
    if( i_r_len > 20 )
242 243 244 245 246
    {
        free( p_sig->hashed_data );
        free( p_sig->unhashed_data );
        return VLC_EGENERIC;
    }
247 248 249 250 251
    else
    {
        memcpy( p_sig->r, p_buf, 2 + i_r_len );
        p_buf += 2 + i_r_len;
    }
252

253 254
    int i_s_len = mpi_len( p_buf );
    if( i_s_len > 20 )
255 256 257 258 259
    {
        free( p_sig->hashed_data );
        free( p_sig->unhashed_data );
        return VLC_EGENERIC;
    }
260 261 262 263 264
    else
    {
        memcpy( p_sig->s, p_buf, 2 + i_s_len );
        p_buf += 2 + i_s_len;
    }
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

    return VLC_SUCCESS;
}

/*
 * crc_octets() was lamely copied from rfc 2440
 * Copyright (C) The Internet Society (1998).  All Rights Reserved.
 */
#define CRC24_INIT 0xB704CEL
#define CRC24_POLY 0x1864CFBL

static long crc_octets( uint8_t *octets, size_t len )
{
    long crc = CRC24_INIT;
    int i;
    while (len--)
    {
        crc ^= (*octets++) << 16;
        for (i = 0; i < 8; i++)
        {
            crc <<= 1;
            if (crc & 0x1000000)
                crc ^= CRC24_POLY;
        }
    }
    return crc & 0xFFFFFFL;
}

/*
 * Transform an armored document in binary format
 * Used on public keys and signatures
 */
static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
                        uint8_t *p_obuf, size_t i_obuf_len )
{
    char *p_ipos = p_ibuf;
    uint8_t *p_opos = p_obuf;
    int i_end = 0;
    int i_header_skipped = 0;

305
    while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    {
        if( *p_ipos == '\r' || *p_ipos == '\n' )
        {
            p_ipos++;
            continue;
        }

        size_t i_line_len = strcspn( p_ipos, "\r\n" );
        if( i_line_len == 0 )
            continue;

        if( !i_header_skipped )
        {
            if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
                i_header_skipped = 1;

            p_ipos += i_line_len + 1;
            continue;
        }
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
        if( !strncmp( p_ipos, "Version:", 8 ) )
        {
            p_ipos += i_line_len + 1;
            continue;
        }

        if( p_ipos[i_line_len - 1] == '=' )
        {
            i_end = 1;
            p_ipos[i_line_len - 1] = '\0';
        }
        else
            p_ipos[i_line_len] = '\0';

        p_opos += vlc_b64_decode_binary_to_buffer(  p_opos,
341
                        p_obuf - p_opos + i_obuf_len, p_ipos );
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        p_ipos += i_line_len + 1;
    }

    /* XXX: the CRC is OPTIONAL, really require it ? */
    if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
        return 0;

    uint8_t p_crc[3];
    if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
        return 0;

    long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
    long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];

    return l_crc2 == l_crc ? p_opos - p_obuf : 0;
}

/*
 * Download the signature associated to a document or a binary file.
361
 * We're given the file's url, we just append ".asc" to it and download
362 363
 */
static int download_signature(  vlc_object_t *p_this,
364 365
                                signature_packet_v3_t *p_sig,
                                const char *psz_url )
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
{
    char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
    if( !psz_sig )
        return VLC_ENOMEM;

    strcpy( psz_sig, psz_url );
    strcat( psz_sig, ".asc" );

    stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
    free( psz_sig );

    if( !p_stream )
        return VLC_ENOMEM;

    int64_t i_size = stream_Size( p_stream );
381
    if( i_size <= 65 ) /* binary format signature */
382
    {
383
        msg_Dbg( p_this, "Downloading unarmored signature" );
384 385 386
        int i_read = stream_Read( p_stream, p_sig, (int)i_size );
        stream_Delete( p_stream );
        if( i_read != i_size )
387 388
        {
            msg_Dbg( p_this, "Couldn't read full signature" );
389
            return VLC_EGENERIC;
390
        }
391 392 393 394
        else
            return VLC_SUCCESS;
    }

395
    msg_Dbg( p_this, "Downloading armored signature" );
396 397 398 399 400 401
    char *p_buf = (char*)malloc( i_size );
    if( !p_buf )
    {
        stream_Delete( p_stream );
        return VLC_ENOMEM;
    }
402

403 404 405 406 407 408
    int i_read = stream_Read( p_stream, p_buf, (int)i_size );

    stream_Delete( p_stream );

    if( i_read != i_size )
    {
409
        msg_Dbg( p_this, "Couldn't read full signature" );
410 411 412
        free( p_buf );
        return VLC_EGENERIC;
    }
413

414 415 416
    int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
    free( p_buf );

417
    if( i_bytes > 65 )
418
    {
419
        msg_Dbg( p_this, "Signature is too big: %d bytes", i_bytes );
420
        return VLC_EGENERIC;
421
    }
422
    else
423 424 425 426 427 428 429 430 431 432 433
    {
        int i_r_len = mpi_len( p_sig->r );
        if( i_r_len > 20 )
        {
            msg_Dbg( p_this, "Signature invalid" );
            return VLC_EGENERIC;
        }
        else if( i_r_len < 20 )
            /* move s to the right place if r is less than 20 bytes */
            memmove( p_sig->s, p_sig->r + 2 + i_r_len, 20 + 2 );

434
        return VLC_SUCCESS;
435
    }
436 437 438 439 440
}

/*
 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
 */
441
static int verify_signature( uint8_t *p_r, uint8_t *p_s,
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        public_key_packet_t *p_key, uint8_t *p_hash )
{
    /* the data to be verified (a SHA-1 hash) */
    const char *hash_sexp_s = "(data(flags raw)(value %m))";
    /* the public key */
    const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
    /* the signature */
    const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";

    size_t erroff;
    gcry_mpi_t p, q, g, y, r, s, hash;
    p = q = g = y = r = s = hash = NULL;
    gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
    key_sexp = hash_sexp = sig_sexp = NULL;

457 458 459 460 461 462 463 464
    int i_p_len = mpi_len( p_key->p );
    int i_q_len = mpi_len( p_key->q );
    int i_g_len = mpi_len( p_key->g );
    int i_y_len = mpi_len( p_key->y );
    if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, i_p_len, NULL ) ||
        gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, i_q_len, NULL ) ||
        gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, i_g_len, NULL ) ||
        gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, i_y_len, NULL ) ||
465 466 467
        gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
        goto problem;

468 469 470 471
    int i_r_len = mpi_len( p_r );
    int i_s_len = mpi_len( p_s );
    if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, i_r_len, NULL ) ||
        gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, i_s_len, NULL ) ||
472 473 474
        gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
        goto problem;

475 476
    int i_hash_len = 20;
    if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
        gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
        goto problem;

    if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
        goto problem;

    return VLC_SUCCESS;

problem:
    if( p ) gcry_mpi_release( p );
    if( q ) gcry_mpi_release( q );
    if( g ) gcry_mpi_release( g );
    if( y ) gcry_mpi_release( y );
    if( r ) gcry_mpi_release( r );
    if( s ) gcry_mpi_release( s );
    if( hash ) gcry_mpi_release( hash );
    if( key_sexp ) gcry_sexp_release( key_sexp );
    if( sig_sexp ) gcry_sexp_release( sig_sexp );
    if( hash_sexp ) gcry_sexp_release( hash_sexp );
    return VLC_EGENERIC;
}

/*
 * Return the long id (8 bytes) of the public key used to generate a signature
 */
static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
{
    uint8_t *p = p_sig->unhashed_data;
    uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );

    while( p < max_pos )
    {
        int i_subpacket_len = *p < 192 ? *p++ :
                *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
                ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;

        if( p >= max_pos - 1 )
            return NULL;

        if( *p == ISSUER_SUBPACKET )
            return p+1;
        else
            p += i_subpacket_len;
    }
    return NULL;
}

/*
 * fill a public_key_t with public key data, including:
 *   * public key packet
 *   * signature packet issued by key which long id is p_sig_issuer
 *   * user id packet
 */
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 )
{
    uint8_t *pos = (uint8_t*) p_key_data;
    uint8_t *max_pos = pos + i_key_len;

    int i_status = 0;
#define PUBLIC_KEY_FOUND    0x01
#define USER_ID_FOUND       0x02
#define SIGNATURE_FOUND     0X04

    uint8_t *p_key_unarmored = NULL;

    signature_packet_v4_t sig;

    p_key->psz_username = NULL;
    p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;

    if( !( *pos & 0x80 ) )
    {   /* first byte is ASCII, unarmoring */
        p_key_unarmored = (uint8_t*)malloc( i_key_len );
        if( !p_key_unarmored )
            return VLC_ENOMEM;
        int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
                                 p_key_unarmored, i_key_len );

        if( i_len == 0 )
            goto error;

        pos = p_key_unarmored;
        max_pos = pos + i_len;
    }

    while( pos < max_pos )
    {
        if( !(*pos & 0x80) || *pos & 0x40 )
            goto error;

        int i_type = packet_type( *pos );

        int i_header_len = packet_header_len( *pos++ );
        if( pos + i_header_len > max_pos )
            goto error;

        int i_packet_len = scalar_number( pos, i_header_len );
        pos += i_header_len;

        if( pos + i_packet_len > max_pos )
            goto error;

        switch( i_type )
        {
            uint8_t *p_issuer;

            case PUBLIC_KEY_PACKET:
                i_status |= PUBLIC_KEY_FOUND;
                if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
                    goto error;
                break;

            case SIGNATURE_PACKET:
                if( !p_sig_issuer || i_status & SIGNATURE_FOUND ||
                    parse_signature_v4_packet( &sig, pos, i_packet_len ) != VLC_SUCCESS )
                    break;
                p_issuer = get_issuer_from_signature_v4( &sig );
                if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
                {
                    memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
                    i_status |= SIGNATURE_FOUND;
                }
                else
                {
                    free( sig.hashed_data );
                    free( sig.unhashed_data );
                }
                break;

            case USER_ID_PACKET:
                if( p_key->psz_username ) /* save only the first User ID */
                    break;
                i_status |= USER_ID_FOUND;
                p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
                if( !p_key->psz_username )
                    goto error;

                memcpy( p_key->psz_username, pos, i_packet_len );
                p_key->psz_username[i_packet_len] = '\0';
                break;
617

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
            default:
                break;
        }
        pos += i_packet_len;
    }
    free( p_key_unarmored );

    if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
        return VLC_EGENERIC;

    if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
        return VLC_EGENERIC;

    return VLC_SUCCESS;

error:
    free( p_key->sig.hashed_data );
    free( p_key->sig.unhashed_data );
    free( p_key->psz_username );
    free( p_key_unarmored );
    return VLC_EGENERIC;
}

/*
 * return a sha1 hash of a file
 */
static uint8_t *hash_sha1_from_file( const char *psz_file,
                            signature_packet_v3_t *p_sig )
{
    FILE *f = utf8_fopen( psz_file, "r" );
    if( !f )
        return NULL;

651
    uint8_t buffer[4096];
652 653 654 655 656 657

    gcry_md_hd_t hd;
    if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
    {
        fclose( f );
        return NULL;
658
    }
659 660 661 662 663 664 665 666 667 668 669

    size_t i_read;
    while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
        gcry_md_write( hd, buffer, i_read );

    gcry_md_putc( hd, p_sig->type );
    gcry_md_write( hd, &p_sig->timestamp, 4 );

    fclose( f );
    gcry_md_final( hd );

Antoine Cellerier's avatar
Antoine Cellerier committed
670 671 672 673
    uint8_t *p_hash = (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1);
    p_hash = strdup( p_hash );
    gcry_md_close( hd );
    return p_hash;
674 675 676 677 678 679 680 681
}

/*
 * download a public key (the last one) from videolan server, and parse it
 */
static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
{
    char *psz_url;
682 683 684
    if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
                    p_longid[0], p_longid[1], p_longid[2], p_longid[3],
                    p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
        return NULL;

    stream_t *p_stream = stream_UrlNew( p_this, psz_url );
    free( psz_url );
    if( !p_stream )
        return NULL;

    int64_t i_size = stream_Size( p_stream );
    if( i_size < 0 )
    {
        stream_Delete( p_stream );
        return NULL;
    }

    uint8_t *p_buf = (uint8_t*)malloc( i_size );
    if( !p_buf )
    {
        stream_Delete( p_stream );
        return NULL;
    }

    int i_read = stream_Read( p_stream, p_buf, (int)i_size );
    stream_Delete( p_stream );

    if( i_read != (int)i_size )
    {
711
        msg_Dbg( p_this, "Couldn't read full GPG key" );
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
        free( p_buf );
        return NULL;
    }

    public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
    if( !p_pkey )
    {
        free( p_buf );
        return NULL;
    }

    int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
    free( p_buf );

    if( i_error != VLC_SUCCESS )
    {
728
        msg_Dbg( p_this, "Couldn't parse GPG key" );
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
        free( p_pkey );
        return NULL;
    }

    return p_pkey;
}

/*
 * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
 * Note that we need the signature to compute the hash
 */
static uint8_t *key_sign_hash( public_key_t *p_pkey )
{
    gcry_error_t error = 0;
    gcry_md_hd_t hd;

    error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
    if( error )
        return NULL;

    gcry_md_putc( hd, 0x99 );

    gcry_md_putc( hd, (418 >> 8) & 0xff );
    gcry_md_putc( hd, 418 & 0xff );

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    gcry_md_write( hd, (uint8_t*)&p_pkey->key, 6 ); /* version,timestamp,algo */

    int i_p_len = mpi_len( p_pkey->key.p );
    gcry_md_write( hd, (uint8_t*)&p_pkey->key.p, 2 );
    gcry_md_write( hd, (uint8_t*)&p_pkey->key.p + 2, i_p_len );

    int i_g_len = mpi_len( p_pkey->key.g );
    gcry_md_write( hd, (uint8_t*)&p_pkey->key.g, 2 );
    gcry_md_write( hd, (uint8_t*)&p_pkey->key.g + 2, i_g_len );

    int i_q_len = mpi_len( p_pkey->key.q );
    gcry_md_write( hd, (uint8_t*)&p_pkey->key.q, 2 );
    gcry_md_write( hd, (uint8_t*)&p_pkey->key.q + 2, i_q_len );

    int i_y_len = mpi_len( p_pkey->key.y );
    gcry_md_write( hd, (uint8_t*)&p_pkey->key.y, 2 );
    gcry_md_write( hd, (uint8_t*)&p_pkey->key.y + 2, i_y_len );
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

    gcry_md_putc( hd, 0xb4 );

    int i_len = strlen((char*)p_pkey->psz_username);

    gcry_md_putc( hd, (i_len << 24) & 0xff );
    gcry_md_putc( hd, (i_len << 16) & 0xff );
    gcry_md_putc( hd, (i_len << 8) & 0xff );
    gcry_md_putc( hd, (i_len) & 0xff );

    gcry_md_write( hd, p_pkey->psz_username, i_len );

    size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );

    gcry_md_putc( hd, p_pkey->sig.version );
    gcry_md_putc( hd, p_pkey->sig.type );
    gcry_md_putc( hd, p_pkey->sig.public_key_algo );
    gcry_md_putc( hd, p_pkey->sig.digest_algo );
    gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
    gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );

    gcry_md_putc( hd, 0x04 );
    gcry_md_putc( hd, 0xff );

    i_hashed_data_len += 6; /* hashed data + 6 bytes header */

    gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
    gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
    gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
    gcry_md_putc( hd, (i_hashed_data_len) & 0xff );

    gcry_md_final( hd );

    uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);

    if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
        p_hash[1] != p_pkey->sig.hash_verification[1] )
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
809
        gcry_md_close( hd );
810 811 812
        return NULL;
    }

Antoine Cellerier's avatar
Antoine Cellerier committed
813 814
    p_hash = strdup( p_hash );
    gcry_md_close( hd );
815 816 817
    return p_hash;
}

818 819 820 821 822 823 824 825 826 827 828 829 830 831

/*****************************************************************************
 * Update_t functions
 *****************************************************************************/

/**
 * Create a new update VLC struct
 *
 * \param p_this the calling vlc_object
 * \return pointer to new update_t or NULL
 */
update_t *__update_New( vlc_object_t *p_this )
{
    update_t *p_update;
832
    assert( p_this );
833 834

    p_update = (update_t *)malloc( sizeof( update_t ) );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
835
    if( !p_update ) return NULL;
836 837 838

    vlc_mutex_init( p_this, &p_update->lock );

839
    p_update->p_libvlc = p_this->p_libvlc;
840

841 842
    p_update->release.psz_url = NULL;
    p_update->release.psz_desc = NULL;
843

844
    p_update->p_pkey = NULL;
845

846
    return p_update;
847 848 849 850 851 852 853 854 855 856
}

/**
 * Delete an update_t struct
 *
 * \param p_update update_t* pointer
 * \return nothing
 */
void update_Delete( update_t *p_update )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
857 858
    assert( p_update );

859 860
    vlc_mutex_destroy( &p_update->lock );

861 862 863
    free( p_update->release.psz_url );
    free( p_update->release.psz_desc );
    free( p_update->p_pkey );
864 865

    free( p_update );
866 867 868
}

/**
869
 * Empty the release struct
870
 *
871
 * \param p_update update_t* pointer
872 873
 * \return nothing
 */
874
static void EmptyRelease( update_t *p_update )
875
{
876 877 878
    p_update->release.i_major = 0;
    p_update->release.i_minor = 0;
    p_update->release.i_revision = 0;
879

880 881
    FREENULL( p_update->release.psz_url );
    FREENULL( p_update->release.psz_desc );
882 883 884
}

/**
885
 * Get the update file and parse it
886
 * *p_update has to be locked when calling this function
887 888
 *
 * \param p_update pointer to update struct
889
 * \return VLC_TRUE if the update is valid and authenticated
890
 */
891
static vlc_bool_t GetUpdateFile( update_t *p_update )
892 893
{
    stream_t *p_stream = NULL;
894 895 896
    int i_major = 0;
    int i_minor = 0;
    int i_revision = 0;
Rafaël Carré's avatar
Rafaël Carré committed
897
    unsigned char extra;
898
    char *psz_line = NULL;
899
    char *psz_version_line = NULL;
900

901
    p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
902 903
    if( !p_stream )
    {
904
        msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
905 906 907 908
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }

909 910
    /* Try to read three lines */
    if( !( psz_line = stream_ReadLine( p_stream ) ) )
911
    {
912
        msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
913 914 915 916
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }

917
    psz_version_line = psz_line;
918
    /* first line : version number */
Rafaël Carré's avatar
Rafaël Carré committed
919 920
    p_update->release.extra = 0;
    switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
921
    {
Rafaël Carré's avatar
Rafaël Carré committed
922 923 924 925 926 927 928 929 930 931
        case 4:
            p_update->release.extra = extra;
        case 3:
            p_update->release.i_major = i_major;
            p_update->release.i_minor = i_minor;
            p_update->release.i_revision = i_revision;
            break;
        default:
            msg_Err( p_update->p_libvlc, "Update version false formated" );
            goto error;
932
    }
933

934 935 936 937 938 939 940 941
    /* Second line : URL */
    if( !( psz_line = stream_ReadLine( p_stream ) ) )
    {
        msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }
    p_update->release.psz_url = psz_line;
942 943


944 945 946 947 948 949
    /* Third line : description */
    if( !( psz_line = stream_ReadLine( p_stream ) ) )
    {
        msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
                 UPDATE_VLC_STATUS_URL );
        goto error;
950
    }
951
    p_update->release.psz_desc = psz_line;
952

953 954 955
    stream_Delete( p_stream );
    p_stream = NULL;

956
    /* Now that we know the status is valid, we must download its signature
957 958
     * to authenticate it */
    signature_packet_v3_t sign;
959
    if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
            UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
    {
        msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
        goto error;
    }

    if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
    {
        msg_Err( p_update->p_libvlc, "Invalid signature type" );
        goto error;
    }

    p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
    if( !p_update->p_pkey )
        goto error;

    if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
                        p_update->p_pkey, NULL ) != VLC_SUCCESS )
    {
        msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
        FREENULL( p_update->p_pkey );
        goto error;
    }

    if( memcmp( sign.issuer_longid, videolan_public_key_longid , 8 ) != 0 )
    {
        msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
        public_key_t *p_new_pkey = download_key(
                VLC_OBJECT(p_update->p_libvlc),
                sign.issuer_longid, videolan_public_key_longid );
        if( !p_new_pkey )
        {
            msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
            FREENULL( p_update->p_pkey );
            goto error;
        }

        uint8_t *p_hash = key_sign_hash( p_new_pkey );
        if( !p_hash )
        {
            msg_Err( p_update->p_libvlc, "Failed to hash signature" );
            free( p_new_pkey );
            FREENULL( p_update->p_pkey );
            goto error;
        }

1006
        if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
                    &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
        {
            free( p_hash );
            msg_Info( p_update->p_libvlc, "Key authenticated" );
            free( p_update->p_pkey );
            p_update->p_pkey = p_new_pkey;
        }
        else
        {
            free( p_hash );
            msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
            goto error;
        }
    }

    gcry_md_hd_t hd;
    if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
        goto error;

    gcry_md_write( hd, psz_version_line, strlen( psz_version_line ) );
    FREENULL( psz_version_line );
    if( sign.type == TEXT_SIGNATURE )
        gcry_md_putc( hd, '\r' );
    gcry_md_putc( hd, '\n' );
    gcry_md_write( hd, p_update->release.psz_url,
                        strlen( p_update->release.psz_url ) );
    if( sign.type == TEXT_SIGNATURE )
        gcry_md_putc( hd, '\r' );
    gcry_md_putc( hd, '\n' );
    gcry_md_write( hd, p_update->release.psz_desc,
                        strlen( p_update->release.psz_desc ) );
    if( sign.type == TEXT_SIGNATURE )
        gcry_md_putc( hd, '\r' );
    gcry_md_putc( hd, '\n' );

    gcry_md_putc( hd, sign.type );
    gcry_md_write( hd, &sign.timestamp, 4 );

    gcry_md_final( hd );

    uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );

    if( p_hash[0] != sign.hash_verification[0] ||
        p_hash[1] != sign.hash_verification[1] )
    {
        msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
        goto error;
    }

1056 1057
    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
            != VLC_SUCCESS )
1058 1059 1060 1061 1062 1063 1064
    {
        msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
        goto error;
    }
    else
    {
        msg_Info( p_update->p_libvlc, "Status file authenticated" );
1065
        gcry_md_close( hd );
1066 1067 1068
        return VLC_TRUE;
    }

1069
error:
1070
    gcry_md_close( hd );
1071 1072
    if( p_stream )
        stream_Delete( p_stream );
1073 1074
    free( psz_version_line );
    return VLC_FALSE;
1075 1076
}

1077 1078 1079 1080 1081 1082 1083 1084

/**
 * Struct to launch the check in an other thread
 */
typedef struct
{
    VLC_COMMON_MEMBERS
    update_t *p_update;
1085
    void (*pf_callback)( void *, vlc_bool_t );
1086
    void *p_data;
1087 1088 1089 1090
} update_check_thread_t;

void update_CheckReal( update_check_thread_t *p_uct );

1091 1092 1093 1094
/**
 * Check for updates
 *
 * \param p_update pointer to update struct
1095 1096
 * \param pf_callback pointer to a function to call when the update_check is finished
 * \param p_data pointer to some datas to give to the callback
1097 1098
 * \returns nothing
 */
1099
void update_Check( update_t *p_update, void (*pf_callback)( void*, vlc_bool_t ), void *p_data )
1100
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1101 1102
    assert( p_update );

1103 1104 1105
    update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
                                            sizeof( update_check_thread_t ) );
    p_uct->p_update = p_update;
1106 1107
    p_uct->pf_callback = pf_callback;
    p_uct->p_data = p_data;
1108 1109 1110 1111 1112 1113 1114

    vlc_thread_create( p_uct, "check for update", update_CheckReal,
                       VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
}

void update_CheckReal( update_check_thread_t *p_uct )
{
1115
    vlc_bool_t b_ret;
1116 1117 1118
    vlc_mutex_lock( &p_uct->p_update->lock );

    EmptyRelease( p_uct->p_update );
1119
    b_ret = GetUpdateFile( p_uct->p_update );
1120
    vlc_mutex_unlock( &p_uct->p_update->lock );
1121

1122 1123
    if( p_uct->pf_callback )
        (p_uct->pf_callback)( p_uct->p_data, b_ret );
Rémi Duraffort's avatar
Rémi Duraffort committed
1124 1125

    vlc_object_destroy( p_uct );
1126 1127 1128
}

/**
1129
 * Compare two release numbers
1130
 *
1131 1132 1133
 * \param p1 first release
 * \param p2 second release
 * \return UpdateReleaseStatus(Older|Equal|Newer)
1134
 */
1135 1136
static int CompareReleases( const struct update_release_t *p1,
                            const struct update_release_t *p2 )
1137
{
1138
    int32_t d;
Rafaël Carré's avatar
Rafaël Carré committed
1139 1140 1141
    d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
      - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
      + ( p1->extra ) - ( p2->extra );
1142

1143 1144 1145 1146
    if( d < 0 )
        return UpdateReleaseStatusOlder;
    else if( d == 0 )
        return UpdateReleaseStatusEqual;
1147
    else
1148
        return UpdateReleaseStatusNewer;
1149 1150
}

1151
/**
1152
 * Compare a given release's version number to the current VLC's one
1153
 *
1154
 * \param p_update structure
1155
 * \return UpdateReleaseStatus(Older|Equal|Newer)
1156
 */
1157
int update_CompareReleaseToCurrent( update_t *p_update )
1158
{
1159
    assert( p_update );
1160

1161 1162 1163
    struct update_release_t c;

    /* get the current version number */
Rafaël Carré's avatar
Rafaël Carré committed
1164 1165 1166 1167
    c.i_major = *PACKAGE_VERSION_MAJOR - '0';
    c.i_minor = *PACKAGE_VERSION_MINOR - '0';
    c.i_revision = *PACKAGE_VERSION_REVISION - '0';
    c.extra = *PACKAGE_VERSION_EXTRA;
1168

Rafaël Carré's avatar
Rafaël Carré committed
1169
    return CompareReleases( &p_update->release, &c );
1170
}
1171

1172 1173 1174 1175 1176 1177 1178 1179
/**
 * Convert a long int size in bytes to a string
 *
 * \param l_size the size in bytes
 * \return the size as a string
 */
static char *size_str( long int l_size )
{
Rafaël Carré's avatar
Rafaël Carré committed
1180
    char *psz_tmp = NULL;
1181
    int i_retval = 0;
Rafaël Carré's avatar
Rafaël Carré committed
1182
    if( l_size >> 30 )
1183
        i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
Rafaël Carré's avatar
Rafaël Carré committed
1184
    else if( l_size >> 20 )
1185
        i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1186
    else if( l_size >> 10 )
1187
        i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1188
    else
1189 1190 1191
        i_retval = asprintf( &psz_tmp, "%ld B", l_size );

    return i_retval == -1 ? NULL : psz_tmp;
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
}


/*
 * Struct to launch the download in a thread
 */
typedef struct
{
    VLC_COMMON_MEMBERS
    update_t *p_update;
    char *psz_destdir;
} update_download_thread_t;

void update_DownloadReal( update_download_thread_t *p_udt );

/**
 * Download the file given in the update_t
 *
 * \param p_update structure
 * \param dir to store the download file
 * \return nothing
 */
void update_Download( update_t *p_update, char *psz_destdir )
{
    assert( p_update );

    update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
                                                      sizeof( update_download_thread_t ) );

    p_udt->p_update = p_update;
1222
    p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1223 1224 1225 1226

    vlc_thread_create( p_udt, "download update", update_DownloadReal,
                       VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
}
Rémi Duraffort's avatar
Rémi Duraffort committed
1227

1228 1229 1230 1231 1232
void update_DownloadReal( update_download_thread_t *p_udt )
{
    int i_progress = 0;
    long int l_size;
    long int l_downloaded = 0;
Rémi Duraffort's avatar
Rémi Duraffort committed
1233 1234 1235 1236 1237 1238
    float f_progress;
    char *psz_status = NULL;
    char *psz_downloaded = NULL;
    char *psz_size = NULL;
    char *psz_destfile = NULL;
    char *psz_tmpdestfile = NULL;
1239 1240

    FILE *p_file = NULL;
Rémi Duraffort's avatar
Rémi Duraffort committed
1241 1242
    stream_t *p_stream = NULL;
    void* p_buffer = NULL;
1243 1244 1245 1246 1247 1248
    int i_read;

    update_t *p_update = p_udt->p_update;
    char *psz_destdir = p_udt->psz_destdir;

    /* Open the stream */
1249
    p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1250 1251
    if( !p_stream )
    {
1252
        msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1253
        goto end;
1254
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
1255 1256 1257 1258 1259 1260 1261 1262

    /* Get the stream size */
    l_size = stream_Size( p_stream );

    /* Get the file name and open it*/
    psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
    if( !psz_tmpdestfile )
    {
1263
        msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1264
        goto end;
Rémi Duraffort's avatar
Rémi Duraffort committed
1265 1266 1267
    }
    psz_tmpdestfile++;
    if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1268
        goto end;
Rémi Duraffort's avatar
Rémi Duraffort committed
1269 1270 1271 1272

    p_file = utf8_fopen( psz_destfile, "w" );
    if( !p_file )
    {
1273
        msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1274
        goto end;
Rémi Duraffort's avatar
Rémi Duraffort committed
1275 1276 1277 1278 1279
    }

    /* Create a buffer and fill it with the downloaded file */
    p_buffer = (void *)malloc( 1 << 10 );
    if( !p_buffer )
1280
        goto end;
Rémi Duraffort's avatar
Rémi Duraffort committed
1281 1282 1283 1284

    psz_size = size_str( l_size );
    if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",  p_update->release.psz_url, psz_size, 0.0 ) != -1 )
    {
1285
        i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
Rémi Duraffort's avatar
Rémi Duraffort committed
1286 1287 1288 1289
        free( psz_status );
    }

    while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1290
                                   !intf_ProgressIsCancelled( p_udt, i_progress ) )
1291
    {
1292 1293 1294 1295 1296
        if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
        {
            msg_Err( p_udt, "Failed to write into %s", psz_destfile );
            break;
        }
Rémi Duraffort's avatar
Rémi Duraffort committed
1297 1298 1299 1300

        l_downloaded += i_read;
        psz_downloaded = size_str( l_downloaded );
        f_progress = 100.0*(float)l_downloaded/(float)l_size;
1301

Rémi Duraffort's avatar
Rémi Duraffort committed
1302
        if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done", p_update->release.psz_url,
1303
                      psz_downloaded, psz_size, f_progress ) != -1 )
1304
        {
1305
            intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
Rémi Duraffort's avatar
Rémi Duraffort committed
1306
            free( psz_status );
1307
        }
Rémi Duraffort's avatar
Rémi Duraffort committed
1308 1309 1310 1311 1312 1313
        free( psz_downloaded );
    }

    /* Finish the progress bar or delete the file if the user had canceled */
    fclose( p_file );
    p_file = NULL;
1314

1315
    if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
Rémi Duraffort's avatar
Rémi Duraffort committed
1316 1317
    {
        if( asprintf( &psz_status, "%s\nDone %s (100.0%%)", p_update->release.psz_url, psz_size ) != -1 )
1318
        {
1319
            intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
Rémi Duraffort's avatar
Rémi Duraffort committed
1320
            free( psz_status );
1321 1322
        }
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
1323
    else
1324
    {
1325
        utf8_unlink( psz_destfile );
1326 1327
        goto end;
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
1328

1329 1330 1331 1332
    signature_packet_v3_t sign;
    if( download_signature( VLC_OBJECT( p_udt ), &sign,
            p_update->release.psz_url ) != VLC_SUCCESS )
    {
1333 1334 1335 1336 1337 1338
        utf8_unlink( psz_destfile );

        intf_UserFatal( p_udt, VLC_TRUE, _("File can not be verified"),
            _("It was not possible to downlaod a cryptographic signature for "
              "downloaded file \"%s\", and so VLC deleted it."),
            psz_destfile );
1339
        msg_Err( p_udt, "Couldn't download signature of downloaded file" );
1340 1341 1342 1343 1344
        goto end;
    }

    if( sign.type != BINARY_SIGNATURE )
    {
1345
        utf8_unlink( psz_destfile );
1346
        msg_Err( p_udt, "Invalid signature type" );
1347 1348 1349 1350 1351
        intf_UserFatal( p_udt, VLC_TRUE, _("Invalid signature"),
            _("The cryptographic signature for downloaded file \"%s\" was "
              "invalid and couldn't be used to securely verify it, and so "
              "VLC deleted it."),
            psz_destfile );
1352 1353 1354 1355 1356 1357 1358
        goto end;
    }

    uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
    if( !p_hash )
    {
        msg_Err( p_udt, "Unable to hash %s", psz_destfile );
1359
        utf8_unlink( psz_destfile );
1360 1361 1362 1363 1364
        intf_UserFatal( p_udt, VLC_TRUE, _("File not verifiable"),
            _("It was not possible to securely verify downloaded file \"%s\", "
              "and so VLC deleted it."),
            psz_destfile );

1365 1366 1367 1368 1369 1370
        goto end;
    }

    if( p_hash[0] != sign.hash_verification[0] ||
        p_hash[1] != sign.hash_verification[1] )
    {
1371
        utf8_unlink( psz_destfile );
1372 1373 1374 1375 1376
        intf_UserFatal( p_udt, VLC_TRUE, _("File corrupted"),
            _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
             psz_destfile );
        msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
        free( p_hash );
1377 1378 1379
        goto end;
    }

1380 1381
    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
            != VLC_SUCCESS )
1382
    {
1383 1384 1385 1386
        utf8_unlink( psz_destfile );
        intf_UserFatal( p_udt, VLC_TRUE, _("File corrupted"),
            _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
             psz_destfile );
1387 1388 1389 1390 1391 1392 1393 1394 1395
        msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
        free( p_hash );
        goto end;
    }

    msg_Info( p_udt, "%s authenticated", psz_destfile );
    free( p_hash );

end:
1396 1397 1398 1399 1400 1401 1402 1403
    if( p_stream )
        stream_Delete( p_stream );
    if( p_file )
        fclose( p_file );
    free( psz_destdir );
    free( psz_destfile );
    free( p_buffer );
    free( psz_size );
Rémi Duraffort's avatar
Rémi Duraffort committed
1404 1405

    vlc_object_destroy( p_udt );
1406 1407
}

1408
#endif