Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by dcariola · Sep 20, 2011 at 10:43 AM · androidwwwexceptionhttpscertificate

Android https request returns SSLHandshakeException

I'm trying to send a simple https POST request through WWW class. I have to say beforehand than my server certificate is not trusted by a Certification Authority for the moment.

While on Editor, Webplayer and iOS the request works fine, ignoring the not trusted certificate, on Android device (Motorola Xoom in my case) i got an exeption:

 javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

Is there a way to let device ignore and go through it without a trusted certificate? Am I missing something?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SolidSnake · Oct 03, 2012 at 03:04 PM 0
Share

any luck with this? i am trying to do the same with self signed SSL certificate

avatar image cowlinator · Nov 07, 2012 at 11:22 PM 0
Share

any luck with this yet? i am trying to do the same thing.

avatar image sunkas85 · Dec 07, 2012 at 09:40 AM 0
Share

I wonder the same thing! Any leads at all? $$anonymous$$y certificate should be CA trusted but it's not working on Android. In iOS and editor i works fine.

avatar image SolidSnake · Dec 07, 2012 at 12:19 PM 0
Share

i had the issue with trusted certificate as well which worked only on Android 2.3 and above.. not 2.2 did you have the same issue or it didn't work regardless of the OS version? it appears that on Android 2.2 and below it has an issue: http://stackoverflow.com/questions/9538714/android-2-2-ssl-bug-with-client-certificate

avatar image sunkas85 · Dec 07, 2012 at 12:37 PM 0
Share

Thanks for your reply! Had 2.2, but it makes no difference when switching. Still same error. Tried 2.3, 3.0 and 4.0.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by devluz · Mar 01, 2015 at 07:38 AM

Old question but I couldn't find any answer myself so I will leave my solution here in case someone else ends up here:

The problem is that androids HTTPS classes refuse to connect to systems with self signed/not trusted certificates. So you can get a certificate from one of the trusted CA's or you can change androids default trust manager which decides if your certificate is trustworthy. This works because the WWW class seems to simply use the default trust manager. More about the Android side of this topic: https://developer.android.com/training/articles/security-ssl.html

The problem is solvable by using a selfmade android plugin for Unity. The following code is from my Java plugin code. It contains a class with a static method which gets from unity the certificate and adds it to a list of allowed certificates. It has to be called once before the first use of WWW.

 public class JavaSSLHelper
 {
     //see https://developer.android.com/training/articles/security-ssl.html
     public static void trust(byte[] crtFileContent)
     {
         try
         {
             // Load CAs from an InputStream
             CertificateFactory cf = CertificateFactory.getInstance("X.509");

             InputStream caInput = new BufferedInputStream(new ByteArrayInputStream(crtFileContent));
             Certificate ca;
             try {
                 ca = cf.generateCertificate(caInput);
                 Log.d("JavaSSLHelper", "ca=" + ((X509Certificate) ca).getSubjectDN());
                 Log.d("JavaSSLHelper", "Certificate successfully created");
             } finally
             {
                 caInput.close();
             }

             // Create a KeyStore containing our trusted CAs
             String keyStoreType = KeyStore.getDefaultType();
             KeyStore keyStore = KeyStore.getInstance(keyStoreType);
             keyStore.load(null, null);
             keyStore.setCertificateEntry("ca", ca);

             // Create a TrustManager that trusts the CAs in our KeyStore
             String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
             TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
             tmf.init(keyStore);

             try
             {
                 // Create an SSLContext that uses our TrustManager
                 SSLContext context = SSLContext.getInstance("TLS");
                 context.init(null, tmf.getTrustManagers(), null);

                 //this is important: unity will use the default ssl socket factory we just created
                 HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
                 Log.d("JavaSSLHelper", "Default SSL Socket set.");
             } catch (NoSuchAlgorithmException e) {
                 throw new RuntimeException(e);
             } catch (KeyManagementException e) {
                 throw new RuntimeException(e);
             }
         }catch(Exception e)
         {
             throw new RuntimeException(e);
         }
     }
 }


Then in Unity you just have to call this method giving the certificate down to android:

         string cert = @""; //<-- include your CRT file as string

         AndroidJavaClass clsJavaSSLHelper = new AndroidJavaClass("your.package.name.JavaSSLHelper");
         byte[] certBytes = System.Text.Encoding.ASCII.GetBytes(cert);
         clsJavaSSLHelper.CallStatic("trust", certBytes); //here we call the trust method from above


After this call WWW should simply do its job.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image devluz · Jun 02, 2015 at 11:50 PM 0
Share

Because of requests I added the described plugin as a download in my blog here

avatar image churchTEA · Jul 13, 2018 at 04:28 PM 0
Share

Hey DevLuz, 3 years later.

I downloaded your plugin for c#, an UnityPackage named AndroidHttpsHelper... and is not working, I'm a novice programmer focused more on animations and interfaces, not really a network guy but there's no one else that can do this cuz no one knows Unity, can you help me with this?

avatar image
-1

Answer by Utopya · Jul 26, 2013 at 10:49 AM

Hey I have the same problem, have you solved it?

thanx

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW with HTTPS on Android not working 0 Answers

www not working on android and iOS 0 Answers

HTTPS + Android (sha2 certificate) 0 Answers

WWW and SSL on Android 1 Answer

Server certificates on Android question 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges