public JsonResult SignAndEncrypt(IEnumerable <string> targets) { Settings settings = new Settings() { _SignatureSettings = new SignatureSettings() { _EncodingType = EncodingType.Base64 } }; PINSettings pinSettings = new PINSettings() { NeedToPIN = false }; DAVListCryptoOperationResponse response = service.DAVListSignOperation(GetFileNamesFromTargets(targets), settings, pinSettings, token); return(Json(response)); }
public JsonResult Sign(IEnumerable <string> targets) { Settings settings = new Settings() { _SignatureVerificationSettings = new SignatureVerificationSettings() { VerifyFlag = SignatureVerificationFlags.SignatureOnly } }; PINSettings pinSettings = new PINSettings() { NeedToPIN = false }; DAVListCryptoOperationResponse response = service.DAVListSignOperation(GetFileNamesFromTargets(targets), settings, pinSettings, token); return(Json(response)); }
/// <summary> /// Выполнить операцию /// </summary> /// <param name="settingsJSON"></param> /// <param name="signedFileInfosJSON"></param> /// <param name="type"></param> /// <param name="savePIN"></param> /// <returns></returns> public ActionResult MakeOperation(string settingsJSON, string signedFileInfosJSON, OperationType type, bool?savePIN, bool?needToPIN) { //_logger.Info("MakeOperation PID:" + System.Diagnostics.Process.GetCurrentProcess().Id); //_logger.Info("MakeOperation TID:" + System.Threading.Thread.CurrentThread.ManagedThreadId); Guid token = CheckSessionAuthState(CurrentUser, _authService); if (token == Guid.Empty) { return (Json( new { OperationResult = new Result { Exception = new MasterException(401, "Токен истек", null) } }, JsonRequestBehavior.AllowGet)); } Settings settings; Exception globalException = null; List <SignedFileInfo> signedFileInfos; try { var serializer = new DataContractJsonSerializer(typeof(List <SignedFileInfo>)); var stream = new MemoryStream(Encoding.UTF8.GetBytes(signedFileInfosJSON)); signedFileInfos = (List <SignedFileInfo>)serializer.ReadObject(stream); stream.Close(); } catch (Exception exception) { return (Json( new { OperationResult = new Result { Exception = new MasterException(500, "Не удалось сериализовать список файлов от клиента", null) } }, JsonRequestBehavior.AllowGet)); } try { var serializer = new DataContractJsonSerializer(typeof(Settings)); var stream = new MemoryStream(Encoding.UTF8.GetBytes(settingsJSON)); settings = (Settings)serializer.ReadObject(stream); stream.Close(); } catch (Exception exception) { return (Json( new { OperationResult = new Result { Exception = new MasterException(500, "Не удалось сериализовать настройки от клиента", null) } }, JsonRequestBehavior.AllowGet)); } var davListCryptoOperationResponse = new DAVListCryptoOperationResponse(); var pinsettings = new PINSettings { NeedToPIN = (needToPIN != null && (bool)needToPIN), PIN = settings._SignatureSettings.KeysetPassword, SavePIN = (savePIN != null && (bool)savePIN) }; if (!string.IsNullOrEmpty(settings._SignatureSettings.KeysetPassword)) { pinsettings.PIN = settings._SignatureSettings.KeysetPassword; } if (!string.IsNullOrEmpty(settings._DecryptionSettings.KeysetPassword)) { pinsettings.PIN = settings._DecryptionSettings.KeysetPassword; } var doubleOperationResult = new DoubleOperationResult(); //список файлов с ошибками var errorDataOperationResults = new List <DataOperationResult>(); #region Единичные операции if (type != OperationType.DecryptSignverify && type != OperationType.SignEncrypt) { var result = new Result(); #region Подпись if (type == OperationType.Sign) { settings._SignatureSettings.KeysetPassword = string.Empty; List <string> files = signedFileInfos.Select(signedFileInfo => signedFileInfo.FileUri).ToList(); try { davListCryptoOperationResponse = _cryptxService.DAVListSignOperation(files, settings, pinsettings, token); if (davListCryptoOperationResponse.Exception != null) { if (davListCryptoOperationResponse.Exception.Message.Contains("0x8010006b")) { result.Exception = new MasterException(Int32.Parse("8010006b", NumberStyles.HexNumber), davListCryptoOperationResponse.Exception.Message, davListCryptoOperationResponse.Exception); } else { result.Exception = new MasterException( davListCryptoOperationResponse.Exception.HResult, davListCryptoOperationResponse.Exception.Message, davListCryptoOperationResponse.Exception); } } else { foreach ( DataOperationResult dataOperationResult in davListCryptoOperationResponse.OperationResults) { var signResult = new SignResult(); signResult.UnsignedFile = dataOperationResult.SourceFile; if (dataOperationResult.Exception != null) { Exception curException = dataOperationResult.Exception; signResult.Exception = new MasterException(curException.HResult, curException.Message, curException); } else { signResult.SignedFile = dataOperationResult.OperatedFile; } result.OperationResults.Add(signResult); } } } catch (Exception ex) { result.Exception = new MasterException(ex.HResult, "", ex); globalException = ex; } } #endregion #region Шифровка if (type == OperationType.Encrypt) { try { List <string> files = signedFileInfos.Select(signedFileInfo => signedFileInfo.FileUri).ToList(); davListCryptoOperationResponse = _cryptxService.DAVListEncryptOperation(files, settings, token); if (davListCryptoOperationResponse.Exception != null) { result.Exception = new MasterException(davListCryptoOperationResponse.Exception.HResult, davListCryptoOperationResponse.Exception.Message, davListCryptoOperationResponse.Exception); globalException = davListCryptoOperationResponse.Exception; } else { foreach ( DataOperationResult dataOperationResult in davListCryptoOperationResponse.OperationResults) { var encryptResult = new EncryptResult(); encryptResult.UnencryptedFile = dataOperationResult.SourceFile; if (dataOperationResult.Exception != null) { Exception curException = dataOperationResult.Exception; encryptResult.Exception = new MasterException(curException.HResult, curException.Message, curException); } else { encryptResult.EncryptedFile = dataOperationResult.OperatedFile; } result.OperationResults.Add(encryptResult); } } } catch (Exception ex) { result.Exception = new MasterException(ex.HResult, ex.Message, ex); globalException = ex; } } #endregion #region Проверка подписи if (type == OperationType.SignVerify) { var response = new List <SignVerifyResponse>(); try { response = _cryptxService.DAVListSignVerify(signedFileInfos, settings, true, token); foreach (SignVerifyResponse signVerifyResponse in response) { var signVerifyResult = new SignVerifyResult(); signVerifyResult.CertificateStatuses = signVerifyResponse.CertificateStatuses; signVerifyResult.MainStatus = signVerifyResponse.MainStatus; signVerifyResult.Detached = signVerifyResponse.Detached; signVerifyResult.SignedFile = signVerifyResponse.SourceFile; signVerifyResult.DataFile = signVerifyResponse.DataSourceFile; result.OperationResults.Add(signVerifyResult); } } catch (Exception ex) { result.Exception = new MasterException(ex.HResult, ex.Message, ex); globalException = ex; } //return Json(new { response, errorDataOperationResults, operationError = globalException }, JsonRequestBehavior.AllowGet); } #endregion #region асшифровать if (type == OperationType.Decrypt) { try { settings._DecryptionSettings.KeysetPassword = string.Empty; List <string> files = signedFileInfos.Select(signedFileInfo => signedFileInfo.FileUri).ToList(); davListCryptoOperationResponse = _cryptxService.DAVListDecryptOperation(files, settings, pinsettings, token); if (davListCryptoOperationResponse.Exception != null) { Exception curException = davListCryptoOperationResponse.Exception; result.Exception = new MasterException(curException.HResult, curException.Message, curException); globalException = davListCryptoOperationResponse.Exception; } else { foreach ( DataOperationResult dataOperationResult in davListCryptoOperationResponse.OperationResults) { var decryptResult = new DecryptResult(); if (dataOperationResult.Exception != null) { Exception curException = dataOperationResult.Exception; decryptResult.Exception = new MasterException(curException.HResult, curException.Message, curException); } decryptResult.DecryptedFile = dataOperationResult.SourceFile; decryptResult.EncryptedFile = dataOperationResult.OperatedFile; result.OperationResults.Add(decryptResult); } } } catch (Exception ex) { result.Exception = new MasterException(ex.HResult, ex.Message, ex); globalException = ex; } } #endregion return(Json(new { OperationResult = result }, JsonRequestBehavior.AllowGet)); } #endregion #region Двойные операции #region Подпись-шифрование if (type == OperationType.SignEncrypt) { try { List <string> files = signedFileInfos.Select(signedFileInfo => signedFileInfo.FileUri).ToList(); //подпись davListCryptoOperationResponse = _cryptxService.DAVListSignOperation(files, settings, pinsettings, token); if (davListCryptoOperationResponse.Exception != null) { doubleOperationResult.Exception = new MasterException(davListCryptoOperationResponse.Exception.HResult, davListCryptoOperationResponse.Exception.Message, davListCryptoOperationResponse.Exception); return(Json(new { OperationResult = doubleOperationResult }, JsonRequestBehavior.AllowGet)); } var toEncrypt = new List <string>(); foreach (DataOperationResult dataOperationResult in davListCryptoOperationResponse.OperationResults) { var signResult = new SignResult(); signResult.UnsignedFile = dataOperationResult.SourceFile; //firsOperationResult.Add(signResult); if (dataOperationResult.Exception != null) { Exception curException = dataOperationResult.Exception; signResult.Exception = new MasterException(curException.HResult, curException.Message, curException); errorDataOperationResults.Add(dataOperationResult); } else { signResult.SignedFile = dataOperationResult.OperatedFile; toEncrypt.Add(dataOperationResult.OperatedFile); } doubleOperationResult.OperationResults.Add(new DoubleResult { FirsOperationResult = signResult }); } List <string> filesToEncryption = doubleOperationResult.OperationResults.Where(x => x.FirsOperationResult.Exception == null) .Select(x => ((SignResult)x.FirsOperationResult).SignedFile) .ToList(); //шифрование davListCryptoOperationResponse = _cryptxService.DAVListEncryptOperation(toEncrypt, settings, token); //цикл в цикле для сопастовления данных первой операции со второй foreach (DataOperationResult dataOperationResult in davListCryptoOperationResponse.OperationResults) { foreach (DoubleResult doubleResult in doubleOperationResult.OperationResults) { if (((SignResult)doubleResult.FirsOperationResult).SignedFile == dataOperationResult.SourceFile) { var encryptResult = new EncryptResult(); encryptResult.UnencryptedFile = dataOperationResult.SourceFile; if (dataOperationResult.Exception != null) { encryptResult.Exception = new MasterException( dataOperationResult.Exception.HResult, dataOperationResult.Exception.Message, dataOperationResult.Exception); } else { encryptResult.EncryptedFile = dataOperationResult.OperatedFile; } doubleResult.SecondOperationResult = encryptResult; } } } } catch (Exception ex) { doubleOperationResult.Exception = new MasterException(ex.HResult, ex.Message, ex); } } #endregion #region асшифровать - проверить подпись if (type == OperationType.DecryptSignverify) { var response = new List <SignVerifyResponse>(); try { settings._DecryptionSettings.KeysetPassword = string.Empty; List <string> files = signedFileInfos.Select(signedFileInfo => signedFileInfo.FileUri).ToList(); davListCryptoOperationResponse = _cryptxService.DAVListDecryptOperation(files, settings, pinsettings, token); if (davListCryptoOperationResponse.Exception != null) { doubleOperationResult.Exception = new MasterException(davListCryptoOperationResponse.Exception.HResult, davListCryptoOperationResponse.Exception.Message, davListCryptoOperationResponse.Exception); return(Json(new { OperationResult = doubleOperationResult }, JsonRequestBehavior.AllowGet)); } var toSignVerify = new List <SignedFileInfo>(); foreach (DataOperationResult dataOperationResult in davListCryptoOperationResponse.OperationResults) { var decryptResult = new DecryptResult(); decryptResult.DecryptedFile = dataOperationResult.SourceFile; if (dataOperationResult.Exception != null) { Exception curException = dataOperationResult.Exception; decryptResult.Exception = new MasterException(curException.HResult, curException.Message, curException); errorDataOperationResults.Add(dataOperationResult); } else { decryptResult.EncryptedFile = dataOperationResult.OperatedFile; var info = new SignedFileInfo(); info.FileUri = dataOperationResult.OperatedFile; toSignVerify.Add(info); } doubleOperationResult.OperationResults.Add(new DoubleResult { FirsOperationResult = decryptResult }); } response = _cryptxService.DAVListSignVerify(toSignVerify, settings, true, token); foreach (SignVerifyResponse signVerifyResponse in response) { foreach (DoubleResult doubleResult in doubleOperationResult.OperationResults) { //сопоставляем выход первой операции со входом второй if (((DecryptResult)doubleResult.FirsOperationResult).EncryptedFile == signVerifyResponse.SourceFile) { var signVerifyResult = new SignVerifyResult(); if (signVerifyResponse.Exception != null) { Exception curException = signVerifyResponse.Exception; signVerifyResult.Exception = new MasterException(curException.HResult, curException.Message, curException); } else { signVerifyResult.CertificateStatuses = signVerifyResponse.CertificateStatuses; signVerifyResult.MainStatus = signVerifyResponse.MainStatus; signVerifyResult.Detached = signVerifyResponse.Detached; signVerifyResult.SignedFile = signVerifyResponse.SourceFile; signVerifyResult.DataFile = signVerifyResponse.DataSourceFile; } doubleResult.SecondOperationResult = signVerifyResult; } } } } catch (Exception ex) { doubleOperationResult.Exception = new MasterException(ex.HResult, ex.Message, ex); } } #endregion return(Json(new { OperationResult = doubleOperationResult }, JsonRequestBehavior.AllowGet)); #endregion }