Skip to content

Commit 0123856

Browse files
committed
github #222 addressed OAEP parameter setting, refactored KeyTransRecipientInfoGenerator to allow deprecation of sub class
1 parent 357e91b commit 0123856

File tree

11 files changed

+434
-100
lines changed

11 files changed

+434
-100
lines changed

crypto/src/asn1/pkcs/RSAESOAEPparams.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ public static RsaesOaepParameters GetInstance(
3636
* The default version
3737
*/
3838
public RsaesOaepParameters()
39+
: this(DefaultHashAlgorithm, DefaultMaskGenFunction, DefaultPSourceAlgorithm)
40+
{
41+
}
42+
43+
public RsaesOaepParameters(
44+
AlgorithmIdentifier hashAlgorithm,
45+
AlgorithmIdentifier maskGenAlgorithm)
46+
: this(DefaultHashAlgorithm, DefaultMaskGenFunction, DefaultPSourceAlgorithm)
3947
{
40-
hashAlgorithm = DefaultHashAlgorithm;
41-
maskGenAlgorithm = DefaultMaskGenFunction;
42-
pSourceAlgorithm = DefaultPSourceAlgorithm;
4348
}
4449

4550
public RsaesOaepParameters(

crypto/src/cms/CMSEnvelopedGenerator.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Org.BouncyCastle.Asn1.X509;
1111
using Org.BouncyCastle.Asn1.X9;
1212
using Org.BouncyCastle.Crypto;
13+
using Org.BouncyCastle.Crypto.Operators;
1314
using Org.BouncyCastle.Crypto.Parameters;
1415
using Org.BouncyCastle.Security;
1516
using Org.BouncyCastle.Utilities;
@@ -132,10 +133,9 @@ public CmsAttributeTableGenerator UnprotectedAttributeGenerator
132133
public void AddKeyTransRecipient(
133134
X509Certificate cert)
134135
{
135-
KeyTransRecipientInfoGenerator ktrig = new KeyTransRecipientInfoGenerator();
136-
ktrig.RecipientCert = cert;
137-
138-
recipientInfoGenerators.Add(ktrig);
136+
TbsCertificateStructure recipientTbsCert = CmsUtilities.GetTbsCertificateStructure(cert);
137+
SubjectPublicKeyInfo info = recipientTbsCert.SubjectPublicKeyInfo;
138+
this.AddRecipientInfoGenerator(new KeyTransRecipientInfoGenerator(cert, new Asn1KeyWrapper(info.AlgorithmID.Algorithm, info.AlgorithmID.Parameters, cert)));
139139
}
140140

141141
/**
@@ -149,11 +149,8 @@ public void AddKeyTransRecipient(
149149
AsymmetricKeyParameter pubKey,
150150
byte[] subKeyId)
151151
{
152-
KeyTransRecipientInfoGenerator ktrig = new KeyTransRecipientInfoGenerator();
153-
ktrig.RecipientPublicKey = pubKey;
154-
ktrig.SubjectKeyIdentifier = new DerOctetString(subKeyId);
155-
156-
recipientInfoGenerators.Add(ktrig);
152+
SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubKey);
153+
this.AddRecipientInfoGenerator(new KeyTransRecipientInfoGenerator(subKeyId, new Asn1KeyWrapper(info.AlgorithmID.Algorithm, info.AlgorithmID.Parameters, pubKey)));
157154
}
158155

159156
/**

crypto/src/cms/KeyTransRecipientInfoGenerator.cs

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,30 @@ public class KeyTransRecipientInfoGenerator : RecipientInfoGenerator
1515
{
1616
private static readonly CmsEnvelopedHelper Helper = CmsEnvelopedHelper.Instance;
1717

18-
private TbsCertificateStructure recipientTbsCert;
19-
private AsymmetricKeyParameter recipientPublicKey;
2018
private Asn1OctetString subjectKeyIdentifier;
19+
private IKeyWrapper keyWrapper;
2120

2221
// Derived fields
2322
private SubjectPublicKeyInfo info;
2423
private IssuerAndSerialNumber issuerAndSerialNumber;
2524
private SecureRandom random;
25+
2626

27-
internal KeyTransRecipientInfoGenerator()
27+
public KeyTransRecipientInfoGenerator(X509Certificate recipCert, IKeyWrapper keyWrapper)
28+
: this(new Asn1.Cms.IssuerAndSerialNumber(recipCert.IssuerDN, new DerInteger(recipCert.SerialNumber)), keyWrapper)
2829
{
2930
}
3031

31-
protected KeyTransRecipientInfoGenerator(IssuerAndSerialNumber issuerAndSerialNumber)
32+
public KeyTransRecipientInfoGenerator(IssuerAndSerialNumber issuerAndSerial, IKeyWrapper keyWrapper)
3233
{
33-
this.issuerAndSerialNumber = issuerAndSerialNumber;
34+
this.issuerAndSerialNumber = issuerAndSerial;
35+
this.keyWrapper = keyWrapper;
3436
}
3537

36-
protected KeyTransRecipientInfoGenerator(byte[] subjectKeyIdentifier)
38+
public KeyTransRecipientInfoGenerator(byte[] subjectKeyID, IKeyWrapper keyWrapper)
3739
{
3840
this.subjectKeyIdentifier = new DerOctetString(subjectKeyIdentifier);
39-
}
40-
41-
internal X509Certificate RecipientCert
42-
{
43-
set
44-
{
45-
this.recipientTbsCert = CmsUtilities.GetTbsCertificateStructure(value);
46-
this.recipientPublicKey = value.GetPublicKey();
47-
this.info = recipientTbsCert.SubjectPublicKeyInfo;
48-
}
49-
}
50-
51-
internal AsymmetricKeyParameter RecipientPublicKey
52-
{
53-
set
54-
{
55-
this.recipientPublicKey = value;
56-
57-
try
58-
{
59-
info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(
60-
recipientPublicKey);
61-
}
62-
catch (IOException)
63-
{
64-
throw new ArgumentException("can't extract key algorithm from this key");
65-
}
66-
}
67-
}
68-
69-
internal Asn1OctetString SubjectKeyIdentifier
70-
{
71-
set { this.subjectKeyIdentifier = value; }
41+
this.keyWrapper = keyWrapper;
7242
}
7343

7444
public RecipientInfo Generate(KeyParameter contentEncryptionKey, SecureRandom random)
@@ -80,11 +50,9 @@ public RecipientInfo Generate(KeyParameter contentEncryptionKey, SecureRandom ra
8050
byte[] encryptedKeyBytes = GenerateWrappedKey(contentEncryptionKey);
8151

8252
RecipientIdentifier recipId;
83-
if (recipientTbsCert != null)
53+
if (issuerAndSerialNumber != null)
8454
{
85-
IssuerAndSerialNumber issuerAndSerial = new IssuerAndSerialNumber(
86-
recipientTbsCert.Issuer, recipientTbsCert.SerialNumber.Value);
87-
recipId = new RecipientIdentifier(issuerAndSerial);
55+
recipId = new RecipientIdentifier(issuerAndSerialNumber);
8856
}
8957
else
9058
{
@@ -99,18 +67,17 @@ protected virtual AlgorithmIdentifier AlgorithmDetails
9967
{
10068
get
10169
{
70+
if (this.keyWrapper != null)
71+
{
72+
return (AlgorithmIdentifier)keyWrapper.AlgorithmDetails;
73+
}
10274
return info.AlgorithmID;
10375
}
10476
}
10577

10678
protected virtual byte[] GenerateWrappedKey(KeyParameter contentEncryptionKey)
10779
{
108-
byte[] keyBytes = contentEncryptionKey.GetKey();
109-
AlgorithmIdentifier keyEncryptionAlgorithm = info.AlgorithmID;
110-
111-
IWrapper keyWrapper = Helper.CreateWrapper(keyEncryptionAlgorithm.Algorithm.Id);
112-
keyWrapper.Init(true, new ParametersWithRandom(recipientPublicKey, random));
113-
return keyWrapper.Wrap(keyBytes, 0, keyBytes.Length);
80+
return keyWrapper.Wrap(contentEncryptionKey.GetKey()).Collect();
11481
}
11582
}
11683
}

crypto/src/cms/KeyTransRecipientInformation.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using Org.BouncyCastle.Crypto.Parameters;
1010
using Org.BouncyCastle.Security;
1111
using Org.BouncyCastle.X509;
12+
using Org.BouncyCastle.Asn1.Pkcs;
13+
using Org.BouncyCastle.Crypto.Operators;
1214

1315
namespace Org.BouncyCastle.Cms
1416
{
@@ -42,7 +44,7 @@ internal KeyTransRecipientInformation(
4244
}
4345
else
4446
{
45-
IssuerAndSerialNumber iAnds = IssuerAndSerialNumber.GetInstance(r.ID);
47+
Asn1.Cms.IssuerAndSerialNumber iAnds = Asn1.Cms.IssuerAndSerialNumber.GetInstance(r.ID);
4648

4749
rid.Issuer = iAnds.Name;
4850
rid.SerialNumber = iAnds.SerialNumber.Value;
@@ -74,16 +76,27 @@ private string GetExchangeEncryptionAlgorithmName(
7476
internal KeyParameter UnwrapKey(ICipherParameters key)
7577
{
7678
byte[] encryptedKey = info.EncryptedKey.GetOctets();
77-
string keyExchangeAlgorithm = GetExchangeEncryptionAlgorithmName(keyEncAlg);
79+
7880

7981
try
8082
{
81-
IWrapper keyWrapper = WrapperUtilities.GetWrapper(keyExchangeAlgorithm);
82-
keyWrapper.Init(false, key);
83+
if (keyEncAlg.Algorithm.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
84+
{
85+
IKeyUnwrapper keyWrapper = new Asn1KeyUnwrapper(keyEncAlg.Algorithm, keyEncAlg.Parameters, key);
8386

84-
// FIXME Support for MAC algorithm parameters similar to cipher parameters
85-
return ParameterUtilities.CreateKeyParameter(
86-
GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length));
87+
return ParameterUtilities.CreateKeyParameter(
88+
GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length).Collect());
89+
}
90+
else
91+
{
92+
string keyExchangeAlgorithm = GetExchangeEncryptionAlgorithmName(keyEncAlg);
93+
IWrapper keyWrapper = WrapperUtilities.GetWrapper(keyExchangeAlgorithm);
94+
keyWrapper.Init(false, key);
95+
96+
// FIXME Support for MAC algorithm parameters similar to cipher parameters
97+
return ParameterUtilities.CreateKeyParameter(
98+
GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length));
99+
}
87100
}
88101
catch (SecurityUtilityException e)
89102
{

crypto/src/crypto/encodings/OaepEncoding.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private byte[] EncodeBlock(
169169
//
170170
// mask the message block.
171171
//
172-
byte[] mask = maskGeneratorFunction1(seed, 0, seed.Length, block.Length - defHash.Length);
172+
byte[] mask = MaskGeneratorFunction(seed, 0, seed.Length, block.Length - defHash.Length);
173173

174174
for (int i = defHash.Length; i != block.Length; i++)
175175
{
@@ -184,7 +184,7 @@ private byte[] EncodeBlock(
184184
//
185185
// mask the seed.
186186
//
187-
mask = maskGeneratorFunction1(
187+
mask = MaskGeneratorFunction(
188188
block, defHash.Length, block.Length - defHash.Length, defHash.Length);
189189

190190
for (int i = 0; i != defHash.Length; i++)
@@ -227,7 +227,7 @@ private byte[] DecodeBlock(
227227
//
228228
// unmask the seed.
229229
//
230-
byte[] mask = maskGeneratorFunction1(
230+
byte[] mask = MaskGeneratorFunction(
231231
block, defHash.Length, block.Length - defHash.Length, defHash.Length);
232232

233233
for (int i = 0; i != defHash.Length; i++)
@@ -238,7 +238,7 @@ private byte[] DecodeBlock(
238238
//
239239
// unmask the message block.
240240
//
241-
mask = maskGeneratorFunction1(block, 0, defHash.Length, block.Length - defHash.Length);
241+
mask = MaskGeneratorFunction(block, 0, defHash.Length, block.Length - defHash.Length);
242242

243243
for (int i = defHash.Length; i != block.Length; i++)
244244
{
@@ -306,10 +306,30 @@ private void ItoOSP(
306306
sp[3] = (byte)((uint)i >> 0);
307307
}
308308

309+
private byte[] MaskGeneratorFunction(
310+
byte[] Z,
311+
int zOff,
312+
int zLen,
313+
int length)
314+
{
315+
if (mgf1Hash is IXof)
316+
{
317+
byte[] mask = new byte[length];
318+
mgf1Hash.BlockUpdate(Z, zOff, zLen);
319+
((IXof)mgf1Hash).DoFinal(mask, 0, mask.Length);
320+
321+
return mask;
322+
}
323+
else
324+
{
325+
return MaskGeneratorFunction1(Z, zOff, zLen, length);
326+
}
327+
}
328+
309329
/**
310330
* mask generator function, as described in PKCS1v2.
311331
*/
312-
private byte[] maskGeneratorFunction1(
332+
private byte[] MaskGeneratorFunction1(
313333
byte[] Z,
314334
int zOff,
315335
int zLen,

0 commit comments

Comments
 (0)