Skip to content

Commit e05d3f5

Browse files
committed
added build method taking a pre-configured key[] to BcCMSContentEncryptorBuilder - relates to github #2115
1 parent 0e100a5 commit e05d3f5

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

pkix/src/main/java/org/bouncycastle/cms/bc/BcCMSContentEncryptorBuilder.java

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,59 @@ public BcCMSContentEncryptorBuilder setSecureRandom(SecureRandom random)
7373
return this;
7474
}
7575

76+
/**
77+
* Build the OutputEncryptor with an internally generated key.
78+
*
79+
* @return an OutputEncryptor configured to use an internal key.
80+
* @throws CMSException
81+
*/
7682
public OutputEncryptor build()
7783
throws CMSException
7884
{
85+
if (random == null)
86+
{
87+
random = new SecureRandom();
88+
}
89+
90+
CipherKeyGenerator keyGen = helper.createKeyGenerator(encryptionOID, keySize, random);
91+
92+
return build(keyGen.generateKey());
93+
}
94+
95+
/**
96+
* Build the OutputEncryptor using a pre-generated key.
97+
*
98+
* @param encKey a raw byte encoding of the key to be used for encryption.
99+
* @return an OutputEncryptor configured to use encKey.
100+
* @throws CMSException
101+
*/
102+
public OutputEncryptor build(byte[] encKey)
103+
throws CMSException
104+
{
105+
if (random == null)
106+
{
107+
random = new SecureRandom();
108+
}
109+
110+
// fixed key size defined
111+
if (this.keySize > 0)
112+
{
113+
if (((this.keySize + 7) / 8) != encKey.length)
114+
{
115+
if ((this.keySize != 56 && encKey.length != 8)
116+
&& (this.keySize != 168 && encKey.length != 24))
117+
{
118+
throw new IllegalArgumentException("attempt to create encryptor with the wrong sized key");
119+
}
120+
}
121+
}
122+
79123
if (helper.isAuthEnveloped(encryptionOID))
80124
{
81-
return new CMSAuthOutputEncryptor(encryptionOID, keySize, random);
125+
return new CMSAuthOutputEncryptor(encryptionOID, new KeyParameter(encKey), random);
82126
}
83-
return new CMSOutputEncryptor(encryptionOID, keySize, random);
127+
128+
return new CMSOutputEncryptor(encryptionOID, new KeyParameter(encKey), random);
84129
}
85130

86131
private class CMSOutputEncryptor
@@ -90,21 +135,12 @@ private class CMSOutputEncryptor
90135
private AlgorithmIdentifier algorithmIdentifier;
91136
protected Object cipher;
92137

93-
CMSOutputEncryptor(ASN1ObjectIdentifier encryptionOID, int keySize, SecureRandom random)
138+
CMSOutputEncryptor(ASN1ObjectIdentifier encryptionOID, KeyParameter encKey, SecureRandom random)
94139
throws CMSException
95140
{
96-
if (random == null)
97-
{
98-
random = new SecureRandom();
99-
}
100-
101-
CipherKeyGenerator keyGen = helper.createKeyGenerator(encryptionOID, keySize, random);
102-
103-
encKey = new KeyParameter(keyGen.generateKey());
104-
105-
algorithmIdentifier = helper.generateEncryptionAlgID(encryptionOID, encKey, random);
106-
107-
cipher = EnvelopedDataHelper.createContentCipher(true, encKey, algorithmIdentifier);
141+
this.algorithmIdentifier = helper.generateEncryptionAlgID(encryptionOID, encKey, random);
142+
this.encKey = encKey;
143+
this.cipher = EnvelopedDataHelper.createContentCipher(true, encKey, algorithmIdentifier);
108144
}
109145

110146
public AlgorithmIdentifier getAlgorithmIdentifier()
@@ -130,10 +166,10 @@ private class CMSAuthOutputEncryptor
130166
private AEADBlockCipher aeadCipher;
131167
private MacCaptureStream macOut;
132168

133-
CMSAuthOutputEncryptor(ASN1ObjectIdentifier encryptionOID, int keySize, SecureRandom random)
169+
CMSAuthOutputEncryptor(ASN1ObjectIdentifier encryptionOID, KeyParameter encKey, SecureRandom random)
134170
throws CMSException
135171
{
136-
super(encryptionOID, keySize, random);
172+
super(encryptionOID, encKey, random);
137173

138174
aeadCipher = getCipher();
139175
}

0 commit comments

Comments
 (0)