如何在Android设备上安装证书链

问题描述 投票:0回答:2

API 27上的Android设备,我的应用程序是设备所有者。

使用DevicePolicyManager,有人可以提供一个installKeyPair功能的例子吗?

public boolean installKeyPair (ComponentName admin, 
            PrivateKey privKey, 
            Certificate[] certs, 
            String alias, 
            boolean requestAccess)

如何获取.pem文件(从.p12)到PrivateKey以及Certificate对象?

我似乎无法在网上找到任何例子......

java android certificate ssl-certificate device-admin
2个回答
0
投票

如果您查看CertInstallerActivity的AOSP源代码,您将看到一个示例用法:

您需要将文件作为字符串读取,然后您似乎只需遵循其使用模式:

String alias = "alias_as_string";
String key = "pem_as_string";
String cert = "crt_as_string";

// create keySpec from the pem file
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
        Base64.decode(key, Base64.DEFAULT));

// generate the RSP private key from the keySpec
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privatekey = kf.generatePrivate(keySpec);

// generate the certificate object for the given content
Certificate certificate = CertificateFactory.getInstance("X.509")
        .generateCertificate(
                new Base64InputStream(new ByteArrayInputStream(cert.getBytes()),
                        Base64.DEFAULT));

然后实际调用DevicePolicyManager:

dpm.installKeyPair(null, privatekey, certificate,  alias);

我从来没有用过这个,只是在阅读AOSP


0
投票

如果您从.p12(PKCS12)文件开始,您可能还想查看KeyChain.createInstallIntentdocumentation)。

© www.soinside.com 2019 - 2024. All rights reserved.