private bool ValidateSignImpl(CryptoFactoryBase cryptoFactory, string cryptoProvider, string hashAlgName, string signAlgName, byte[] publicKey) { var hashAlg = cryptoFactory.CreateHashAlgorithm(cryptoProvider, hashAlgName); var loginAsBytes = Encoding.UTF8.GetBytes(Login); var hashTrue = hashAlg.ComputeHash(loginAsBytes); if (!hashTrue.SequenceEqual(Hash)) { return(false); } var signAlg = cryptoFactory.CreateSignAlgoritm(cryptoProvider, signAlgName); signAlg.Import(publicKey); return(signAlg.VerifySign(hashTrue, Sign)); }
/// <exception cref="ArgumentNullException">cryptoFactory == null. -or- publicKeySign == null.</exception> /// <exception cref="InvalidOperationException">Одно из свойств не было указано.</exception> /// <exception cref="ArgumentOutOfRangeException">Source: <see cref="CryptoFactoryBase.CreateHashAlgorithm(string, string)"/>. -or- /// Source: <see cref="CryptoFactoryBase.CreateSignAlgoritm(string, string)"/>.</exception> /// <exception cref="CryptographicException">Source: <see cref="IAsymmetricKeysExchange.Import(byte[])"/>.</exception> public bool ValidateSign(CryptoFactoryBase cryptoFactory, byte[] publicKeySign) { if (cryptoFactory == null) { throw new ArgumentNullException(nameof(cryptoFactory)) { Source = GetType().AssemblyQualifiedName } } ; if (publicKeySign == null) { throw new ArgumentNullException(nameof(publicKeySign)) { Source = GetType().AssemblyQualifiedName } } ; if (Key == null || CryptoProvider == null || CryptoAlg == null || HashAlg == null || Hash == null || Sign == null) { throw new InvalidOperationException("Одно из свойств не было указано.") { Source = GetType().AssemblyQualifiedName } } ; var hashAlg = cryptoFactory.CreateHashAlgorithm(CryptoProvider, HashAlg); var hashTrue = hashAlg.ComputeHash(Key); if (!hashTrue.SequenceEqual(Hash)) { return(false); } var signAlg = cryptoFactory.CreateSignAlgoritm(CryptoProvider, CryptoAlg); signAlg.Import(publicKeySign); return(signAlg.VerifySign(hashTrue, Sign)); } } }
/// <exception cref="ArgumentNullException">factory == null. -or- signImpl == null.</exception> /// <exception cref="InvalidOperationException">Login == null.</exception> /// <exception cref="ArgumentOutOfRangeException">Source: <see cref="CryptoFactoryBase.CreateHashAlgorithm(string, string)"/></exception> public byte[] CreateSign(CryptoFactoryBase factory, string hashAlgName, string providerName, ISign signImpl, out byte[] hash) { if (factory == null) { throw new ArgumentNullException(nameof(factory)) { Source = GetType().AssemblyQualifiedName } } ; if (signImpl == null) { throw new ArgumentNullException(nameof(signImpl)) { Source = GetType().AssemblyQualifiedName } } ; if (Login == null) { throw new InvalidOperationException("Login == null.") { Source = GetType().AssemblyQualifiedName } } ; try { var hashAlg = factory.CreateHashAlgorithm(providerName, hashAlgName); var loginAsBytes = Encoding.UTF8.GetBytes(Login); hash = hashAlg.ComputeHash(loginAsBytes); var sign = signImpl.SignData(hash); return(sign); } catch (ArgumentOutOfRangeException ex) { //factory.CreateHashAlgorithm(providerName, hashAlgName); throw; } }
//public override UserForm GetPublicKey() //{ // var formNew = new UserForm(); // if (CryptoProvider == CryptoProvider.CngMicrosoft) // { // if (AsymmetricAlgorithm is RSACng) // { // var rsa = (RSACng) AsymmetricAlgorithm; // //var cngKeyBlob = rsa.Key.Export(CngKeyBlobFormat.GenericPublicBlob); // var key = rsa.ExportParameters(false); // var cngKeyBlob = Serializer.Serialize(key, false); // formNew = new UserForm() // { // CryptoProvider = CryptoProvider.CngMicrosoft, // PublicKeyParamsBlob = cngKeyBlob // }; // } // } // return formNew; //} ///// <exception cref="ArgumentNullException">form == null. -или- resultOfOperation == null.</exception> //public override UserForm AuthenticateUser(UserForm form, out IEnumerable<byte[]> messages, // ResultOfOperation resultOfOperation) //{ // if (form == null) // throw new ArgumentNullException(nameof(form)) { Source = GetType().AssemblyQualifiedName }; // if (resultOfOperation == null) // throw new ArgumentNullException(nameof(resultOfOperation)) { Source = GetType().AssemblyQualifiedName }; // messages = new byte[0][]; // var formWasFinded = UsersF.FirstOrDefault((registrationForm => registrationForm.Login.Equals(form.Login))); // var KeyBlob = formWasFinded.SignPublicKeyBlob; // if (KeyBlob == null) // { // resultOfOperation.ErrorMessage = "Пользователь не зарегистрирован."; // resultOfOperation.OperationWasFinishedSuccessful = false; // return null; // } // form.SignPublicKeyBlob = KeyBlob; // if (!form.ValidateSign(false, resultOfOperation)) // return null; // if (OfflineMessagesF.ContainsKey(form.Login)) // messages = OfflineMessagesF[form.Login].MessagesAsBytes.ToArray(); // return form; //} /// <exception cref="ArgumentNullException">form == null. -или- resultOfOperation == null.</exception> public override async Task <Tuple <UserFormSurrogate, OfflineMessagesConcurent> > AuthenticateUserAsync(IAuthenticationForm formAuth, CryptoFactoryBase cryptoFactory, CryptoInfo choosenCrypto, ResultOfOperation resultOfOperation) { if (formAuth == null) { throw new ArgumentNullException(nameof(formAuth)) { Source = GetType().AssemblyQualifiedName } } ; if (resultOfOperation == null) { throw new ArgumentNullException(nameof(resultOfOperation)) { Source = GetType().AssemblyQualifiedName } } ; //messages = null; try { //var formWasFinded = UsersF.First(registrationForm => registrationForm.Login.Equals(formAuth.Login)); var formWasFinded = await UsersF.Users.FirstAsync(registrationForm => registrationForm.Login == formAuth.Login) .ConfigureAwait(false); switch (formAuth.AuthenticationMethod) { case AuthenticationMethod.Classic: var formClassicAuth = formAuth as IAuthenticationFormClassic; if (formClassicAuth == null) { throw new InvalidCastException("Преобразование из типа IAuthenticationForm в тип " + "IAuthenticationFormClassic завершилось с ошибкой.") { Source = GetType().AssemblyQualifiedName } } ; var hashAlg = cryptoFactory.CreateHashAlgorithm(choosenCrypto.Provider, formClassicAuth.HashAlgotitm); var hashOfTruePass = hashAlg.ComputeHash(Encoding.UTF8.GetBytes(formWasFinded.Password)); if (hashOfTruePass.SequenceEqual(formClassicAuth.HashOfPassword)) { resultOfOperation.OperationWasFinishedSuccessful = true; } else { resultOfOperation.ErrorMessage = "Правильность пароля под сомнением?"; resultOfOperation.OperationWasFinishedSuccessful = false; } break; case AuthenticationMethod.Sign: var formSignAuth = formAuth as IAuthenticationFormSign; if (formSignAuth == null) { throw new InvalidCastException("Преобразование из типа IAuthenticationForm в тип " + "IAuthenticationFormSign завершилось с ошибкой.") { Source = GetType().AssemblyQualifiedName } } ; var signAlg = cryptoFactory.CreateSignAlgoritm(formSignAuth.CryptoProvider, formSignAuth.SignantureAlgoritmName); //var signAlg = cryptoFactory.CreateSignAlgoritm(choosenCrypto.Provider, formSignAuth.SignantureAlgoritmName); signAlg.Import(formWasFinded.KeyParametrsBlob.Key); if (signAlg.VerifySign(formSignAuth.Hash, formSignAuth.Sign)) { resultOfOperation.OperationWasFinishedSuccessful = true; } else { resultOfOperation.ErrorMessage = "Достоверность цифровой подписи под сомнением?"; resultOfOperation.OperationWasFinishedSuccessful = false; } break; } OfflineMessagesConcurent messages; OfflineMessagesF.TryRemove(formWasFinded.Login, out messages); if (messages == null) { messages = new OfflineMessagesConcurent(formWasFinded.Login); } return(new Tuple <UserFormSurrogate, OfflineMessagesConcurent>(formWasFinded, messages)); } catch (InvalidOperationException ex) { CreateResultWithError(3, resultOfOperation, formAuth.Login); return(null); } }