Search in sources :

Example 16 with OtrCryptoEngineImpl

use of net.java.otr4j.crypto.OtrCryptoEngineImpl in project Zom-Android by zom.

the class AuthContextImpl method handleSignatureMessage.

private void handleSignatureMessage(SignatureMessage m) throws OtrException {
    Session session = getSession();
    SessionID sessionID = session.getSessionID();
    logger.finest(sessionID.getLocalUserId() + " received a signature message from " + sessionID.getRemoteUserId() + " throught " + sessionID.getProtocolName() + ".");
    if (!session.getSessionPolicy().getAllowV2()) {
        logger.finest("Policy does not allow OTRv2, ignoring message.");
        return;
    }
    switch(this.getAuthenticationState()) {
        case AWAITING_SIG:
            // Verify MAC.
            if (!m.verify(this.getM2p())) {
                logger.finest("Signature MACs are not equal, ignoring message.");
                return;
            }
            // Decrypt X.
            byte[] remoteXDecrypted = m.decrypt(this.getCp());
            SignatureX remoteX;
            try {
                remoteX = SerializationUtils.toMysteriousX(remoteXDecrypted);
            } catch (IOException e) {
                throw new OtrException(e);
            }
            // Compute signature.
            PublicKey remoteLongTermPublicKey = remoteX.longTermPublicKey;
            SignatureM remoteM = new SignatureM(this.getRemoteDHPublicKey(), (DHPublicKey) this.getLocalDHKeyPair().getPublic(), remoteLongTermPublicKey, remoteX.dhKeyID);
            OtrCryptoEngine otrCryptoEngine = new OtrCryptoEngineImpl();
            // Verify signature.
            byte[] signature;
            try {
                signature = otrCryptoEngine.sha256Hmac(SerializationUtils.toByteArray(remoteM), this.getM1p());
            } catch (IOException e) {
                throw new OtrException(e);
            }
            if (!otrCryptoEngine.verify(signature, remoteLongTermPublicKey, remoteX.signature)) {
                session.showWarning("Bad signature");
                logger.finest("Signature verification failed.");
                return;
            }
            this.setIsSecure(true);
            this.setRemoteLongTermPublicKey(remoteLongTermPublicKey);
            break;
        default:
            logger.finest("We were not expecting a signature, ignoring message.");
            return;
    }
}
Also used : SignatureM(net.java.otr4j.io.messages.SignatureM) OtrCryptoEngine(net.java.otr4j.crypto.OtrCryptoEngine) PublicKey(java.security.PublicKey) DHPublicKey(javax.crypto.interfaces.DHPublicKey) SignatureX(net.java.otr4j.io.messages.SignatureX) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) IOException(java.io.IOException) OtrException(net.java.otr4j.OtrException)

Example 17 with OtrCryptoEngineImpl

use of net.java.otr4j.crypto.OtrCryptoEngineImpl in project Zom-Android by zom.

the class OtrKeyManagerDefaultImpl method getLocalFingerprint.

public String getLocalFingerprint(SessionID sessionID) {
    KeyPair keyPair = loadLocalKeyPair(sessionID);
    if (keyPair == null)
        return null;
    PublicKey pubKey = keyPair.getPublic();
    try {
        return new OtrCryptoEngineImpl().getFingerprint(pubKey);
    } catch (OtrCryptoException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : KeyPair(java.security.KeyPair) OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl)

Example 18 with OtrCryptoEngineImpl

use of net.java.otr4j.crypto.OtrCryptoEngineImpl in project Zom-Android by zom.

the class OtrAndroidKeyManagerImpl method getRemoteFingerprint.

public String getRemoteFingerprint(String fullUserId) {
    String fingerprint = this.store.getProperty(fullUserId + ".fingerprint");
    if (fingerprint != null) {
        // If we have a fingerprint stashed, assume it is correct.
        return fingerprint;
    }
    // if we can't find an exact match, let's show the first one that matches the id sans resource
    for (Object fpKey : store.getKeySet().toArray()) {
        String fpKeyString = (String) fpKey;
        if (fpKeyString.startsWith(fullUserId) && fpKeyString.endsWith(".fingerprint")) {
            fingerprint = store.getProperty(fpKeyString);
            if (fingerprint != null)
                return fingerprint;
        }
    }
    PublicKey remotePublicKey = loadRemotePublicKeyFromStore(fullUserId);
    if (remotePublicKey == null)
        return null;
    try {
        // Store the fingerprint, for posterity.
        String fingerprintString = new OtrCryptoEngineImpl().getFingerprint(remotePublicKey);
        this.store.setProperty(fullUserId + ".fingerprint", fingerprintString);
        return fingerprintString;
    } catch (OtrCryptoException e) {
        throw new RuntimeException("OtrCryptoException getting remote fingerprint", e);
    }
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl)

Example 19 with OtrCryptoEngineImpl

use of net.java.otr4j.crypto.OtrCryptoEngineImpl in project Pix-Art-Messenger by kriztan.

the class Account method getOtrFingerprint.

public String getOtrFingerprint() {
    if (this.otrFingerprint == null) {
        try {
            if (this.mOtrService == null) {
                return null;
            }
            final PublicKey publicKey = this.mOtrService.getPublicKey();
            if (publicKey == null || !(publicKey instanceof DSAPublicKey)) {
                return null;
            }
            this.otrFingerprint = new OtrCryptoEngineImpl().getFingerprint(publicKey).toLowerCase(Locale.US);
            return this.otrFingerprint;
        } catch (final OtrCryptoException ignored) {
            return null;
        }
    } else {
        return this.otrFingerprint;
    }
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Aggregations

OtrCryptoEngineImpl (net.java.otr4j.crypto.OtrCryptoEngineImpl)19 PublicKey (java.security.PublicKey)8 OtrException (net.java.otr4j.OtrException)8 KeyPair (java.security.KeyPair)7 OtrCryptoException (net.java.otr4j.crypto.OtrCryptoException)7 IOException (java.io.IOException)6 DHPublicKey (javax.crypto.interfaces.DHPublicKey)5 OtrCryptoEngine (net.java.otr4j.crypto.OtrCryptoEngine)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OtrInputStream (net.java.otr4j.io.OtrInputStream)3 OtrOutputStream (net.java.otr4j.io.OtrOutputStream)3 BigInteger (java.math.BigInteger)2 ByteBuffer (java.nio.ByteBuffer)2 DSAPublicKey (java.security.interfaces.DSAPublicKey)2 SignatureM (net.java.otr4j.io.messages.SignatureM)2 SignatureX (net.java.otr4j.io.messages.SignatureX)2 AbstractChat (com.xabber.android.data.message.AbstractChat)1 RegularChat (com.xabber.android.data.message.RegularChat)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1