]> git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/rsalist.c
ipsec-34.0.1.tar.gz
[apple/ipsec.git] / ipsec-tools / racoon / rsalist.c
1 /* $Id: rsalist.c,v 1.3 2004/11/08 12:04:23 ludvigm Exp $ */
2
3 /*
4 * Copyright (C) 2004 SuSE Linux AG, Nuernberg, Germany.
5 * Contributed by: Michal Ludvig <mludvig@suse.cz>, SUSE Labs
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "config.h"
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
41 #include <netdb.h>
42
43 #include <openssl/bn.h>
44 #include <openssl/rsa.h>
45
46 #include "misc.h"
47 #include "plog.h"
48 #include "sockmisc.h"
49 #include "rsalist.h"
50 #include "genlist.h"
51 #include "remoteconf.h"
52 #include "crypto_openssl.h"
53
54 #ifndef LIST_FIRST
55 #define LIST_FIRST(head) ((head)->lh_first)
56 #endif
57
58 #ifndef LIST_NEXT
59 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
60 #endif
61
62 /* from prsa_tok.l */
63 int prsa_parse_file(struct genlist *list, const char *fname, enum rsa_key_type type);
64
65 int
66 rsa_key_insert(struct genlist *list, struct netaddr *src,
67 struct netaddr *dst, RSA *rsa)
68 {
69 struct rsa_key *rsa_key;
70
71 rsa_key = calloc(sizeof(struct rsa_key), 1);
72 rsa_key->rsa = rsa;
73
74 if (src)
75 rsa_key->src = src;
76 else
77 rsa_key->src = calloc(sizeof(*rsa_key->src), 1);
78
79 if (dst)
80 rsa_key->dst = dst;
81 else
82 rsa_key->dst = calloc(sizeof(*rsa_key->dst), 1);
83
84 genlist_append(list, rsa_key);
85
86 return 0;
87 }
88
89 static void *
90 rsa_key_dump_one(void *entry, void *arg)
91 {
92 struct rsa_key *key = entry;
93
94 plog(LLV_DEBUG, LOCATION, NULL, "Entry %s\n",
95 naddrwop2str_fromto("%s -> %s", key->src,
96 key->dst));
97 if (loglevel > LLV_DEBUG)
98 RSA_print_fp(stdout, key->rsa, 4);
99
100 return NULL;
101 }
102
103 void
104 rsa_key_dump(struct genlist *list)
105 {
106 genlist_foreach(list, rsa_key_dump_one, NULL);
107 }
108
109 static void *
110 rsa_list_count_one(void *entry, void *arg)
111 {
112 if (arg)
113 (*(unsigned long *)arg)++;
114 return NULL;
115 }
116
117 unsigned long
118 rsa_list_count(struct genlist *list)
119 {
120 unsigned long count = 0;
121 genlist_foreach(list, rsa_list_count_one, &count);
122 return count;
123 }
124
125 struct lookup_result {
126 struct ph1handle *iph1;
127 int max_score;
128 struct genlist *winners;
129 };
130
131 static void *
132 rsa_lookup_key_one(void *entry, void *data)
133 {
134 int local_score, remote_score;
135 struct lookup_result *req = data;
136 struct rsa_key *key = entry;
137
138 local_score = naddr_score(key->src, req->iph1->local);
139 remote_score = naddr_score(key->dst, req->iph1->remote);
140
141 plog(LLV_DEBUG, LOCATION, NULL, "Entry %s scored %d/%d\n",
142 naddrwop2str_fromto("%s -> %s", key->src, key->dst),
143 local_score, remote_score);
144
145 if (local_score >= 0 && remote_score >= 0) {
146 if (local_score + remote_score > req->max_score) {
147 req->max_score = local_score + remote_score;
148 // genlist_free(req->winners, NULL);
149 }
150
151 if (local_score + remote_score >= req->max_score) {
152 genlist_append(req->winners, key);
153 }
154 }
155
156 /* Always traverse the whole list */
157 return NULL;
158 }
159
160 struct genlist *
161 rsa_lookup_keys(struct ph1handle *iph1, int my)
162 {
163 struct genlist *list;
164 struct lookup_result r;
165
166 plog(LLV_DEBUG, LOCATION, NULL, "Looking up RSA key for %s\n",
167 saddr2str_fromto("%s <-> %s", iph1->local, iph1->remote));
168
169 r.iph1 = iph1;
170 r.max_score = -1;
171 r.winners = genlist_init();
172
173 if (my)
174 list = iph1->rmconf->rsa_private;
175 else
176 list = iph1->rmconf->rsa_public;
177
178 genlist_foreach(list, rsa_lookup_key_one, &r);
179
180 if (loglevel >= LLV_DEBUG)
181 rsa_key_dump(r.winners);
182
183 return r.winners;
184 }
185
186 int
187 rsa_parse_file(struct genlist *list, const char *fname, enum rsa_key_type type)
188 {
189 int ret;
190
191 plog(LLV_DEBUG, LOCATION, NULL, "Parsing %s\n", fname);
192 ret = prsa_parse_file(list, fname, type);
193 if (loglevel >= LLV_DEBUG)
194 rsa_key_dump(list);
195 return ret;
196 }
197
198 RSA *
199 rsa_try_check_rsasign(vchar_t *source, vchar_t *sig, struct genlist *list)
200 {
201 struct rsa_key *key;
202 struct genlist_entry *gp;
203
204 for(key = genlist_next(list, &gp); key; key = genlist_next(NULL, &gp)) {
205 plog(LLV_DEBUG, LOCATION, NULL, "Checking key %s...\n",
206 naddrwop2str_fromto("%s -> %s", key->src, key->dst));
207 if (eay_check_rsasign(source, sig, key->rsa) == 0) {
208 plog(LLV_DEBUG, LOCATION, NULL, " ... YEAH!\n");
209 return key->rsa;
210 }
211 plog(LLV_DEBUG, LOCATION, NULL, " ... nope.\n");
212 }
213 return NULL;
214 }