/// <summary> /// /// </summary> /// <param name="x509_certificate2"></param> /// <param name="encrypted_data"></param> /// <returns></returns> public byte[] GetDecryptedContent(X509Certificate2 x509_certificate2, byte[] encrypted_data) { Org.BouncyCastle.Asn1.Cms.ContentInfo _content = Org.BouncyCastle.Asn1.Cms.ContentInfo.GetInstance(Asn1Sequence.FromByteArray(encrypted_data)); EnvelopedData _envelopedData = EnvelopedData.GetInstance(_content.Content); EncryptedContentInfo _encryptedContentInfo = _envelopedData.EncryptedContentInfo; byte[] _encrypt = _encryptedContentInfo.EncryptedContent.GetOctets(); RecipientInfo _recipientInfo = RecipientInfo.GetInstance(_envelopedData.RecipientInfos[0]); KeyTransRecipientInfo _keyTransRecipientInfo = KeyTransRecipientInfo.GetInstance(_recipientInfo.Info); byte[] _byteEncryptedKey = _keyTransRecipientInfo.EncryptedKey.GetOctets(); RSACryptoServiceProvider _rsaCrypto = (RSACryptoServiceProvider)x509_certificate2.PrivateKey; byte[] _randomKey = _rsaCrypto.Decrypt(_byteEncryptedKey, false); AlgorithmIdentifier _contentEncryptionAlgorithm = _encryptedContentInfo.ContentEncryptionAlgorithm; Asn1OctetString _paramIV = Asn1OctetString.GetInstance(_contentEncryptionAlgorithm.Parameters); byte[] _initVector = _paramIV.GetOctets(); tDESCrypto _cryptoService = new tDESCrypto(_randomKey, _initVector); return(_cryptoService.Decrypt(_encrypt)); }
public CmsEnvelopedDataParser( Stream envelopedData) : base(envelopedData) { this._attrNotRead = true; this.envelopedData = new EnvelopedDataParser( (Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence)); // // load the RecepientInfoStore // Asn1SetParser s = this.envelopedData.GetRecipientInfos(); IList baseInfos = new ArrayList(); Asn1Set set = Asn1Set.GetInstance(s.ToAsn1Object()); foreach (object o in set) { baseInfos.Add(RecipientInfo.GetInstance(o)); } // // read the encrypted content info // EncryptedContentInfoParser encInfo = this.envelopedData.GetEncryptedContentInfo(); this._encAlg = encInfo.ContentEncryptionAlgorithm; // // prime the recepients // IList infos = new ArrayList(); Stream dataStream = ((Asn1OctetStringParser)encInfo.GetEncryptedContent(Asn1Tags.OctetString)).GetOctetStream(); foreach (Asn1.Cms.RecipientInfo info in baseInfos) { Asn1Encodable recipInfo = info.Info; if (recipInfo is Asn1.Cms.KeyTransRecipientInfo) { infos.Add(new KeyTransRecipientInformation( (KeyTransRecipientInfo)recipInfo, _encAlg, dataStream)); } else if (recipInfo is Asn1.Cms.KekRecipientInfo) { infos.Add(new KekRecipientInformation( (KekRecipientInfo)recipInfo, _encAlg, dataStream)); } else if (recipInfo is KeyAgreeRecipientInfo) { infos.Add(new KeyAgreeRecipientInformation( (KeyAgreeRecipientInfo)recipInfo, _encAlg, dataStream)); } else if (recipInfo is PasswordRecipientInfo) { infos.Add(new PasswordRecipientInformation( (PasswordRecipientInfo)recipInfo, _encAlg, dataStream)); } } this.recipientInfoStore = new RecipientInformationStore(infos); }
internal static RecipientInformationStore BuildRecipientInformationStore(Asn1Set recipientInfos, CmsSecureReadable secureReadable) { global::System.Collections.IList list = Platform.CreateArrayList(); for (int i = 0; i != recipientInfos.Count; i++) { RecipientInfo instance = RecipientInfo.GetInstance(recipientInfos[i]); ReadRecipientInfo(list, instance, secureReadable); } return(new RecipientInformationStore((global::System.Collections.ICollection)list)); }
public CmsEnvelopedData( ContentInfo contentInfo) { this.contentInfo = contentInfo; EnvelopedData envData = EnvelopedData.GetInstance(contentInfo.Content); // // read the encrypted content info // EncryptedContentInfo encInfo = envData.EncryptedContentInfo; this.encAlg = encInfo.ContentEncryptionAlgorithm; // // load the RecipientInfoStore // Asn1Set s = envData.RecipientInfos; IList infos = new ArrayList(); byte[] contentOctets = encInfo.EncryptedContent.GetOctets(); foreach (Asn1Encodable ae in s) { RecipientInfo info = RecipientInfo.GetInstance(ae); MemoryStream contentStream = new MemoryStream(contentOctets, false); object type = info.Info; if (type is KeyTransRecipientInfo) { infos.Add(new KeyTransRecipientInformation( (KeyTransRecipientInfo)type, encAlg, contentStream)); } else if (type is KekRecipientInfo) { infos.Add(new KekRecipientInformation( (KekRecipientInfo)type, encAlg, contentStream)); } else if (type is KeyAgreeRecipientInfo) { infos.Add(new KeyAgreeRecipientInformation( (KeyAgreeRecipientInfo)type, encAlg, contentStream)); } else if (type is PasswordRecipientInfo) { infos.Add(new PasswordRecipientInformation( (PasswordRecipientInfo)type, encAlg, contentStream)); } } this.recipientInfoStore = new RecipientInformationStore(infos); this.unprotectedAttributes = envData.UnprotectedAttrs; }
internal static RecipientInformationStore BuildRecipientInformationStore(Asn1Set recipientInfos, CmsSecureReadable secureReadable) { IList list = Platform.CreateArrayList(); for (int num = 0; num != recipientInfos.Count; num++) { RecipientInfo instance = RecipientInfo.GetInstance(recipientInfos[num]); CmsEnvelopedHelper.ReadRecipientInfo(list, instance, secureReadable); } return(new RecipientInformationStore(list)); }
internal static RecipientInformationStore BuildRecipientInformationStore( Asn1Set recipientInfos, CmsSecureReadable secureReadable) { IList infos = Platform.CreateArrayList(); for (int i = 0; i != recipientInfos.Count; i++) { RecipientInfo info = RecipientInfo.GetInstance(recipientInfos[i]); ReadRecipientInfo(infos, info, secureReadable); } return(new RecipientInformationStore(infos)); }
internal static RecipientInformationStore BuildRecipientInformationStore( Asn1Set recipientInfos, AlgorithmIdentifier messageAlgorithm, ICmsSecureReadable secureReadable, IAuthAttributesProvider additionalData) { IList <RecipientInformation> infos = new List <RecipientInformation>(); for (int i = 0; i != recipientInfos.Count; i++) { RecipientInfo info = RecipientInfo.GetInstance(recipientInfos[i]); readRecipientInfo(infos, info, messageAlgorithm, secureReadable, additionalData); } return(new RecipientInformationStore(infos)); }
internal static RecipientInformationStore BuildRecipientInformationStore( Asn1Set recipientInfos, CmsSecureReadable secureReadable) { IList infos = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(); for (int i = 0; i != recipientInfos.Count; i++) { RecipientInfo info = RecipientInfo.GetInstance(recipientInfos[i]); ReadRecipientInfo(infos, info, secureReadable); } return(new RecipientInformationStore(infos)); }
public static int CalculateVersion(OriginatorInfo originatorInfo, Asn1Set recipientInfos, Asn1Set unprotectedAttrs) { if (originatorInfo != null || unprotectedAttrs != null) { return(2); } foreach (object recipientInfo in recipientInfos) { RecipientInfo instance = RecipientInfo.GetInstance(recipientInfo); if (instance.Version.Value.IntValue != 0) { return(2); } } return(0); }
internal static IList ReadRecipientInfos( Asn1Set recipientInfos, byte[] contentOctets, AlgorithmIdentifier encAlg, AlgorithmIdentifier macAlg, AlgorithmIdentifier authEncAlg) { IList infos = new ArrayList(); foreach (Asn1Encodable ae in recipientInfos) { RecipientInfo info = RecipientInfo.GetInstance(ae); MemoryStream contentStream = new MemoryStream(contentOctets, false); ReadRecipientInfo(infos, info, contentStream, encAlg, macAlg, authEncAlg); } return(infos); }
public CmsAuthenticatedDataParser( Stream envelopedData) : base(envelopedData) { this.authAttrNotRead = true; this.authData = new AuthenticatedDataParser( (Asn1SequenceParser)contentInfo.GetContent(Asn1Tags.Sequence)); // TODO Validate version? //DerInteger version = this.authData.getVersion(); // // load the RecipientInfoStore // Asn1SetParser s = authData.GetRecipientInfos(); IList baseInfos = new ArrayList(); IAsn1Convertible entry; while ((entry = s.ReadObject()) != null) { baseInfos.Add(RecipientInfo.GetInstance(entry.ToAsn1Object())); } this.macAlg = authData.GetMacAlgorithm(); // // read the encrypted content info // ContentInfoParser data = authData.GetEnapsulatedContentInfo(); // // prime the recipients // Stream contentStream = ((Asn1OctetStringParser)data.GetContent(Asn1Tags.OctetString)).GetOctetStream(); IList infos = CmsEnvelopedHelper.ReadRecipientInfos( baseInfos, contentStream, null, macAlg, null); _recipientInfoStore = new RecipientInformationStore(infos); }
public CmsEnvelopedDataParser( Stream envelopedData) : base(envelopedData) { this._attrNotRead = true; this.envelopedData = new EnvelopedDataParser( (Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence)); // TODO Validate version? //DerInteger version = this.envelopedData.Version; // // load the RecipientInfoStore // Asn1SetParser s = this.envelopedData.GetRecipientInfos(); IList baseInfos = new ArrayList(); IAsn1Convertible entry; while ((entry = s.ReadObject()) != null) { baseInfos.Add(RecipientInfo.GetInstance(entry.ToAsn1Object())); } // // read the encrypted content info // EncryptedContentInfoParser encInfo = this.envelopedData.GetEncryptedContentInfo(); this._encAlg = encInfo.ContentEncryptionAlgorithm; // // prime the recipients // Stream contentStream = ((Asn1OctetStringParser)encInfo.GetEncryptedContent(Asn1Tags.OctetString)).GetOctetStream(); IList infos = CmsEnvelopedHelper.ReadRecipientInfos( baseInfos, contentStream, _encAlg, null, null); this.recipientInfoStore = new RecipientInformationStore(infos); }
private ITestResult EnvelopedTest() { try { // Key trans ContentInfo info = ContentInfo.GetInstance( Asn1Object.FromByteArray(envDataKeyTrns)); EnvelopedData envData = EnvelopedData.GetInstance(info.Content); Asn1Set s = envData.RecipientInfos; if (s.Count != 1) { return(new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped, wrong number of recipients")); } RecipientInfo recip = RecipientInfo.GetInstance(s[0]); if (recip.Info is KeyTransRecipientInfo) { KeyTransRecipientInfo inf = KeyTransRecipientInfo.GetInstance(recip.Info); inf = new KeyTransRecipientInfo(inf.RecipientIdentifier, inf.KeyEncryptionAlgorithm, inf.EncryptedKey); s = new DerSet(new RecipientInfo(inf)); } else { return(new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped, wrong recipient type")); } envData = new EnvelopedData(envData.OriginatorInfo, s, envData.EncryptedContentInfo, envData.UnprotectedAttrs); info = new ContentInfo(CmsObjectIdentifiers.EnvelopedData, envData); if (!Arrays.AreEqual(info.GetEncoded(), envDataKeyTrns)) { return(new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped failed to re-encode")); } // KEK info = ContentInfo.GetInstance( Asn1Object.FromByteArray(envDataKEK)); envData = EnvelopedData.GetInstance(info.Content); s = envData.RecipientInfos; if (s.Count != 1) { return(new SimpleTestResult(false, Name + ": CMS KEK enveloped, wrong number of recipients")); } recip = RecipientInfo.GetInstance(s[0]); if (recip.Info is KekRecipientInfo) { KekRecipientInfo inf = KekRecipientInfo.GetInstance(recip.Info); inf = new KekRecipientInfo(inf.KekID, inf.KeyEncryptionAlgorithm, inf.EncryptedKey); s = new DerSet(new RecipientInfo(inf)); } else { return(new SimpleTestResult(false, Name + ": CMS KEK enveloped, wrong recipient type")); } envData = new EnvelopedData(envData.OriginatorInfo, s, envData.EncryptedContentInfo, envData.UnprotectedAttrs); info = new ContentInfo(CmsObjectIdentifiers.EnvelopedData, envData); if (!Arrays.AreEqual(info.GetEncoded(), envDataKEK)) { return(new SimpleTestResult(false, Name + ": CMS KEK enveloped failed to re-encode")); } // Nested NDEF problem Asn1StreamParser asn1In = new Asn1StreamParser(new MemoryStream(envDataNestedNDEF, false)); ContentInfoParser ci = new ContentInfoParser((Asn1SequenceParser)asn1In.ReadObject()); EnvelopedDataParser ed = new EnvelopedDataParser((Asn1SequenceParser)ci .GetContent(Asn1Tags.Sequence)); Touch(ed.Version); ed.GetOriginatorInfo(); ed.GetRecipientInfos().ToAsn1Object(); EncryptedContentInfoParser eci = ed.GetEncryptedContentInfo(); Touch(eci.ContentType); Touch(eci.ContentEncryptionAlgorithm); Stream dataIn = ((Asn1OctetStringParser)eci.GetEncryptedContent(Asn1Tags.OctetString)) .GetOctetStream(); Streams.Drain(dataIn); dataIn.Close(); // Test data doesn't have unprotected attrs, bug was being thrown by this call Asn1SetParser upa = ed.GetUnprotectedAttrs(); if (upa != null) { upa.ToAsn1Object(); } return(new SimpleTestResult(true, Name + ": Okay")); } catch (Exception e) { return(new SimpleTestResult(false, Name + ": CMS enveloped failed - " + e.ToString(), e)); } }