add minimally working otr

Mon, 30 Dec 2024 11:44:48 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 30 Dec 2024 11:44:48 +0100
changeset 4
856befba7674
parent 3
25a32e2dfde5
child 5
d05262580576

add minimally working otr

src/main/java/de/unixwork/im/OTR.java file | annotate | diff | comparison | revisions
src/main/java/de/unixwork/im/Xmpp.java file | annotate | diff | comparison | revisions
--- a/src/main/java/de/unixwork/im/OTR.java	Thu Dec 26 17:19:15 2024 +0100
+++ b/src/main/java/de/unixwork/im/OTR.java	Mon Dec 30 11:44:48 2024 +0100
@@ -57,7 +57,7 @@
 
     @Override
     public void smpAborted(SessionID sid) throws OtrException {
-        
+        System.out.println("smpAborted");
     }
 
     @Override
@@ -72,16 +72,17 @@
 
     @Override
     public OtrPolicy getSessionPolicy(SessionID sid) {
-        return new OtrPolicyImpl(OtrPolicy.ALLOW_V2 | OtrPolicy.ALLOW_V3 | OtrPolicy.OPPORTUNISTIC);
+        return new OtrPolicyImpl(OtrPolicy.ALLOW_V2 | OtrPolicy.ALLOW_V3 | OtrPolicy.OPPORTUNISTIC | OtrPolicy.ERROR_START_AKE);
     }
 
     @Override
     public FragmenterInstructions getFragmenterInstructions(SessionID sid) {
-        return new FragmenterInstructions(4096, 131072);
+        return new FragmenterInstructions(16, 2048);
     }
 
     @Override
     public KeyPair getLocalKeyPair(SessionID sid) throws OtrException {
+        System.out.println("getLocalKeyPair");
         // Check if a key pair already exists for the session
         if (keyPairCache.containsKey(sid)) {
             return keyPairCache.get(sid);
@@ -109,6 +110,7 @@
 
     @Override
     public byte[] getLocalFingerprintRaw(SessionID sid) {
+        System.out.println("getLocalFingerprintRaw");
         // code from DummyClient: https://github.com/jitsi/otr4j/blob/master/src/test/java/net/java/otr4j/session/DummyClient.java
         try {
             return new OtrCryptoEngineImpl()
@@ -144,7 +146,7 @@
 
     @Override
     public String getFallbackMessage(SessionID sid) {
-        return "error";
+        return "";
     }
 
     @Override
--- a/src/main/java/de/unixwork/im/Xmpp.java	Thu Dec 26 17:19:15 2024 +0100
+++ b/src/main/java/de/unixwork/im/Xmpp.java	Mon Dec 30 11:44:48 2024 +0100
@@ -62,9 +62,9 @@
         String account = config.getUsername() + "@" + config.getHostString() + "/IM5";
         SessionID sid = new SessionID(account, xid, "xmpp");
         
-        Session session = new SessionImpl(sid, otr);
-        String[] outgoingMessage;
+        Session session = otrSM.getSession(sid); //new SessionImpl(sid, otr);
         try {
+            System.out.println("otr session start");
             session.startSession();
         } catch (OtrException ex) {
             Logger.getLogger(Xmpp.class.getName()).log(Level.SEVERE, null, ex);
@@ -130,9 +130,28 @@
             connect();
             connection.addAsyncStanzaListener((stanza -> {
                         var jid = stanza.getFrom();
+                        boolean isSecure = false;
                         if(jid != null) {
                             String body = ((Message)stanza).getBody();
-                            App.getInstance().dispatchMessage(jid, body, true);
+                            if(body.startsWith("?OTR")) {
+                                String account = config.getUsername() + "@" + config.getHostString() + "/IM5";
+                                SessionID sid = new SessionID(account, jid.asBareJid().toString(), "xmpp");
+                                Session sn = otrSM.getSession(sid);
+                                String cryptoMsg = body;
+                                body = null;
+                                try {
+                                    String otrMsg = sn.transformReceiving(cryptoMsg);
+                                    System.out.println("otr transformed: " + otrMsg);
+                                    body = otrMsg;
+                                    isSecure = true;
+                                } catch (OtrException ex) {
+                                    Logger.getLogger(Xmpp.class.getName()).log(Level.SEVERE, null, ex);
+                                }
+                            }
+                            
+                            if(body != null) {
+                                App.getInstance().dispatchMessage(jid, body, isSecure);
+                            }
                         }
                     }), MessageWithBodiesFilter.INSTANCE);
             connection.addAsyncStanzaListener(stanza -> {

mercurial