Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failure reasons for App Attest error cases #32

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions AppCheckCore/Sources/AppAttestProvider/GACAppAttestProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,21 @@ - (void)getTokenWithLimitedUse:(BOOL)limitedUse
do:^NSData *_Nullable {
return [GACAppCheckCryptoUtils sha256HashFromData:challenge];
}]
.thenOn(
self.queue,
^FBLPromise<NSData *> *(NSData *challengeHash) {
return [FBLPromise onQueue:self.queue
wrapObjectOrErrorCompletion:^(FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
[self.appAttestService attestKey:keyID
clientDataHash:challengeHash
completionHandler:handler];
}];
})
.thenOn(self.queue,
^FBLPromise<NSData *> *(NSData *challengeHash) {
return [FBLPromise onQueue:self.queue
wrapObjectOrErrorCompletion:^(
FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
[self.appAttestService attestKey:keyID
clientDataHash:challengeHash
completionHandler:handler];
}]
.recoverOn(self.queue, ^id(NSError *error) {
return [GACAppCheckErrorUtil appAttestAttestKeyFailedWithError:error
keyId:keyID
clientDataHash:challengeHash];
});
})
.thenOn(self.queue, ^FBLPromise<GACAppAttestKeyAttestationResult *> *(NSData *attestation) {
GACAppAttestKeyAttestationResult *result =
[[GACAppAttestKeyAttestationResult alloc] initWithKeyID:keyID
Expand Down Expand Up @@ -433,17 +438,23 @@ - (void)getTokenWithLimitedUse:(BOOL)limitedUse
// 1.2. Get the statement SHA256 hash.
return [GACAppCheckCryptoUtils sha256HashFromData:[statementForAssertion copy]];
}]
.thenOn(
self.queue,
^FBLPromise<NSData *> *(NSData *statementHash) {
// 2. Generate App Attest assertion.
return [FBLPromise onQueue:self.queue
wrapObjectOrErrorCompletion:^(FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
[self.appAttestService generateAssertion:keyID
clientDataHash:statementHash
completionHandler:handler];
}];
})
.thenOn(self.queue,
^FBLPromise<NSData *> *(NSData *statementHash) {
// 2. Generate App Attest assertion.
return [FBLPromise onQueue:self.queue
wrapObjectOrErrorCompletion:^(
FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
[self.appAttestService generateAssertion:keyID
clientDataHash:statementHash
completionHandler:handler];
}]
.recoverOn(self.queue, ^id(NSError *error) {
return [GACAppCheckErrorUtil
appAttestGenerateAssertionFailedWithError:error
keyId:keyID
clientDataHash:statementHash];
});
})
// 3. Compose the result object.
.thenOn(self.queue, ^GACAppAttestAssertionData *(NSData *assertion) {
return [[GACAppAttestAssertionData alloc] initWithChallenge:challenge
Expand Down Expand Up @@ -521,6 +532,10 @@ - (void)getTokenWithLimitedUse:(BOOL)limitedUse
wrapObjectOrErrorCompletion:^(FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
[self.appAttestService generateKeyWithCompletionHandler:handler];
}]
.recoverOn(self.queue,
^id(NSError *error) {
return [GACAppCheckErrorUtil appAttestGenerateKeyFailedWithError:error];
})
.thenOn(self.queue, ^FBLPromise<NSString *> *(NSString *keyID) {
return [self.keyIDStorage setAppAttestKeyID:keyID];
});
Expand Down
12 changes: 12 additions & 0 deletions AppCheckCore/Sources/Core/Errors/GACAppCheckErrorUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,20 @@ void GACAppCheckSetErrorToPointer(NSError *error, NSError **pointer);

+ (NSError *)unsupportedAttestationProvider:(NSString *)providerName;

// MARK: - App Attest Errors

+ (NSError *)appAttestKeyIDNotFound;

+ (NSError *)appAttestGenerateKeyFailedWithError:(NSError *)error;

+ (NSError *)appAttestAttestKeyFailedWithError:(NSError *)error
keyId:(NSString *)keyId
clientDataHash:(NSData *)clientDataHash;

+ (NSError *)appAttestGenerateAssertionFailedWithError:(NSError *)error
keyId:(NSString *)keyId
clientDataHash:(NSData *)clientDataHash;

@end

NS_ASSUME_NONNULL_END
43 changes: 41 additions & 2 deletions AppCheckCore/Sources/Core/Errors/GACAppCheckErrorUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,56 @@ + (NSError *)unsupportedAttestationProvider:(NSString *)providerName {
underlyingError:nil];
}

+ (NSError *)errorWithFailureReason:(NSString *)failureReason {
return [self appCheckErrorWithCode:GACAppCheckErrorCodeUnknown
failureReason:failureReason
underlyingError:nil];
}

#pragma mark - App Attest

+ (NSError *)appAttestKeyIDNotFound {
NSString *failureReason = [NSString stringWithFormat:@"App attest key ID not found."];
return [self appCheckErrorWithCode:GACAppCheckErrorCodeUnknown
failureReason:failureReason
underlyingError:nil];
}

+ (NSError *)errorWithFailureReason:(NSString *)failureReason {
+ (NSError *)appAttestGenerateKeyFailedWithError:(NSError *)error {
NSString *failureReason = @"Failed to generate a new cryptographic key for use with the App "
@"Attest service (`generateKeyWithCompletionHandler:`).";
// TODO(#31): Add a new error code for this case (e.g., GACAppCheckAppAttestGenerateKeyFailed).
return [self appCheckErrorWithCode:GACAppCheckErrorCodeUnknown
failureReason:failureReason
underlyingError:nil];
underlyingError:error];
}

+ (NSError *)appAttestAttestKeyFailedWithError:(NSError *)error
keyId:(NSString *)keyId
clientDataHash:(NSData *)clientDataHash {
NSString *failureReason =
[NSString stringWithFormat:@"Failed to attest the validity of the generated cryptographic "
@"key (`attestKey:clientDataHash:completionHandler:`); "
@"keyId.length = %lu, clientDataHash.length = %lu",
keyId.length, clientDataHash.length];
// TODO(#31): Add a new error code for this case (e.g., GACAppCheckAppAttestAttestKeyFailed).
return [self appCheckErrorWithCode:GACAppCheckErrorCodeUnknown
failureReason:failureReason
underlyingError:error];
}

+ (NSError *)appAttestGenerateAssertionFailedWithError:(NSError *)error
keyId:(NSString *)keyId
clientDataHash:(NSData *)clientDataHash {
NSString *failureReason = [NSString
stringWithFormat:@"Failed to create a block of data that demonstrates the legitimacy of the "
@"app instance (`generateAssertion:clientDataHash:completionHandler:`); "
@"keyId.length = %lu, clientDataHash.length = %lu.",
keyId.length, clientDataHash.length];
// TODO(#31): Add error code for this case (e.g., GACAppCheckAppAttestGenerateAssertionFailed).
return [self appCheckErrorWithCode:GACAppCheckErrorCodeUnknown
failureReason:failureReason
underlyingError:error];
}

#pragma mark - Helpers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ - (void)testGetToken_WhenUnregisteredKeyAndKeyAttestationError {
NSError *attestationError = [NSError errorWithDomain:@"testGetTokenWhenKeyAttestationError"
code:0
userInfo:nil];
NSError *expectedError =
[GACAppCheckErrorUtil appAttestAttestKeyFailedWithError:attestationError
keyId:existingKeyID
clientDataHash:self.randomChallengeHash];
id attestCompletionArg = [OCMArg invokeBlockWithArgs:[NSNull null], attestationError, nil];
OCMExpect([self.mockAppAttestService attestKey:existingKeyID
clientDataHash:self.randomChallengeHash
Expand All @@ -410,7 +414,7 @@ - (void)testGetToken_WhenUnregisteredKeyAndKeyAttestationError {
[completionExpectation fulfill];

XCTAssertNil(token);
XCTAssertEqualObjects(error, attestationError);
XCTAssertEqualObjects(error, expectedError);
}];

[self waitForExpectations:@[ self.fakeBackoffWrapper.backoffExpectation, completionExpectation ]
Expand All @@ -421,7 +425,7 @@ - (void)testGetToken_WhenUnregisteredKeyAndKeyAttestationError {
[self verifyAllMocks];

// 9. Verify backoff error.
XCTAssertEqualObjects(self.fakeBackoffWrapper.operationError, attestationError);
XCTAssertEqualObjects(self.fakeBackoffWrapper.operationError, expectedError);
}

- (void)testGetToken_WhenUnregisteredKeyAndKeyAttestationExchangeError {
Expand Down Expand Up @@ -697,6 +701,10 @@ - (void)testGetToken_WhenKeyRegisteredAndGenerateAssertionError {
[NSError errorWithDomain:@"testGetToken_WhenKeyRegisteredAndGenerateAssertionError"
code:0
userInfo:nil];
NSError *expectedError =
[GACAppCheckErrorUtil appAttestGenerateAssertionFailedWithError:generateAssertionError
keyId:existingKeyID
clientDataHash:self.randomChallengeHash];
id completionBlockArg = [OCMArg invokeBlockWithArgs:[NSNull null], generateAssertionError, nil];
OCMExpect([self.mockAppAttestService
generateAssertion:existingKeyID
Expand All @@ -718,7 +726,7 @@ - (void)testGetToken_WhenKeyRegisteredAndGenerateAssertionError {
[completionExpectation fulfill];

XCTAssertNil(token);
XCTAssertEqualObjects(error, generateAssertionError);
XCTAssertEqualObjects(error, expectedError);
}];

[self waitForExpectations:@[ completionExpectation ] timeout:0.5];
Expand Down