Exemple #1
0
        public OutputResponseMarca VerificaMarca(byte[] filep7m, byte[] fileTSR)
        {
            OutputResponseMarca outputMarca = new OutputResponseMarca();

            BusinessLogic.Documenti.DigitalSignature.VerifyTimeStamp checkMarca = new BusinessLogic.Documenti.DigitalSignature.VerifyTimeStamp();
            outputMarca = checkMarca.Verify(filep7m, fileTSR);

            return(outputMarca);
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="TimeStampQuery"></param>
        /// <returns></returns>
        public OutputResponseMarca getTimeStamp(InputMarca TimeStampQuery)
        {
            OutputResponseMarca outputMarca = new OutputResponseMarca();


            byte[] dati = String_To_Bytes(TimeStampQuery.file_p7m);
            //SHA1 sha1 = SHA1CryptoServiceProvider.Create();
            //byte[] hash = sha1.ComputeHash(dati);

            SHA256Managed sha256 = new SHA256Managed();

            byte[] hash = sha256.ComputeHash(dati);

            TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();

            reqGen.SetCertReq(true);


            //Funzione randomica per il Nonce.
            //RandomNumberGenerator nRand = new RNGCryptoServiceProvider();
            long casuale = (long)nRandom.Next();
            //TimeStampRequest tsReq = reqGen.Generate(TspAlgorithms.Sha1, hash, BigInteger.ValueOf(casuale));
            TimeStampRequest tsReq = reqGen.Generate(TspAlgorithms.Sha256, hash, BigInteger.ValueOf(casuale));

            byte[] tsData = tsReq.GetEncoded();

            string urlTSA = string.Empty;

            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["URL_TSA"]))
            {
                urlTSA = ConfigurationManager.AppSettings["URL_TSA"].ToString();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlTSA);
                req.Method      = "POST";
                req.ContentType = "application/timestamp-query";

                //Username e password per accedere alla Time Stamping Authority
                string pwd = string.Empty;
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["PASSWORD_UTENTE_TSA"]))
                {
                    pwd = ConfigurationManager.AppSettings["PASSWORD_UTENTE_TSA"].ToString();
                    req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(pwd)));
                }

                req.ContentLength = tsData.Length;

                Stream reqStream = req.GetRequestStream();
                reqStream.Write(tsData, 0, tsData.Length);
                reqStream.Close();

                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                if (res == null)
                {
                    outputMarca.esito             = "KO";
                    outputMarca.descrizioneErrore = "Impossibile contattare la TSA o autorizzazione negata";
                    return(outputMarca);
                }
                else
                {
                    Stream            resStream = new BufferedStream(res.GetResponseStream());
                    TimeStampResponse tsRes     = new TimeStampResponse(resStream);
                    resStream.Close();
                    BusinessLogic.Documenti.DigitalSignature.VerifyTimeStamp checkMarca = new BusinessLogic.Documenti.DigitalSignature.VerifyTimeStamp();
                    outputMarca = checkMarca.Verify(tsReq, tsRes);
                }
            }
            else
            {
                outputMarca.esito             = "KO";
                outputMarca.descrizioneErrore = "Impossibile contattare la TSA o url configurata errata!";
                return(outputMarca);
            }

            return(outputMarca);
        }