public SignFileResponse SignFile(SignFileRequest signFileRequest) { var signFileResponse = new SignFileResponse(); signFileResponse.DeleteFailed += (response, file, exception) => { Log.Error(exception, $"Failed to delete file '{file}'"); }; signFileResponse.DeleteSkipped += (response, file) => { Log.Warn($"Skipped file delete '{file}'"); }; signFileResponse.DeleteSuccess += (response, file) => { Log.Trace($"Successfully deleted file '{file}'"); }; var remoteIp = RemoteIp; var isLegacy = IsLegacyEndpoint; string inputFileName = null; try { // // validate input if (isLegacy) { Log.Warn($"[{remoteIp}] Client is using legacy endpoint!"); } Log.Info($"[{remoteIp}] New sign request for file {signFileRequest.FileName} by {remoteIp} ({signFileRequest.FileSize} bytes)"); if (signFileRequest.FileSize == 0 || signFileRequest.FileContent == null) { signFileResponse.Result = SignFileResponseResult.FileNotSignedError; signFileResponse.ErrorMessage = "No file was received"; return(signFileResponse); } // // find certificate CertificateConfiguration certificate; if (string.IsNullOrWhiteSpace(signFileRequest.Username)) { certificate = Configuration.Certificates.FirstOrDefault(c => c.IsAnonymous); } else { certificate = Configuration.Certificates.FirstOrDefault( c => c.IsAuthorized(signFileRequest.Username, signFileRequest.Password)); } if (certificate == null) { Log.Warn("Unauthorized signing request"); signFileResponse.Result = SignFileResponseResult.FileNotSignedUnauthorized; return(signFileResponse); } // // find compatible signing tool var signingTool = SigningToolProvider.GetSigningTool(signFileRequest.FileName); if (signingTool == null) { signFileResponse.Result = SignFileResponseResult.FileNotSignedUnsupportedFormat; return(signFileResponse); } // // upload file to working directory inputFileName = signFileRequest.FileName ?? ""; inputFileName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + Path.GetFileNameWithoutExtension(inputFileName) + "_" + Guid.NewGuid() + (Path.GetExtension(inputFileName)); inputFileName = Path.Combine(Configuration.WorkingDirectory, inputFileName); using (var targetFile = new FileStream(inputFileName, FileMode.Create, FileAccess.ReadWrite)) { signFileRequest.FileContent.CopyTo(targetFile); } // // sign file signingTool.SignFile(inputFileName, certificate.Certificate, Configuration.TimestampServer, signFileRequest, signFileResponse); Log.Info($"[{remoteIp}] New sign request for file {signFileRequest.FileName} finished ({signFileRequest.FileSize} bytes)"); switch (signFileResponse.Result) { case SignFileResponseResult.FileSigned: case SignFileResponseResult.FileResigned: break; case SignFileResponseResult.FileAlreadySigned: case SignFileResponseResult.FileNotSignedUnsupportedFormat: case SignFileResponseResult.FileNotSignedError: case SignFileResponseResult.FileNotSignedUnauthorized: // ensure input file is cleaned in error cases where the sign tool does not have a result if (!(signFileResponse.FileContent is FileStream)) { try { Log.Trace($"Deleting file {inputFileName}"); File.Delete(inputFileName); Log.Trace($"File successfully deleted {inputFileName}"); } catch (Exception e) { Log.Error(e, "Could not delete input file for failed request"); } } else { Log.Trace($"Delete file skipped for failed request {signFileResponse.Result} {inputFileName}, {signFileResponse.FileContent.GetType()}"); } break; } } catch (Exception e) { Log.Error(e, $"[{remoteIp}] Signing of {signFileRequest.FileName} failed: {e.Message}"); signFileResponse.Result = SignFileResponseResult.FileNotSignedError; signFileResponse.ErrorMessage = e.Message; if (!string.IsNullOrEmpty(inputFileName) && File.Exists(inputFileName)) { try { File.Delete(inputFileName); } catch (Exception fileException) { Log.Error(fileException, $"[{remoteIp}] Failed to delete file {inputFileName}"); } } } return(signFileResponse); }
public SignFileResponse SignFile(SignFileRequest signFileRequest) { var signFileResponse = new SignFileResponse(); var remoteIp = RemoteIp; string inputFileName = null; try { // // validate input Log.Info("New sign request for file {0} by {1} ({2} bytes)", signFileRequest.FileName, remoteIp, signFileRequest.FileSize); if (signFileRequest.FileSize == 0 || signFileRequest.FileContent == null) { signFileResponse.Result = SignFileResponseResult.FileNotSignedError; signFileResponse.ErrorMessage = "No file was received"; return(signFileResponse); } // // find certificate CertificateConfiguration certificate; if (string.IsNullOrWhiteSpace(signFileRequest.Username)) { certificate = Configuration.Certificates.FirstOrDefault(c => c.IsAnonymous); } else { certificate = Configuration.Certificates.FirstOrDefault( c => c.IsAuthorized(signFileRequest.Username, signFileRequest.Password)); } if (certificate == null) { Log.Warn("Unauthorized signing request"); signFileResponse.Result = SignFileResponseResult.FileNotSignedUnauthorized; return(signFileResponse); } // // find compatible signing tool var signingTool = SigningToolProvider.GetSigningTool(signFileRequest.FileName); if (signingTool == null) { signFileResponse.Result = SignFileResponseResult.FileNotSignedUnsupportedFormat; return(signFileResponse); } // // upload file to working directory inputFileName = signFileRequest.FileName ?? ""; inputFileName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + Path.GetFileNameWithoutExtension(inputFileName) + "_" + Guid.NewGuid() + (Path.GetExtension(inputFileName)); inputFileName = Path.Combine(Configuration.WorkingDirectory, inputFileName); using (var targetFile = new FileStream(inputFileName, FileMode.Create, FileAccess.ReadWrite)) { signFileRequest.FileContent.CopyTo(targetFile); } // // sign file signingTool.SignFile(inputFileName, certificate.Certificate, Configuration.TimestampServer, signFileRequest, signFileResponse); Log.Info("New sign request for file {0} finished ({1} bytes)", signFileRequest.FileName, signFileRequest.FileSize); } catch (Exception e) { Log.Error(e, $"Signing of {signFileRequest.FileName} by {remoteIp} failed: {e.Message}"); signFileResponse.Result = SignFileResponseResult.FileNotSignedError; signFileResponse.ErrorMessage = e.Message; if (!string.IsNullOrEmpty(inputFileName) && File.Exists(inputFileName)) { try { File.Delete(inputFileName); } catch (Exception fileException) { Log.Error(fileException, $"Failed to delete file {inputFileName} by {remoteIp}"); } } } return(signFileResponse); }