static uint8_t* AppendEncryptedSignature(SecOTRSessionRef session, const cc_unit* s, bool usePrime, CFMutableDataRef appendTo) { CFMutableDataRef signature = CFDataCreateMutable(kCFAllocatorDefault, 0); CFMutableDataRef mbData = CFDataCreateMutable(kCFAllocatorDefault, 0); CFMutableDataRef mb = CFDataCreateMutable(kCFAllocatorDefault, 0); SecFDHKAppendPublicSerialization(session->_myKey, mbData); SecPDHKAppendSerialization(session->_theirKey, mbData); CFIndex publicKeyOffset = CFDataGetLength(mbData); SecOTRPublicIdentityRef myPublic = SecOTRPublicIdentityCopyFromPrivate(kCFAllocatorDefault, session->_me, NULL); AppendPublicKey(mbData, myPublic); CFReleaseNull(myPublic); AppendLong(mbData, session->_keyID); DeriveAndAppendSHA256HMAC(mb, kExponentiationUnits, s, usePrime ? kM1Prime : kM1, (size_t)CFDataGetLength(mbData), CFDataGetBytePtr(mbData)); CFDataDeleteBytes(mbData, CFRangeMake(0, publicKeyOffset)); CFMutableDataRef xb = mbData; mbData = NULL; SecOTRFIAppendSignature(session->_me, mb, signature, NULL); CFReleaseNull(mb); AppendCFDataAsDATA(xb, signature); CFReleaseNull(signature); CFIndex dataLength = CFDataGetLength(xb); CFIndex signatureStartIndex = CFDataGetLength(appendTo); /* 64 bits cast: We are appending the signature we just generated, which is never bigger than 2^32 bytes. */ assert(((unsigned long)dataLength)<=UINT32_MAX); /* debug check, correct as long as CFIndex is a signed long */ AppendLong(appendTo, (uint32_t)dataLength); uint8_t *destination = CFDataIncreaseLengthAndGetMutableBytes(appendTo, dataLength); uint8_t c[kOTRAuthKeyBytes]; DeriveOTR128BitPairFromS(kCs, kExponentiationUnits, s, sizeof(c), usePrime ? NULL : c, sizeof(c), usePrime ? c : NULL); AES_CTR_IV0_Transform(sizeof(c), c, (size_t)dataLength, CFDataGetBytePtr(xb), destination); bzero(c, sizeof(c)); CFReleaseNull(xb); return CFDataGetMutableBytePtr(appendTo) + signatureStartIndex; }
static void tests() { CFErrorRef testError = NULL; SecOTRFullIdentityRef aliceID = SecOTRFullIdentityCreate(kCFAllocatorDefault, &testError); SecMPLogError(testError); CFReleaseNull(testError); testError = NULL; SecOTRFullIdentityRef bobID = SecOTRFullIdentityCreate(kCFAllocatorDefault, &testError); SecMPLogError(testError); CFReleaseNull(testError); testError = NULL; ok(aliceID, "create alice ID"); ok(bobID, "create bob ID"); SecOTRPublicIdentityRef alicePublicID = SecOTRPublicIdentityCopyFromPrivate(kCFAllocatorDefault, aliceID, &testError); SecMPLogError(testError); CFReleaseNull(testError); SecOTRPublicIdentityRef bobPublicID = SecOTRPublicIdentityCopyFromPrivate(kCFAllocatorDefault, bobID, &testError); SecMPLogError(testError); CFReleaseNull(testError); ok(alicePublicID, "extract alice public"); ok(bobPublicID, "extract bob public"); SecOTRSessionRef aliceSession = SecOTRSessionCreateFromIDAndFlags(kCFAllocatorDefault, aliceID, bobPublicID, kSecOTRSendTextMessages); SecOTRSessionRef bobSession = SecOTRSessionCreateFromIDAndFlags(kCFAllocatorDefault, bobID, alicePublicID, kSecOTRSendTextMessages); ok(aliceSession, "create alice session"); ok(bobSession, "create bob session"); SecOTRSessionRef aliceCompactSession = SecOTRSessionCreateFromIDAndFlags(kCFAllocatorDefault, aliceID, bobPublicID, kSecOTRUseAppleCustomMessageFormat); SecOTRSessionRef bobCompactSession = SecOTRSessionCreateFromIDAndFlags(kCFAllocatorDefault, bobID, alicePublicID, kSecOTRUseAppleCustomMessageFormat); ok(aliceCompactSession, "create alice compact session"); ok(bobCompactSession, "create bob compact session"); SecOTRSessionRef aliceCompactHashesSession = SecOTRSessionCreateFromIDAndFlags(kCFAllocatorDefault, aliceID, bobPublicID, kSecOTRUseAppleCustomMessageFormat|kSecOTRIncludeHashesInMessages); SecOTRSessionRef bobCompactHashesSession = SecOTRSessionCreateFromIDAndFlags(kCFAllocatorDefault, bobID, alicePublicID, kSecOTRUseAppleCustomMessageFormat|kSecOTRIncludeHashesInMessages); ok(aliceCompactHashesSession, "create alice compact session with hashes"); ok(bobCompactHashesSession, "create bob compact session with hashes"); // Release the IDs, sessions shouldn't need us to retain them for them. CFReleaseNull(aliceID); CFReleaseNull(bobID); CFReleaseNull(alicePublicID); CFReleaseNull(bobPublicID); negotiate(&aliceSession, &bobSession, true, true, true, false); negotiate(&aliceSession, &bobSession, true, false, true, false); negotiate(&aliceCompactSession, &bobCompactSession, true, true, false, true); negotiate(&aliceCompactSession, &bobCompactSession, true, false, false, true); negotiate(&aliceCompactHashesSession, &bobCompactHashesSession, true, true, false, true); negotiate(&aliceCompactHashesSession, &bobCompactHashesSession, true, false, false, true); /* cleanup keychain */ ok(SecOTRFIPurgeAllFromKeychain(&testError),"cleanup keychain"); SecMPLogError(testError); CFReleaseNull(testError); CFReleaseNull(aliceSession); CFReleaseNull(bobSession); CFReleaseNull(aliceCompactSession); CFReleaseNull(bobCompactSession); CFReleaseNull(aliceCompactHashesSession); CFReleaseNull(bobCompactHashesSession); }
static void tests(void) { CFErrorRef testError = NULL; SecOTRFullIdentityRef idToPurge = SecOTRFullIdentityCreate(kCFAllocatorDefault, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(idToPurge != NULL, "Make Identity"); CFMutableDataRef purgeExport = CFDataCreateMutable(kCFAllocatorDefault, 0); ok(SecOTRFIAppendSerialization(idToPurge, purgeExport, &testError), "First export"); RegressionsLogError(testError); CFReleaseNull(testError); SecOTRFullIdentityRef purgeIdInflate = SecOTRFullIdentityCreateFromData(kCFAllocatorDefault, purgeExport, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(purgeIdInflate != NULL, "Inflate Identity"); SecOTRFIPurgeFromKeychain(idToPurge, &testError); RegressionsLogError(testError); CFReleaseNull(testError); SecOTRFullIdentityRef failIDInflate = SecOTRFullIdentityCreateFromData(kCFAllocatorDefault, purgeExport, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(failIDInflate == NULL, "Should fail"); CFReleaseSafe(idToPurge); idToPurge = SecOTRFullIdentityCreate(kCFAllocatorDefault, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(idToPurge != NULL, "Make Identity again"); SecOTRFIPurgeAllFromKeychain(&testError); RegressionsLogError(testError); CFReleaseNull(testError); SecOTRFullIdentityRef failIDInflate2 = SecOTRFullIdentityCreateFromData(kCFAllocatorDefault, purgeExport, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(failIDInflate2 == NULL, "Should fail 2"); SecOTRFullIdentityRef id = SecOTRFullIdentityCreate(kCFAllocatorDefault, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(id != NULL, "Make Identity"); CFMutableDataRef firstExport = CFDataCreateMutable(kCFAllocatorDefault, 0); ok(SecOTRFIAppendSerialization(id, firstExport, &testError), "First export"); RegressionsLogError(testError); CFReleaseNull(testError); SecOTRFullIdentityRef idInflate = SecOTRFullIdentityCreateFromData(kCFAllocatorDefault, firstExport, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(idInflate != NULL, "Inflate Identity"); CFMutableDataRef secondExport = CFDataCreateMutable(kCFAllocatorDefault, 0); ok(SecOTRFIAppendSerialization(idInflate, secondExport, &testError), "second export"); RegressionsLogError(testError); CFReleaseNull(testError); ok(CFDataGetLength(firstExport) == CFDataGetLength(secondExport) && 0 == memcmp(CFDataGetBytePtr(firstExport), CFDataGetBytePtr(secondExport), (size_t)CFDataGetLength(firstExport)), "Different exports"); SecOTRPublicIdentityRef pubID = SecOTRPublicIdentityCopyFromPrivate(kCFAllocatorDefault, id, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(id != NULL, "Failed to copy public identity"); CFMutableDataRef firstPublicExport = CFDataCreateMutable(kCFAllocatorDefault, 0); ok(SecOTRPIAppendSerialization(pubID, firstPublicExport, &testError), "failed first public export"); RegressionsLogError(testError); CFReleaseNull(testError); SecOTRPublicIdentityRef pubIDInflate = SecOTRPublicIdentityCreateFromData(kCFAllocatorDefault, firstPublicExport, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(pubIDInflate != NULL, "Pub inflate failed"); CFMutableDataRef secondPublicExport = CFDataCreateMutable(kCFAllocatorDefault, 0); ok(SecOTRPIAppendSerialization(pubID, secondPublicExport, &testError), "failed second public export"); RegressionsLogError(testError); CFReleaseNull(testError); ok(CFDataGetLength(firstPublicExport) == CFDataGetLength(secondPublicExport) && 0 == memcmp(CFDataGetBytePtr(firstPublicExport), CFDataGetBytePtr(secondPublicExport), (size_t)CFDataGetLength(firstPublicExport)), "Different public exports"); uint8_t sampleByteString[] = { 0x30, 0x81, 0xf6, 0x81, 0x43, 0x00, 0x41, 0x04, 0xc6, 0x8a, 0x2a, 0x5c, 0x29, 0xa4, 0xb7, 0x58, 0xe1, 0x3c, 0x07, 0x19, 0x20, 0xf3, 0x0b, 0xb8, 0xb3, 0x40, 0x41, 0x29, 0x4a, 0xa6, 0x7a, 0x56, 0x28, 0x6d, 0x10, 0x85, 0x2b, 0x14, 0x83, 0xaa, 0x1f, 0x6a, 0x47, 0xbc, 0x19, 0x26, 0x39, 0x1c, 0xd4, 0xbb, 0x8c, 0xd6, 0x94, 0x24, 0x79, 0x60, 0xfb, 0x8e, 0x4b, 0xf4, 0x0f, 0xbf, 0x38, 0x81, 0x78, 0xce, 0x1d, 0xd9, 0x03, 0xec, 0x65, 0xcd, 0x82, 0x81, 0xae, 0x00, 0xac, 0x30, 0x81, 0xa9, 0x02, 0x81, 0xa1, 0x00, 0xd2, 0xf4, 0x40, 0x8b, 0x2f, 0x09, 0x75, 0x2c, 0x68, 0x12, 0x76, 0xb9, 0xfb, 0x1b, 0x02, 0x91, 0x6d, 0xd7, 0x86, 0x49, 0xdc, 0xef, 0x38, 0xf3, 0x50, 0x58, 0xb5, 0xff, 0x5c, 0x02, 0x8a, 0xb0, 0xcd, 0xb3, 0x3d, 0x94, 0x71, 0x7d, 0x32, 0x53, 0xed, 0x43, 0xfb, 0xde, 0xbc, 0x20, 0x21, 0x33, 0xe3, 0xeb, 0x93, 0x48, 0xe8, 0xd1, 0x32, 0x2f, 0x40, 0x40, 0x47, 0x1f, 0xeb, 0x7e, 0xf6, 0x43, 0x81, 0x51, 0xd6, 0x4f, 0xe0, 0x57, 0xbf, 0x12, 0xeb, 0x18, 0x2e, 0x81, 0x0b, 0x3a, 0x04, 0xf1, 0xeb, 0x3c, 0xe1, 0xb9, 0xf4, 0x87, 0x37, 0x83, 0x5a, 0x2e, 0x09, 0xf8, 0xd5, 0xa0, 0x12, 0xfb, 0x35, 0xe4, 0xd4, 0x3f, 0xef, 0x24, 0x3e, 0x6c, 0xff, 0xb1, 0x35, 0x7e, 0x9f, 0xe7, 0x6d, 0x2f, 0xf8, 0x0d, 0xc6, 0xbc, 0x19, 0xe2, 0x78, 0xb3, 0x71, 0xe1, 0x35, 0xe7, 0xc7, 0x22, 0x6b, 0x4d, 0x92, 0xc4, 0x10, 0x75, 0x1a, 0x9b, 0x9f, 0x7f, 0xac, 0x2d, 0xfb, 0xc9, 0x64, 0x1e, 0x80, 0x11, 0x7f, 0x75, 0x8a, 0x86, 0x7e, 0x09, 0x44, 0xc4, 0x71, 0xbf, 0xd4, 0xfa, 0x8b, 0x6a, 0xb8, 0x9f, 0x02, 0x03, 0x01, 0x00, 0x01}; CFDataRef testInteropImport = CFDataCreate(kCFAllocatorDefault, sampleByteString, sizeof(sampleByteString)); SecOTRPublicIdentityRef interopIDInflate = SecOTRPublicIdentityCreateFromData(kCFAllocatorDefault, testInteropImport, &testError); RegressionsLogError(testError); CFReleaseNull(testError); ok(interopIDInflate != NULL, "Interop inflate failed"); /* cleanup keychain */ ok(SecOTRFIPurgeAllFromKeychain(&testError),"cleanup keychain"); RegressionsLogError(testError); CFReleaseNull(testError); CFReleaseSafe(pubID); CFReleaseSafe(pubIDInflate); CFReleaseSafe(firstPublicExport); CFReleaseSafe(secondPublicExport); CFReleaseSafe(id); CFReleaseSafe(idToPurge); CFReleaseSafe(idInflate); CFReleaseSafe(firstExport); CFReleaseSafe(secondExport); CFReleaseSafe(purgeExport); CFReleaseSafe(purgeIdInflate); CFReleaseSafe(failIDInflate); CFReleaseSafe(failIDInflate2); CFReleaseSafe(testInteropImport); CFReleaseSafe(interopIDInflate); }