-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPolluxImpl+CredentialVerification.swift
303 lines (269 loc) · 11.5 KB
/
PolluxImpl+CredentialVerification.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import AnoncredsSwift
import Core
import Domain
import Foundation
import JSONWebAlgorithms
import JSONWebToken
import JSONWebSignature
import Sextant
import JSONSchema
extension PolluxImpl {
private enum ValidJsonTypeFilter: String {
case string
case number
case boolean
}
public func verifyPresentation(message: Message, options: [CredentialOperationsOptions]) async throws -> Bool {
guard
let attachment = message.attachments.first,
let requestId = message.thid
else {
throw PolluxError.couldNotFindPresentationInAttachments
}
let jsonData: Data
switch attachment.data {
case let attchedData as AttachmentBase64:
guard let decoded = Data(fromBase64URL: attchedData.base64) else {
throw CommonError.invalidCoding(message: "Invalid base64 url attachment")
}
jsonData = decoded
case let attchedData as AttachmentJsonData:
jsonData = attchedData.data
default:
throw PolluxError.invalidAttachmentType(supportedTypes: ["Json", "Base64"])
}
switch attachment.format {
case "dif/presentation-exchange/[email protected]":
return try await verifyPresentationSubmission(json: jsonData, requestId: requestId)
case "anoncreds/[email protected]":
return try await verifyAnoncreds(
presentation: jsonData,
requestId: requestId,
options: options
)
default:
throw PolluxError.unsupportedAttachmentFormat(attachment.format)
}
}
private func verifyPresentationSubmission(json: Data, requestId: String) async throws -> Bool {
let presentationContainer = try JSONDecoder.didComm().decode(PresentationContainer.self, from: json)
let presentationRequest = try await getDefinition(id: requestId)
guard let submission = presentationContainer.presentationSubmission else {
throw PolluxError.presentationSubmissionNotAvailable
}
let credentials = try getCredentialJWT(submission: submission, presentationData: json)
try VerifyPresentationSubmission.verifyPresentationSubmissionClaims(
request: presentationRequest.presentationDefinition,
credentials: try credentials.map {
try JWT.getPayload(jwtString: $0)
}
)
try await verifyJWTs(credentials: credentials)
return true
}
private func getCredentialJWT(submission: PresentationSubmission, presentationData: Data) throws -> [String] {
return submission.descriptorMap
.filter({ $0.format == "jwt" || $0.format == "jwt_vc" || $0.format == "jwt_vp" })
.compactMap { try? processJWTPath(descriptor: $0, presentationData: presentationData) }
}
private func processJWTPath(descriptor: PresentationSubmission.Descriptor, presentationData: Data) throws -> String {
guard descriptor.format == "jwt" || descriptor.format == "jwt_vc" || descriptor.format == "jwt_vp" else {
throw UnknownError.somethingWentWrongError(
customMessage: "This should not happen since its filtered before",
underlyingErrors: nil
)
}
guard
let jwts = presentationData.query(string: descriptor.path)
else {
throw PolluxError.credentialPathInvalid(path: descriptor.path)
}
guard let nestedDescriptor = descriptor.pathNested else {
return jwts
}
let nestedPayload: Data = try JWT.getPayload(jwtString: jwts)
return try processJWTPath(descriptor: nestedDescriptor, presentationData: nestedPayload)
}
private func verifyJWTs(credentials: [String]) async throws {
var errors = [Error]()
await credentials
.asyncForEach {
do {
guard try await verifyJWT(jwtString: $0) else {
errors.append(PolluxError.invalidSignature())
return
}
} catch {
errors.append(error)
}
}
guard errors.isEmpty else {
throw PolluxError.invalidSignature(internalErrors: errors)
}
}
private func verifyJWT(jwtString: String) async throws -> Bool {
try await verifyJWTCredentialRevocation(jwtString: jwtString)
let payload: DefaultJWTClaimsImpl = try JWT.getPayload(jwtString: jwtString)
guard let issuer = payload.iss else {
throw PolluxError.requiresThatIssuerExistsAndIsAPrismDID
}
let issuerDID = try DID(string: issuer)
let issuerKeys = try await castor.getDIDPublicKeys(did: issuerDID)
ES256KVerifier.bouncyCastleFailSafe = true
let validations = issuerKeys
.compactMap(\.exporting)
.compactMap {
try? JWT.verify(jwtString: jwtString, senderKey: $0.jwk.toJoseJWK())
}
ES256KVerifier.bouncyCastleFailSafe = false
return !validations.isEmpty
}
private func verifyJWTCredentialRevocation(jwtString: String) async throws {
guard let credential = try? JWTCredential(data: jwtString.tryToData()) else {
return
}
let isRevoked = try await credential.isRevoked
let isSuspended = try await credential.isSuspended
guard !isRevoked else {
throw PolluxError.credentialIsRevoked(jwtString: jwtString)
}
guard !isSuspended else {
throw PolluxError.credentialIsSuspended(jwtString: jwtString)
}
}
private func getDefinition(id: String) async throws -> PresentationExchangeRequest {
guard
let request = try await pluto.getMessage(id: id).first().await(),
let attachmentData = request.attachments.first?.data
else {
throw PolluxError.couldNotFindPresentationRequest(id: id)
}
let json: Data
switch attachmentData {
case let jsonData as AttachmentJsonData:
json = jsonData.data
case let base64Data as AttachmentBase64:
json = try base64Data.decoded()
default:
throw PolluxError.invalidAttachmentType(supportedTypes: ["base64", "json"])
}
return try JSONDecoder.didComm().decode(PresentationExchangeRequest.self, from: json)
}
private func verifyAnoncreds(
presentation: Data,
requestId: String,
options: [CredentialOperationsOptions]
) async throws -> Bool {
let anonPresentation = try AnoncredsSwift.Presentation(jsonString: presentation.tryToString())
let requestPresentation = try await getAnoncredsRequest(id: requestId)
let internalPresentation = try JSONDecoder.didComm().decode(AnonPresentation.self, from: presentation)
let schemaIds = internalPresentation.identifiers.map(\.schemaId)
let credentialDefinitionsIds = internalPresentation.identifiers.map(\.credDefId)
let anonSchemas = try await getSchemas(ids: schemaIds, options: options)
.mapValues { try JSONDecoder.didComm().decode(AnonCredentialSchema.self, from: $0) }
.mapValues {
Schema(
name: $0.name,
version: $0.version,
attrNames: $0.attrNames.map { AttributeNames($0) } ?? AttributeNames(),
issuerId: $0.issuerId
)
}
let anonCredentialDefinitions = try await getCredentialDefinitions(
ids: credentialDefinitionsIds,
options: options
).mapValues { try AnoncredsSwift.CredentialDefinition(jsonString: $0.tryToString()) }
return try Verifier().verifyPresentation(
presentation: anonPresentation,
presentationRequest: requestPresentation,
schemas: anonSchemas,
credentialDefinitions: anonCredentialDefinitions
)
}
private func getAnoncredsRequest(id: String) async throws -> AnoncredsSwift.PresentationRequest {
guard
let request = try await pluto.getMessage(id: id).first().await(),
let attachmentData = request.attachments.first?.data
else {
throw PolluxError.couldNotFindPresentationRequest(id: id)
}
let json: Data
switch attachmentData {
case let jsonData as AttachmentJsonData:
json = jsonData.data
case let base64Data as AttachmentBase64:
guard let data = try Data(fromBase64URL: base64Data.base64.tryToData()) else {
throw CommonError.invalidCoding(message: "Could not decode base64 message attchment")
}
json = data
default:
throw PolluxError.invalidAttachmentType(supportedTypes: [])
}
return try PresentationRequest(jsonString: json.tryToString())
}
private func getSchemas(ids: [String], options: [CredentialOperationsOptions]) async throws -> [String: Data] {
if
let schemasOption = options.first(where: {
if case .schema = $0 { return true }
return false
}),
case let CredentialOperationsOptions.schema(id, json) = schemasOption
{
return try [id: json.tryToData()]
}
guard
let schemaDownloaderOption = options.first(where: {
if case .schemaDownloader = $0 { return true }
return false
}),
case let CredentialOperationsOptions.schemaDownloader(downloader) = schemaDownloaderOption
else {
throw PolluxError.missingAndIsRequiredForOperation(type: "schemaDownloader")
}
let schemas = try await ids.asyncMap { ($0, try await downloader.downloadFromEndpoint(urlOrDID: $0)) }
return schemas.reduce( [String: Data]()) { partialResult, schemas in
var dic = partialResult
dic[schemas.0] = schemas.1
return dic
}
}
private func getCredentialDefinitions(
ids: [String],
options: [CredentialOperationsOptions]
) async throws -> [String: Data] {
if
let credentialDefinitionsOption = options.first(where: {
if case .credentialDefinition = $0 { return true }
return false
}),
case let CredentialOperationsOptions.credentialDefinition(id, json) = credentialDefinitionsOption
{
return try [id: json.tryToData()]
}
guard
let credentialDefinitionsDownloaderOption = options.first(where: {
if case .credentialDefinitionDownloader = $0 { return true }
return false
}),
case let CredentialOperationsOptions.credentialDefinitionDownloader(downloader) = credentialDefinitionsDownloaderOption
else {
throw PolluxError.missingAndIsRequiredForOperation(type: "credentialDefinitionDownloader")
}
let definitions = try await ids.asyncMap { ($0, try await downloader.downloadFromEndpoint(urlOrDID: $0)) }
return definitions.reduce( [String: Data]()) { partialResult, definitions in
var dic = partialResult
dic[definitions.0] = definitions.1
return dic
}
}
}
private struct AnonPresentation: Codable {
struct Identifiers: Codable {
let schemaId: String
let credDefId: String
}
let identifiers: [Identifiers]
}
private struct ValidateJsonSchemaContainer: Codable {
let value: AnyCodable
}