]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-67-prefixedKeyIDs.m
Security-59754.80.3.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-67-prefixedKeyIDs.m
1 //
2 // secd-67-prefixedKeyIDs.c
3 // Security
4 //
5 // Created by Richard Murphy on 11/1/16.
6 //
7 //
8
9 #include <stdio.h>
10 //
11 // secd-66-account-recovery.c
12 // Security
13 //
14 // Created by Richard Murphy on 10/5/16.
15 //
16 //
17
18 #include <stdio.h>
19
20 /*
21 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
22 *
23 * @APPLE_LICENSE_HEADER_START@
24 *
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
30 * file.
31 *
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.
39 *
40 * @APPLE_LICENSE_HEADER_END@
41 */
42
43
44
45
46 #include <Security/SecBase.h>
47 #include <CoreFoundation/CFDictionary.h>
48 #include "SOSKeyedPubKeyIdentifier.h"
49
50 #include <stdlib.h>
51 #include <unistd.h>
52
53 #include "secd_regressions.h"
54 #include "SOSTestDataSource.h"
55
56 #include "SOSRegressionUtilities.h"
57
58 #include <utilities/SecCFWrappers.h>
59 #include <Security/SecKeyPriv.h>
60
61
62
63 #include "SOSAccountTesting.h"
64 #if SOS_ENABLED
65
66 static void tests() {
67 CFErrorRef error = NULL;
68 int keySizeInBits = 256;
69 CFNumberRef kzib = CFNumberCreate(NULL, kCFNumberIntType, &keySizeInBits);
70
71 CFDictionaryRef keyattributes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
72 kSecAttrKeyType, kSecAttrKeyTypeEC,
73 kSecAttrKeySizeInBits, kzib,
74 NULL);
75 CFReleaseNull(kzib);
76
77
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");
86
87 ok(key, "Made private key");
88 ok(pubKey, "Made public key");
89 ok(pubKeyData, "Made public key data");
90
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");
97
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");
105
106
107 CFStringRef pkiddata = SOSKeyedPubKeyIdentifierCreateWithData(properPref, pubKeyData);
108 ok(pkiddata, "made string");
109 ok(CFEqualSafe(pkiddata, pkidseckey), "strings match");
110
111 //diag("pkiddata %@", pkiddata);
112 //diag("retPref %@", retPref);
113 CFReleaseNull(retHpub);
114 CFReleaseNull(key);
115 CFReleaseNull(pubKey);
116 CFReleaseNull(pubKeyData);
117 CFReleaseNull(pkidseckey);
118 CFReleaseNull(pkidseckeyshort);
119 CFReleaseNull(pkidseckeylong);
120 CFReleaseNull(pkiddata);
121
122
123 }
124 #endif
125
126 int secd_67_prefixedKeyIDs(int argc, char *const *argv) {
127 #if SOS_ENABLED
128 plan_tests(12);
129 tests();
130 #else
131 plan_tests(0);
132 #endif
133 return 0;
134 }
135