I’m obviously missing something fundamental here. I have the following Scala Code:
package org.carlskii
import java.io._
import java.security.cert.CertificateFactory
import java.security.Security
import org.bouncycastle.jce.provider._
object Main extends App {
Security.addProvider(new BouncyCastleProvider)
val provider = new BouncyCastleProvider
val in = new FileInputStream("cert.cer")
val certificateFactory = CertificateFactory.getInstance("X509", provider)
val certificate = certificateFactory.generateCertificate(file)
println(certificate.getClass)
}
Which produces this:
class org.bouncycastle.jce.provider.X509CertificateObject
So I have a Bouncy Castle X509CertificateObject object. If I call the certificate.getPublicKey method it correctly returns the public key for the certificate.
However if I call certificate.getSerialNumber it throws the following error:
error: value getSerialNumber is not a member of java.security.cert.Certificate
println(certificate.getSerialNumber)
The interesting thing here is that Scala thinks it’s a java.security.cert.Certificate and not an org.bouncycastle.jce.provider.X509CertificateObject object. Why is this the case?
The generateCertificate method returns a Certificate object, if you are sure you’re going to get a
org.bouncycastle.jce.provider.X509CertificateObjectjust cast the result to it: