private static void getAdditionalInfos(byte[] contents, string subFilter, out string digestAlgOid, out byte[] messageDigest, out bool isEpes) { Org.BouncyCastle.Cms.CmsSignedData signedData = new Org.BouncyCastle.Cms.CmsSignedData( Org.BouncyCastle.Asn1.Cms.ContentInfo.GetInstance(( new Org.BouncyCastle.Asn1.Asn1InputStream(contents)).ReadObject())); Org.BouncyCastle.Cms.SignerInformationStore sigInfStore = signedData.GetSignerInfos(); System.Collections.IEnumerator signers = sigInfStore.GetSigners().GetEnumerator(); signers.Reset(); signers.MoveNext(); Org.BouncyCastle.Cms.SignerInformation signerInfo = (Org.BouncyCastle.Cms.SignerInformation)signers.Current; messageDigest = null; digestAlgOid = signerInfo.DigestAlgOid; isEpes = false; if (subFilter.ToLower() == "adbe.pkcs7.sha1") { digestAlgOid = "1.3.14.3.2.26"; messageDigest = ((Org.BouncyCastle.Asn1.DerOctetString)Org.BouncyCastle.Asn1.Cms.SignedData.GetInstance(signedData.ContentInfo.Content).EncapContentInfo.Content).GetOctets(); } else { if (signerInfo.SignedAttributes != null) { //messageDigest Org.BouncyCastle.Asn1.Cms.Attribute messageDigestAttr = signerInfo.SignedAttributes[Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.Pkcs9AtMessageDigest]; if (messageDigestAttr != null && messageDigestAttr.AttrValues.Count > 0) { messageDigest = Org.BouncyCastle.Asn1.DerOctetString.GetInstance(messageDigestAttr.AttrValues[0]).GetOctets(); } //signature policy isEpes = (signerInfo.SignedAttributes[Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAAEtsSigPolicyID] != null && signerInfo.SignedAttributes[Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAAEtsSigPolicyID].AttrValues.Count > 0); } } }
/// <summary> /// Initializes a new instance of the <see cref="TimeStampToken"/> class. /// </summary> /// <param name="encodedToken">DER encoded time stamp token</param> public TimeStampToken(byte[] encodedToken) { if (null == encodedToken) { throw new ArgumentNullException("encodedToken"); } EncodedToken = encodedToken; timeStampToken = new Org.BouncyCastle.Tsp.TimeStampToken(new Org.BouncyCastle.Cms.CmsSignedData(encodedToken)); this.MessageImprint = new MsgImprint(this.timeStampToken.TimeStampInfo.GetMessageImprintDigest(), this.timeStampToken.TimeStampInfo.MessageImprintAlgOid); X509Certificate2 tsaCert = null; Org.BouncyCastle.Cms.SignerID signerId = this.timeStampToken.SignerID; Org.BouncyCastle.Cms.CmsSignedData cmsSignedData = new Org.BouncyCastle.Cms.CmsSignedData(this.timeStampToken.GetEncoded()); ICollection signerCerts = cmsSignedData.GetCertificates("COLLECTION").GetMatches(this.timeStampToken.SignerID); foreach (Org.BouncyCastle.X509.X509Certificate cert in signerCerts) { tsaCert = new X509Certificate2(cert.GetEncoded()); } this.TsaInformation = new TsaId(signerId, tsaCert); }