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