Inheritance: Org.BouncyCastle.Asn1.Asn1Encodable
Ejemplo n.º 1
0
 public EnvelopedData(OriginatorInfo originatorInfo, Asn1Set recipientInfos, EncryptedContentInfo encryptedContentInfo, Attributes unprotectedAttrs)
 {
     this.version              = new DerInteger(EnvelopedData.CalculateVersion(originatorInfo, recipientInfos, Asn1Set.GetInstance(unprotectedAttrs)));
     this.originatorInfo       = originatorInfo;
     this.recipientInfos       = recipientInfos;
     this.encryptedContentInfo = encryptedContentInfo;
     this.unprotectedAttrs     = Asn1Set.GetInstance(unprotectedAttrs);
 }
        private Asn1Object CreateDERForRecipient(byte[] inp, X509Certificate cert) {
            
            String s = "1.2.840.113549.3.2";
            
            byte[] outp = new byte[100];
            DerObjectIdentifier derob = new DerObjectIdentifier(s);
            byte[] keyp = IVGenerator.GetIV(16);
            IBufferedCipher cf = CipherUtilities.GetCipher(derob);
            KeyParameter kp = new KeyParameter(keyp);
            byte[] iv = IVGenerator.GetIV(cf.GetBlockSize());
            ParametersWithIV piv = new ParametersWithIV(kp, iv);
            cf.Init(true, piv);
            int len = cf.DoFinal(inp, outp, 0);

            byte[] abyte1 = new byte[len];
            System.Array.Copy(outp, 0, abyte1, 0, len);
            DerOctetString deroctetstring = new DerOctetString(abyte1);
            KeyTransRecipientInfo keytransrecipientinfo = ComputeRecipientInfo(cert, keyp);
            DerSet derset = new DerSet(new RecipientInfo(keytransrecipientinfo));
            Asn1EncodableVector ev = new Asn1EncodableVector();
            ev.Add(new DerInteger(58));
            ev.Add(new DerOctetString(iv));
            DerSequence seq = new DerSequence(ev);
            AlgorithmIdentifier algorithmidentifier = new AlgorithmIdentifier(derob, seq);
            EncryptedContentInfo encryptedcontentinfo = 
                new EncryptedContentInfo(PkcsObjectIdentifiers.Data, algorithmidentifier, deroctetstring);
            Asn1Set set = null;
            EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, set);
            Org.BouncyCastle.Asn1.Cms.ContentInfo contentinfo = 
                new Org.BouncyCastle.Asn1.Cms.ContentInfo(PkcsObjectIdentifiers.EnvelopedData, env);
            return contentinfo.ToAsn1Object();        
        }
Ejemplo n.º 3
0
 public EncryptedKey(EnvelopedData envelopedData)
 {
     this.envelopedData = envelopedData;
 }
Ejemplo n.º 4
0
		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);
			}
		}
Ejemplo n.º 5
0
 public static EnvelopedData GetInstance(Asn1TaggedObject obj, bool explicitly)
 {
     return(EnvelopedData.GetInstance(Asn1Sequence.GetInstance(obj, explicitly)));
 }