public void LoadMyCertificate()
        {
            using (new OperationContextScope(raProxy.GetChannel()))
            {
                MessageHeader aMessageHeader = MessageHeader.CreateHeader("UserName", "", ServiceName);
                OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);

                X509Certificate2 retCert = null;
                CertificateDto   certDto = null;
                certDto = raProxy.RegisterClient(HostAddress);
                retCert = certDto.GetCert();

                myCertificate = retCert;
            }
        }
        /// <summary>
        /// Save specified certificate to backup
        /// </summary>
        /// <param name="certDto">Certificate</param>
        /// <returns></returns>
        public bool SaveCertificateToBackupDisc(CertificateDto certDto)
        {
            if (!IsUserAccessGranted(WindowsIdentity.GetCurrent().Name))
            {
                Audit.WriteEvent("User '" + WindowsIdentity.GetCurrent().Name + "' had denied access for method SaveCertificateToBackupDisc", EventLogEntryType.FailureAudit);
            }

            X509Certificate2 certificate = certDto.GetCert();
            activeCertificates.Add(certificate);
            CertificateHandler.ExportToFileSystem(X509ContentType.Pfx, certificate, certificate.SubjectName.Name);

            string logMessage = "Certificate with subject name '" + certificate.SubjectName.Name + "' is saved on backup server.'";
            Audit.WriteEvent(logMessage, EventLogEntryType.Information);

            return true;
        }
        public CertificateDto SendCert(CertificateDto certDto)
        {
            if (!vaProxy.isCertificateValidate(certDto.GetCert(false)))
            {
                return(null);
            }
            IClientContract otherSide       = OperationContext.Current.GetCallbackChannel <IClientContract>();
            string          callbackSession = otherSide.GetSessionId();
            string          proxySession    = OperationContext.Current.SessionId;

            SessionData newSd = new SessionData(null, otherSide, callbackSession, proxySession);

            newSd.Address = string.Format("temp{0}", tempSessionNum++);
            clientSessions.Add(newSd.Address, newSd);

            return(new CertificateDto(myCertificate, false));
        }
        public static void testCAProxy()
        {
            CertificateDto certDto = null;

            Console.WriteLine("Test of using CAProxy in RA started...");
            RegistrationAuthorityService service = new RegistrationAuthorityService();

            certDto = service.RegisterClient("testClient");
            Console.WriteLine("Test of using CAProxy in RA finished. Name of new certificate - " + ((certDto.GetCert() != null) ? certDto.GetCert().SubjectName.ToString() : "registration not implemented"));
        }
        public void StartComunication(string address)
        {
            if (this.HostAddress.Equals(address))
            {
                return;
            }
            if (clientSessions.ContainsKey(address))
            {
                PrintMessage.Print(string.Format("You are already connected to client: {0}", address));
                return;
            }

            NetTcpBinding binding = new NetTcpBinding();

            binding.SendTimeout    = new TimeSpan(0, 5, 5);
            binding.ReceiveTimeout = new TimeSpan(0, 5, 5);
            binding.OpenTimeout    = new TimeSpan(0, 5, 5);
            binding.CloseTimeout   = new TimeSpan(0, 5, 5);
            IClientContract serverProxy = new ClientProxy(new EndpointAddress(address), binding, this);

            byte[]      sessionKey = RandomGenerateKey();
            SessionData sd         = new SessionData()
            {
                AesAlgorithm = new AES128_ECB(sessionKey), Proxy = serverProxy, Address = address
            };

            CertificateDto serverCert = serverProxy.SendCert(new CertificateDto(myCertificate, false));

            if (!vaProxy.isCertificateValidate(serverCert.GetCert(false)))
            {
                PrintMessage.Print("Starting communication failed!");
                return;
            }

            byte[] encryptedSessionKey = null;
            try
            {
                RSACryptoServiceProvider publicKey = (RSACryptoServiceProvider)serverCert.GetCert(false).PublicKey.Key;

                if (publicKey != null)
                {
                    encryptedSessionKey = publicKey.Encrypt(sessionKey, true);
                }
                else
                {
                    PrintMessage.Print("Error, public key is null");
                    return;
                }
            }
            catch (Exception e)
            {
                PrintMessage.Print(string.Format("Error: {0}", e.Message));
            }
            bool success = serverProxy.SendKey(encryptedSessionKey);

            if (success)
            {
                sqliteWrapper.InsertToTable(sd.Address);

                object sessionInfo = serverProxy.GetSessionInfo(HostAddress);
                if (sessionInfo != null)
                {
                    string   sessionId      = System.Text.Encoding.UTF8.GetString(sd.AesAlgorithm.Decrypt((byte[])sessionInfo)).Trim();
                    string[] sessionIdSplit = sessionId.Split('|');
                    sd.CallbackSessionId = sessionIdSplit[0];
                    sd.ProxySessionId    = sessionIdSplit[1];
                    lock (objLock)
                    {
                        clientSessions.Add(sd.Address, sd);
                        PrintMessage.Print("Session is opened");
                    }
                }
            }
            else
            {
                PrintMessage.Print("Starting communication failed!");
            }
        }