diff options
| author | Nils Schwabe | 2014-05-09 18:28:06 +0200 |
|---|---|---|
| committer | Nils Schwabe | 2014-05-09 18:28:06 +0200 |
| commit | b496a9211e738b92d421914e984a5574af32622e (patch) | |
| tree | 303fe9b4c23e13caeb57be722a2bf41c7ac8bc0b /src/main/java/org/openslx | |
| parent | Fix bug where the user list was accessed by multiple threads (diff) | |
| download | masterserver-b496a9211e738b92d421914e984a5574af32622e.tar.gz masterserver-b496a9211e738b92d421914e984a5574af32622e.tar.xz masterserver-b496a9211e738b92d421914e984a5574af32622e.zip | |
Forgot to commit some files.
Diffstat (limited to 'src/main/java/org/openslx')
| -rw-r--r-- | src/main/java/org/openslx/imagemaster/db/DbKey.java | 16 | ||||
| -rw-r--r-- | src/main/java/org/openslx/imagemaster/util/AsymMessageVerifier.java | 68 |
2 files changed, 84 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/imagemaster/db/DbKey.java b/src/main/java/org/openslx/imagemaster/db/DbKey.java new file mode 100644 index 0000000..b57065f --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/db/DbKey.java @@ -0,0 +1,16 @@ +package org.openslx.imagemaster.db; + + +public class DbKey +{ + + public final byte[] bytes; + + public DbKey(byte[] bytes) { + this.bytes = bytes; + } + + public static DbKey fromOrganization(String organization) { + return MySQL.findUniqueOrNull( DbKey.class, "SELECT publickey FROM satellite WHERE organization = ?", organization ); + } +} diff --git a/src/main/java/org/openslx/imagemaster/util/AsymMessageVerifier.java b/src/main/java/org/openslx/imagemaster/util/AsymMessageVerifier.java new file mode 100644 index 0000000..e2a0a0e --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/util/AsymMessageVerifier.java @@ -0,0 +1,68 @@ +package org.openslx.imagemaster.util; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.KeyFactory; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; +import java.security.Signature; +import java.security.SignatureException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.security.spec.X509EncodedKeySpec; + +import org.openslx.imagemaster.db.DbKey; +import org.openslx.imagemaster.db.DbSatellite; + +public class AsymMessageVerifier +{ + + private PublicKey key; + + /** + * Load the key + * @param organization the organization to verify + * @throws Exception + * @throws NoSuchAlgorithmException + * @throws CertificateException + * @throws FileNotFoundException + * @throws IOException + * @throws KeyStoreException + * @throws UnrecoverableKeyException + */ + public AsymMessageVerifier(String organization) throws Exception + { + byte[] b = DbKey.fromOrganization( organization ).bytes; + + if (b == null) throw new Exception("Organization not found."); + + KeyFactory kf = KeyFactory.getInstance( "RSA" ); + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(b); + key = kf.generatePublic(keySpec); + } + + /** + * Verify an encrypted message + * @param signedMessage The signed message from hs/uni server + * @param realMessage The message that was sent before + * @param alias the alias of the certificate + * @param password the password of the certificate + * @return Whether the message could be verfied or not + * @throws NoSuchAlgorithmException + * @throws InvalidKeyException + * @throws SignatureException + * @throws UnrecoverableKeyException + * @throws KeyStoreException + */ + public boolean verifyMessage( byte[] signedMessage, byte[] realMessage, String alias ) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnrecoverableKeyException, KeyStoreException + { + // verify message + Signature signature = Signature.getInstance( "SHA256WITHRSA" ); + signature.initVerify( key ); + signature.update( realMessage ); + return signature.verify( signedMessage ); + } + +} |
