diff --git a/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.h b/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.h index 47d631f5..6cc55ccc 100644 --- a/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.h +++ b/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.h @@ -15,7 +15,7 @@ + (SecKeyRef)addKeyWithData:(NSData *)data asPublic:(BOOL)public tag:(NSString *)tag type:(NSString *)type error:(NSError *__autoreleasing*)error; + (SecKeyRef)addKeyWithData:(NSData *)data asPublic:(BOOL)public tag:(NSString *)tag error:(NSError *__autoreleasing*)error; + (SecKeyRef)keyByTag:(NSString *)tag error:(NSError *__autoreleasing*)error; -+ (void)removeKeyByTag:(NSString *)tag error:(NSError *__autoreleasing*)error; ++ (BOOL)removeKeyByTag:(NSString *)tag error:(NSError *__autoreleasing*)error; @end @interface JWTCryptoSecurity (Certificates) diff --git a/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.m b/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.m index e65384dc..418af600 100644 --- a/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.m +++ b/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.m @@ -7,6 +7,8 @@ // #import "JWTCryptoSecurity.h" +#import "JWTErrorDescription.h" + @interface JWTMemoryLayout : NSObject + (NSString *)typeUInt8; + (NSString *)typeCUnsignedChar; @@ -127,18 +129,28 @@ + (SecKeyRef)keyByTag:(NSString *)tag error:(NSError *__autoreleasing*)error; { return NULL; } -+ (void)removeKeyByTag:(NSString *)tag error:(NSError *__autoreleasing*)error; { ++ (BOOL)removeKeyByTag:(NSString *)tag error:(NSError *__autoreleasing*)error; { NSData *tagData = [tag dataUsingEncoding:NSUTF8StringEncoding]; if (tagData == nil) { - // tell that nothing to remove. - return; + // tell that nothing to remove. + if (error) { + *error = [JWTErrorDescription errorWithCode:JWTUnexpectedError]; + } + return NO; } NSDictionary *removeAttributes = @{ (__bridge NSString*)kSecClass: (__bridge NSString*)kSecClassKey, (__bridge NSString*)kSecAttrKeyType: (__bridge NSString*)kSecAttrKeyTypeRSA, - (__bridge NSString*)kSecAttrApplicationTag: tagData, + (__bridge NSString*)kSecAttrApplicationTag: tagData }; - SecItemDelete((__bridge CFDictionaryRef)removeAttributes); + OSStatus status = SecItemDelete((__bridge CFDictionaryRef)removeAttributes); + if (status != errSecSuccess) { + if (error) { + *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil]; + } + return NO; + } + return YES; } @end diff --git a/Core/Supplement/JWTErrorDescription.h b/Core/Supplement/JWTErrorDescription.h index f46c0068..5e135e1d 100644 --- a/Core/Supplement/JWTErrorDescription.h +++ b/Core/Supplement/JWTErrorDescription.h @@ -26,7 +26,8 @@ typedef NS_ENUM(NSInteger, JWTError) { JWTBlacklistedAlgorithmError, JWTDecodingHeaderError, JWTDecodingPayloadError, - JWTDecodingHoldersChainEmptyError + JWTDecodingHoldersChainEmptyError, + JWTUnexpectedError }; @interface JWTErrorDescription : NSObject diff --git a/Core/Supplement/JWTErrorDescription.m b/Core/Supplement/JWTErrorDescription.m index 4aad559d..2f049506 100644 --- a/Core/Supplement/JWTErrorDescription.m +++ b/Core/Supplement/JWTErrorDescription.m @@ -27,7 +27,8 @@ + (NSDictionary *)userDescriptionsAndCodes { @(JWTBlacklistedAlgorithmError): @"Algorithm in blacklist? Try to check whitelist parameter", @(JWTDecodingHeaderError): @"Error decoding the JWT Header segment.", @(JWTDecodingPayloadError): @"Error decoding the JWT Payload segment.", - @(JWTDecodingHoldersChainEmptyError) : @"Error decoding the JWT algorithm and data holdersĀ chain is empty!" + @(JWTDecodingHoldersChainEmptyError): @"Error decoding the JWT algorithm and data holdersĀ chain is empty!", + @(JWTUnexpectedError): @"Unexpected Error" }); } @@ -49,7 +50,8 @@ + (NSDictionary *)errorDescriptionsAndCodes { @(JWTBlacklistedAlgorithmError): @"JWTBlacklistedAlgorithmError", @(JWTDecodingHeaderError): @"JWTDecodingHeaderError", @(JWTDecodingPayloadError): @"JWTDecodingPayloadError", - @(JWTDecodingHoldersChainEmptyError) :@"JWTDecodingHoldersChainEmptyError" + @(JWTDecodingHoldersChainEmptyError): @"JWTDecodingHoldersChainEmptyError", + @(JWTUnexpectedError): @"JWTUnexpectedError" }); }