and added files
[bcm963xx.git] / userapps / opensource / ipsec-tools / src / racoon / dnssec.c
1 /*      $KAME: dnssec.c,v 1.2 2001/08/05 18:46:07 itojun Exp $  */
2
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  * 
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include "config.h"
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "var.h"
40 #include "vmbuf.h"
41 #include "misc.h"
42 #include "plog.h"
43 #include "debug.h"
44
45 #include "isakmp_var.h"
46 #include "isakmp.h"
47 #include "ipsec_doi.h"
48 #include "oakley.h"
49 #include "netdb_dnssec.h"
50 #include "strnames.h"
51 #include "dnssec.h"
52 #include "gcmalloc.h"
53
54 extern int h_errno;
55
56 cert_t *
57 dnssec_getcert(id)
58         vchar_t *id;
59 {
60         cert_t *cert = NULL;
61         struct certinfo *res = NULL;
62         struct ipsecdoi_id_b *id_b;
63         int type;
64         char *name = NULL;
65         int namelen;
66         int error;
67
68         id_b = (struct ipsecdoi_id_b *)id->v;
69
70         namelen = id->l - sizeof(*id_b);
71         name = racoon_malloc(namelen + 1);
72         if (!name) {
73                 plog(LLV_ERROR, LOCATION, NULL,
74                         "failed to get buffer.\n");
75                 return NULL;
76         }
77         memcpy(name, id_b + 1, namelen);
78         name[namelen] = '\0';
79
80         switch (id_b->type) {
81         case IPSECDOI_ID_FQDN:
82                 error = getcertsbyname(name, &res);
83                 if (error != 0) {
84                         plog(LLV_ERROR, LOCATION, NULL,
85                                 "getcertsbyname(\"%s\") failed.\n", name);
86                         goto err;
87                 }
88                 break;
89         case IPSECDOI_ID_IPV4_ADDR:
90         case IPSECDOI_ID_IPV6_ADDR:
91                 /* XXX should be processed to query PTR ? */
92         default:
93                 plog(LLV_ERROR, LOCATION, NULL,
94                         "inpropper ID type passed %s "
95                         "though getcert method is dnssec.\n",
96                         s_ipsecdoi_ident(id_b->type));
97                 return NULL;
98         }
99
100         /* check response */
101         if (res->ci_next == NULL) {
102                 plog(LLV_WARNING, LOCATION, NULL,
103                         "not supported multiple CERT RR.\n");
104         }
105         switch (res->ci_type) {
106         case DNSSEC_TYPE_PKIX:
107                 /* XXX is it enough condition to set this type ? */
108                 type = ISAKMP_CERT_X509SIGN;
109                 break;
110         default:
111                 plog(LLV_ERROR, LOCATION, NULL,
112                         "not supported CERT RR type %d.\n", res->ci_type);
113                 goto err;
114         }
115
116         /* create cert holder */
117         cert = oakley_newcert();
118         if (cert == NULL) {
119                 plog(LLV_ERROR, LOCATION, NULL,
120                         "failed to get cert buffer.\n");
121                 goto err;
122         }
123         cert->pl = vmalloc(res->ci_certlen + 1);
124         if (cert->pl == NULL) {
125                 plog(LLV_ERROR, LOCATION, NULL,
126                         "failed to get cert buffer.\n");
127                 goto err;
128         }
129         memcpy(cert->pl->v + 1, res->ci_cert, res->ci_certlen);
130         cert->pl->v[0] = type;
131         cert->cert.v = cert->pl->v + 1;
132         cert->cert.l = cert->pl->l - 1;
133
134         plog(LLV_DEBUG, LOCATION, NULL, "created CERT payload:\n");
135         plogdump(LLV_DEBUG, cert->pl->v, cert->pl->l);
136
137 end:
138         if (res)
139                 freecertinfo(res);
140
141         return cert;
142
143 err:
144         if (name)
145                 racoon_free(name);
146         if (cert)
147                 oakley_delcert(cert);
148         goto end;
149 }