#include <Security/SecBase.h>
#include <Security/SecItem.h>
#include <Security/SecKey.h>
#include <Security/SecKeyPriv.h>
#include <SOSPeerInfoDER.h>
#include <Security/SecureObjectSync/SOSCircle.h>
#include <Security/SecureObjectSync/SOSPeerInfo.h>
#include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
#include <Security/SecureObjectSync/SOSInternal.h>
#include <Security/SecureObjectSync/SOSUserKeygen.h>
#include <utilities/SecCFWrappers.h>
#include <CoreFoundation/CoreFoundation.h>
#include <stdlib.h>
#include <unistd.h>
#include "SOSCircle_regressions.h"
#include "SOSRegressionUtilities.h"
#if TARGET_OS_IPHONE
#include <MobileGestalt.h>
#endif
static CFDataRef CopyTestBackupKey(void) {
static uint8_t data[] = { 'A', 'b', 'c' };
return CFDataCreate(kCFAllocatorDefault, data, sizeof(data));
}
static bool PeerInfoRoundTrip(SOSPeerInfoRef pi) {
bool retval = false;
size_t size = SOSPeerInfoGetDEREncodedSize(pi, NULL);
uint8_t buffer[size];
const uint8_t *buffer_p = SOSPeerInfoEncodeToDER(pi, NULL, buffer, buffer + sizeof(buffer));
ok(buffer_p != NULL, "encode");
if(buffer_p == NULL) return false;
SOSPeerInfoRef pi2 = SOSPeerInfoCreateFromDER(NULL, NULL, &buffer_p, buffer + sizeof(buffer));
ok(pi2 != NULL, "decode");
if(!pi2) return false;
ok(CFEqual(pi, pi2), "Decode matches");
if(CFEqual(pi, pi2)) retval = true;
CFReleaseNull(pi2);
return retval;
}
static bool FullPeerInfoRoundTrip(SOSFullPeerInfoRef fpi) {
bool retval = false;
size_t size = SOSFullPeerInfoGetDEREncodedSize(fpi, NULL);
uint8_t buffer[size];
const uint8_t *buffer_p = SOSFullPeerInfoEncodeToDER(fpi, NULL, buffer, buffer + sizeof(buffer));
ok(buffer_p != NULL, "encode");
if(buffer_p == NULL) return false;
SOSFullPeerInfoRef fpi2 = SOSFullPeerInfoCreateFromDER(NULL, NULL, &buffer_p, buffer + sizeof(buffer));
ok(fpi2 != NULL, "decode");
if(!fpi2) return false;
ok(CFEqual(fpi, fpi2), "Decode matches");
if(CFEqual(fpi, fpi2)) retval = true;
CFReleaseNull(fpi2);
return retval;
}
static int kTestTestCount = 24;
static void tests(void)
{
SecKeyRef signingKey = NULL;
SecKeyRef octagonSigningKey = NULL;
SecKeyRef octagonEncryptionKey = NULL;
SOSFullPeerInfoRef fpi = SOSCreateFullPeerInfoFromName(CFSTR("Test Peer"), &signingKey, &octagonSigningKey, &octagonEncryptionKey, NULL);
SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
ok(NULL != pi, "info creation");
ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips");
ok(FullPeerInfoRoundTrip(fpi), "FullPeerInfo safely round-trips");
CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
CFErrorRef error = NULL;
CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
ok(parameters, "No parameters!");
ok(error == NULL, "Error: (%@)", error);
CFReleaseNull(error);
SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
CFReleaseSafe(cfpassword);
CFReleaseNull(parameters);
SecKeyRef user_pubkey = SecKeyCreatePublicFromPrivate(user_privkey);
ok(SOSFullPeerInfoPromoteToApplication(fpi, user_privkey, &error), "Promote to Application");
ok(SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(fpi), user_pubkey, &error), "Promote to Application");
pi = SOSFullPeerInfoGetPeerInfo(fpi);
ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips");
CFDataRef testBackupKey = CopyTestBackupKey();
ok(SOSFullPeerInfoUpdateBackupKey(fpi, testBackupKey, &error), "Set Backup (%@)", error);
CFReleaseNull(error);
CFReleaseNull(testBackupKey); testBackupKey = CopyTestBackupKey();
pi = SOSFullPeerInfoGetPeerInfo(fpi);
CFDataRef piBackupKey = SOSPeerInfoCopyBackupKey(pi);
ok(CFEqualSafe(testBackupKey, piBackupKey), "Same Backup Key");
ok(PeerInfoRoundTrip(pi), "PeerInfo safely round-trips with backup key");
CFReleaseNull(piBackupKey);
piBackupKey = SOSPeerInfoCopyBackupKey(pi);
ok(CFEqualSafe(testBackupKey, piBackupKey), "Same Backup Key after round trip");
CFReleaseNull(testBackupKey);
CFReleaseNull(piBackupKey);
CFReleaseNull(user_privkey);
CFReleaseNull(user_pubkey);
CFReleaseNull(signingKey);
CFReleaseNull(octagonSigningKey);
CFReleaseNull(octagonEncryptionKey);
CFReleaseNull(fpi);
}
int sc_30_peerinfo(int argc, char *const *argv)
{
plan_tests(kTestTestCount);
tests();
return 0;
}