0

In a Scala project, I am using Java livrary (bouncycastle).

I have a compilation issue when using a method that required an object implementing a generic type.

Here is the interface in Java:

public interface Selector<T> extends Cloneable
{
    boolean match(T obj);
    Object clone();
}

Here is the piece of code that does not compile:

def verify(data: File): Boolean = {
    val signedData = new CMSSignedData(new CMSProcessableFile(data), Base64.decode(this.value))
    val certStore = signedData.getCertificates
    val signers = signedData.getSignerInfos.getSigners
    val signer = signers.iterator.next
    val certs = certStore.getMatches(signer.getSID)
    val cert = certs.iterator.next.asInstanceOf[X509CertificateHolder]
    signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(cert))
}

When I compile the code, I get the following error:

[error]  found   : org.bouncycastle.cms.SignerId
[error]  required: org.bouncycastle.util.Selector[?0]
[error]     val certs = certStore.getMatches(signer.getSID)

I tried casting but I does not compile.

Coule you please help?

Regards,

1
  • What is the signature of getMatches ? Commented Aug 6, 2016 at 10:56

2 Answers 2

1

If you look at https://www.bouncycastle.org/docs/pkixdocs1.5on/org/bouncycastle/cms/CMSSignedData.html, getCertificates returns a raw Store object instead of Store<Something>. Scala doesn't support working with raw types, generally speaking (they only exist in Java to interoperate with pre-Java-5 code). It's documented as

Return any X.509 certificate objects in this SignedData structure as a Store of X509CertificateHolder objects.

So you can try

val certStore = signedData.getCertificates.asInstanceOf[Store[X509CertificateHolder]]

and you'll also need casts in any other places where raw Store or Selector are returned.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is what I modified:

def verify(data: File): Boolean = {
  val signedData = new CMSSignedData(new CMSProcessableFile(data), Base64.decode(this.value))
  val certStore = signedData.getCertificates.asInstanceOf[Store[X509CertificateHolder]]
  val signers = signedData.getSignerInfos.getSigners
  val signer = signers.iterator.next
  val certs = certStore.getMatches(signer.getSID.asInstanceOf[Selector[X509CertificateHolder]])
  val cert = certs.iterator.next
  signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(cert))
}

Now, the code compile without problem!

Thanks a lot.

Kind Regards,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.