Provide sources for the EC math and use a faster double algorithm
[dnssec-prover] / wasmpack / doh_lookup.js
1 import init from './dnssec_prover_wasm.js';
2 import * as wasm from './dnssec_prover_wasm.js';
3
4 /**
5 * Asynchronously resolves a given domain and type using the provided DoH endpoint, then verifies
6 * the returned DNSSEC data and ultimately returns a JSON-encoded list of validated records.
7 */
8 export async function lookup_doh(domain, ty, doh_endpoint) {
9         await init();
10
11         if (!domain.endsWith(".")) domain += ".";
12         if (ty.toLowerCase() == "txt") {
13                 ty = 16;
14         } else if (ty.toLowerCase() == "tlsa") {
15                 ty = 52;
16         } else if (ty.toLowerCase() == "a") {
17                 ty = 1;
18         } else if (ty.toLowerCase() == "aaaa") {
19                 ty = 28;
20         }
21         if (typeof(ty) == "number") {
22                 var builder = wasm.init_proof_builder(domain, ty);
23                 if (builder == null) {
24                         return "{\"error\":\"Bad domain\"}";
25                 } else {
26                         var queries_pending = 0;
27                         var send_next_query;
28                         send_next_query = async function() {
29                                 var query = wasm.get_next_query(builder);
30                                 if (query != null) {
31                                         queries_pending += 1;
32                                         var b64 = btoa(String.fromCodePoint(...query));
33                                         var b64url = b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
34                                         try {
35                                                 var resp = await fetch(doh_endpoint + "?dns=" + b64url,
36                                                         {headers: {"accept": "application/dns-message"}});
37                                                 if (!resp.ok) { throw "Query returned HTTP " + resp.status; }
38                                                 var array = await resp.arrayBuffer();
39                                                 var buf = new Uint8Array(array);
40                                                 wasm.process_query_response(builder, buf);
41                                                 queries_pending -= 1;
42                                         } catch (e) {
43                                                 return "{\"error\":\"DoH Query failed: " + e + "\"}";
44                                         }
45                                         return await send_next_query();
46                                 } else if (queries_pending == 0) {
47                                         var proof = wasm.get_unverified_proof(builder);
48                                         if (proof != null) {
49                                                 var result = wasm.verify_byte_stream(proof, domain);
50                                                 return JSON.stringify(JSON.parse(result), null, 1);
51                                         } else {
52                                                 return "{\"error\":\"Failed to build proof\"}";
53                                         }
54                                 }
55                         }
56                         return await send_next_query();
57                 }
58         } else {
59                 return "{\"error\":\"Unsupported Type\"}";
60         }
61 }