summaryrefslogtreecommitdiff
path: root/sbin/isakmpd/dnssec.c
blob: e6738a2bf6de44f63eb1a20f0f5c132d298c234a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*	$OpenBSD: dnssec.c,v 1.4 2001/06/27 03:31:40 angelos Exp $	*/

/*
 * Copyright (c) 2001 Håkan Olsson.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>
#include <stdlib.h>

#include <openssl/rsa.h>
#include <dns/keyvalues.h>
#include <lwres/lwres.h>
#include <lwres/netdb.h>

#include "sysdep.h"

#include "dnssec.h"
#include "exchange.h"
#include "ipsec_num.h"
#include "libcrypto.h"
#include "log.h"
#include "message.h"
#include "transport.h"

/* adapted from <dns/rdatastruct.h> / RFC 2535  */
struct dns_rdata_key {
  u_int16_t flags;
  u_int8_t protocol;
  u_int8_t algorithm;
  u_int16_t datalen;
  unsigned char *data;
};

/* XXX IPv4 specific */
void *
dns_get_key (int type, struct message *msg, int *keylen)
{
  struct rrsetinfo *rr;
  struct hostent *hostent;
  struct sockaddr_in *dst;
  int ret, i;
  struct dns_rdata_key key_rr;
  u_int8_t algorithm;

  switch (type)
    {
    case IKE_AUTH_RSA_SIG:
      algorithm = DNS_KEYALG_RSA;
      break;

    case IKE_AUTH_RSA_ENC:
    case IKE_AUTH_RSA_ENC_REV:
      /* XXX Not yet. */
      /* algorithm = DNS_KEYALG_RSA; */
      return 0;

    case IKE_AUTH_DSS:
      /* XXX Not yet. */
      /* algorithm = DNS_KEYALG_DSS; */
      return 0;

    case IKE_AUTH_PRE_SHARED: 
    default:
      return 0;
    }

  /* Get peer IP address */
  msg->transport->vtbl->get_dst (msg->transport, (struct sockaddr **)&dst, &i);
  /* Get peer name and aliases */
  hostent = lwres_gethostbyaddr ((char *)&dst->sin_addr, 
				 sizeof (struct in_addr), PF_INET);

  if (!hostent)
    {
      LOG_DBG ((LOG_MISC, 30, 
		"dns_get_key: lwres_gethostbyaddr (%s) failed: %s", 
		inet_ntoa (((struct sockaddr_in *)dst)->sin_addr),
		lwres_hstrerror (lwres_h_errno)));
      return 0;
    }

  /* Try host official name */
  LOG_DBG ((LOG_MISC, 50, "dns_get_key: trying KEY RR for %s", 
	    hostent->h_name));
  ret = lwres_getrrsetbyname (hostent->h_name, C_IN, T_KEY, 0, &rr);
  if (ret)
    {
      /* Try host aliases */
      i = 0;
      while (hostent->h_aliases[i] && ret)
	{
	  LOG_DBG ((LOG_MISC, 50, "dns_get_key: trying KEY RR for alias %s", 
		    hostent->h_aliases[i]));
	  ret = lwres_getrrsetbyname (hostent->h_aliases[i], C_IN, T_KEY, 0,
				      &rr);
	  i++;
	}
    }

  if (ret)
    {
      LOG_DBG ((LOG_MISC, 30, "dns_get_key: no DNS responses (error %d)", 
		ret));
      return 0;
    }
  
  LOG_DBG ((LOG_MISC, 80, 
	    "dns_get_key: rrset class %d type %d ttl %d nrdatas %d nrsigs %d",
	    rr->rri_rdclass, rr->rri_rdtype, rr->rri_ttl, rr->rri_nrdatas, 
	    rr->rri_nsigs));
  
  /* We don't accept unvalidated data. */
  if (!(rr->rri_flags & RRSET_VALIDATED))
    {
      LOG_DBG ((LOG_MISC, 10, "dns_get_key: got unvalidated response"));
      lwres_freerrset (rr);
      return 0;
    }
  
  /* Sanity. */
  if (rr->rri_nrdatas == 0 || rr->rri_rdtype != T_KEY)
    {
      LOG_DBG ((LOG_MISC, 30, "dns_get_key: no KEY RRs recieved"));
      lwres_freerrset (rr);
      return 0;
    } 

  memset (&key_rr, 0, sizeof key_rr);

  /* 
   * Find a key with the wanted algorithm, if any.
   * XXX If there are several keys present, we currently only find the first.
   */
  for (i = 0; i < rr->rri_nrdatas && key_rr.datalen == 0; i++)
    {
      key_rr.flags     = ntohs ((u_int16_t) *rr->rri_rdatas[i].rdi_data);
      key_rr.protocol  = *(rr->rri_rdatas[i].rdi_data + 2);
      key_rr.algorithm = *(rr->rri_rdatas[i].rdi_data + 3);

      if (key_rr.protocol != DNS_KEYPROTO_IPSEC)
	{
	  LOG_DBG ((LOG_MISC, 50, "dns_get_key: ignored non-IPsec key"));
	  continue;
	}

      if (key_rr.algorithm != algorithm)
	{
	  LOG_DBG ((LOG_MISC, 50, "dns_get_key: ignored key with other alg"));
	  continue;
	}

      key_rr.datalen = rr->rri_rdatas[i].rdi_length - 4;
      if (key_rr.datalen <= 0)
	{
	  LOG_DBG ((LOG_MISC, 50, "dns_get_key: ignored bad key"));
	  key_rr.datalen = 0;
	  continue;
	}

      /* This key seems to fit our requirements... */
      key_rr.data = (char *)malloc (key_rr.datalen);
      if (!key_rr.data)
	{
	  log_error ("dns_get_key: malloc (%d) failed", key_rr.datalen);
	  lwres_freerrset (rr);
	  return 0;
	}
      memcpy (key_rr.data, rr->rri_rdatas[i].rdi_data + 4, key_rr.datalen);
      *keylen = key_rr.datalen;
    }

  lwres_freerrset (rr);

  if (key_rr.datalen)
    return key_rr.data;
  else
    return 0;
}

int
dns_RSA_dns_to_x509 (u_int8_t *key, int keylen, RSA **rsa_key)
{
  RSA *rsa;
  int key_offset;
  u_int8_t e_len;

  if (!key || keylen <= 0)
    {
      log_print ("dns_RSA_dns_to_x509: invalid public key");
      return -1;
    }

  rsa = LC (RSA_new, ());
  if (rsa == NULL)
    {
      log_error ("dns_RSA_dns_to_x509: failed to allocate new RSA struct");
      return -1;
    }

  e_len = *key;
  key_offset = 1;

  if (e_len == 0)
    {
      if (keylen < 3)
	{
	  log_print ("dns_RSA_dns_to_x509: invalid public key");
	  LC (RSA_free, (rsa));
	  return -1;
	}
      e_len  = *(key + key_offset++) << 8;
      e_len += *(key + key_offset++);
    }

  if (e_len > (keylen - key_offset))
    {
      log_print ("dns_RSA_dns_to_x509: invalid public key");
      LC (RSA_free, (rsa));
      return -1;
    }

  rsa->e = LC (BN_bin2bn, (key + key_offset, e_len, NULL));
  key_offset += e_len;

  /* XXX if (keylen <= key_offset) -> "invalid public key" ? */

  rsa->n = LC (BN_bin2bn, (key + key_offset, keylen - key_offset, NULL));

  *rsa_key = rsa;

  LOG_DBG ((LOG_MISC, 30, "dns_RSA_dns_to_x509: got %d bits RSA key",
	    LC (BN_num_bits, (rsa->n))));

  return 0;
}

#if notyet
int
dns_RSA_x509_to_dns (RSA *rsa_key, u_int8_t *key, int *keylen)
{
  return 0;
}
#endif