public m7m(byte[] m7mFile) { _m7m = new CryptoFile { Content = m7mFile, MessageFileType = fileType.Unknown, Name = "default.m7m" }; explode(_m7m.Content); }
/// <summary> /// Esplode un M7m infocert nelle sue due compomenti (un p7m e un tsr) /// </summary> /// <param name="fileContents">bytearray contente il messaggio m7m</param> public void explode(byte[] fileContents) { int posi = BusinessLogic.Documenti.DigitalSignature.Helpers.IndexOfInArray(fileContents, System.Text.ASCIIEncoding.ASCII.GetBytes("Mime-Version:")); if (posi == 0) //E' un mime m7m { using (MemoryStream ms = new MemoryStream(fileContents)) { anmar.SharpMimeTools.SharpMessage sm = new anmar.SharpMimeTools.SharpMessage(ms); if (sm.Attachments.Count > 0) { foreach (anmar.SharpMimeTools.SharpAttachment att in sm.Attachments) { if (System.IO.Path.GetExtension(att.Name).ToLower().Contains("p7m")) { att.Stream.Position = 0; BinaryReader sr = new BinaryReader(att.Stream); _p7m = new CryptoFile { Content = sr.ReadBytes((int)att.Size), MessageFileType = CryptoFile.mapType(att.ContentTransferEncoding), Name = att.Name }; } if (System.IO.Path.GetExtension(att.Name).ToLower().Contains("tsr")) { att.Stream.Position = 0; BinaryReader sr = new BinaryReader(att.Stream); _tsr.Add(new CryptoFile { Content = sr.ReadBytes((int)att.Size), MessageFileType = CryptoFile.mapType(att.ContentTransferEncoding), Name = att.Name }); } } } } } }
public CryptoFile create(CryptoFile p7mFile, CryptoFile[] tsrFiles) { MemoryStream m7mStream = new MemoryStream(); string p7mName = p7mFile.Name; if (p7mName == null) { p7mName = "default.p7m"; } StringBuilder sb = new StringBuilder(); sb.AppendLine("Mime-Version: 1.0"); sb.AppendLine("Content-Type: multipart/mixed; boundary=\"DiKeCades\""); sb.AppendLine(""); sb.AppendLine("--DiKeCades"); sb.AppendLine(String.Format("Content-Type: application/pkcs7-mime; smime-type=signed-data; name=\"{0}\"", p7mName)); sb.AppendLine("Content-Transfer-Encoding: binary"); sb.AppendLine(String.Format("Content-Disposition: attachment; filename=\"{0}\"", p7mName)); sb.AppendLine("Content-Description: Signed envelope"); sb.AppendLine(""); byte[] header = System.Text.ASCIIEncoding.Default.GetBytes(sb.ToString()); m7mStream.Write(header, 0, header.Length); m7mStream.Write(p7mFile.Content, 0, p7mFile.Content.Length); int counter = 1; foreach (CryptoFile tsrFile in tsrFiles) { sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine("--DiKeCades"); sb.AppendLine(String.Format("Content-Type: application/timestamp-reply; name=\"{0}\"", tsrFile.Name)); sb.AppendLine("Content-Transfer-Encoding: base64"); sb.AppendLine(String.Format("Content-Disposition: attachment; filename=\"{0}\"", tsrFile.Name)); sb.AppendLine("Content-Description: time-stamp response"); sb.AppendLine(""); sb.AppendLine(CryptoFile.splitInLines(tsrFile.Base64Content, 76)); if (counter++ == tsrFiles.Length) { sb.AppendLine(""); sb.AppendLine("--DiKeCades--"); } header = System.Text.ASCIIEncoding.Default.GetBytes(sb.ToString()); m7mStream.Write(header, 0, header.Length); } m7mStream.Position = 0; BinaryReader br = new BinaryReader(m7mStream); CryptoFile retval = new CryptoFile { Content = br.ReadBytes((int)m7mStream.Length), MessageFileType = fileType.Unknown, Name = p7mName.ToLower().Replace("p7m", "m7m") }; return(retval); }
public CryptoFile create(CryptoFile p7mFile, CryptoFile[] tsrFiles) { List <TimeStampAndCrl> tsCRLLst = new List <TimeStampAndCrl>(); Asn1OctetString p7mOctecString = (Asn1OctetString) new DerOctetString(p7mFile.Content).ToAsn1Object(); foreach (CryptoFile tsrFile in tsrFiles) { TimeStampResponse tsr = new TimeStampResponse(tsrFile.Content); TimeStampAndCrl tsCRL = new TimeStampAndCrl(tsr.TimeStampToken.ToCmsSignedData().ContentInfo); tsCRLLst.Add(tsCRL); } Evidence ev = new Evidence(new TimeStampTokenEvidence(tsCRLLst.ToArray())); TimeStampedData newTSD = new TimeStampedData(null, null, p7mOctecString, ev); ContentInfo info = new ContentInfo(CmsObjectIdentifiers.timestampedData, newTSD.ToAsn1Object()); CryptoFile retval = new CryptoFile { Content = info.GetDerEncoded(), Name = "default.tsd", MessageFileType = fileType.Binary }; return(retval); }
public void explode(byte[] fileContents) { try { Asn1Sequence sequenza = Asn1Sequence.GetInstance(fileContents); DerObjectIdentifier tsdOIDFile = sequenza[0] as DerObjectIdentifier; if (tsdOIDFile != null) { if (tsdOIDFile.Id == CmsObjectIdentifiers.timestampedData.Id) //TSD { DerTaggedObject taggedObject = sequenza[1] as DerTaggedObject; if (taggedObject != null) { Asn1Sequence asn1seq = Asn1Sequence.GetInstance(taggedObject, true); TimeStampedData tsd = TimeStampedData.GetInstance(asn1seq); _p7m = new CryptoFile { Content = tsd.Content.GetOctets(), MessageFileType = fileType.Binary, Name = "default.p7m" }; TimeStampAndCrl[] crlTS = tsd.TemporalEvidence.TstEvidence.ToTimeStampAndCrlArray(); foreach (TimeStampAndCrl tokCRL in crlTS) { TimeStampToken tsToken = new TimeStampToken(tokCRL.TimeStampToken); ContentInfo o = tokCRL.TimeStampToken; Org.BouncyCastle.Asn1.Cmp.PkiStatusInfo si = new Org.BouncyCastle.Asn1.Cmp.PkiStatusInfo(0); Org.BouncyCastle.Asn1.Tsp.TimeStampResp re = new Org.BouncyCastle.Asn1.Tsp.TimeStampResp(si, o); string serial = tsToken.TimeStampInfo.SerialNumber.ToString(); _tsr.Add(new CryptoFile { Content = re.GetEncoded(), Name = String.Format("default.{0}.tsr", serial), MessageFileType = fileType.Binary }); } } } } } catch (Exception e) { throw e; } }
public m7m(CryptoFile M7Mfile) { explode(M7Mfile.Content); }
public m7m(CryptoFile p7mFile, List <CryptoFile> tsrFiles) { this._p7m = p7mFile; this._tsr = tsrFiles; create(p7mFile, tsrFiles.ToArray()); }
public tsd(CryptoFile TSDfile) { }
public tsd(CryptoFile p7mFile, CryptoFile tsrFile) { }