如何在 macos 上的 LaunchDaemon 中使用 SecTrustSettingsSetTrustSettings?

How to use SecTrustSettingsSetTrustSettings In a LaunchDaemon on macos?

提问人:Shadow fiend 提问时间:10/30/2023 更新时间:10/30/2023 访问量:20

问:

我使用此代码将文件中的证书添加到系统钥匙串,并使其受信任。

// Load the certificate from file
           NSData *certData = [NSData dataWithContentsOfFile:@"/Users/zhuzhengjia/cert.der"];
           if (!certData) {
               NSLog(@"Failed to read the certificate file.");
               return -1;
           }
           
           // Create a certificate object from the data
           SecCertificateRef certRef = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
           if (!certRef) {
               NSLog(@"Failed to create certificate from data.");
               return -1;
           }
           // 3. Add the certificate to the system keychain
                   SecKeychainRef keychain = NULL;
                   OSStatus status = SecKeychainOpen("/Library/Keychains/System.keychain", &keychain);
                   if (status == errSecSuccess) {
                       status = SecCertificateAddToKeychain(certRef, keychain);
                       CFRelease(keychain);
                   }
                   if (status != errSecSuccess) {
                       NSLog(@"Failed to add certificate to keychain: %d", (int)status);
                       CFRelease(certRef);
                       return -1;
                   }

                   // 4. Set trust settings for the certificate
                   status = SecTrustSettingsSetTrustSettings(certRef, kSecTrustSettingsDomainAdmin, NULL);
                   if (status != errSecSuccess) {
                       spdlog::error("Failed to set trust settings: {}", (int)status);
                       CFRelease(certRef);
                       return -1;
                   }
           
           CFRelease(certRef);

如果我通过命令行运行executatle,例如sudo ./program。它运作良好。但是,如果我在 LaunchDaemon 中运行此代码。SecTrustSettingsSetTrustSettings 返回 -60007。为什么会这样?我应该如何让它在守护进程中工作?

Objective-C macOS

评论


答: 暂无答案