diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-07-24 16:49:28 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-07-24 16:49:28 +0700 |
commit | 7bd0d90741a85da07c6949007e5e020c829a65fa (patch) | |
tree | f49a3f08628f1d750ee5d4a65ca48a69534a9ff2 /crypto/src | |
parent | Update OID registries (diff) | |
download | BouncyCastle.NET-ed25519-7bd0d90741a85da07c6949007e5e020c829a65fa.tar.xz |
Refactoring in Cms
Diffstat (limited to 'crypto/src')
-rw-r--r-- | crypto/src/cms/CMSEnvelopedGenerator.cs | 14 | ||||
-rw-r--r-- | crypto/src/cms/KeyAgreeRecipientInformation.cs | 43 |
2 files changed, 32 insertions, 25 deletions
diff --git a/crypto/src/cms/CMSEnvelopedGenerator.cs b/crypto/src/cms/CMSEnvelopedGenerator.cs index 702afc664..3a7ef8f3f 100644 --- a/crypto/src/cms/CMSEnvelopedGenerator.cs +++ b/crypto/src/cms/CMSEnvelopedGenerator.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cms; +using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.Kisa; using Org.BouncyCastle.Asn1.Nist; using Org.BouncyCastle.Asn1.Ntt; @@ -79,10 +80,13 @@ namespace Org.BouncyCastle.Cms public static readonly string DesCbc = OiwObjectIdentifiers.DesCbc.Id; public static readonly string DesEde3Cbc = PkcsObjectIdentifiers.DesEde3Cbc.Id; public static readonly string RC2Cbc = PkcsObjectIdentifiers.RC2Cbc.Id; - // TODO[api] Change these to static readonly (and ideally find actual OIDs) + // TODO[api] Change to static readonly public const string IdeaCbc = "1.3.6.1.4.1.188.7.1.1.2"; - public const string Cast5Cbc = "1.2.840.113533.7.66.10"; - public static readonly string Aes128Cbc = NistObjectIdentifiers.IdAes128Cbc.Id; + //public static readonly string IdeaCbc = MiscObjectIdentifiers.as_sys_sec_alg_ideaCBC.Id; + // TODO[api] Change to static readonly + public const string Cast5Cbc = "1.2.840.113533.7.66.10"; + //public static readonly string CastCbc = MiscObjectIdentifiers.cast5CBC.Id; + public static readonly string Aes128Cbc = NistObjectIdentifiers.IdAes128Cbc.Id; public static readonly string Aes192Cbc = NistObjectIdentifiers.IdAes192Cbc.Id; public static readonly string Aes256Cbc = NistObjectIdentifiers.IdAes256Cbc.Id; public static readonly string Aes128Ccm = NistObjectIdentifiers.IdAes128Ccm.Id; @@ -105,6 +109,10 @@ namespace Org.BouncyCastle.Cms public static readonly string Camellia256Wrap = NttObjectIdentifiers.IdCamellia256Wrap.Id; public static readonly string SeedWrap = KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap.Id; + public static readonly string Gost28147Wrap = CryptoProObjectIdentifiers.id_Gost28147_89_None_KeyWrap.Id; + public static readonly string Gost28147CryptoProWrap = CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap.Id; + + public static readonly string ECCDHSha1Kdf = X9ObjectIdentifiers.DHSinglePassCofactorDHSha1KdfScheme.Id; public static readonly string ECDHSha1Kdf = X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme.Id; public static readonly string ECMqvSha1Kdf = X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme.Id; public static readonly string ECMqvSha224Kdf = SecObjectIdentifiers.mqvSinglePass_sha224kdf_scheme.Id; diff --git a/crypto/src/cms/KeyAgreeRecipientInformation.cs b/crypto/src/cms/KeyAgreeRecipientInformation.cs index 32679803d..893b88b73 100644 --- a/crypto/src/cms/KeyAgreeRecipientInformation.cs +++ b/crypto/src/cms/KeyAgreeRecipientInformation.cs @@ -5,6 +5,7 @@ using System.IO; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.Cms.Ecc; +using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.X9; @@ -23,8 +24,8 @@ namespace Org.BouncyCastle.Cms public class KeyAgreeRecipientInformation : RecipientInformation { - private KeyAgreeRecipientInfo info; - private Asn1OctetString encryptedKey; + private readonly KeyAgreeRecipientInfo m_info; + private readonly Asn1OctetString m_encryptedKey; internal static void ReadRecipientInfo(IList<RecipientInformation> infos, KeyAgreeRecipientInfo info, CmsSecureReadable secureReadable) @@ -64,30 +65,24 @@ namespace Org.BouncyCastle.Cms } } - internal KeyAgreeRecipientInformation( - KeyAgreeRecipientInfo info, - RecipientID rid, - Asn1OctetString encryptedKey, - CmsSecureReadable secureReadable) + internal KeyAgreeRecipientInformation(KeyAgreeRecipientInfo info, RecipientID rid, Asn1OctetString encryptedKey, + CmsSecureReadable secureReadable) : base(info.KeyEncryptionAlgorithm, secureReadable) { - this.info = info; + m_info = info; this.rid = rid; - this.encryptedKey = encryptedKey; + m_encryptedKey = encryptedKey; } - private AsymmetricKeyParameter GetSenderPublicKey( - AsymmetricKeyParameter receiverPrivateKey, - OriginatorIdentifierOrKey originator) + private AsymmetricKeyParameter GetSenderPublicKey(AsymmetricKeyParameter receiverPrivateKey, + OriginatorIdentifierOrKey originator) { OriginatorPublicKey opk = originator.OriginatorPublicKey; if (opk != null) - { return GetPublicKeyFromOriginatorPublicKey(receiverPrivateKey, opk); - } - + OriginatorID origID = new OriginatorID(); - + Asn1.Cms.IssuerAndSerialNumber iAndSN = originator.IssuerAndSerialNumber; if (iAndSN != null) { @@ -131,7 +126,7 @@ namespace Org.BouncyCastle.Cms if (agreeAlgID.Id.Equals(CmsEnvelopedGenerator.ECMqvSha1Kdf)) { - byte[] ukmEncoding = info.UserKeyingMaterial.GetOctets(); + byte[] ukmEncoding = m_info.UserKeyingMaterial.GetOctets(); MQVuserKeyingMaterial ukm = MQVuserKeyingMaterial.GetInstance( Asn1Object.FromByteArray(ukmEncoding)); @@ -157,7 +152,7 @@ namespace Org.BouncyCastle.Cms private KeyParameter UnwrapSessionKey(DerObjectIdentifier wrapAlgOid, KeyParameter agreedKey) { - byte[] encKeyOctets = encryptedKey.GetOctets(); + byte[] encKeyOctets = m_encryptedKey.GetOctets(); IWrapper keyCipher = WrapperUtilities.GetWrapper(wrapAlgOid); keyCipher.Init(false, agreedKey); @@ -165,18 +160,22 @@ namespace Org.BouncyCastle.Cms return ParameterUtilities.CreateKeyParameter(GetContentAlgorithmName(), sKeyBytes); } - internal KeyParameter GetSessionKey( - AsymmetricKeyParameter receiverPrivateKey) + internal KeyParameter GetSessionKey(AsymmetricKeyParameter receiverPrivateKey) { try { var wrapAlgOid = DerObjectIdentifier.GetInstance(Asn1Sequence.GetInstance(keyEncAlg.Parameters)[0]); - AsymmetricKeyParameter senderPublicKey = GetSenderPublicKey( - receiverPrivateKey, info.Originator); + AsymmetricKeyParameter senderPublicKey = GetSenderPublicKey(receiverPrivateKey, m_info.Originator); KeyParameter agreedWrapKey = CalculateAgreedWrapKey(wrapAlgOid, senderPublicKey, receiverPrivateKey); + if (CryptoProObjectIdentifiers.id_Gost28147_89_None_KeyWrap.Equals(wrapAlgOid) || + CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap.Equals(wrapAlgOid)) + { + // TODO[cms] GOST key wrapping + } + return UnwrapSessionKey(wrapAlgOid, agreedWrapKey); } catch (SecurityUtilityException e) |