98a6dd4ba2a4bd085d9102d600716ff0257b7cf4
[oweals/opkg-lede.git] / libopkg / opkg_pathfinder.c
1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_pathfinder.c - the opkg package management system
3
4    Copyright (C) 2009 Camille Moncelier <moncelier@devlife.org>
5
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License as
8    published by the Free Software Foundation; either version 2, or (at
9    your option) any later version.
10
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15 */
16 #include "config.h"
17
18 #include <openssl/ssl.h>
19 #include <libpathfinder.h>
20 #include <stdlib.h>
21 #if defined(HAVE_SSLCURL)
22 #include <curl/curl.h>
23 #endif
24
25 #include "libbb/libbb.h"
26 #include "opkg_message.h"
27
28 #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
29 /*
30  *      This callback is called instead of X509_verify_cert to perform path
31  *      validation on a certificate using pathfinder.
32  *
33  */
34 static int pathfinder_verify_callback(X509_STORE_CTX * ctx, void *arg)
35 {
36         char *errmsg;
37         const char *hex = "0123456789ABCDEF";
38         size_t size = i2d_X509(ctx->cert, NULL);
39         unsigned char *keybuf, *iend;
40         iend = keybuf = xmalloc(size);
41         i2d_X509(ctx->cert, &iend);
42         char *certdata_str = xmalloc(size * 2 + 1);
43         unsigned char *cp = keybuf;
44         char *certdata_str_i = certdata_str;
45         while (cp < iend) {
46                 unsigned char ch = *cp++;
47                 *certdata_str_i++ = hex[(ch >> 4) & 0xf];
48                 *certdata_str_i++ = hex[ch & 0xf];
49         }
50         *certdata_str_i = 0;
51         free(keybuf);
52
53         const char *policy = "2.5.29.32.0";     // anyPolicy
54         int validated =
55             pathfinder_dbus_verify(certdata_str, policy, 0, 0, &errmsg);
56
57         if (!validated)
58                 opkg_msg(ERROR, "Path verification failed: %s.\n", errmsg);
59
60         free(certdata_str);
61         free(errmsg);
62
63         return validated;
64 }
65 #endif
66
67 #if defined(HAVE_OPENSSL)
68 int pkcs7_pathfinder_verify_signers(PKCS7 * p7)
69 {
70         STACK_OF(X509) * signers;
71         int i, ret = 1;         /* signers are verified by default */
72
73         signers = PKCS7_get0_signers(p7, NULL, 0);
74
75         for (i = 0; i < sk_X509_num(signers); i++) {
76                 X509_STORE_CTX ctx = {
77                         .cert = sk_X509_value(signers, i),
78                 };
79
80                 if (!pathfinder_verify_callback(&ctx, NULL)) {
81                         /* Signer isn't verified ! goto jail; */
82                         ret = 0;
83                         break;
84                 }
85         }
86
87         sk_X509_free(signers);
88         return ret;
89 }
90 #endif
91
92 #if defined(HAVE_SSLCURL)
93 CURLcode curl_ssl_ctx_function(CURL * curl, void *sslctx, void *parm)
94 {
95
96         SSL_CTX *ctx = (SSL_CTX *) sslctx;
97         SSL_CTX_set_cert_verify_callback(ctx, pathfinder_verify_callback, parm);
98
99         return CURLE_OK;
100 }
101 #endif