Search in sources :

Example 1 with OtrCryptoEngineImpl

use of net.java.otr4j.crypto.OtrCryptoEngineImpl in project Spark by igniterealtime.

the class MyOtrKeyManager method getLocalFingerprint.

/**
 * Returns the local finger print for specified session. If there is no
 * finger print you might generate one.
 *
 * @return the local finger print for this sessionID
 */
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 2 with OtrCryptoEngineImpl

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

the class OtrAndroidKeyManagerImpl method savePublicKey.

public void savePublicKey(SessionID sessionID, PublicKey pubKey) {
    if (sessionID == null)
        return;
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey.getEncoded());
    // if (!Address.hasResource(sessionID.getRemoteUserId()))
    // return;
    String fullUserId = sessionID.getRemoteUserId();
    this.store.setProperty(fullUserId + ".publicKey", x509EncodedKeySpec.getEncoded());
    // and is useful for transferring rosters to other apps.
    try {
        String fingerprintString = new OtrCryptoEngineImpl().getFingerprint(pubKey);
        String verifiedToken = buildPublicKeyVerifiedId(sessionID.getRemoteUserId(), fingerprintString);
        String fingerprintKey = fullUserId + ".fingerprint";
        // if a fingerprint for this userid exists, then check if the key is verified
        if (this.store.hasProperty(fingerprintKey)) {
            if (!this.store.hasProperty(verifiedToken))
                this.store.setProperty(verifiedToken, false);
        } else {
            // if there is no key, then we can "trust on first use"!
            this.store.setProperty(fingerprintKey, fingerprintString);
            this.store.setProperty(verifiedToken, true);
        }
    } catch (OtrCryptoException e) {
        Log.e(ImApp.LOG_TAG, "otr error: " + e.getMessage(), e);
    }
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 3 with OtrCryptoEngineImpl

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

the class SignatureMessage method verify.

public boolean verify(byte[] key) throws OtrException {
    // Hash the key.
    byte[] xbEncrypted;
    try {
        xbEncrypted = SerializationUtils.writeData(xEncrypted);
    } catch (IOException e) {
        throw new OtrException(e);
    }
    byte[] xEncryptedMAC = new OtrCryptoEngineImpl().sha256Hmac160(xbEncrypted, key);
    // Verify signature.
    return Arrays.equals(this.xEncryptedMAC, xEncryptedMAC);
}
Also used : OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) IOException(java.io.IOException) OtrException(net.java.otr4j.OtrException)

Example 4 with OtrCryptoEngineImpl

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

the class AuthContextImpl method h2.

private byte[] h2(byte b) throws OtrException {
    byte[] secbytes;
    try {
        secbytes = SerializationUtils.writeMpi(getS());
    } catch (IOException e) {
        throw new OtrException(e);
    }
    int len = secbytes.length + 1;
    ByteBuffer buff = ByteBuffer.allocate(len);
    buff.put(b);
    buff.put(secbytes);
    byte[] sdata = buff.array();
    return new OtrCryptoEngineImpl().sha256Hash(sdata);
}
Also used : OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) IOException(java.io.IOException) OtrException(net.java.otr4j.OtrException) ByteBuffer(java.nio.ByteBuffer)

Example 5 with OtrCryptoEngineImpl

use of net.java.otr4j.crypto.OtrCryptoEngineImpl in project xabber-android by redsolution.

the class OTRManager method sessionStatusChanged.

@Override
public void sessionStatusChanged(SessionID sessionID) {
    removeSMRequest(sessionID.getAccountID(), sessionID.getUserID());
    removeSMProgress(sessionID.getAccountID(), sessionID.getUserID());
    Session session = sessions.get(sessionID.getAccountID(), sessionID.getUserID());
    SessionStatus sStatus = session.getSessionStatus();
    LogManager.i(this, "session status changed " + sessionID.getUserID() + " status: " + sStatus);
    if (sStatus == SessionStatus.ENCRYPTED) {
        finished.remove(sessionID.getAccountID(), sessionID.getUserID());
        PublicKey remotePublicKey = session.getRemotePublicKey();
        String value;
        try {
            OtrCryptoEngine otrCryptoEngine = new OtrCryptoEngineImpl();
            value = otrCryptoEngine.getFingerprint(remotePublicKey);
        } catch (OtrCryptoException e) {
            LogManager.exception(this, e);
            value = null;
        }
        if (value != null) {
            actives.put(sessionID.getAccountID(), sessionID.getUserID(), value);
            if (fingerprints.get(sessionID.getAccountID(), sessionID.getUserID(), value) == null) {
                fingerprints.put(sessionID.getAccountID(), sessionID.getUserID(), value, false);
                requestToWrite(sessionID.getAccountID(), sessionID.getUserID(), value, false);
            }
        }
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, isVerified(sessionID.getAccountID(), sessionID.getUserID()) ? ChatAction.otr_verified : ChatAction.otr_encryption);
        AbstractChat chat = getChat(sessionID.getAccountID(), sessionID.getUserID());
        if (chat != null) {
            chat.sendMessages();
        }
    } else if (sStatus == SessionStatus.PLAINTEXT) {
        actives.remove(sessionID.getAccountID(), sessionID.getUserID());
        sessions.remove(sessionID.getAccountID(), sessionID.getUserID());
        finished.remove(sessionID.getAccountID(), sessionID.getUserID());
        try {
            session.endSession();
        } catch (OtrException e) {
            LogManager.exception(this, e);
        }
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_plain);
    } else if (sStatus == SessionStatus.FINISHED) {
        actives.remove(sessionID.getAccountID(), sessionID.getUserID());
        sessions.remove(sessionID.getAccountID(), sessionID.getUserID());
        finished.put(sessionID.getAccountID(), sessionID.getUserID(), true);
        newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_finish);
        // if session was finished then clear OTR-resource for this chat
        RegularChat chat = (RegularChat) getChat(sessionID.getAccountID(), sessionID.getUserID());
        if (chat != null) {
            chat.setOTRresource(null);
        }
    } else {
        throw new IllegalStateException();
    }
    onContactChanged(sessionID);
}
Also used : OtrCryptoEngine(net.java.otr4j.crypto.OtrCryptoEngine) OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) AbstractChat(com.xabber.android.data.message.AbstractChat) SessionStatus(net.java.otr4j.session.SessionStatus) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) OtrException(net.java.otr4j.OtrException) RegularChat(com.xabber.android.data.message.RegularChat) Session(net.java.otr4j.session.Session)

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