Skip to content

Commit

Permalink
Avoid exception when DNSKEY record references unknown signature algor…
Browse files Browse the repository at this point in the history
…ithm
  • Loading branch information
cketti committed Mar 28, 2024
1 parent 15a8da3 commit 5bce77f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions minidns-core/src/main/java/org/minidns/record/DNSKEY.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private DNSKEY(short flags, byte protocol, SignatureAlgorithm algorithm, byte al
}

public DNSKEY(short flags, byte protocol, byte algorithm, byte[] key) {
this(flags, protocol, SignatureAlgorithm.forByte(algorithm), key);
this(flags, protocol, SignatureAlgorithm.forByte(algorithm), algorithm, key);
}

public DNSKEY(short flags, byte protocol, SignatureAlgorithm algorithm, byte[] key) {
Expand Down Expand Up @@ -140,7 +140,7 @@ public TYPE getType() {
public void serialize(DataOutputStream dos) throws IOException {
dos.writeShort(flags);
dos.writeByte(protocol);
dos.writeByte(algorithm.number);
dos.writeByte(algorithmByte);
dos.write(key);
}

Expand Down
13 changes: 13 additions & 0 deletions minidns-core/src/test/java/org/minidns/record/RecordsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
Expand Down Expand Up @@ -111,6 +112,18 @@ public void testDnskeyRecord() throws Exception {
assertArrayEquals(new byte[] {42}, dnskey.getKey());
}

@Test
public void testDnskeyRecordWithUnknownSignatureAlgorithm() throws Exception {
byte unknownSignatureAlgorithm = (byte) 255;
DNSKEY dnskey = new DNSKEY(DNSKEY.FLAG_ZONE, DNSKEY.PROTOCOL_RFC4034, unknownSignatureAlgorithm, new byte[]{42});
assertEquals(unknownSignatureAlgorithm, dnskey.algorithmByte);
assertNull(dnskey.algorithm);
byte[] dnskeyb = dnskey.toByteArray();
dnskey = DNSKEY.parse(new DataInputStream(new ByteArrayInputStream(dnskeyb)), dnskeyb.length);
assertEquals(unknownSignatureAlgorithm, dnskey.algorithmByte);
assertNull(dnskey.algorithm);
}

@Test
public void testDsRecord() throws Exception {
DS ds = new DS(42, (byte) 8, (byte) 2, new byte[] {0x13, 0x37});
Expand Down

0 comments on commit 5bce77f

Please sign in to comment.