]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/getcertsbyname.c
1 /* $KAME: getcertsbyname.c,v 1.7 2001/11/16 04:12:59 sakane Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/nameser_compat.h>
41 #ifdef HAVE_LWRES_GETRRSETBYNAME
42 #include <lwres/netdb.h>
43 #include <lwres/lwres.h>
57 #include "netdb_dnssec.h"
59 /* XXX should it use ci_errno to hold errno instead of h_errno ? */
62 static struct certinfo
*getnewci (int, int, int, int, int,
65 static struct certinfo
*
66 getnewci(qtype
, keytag
, algorithm
, flags
, certlen
, cert
)
67 int qtype
, keytag
, algorithm
, flags
, certlen
;
72 res
= malloc(sizeof(*res
));
76 memset(res
, 0, sizeof(*res
));
78 res
->ci_keytag
= keytag
;
79 res
->ci_algorithm
= algorithm
;
80 res
->ci_flags
= flags
;
81 res
->ci_certlen
= certlen
;
82 res
->ci_cert
= malloc(certlen
);
87 memcpy(res
->ci_cert
, cert
, certlen
);
96 struct certinfo
*next
;
108 * get CERT RR by FQDN and create certinfo structure chain.
110 #ifdef HAVE_LWRES_GETRRSETBYNAME
111 #define getrrsetbyname lwres_getrrsetbyname
112 #define freerrset lwres_freerrset
113 #define hstrerror lwres_hstrerror
115 #if defined(HAVE_LWRES_GETRRSETBYNAME) || defined(HAVE_GETRRSETBYNAME) //%%% BUG FIX - HAVE misspelled
117 getcertsbyname(name
, res
)
119 struct certinfo
**res
;
123 int type
, keytag
, algorithm
;
124 struct certinfo head
, *cur
;
125 struct rrsetinfo
*rr
= NULL
;
132 memset(&head
, 0, sizeof(head
));
135 error
= getrrsetbyname(name
, C_IN
, T_CERT
, 0, &rr
);
138 printf("getrrsetbyname: %s\n", hstrerror(error
));
140 h_errno
= NO_RECOVERY
;
144 if (rr
->rri_rdclass
!= C_IN
145 || rr
->rri_rdtype
!= T_CERT
146 || rr
->rri_nrdatas
== 0) {
148 printf("getrrsetbyname: %s", hstrerror(error
));
150 h_errno
= NO_RECOVERY
;
154 if (!(rr
->rri_flags
& LWRDATA_VALIDATED
))
155 printf("rr is not valid");
158 for (i
= 0; i
< rr
->rri_nrdatas
; i
++) {
159 rdlength
= rr
->rri_rdatas
[i
].rdi_length
;
160 cp
= rr
->rri_rdatas
[i
].rdi_data
;
162 GETSHORT(type
, cp
); /* type */
164 GETSHORT(keytag
, cp
); /* key tag */
166 algorithm
= *cp
++; /* algorithm */
170 printf("type=%d keytag=%d alg=%d len=%d\n",
171 type
, keytag
, algorithm
, rdlength
);
174 /* create new certinfo */
175 cur
->ci_next
= getnewci(type
, keytag
, algorithm
,
176 rr
->rri_flags
, rdlength
, cp
);
179 printf("getnewci: %s", strerror(errno
));
181 h_errno
= NO_RECOVERY
;
193 if (error
&& head
.ci_next
)
194 freecertinfo(head
.ci_next
);
198 #else /*!HAVE_LWRES_GETRRSETBYNAME*/
200 getcertsbyname(name
, res
)
202 struct certinfo
**res
;
204 unsigned char *answer
= NULL
, *p
;
205 int buflen
, anslen
, len
;
207 int qdcount
, ancount
, rdlength
;
208 unsigned char *cp
, *eom
;
209 char hostbuf
[1024]; /* XXX */
210 int qtype
, qclass
, keytag
, algorithm
;
211 struct certinfo head
, *cur
;
217 memset(&head
, 0, sizeof(head
));
225 p
= realloc(answer
, buflen
);
228 printf("realloc: %s", strerror(errno
));
230 h_errno
= NO_RECOVERY
;
235 anslen
= res_query(name
, C_IN
, T_CERT
, answer
, buflen
);
239 } while (buflen
< anslen
);
242 printf("get a DNS packet len=%d\n", anslen
);
246 eom
= answer
+ anslen
;
248 hp
= ALIGNED_CAST(HEADER
*)answer
;
249 qdcount
= ntohs(hp
->qdcount
);
250 ancount
= ntohs(hp
->ancount
);
252 /* question section */
255 printf("query count is not 1.\n");
257 h_errno
= NO_RECOVERY
;
260 cp
= (unsigned char *)(hp
+ 1);
261 len
= dn_expand(answer
, eom
, cp
, hostbuf
, sizeof(hostbuf
));
264 printf("dn_expand failed.\n");
269 GETSHORT(qtype
, cp
); /* QTYPE */
270 GETSHORT(qclass
, cp
); /* QCLASS */
273 while (ancount
-- && cp
< eom
) {
274 len
= dn_expand(answer
, eom
, cp
, hostbuf
, sizeof(hostbuf
));
277 printf("dn_expand failed.\n");
282 GETSHORT(qtype
, cp
); /* TYPE */
283 GETSHORT(qclass
, cp
); /* CLASS */
284 cp
+= INT32SZ
; /* TTL */
285 GETSHORT(rdlength
, cp
); /* RDLENGTH */
288 if (qtype
!= T_CERT
) {
290 printf("not T_CERT\n");
292 h_errno
= NO_RECOVERY
;
295 GETSHORT(qtype
, cp
); /* type */
297 GETSHORT(keytag
, cp
); /* key tag */
299 algorithm
= *cp
++; /* algorithm */
301 if (cp
+ rdlength
> eom
) {
303 printf("rdlength is too long.\n");
305 h_errno
= NO_RECOVERY
;
309 printf("type=%d keytag=%d alg=%d len=%d\n",
310 qtype
, keytag
, algorithm
, rdlength
);
313 /* create new certinfo */
314 cur
->ci_next
= getnewci(qtype
, keytag
, algorithm
,
318 printf("getnewci: %s", strerror(errno
));
320 h_errno
= NO_RECOVERY
;
334 if (error
&& head
.ci_next
)
335 freecertinfo(head
.ci_next
);
347 static const char b64t
[] =
348 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
349 "abcdefghijklmnopqrstuvwxyz"
353 printf("%c", b64t
[(p
[0] >> 2) & 0x3f]);
354 printf("%c", b64t
[((p
[0] << 4) & 0x30) | ((p
[1] >> 4) & 0x0f)]);
355 printf("%c", b64t
[((p
[1] << 2) & 0x3c) | ((p
[2] >> 6) & 0x03)]);
356 printf("%c", b64t
[p
[2] & 0x3f]);
362 printf("%c", b64t
[(p
[0] >> 2) & 0x3f]);
363 printf("%c", b64t
[((p
[0] << 4) & 0x30)| ((p
[1] >> 4) & 0x0f)]);
364 printf("%c", b64t
[((p
[1] << 2) & 0x3c)]);
366 } else if (len
== 1) {
367 printf("%c", b64t
[(p
[0] >> 2) & 0x3f]);
368 printf("%c", b64t
[((p
[0] << 4) & 0x30)]);
381 struct certinfo
*res
, *p
;
385 printf("Usage: a.out (FQDN)\n");
389 i
= getcertsbyname(*(av
+ 1), &res
);
391 herror("getcertsbyname");
394 printf("getcertsbyname succeeded.\n");
397 for (p
= res
; p
; p
= p
->ci_next
) {
398 printf("certinfo[%d]:\n", i
);
399 printf("\tci_type=%d\n", p
->ci_type
);
400 printf("\tci_keytag=%d\n", p
->ci_keytag
);
401 printf("\tci_algorithm=%d\n", p
->ci_algorithm
);
402 printf("\tci_flags=%d\n", p
->ci_flags
);
403 printf("\tci_certlen=%d\n", p
->ci_certlen
);
404 printf("\tci_cert: ");
405 b64encode(p
->ci_cert
, p
->ci_certlen
);