|
|
@ -12,7 +12,7 @@
|
|
|
|
require(macKey != nil);
|
|
|
|
require(macKey != nil);
|
|
|
|
require(cipherIvSalt != nil);
|
|
|
|
require(cipherIvSalt != nil);
|
|
|
|
require([cipherIvSalt length] == IV_SALT_LENGTH);
|
|
|
|
require([cipherIvSalt length] == IV_SALT_LENGTH);
|
|
|
|
|
|
|
|
|
|
|
|
SrtpStream* s = [SrtpStream new];
|
|
|
|
SrtpStream* s = [SrtpStream new];
|
|
|
|
s->cipherIvSalt = cipherIvSalt;
|
|
|
|
s->cipherIvSalt = cipherIvSalt;
|
|
|
|
s->macKey = macKey;
|
|
|
|
s->macKey = macKey;
|
|
|
@ -24,38 +24,38 @@
|
|
|
|
-(RtpPacket*) encryptAndAuthenticateNormalRtpPacket:(RtpPacket*)normalRtpPacket {
|
|
|
|
-(RtpPacket*) encryptAndAuthenticateNormalRtpPacket:(RtpPacket*)normalRtpPacket {
|
|
|
|
require(normalRtpPacket != nil);
|
|
|
|
require(normalRtpPacket != nil);
|
|
|
|
NSData* payload = [normalRtpPacket payload];
|
|
|
|
NSData* payload = [normalRtpPacket payload];
|
|
|
|
|
|
|
|
|
|
|
|
NSData* iv = [self getIvForSequenceNumber:[normalRtpPacket sequenceNumber] andSynchronizationSourceIdentifier:[normalRtpPacket synchronizationSourceIdentifier]];
|
|
|
|
NSData* iv = [self getIvForSequenceNumber:[normalRtpPacket sequenceNumber] andSynchronizationSourceIdentifier:[normalRtpPacket synchronizationSourceIdentifier]];
|
|
|
|
NSData* encryptedPayload = [payload encryptWithAesInCounterModeWithKey:cipherKey andIv:iv];
|
|
|
|
NSData* encryptedPayload = [payload encryptWithAesInCounterModeWithKey:cipherKey andIv:iv];
|
|
|
|
|
|
|
|
|
|
|
|
RtpPacket* encryptedRtpPacket = [normalRtpPacket withPayload:encryptedPayload];
|
|
|
|
RtpPacket* encryptedRtpPacket = [normalRtpPacket withPayload:encryptedPayload];
|
|
|
|
NSData* hmac = [[encryptedRtpPacket rawPacketDataUsingInteropOptions:@[]] hmacWithSha1WithKey:macKey];
|
|
|
|
NSData* hmac = [[encryptedRtpPacket rawPacketDataUsingInteropOptions:@[]] hmacWithSha1WithKey:macKey];
|
|
|
|
NSData* authenticatedEncryptedPayload = [@[encryptedPayload, hmac] concatDatas];
|
|
|
|
NSData* authenticatedEncryptedPayload = [@[encryptedPayload, hmac] concatDatas];
|
|
|
|
|
|
|
|
|
|
|
|
return [encryptedRtpPacket withPayload:authenticatedEncryptedPayload];
|
|
|
|
return [encryptedRtpPacket withPayload:authenticatedEncryptedPayload];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-(RtpPacket*) verifyAuthenticationAndDecryptSecuredRtpPacket:(RtpPacket*)securedRtpPacket {
|
|
|
|
-(RtpPacket*) verifyAuthenticationAndDecryptSecuredRtpPacket:(RtpPacket*)securedRtpPacket {
|
|
|
|
require(securedRtpPacket != nil);
|
|
|
|
require(securedRtpPacket != nil);
|
|
|
|
checkOperationDescribe([[securedRtpPacket payload] length] >= HMAC_LENGTH, @"Payload not long enough to include hmac");
|
|
|
|
checkOperationDescribe([[securedRtpPacket payload] length] >= HMAC_LENGTH, @"Payload not long enough to include hmac");
|
|
|
|
|
|
|
|
|
|
|
|
NSData* authenticatedData = [securedRtpPacket rawPacketDataUsingInteropOptions:nil];
|
|
|
|
NSData* authenticatedData = [securedRtpPacket rawPacketDataUsingInteropOptions:nil];
|
|
|
|
NSData* includedHmac = [authenticatedData takeLastVolatile:HMAC_LENGTH];
|
|
|
|
NSData* includedHmac = [authenticatedData takeLastVolatile:HMAC_LENGTH];
|
|
|
|
NSData* expectedHmac = [[authenticatedData skipLastVolatile:HMAC_LENGTH] hmacWithSha1WithKey:macKey];
|
|
|
|
NSData* expectedHmac = [[authenticatedData skipLastVolatile:HMAC_LENGTH] hmacWithSha1WithKey:macKey];
|
|
|
|
checkOperationDescribe([expectedHmac length] == HMAC_LENGTH, @"Hmac length constant is wrong");
|
|
|
|
checkOperationDescribe([expectedHmac length] == HMAC_LENGTH, @"Hmac length constant is wrong");
|
|
|
|
checkOperationDescribe([includedHmac isEqualToData_TimingSafe:expectedHmac], @"Authentication failed.");
|
|
|
|
checkOperationDescribe([includedHmac isEqualToData_TimingSafe:expectedHmac], @"Authentication failed.");
|
|
|
|
|
|
|
|
|
|
|
|
NSData* iv = [self getIvForSequenceNumber:[securedRtpPacket sequenceNumber] andSynchronizationSourceIdentifier:[securedRtpPacket synchronizationSourceIdentifier]];
|
|
|
|
NSData* iv = [self getIvForSequenceNumber:[securedRtpPacket sequenceNumber] andSynchronizationSourceIdentifier:[securedRtpPacket synchronizationSourceIdentifier]];
|
|
|
|
NSData* encryptedPayload = [[securedRtpPacket payload] skipLastVolatile:HMAC_LENGTH];
|
|
|
|
NSData* encryptedPayload = [[securedRtpPacket payload] skipLastVolatile:HMAC_LENGTH];
|
|
|
|
NSData* decryptedPayload = [encryptedPayload decryptWithAesInCounterModeWithKey:cipherKey andIv:iv];
|
|
|
|
NSData* decryptedPayload = [encryptedPayload decryptWithAesInCounterModeWithKey:cipherKey andIv:iv];
|
|
|
|
|
|
|
|
|
|
|
|
return [securedRtpPacket withPayload:decryptedPayload];
|
|
|
|
return [securedRtpPacket withPayload:decryptedPayload];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-(NSData*)getIvForSequenceNumber:(uint16_t)sequenceNumber andSynchronizationSourceIdentifier:(uint64_t)synchronizationSourceIdentifier {
|
|
|
|
-(NSData*)getIvForSequenceNumber:(uint16_t)sequenceNumber andSynchronizationSourceIdentifier:(uint64_t)synchronizationSourceIdentifier {
|
|
|
|
int64_t logicalSequence = [sequenceCounter convertNext:sequenceNumber];
|
|
|
|
int64_t logicalSequence = [sequenceCounter convertNext:sequenceNumber];
|
|
|
|
NSMutableData* iv = [NSMutableData dataWithLength:IV_LENGTH];
|
|
|
|
NSMutableData* iv = [NSMutableData dataWithLength:IV_LENGTH];
|
|
|
|
|
|
|
|
|
|
|
|
[iv replaceBytesStartingAt:0 withData:cipherIvSalt];
|
|
|
|
[iv replaceBytesStartingAt:0 withData:cipherIvSalt];
|
|
|
|
uint8_t* b = (uint8_t*)[iv bytes];
|
|
|
|
uint8_t* b = (uint8_t*)[iv bytes];
|
|
|
|
|
|
|
|
|
|
|
@ -67,7 +67,7 @@
|
|
|
|
b[11] ^= (uint8_t)((logicalSequence >> 16) & 0xFF);
|
|
|
|
b[11] ^= (uint8_t)((logicalSequence >> 16) & 0xFF);
|
|
|
|
b[12] ^= (uint8_t)((logicalSequence >> 8) & 0xFF);
|
|
|
|
b[12] ^= (uint8_t)((logicalSequence >> 8) & 0xFF);
|
|
|
|
b[13] ^= (uint8_t)((logicalSequence >> 0) & 0xFF);
|
|
|
|
b[13] ^= (uint8_t)((logicalSequence >> 0) & 0xFF);
|
|
|
|
|
|
|
|
|
|
|
|
return iv;
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|