public KeyAgreeRecipientInformation(
            KeyAgreeRecipientInfo info,
            AlgorithmIdentifier encAlg,
            Stream data)
            : base(encAlg, AlgorithmIdentifier.GetInstance(info.KeyEncryptionAlgorithm), data)
        {
            _info = info;
//			_encAlg = encAlg;

            try
            {
                Asn1Sequence          s  = _info.RecipientEncryptedKeys;
                RecipientEncryptedKey id = RecipientEncryptedKey.GetInstance(s[0]);

                Asn1.Cms.IssuerAndSerialNumber iAnds = id.Identifier.IssuerAndSerialNumber;

//				byte[] issuerBytes = iAnds.Name.GetEncoded();

                _rid = new RecipientID();
//				_rid.SetIssuer(issuerBytes);
                _rid.Issuer       = iAnds.Name;
                _rid.SerialNumber = iAnds.SerialNumber.Value;

                _encryptedKey = id.EncryptedKey;
            }
            catch (IOException e)
            {
                throw new ArgumentException("invalid rid in KeyAgreeRecipientInformation", e);
            }
        }
 internal static void ReadRecipientInfo(IList infos, KeyAgreeRecipientInfo info, CmsSecureReadable secureReadable)
 {
     try
     {
         foreach (Asn1Encodable asn1Encodable in info.RecipientEncryptedKeys)
         {
             RecipientEncryptedKey       instance    = RecipientEncryptedKey.GetInstance(asn1Encodable.ToAsn1Object());
             RecipientID                 recipientID = new RecipientID();
             KeyAgreeRecipientIdentifier identifier  = instance.Identifier;
             Org.BouncyCastle.Asn1.Cms.IssuerAndSerialNumber issuerAndSerialNumber = identifier.IssuerAndSerialNumber;
             if (issuerAndSerialNumber != null)
             {
                 recipientID.Issuer       = issuerAndSerialNumber.Name;
                 recipientID.SerialNumber = issuerAndSerialNumber.SerialNumber.Value;
             }
             else
             {
                 RecipientKeyIdentifier rKeyID = identifier.RKeyID;
                 recipientID.SubjectKeyIdentifier = rKeyID.SubjectKeyIdentifier.GetOctets();
             }
             infos.Add(new KeyAgreeRecipientInformation(info, recipientID, instance.EncryptedKey, secureReadable));
         }
     }
     catch (IOException innerException)
     {
         throw new ArgumentException("invalid rid in KeyAgreeRecipientInformation", innerException);
     }
 }
Exemple #3
0
 internal static void ReadRecipientInfo(global::System.Collections.IList infos, KeyAgreeRecipientInfo info, CmsSecureReadable secureReadable)
 {
     //IL_00bb: Expected O, but got Unknown
     //IL_00c2: Unknown result type (might be due to invalid IL or missing references)
     try
     {
         global::System.Collections.IEnumerator enumerator = info.RecipientEncryptedKeys.GetEnumerator();
         try
         {
             while (enumerator.MoveNext())
             {
                 Asn1Encodable               asn1Encodable = (Asn1Encodable)enumerator.get_Current();
                 RecipientEncryptedKey       instance      = RecipientEncryptedKey.GetInstance(asn1Encodable.ToAsn1Object());
                 RecipientID                 recipientID   = new RecipientID();
                 KeyAgreeRecipientIdentifier identifier    = instance.Identifier;
                 Org.BouncyCastle.Asn1.Cms.IssuerAndSerialNumber issuerAndSerialNumber = identifier.IssuerAndSerialNumber;
                 if (issuerAndSerialNumber != null)
                 {
                     recipientID.Issuer       = issuerAndSerialNumber.Name;
                     recipientID.SerialNumber = issuerAndSerialNumber.SerialNumber.Value;
                 }
                 else
                 {
                     RecipientKeyIdentifier rKeyID = identifier.RKeyID;
                     recipientID.SubjectKeyIdentifier = rKeyID.SubjectKeyIdentifier.GetOctets();
                 }
                 infos.Add((object)new KeyAgreeRecipientInformation(info, recipientID, instance.EncryptedKey, secureReadable));
             }
         }
         finally
         {
             global::System.IDisposable disposable = enumerator as global::System.IDisposable;
             if (disposable != null)
             {
                 disposable.Dispose();
             }
         }
     }
     catch (IOException val)
     {
         IOException val2 = val;
         throw new ArgumentException("invalid rid in KeyAgreeRecipientInformation", (global::System.Exception)(object) val2);
     }
 }
        internal static void ReadRecipientInfo(IList infos, KeyAgreeRecipientInfo info,
                                               AlgorithmIdentifier encAlg, AlgorithmIdentifier macAlg, AlgorithmIdentifier authEncAlg,
                                               Stream data)
        {
            try
            {
                foreach (Asn1Encodable rek in info.RecipientEncryptedKeys)
                {
                    RecipientEncryptedKey id = RecipientEncryptedKey.GetInstance(rek.ToAsn1Object());

                    RecipientID rid = new RecipientID();

                    Asn1.Cms.KeyAgreeRecipientIdentifier karid = id.Identifier;

                    Asn1.Cms.IssuerAndSerialNumber iAndSN = karid.IssuerAndSerialNumber;
                    if (iAndSN != null)
                    {
                        rid.Issuer       = iAndSN.Name;
                        rid.SerialNumber = iAndSN.SerialNumber.Value;
                    }
                    else
                    {
                        Asn1.Cms.RecipientKeyIdentifier rKeyID = karid.RKeyID;

                        // Note: 'date' and 'other' fields of RecipientKeyIdentifier appear to be only informational

                        rid.SubjectKeyIdentifier = rKeyID.SubjectKeyIdentifier.GetOctets();
                    }

                    infos.Add(new KeyAgreeRecipientInformation(info, rid, id.EncryptedKey,
                                                               encAlg, macAlg, authEncAlg, data));
                }
            }
            catch (IOException e)
            {
                throw new ArgumentException("invalid rid in KeyAgreeRecipientInformation", e);
            }
        }
Exemple #5
0
            internal RecipientInfo ToRecipientInfo(
                KeyParameter key,
                SecureRandom random)
            {
                byte[] keyBytes = key.GetKey();

                if (pubKey != null)
                {
                    IWrapper keyWrapper = Helper.CreateWrapper(keyEncAlg.ObjectID.Id);

                    keyWrapper.Init(true, new ParametersWithRandom(pubKey, random));

                    Asn1OctetString encKey = new DerOctetString(
                        keyWrapper.Wrap(keyBytes, 0, keyBytes.Length));

                    RecipientIdentifier recipId;
                    if (cert != null)
                    {
                        TbsCertificateStructure tbs = TbsCertificateStructure.GetInstance(
                            Asn1Object.FromByteArray(cert.GetTbsCertificate()));

                        Asn1.Cms.IssuerAndSerialNumber encSid = new Asn1.Cms.IssuerAndSerialNumber(
                            tbs.Issuer, tbs.SerialNumber.Value);

                        recipId = new RecipientIdentifier(encSid);
                    }
                    else
                    {
                        recipId = new RecipientIdentifier(subKeyId);
                    }

                    return(new RecipientInfo(new KeyTransRecipientInfo(recipId, keyEncAlg, encKey)));
                }
                else if (originator != null)
                {
                    IWrapper keyWrapper = Helper.CreateWrapper(
                        DerObjectIdentifier.GetInstance(
                            Asn1Sequence.GetInstance(keyEncAlg.Parameters)[0]).Id);

                    keyWrapper.Init(true, new ParametersWithRandom(secKey, random));

                    Asn1OctetString encKey = new DerOctetString(
                        keyWrapper.Wrap(keyBytes, 0, keyBytes.Length));

                    RecipientEncryptedKey rKey = new RecipientEncryptedKey(
                        new KeyAgreeRecipientIdentifier(
                            new Asn1.Cms.IssuerAndSerialNumber(
                                PrincipalUtilities.GetIssuerX509Principal(cert),
                                cert.SerialNumber)),
                        encKey);

                    return(new RecipientInfo(
                               new KeyAgreeRecipientInfo(originator, ukm, keyEncAlg, new DerSequence(rKey))));
                }
                else if (derivationAlg != null)
                {
                    string   rfc3211WrapperName = Helper.GetRfc3211WrapperName(secKeyAlgorithm);
                    IWrapper keyWrapper         = Helper.CreateWrapper(rfc3211WrapperName);


                    // Note: In Java build, the IV is automatically generated in JCE layer
                    int    ivLength = rfc3211WrapperName.StartsWith("DESEDE") ? 8 : 16;
                    byte[] iv       = new byte[ivLength];
                    random.NextBytes(iv);


                    ICipherParameters parameters = new ParametersWithIV(secKey, iv);
                    keyWrapper.Init(true, new ParametersWithRandom(parameters, random));

                    Asn1OctetString encKey = new DerOctetString(
                        keyWrapper.Wrap(keyBytes, 0, keyBytes.Length));

//					byte[] iv = keyWrapper.GetIV();

                    DerSequence seq = new DerSequence(
                        new DerObjectIdentifier(secKeyAlgorithm),
                        new DerOctetString(iv));

                    keyEncAlg = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdAlgPwriKek, seq);

                    return(new RecipientInfo(new PasswordRecipientInfo(derivationAlg, keyEncAlg, encKey)));
                }
                else
                {
                    IWrapper keyWrapper = Helper.CreateWrapper(keyEncAlg.ObjectID.Id);

                    keyWrapper.Init(true, new ParametersWithRandom(secKey, random));

                    Asn1OctetString encKey = new DerOctetString(
                        keyWrapper.Wrap(keyBytes, 0, keyBytes.Length));

                    return(new RecipientInfo(new KekRecipientInfo(secKeyId, keyEncAlg, encKey)));
                }
            }