Skip to content

Commit eb033f9

Browse files
committed
Addressing comments. Adding logging, nil out generated keys
1 parent 8369600 commit eb033f9

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

IdentityCore/src/JWEResponse/MSIDJweResponse+EcdhAesGcm.m

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ - (nullable NSDictionary *)decryptJweResponseWithPrivateStk:(nonnull SecKeyRef)p
4444
jweCrypto:(nonnull MSIDJWECrypto *)jweCrypto
4545
error:(NSError * _Nullable __autoreleasing * _Nullable)error
4646
{
47+
MSID_LOG_WITH_CTX(MSIDLogLevelInfo, nil, @"Starting to decrypt JWE response using ECDH-AESGCM using jwe_crypto : %@", jweCrypto.urlEncodedJweCrypto);
48+
4749
// 1. Check for necessary request parameters
4850
NSData *apv = [NSData msidDataFromBase64UrlEncodedString:jweCrypto.apv.APV];
4951

@@ -65,7 +67,7 @@ - (nullable NSDictionary *)decryptJweResponseWithPrivateStk:(nonnull SecKeyRef)p
6567
{
6668
if (error)
6769
{
68-
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, @"Unexpected server response, no epk present in JWE header", nil, nil, nil, nil, nil, NO);
70+
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, @"Unexpected server response, no epk present in JWE header", nil, nil, nil, nil, nil, YES);
6971
}
7072
MSID_LOG_WITH_CTX(MSIDLogLevelError, nil, @"Unexpected server response, no epk present in JWE header, epk %@", epk);
7173
return nil;
@@ -174,7 +176,8 @@ - (NSData *)calculateDerivedKeyWithSharedKey:(NSData *)sharedSecret
174176
partyUInfo:apu
175177
partyVInfo:apv
176178
error:&concatKDFError];
177-
179+
// Deallocating sharedSecret as it is no longer needed
180+
sharedSecret = nil;
178181
if (!derivedKey)
179182
{
180183
if (error)
@@ -196,7 +199,7 @@ - (NSDictionary *)decryptJweResponseUsingSymmetricKey:(NSData *)symmetricKey
196199
{
197200
if (error)
198201
{
199-
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, @"Symmetric key is nil", nil, nil, nil, nil, nil, NO);
202+
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, @"Symmetric key is nil", nil, nil, nil, nil, nil, YES);
200203
}
201204
return nil;
202205
}
@@ -209,12 +212,14 @@ - (NSDictionary *)decryptJweResponseUsingSymmetricKey:(NSData *)symmetricKey
209212
// Since only A256GCM is supported, we can decrypt jwe message using AES256GCM.
210213
MSIDAesGcmDecryptor *decryptor = [MSIDAesGcmDecryptor new];
211214
NSData *decryptedData = [decryptor decryptWithAES256GCMHandlerWithMessage:self.payload iv:self.iv key:symmetricKey tag:self.tag aad:self.aad error:error];
212-
215+
// Deallocate symmetricKey as it is no longer needed
216+
symmetricKey = nil;
213217
if (!decryptedData)
214218
{
215219
if (error)
216220
{
217-
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, @"Unexpected server response, failed to decrypt JWE", nil, nil, nil, nil, nil, NO);
221+
NSError *subError = *error ? *error : nil;
222+
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, @"Unexpected server response, failed to decrypt JWE", nil, [subError description], subError, nil, nil, YES);
218223
}
219224
MSID_LOG_WITH_CTX(MSIDLogLevelError, nil, @"Unexpected server response, failed to decrypt JWE");
220225
return nil;
@@ -223,6 +228,14 @@ - (NSDictionary *)decryptJweResponseUsingSymmetricKey:(NSData *)symmetricKey
223228
MSIDJsonSerializer *serializer = [MSIDJsonSerializer new];
224229

225230
NSDictionary *jsonResult = [serializer deserializeJSON:decryptedData error:error];
231+
if (!jsonResult)
232+
{
233+
if (error)
234+
{
235+
NSError *subError = *error ? *error : nil;
236+
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, @"Failed to serialize decrypted data to JSON", nil, [subError description], subError, nil, nil, YES);
237+
}
238+
}
226239
return jsonResult;
227240
}
228241

@@ -232,16 +245,16 @@ - (BOOL)IsJweResponseAlgorithmSupported:(NSError * _Nullable __autoreleasing * _
232245
{
233246
if (error)
234247
{
235-
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, [NSString stringWithFormat:@"Unsupported JWE algorithm : %@", self.headerAlgorithm], nil, nil, nil, nil, nil, NO);
248+
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, [NSString stringWithFormat:@"Unsupported JWE algorithm : %@", self.headerAlgorithm], nil, nil, nil, nil, nil, YES);
236249
}
237250
return NO;
238251
}
239252

240-
if (![self.jweHeader[@"enc"] isEqualToString:MSID_RESPONSE_ENCRYPTION_ALGORITHM_A256GCM])
253+
if (![self.headerEncryptionAlgorithm isEqualToString:MSID_RESPONSE_ENCRYPTION_ALGORITHM_A256GCM])
241254
{
242255
if (error)
243256
{
244-
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, [NSString stringWithFormat:@"Unsupported JWE encryption algorithm : %@", self.jweHeader[@"enc"]], nil, nil, nil, nil, nil, NO);
257+
*error = MSIDCreateError(MSIDErrorDomain, MSIDErrorInternal, [NSString stringWithFormat:@"Unsupported JWE encryption algorithm : %@", self.jweHeader[@"enc"]], nil, nil, nil, nil, nil, YES);
245258
}
246259
return NO;
247260
}

IdentityCore/src/MSIDJweResponse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
3131
@property (readonly, nonatomic) NSData *payload;
3232
@property (readonly, nonatomic) NSData *headerContext;
3333
@property (readonly, nonatomic) NSString *headerAlgorithm;
34+
@property (readonly, nonatomic) NSString *headerEncryptionAlgorithm;
3435
@property (readonly, nonatomic) NSData *tag;
3536
@property (readonly, nonatomic) NSData *aad;
3637
@property (readonly, nonatomic) NSDictionary *jweHeader;

IdentityCore/src/MSIDJweResponse.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ - (id)initWithRawJWE:(NSString *)rawJWE
6868
{
6969
NSDictionary *dict = (NSDictionary *)jsonObject;
7070
_headerAlgorithm = [dict objectForKey:@"alg"];
71+
_headerEncryptionAlgorithm = [dict objectForKey:@"enc"];
7172
if([dict objectForKey:@"ctx"])
7273
{
7374
_headerContext = [[NSData alloc] initWithBase64EncodedString:[dict objectForKey:@"ctx"] options:0];

0 commit comments

Comments
 (0)