// **** // **** SERVER-ONLY CALLED METHODS // **** /// <summary> /// Attempts to register the client with the server. /// </summary> /// <param name="data">Information related to registration.</param> /// <param name="newRegisterableClientConfiguration">A <see cref="IClientConfiguration"/> for the newly registered client.</param> /// <returns>A response relative to the registration process.</returns> public byte[] TryAddDataFromRegistrationChannel(byte[] data, out IClientConfiguration newRegisterableClientConfiguration) { if ((data == null) || (data.Length == 0)) { throw new ArgumentNullException("data"); } newRegisterableClientConfiguration = null; var transportConfigurationConvertor = new dodSON.Core.Converters.TypeSerializer <ITransportConfiguration>(); var transportDataConverter = new dodSON.Core.Converters.TypeSerializer <TransportData>(); var listConverter = new dodSON.Core.Converters.TypeSerializer <List <byte[]> >(); var boolConverter = new dodSON.Core.Converters.TypeSerializer <bool>(); var clientConfigurationConverter = new dodSON.Core.Converters.TypeSerializer <IClientConfiguration>(); // var request = transportDataConverter.FromByteArray(data); if (request.Alpha == ContextFor("Hello")) { // ######## create tunnel // create public/private keys var KeyGenerator = AsymmetricCryptoProvider; var serverPublicKey = KeyGenerator.ToXmlString(false); var serverPrivateKey = KeyGenerator.ToXmlString(true); // clear some bullshit here, I do not understand this, it refuses to register clients whom have, in theory, already unregistered... if (_RegistrationCandidates.ContainsKey(request.Beta)) { _RegistrationCandidates.Remove(request.Beta); } // add candidate _RegistrationCandidates.Add(request.Beta, new ServerRegistrationCandidate() { ClientId = request.Beta, ClientPublicKey = System.Text.Encoding.Unicode.GetString(request.Gamma), CreatedDate = DateTime.Now, ServerPrivateKey = serverPrivateKey, ServerPublicKey = serverPublicKey }); // generate response var response = transportDataConverter.ToByteArray( new TransportData() { Alpha = ContextFor("Hello"), Beta = request.Beta, Gamma = System.Text.Encoding.Unicode.GetBytes(serverPublicKey), Delta = null, Epsilon = null }); return(response); } else if (request.Alpha == ContextFor("Challenge")) { if (_RegistrationCandidates.ContainsKey(request.Beta)) { var candidate = _RegistrationCandidates[request.Beta]; var cryptoProvider = AsymmetricCryptoProvider; cryptoProvider.FromXmlString(candidate.ServerPrivateKey); var challengeEvidence = RestoreFromTransport(request.Epsilon, cryptoProvider); if (ChallengeController.Challenge(challengeEvidence)) { return(boolConverter.ToByteArray(true)); } _RegistrationCandidates.Remove(request.Beta); return(boolConverter.ToByteArray(false)); } return(boolConverter.ToByteArray(false)); } else if (request.Alpha == ContextFor("RequestServerSetup")) { if (_RegistrationCandidates.ContainsKey(request.Beta)) { var candidate = _RegistrationCandidates[request.Beta]; var cryptoProvider = AsymmetricCryptoProvider; cryptoProvider.FromXmlString(candidate.ServerPrivateKey); var challengeEvidence = RestoreFromTransport(request.Epsilon, cryptoProvider); if (ChallengeController.Challenge(challengeEvidence)) { var clientProvider = AsymmetricCryptoProvider; clientProvider.FromXmlString(candidate.ClientPublicKey); // ################ // TODO: Problem ID: dc95c9d1ab4a422cbca3834e1361aa0 : The Problem // The Problem. The TransportConfiguration referenced here is different depending on the ctor used to create it. // Using the Standard Ctors, this reference here is the same as the reference in RegistrationController. // However, using the IConfigurable interface code results in the two references being different objects. // // ######## When building in code, be sure to use the same reference in all types ########. // // Search for the Problem ID: dc95c9d1ab4a422cbca3834e1361aa0 for other elements of this problem. // Current hypothesis is that somehow the referenced registration controller are different in the two places when it should be one. // This problem manifests itself ONLY when the objects are created using the IConfiguration system. // The answer is apparent, the IConfiguration system is creating two of these items. var response = listConverter.ToByteArray(PrepareForTransport(transportConfigurationConvertor.ToByteArray(TransportConfiguration), clientProvider, TransportPartsLengthInBytes)); return(response); } _RegistrationCandidates.Remove(request.Beta); return(null); } return(null); } else if (request.Alpha == ContextFor("Register")) { if (_RegistrationCandidates.ContainsKey(request.Beta)) { var candidate = _RegistrationCandidates[request.Beta]; var cryptoProvider = AsymmetricCryptoProvider; cryptoProvider.FromXmlString(candidate.ServerPrivateKey); var challengeEvidence = RestoreFromTransport(request.Epsilon, cryptoProvider); if (ChallengeController.Challenge(challengeEvidence)) { // register newRegisterableClientConfiguration = clientConfigurationConverter.FromByteArray(RestoreFromTransport(request.Delta, cryptoProvider)); _RegistrationCandidates.Remove(request.Beta); // generate response return(boolConverter.ToByteArray(true)); } _RegistrationCandidates.Remove(request.Beta); return(boolConverter.ToByteArray(false)); } return(boolConverter.ToByteArray(false)); } else if (request.Alpha == ContextFor("Unregister")) { if (_RegistrationCandidates.ContainsKey(request.Beta)) { var candidate = _RegistrationCandidates[request.Beta]; var cryptoProvider = AsymmetricCryptoProvider; cryptoProvider.FromXmlString(candidate.ServerPrivateKey); var challengeEvidence = RestoreFromTransport(request.Epsilon, cryptoProvider); if (ChallengeController.Challenge(challengeEvidence)) { // unregister newRegisterableClientConfiguration = clientConfigurationConverter.FromByteArray(RestoreFromTransport(request.Delta, cryptoProvider)); _RegistrationCandidates.Remove(request.Beta); // generate response return(boolConverter.ToByteArray(true)); } _RegistrationCandidates.Remove(request.Beta); return(boolConverter.ToByteArray(false)); } return(boolConverter.ToByteArray(false)); } return(null); }