public string Session_PutFileToSign(string SessionToken, byte[] FileDafirmare, string FileName) { SessionToken = SessionToken.ToUpper(); string cacheDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MultiSignWorkDir"); string sessionDir = Path.Combine(cacheDir, SessionToken); if (Directory.Exists(sessionDir)) { string manifestFile = Path.Combine(sessionDir, "Manifest.xml"); if (File.Exists(manifestFile)) { String manifestXML = File.ReadAllText(manifestFile); Manifest.ManifestFile mft = Manifest.ManifestFile.Deserialize(manifestXML); SHA256 mySHA256 = SHA256Managed.Create(); string sha256Hash = BitConverter.ToString(mySHA256.ComputeHash(FileDafirmare)).Replace("-", ""); foreach (Manifest.MainfestFileInformation FileInformation in mft.FileInformation) { if (FileInformation.hash.ToUpper() == sha256Hash.ToUpper()) { //file esiste nel manifest uscire. return(null); } } try { string SignFileName = Guid.NewGuid().ToString() + FileName; mft.FileInformation.Add(new Manifest.MainfestFileInformation { hash = sha256Hash.ToUpper(), OriginalFullName = SignFileName }); File.WriteAllBytes(Path.Combine(sessionDir, SignFileName), FileDafirmare); File.WriteAllText(manifestFile, mft.Serialize()); return(sha256Hash); } catch (Exception e) { //errori scrivendo il file o il manifest.. uscire con null //return null; logger.ErrorFormat("Errore in PutFileToSign {0} stk {1}", e.Message, e.StackTrace); throw e; } } } return(null); }
public string OpenMultiSignSession(bool cosign, bool timestamp, int Type) { string cacheDir = AppDomain.CurrentDomain.BaseDirectory + "MultiSignWorkDir"; string sessionToken = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); try { DirectoryInfo di = System.IO.Directory.CreateDirectory(String.Format("{0}\\{1}", cacheDir, sessionToken)); Manifest.SignType st = (Manifest.SignType)Type; Manifest.ManifestFile m = new Manifest.ManifestFile { SignatureType = st, timestamp = timestamp, Token = sessionToken }; File.WriteAllText(Path.Combine(di.FullName, "Manifest.xml"), m.Serialize()); return(sessionToken); } catch (Exception e) { logger.ErrorFormat("Errore in OpenMultiSignSession {0} stk {1}", e.Message, e.StackTrace); throw e; } }
public byte[] Session_GetSignedFile(string SessionToken, string hashFileDaFirmare) { SessionToken = SessionToken.ToUpper(); string cacheDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MultiSignWorkDir"); string sessionDir = Path.Combine(cacheDir, SessionToken); if (Directory.Exists(sessionDir)) { string manifestFile = Path.Combine(sessionDir, "Manifest.xml"); if (File.Exists(manifestFile)) { String manifestXML = File.ReadAllText(manifestFile); Manifest.ManifestFile mft = Manifest.ManifestFile.Deserialize(manifestXML); foreach (Manifest.MainfestFileInformation FileInformation in mft.FileInformation) { if (FileInformation.hash.ToUpper() == hashFileDaFirmare.ToUpper()) { //file esiste nel manifest leggere e uscire. //per test, poi commentare, se no torna solo e sempre quello inviato (echo) //return File.ReadAllBytes(Path.Combine(sessionDir, FileInformation.OriginalFullName)); try { return(File.ReadAllBytes(Path.Combine(sessionDir, FileInformation.SignedFullName))); } catch { logger.ErrorFormat("Il file {0} | {1} non è leggibile", sessionDir, FileInformation.SignedFullName); return(null); } } } } } return(null); }
public bool Session_RemoteSign(string SessionToken, string aliasCertificato, string dominioCertificato, string pinCertificato, string otpFirma, object client) { List <byte[]> filesRead = new List <byte[]>(); SessionToken = SessionToken.ToUpper(); ArubaSignServicePortBindingQSService wsclient = client as ArubaSignServicePortBindingQSService; if (wsclient == null) { return(false); } auth authData = new auth { otpPwd = otpFirma, user = aliasCertificato, userPWD = pinCertificato, typeOtpAuth = dominioCertificato, typeHSM = "COSIGN" // come specificato a pagina 9 del manuale ARSS Developer Guide v1.0 }; pdfSignApparence pdfsignApp = new pdfSignApparence { page = 1 }; string cacheDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MultiSignWorkDir"); string sessionDir = Path.Combine(cacheDir, SessionToken); if (Directory.Exists(sessionDir)) { string manifestFile = Path.Combine(sessionDir, "Manifest.xml"); if (File.Exists(manifestFile)) { String manifestXML = File.ReadAllText(manifestFile); Manifest.ManifestFile mft = Manifest.ManifestFile.Deserialize(manifestXML); string sessionID = wsclient.opensession(authData); if (sessionID == "KO-0001") { string errMsg = String.Format("WSFAULT #CODE {0} #MESSAGE {1} #TYPE {2}#", sessionID, "Errore generico", "ERROR"); logger.Error(errMsg); throw new Exception(errMsg); } if (sessionID == "KO-0003") { string errMsg = String.Format("WSFAULT #CODE {0} #MESSAGE {1} #TYPE {2}#", sessionID, "Errore in fase di verifica delle credenziali", "ERROR"); logger.Error(errMsg); throw new Exception(errMsg); } if (sessionID == "KO-0004") { string errMsg = String.Format("WSFAULT #CODE {0} #MESSAGE {1} #TYPE {2}#", sessionID, "Errore nel PIN", "ERROR"); logger.Error(errMsg); throw new Exception(errMsg); } foreach (Manifest.MainfestFileInformation FileInformation in mft.FileInformation) { byte[] content = File.ReadAllBytes(Path.Combine(sessionDir, FileInformation.OriginalFullName)); signReturnV2 retval = null; signRequestV2 sr = new signRequestV2 { binaryinput = content, transport = typeTransport.BYNARYNET, transportSpecified = true, requiredmark = mft.timestamp, identity = authData, certID = "AS0", profile = null }; if (mft.SignatureType == Manifest.SignType.CADES) { if (mft.cosign) //aggiunta firma parallela. { retval = wsclient.addpkcs7sign(sr, false); } else { retval = wsclient.pkcs7signV2(sr, false); } if (retval.status.Equals("OK")) { filesRead.Add(retval.binaryoutput); } else { string errorCode = retval.return_code; string errorMessage = retval.description; string faultMSG = String.Format("WSFAULT #CODE {0} #MESSAGE {1} #TYPE {2}#", errorCode, errorMessage, "ERROR"); logger.ErrorFormat("Errore in pkcs7signV2 o addpkcs7sign / multi Codice {0} Messaggio {1}", errorCode, errorMessage); filesRead.Add(null); } } else { retval = wsclient.pdfsignatureV2(sr, pdfsignApp); if (retval.status.Equals("OK")) { filesRead.Add(retval.binaryoutput); } else { string errorCode = retval.return_code; string errorMessage = retval.description; string faultMSG = String.Format("WSFAULT #CODE {0} #MESSAGE {1} #TYPE {2}#", errorCode, errorMessage, "ERROR"); logger.ErrorFormat("Errore in pdfsignatureV2_multi Codice {0} Messaggio {1}", errorCode, errorMessage); filesRead.Add(null); } } } int index = 0; foreach (Manifest.MainfestFileInformation FileInformation in mft.FileInformation) { byte[] content = filesRead[index++]; string newName = "signed_" + FileInformation.OriginalFullName; File.WriteAllBytes(Path.Combine(sessionDir, newName), content); FileInformation.SignedFullName = newName; } File.WriteAllText(manifestFile, mft.Serialize()); string closeRetval = wsclient.closesession(authData, sessionID); if (closeRetval == "KO-0001") { string errMsg = String.Format("WSFAULT #CODE {0} #MESSAGE {1} #TYPE {2}#", sessionID, "Errore generico", "ERROR"); logger.Error(errMsg); throw new Exception(errMsg); } return(true); } } return(false); }