forked from actions/runner-images
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macOS] Add swift program to save certificate (actions#3311)
- Loading branch information
1 parent
adf1f3d
commit 5475c40
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
images/macos/provision/configuration/add-certificate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 { | ||
SecKeychainSetPreferenceDomain(SecPreferencesDomain.system) | ||
deleteCertificateFromKeyChain(certificateLabel) | ||
|
||
let setQuery: [NSString: AnyObject] = [ | ||
kSecClass: kSecClassCertificate, | ||
kSecValueRef: certificate, | ||
kSecAttrLabel: certificateLabel as AnyObject, | ||
kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked, | ||
] | ||
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(base64Encoded: stringData, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) { | ||
if let certificate = SecCertificateCreateWithData(kCFAllocatorDefault, data) { | ||
return certificate | ||
} | ||
} | ||
throw SecurityError.generalError | ||
} | ||
|
||
if CommandLine.arguments.count > 1 { | ||
let fileURL = URL(fileURLWithPath: CommandLine.arguments[1]) | ||
do { | ||
let certData = try Data(contentsOf: fileURL) | ||
let certificate = SecCertificateCreateWithData(nil, certData as CFData) | ||
if certificate != nil { | ||
print("Saving certificate") | ||
try? saveCertificateToKeyChain(certificate!, certificateLabel: "Test") | ||
} else { | ||
print("Certificate can't be read") | ||
} | ||
} catch { | ||
print("Unable to read the file \(CommandLine.arguments[1])") | ||
} | ||
} else { | ||
print("Usage: \(CommandLine.arguments[0]) [cert.file]") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters