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 "keychain/SecureObjectSync/SOSPeerInfoDER.h"
32 #include "keychain/SecureObjectSync/SOSCircle.h"
33 #include <Security/SecureObjectSync/SOSPeerInfo.h>
34 #include "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
35 #include "keychain/SecureObjectSync/SOSInternal.h"
36 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
38 #include <utilities/SecCFWrappers.h>
40 #include <CoreFoundation/CoreFoundation.h>
45 #include "SOSCircle_regressions.h"
47 #include "SOSRegressionUtilities.h"
52 #include <MobileGestalt.h>
55 static CFDataRef
CopyTestBackupKey(void) {
56 static uint8_t data
[] = { 'A', 'b', 'c' };
58 return CFDataCreate(kCFAllocatorDefault
, data
, sizeof(data
));
61 static bool PeerInfoRoundTrip(SOSPeerInfoRef pi
) {
63 size_t size
= SOSPeerInfoGetDEREncodedSize(pi
, NULL
);
65 const uint8_t *buffer_p
= SOSPeerInfoEncodeToDER(pi
, NULL
, buffer
, buffer
+ sizeof(buffer
));
66 ok(buffer_p
!= NULL
, "encode");
67 if(buffer_p
== NULL
) return false;
68 SOSPeerInfoRef pi2
= SOSPeerInfoCreateFromDER(NULL
, NULL
, &buffer_p
, buffer
+ sizeof(buffer
));
69 ok(pi2
!= NULL
, "decode");
70 if(!pi2
) return false;
71 ok(CFEqual(pi
, pi2
), "Decode matches");
72 if(CFEqual(pi
, pi2
)) retval
= true;
77 static bool FullPeerInfoRoundTrip(SOSFullPeerInfoRef fpi
) {
79 size_t size
= SOSFullPeerInfoGetDEREncodedSize(fpi
, NULL
);
81 const uint8_t *buffer_p
= SOSFullPeerInfoEncodeToDER(fpi
, NULL
, buffer
, buffer
+ sizeof(buffer
));
82 ok(buffer_p
!= NULL
, "encode");
83 if(buffer_p
== NULL
) return false;
84 SOSFullPeerInfoRef fpi2
= SOSFullPeerInfoCreateFromDER(NULL
, NULL
, &buffer_p
, buffer
+ sizeof(buffer
));
85 ok(fpi2
!= NULL
, "decode");
86 if(!fpi2
) return false;
87 ok(CFEqual(fpi
, fpi2
), "Decode matches");
88 if(CFEqual(fpi
, fpi2
)) retval
= true;
93 static int kTestTestCount
= 24;
94 static void tests(void)
96 SecKeyRef signingKey
= NULL
;
97 SecKeyRef octagonSigningKey
= NULL
;
98 SecKeyRef octagonEncryptionKey
= NULL
;
99 SOSFullPeerInfoRef fpi
= SOSCreateFullPeerInfoFromName(CFSTR("Test Peer"), &signingKey
, &octagonSigningKey
, &octagonEncryptionKey
, NULL
);
100 SOSPeerInfoRef pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
102 ok(NULL
!= pi
, "info creation");
104 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips");
105 ok(FullPeerInfoRoundTrip(fpi
), "FullPeerInfo safely round-trips");
107 // Application ticket time.
108 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
109 CFErrorRef error
= NULL
;
111 CFDataRef parameters
= SOSUserKeyCreateGenerateParameters(&error
);
112 ok(parameters
, "No parameters!");
113 ok(error
== NULL
, "Error: (%@)", error
);
114 CFReleaseNull(error
);
116 SecKeyRef user_privkey
= SOSUserKeygen(cfpassword
, parameters
, &error
);
117 CFReleaseSafe(cfpassword
);
118 CFReleaseNull(parameters
);
119 SecKeyRef user_pubkey
= SecKeyCreatePublicFromPrivate(user_privkey
);
121 ok(SOSFullPeerInfoPromoteToApplication(fpi
, user_privkey
, &error
), "Promote to Application");
122 ok(SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(fpi
), user_pubkey
, &error
), "Promote to Application");
124 pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
125 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips");
127 CFDataRef testBackupKey
= CopyTestBackupKey();
129 ok(SOSFullPeerInfoUpdateBackupKey(fpi
, testBackupKey
, &error
), "Set Backup (%@)", error
);
130 CFReleaseNull(error
);
132 CFReleaseNull(testBackupKey
); // Make sure our ref doesn't save them.
133 testBackupKey
= CopyTestBackupKey();
135 pi
= SOSFullPeerInfoGetPeerInfo(fpi
);
136 CFDataRef piBackupKey
= SOSPeerInfoCopyBackupKey(pi
);
138 ok(CFEqualSafe(testBackupKey
, piBackupKey
), "Same Backup Key");
140 ok(PeerInfoRoundTrip(pi
), "PeerInfo safely round-trips with backup key");
142 CFReleaseNull(piBackupKey
);
143 piBackupKey
= SOSPeerInfoCopyBackupKey(pi
);
144 ok(CFEqualSafe(testBackupKey
, piBackupKey
), "Same Backup Key after round trip");
146 // Don't own the piBackupKey key
147 CFReleaseNull(testBackupKey
);
148 CFReleaseNull(piBackupKey
);
149 CFReleaseNull(user_privkey
);
150 CFReleaseNull(user_pubkey
);
152 CFReleaseNull(signingKey
);
153 CFReleaseNull(octagonSigningKey
);
154 CFReleaseNull(octagonEncryptionKey
);
159 int sc_30_peerinfo(int argc
, char *const *argv
)
162 plan_tests(kTestTestCount
);