Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

So I'm doing an Android app and banging my head against our company's OAuth2 implementation (worse integration of my professional career by far, and it's not even done yet). I'm up to the exchange of client credentials part -- I'm getting back the credential in PFX format as a Base 64 encoded string. I then attempt to do this:

CredentialResponse resp = ServerAccessLayer.SSO.Model.CredentialFromJson(json);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream cert = new ByteArrayInputStream(Base64.decode(resp.credential, Base64.DEFAULT));
X509Certificate x509 = (X509Certificate)cf.generateCertificate(cert);

I've seen this code snippet linked in several places both on SO and in blog posts -- as near as I can tell it should work, but I'm getting this:

java.security.cert.CertificateException: 
org.apache.harmony.security.asn1.ASN1Exception: 
DER: only definite length encoding MUST be used

I'm a bit out of my depth, being a mainly web/Javascript UI programmer that's been suckered into learning Android and writing this app. So this might be too much or too little information, but here's the Base64 encoded string I'm getting back as a credential:

MIACAQMwgAYJKoZIhvcNAQcBoIAkgASCA+gwgDCABgkqhkiG9w0BBwGggCSABIIDKzCCAycwggMjBgsqhkiG9w0BDAoBAqCCArIwggKuMCgGCiqGSIb3DQEMAQMwGgQUttWa2nEvTrfVXeWDOCNh8qEpljYCAgQABIICgJwzIJFcp/NpJnxO9vh8cLUR7GsYOdm1CDzPZs+JplogTakQmiqlmwnybAIGaj6qXgDadYbG5k/fIrXW1OLRWWQFMUaYxH6lDDPKLqCsSwnhbxsvmp51EC+V5QwS9Am6zoNdAUE1SDQILuei7QOgmTcYM5uFo8pM+iVT6E4MC8S9EDCFppT75+w5PAhlYOURq2IF3HKAmT+VDa3ucB8P8me4k48HqP/6jEObiSEFA8ecz4Qx9jBSmqpBUVECpJXznThrexD7wUplyiWDcT7AUyaIBt+Nlhi8KZx4suuMaz6+1POqnKt938WPEKLOfzNA1+ApvonMl8K2QjHrHZzEc9DuQHw+gU+daR5xRfjxuYGn06GaC2SOQlb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0M/qvuKAEWep5C/DsEPdlLODzc2eAB+BdDnktsF0N26uSPgNVsFWcPQqeor9JXquY54D/P4Reim9cr6f1WQ3sAAAAAAAAAAAAAAAAAAAAAAAAwPTAhMAkGBSsOAwIaBQAEFKc7/KC56/8KHnEF46zNWh+L6HxLBBQ0t41WJfRszNhVGtti7Zi6m8/q9wICBAAAAA==

I have almost no ability to contact the people who actually wrote the SSO provider (they're in another country, and respond to even the most detailed e-mails with basically useless information and log entries that mean nothing to me), so I kind of have to figure this out myself. If anyone has anything on this process, this exception, something wrong with the encoded certificate or the code I'm using, please help out, I feel like I'm starting to go crazy from trying random things and having them not work.

According to docs.oracle.com/javase/6/docs/api/java/security/cert/… you need a DER format certificate. Is there a particular reason you are getting a PFX? – PrimeNerd Feb 28, 2013 at 23:57 The only format my Oauth provider can send back to the GetClientCredentials call is PFX. From the documentation: "The format that the client would like the client credential delivered in. This must be "pfx", which is the default." – Gakidou Mar 1, 2013 at 0:01

Here is function made in Kotlin using CertificateFactory. Input could be as You mention (not DER).

private fun certificateFromString(base64: String): X509Certificate? {
    val decoded = Base64.decode(base64, Base64.NO_WRAP)
    val inputStream = ByteArrayInputStream(decoded)
    return CertificateFactory.getInstance("X.509").generateCertificate(inputStream) as? X509Certificate
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.