-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow EC signing keys and multiple checking keys in ManagementPortal
Also adds configuration keys for the keystore password and the key aliases to use for signing and checking.
- Loading branch information
1 parent
b06e8df
commit 24c31fd
Showing
26 changed files
with
588 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/org/radarcns/management/security/jwt/EcdsaSigner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.radarcns.management.security.jwt; | ||
|
||
import org.springframework.security.jwt.crypto.sign.Signer; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.Signature; | ||
import java.security.interfaces.ECPrivateKey; | ||
|
||
/** | ||
* Class that creates ECDSA signatures for use in Spring Security. | ||
*/ | ||
public class EcdsaSigner implements Signer { | ||
|
||
public static final String DEFAULT_ALGORITHM = "SHA256withECDSA"; | ||
private final ECPrivateKey privateKey; | ||
private final String algorithm; | ||
|
||
public EcdsaSigner(ECPrivateKey privateKey) { | ||
this(privateKey, DEFAULT_ALGORITHM); | ||
} | ||
|
||
public EcdsaSigner(ECPrivateKey privateKey, String signingAlgorithm) { | ||
this.privateKey = privateKey; | ||
this.algorithm = signingAlgorithm; | ||
} | ||
|
||
@Override | ||
public byte[] sign(byte[] bytes) { | ||
try { | ||
Signature signature = Signature.getInstance(algorithm); | ||
signature.initSign(privateKey); | ||
signature.update(bytes); | ||
return signature.sign(); | ||
} catch (GeneralSecurityException ex) { | ||
throw new SignatureException("Could not provide a signature", ex); | ||
} | ||
} | ||
|
||
@Override | ||
public String algorithm() { | ||
return algorithm; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/org/radarcns/management/security/jwt/EcdsaVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.radarcns.management.security.jwt; | ||
|
||
import org.springframework.security.jwt.crypto.sign.InvalidSignatureException; | ||
import org.springframework.security.jwt.crypto.sign.SignatureVerifier; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.Signature; | ||
import java.security.interfaces.ECPublicKey; | ||
|
||
public class EcdsaVerifier implements SignatureVerifier { | ||
|
||
private final ECPublicKey publicKey; | ||
private final String algorithm; | ||
|
||
public EcdsaVerifier(ECPublicKey publicKey) { | ||
this(publicKey, EcdsaSigner.DEFAULT_ALGORITHM); | ||
} | ||
|
||
public EcdsaVerifier(ECPublicKey publicKey, String algorithm) { | ||
this.publicKey = publicKey; | ||
this.algorithm = algorithm; | ||
} | ||
|
||
@Override | ||
public void verify(byte[] content, byte[] sig) { | ||
try { | ||
Signature signature = Signature.getInstance(algorithm); | ||
signature.initVerify(publicKey); | ||
signature.update(content); | ||
|
||
if (!signature.verify(sig)) { | ||
throw new InvalidSignatureException("EC Signature did not match content"); | ||
} | ||
} catch (GeneralSecurityException ex) { | ||
throw new SignatureException("An error occured verifying the signature", ex); | ||
} | ||
} | ||
|
||
@Override | ||
public String algorithm() { | ||
return algorithm; | ||
} | ||
} |
Oops, something went wrong.