以编程方式检查 Android 中是否已安装客户端证书

发布于 2025-01-11 01:57:55 字数 1771 浏览 3 评论 0原文

我有一个 Android 应用程序,我使用以下代码安装客户端证书。

 val inputStream: InputStream = resources.openRawResource(R.raw.client)
val intent = KeyChain.createInstallIntent()                           
val p12: ByteArray = inputStream.readBytes()                          
intent.putExtra(KeyChain.EXTRA_PKCS12, p12)                      
intent.putExtra(KeyChain.EXTRA_NAME, "Sample cert")                   
startActivityForResult(intent,3)  

现在,一旦用户安装了证书,我不想再次重复此操作,因此我想检查证书是否已安装。

我使用以下代码进行检查,但没有获得“AndroidCAStore”和“PKCS12”的证书。

“AndroidCAStore” - 返回所有受信任的 CA 证书,但我的证书位于用户凭据中。

“PKCS12” - 为空

    //val ks = KeyStore.getInstance("AndroidCAStore")                               
  val ks: KeyStore = KeyStore.getInstance("PKCS12")                               
  if (ks != null) {                                                               
      ks.load(null, null)                                                         
      val aliases = ks.aliases()                                                  
      while (aliases.hasMoreElements()) {                                         
          val alias = aliases.nextElement() as String                             
          val cert = ks.getCertificate(alias) as X509Certificate                  
          Log.d("Cert ---->",cert.issuerDN.name)                                  
          if (cert.issuerDN.name.contains(issuerDn)) {                            
              return true                                                         
          }                                                                       
      }                                                                           
  }       

有人可以帮我解决这个问题吗?

I have an android app where I am installing the client certificate using the following code.

 val inputStream: InputStream = resources.openRawResource(R.raw.client)
val intent = KeyChain.createInstallIntent()                           
val p12: ByteArray = inputStream.readBytes()                          
intent.putExtra(KeyChain.EXTRA_PKCS12, p12)                      
intent.putExtra(KeyChain.EXTRA_NAME, "Sample cert")                   
startActivityForResult(intent,3)  

Now once user installs the certificate, I dont want to repeat this again so I want to check if the certificate is already installed.

I used the following code to check it, but doest get the certificate with both "AndroidCAStore" and "PKCS12".

"AndroidCAStore" - returns all trusted CA certs but my certificate is in user credentials.

"PKCS12" - IS empty

    //val ks = KeyStore.getInstance("AndroidCAStore")                               
  val ks: KeyStore = KeyStore.getInstance("PKCS12")                               
  if (ks != null) {                                                               
      ks.load(null, null)                                                         
      val aliases = ks.aliases()                                                  
      while (aliases.hasMoreElements()) {                                         
          val alias = aliases.nextElement() as String                             
          val cert = ks.getCertificate(alias) as X509Certificate                  
          Log.d("Cert ---->",cert.issuerDN.name)                                  
          if (cert.issuerDN.name.contains(issuerDn)) {                            
              return true                                                         
          }                                                                       
      }                                                                           
  }       

Can some one help me fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻梦 2025-01-18 01:57:55

当您调用

val Intent = KeyChain.createInstallIntent()

时,您将证书存储在 Android 钥匙串中,我认为没有办法以编程方式访问存储在那里的证书,请参阅 这个未答复发布

由于您想检查证书是否安装在 KeyChain 中,因此可以调用 KeyChain.getPrivateKey()KeyChain.getCertificateChain() 并如果它们返回 null,则表示证书尚未安装。

注意:您有一个限制,必须首先调用 KeyChain.choosePrivateKeyAlias 才能在应用和 KeyChain 之间建立信任,否则您将获取 KeyChain 异常。

如果您不需要使用 KeyChain,那么您只需创建自己的 KeyStore 并向其中添加您的证书即可。然后,您将能够调用 aliases() 来获取 KeyStore 中证书的所有别名。

When you call

val intent = KeyChain.createInstallIntent()

you are storing the Certificate in the Android Keychain and I don't think there's a way of accessing the Certificates stored there programatecally, see this unanswered post.

Since you want to check if the certificate was installed in the KeyChain, you can call KeyChain.getPrivateKey() or KeyChain.getCertificateChain() and if they return null, then it means that the Certificate has not been installed yet.

Note: You have the limitation that you have to call KeyChain.choosePrivateKeyAlias first to establish trust between the app and the KeyChain, otherwise you'll get a KeyChain exception.

If you don't need to use the KeyChain, then you can simply create your own KeyStore and add your certificates to it. Then you will be able to call aliases() to get all of the aliases of the certificates in the KeyStore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文