2020-03-24 06:22:22 +00:00
|
|
|
import SecretKit
|
|
|
|
import CryptoKit
|
|
|
|
|
|
|
|
struct Stub {}
|
|
|
|
|
|
|
|
extension Stub {
|
|
|
|
|
|
|
|
public class Store: SecretStore {
|
|
|
|
|
|
|
|
public let isAvailable = true
|
|
|
|
public let id = UUID()
|
|
|
|
public let name = "Stub"
|
|
|
|
public var secrets: [Secret] = []
|
|
|
|
public var shouldThrow = false
|
|
|
|
|
|
|
|
public init() {
|
|
|
|
// try! create(size: 256)
|
|
|
|
// try! create(size: 384)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func create(size: Int) throws {
|
|
|
|
let flags: SecAccessControlCreateFlags = []
|
|
|
|
let access =
|
|
|
|
SecAccessControlCreateWithFlags(kCFAllocatorDefault,
|
|
|
|
kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
|
|
|
flags,
|
|
|
|
nil) as Any
|
|
|
|
|
|
|
|
let attributes = [
|
|
|
|
kSecAttrLabel: name,
|
|
|
|
kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
|
|
|
|
kSecAttrKeySizeInBits: size,
|
|
|
|
kSecPrivateKeyAttrs: [
|
|
|
|
kSecAttrIsPermanent: true,
|
|
|
|
kSecAttrAccessControl: access
|
|
|
|
]
|
|
|
|
] as CFDictionary
|
|
|
|
|
|
|
|
var privateKey: SecKey! = nil
|
|
|
|
var publicKey: SecKey! = nil
|
|
|
|
SecKeyGeneratePair(attributes, &publicKey, &privateKey)
|
|
|
|
let publicAttributes = SecKeyCopyAttributes(publicKey) as! [CFString: Any]
|
|
|
|
let privateAttributes = SecKeyCopyAttributes(privateKey) as! [CFString: Any]
|
|
|
|
let publicData = (publicAttributes[kSecValueData] as! Data)
|
|
|
|
let privateData = (privateAttributes[kSecValueData] as! Data)
|
|
|
|
let secret = Secret(keySize: size, publicKey: publicData, privateKey: privateData)
|
|
|
|
print(secret)
|
|
|
|
print("Public Key OpenSSH: \(OpenSSHKeyWriter().openSSHString(secret: secret))")
|
|
|
|
}
|
|
|
|
|
2021-11-08 01:41:59 +00:00
|
|
|
public func sign(data: Data, with secret: Secret, for provenance: SigningRequestProvenance) throws -> SignedData {
|
2020-03-24 06:22:22 +00:00
|
|
|
guard !shouldThrow else {
|
2021-09-23 04:10:04 +00:00
|
|
|
throw NSError(domain: "test", code: 0, userInfo: nil)
|
2020-03-24 06:22:22 +00:00
|
|
|
}
|
|
|
|
let privateKey = SecKeyCreateWithData(secret.privateKey as CFData, [
|
|
|
|
kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
|
|
|
|
kSecAttrKeySizeInBits: secret.keySize,
|
|
|
|
kSecAttrKeyClass: kSecAttrKeyClassPrivate
|
|
|
|
] as CFDictionary
|
|
|
|
, nil)!
|
|
|
|
let signatureAlgorithm: SecKeyAlgorithm
|
|
|
|
switch secret.keySize {
|
|
|
|
case 256:
|
|
|
|
signatureAlgorithm = .ecdsaSignatureMessageX962SHA256
|
|
|
|
case 384:
|
|
|
|
signatureAlgorithm = .ecdsaSignatureMessageX962SHA384
|
|
|
|
default:
|
|
|
|
fatalError()
|
|
|
|
}
|
2021-11-08 01:41:59 +00:00
|
|
|
return SignedData(data: SecKeyCreateSignature(privateKey, signatureAlgorithm, data as CFData, nil)! as Data, requiredAuthentication: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func persistAuthentication(secret: Stub.Secret, forDuration duration: TimeInterval) throws {
|
2020-03-24 06:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Stub {
|
|
|
|
|
|
|
|
struct Secret: SecretKit.Secret, CustomDebugStringConvertible {
|
|
|
|
|
|
|
|
let id = UUID().uuidString.data(using: .utf8)!
|
|
|
|
let name = UUID().uuidString
|
|
|
|
let algorithm = Algorithm.ellipticCurve
|
|
|
|
|
|
|
|
let keySize: Int
|
|
|
|
let publicKey: Data
|
|
|
|
let privateKey: Data
|
|
|
|
|
|
|
|
init(keySize: Int, publicKey: Data, privateKey: Data) {
|
|
|
|
self.keySize = keySize
|
|
|
|
self.publicKey = publicKey
|
|
|
|
self.privateKey = privateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
var debugDescription: String {
|
|
|
|
"""
|
|
|
|
Key Size \(keySize)
|
|
|
|
Private: \(privateKey.base64EncodedString())
|
|
|
|
Public: \(publicKey.base64EncodedString())
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extension Stub.Store {
|
|
|
|
|
|
|
|
struct StubError: Error {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|