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"
65 CFErrorRef error = NULL;
66 int keySizeInBits = 256;
67 CFNumberRef kzib = CFNumberCreate(NULL, kCFNumberIntType, &keySizeInBits);
69 CFDictionaryRef keyattributes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
70 kSecAttrKeyType, kSecAttrKeyTypeEC,
71 kSecAttrKeySizeInBits, kzib,
76 SecKeyRef key = SecKeyCreateRandomKey(keyattributes, &error);
77 CFReleaseNull(keyattributes);
78 SecKeyRef pubKey = SecKeyCreatePublicFromPrivate(key);
79 CFDataRef pubKeyData = NULL;
80 SecKeyCopyPublicBytes(pubKey, &pubKeyData);
81 CFStringRef properPref = CFSTR("RK");
82 CFStringRef shortPref = CFSTR("R");
83 CFStringRef longPref = CFSTR("RKR");
85 ok(key, "Made private key");
86 ok(pubKey, "Made public key");
87 ok(pubKeyData, "Made public key data");
89 CFStringRef pkidseckey = SOSKeyedPubKeyIdentifierCreateWithSecKey(properPref, pubKey);
90 ok(pkidseckey, "made string");
91 CFStringRef pkidseckeyshort = SOSKeyedPubKeyIdentifierCreateWithSecKey(shortPref, pubKey);
92 ok(!pkidseckeyshort, "didn't make string");
93 CFStringRef pkidseckeylong = SOSKeyedPubKeyIdentifierCreateWithSecKey(longPref, pubKey);
94 ok(!pkidseckeylong, "didn't make string");
96 ok(SOSKeyedPubKeyIdentifierIsPrefixed(pkidseckey), "properly prefixed string was made");
97 CFStringRef retPref = SOSKeyedPubKeyIdentifierCopyPrefix(pkidseckey);
98 ok(retPref, "got prefix");
99 ok(CFEqualSafe(retPref, properPref), "prefix matches");
100 CFReleaseNull(retPref);
101 CFStringRef retHpub = SOSKeyedPubKeyIdentifierCopyHpub(pkidseckey);
102 ok(retHpub, "got hash of pubkey");
105 CFStringRef pkiddata = SOSKeyedPubKeyIdentifierCreateWithData(properPref, pubKeyData);
106 ok(pkiddata, "made string");
107 ok(CFEqualSafe(pkiddata, pkidseckey), "strings match");
109 //diag("pkiddata %@", pkiddata);
110 //diag("retPref %@", retPref);
111 CFReleaseNull(retHpub);
113 CFReleaseNull(pubKey);
114 CFReleaseNull(pubKeyData);
115 CFReleaseNull(pkidseckey);
116 CFReleaseNull(pkidseckeyshort);
117 CFReleaseNull(pkidseckeylong);
118 CFReleaseNull(pkiddata);
123 int secd_67_prefixedKeyIDs(int argc, char *const *argv) {