From 85517b70218d9a771c23634d98eebb60c74a499e Mon Sep 17 00:00:00 2001 From: Konradsop Date: Thu, 30 Jul 2026 16:01:03 +0200 Subject: [PATCH 1/2] Whitespace cleanup in CMS recipient types --- crypto/src/cms/KEKRecipientInformation.cs | 52 +++++++++++------------ crypto/src/cms/RecipientId.cs | 12 +++--- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/crypto/src/cms/KEKRecipientInformation.cs b/crypto/src/cms/KEKRecipientInformation.cs index 649760103c..53b7f9ae9f 100644 --- a/crypto/src/cms/KEKRecipientInformation.cs +++ b/crypto/src/cms/KEKRecipientInformation.cs @@ -18,45 +18,45 @@ public class KekRecipientInformation { private KekRecipientInfo info; - internal KekRecipientInformation( - KekRecipientInfo info, - CmsSecureReadable secureReadable) - : base(info.KeyEncryptionAlgorithm, secureReadable) - { + internal KekRecipientInformation( + KekRecipientInfo info, + CmsSecureReadable secureReadable) + : base(info.KeyEncryptionAlgorithm, secureReadable) + { this.info = info; this.rid = new RecipientID(); - KekIdentifier kekId = info.KekID; + KekIdentifier kekId = info.KekID; - rid.KeyIdentifier = kekId.KeyIdentifier.GetOctets(); + rid.KeyIdentifier = kekId.KeyIdentifier.GetOctets(); } - /** + /** * decrypt the content and return an input stream. */ public override CmsTypedStream GetContentStream( ICipherParameters key) { - try - { - byte[] encryptedKey = info.EncryptedKey.GetOctets(); + try + { + byte[] encryptedKey = info.EncryptedKey.GetOctets(); IWrapper keyWrapper = WrapperUtilities.GetWrapper(keyEncAlg.Algorithm); - keyWrapper.Init(false, key); - - KeyParameter sKey = ParameterUtilities.CreateKeyParameter( - GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length)); - - return GetContentFromSessionKey(sKey); - } - catch (SecurityUtilityException e) - { - throw new CmsException("couldn't create cipher.", e); - } - catch (InvalidKeyException e) - { - throw new CmsException("key invalid in message.", e); - } + keyWrapper.Init(false, key); + + KeyParameter sKey = ParameterUtilities.CreateKeyParameter( + GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length)); + + return GetContentFromSessionKey(sKey); + } + catch (SecurityUtilityException e) + { + throw new CmsException("couldn't create cipher.", e); + } + catch (InvalidKeyException e) + { + throw new CmsException("key invalid in message.", e); + } } } } diff --git a/crypto/src/cms/RecipientId.cs b/crypto/src/cms/RecipientId.cs index c4107b14e8..140ff6fb31 100644 --- a/crypto/src/cms/RecipientId.cs +++ b/crypto/src/cms/RecipientId.cs @@ -11,11 +11,11 @@ public class RecipientID { private byte[] m_keyIdentifier; - public byte[] KeyIdentifier - { - get { return Arrays.Clone(m_keyIdentifier); } - set { m_keyIdentifier = Arrays.Clone(value); } - } + public byte[] KeyIdentifier + { + get { return Arrays.Clone(m_keyIdentifier); } + set { m_keyIdentifier = Arrays.Clone(value); } + } public virtual bool Equals(RecipientID other) { @@ -32,7 +32,7 @@ public virtual bool Equals(RecipientID other) public override int GetHashCode() { return Arrays.GetHashCode(m_keyIdentifier) - ^ GetHashCodeOfSubjectKeyIdentifier() + ^ GetHashCodeOfSubjectKeyIdentifier() ^ Objects.GetHashCode(SerialNumber) ^ Objects.GetHashCode(Issuer); } From 99555bee92a0242d86c6a3a7680ddaed87bb284b Mon Sep 17 00:00:00 2001 From: Konradsop Date: Thu, 30 Jul 2026 16:05:00 +0200 Subject: [PATCH 2/2] Add XML documentation for CMS recipient types --- crypto/src/cms/KEKRecipientInformation.cs | 14 +++++++------- crypto/src/cms/KeyAgreeRecipientInformation.cs | 16 +++++++++------- crypto/src/cms/KeyTransRecipientInformation.cs | 14 ++++++++------ crypto/src/cms/PasswordRecipientInformation.cs | 11 ++++++----- crypto/src/cms/RecipientId.cs | 10 ++++++++++ crypto/src/cms/RecipientInformation.cs | 12 ++++++++++++ 6 files changed, 52 insertions(+), 25 deletions(-) diff --git a/crypto/src/cms/KEKRecipientInformation.cs b/crypto/src/cms/KEKRecipientInformation.cs index 53b7f9ae9f..c8e1b8bf5a 100644 --- a/crypto/src/cms/KEKRecipientInformation.cs +++ b/crypto/src/cms/KEKRecipientInformation.cs @@ -9,10 +9,9 @@ namespace Org.BouncyCastle.Cms { - /** - * the RecipientInfo class for a recipient who has been sent a message - * encrypted using a secret key known to the other side. - */ + /// + /// CMS recipient information for key-encryption-key (KEK) recipients that share a symmetric wrapping key. + /// public class KekRecipientInformation : RecipientInformation { @@ -31,9 +30,10 @@ internal KekRecipientInformation( rid.KeyIdentifier = kekId.KeyIdentifier.GetOctets(); } - /** - * decrypt the content and return an input stream. - */ + /// Decrypts the content using the recipient's shared key-encryption key. + /// The shared key-encryption key. + /// A typed stream over the decrypted content. + /// Thrown if the content-encryption key cannot be recovered. public override CmsTypedStream GetContentStream( ICipherParameters key) { diff --git a/crypto/src/cms/KeyAgreeRecipientInformation.cs b/crypto/src/cms/KeyAgreeRecipientInformation.cs index 85be5199ad..1073050226 100644 --- a/crypto/src/cms/KeyAgreeRecipientInformation.cs +++ b/crypto/src/cms/KeyAgreeRecipientInformation.cs @@ -17,10 +17,9 @@ namespace Org.BouncyCastle.Cms { - /** - * the RecipientInfo class for a recipient who has been sent a message - * encrypted using key agreement. - */ + /// + /// CMS recipient information for key agreement, where a sender and recipient derive the key-encryption key. + /// public class KeyAgreeRecipientInformation : RecipientInformation { @@ -197,9 +196,12 @@ internal KeyParameter GetSessionKey(AsymmetricKeyParameter receiverPrivateKey) } } - /** - * decrypt the content and return an input stream. - */ + /// Decrypts the content using the recipient's key-agreement private key. + /// The recipient's private asymmetric key. + /// A typed stream over the decrypted content. + /// Thrown if is not a private asymmetric key. + /// + /// Thrown if key agreement or content-key recovery fails. public override CmsTypedStream GetContentStream( ICipherParameters key) { diff --git a/crypto/src/cms/KeyTransRecipientInformation.cs b/crypto/src/cms/KeyTransRecipientInformation.cs index 06ae0921ef..58371099a1 100644 --- a/crypto/src/cms/KeyTransRecipientInformation.cs +++ b/crypto/src/cms/KeyTransRecipientInformation.cs @@ -13,11 +13,10 @@ namespace Org.BouncyCastle.Cms { - /** - * the KeyTransRecipientInformation class for a recipient who has been sent a secret - * key encrypted using their public key that needs to be used to - * extract the message. - */ + /// + /// CMS recipient information for key transport, where the content-encryption key is encrypted for a recipient's + /// public key. + /// public class KeyTransRecipientInformation : RecipientInformation { @@ -142,7 +141,10 @@ internal KeyParameter UnwrapKey(ICipherParameters key) } } - /// Decrypt the content and return it as a byte array. + /// Decrypts the content using the recipient's private key and returns a stream over it. + /// The recipient's private key. + /// A typed stream over the decrypted content. + /// Thrown if the content-encryption key cannot be recovered. public override CmsTypedStream GetContentStream(ICipherParameters key) => GetContentFromSessionKey(UnwrapKey(key)); } } diff --git a/crypto/src/cms/PasswordRecipientInformation.cs b/crypto/src/cms/PasswordRecipientInformation.cs index dbdc46a96e..679efadedf 100644 --- a/crypto/src/cms/PasswordRecipientInformation.cs +++ b/crypto/src/cms/PasswordRecipientInformation.cs @@ -9,7 +9,7 @@ namespace Org.BouncyCastle.Cms { - /// The RecipientInfo class for a recipient who has been sent a message encrypted using a password. + /// CMS recipient information for a recipient that recovers content using a password-derived key. public class PasswordRecipientInformation : RecipientInformation { @@ -22,12 +22,13 @@ internal PasswordRecipientInformation(PasswordRecipientInfo info, CmsSecureReada this.rid = new RecipientID(); } - /// - /// Return the object identifier for the key derivation algorithm, or null if there is none present. - /// + /// Gets the key-derivation algorithm, or null when the message does not include one. public virtual AlgorithmIdentifier KeyDerivationAlgorithm => m_info.KeyDerivationAlgorithm; - /// Decrypt the content and return an input stream. + /// Decrypts the content using a password-based recipient key. + /// The password-based recipient key. + /// A typed stream over the decrypted content. + /// Thrown if the content-encryption key cannot be recovered. public override CmsTypedStream GetContentStream(ICipherParameters key) { try diff --git a/crypto/src/cms/RecipientId.cs b/crypto/src/cms/RecipientId.cs index 140ff6fb31..acdf60222b 100644 --- a/crypto/src/cms/RecipientId.cs +++ b/crypto/src/cms/RecipientId.cs @@ -5,18 +5,26 @@ namespace Org.BouncyCastle.Cms { + /// + /// Identifies a CMS recipient by issuer and serial number, subject key identifier, or KEK key identifier. + /// // TODO[api] sealed public class RecipientID : X509CertStoreSelector, IEquatable { private byte[] m_keyIdentifier; + /// Gets or sets the recipient key identifier. public byte[] KeyIdentifier { get { return Arrays.Clone(m_keyIdentifier); } set { m_keyIdentifier = Arrays.Clone(value); } } + /// Determines whether this identifier selects the same recipient as . + /// + /// The identifier to compare. + /// true if the identifiers match; otherwise, false. public virtual bool Equals(RecipientID other) { return other == null ? false @@ -27,8 +35,10 @@ public virtual bool Equals(RecipientID other) && MatchesIssuer(other); } + /// public override bool Equals(object obj) => Equals(obj as RecipientID); + /// public override int GetHashCode() { return Arrays.GetHashCode(m_keyIdentifier) diff --git a/crypto/src/cms/RecipientInformation.cs b/crypto/src/cms/RecipientInformation.cs index 847008a7fa..e5d1aef4d3 100644 --- a/crypto/src/cms/RecipientInformation.cs +++ b/crypto/src/cms/RecipientInformation.cs @@ -10,6 +10,10 @@ namespace Org.BouncyCastle.Cms { + /// + /// Base class for CMS recipient information. Use to select a recipient, then supply + /// the matching key material to or . + /// public abstract class RecipientInformation { internal RecipientID rid = new RecipientID(); @@ -31,8 +35,10 @@ internal string GetContentAlgorithmName() return algorithm.Algorithm.Id; } + /// Gets the identifier used to match this recipient. public RecipientID RecipientID => rid; + /// Gets the algorithm identifier used to encrypt or wrap the content-encryption key. public AlgorithmIdentifier KeyEncryptionAlgorithmID => keyEncAlg; /// Return the object identifier for the key encryption algorithm. @@ -57,6 +63,9 @@ internal CmsTypedStream GetContentFromSessionKey(KeyParameter sKey) } } + /// Decrypts the content using and returns all of its bytes. + /// The recipient key material needed to recover the content-encryption key. + /// The decrypted or authenticated content. public byte[] GetContent(ICipherParameters key) { try @@ -87,6 +96,9 @@ public byte[] GetMac() return Arrays.Clone(resultMac); } + /// Returns a stream that exposes the recovered content. + /// The recipient key material needed to recover the content-encryption key. + /// A typed stream over the recovered content. public abstract CmsTypedStream GetContentStream(ICipherParameters key); } }