]>
git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-30-peerinfo.c
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecKey.h>
29 #include <Security/SecKeyPriv.h>
30 #include <SOSPeerInfoDER.h>
32 #include <Security/SecureObjectSync/SOSCircle.h>
33 #include <Security/SecureObjectSync/SOSPeerInfo.h>
34 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
35 #include <Security/SecureObjectSync/SOSInternal.h>
36 #include <Security/SecureObjectSync/SOSUserKeygen.h>
38 #include <utilities/SecCFWrappers.h>
40 #include <CoreFoundation/CoreFoundation.h>
45 #include "SOSCircle_regressions.h"
47 #include "SOSRegressionUtilities.h"
50 #include <MobileGestalt.h>
53 static CFDataRef
CopyTestBackupKey(void) {
54 static uint8_t data
[] = { 'A', 'b', 'c' };
56 return CFDataCreate(kCFAllocatorDefault
, data
, sizeof(data
));
59 static bool PeerInfoRoundTrip(SOSPeerInfoRef pi
) {
61 size_t size
= SOSPeerInfoGetDEREncodedSize(pi
, NULL
);
63 const uint8_t *buffer_p
= SOSPeerInfoEncodeToDER(pi
, NULL
, buffer
, buffer
+ sizeof(buffer
));
64 ok(buffer_p
!= NULL
, "encode");
65 if(buffer_p
== NULL
) return false;
66 SOSPeerInfoRef pi2
= SOSPeerInfoCreateFromDER(NULL
, NULL
, &buffer_p
, buffer
+ sizeof(buffer
));
67 ok(pi2
!= NULL
, "decode");
68 if(!pi2
) return false;
69 ok(CFEqual(pi
, pi2
), "Decode matches");
70 if(CFEqual(pi
, pi2
)) retval
= true;
75 static bool FullPeerInfoRoundTrip(SOSFullPeerInfoRef fpi
) {
77 size_t size
= SOSFullPeerInfoGetDEREncodedSize(fpi
, NULL
);
79 const uint8_t *buffer_p
= SOSFullPeerInfoEncodeToDER(fpi
, NULL
, buffer
, buffer
+ sizeof(buffer
));
80 ok(buffer_p
!= NULL
, "encode");
81 if(buffer_p
== NULL
) return false;
82 SOSFullPeerInfoRef fpi2
= SOSFullPeerInfoCreateFromDER(NULL
, NULL
, &buffer_p
, buffer
+ sizeof(buffer
));
83 ok(fpi2
!= NULL
, "decode");
84 if(!fpi2
) return false;
85 ok(CFEqual(fpi
, fpi2
), "Decode matches");
86 if(CFEqual(fpi
, fpi2
)) retval
= true;
91 static int kTestTestCount
= 24;
92 static void tests(void)
94 SecKeyRef signingKey
= NULL
;
95 SOSFullPeerInfoRef fpi
= SOSCreateFullPeerInfoFromName(CFSTR("Test Peer"), &signingKey
, NULL
);
96 SOSPeerInfoRef pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
98 ok(NULL
!= pi
, "info creation");
100 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips");
101 ok(FullPeerInfoRoundTrip(fpi
), "FullPeerInfo safely round-trips");
103 // Application ticket time.
104 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
105 CFErrorRef error
= NULL
;
107 CFDataRef parameters
= SOSUserKeyCreateGenerateParameters(&error
);
108 ok(parameters
, "No parameters!");
109 ok(error
== NULL
, "Error: (%@)", error
);
110 CFReleaseNull(error
);
112 SecKeyRef user_privkey
= SOSUserKeygen(cfpassword
, parameters
, &error
);
113 CFReleaseSafe(cfpassword
);
114 CFReleaseNull(parameters
);
115 SecKeyRef user_pubkey
= SecKeyCreatePublicFromPrivate(user_privkey
);
117 ok(SOSFullPeerInfoPromoteToApplication(fpi
, user_privkey
, &error
), "Promote to Application");
118 ok(SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(fpi
), user_pubkey
, &error
), "Promote to Application");
120 pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
121 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips");
123 CFDataRef testBackupKey
= CopyTestBackupKey();
125 ok(SOSFullPeerInfoUpdateBackupKey(fpi
, testBackupKey
, &error
), "Set Backup (%@)", error
);
126 CFReleaseNull(error
);
128 CFReleaseNull(testBackupKey
); // Make sure our ref doesn't save them.
129 testBackupKey
= CopyTestBackupKey();
131 pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
132 CFDataRef piBackupKey
= SOSPeerInfoCopyBackupKey(pi
);
134 ok(CFEqualSafe(testBackupKey
, piBackupKey
), "Same Backup Key");
136 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips with backup key");
138 CFReleaseNull(piBackupKey
);
139 piBackupKey
= SOSPeerInfoCopyBackupKey(pi
);
140 ok(CFEqualSafe(testBackupKey
, piBackupKey
), "Same Backup Key after round trip");
142 // Don't own the piBackupKey key
143 CFReleaseNull(testBackupKey
);
144 CFReleaseNull(piBackupKey
);
145 CFReleaseNull(user_privkey
);
146 CFReleaseNull(user_pubkey
);
148 CFReleaseNull(signingKey
);
152 int sc_30_peerinfo(int argc
, char *const *argv
)
154 plan_tests(kTestTestCount
);