Android 2.3 中出现“无对等证书”错误,但 4 中则没有

2023-12-23

得到"javax.net.ssl.SSLPeerUnverifiedException: No peer certificate error"在运行 Android 2.3 的模拟器中,但在 4 中则不然。在 4 中,它运行得很好。我正在尝试通过 https 连接到实时服务器。它使用有效的 Thawte 证书,在所有浏览器以及 Android 3 和 4 中都能正常工作。

如果有人有代码帮助,请感谢。另外,如果有人对安全解决方法有任何建议,我将不胜感激。我还在学习,我已经解决这个问题一周了。它必须结束,这样我才能继续工作和学习。呃。

这是 HttpCLient 代码,由 Antoine Hauck 提供(http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/ http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/):

 import java.io.InputStream;
    import java.security.KeyStore;
    import java.security.cert.CertificateException;

    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import javax.security.cert.X509Certificate;

    import org.apache.http.conn.ClientConnectionManager;
    import org.apache.http.conn.scheme.PlainSocketFactory;
    import org.apache.http.conn.scheme.Scheme;
    import org.apache.http.conn.scheme.SchemeRegistry;
    import org.apache.http.conn.ssl.SSLSocketFactory;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.conn.SingleClientConnManager;

    import android.content.Context;

    public class MyHttpClient extends DefaultHttpClient {

    final Context context;

    public MyHttpClient(Context context) {
        this.context = context;
    }

    @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        // Register for port 443 our SSLSocketFactory with our keystore
        // to the ConnectionManager
        registry.register(new Scheme("https", newSslSocketFactory(), 443));
        return new SingleClientConnManager(getParams(), registry);
    }

    private SSLSocketFactory newSslSocketFactory() {
         try {
             // Get an instance of the Bouncy Castle KeyStore format
             KeyStore trusted = KeyStore.getInstance("BKS");
             // Get the raw resource, which contains the keystore with
             // your trusted certificates (root and any intermediate certs)
             InputStream in = context.getResources().openRawResource(R.raw.my_cert);
             try {
                 // Initialize the keystore with the provided trusted certificates
                 // Also provide the password of the keystore
                 trusted.load(in, "my_pass".toCharArray());
             } finally {
                 in.close();
             }

            // Pass the keystore to the SSLSocketFactory. The factory is responsible
            // for the verification of the server certificate.
            SSLSocketFactory sf = new SSLSocketFactory(trusted);
            // Hostname verification from certificate
            // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
            sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            return sf;
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}

这是实例化它的代码:

DefaultHttpClient client = new MyHttpClient(getApplicationContext());

           HttpPost post = new HttpPost(server_login_url);
           List <NameValuePair> parameters = new ArrayList <NameValuePair>();
           parameters.add(new BasicNameValuePair("username", user));
           parameters.add(new BasicNameValuePair("password", pass));

            try {
               post.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
            } catch (UnsupportedEncodingException e2) {
                // TODO Auto-generated catch block
                Log.d(DEBUG_TAG, "in  UnsupportedEncodingException - " + e2.getMessage());
                e2.printStackTrace();
            }
                // Execute the GET call and obtain the response
           HttpResponse getResponse = null;

            try {
                getResponse = client.execute(post);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                // Toast.makeText(getBaseContext(),message,Toast.LENGTH_LONG).show();
                Log.d(DEBUG_TAG, "in ClientProtocolException - " + e.getMessage());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // Toast.makeText(getBaseContext(),message,Toast.LENGTH_LONG).show();
                Log.d(DEBUG_TAG, "in  client.execute IOException - " + e.getMessage());
                e.printStackTrace();
            }

该错误被捕获在 IOException 块中。这是堆栈:

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:258)
org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:164)
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
org.ffb.tools.SplashActivity$LoginTask.makeConnection(SplashActivity.java:506)
org.ffb.tools.SplashActivity$LoginTask.doLogin(SplashActivity.java:451)
org.ffb.tools.SplashActivity$LoginTask.doInBackground(SplashActivity.java:439)
org.ffb.tools.SplashActivity$LoginTask.doInBackground(SplashActivity.java:1)
android.os.AsyncTask$2.call(AsyncTask.java:185)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
java.util.concurrent.FutureTask.run(FutureTask.java:138)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
java.lang.Thread.run(Thread.java:1019)

这是链顺序(来自 openssl 命令):

我觉得链条挺好看的

    i:/C=US/O=Thawte, Inc./OU=Domain Validated SSL/CN=Thawte DV SSL CA
  1 s:/C=US/O=Thawte, Inc./OU=Domain Validated SSL/CN=Thawte DV SSL CA
  i:/C=US/O=thawte, Inc./OU=Certification Services Division/OU=(c) 2006 thawte, Inc. - For authorized      use only/CN=thawte Primary Root CA
  2 s:/C=US/O=thawte, Inc./OU=Certification Services Division/OU=(c) 2006 thawte, Inc. - For      authorized use only/CN=thawte Primary Root CA
  i:/C=ZA/ST=Western Cape/L=Cape Town/O=Thawte Consulting cc/OU=Certification Services      Division/CN=Thawte Premium Server CA/[email protected] /cdn-cgi/l/email-protection

This thread https://stackoverflow.com/questions/4115101/apache-httpclient-on-android-producing-certpathvalidatorexception-issuername当我调试类似问题时确实很有帮助。

Android 2.3 HTTPS/SSL 清单摘要:

  • 如果您的CA是Android 2.3受信任的 CA 列表 http://www.andreabaccega.com/blog/2010/09/23/android-root-certification-authorities-list/-- Thawte 是 -- 无需在应用程序中包含证书。
  • 安卓2.3不支持服务器名称指示 http://en.wikipedia.org/wiki/Server_Name_Indication因此,如果您的服务器依赖它进行 SSL 握手,Android 可能无法获得您期望的证书。
  • 您的服务器上是否安装了证书链,并且顺序是否正确?大多数浏览器都会处理乱序证书链,但 Android 2.3 不会。 bdc 在我上面提到的线程中的回答描述了如何使用“openssl s_client -connect yourserver.com:443”检查 SSL 证书和链的有效性。
  • 当您翻出底部抽屉中的旧 2.3 设备时,请确保其日期和时间在长时间无电后设置正确。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 2.3 中出现“无对等证书”错误,但 4 中则没有 的相关文章

随机推荐

  • 从分叉终端运行 xcodebuild

    我正在尝试为 iPhone 应用程序设置自动构建服务器 我希望能够每晚进行临时测试版构建 以便测试人员可以跟踪开发情况 我已经成功设置了 xcode xcode 来执行即席构建 我还可以从命令行启动构建 xcodebuild configu
  • 尝试从空对象引用上的字段“android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView”读取

    我正在制作一个从 sql 获取数据并将其放入回收视图中的应用程序 我首先在列表视图中尝试过它 但我发现在回收视图中更容易 它基本上就像一个聊天系统应用程序 当我将数据添加到回收视图它在 logcat 中给了我这个错误 java lang N
  • GCP 堆栈中的 AWS SNS 等效项

    因此 我们花了近 6 个月的时间将当前的应用程序堆栈从 AWS 迁移到 GCP 现在我们陷入了 AWS SNS 部分 显然 GCP 堆栈中似乎没有任何服务可以简单地提供 SNS 的替代品 还是我错过了什么 现在 GCP 上的一切都运行得很好
  • 在 antd Form + ReactJs 中使用 antd Tooltip

    如果我输入无效的邮件 ID 我需要使用 antd 工具提示显示 无效的电子邮件 如何在ReactJS antd Form中使用它 我现在使用的代码是 div div
  • Azure 网站和 Azure Web 角色之间有什么区别

    新的和新的有什么实质性的区别 Azure 网站 https www windowsazure com en us home scenarios web sites 以及 ASP NET MVC 应用程序的传统 Azure Web 角色 我为
  • 返回这个而不是 void 有什么缺点吗?

    假设不是返回 void 方法 而是返回对该类的引用 即使它没有任何特定的语义意义 在我看来 它会给你更多关于如何调用方法的选择 允许你以类似流畅界面的风格使用它 而且我真的想不出任何缺点 因为你不需要做任何事情与返回值 甚至存储它 因此 假
  • git:有类似每个分支标签的东西吗?

    我有一些历史重写要做 为此我想暂时保持我原来的树完好无损 然而 重写的树也应该复制以前使用的标签 有没有比例如更少的手动选项在标签名称前面添加分支名称 不 git 中没有像每个分支标签那样的东西 所有分支和标签都只是 Git 中的引用 re
  • 排序算法值得在这里实施吗?

    我有一个正整数列表 我想将 3 个最大值存储在变量中h1 h2 and h3 其余值无关紧要 我考虑用一个int and realloc当内存被填满时 先对其进行排序 然后采用合适的排序算法 但这真的值得吗 因为我真的不需要对整个数组进行排
  • 如何检查 Java 中的字符串是否已到达末尾?

    我不想通过使用正式的方式来做到这一点for循环遍历字符串的所有元素 特定次数 字符串的长度 Java 中是否有任何字符始终位于每个字符串的末尾 就像 C 中的字符一样 您有两个基本选择 String myString ABCD for ch
  • 程序集版本“.001”变为“.1”

    在 WinForms 中我有一个 AssemblyVersion assembly AssemblyVersion 01 01 01 002 然而 当启动屏幕出现时 它完全忽略显示的零 1 1 1 2 作为非常不方便的版本 因为稍后我实际上
  • Python OS X - 从文件中获取“添加日期”信息

    你好 这是我的第一篇文章 我开始做一些编码Python今天在 OS X 上 我注意到 OS X 有这样的东西date added这是文件放入指定文件夹的时间 我正在尝试将该日期作为timestamp 但是没有一种类型有效 我已经尝试了我所知
  • iPhone 上的 HTML5 视频元素有边框

    我最近一直在构建一个应用程序 并在开始时播放加载动画 只是一个 mp4 视频 出于美观目的 除了 iPhone 之外 它在任何地方都可以正常工作 问题在于 视频的某些部分周围有一条灰线 每边并不相同 如果我尝试对页面进行屏幕截图 则线条将不
  • Pandas.groupby.apply() 中的内存泄漏?

    我目前正在使用 Pandas 进行一个 csv 源文件大约 600mb 的项目 在分析过程中 我将 csv 读入数据帧 对某些列进行分组并对分组的数据帧应用一个简单的函数 我注意到在此过程中我进入了交换内存 因此进行了基本测试 我首先在 s
  • Android 应用程序可以在模拟器中运行,但不能在设备中运行

    我使用 android 1 6 api 4 创建了一个 android 应用程序 android minSdkVersion 是 3 我使用的模拟器运行这个应用程序是2 2 它在模拟器中工作正常 然后我将其安装到 htc Hero andr
  • 为什么 Blazor 应用程序中的 XML 验证在本地主机上和作为 Azure 静态 Web 应用程序给出不同的消息?

    edit我做了一个简化的回购协议https github com GilShalit XMLValidation https github com GilShalit XMLValidation 我正在 Blazor WebAssembly
  • 如何将 hset 与 django-redis-cache 一起使用?

    我是 django redis 的新手 我开始熟悉 heroku redis 插件 但是 我只能使用set and get 当我尝试使用其他方法时 例如hset 我收到此错误 RedisCache object has no attribu
  • Android Studio 很慢

    我有一个项目 里面有大约 20 个模块 Gradle clean 大约需要 5 分钟 同样 如果我添加一个新模块 gradle 更新项目需要超过 5 分钟 不依赖于外部库 依赖关系仅存在于项目内部的几个模块之间 我有相同的项目 没有 gra
  • 如何获取工作流活动 (SharePoint) 中的上下文项

    我正在为 sharepoint 工作流编写自定义活动 但我不知道如何使用当前工作流项目 SPWeb 或 SPSite I see http blogs microsoft co il blogs davidbi archive 2008 0
  • htaccess如何将子目录重定向到外部URL

    I tried 301 Redirect Old File Redirect 301 www mydomain com subdirectory http newurl com 但这让我进入了 newurl com subdirectory
  • Android 2.3 中出现“无对等证书”错误,但 4 中则没有

    得到 javax net ssl SSLPeerUnverifiedException No peer certificate error 在运行 Android 2 3 的模拟器中 但在 4 中则不然 在 4 中 它运行得很好 我正在尝试