1 #include <openssl/opensslconf.h>
5 #include <openssl/bio.h>
6 #include <openssl/dso.h>
10 #ifndef OPENSSL_NO_LIBUNBOUND
13 static struct ub_ctx *ctx = NULL;
14 static DSO *unbound_dso = NULL;
17 void *p; struct ub_ctx *(*f)(); }
18 p_ub_ctx_create = {NULL};
21 void *p; int (*f)(struct ub_ctx *,const char *); }
22 p_ub_ctx_resolvconf = {NULL};
25 void *p; int (*f)(struct ub_ctx *,const char *); }
26 p_ub_ctx_add_ta_file = {NULL};
29 void *p; void (*f)(struct ub_ctx *); }
30 p_ub_ctx_delete = {NULL};
33 void *p; int (*f)(struct ub_ctx *,const char *,int,int,struct ub_result**); }
34 p_ub_resolve = {NULL};
37 void *p; void (*f)(struct ub_result*); }
38 p_ub_resolve_free = {NULL};
40 #if defined(__GNUC__) && __GNUC__>=2
41 static void unbound_init(void) __attribute__((constructor));
42 static void unbound_fini(void) __attribute__((destructor));
45 static void unbound_init(void)
49 if ((dso = DSO_load(NULL, "unbound", NULL, 0)) == NULL) return;
51 if ((p_ub_ctx_create.p = DSO_bind_func(dso,"ub_ctx_create")) == NULL ||
52 (p_ub_ctx_resolvconf.p = DSO_bind_func(dso,"ub_ctx_resolvconf")) == NULL ||
53 (p_ub_ctx_add_ta_file.p = DSO_bind_func(dso,"ub_ctx_add_ta_file")) == NULL ||
54 (p_ub_ctx_delete.p = DSO_bind_func(dso,"ub_ctx_delete")) == NULL ||
55 (p_ub_resolve.p = DSO_bind_func(dso,"ub_resolve")) == NULL ||
56 (p_ub_resolve_free.p = DSO_bind_func(dso,"ub_resolve_free")) == NULL ||
57 (ctx = p_ub_ctx_create.f()) == NULL) {
64 /* FIXME: parameterize these through CONF */
65 p_ub_ctx_resolvconf.f(ctx,"/etc/resolv.conf");
66 p_ub_ctx_add_ta_file.f(ctx,"/var/lib/unbound/root.key");
69 static void unbound_fini(void)
71 if (ctx != NULL) p_ub_ctx_delete.f(ctx);
72 if (unbound_dso != NULL) DSO_free(unbound_dso);
77 * Output is array packed as [len][data][len][data][0]
79 unsigned char *SSL_get_tlsa_record_byname (const char *name,int port,int type)
81 unsigned char *ret=NULL;
85 #ifndef OPENSSL_NO_LIBUNBOUND
86 if (ctx == NULL) return NULL;
87 #elif defined(RRSET_VALIDATED)
89 void *p; int (*f)(const char*,unsigned int,unsigned int,unsigned int,struct rrsetinfo **); }
90 p_getrrsetbyname = {NULL};
92 void *p; void (*f)(struct rrsetinfo *); }
95 if (p_getrrsetbyname.p==NULL) {
96 if ((p_getrrsetbyname.p = DSO_global_lookup("getrrsetbyname")) == NULL ||
97 (p_freerrset.p = DSO_global_lookup("freerrset")) == NULL)
98 p_getrrsetbyname.p = (void*)-1;
101 if (p_getrrsetbyname.p == (void *)-1) return NULL;
104 qlen = 7+5+strlen(name)+1;
105 if ((query = OPENSSL_malloc(qlen)) == NULL)
108 BIO_snprintf(query,qlen,"_%u._%s.%s",port&0xffff,type==SOCK_STREAM?"tcp":"udp",name);
110 #ifndef OPENSSL_NO_LIBUNBOUND
112 struct ub_result *tlsa=NULL;
114 if (p_ub_resolve.f(ctx,query,52,1,&tlsa)==0 &&
115 tlsa->havedata && tlsa->data[0]!=NULL) {
116 ret=(void*)-1; /* -1 means insecure */
117 if (tlsa->secure) do {
119 unsigned int dlen, i;
121 for (dlen=0, i=0; tlsa->data[i]; i++)
122 dlen += sizeof(int)+(unsigned int)tlsa->len[i];
125 if ((ret = OPENSSL_malloc(dlen)) == NULL) break;
127 for (data=ret, i=0; tlsa->data[i]; i++) {
128 *(unsigned int *)data = dlen = (unsigned int)tlsa->len[i];
129 data += sizeof(unsigned int);
130 memcpy(data,tlsa->data[i],dlen);
133 *(unsigned int *)data = 0; /* trailing zero */
135 p_ub_resolve_free.f(tlsa);
138 #elif defined(RRSET_VALIDATED)
140 struct rrsetinfo *rrset=NULL;
142 if (p_getrrsetbyname.f(query,1,52,RRSET_VALIDATED,&rrset) == 0 && rrset->rri_nrdatas) {
143 ret=(void*)-1; /* -1 means insecure */
144 if ((rrset->rri_flags&RRSET_VALIDATED)) do {
146 unsigned int dlen, i;
148 for (dlen=0, i=0; i<rrset->rri_nrdatas; i++)
149 dlen += sizeof(int)+rrset->rri_rdatas[i].rdi_length;
152 if ((ret = OPENSSL_malloc(sizeof(int)+dlen)) == NULL) break;
154 for (data=ret, i=0; i<rrset->rri_rdatas[i].rdi_length; i++) {
155 *(unsigned int *)data = dlen = rrset->rri_rdatas[i].rdi_length;
156 data += sizeof(unsigned int);
157 memcpy(data,rrset->rri_rdatas[i].rdi_data,dlen);
160 *(unsigned int *)data = 0; /* trailing zero */
162 p_freerrset.f(rrset);
165 #elif defined(_WIN32_NOT_YET)
169 DnsQuery_A(query,52,DNS_QUERY_STANDARD,NULL,&rrset,NULL);
170 DnsRecordListFree(rrset,DnsFreeRecordList);