Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用WifiEnterpriseConfig setClientKeyEntry的Android Wifi EAP TLS_Android_Android Wifi_Wifi - Fatal编程技术网

使用WifiEnterpriseConfig setClientKeyEntry的Android Wifi EAP TLS

使用WifiEnterpriseConfig setClientKeyEntry的Android Wifi EAP TLS,android,android-wifi,wifi,Android,Android Wifi,Wifi,WifiEnterpriseConfig setClientKeyEntry方法接受私钥和证书引用。 我有一个.p12证书存储在Android证书存储中。 我可以使用密钥链API(getCertificateChain,getPrivateKey)获取私钥和证书引用并将其传递给setClientKeyEntry吗? 或者,如果我有一个字符串或字节数组格式的.p12证书,那么我是否需要将其存储在证书存储中才能将其用于Wifi EAP-TLS 假设: Android 4.3+客户端上编程设置的EAP

WifiEnterpriseConfig setClientKeyEntry方法接受私钥和证书引用。 我有一个.p12证书存储在Android证书存储中。 我可以使用密钥链API(getCertificateChain,getPrivateKey)获取私钥和证书引用并将其传递给setClientKeyEntry吗? 或者,如果我有一个字符串或字节数组格式的.p12证书,那么我是否需要将其存储在证书存储中才能将其用于Wifi EAP-TLS

假设: Android 4.3+客户端上编程设置的EAP-TLS需要setClientKeyEntry方法。

尝试以下方法:

WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"your_ssid\"";
wc.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
wc.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
wc.enterpriseConfig.setEapMethod(Eap.TLS);
wc.status = WifiConfiguration.Status.ENABLED;

...

KeyStore pkcs12ks = KeyStore.getInstance("pkcs12");

in = new BufferedInputStream(new FileInputStream(new File("/path/to/your.p12")));
// alternatively you can read from any input stream, e.g. ByteArrayInputStream to read from String

pkcs12ks.load(in, "pasword".toCharArray());

Enumeration<String> aliases = pkcs12ks.aliases();
while (aliases.hasMoreElements()) {
    String alias = aliases.nextElement();
    Log.d(TAG, "Processing alias " + alias);

    X509Certificate cert = (X509Certificate) pkcs12ks.getCertificate(alias);
    Log.d(TAG, cert.toString());

    PrivateKey key = (PrivateKey) pkcs12ks.getKey(alias, "password".toCharArray());
    Log.d(TAG, key.toString());

    wc.enterpriseConfig.setClientKeyEntry(key, cert);
    wc.enterpriseConfig.setIdentity("WiFi-1");
}

...

int netID = wifiManager.addNetwork(wc);
wifiManager.saveConfiguration();
wifiManager.enableNetwork(netID, true);
WifiConfiguration wc=新的WifiConfiguration();
wc.SSID=“\”您的\u SSID\”;
wc.allowedKeyManagement.set(keymanagem.WPA_EAP);
wc.allowedKeyManagement.set(keymanagemt.IEEE8021X);
wc.enterpriseConfig.setEapMethod(Eap.TLS);
wc.status=WifiConfiguration.status.ENABLED;
...
KeyStore pkcs12ks=KeyStore.getInstance(“pkcs12”);
in=new BufferedInputStream(new FileInputStream(new File(“/path/to/your.p12”));
//或者,您可以从任何输入流中读取,例如,从字符串读取的ByteArrayInputStream
加载(在“pasword.tocharray()中);
枚举别名=pkcs12ks.alias();
while(别名.hasMoreElements()){
字符串别名=别名。nextElement();
Log.d(标记“处理别名”+别名);
X509Certificate cert=(X509Certificate)pkcs12ks.getCertificate(别名);
Log.d(TAG,cert.toString());
PrivateKey=(PrivateKey)pkcs12ks.getKey(别名“password”.tocharray());
Log.d(标记,key.toString());
wc.enterpriseConfig.setClientKeyEntry(密钥、证书);
wc.enterpriseConfig.setIdentity(“WiFi-1”);
}
...
int netID=wifiManager.addNetwork(wc);
wifiManager.saveConfiguration();
wifiManager.enableNetwork(netID,true);

未显示正确的异常处理。

从测试中,我观察到,当我使用KeyChain getPrivateKey方法获取PrivateKey引用,然后在setClientKeyEntry中使用它时,它会抛出异常-java.lang.IllegalArgumentException:私钥无法编码。如果getEncoded方法为PrivateKey对象引用返回null,setClientKeyEntry方法将引发此异常。不清楚getEncoded为什么返回null。