-
Notifications
You must be signed in to change notification settings - Fork 12
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
13 changed files
with
655 additions
and
133 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
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,111 @@ | ||
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
import HomomorphicEncryption | ||
import PrivateInformationRetrieval | ||
import PrivateInformationRetrievalProtobuf | ||
import SwiftProtobuf | ||
|
||
extension PIRClient { | ||
mutating func fetchKeyStatus(for usecase: String) async throws | ||
-> Apple_SwiftHomomorphicEncryption_Api_Shared_V1_KeyStatus | ||
{ | ||
let configRequest = Apple_SwiftHomomorphicEncryption_Api_Pir_V1_ConfigRequest.with { configRequest in | ||
configRequest.usecases = [usecase] | ||
configRequest.existingConfigIds = [configCache[usecase]?.configurationId ?? Data()] | ||
} | ||
|
||
let configResponse: Apple_SwiftHomomorphicEncryption_Api_Pir_V1_ConfigResponse = try await post( | ||
path: "/config", | ||
body: configRequest) | ||
guard let config = configResponse.configs[usecase] else { | ||
throw PIRClientError.missingConfiguration | ||
} | ||
configCache[usecase] = Configuration(config: config.pirConfig, configurationId: config.configID) | ||
|
||
guard let keyStatus = configResponse.keyInfo.first else { | ||
throw PIRClientError.missingKeyStatus | ||
} | ||
return keyStatus | ||
} | ||
|
||
mutating func rotateKey(for usecase: String) async throws { | ||
let keyStatus = try await fetchKeyStatus(for: usecase) | ||
guard let config = configCache[usecase]?.config else { | ||
throw PIRClientError.missingConfiguration | ||
} | ||
|
||
if let storedSecretKey = secretKeys[config.evaluationKeyConfigHash], | ||
storedSecretKey.timestamp == keyStatus.timestamp | ||
{ | ||
// we do not need to rotate | ||
return | ||
} | ||
|
||
let context = try Context<PIRClient.Scheme>(encryptionParameters: config.encryptionParameters.native()) | ||
let secretKey = try context.generateSecretKey() | ||
let storedSecretKey = StoredSecretKey(secretKey: secretKey.serialize()) | ||
let evaluationKey = try context.generateEvaluationKey(config: keyStatus.keyConfig.native(), using: secretKey) | ||
secretKeys[config.evaluationKeyConfigHash] = storedSecretKey | ||
|
||
let evaluationKeyWithMetadata = Apple_SwiftHomomorphicEncryption_Api_Shared_V1_EvaluationKey.with { evalKey in | ||
evalKey.metadata = .with { metadata in | ||
metadata.timestamp = storedSecretKey.timestamp | ||
metadata.identifier = config.evaluationKeyConfigHash | ||
} | ||
evalKey.evaluationKey = evaluationKey.serialize().proto() | ||
} | ||
|
||
try await uploadKey(evaluationKeyWithMetadata) | ||
} | ||
|
||
mutating func uploadKey(_ key: Apple_SwiftHomomorphicEncryption_Api_Shared_V1_EvaluationKey) async throws { | ||
let keys = Apple_SwiftHomomorphicEncryption_Api_Shared_V1_EvaluationKeys.with { keyRequest in | ||
keyRequest.keys = [key] | ||
} | ||
|
||
let _: EmptyProtobufMessage = try await post(path: "/key", body: keys) | ||
} | ||
} | ||
|
||
/// Empty message | ||
private struct EmptyProtobufMessage: Message { | ||
static let protoMessageName = "Empty" | ||
|
||
var unknownFields = SwiftProtobuf.UnknownStorage() | ||
|
||
static func == (lhs: EmptyProtobufMessage, rhs: EmptyProtobufMessage) -> Bool { | ||
if lhs.unknownFields != rhs.unknownFields { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
mutating func decodeMessage(decoder: inout some SwiftProtobuf.Decoder) throws { | ||
// Load everything into unknown fields | ||
while try decoder.nextFieldNumber() != nil {} | ||
} | ||
|
||
func traverse(visitor: inout some SwiftProtobuf.Visitor) throws { | ||
try unknownFields.traverse(visitor: &visitor) | ||
} | ||
|
||
func isEqualTo(message: any SwiftProtobuf.Message) -> Bool { | ||
guard let other = message as? EmptyProtobufMessage else { | ||
return false | ||
} | ||
return self == other | ||
} | ||
} |
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,90 @@ | ||
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
import PrivacyPass | ||
|
||
extension PIRClient { | ||
func fetchTokenDirectory() async throws -> TokenIssuerDirectory { | ||
let response = try await connection.get( | ||
path: "/.well-known/private-token-issuer-directory", | ||
body: [], | ||
headers: [:]) | ||
|
||
let body = Data(buffer: response.body) | ||
guard response.status == .ok else { | ||
throw PIRClientError.failedToFetchTokenIssuerDirectory( | ||
status: response.status, | ||
message: String(data: body, encoding: .utf8) ?? "<\(body.count) bytes of binary response>") | ||
} | ||
return try JSONDecoder().decode(TokenIssuerDirectory.self, from: body) | ||
} | ||
|
||
func fetchPublicKeyForUserToken(authenticationToken: String) async throws -> PublicKey { | ||
let response = try await connection.get( | ||
path: "/token-key-for-user-token", | ||
body: [], | ||
headers: [.authorization: "Bearer \(authenticationToken)"]) | ||
|
||
let body = Array(buffer: response.body) | ||
guard response.status == .ok else { | ||
throw PIRClientError.failedToFetchTokenPublicKey( | ||
status: response.status, | ||
message: String(data: Data(body), encoding: .utf8) ?? "<\(body.count) bytes of binary response>") | ||
} | ||
|
||
return try PublicKey(fromSPKI: body) | ||
} | ||
|
||
mutating func fetchTokens(count: Int) async throws { | ||
guard let userToken else { | ||
throw PIRClientError.missingAuthenticationToken | ||
} | ||
|
||
let tokenIssuerDirectory = try await fetchTokenDirectory() | ||
let publicKey = try await fetchPublicKeyForUserToken(authenticationToken: userToken) | ||
|
||
guard try tokenIssuerDirectory.isValid(tokenKey: publicKey.spki()) else { | ||
throw PIRClientError.invalidTokenIssuerPublicKey | ||
} | ||
|
||
let connection = connection | ||
let challenge = try TokenChallenge(tokenType: TokenTypeBlindRSA, issuer: "test") | ||
|
||
try await withThrowingTaskGroup(of: Token.self) { group in | ||
for _ in 0..<count { | ||
group.addTask { | ||
let preparedRequest = try publicKey.request(challenge: challenge.bytes()) | ||
let response = try await connection.post( | ||
path: "/issue", | ||
body: preparedRequest.tokenRequest.bytes(), | ||
headers: [.authorization: "Bearer \(userToken)"]) | ||
let body = Array(buffer: response.body) | ||
guard response.status == .ok else { | ||
throw PIRClientError.failedToFetchToken( | ||
status: response.status, | ||
message: String(data: Data(body), encoding: .utf8) ?? | ||
"<\(body.count) bytes of binary response>") | ||
} | ||
let tokenResponse = try TokenResponse(from: body) | ||
return try preparedRequest.finalize(response: tokenResponse) | ||
} | ||
} | ||
|
||
for try await token in group { | ||
tokens.append(token) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.