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 SecKeyRef octagonSigningKey
= NULL
;
96 SecKeyRef octagonEncryptionKey
= NULL
;
97 SOSFullPeerInfoRef fpi
= SOSCreateFullPeerInfoFromName(CFSTR("Test Peer"), &signingKey
, &octagonSigningKey
, &octagonEncryptionKey
, NULL
);
98 SOSPeerInfoRef pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
100 ok(NULL
!= pi
, "info creation");
102 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips");
103 ok(FullPeerInfoRoundTrip(fpi
), "FullPeerInfo safely round-trips");
105 // Application ticket time.
106 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
107 CFErrorRef error
= NULL
;
109 CFDataRef parameters
= SOSUserKeyCreateGenerateParameters(&error
);
110 ok(parameters
, "No parameters!");
111 ok(error
== NULL
, "Error: (%@)", error
);
112 CFReleaseNull(error
);
114 SecKeyRef user_privkey
= SOSUserKeygen(cfpassword
, parameters
, &error
);
115 CFReleaseSafe(cfpassword
);
116 CFReleaseNull(parameters
);
117 SecKeyRef user_pubkey
= SecKeyCreatePublicFromPrivate(user_privkey
);
119 ok(SOSFullPeerInfoPromoteToApplication(fpi
, user_privkey
, &error
), "Promote to Application");
120 ok(SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(fpi
), user_pubkey
, &error
), "Promote to Application");
122 pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
123 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips");
125 CFDataRef testBackupKey
= CopyTestBackupKey();
127 ok(SOSFullPeerInfoUpdateBackupKey(fpi
, testBackupKey
, &error
), "Set Backup (%@)", error
);
128 CFReleaseNull(error
);
130 CFReleaseNull(testBackupKey
); // Make sure our ref doesn't save them.
131 testBackupKey
= CopyTestBackupKey();
133 pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
134 CFDataRef piBackupKey
= SOSPeerInfoCopyBackupKey(pi
);
136 ok(CFEqualSafe(testBackupKey
, piBackupKey
), "Same Backup Key");
138 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips with backup key");
140 CFReleaseNull(piBackupKey
);
141 piBackupKey
= SOSPeerInfoCopyBackupKey(pi
);
142 ok(CFEqualSafe(testBackupKey
, piBackupKey
), "Same Backup Key after round trip");
144 // Don't own the piBackupKey key
145 CFReleaseNull(testBackupKey
);
146 CFReleaseNull(piBackupKey
);
147 CFReleaseNull(user_privkey
);
148 CFReleaseNull(user_pubkey
);
150 CFReleaseNull(signingKey
);
151 CFReleaseNull(octagonSigningKey
);
152 CFReleaseNull(octagonEncryptionKey
);
156 int sc_30_peerinfo(int argc
, char *const *argv
)
158 plan_tests(kTestTestCount
);