package foundation.pEp.jniadapter; import java.util.ArrayList; import java.util.Vector; import foundation.pEp.jniadapter.Sync.DefaultCallback; import java.io.UnsupportedEncodingException; import java.text.Normalizer; abstract class AbstractEngine extends UniquelyIdentifiable implements AutoCloseable { static { System.loadLibrary("pEpJNI"); } private Sync.MessageToSendCallback messageToSendCallback; private Sync.NotifyHandshakeCallback notifyHandshakeCallback; private Sync.NeedsFastPollCallback needsFastPollCallback; private final static DefaultCallback defaultCallback = new DefaultCallback(); private native void init(); private native void release(); public AbstractEngine() throws pEpException { synchronized (AbstractEngine.class) { init(); } } final public void close() { synchronized (AbstractEngine.class){ release(); } } public native String getVersion(); public native String getProtocolVersion(); private long keyserverThread; private long keyserverQueue; public native void startKeyserverLookup(); public native void stopKeyserverLookup(); public native void startSync(); public native void stopSync(); public native boolean isSyncRunning(); public static byte[] toUTF8(String str) { if (str == null) return null; try { String _str = Normalizer.normalize(str, Normalizer.Form.NFC); byte _buf[] = _str.getBytes("UTF-8"); byte _cpy[] = new byte[_buf.length]; System.arraycopy(_buf,0,_cpy,0,_buf.length); return _cpy; } catch (UnsupportedEncodingException e) { assert false; return new byte[0]; } } public static Vector toUTF8(Vector list) { if (list == null) return null; Vector result = new Vector(list.size()); for (int i=0; i toUTF8(Pair pair) { if (pair == null) return null; Pair result = new Pair(); result.first = toUTF8(pair.first); result.second = toUTF8(pair.second); return result; } public static ArrayList> toUTF8(ArrayList> list) { if (list == null) return null; ArrayList> result = new ArrayList>(list.size()); for (int i=0; i toUTF16(Vector list) { if (list == null) return null; Vector result = new Vector(list.size()); for (int i=0; i toUTF16(Pair pair) { if (pair == null) return null; Pair result = new Pair(); result.first = toUTF16(pair.first); result.second = toUTF16(pair.second); return result; } public static ArrayList> toUTF16(ArrayList> list) { if (list == null) return null; ArrayList> result = new ArrayList>(list.size()); for (int i=0; i> opts = new ArrayList<>(); Pair xpEp = new Pair<>(); xpEp.first = "X-pEp-Version"; xpEp.second = this.getProtocolVersion();; opts.add(xpEp); msg.setOptFields(opts); if(encFormat == Message.EncFormat.PEP) { // For EncFormat.PEP // The pgpText goes into the attachment index 1 msg.setShortmsg("p≡p"); msg.setLongmsg("this message was encrypted with p≡p https://pEp-project.org"); // Attachments Blob att0 = new Blob(); att0.mime_type = "application/pgp-encrypted"; att0.filename = null; att0.data = "Version: 1".getBytes(); Blob att1 = new Blob(); att1.mime_type = "application/octet-stream"; att1.filename = "file://msg.asc"; att1.data = pgpText.getBytes(); Vector attachments = new Vector<>(); attachments.add(att0); attachments.add(att1); msg.setAttachments(attachments); } else if (encFormat == Message.EncFormat.PEPEncInlineEA) { // For EncFormat.PEPEncInlineEA // The pgpText goes into the longMessage msg.setShortmsg(""); msg.setLongmsg(pgpText); } else { throw new pEpCannotEncode("Message.Encformat not supported: " + encFormat.toString()); } return msg; } }