Skip to content

Commit

Permalink
Fixes an issue when verifying a message without expiration date.
Browse files Browse the repository at this point in the history
  • Loading branch information
shifteverywhere committed Nov 24, 2021
1 parent a022ca4 commit 71a24e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/main/java/io/dimeformat/Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ public static Key generateSharedSecret(Key clientKey, Key serverKey) throws Dime
} else {
throw new DimeKeyMismatchException("Invalid keys provided.");
}
System.out.println("shared: " + Utility.toHex(shared));
System.out.println("---");
return new Key(UUID.randomUUID(), KeyType.ENCRYPTION, shared, null);
}

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/io/dimeformat/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ public Message(UUID issuerId, long validFor) {
this(null, issuerId, validFor, null);
}

/**
* Creates a message to a specified audience (receiver) from a specified issuer (sender).
* @param audienceId The audience identifier. Providing -1 as validFor will skip setting an expiration date.
* @param issuerId The issuer identifier.
*/
public Message(UUID audienceId, UUID issuerId) {
this(audienceId, issuerId, -1, null);
}

/**
* Creates a message to a specified audience (receiver) from a specified issuer (sender), with an expiration date.
* @param audienceId The audience identifier. Providing -1 as validFor will skip setting an expiration date.
Expand Down Expand Up @@ -216,8 +225,10 @@ public void verify(Key key) throws DimeDateException, DimeIntegrityException {
// Verify IssuedAt and ExpiresAt
Instant now = Instant.now();
if (this.getIssuedAt().compareTo(now) > 0) { throw new DimeDateException("Issuing date in the future."); }
if (this.getIssuedAt().compareTo(this.getExpiresAt()) > 0) { throw new DimeDateException("Expiration before issuing date."); }
if (this.getExpiresAt().compareTo(now) < 0) { throw new DimeDateException("Passed expiration date."); }
if (this.getExpiresAt() != null) {
if (this.getIssuedAt().compareTo(this.getExpiresAt()) > 0) { throw new DimeDateException("Expiration before issuing date."); }
if (this.getExpiresAt().compareTo(now) < 0) { throw new DimeDateException("Passed expiration date."); }
}
super.verify(key);
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/io/dimeformat/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ void verifyTest3() {
}
}

@Test
void verifyTest4() {
try {
Identity.setTrustedIdentity(Commons.getTrustedIdentity());
Message message = new Message(Commons.getAudienceIdentity().getSubjectId(), Commons.getIssuerIdentity().getSubjectId());
message.setPayload("Racecar is racecar backwards.".getBytes(StandardCharsets.UTF_8));
message.sign(Commons.getIssuerKey());
message.verify(Commons.getIssuerIdentity().getPublicKey());
} catch (Exception e) {
fail("Unexpected exception thrown: " + e);
}
}

@Test
void importTest1() {
try {
Expand Down

0 comments on commit 71a24e9

Please sign in to comment.