Skip to content

Commit 6d7b77d

Browse files
committed
assuming an ASN1Encodable to be a PKCS7 object can throw ClassCastException
whenever some strange ASN1 comes from the outside which is not a PKCS7 object, BC internally assumes the right object tree structure which can lead to ClassCastException when the structure is something else then expected. Sponsored by Lookout Inc.
1 parent 394b5d6 commit 6d7b77d

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/main/java/org/jruby/ext/openssl/PKCS7.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ public IRubyObject _initialize(final ThreadContext context, IRubyObject[] args)
311311
p7 = org.jruby.ext.openssl.impl.PKCS7.fromASN1(input);
312312
}
313313
}
314+
catch (IllegalArgumentException e) {
315+
throw getRuntime().newArgumentError(e.getMessage());
316+
}
314317
catch (IOException ioe) {
315318
throw newPKCS7Error(getRuntime(), ioe.getMessage());
316319
}

src/main/java/org/jruby/ext/openssl/impl/PKCS7.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Iterator;
3737
import java.util.List;
3838
import java.util.TimeZone;
39-
4039
import java.security.MessageDigest;
4140
import java.security.PrivateKey;
4241
import java.security.PublicKey;
@@ -66,7 +65,7 @@
6665
import org.bouncycastle.asn1.pkcs.IssuerAndSerialNumber;
6766
import org.bouncycastle.asn1.x500.X500Name;
6867
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
69-
68+
import org.bouncycastle.pkcs.PKCSException;
7069
import org.jruby.ext.openssl.SecurityHelper;
7170
import org.jruby.ext.openssl.x509store.Name;
7271
import org.jruby.ext.openssl.x509store.Store;
@@ -130,26 +129,32 @@ public static PKCS7 newEmpty() {
130129
public static PKCS7 fromASN1(ASN1Encodable obj) throws PKCS7Exception {
131130
PKCS7 p7 = new PKCS7();
132131

133-
int size = ((ASN1Sequence) obj).size();
134-
if (size == 0) {
135-
return p7;
136-
}
137-
138-
ASN1ObjectIdentifier contentType = (ASN1ObjectIdentifier) (((ASN1Sequence) obj).getObjectAt(0));
139-
if ( EMPTY_PKCS7_OID.equals( contentType.getId() ) ) {
140-
// OpenSSL behavior
141-
p7.setType(ASN1Registry.NID_undef);
142-
}
143-
else {
144-
final int nid = ASN1Registry.oid2nid(contentType);
132+
try {
133+
int size = ((ASN1Sequence) obj).size();
134+
if (size == 0) {
135+
return p7;
136+
}
137+
ASN1ObjectIdentifier contentType = (ASN1ObjectIdentifier) (((ASN1Sequence) obj).getObjectAt(0));
138+
if ( EMPTY_PKCS7_OID.equals( contentType.getId() ) ) {
139+
// OpenSSL behavior
140+
p7.setType(ASN1Registry.NID_undef);
141+
}
142+
else {
143+
final int nid = ASN1Registry.oid2nid(contentType);
145144

146-
ASN1Encodable content = size == 1 ? (ASN1Encodable) null : ((ASN1Sequence) obj).getObjectAt(1);
145+
ASN1Encodable content = size == 1 ? (ASN1Encodable) null : ((ASN1Sequence) obj).getObjectAt(1);
147146

148-
if (content != null && content instanceof ASN1TaggedObject && ((ASN1TaggedObject) content).getTagNo() == 0) {
149-
content = ((ASN1TaggedObject) content).getObject();
147+
if (content != null && content instanceof ASN1TaggedObject && ((ASN1TaggedObject) content).getTagNo() == 0) {
148+
content = ((ASN1TaggedObject) content).getObject();
149+
}
150+
p7.initiateWith(nid, content);
150151
}
151-
p7.initiateWith(nid, content);
152152
}
153+
// somewhere the object does not obey to be PKCS7 object
154+
catch (ClassCastException e) {
155+
throw new IllegalArgumentException("not a PKCS7 Object");
156+
}
157+
153158
return p7;
154159
}
155160

0 commit comments

Comments
 (0)