Some refactoring of pathfinder support
[oweals/opkg-lede.git] / libopkg / opkg_pathfinder.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 #include <openssl/ssl.h>
22 #include <libpathfinder.h>
23
24 #if defined(HAVE_SSLCURL)
25 #include <curl/curl.h>
26 #endif
27
28 #include "includes.h"
29 #include "opkg_message.h"
30
31 #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
32 /*
33  *      This callback is called instead of X509_verify_cert to perform path
34  *      validation on a certificate using pathfinder.
35  *
36  */
37 static int pathfinder_verify_callback(X509_STORE_CTX *ctx, void *arg)
38 {
39     char *errmsg;
40     const char *hex = "0123456789ABCDEF";
41     size_t size = i2d_X509(ctx->cert, NULL);
42     unsigned char *keybuf, *iend;
43     iend = keybuf = malloc(size);
44     i2d_X509(ctx->cert, &iend);
45     char *certdata_str = malloc(size * 2 + 1);
46     unsigned char *cp = keybuf;
47     char *certdata_str_i = certdata_str;
48     while (cp < iend)
49     {
50         unsigned char ch = *cp++;
51         *certdata_str_i++ = hex[(ch >> 4) & 0xf];
52         *certdata_str_i++ = hex[ch & 0xf];
53     }
54     *certdata_str_i = 0;
55     free(keybuf);
56
57     const char *policy = "2.5.29.32.0"; // anyPolicy
58     int validated = pathfinder_dbus_verify(certdata_str, policy, 0, 0, &errmsg);
59
60     if (!validated)
61         fprintf(stderr, "curlcb_pathfinder: Path verification failed: %s", errmsg);
62
63     free(certdata_str);
64     free(errmsg);
65
66     return validated;
67 }
68 #endif
69
70 #if defined(HAVE_OPENSSL)
71 int pkcs7_pathfinder_verify_signers(PKCS7* p7)
72 {
73     STACK_OF(X509) *signers;
74     int i, ret = 1; /* signers are verified by default */
75
76     signers = PKCS7_get0_signers(p7, NULL, 0);
77
78     for(i = 0; i < sk_X509_num(signers); i++){
79         X509_STORE_CTX ctx = {
80             .cert = sk_X509_value(signers, i),
81         };
82
83         if(!pathfinder_verify_callback(&ctx, NULL)){
84             /* Signer isn't verified ! goto jail; */
85             ret = 0;
86             break;
87         }
88     }
89
90     sk_X509_free(signers);
91     return ret;
92 }
93 #endif
94
95 #if defined(HAVE_SSLCURL)
96 CURLcode curl_ssl_ctx_function(CURL * curl, void * sslctx, void * parm) {
97
98   SSL_CTX * ctx = (SSL_CTX *) sslctx;
99   SSL_CTX_set_cert_verify_callback(ctx, pathfinder_verify_callback, parm);
100
101   return CURLE_OK ;
102 }
103 #endif