update.c 48.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
 *
 * 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.
 *****************************************************************************/
25

26 27
/**
 *   \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
#include <vlc_common.h>
40
#include <vlc_update.h>
41

42 43
#ifdef UPDATE_CHECK

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

46
#include <vlc_pgpkey.h>
Clément Stenac's avatar
Clément Stenac committed
47
#include <vlc_stream.h>
48 49
#include <vlc_strings.h>
#include <vlc_charset.h>
Clément Stenac's avatar
Clément Stenac committed
50
#include <vlc_interface.h>
51 52

#include <gcrypt.h>
53
#include <vlc_gcrypt.h>
54

55
#include "update.h"
56

57 58 59 60
/*****************************************************************************
 * Misc defines
 *****************************************************************************/

Rafaël Carré's avatar
Rafaël Carré committed
61 62 63 64 65 66 67 68
/*
 * 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"
69
 * Second line is an url of the binary for this last version
70
 * Remaining text is a required description of the update
Rafaël Carré's avatar
Rafaël Carré committed
71 72
 */

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 78
#elif defined( __APPLE__ )
#   if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
79
#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
80
#   else
81
#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
82 83
#   endif
#elif defined( SYS_BEOS )
84
#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
85
#else
86
#   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
87 88
#endif

89

90 91 92
/*****************************************************************************
 * Local Prototypes
 *****************************************************************************/
93
static void EmptyRelease( update_t *p_update );
94
static bool GetUpdateFile( update_t *p_update );
95 96
static char * size_str( long int l_size );

97

98 99 100 101 102 103 104 105 106
/*****************************************************************************
 * 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 )
{
107 108
    assert( header_len == 1 || header_len == 2 || header_len == 4 );

109 110 111 112 113 114
    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] );
115 116

    abort(); /* to shut up GCC warning */
117 118 119 120 121
}

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

122
/*
123 124 125 126 127 128
 * 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 )
{
129 130

    if( i_packet_len > 418 || i_packet_len < 6 )
131 132
        return VLC_EGENERIC;

133 134 135
    size_t i_read = 0;

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

139 140
    /* XXX: warn when timestamp is > date ? */
    memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4; i_read += 4;
141

142
    p_key->algo      = *p_buf++; i_read++;
143 144 145
    if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
        return VLC_EGENERIC;

146 147 148 149
    /* read p */
    if( i_read + 2 > i_packet_len )
        return VLC_EGENERIC;

150
    int i_p_len = mpi_len( p_buf );
151 152 153 154 155 156 157 158 159

    if( i_p_len > 128 || i_read + 2 + i_p_len > i_packet_len )
        return VLC_EGENERIC;

    memcpy( p_key->p, p_buf, 2+i_p_len );
    p_buf += 2+i_p_len; i_read += 2+i_p_len;

    /* read q */
    if( i_read + 2 > i_packet_len )
160 161
        return VLC_EGENERIC;

162
    int i_q_len = mpi_len( p_buf );
163 164 165 166 167 168 169 170 171

    if( i_q_len > 20 || i_read+2+i_q_len > i_packet_len )
        return VLC_EGENERIC;

    memcpy( p_key->q, p_buf, 2+i_q_len );
    p_buf += 2+i_q_len; i_read += 2+i_q_len;

    /* read g */
    if( i_read + 2 > i_packet_len )
172 173
        return VLC_EGENERIC;

174
    int i_g_len = mpi_len( p_buf );
175 176 177 178 179 180 181 182 183

    if( i_g_len > 128 || i_read+2+i_g_len > i_packet_len )
        return VLC_EGENERIC;

    memcpy( p_key->g, p_buf, 2+i_g_len );
    p_buf += 2+i_g_len; i_read += 2+i_g_len;

    /* read y */
    if( i_read + 2 > i_packet_len )
184
        return VLC_EGENERIC;
185

186
    int i_y_len = mpi_len( p_buf );
187 188 189 190 191 192 193 194 195


    if( i_y_len > 128 || i_read+2+i_y_len > i_packet_len )
        return VLC_EGENERIC;

    memcpy( p_key->y, p_buf, 2+i_y_len );
    i_read += 2+i_y_len;

    if( i_read != i_packet_len ) /* some extra data eh ? */
196 197 198 199 200
        return VLC_EGENERIC;

    return VLC_SUCCESS;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static size_t parse_signature_v3_packet( signature_packet_t *p_sig,
                                      uint8_t *p_buf, size_t i_sig_len )
{
    size_t i_read = 1; /* we already read the version byte */

    if( i_sig_len < 19 ) /* signature is at least 19 bytes + the 2 MPIs */
        return 0;

    p_sig->specific.v3.hashed_data_len = *p_buf++; i_read++;
    if( p_sig->specific.v3.hashed_data_len != 5 )
        return 0;

    p_sig->type = *p_buf++; i_read++;

    memcpy( p_sig->specific.v3.timestamp, p_buf, 4 );
    p_buf += 4; i_read += 4;

    memcpy( p_sig->issuer_longid, p_buf, 8 );
    p_buf += 8; i_read += 8;

    p_sig->public_key_algo = *p_buf++; i_read++;

    p_sig->digest_algo = *p_buf++; i_read++;

    p_sig->hash_verification[0] = *p_buf++; i_read++;
    p_sig->hash_verification[1] = *p_buf++; i_read++;

    assert( i_read == 19 );

    return i_read;
}

233 234 235 236
/*
 * fill a signature_packet_v4_t from signature packet data
 * 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 239
                                      uint8_t *p_buf, size_t i_sig_len )
{
240
    size_t i_read = 1; /* we already read the version byte */
241

242 243
    if( i_sig_len < 10 ) /* signature is at least 10 bytes + the 2 MPIs */
        return 0;
244

245
    p_sig->type = *p_buf++; i_read++;
246

247
    p_sig->public_key_algo = *p_buf++; i_read++;
248

249
    p_sig->digest_algo = *p_buf++; i_read++;
250

251 252
    memcpy( p_sig->specific.v4.hashed_data_len, p_buf, 2 );
    p_buf += 2; i_read += 2;
253

254 255 256 257 258
    size_t i_hashed_data_len =
        scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
    i_read += i_hashed_data_len;
    if( i_read + 4 > i_sig_len )
        return 0;
259

260 261 262 263
    p_sig->specific.v4.hashed_data = (uint8_t*) malloc( i_hashed_data_len );
    if( !p_sig->specific.v4.hashed_data )
        return 0;
    memcpy( p_sig->specific.v4.hashed_data, p_buf, i_hashed_data_len );
264 265
    p_buf += i_hashed_data_len;

266 267
    memcpy( p_sig->specific.v4.unhashed_data_len, p_buf, 2 );
    p_buf += 2; i_read += 2;
268

269 270 271 272 273
    size_t i_unhashed_data_len =
        scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
    i_read += i_unhashed_data_len;
    if( i_read + 2 > i_sig_len )
        return 0;
274

275 276 277 278 279
    p_sig->specific.v4.unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
    if( !p_sig->specific.v4.unhashed_data )
        return 0;

    memcpy( p_sig->specific.v4.unhashed_data, p_buf, i_unhashed_data_len );
280 281
    p_buf += i_unhashed_data_len;

282 283 284 285 286 287
    memcpy( p_sig->hash_verification, p_buf, 2 );
    p_buf += 2; i_read += 2;

    uint8_t *p, *max_pos;
    p = p_sig->specific.v4.unhashed_data;
    max_pos = p + scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
288

289
    for( ;; )
290
    {
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
        if( p > max_pos )
            return 0;

        size_t i_subpacket_len;
        if( *p < 192 )
        {
            if( p + 1 > max_pos )
                return 0;
            i_subpacket_len = *p++;
        }
        else if( *p < 255 )
        {
            if( p + 2 > max_pos )
                return 0;
            i_subpacket_len = (*p++ - 192) << 8;
            i_subpacket_len += *p++ + 192;
        }
        else
        {
            if( p + 4 > max_pos )
                return 0;
            i_subpacket_len = *++p << 24;
            i_subpacket_len += *++p << 16;
            i_subpacket_len += *++p << 8;
            i_subpacket_len += *++p;
        }

        if( *p == ISSUER_SUBPACKET )
        {
            if( p + 9 > max_pos )
                return 0;

            memcpy( &p_sig->issuer_longid, p+1, 8 );

            return i_read;
        }

        p += i_subpacket_len;
329
    }
330 331 332 333 334 335 336 337 338 339 340 341
}

static int parse_signature_packet( signature_packet_t *p_sig,
                                   uint8_t *p_buf, size_t i_sig_len )
{
    if( !i_sig_len ) /* 1st sanity check, we need at least the version */
        return VLC_EGENERIC;

    p_sig->version = *p_buf++;

    size_t i_read;
    switch( p_sig->version )
342
    {
343 344 345 346 347 348 349 350 351 352
        case 3:
            i_read = parse_signature_v3_packet( p_sig, p_buf, i_sig_len );
            break;
        case 4:
            p_sig->specific.v4.hashed_data = NULL;
            p_sig->specific.v4.unhashed_data = NULL;
            i_read = parse_signature_v4_packet( p_sig, p_buf, i_sig_len );
            break;
        default:
            return VLC_EGENERIC;
353
    }
354

355 356 357 358 359 360 361 362 363 364
    if( i_read == 0 ) /* signature packet parsing has failed */
        goto error;

    if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
        goto error;

    if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
        goto error;

    switch( p_sig->type )
365
    {
366 367 368 369 370 371 372 373 374
        case BINARY_SIGNATURE:
        case TEXT_SIGNATURE:
        case GENERIC_KEY_SIGNATURE:
        case PERSONA_KEY_SIGNATURE:
        case CASUAL_KEY_SIGNATURE:
        case POSITIVE_KEY_SIGNATURE:
            break;
        default:
            goto error;
375
    }
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

    p_buf--; /* rewind to the version byte */
    p_buf += i_read;

    if( i_read + 2 > i_sig_len )
        goto error;

    size_t i_r_len = mpi_len( p_buf ); i_read += 2;
    if( i_read + i_r_len > i_sig_len || i_r_len > 20 )
        goto error;

    memcpy( p_sig->r, p_buf, 2 + i_r_len );
    p_buf += 2 + i_r_len;
    i_read += i_r_len;

    if( i_read + 2 > i_sig_len )
        goto error;

    size_t i_s_len = mpi_len( p_buf ); i_read += 2;
    if( i_read + i_s_len > i_sig_len || i_s_len > 20 )
        goto error;

    memcpy( p_sig->s, p_buf, 2 + i_s_len );
    p_buf += 2 + i_s_len;
    i_read += i_s_len;

    assert( i_read == i_sig_len );
    if( i_read < i_sig_len ) /* some extra data, hm ? */
        goto error;

    return VLC_SUCCESS;

error:

    if( p_sig->version == 4 )
411
    {
412 413
        free( p_sig->specific.v4.hashed_data );
        free( p_sig->specific.v4.unhashed_data );
414
    }
415

416
    return VLC_EGENERIC;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
}

/*
 * 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;

455
    while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    {
        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;
        }
475

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
        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,
491
                        p_obuf - p_opos + i_obuf_len, p_ipos );
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
        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.
511
 * We're given the file's url, we just append ".asc" to it and download
512 513
 */
static int download_signature(  vlc_object_t *p_this,
514
                                signature_packet_t *p_sig,
515
                                const char *psz_url )
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
{
    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 );

532 533
    msg_Dbg( p_this, "Downloading signature (%"PRId64" bytes)", i_size );
    uint8_t *p_buf = (uint8_t*)malloc( i_size );
534 535 536 537 538
    if( !p_buf )
    {
        stream_Delete( p_stream );
        return VLC_ENOMEM;
    }
539

540 541 542 543
    int i_read = stream_Read( p_stream, p_buf, (int)i_size );

    stream_Delete( p_stream );

544
    if( i_read != (int)i_size )
545
    {
546 547
        msg_Dbg( p_this,
            "Couldn't download full signature (only %d bytes)", i_read );
548 549 550
        free( p_buf );
        return VLC_EGENERIC;
    }
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
    if( (uint8_t)*p_buf < 0x80 ) /* ASCII */
    {
        msg_Dbg( p_this, "Unarmoring signature" );

        uint8_t* p_unarmored = (uint8_t*) malloc( ( i_size * 3 ) / 4 + 1 );
        if( !p_unarmored )
        {
            free( p_buf );
            return VLC_EGENERIC;
        }

        int i_bytes = pgp_unarmor( (char*)p_buf, i_size, p_unarmored, i_size );
        free( p_buf );

        p_buf = p_unarmored;
        i_size = i_bytes;

        if( i_bytes < 2 )
        {
            free( p_buf );
            msg_Dbg( p_this, "Unarmoring failed : corrupted signature ?" );
            return VLC_EGENERIC;
        }
    }
576

577
    if( packet_type( *p_buf ) != SIGNATURE_PACKET )
578
    {
579 580
        free( p_buf );
        msg_Dbg( p_this, "Not a signature: %d", *p_buf );
581 582
        return VLC_EGENERIC;
    }
583 584 585 586

    size_t i_header_len = packet_header_len( *p_buf );
    if( ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) ||
        i_header_len + 1 > (size_t)i_size )
587
    {
588 589
        free( p_buf );
        msg_Dbg( p_this, "Invalid signature packet header" );
590
        return VLC_EGENERIC;
591
    }
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

    size_t i_len = scalar_number( p_buf+1, i_header_len );
    if( i_len + i_header_len + 1 != (size_t)i_size )
    {
        free( p_buf );
        msg_Dbg( p_this, "Invalid signature packet" );
        return VLC_EGENERIC;
    }

    int i_ret = parse_signature_packet( p_sig, p_buf+1+i_header_len, i_len );
    free( p_buf );
    if( i_ret != VLC_SUCCESS )
    {
        msg_Dbg( p_this, "Couldn't parse signature" );
        return i_ret;
    }

    if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
610
    {
611 612
        msg_Dbg( p_this, "Invalid signature type: %d", p_sig->type );
        if( p_sig->version == 4 )
613
        {
614 615
            free( p_sig->specific.v4.hashed_data );
            free( p_sig->specific.v4.unhashed_data );
616
        }
617
        return VLC_EGENERIC;
618
    }
619 620

    return VLC_SUCCESS;
621 622 623 624 625
}

/*
 * 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 628 629 630 631 632 633 634 635 636 637 638 639 640 641
        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;

642 643 644 645 646 647 648 649
    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 ) ||
650 651 652
        gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
        goto problem;

653 654 655 656
    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 ) ||
657 658 659
        gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
        goto problem;

660 661
    int i_hash_len = 20;
    if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
        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;
}

/*
 * 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
 */
690 691
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 )
692 693 694 695 696 697 698 699 700 701 702 703
{
    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;

    p_key->psz_username = NULL;
704 705
    p_key->sig.specific.v4.hashed_data = NULL;
    p_key->sig.specific.v4.unhashed_data = NULL;
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729

    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++ );
730 731
        if( pos + i_header_len > max_pos ||
            ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) )
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
            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 )
        {
            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;

748 749
            case SIGNATURE_PACKET: /* we accept only v4 signatures here */
                if( i_status & SIGNATURE_FOUND || !p_sig_issuer )
750
                    break;
751 752 753
                int i_ret = parse_signature_packet( &p_key->sig, pos,
                                                    i_packet_len );
                if( i_ret == VLC_SUCCESS )
754
                {
755 756 757 758 759 760 761 762 763 764
                    if( p_key->sig.version != 4 )
                        break;
                    if( memcmp( p_key->sig.issuer_longid, p_sig_issuer, 8 ) )
                    {
                        free( p_key->sig.specific.v4.hashed_data );
                        free( p_key->sig.specific.v4.unhashed_data );
                        p_key->sig.specific.v4.hashed_data = NULL;
                        p_key->sig.specific.v4.unhashed_data = NULL;
                        break;
                    }
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
                    i_status |= SIGNATURE_FOUND;
                }
                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;
780

781 782 783 784 785 786 787
            default:
                break;
        }
        pos += i_packet_len;
    }
    free( p_key_unarmored );

788
    if( !( i_status & ( PUBLIC_KEY_FOUND | USER_ID_FOUND ) ) )
789 790 791 792 793 794 795 796
        return VLC_EGENERIC;

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

    return VLC_SUCCESS;

error:
797 798 799 800 801
    if( p_key->sig.version == 4 )
    {
        free( p_key->sig.specific.v4.hashed_data );
        free( p_key->sig.specific.v4.unhashed_data );
    }
802 803 804 805 806 807 808 809 810
    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,
811
                            signature_packet_t *p_sig )
812
{
813 814 815
    if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
        return NULL;

816 817 818 819
    FILE *f = utf8_fopen( psz_file, "r" );
    if( !f )
        return NULL;

820
    uint8_t buffer[4096];
821 822 823 824 825 826

    gcry_md_hd_t hd;
    if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
    {
        fclose( f );
        return NULL;
827
    }
828 829 830 831 832

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

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
    if( p_sig->version == 3 )
    {
        gcry_md_putc( hd, p_sig->type );
        gcry_md_write( hd, &p_sig->specific.v3.timestamp, 4 );
    }
    else if( p_sig->version == 4 )
    {
        gcry_md_putc( hd, p_sig->version );
        gcry_md_putc( hd, p_sig->type );
        gcry_md_putc( hd, p_sig->public_key_algo );
        gcry_md_putc( hd, p_sig->digest_algo );
        gcry_md_write( hd, p_sig->specific.v4.hashed_data_len, 2 );
        size_t i_len = scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
        gcry_md_write( hd, p_sig->specific.v4.hashed_data, i_len );
    }
    else
    {   /* RFC 4880 only tells about versions 3 and 4 */
        gcry_md_close( hd );
        return NULL;
    }
853 854 855 856

    fclose( f );
    gcry_md_final( hd );

857 858 859 860
    uint8_t *p_tmp = (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1);
    uint8_t *p_hash = malloc( 20 );
    if( p_hash )
        memcpy( p_hash, p_tmp, 20 );
Antoine Cellerier's avatar
Antoine Cellerier committed
861 862
    gcry_md_close( hd );
    return p_hash;
863 864 865 866 867
}

/*
 * download a public key (the last one) from videolan server, and parse it
 */
868 869
static public_key_t *download_key( vlc_object_t *p_this,
                    const uint8_t *p_longid, const uint8_t *p_signature_issuer )
870 871
{
    char *psz_url;
872 873 874
    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 )
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
        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 )
    {
901
        msg_Dbg( p_this, "Couldn't read full GPG key" );
902 903 904 905 906 907 908 909 910 911 912
        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;
    }

913 914
    memcpy( p_pkey->longid, p_longid, 8 );

915 916 917 918 919
    int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
    free( p_buf );

    if( i_error != VLC_SUCCESS )
    {
920
        msg_Dbg( p_this, "Couldn't parse GPG key" );
921 922 923 924 925 926 927 928
        free( p_pkey );
        return NULL;
    }

    return p_pkey;
}

/*
929 930
 * Generate a SHA1 hash on a public key, to verify a signature made on that hash
 * Note that we need the signature (v4) to compute the hash
931 932 933
 */
static uint8_t *key_sign_hash( public_key_t *p_pkey )
{
934 935 936 937 938 939 940
    if( p_pkey->sig.version != 4 )
        return NULL;

    if( p_pkey->sig.type < GENERIC_KEY_SIGNATURE ||
        p_pkey->sig.type > POSITIVE_KEY_SIGNATURE )
        return NULL;

941 942 943 944 945 946 947 948 949
    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 );

950 951 952 953
    size_t i_p_len = mpi_len( p_pkey->key.p );
    size_t i_g_len = mpi_len( p_pkey->key.g );
    size_t i_q_len = mpi_len( p_pkey->key.q );
    size_t i_y_len = mpi_len( p_pkey->key.y );
954

955 956 957 958 959 960 961 962
    size_t i_size = 6 + 2*4 + i_p_len + i_g_len + i_q_len + i_y_len;

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

    gcry_md_putc( hd, p_pkey->key.version );
    gcry_md_write( hd, p_pkey->key.timestamp, 4 );
    gcry_md_putc( hd, p_pkey->key.algo );
963 964 965 966 967 968 969

    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 );

    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 );

970 971 972
    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 );

973 974
    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 );
975 976 977

    gcry_md_putc( hd, 0xb4 );

978
    size_t i_len = strlen((char*)p_pkey->psz_username);
979 980 981 982 983 984 985 986

    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 );

987 988
    size_t i_hashed_data_len =
        scalar_number( p_pkey->sig.specific.v4.hashed_data_len, 2 );
989 990 991 992 993

    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 );
994 995
    gcry_md_write( hd, p_pkey->sig.specific.v4.hashed_data_len, 2 );
    gcry_md_write( hd, p_pkey->sig.specific.v4.hashed_data, i_hashed_data_len );
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

    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 );

1009
    uint8_t *p_tmp = gcry_md_read( hd, GCRY_MD_SHA1);
1010

1011 1012 1013
    if( !p_tmp ||
        p_tmp[0] != p_pkey->sig.hash_verification[0] ||
        p_tmp[1] != p_pkey->sig.hash_verification[1] )
1014
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
1015
        gcry_md_close( hd );
1016 1017 1018
        return NULL;
    }

1019 1020 1021
    uint8_t *p_hash = malloc( 20 );
    if( p_hash )
        memcpy( p_hash, p_tmp, 20 );
Antoine Cellerier's avatar
Antoine Cellerier committed
1022
    gcry_md_close( hd );
1023 1024 1025
    return p_hash;
}

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

/*****************************************************************************
 * 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;
1040
    assert( p_this );
1041 1042

    p_update = (update_t *)malloc( sizeof( update_t ) );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1043
    if( !p_update ) return NULL;
1044

1045
    vlc_mutex_init( &p_update->lock );
1046

1047
    p_update->p_libvlc = p_this->p_libvlc;
1048

1049 1050
    p_update->release.psz_url = NULL;
    p_update->release.psz_desc = NULL;
1051

1052 1053 1054
    p_update->p_download = NULL;
    p_update->p_check = NULL;

1055
    p_update->p_pkey = NULL;
1056
    vlc_gcrypt_init();
1057

1058
    return p_update;
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
}

/**
 * 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
1069 1070
    assert( p_update );

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    if( p_update->p_check )
    {
        assert( !p_update->p_download );
        vlc_object_kill( p_update->p_check );
        vlc_thread_join( p_update->p_check );
    }
    else if( p_update->p_download )
    {
        vlc_object_kill( p_update->p_download );
        vlc_thread_join( p_update->p_download );
    }

1083 1084
    vlc_mutex_destroy( &p_update->lock );

1085 1086 1087
    free( p_update->release.psz_url );
    free( p_update->release.psz_desc );
    free( p_update->p_pkey );
1088 1089

    free( p_update );
1090 1091 1092
}

/**
1093
 * Empty the release struct
1094
 *
1095
 * \param p_update update_t* pointer
1096 1097
 * \return nothing
 */
1098
static void EmptyRelease( update_t *p_update )
1099
{
1100 1101 1102
    p_update->release.i_major = 0;
    p_update->release.i_minor = 0;
    p_update->release.i_revision = 0;
1103

1104 1105
    FREENULL( p_update->release.psz_url );
    FREENULL( p_update->release.psz_desc );
1106 1107 1108
}

/**
1109
 * Get the update file and parse it
Rémi Duraffort's avatar
Rémi Duraffort committed
1110
 * p_update has to be locked when calling this function
1111 1112
 *
 * \param p_update pointer to update struct
1113
 * \return true if the update is valid and authenticated
1114
 */
1115
static bool GetUpdateFile( update_t *p_update )
1116 1117
{
    stream_t *p_stream = NULL;
1118 1119 1120
    int i_major = 0;
    int i_minor = 0;
    int i_revision = 0;
Rafaël Carré's avatar
Rafaël Carré committed
1121
    unsigned char extra;
1122
    char *psz_version_line = NULL;
1123

1124
    p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
1125 1126
    if( !p_stream )
    {
1127
        msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
1128 1129 1130 1131
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }

1132 1133
    /* Start reading the status file */
    if( !( psz_version_line = stream_ReadLine( p_stream ) ) )
1134
    {
1135
        msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
1136 1137 1138 1139
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }

1140
    /* first line : version number */
Rafaël Carré's avatar
Rafaël Carré committed
1141
    p_update->release.extra = 0;
1142 1143
    switch( sscanf( psz_version_line, "%i.%i.%i%c",
                    &i_major, &i_minor, &i_revision, &extra ) )
1144
    {
Rafaël Carré's avatar
Rafaël Carré committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
        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;
1155
    }
1156

1157 1158
    /* second line : URL */
    if( !( p_update->release.psz_url = stream_ReadLine( p_stream ) ) )
1159 1160 1161 1162 1163
    {
        msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }
1164

1165 1166 1167 1168 1169 1170 1171 1172 1173
    /* Remaining data : description */
    int i_read = stream_Size( p_stream ) - stream_Tell( p_stream );
    if( i_read <= 0 )
    {
        msg_Err( p_update->p_libvlc,
                "Update file %s is corrupted: description missing",
                UPDATE_VLC_STATUS_URL );
        goto error;
    }
1174

1175 1176 1177 1178 1179
    p_update->release.psz_desc = (char*) malloc( i_read + 1 );
    if( !p_update->release.psz_desc )
        goto error;

    if( stream_Read( p_stream, p_update->release.psz_desc, i_read ) != i_read )
1180
    {
1181 1182
        msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
                UPDATE_VLC_STATUS_URL );
1183
        goto error;
1184
    }
1185
    p_update->release.psz_desc[i_read] = '\0';
1186

1187 1188 1189
    stream_Delete( p_stream );
    p_stream = NULL;

1190
    /* Now that we know the status is valid, we must download its signature
1191
     * to authenticate it */
1192
    signature_packet_t sign;
1193
    if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
            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;
    }

1218 1219 1220
    memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );

    if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
    {
        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;
        }

1242
        if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
                    &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 ) )
1260
        goto error_hd;
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

    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' );

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
    char *psz_desc = p_update->release.psz_desc;
    while( *psz_desc )
    {
        size_t i_len = strcspn( psz_desc, "\r\n" );
        if( !i_len )
            break;

        gcry_md_write( hd, psz_desc, i_len );
        if( sign.type == TEXT_SIGNATURE )
            gcry_md_putc( hd, '\r' );
        gcry_md_putc( hd, '\n' );

        psz_desc += i_len;
        while( *psz_desc == '\r' || *psz_desc == '\n' )
            psz_desc++;
    }
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

    if( sign.version == 3 )
    {
        gcry_md_putc( hd, sign.type );
        gcry_md_write( hd, &sign.specific.v3.timestamp, 4 );
    }
    else if( sign.version == 4 )
    {
        gcry_md_putc( hd, sign.version );
        gcry_md_putc( hd, sign.type );
        gcry_md_putc( hd, sign.public_key_algo );
        gcry_md_putc( hd, sign.digest_algo );
        gcry_md_write( hd, sign.specific.v4.hashed_data_len, 2 );
        size_t i_len = scalar_number( sign.specific.v4.hashed_data_len, 2 );
        gcry_md_write( hd, sign.specific.v4.hashed_data, i_len );
        gcry_md_putc( hd, 0x04 );
        gcry_md_putc( hd, 0xFF );

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

        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 );
    }
    else
    {   /* RFC 4880 only tells about versions 3 and 4 */
        msg_Warn( p_update->p_libvlc, "Invalid signature version %d",
                sign.version);
        goto error_hd;
    }
1320 1321 1322 1323 1324 1325 1326 1327 1328

    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" );
1329
        goto error_hd;
1330 1331
    }

1332 1333
    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
            != VLC_SUCCESS )
1334 1335
    {
        msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
1336
        goto error_hd;
1337 1338 1339 1340
    }
    else
    {
        msg_Info( p_update->p_libvlc, "Status file authenticated" );
1341
        gcry_md_close( hd );
1342
        return true;
1343 1344
    }

1345
error_hd:
1346
    gcry_md_close( hd );
1347
error:
1348 1349
    if( p_stream )
        stream_Delete( p_stream );
1350
    free( psz_version_line );
1351
    return false;
1352 1353
}

1354
static void update_CheckReal( update_check_thread_t *p_uct );
1355

1356 1357 1358 1359
/**
 * Check for updates
 *
 * \param p_update pointer to update struct
1360 1361
 * \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
1362 1363
 * \returns nothing
 */
1364
void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
1365
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1366 1367
    assert( p_update );

1368 1369
    update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
                                            sizeof( update_check_thread_t ) );
1370
    if( !p_uct ) return;
1371

1372
    p_uct->p_update = p_update;
1373
    p_update->p_check = p_uct;
1374 1375
    p_uct->pf_callback = pf_callback;
    p_uct->p_data = p_data;
1376 1377

    vlc_thread_create( p_uct, "check for update", update_CheckReal,
1378
                       VLC_THREAD_PRIORITY_LOW, false );
1379 1380 1381 1382
}

void update_CheckReal( update_check_thread_t *p_uct )
{
1383
    bool b_ret;
1384 1385 1386
    vlc_mutex_lock( &p_uct->p_update->lock );

    EmptyRelease( p_uct->p_update );
1387
    b_ret = GetUpdateFile( p_uct->p_update );
1388
    vlc_mutex_unlock( &p_uct->p_update->lock );
1389

1390 1391
    if( p_uct->pf_callback )
        (p_uct->pf_callback)( p_uct->p_data, b_ret );
1392 1393 1394 1395

    p_uct->p_update->p_check = NULL;

    vlc_object_release( p_uct );
1396 1397
}

1398
/**
1399
 * Compare a given release's version number to the current VLC's one
1400
 *
1401
 * \param p_update structure
1402
 * \return true if we have to upgrade to the given version to be up to date
1403
 */
1404
bool update_NeedUpgrade( update_t *p_update )
1405
{
1406
    assert( p_update );
1407

1408 1409 1410 1411
    return  p_update->release.i_major    < *PACKAGE_VERSION_MAJOR    - '0'  ||
            p_update->release.i_minor    < *PACKAGE_VERSION_MINOR    - '0'  ||
            p_update->release.i_revision < *PACKAGE_VERSION_REVISION - '0'  ||
            p_update->release.extra      < *PACKAGE_VERSION_EXTRA;
1412
}
1413

1414 1415 1416 1417 1418 1419 1420 1421
/**
 * 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
1422
    char *psz_tmp = NULL;
1423
    int i_retval = 0;
Rafaël Carré's avatar
Rafaël Carré committed
1424
    if( l_size >> 30 )
1425
        i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
Rafaël Carré's avatar
Rafaël Carré committed
1426
    else if( l_size >> 20 )
1427
        i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1428
    else if( l_size >> 10 )
1429
        i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1430
    else
1431 1432 1433
        i_retval = asprintf( &psz_tmp, "%ld B", l_size );

    return i_retval == -1 ? NULL : psz_tmp;
1434 1435
}

1436
static void update_DownloadReal( update_download_thread_t *p_udt );
1437 1438 1439 1440 1441 1442 1443 1444

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

    update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
Rémi Duraffort's avatar
Rémi Duraffort committed
1450
                                        sizeof( update_download_thread_t ) );
1451 1452
    if( !p_udt )
        return;
1453 1454

    p_udt->p_update = p_update;
1455
    p_update->p_download = p_udt;
1456
    p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1457 1458

    vlc_thread_create( p_udt, "download update", update_DownloadReal,
1459
                       VLC_THREAD_PRIORITY_LOW, false );
1460
}
Rémi Duraffort's avatar
Rémi Duraffort committed
1461

1462
static void update_DownloadReal( update_download_thread_t *p_udt )
1463 1464 1465 1466
{
    int i_progress = 0;
    long int l_size;
    long int l_downloaded = 0;
Rémi Duraffort's avatar
Rémi Duraffort committed
1467 1468 1469 1470 1471 1472
    float f_progress;
    char *psz_status = NULL;
    char *psz_downloaded = NULL;
    char *psz_size = NULL;
    char *psz_destfile = NULL;
    char *psz_tmpdestfile = NULL;
1473 1474

    FILE *p_file = NULL;
Rémi Duraffort's avatar
Rémi Duraffort committed
1475 1476
    stream_t *p_stream = NULL;
    void* p_buffer = NULL;
1477 1478 1479 1480 1481 1482
    int i_read;

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

    /* Open the stream */
1483
    p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1484 1485
    if( !p_stream )
    {
1486
        msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1487
        goto end;
1488
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
1489 1490 1491 1492 1493 1494 1495 1496

    /* 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 )
    {
1497
        msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1498
        goto end;
Rémi Duraffort's avatar
Rémi Duraffort committed
1499 1500 1501
    }
    psz_tmpdestfile++;
    if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1502
        goto end;
Rémi Duraffort's avatar
Rémi Duraffort committed
1503 1504 1505 1506

    p_file = utf8_fopen( psz_destfile, "w" );
    if( !p_file )
    {
1507
        msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1508
        goto end;
Rémi Duraffort's avatar
Rémi Duraffort committed
1509 1510 1511 1512 1513
    }

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

    psz_size = size_str( l_size );
Rémi Duraffort's avatar
Rémi Duraffort committed
1517 1518
    if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",
        p_update->release.psz_url, psz_size, 0.0 ) != -1 )
Rémi Duraffort's avatar
Rémi Duraffort committed
1519
    {
1520
        i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
Rémi Duraffort's avatar
Rémi Duraffort committed
1521 1522 1523
        free( psz_status );
    }

1524 1525 1526 1527
    vlc_object_lock( p_udt );
    while( vlc_object_alive( p_udt ) &&
           ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
           !intf_ProgressIsCancelled( p_udt, i_progress ) )
1528
    {
1529
        vlc_object_unlock( p_udt );
1530 1531 1532 1533 1534
        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
1535 1536 1537 1538

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

Rémi Duraffort's avatar
Rémi Duraffort committed
1540 1541 1542
        if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done",
                      p_update->release.psz_url, psz_downloaded, psz_size,
                      f_progress ) != -1 )
1543
        {
1544
            intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
Rémi Duraffort's avatar
Rémi Duraffort committed
1545
            free( psz_status );
1546
        }
Rémi Duraffort's avatar
Rémi Duraffort committed
1547
        free( psz_downloaded );
1548
        vlc_object_lock( p_udt );
Rémi Duraffort's avatar
Rémi Duraffort committed
1549 1550 1551 1552 1553
    }

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

1555 1556
    if( vlc_object_alive( p_udt ) &&
        !intf_ProgressIsCancelled( p_udt, i_progress ) )
Rémi Duraffort's avatar
Rémi Duraffort committed
1557
    {
1558
        vlc_object_unlock( p_udt );
Rémi Duraffort's avatar
Rémi Duraffort committed
1559 1560
        if( asprintf( &psz_status, "%s\nDone %s (100.0%%)",
            p_update->release.psz_url, psz_size ) != -1 )
1561
        {
1562
            intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
Rémi Duraffort's avatar
Rémi Duraffort committed
1563
            free( psz_status );
1564 1565
        }
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
1566
    else
1567
    {
1568
        vlc_object_unlock( p_udt );
1569
        utf8_unlink( psz_destfile );
1570 1571
        goto end;
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
1572

1573
    signature_packet_t sign;
1574 1575 1576
    if( download_signature( VLC_OBJECT( p_udt ), &sign,
            p_update->release.psz_url ) != VLC_SUCCESS )
    {
1577 1578
        utf8_unlink( psz_destfile );

Felix Paul Kühne's avatar
Felix Paul Kühne committed
1579
        intf_UserFatal( p_udt, true, _("File could not be verified"),
Antoine Cellerier's avatar
Antoine Cellerier committed
1580
            _("It was not possible to download a cryptographic signature for "
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1581
              "the downloaded file \"%s\". Thus, it was deleted."),
1582
            psz_destfile );
1583
        msg_Err( p_udt, "Couldn't download signature of downloaded file" );
1584 1585
        goto end;
    }
1586 1587 1588 1589 1590

    if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
    {
        utf8_unlink( psz_destfile );
        msg_Err( p_udt, "Invalid signature issuer" );
1591
        intf_UserFatal( p_udt, true, _("Invalid signature"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1592 1593 1594
            _("The cryptographic signature for the downloaded file \"%s\" was "
              "invalid and could not be used to securely verify it. Thus, the "
              "file was deleted."),
1595 1596 1597
            psz_destfile );
        goto end;
    }
1598 1599 1600

    if( sign.type != BINARY_SIGNATURE )
    {
1601
        utf8_unlink( psz_destfile );
1602
        msg_Err( p_udt, "Invalid signature type" );
1603
        intf_UserFatal( p_udt, true, _("Invalid signature"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1604 1605
            _("The cryptographic signature for the downloaded file \"%s\" was "
              "invalid and could not be used to securely verify it. Thus, the "
Felix Paul Kühne's avatar
Typo  
Felix Paul Kühne committed
1606
              "file was deleted."),
1607
            psz_destfile );
1608 1609 1610 1611 1612 1613 1614
        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 );
1615
        utf8_unlink( psz_destfile );
1616
        intf_UserFatal( p_udt, true, _("File not verifiable"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1617 1618
            _("It was not possible to securely verify the downloaded file"
              " \"%s\". Thus, it was VLC deleted."),
1619 1620
            psz_destfile );

1621 1622 1623 1624 1625 1626
        goto end;
    }

    if( p_hash[0] != sign.hash_verification[0] ||
        p_hash[1] != sign.hash_verification[1] )
    {
1627
        utf8_unlink( psz_destfile );
1628
        intf_UserFatal( p_udt, true, _("File corrupted"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1629
            _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
1630 1631 1632
             psz_destfile );
        msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
        free( p_hash );
1633 1634 1635
        goto end;
    }

1636 1637
    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
            != VLC_SUCCESS )
1638
    {
1639
        utf8_unlink( psz_destfile );
1640
        intf_UserFatal( p_udt, true, _("File corrupted"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1641
            _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
1642
             psz_destfile );
1643 1644 1645 1646 1647 1648 1649 1650 1651
        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:
1652 1653 1654 1655 1656 1657 1658 1659
    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 );
1660 1661 1662 1663

    p_udt->p_update->p_download = NULL;

    vlc_object_release( p_udt );
1664
}
1665 1666 1667 1668 1669 1670

update_release_t *update_GetRelease( update_t *p_update )
{
    return &p_update->release;
}

1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
#else
update_t *__update_New( vlc_object_t *p_this )
{
    (void)p_this;
    return NULL;
}

void update_Delete( update_t *p_update )
{
    (void)p_update;
}
1682

1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
                   void *p_data )
{
    (void)p_update; (void)pf_callback; (void)p_data;
}

bool update_NeedUpgrade( update_t *p_update )
{
    (void)p_update;
    return false;
}

1695
void update_Download( update_t *p_update, const char *psz_destdir )
1696 1697 1698
{
    (void)p_update; (void)psz_destdir;
}
1699 1700 1701 1702 1703 1704

update_release_t *update_GetRelease( update_t *p_update )
{
    (void)p_update;
    return NULL;
}
1705
#endif