V0.6.1 Arroyo Toad

Pushing Private Keys (Obviously these are test only)
This commit is contained in:
Christian Risi
2024-12-11 21:08:38 +00:00
parent d48ea94471
commit 5d78c9be58
929 changed files with 4644 additions and 23 deletions

View File

@@ -19,7 +19,7 @@ public func privateP256_2_pem(privateKey: P256.Signing.PrivateKey) -> String {
}
// UGLY: Refactor to make it easier to comprehend
public func fetchPrivateP256Key(deviceID: UInt128) async throws -> P256.Signing.PrivateKey {
public func fetchPrivateP256Key(deviceID: UInt) async throws -> P256.Signing.PrivateKey {
// UGLY: but fast
let privateKeyFolder = ProcessInfo.processInfo.environment["PRIVATE_KEY_FOLDER"] ?? "./Private/PrivateKeysP256"
@@ -29,21 +29,7 @@ public func fetchPrivateP256Key(deviceID: UInt128) async throws -> P256.Signing.
do {
let key = try pem2_P265_PrivateKey(filePath: keyFilePath)
// TODO: send public key to another server
let publicKey = key.publicKey.pemRepresentation
// UGLY: hardcoded
var httpRequest = URLRequest(url: URL(string: "http://publick-key-db.internal/key")!)
httpRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
httpRequest.httpMethod = "POST"
let message: [String : Encodable] = [
"deviceID": deviceID,
"publicKey": publicKey
]
let data = try JSONSerialization.data(withJSONObject: message)
httpRequest.httpBody = data
let _ = try await URLSession.shared.upload(for: httpRequest, from: data)
return key
} catch {
@@ -52,7 +38,23 @@ public func fetchPrivateP256Key(deviceID: UInt128) async throws -> P256.Signing.
do {
let key = createPrivateP256Key()
let publicKey = key.publicKey.pemRepresentation
try privateP256_2_pem(privateKey: key).write(to: URL(filePath: keyFilePath), atomically: true, encoding: String.Encoding.utf8)
// UGLY: hardcoded
var httpRequest = URLRequest(url: URL(string: "http://public-key-db.internal/key")!)
httpRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
httpRequest.httpMethod = "POST"
let message = PublicKeyMessage(deviceID: deviceID, publicKey: publicKey)
let encoder = JSONEncoder()
let data = try encoder.encode(message)
httpRequest.httpBody = data
let _ = try await URLSession.shared.upload(for: httpRequest, from: data)
return key
} catch {
throw ParsingError.ImpossibleToWriteKeyToFileSystem

View File

@@ -0,0 +1,4 @@
public struct PublicKeyMessage : Codable {
public let deviceID: UInt
public let publicKey: String
}

View File

@@ -103,7 +103,7 @@ private func json2edge_dev(_ json: [String: Any]) async throws -> [EdgeDevice] {
if let number = json["number"] as? UInt {
for _ in 0..<number {
let deviceID = try DeviceFactory.getUnusedID()
let privateKey = try await fetchPrivateP256Key(deviceID: deviceID)
let privateKey = try await fetchPrivateP256Key(deviceID: UInt(deviceID))
// TODO: Get ID from DeviceFactory and push ID inside
devices.append(