2 // secd-67-prefixedKeyIDs.c
5 // Created by Richard Murphy on 11/1/16.
11 // secd-66-account-recovery.c
14 // Created by Richard Murphy on 10/5/16.
21 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
23 * @APPLE_LICENSE_HEADER_START@
25 * This file contains Original Code and/or Modifications of Original Code
26 * as defined in and that are subject to the Apple Public Source License
27 * Version 2.0 (the 'License'). You may not use this file except in
28 * compliance with the License. Please obtain a copy of the License at
29 * http://www.opensource.apple.com/apsl/ and read it before using this
32 * The Original Code and all software distributed under the License are
33 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
34 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
35 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
37 * Please see the License for the specific language governing rights and
38 * limitations under the License.
40 * @APPLE_LICENSE_HEADER_END@
46 #include <Security/SecBase.h>
47 #include <CoreFoundation/CFDictionary.h>
48 #include "SOSKeyedPubKeyIdentifier.h"
53 #include "secd_regressions.h"
54 #include "SOSTestDataSource.h"
56 #include "SOSRegressionUtilities.h"
58 #include <utilities/SecCFWrappers.h>
59 #include <Security/SecKeyPriv.h>
63 #include "SOSAccountTesting.h"
67 CFErrorRef error = NULL;
68 int keySizeInBits = 256;
69 CFNumberRef kzib = CFNumberCreate(NULL, kCFNumberIntType, &keySizeInBits);
71 CFDictionaryRef keyattributes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
72 kSecAttrKeyType, kSecAttrKeyTypeEC,
73 kSecAttrKeySizeInBits, kzib,
78 SecKeyRef key = SecKeyCreateRandomKey(keyattributes, &error);
79 CFReleaseNull(keyattributes);
80 SecKeyRef pubKey = SecKeyCreatePublicFromPrivate(key);
81 CFDataRef pubKeyData = NULL;
82 SecKeyCopyPublicBytes(pubKey, &pubKeyData);
83 CFStringRef properPref = CFSTR("RK");
84 CFStringRef shortPref = CFSTR("R");
85 CFStringRef longPref = CFSTR("RKR");
87 ok(key, "Made private key");
88 ok(pubKey, "Made public key");
89 ok(pubKeyData, "Made public key data");
91 CFStringRef pkidseckey = SOSKeyedPubKeyIdentifierCreateWithSecKey(properPref, pubKey);
92 ok(pkidseckey, "made string");
93 CFStringRef pkidseckeyshort = SOSKeyedPubKeyIdentifierCreateWithSecKey(shortPref, pubKey);
94 ok(!pkidseckeyshort, "didn't make string");
95 CFStringRef pkidseckeylong = SOSKeyedPubKeyIdentifierCreateWithSecKey(longPref, pubKey);
96 ok(!pkidseckeylong, "didn't make string");
98 ok(SOSKeyedPubKeyIdentifierIsPrefixed(pkidseckey), "properly prefixed string was made");
99 CFStringRef retPref = SOSKeyedPubKeyIdentifierCopyPrefix(pkidseckey);
100 ok(retPref, "got prefix");
101 ok(CFEqualSafe(retPref, properPref), "prefix matches");
102 CFReleaseNull(retPref);
103 CFStringRef retHpub = SOSKeyedPubKeyIdentifierCopyHpub(pkidseckey);
104 ok(retHpub, "got hash of pubkey");
107 CFStringRef pkiddata = SOSKeyedPubKeyIdentifierCreateWithData(properPref, pubKeyData);
108 ok(pkiddata, "made string");
109 ok(CFEqualSafe(pkiddata, pkidseckey), "strings match");
111 //diag("pkiddata %@", pkiddata);
112 //diag("retPref %@", retPref);
113 CFReleaseNull(retHpub);
115 CFReleaseNull(pubKey);
116 CFReleaseNull(pubKeyData);
117 CFReleaseNull(pkidseckey);
118 CFReleaseNull(pkidseckeyshort);
119 CFReleaseNull(pkidseckeylong);
120 CFReleaseNull(pkiddata);
126 int secd_67_prefixedKeyIDs(int argc, char *const *argv) {