Commit 11922961 authored by Rafaël Carré's avatar Rafaël Carré

Update: supports file v4 signatures

Note: public key v3 signatures are not supported
Fix various bugs and potentials buffer overflows
parent 96250779
......@@ -23,7 +23,7 @@
- copy the tar.gz and tar.bz2 file on ftp.videolan.org in
/opt/ftp/pub/videolan/testing/vlc-X.X.X/
- generate md5 hashes and gpg signature of these files
(use gpg --sign --detach --armor --force-v3-sigs)
(use gpg --sign --detach --armor)
* Contribs
- Put a copy of the libraries or git snapshot in vlc-X.X.X/contrib
......@@ -36,7 +36,7 @@
Build in the "buildbeos" chroot on altair.
# add the .zip files to /opt/ftp/pub/videolan/testing/vlc-X.X.X/beos/
generate md5 hashes and gpg signature of these files
(use gpg --sign --detach --armor --force-v3-sigs)
(use gpg --sign --detach --armor)
* Win32 Packages
make the packages using the nightly builds configure/options/... , don't forget --enable-update-check
......@@ -44,12 +44,12 @@
kind of suxxs)
add the .zip and .exe files to /opt/ftp/pub/videolan/testing/vlc-X.X.X/win32/
generate md5 hashes and gpg signature of these files
(use gpg --sign --detach --armor --force-v3-sigs)
(use gpg --sign --detach --armor)
* OS X packages
configure with --enable-update-check
generate md5 hashes and gpg signature of these files
(use gpg --sign --detach --armor --force-v3-sigs)
(use gpg --sign --detach --armor)
* Commit changes ... it never works the first time
......
......@@ -32,6 +32,7 @@ static uint8_t videolan_public_key_longid[8] = {
0x8B, 0x08, 0x52, 0x31, 0xD0, 0x38, 0x35, 0x37
};
/* gpg --export --armor "VideoLAN Release"|sed -e s/^/\"/ -e s/\$/\\\\n\"/ */
static uint8_t videolan_public_key[] = {
"-----BEGIN PGP PUBLIC KEY BLOCK-----\n"
"Version: GnuPG v2.0.4 (FreeBSD)\n"
......@@ -78,4 +79,3 @@ static uint8_t videolan_public_key[] = {
"=Ic/K\n"
"-----END PGP PUBLIC KEY BLOCK-----\n"
};
......@@ -33,17 +33,25 @@
#include <vlc/vlc.h>
#include <vlc_stream.h> /* key & signature downloading */
#include <vlc_strings.h> /* b64 decoding */
#include <vlc_charset.h> /* utf8_fopen() */
#include <gcrypt.h> /* cryptography and digest algorithms */
/**
* \defgroup update Update
*
* @{
*/
/* Go reading the rfc 4880 ! NOW !! */
/*
* XXX
* When PGP-signing a file, we only sign a SHA-1 hash of this file
*
* As soon as SHA-1 is broken, this method is not secure anymore, because an
* attacker could generate a file with the same SHA-1 hash.
*
* Whenever this happens, we need to use another algorithm / type of key.
* XXX
*/
enum /* Public key algorithms */
{
/* we will only use DSA public keys */
......@@ -75,14 +83,11 @@ enum /* Signature types */
POSITIVE_KEY_SIGNATURE = 0x13 /* Substantial verification */
};
enum /* Signature subpacket types */
{
ISSUER_SUBPACKET = 0x10
};
struct public_key_packet_t
{ /* a public key packet (DSA/SHA-1) is 418 bytes */
......@@ -96,52 +101,49 @@ struct public_key_packet_t
uint8_t y[2+128];
};
/* used for public key signatures */
struct signature_packet_v4_t
{ /* hashed_data or unhashed_data can be empty, so the signature packet is
* theorically at least 54 bytes long, but always more than that. */
/* used for public key and file signatures */
struct signature_packet_t
{
uint8_t version; /* 3 or 4 */
uint8_t version;
uint8_t type;
uint8_t public_key_algo;
uint8_t digest_algo;
uint8_t hashed_data_len[2];
uint8_t *hashed_data;
uint8_t unhashed_data_len[2];
uint8_t *unhashed_data;
uint8_t hash_verification[2];
/* The part below is made of consecutive MPIs, their number and size being
* public-key-algorithm dependant.
* But since we use DSA signatures only, we fix it. */
uint8_t r[2+20];
uint8_t s[2+20];
};
uint8_t public_key_algo; /* DSA only */
uint8_t digest_algo; /* SHA-1 only */
/* Used for binary document signatures (to be compatible with older software)
* DSA/SHA-1 is always 65 bytes */
struct signature_packet_v3_t
{
uint8_t header[2];
uint8_t version; /* 3 */
uint8_t hashed_data_len; /* MUST be 5 */
uint8_t type;
uint8_t timestamp[4]; /* 4 bytes scalar number */
uint8_t issuer_longid[8]; /* The key which signed the document */
uint8_t public_key_algo; /* we only know about DSA */
uint8_t digest_algo; /* and his little sister SHA-1 */
uint8_t hash_verification[2];/* the 2 1st bytes of the SHA-1 hash */
uint8_t hash_verification[2];
uint8_t issuer_longid[8];
union /* version specific data */
{
struct
{
uint8_t hashed_data_len[2]; /* scalar number */
uint8_t *hashed_data; /* hashed_data_len bytes */
uint8_t unhashed_data_len[2]; /* scalar number */
uint8_t *unhashed_data; /* unhashed_data_len bytes */
} v4;
struct
{
uint8_t hashed_data_len; /* MUST be 5 */
uint8_t timestamp[4]; /* 4 bytes scalar number */
} v3;
} specific;
/* The part below is made of consecutive MPIs, their number and size being
* public-key-algorithm dependant.
* But since we use DSA signatures only, we fix it. */
*
* Since we use DSA signatures only, there is 2 integers, r & s, made of:
* 2 bytes for the integer length (scalar number)
* 160 bits (20 bytes) for the integer itself
*
* Note: the integers may be less than 160 significant bits
*/
uint8_t r[2+20];
uint8_t s[2+20];
};
typedef struct public_key_packet_t public_key_packet_t;
typedef struct signature_packet_v4_t signature_packet_v4_t;
typedef struct signature_packet_v3_t signature_packet_v3_t;
typedef struct signature_packet_t signature_packet_t;
struct public_key_t
{
......@@ -150,7 +152,7 @@ struct public_key_t
public_key_packet_t key; /* Public key packet */
signature_packet_v4_t sig; /* Signature packet, by the embedded key */
signature_packet_t sig; /* Signature packet, by the embedded key */
};
typedef struct public_key_t public_key_t;
......
......@@ -22,9 +22,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
/*
* XXX: should use v4 signatures for binary files (already used for public key)
*/
/**
* \file
* This file contains functions related to VLC update management
......@@ -47,7 +45,11 @@
#include <vlc_update.h>
#include <vlc_pgpkey.h>
#include <vlc_stream.h>
#include <vlc_strings.h>
#include <vlc_charset.h>
#include <vlc_interface.h>
#include <gcrypt.h>
#include <vlc_gcrypt.h>
......@@ -101,14 +103,16 @@ static char * size_str( long int l_size );
static inline int scalar_number( uint8_t *p, int header_len )
{
assert( header_len == 1 || header_len == 2 || header_len == 4 );
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();
abort(); /* to shut up GCC warning */
}
/* number of data bytes in a MPI */
......@@ -121,150 +125,294 @@ static inline int scalar_number( uint8_t *p, int header_len )
static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
size_t i_packet_len )
{
if( i_packet_len > 418 )
if( i_packet_len > 418 || i_packet_len < 6 )
return VLC_EGENERIC;
p_key->version = *p_buf++;
size_t i_read = 0;
p_key->version = *p_buf++; i_read++;
if( p_key->version != 4 )
return VLC_EGENERIC;
/* warn when timestamp is > date ? */
memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;
/* XXX: warn when timestamp is > date ? */
memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4; i_read += 4;
p_key->algo = *p_buf++;
p_key->algo = *p_buf++; i_read++;
if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
return VLC_EGENERIC;
/* read p */
if( i_read + 2 > i_packet_len )
return VLC_EGENERIC;
int i_p_len = mpi_len( p_buf );
if( i_p_len > 128 )
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 )
return VLC_EGENERIC;
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 );
}
int i_q_len = mpi_len( p_buf );
if( i_q_len > 20 )
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 )
return VLC_EGENERIC;
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 );
}
int i_g_len = mpi_len( p_buf );
if( i_g_len > 128 )
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 )
return VLC_EGENERIC;
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 );
}
int i_y_len = mpi_len( p_buf );
if( i_y_len > 128 )
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 ? */
return VLC_EGENERIC;
else
memcpy( p_key->y, p_buf, 2+i_y_len );
return VLC_SUCCESS;
}
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;
}
/*
* 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,
static size_t parse_signature_v4_packet( signature_packet_t *p_sig,
uint8_t *p_buf, size_t i_sig_len )
{
if( i_sig_len < 54 )
return VLC_EGENERIC;
size_t i_read = 1; /* we already read the version byte */
p_sig->version = *p_buf++;
if( p_sig->version != 4 )
return VLC_EGENERIC;
if( i_sig_len < 10 ) /* signature is at least 10 bytes + the 2 MPIs */
return 0;
p_sig->type = *p_buf++;
if( p_sig->type < GENERIC_KEY_SIGNATURE ||
p_sig->type > POSITIVE_KEY_SIGNATURE )
return VLC_EGENERIC;
p_sig->type = *p_buf++; i_read++;
p_sig->public_key_algo = *p_buf++;
if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
return VLC_EGENERIC;
p_sig->public_key_algo = *p_buf++; i_read++;
p_sig->digest_algo = *p_buf++;
if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
return VLC_EGENERIC;
p_sig->digest_algo = *p_buf++; i_read++;
memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;
memcpy( p_sig->specific.v4.hashed_data_len, p_buf, 2 );
p_buf += 2; i_read += 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;
if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total,
* + the unhashed data length (2 bytes)
* + the hash verification (2 bytes) */
return VLC_EGENERIC;
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;
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_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 );
p_buf += i_hashed_data_len;
memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;
memcpy( p_sig->specific.v4.unhashed_data_len, p_buf, 2 );
p_buf += 2; i_read += 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;
}
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;
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_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 );
p_buf += i_unhashed_data_len;
memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
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 );
int i_r_len = mpi_len( p_buf );
if( i_r_len > 20 )
for( ;; )
{
free( p_sig->hashed_data );
free( p_sig->unhashed_data );
return VLC_EGENERIC;
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;
}
else
}
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 )
{
memcpy( p_sig->r, p_buf, 2 + i_r_len );
p_buf += 2 + i_r_len;
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;
}
int i_s_len = mpi_len( p_buf );
if( i_s_len > 20 )
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 )
{
free( p_sig->hashed_data );
free( p_sig->unhashed_data );
return VLC_EGENERIC;
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;
}
else
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 )
{
memcpy( p_sig->s, p_buf, 2 + i_s_len );
p_buf += 2 + i_s_len;
free( p_sig->specific.v4.hashed_data );
free( p_sig->specific.v4.unhashed_data );
}
return VLC_SUCCESS;
return VLC_EGENERIC;
}
/*
......@@ -362,7 +510,7 @@ static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
* We're given the file's url, we just append ".asc" to it and download
*/
static int download_signature( vlc_object_t *p_this,
signature_packet_v3_t *p_sig,
signature_packet_t *p_sig,
const char *psz_url )
{
char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
......@@ -379,22 +527,9 @@ static int download_signature( vlc_object_t *p_this,
return VLC_ENOMEM;
int64_t i_size = stream_Size( p_stream );
if( i_size <= 65 ) /* binary format signature */
{
msg_Dbg( p_this, "Downloading unarmored signature" );
int i_read = stream_Read( p_stream, p_sig, (int)i_size );
stream_Delete( p_stream );
if( i_read != i_size )
{
msg_Dbg( p_this, "Couldn't read full signature" );