From b496a9211e738b92d421914e984a5574af32622e Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Fri, 9 May 2014 18:28:06 +0200 Subject: Forgot to commit some files. --- .../java/org/openslx/imagemaster/db/DbKey.java | 16 +++++ .../imagemaster/util/AsymMessageVerifier.java | 68 ++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/org/openslx/imagemaster/db/DbKey.java create mode 100644 src/main/java/org/openslx/imagemaster/util/AsymMessageVerifier.java (limited to 'src') 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 ); + } + +} -- cgit v1.2.3-55-g7522