Ejemplo n.º 1
0
        /// <summary>
        /// Verifica la marca ottenuta in risposta ed estrae tutti i dati dalla marca completando i dati dell'oggetto OutputResponseMarca con quelli letti direttamente dalla marca!
        /// </summary>
        /// <param name="filep7m"></param>
        /// <param name="fileTSR"></param>
        /// <returns></returns>
        private static DocsPaVO.areaConservazione.OutputResponseMarca VerificaMarca(byte[] filep7m, byte[] fileTSR)
        {
            DocsPaVO.areaConservazione.OutputResponseMarca outputMarca = new DocsPaVO.areaConservazione.OutputResponseMarca();
            DigitalSignature.VerifyTimeStamp checkMarca = new DigitalSignature.VerifyTimeStamp();
            outputMarca = checkMarca.Verify(filep7m, fileTSR);

            return(outputMarca);
        }
Ejemplo n.º 2
0
        private DocsPaVO.documento.SignerInfo ExtractSignerInfo(List <string> ErrorMessageLst, IX509Store store, SignerInformation signer)
        {
            DocsPaVO.documento.SignerInfo         thisSinger = new DocsPaVO.documento.SignerInfo();
            Org.BouncyCastle.X509.X509Certificate cert1      = GetCertificate(signer, store);
            try
            {
                if (!signer.Verify(cert1))
                {
                    ErrorMessageLst.Add("Not valid signature");
                }
            }
            catch (Exception e)
            {
                ErrorMessageLst.Add(e.Message);
            }

            thisSinger.isCountersigner = signer.IsCounterSignature;
            if (signer.SignedAttributes != null)
            {
                if (signer.SignedAttributes[Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAASigningCertificateV2] == null)
                {
                    ErrorMessageLst.Add("Id-AA-SigningCertificateV2 not found");
                }

                if (signer.SignedAttributes[CmsAttributes.MessageDigest] == null)
                {
                    ErrorMessageLst.Add("Pkcs9AtMessageDigest not found");
                }


                if (!signer.IsCounterSignature) //Pare che i controfirmatari non ncessitino di questo parametro
                {
                    if (signer.SignedAttributes[Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.Pkcs9AtContentType] == null)
                    {
                        ErrorMessageLst.Add("Pkcs9AtContentType not found");
                    }
                }

                thisSinger.SignatureAlgorithm = Org.BouncyCastle.Security.DigestUtilities.GetAlgorithmName(signer.DigestAlgorithmID.ObjectID);
                if (signer.SignedAttributes[CmsAttributes.SigningTime] != null)
                {
                    Org.BouncyCastle.Asn1.Cms.Attribute sigTime = signer.SignedAttributes[CmsAttributes.SigningTime];
                    if (sigTime.AttrValues.Count > 0)
                    {
                        try
                        {
                            thisSinger.SigningTime = GetSigningTime(sigTime.AttrValues[0]);
                        }
                        catch (Exception e)
                        {
                            ErrorMessageLst.Add("Error retriving SigningTime");
                        }
                    }
                }
            }
            else
            {
                ErrorMessageLst.Add("Missing SignedAttributes");
            }

            if (gestioneTSFirma)
            {
                List <TSInfo> tsArr = new List <TSInfo>();
                if (signer.UnsignedAttributes != null && signer.UnsignedAttributes[Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAASignatureTimeStampToken] != null)
                {
                    ICollection ret = Org.BouncyCastle.Tsp.TspUtil.GetSignatureTimestamps(signer);
                    foreach (Org.BouncyCastle.Tsp.TimeStampToken token in ret)
                    {
                        VerifyTimeStamp verifyTimeStamp = new VerifyTimeStamp();
                        ICollection     certsColl       = token.GetCertificates("COLLECTION").GetMatches(null);
                        TSInfo          timeStamp       = verifyTimeStamp.getTSCertInfo(certsColl);

                        timeStamp.TSdateTime     = token.TimeStampInfo.GenTime.ToLocalTime();
                        timeStamp.TSserialNumber = token.TimeStampInfo.SerialNumber.ToString();
                        timeStamp.TSimprint      = Convert.ToBase64String(token.TimeStampInfo.TstInfo.MessageImprint.GetEncoded());
                        timeStamp.TSdateTime     = token.TimeStampInfo.GenTime;
                        timeStamp.TSType         = TsType.PKCS;
                        tsArr.Add(timeStamp);
                    }
                }

                if (tsArr.Count > 0)
                {
                    thisSinger.SignatureTimeStampInfo = tsArr.ToArray();
                }
            }
            X509Certificate2 cert = new X509Certificate2(cert1.GetEncoded());

            thisSinger.CertificateInfo.RevocationStatus = CheckCertificate(cert);
            thisSinger.CertificateInfo.X509Certificate  = cert1.GetEncoded();

            thisSinger.CertificateInfo.RevocationStatusDescription = DecodeStatus(thisSinger.CertificateInfo.RevocationStatus);
            ParseCNIPASubjectInfo(ref thisSinger.SubjectInfo, cert.SubjectName.Name);

            thisSinger.CertificateInfo.IssuerName         = cert.IssuerName.Name;
            thisSinger.CertificateInfo.SerialNumber       = cert.SerialNumber;
            thisSinger.CertificateInfo.SignatureAlgorithm = cert.SignatureAlgorithm.FriendlyName;
            thisSinger.CertificateInfo.SubjectName        = cert.SubjectName.Name;
            thisSinger.CertificateInfo.ValidFromDate      = cert.NotBefore;
            thisSinger.CertificateInfo.ValidToDate        = cert.NotAfter;
            thisSinger.CertificateInfo.ThumbPrint         = cert.Thumbprint;

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("CertificateInfo.IssuerName: '{0}'", thisSinger.CertificateInfo.IssuerName);
            sb.AppendFormat("CertificateInfo.SerialNumber: '{0}'", thisSinger.CertificateInfo.SerialNumber);
            sb.AppendFormat("CertificateInfo.SignatureAlgorithm: '{0}'", thisSinger.CertificateInfo.SignatureAlgorithm);
            sb.AppendFormat("CertificateInfo.SubjectName: '{0}'", thisSinger.CertificateInfo.SubjectName);
            sb.AppendFormat("CertificateInfo.ValidFromDate: '{0}'", thisSinger.CertificateInfo.ValidFromDate);
            sb.AppendFormat("CertificateInfo.ValidToDate: '{0}'", thisSinger.CertificateInfo.ValidToDate);
            sb.AppendFormat("CertificateInfo.ThumbPrint: '{0}'", thisSinger.CertificateInfo.ThumbPrint);
            //DocsPaUtils.LogsManagement.Debugger.Write(string.Format("SignedDocument.Verify - {0}", sb.ToString()));


            //gestione controfirma
            if (signer.UnsignedAttributes != null)
            {
                if (signer.UnsignedAttributes[CmsAttributes.CounterSignature] != null)
                {
                    List <DocsPaVO.documento.SignerInfo> cSigsList = new List <DocsPaVO.documento.SignerInfo>();
                    List <string>          csignErrs         = new List <string> ();
                    SignerInformationStore counterSignatures = signer.GetCounterSignatures();
                    foreach (SignerInformation conunterSig in counterSignatures.GetSigners())
                    {
                        DocsPaVO.documento.SignerInfo cSigs = ExtractSignerInfo(csignErrs, store, conunterSig);
                        cSigsList.Add(cSigs);
                    }
                    if (csignErrs.Count > 0)
                    {
                        ErrorMessageLst.AddRange(csignErrs);
                    }
                    if (cSigsList.Count > 0)
                    {
                        thisSinger.counterSignatures = cSigsList.ToArray();
                    }
                }
            }
            return(thisSinger);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Crea una nuova versione da un documento acquisito, se è associato un TSR, verrà unito al documento per creare un TSD
        /// </summary>
        /// <param name="infoUtente"></param>
        /// <param name="fileRequest"></param>
        /// <returns></returns>
        public static DocsPaVO.documento.FileRequest CreateTSDVersion(DocsPaVO.utente.InfoUtente infoUtente, DocsPaVO.documento.FileRequest fileRequest)
        {
            byte [] lastTSR = BusinessLogic.Interoperabilità.InteroperabilitaUtils.GetTSRForDocument(infoUtente, fileRequest);
            if (lastTSR == null)
            {
                return(null);
            }

            //Conversione in TSD
            DocsPaVO.documento.FileDocumento docFile = FileManager.getFileFirmato(fileRequest, infoUtente, false);
            BusinessLogic.Documenti.DigitalSignature.VerifyTimeStamp vts = new DigitalSignature.VerifyTimeStamp();
            if (vts.Verify(docFile.content, lastTSR).esito != "OK")
            {
                return(null);
            }

            try
            {
                BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.tsd        tsdMgr = new BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.tsd();
                BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.CryptoFile file   = new BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.CryptoFile {
                    Content = docFile.content, Name = docFile.name, MessageFileType = BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.fileType.Binary
                };
                List <BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.CryptoFile> tsrLst  = new List <BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.CryptoFile>();
                BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.CryptoFile        tsrfile = new BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.CryptoFile {
                    Content = lastTSR, MessageFileType = BusinessLogic.Documenti.DigitalSignature.PKCS_Utils.fileType.Binary
                };
                tsrLst.Add(tsrfile);
                docFile.content        = tsdMgr.create(file, tsrLst.ToArray()).Content;
                docFile.estensioneFile = "tsd";
                docFile.fullName       = docFile.fullName + ".tsd";
                docFile.length         = (int)docFile.content.Length;
                docFile.name           = docFile.name + ".tsd";
                docFile.nomeOriginale  = docFile.nomeOriginale + ".tsd";
            }
            catch (Exception e)
            {
                logger.ErrorFormat("Errore creando il TSD!! {0},{1}", e.Message, e.StackTrace);
                return(null);
            }
            DocsPaVO.documento.FileRequest tsdFileReq;
            if (fileRequest.GetType() == typeof(DocsPaVO.documento.Allegato))
            {
                tsdFileReq = new DocsPaVO.documento.Allegato();
                (tsdFileReq as DocsPaVO.documento.Allegato).numeroPagine = (fileRequest as DocsPaVO.documento.Allegato).numeroPagine;
            }
            else
            {
                tsdFileReq = new DocsPaVO.documento.Documento();
            }

            tsdFileReq.docNumber   = fileRequest.docNumber;
            tsdFileReq.descrizione = "Versione creata per conversione in TSD";
            tsdFileReq.firmato     = fileRequest.firmato;
            tsdFileReq             = BusinessLogic.Documenti.VersioniManager.addVersion(tsdFileReq, infoUtente, false);

            if (tsdFileReq != null)
            {
                BusinessLogic.UserLog.UserLog.WriteLog(infoUtente, "DOCUMENTOAGGIUNGIVERSIONE", tsdFileReq.docNumber, string.Format("{0}{1}{2}{3}", "Aggiunta al N.ro Doc.: ", tsdFileReq.docNumber, " la Ver. ", tsdFileReq.version + " di tipo TSD"), DocsPaVO.Logger.CodAzione.Esito.OK);
            }

            else
            {
                BusinessLogic.UserLog.UserLog.WriteLog(infoUtente, "DOCUMENTOAGGIUNGIVERSIONE", tsdFileReq.docNumber, string.Format("{0}{1}{2}{3}", "Aggiunta al N.ro Doc.: ", tsdFileReq.docNumber, " la Ver. ", tsdFileReq.version + " di tipo TSD"), DocsPaVO.Logger.CodAzione.Esito.KO);
            }


            tsdFileReq = BusinessLogic.Documenti.FileManager.putFile(tsdFileReq, docFile, infoUtente);

            List <DocsPaVO.LibroFirma.FirmaElettronica> firmaE = LibroFirma.LibroFirmaManager.GetFirmaElettronicaDaFileRequest(fileRequest);
            bool isFirmatoElettonicamente = firmaE != null && firmaE.Count > 0;

            if (isFirmatoElettonicamente)
            {
                string impronta = string.Empty;
                DocsPaDB.Query_DocsPAWS.Documenti docInfoDB = new DocsPaDB.Query_DocsPAWS.Documenti();
                docInfoDB.GetImpronta(out impronta, tsdFileReq.versionId, tsdFileReq.docNumber);

                foreach (DocsPaVO.LibroFirma.FirmaElettronica firma in firmaE)
                {
                    firma.UpdateXml(impronta, tsdFileReq.versionId, tsdFileReq.version);
                    LibroFirma.LibroFirmaManager.InserisciFirmaElettronica(firma);
                }
            }

            return(tsdFileReq);
        }