3 /* $KAME: getcertsbyname.c,v 1.7 2001/11/16 04:12:59 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/nameser.h>
42 #if (defined(__APPLE__) && defined(__MACH__))
43 # include <nameser8_compat.h>
46 #ifdef HAVE_LWRES_GETRRSETBYNAME
47 #include <lwres/netdb.h>
48 #include <lwres/lwres.h>
61 #include "netdb_dnssec.h"
63 /* XXX should it use ci_errno to hold errno instead of h_errno ? */
66 static struct certinfo
*getnewci
__P((int, int, int, int, int,
69 static struct certinfo
*
70 getnewci(qtype
, keytag
, algorithm
, flags
, certlen
, cert
)
71 int qtype
, keytag
, algorithm
, flags
, certlen
;
76 res
= malloc(sizeof(*res
));
80 memset(res
, 0, sizeof(*res
));
82 res
->ci_keytag
= keytag
;
83 res
->ci_algorithm
= algorithm
;
84 res
->ci_flags
= flags
;
85 res
->ci_certlen
= certlen
;
86 res
->ci_cert
= malloc(certlen
);
91 memcpy(res
->ci_cert
, cert
, certlen
);
100 struct certinfo
*next
;
112 * get CERT RR by FQDN and create certinfo structure chain.
114 #ifdef HAVE_LWRES_GETRRSETBYNAME
115 #define getrrsetbyname lwres_getrrsetbyname
116 #define freerrset lwres_freerrset
117 #define hstrerror lwres_hstrerror
119 #if defined(HAVE_LWRES_GETRRSETBYNAME) || defined(AHVE_GETRRSETBYNAME)
121 getcertsbyname(name
, res
)
123 struct certinfo
**res
;
127 int type
, keytag
, algorithm
;
128 struct certinfo head
, *cur
;
129 struct rrsetinfo
*rr
= NULL
;
136 memset(&head
, 0, sizeof(head
));
139 error
= getrrsetbyname(name
, C_IN
, T_CERT
, 0, &rr
);
142 printf("getrrsetbyname: %s\n", hstrerror(error
));
144 h_errno
= NO_RECOVERY
;
148 if (rr
->rri_rdclass
!= C_IN
149 || rr
->rri_rdtype
!= T_CERT
150 || rr
->rri_nrdatas
== 0) {
152 printf("getrrsetbyname: %s", hstrerror(error
));
154 h_errno
= NO_RECOVERY
;
158 if (!(rr
->rri_flags
& LWRDATA_VALIDATED
))
159 printf("rr is not valid");
162 for (i
= 0; i
< rr
->rri_nrdatas
; i
++) {
163 rdlength
= rr
->rri_rdatas
[i
].rdi_length
;
164 cp
= rr
->rri_rdatas
[i
].rdi_data
;
166 GETSHORT(type
, cp
); /* type */
168 GETSHORT(keytag
, cp
); /* key tag */
170 algorithm
= *cp
++; /* algorithm */
174 printf("type=%d keytag=%d alg=%d len=%d\n",
175 type
, keytag
, algorithm
, rdlength
);
178 /* create new certinfo */
179 cur
->ci_next
= getnewci(type
, keytag
, algorithm
,
180 rr
->rri_flags
, rdlength
, cp
);
183 printf("getnewci: %s", strerror(errno
));
185 h_errno
= NO_RECOVERY
;
197 if (error
&& head
.ci_next
)
198 freecertinfo(head
.ci_next
);
202 #else /*!HAVE_LWRES_GETRRSETBYNAME*/
204 getcertsbyname(name
, res
)
206 struct certinfo
**res
;
208 unsigned char *answer
= NULL
, *p
;
209 int buflen
, anslen
, len
;
211 int qdcount
, ancount
, rdlength
;
212 unsigned char *cp
, *eom
;
213 char hostbuf
[1024]; /* XXX */
214 int qtype
, qclass
, keytag
, algorithm
;
215 struct certinfo head
, *cur
;
221 memset(&head
, 0, sizeof(head
));
229 p
= realloc(answer
, buflen
);
232 printf("realloc: %s", strerror(errno
));
234 h_errno
= NO_RECOVERY
;
239 anslen
= res_query(name
, C_IN
, T_CERT
, answer
, buflen
);
243 } while (buflen
< anslen
);
246 printf("get a DNS packet len=%d\n", anslen
);
250 eom
= answer
+ anslen
;
252 hp
= (HEADER
*)answer
;
253 qdcount
= ntohs(hp
->qdcount
);
254 ancount
= ntohs(hp
->ancount
);
256 /* question section */
259 printf("query count is not 1.\n");
261 h_errno
= NO_RECOVERY
;
264 cp
= (unsigned char *)(hp
+ 1);
265 len
= dn_expand(answer
, eom
, cp
, hostbuf
, sizeof(hostbuf
));
268 printf("dn_expand failed.\n");
273 GETSHORT(qtype
, cp
); /* QTYPE */
274 GETSHORT(qclass
, cp
); /* QCLASS */
277 while (ancount
-- && cp
< eom
) {
278 len
= dn_expand(answer
, eom
, cp
, hostbuf
, sizeof(hostbuf
));
281 printf("dn_expand failed.\n");
286 GETSHORT(qtype
, cp
); /* TYPE */
287 GETSHORT(qclass
, cp
); /* CLASS */
288 cp
+= INT32SZ
; /* TTL */
289 GETSHORT(rdlength
, cp
); /* RDLENGTH */
292 if (qtype
!= T_CERT
) {
294 printf("not T_CERT\n");
296 h_errno
= NO_RECOVERY
;
299 GETSHORT(qtype
, cp
); /* type */
301 GETSHORT(keytag
, cp
); /* key tag */
303 algorithm
= *cp
++; /* algorithm */
305 if (cp
+ rdlength
> eom
) {
307 printf("rdlength is too long.\n");
309 h_errno
= NO_RECOVERY
;
313 printf("type=%d keytag=%d alg=%d len=%d\n",
314 qtype
, keytag
, algorithm
, rdlength
);
317 /* create new certinfo */
318 cur
->ci_next
= getnewci(qtype
, keytag
, algorithm
,
322 printf("getnewci: %s", strerror(errno
));
324 h_errno
= NO_RECOVERY
;
338 if (error
&& head
.ci_next
)
339 freecertinfo(head
.ci_next
);
351 static const char b64t
[] =
352 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
353 "abcdefghijklmnopqrstuvwxyz"
357 printf("%c", b64t
[(p
[0] >> 2) & 0x3f]);
358 printf("%c", b64t
[((p
[0] << 4) & 0x30) | ((p
[1] >> 4) & 0x0f)]);
359 printf("%c", b64t
[((p
[1] << 2) & 0x3c) | ((p
[2] >> 6) & 0x03)]);
360 printf("%c", b64t
[p
[2] & 0x3f]);
366 printf("%c", b64t
[(p
[0] >> 2) & 0x3f]);
367 printf("%c", b64t
[((p
[0] << 4) & 0x30)| ((p
[1] >> 4) & 0x0f)]);
368 printf("%c", b64t
[((p
[1] << 2) & 0x3c)]);
370 } else if (len
== 1) {
371 printf("%c", b64t
[(p
[0] >> 2) & 0x3f]);
372 printf("%c", b64t
[((p
[0] << 4) & 0x30)]);
385 struct certinfo
*res
, *p
;
389 printf("Usage: a.out (FQDN)\n");
393 i
= getcertsbyname(*(av
+ 1), &res
);
395 herror("getcertsbyname");
398 printf("getcertsbyname succeeded.\n");
401 for (p
= res
; p
; p
= p
->ci_next
) {
402 printf("certinfo[%d]:\n", i
);
403 printf("\tci_type=%d\n", p
->ci_type
);
404 printf("\tci_keytag=%d\n", p
->ci_keytag
);
405 printf("\tci_algorithm=%d\n", p
->ci_algorithm
);
406 printf("\tci_flags=%d\n", p
->ci_flags
);
407 printf("\tci_certlen=%d\n", p
->ci_certlen
);
408 printf("\tci_cert: ");
409 b64encode(p
->ci_cert
, p
->ci_certlen
);