使用WifiEnterpriseConfig setClientKeyEntry的Android Wifi EAP TLS

时间:2014-02-18 17:25:29

标签: android android-wifi wificonfiguration

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

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

1 个答案:

答案 0 :(得分:1)

试试这个:

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);

未显示正确的异常处理。