Exemplo n.º 1
0
        /// <summary>
        /// 处理StreamElement
        /// </summary>
        /// <param name="e"></param>
        private void OnStreamElement(Node e)
        {
            if (ConnectionState == XmppConnectionState.Securing ||
                ConnectionState == XmppConnectionState.StartCompression)
            {
                return;
            }

            if (e is Features)
            {
                Features f = e as Features;

                SaslEventArgs args = new SaslEventArgs(f.Mechanisms);

                if (OnSaslStart != null)
                {
                    OnSaslStart(this, args);
                }

                if (args.Auto)
                {
                    if (!Authenticated)
                    {
                        string mechanism = null;
                        if (f.Mechanisms != null)
                        {
                            if (f.Mechanisms.SupportsMechanism(MechanismType.SCRAM_SHA_1))
                            {
                                mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName(MechanismType.SCRAM_SHA_1);
                            }
                            if (f.Mechanisms.SupportsMechanism(MechanismType.DIGEST_MD5))
                            {
                                mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName(MechanismType.DIGEST_MD5);
                            }
                            else if (f.Mechanisms.SupportsMechanism(MechanismType.PLAIN))
                            {
                                mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName(MechanismType.PLAIN);
                            }
                        }
                        args.Mechanism = mechanism;
                        if (String.IsNullOrEmpty(mechanism))
                        {
                            //
                        }
                        else
                        {
                            WSMechanism wsmechanism = MechanismFactory.GetMechanism(mechanism);
                            // Set properties for the SASL mechanism
                            wsmechanism.Username = FullUserName;
                            wsmechanism.Password = Password;
                            wsmechanism.Server   = Server;
                            wsmechanism.AuthStr  = TokenData.access_token;// "YWMtwFZU6n3bEeWPKIERbxAADQAAAVHmL3PeTIwn69ar3nW_uwFcIiqVYO2XgGA";
                            // Call Init Method on the mechanism
                            Send(wsmechanism.BuildeAuthNode());
                        }
                    }
                    else if (!Binded) // 如果没有绑定,则发送绑定消息
                    {
                        if (f.SupportsBind)
                        {
                            DoChangeXmppConnectionState(XmppConnectionState.Binding);

                            BindIq bIq = string.IsNullOrEmpty(Resource) ? new BindIq(IqType.set) : new BindIq(IqType.set, Resource);

                            this.IqGrabber.SendIq(bIq, BindResult, null);
                        }
                    }
                }
            }
            else if (e is Success)
            {
                if (OnSaslEnd != null)
                {
                    OnSaslEnd(this);
                }

                // SASL authentication was successfull
                DoChangeXmppConnectionState(XmppConnectionState.Authenticated);
                Authenticated = true;
                SendOpen();
            }
            else if (e is Failure)
            {
                // Authentication failure
                if (OnAuthError != null)
                {
                    OnAuthError(this, (Failure)e);
                }
            }
            else if (e is Message)
            {
                Message msg  = (Message)e;
                string  body = msg.Body; //msg.Type;
            }
        }
Exemplo n.º 2
0
        public byte[] Sign(byte[] message)
        {
            MechanismFactory mechanismFactory = new MechanismFactory();
            IMechanism       mechanism;

            switch (encryptionAlgorithm)
            {
            case "DSA":
                switch (hashAlgorithm)
                {
                case "SHA1":
                    mechanism = mechanismFactory.Create(CKM.CKM_DSA_SHA1);
                    break;

                case "SHA224":
                    mechanism = mechanismFactory.Create(CKM.CKM_DSA_SHA224);
                    break;

                case "SHA256":
                    mechanism = mechanismFactory.Create(CKM.CKM_DSA_SHA256);
                    break;

                case "SHA384":
                    mechanism = mechanismFactory.Create(CKM.CKM_DSA_SHA384);
                    break;

                case "SHA512":
                    mechanism = mechanismFactory.Create(CKM.CKM_DSA_SHA512);
                    break;

                default:
                    throw new ArgumentException("Not supported: " + hashAlgorithm + "with" + encryptionAlgorithm);
                }
                break;

            case "ECDSA":
                switch (hashAlgorithm)
                {
                case "SHA1":
                    mechanism = mechanismFactory.Create(CKM.CKM_ECDSA_SHA1);
                    break;

                case "SHA224":
                    mechanism = mechanismFactory.Create(CKM.CKM_ECDSA_SHA224);
                    break;

                case "SHA256":
                    mechanism = mechanismFactory.Create(CKM.CKM_ECDSA_SHA256);
                    break;

                case "SHA384":
                    mechanism = mechanismFactory.Create(CKM.CKM_ECDSA_SHA384);
                    break;

                case "SHA512":
                    mechanism = mechanismFactory.Create(CKM.CKM_ECDSA_SHA512);
                    break;

                default:
                    throw new ArgumentException("Not supported: " + hashAlgorithm + "with" + encryptionAlgorithm);
                }
                break;

            case "RSA":
                switch (hashAlgorithm)
                {
                case "SHA1":
                    mechanism = mechanismFactory.Create(CKM.CKM_SHA1_RSA_PKCS);
                    break;

                case "SHA224":
                    mechanism = mechanismFactory.Create(CKM.CKM_SHA224_RSA_PKCS);
                    break;

                case "SHA256":
                    mechanism = mechanismFactory.Create(CKM.CKM_SHA256_RSA_PKCS);
                    break;

                case "SHA384":
                    mechanism = mechanismFactory.Create(CKM.CKM_SHA384_RSA_PKCS);
                    break;

                case "SHA512":
                    mechanism = mechanismFactory.Create(CKM.CKM_SHA512_RSA_PKCS);
                    break;

                default:
                    throw new ArgumentException("Not supported: " + hashAlgorithm + "with" + encryptionAlgorithm);
                }
                break;

            default:
                throw new ArgumentException("Not supported: " + hashAlgorithm + "with" + encryptionAlgorithm);
            }

            return(session.Sign(mechanism, privateKeyHandle, message));
        }