Skip to content

Commit 8d79cb3

Browse files
committed
Add options to sign and verify DNSSEC messages at a given time
1 parent 6536727 commit 8d79cb3

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/main/java/org/xbill/DNS/DNSSEC.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ static SIGRecord signMessage(
11781178
}
11791179

11801180
static void verifyMessage(
1181-
Message message, byte[] bytes, SIGRecord sig, SIGRecord previous, KEYRecord key)
1181+
Message message, byte[] bytes, SIGRecord sig, SIGRecord previous, KEYRecord key, Instant now)
11821182
throws DNSSECException {
11831183
if (message.sig0start == 0) {
11841184
throw new NoSignatureException();
@@ -1188,8 +1188,6 @@ static void verifyMessage(
11881188
throw new KeyMismatchException(key, sig);
11891189
}
11901190

1191-
Instant now = Instant.now();
1192-
11931191
if (now.compareTo(sig.getExpire()) > 0) {
11941192
throw new SignatureExpiredException(sig.getExpire(), now);
11951193
}

src/main/java/org/xbill/DNS/SIG0.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.security.PrivateKey;
66
import java.time.Duration;
77
import java.time.Instant;
8+
import org.xbill.DNS.DNSSEC.DNSSECException;
89

910
/**
1011
* Creates SIG(0) transaction signatures.
@@ -34,6 +35,22 @@ private SIG0() {}
3435
public static void signMessage(
3536
Message message, KEYRecord key, PrivateKey privkey, SIGRecord previous)
3637
throws DNSSEC.DNSSECException {
38+
signMessage(message, key, privkey, previous, Instant.now());
39+
}
40+
41+
/**
42+
* Sign a message with SIG(0). The DNS key and private key must refer to the same underlying
43+
* cryptographic key.
44+
*
45+
* @param message The message to be signed
46+
* @param key The DNSKEY record to use as part of signing
47+
* @param privkey The PrivateKey to use when signing
48+
* @param previous If this message is a response, the SIG(0) from the query
49+
* @param timeSigned The time instant when the message has been signed.
50+
*/
51+
public static void signMessage(
52+
Message message, KEYRecord key, PrivateKey privkey, SIGRecord previous, Instant timeSigned)
53+
throws DNSSEC.DNSSECException {
3754

3855
int validityOption = Options.intValue("sig0validity");
3956
Duration validity;
@@ -43,7 +60,6 @@ public static void signMessage(
4360
validity = Duration.ofSeconds(validityOption);
4461
}
4562

46-
Instant timeSigned = Instant.now();
4763
Instant timeExpires = timeSigned.plus(validity);
4864

4965
SIGRecord sig = DNSSEC.signMessage(message, previous, key, privkey, timeSigned, timeExpires);
@@ -52,7 +68,7 @@ public static void signMessage(
5268
}
5369

5470
/**
55-
* Verify a message using SIG(0).
71+
* Verify a message using SIG(0). Uses the current system clock for the date/time.
5672
*
5773
* @param message The message to be signed
5874
* @param b An array containing the message in unparsed form. This is necessary since SIG(0) signs
@@ -62,6 +78,23 @@ public static void signMessage(
6278
* @param previous If this message is a response, the SIG(0) from the query
6379
*/
6480
public static void verifyMessage(Message message, byte[] b, KEYRecord key, SIGRecord previous)
81+
throws DNSSECException {
82+
verifyMessage(message, b, key, previous, Instant.now());
83+
}
84+
85+
/**
86+
* Verify a message using SIG(0).
87+
*
88+
* @param message The message to be signed
89+
* @param b An array containing the message in unparsed form. This is necessary since SIG(0) signs
90+
* the message in wire format, and we can't recreate the exact wire format (with the same name
91+
* compression).
92+
* @param key The KEY record to verify the signature with.
93+
* @param previous If this message is a response, the SIG(0) from the query
94+
* @param now the time instant to verify the message.
95+
*/
96+
public static void verifyMessage(
97+
Message message, byte[] b, KEYRecord key, SIGRecord previous, Instant now)
6598
throws DNSSEC.DNSSECException {
6699
SIGRecord sig = null;
67100
Record[] additional = message.getSectionArray(Section.ADDITIONAL);
@@ -75,6 +108,6 @@ public static void verifyMessage(Message message, byte[] b, KEYRecord key, SIGRe
75108
sig = (SIGRecord) record;
76109
break;
77110
}
78-
DNSSEC.verifyMessage(message, b, sig, previous, key);
111+
DNSSEC.verifyMessage(message, b, sig, previous, key, now);
79112
}
80113
}

0 commit comments

Comments
 (0)