简体   繁体   English

如何使Android上的NanoHTTPD接受具有专用客户端证书的客户端连接

[英]How to make NanoHTTPD on Android accept connection from client with dedicated client certificate

I make an web service on an Android device using NanoHTTPD. 我使用NanoHTTPD在Android设备上制作了Web服务。 But it will trust all certificate and accept SSL connection from all client. 但是它将信任所有证书并接受来自所有客户端的SSL连接。 I want to limit the access from specific client only. 我只想限制来自特定客户端的访问。

Update: I try to work like this: 更新:我尝试像这样工作:

        String KEYSTOREPASS = "test";
        char[]ctpass = KEYSTOREPASS.toCharArray();
        KeyStore ks = KeyStore.getInstance("PKCS12");

        //Directly load cert from Resources
        //ks.load(ctx.getResources().openRawResource(R.raw.cayan_cert),kspass);

        //Or dynamically generate a cert and use it
        ipAddressInCN = MainApplication.getIPAddress();

        //Use the current IP Address to generate a cert that signed by hard coded CA, and add to keystore
        String CN = "CN=" + ipAddressInCN;
        ks.load(null, null);
        GenerateCSR.AddCertToKeyStore(ks, ctpass, CN);

        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, ctpass);

        SSLContext sc = SSLContext.getInstance("TLS");

        TrustManager[] tm = new TrustManager[]{new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {

                return new java.security.cert.X509Certificate[0];
            }

            public void checkClientTrusted(java.security.cert.X509Certificate[] certs,
                                           String authType) {
                System.out.println("abc");
                return;
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs,
                                           String authType) {

                return;
            }

        }};

        sc.init(kmf.getKeyManagers(), tm, null);
        server.makeSecure(sc.getServerSocketFactory(), null);

I try to set break point to my custom trust manager functions but they are never called. 我尝试为我的自定义信任管理器功能设置断点,但从未调用过它们。

But it will trust all certificate and accept SSL connection from all client. 但是它将信任所有证书并接受来自所有客户端的SSL连接。

Not true. 不对。 It will only accept SSL connections from clients with trusted certificates, unless you have installed some brain-dead trust-all-certificates garbage, in which case you should remove them. 除非您安装了一些令人讨厌的信任所有证书的垃圾,否则它将只接受带有受信任证书的客户端的SSL连接,在这种情况下,您应该删除它们。

I want to limit the access from specific client only. 我只想限制来自特定客户端的访问。

You should do that via authorization, which you have to implement yourself in NanoHTTPD. 您应该通过授权来实现,您必须在NanoHTTPD中实现自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM