]> git.sesse.net Git - vlc/blob - src/misc/update_crypto.c
update: sha1 is not mandatory anymore
[vlc] / src / misc / update_crypto.c
1 /*****************************************************************************
2  * update_crypto.c: DSA related functions used for updating
3  *****************************************************************************
4  * Copyright © 2008-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Rafaël Carré <funman@videolanorg>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /**
25  *   \file
26  *   This file contains functions related to OpenPGP in VLC update management
27  */
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #ifdef UPDATE_CHECK
38
39 #include <gcrypt.h>
40 #include <assert.h>
41
42 #include "vlc_common.h"
43 #include <vlc_stream.h>
44 #include <vlc_strings.h>
45 #include <vlc_fs.h>
46
47 #include "update.h"
48
49
50 /*****************************************************************************
51  * OpenPGP functions
52  *****************************************************************************/
53
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 */
56
57
58 static inline int scalar_number( const uint8_t *p, int header_len )
59 {
60     assert( header_len == 1 || header_len == 2 || header_len == 4 );
61
62     if( header_len == 1 )
63         return( p[0] );
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] );
68     else
69         abort();
70 }
71
72
73 /* number of data bytes in a MPI */
74 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
75
76 #define READ_MPI(n, bits) do { \
77     if( i_read + 2 > i_packet_len ) \
78         goto error; \
79     int len = mpi_len( p_buf ); \
80     if( len > (bits)/8 || i_read + 2 + len > i_packet_len ) \
81         goto error; \
82     len += 2; \
83     memcpy( n, p_buf, len ); \
84     p_buf += len; i_read += len; \
85     } while(0)
86
87 /*
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
90  */
91 static int parse_public_key_packet( public_key_packet_t *p_key,
92                                     const uint8_t *p_buf, size_t i_packet_len )
93 {
94
95     if( i_packet_len > 418 || i_packet_len < 6 )
96         return VLC_EGENERIC;
97
98     size_t i_read = 0;
99
100     p_key->version   = *p_buf++; i_read++;
101     if( p_key->version != 4 )
102         return VLC_EGENERIC;
103
104     /* XXX: warn when timestamp is > date ? */
105     memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4; i_read += 4;
106
107     p_key->algo      = *p_buf++; i_read++;
108     if( p_key->algo != GCRY_PK_DSA )
109         return VLC_EGENERIC;
110
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);
115
116     if( i_read != i_packet_len ) /* some extra data eh ? */
117         return VLC_EGENERIC;
118
119     return VLC_SUCCESS;
120
121 error:
122     return VLC_EGENERIC;
123 }
124
125
126 static size_t parse_signature_v3_packet( signature_packet_t *p_sig,
127                                       const uint8_t *p_buf, size_t i_sig_len )
128 {
129     size_t i_read = 1; /* we already read the version byte */
130
131     if( i_sig_len < 19 ) /* signature is at least 19 bytes + the 2 MPIs */
132         return 0;
133
134     p_sig->specific.v3.hashed_data_len = *p_buf++; i_read++;
135     if( p_sig->specific.v3.hashed_data_len != 5 )
136         return 0;
137
138     p_sig->type = *p_buf++; i_read++;
139
140     memcpy( p_sig->specific.v3.timestamp, p_buf, 4 );
141     p_buf += 4; i_read += 4;
142
143     memcpy( p_sig->issuer_longid, p_buf, 8 );
144     p_buf += 8; i_read += 8;
145
146     p_sig->public_key_algo = *p_buf++; i_read++;
147
148     p_sig->digest_algo = *p_buf++; i_read++;
149
150     p_sig->hash_verification[0] = *p_buf++; i_read++;
151     p_sig->hash_verification[1] = *p_buf++; i_read++;
152
153     assert( i_read == 19 );
154
155     return i_read;
156 }
157
158
159 /*
160  * fill a signature_packet_v4_t from signature packet data
161  * verify that it was used with a DSA public key
162  */
163 static size_t parse_signature_v4_packet( signature_packet_t *p_sig,
164                                       const uint8_t *p_buf, size_t i_sig_len )
165 {
166     size_t i_read = 1; /* we already read the version byte */
167
168     if( i_sig_len < 10 ) /* signature is at least 10 bytes + the 2 MPIs */
169         return 0;
170
171     p_sig->type = *p_buf++; i_read++;
172
173     p_sig->public_key_algo = *p_buf++; i_read++;
174
175     p_sig->digest_algo = *p_buf++; i_read++;
176
177     memcpy( p_sig->specific.v4.hashed_data_len, p_buf, 2 );
178     p_buf += 2; i_read += 2;
179
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 )
184         return 0;
185
186     p_sig->specific.v4.hashed_data = (uint8_t*) malloc( i_hashed_data_len );
187     if( !p_sig->specific.v4.hashed_data )
188         return 0;
189     memcpy( p_sig->specific.v4.hashed_data, p_buf, i_hashed_data_len );
190     p_buf += i_hashed_data_len;
191
192     memcpy( p_sig->specific.v4.unhashed_data_len, p_buf, 2 );
193     p_buf += 2; i_read += 2;
194
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 )
199         return 0;
200
201     p_sig->specific.v4.unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
202     if( !p_sig->specific.v4.unhashed_data )
203         return 0;
204
205     memcpy( p_sig->specific.v4.unhashed_data, p_buf, i_unhashed_data_len );
206     p_buf += i_unhashed_data_len;
207
208     memcpy( p_sig->hash_verification, p_buf, 2 );
209     p_buf += 2; i_read += 2;
210
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 );
214
215     for( ;; )
216     {
217         if( p > max_pos )
218             return 0;
219
220         size_t i_subpacket_len;
221         if( *p < 192 )
222         {
223             if( p + 1 > max_pos )
224                 return 0;
225             i_subpacket_len = *p++;
226         }
227         else if( *p < 255 )
228         {
229             if( p + 2 > max_pos )
230                 return 0;
231             i_subpacket_len = (*p++ - 192) << 8;
232             i_subpacket_len += *p++ + 192;
233         }
234         else
235         {
236             if( p + 4 > max_pos )
237                 return 0;
238             i_subpacket_len = *++p << 24;
239             i_subpacket_len += *++p << 16;
240             i_subpacket_len += *++p << 8;
241             i_subpacket_len += *++p;
242         }
243
244         if( *p == ISSUER_SUBPACKET )
245         {
246             if( p + 9 > max_pos )
247                 return 0;
248
249             memcpy( &p_sig->issuer_longid, p+1, 8 );
250
251             return i_read;
252         }
253
254         p += i_subpacket_len;
255     }
256 }
257
258
259 static int parse_signature_packet( signature_packet_t *p_sig,
260                                    const uint8_t *p_buf, size_t i_packet_len )
261 {
262     if( !i_packet_len ) /* 1st sanity check, we need at least the version */
263         return VLC_EGENERIC;
264
265     p_sig->version = *p_buf++;
266
267     size_t i_read;
268     switch( p_sig->version )
269     {
270         case 3:
271             i_read = parse_signature_v3_packet( p_sig, p_buf, i_packet_len );
272             break;
273         case 4:
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 );
277             break;
278         default:
279             return VLC_EGENERIC;
280     }
281
282     if( i_read == 0 ) /* signature packet parsing has failed */
283         goto error;
284
285     if( p_sig->public_key_algo != GCRY_PK_DSA )
286         goto error;
287
288     switch( p_sig->type )
289     {
290         case BINARY_SIGNATURE:
291         case TEXT_SIGNATURE:
292         case GENERIC_KEY_SIGNATURE:
293         case PERSONA_KEY_SIGNATURE:
294         case CASUAL_KEY_SIGNATURE:
295         case POSITIVE_KEY_SIGNATURE:
296             break;
297         default:
298             goto error;
299     }
300
301     p_buf--; /* rewind to the version byte */
302     p_buf += i_read;
303
304     READ_MPI(p_sig->r, 160);
305     READ_MPI(p_sig->s, 160);
306
307     assert( i_read == i_packet_len );
308     if( i_read < i_packet_len ) /* some extra data, hm ? */
309         goto error;
310
311     return VLC_SUCCESS;
312
313 error:
314
315     if( p_sig->version == 4 )
316     {
317         free( p_sig->specific.v4.hashed_data );
318         free( p_sig->specific.v4.unhashed_data );
319     }
320
321     return VLC_EGENERIC;
322 }
323
324
325 /*
326  * crc_octets() was lamely copied from rfc 2440
327  * Copyright (C) The Internet Society (1998).  All Rights Reserved.
328  */
329 #define CRC24_INIT 0xB704CEL
330 #define CRC24_POLY 0x1864CFBL
331
332 static long crc_octets( uint8_t *octets, size_t len )
333 {
334     long crc = CRC24_INIT;
335     int i;
336     while (len--)
337     {
338         crc ^= (*octets++) << 16;
339         for (i = 0; i < 8; i++)
340         {
341             crc <<= 1;
342             if (crc & 0x1000000)
343                 crc ^= CRC24_POLY;
344         }
345     }
346     return crc & 0xFFFFFFL;
347 }
348
349
350 /*
351  * Transform an armored document in binary format
352  * Used on public keys and signatures
353  */
354 static int pgp_unarmor( const char *p_ibuf, size_t i_ibuf_len,
355                         uint8_t *p_obuf, size_t i_obuf_len )
356 {
357     const char *p_ipos = p_ibuf;
358     uint8_t *p_opos = p_obuf;
359     int i_end = 0;
360     int i_header_skipped = 0;
361
362     while( !i_end && p_ipos < p_ibuf + i_ibuf_len && *p_ipos != '=' )
363     {
364         if( *p_ipos == '\r' || *p_ipos == '\n' )
365         {
366             p_ipos++;
367             continue;
368         }
369
370         size_t i_line_len = strcspn( p_ipos, "\r\n" );
371         if( i_line_len == 0 )
372             continue;
373
374         if( !i_header_skipped )
375         {
376             if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
377                 i_header_skipped = 1;
378
379             p_ipos += i_line_len + 1;
380             continue;
381         }
382
383         if( !strncmp( p_ipos, "Version:", 8 ) )
384         {
385             p_ipos += i_line_len + 1;
386             continue;
387         }
388
389         if( p_ipos[i_line_len - 1] == '=' )
390         {
391             i_end = 1;
392         }
393
394         p_opos += vlc_b64_decode_binary_to_buffer(  p_opos,
395                         p_obuf - p_opos + i_obuf_len, p_ipos );
396         p_ipos += i_line_len + 1;
397     }
398
399     /* XXX: the CRC is OPTIONAL, really require it ? */
400     if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
401         return 0;
402
403     uint8_t p_crc[3];
404     if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
405         return 0;
406
407     long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
408     long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
409
410     return l_crc2 == l_crc ? p_opos - p_obuf : 0;
411 }
412
413
414 /*
415  * Verify an OpenPGP signature made with some DSA public key
416  */
417 int verify_signature( signature_packet_t *sign, public_key_packet_t *p_key,
418                       uint8_t *p_hash )
419 {
420     /* the data to be verified (a hash) */
421     const char *hash_sexp_s = "(data(flags raw)(value %m))";
422     /* the public key */
423     const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
424     /* the signature */
425     const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
426
427     size_t erroff;
428     gcry_mpi_t p, q, g, y, r, s, hash;
429     p = q = g = y = r = s = hash = NULL;
430     gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
431     key_sexp = hash_sexp = sig_sexp = NULL;
432
433     int i_p_len = mpi_len( p_key->p );
434     int i_q_len = mpi_len( p_key->q );
435     int i_g_len = mpi_len( p_key->g );
436     int i_y_len = mpi_len( p_key->y );
437     if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, i_p_len, NULL ) ||
438         gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, i_q_len, NULL ) ||
439         gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, i_g_len, NULL ) ||
440         gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, i_y_len, NULL ) ||
441         gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
442         goto problem;
443
444     uint8_t *p_r = sign->r;
445     uint8_t *p_s = sign->s;
446     int i_r_len = mpi_len( p_r );
447     int i_s_len = mpi_len( p_s );
448     if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, i_r_len, NULL ) ||
449         gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, i_s_len, NULL ) ||
450         gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
451         goto problem;
452
453     int i_hash_len = gcry_md_get_algo_dlen (sign->digest_algo);
454     if (sign->public_key_algo == GCRY_PK_DSA) {
455         if (i_hash_len > 20)
456             i_hash_len = 20;
457     }
458     if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, i_hash_len, NULL ) ||
459         gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
460         goto problem;
461
462     if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
463         goto problem;
464
465     return VLC_SUCCESS;
466
467 problem:
468     if( p ) gcry_mpi_release( p );
469     if( q ) gcry_mpi_release( q );
470     if( g ) gcry_mpi_release( g );
471     if( y ) gcry_mpi_release( y );
472     if( r ) gcry_mpi_release( r );
473     if( s ) gcry_mpi_release( s );
474     if( hash ) gcry_mpi_release( hash );
475     if( key_sexp ) gcry_sexp_release( key_sexp );
476     if( sig_sexp ) gcry_sexp_release( sig_sexp );
477     if( hash_sexp ) gcry_sexp_release( hash_sexp );
478     return VLC_EGENERIC;
479 }
480
481
482 /*
483  * fill a public_key_t with public key data, including:
484  *   * public key packet
485  *   * signature packet issued by key which long id is p_sig_issuer
486  *   * user id packet
487  */
488 int parse_public_key( const uint8_t *p_key_data, size_t i_key_len,
489                       public_key_t *p_key, const uint8_t *p_sig_issuer )
490 {
491     const uint8_t *pos = p_key_data;
492     const uint8_t *max_pos = pos + i_key_len;
493
494     int i_status = 0;
495 #define PUBLIC_KEY_FOUND    0x01
496 #define USER_ID_FOUND       0x02
497 #define SIGNATURE_FOUND     0X04
498
499     uint8_t *p_key_unarmored = NULL;
500
501     p_key->psz_username = NULL;
502     p_key->sig.specific.v4.hashed_data = NULL;
503     p_key->sig.specific.v4.unhashed_data = NULL;
504
505     if( !( *pos & 0x80 ) )
506     {   /* first byte is ASCII, unarmoring */
507         p_key_unarmored = (uint8_t*)malloc( i_key_len );
508         if( !p_key_unarmored )
509             return VLC_ENOMEM;
510         int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
511                                  p_key_unarmored, i_key_len );
512
513         if( i_len == 0 )
514             goto error;
515
516         pos = p_key_unarmored;
517         max_pos = pos + i_len;
518     }
519
520     while( pos < max_pos )
521     {
522         if( !(*pos & 0x80) || *pos & 0x40 )
523             goto error;
524
525         int i_type = packet_type( *pos );
526
527         int i_header_len = packet_header_len( *pos++ );
528         if( pos + i_header_len > max_pos ||
529             ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) )
530             goto error;
531
532         int i_packet_len = scalar_number( pos, i_header_len );
533         pos += i_header_len;
534
535         if( pos + i_packet_len > max_pos )
536             goto error;
537
538         switch( i_type )
539         {
540             case PUBLIC_KEY_PACKET:
541                 i_status |= PUBLIC_KEY_FOUND;
542                 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
543                     goto error;
544                 break;
545
546             case SIGNATURE_PACKET: /* we accept only v4 signatures here */
547                 if( i_status & SIGNATURE_FOUND || !p_sig_issuer )
548                     break;
549                 int i_ret = parse_signature_packet( &p_key->sig, pos,
550                                                     i_packet_len );
551                 if( i_ret == VLC_SUCCESS )
552                 {
553                     if( p_key->sig.version != 4 )
554                         break;
555                     if( memcmp( p_key->sig.issuer_longid, p_sig_issuer, 8 ) )
556                     {
557                         free( p_key->sig.specific.v4.hashed_data );
558                         free( p_key->sig.specific.v4.unhashed_data );
559                         p_key->sig.specific.v4.hashed_data = NULL;
560                         p_key->sig.specific.v4.unhashed_data = NULL;
561                         break;
562                     }
563                     i_status |= SIGNATURE_FOUND;
564                 }
565                 break;
566
567             case USER_ID_PACKET:
568                 if( p_key->psz_username ) /* save only the first User ID */
569                     break;
570                 i_status |= USER_ID_FOUND;
571                 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
572                 if( !p_key->psz_username )
573                     goto error;
574
575                 memcpy( p_key->psz_username, pos, i_packet_len );
576                 p_key->psz_username[i_packet_len] = '\0';
577                 break;
578
579             default:
580                 break;
581         }
582         pos += i_packet_len;
583     }
584     free( p_key_unarmored );
585
586     if( !( i_status & ( PUBLIC_KEY_FOUND | USER_ID_FOUND ) ) )
587         return VLC_EGENERIC;
588
589     if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
590         return VLC_EGENERIC;
591
592     return VLC_SUCCESS;
593
594 error:
595     if( p_key->sig.version == 4 )
596     {
597         free( p_key->sig.specific.v4.hashed_data );
598         free( p_key->sig.specific.v4.unhashed_data );
599     }
600     free( p_key->psz_username );
601     free( p_key_unarmored );
602     return VLC_EGENERIC;
603 }
604
605
606 /* hash a binary file */
607 static int hash_from_binary_file( const char *psz_file, gcry_md_hd_t hd )
608 {
609     uint8_t buffer[4096];
610     size_t i_read;
611
612     FILE *f = vlc_fopen( psz_file, "r" );
613     if( !f )
614         return -1;
615
616     while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
617         gcry_md_write( hd, buffer, i_read );
618
619     fclose( f );
620
621     return 0;
622 }
623
624
625 /* final part of the hash */
626 static uint8_t *hash_finish( gcry_md_hd_t hd, signature_packet_t *p_sig )
627 {
628     if( p_sig->version == 3 )
629     {
630         gcry_md_putc( hd, p_sig->type );
631         gcry_md_write( hd, &p_sig->specific.v3.timestamp, 4 );
632     }
633     else if( p_sig->version == 4 )
634     {
635         gcry_md_putc( hd, p_sig->version );
636         gcry_md_putc( hd, p_sig->type );
637         gcry_md_putc( hd, p_sig->public_key_algo );
638         gcry_md_putc( hd, p_sig->digest_algo );
639         gcry_md_write( hd, p_sig->specific.v4.hashed_data_len, 2 );
640         size_t i_len = scalar_number( p_sig->specific.v4.hashed_data_len, 2 );
641         gcry_md_write( hd, p_sig->specific.v4.hashed_data, i_len );
642
643         gcry_md_putc( hd, 0x04 );
644         gcry_md_putc( hd, 0xFF );
645
646         i_len += 6; /* hashed data + 6 bytes header */
647
648         gcry_md_putc( hd, (i_len >> 24) & 0xff );
649         gcry_md_putc( hd, (i_len >> 16) & 0xff );
650         gcry_md_putc( hd, (i_len >> 8) & 0xff );
651         gcry_md_putc( hd, (i_len) & 0xff );
652     }
653     else
654     {   /* RFC 4880 only tells about versions 3 and 4 */
655         return NULL;
656     }
657
658     gcry_md_final( hd );
659
660     uint8_t *p_tmp = (uint8_t*) gcry_md_read( hd, p_sig->digest_algo) ;
661     unsigned int hash_len = gcry_md_get_algo_dlen (p_sig->digest_algo);
662     uint8_t *p_hash = malloc(hash_len);
663     if( p_hash )
664         memcpy(p_hash, p_tmp, hash_len);
665     gcry_md_close( hd );
666     return p_hash;
667 }
668
669
670 /*
671  * return a hash of a text
672  */
673 uint8_t *hash_from_text( const char *psz_string,
674         signature_packet_t *p_sig )
675 {
676     gcry_md_hd_t hd;
677     if( gcry_md_open( &hd, p_sig->digest_algo, 0 ) )
678         return NULL;
679
680     if( p_sig->type == TEXT_SIGNATURE )
681     while( *psz_string )
682     {
683         size_t i_len = strcspn( psz_string, "\r\n" );
684
685         if( i_len )
686         {
687             gcry_md_write( hd, psz_string, i_len );
688             psz_string += i_len;
689         }
690         gcry_md_putc( hd, '\r' );
691         gcry_md_putc( hd, '\n' );
692
693         if( *psz_string == '\r' )
694             psz_string++;
695         if( *psz_string == '\n' )
696             psz_string++;
697     }
698     else
699         gcry_md_write( hd, psz_string, strlen( psz_string ) );
700
701     return hash_finish( hd, p_sig );
702 }
703
704
705 /*
706  * return a hash of a file
707  */
708 uint8_t *hash_from_file( const char *psz_file, signature_packet_t *p_sig )
709 {
710     gcry_md_hd_t hd;
711     if( gcry_md_open( &hd, p_sig->digest_algo, 0 ) )
712         return NULL;
713
714     if( hash_from_binary_file( psz_file, hd ) < 0 )
715     {
716         gcry_md_close( hd );
717         return NULL;
718     }
719
720     return hash_finish( hd, p_sig );
721 }
722
723
724 /*
725  * Generate a hash on a public key, to verify a signature made on that hash
726  * Note that we need the signature (v4) to compute the hash
727  */
728 uint8_t *hash_from_public_key( public_key_t *p_pkey )
729 {
730     if( p_pkey->sig.version != 4 )
731         return NULL;
732
733     if( p_pkey->sig.type < GENERIC_KEY_SIGNATURE ||
734         p_pkey->sig.type > POSITIVE_KEY_SIGNATURE )
735         return NULL;
736
737     gcry_error_t error = 0;
738     gcry_md_hd_t hd;
739
740     error = gcry_md_open( &hd, p_pkey->sig.digest_algo, 0 );
741     if( error )
742         return NULL;
743
744     gcry_md_putc( hd, 0x99 );
745
746     size_t i_p_len = mpi_len( p_pkey->key.p );
747     size_t i_g_len = mpi_len( p_pkey->key.g );
748     size_t i_q_len = mpi_len( p_pkey->key.q );
749     size_t i_y_len = mpi_len( p_pkey->key.y );
750
751     size_t i_size = 6 + 2*4 + i_p_len + i_g_len + i_q_len + i_y_len;
752
753     gcry_md_putc( hd, (i_size >> 8) & 0xff );
754     gcry_md_putc( hd, i_size & 0xff );
755
756     gcry_md_putc( hd, p_pkey->key.version );
757     gcry_md_write( hd, p_pkey->key.timestamp, 4 );
758     gcry_md_putc( hd, p_pkey->key.algo );
759
760     gcry_md_write( hd, (uint8_t*)&p_pkey->key.p, 2 );
761     gcry_md_write( hd, (uint8_t*)&p_pkey->key.p + 2, i_p_len );
762
763     gcry_md_write( hd, (uint8_t*)&p_pkey->key.q, 2 );
764     gcry_md_write( hd, (uint8_t*)&p_pkey->key.q + 2, i_q_len );
765
766     gcry_md_write( hd, (uint8_t*)&p_pkey->key.g, 2 );
767     gcry_md_write( hd, (uint8_t*)&p_pkey->key.g + 2, i_g_len );
768
769     gcry_md_write( hd, (uint8_t*)&p_pkey->key.y, 2 );
770     gcry_md_write( hd, (uint8_t*)&p_pkey->key.y + 2, i_y_len );
771
772     gcry_md_putc( hd, 0xb4 );
773
774     size_t i_len = strlen((char*)p_pkey->psz_username);
775
776     gcry_md_putc( hd, (i_len >> 24) & 0xff );
777     gcry_md_putc( hd, (i_len >> 16) & 0xff );
778     gcry_md_putc( hd, (i_len >> 8) & 0xff );
779     gcry_md_putc( hd, (i_len) & 0xff );
780
781     gcry_md_write( hd, p_pkey->psz_username, i_len );
782
783     uint8_t *p_hash = hash_finish( hd, &p_pkey->sig );
784     if( !p_hash ||
785         p_hash[0] != p_pkey->sig.hash_verification[0] ||
786         p_hash[1] != p_pkey->sig.hash_verification[1] )
787     {
788         free(p_hash);
789         return NULL;
790     }
791
792     return p_hash;
793 }
794
795
796 /*
797  * download a public key (the last one) from videolan server, and parse it
798  */
799 public_key_t *download_key( vlc_object_t *p_this,
800                     const uint8_t *p_longid, const uint8_t *p_signature_issuer )
801 {
802     char *psz_url;
803     if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X.asc",
804                     p_longid[0], p_longid[1], p_longid[2], p_longid[3],
805                     p_longid[4], p_longid[5], p_longid[6], p_longid[7] ) == -1 )
806         return NULL;
807
808     stream_t *p_stream = stream_UrlNew( p_this, psz_url );
809     free( psz_url );
810     if( !p_stream )
811         return NULL;
812
813     int64_t i_size = stream_Size( p_stream );
814     if( i_size < 0 )
815     {
816         stream_Delete( p_stream );
817         return NULL;
818     }
819
820     uint8_t *p_buf = (uint8_t*)malloc( i_size );
821     if( !p_buf )
822     {
823         stream_Delete( p_stream );
824         return NULL;
825     }
826
827     int i_read = stream_Read( p_stream, p_buf, (int)i_size );
828     stream_Delete( p_stream );
829
830     if( i_read != (int)i_size )
831     {
832         msg_Dbg( p_this, "Couldn't read full GPG key" );
833         free( p_buf );
834         return NULL;
835     }
836
837     public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
838     if( !p_pkey )
839     {
840         free( p_buf );
841         return NULL;
842     }
843
844     memcpy( p_pkey->longid, p_longid, 8 );
845
846     int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
847     free( p_buf );
848
849     if( i_error != VLC_SUCCESS )
850     {
851         msg_Dbg( p_this, "Couldn't parse GPG key" );
852         free( p_pkey );
853         return NULL;
854     }
855
856     return p_pkey;
857 }
858
859
860 /*
861  * Download the signature associated to a document or a binary file.
862  * We're given the file's url, we just append ".asc" to it and download
863  */
864 int download_signature( vlc_object_t *p_this, signature_packet_t *p_sig,
865                         const char *psz_url )
866 {
867     char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
868     if( !psz_sig )
869         return VLC_ENOMEM;
870
871     strcpy( psz_sig, psz_url );
872     strcat( psz_sig, ".asc" );
873
874     stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
875     free( psz_sig );
876
877     if( !p_stream )
878         return VLC_ENOMEM;
879
880     int64_t i_size = stream_Size( p_stream );
881
882     msg_Dbg( p_this, "Downloading signature (%"PRId64" bytes)", i_size );
883     uint8_t *p_buf = (uint8_t*)malloc( i_size );
884     if( !p_buf )
885     {
886         stream_Delete( p_stream );
887         return VLC_ENOMEM;
888     }
889
890     int i_read = stream_Read( p_stream, p_buf, (int)i_size );
891
892     stream_Delete( p_stream );
893
894     if( i_read != (int)i_size )
895     {
896         msg_Dbg( p_this,
897             "Couldn't download full signature (only %d bytes)", i_read );
898         free( p_buf );
899         return VLC_EGENERIC;
900     }
901
902     if( (uint8_t)*p_buf < 0x80 ) /* ASCII */
903     {
904         msg_Dbg( p_this, "Unarmoring signature" );
905
906         uint8_t* p_unarmored = (uint8_t*) malloc( ( i_size * 3 ) / 4 + 1 );
907         if( !p_unarmored )
908         {
909             free( p_buf );
910             return VLC_EGENERIC;
911         }
912
913         int i_bytes = pgp_unarmor( (char*)p_buf, i_size, p_unarmored, i_size );
914         free( p_buf );
915
916         p_buf = p_unarmored;
917         i_size = i_bytes;
918
919         if( i_bytes < 2 )
920         {
921             free( p_buf );
922             msg_Dbg( p_this, "Unarmoring failed : corrupted signature ?" );
923             return VLC_EGENERIC;
924         }
925     }
926
927     if( packet_type( *p_buf ) != SIGNATURE_PACKET )
928     {
929         msg_Dbg( p_this, "Not a signature: %d", *p_buf );
930         free( p_buf );
931         return VLC_EGENERIC;
932     }
933
934     size_t i_header_len = packet_header_len( *p_buf );
935     if( ( i_header_len != 1 && i_header_len != 2 && i_header_len != 4 ) ||
936         i_header_len + 1 > (size_t)i_size )
937     {
938         free( p_buf );
939         msg_Dbg( p_this, "Invalid signature packet header" );
940         return VLC_EGENERIC;
941     }
942
943     size_t i_len = scalar_number( p_buf+1, i_header_len );
944     if( i_len + i_header_len + 1 != (size_t)i_size )
945     {
946         free( p_buf );
947         msg_Dbg( p_this, "Invalid signature packet" );
948         return VLC_EGENERIC;
949     }
950
951     int i_ret = parse_signature_packet( p_sig, p_buf+1+i_header_len, i_len );
952     free( p_buf );
953     if( i_ret != VLC_SUCCESS )
954     {
955         msg_Dbg( p_this, "Couldn't parse signature" );
956         return i_ret;
957     }
958
959     if( p_sig->type != BINARY_SIGNATURE && p_sig->type != TEXT_SIGNATURE )
960     {
961         msg_Dbg( p_this, "Invalid signature type: %d", p_sig->type );
962         if( p_sig->version == 4 )
963         {
964             free( p_sig->specific.v4.hashed_data );
965             free( p_sig->specific.v4.unhashed_data );
966         }
967         return VLC_EGENERIC;
968     }
969
970     return VLC_SUCCESS;
971 }
972
973 #endif /* UPDATE_CHECK */