-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathApp.java
More file actions
587 lines (507 loc) · 25.3 KB
/
App.java
File metadata and controls
587 lines (507 loc) · 25.3 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package com.yubicolabs;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Closeables;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.yubico.fido.metadata.AAGUID;
import com.yubico.fido.metadata.AAID;
import com.yubico.fido.metadata.AttachmentHint;
import com.yubico.fido.metadata.AuthenticatorGetInfo;
import com.yubico.fido.metadata.FidoMetadataDownloader;
import com.yubico.fido.metadata.FidoMetadataService;
import com.yubico.fido.metadata.MetadataBLOB;
import com.yubico.internal.util.JacksonCodecs;
import com.yubico.webauthn.AssertionResult;
import com.yubico.webauthn.FinishAssertionOptions;
import com.yubico.webauthn.FinishRegistrationOptions;
import com.yubico.webauthn.RegisteredCredential;
import com.yubico.webauthn.RegistrationResult;
import com.yubico.webauthn.RelyingParty;
import com.yubico.webauthn.StartAssertionOptions;
import com.yubico.webauthn.StartRegistrationOptions;
import com.yubico.webauthn.data.AttestationConveyancePreference;
import com.yubico.webauthn.data.AuthenticatorSelectionCriteria;
import com.yubico.webauthn.data.AuthenticatorTransport;
import com.yubico.webauthn.data.ByteArray;
import com.yubico.webauthn.data.PublicKeyCredentialDescriptor;
import com.yubico.webauthn.data.ResidentKeyRequirement;
import com.yubico.webauthn.data.UserIdentity;
import com.yubico.webauthn.data.UserVerificationRequirement;
import com.yubico.webauthn.data.exception.Base64UrlException;
import com.yubico.webauthn.exception.AssertionFailedException;
import com.yubico.webauthn.exception.RegistrationFailedException;
import com.yubicolabs.data.AssertionRequestWrapper;
import com.yubicolabs.data.AssertionResponse;
import com.yubicolabs.data.AttestationRegistration;
import com.yubico.webauthn.data.AuthenticatorAttachment;
import com.yubicolabs.data.CredentialRegistration;
import com.yubicolabs.data.RegistrationRequest;
import com.yubicolabs.data.RegistrationResponse;
import org.checkerframework.checker.nullness.Opt;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.cert.PKIXRevocationChecker.Option;
import java.io.IOException;
import java.io.InputStream;
import java.security.SecureRandom;
import java.time.Clock;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.yubico.fido.metadata.MetadataBLOBPayloadEntry;
import com.yubico.fido.metadata.MetadataStatement;
/**
* Lambda function entry point. You can change to use other pojo type or
* implement
* a different RequestHandler.
*
* @see <a
* href=https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html>Lambda
* Java Handler</a> for more information
*/
@Slf4j
public class App implements RequestHandler<Object, Object> {
private static final SecureRandom random = new SecureRandom();
private final Clock clock = Clock.systemDefaultZone();
// RVW: This is in package com.yubico.internal, so should be eliminated. Can we
// use gson instead?
private final ObjectMapper jsonMapper = JacksonCodecs.json();
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final AssertionRequestStorage assertRequestStorage = new AssertionRequestStorage();
private final RegistrationRequestStorage registerRequestStorage = new RegistrationRequestStorage();
private final RegistrationStorage userStorage = new RDSRegistrationStorage();
private static final String METADATA_PATH = "/metadata.json";
private final FidoMetadataService mds = initMDS();
private FidoMetadataService initMDS() {
try {
MetadataBLOB downloader = FidoMetadataDownloader.builder()
.expectLegalHeader(
"Retrieval and use of this BLOB indicates acceptance of the appropriate agreement located at https://fidoalliance.org/metadata/metadata-legal-terms/")
.useDefaultTrustRoot()
.useTrustRootCacheFile(new File("/tmp/fido-mds-trust-root-cache.bin"))
.useDefaultBlob()
.useBlobCacheFile(new File("/tmp/fido-mds-blob-cache.bin"))
.build()
.loadCachedBlob();
FidoMetadataService mds = FidoMetadataService.builder()
.useBlob(downloader)
.build();
return mds;
} catch (Exception e) {
log.info("Error initializing MDS: {}", gson.toJson(e));
return null;
}
}
private final RelyingParty rp = RelyingParty.builder()
.identity(Config.getRpIdentity())
.credentialRepository(this.userStorage)
.origins(Config.getOrigins())
.attestationConveyancePreference(Optional.of(AttestationConveyancePreference.DIRECT))
.attestationTrustSource(mds)
.allowUntrustedAttestation(true)
.validateSignatureCounter(true)
.build();
public App() {
jsonMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
@Override
public Object handleRequest(final Object input, final Context context) {
// Note: This is likely to contain secrets (like database credentials) in
// downstream apps
// log.info("ENVIRONMENT VARIABLES: {}", gson.toJson(System.getenv()));
log.info("CONTEXT: {}", gson.toJson(context));
log.info("EVENT: {}", gson.toJson(input));
final String type;
final JsonObject object;
try {
log.debug("handleRequest() input: {}", input.toString());
object = gson.fromJson(input.toString(), JsonObject.class);
type = object.get("type").getAsString();
} catch (JsonSyntaxException e) {
log.error("JSON error in finishRegistration; input: {}", input, e);
return e;
}
log.debug("type: {}", type);
switch (type) {
case "startRegistration":
return startRegistration(object);
case "finishRegistration":
return finishRegistration(object);
case "startAuthentication":
return startAuthentication(object);
case "finishAuthentication":
return finishAuthentication(object);
case "getCredentialIdsForUsername":
return getCredentialIdsForUsername(object);
case "getRegistrationsByUsername":
return getRegistrationsByUsername(object);
case "updateCredentialNickname":
return updateCredentialNickname(object);
case "removeRegistrationByUsername":
return removeRegistrationByUsername(object);
case "removeAllRegistrations":
return removeAllRegistrations(object);
default:
return input;
}
}
Object startRegistration(JsonObject jsonRequest) {
String username = jsonRequest.get("username").getAsString();
String displayName = jsonRequest.get("displayName").getAsString();
boolean requireResidentKey = jsonRequest.get("requireResidentKey").getAsBoolean();
AuthenticatorAttachment requireAuthenticatorAttachment = (jsonRequest.has("requireAuthenticatorAttachment"))
? (resolveAuthenticatorAttachment(jsonRequest.get("requireAuthenticatorAttachment").getAsString()))
: null;
String uid = jsonRequest.get("uid").getAsString();
log.trace(
"startRegistration username: {}, displayName: {}, requireResidentKey: {}, uid {}",
username, displayName, requireResidentKey, uid);
ByteArray id;
try {
id = ByteArray.fromBase64Url(uid);
} catch (Base64UrlException e) {
log.error("ByteArray.fromBase64Url exception", e);
return e;
}
final Collection<CredentialRegistration> registrations = userStorage.getRegistrationsByUsername(username);
final Optional<UserIdentity> existingUser = registrations.stream().findAny()
.map(CredentialRegistration::getUserIdentity);
final UserIdentity registrationUserId = existingUser.orElseGet(() -> UserIdentity.builder()
.name(username)
.displayName(displayName)
.id(id)
.build());
RegistrationRequest request = new RegistrationRequest(
"startRegistration",
username,
displayName,
"New Credential",
requireResidentKey,
generateRandom(32),
rp.startRegistration(
StartRegistrationOptions.builder()
.user(registrationUserId)
.authenticatorSelection(AuthenticatorSelectionCriteria.builder()
.residentKey(ResidentKeyRequirement.PREFERRED)
.authenticatorAttachment(
requireAuthenticatorAttachment != null
? requireAuthenticatorAttachment
: null)
.userVerification(UserVerificationRequirement.PREFERRED)
.build())
.build()));
log.debug("request: {}", request);
registerRequestStorage.put(request.getRequestId(), request);
String registerRequestJson = gson.toJson(request, RegistrationRequest.class);
log.debug("registerRequestJson: {}", registerRequestJson);
return registerRequestJson;
}
AuthenticatorAttachment resolveAuthenticatorAttachment(String value) {
if (value.equals("PLATFORM")) {
return AuthenticatorAttachment.PLATFORM;
} else if (value.equals("CROSS_PLATFORM")) {
return AuthenticatorAttachment.CROSS_PLATFORM;
}
return null;
}
Object finishRegistration(JsonObject responseJson) {
log.debug("finishRegistration responseJson: {}", responseJson);
RegistrationResponse response;
try {
response = jsonMapper.readValue(responseJson.toString(), RegistrationResponse.class);
} catch (Exception e) {
log.error("JSON error in finishRegistration. Failed to decode response object.", e);
return e;
}
log.debug("response: {}", response);
RegistrationRequest request = registerRequestStorage.getIfPresent(response.getRequestId());
log.debug("request: {}", request);
registerRequestStorage.invalidate(response.getRequestId());
if (request == null) {
String msg = "fail finishRegistration - no such registration in progress: {}" + response.getRequestId();
log.error(msg);
return new Exception(msg);
} else {
try {
com.yubico.webauthn.RegistrationResult registration = rp.finishRegistration(
FinishRegistrationOptions.builder()
.request(request.getPublicKeyCredentialCreationOptions())
.response(response.getCredential())
.build());
log.debug("registration: {}", registration);
return addRegistration(
request.getPublicKeyCredentialCreationOptions().getUser(),
response,
registration,
request);
} catch (RegistrationFailedException e) {
log.error("Registration failed!", e);
return e;
} catch (Exception e) {
log.error("Registration failed unexpectedly; this is likely a bug.", e);
return e;
}
}
}
Object startAuthentication(JsonObject jsonRequest) {
JsonElement jsonElement = jsonRequest.get("username");
Optional<String> username = Optional.ofNullable(jsonElement).map(JsonElement::getAsString);
log.debug("startAuthentication username: {}", username);
if (username.isPresent() && !userStorage.userExists(username.get())) {
String msg = "The username \"" + username + "\" is not registered.";
return new Exception(msg);
} else {
AssertionRequestWrapper request = new AssertionRequestWrapper(
generateRandom(32),
rp.startAssertion(
StartAssertionOptions.builder()
.username(username)
.userVerification(UserVerificationRequirement.PREFERRED)
.build()));
log.debug("request: {}", request);
assertRequestStorage.put(request.getRequestId(), request);
String authRequestJson = gson.toJson(request, AssertionRequestWrapper.class);
log.debug("authRequestJson: {}", authRequestJson);
return authRequestJson;
}
}
Object finishAuthentication(JsonObject responseJson) {
log.debug("finishAuthentication responseJson: {}", responseJson);
final AssertionResponse response;
try {
response = jsonMapper.readValue(responseJson.toString(), AssertionResponse.class);
} catch (Exception e) {
log.error("Assertion failed! Failed to decode response object", e);
return e;
}
log.debug("finishAuthentication response: {}", response);
AssertionRequestWrapper request = assertRequestStorage.getIfPresent(response.getRequestId());
log.debug("finishAuthentication request: {}", request);
assertRequestStorage.invalidate(response.getRequestId());
if (request == null) {
String msg = "Assertion failed!" + "No such assertion in progress: " + response.getRequestId();
log.error(msg);
return new Exception(msg);
} else {
try {
FinishAssertionOptions finishAssertionOptions = FinishAssertionOptions.builder()
.request(request.getRequest())
.response(response.getCredential())
.build();
log.debug("finishAuthentication finishAssertionOptions: {}", finishAssertionOptions);
AssertionResult result = rp.finishAssertion(
FinishAssertionOptions.builder()
.request(request.getRequest())
.response(response.getCredential())
.build());
if (result.isSuccess()) {
try {
userStorage.updateSignatureCount(result);
} catch (Exception e) {
log.error(
"Failed to update signature count for user \"{}\", credential \"{}\"",
result.getUsername(),
response.getCredential().getId(),
e);
}
log.debug("result: {}", result);
return result;
} else {
String msg = "Assertion failed: Invalid assertion.";
log.error(msg);
return new Exception(msg);
}
} catch (AssertionFailedException e) {
log.debug("Assertion failed", e);
return e;
} catch (Exception e) {
log.error("Assertion failed unexpectedly; this is likely a bug.", e);
return e;
}
}
}
Object getCredentialIdsForUsername(JsonObject jsonRequest) {
String username = jsonRequest.get("username").getAsString();
log.trace("getCredentialIdsForUsername username: {}", username);
Collection<PublicKeyCredentialDescriptor> credentials = userStorage.getCredentialIdsForUsername(username);
log.debug("credentials: {}", credentials);
String credentialsRequestJson = gson.toJson(credentials, Collection.class);
log.debug("credentialsRequestJson: {}", credentialsRequestJson);
return credentialsRequestJson;
}
Object getRegistrationsByUsername(JsonObject jsonRequest) {
String username = jsonRequest.get("username").getAsString();
log.trace("getRegistrationsByUsername username: {}", username);
Collection<CredentialRegistration> credentials = userStorage.getRegistrationsByUsername(username);
log.debug("credentials: {}", credentials);
String credentialsRequestJson = gson.toJson(credentials, Collection.class);
log.debug("credentialsRequestJson: {}", credentialsRequestJson);
return credentialsRequestJson;
}
Object updateCredentialNickname(JsonObject jsonRequest) {
String username = jsonRequest.get("username").getAsString();
String credentialId = jsonRequest.get("credentialId").getAsString();
String nickname = jsonRequest.get("nickname").getAsString();
log.debug("updateCredentialNickname username: {}, credentialId: {} nickname: {}", username, credentialId,
nickname);
try {
ByteArray id = ByteArray.fromBase64Url(credentialId);
userStorage.updateCredentialNickname(username, id, nickname);
return true;
} catch (Exception e) {
log.error("updateCredentialNickname error", e);
return e;
}
}
Object removeRegistrationByUsername(JsonObject jsonRequest) {
String username = jsonRequest.get("username").getAsString();
String credentialId = jsonRequest.get("credentialId").getAsString();
log.trace("removeRegistrationByUsername username: {}", username);
try {
ByteArray id = ByteArray.fromBase64Url(credentialId);
return userStorage.getRegistrationByUsernameAndCredentialId(username, id)
.map(registration -> userStorage.removeRegistrationByUsername(username, registration))
.orElse(false);
} catch (Exception e) {
log.error("removeRegistrationByUsername error", e);
return e;
}
}
Object removeAllRegistrations(JsonObject jsonRequest) {
String username = jsonRequest.get("username").getAsString();
log.trace("removeAllRegistrations username: {}", username);
return userStorage.removeAllRegistrations(username);
}
private static ByteArray generateRandom(int length) {
byte[] bytes = new byte[length];
random.nextBytes(bytes);
return new ByteArray(bytes);
}
private CredentialRegistration addRegistration(
UserIdentity userIdentity,
RegistrationResponse response,
RegistrationResult result,
RegistrationRequest request) {
Optional<AttestationRegistration> attestationMetadata = buildAttestationResult(result);
Optional<String> nickname = Optional.empty();
if (attestationMetadata.isPresent()) {
if (attestationMetadata.get().description != null) {
nickname = Optional.ofNullable(attestationMetadata.get().description);
}
}
if (!nickname.isPresent()) {
log.debug("addRegistration Evaluate AuthSelection: No attestation found");
Optional<AuthenticatorSelectionCriteria> evaluate = request.publicKeyCredentialCreationOptions
.getAuthenticatorSelection();
log.debug("addRegistration Evaluate AuthSelection publicKeyCreate: {}", gson.toJson(evaluate));
if (evaluate.isPresent() && evaluate.get().getAuthenticatorAttachment().isPresent()) {
log.debug("addRegistration Evaluate AuthSelection found, checking authattachment value2: {}",
gson.toJson(evaluate.get().getAuthenticatorAttachment().get()));
if (evaluate.get().getAuthenticatorAttachment().get() == AuthenticatorAttachment.PLATFORM) {
nickname = Optional.ofNullable("My Trusted Device");
}
}
}
if (!nickname.isPresent()) {
nickname = Optional.ofNullable("My Security Key");
}
return addRegistration(
userIdentity,
nickname,
response.getCredential().getResponse().getAttestation().getAuthenticatorData().getSignatureCounter(),
RegisteredCredential.builder()
.credentialId(result.getKeyId().getId())
.userHandle(userIdentity.getId())
.publicKeyCose(result.getPublicKeyCose())
.signatureCount(response.getCredential().getResponse().getParsedAuthenticatorData()
.getSignatureCounter())
.build(),
attestationMetadata,
request);
}
private CredentialRegistration addRegistration(
UserIdentity userIdentity,
Optional<String> nickname,
long signatureCount,
RegisteredCredential credential,
Optional<AttestationRegistration> attestationMetadata,
RegistrationRequest request) {
CredentialRegistration reg = CredentialRegistration.builder()
.userIdentity(userIdentity)
.credentialNickname(nickname)
.registrationTime(clock.instant())
.lastUsedTime(clock.instant())
.lastUpdatedTime(clock.instant())
.credential(credential)
.signatureCount(signatureCount)
.registrationRequest(request)
.attestationMetadata(attestationMetadata)
.build();
log.debug(
"Adding registration: user: {}, nickname: {}, credential: {}",
userIdentity,
nickname,
credential);
userStorage.addRegistrationByUsername(userIdentity.getName(), reg);
return reg;
}
private Optional<AttestationRegistration> buildAttestationResult(RegistrationResult result) {
log.debug("buildAttestationResult() result aaguid: {}", result.getAaguid().getHex());
// Find MDS entries based on both the AAGUID and TrustRootCert provided during
// Attestation
Set<MetadataBLOBPayloadEntry> entries = mds.findEntries(result);
log.debug("buildAttestationResult() number of entries found in entries: {}", entries.size());
log.debug("buildAttestationResult() entries found in entries: {}", gson.toJson(entries));
List<MetadataBLOBPayloadEntry> entriesAaguid = entries.stream()
.filter(ent -> ent.getAaguid().isPresent()
&& ent.getAaguid().get().asHexString().equals(result.getAaguid().getHex()))
.collect(Collectors.toList());
log.debug("buildAttestationResult() entries found in filter for AAGUID: {}", entriesAaguid.size());
Optional<MetadataStatement> entriesFinal;
if (entriesAaguid.size() == 0) {
entriesFinal = entries.stream().findAny().flatMap(MetadataBLOBPayloadEntry::getMetadataStatement);
} else {
entriesFinal = entriesAaguid.get(0).getMetadataStatement();
}
AttestationRegistration attResult = null;
if (entriesFinal.isPresent()) {
MetadataStatement entryValue = entriesFinal.get();
Optional<AAGUID> aaguid = entryValue.getAaguid();
Optional<AAID> aaid = entryValue.getAaid();
Optional<Set<AttachmentHint>> attachmentHint = entryValue.getAttachmentHint();
Optional<String> icon = entryValue.getIcon();
Optional<String> description = entryValue.getDescription();
Optional<Set<AuthenticatorTransport>> authenticatorTransport;
if (entryValue.getAuthenticatorGetInfo().isPresent()) {
authenticatorTransport = entryValue.getAuthenticatorGetInfo().get().getTransports();
} else {
authenticatorTransport = Optional.empty();
}
attResult = AttestationRegistration.builder()
.aaguid(aaguid.isPresent() ? aaguid.get().asGuidString() : null)
.aaid(aaid.isPresent() ? aaid.get().getValue() : null)
.attachmentHint(attachmentHint.isPresent() ? attachmentHint.get() : null)
.icon(icon.isPresent() ? icon.get() : null)
.description(description.isPresent() ? description.get() : null)
.authenticatorTransport(authenticatorTransport.isPresent() ? authenticatorTransport.get() : null)
.build();
log.debug("AttestationRegistration result: {}", attResult);
}
return Optional.ofNullable(attResult);
}
}