Add pathfinder support for certificate validation
[oweals/opkg-lede.git] / libopkg / opkg_download.c
1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the opkg package management system
3
4    Carl D. Worth
5
6    Copyright (C) 2001 University of Southern California
7    Copyright (C) 2008 OpenMoko Inc
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18 */
19 #include "config.h"
20
21 #ifdef HAVE_CURL
22 #include <curl/curl.h>
23 #endif
24
25 #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
26 #include <openssl/conf.h>
27 #include <openssl/evp.h>
28 #include <openssl/err.h>
29 #include <openssl/ssl.h>
30 #endif
31
32 #if defined(HAVE_GPGME)
33 #include <gpgme.h>
34 #elif defined(HAVE_OPENSSL)
35 #include <openssl/bio.h>
36 #include <openssl/objects.h>
37 #include <openssl/x509.h>
38 #include <openssl/pem.h>
39 #include <openssl/hmac.h>
40 #endif
41
42 #include "includes.h"
43 #include "opkg_download.h"
44 #include "opkg_message.h"
45
46 #include "sprintf_alloc.h"
47 #include "xsystem.h"
48 #include "file_util.h"
49 #include "str_util.h"
50 #include "opkg_defines.h"
51 #include "libbb/libbb.h"
52
53 #ifdef HAVE_PATHFINDER
54 #include "opkg_pathfinder.h"
55 #endif
56
57 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
58 static void openssl_init(void);
59 #endif
60
61 #ifdef HAVE_OPENSSL
62 static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath);
63 #endif
64
65 #ifdef HAVE_CURL
66 /*
67  * Make curl an instance variable so we don't have to instanciate it
68  * each time
69  */
70 static CURL *curl = NULL;
71 static void opkg_curl_cleanup(void);
72 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data);
73 #endif
74
75 int opkg_download(opkg_conf_t *conf, const char *src,
76   const char *dest_file_name, curl_progress_func cb, void *data)
77 {
78     int err = 0;
79
80     char *src_basec = xstrdup(src);
81     char *src_base = basename(src_basec);
82     char *tmp_file_location;
83
84     opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
85         
86     if (str_starts_with(src, "file:")) {
87         int ret;
88         const char *file_src = src + 5;
89         opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
90         ret = file_copy(src + 5, dest_file_name);
91         opkg_message(conf,OPKG_INFO,"Done\n");
92         free(src_basec);
93         return ret;
94     }
95
96     sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
97     err = unlink(tmp_file_location);
98     if (err && errno != ENOENT) {
99         opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
100                 __FUNCTION__, tmp_file_location, strerror(errno));
101         free(tmp_file_location);
102         free(src_basec);
103         return errno;
104     }
105
106     if (conf->http_proxy) {
107         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
108         setenv("http_proxy", conf->http_proxy, 1);
109     }
110     if (conf->ftp_proxy) {
111         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
112         setenv("ftp_proxy", conf->ftp_proxy, 1);
113     }
114     if (conf->no_proxy) {
115         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
116         setenv("no_proxy", conf->no_proxy, 1);
117     }
118
119 #ifdef HAVE_CURL
120     CURL *curl;
121     CURLcode res;
122     FILE * file = fopen (tmp_file_location, "w");
123
124     curl = opkg_curl_init (conf, cb, data);
125     if (curl)
126     {
127         curl_easy_setopt (curl, CURLOPT_URL, src);
128         curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
129
130         res = curl_easy_perform (curl);
131         fclose (file);
132         if (res)
133         {
134             long error_code;
135             curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
136             opkg_message(conf, OPKG_ERROR, "Failed to download %s. \nerror detail: %s\n", src, curl_easy_strerror(res));
137             free(tmp_file_location);
138             free(src_basec);
139             return res;
140         }
141
142     }
143     else
144     {
145         free(tmp_file_location);
146         free(src_basec);
147         return -1;
148     }
149 #else
150     {
151       int res;
152       char *wgetcmd;
153       char *wgetopts;
154       wgetopts = getenv("OPKG_WGETOPTS");
155       sprintf_alloc(&wgetcmd, "wget -q %s%s -O \"%s\" \"%s\"",
156                     (conf->http_proxy || conf->ftp_proxy) ? "-Y on " : "",
157                     (wgetopts!=NULL) ? wgetopts : "",
158                     tmp_file_location, src);
159       opkg_message(conf, OPKG_INFO, "Executing: %s\n", wgetcmd);
160       res = xsystem(wgetcmd);
161       free(wgetcmd);
162       if (res) {
163         opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, res);
164         free(tmp_file_location);
165         free(src_basec);
166         return res;
167       }
168     }
169 #endif
170
171     err = file_move(tmp_file_location, dest_file_name);
172
173     free(tmp_file_location);
174     free(src_basec);
175
176     if (err) {
177         return err;
178     }
179
180     return 0;
181 }
182
183 static int opkg_download_cache(opkg_conf_t *conf, const char *src,
184   const char *dest_file_name, curl_progress_func cb, void *data)
185 {
186     char *cache_name = xstrdup(src);
187     char *cache_location, *p;
188     int err = 0;
189
190     if (!conf->cache || str_starts_with(src, "file:")) {
191         err = opkg_download(conf, src, dest_file_name, cb, data);
192         goto out1;
193     }
194
195     for (p = cache_name; *p; p++)
196         if (*p == '/')
197             *p = ',';   /* looks nicer than | or # */
198
199     sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
200     if (file_exists(cache_location))
201         opkg_message(conf, OPKG_NOTICE, "Copying %s\n", cache_location);
202     else {
203         err = opkg_download(conf, src, cache_location, cb, data);
204         if (err) {
205             (void) unlink(cache_location);
206             goto out2;
207         }
208     }
209
210     err = file_copy(cache_location, dest_file_name);
211
212
213 out2:
214     free(cache_location);
215 out1:
216     free(cache_name);
217     return err;
218 }
219
220 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
221 {
222     int err;
223     char *url;
224     char *stripped_filename;
225
226     if (pkg->src == NULL) {
227         opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
228                 pkg->name, pkg->parent->name);
229         return -1;
230     }
231     if (pkg->filename == NULL) {
232         opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) does not have a valid filename field.\n",pkg->name, pkg->parent->name);
233         return -1;
234     }
235
236     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
237
238     /* XXX: BUG: The pkg->filename might be something like
239        "../../foo.opk". While this is correct, and exactly what we
240        want to use to construct url above, here we actually need to
241        use just the filename part, without any directory. */
242
243     stripped_filename = strrchr(pkg->filename, '/');
244     if ( ! stripped_filename )
245         stripped_filename = pkg->filename;
246
247     sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
248
249     err = opkg_download_cache(conf, url, pkg->local_filename, NULL, NULL);
250     free(url);
251
252     return err;
253 }
254
255 /*
256  * Downloads file from url, installs in package database, return package name. 
257  */
258 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
259 {
260      int err = 0;
261      pkg_t *pkg;
262      pkg = pkg_new();
263      if (pkg == NULL)
264           return ENOMEM;
265
266      if (str_starts_with(url, "http://")
267          || str_starts_with(url, "ftp://")) {
268           char *tmp_file;
269           char *file_basec = xstrdup(url);
270           char *file_base = basename(file_basec);
271
272           sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
273           err = opkg_download(conf, url, tmp_file, NULL, NULL);
274           if (err)
275                return err;
276
277           err = pkg_init_from_file(pkg, tmp_file);
278           if (err)
279                return err;
280
281           free(tmp_file);
282           free(file_basec);
283
284      } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
285                 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
286                 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
287
288           err = pkg_init_from_file(pkg, url);
289           if (err)
290                return err;
291           opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
292           pkg->provided_by_hand = 1;
293
294      } else {
295        pkg_deinit(pkg);
296        free(pkg);
297        return 0;
298      }
299
300      if (!pkg->architecture) {
301           opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
302           return -EINVAL;
303      }
304
305      pkg->dest = conf->default_dest;
306      pkg->state_want = SW_INSTALL;
307      pkg->state_flag |= SF_PREFER;
308      pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);  
309      if ( pkg == NULL ){
310         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
311         return 0;
312      }
313      if (namep) {
314           *namep = xstrdup(pkg->name);
315      }
316      return 0;
317 }
318
319 int
320 opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
321 {
322 #if defined HAVE_GPGME
323     if (conf->check_signature == 0 )
324         return 0;
325     int status = -1;
326     gpgme_ctx_t ctx;
327     gpgme_data_t sig, text, key;
328     gpgme_error_t err = -1;
329     gpgme_verify_result_t result;
330     gpgme_signature_t s;
331     char *trusted_path = NULL;
332     
333     err = gpgme_new (&ctx);
334
335     if (err)
336         return -1;
337
338     sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg");
339     err = gpgme_data_new_from_file (&key, trusted_path, 1); 
340     free (trusted_path);
341     if (err)
342     {
343       return -1;
344     }
345     err = gpgme_op_import (ctx, key);
346     if (err)
347     {
348       gpgme_data_release (key);
349       return -1;
350     }
351     gpgme_data_release (key);
352
353     err = gpgme_data_new_from_file (&sig, sig_file, 1); 
354     if (err)
355     {
356         gpgme_release (ctx);
357         return -1;
358     }
359
360     err = gpgme_data_new_from_file (&text, text_file, 1); 
361     if (err)
362     {
363         gpgme_data_release (sig);
364         gpgme_release (ctx);
365         return -1;
366     }
367
368     err = gpgme_op_verify (ctx, sig, text, NULL);
369
370     result = gpgme_op_verify_result (ctx);
371     if (!result)
372         return -1;
373
374     /* see if any of the signitures matched */
375     s = result->signatures;
376     while (s)
377     {
378         status = gpg_err_code (s->status);
379         if (status == GPG_ERR_NO_ERROR)
380             break;
381         s = s->next;
382     }
383
384
385     gpgme_data_release (sig);
386     gpgme_data_release (text);
387     gpgme_release (ctx);
388
389     return status;
390 #elif defined HAVE_OPENSSL
391     X509_STORE *store = NULL;
392     PKCS7 *p7 = NULL;
393     BIO *in = NULL, *indata = NULL;
394
395     // Sig check failed by default !
396     int status = -1;
397
398     openssl_init();
399
400     // Set-up the key store
401     if(!(store = setup_verify(conf, conf->signature_ca_file, conf->signature_ca_path))){
402         opkg_message(conf, OPKG_ERROR,
403                 "Can't open CA certificates\n");
404         goto verify_file_end;
405     }
406
407     // Open a BIO to read the sig file
408     if (!(in = BIO_new_file(sig_file, "rb"))){
409         opkg_message(conf, OPKG_ERROR,
410                 "Can't open signature file %s\n", sig_file);
411         goto verify_file_end;
412     }
413
414     // Read the PKCS7 block contained in the sig file
415     p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
416     if(!p7){
417         opkg_message(conf, OPKG_ERROR,
418                 "Can't read signature file (Corrupted ?)\n");
419         goto verify_file_end;
420     }
421 #if defined(HAVE_PATHFINDER)
422     if(!pkcs7_pathfinder_verify_signers(p7)){
423         opkg_message(conf,  OPKG_ERROR, "pkcs7_pathfinder_verify_signers: "
424                 "Path verification failed\n");
425     }
426
427 #endif
428
429     // Open the Package file to authenticate
430     if (!(indata = BIO_new_file(text_file, "rb"))){
431         opkg_message(conf, OPKG_ERROR,
432                 "Can't open file %s\n", text_file);
433         goto verify_file_end;
434     }
435
436     // Let's verify the autenticity !
437     if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1){
438         // Get Off My Lawn!
439         opkg_message(conf, OPKG_ERROR,
440                 "Verification failure\n");
441     }else{
442         // Victory !
443         status = 0;
444     }
445
446 verify_file_end:
447     BIO_free(in);
448     BIO_free(indata);
449     PKCS7_free(p7);
450     X509_STORE_free(store);
451
452     return status;
453 #else
454     /* mute `unused variable' warnings. */
455     (void) sig_file;
456     (void) text_file;
457     (void) conf;
458     return 0;
459 #endif
460 }
461
462
463 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
464 static void openssl_init(void){
465     static int init = 0;
466
467     if(!init){
468         OPENSSL_config(NULL);
469         OpenSSL_add_all_algorithms();
470         ERR_load_crypto_strings();
471         init = 1;
472     }
473 }
474
475 #endif
476
477
478 #if defined HAVE_OPENSSL
479 static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath){
480     X509_STORE *store = NULL;
481     X509_LOOKUP *lookup = NULL;
482
483     if(!(store = X509_STORE_new())){
484         // Something bad is happening...
485         goto end;
486     }
487
488     // adds the X509 file lookup method
489     lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file());
490     if (lookup == NULL){
491         goto end;
492     }
493
494     // Autenticating against one CA file
495     if (CAfile) {
496         if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
497             // Invalid CA => Bye bye
498             opkg_message(conf, OPKG_ERROR,
499                     "Error loading file %s\n", CAfile);
500             goto end;
501         }
502     } else {
503         X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
504     }
505
506     // Now look into CApath directory if supplied
507     lookup = X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
508     if (lookup == NULL){
509         goto end;
510     }
511
512     if (CApath) {
513         if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
514             opkg_message(conf, OPKG_ERROR,
515                     "Error loading directory %s\n", CApath);
516             goto end;
517         }
518     } else {
519         X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
520     }
521
522     // All right !
523     ERR_clear_error();
524     return store;
525
526 end:
527
528     X509_STORE_free(store);
529     return NULL;
530
531 }
532
533 #endif
534
535 #ifdef HAVE_CURL
536 static void opkg_curl_cleanup(void){
537     if(curl != NULL){
538         curl_easy_cleanup (curl);
539         curl = NULL;
540     }
541 }
542
543 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data){
544
545     if(curl == NULL){
546         curl = curl_easy_init();
547
548 #ifdef HAVE_SSLCURL
549         openssl_init();
550
551         if (conf->ssl_engine) {
552
553             /* use crypto engine */
554             if (curl_easy_setopt(curl, CURLOPT_SSLENGINE, conf->ssl_engine) != CURLE_OK){
555                 opkg_message(conf, OPKG_ERROR, "can't set crypto engine: '%s'\n",
556                         conf->ssl_engine);
557
558                 opkg_curl_cleanup();
559                 return NULL;
560             }
561             /* set the crypto engine as default */
562             if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK){
563                 opkg_message(conf, OPKG_ERROR, "can't set crypto engine as default\n");
564
565                 opkg_curl_cleanup();
566                 return NULL;
567             }
568         }
569
570         /* cert & key can only be in PEM case in the same file */
571         if(conf->ssl_key_passwd){
572             if (curl_easy_setopt(curl, CURLOPT_SSLKEYPASSWD, conf->ssl_key_passwd) != CURLE_OK)
573             {
574                 opkg_message(conf, OPKG_DEBUG, "Failed to set key password\n");
575             }
576         }
577
578         /* sets the client certificate and its type */
579         if(conf->ssl_cert_type){
580             if (curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, conf->ssl_cert_type) != CURLE_OK)
581             {
582                 opkg_message(conf, OPKG_DEBUG, "Failed to set certificate format\n");
583             }
584         }
585         /* SSL cert name isn't mandatory */
586         if(conf->ssl_cert){
587                 curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert);
588         }
589
590         /* sets the client key and its type */
591         if(conf->ssl_key_type){
592             if (curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, conf->ssl_key_type) != CURLE_OK)
593             {
594                 opkg_message(conf, OPKG_DEBUG, "Failed to set key format\n");
595             }
596         }
597         if(conf->ssl_key){
598             if (curl_easy_setopt(curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK)
599             {
600                 opkg_message(conf, OPKG_DEBUG, "Failed to set key\n");
601             }
602         }
603
604         /* Should we verify the peer certificate ? */
605         if(conf->ssl_dont_verify_peer){
606             /*
607              * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
608              */
609             curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
610         }else{
611 #ifdef HAVE_PATHFINDER
612             if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_ssl_ctx_function) != CURLE_OK){
613                 opkg_message(conf, OPKG_DEBUG, "Failed to set ssl path verification callback\n");
614             }else{
615                 curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, NULL);
616             }
617
618             //curl_easy_setopt(curl, CURLOPT_SSL_CERT_VERIFY_FUNCTION, curlcb_pathfinder);
619 #endif
620         }
621
622         /* certification authority file and/or path */
623         if(conf->ssl_ca_file){
624             curl_easy_setopt(curl, CURLOPT_CAINFO, conf->ssl_ca_file);
625         }
626         if(conf->ssl_ca_path){
627             curl_easy_setopt(curl, CURLOPT_CAPATH, conf->ssl_ca_path);
628         }
629 #endif
630
631         curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
632         curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
633         if (conf->http_proxy || conf->ftp_proxy)
634         {
635             char *userpwd;
636             sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
637                     conf->proxy_passwd);
638             curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
639             free (userpwd);
640         }
641
642         /* add curl cleanup callback */
643         if(!atexit(opkg_curl_cleanup)){
644             opkg_message(conf,OPKG_DEBUG, "Failed to register atexit curl cleanup function\n");
645         }
646
647     }
648
649     curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));
650     if (cb)
651     {
652         curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data);
653         curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb);
654     }
655
656     return curl;
657
658 }
659 #endif