1 /*****************************************************************************
2 * update_crypto.c: DSA/SHA1 related functions used for updating
3 *****************************************************************************
4 * Copyright © 2008-2009 VLC authors and VideoLAN
7 * Authors: Rafaël Carré <funman@videolanorg>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either release 2 of the License, or
12 * (at your option) any later release.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains functions related to OpenPGP in VLC update management
29 /*****************************************************************************
31 *****************************************************************************/
42 #include "vlc_common.h"
43 #include <vlc_stream.h>
44 #include <vlc_strings.h>
50 /*****************************************************************************
52 *****************************************************************************/
54 #define packet_type( c ) ( ( c & 0x3c ) >> 2 ) /* 0x3C = 00111100 */
55 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
58 static inline int scalar_number( const uint8_t *p, int header_len )
60 assert( header_len == 1 || header_len == 2 || header_len == 4 );
64 else if( header_len == 2 )
65 return( (p[0] << 8) + p[1] );
66 else if( header_len == 4 )
67 return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
73 /* number of data bytes in a MPI */
74 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
76 #define READ_MPI(n, bits) do { \
77 if( i_read + 2 > i_packet_len ) \
79 int len = mpi_len( p_buf ); \
80 if( len > (bits)/8 || i_read + 2 + len > i_packet_len ) \
83 memcpy( n, p_buf, len ); \
84 p_buf += len; i_read += len; \
88 * fill a public_key_packet_t structure from public key packet data
89 * verify that it is a version 4 public key packet, using DSA
91 static int parse_public_key_packet( public_key_packet_t *p_key,
92 const uint8_t *p_buf, size_t i_packet_len )
95 if( i_packet_len > 418 || i_packet_len < 6 )
100 p_key->version = *p_buf++; i_read++;
101 if( p_key->version != 4 )
104 /* XXX: warn when timestamp is > date ? */
105 memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4; i_read += 4;
107 p_key->algo = *p_buf++; i_read++;
108 if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
111 READ_MPI(p_key->p, 1024);
112 READ_MPI(p_key->q, 160);
113 READ_MPI(p_key->g, 1024);
114 READ_MPI(p_key->y, 1024);
116 if( i_read != i_packet_len ) /* some extra data eh ? */
126 static size_t parse_signature_v3_packet( signature_packet_t *p_sig,
127 const uint8_t *p_buf, size_t i_sig_len )
129 size_t i_read = 1; /* we already read the version byte */
131 if( i_sig_len < 19 ) /* signature is at least 19 bytes + the 2 MPIs */
134 p_sig->specific.v3.hashed_data_len = *p_buf++; i_read++;
135 if( p_sig->specific.v3.hashed_data_len != 5 )
138 p_sig->type = *p_buf++; i_read++;
140 memcpy( p_sig->specific.v3.timestamp, p_buf, 4 );
141 p_buf += 4; i_read += 4;
143 memcpy( p_sig->issuer_longid, p_buf, 8 );
144 p_buf += 8; i_read += 8;
146 p_sig->public_key_algo = *p_buf++; i_read++;
148 p_sig->digest_algo = *p_buf++; i_read++;
150 p_sig->hash_verification[0] = *p_buf++; i_read++;
151 p_sig->hash_verification[1] = *p_buf++; i_read++;
153 assert( i_read == 19 );
160 * fill a signature_packet_v4_t from signature packet data
161 * verify that it was used with a DSA public key, using SHA-1 digest
163 static size_t parse_signature_v4_packet( signature_packet_t *p_sig,
164 const uint8_t *p_buf, size_t i_sig_len )
166 size_t i_read = 1; /* we already read the version byte */
168 if( i_sig_len < 10 ) /* signature is at least 10 bytes + the 2 MPIs */
171 p_sig->type = *p_buf++; i_read++;
173 p_sig->public_key_algo = *p_buf++; i_read++;
175 p_sig->digest_algo = *p_buf++; i_read++;
177 memcpy( p_sig->specific.v4.hashed_data_len, p_buf, 2 );
178 p_buf += 2; i_read += 2;
180 size_t i_hashed_data_len =
181 scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
182 i_read += i_hashed_data_len;
183 if( i_read + 4 > i_sig_len )
186 p_sig->specific.v4.hashed_data = (uint8_t*) malloc( i_hashed_data_len );
187 if( !p_sig->specific.v4.hashed_data )
189 memcpy( p_sig->specific.v4.hashed_data, p_buf, i_hashed_data_len );
190 p_buf += i_hashed_data_len;
192 memcpy( p_sig->specific.v4.unhashed_data_len, p_buf, 2 );
193 p_buf += 2; i_read += 2;
195 size_t i_unhashed_data_len =
196 scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
197 i_read += i_unhashed_data_len;
198 if( i_read + 2 > i_sig_len )
201 p_sig->specific.v4.unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
202 if( !p_sig->specific.v4.unhashed_data )
205 memcpy( p_sig->specific.v4.unhashed_data, p_buf, i_unhashed_data_len );
206 p_buf += i_unhashed_data_len;
208 memcpy( p_sig->hash_verification, p_buf, 2 );
209 p_buf += 2; i_read += 2;
211 uint8_t *p, *max_pos;
212 p = p_sig->specific.v4.unhashed_data;
213 max_pos = p + scalar_number( p_sig->specific.v4.unhashed_data_len, 2 );
220 size_t i_subpacket_len;
223 if( p + 1 > max_pos )
225 i_subpacket_len = *p++;
229 if( p + 2 > max_pos )
231 i_subpacket_len = (*p++ - 192) << 8;
232 i_subpacket_len += *p++ + 192;
236 if( p + 4 > max_pos )
238 i_subpacket_len = *++p << 24;
239 i_subpacket_len += *++p << 16;
240 i_subpacket_len += *++p << 8;
241 i_subpacket_len += *++p;
244 if( *p == ISSUER_SUBPACKET )
246 if( p + 9 > max_pos )
249 memcpy( &p_sig->issuer_longid, p+1, 8 );
254 p += i_subpacket_len;
259 static int parse_signature_packet( signature_packet_t *p_sig,
260 const uint8_t *p_buf, size_t i_packet_len )
262 if( !i_packet_len ) /* 1st sanity check, we need at least the version */
265 p_sig->version = *p_buf++;
268 switch( p_sig->version )
271 i_read = parse_signature_v3_packet( p_sig, p_buf, i_packet_len );
274 p_sig->specific.v4.hashed_data = NULL;
275 p_sig->specific.v4.unhashed_data = NULL;
276 i_read = parse_signature_v4_packet( p_sig, p_buf, i_packet_len );
282 if( i_read == 0 ) /* signature packet parsing has failed */
285 if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
288 if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
291 switch( p_sig->type )
293 case BINARY_SIGNATURE:
295 case GENERIC_KEY_SIGNATURE:
296 case PERSONA_KEY_SIGNATURE:
297 case CASUAL_KEY_SIGNATURE:
298 case POSITIVE_KEY_SIGNATURE:
304 p_buf--; /* rewind to the version byte */
307 READ_MPI(p_sig->r, 160);
308 READ_MPI(p_sig->s, 160);
310 assert( i_read == i_packet_len );
311 if( i_read < i_packet_len ) /* some extra data, hm ? */
318 if( p_sig->version == 4 )
320 free( p_sig->specific.v4.hashed_data );
321 free( p_sig->specific.v4.unhashed_data );
329 * crc_octets() was lamely copied from rfc 2440
330 * Copyright (C) The Internet Society (1998). All Rights Reserved.
332 #define CRC24_INIT 0xB704CEL
333 #define CRC24_POLY 0x1864CFBL
335 static long crc_octets( uint8_t *octets, size_t len )
337 long crc = CRC24_INIT;
341 crc ^= (*octets++) << 16;
342 for (i = 0; i < 8; i++)
349 return crc & 0xFFFFFFL;
354 * Transform an armored document in binary format
355 * Used on public keys and signatures
357 static int pgp_unarmor( const char *p_ibuf, size_t i_ibuf_len,
358 uint8_t *p_obuf, size_t i_obuf_len )
360 const char *p_ipos = p_ibuf;
361 uint8_t *p_opos = p_obuf;
363 int i_header_skipped = 0;
365 while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
367 if( *p_ipos == '\r' || *p_ipos == '\n' )
373 size_t i_line_len = strcspn( p_ipos, "\r\n" );
374 if( i_line_len == 0 )
377 if( !i_header_skipped )
379 if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
380 i_header_skipped = 1;
382 p_ipos += i_line_len + 1;
386 if( !strncmp( p_ipos, "Version:", 8 ) )
388 p_ipos += i_line_len + 1;
392 if( p_ipos[i_line_len - 1] == '=' )
397 p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
398 p_obuf - p_opos + i_obuf_len, p_ipos );
399 p_ipos += i_line_len + 1;
402 /* XXX: the CRC is OPTIONAL, really require it ? */
403 if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
407 if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
410 long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
411 long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
413 return l_crc2 == l_crc ? p_opos - p_obuf : 0;
418 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
420 int verify_signature( uint8_t *p_r, uint8_t *p_s, public_key_packet_t *p_key,
423 /* the data to be verified (a SHA-1 hash) */
424 const char *hash_sexp_s = "(data(flags raw)(value %m))";
426 const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
428 const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
431 gcry_mpi_t p, q, g, y, r, s, hash;
432 p = q = g = y = r = s = hash = NULL;
433 gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
434 key_sexp = hash_sexp = sig_sexp = NULL;
436 int i_p_len = mpi_len( p_key->p );
437 int i_q_len = mpi_len( p_key->q );
438 int i_g_len = mpi_len( p_key->g );
439 int i_y_len = mpi_len( p_key->y );
440 if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, i_p_len, NULL ) ||
441 gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, i_q_len, NULL ) ||
442 gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, i_g_len, NULL ) ||
443 gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, i_y_len, NULL ) ||
444 gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
447 int i_r_len = mpi_len( p_r );
448 int i_s_len = mpi_len( p_s );
449 if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, i_r_len, NULL ) ||
450 gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, i_s_len, NULL ) ||
451 gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
455 if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
456 gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
459 if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
465 if( p ) gcry_mpi_release( p );
466 if( q ) gcry_mpi_release( q );
467 if( g ) gcry_mpi_release( g );
468 if( y ) gcry_mpi_release( y );
469 if( r ) gcry_mpi_release( r );
470 if( s ) gcry_mpi_release( s );
471 if( hash ) gcry_mpi_release( hash );
472 if( key_sexp ) gcry_sexp_release( key_sexp );
473 if( sig_sexp ) gcry_sexp_release( sig_sexp );
474 if( hash_sexp ) gcry_sexp_release( hash_sexp );
480 * fill a public_key_t with public key data, including:
481 * * public key packet
482 * * signature packet issued by key which long id is p_sig_issuer
485 int parse_public_key( const uint8_t *p_key_data, size_t i_key_len,
486 public_key_t *p_key, const uint8_t *p_sig_issuer )
488 const uint8_t *pos = p_key_data;
489 const uint8_t *max_pos = pos + i_key_len;
492 #define PUBLIC_KEY_FOUND 0x01
493 #define USER_ID_FOUND 0x02
494 #define SIGNATURE_FOUND 0X04
496 uint8_t *p_key_unarmored = NULL;
498 p_key->psz_username = NULL;
499 p_key->sig.specific.v4.hashed_data = NULL;
500 p_key->sig.specific.v4.unhashed_data = NULL;
502 if( !( *pos & 0x80 ) )
503 { /* first byte is ASCII, unarmoring */
504 p_key_unarmored = (uint8_t*)malloc( i_key_len );
505 if( !p_key_unarmored )
507 int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
508 p_key_unarmored, i_key_len );
513 pos = p_key_unarmored;
514 max_pos = pos + i_len;
517 while( pos < max_pos )
519 if( !(*pos & 0x80) || *pos & 0x40 )
522 int i_type = packet_type( *pos );
524 int i_header_len = packet_header_len( *pos++ );
525 if( pos + i_header_len > max_pos ||
526 ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) )
529 int i_packet_len = scalar_number( pos, i_header_len );
532 if( pos + i_packet_len > max_pos )
537 case PUBLIC_KEY_PACKET:
538 i_status |= PUBLIC_KEY_FOUND;
539 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
543 case SIGNATURE_PACKET: /* we accept only v4 signatures here */
544 if( i_status & SIGNATURE_FOUND || !p_sig_issuer )
546 int i_ret = parse_signature_packet( &p_key->sig, pos,
548 if( i_ret == VLC_SUCCESS )
550 if( p_key->sig.version != 4 )
552 if( memcmp( p_key->sig.issuer_longid, p_sig_issuer, 8 ) )
554 free( p_key->sig.specific.v4.hashed_data );
555 free( p_key->sig.specific.v4.unhashed_data );
556 p_key->sig.specific.v4.hashed_data = NULL;
557 p_key->sig.specific.v4.unhashed_data = NULL;
560 i_status |= SIGNATURE_FOUND;
565 if( p_key->psz_username ) /* save only the first User ID */
567 i_status |= USER_ID_FOUND;
568 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
569 if( !p_key->psz_username )
572 memcpy( p_key->psz_username, pos, i_packet_len );
573 p_key->psz_username[i_packet_len] = '\0';
581 free( p_key_unarmored );
583 if( !( i_status & ( PUBLIC_KEY_FOUND | USER_ID_FOUND ) ) )
586 if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
592 if( p_key->sig.version == 4 )
594 free( p_key->sig.specific.v4.hashed_data );
595 free( p_key->sig.specific.v4.unhashed_data );
597 free( p_key->psz_username );
598 free( p_key_unarmored );
603 /* hash a binary file */
604 static int hash_from_binary_file( const char *psz_file, gcry_md_hd_t hd )
606 uint8_t buffer[4096];
609 FILE *f = vlc_fopen( psz_file, "r" );
613 while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
614 gcry_md_write( hd, buffer, i_read );
622 /* final part of the hash */
623 static uint8_t *hash_finish( gcry_md_hd_t hd, signature_packet_t *p_sig )
625 if( p_sig->version == 3 )
627 gcry_md_putc( hd, p_sig->type );
628 gcry_md_write( hd, &p_sig->specific.v3.timestamp, 4 );
630 else if( p_sig->version == 4 )
632 gcry_md_putc( hd, p_sig->version );
633 gcry_md_putc( hd, p_sig->type );
634 gcry_md_putc( hd, p_sig->public_key_algo );
635 gcry_md_putc( hd, p_sig->digest_algo );
636 gcry_md_write( hd, p_sig->specific.v4.hashed_data_len, 2 );
637 size_t i_len = scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
638 gcry_md_write( hd, p_sig->specific.v4.hashed_data, i_len );
640 gcry_md_putc( hd, 0x04 );
641 gcry_md_putc( hd, 0xFF );
643 i_len += 6; /* hashed data + 6 bytes header */
645 gcry_md_putc( hd, (i_len >> 24) & 0xff );
646 gcry_md_putc( hd, (i_len >> 16) & 0xff );
647 gcry_md_putc( hd, (i_len >> 8) & 0xff );
648 gcry_md_putc( hd, (i_len) & 0xff );
651 { /* RFC 4880 only tells about versions 3 and 4 */
657 uint8_t *p_tmp = (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1);
658 uint8_t *p_hash = malloc( 20 );
660 memcpy( p_hash, p_tmp, 20 );
667 * return a sha1 hash of a text
669 uint8_t *hash_sha1_from_text( const char *psz_string,
670 signature_packet_t *p_sig )
673 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
676 if( p_sig->type == TEXT_SIGNATURE )
679 size_t i_len = strcspn( psz_string, "\r\n" );
683 gcry_md_write( hd, psz_string, i_len );
686 gcry_md_putc( hd, '\r' );
687 gcry_md_putc( hd, '\n' );
689 if( *psz_string == '\r' )
691 if( *psz_string == '\n' )
695 gcry_md_write( hd, psz_string, strlen( psz_string ) );
697 return hash_finish( hd, p_sig );
702 * return a sha1 hash of a file
704 uint8_t *hash_sha1_from_file( const char *psz_file, signature_packet_t *p_sig )
707 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
710 if( hash_from_binary_file( psz_file, hd ) < 0 )
716 return hash_finish( hd, p_sig );
721 * Generate a SHA1 hash on a public key, to verify a signature made on that hash
722 * Note that we need the signature (v4) to compute the hash
724 uint8_t *hash_sha1_from_public_key( public_key_t *p_pkey )
726 if( p_pkey->sig.version != 4 )
729 if( p_pkey->sig.type < GENERIC_KEY_SIGNATURE ||
730 p_pkey->sig.type > POSITIVE_KEY_SIGNATURE )
733 gcry_error_t error = 0;
736 error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
740 gcry_md_putc( hd, 0x99 );
742 size_t i_p_len = mpi_len( p_pkey->key.p );
743 size_t i_g_len = mpi_len( p_pkey->key.g );
744 size_t i_q_len = mpi_len( p_pkey->key.q );
745 size_t i_y_len = mpi_len( p_pkey->key.y );
747 size_t i_size = 6 + 2*4 + i_p_len + i_g_len + i_q_len + i_y_len;
749 gcry_md_putc( hd, (i_size >> 8) & 0xff );
750 gcry_md_putc( hd, i_size & 0xff );
752 gcry_md_putc( hd, p_pkey->key.version );
753 gcry_md_write( hd, p_pkey->key.timestamp, 4 );
754 gcry_md_putc( hd, p_pkey->key.algo );
756 gcry_md_write( hd, (uint8_t*)&p_pkey->key.p, 2 );
757 gcry_md_write( hd, (uint8_t*)&p_pkey->key.p + 2, i_p_len );
759 gcry_md_write( hd, (uint8_t*)&p_pkey->key.q, 2 );
760 gcry_md_write( hd, (uint8_t*)&p_pkey->key.q + 2, i_q_len );
762 gcry_md_write( hd, (uint8_t*)&p_pkey->key.g, 2 );
763 gcry_md_write( hd, (uint8_t*)&p_pkey->key.g + 2, i_g_len );
765 gcry_md_write( hd, (uint8_t*)&p_pkey->key.y, 2 );
766 gcry_md_write( hd, (uint8_t*)&p_pkey->key.y + 2, i_y_len );
768 gcry_md_putc( hd, 0xb4 );
770 size_t i_len = strlen((char*)p_pkey->psz_username);
772 gcry_md_putc( hd, (i_len >> 24) & 0xff );
773 gcry_md_putc( hd, (i_len >> 16) & 0xff );
774 gcry_md_putc( hd, (i_len >> 8) & 0xff );
775 gcry_md_putc( hd, (i_len) & 0xff );
777 gcry_md_write( hd, p_pkey->psz_username, i_len );
779 uint8_t *p_hash = hash_finish( hd, &p_pkey->sig );
781 p_hash[0] != p_pkey->sig.hash_verification[0] ||
782 p_hash[1] != p_pkey->sig.hash_verification[1] )
793 * download a public key (the last one) from videolan server, and parse it
795 public_key_t *download_key( vlc_object_t *p_this,
796 const uint8_t *p_longid, const uint8_t *p_signature_issuer )
799 if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
800 p_longid[0], p_longid[1], p_longid[2], p_longid[3],
801 p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
804 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
809 int64_t i_size = stream_Size( p_stream );
812 stream_Delete( p_stream );
816 uint8_t *p_buf = (uint8_t*)malloc( i_size );
819 stream_Delete( p_stream );
823 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
824 stream_Delete( p_stream );
826 if( i_read != (int)i_size )
828 msg_Dbg( p_this, "Couldn't read full GPG key" );
833 public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
840 memcpy( p_pkey->longid, p_longid, 8 );
842 int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
845 if( i_error != VLC_SUCCESS )
847 msg_Dbg( p_this, "Couldn't parse GPG key" );
857 * Download the signature associated to a document or a binary file.
858 * We're given the file's url, we just append ".asc" to it and download
860 int download_signature( vlc_object_t *p_this, signature_packet_t *p_sig,
861 const char *psz_url )
863 char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
867 strcpy( psz_sig, psz_url );
868 strcat( psz_sig, ".asc" );
870 stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
876 int64_t i_size = stream_Size( p_stream );
878 msg_Dbg( p_this, "Downloading signature (%"PRId64" bytes)", i_size );
879 uint8_t *p_buf = (uint8_t*)malloc( i_size );
882 stream_Delete( p_stream );
886 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
888 stream_Delete( p_stream );
890 if( i_read != (int)i_size )
893 "Couldn't download full signature (only %d bytes)", i_read );
898 if( (uint8_t)*p_buf < 0x80 ) /* ASCII */
900 msg_Dbg( p_this, "Unarmoring signature" );
902 uint8_t* p_unarmored = (uint8_t*) malloc( ( i_size * 3 ) / 4 + 1 );
909 int i_bytes = pgp_unarmor( (char*)p_buf, i_size, p_unarmored, i_size );
918 msg_Dbg( p_this, "Unarmoring failed : corrupted signature ?" );
923 if( packet_type( *p_buf ) != SIGNATURE_PACKET )
926 msg_Dbg( p_this, "Not a signature: %d", *p_buf );
930 size_t i_header_len = packet_header_len( *p_buf );
931 if( ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) ||
932 i_header_len + 1 > (size_t)i_size )
935 msg_Dbg( p_this, "Invalid signature packet header" );
939 size_t i_len = scalar_number( p_buf+1, i_header_len );
940 if( i_len + i_header_len + 1 != (size_t)i_size )
943 msg_Dbg( p_this, "Invalid signature packet" );
947 int i_ret = parse_signature_packet( p_sig, p_buf+1+i_header_len, i_len );
949 if( i_ret != VLC_SUCCESS )
951 msg_Dbg( p_this, "Couldn't parse signature" );
955 if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
957 msg_Dbg( p_this, "Invalid signature type: %d", p_sig->type );
958 if( p_sig->version == 4 )
960 free( p_sig->specific.v4.hashed_data );
961 free( p_sig->specific.v4.unhashed_data );
969 #endif /* UPDATE_CHECK */