Esempio n. 1
0
        public static string Session_PutFileToSign(string SessionToken, byte[] FileDafirmare, string FileName)
        {
            SessionToken = SessionToken.ToUpper();
            //string cacheDir = Path.Combine ( AppDomain.CurrentDomain.BaseDirectory , "MultiSignWorkDir");
            string cacheDir   = ConfigurationManager.AppSettings["CACHEDIR"] + "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);
        }
Esempio n. 2
0
        public static string OpenMultiSignSession(bool cosign, bool timestamp, int Type)
        {
            //string cacheDir = AppDomain.CurrentDomain.BaseDirectory + "MultiSignWorkDir";
            string cacheDir     = ConfigurationManager.AppSettings["CACHEDIR"] + "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, cosign = cosign
                };
                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;
                //non creata per svariati motivi
                //return null;
            }
        }
Esempio n. 3
0
        public static byte[] Session_GetSignedFile(string SessionToken, string hashFileDaFirmare)
        {
            SessionToken = SessionToken.ToUpper();
            //string cacheDir = Path.Combine ( AppDomain.CurrentDomain.BaseDirectory , "MultiSignWorkDir");
            string cacheDir   = ConfigurationManager.AppSettings["CACHEDIR"] + "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);
        }
Esempio n. 4
0
        public static bool Session_RemoteSign(string SessionToken, string aliasCertificato, string dominioCertificato, string pinCertificato, string otpFirma, FirmaRemota.FirmaRemotaPortTypeClient client)
        {
            List <byte[]> filesRead = new List <byte[]>();

            SessionToken = SessionToken.ToUpper();

            //string cacheDir = Path.Combine ( AppDomain.CurrentDomain.BaseDirectory , "MultiSignWorkDir");
            string cacheDir   = ConfigurationManager.AppSettings["CACHEDIR"] + "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)
                    {
                        byte[] content = File.ReadAllBytes(Path.Combine(sessionDir, FileInformation.OriginalFullName));

                        filesRead.Add(content);
                    }

                    sbyte marca          = 0;
                    sbyte firmaParallela = 0;

                    if (mft.timestamp)
                    {
                        marca = 1;
                    }

                    if (mft.cosign)
                    {
                        firmaParallela = 1;
                    }

                    ClrVerificationService.FirmaRemota.FileFirmatiType retval = null;
                    try
                    {
                        if (client != null)
                        {
                            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Ssl3;
                            if (mft.SignatureType == Manifest.SignType.CADES)
                            {
                                retval = client.FirmaRemotaMultiplaP7M(filesRead.ToArray(), aliasCertificato, dominioCertificato, pinCertificato, otpFirma, marca, firmaParallela);
                            }
                            else
                            {
                                retval = client.FirmaRemotaMultiplaPDF(filesRead.ToArray(), aliasCertificato, dominioCertificato, pinCertificato, otpFirma, marca);
                            }
                        }
                        else
                        {
                            retval = new ClrVerificationService.FirmaRemota.FileFirmatiType();
                            //dummy
                            retval.documentoFirmato = filesRead.ToArray();
                        }
                    }
                    catch (FaultException <FirmaRemota.WSFault> f)
                    {
                        string errMsg = componiErroreWSFault(f.Detail);
                        logger.Error(errMsg);
                        throw new Exception(errMsg);
                    }
                    catch (Exception e)
                    {
                        logger.ErrorFormat("Errore chimando la firma multipla {0} {1}", e.Message, e.StackTrace);
                        return(false);
                    }
                    int index = 0;
                    foreach (byte[] filebyteArray in retval.documentoFirmato)
                    {
                        filesRead[index++] = filebyteArray;
                    }

                    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());

                    return(true);
                }
            }
            return(false);
        }