【问题标题】:Is it possible to automatically import certificates in Firefox?是否可以在 Firefox 中自动导入证书?
【发布时间】:2016-05-31 18:30:04
【问题描述】:

在 Linux 上部署 Firefox 时,是否可以使用类似于 autoconfig 的方式自动导入证书?

我搜索了一下,发现了一些执行此操作的说明,但似乎都已过时。

【问题讨论】:

    标签: javascript firefox certificate


    【解决方案1】:

    这是可能的,并且在任何受支持的平台(包括 Linux)上都可以正常工作。

    你主要有两种可能:

    使用https://mike.kaply.com/cck2/

    这是一个 Firefox 扩展,您可以使用它来自定义 Firefox 中的许多内容(包括添加 CA 证书)。它会生成一个 zip 文件,您可以将其部署在 Firefox 安装目录(类似于 Linux 上的 /usr/lib/firefox)之上。这可能是最简单的方法。

    使用自动配置

    我用它在企业环境中部署 CA 证书(未使用其他类型的证书进行测试)。它还允许进行您需要的任何自定义。

    首先,您需要配置一个自动配置文件,具体方法请参见https://developer.mozilla.org/en-US/Firefox/Enterprise_deployment#Configuration

    然后,您需要将您的证书文件放入 defaults/pref 子目录(例如:/usr/lib/firefox/defaults/pref),将下面的函数放入您的 autoconfig 文件中,然后调用它:

    // This imports a root certificate into Firefox
    // The certificate has to be in defaults/pref directory of Firefox
    // Source : http://xulfr.org/forums/read.php?1,8256
    function importCert(certFileName) {
        var BEGIN_CERT = "-----BEGIN CERTIFICATE-----";
        var END_CERT = "-----END CERTIFICATE-----";
    
        var x509certdb = Components.classes["@mozilla.org/security/x509certdb;1"];
        var certDB ;
        try {
            // For Firefox <=32
            certDB = x509certdb.getService(Components.interfaces.nsIX509CertDB2);
        }
        catch (exc) {
            // For Firefox >=33
            certDB = x509certdb.getService(Components.interfaces.nsIX509CertDB);
        }
    
        var ioService = Components.classes["@mozilla.org/network/io-service;1"]
                                .getService(Components.interfaces.nsIIOService);
    
        var scriptableStream = Components.classes["@mozilla.org/scriptableinputstream;1"]
                                        .getService(Components.interfaces.nsIScriptableInputStream);
    
    
        // https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O#Getting_special_files
        Components.utils.import("resource://gre/modules/FileUtils.jsm");  
        var certFile = FileUtils.getFile("PrfDef", [certFileName]);
    
        // http://www.mozilla.org/projects/security/pki/nss/tools/certutil.html
        var trustFlags = "C,C,C";
    
        var channel = ioService.newChannelFromURI(ioService.newFileURI(certFile));
        var input = channel.open();
        scriptableStream.init(input);
        var certfile = scriptableStream.read(input.available());
        scriptableStream.close();
        input.close();
    
        certfile = certfile.replace(/[\r\n]/g, "");
        begin = certfile.indexOf(BEGIN_CERT);
        end = certfile.indexOf(END_CERT);
        cert = certfile.substring(begin + BEGIN_CERT.length, end);
    
        certDB.addCertFromBase64(cert, trustFlags, "");
    }
    importCert("myCert.pem");
    

    您的证书需要采用 ASCII Base64 X509 格式(以 -----BEGIN CERTIFICATE----- 开头的文本文件)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 2020-10-27
      相关资源
      最近更新 更多