1/*
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26#include <Security/SecBase.h>
27#include <Security/SecItem.h>
28#include <Security/SecKey.h>
29#include <Security/SecKeyPriv.h>
30
31#include <SecureObjectSync/SOSCircle.h>
32#include <SecureObjectSync/SOSPeerInfo.h>
33#include <SecureObjectSync/SOSInternal.h>
34#include <SecureObjectSync/SOSUserKeygen.h>
35
36#include <utilities/SecCFWrappers.h>
37
38#include <CoreFoundation/CoreFoundation.h>
39
40#include <stdlib.h>
41#include <unistd.h>
42
43#include "SOSCircle_regressions.h"
44
45#include "SOSRegressionUtilities.h"
46
47#if TARGET_OS_IPHONE
48#include <MobileGestalt.h>
49#endif
50
51static int kTestTestCount = 11;
52static void tests(void)
53{
54    SecKeyRef signingKey = NULL;
55    SOSFullPeerInfoRef fpi = SOSCreateFullPeerInfoFromName(CFSTR("Test Peer"), &signingKey, NULL);
56    SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
57    CFRetainSafe(pi);
58
59    ok(NULL != pi, "info creation");
60
61    uint8_t buffer[4096];
62
63    const uint8_t *buffer_p = SOSPeerInfoEncodeToDER(pi, NULL, buffer, buffer + sizeof(buffer));
64
65    ok(buffer_p != NULL, "encode");
66
67    SOSPeerInfoRef pi2 = SOSPeerInfoCreateFromDER(NULL, NULL, &buffer_p, buffer + sizeof(buffer));
68
69    SKIP:
70    {
71        skip("Decode failed", 1, ok(NULL != pi2, "Decode"));
72        ok(CFEqual(pi, pi2), "Decode matches");
73    }
74
75    buffer_p = SOSFullPeerInfoEncodeToDER(fpi, NULL, buffer, buffer + sizeof(buffer));
76
77    ok(buffer_p != NULL, "Full peer encode");
78
79    SOSFullPeerInfoRef  fpi2 = SOSFullPeerInfoCreateFromDER(kCFAllocatorDefault, NULL, &buffer_p, buffer + sizeof(buffer));
80
81    SKIP:
82    {
83        skip("Full Peer Decode failed", 1, ok(fpi2, "Full peer inflated"));
84
85        ok(CFEqual(fpi, fpi2), "Full peer inflate matches");
86    }
87
88
89// Application ticket time.
90    CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
91    CFErrorRef error = NULL;
92
93    CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
94    ok(parameters, "No parameters!");
95    ok(error == NULL, "Error: (%@)", error);
96    CFReleaseNull(error);
97
98    SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
99    CFReleaseSafe(cfpassword);
100    CFReleaseNull(parameters);
101    SecKeyRef user_pubkey = SecKeyCreatePublicFromPrivate(user_privkey);
102
103    ok(SOSFullPeerInfoPromoteToApplication(fpi, user_privkey, &error), "Promote to Application");
104    ok(SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(fpi), user_pubkey, &error), "Promote to Application");
105
106
107    CFReleaseNull(user_privkey);
108    CFReleaseNull(user_pubkey);
109
110    CFReleaseNull(signingKey);
111    CFReleaseNull(pi);
112    CFReleaseNull(pi2);
113    CFReleaseNull(fpi);
114    CFReleaseNull(fpi2);
115}
116
117int sc_30_peerinfo(int argc, char *const *argv)
118{
119    plan_tests(kTestTestCount);
120
121    tests();
122
123	return 0;
124}
125