Search in sources :

Example 1 with WireWriter

use of dev.hawala.xns.level3.courier.WireWriter in project dodo by devhawala.

the class MailingNewImpl method transport5_postBegin.

/*
	 * postBegin
	 *  = procedure 1
	 */
private static void transport5_postBegin(PostBeginParams params, PostBeginResults results) {
    // log ingoing data
    if (logParamsAndResults) {
        StringBuilder sb = new StringBuilder();
        params.append(sb, "  ", "params");
        logf("##\n## procedure MailingNewImpl.transport5_postBegin() -- params\n%s\n##\n", sb.toString());
    }
    // check the credentials:
    // - this procedure is called for the generic mail server ("Mail Service:CHServers:CHServers")
    // - not for this specific mail service (which the 1st mail service name in the clearinghouse database)
    // - so use the generic nameconversationKey
    // - and: only the machine id for *this* mail service works...
    Credentials credentials = params.getCredentials();
    Verifier verifier = params.getVerifier();
    StrongVerifier decodedVerifier = StrongVerifier.make();
    int[] decodedConversationKey = new int[4];
    ThreePartName senderName = // throws an exception on invalid credentials
    mailService.checkCredentials(mailService.getChsDatabase().getGenericMailServiceName(), mailService.getMachineId(), credentials, verifier, decodedConversationKey, decodedVerifier);
    // start the mail transaction for this post call sequence
    PostMailTransaction mailTransaction = createMailTransaction();
    // just to be sure we have some sender name
    mailTransaction.senderName = senderName;
    // set return values
    results.mailTransaction.set(mailTransaction.transactionId);
    if (credentials.type.get() == CredentialsType.simple) {
        // return the initiators verifier
        results.verifier.add().set(verifier.get(0).get());
    } else {
        // create a strong verifier based on the received verifier
        // session.getConversationKey();
        int[] conversationKey = decodedConversationKey;
        if (conversationKey != null && conversationKey.length == 4) {
            // xor-ing values
            // the server machine, not(!) the remoteHostId extracted from the Logon request
            long xorHostId = machineId;
            // left justified machine-id => upper 32 bits
            long rcptTimestampMachineId32Bits = (xorHostId >> 16) & 0xFFFFFFFFL;
            // left justified machine-id => lower 32 bits
            long rcptTicksMachineId32Bits = (xorHostId & 0x0000FFFFL) << 16;
            // new verifier values
            long newTicks = decodedVerifier.ticks.get() + 1;
            long newTimestamp = decodedVerifier.timeStamp.get();
            if (newTicks > 0xFFFFFFFFL) {
                newTicks = 0;
                newTimestamp++;
            }
            // plain (unencrypted) verifier with xor-ed values
            StrongVerifier verfr = StrongVerifier.make();
            verfr.ticks.set(newTicks ^ rcptTicksMachineId32Bits);
            verfr.timeStamp.set(newTimestamp ^ rcptTimestampMachineId32Bits);
            // encrypt verifier and transfer into results
            try {
                WireWriter writer = new WireWriter();
                verfr.serialize(writer);
                int[] sourceBytes = writer.getWords();
                int[] encrypted = StrongAuthUtils.xnsDesEncrypt(conversationKey, sourceBytes);
                for (int i = 0; i < encrypted.length; i++) {
                    results.verifier.add().set(encrypted[i]);
                }
            } catch (Exception e) {
                // log and set no verifier => let the invoker decide if acceptable
                logf("** !! unable to serialize or encrypt the verifier in logon results: " + e.getMessage());
            }
        }
    }
    results.unknown0.set(0);
    // log outgoing data
    if (logParamsAndResults) {
        StringBuilder sb = new StringBuilder();
        results.append(sb, "  ", "results");
        logf("##\n## procedure MailingNewImpl.transport5_postBegin() -- results\n%s\n##\n", sb.toString());
    }
}
Also used : StrongVerifier(dev.hawala.xns.level4.common.AuthChsCommon.StrongVerifier) WireWriter(dev.hawala.xns.level3.courier.WireWriter) ThreePartName(dev.hawala.xns.level4.common.AuthChsCommon.ThreePartName) StrongVerifier(dev.hawala.xns.level4.common.AuthChsCommon.StrongVerifier) Verifier(dev.hawala.xns.level4.common.AuthChsCommon.Verifier) Credentials(dev.hawala.xns.level4.common.AuthChsCommon.Credentials) NoMoreWriteSpaceException(dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException) EndOfMessageException(dev.hawala.xns.level3.courier.iWireStream.EndOfMessageException) IOException(java.io.IOException)

Example 2 with WireWriter

use of dev.hawala.xns.level3.courier.WireWriter in project dodo by devhawala.

the class MailingNewImpl method inbasket2_getNextMail.

private static void inbasket2_getNextMail(GetNextMailParams params, GetNextMailResults results) {
    // logging callback used before returning
    UnaryOperator<String> retlog = m -> {
        if (logParamsAndResults) {
            if (m == null) {
                m = "";
            }
            StringBuilder sb = new StringBuilder();
            results.append(sb, "  ", "results");
            logf("##\n## procedure MailingNewImpl.inbasket2_getNextMail() %s -- results\n%s\n##\n", m, sb.toString());
        }
        return null;
    };
    // log ingoing data
    if (logParamsAndResults) {
        StringBuilder sb = new StringBuilder();
        params.append(sb, "  ", "params");
        logf("##\n## procedure MailingNewImpl.inbasket2_getNextMail() -- params\n%s\n##\n", sb.toString());
    }
    // get the session (we do not check the verifier, as we trust our clients...)
    dlogf("  ... fetching mail session ...");
    MailSession session = mailService.getSession(params.sessionId.get());
    if (session == null) {
        logf("failed (invalid session id)\n");
        retlog.apply("[invalid mail session]");
        return;
    }
    // check for valid index for the mail in the inbox
    MailData prevMailData = session.getClientData();
    int index = (prevMailData == null) ? 0 : prevMailData.mailboxIndex + 1;
    dlogf("  ... mailIndex: %d\n", index);
    if (index >= session.getMailCount() || index < 0) {
        // not or no longer present => return empty results
        retlog.apply("[no more mails available]");
        return;
    }
    // get the mail content
    dlogf("  ... getting mailbox entry ... ");
    MailboxEntry me = session.getMailEntry(index);
    dlogf("%s\n", (me != null) ? "ok" : "failed (null)");
    if (me == null) {
        // not or no longer present => return empty results
        retlog.apply("[mail already deleted]");
        return;
    }
    dlogf("  ... loading mail content file ... ");
    ByteArrayOutputStream mailBos = new ByteArrayOutputStream();
    try {
        me.transferContent(istream -> {
            byte[] buffer = new byte[512];
            try {
                int count = istream.read(buffer);
                while (count > 0) {
                    mailBos.write(buffer, 0, count);
                    count = istream.read(buffer);
                }
            } catch (IOException e) {
            // ignore that for now
            }
        });
    } catch (IOException e) {
        // we must at least be able to read the mail file, so...
        retlog.apply("[unable to read mail content]");
        return;
    }
    byte[] mailContentBytes = mailBos.toByteArray();
    dlogf("%d bytes\n", mailContentBytes.length);
    // extract the relevant fields from the (old format) mail content
    dlogf("  ... deserializing mail content ... ");
    SerializedFile mailContentFile = SerializedFile.make();
    try {
        mailContentFile.deserialize(new ByteArrayWireInputStream(mailContentBytes));
    } catch (EndOfMessageException e) {
        // invalid mail file content??
        retlog.apply("[invalid mail file content]");
        return;
    }
    dlogf("ok\n");
    dlogf("  ... extracting relevant data\n");
    NameList xfrom = null;
    NameList xto = null;
    NameList xcc = null;
    NameList xreplyTo = null;
    STRING xsubject = null;
    STRING mailText = null;
    boolean xisFolderAttachment = false;
    SEQUENCE<Attribute> attrs = mailContentFile.file.attributes.value;
    for (int i = 0; i < attrs.size(); i++) {
        try {
            Attribute attr = attrs.get(i);
            int attributeType = (int) (attr.type.get() & 0xFFFF_FFFFL);
            switch(attributeType) {
                case MailingCommon.from:
                    xfrom = attr.decodeData(NameList::make);
                    dlogf("    ... found from\n");
                    break;
                case MailingCommon.to:
                    xto = attr.decodeData(NameList::make);
                    dlogf("    ... found to\n");
                    break;
                case MailingCommon.cc:
                    xcc = attr.decodeData(NameList::make);
                    dlogf("    ... found cc\n");
                    break;
                case MailingCommon.replyTo:
                    xreplyTo = attr.decodeData(NameList::make);
                    dlogf("    ... found replyTo\n");
                    break;
                case MailingCommon.subject:
                    xsubject = attr.decodeData(STRING::make);
                    dlogf("    ... found subject\n");
                    break;
                case MailingCommon.comments:
                    mailText = attr.decodeData(STRING::make);
                    dlogf("    ... found comments\n");
                    break;
                case FilingCommon.atIsDirectory:
                    xisFolderAttachment = attr.getAsBoolean();
                    dlogf("    ... found isDirectory\n");
                    break;
            }
        } catch (EndOfMessageException e) {
            // invalid mail file content??
            retlog.apply("[unable to extract attribute mail file content]");
            return;
        }
    }
    dlogf("  ... done extracting mail content data\n");
    if (xfrom == null || xfrom.size() == 0 || xto == null || xto.size() == 0) {
        retlog.apply("[missing one of from, to]");
        return;
    }
    // extract the relevant fields from the (old format) mail envelope
    dlogf("  ... loading old-format envelope ... ");
    ByteArrayOutputStream envBos = new ByteArrayOutputStream();
    try {
        me.transferPostboxEnvelope(istream -> {
            byte[] buffer = new byte[512];
            try {
                int count = istream.read(buffer);
                while (count > 0) {
                    envBos.write(buffer, 0, count);
                    count = istream.read(buffer);
                }
            } catch (IOException e) {
            // ignore that for now
            }
        });
    } catch (IOException e) {
        // we must at least be able to read the mail envelope, so...
        retlog.apply("[unable to read mail envelope]");
        return;
    }
    byte[] oldEnvelopeBytes = envBos.toByteArray();
    dlogf("ok -> %d bytes\n", oldEnvelopeBytes.length);
    dlogf("  ... deserializing old-format envelope ... ");
    EncodedList oldEnvelope = EncodedList.make();
    try {
        oldEnvelope.deserialize(new ByteArrayWireInputStream(oldEnvelopeBytes));
    } catch (EndOfMessageException e) {
        // invalid mail file envelope??
        retlog.apply("[invalid mail file envelope]");
        return;
    }
    dlogf("ok\n");
    StringBuilder envSb = new StringBuilder();
    oldEnvelope.append(envSb, "  ", "old-envelope");
    dlogf("+++ %s\n", envSb.toString());
    dlogf("  ... extracting mail-id ... ");
    MessageID xmailId = null;
    for (int i = 0; i < oldEnvelope.size(); i++) {
        Attribute attr = oldEnvelope.get(i);
        if (attr.type.get() == MailingCommon.atMtMessageID) {
            try {
                xmailId = attr.decodeData(MessageID::make);
            } catch (EndOfMessageException e) {
                // invalid mail file envelope??
                retlog.apply("[cannot decode messageId from mail file envelope]");
                return;
            }
        }
    }
    if (xmailId == null) {
        // invalid mail file envelope??
        retlog.apply("[invalid mail file envelope]");
        return;
    }
    dlogf("ok\n");
    MessageID mailId = xmailId;
    // synthesize a new format envelope from the old format mail data
    dlogf("  ... creating new-format envelope\n");
    NameList from = xfrom;
    NameList to = xto;
    NameList cc = xcc;
    NameList replyTo = xreplyTo;
    STRING subject = (xsubject == null) ? STRING.make() : xsubject;
    boolean isFolderAttachment = xisFolderAttachment;
    // long mailCreatedOn = me.inboxEntry().getCreatedOn();
    SEQUENCE<Attribute> newEnvelope = new SEQUENCE<>(Attribute::make);
    newEnvelope.add().set(MailTransport5.atSenderAndDate, ThreePartNameWithTagAndDateString::make, v -> {
        v.nameWithTag.name.from(from.get(0));
        // check where this thing possibly re-appears in GlobalView and if so implement as non-constant
        v.date.set("24-Jan-94 21:40:54");
    });
    newEnvelope.add().set(MailTransport5.atSenderA, ThreePartNameWithTag::make, v -> {
        v.name.from(from.get(0));
    });
    newEnvelope.add().set(MailTransport5.atSenderB, ListOfThreePartNameWithTag::make, v -> {
        copyNames(from, v);
    });
    newEnvelope.add().set(MailTransport5.atTo, ListOfThreePartNameWithTag::make, v -> {
        copyNames(to, v);
    });
    if (cc != null && cc.size() > 0) {
        newEnvelope.add().set(MailTransport5.atCopiesTo, ListOfThreePartNameWithTag::make, v -> {
            copyNames(cc, v);
        });
    }
    if (replyTo != null && replyTo.size() > 0) {
        newEnvelope.add().set(MailTransport5.atReplyTo, ListOfThreePartNameWithTag::make, v -> {
            copyNames(replyTo, v);
        });
    }
    newEnvelope.add().set(MailTransport5.atSubject, STRING::make, v -> v.set(subject));
    dlogf("  ... done\n");
    // prepare the temp object holding the contents data to be delivered later
    // and create the serialized data for later use
    MailData mailData = new MailData(((long) session.getSessionId() & 0x0000_0000_FFFF_FFFFL) + index, index);
    dlogf("  ... serializing new-format envelope ...");
    WireWriter wireWriter = new WireWriter();
    try {
        newEnvelope.serialize(wireWriter);
    } catch (NoMoreWriteSpaceException e) {
    // should never happen => ignore
    }
    mailData.envelopeBytes = wireWriter.getBytes();
    dlogf("ok, %d bytes\n", mailData.envelopeBytes.length);
    if (mailText != null && mailText.get() != null) {
        dlogf("  ... creating mail-text (comments) bytes ... ");
        wireWriter = new WireWriter();
        try {
            mailText.serialize(wireWriter);
        } catch (NoMoreWriteSpaceException e) {
        // should never happen => ignore
        }
        // skip the length word
        byte[] mailtextBytes = wireWriter.getBytes(2);
        if (mailtextBytes.length > 0) {
            if (mailtextBytes[mailtextBytes.length - 1] == 0x00) {
                mailtextBytes[mailtextBytes.length - 1] = ' ';
            }
            mailData.mailtextBytes = mailtextBytes;
            dlogf("ok, %d bytes\n", mailData.mailtextBytes.length);
        } else {
            dlogf(" empty mail-text, mail part NOT created\n");
        }
    }
    if (mailContentFile.file.content.data.size() > 0 || xisFolderAttachment) {
        mailData.attachmentBytes = mailContentBytes;
        dlogf("  ... added mail attachment, %d bytes\n", mailData.attachmentBytes.length);
    }
    // prepare the mailInfo-Attributes in the return object
    dlogf("  ... building mail-infos for return data\n");
    dlogf("    ... miMailServer ... ");
    results.mailInfos.value.add().set(Inbasket2.miMailServer, MiMailServer::make, v -> {
        v.name.from(mailService.getServiceName());
        // obscure ...
        v.unknown1.set(0xA926);
        // ... constants?
        v.unknown2.set(0xEB6D);
    });
    dlogf("ok\n");
    dlogf("    ... miMessageId ... ");
    results.mailInfos.value.add().set(Inbasket2.miMessageId, MailingCommon.MessageID::make, v -> {
        for (int i = 0; i < 5; i++) {
            v.get(i).set(mailId.get(i).get());
        }
    });
    dlogf("ok\n");
    dlogf("    ... miWhatever ... ");
    results.mailInfos.value.add().set(Inbasket2.miWhatever, MiWhatever::make, v -> {
        v.value.set(4);
    });
    dlogf("ok\n");
    dlogf("    ... miMailparts ... ");
    long[] lengthSum = { 0 };
    results.mailInfos.value.add().set(Inbasket2.miMailparts, MiMailParts::make, v -> {
        // envelope (always present)
        MiMailPart envPart = v.add();
        envPart.mailPartType.set(MailTransport5.mptEnvelope);
        envPart.mailPartLength.set(mailData.envelopeBytes.length);
        lengthSum[0] = mailData.envelopeBytes.length;
        mailData.parts.add(mailData.envelopeBytes);
        dlogf("envelope ");
        // having mail-text?
        if (mailData.mailtextBytes != null) {
            MiMailPart mailTextPart = v.add();
            mailTextPart.mailPartType.set(MailTransport5.mptNoteGV);
            mailTextPart.mailPartLength.set(mailData.mailtextBytes.length);
            lengthSum[0] += mailData.mailtextBytes.length;
            mailData.parts.add(mailData.mailtextBytes);
            dlogf("noteGV ");
        }
        // having mail-attachment?
        if (mailData.attachmentBytes != null) {
            MiMailPart mailTextPart = v.add();
            mailTextPart.mailPartType.set(isFolderAttachment ? MailTransport5.mptAttachmentFolder : 4);
            mailTextPart.mailPartLength.set(mailData.attachmentBytes.length);
            lengthSum[0] += mailData.attachmentBytes.length;
            mailData.parts.add(mailData.attachmentBytes);
            dlogf("attachment(%d) ", mailTextPart.mailPartType.get());
        }
    });
    dlogf("... ok\n");
    dlogf("    ... miTotalPartsLength ... ");
    results.mailInfos.value.add().set(Inbasket2.miTotalPartsLength, MiTotalPartsLength::make, v -> {
        v.totalLength.set(lengthSum[0]);
    });
    dlogf("ok\n");
    dlogf("    ... miUser0 ... ");
    results.mailInfos.value.add().set(Inbasket2.miSender0, MiSender::make, v -> {
        v.senderName.from(from.get(0));
    });
    dlogf("ok\n");
    dlogf("    ... miUser1 ... ");
    results.mailInfos.value.add().set(Inbasket2.miSender1, MiSender::make, v -> {
        v.senderName.from(from.get(0));
    });
    dlogf("ok\n");
    dlogf("  ... done\n");
    // fill the obscure array values in the return data
    dlogf("  ... adding obscure array ... ");
    if (mailData.mailtextBytes != null && mailData.attachmentBytes != null) {
        // all 3 mail parts
        int[] vals = { 0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0000, 0x0001, 0x0001 };
        for (int i = 0; i < vals.length; i++) {
            results.unknownSeq.add().set(vals[i]);
        }
    } else if (mailData.mailtextBytes == null && mailData.attachmentBytes == null) {
        // only 1 mail part (envelope)
        int[] vals = { 0x0000, 0x0001, 0x0000, 0x0000 };
        for (int i = 0; i < vals.length; i++) {
            results.unknownSeq.add().set(vals[i]);
        }
    } else {
        // 2 mail parts (envelope and mail-text or attachment)
        int[] vals = { 0x0000, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 };
        for (int i = 0; i < vals.length; i++) {
            results.unknownSeq.add().set(vals[i]);
        }
    }
    dlogf("%d words\n", results.unknownSeq.size());
    // done: remember the data container with the mail-parts for later transfer and add the id to the returned data
    session.setClientData(mailData);
    results.uniqueMailNo.set(mailData.mailTransferId);
    dlogf("  ... uniqueMailNo: 0x%08X\n", results.uniqueMailNo.get());
    // log outgoing data
    retlog.apply(null);
}
Also used : SEQUENCE(dev.hawala.xns.level3.courier.SEQUENCE) PostMailPartParams(dev.hawala.xns.level4.mailing.MailTransport5.PostMailPartParams) NameList(dev.hawala.xns.level4.mailing.MailingCommon.NameList) ServerPollResults(dev.hawala.xns.level4.mailing.MailTransport5.ServerPollResults) Time(dev.hawala.xns.level4.common.Time2.Time) MiSender(dev.hawala.xns.level4.mailing.Inbasket2.MiSender) ThreePartNameWithTagAndDateString(dev.hawala.xns.level4.mailing.MailTransport5.ThreePartNameWithTagAndDateString) AccessProblem(dev.hawala.xns.level4.mailing.MailingCommon.AccessProblem) UnaryOperator(java.util.function.UnaryOperator) EncodedList(dev.hawala.xns.level4.mailing.MailingCommon.EncodedList) Map(java.util.Map) MiTotalPartsLength(dev.hawala.xns.level4.mailing.Inbasket2.MiTotalPartsLength) ServiceProblem(dev.hawala.xns.level4.mailing.MailingCommon.ServiceProblem) GetNextMailResults(dev.hawala.xns.level4.mailing.Inbasket2.GetNextMailResults) InbasketPollResults(dev.hawala.xns.level4.mailing.Inbasket2.InbasketPollResults) Credentials(dev.hawala.xns.level4.common.AuthChsCommon.Credentials) ErrorRECORD(dev.hawala.xns.level3.courier.ErrorRECORD) ListOfThreePartNameWithTag(dev.hawala.xns.level4.mailing.MailTransport5.ListOfThreePartNameWithTag) Attribute(dev.hawala.xns.level4.filing.FilingCommon.Attribute) LogonParams(dev.hawala.xns.level4.mailing.Inbasket2.LogonParams) NoMoreWriteSpaceException(dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException) PostBeginResults(dev.hawala.xns.level4.mailing.MailTransport5.PostBeginResults) dev.hawala.xns.level3.courier.iWireData(dev.hawala.xns.level3.courier.iWireData) EndOfMessageException(dev.hawala.xns.level3.courier.iWireStream.EndOfMessageException) HandleMailPartsParams(dev.hawala.xns.level4.mailing.Inbasket2.HandleMailPartsParams) List(java.util.List) CredentialsType(dev.hawala.xns.level4.common.AuthChsCommon.CredentialsType) ChsDatabase(dev.hawala.xns.level4.common.ChsDatabase) PostEndResults(dev.hawala.xns.level4.mailing.MailTransport5.PostEndResults) CourierRegistry(dev.hawala.xns.level3.courier.CourierRegistry) RECORD(dev.hawala.xns.level3.courier.RECORD) ServiceErrorRecord(dev.hawala.xns.level4.mailing.MailTransport4.ServiceErrorRecord) PostBeginParams(dev.hawala.xns.level4.mailing.MailTransport5.PostBeginParams) SerializedFile(dev.hawala.xns.level4.filing.FilingCommon.SerializedFile) dev.hawala.xns.level4.filing.fs.iContentSource(dev.hawala.xns.level4.filing.fs.iContentSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) dev.hawala.xns.level3.courier.iWireStream(dev.hawala.xns.level3.courier.iWireStream) StrongVerifier(dev.hawala.xns.level4.common.AuthChsCommon.StrongVerifier) LogoffParams(dev.hawala.xns.level4.mailing.Inbasket2.LogoffParams) MiWhatever(dev.hawala.xns.level4.mailing.Inbasket2.MiWhatever) HandleMailPartsResults(dev.hawala.xns.level4.mailing.Inbasket2.HandleMailPartsResults) HashMap(java.util.HashMap) StrongAuthUtils(dev.hawala.xns.level4.common.StrongAuthUtils) AccessErrorRecord(dev.hawala.xns.level4.mailing.MailingCommon.AccessErrorRecord) ByteContentSink(dev.hawala.xns.level4.filing.ByteContentSink) STRING(dev.hawala.xns.level3.courier.STRING) ArrayList(java.util.ArrayList) WireBaseStream(dev.hawala.xns.level3.courier.WireBaseStream) Name(dev.hawala.xns.level4.common.AuthChsCommon.Name) MiMailParts(dev.hawala.xns.level4.mailing.Inbasket2.MiMailParts) WireWriter(dev.hawala.xns.level3.courier.WireWriter) GetMailPartParams(dev.hawala.xns.level4.mailing.Inbasket2.GetMailPartParams) GetNextMailParams(dev.hawala.xns.level4.mailing.Inbasket2.GetNextMailParams) MiMailServer(dev.hawala.xns.level4.mailing.Inbasket2.MiMailServer) LogonResults(dev.hawala.xns.level4.mailing.Inbasket2.LogonResults) Verifier(dev.hawala.xns.level4.common.AuthChsCommon.Verifier) PostEndParams(dev.hawala.xns.level4.mailing.MailTransport5.PostEndParams) IOException(java.io.IOException) BulkData1(dev.hawala.xns.level4.common.BulkData1) FilingCommon(dev.hawala.xns.level4.filing.FilingCommon) InbasketPollParams(dev.hawala.xns.level4.mailing.Inbasket2.InbasketPollParams) ByteContentSource(dev.hawala.xns.level4.filing.ByteContentSource) MessageID(dev.hawala.xns.level4.mailing.MailingCommon.MessageID) MiMailPart(dev.hawala.xns.level4.mailing.Inbasket2.MiMailPart) ThreePartName(dev.hawala.xns.level4.common.AuthChsCommon.ThreePartName) ThreePartNameWithTag(dev.hawala.xns.level4.mailing.MailTransport5.ThreePartNameWithTag) State(dev.hawala.xns.level4.mailing.Inbasket1.State) MiMailPart(dev.hawala.xns.level4.mailing.Inbasket2.MiMailPart) SerializedFile(dev.hawala.xns.level4.filing.FilingCommon.SerializedFile) Attribute(dev.hawala.xns.level4.filing.FilingCommon.Attribute) ThreePartNameWithTagAndDateString(dev.hawala.xns.level4.mailing.MailTransport5.ThreePartNameWithTagAndDateString) MiTotalPartsLength(dev.hawala.xns.level4.mailing.Inbasket2.MiTotalPartsLength) MiMailServer(dev.hawala.xns.level4.mailing.Inbasket2.MiMailServer) MiMailParts(dev.hawala.xns.level4.mailing.Inbasket2.MiMailParts) EncodedList(dev.hawala.xns.level4.mailing.MailingCommon.EncodedList) SEQUENCE(dev.hawala.xns.level3.courier.SEQUENCE) EndOfMessageException(dev.hawala.xns.level3.courier.iWireStream.EndOfMessageException) MiSender(dev.hawala.xns.level4.mailing.Inbasket2.MiSender) ListOfThreePartNameWithTag(dev.hawala.xns.level4.mailing.MailTransport5.ListOfThreePartNameWithTag) ThreePartNameWithTag(dev.hawala.xns.level4.mailing.MailTransport5.ThreePartNameWithTag) MessageID(dev.hawala.xns.level4.mailing.MailingCommon.MessageID) NoMoreWriteSpaceException(dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException) NameList(dev.hawala.xns.level4.mailing.MailingCommon.NameList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MiWhatever(dev.hawala.xns.level4.mailing.Inbasket2.MiWhatever) ListOfThreePartNameWithTag(dev.hawala.xns.level4.mailing.MailTransport5.ListOfThreePartNameWithTag) WireWriter(dev.hawala.xns.level3.courier.WireWriter) STRING(dev.hawala.xns.level3.courier.STRING) ThreePartNameWithTagAndDateString(dev.hawala.xns.level4.mailing.MailTransport5.ThreePartNameWithTagAndDateString)

Example 3 with WireWriter

use of dev.hawala.xns.level3.courier.WireWriter in project dodo by devhawala.

the class MailingOldImpl method inbasket_logon.

/*
	 * ************************* implementation of Inbasket service procedures
	 */
/*
	 * logon
	 * 	= procedure 5
	 */
private static void inbasket_logon(LogonParams params, LogonResults results) {
    // log ingoing data
    if (logParamsAndResults) {
        StringBuilder sb = new StringBuilder();
        params.append(sb, "  ", "params");
        log("##\n## procedure MailingImpl.inbasket_logon() -- params\n%s\n##\n", sb.toString());
    }
    // check user credentials
    Credentials credentials = params.mbx.creds.credentials;
    Verifier verifier = params.mbx.creds.verifier;
    StrongVerifier decodedVerifier = StrongVerifier.make();
    int[] decodedConversationKey = new int[4];
    // throws an exception on invalid credentials
    mailService.checkCredentials(mailService.getServiceName(), mailService.getMachineId(), credentials, verifier, decodedConversationKey, decodedVerifier);
    // check if the mailbox is available
    Name reqMbxName = params.mbx.name;
    // mbxName.getLcFqn();
    String mailboxFqn = mailService.getChsDatabase().resolveName(reqMbxName);
    if (mailboxFqn == null) {
        new AccessErrorRecord(AccessProblem.noSuchMailbox).raise();
    }
    log("## logon to mailbox: %s\n", mailboxFqn);
    Name mbxName = Name.make();
    mbxName.from(mailboxFqn);
    if (!mailService.hasMailbox(mailboxFqn)) {
        new AccessErrorRecord(AccessProblem.noSuchMailbox).raise();
    }
    // prevent concurrent sessions to the same mailbox if sharing is disallowed
    if (!params.allowSharing.get() && mailService.hasSession(mailboxFqn)) {
        new AccessErrorRecord(AccessProblem.mailboxBusy).raise();
    }
    // start the new session
    long remoteHostId = credentials.remoteHostId.get();
    MailSession session;
    try {
        session = mailService.createSession(mbxName, remoteHostId, decodedConversationKey);
    } catch (IOException ioe) {
        new ServiceErrorRecord(ServiceProblem.serviceUnavailable).raise();
        // prevent the 'session not initialized' compiler warning as it cannot know that .raise() never returns...
        return;
    }
    // build results (verifier encrypted in analogy to FilingImpl, see comments in 'innerLogon')
    int sessionId = session.getSessionId();
    results.session.token.set(sessionId);
    if (credentials.type.get() == CredentialsType.simple) {
        // return the initiators verifier
        results.session.verifier.add().set(verifier.get(0).get());
    } else {
        // create a strong verifier based on the received verifier
        int[] conversationKey = session.getConversationKey();
        if (conversationKey != null && conversationKey.length == 4) {
            // xor-ing values
            // the server machine, not(!) the remoteHostId extracted from the Logon request
            long xorHostId = machineId;
            // left justified machine-id => upper 32 bits
            long rcptTimestampMachineId32Bits = (xorHostId >> 16) & 0xFFFFFFFFL;
            // left justified machine-id => lower 32 bits
            long rcptTicksMachineId32Bits = (xorHostId & 0x0000FFFFL) << 16;
            // new verifier values
            long newTicks = decodedVerifier.ticks.get() + 1;
            long newTimestamp = decodedVerifier.timeStamp.get();
            if (newTicks > 0xFFFFFFFFL) {
                newTicks = 0;
                newTimestamp++;
            }
            // plain (unencrypted) verifier with xor-ed values
            StrongVerifier verfr = StrongVerifier.make();
            verfr.ticks.set(newTicks ^ rcptTicksMachineId32Bits);
            verfr.timeStamp.set(newTimestamp ^ rcptTimestampMachineId32Bits);
            // encrypt verifier and transfer into results
            try {
                WireWriter writer = new WireWriter();
                verfr.serialize(writer);
                int[] sourceBytes = writer.getWords();
                int[] encrypted = StrongAuthUtils.xnsDesEncrypt(conversationKey, sourceBytes);
                for (int i = 0; i < encrypted.length; i++) {
                    results.session.verifier.add().set(encrypted[i]);
                }
            } catch (Exception e) {
                // log and set no verifier => let the invoker decide if acceptable
                log("** !! unable to serialize or encrypt the verifier in logon results: " + e.getMessage());
            }
        }
    }
    results.cacheStatus.set(CacheStatus.invalid);
    // log outgoing data
    if (logParamsAndResults) {
        StringBuilder sb = new StringBuilder();
        results.append(sb, "  ", "results");
        log("##\n## procedure MailingImpl.inbasket_logon() -- results\n%s\n##\n", sb.toString());
    }
}
Also used : AccessErrorRecord(dev.hawala.xns.level4.mailing.MailingCommon.AccessErrorRecord) IOException(java.io.IOException) StrongVerifier(dev.hawala.xns.level4.common.AuthChsCommon.StrongVerifier) Verifier(dev.hawala.xns.level4.common.AuthChsCommon.Verifier) ServiceErrorRecord(dev.hawala.xns.level4.mailing.MailTransport4.ServiceErrorRecord) NoMoreWriteSpaceException(dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException) EndOfMessageException(dev.hawala.xns.level3.courier.iWireStream.EndOfMessageException) IOException(java.io.IOException) Name(dev.hawala.xns.level4.common.AuthChsCommon.Name) UndeliveredName(dev.hawala.xns.level4.mailing.MailingCommon.UndeliveredName) ThreePartName(dev.hawala.xns.level4.common.AuthChsCommon.ThreePartName) StrongVerifier(dev.hawala.xns.level4.common.AuthChsCommon.StrongVerifier) WireWriter(dev.hawala.xns.level3.courier.WireWriter) Credentials(dev.hawala.xns.level4.common.AuthChsCommon.Credentials)

Example 4 with WireWriter

use of dev.hawala.xns.level3.courier.WireWriter in project dodo by devhawala.

the class Authentication2Impl method encryptInto.

private static void encryptInto(byte[] strongPw, iWireData source, SEQUENCE<UNSPECIFIED> target) {
    WireWriter writer = new WireWriter();
    try {
        source.serialize(writer);
        int[] sourceBytes = writer.getWords();
        int[] encrypted = StrongAuthUtils.xnsDesEncrypt(strongPw, sourceBytes);
        for (int i = 0; i < encrypted.length; i++) {
            target.add().set(encrypted[i]);
        }
    } catch (Exception e) {
        // report an "other" error if encrypting fails
        Authentication2.CallErrorRecord err = new Authentication2.CallErrorRecord(CallProblem.other, Which.notApplicable);
        Log.C.printf("Auth2", "Authentication2Impl.encryptInto() -> CallError[other,notApplicable] :: %s\n", e.getMessage());
        err.raise();
    }
}
Also used : WireWriter(dev.hawala.xns.level3.courier.WireWriter) EndOfMessageException(dev.hawala.xns.level3.courier.iWireStream.EndOfMessageException)

Example 5 with WireWriter

use of dev.hawala.xns.level3.courier.WireWriter in project dodo by devhawala.

the class MailingNewImpl method inbasket2_logon.

/*
	 * logon
	 *   = procedure 5
	 */
private static void inbasket2_logon(LogonParams params, LogonResults results) {
    // log ingoing data
    if (logParamsAndResults) {
        StringBuilder sb = new StringBuilder();
        params.append(sb, "  ", "params");
        logf("##\n## procedure MailingNewImpl.inbasket2_logon() -- params\n%s\n##\n", sb.toString());
    }
    // check user credentials
    Credentials credentials = params.credentials;
    Verifier verifier = params.verifier;
    StrongVerifier decodedVerifier = StrongVerifier.make();
    int[] decodedConversationKey = new int[4];
    // throws an exception on invalid credentials
    mailService.checkCredentials(mailService.getServiceName(), mailService.getMachineId(), credentials, verifier, decodedConversationKey, decodedVerifier);
    // check if the mailbox is available
    ThreePartName reqMbxName = params.mailboxName;
    // mbxName.getLcFqn();
    String mailboxFqn = mailService.getChsDatabase().resolveName(reqMbxName);
    if (mailboxFqn == null) {
        new AccessErrorRecord(AccessProblem.noSuchMailbox).raise();
    }
    logf("## logon to mailbox: %s\n", mailboxFqn);
    Name mbxName = Name.make();
    mbxName.from(mailboxFqn);
    if (!mailService.hasMailbox(mailboxFqn)) {
        new AccessErrorRecord(AccessProblem.noSuchMailbox).raise();
    }
    // start the new session
    long remoteHostId = credentials.remoteHostId.get();
    MailSession session;
    try {
        session = mailService.createSession(mbxName, remoteHostId, decodedConversationKey);
    } catch (IOException ioe) {
        new ServiceErrorRecord(ServiceProblem.serviceUnavailable).raise();
        // prevent the 'session not initialized' compiler warning as it cannot know that .raise() never returns...
        return;
    }
    // build results (verifier encrypted in analogy to FilingImpl, see comments in 'innerLogon')
    int sessionId = session.getSessionId();
    results.sessionId.set(sessionId);
    if (credentials.type.get() == CredentialsType.simple) {
        // return the initiators verifier
        results.verifier.add().set(verifier.get(0).get());
    } else {
        // create a strong verifier based on the received verifier
        int[] conversationKey = session.getConversationKey();
        if (conversationKey != null && conversationKey.length == 4) {
            // xor-ing values
            // the server machine, not(!) the remoteHostId extracted from the Logon request
            long xorHostId = machineId;
            // left justified machine-id => upper 32 bits
            long rcptTimestampMachineId32Bits = (xorHostId >> 16) & 0xFFFFFFFFL;
            // left justified machine-id => lower 32 bits
            long rcptTicksMachineId32Bits = (xorHostId & 0x0000FFFFL) << 16;
            // new verifier values
            long newTicks = decodedVerifier.ticks.get() + 1;
            long newTimestamp = decodedVerifier.timeStamp.get();
            if (newTicks > 0xFFFFFFFFL) {
                newTicks = 0;
                newTimestamp++;
            }
            // plain (unencrypted) verifier with xor-ed values
            StrongVerifier verfr = StrongVerifier.make();
            verfr.ticks.set(newTicks ^ rcptTicksMachineId32Bits);
            verfr.timeStamp.set(newTimestamp ^ rcptTimestampMachineId32Bits);
            // encrypt verifier and transfer into results
            try {
                WireWriter writer = new WireWriter();
                verfr.serialize(writer);
                int[] sourceBytes = writer.getWords();
                int[] encrypted = StrongAuthUtils.xnsDesEncrypt(conversationKey, sourceBytes);
                for (int i = 0; i < encrypted.length; i++) {
                    results.verifier.add().set(encrypted[i]);
                }
            } catch (Exception e) {
                // log and set no verifier => let the invoker decide if acceptable
                logf("** !! unable to serialize or encrypt the verifier in logon results: " + e.getMessage());
            }
        }
    }
    // fill the mail server's machine-id
    results.machineId.set(machineId);
    // fill in the mailbox counts
    // State pollState = State.make();
    // mailService.getMailboxState(mbxName, pollState);
    // results.lastIndex.set(pollState.lastIndex.get());
    // results.newCount.set(pollState.newCount.get());
    results.lastIndex.set(session.getMailCount());
    results.newCount.set(session.getMailCount());
    // log outgoing data
    if (logParamsAndResults) {
        StringBuilder sb = new StringBuilder();
        results.append(sb, "  ", "results");
        logf("##\n## procedure MailingNewImpl.inbasket2_logon() -- results\n%s\n##\n", sb.toString());
    }
}
Also used : AccessErrorRecord(dev.hawala.xns.level4.mailing.MailingCommon.AccessErrorRecord) ThreePartNameWithTagAndDateString(dev.hawala.xns.level4.mailing.MailTransport5.ThreePartNameWithTagAndDateString) IOException(java.io.IOException) StrongVerifier(dev.hawala.xns.level4.common.AuthChsCommon.StrongVerifier) Verifier(dev.hawala.xns.level4.common.AuthChsCommon.Verifier) ServiceErrorRecord(dev.hawala.xns.level4.mailing.MailTransport4.ServiceErrorRecord) NoMoreWriteSpaceException(dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException) EndOfMessageException(dev.hawala.xns.level3.courier.iWireStream.EndOfMessageException) IOException(java.io.IOException) Name(dev.hawala.xns.level4.common.AuthChsCommon.Name) ThreePartName(dev.hawala.xns.level4.common.AuthChsCommon.ThreePartName) StrongVerifier(dev.hawala.xns.level4.common.AuthChsCommon.StrongVerifier) WireWriter(dev.hawala.xns.level3.courier.WireWriter) ThreePartName(dev.hawala.xns.level4.common.AuthChsCommon.ThreePartName) Credentials(dev.hawala.xns.level4.common.AuthChsCommon.Credentials)

Aggregations

WireWriter (dev.hawala.xns.level3.courier.WireWriter)6 EndOfMessageException (dev.hawala.xns.level3.courier.iWireStream.EndOfMessageException)6 NoMoreWriteSpaceException (dev.hawala.xns.level3.courier.iWireStream.NoMoreWriteSpaceException)5 StrongVerifier (dev.hawala.xns.level4.common.AuthChsCommon.StrongVerifier)5 IOException (java.io.IOException)5 Credentials (dev.hawala.xns.level4.common.AuthChsCommon.Credentials)4 ThreePartName (dev.hawala.xns.level4.common.AuthChsCommon.ThreePartName)4 Verifier (dev.hawala.xns.level4.common.AuthChsCommon.Verifier)4 Name (dev.hawala.xns.level4.common.AuthChsCommon.Name)3 ServiceErrorRecord (dev.hawala.xns.level4.mailing.MailTransport4.ServiceErrorRecord)3 AccessErrorRecord (dev.hawala.xns.level4.mailing.MailingCommon.AccessErrorRecord)3 CourierRegistry (dev.hawala.xns.level3.courier.CourierRegistry)1 ErrorRECORD (dev.hawala.xns.level3.courier.ErrorRECORD)1 RECORD (dev.hawala.xns.level3.courier.RECORD)1 SEQUENCE (dev.hawala.xns.level3.courier.SEQUENCE)1 STRING (dev.hawala.xns.level3.courier.STRING)1 WireBaseStream (dev.hawala.xns.level3.courier.WireBaseStream)1 CourierException (dev.hawala.xns.level3.courier.exception.CourierException)1 dev.hawala.xns.level3.courier.iWireData (dev.hawala.xns.level3.courier.iWireData)1 dev.hawala.xns.level3.courier.iWireStream (dev.hawala.xns.level3.courier.iWireStream)1