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