Fix missing/incorrect prototype.
[openssl.git] / ssl / dnssec.c
1 #include <openssl/opensslconf.h>
2
3 #include <string.h>
4 #include <netdb.h>
5 #include <openssl/bio.h>
6 #include <openssl/dso.h>
7
8 #include "ssl.h"
9
10 #ifndef OPENSSL_NO_LIBUNBOUND
11 #include <unbound.h>
12
13 static struct ub_ctx *ctx = NULL;
14 static DSO *unbound_dso = NULL;
15
16 static union {
17         void *p; struct ub_ctx *(*f)(); }
18         p_ub_ctx_create = {NULL};
19
20 static union {
21         void *p; int (*f)(struct ub_ctx *,const char *); }
22         p_ub_ctx_resolvconf = {NULL};
23
24 static union {
25         void *p; int (*f)(struct ub_ctx *,const char *); }
26         p_ub_ctx_add_ta_file = {NULL};
27
28 static union {
29         void *p; void (*f)(struct ub_ctx *); }
30         p_ub_ctx_delete = {NULL};
31
32 static union {
33         void *p; int (*f)(struct ub_ctx *,const char *,int,int,struct ub_result**); }
34         p_ub_resolve = {NULL};
35
36 static union {
37         void *p; void (*f)(struct ub_result*); }
38         p_ub_resolve_free = {NULL};
39
40 #if defined(__GNUC__) && __GNUC__>=2
41  static void unbound_init(void) __attribute__((constructor));
42  static void unbound_fini(void) __attribute__((destructor));
43 #endif 
44
45 static void unbound_init(void)
46 {
47         DSO *dso;
48
49         if ((dso = DSO_load(NULL, "unbound", NULL, 0)) == NULL) return;
50
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) {
58                 DSO_free(dso);
59                 return;
60         }
61
62         unbound_dso = dso;
63
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");
67 }
68
69 static void unbound_fini(void)
70 {
71         if (ctx != NULL) p_ub_ctx_delete.f(ctx);
72         if (unbound_dso != NULL) DSO_free(unbound_dso);
73 }
74 #endif
75
76 /*
77  * Output is array packed as [len][data][len][data][0]
78  */
79 unsigned char *SSL_get_tlsa_record_byname (const char *name,int port,int type)
80 {
81         unsigned char *ret=NULL;
82         char *query=NULL;
83         size_t qlen;
84
85 #ifndef OPENSSL_NO_LIBUNBOUND
86         if (ctx == NULL) return NULL;
87 #elif defined(RRSET_VALIDATED)
88         static union {
89                 void *p; int (*f)(const char*,unsigned int,unsigned int,unsigned int,struct rrsetinfo **); }
90                 p_getrrsetbyname = {NULL};
91         static union {
92                 void *p; void (*f)(struct rrsetinfo *); }
93                 p_freerrset = {NULL};
94
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;
99         }
100
101         if (p_getrrsetbyname.p == (void *)-1) return NULL;
102 #endif
103
104         qlen = 7+5+strlen(name)+1;
105         if ((query = OPENSSL_malloc(qlen)) == NULL)
106                 return NULL;
107
108         BIO_snprintf(query,qlen,"_%u._%s.%s",port&0xffff,type==SOCK_STREAM?"tcp":"udp",name);
109
110 #ifndef OPENSSL_NO_LIBUNBOUND
111         {
112         struct ub_result *tlsa=NULL;
113
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 {
118                         unsigned char *data;
119                         unsigned int dlen, i;
120
121                         for (dlen=0, i=0; tlsa->data[i]; i++)
122                                 dlen += sizeof(int)+(unsigned int)tlsa->len[i];
123                         dlen +=sizeof(int);
124
125                         if ((ret = OPENSSL_malloc(dlen)) == NULL) break;
126                         
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);
131                                 data += dlen;
132                         }
133                         *(unsigned int *)data = 0;      /* trailing zero */
134                 } while (0);    
135                 p_ub_resolve_free.f(tlsa);
136         }
137         }
138 #elif defined(RRSET_VALIDATED)
139         {
140         struct rrsetinfo *rrset=NULL;
141
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 {
145                         unsigned char *data;
146                         unsigned int dlen, i;
147
148                         for (dlen=0, i=0; i<rrset->rri_nrdatas; i++)
149                                 dlen += sizeof(int)+rrset->rri_rdatas[i].rdi_length;
150                         dlen +=sizeof(int);
151
152                         if ((ret = OPENSSL_malloc(sizeof(int)+dlen)) == NULL) break;
153
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);
158                                 data += dlen;
159                         }
160                         *(unsigned int *)data = 0;      /* trailing zero */
161                 } while (0);    
162                 p_freerrset.f(rrset);
163         }
164         }
165 #elif defined(_WIN32_NOT_YET)
166         {
167         PDNS_RECORD rrset;
168
169         DnsQuery_A(query,52,DNS_QUERY_STANDARD,NULL,&rrset,NULL);
170         DnsRecordListFree(rrset,DnsFreeRecordList);
171         }
172 #endif
173         CRYPTO_free(query);
174
175         return ret;
176 }