use of org.minidns.record.Data in project minidns by MiniDNS.
the class Verifier method combine.
static byte[] combine(RRSIG rrsig, List<Record<? extends Data>> records) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
// Write RRSIG without signature
try {
rrsig.writePartialSignature(dos);
DNSName sigName = records.get(0).name;
if (!sigName.isRootLabel()) {
if (sigName.getLabelCount() < rrsig.labels) {
throw new DNSSECValidationFailedException("Invalid RRsig record");
}
if (sigName.getLabelCount() > rrsig.labels) {
// Expand wildcards
sigName = DNSName.from("*." + sigName.stripToLabels(rrsig.labels));
}
}
List<byte[]> recordBytes = new ArrayList<>();
for (Record<? extends Data> record : records) {
Record<Data> ref = new Record<>(sigName, record.type, record.clazzValue, rrsig.originalTtl, (Data) record.payloadData);
recordBytes.add(ref.toByteArray());
}
// Sort correctly (cause they might be ordered randomly)
// Where the RDATA begins
final int offset = sigName.size() + 10;
Collections.sort(recordBytes, new Comparator<byte[]>() {
@Override
public int compare(byte[] b1, byte[] b2) {
for (int i = offset; i < b1.length && i < b2.length; i++) {
if (b1[i] != b2[i]) {
return (b1[i] & 0xFF) - (b2[i] & 0xFF);
}
}
return b1.length - b2.length;
}
});
for (byte[] recordByte : recordBytes) {
dos.write(recordByte);
}
dos.flush();
} catch (IOException e) {
// Never happens
throw new RuntimeException(e);
}
return bos.toByteArray();
}
use of org.minidns.record.Data in project minidns by MiniDNS.
the class DNSMessage method getAnswersFor.
public <D extends Data> Set<D> getAnswersFor(Question q) {
if (responseCode != RESPONSE_CODE.NO_ERROR)
return null;
// It would be great if we could verify that D matches q.type at this
// point. But on the other hand, if it does not, then the cast to D
// below will fail.
Set<D> res = new HashSet<>(answerSection.size());
for (Record<? extends Data> record : answerSection) {
if (!record.isAnswer(q))
continue;
Data data = record.getPayload();
@SuppressWarnings("unchecked") D d = (D) data;
boolean isNew = res.add(d);
if (!isNew) {
LOGGER.log(Level.WARNING, "DNSMessage contains duplicate answers. Record: " + record + "; DNSMessage: " + this);
}
}
return res;
}
use of org.minidns.record.Data in project minidns by MiniDNS.
the class DNSMessageTest method testComDsAndRrsigLookup.
@Test
public void testComDsAndRrsigLookup() throws Exception {
DNSMessage m = getMessageFromResource("com-ds-rrsig");
assertFalse(m.authoritativeAnswer);
assertTrue(m.recursionDesired);
assertTrue(m.recursionAvailable);
List<Record<? extends Data>> answers = m.answerSection;
assertEquals(2, answers.size());
assertEquals(TYPE.DS, answers.get(0).type);
assertEquals(TYPE.DS, answers.get(0).payloadData.getType());
DS ds = (DS) answers.get(0).payloadData;
assertEquals(30909, ds.keyTag);
assertEquals(SignatureAlgorithm.RSASHA256, ds.algorithm);
assertEquals(DigestAlgorithm.SHA256, ds.digestType);
assertCsEquals("E2D3C916F6DEEAC73294E8268FB5885044A833FC5459588F4A9184CFC41A5766", ds.getDigestHex());
assertEquals(TYPE.RRSIG, answers.get(1).type);
assertEquals(TYPE.RRSIG, answers.get(1).payloadData.getType());
RRSIG rrsig = (RRSIG) answers.get(1).payloadData;
assertEquals(TYPE.DS, rrsig.typeCovered);
assertEquals(SignatureAlgorithm.RSASHA256, rrsig.algorithm);
assertEquals(1, rrsig.labels);
assertEquals(86400, rrsig.originalTtl);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
assertCsEquals("20150629170000", dateFormat.format(rrsig.signatureExpiration));
assertCsEquals("20150619160000", dateFormat.format(rrsig.signatureInception));
assertEquals(48613, rrsig.keyTag);
assertCsEquals(".", rrsig.signerName);
assertEquals(128, rrsig.signature.length);
List<Record<? extends Data>> arr = m.additionalSection;
assertEquals(1, arr.size());
assertEquals(TYPE.OPT, arr.get(0).getPayload().getType());
Record<? extends Data> opt = arr.get(0);
EDNS edns = EDNS.fromRecord(opt);
assertEquals(512, edns.udpPayloadSize);
assertEquals(0, edns.version);
assertTrue(edns.dnssecOk);
}
use of org.minidns.record.Data in project minidns by MiniDNS.
the class DNSMessageTest method testAAAALookup.
@Test
public void testAAAALookup() throws Exception {
DNSMessage m = getMessageFromResource("google-aaaa");
assertFalse(m.authoritativeAnswer);
List<Record<? extends Data>> answers = m.answerSection;
assertEquals(1, answers.size());
Record<? extends Data> answer = answers.get(0);
assertCsEquals("google.com", answer.name);
assertTrue(answer.getPayload() instanceof AAAA);
assertEquals(TYPE.AAAA, answer.getPayload().getType());
assertCsEquals("2a00:1450:400c:c02:0:0:0:8a", answer.getPayload().toString());
}
use of org.minidns.record.Data in project minidns by MiniDNS.
the class DNSMessageTest method testComNsLookup.
@Test
public void testComNsLookup() throws Exception {
DNSMessage m = getMessageFromResource("com-ns");
assertFalse(m.authoritativeAnswer);
assertFalse(m.authenticData);
assertTrue(m.recursionDesired);
assertTrue(m.recursionAvailable);
assertTrue(m.qr);
List<Record<? extends Data>> answers = m.answerSection;
assertEquals(13, answers.size());
for (Record<? extends Data> answer : answers) {
assertCsEquals("com", answer.name);
assertEquals(Record.CLASS.IN, answer.clazz);
assertEquals(TYPE.NS, answer.type);
assertEquals(112028, answer.ttl);
assertTrue(((NS) answer.payloadData).target.ace.endsWith(".gtld-servers.net"));
}
List<Record<? extends Data>> arr = m.additionalSection;
assertEquals(1, arr.size());
EDNS edns = EDNS.fromRecord(arr.get(0));
assertEquals(4096, edns.udpPayloadSize);
assertEquals(0, edns.version);
}
Aggregations