Electron import x509 cert to local keychain (macOS) - 授权被拒绝,因为无法进行用户交互

Electron import x509 cert to local keychain (macOS) - The authorization was denied since no user interaction was possible

我想将证书 (x509) 导入本地密钥库(win 和 macOS)。使用此电子应用程序,用户可以在本地创建网站并启用 HTTPS,这就是我们需要存储证书的原因。

问题:

在极少数情况下,用户在证书导入期间收到错误消息 仅在 macOS:

Command failed: security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain /Users/user/ssl/asdasd.local.crt SecTrustSettingsSetTrustSettings: The authorization was denied since no user interaction was possible.

如果用户 运行 在终端中使用 sudo 完全相同的命令,它将按预期工作。

注意:所有收到这个错误信息的用户都开启了apple watch认证,但是我们知道有一个用户也开启了apple watch auth,但是他并没有收到错误信息。

我们做什么:

在 electron 渲染器进程中,我们 运行 使用 sudo-prompt npm 包的以下命令。每次第一次我们从密钥库中删除证书(如果它确实存在)并添加新的。

Windows 添加证书:

certutil -addstore -f ROOT C:\Users\user\ssl\asdasd.local.crt

Windows 删除证书:

certutil -delstore ROOT asdasd.local

macOS 添加证书:

security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain /Users/user/ssl/asdasd.local.crt

macOS 删除证书:

security delete-certificate -c asdasd.local

更新

我在 sudo-prompt 回购中打开了一个问题:https://github.com/jorangreef/sudo-prompt/issues/137

M1 mac 两次询问密码。

更新 2

我刚刚意识到这个问题与 M1 无关。可能与 Big Sur 11.1 有关。

看起来 sudo-prompt 包隐藏了第二个密码对话框。

更新 3

该命令将证书存储在钥匙串中,但如果发生错误,则信任值不是“始终信任”。

更新 4

我试过 osascript 方法。它在 Big Sur 11.0 上完美运行,但在 M1 Big Sur 11.1 上出现相同的错误。 (附截图)

我要如何测试?

运行执行以下命令:

const sudo = require('sudo-prompt')

sudo.exec(
  'security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain /Users/kotapeter/ssl/test.local.crt',
  {
    name: 'test',
  },
  (error, stdout) => {
    if (error) {
      console.log(error)
    } else {
      console.log(stdout)
    }
  }
)

我们使用以下证书格式:

我建议使用如下所示的方法,它会通过 GUI 自动请求管理员访问权限

const { exec } = require('child_process');

const proc = exec('osascript -e \'do shell script "pwd" with administrator privileges\'', function (error, stdout, stderr) {
    if (error) {
      console.log(error.stack);
      console.log('Error code: '+error.code);
      console.log('Signal received: '+error.signal);
    }
    console.log('Child Process STDOUT: '+stdout);
    console.log('Child Process STDERR: '+stderr);
  });
  
  proc.on('exit', function (code) {
    console.log('Child process exited with exit code '+code);
  });

这是在 MacOS Big Sur 11.1

更新 23-Jan-21:

您可以构建自己的可执行文件,并 运行 从 NodeJS

//
//  main.swift
//  AddCert
//
//  Created by Tarun Lalwani on 23/01/21.
//

import Foundation

import Security

let certInfo: CFDictionary

enum SecurityError:Error {
    case generalError
}

func deleteCertificateFromKeyChain(_ certificateLabel:String) -> Bool{
    
    let delQuery : [NSString:Any] = [
        kSecClass: kSecClassCertificate,
        kSecAttrLabel: certificateLabel,
    ]
    let delStatus:OSStatus = SecItemDelete(delQuery as CFDictionary)
    
    return delStatus == errSecSuccess
    
}

func saveCertificateToKeyChain(_ certificate:SecCertificate, certificateLabel:String) throws {
    deleteCertificateFromKeyChain(certificateLabel)

    let setQuery: [NSString: AnyObject] = [
        kSecClass: kSecClassCertificate,
        kSecValueRef: certificate,
        kSecAttrLabel: certificateLabel as AnyObject,
        kSecAttrAccessible: kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
    ]
    let addStatus:OSStatus = SecItemAdd(setQuery as CFDictionary, nil)
    
    
    guard addStatus == errSecSuccess else {
        throw SecurityError.generalError
    }
    
    var status = SecTrustSettingsSetTrustSettings(certificate, SecTrustSettingsDomain.admin, nil)
    
}

func getCertificateFromString(stringData:String) throws -> SecCertificate{
    
    if let data:NSData = NSData(base64Encoded: stringData, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)  {
        if let certificate = SecCertificateCreateWithData(kCFAllocatorDefault, data) {
            return certificate
        }
    }
    throw SecurityError.generalError
}

var certificateString:String = "MI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EvzMIiqK94YYPmOC62cdhOaZIJmro6lL7eFLqlYfLU4H52ICuntBxvOx0UBExn"


let certificate = try! getCertificateFromString(stringData: certificateString)


try? saveCertificateToKeyChain(certificate, certificateLabel: "Test")

在上面的例子中,我没有使用证书文件,但是你可以更新代码来使用它,你也可以使用命令行参数来获取文件路径

感谢以下文件使此代码正常工作

https://github.com/ibm-bluemix-mobile-services/bms-clientsdk-swift-security/blob/c26988a7f5de338c1d9e0d43a64c2b6db33be541/Source/mca/internal/certificate/SecurityUtils.swift