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(); } }
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); }
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); }
void initializeAssociation() { string assoc_type = Util.GetRequiredArg(Args, Protocol.openidnp.assoc_type); if (Array.IndexOf(Protocol.Args.SignatureAlgorithm.All, assoc_type) >= 0) { byte[] secret; string session_type; if (!Args.TryGetValue(Protocol.openidnp.session_type, out session_type) || Protocol.Args.SessionType.NoEncryption.Equals(session_type, StringComparison.Ordinal)) { secret = getDecoded(Protocol.openidnp.mac_key); } else { try { byte[] dh_server_public = getDecoded(Protocol.openidnp.dh_server_public); byte[] enc_mac_key = getDecoded(Protocol.openidnp.enc_mac_key); secret = DiffieHellmanUtil.SHAHashXorSecret(DiffieHellmanUtil.Lookup(Protocol, session_type), DH, dh_server_public, enc_mac_key); } catch (ArgumentException ex) { throw new OpenIdException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidOpenIdQueryParameterValue, Protocol.openid.session_type, session_type), ex); } } string assocHandle = Util.GetRequiredArg(Args, Protocol.openidnp.assoc_handle); TimeSpan expiresIn = new TimeSpan(0, 0, Convert.ToInt32(Util.GetRequiredArg(Args, Protocol.openidnp.expires_in), CultureInfo.InvariantCulture)); try { Association = HmacShaAssociation.Create(Protocol, assoc_type, assocHandle, secret, expiresIn); } catch (ArgumentException ex) { throw new OpenIdException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidOpenIdQueryParameterValue, Protocol.openid.assoc_type, assoc_type), ex); } } else { throw new OpenIdException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidOpenIdQueryParameterValue, Protocol.openid.assoc_type, assoc_type)); } }
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)); }