I am trying to read an ASN1 object using Bouncycastle on Android. I expect it to be a DERSequence, which in Bouncycastle is a subclass of ASN1Sequence, which is a subclass of ASN1Object.
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
...
ASN1InputStream ais = ...;
Object o = ais.readObject();
// Eclipse's debugger now says o is a DERSequence, as expected.
DERSequence o2 = (DERSequence)o;
ASN1Sequence o3 = o2;
ASN1Object o4 = o3;
// And o4 is now exactly what I want.
ASN1Object o5 = (ASN1Object)o;
// But this throws:
/// java.lang.ClassCastException: org.bouncycastle.asn1.DERSequence
Based on feedback from the answers, I have constructed another, shorter example:
Object o = new DERSequence();
ASN1Object o1 = new DERSequence(); // This behaves fine.
ASN1Object o2 = (ASN1Object)o; // Throws ClassCastException.
What causes this cast to fail?
(ASN1Object)(ASN1Sequence)(DERSequence)o
? :p – Blareo5 = ...
line of code alone causes the exception - everything above it in the stack is just Android noise. That line I posted is the first line of the stack trace. – MarloObject
o
is pointing to the same reference?. That's why i asked. – Fieldpiece