Skip to content

Commit

Permalink
Fix #524: Include version in the token digest (#525)
Browse files Browse the repository at this point in the history
* Fix #524: Include version in the token digest

* Include version decision making in the library

* Improve the switch-case block a bit
  • Loading branch information
petrdvorak authored Sep 7, 2023
1 parent 504c9a4 commit b326290
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ public byte[] generateTokenTimestamp() {
*
* @param nonce Token nonce, 16 random bytes.
* @param timestamp Token timestamp, Unix timestamp format encoded as bytes (from string representation).
* @param version Protocol version.
* @param tokenSecret Token secret, 16 random bytes.
* @return Token digest computed using provided data bytes with given token secret.
* @throws GenericCryptoException In case digest computation fails.
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException {
return tokenUtils.computeTokenDigest(nonce, timestamp, tokenSecret);
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, String version, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException {
return tokenUtils.computeTokenDigest(nonce, timestamp, version, tokenSecret);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,36 @@ public byte[] convertTokenTimestamp(long timestamp) {
* Compute the digest of provided token information using given token secret.
* @param nonce Token nonce, 16 random bytes.
* @param timestamp Token timestamp, Unix timestamp format encoded as bytes (string representation).
* @param version Protocol version.
* @param tokenSecret Token secret, 16 random bytes.
* @return Token digest computed using provided data bytes with given token secret.
* @throws GenericCryptoException In case digest computation fails.
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException {
byte[] amp = "&".getBytes(StandardCharsets.UTF_8);
byte[] data = ByteUtils.concat(nonce, amp, timestamp);
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, String version, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException {
final byte[] amp = "&".getBytes(StandardCharsets.UTF_8);
final byte[] data;
switch (version) {
case "3.2" -> data = ByteUtils.concat(nonce, amp, timestamp, amp, version.getBytes(StandardCharsets.UTF_8));
case "3.0", "3.1" -> data = ByteUtils.concat(nonce, amp, timestamp);
default -> throw new GenericCryptoException("Unsupported version value was specified: " + version);
}
return hmac.hash(tokenSecret, data);
}

/**
* Validate provided token digest for given input data and provided token secret.
* @param nonce Token nonce, 16 random bytes.
* @param timestamp Token timestamp, Unix timestamp format encoded as bytes (string representation).
* @param version Protocol version.
* @param tokenSecret Token secret, 16 random bytes.
* @param tokenDigest Token digest, 32 bytes to be validated.
* @return Token digest computed using provided data bytes with given token secret.
* @throws GenericCryptoException In case digest computation fails.
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException {
return SideChannelUtils.constantTimeAreEqual(computeTokenDigest(nonce, timestamp, tokenSecret), tokenDigest);
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, String version, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException {
return SideChannelUtils.constantTimeAreEqual(computeTokenDigest(nonce, timestamp, version, tokenSecret), tokenDigest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public byte[] convertTokenTimestamp(long timestamp) {
* Validate provided token digest for given input data and provided token secret.
* @param nonce Token nonce, 16 random bytes.
* @param timestamp Token timestamp, Unix timestamp format encoded as bytes (from string representation).
* @param version Protocol version.
* @param tokenSecret Token secret, 16 random bytes.
* @param tokenDigest Token digest, 32 bytes to be validated.
* @return Token digest computed using provided data bytes with given token secret.
* @throws GenericCryptoException In case digest computation fails.
* @throws CryptoProviderException In case cryptography provider is incorrectly initialized.
*/
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException {
return tokenUtils.validateTokenDigest(nonce, timestamp, tokenSecret, tokenDigest);
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, String version, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException {
return tokenUtils.validateTokenDigest(nonce, timestamp, version, tokenSecret, tokenDigest);
}

}

0 comments on commit b326290

Please sign in to comment.