Repository navigation
Expand file tree
/
Copy pathBcKeyTransRecipient.java
More file actions
58 lines (49 loc) · 2.14 KB
/
Copy pathBcKeyTransRecipient.java
File metadata and controls
58 lines (49 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.bouncycastle.cms.bc;
import java.util.Set;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.cms.AbstractRecipient;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.KeyTransRecipient;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.operator.AsymmetricKeyUnwrapper;
import org.bouncycastle.operator.OperatorException;
import org.bouncycastle.operator.bc.BcRSAAsymmetricKeyUnwrapper;
public abstract class BcKeyTransRecipient
extends AbstractRecipient
implements KeyTransRecipient
{
private AsymmetricKeyParameter recipientKey;
public BcKeyTransRecipient(AsymmetricKeyParameter recipientKey)
{
this.recipientKey = recipientKey;
}
/**
* Set the content-encryption algorithms this recipient is willing to unwrap a key for. When set, an
* attempt to recover content protected under any other algorithm is rejected, mitigating an attacker
* substituting a weaker content-encryption algorithm into the recipient info.
*
* @param allowedContentAlgorithms the set of permitted content-encryption algorithm OIDs.
* @return this recipient.
*/
public BcKeyTransRecipient setAllowedContentAlgorithms(Set<ASN1ObjectIdentifier> allowedContentAlgorithms)
{
setAllowedContentAlgorithmSet(allowedContentAlgorithms);
return this;
}
protected CipherParameters extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, AlgorithmIdentifier encryptedKeyAlgorithm, byte[] encryptedEncryptionKey)
throws CMSException
{
checkContentAlgorithm(encryptedKeyAlgorithm);
AsymmetricKeyUnwrapper unwrapper = new BcRSAAsymmetricKeyUnwrapper(keyEncryptionAlgorithm, recipientKey);
try
{
return CMSUtils.getBcKey(unwrapper.generateUnwrappedKey(encryptedKeyAlgorithm, encryptedEncryptionKey));
}
catch (OperatorException e)
{
throw new CMSException("exception unwrapping key: " + e.getMessage(), e);
}
}
}