KeyTransRecipientInformation
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Operators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.IO;
namespace Org.BouncyCastle.Cms
{
public class KeyTransRecipientInformation : RecipientInformation
{
private readonly KeyTransRecipientInfo m_info;
internal KeyTransRecipientInformation(KeyTransRecipientInfo info, CmsSecureReadable secureReadable)
: base(info.KeyEncryptionAlgorithm, secureReadable)
{
rid = new RecipientID();
m_info = info;
RecipientIdentifier recipientIdentifier = info.RecipientIdentifier;
try {
if (recipientIdentifier.IsTagged) {
SubjectKeyIdentifier instance = SubjectKeyIdentifier.GetInstance(recipientIdentifier.ID);
rid.SubjectKeyIdentifier = instance.GetEncoded("DER");
} else {
Org.BouncyCastle.Asn1.Cms.IssuerAndSerialNumber instance2 = Org.BouncyCastle.Asn1.Cms.IssuerAndSerialNumber.GetInstance(recipientIdentifier.ID);
rid.Issuer = instance2.Issuer;
rid.SerialNumber = instance2.SerialNumber.Value;
}
} catch (IOException) {
throw new ArgumentException("invalid rid in KeyTransRecipientInformation");
}
}
private string GetExchangeEncryptionAlgorithmName(AlgorithmIdentifier algID)
{
DerObjectIdentifier algorithm = algID.Algorithm;
if (PkcsObjectIdentifiers.RsaEncryption.Equals(algorithm))
return "RSA//PKCS1Padding";
if (PkcsObjectIdentifiers.IdRsaesOaep.Equals(algorithm)) {
string algorithmName = DigestUtilities.GetAlgorithmName(RsaesOaepParameters.GetInstance(algID.Parameters).HashAlgorithm.Algorithm);
return "RSA//OAEPWITH" + algorithmName + "ANDMGF1Padding";
}
return algorithm.GetID();
}
internal KeyParameter UnwrapKey(ICipherParameters key)
{
byte[] octets = m_info.EncryptedKey.GetOctets();
try {
if (!PkcsObjectIdentifiers.IdRsaesOaep.Equals(keyEncAlg.Algorithm)) {
IWrapper wrapper = WrapperUtilities.GetWrapper(GetExchangeEncryptionAlgorithmName(keyEncAlg));
wrapper.Init(false, key);
return ParameterUtilities.CreateKeyParameter(GetContentAlgorithmName(), wrapper.Unwrap(octets, 0, octets.Length));
}
IKeyUnwrapper keyUnwrapper = new Asn1KeyUnwrapper(keyEncAlg.Algorithm, keyEncAlg.Parameters, key);
return ParameterUtilities.CreateKeyParameter(GetContentAlgorithmName(), keyUnwrapper.Unwrap(octets, 0, octets.Length).Collect());
} catch (SecurityUtilityException innerException) {
throw new CmsException("couldn't create cipher.", innerException);
} catch (InvalidKeyException innerException2) {
throw new CmsException("key invalid in message.", innerException2);
} catch (DataLengthException innerException3) {
throw new CmsException("illegal blocksize in message.", innerException3);
} catch (InvalidCipherTextException innerException4) {
throw new CmsException("bad padding in message.", innerException4);
}
}
public override CmsTypedStream GetContentStream(ICipherParameters key)
{
KeyParameter sKey = UnwrapKey(key);
return GetContentFromSessionKey(sKey);
}
}
}