Esempio n. 1
0
        public void TestPublic()
        {
            StreamReader sr = new StreamReader(@"..\..\src\DotNetOpenId.Test\dhpriv.txt");

            try {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[]             parts = line.Trim().Split(' ');
                    byte[]               x     = Convert.FromBase64String(parts[0]);
                    DiffieHellmanManaged dh    = new DiffieHellmanManaged(DiffieHellmanUtil.DEFAULT_MOD, DiffieHellmanUtil.DEFAULT_GEN, x);
                    byte[]               pub   = dh.CreateKeyExchange();
                    byte[]               y     = Convert.FromBase64String(parts[1]);

                    if (y[0] == 0 && y[1] <= 127)
                    {
                        y.CopyTo(y, 1);
                    }

                    Assert.AreEqual(y, Convert.FromBase64String(DiffieHellmanUtil.UnsignedToBase64(pub)), line);
                }
            } finally {
                sr.Close();
            }
        }
Esempio n. 2
0
        public override Dictionary <string, string> Answer(byte[] secret)
        {
            byte[] mac_key = DiffieHellmanUtil.SHAHashXorSecret(DiffieHellmanUtil.Lookup(Protocol, SessionType),
                                                                dh, consumerPublicKey, secret);
            var nvc = new Dictionary <string, string>();

            nvc.Add(Protocol.openidnp.dh_server_public, DiffieHellmanUtil.UnsignedToBase64(dh.CreateKeyExchange()));
            nvc.Add(Protocol.openidnp.enc_mac_key, Convert.ToBase64String(mac_key));

            return(nvc);
        }
Esempio n. 3
0
        public static AssociateRequest Create(OpenIdRelyingParty relyingParty, ServiceEndpoint provider, string assoc_type, string session_type, bool allowNoSession)
        {
            if (relyingParty == null)
            {
                throw new ArgumentNullException("relyingParty");
            }
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            if (assoc_type == null)
            {
                throw new ArgumentNullException("assoc_type");
            }
            if (session_type == null)
            {
                throw new ArgumentNullException("session_type");
            }
            Debug.Assert(Array.IndexOf(provider.Protocol.Args.SignatureAlgorithm.All, assoc_type) >= 0);
            Debug.Assert(Array.IndexOf(provider.Protocol.Args.SessionType.All, session_type) >= 0);

            if (!HmacShaAssociation.IsDHSessionCompatible(provider.Protocol, assoc_type, session_type))
            {
                throw new OpenIdException(string.Format(CultureInfo.CurrentCulture,
                                                        Strings.IncompatibleAssociationAndSessionTypes, assoc_type, session_type));
            }

            var      args     = new Dictionary <string, string>();
            Protocol protocol = provider.Protocol;

            args.Add(protocol.openid.mode, protocol.Args.Mode.associate);
            args.Add(protocol.openid.assoc_type, assoc_type);

            DiffieHellman dh = null;

            if (provider.ProviderEndpoint.Scheme == Uri.UriSchemeHttps && allowNoSession)
            {
                Logger.InfoFormat("Requesting association with {0} (assoc_type = '{1}', session_type = '{2}').",
                                  provider.ProviderEndpoint, assoc_type, protocol.Args.SessionType.NoEncryption);
                args.Add(protocol.openid.session_type, protocol.Args.SessionType.NoEncryption);
            }
            else
            {
                Logger.InfoFormat("Requesting association with {0} (assoc_type = '{1}', session_type = '{2}').",
                                  provider.ProviderEndpoint, assoc_type, session_type);

                // Initiate Diffie-Hellman Exchange
                dh = DiffieHellmanUtil.CreateDiffieHellman();

                byte[] dhPublic = dh.CreateKeyExchange();
                string cpub     = DiffieHellmanUtil.UnsignedToBase64(dhPublic);

                args.Add(protocol.openid.session_type, session_type);
                args.Add(protocol.openid.dh_consumer_public, cpub);

                DHParameters dhps = dh.ExportParameters(true);

                if (dhps.P != DiffieHellmanUtil.DEFAULT_MOD || dhps.G != DiffieHellmanUtil.DEFAULT_GEN)
                {
                    args.Add(protocol.openid.dh_modulus, DiffieHellmanUtil.UnsignedToBase64(dhps.P));
                    args.Add(protocol.openid.dh_gen, DiffieHellmanUtil.UnsignedToBase64(dhps.G));
                }
            }

            return(new AssociateRequest(relyingParty, provider, args, dh));
        }