Exemplo n.º 1
0
        /// <summary> Initiator API
        /// Receive Challenge, respond with a hash.
        /// </summary>
        //UPGRADE_NOTE: Synchronized keyword was removed from method 'receiveChallenge'. Lock expression was added. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1027"'
        internal virtual void receiveChallenge(Blob blob)
        {
            lock (this)
            {
                log.debug("OTP Authenticator received Challenge");

                // If we're initiating, the last state we should
                // have gotten to was STATE_STARTED
                if (state != STATE_STARTED)
                {
                    abortNoThrow(ERR_OTP_STATE);
                }

                if (blob.Status.Equals(Blob.ABORT))
                {
                    abort(ERR_PEER_ABORTED);
                }
                System.String challenge = blob.Data;

                // Parse Challenge, provide response
                state = STATE_CHALLENGE;

                int sequence = 0;
                System.String seed = null, algo = null;

                if (log.isDebugEnabled())
                {
                    log.debug("Tokenizing=>" + challenge);
                }

                SupportClass.Tokenizer st = new SupportClass.Tokenizer(challenge);

                if (st.Count != 4)
                {
                    abort("Failed to understand server's Challenge" + st.Count);
                }

                algo = st.NextToken();
                algorithm = SASLOTPProfile.getAlgorithm(algo);

                if (algorithm == null)
                {
                    abort("Unrecognized algorithm in server challenge");
                }

                sequence = System.Int32.Parse(st.NextToken());
                seed = st.NextToken().ToLower();
                if (!OTPGenerator.validateSeed(seed))
                    abort("Invalid Seed");

                if (log.isDebugEnabled())
                {
                    log.debug("Algo is=>" + algo + " seed is=>" + seed + " seq=>" + sequence);
                }

                System.String phrase = new System.Text.StringBuilder(seed + password).ToString();
                password = null;
                sbyte[] response = null, temp;

                temp = SupportClass.ToSByteArray(SupportClass.ToByteArray(phrase));

                for (int i = 0; i < sequence; i++)
                {
                    response = algorithm.generateHash(temp);
                    temp = response;
                }

                if (log.isDebugEnabled())
                {
                    log.debug(SASLOTPProfile.convertBytesToHex(temp));
                }
                long l = profile.convertBytesToLong(temp);
                phrase = new System.Text.StringBuilder(WORD + OTPDictionary.convertHashToWords(l)).ToString();

                if (log.isDebugEnabled())
                {
                    log.debug("Prelim response is =>" + phrase + "<=");
                }

                // IF this is an init request
                if ((System.Object) initData != null)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder(128);
                    sb.Append(SASLOTPProfile.HEX_INIT);
                    sb.Append(SASLOTPProfile.convertBytesToHex(temp));
                    sb.Append(initData);
                    phrase = sb.ToString();
                    if (log.isDebugEnabled())
                    {
                        log.debug("Produced INIT response of " + phrase);
                    }
                }
                try
                {
                    blob = new Blob(Blob.STATUS_CONTINUE, phrase);
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1043"'
                    channel.sendMSG(new StringOutputDataStream(blob.ToString()), this);
                }
                catch (BEEPException x)
                {
                    throw new SASLException("Unable to send response to challenge");
                }
            }
        }