Android:连接到https的BKS密钥库抛出错误. [英] Android : BKS keystore throwing error for connecting to https.

查看:119
本文介绍了Android:连接到https的BKS密钥库抛出错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个Android项目中尝试通过Http客户端连接到HTTPS端点.我检查了一些链接,发现我必须将.keystore文件转换为.bks或充气城堡.我使用了 Portecle ,并且能够将密钥库文件转换为bks.当我尝试更新的密钥库文件时,出现以下错误.

I am working on an Android project in which I am trying to connect to an HTTPS endpoint with an Http client. I checked some links and found out that I have to convert the .keystore file to .bks or bouncy castle. I used Portecle and was able to convert the keystore file to bks. I am getting the following error when I try with the updated keystore file.

错误日志:

.059  [main] WARN org.eclipse.jetty.util.component.AbstractLifeCycle - FAILED SslContextFactory@9824b76(null,null): java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
.059  java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
.067      at java.security.KeyStore.getInstance(KeyStore.java:119)
.068      at org.eclipse.jetty.util.security.CertificateUtils.getKeyStore(CertificateUtils.java:52)
.068      at org.eclipse.jetty.util.ssl.SslContextFactory.loadKeyStore(SslContextFactory.java:871)
.068      at org.eclipse.jetty.util.ssl.SslContextFactory.doStart(SslContextFactory.java:273)
.068      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
.068      at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
.068      at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
.068      at org.eclipse.jetty.client.HttpClient.doStart(HttpClient.java:229)
.068      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
.068      at myapp.Activity.ConsoleChatClient.performConnection(ConsoleChatClient.java:109)
.069      at myapp.Activity.ConsoleChatClient.onCreate(ConsoleChatClient.java:56)
.069      at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
.069      at android.app.ActivityThread.-wrap4(ActivityThread.java)
.069      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
.069      at android.os.Handler.dispatchMessage(Handler.java:102)
.069      at android.os.Looper.loop(Looper.java:148)
.069      at android.app.ActivityThread.main(ActivityThread.java:5417)
.069      at java.lang.reflect.Method.invoke(Native Method)
.069      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
.069      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
.069  Caused by: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
.075      at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:190)
.075      at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:139)
.075      at java.security.KeyStore.getInstance(KeyStore.java:116)
.075    ... 19 more
.075  [main] WARN org.eclipse.jetty.util.component.AbstractLifeCycle - FAILED org.eclipse.jetty.client.HttpClient@f573e38: java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
.075  java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
.075      at java.security.KeyStore.getInstance(KeyStore.java:119)
.075      at org.eclipse.jetty.util.security.CertificateUtils.getKeyStore(CertificateUtils.java:52)
.075      at org.eclipse.jetty.util.ssl.SslContextFactory.loadKeyStore(SslContextFactory.java:871)
.075      at org.eclipse.jetty.util.ssl.SslContextFactory.doStart(SslContextFactory.java:273)
.075      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
.075      at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
.075      at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
.075      at org.eclipse.jetty.client.HttpClient.doStart(HttpClient.java:229)
.075      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
.075      at myapp.Activity.ConsoleChatClient.performConnection(ConsoleChatClient.java:109)
.076      at myapp.Activity.ConsoleChatClient.onCreate(ConsoleChatClient.java:56)
.076      at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
.076      at android.app.ActivityThread.-wrap4(ActivityThread.java)
.076      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
.076      at android.os.Handler.dispatchMessage(Handler.java:102)
.076      at android.os.Looper.loop(Looper.java:148)
.076      at android.app.ActivityThread.main(ActivityThread.java:5417)
.076      at java.lang.reflect.Method.invoke(Native Method)
.076      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
.076      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
.076  Caused by: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
.076      at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:190)
.076      at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:139)
.076      at java.security.KeyStore.getInstance(KeyStore.java:116)
.076    ... 19 more
.076  java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
.076      at java.security.KeyStore.getInstance(KeyStore.java:119)
.076      at org.eclipse.jetty.util.security.CertificateUtils.getKeyStore(CertificateUtils.java:52)
.076      at org.eclipse.jetty.util.ssl.SslContextFactory.loadKeyStore(SslContextFactory.java:871)
.076      at org.eclipse.jetty.util.ssl.SslContextFactory.doStart(SslContextFactory.java:273)
.076      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
.077      at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
.077      at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
.077      at org.eclipse.jetty.client.HttpClient.doStart(HttpClient.java:229)
.077      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
.077      at myapp.Activity.ConsoleChatClient.performConnection(ConsoleChatClient.java:109)
.077      at myapp.Activity.ConsoleChatClient.onCreate(ConsoleChatClient.java:56)
.077      at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
.077      at android.app.ActivityThread.-wrap4(ActivityThread.java)
.077      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
.077      at android.os.Handler.dispatchMessage(Handler.java:102)
.077      at android.os.Looper.loop(Looper.java:148)
.077      at android.app.ActivityThread.main(ActivityThread.java:5417)
.077      at java.lang.reflect.Method.invoke(Native Method)
.077      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
.077      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
.077  Caused by: java.security.NoSuchAlgorithmException: KeyStore JKS implementation not found
.077      at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:190)
.077      at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:139)
.077      at java.security.KeyStore.getInstance(KeyStore.java:116)
.077    ... 19 more
.433 7290-7297/myapp W/art: Suspending all threads took: 24.215ms
.725 7290-7360/myapp I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
.725 7290-7360/myapp I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
.725 7290-7360/myapp I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
04-21 10:42:33.849 7290-7301/myapp I/art: Background partial concurrent mark sweep GC freed 2372(599KB) AllocSpace objects, 2(1076KB) LOS objects, 39% free, 14MB/24MB, paused 22.566ms total 40.576ms
04-21 10:42:33.914 7290-7297/myapp W/art: Suspending all threads took: 5.063ms
04-21 10:42:40.786 7290-7336/myapp I/GMPM: Tag Manager is not found and thus will not be used

代码:

SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setTrustAll(true);
            sslContextFactory.setEndpointIdentificationAlgorithm("HTTPS");

            InputStream ins = getResources().openRawResource(
                    getResources().getIdentifier("bksdomain",
                            "raw", getPackageName()));
            File file = new File(getCacheDir(), "bksdomain.bks");

            try {
                OutputStream output = new FileOutputStream(file);
                try {
                    try {
                        byte[] buffer = new byte[4 * 1024]; // or other buffer size
                        int read;

                        while ((read = ins.read(buffer)) != -1) {
                            output.write(buffer, 0, read);
                        }
                        output.flush();
                    } finally {
                        output.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace(); // handle exception, define IOException and others
                }
            } finally {
                ins.close();
            }

            Resource keystore = Resource.newResource(file);
  sslContextFactory.setKeyStoreResource(keystore);
            sslContextFactory.setKeyStorePassword("PASSWORD");
            sslContextFactory.setRenegotiationAllowed(true);
            sslContextFactory.setWantClientAuth(false);
            HttpClient localClient = new HttpClient(sslContextFactory);
            localClient.setFollowRedirects(true);
            localClient.start();
}

我做错了什么?如果需要更多信息,请告诉我.谢谢你.

What am I doing wrong? If any more information is required, kindly let me know. Thank you.

推荐答案

我遇到了类似的问题,必须将setKeyStoreType("BKS")和setTrustStoreType("BKS")设置为已更改,因为我将提供程序更改为"BC"尝试默认加载默认信任库时不了解JKS,所以请当心!看起来好像不能解决您的问题,因为您的堆栈跟踪显示loadKeyStore,我的在甚至没有到达loadKeyStore之前在loadTrustStore的堆栈跟踪中都失败了.

I had a similar problem, had to setKeyStoreType("BKS") AND setTrustStoreType("BKS") because I had changed the provider to "BC" which doesn't understand JKS when it tries to default load the default trust store, so watch out for that! Looks like it doesn't solve your problem as your stack trace shows loadKeyStore, mine was failing in the stack trace at loadTrustStore before it even got to loadKeyStore...

这篇关于Android:连接到https的BKS密钥库抛出错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆