Skip to content

Commit fe7ed9e

Browse files
committed
added KEMRecipientId, introduced PKIXRecipientId to simplify RecipientInformationStore processing.
1 parent 6c0b1cd commit fe7ed9e

File tree

8 files changed

+209
-47
lines changed

8 files changed

+209
-47
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.bouncycastle.cms;
2+
3+
import java.math.BigInteger;
4+
5+
import org.bouncycastle.asn1.x500.X500Name;
6+
import org.bouncycastle.cert.selector.X509CertificateHolderSelector;
7+
8+
public class KEMRecipientId
9+
extends PKIXRecipientId
10+
{
11+
private KEMRecipientId(X509CertificateHolderSelector baseSelector)
12+
{
13+
super(kem, baseSelector);
14+
}
15+
16+
/**
17+
* Construct a key trans recipient ID with the value of a public key's subjectKeyId.
18+
*
19+
* @param subjectKeyId a subjectKeyId
20+
*/
21+
public KEMRecipientId(byte[] subjectKeyId)
22+
{
23+
super(kem, null, null, subjectKeyId);
24+
}
25+
26+
/**
27+
* Construct a key trans recipient ID based on the issuer and serial number of the recipient's associated
28+
* certificate.
29+
*
30+
* @param issuer the issuer of the recipient's associated certificate.
31+
* @param serialNumber the serial number of the recipient's associated certificate.
32+
*/
33+
public KEMRecipientId(X500Name issuer, BigInteger serialNumber)
34+
{
35+
super(kem, issuer, serialNumber, null);
36+
}
37+
38+
/**
39+
* Construct a key trans recipient ID based on the issuer and serial number of the recipient's associated
40+
* certificate.
41+
*
42+
* @param issuer the issuer of the recipient's associated certificate.
43+
* @param serialNumber the serial number of the recipient's associated certificate.
44+
* @param subjectKeyId the subject key identifier to use to match the recipients associated certificate.
45+
*/
46+
public KEMRecipientId(X500Name issuer, BigInteger serialNumber, byte[] subjectKeyId)
47+
{
48+
super(kem, issuer, serialNumber, subjectKeyId);
49+
}
50+
51+
public Object clone()
52+
{
53+
return new KEMRecipientId(this.baseSelector);
54+
}
55+
56+
public boolean match(Object obj)
57+
{
58+
if (obj instanceof KEMRecipientInformation)
59+
{
60+
return ((KEMRecipientInformation)obj).getRID().equals(this);
61+
}
62+
63+
return super.match(obj);
64+
}
65+
}

pkix/src/main/java/org/bouncycastle/cms/KEMRecipientInformation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public class KEMRecipientInformation
2626
{
2727
ASN1OctetString octs = ASN1OctetString.getInstance(r.getId());
2828

29-
rid = new KeyTransRecipientId(octs.getOctets()); // TODO: should be KEM
29+
rid = new KEMRecipientId(octs.getOctets()); // TODO: should be KEM
3030
}
3131
else
3232
{
3333
IssuerAndSerialNumber iAnds = IssuerAndSerialNumber.getInstance(r.getId());
3434

35-
rid = new KeyTransRecipientId(iAnds.getName(), iAnds.getSerialNumber().getValue()); // TODO:
35+
rid = new KEMRecipientId(iAnds.getName(), iAnds.getSerialNumber().getValue()); // TODO:
3636
}
3737
}
3838

pkix/src/main/java/org/bouncycastle/cms/KeyAgreeRecipientId.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
import org.bouncycastle.cert.selector.X509CertificateHolderSelector;
77

88
public class KeyAgreeRecipientId
9-
extends RecipientId
9+
extends PKIXRecipientId
1010
{
11-
private X509CertificateHolderSelector baseSelector;
12-
1311
private KeyAgreeRecipientId(X509CertificateHolderSelector baseSelector)
1412
{
15-
super(keyAgree);
16-
17-
this.baseSelector = baseSelector;
13+
super(keyAgree, baseSelector);
1814
}
1915

2016
/**
@@ -24,7 +20,7 @@ private KeyAgreeRecipientId(X509CertificateHolderSelector baseSelector)
2420
*/
2521
public KeyAgreeRecipientId(byte[] subjectKeyId)
2622
{
27-
this(null, null, subjectKeyId);
23+
super(keyAgree, null, null, subjectKeyId);
2824
}
2925

3026
/**
@@ -36,12 +32,12 @@ public KeyAgreeRecipientId(byte[] subjectKeyId)
3632
*/
3733
public KeyAgreeRecipientId(X500Name issuer, BigInteger serialNumber)
3834
{
39-
this(issuer, serialNumber, null);
35+
super(keyAgree, issuer, serialNumber, null);
4036
}
4137

4238
public KeyAgreeRecipientId(X500Name issuer, BigInteger serialNumber, byte[] subjectKeyId)
4339
{
44-
this(new X509CertificateHolderSelector(issuer, serialNumber, subjectKeyId));
40+
super(keyAgree, issuer, serialNumber, subjectKeyId);
4541
}
4642

4743
public X500Name getIssuer()

pkix/src/main/java/org/bouncycastle/cms/KeyTransRecipientId.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
import org.bouncycastle.cert.selector.X509CertificateHolderSelector;
77

88
public class KeyTransRecipientId
9-
extends RecipientId
9+
extends PKIXRecipientId
1010
{
11-
private X509CertificateHolderSelector baseSelector;
12-
1311
private KeyTransRecipientId(X509CertificateHolderSelector baseSelector)
1412
{
15-
super(keyTrans);
16-
17-
this.baseSelector = baseSelector;
13+
super(keyTrans, baseSelector);
1814
}
1915

2016
/**
@@ -24,7 +20,7 @@ private KeyTransRecipientId(X509CertificateHolderSelector baseSelector)
2420
*/
2521
public KeyTransRecipientId(byte[] subjectKeyId)
2622
{
27-
this(null, null, subjectKeyId);
23+
super(keyTrans, null, null, subjectKeyId);
2824
}
2925

3026
/**
@@ -36,7 +32,7 @@ public KeyTransRecipientId(byte[] subjectKeyId)
3632
*/
3733
public KeyTransRecipientId(X500Name issuer, BigInteger serialNumber)
3834
{
39-
this(issuer, serialNumber, null);
35+
super(keyTrans, issuer, serialNumber, null);
4036
}
4137

4238
/**
@@ -49,27 +45,7 @@ public KeyTransRecipientId(X500Name issuer, BigInteger serialNumber)
4945
*/
5046
public KeyTransRecipientId(X500Name issuer, BigInteger serialNumber, byte[] subjectKeyId)
5147
{
52-
this(new X509CertificateHolderSelector(issuer, serialNumber, subjectKeyId));
53-
}
54-
55-
public X500Name getIssuer()
56-
{
57-
return baseSelector.getIssuer();
58-
}
59-
60-
public BigInteger getSerialNumber()
61-
{
62-
return baseSelector.getSerialNumber();
63-
}
64-
65-
public byte[] getSubjectKeyIdentifier()
66-
{
67-
return baseSelector.getSubjectKeyIdentifier();
68-
}
69-
70-
public int hashCode()
71-
{
72-
return baseSelector.hashCode();
48+
super(keyTrans, issuer, serialNumber, subjectKeyId);
7349
}
7450

7551
public boolean equals(
@@ -97,6 +73,6 @@ public boolean match(Object obj)
9773
return ((KeyTransRecipientInformation)obj).getRID().equals(this);
9874
}
9975

100-
return baseSelector.match(obj);
76+
return super.match(obj);
10177
}
10278
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.bouncycastle.cms;
2+
3+
import java.math.BigInteger;
4+
5+
import org.bouncycastle.asn1.x500.X500Name;
6+
import org.bouncycastle.cert.selector.X509CertificateHolderSelector;
7+
8+
public class PKIXRecipientId
9+
extends RecipientId
10+
{
11+
protected final X509CertificateHolderSelector baseSelector;
12+
13+
protected PKIXRecipientId(int type, X509CertificateHolderSelector baseSelector)
14+
{
15+
super(type);
16+
17+
this.baseSelector = baseSelector;
18+
}
19+
20+
protected PKIXRecipientId(int type, X500Name issuer, BigInteger serialNumber, byte[] subjectKeyId)
21+
{
22+
this(type, new X509CertificateHolderSelector(issuer, serialNumber, subjectKeyId));
23+
}
24+
25+
public X500Name getIssuer()
26+
{
27+
return baseSelector.getIssuer();
28+
}
29+
30+
public BigInteger getSerialNumber()
31+
{
32+
return baseSelector.getSerialNumber();
33+
}
34+
35+
public byte[] getSubjectKeyIdentifier()
36+
{
37+
return baseSelector.getSubjectKeyIdentifier();
38+
}
39+
40+
public Object clone()
41+
{
42+
return new PKIXRecipientId(getType(), baseSelector);
43+
}
44+
45+
public int hashCode()
46+
{
47+
return baseSelector.hashCode();
48+
}
49+
50+
public boolean equals(
51+
Object o)
52+
{
53+
if (!(o instanceof PKIXRecipientId))
54+
{
55+
return false;
56+
}
57+
58+
PKIXRecipientId id = (PKIXRecipientId)o;
59+
60+
return this.baseSelector.equals(id.baseSelector);
61+
}
62+
63+
public boolean match(Object obj)
64+
{
65+
return baseSelector.match(obj);
66+
}
67+
}

pkix/src/main/java/org/bouncycastle/cms/RecipientId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public abstract class RecipientId
99
public static final int kek = 1;
1010
public static final int keyAgree = 2;
1111
public static final int password = 3;
12+
public static final int kem = 4;
1213

1314
private final int type;
1415

pkix/src/main/java/org/bouncycastle/cms/RecipientInformationStore.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,24 @@ public Collection<RecipientInformation> getRecipients()
9999
public Collection<RecipientInformation> getRecipients(
100100
RecipientId selector)
101101
{
102-
if (selector instanceof KeyTransRecipientId)
102+
if (selector instanceof PKIXRecipientId)
103103
{
104-
KeyTransRecipientId keyTrans = (KeyTransRecipientId)selector;
104+
PKIXRecipientId pkixId = (PKIXRecipientId)selector;
105105

106-
X500Name issuer = keyTrans.getIssuer();
107-
byte[] subjectKeyId = keyTrans.getSubjectKeyIdentifier();
106+
X500Name issuer = pkixId.getIssuer();
107+
byte[] subjectKeyId = pkixId.getSubjectKeyIdentifier();
108108

109109
if (issuer != null && subjectKeyId != null)
110110
{
111111
List<RecipientInformation> results = new ArrayList();
112112

113-
Collection<RecipientInformation> match1 = getRecipients(new KeyTransRecipientId(issuer, keyTrans.getSerialNumber()));
113+
List<RecipientInformation> match1 = (ArrayList<RecipientInformation>)table.get(new PKIXRecipientId(pkixId.getType(), issuer, pkixId.getSerialNumber(), null));
114114
if (match1 != null)
115115
{
116116
results.addAll(match1);
117117
}
118118

119-
Collection<RecipientInformation> match2 = getRecipients(new KeyTransRecipientId(subjectKeyId));
119+
Collection<RecipientInformation> match2 = (ArrayList<RecipientInformation>)table.get(new PKIXRecipientId(pkixId.getType(), null, null, subjectKeyId));
120120
if (match2 != null)
121121
{
122122
results.addAll(match2);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.bouncycastle.cms.jcajce;
2+
3+
import java.math.BigInteger;
4+
import java.security.cert.X509Certificate;
5+
6+
import javax.security.auth.x500.X500Principal;
7+
8+
import org.bouncycastle.asn1.x500.X500Name;
9+
import org.bouncycastle.cms.KEMRecipientId;
10+
11+
public class JceKEMRecipientId
12+
extends KEMRecipientId
13+
{
14+
/**
15+
* Construct a recipient id based on the issuer, serial number and subject key identifier (if present) of the passed in
16+
* certificate.
17+
*
18+
* @param certificate certificate providing the issue and serial number and subject key identifier.
19+
*/
20+
public JceKEMRecipientId(X509Certificate certificate)
21+
{
22+
super(convertPrincipal(certificate.getIssuerX500Principal()), certificate.getSerialNumber(), CMSUtils.getSubjectKeyId(certificate));
23+
}
24+
25+
/**
26+
* Construct a recipient id based on the provided issuer and serial number..
27+
*
28+
* @param issuer the issuer to use.
29+
* @param serialNumber the serial number to use.
30+
*/
31+
public JceKEMRecipientId(X500Principal issuer, BigInteger serialNumber)
32+
{
33+
super(convertPrincipal(issuer), serialNumber);
34+
}
35+
36+
/**
37+
* Construct a recipient id based on the provided issuer, serial number, and subjectKeyId..
38+
*
39+
* @param issuer the issuer to use.
40+
* @param serialNumber the serial number to use.
41+
* @param subjectKeyId the subject key ID to use.
42+
*/
43+
public JceKEMRecipientId(X500Principal issuer, BigInteger serialNumber, byte[] subjectKeyId)
44+
{
45+
super(convertPrincipal(issuer), serialNumber, subjectKeyId);
46+
}
47+
48+
private static X500Name convertPrincipal(X500Principal issuer)
49+
{
50+
if (issuer == null)
51+
{
52+
return null;
53+
}
54+
55+
return X500Name.getInstance(issuer.getEncoded());
56+
}
57+
}

0 commit comments

Comments
 (0)