secd-51-account-inflate.c   [plain text]


/*
 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */


#include <Security/SecBase.h>
#include <Security/SecItem.h>

#include <SecureObjectSync/SOSAccount.h>
#include <SecureObjectSync/SOSCloudCircle.h>
#include <SecureObjectSync/SOSInternal.h>
#include <SecureObjectSync/SOSUserKeygen.h>

#include <stdlib.h>
#include <unistd.h>

#include "secd_regressions.h"
#include "SOSTestDataSource.h"

#include "SOSRegressionUtilities.h"
#include <utilities/SecCFWrappers.h>

#include <securityd/SOSCloudCircleServer.h>

#include "SecdTestKeychainUtilities.h"
#include "SOSAccountTesting.h"
#include "SOSTransportTestTransports.h"

uint8_t staticbuffer[439] = {
    0x30, 0x82, 0x01, 0xB3, 0x31, 0x1D, 0x30, 0x1B, 0x0C, 0x0C, 0x43, 0x6F, 0x6D, 0x70, 0x75, 0x74,
    0x65, 0x72, 0x4E, 0x61, 0x6D, 0x65, 0x0C, 0x0B, 0x54, 0x65, 0x73, 0x74, 0x20, 0x44, 0x65, 0x76,
    0x69, 0x63, 0x65, 0x30, 0x82, 0x01, 0x1F, 0x30, 0x82, 0x01, 0x1B, 0x04, 0x1A, 0x30, 0x18, 0x02,
    0x01, 0x01, 0x0C, 0x08, 0x54, 0x65, 0x73, 0x74, 0x54, 0x79, 0x70, 0x65, 0x02, 0x01, 0x01, 0x30,
    0x00, 0x30, 0x00, 0x30, 0x00, 0x31, 0x00, 0x04, 0x81, 0xFC, 0x30, 0x81, 0xF9, 0x30, 0x81, 0xE8,
    0x31, 0x81, 0x9D, 0x30, 0x14, 0x0C, 0x0F, 0x43, 0x6F, 0x6E, 0x66, 0x6C, 0x69, 0x63, 0x74, 0x56,
    0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x02, 0x01, 0x01, 0x30, 0x2E, 0x0C, 0x0D, 0x44, 0x65, 0x76,
    0x69, 0x63, 0x65, 0x47, 0x65, 0x73, 0x74, 0x61, 0x6C, 0x74, 0x31, 0x1D, 0x30, 0x1B, 0x0C, 0x0C,
    0x43, 0x6F, 0x6D, 0x70, 0x75, 0x74, 0x65, 0x72, 0x4E, 0x61, 0x6D, 0x65, 0x0C, 0x0B, 0x54, 0x65,
    0x73, 0x74, 0x20, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x30, 0x55, 0x0C, 0x10, 0x50, 0x75, 0x62,
    0x6C, 0x69, 0x63, 0x53, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67, 0x4B, 0x65, 0x79, 0x04, 0x41, 0x04,
    0x35, 0xD6, 0x80, 0xF7, 0xB1, 0xC8, 0x83, 0x78, 0x74, 0x27, 0x3B, 0xDD, 0x3C, 0xBB, 0x0E, 0x35,
    0xA3, 0xAC, 0x6A, 0xC5, 0xD6, 0xB1, 0xC7, 0x17, 0x17, 0xD9, 0xEE, 0x51, 0x34, 0x3F, 0x83, 0x60,
    0xE0, 0x99, 0xCB, 0xF7, 0x6B, 0xC1, 0x27, 0x34, 0xB8, 0x4B, 0xE9, 0x2F, 0x43, 0xF9, 0x2F, 0x47,
    0xD3, 0x45, 0x45, 0xEA, 0x01, 0x68, 0x53, 0xAF, 0x99, 0xEE, 0x49, 0xD3, 0xC6, 0x04, 0x56, 0x95,
    0x04, 0x46, 0x30, 0x44, 0x02, 0x20, 0x79, 0xC9, 0x7A, 0xC9, 0x5D, 0x96, 0x7A, 0x95, 0xCC, 0xD3,
    0xE5, 0xEC, 0x45, 0x2A, 0x40, 0x06, 0x09, 0x72, 0xFB, 0xBB, 0x20, 0x0E, 0x4A, 0x8E, 0x4A, 0x0B,
    0x22, 0xF1, 0xA5, 0x38, 0x74, 0x78, 0x02, 0x20, 0x2E, 0xB1, 0x92, 0x3A, 0x8F, 0x42, 0xEC, 0x15,
    0x87, 0x53, 0x57, 0xCB, 0x70, 0x2E, 0x17, 0x58, 0xA1, 0x12, 0x3E, 0x77, 0xE2, 0xA9, 0x14, 0x82,
    0x9F, 0xF4, 0x63, 0x4B, 0xB3, 0x0F, 0xEB, 0x55, 0x04, 0x0C, 0x6B, 0x65, 0x79, 0x73, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x41, 0x04, 0x1C, 0x1D, 0x2D, 0x00,
    0x08, 0x30, 0x82, 0x5E, 0x98, 0x84, 0x83, 0x52, 0xEA, 0xA6, 0x65, 0x9B, 0x25, 0x27, 0x1E, 0x40,
    0xBA, 0x36, 0x0C, 0x73, 0x07, 0xEA, 0x9B, 0x6F, 0xB4, 0x4E, 0x41, 0xD1, 0xF9, 0x49, 0x07, 0x59,
    0x66, 0x63, 0xEE, 0x41, 0xB1, 0xF0, 0x7F, 0x5F, 0xBA, 0xE5, 0x3C, 0x0A, 0xDE, 0x42, 0xC6, 0x4A,
    0xEF, 0x6F, 0x10, 0xC5, 0x8C, 0x1B, 0x5C, 0x1B, 0xDA, 0xD4, 0x5C, 0x3B, 0x04, 0x27, 0x30, 0x25,
    0x04, 0x10, 0x24, 0x1F, 0x16, 0xC5, 0x82, 0xF9, 0xC3, 0xEE, 0x00, 0x09, 0x18, 0xF0, 0xFE, 0x55,
    0xCC, 0x72, 0x02, 0x03, 0x00, 0xC3, 0x50, 0x02, 0x02, 0x01, 0x00, 0x06, 0x08, 0x2A, 0x86, 0x48,
    0x86, 0xF7, 0x0D, 0x02, 0x07, 0x31, 0x00
};



static int kTestTestCount = 7 + kSecdTestSetupTestCount;
static void tests(void)
{
    secd_test_setup_temp_keychain("secd_51_account_inflate", ^{
    });
    
    CFErrorRef error = NULL;
    CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
    CFStringRef cfaccount = CFSTR("test@test.org");
    
    SOSDataSourceFactoryRef test_factory = SOSTestDataSourceFactoryCreate();
    SOSDataSourceRef test_source = SOSTestDataSourceCreate();
    SOSTestDataSourceFactoryAddDataSource(test_factory, CFSTR("TestType"), test_source);
    
    SOSAccountRef account = CreateAccountForLocalChanges(CFSTR("Test Device"), CFSTR("TestType"));
    ok(SOSAccountAssertUserCredentials(account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
    CFReleaseNull(error);
    CFReleaseNull(cfpassword);
    
    ok(NULL != account, "Created");
    
    ok(1 == SOSAccountCountCircles(account), "Has one circle");
    
    // Use this part with suitable changes to test when we allow account upgrades.
    size_t size = SOSAccountGetDEREncodedSize(account, &error);
    CFReleaseNull(error);
    uint8_t buffer[size];
    uint8_t* start = SOSAccountEncodeToDER(account, &error, buffer, buffer + sizeof(buffer));
    CFReleaseNull(error);
    
    ok(start, "successful encoding");
    ok(start == buffer, "Used whole buffer");
    
    const uint8_t *der = buffer;
    SOSAccountRef inflated = SOSAccountCreateFromDER(kCFAllocatorDefault, test_factory,
                                                     &error, &der, buffer + sizeof(buffer));
    
    ok(inflated, "inflated");
    ok(CFEqual(inflated, account), "Compares");
    
#if UPGRADE_FROM_PREVIOUS_VERSION
    CFDictionaryRef new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("New Device"));
    
    ok(SOSAccountResetToOffering(account, &error), "Reset to Offering  (%@)", error);
    CFReleaseNull(error);
    
    is(SOSAccountIsInCircles(account, &error), kSOSCCInCircle, "Was in Circle  (%@)", error);
    CFReleaseNull(error);
    
    SOSAccountUpdateGestalt(account, new_gestalt);
    
    is(SOSAccountIsInCircles(account, &error), kSOSCCInCircle, "Still in Circle  (%@)", error);
    CFReleaseNull(error);
    
    // See if this account inflates:
    
    const uint8_t *der2 = staticbuffer;
    SOSAccountRef inflated2 = SOSAccountCreateFromDER(kCFAllocatorDefault, test_factory, interest_block, update_block,
                                                      &error, &der2, staticbuffer + sizeof(staticbuffer));
    
    ok(!inflated2, "inflated2");
#endif
    CFReleaseNull(account);
    
    
    SOSUnregisterAllTransportMessages();
    SOSUnregisterAllTransportCircles();
    SOSUnregisterAllTransportKeyParameters();
    CFArrayRemoveAllValues(key_transports);
    CFArrayRemoveAllValues(circle_transports);
    CFArrayRemoveAllValues(message_transports);
    
    
}

int secd_51_account_inflate(int argc, char *const *argv)
{
    plan_tests(kTestTestCount);
    
    tests();
    
    return 0;
}