Exemplo n.º 1
0
 protected void TheCipherTextIsDecrypted()
 {
     PlainText = CryptographicService.Decrypt(CipherText);
 }
Exemplo n.º 2
0
        public static async Task <InitializationResult <(X509Certificate, AuthenticatedStream)> > HandleClientRegistrationSafelyAsync(
            Stream clientStream,
            Guid serverGuid,
            IAuthenticatedConnectionFactory authenticatedConnectionFactory,
            ICryptographicService otp,
            CancellationToken token)
        {
            InitializationResult <(X509Certificate, AuthenticatedStream)> From(CommunicationResult res) => From <(X509Certificate, AuthenticatedStream)>(res);

            if (!otp.CanDecrypt)
            {
                throw new ArgumentException("otp needs to be able to decrypt");
            }

            var dataTypeResult = await clientStream.ReceiveInt32SafelyAsync(token);

            if (!dataTypeResult.Successful)
            {
                return(From(dataTypeResult));
            }
            if ((CommunicationData)dataTypeResult.Result != CommunicationData.PublicKey)
            {
                return(new InitializationResult <(X509Certificate, AuthenticatedStream)>
                {
                    Successful = false,
                    Error = new InitializationError
                    {
                        ErrorType = InitializationErrorType.Protocol,
                        Message = $"Client sent unexpected data",
                    },
                });
            }

            var encryptedLengthResult = await clientStream.ReceiveInt32SafelyAsync(token);

            if (!encryptedLengthResult.Successful)
            {
                return(From(encryptedLengthResult));
            }
            var encryptedCertificateResult = await clientStream.ReadSafelyAsync(encryptedLengthResult.Result, token);

            if (!encryptedCertificateResult.Successful)
            {
                return(From(encryptedCertificateResult));
            }
            var clientCertificate = new X509Certificate(otp.Decrypt(encryptedCertificateResult.Result));

            var authenticationResult = await EstablishEncryptedCommunication(true, serverGuid, authenticatedConnectionFactory, clientStream, token);

            if (authenticationResult.Successful)
            {
                return(new InitializationResult <(X509Certificate, AuthenticatedStream)>
                {
                    Successful = true,
                    Result = (clientCertificate, authenticationResult.Result),
                });
            }
            else
            {
                return(InitializationResult <(X509Certificate, AuthenticatedStream)> .From(authenticationResult));
            }
        }