コード例 #1
0
        public static string Test1()
        {
            DiffieHellman dh1 = DiffieHellmanUtil.CreateDiffieHellman();
            DiffieHellman dh2 = DiffieHellmanUtil.CreateDiffieHellman();

            string secret1 = Convert.ToBase64String(dh1.DecryptKeyExchange(dh2.CreateKeyExchange()));
            string secret2 = Convert.ToBase64String(dh2.DecryptKeyExchange(dh1.CreateKeyExchange()));

            Assert.AreEqual(secret1, secret2, "Secret keys do not match for some reason.");

            return(secret1);
        }
コード例 #2
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));
        }