how to programmatically acces the window and mac trusted certificate store

怎甘沉沦 提交于 2019-12-06 02:25:12

Below is code snippet for Windows/MAC to add certificate in their trust store.

Window:

    KeyStore root = KeyStore.getInstance("Windows-ROOT","SunMSCAPI");
    root.load(null,null);
    /* certificate must be DER-encoded */
    FileInputStream in = new FileInputStream("yourcertificate.cer");
    X509Certificate cacert = (X509Certificate)CertificateFactory.getInstance("X.509").generateCertificate(in);                      
    root.setCertificateEntry("certificatealiasname", cacert);

In windows it is successfully adding the certificate in trust store, but some system does not work due to not having admin privileges. So in those machine it will work if logged in as Administrator or give the user some Admin privileges.

MAC:

        KeyStore root = KeyStore.getInstance("KeychainStore", "Apple");
        root.load(null);
        /* certificate must be DER-encoded */
        FileInputStream in = new FileInputStream("yourcertificate.cer");
        X509Certificate cacert = (X509Certificate)CertificateFactory.getInstance("X.509").generateCertificate(in);
        root.setCertificateEntry("certificatealiasname", cacert);
        root.store(null, null);

It is able to successfully add the certificate in keychain but not trusting the certificate. So need to go to KeyChain Access and manually trust the certificate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!