feat: generate se keypair

This commit is contained in:
2023-12-09 12:49:03 +08:00
parent 2034db589a
commit a8b2fc62b8
3 changed files with 73 additions and 133 deletions

View File

@@ -8,30 +8,9 @@ import LocalAuthentication
// https://www.andyibanez.com/posts/cryptokit-secure-enclave/
@_cdecl("is_support_secure_enclave")
func isSupportSecureEnclave() -> Bool {
// TODO pending delete
let epub = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE76jmqKrSs8tIVcvYYLpCA2za9GG7VxLdaI8FqynT+G65QgakCjT/P2ey7plz4KEl6ffORfZtZXO+lq2qQaaBHw=="
guard let ephemeralPublicKeyRepresentation = Data(
base64Encoded: epub
) else {
print("err:ephemeral public key base64 decode failed")
return false
}
do {
let a = try CryptoKit.P256.KeyAgreement.PublicKey.init(derRepresentation: ephemeralPublicKeyRepresentation)
print("\(a)")
} catch {
print("error: \(error)")
}
return SecureEnclave.isAvailable
}
// TODO delete print_greeting
@_cdecl("print_greeting")
func printGreeting(name: SRString) {
print("Hello \(name.toString())!")
}
@_cdecl("generate_secure_enclave_p256_keypair")
func generateSecureEnclaveP256KeyPair() -> SRString {
var error: Unmanaged<CFError>? = nil;
@@ -47,17 +26,15 @@ func generateSecureEnclaveP256KeyPair() -> SRString {
let privateKeyReference = try CryptoKit.SecureEnclave.P256.KeyAgreement.PrivateKey.init(
accessControl: accessCtrl
);
let dataRepresentation = privateKeyReference.dataRepresentation;
print("Private key reference: \(privateKeyReference)");
print("Private key reference - publicKey: \(privateKeyReference.publicKey)");
print("Private key reference - dataRepresentation: \(privateKeyReference.dataRepresentation)");
print("Private key reference - dataRepresentation: \(privateKeyReference.dataRepresentation.base64EncodedString())");
return SRString("")
let publicKeyBase64 = privateKeyReference.publicKey.x963Representation.base64EncodedString()
let dataRepresentationBase64 = privateKeyReference.dataRepresentation.base64EncodedString()
return SRString("ok:\(publicKeyBase64),\(dataRepresentationBase64)")
} catch {
return SRString("err:\(error)")
}
}
@_cdecl("compute_secure_enclave_p256_ecdh")
func computeSecureEnclaveP256Ecdh(privateKeyDataRepresentation: SRString, ephemeraPublicKey: SRString) -> SRString {
guard let dataRepresentation = Data(
base64Encoded: privateKeyDataRepresentation.toString()
@@ -87,96 +64,3 @@ func computeSecureEnclaveP256Ecdh(privateKeyDataRepresentation: SRString, epheme
return SRString("err:\(error)")
}
}
@_cdecl("get_file_thumbnail_base64")
func getFileThumbnailBase64(path: SRString) -> SRString {
let path = path.toString();
let image = NSWorkspace.shared.icon(forFile: path)
let bitmap = NSBitmapImageRep(data: image.tiffRepresentation!)!.representation(using: .png, properties: [:])!
return SRString(bitmap.base64EncodedString())
}
class Volume: NSObject {
var name: SRString
var path: SRString
var total_capacity: Int
var available_capacity: Int
var is_removable: Bool
var is_ejectable: Bool
var is_root_filesystem: Bool
public init(name: String, path: String, total_capacity: Int, available_capacity: Int, is_removable: Bool, is_ejectable: Bool, is_root_filesystem: Bool) {
self.name = SRString(name);
self.path = SRString(path);
self.total_capacity = total_capacity
self.available_capacity = available_capacity
self.is_removable = is_removable
self.is_ejectable = is_ejectable
self.is_root_filesystem = is_root_filesystem
}
}
@_cdecl("get_mounts")
func getMounts() -> SRObjectArray {
let keys: [URLResourceKey] = [
.volumeNameKey,
.volumeIsRemovableKey,
.volumeIsEjectableKey,
.volumeTotalCapacityKey,
.volumeAvailableCapacityKey,
.volumeIsRootFileSystemKey,
]
let paths = autoreleasepool {
FileManager().mountedVolumeURLs(includingResourceValuesForKeys: keys, options: [])
}
var validMounts: [Volume] = []
if let urls = paths {
autoreleasepool {
for url in urls {
let components = url.pathComponents
if components.count == 1 || components.count > 1
&& components[1] == "Volumes"
{
let metadata = try? url.promisedItemResourceValues(forKeys: Set(keys))
let volume = Volume(
name: metadata?.volumeName ?? "",
path: url.path,
total_capacity: metadata?.volumeTotalCapacity ?? 0,
available_capacity: metadata?.volumeAvailableCapacity ?? 0,
is_removable: metadata?.volumeIsRemovable ?? false,
is_ejectable: metadata?.volumeIsEjectable ?? false,
is_root_filesystem: metadata?.volumeIsRootFileSystem ?? false
)
validMounts.append(volume)
}
}
}
}
return SRObjectArray(validMounts)
}
class Test: NSObject {
var null: Bool
public init(_ null: Bool)
{
self.null = null;
}
}
@_cdecl("return_nullable")
func returnNullable(null: Bool) -> Test? {
if (null == true) { return nil }
return Test(null)
}