-
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.
- Loading branch information
Showing
10 changed files
with
604 additions
and
51 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// CertHelper.swift | ||
// DotLocal | ||
// | ||
// Created by Suphon Thanakornpakapong on 21/1/2567 BE. | ||
// | ||
|
||
import Foundation | ||
import SecurityInterface | ||
import SwiftProtobuf | ||
|
||
struct CertHelper { | ||
static func getRootCertificate() async throws -> CertHelper.Certificate { | ||
let res = try await DaemonManager.shared.apiClient.getRootCertificate(Google_Protobuf_Empty()) | ||
return try await CertHelper.Certificate(res: res) | ||
} | ||
|
||
static func rootCertificateLogo() -> NSImage { | ||
let bundle = Bundle(for: SFCertificateView.self) | ||
return bundle.image(forResource: "CertLargeRoot")! | ||
} | ||
|
||
struct Certificate { | ||
let secCertificate: SecCertificate | ||
let commonName: String | ||
let notBefore: Date | ||
let notAfter: Date | ||
let trusted: Bool | ||
|
||
init(res: GetRootCertificateResponse) async throws { | ||
guard let certificate = SecCertificateCreateWithData(nil, res.certificate as CFData) else { | ||
throw CertHelperError.invalidCertificate | ||
} | ||
secCertificate = certificate | ||
let tmp = UnsafeMutablePointer<CFString?>.allocate(capacity: 1) | ||
SecCertificateCopyCommonName(certificate, tmp) | ||
commonName = tmp.pointee! as String | ||
notBefore = res.notBefore.date | ||
notAfter = res.notAfter.date | ||
trusted = try await evaluateTrust(certificate: secCertificate) | ||
} | ||
} | ||
} | ||
|
||
enum CertHelperError: Error { | ||
case invalidCertificate | ||
} | ||
|
||
fileprivate func evaluateTrust(certificate: SecCertificate) async throws -> Bool { | ||
var secTrust: SecTrust? | ||
if SecTrustCreateWithCertificates(certificate, SecPolicyCreateBasicX509(), &secTrust) == errSecSuccess, let trust = secTrust { | ||
let error = UnsafeMutablePointer<CFError?>.allocate(capacity: 1) | ||
let result = SecTrustEvaluateWithError(trust, error) | ||
if error.pointee != nil { | ||
return false | ||
} | ||
return result | ||
} else { | ||
return false | ||
} | ||
} |
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
Oops, something went wrong.