Sesame-iOS/Sesame-Watch Watch App/KeyManagement.swift
2023-08-07 15:57:09 +02:00

122 lines
2.7 KiB
Swift

import Foundation
import CryptoKit
import SwiftUI
private let localKey: [UInt8] = [
0x98, 0x36, 0x91, 0x09, 0x29, 0xa0, 0x54, 0x44,
0x03, 0x0c, 0xa5, 0xb4, 0x20, 0x16, 0x10, 0x0d,
0xaf, 0x41, 0x9b, 0x26, 0x4f, 0x75, 0xa4, 0x61,
0xed, 0x15, 0x0c, 0xb3, 0x06, 0x39, 0x92, 0x59]
private let remoteKey: [UInt8] = [
0xfa, 0x23, 0xf6, 0x98, 0xea, 0x87, 0x23, 0xa0,
0xa0, 0xbe, 0x9a, 0xdb, 0x31, 0x28, 0xcb, 0x7d,
0xd3, 0xa5, 0x7b, 0xf0, 0xc0, 0xeb, 0x45, 0x65,
0x4d, 0x94, 0x50, 0x1a, 0x2f, 0x6f, 0xeb, 0x70]
private let authToken: [UInt8] = {
let s = "Y6QzDK5DaFK1w2oEX5OkzoC0nTqP8w5IxpvWAR1mpro="
let t = Data(base64Encoded: s.data(using: .utf8)!)!
return Array(t)
}()
extension KeyManagement {
enum KeyType: String, Identifiable, CaseIterable {
case deviceKey = "sesame-device"
case remoteKey = "sesame-remote"
case authToken = "sesame-remote-auth"
var id: String {
rawValue
}
var displayName: String {
switch self {
case .deviceKey:
return "Device Key"
case .remoteKey:
return "Remote Key"
case .authToken:
return "Authentication Token"
}
}
var keyLength: SymmetricKeySize {
.bits256
}
var usesHashing: Bool {
switch self {
case .authToken:
return true
default:
return false
}
}
}
}
extension KeyManagement.KeyType: CustomStringConvertible {
var description: String {
displayName
}
}
final class KeyManagement: ObservableObject {
@Published
private(set) var hasRemoteKey = true
@Published
private(set) var hasDeviceKey = true
@Published
private(set) var hasAuthToken = true
var hasAllKeys: Bool {
hasRemoteKey && hasDeviceKey && hasAuthToken
}
init() {}
func has(_ type: KeyType) -> Bool {
switch type {
case .deviceKey:
return hasDeviceKey
case .remoteKey:
return hasRemoteKey
case .authToken:
return hasAuthToken
}
}
func get(_ type: KeyType) -> SymmetricKey? {
let bytes: [UInt8] = get(type)
return SymmetricKey(data: bytes)
}
private func get(_ type: KeyType) -> [UInt8] {
switch type {
case .deviceKey:
return remoteKey
case .remoteKey:
return localKey
case .authToken:
return authToken
}
}
func delete(_ type: KeyType) {
}
func generate(_ type: KeyType) {
}
}